1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Flash memory module for Chrome EC */
#include "console.h"
#include "flash.h"
#include "flash_commands.h"
#include "lpc_commands.h"
#include "registers.h" /* TODO: remove; only for temp debugging */
#include "shared_mem.h"
#include "uart.h"
#include "util.h"
/*****************************************************************************/
/* Console commands */
static int command_flash_info(int argc, char **argv)
{
uart_printf("Usable flash size: %d B\n", flash_get_size());
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(flashinfo, command_flash_info);
static int command_flash_erase(int argc, char **argv)
{
int offset = 0;
int size = FLASH_ERASE_BYTES;
char *endptr;
int rv;
if (argc < 2) {
uart_puts("Usage: flasherase <offset> [size]\n");
return EC_ERROR_UNKNOWN;
}
offset = strtoi(argv[1], &endptr, 0);
if (*endptr) {
uart_puts("Invalid offset\n");
return EC_ERROR_UNKNOWN;
}
if (argc > 2) {
size = strtoi(argv[2], &endptr, 0);
if (*endptr) {
uart_puts("Invalid size\n");
return EC_ERROR_UNKNOWN;
}
}
uart_printf("Erasing %d bytes at offset 0x%x (%d)...\n",
size, offset, offset);
rv = flash_erase(offset, size);
if (rv == EC_SUCCESS)
uart_puts("done.\n");
else
uart_printf("failed. (error %d)\n", rv);
return rv;
}
DECLARE_CONSOLE_COMMAND(flasherase, command_flash_erase);
static int command_flash_write(int argc, char **argv)
{
char *data;
int offset = 0;
int size = 1024; /* Default size */
char *endptr;
int rv;
int i;
if (argc < 2) {
uart_puts("Usage: flashwrite <offset> <size>\n");
return EC_ERROR_UNKNOWN;
}
offset = strtoi(argv[1], &endptr, 0);
if (*endptr) {
uart_puts("Invalid offset\n");
return EC_ERROR_UNKNOWN;
}
if (argc > 2) {
size = strtoi(argv[2], &endptr, 0);
if (*endptr) {
uart_puts("Invalid size\n");
return EC_ERROR_UNKNOWN;
}
if (size > shared_mem_size()) {
uart_puts("Truncating size\n");
size = sizeof(data);
}
}
/* Acquire the shared memory buffer */
rv = shared_mem_acquire(size, 0, &data);
if (rv) {
uart_printf("Unable to acquire %d byte buffer\n", size);
return rv;
}
/* Fill the data buffer with a pattern */
for (i = 0; i < size; i++)
data[i] = i;
uart_printf("Writing %d bytes to offset 0x%x (%d)...\n",
size, offset, offset);
rv = flash_write(offset, size, data);
if (rv == EC_SUCCESS)
uart_puts("done.\n");
else
uart_printf("failed. (error %d)\n", rv);
/* Free the buffer */
shared_mem_release(data);
return rv;
}
DECLARE_CONSOLE_COMMAND(flashwrite, command_flash_write);
static int command_flash_wp(int argc, char **argv)
{
int b = 0;
char *endptr;
if (argc < 2) {
uart_puts("Usage: flashwp [bitmask]\n");
uart_printf("(current value of FMPPE1: 0x%08x)\n",
LM4_FLASH_FMPPE1);
return EC_SUCCESS;
}
b = strtoi(argv[1], &endptr, 0);
if (*endptr) {
uart_puts("Invalid bitmask\n");
return EC_ERROR_UNKNOWN;
}
uart_printf("FMPPE1 before: 0x%08x\n", LM4_FLASH_FMPPE1);
LM4_FLASH_FMPPE1 = b;
uart_printf("FMPPE1 after: 0x%08x\n", LM4_FLASH_FMPPE1);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(flashwp, command_flash_wp);
static int command_flash_wp_range(int argc, char **argv)
{
int offset, size;
char *endptr;
int rv;
if (argc < 3) {
uart_puts("Usage: flashwprange [offset size]\n");
rv = flash_get_write_protect_range(&offset, &size);
if (rv)
uart_puts("flash_get_write_protect_range failed\n");
else
uart_printf("Current range : offset(%d) size(%d)\n",
offset, size);
uart_printf("FMPPEs : %08x %08x %08x %08x\n",
LM4_FLASH_FMPPE0, LM4_FLASH_FMPPE1,
LM4_FLASH_FMPPE2, LM4_FLASH_FMPPE3);
} else {
offset = strtoi(argv[1], &endptr, 0);
if (*endptr) {
uart_printf("Invalid offset \"%s\"\n", argv[1]);
return EC_ERROR_UNKNOWN;
}
size = strtoi(argv[2], &endptr, 0);
if (*endptr) {
uart_printf("Invalid size \"%s\"\n", argv[2]);
return EC_ERROR_UNKNOWN;
}
rv = flash_set_write_protect_range(offset, size);
if (rv) {
uart_puts("flash_set_write_protect_range failed\n");
return rv;
}
}
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(flashwprange, command_flash_wp_range);
/*****************************************************************************/
/* Host commands */
enum lpc_status flash_command_get_info(uint8_t *data)
{
struct lpc_response_flash_info *r =
(struct lpc_response_flash_info *)data;
r->flash_size = flash_get_size();
r->write_block_size = FLASH_WRITE_BYTES;
r->erase_block_size = FLASH_ERASE_BYTES;
r->protect_block_size = FLASH_PROTECT_BYTES;
return EC_LPC_STATUS_SUCCESS;
}
#ifdef SUPPORT_CHECKSUM
enum lpc_status flash_command_checksum(uint8_t *data)
{
struct lpc_params_flash_checksum *p =
(struct lpc_params_flash_checksum *)data;
struct lpc_response_flash_checksum *r =
(struct lpc_response_flash_checksum *)data;
uint8_t cs, byte;
int j;
for (cs = 0, j = 0; j < p->size; ++j) {
if (flash_read(p->offset + j, 1, &byte)) {
uart_printf("flash_read() error at 0x%02x.\n",
p->offset + j);
return EC_LPC_STATUS_ERROR;
}
BYTE_IN(cs, byte);
}
r->checksum = cs;
return EC_LPC_STATUS_SUCCESS;
}
#endif
enum lpc_status flash_command_read(uint8_t *data)
{
struct lpc_params_flash_read *p =
(struct lpc_params_flash_read *)data;
struct lpc_response_flash_read *r =
(struct lpc_response_flash_read *)data;
if (p->size > sizeof(r->data))
return EC_LPC_STATUS_ERROR;
if (flash_read(p->offset, p->size, r->data))
return EC_LPC_STATUS_ERROR;
return EC_LPC_STATUS_SUCCESS;
}
enum lpc_status flash_command_write(uint8_t *data)
{
struct lpc_params_flash_write *p =
(struct lpc_params_flash_write *)data;
if (p->size > sizeof(p->data))
return EC_LPC_STATUS_ERROR;
if (flash_write(p->offset, p->size, p->data))
return EC_LPC_STATUS_ERROR;
return EC_LPC_STATUS_SUCCESS;
}
enum lpc_status flash_command_erase(uint8_t *data)
{
struct lpc_params_flash_erase *p =
(struct lpc_params_flash_erase *)data;
if (flash_erase(p->offset, p->size))
return EC_LPC_STATUS_ERROR;
return EC_LPC_STATUS_SUCCESS;
}
/* TODO: use shadow range in EEPROM */
static int shadow_wp_offset;
static int shadow_wp_size;
enum lpc_status flash_command_wp_enable(uint8_t *data)
{
struct lpc_params_flash_wp_enable *p =
(struct lpc_params_flash_wp_enable *)data;
int offset, size;
if (p->enable_wp) {
offset = shadow_wp_offset;
size = shadow_wp_size;
} else {
offset = 0;
size = 0;
}
if (flash_set_write_protect_range(offset, size))
return EC_LPC_STATUS_ERROR;
return EC_LPC_STATUS_SUCCESS;
}
enum lpc_status flash_command_wp_get_state(uint8_t *data)
{
struct lpc_response_flash_wp_enable *p =
(struct lpc_response_flash_wp_enable *)data;
if (flash_get_write_protect_status() & EC_FLASH_WP_RANGE_LOCKED)
p->enable_wp = 1;
else
p->enable_wp = 0;
return EC_LPC_STATUS_SUCCESS;
}
enum lpc_status flash_command_wp_set_range(uint8_t *data)
{
struct lpc_params_flash_wp_range *p =
(struct lpc_params_flash_wp_range *)data;
if (flash_set_write_protect_range(p->offset, p->size))
return EC_LPC_STATUS_ERROR;
return EC_LPC_STATUS_SUCCESS;
}
enum lpc_status flash_command_wp_get_range(uint8_t *data)
{
struct lpc_response_flash_wp_range *p =
(struct lpc_response_flash_wp_range *)data;
if (flash_get_write_protect_range(&p->offset, &p->size))
return EC_LPC_STATUS_ERROR;
return EC_LPC_STATUS_SUCCESS;
}
|