summaryrefslogtreecommitdiff
path: root/common/flash_commands.c
blob: 1e08f26769d06a45da4a5bb922b96fcf7aba2684 (plain)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Copyright (c) 2012 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 commands for Chrome EC */

#include "console.h"
#include "flash.h"
#include "host_command.h"
#include "registers.h"  /* TODO: remove; only for temp debugging */
#include "shared_mem.h"
#include "system.h"
#include "uart.h"
#include "util.h"

/* Parse offset and size from command line argv[0] and argv[1].
 *
 * Default values: If argc<1, leaves offset unchanged, returning error if
 * *offset<0.  If argc<2, leaves size unchanged, returning error if *size<0. */
static int parse_offset_size(int argc, char **argv, int *offset, int *size)
{
	char *e;
	int i;

	if (argc >= 1) {
		i = (uint32_t)strtoi(argv[0], &e, 0);
		if (e && *e) {
			uart_printf("Invalid offset \"%s\"\n", argv[0]);
			return EC_ERROR_INVAL;
		}
		*offset = i;
	} else if (*offset < 0) {
		uart_puts("Must specify offset.\n");
		return EC_ERROR_INVAL;
	}

	if (argc >= 2) {
		i = (uint32_t)strtoi(argv[1], &e, 1);
		if (e && *e) {
			uart_printf("Invalid size \"%s\"\n", argv[1]);
			return EC_ERROR_INVAL;
		}
		*size = i;
	} else if (*size < 0) {
		uart_puts("Must specify offset and size.\n");
		return EC_ERROR_INVAL;
	}

	return EC_SUCCESS;
}


/*****************************************************************************/
/* Console commands */

static int command_flash_info(int argc, char **argv)
{
	const uint8_t *wp;
	int banks = flash_get_size() / flash_get_protect_block_size();
	int i;

	uart_printf("Physical size: %4d KB\n", flash_physical_size() / 1024);
	uart_printf("Usable size:   %4d KB\n", flash_get_size() / 1024);
	uart_printf("Write block:   %4d B\n", flash_get_write_block_size());
	uart_printf("Erase block:   %4d B\n", flash_get_erase_block_size());
	uart_printf("Protect block: %4d B\n", flash_get_protect_block_size());

	i = flash_get_protect_lock();
	uart_printf("Protect lock:  %s%s\n",
		    (i & FLASH_PROTECT_LOCK_SET) ? "LOCKED" : "unlocked",
		    (i & FLASH_PROTECT_LOCK_APPLIED) ? " AND APPLIED" : "");
	uart_printf("WP pin:        %s\n", (i & FLASH_PROTECT_PIN_ASSERTED) ?
		    "ASSERTED" : "deasserted");

	wp = flash_get_protect_array();

	uart_puts("Protected now:");
	for (i = 0; i < banks; i++) {
		if (!(i & 7))
			uart_puts(" ");
		uart_puts(wp[i] & FLASH_PROTECT_UNTIL_REBOOT ? "Y" : ".");
	}
	uart_puts("\n  Persistent: ");
	for (i = 0; i < banks; i++) {
		if (!(i & 7))
			uart_puts(" ");
		uart_puts(wp[i] & FLASH_PROTECT_PERSISTENT ? "Y" : ".");
	}
	uart_puts("\n");

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(flashinfo, command_flash_info);


static int command_flash_erase(int argc, char **argv)
{
	int offset = -1;
	int size = flash_get_erase_block_size();
	int rv;

	rv = parse_offset_size(argc - 1, argv + 1, &offset, &size);
	if (rv)
		return rv;

	uart_printf("Erasing %d bytes at offset 0x%x (%d)...\n",
		    size, offset, offset);
	return flash_erase(offset, size);
}
DECLARE_CONSOLE_COMMAND(flasherase, command_flash_erase);


static int command_flash_write(int argc, char **argv)
{
	int offset = -1;
	int size = flash_get_erase_block_size();
	int rv;
	char *data;
	int i;


	rv = parse_offset_size(argc - 1, argv + 1, &offset, &size);
	if (rv)
		return rv;

	if (size > shared_mem_size()) {
		uart_puts("Truncating size\n");
		size = shared_mem_size();
	}

        /* 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 const char flash_wp_help[] =
	"Usage: flashwp <now | set | clear> <offset> [size]\n"
	"   or: flashwp <lock | unlock>\n";


static int command_flash_wp(int argc, char **argv)
{
	int offset = -1;
	int size = flash_get_protect_block_size();
	int rv;

	if (argc < 2) {
		uart_puts(flash_wp_help);
		return EC_ERROR_INVAL;
	}

	/* Commands that don't need offset and size */
	if (!strcasecmp(argv[1], "lock"))
		return flash_lock_protect(1);
	else if (!strcasecmp(argv[1], "unlock"))
		return flash_lock_protect(0);

	/* All remaining commands need offset and size */
	rv = parse_offset_size(argc - 2, argv + 2, &offset, &size);
	if (rv)
		return rv;

	if (!strcasecmp(argv[1], "now"))
		return flash_protect_until_reboot(offset, size);
	else if (!strcasecmp(argv[1], "set"))
		return flash_set_protect(offset, size, 1);
	else if (!strcasecmp(argv[1], "clear"))
		return flash_set_protect(offset, size, 0);
	else {
		uart_puts(flash_wp_help);
		return EC_ERROR_INVAL;
	}

}
DECLARE_CONSOLE_COMMAND(flashwp, command_flash_wp);

/*****************************************************************************/
/* 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_get_write_block_size();
	r->erase_block_size = flash_get_erase_block_size();
	r->protect_block_size = flash_get_protect_block_size();
	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_INFO, flash_command_get_info);


#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_RESULT_ERROR;
		}
		BYTE_IN(cs, byte);
	}

	r->checksum = cs;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_CHECKSUM, flash_command_checksum);
#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_RESULT_ERROR;

	if (flash_read(p->offset, p->size, r->data))
		return EC_LPC_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_READ, flash_command_read);


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_RESULT_ERROR;

	if (system_unsafe_to_overwrite(p->offset, p->size))
		return EC_LPC_RESULT_ACCESS_DENIED;

	if (flash_write(p->offset, p->size, p->data))
		return EC_LPC_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_WRITE, flash_command_write);


enum lpc_status flash_command_erase(uint8_t *data)
{
	struct lpc_params_flash_erase *p =
			(struct lpc_params_flash_erase *)data;

	if (system_unsafe_to_overwrite(p->offset, p->size))
		return EC_LPC_RESULT_ACCESS_DENIED;

	if (flash_erase(p->offset, p->size))
		return EC_LPC_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_ERASE, flash_command_erase);


#ifdef REWORK_THESE_TO_USE_NEW_WP_MECHANISM
/* TODO: (crosbug.com/p/8448) rework these to use the new WP mechanism.  Note
 * that the concept of a single range oversimplifies how WP works; there are
 * multiple protected regions, including the persistent WP state (last bank)
 * and rollback information (bank before that; will be implemented by
 * vboot). */

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_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_WP_ENABLE,
		     flash_command_wp_enable);


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_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_WP_GET_STATE,
		     flash_command_wp_get_state);


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_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_WP_SET_RANGE,
		     flash_command_wp_set_range);


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_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_FLASH_WP_GET_RANGE,
		     flash_command_wp_get_range);

#endif /* REWORK_THESE_TO_USE_NEW_WP_MECHANISM */