summaryrefslogtreecommitdiff
path: root/common/flash_commands.c
blob: cfae2124a3e17c689c5270ecaed11e72b3b41832 (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
/* 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 "util.h"

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

	if (argc > shift) {
		i = (uint32_t)strtoi(argv[shift], &e, 0);
		if (*e)
			return EC_ERROR_PARAM1;
		*offset = i;
	} else if (*offset < 0)
		return EC_ERROR_PARAM_COUNT;

	if (argc > shift + 1) {
		i = (uint32_t)strtoi(argv[shift + 1], &e, 0);
		if (*e)
			return EC_ERROR_PARAM2;
		*size = i;
	} else if (*size < 0)
		return EC_ERROR_PARAM_COUNT;

	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;

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

	i = flash_get_protect_lock();
	ccprintf("Lock:    %s%s\n",
		 (i & FLASH_PROTECT_LOCK_SET) ? "LOCKED" : "unlocked",
		 (i & FLASH_PROTECT_LOCK_APPLIED) ? ",APPLIED" : "");
	ccprintf("WP pin:  %sasserted\n",
		 (i & FLASH_PROTECT_PIN_ASSERTED) ? "" : "de");

	wp = flash_get_protect_array();

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

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(flashinfo, command_flash_info,
			NULL,
			"Print flash info",
			NULL);


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, argv, 1, &offset, &size);
	if (rv)
		return rv;

	ccprintf("Erasing %d bytes at 0x%x...\n", size, offset, offset);
	return flash_erase(offset, size);
}
DECLARE_CONSOLE_COMMAND(flasherase, command_flash_erase,
			"offset [size]",
			"Erase flash",
			NULL);


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, argv, 1, &offset, &size);
	if (rv)
		return rv;

	if (size > shared_mem_size())
		size = shared_mem_size();

        /* Acquire the shared memory buffer */
	rv = shared_mem_acquire(size, 0, &data);
	if (rv) {
		ccputs("Can't get shared mem\n");
		return rv;
	}

	/* Fill the data buffer with a pattern */
	for (i = 0; i < size; i++)
		data[i] = i;

	ccprintf("Writing %d bytes to 0x%x...\n",
		 size, offset, offset);
	rv = flash_write(offset, size, data);

	/* Free the buffer */
	shared_mem_release(data);

	return rv;
}
DECLARE_CONSOLE_COMMAND(flashwrite, command_flash_write,
			"offset [size]",
			"Write pattern to flash",
			NULL);


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

	if (argc < 2)
		return EC_ERROR_PARAM_COUNT;

	/* 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, 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
		return EC_ERROR_PARAM1;
}
DECLARE_CONSOLE_COMMAND(flashwp, command_flash_wp,
			"<lock | unlock | now | set | clear> offset [size]",
			"Print or modify flash write protect",
			NULL);

/*****************************************************************************/
/* Host commands */

int flash_command_get_info(uint8_t *data, int *resp_size)
{
	struct ec_response_flash_info *r =
			(struct ec_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();
	*resp_size = sizeof(struct ec_response_flash_info);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_INFO, flash_command_get_info);


#ifdef SUPPORT_CHECKSUM
int flash_command_checksum(uint8_t *data, int *resp_size)
{
	struct ec_params_flash_checksum *p =
		(struct ec_params_flash_checksum *)data;
	struct ec_response_flash_checksum *r =
		(struct ec_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))
			return EC_RES_ERROR;
		BYTE_IN(cs, byte);
	}

	r->checksum = cs;

	*resp_size = sizeof(struct ec_response_flash_checksum);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_CHECKSUM, flash_command_checksum);
#endif


int flash_command_read(uint8_t *data, int *resp_size)
{
	struct ec_params_flash_read *p =
			(struct ec_params_flash_read *)data;
	struct ec_response_flash_read *r =
			(struct ec_response_flash_read *)data;

	if (p->size > sizeof(r->data))
		return EC_RES_ERROR;

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

	*resp_size = sizeof(struct ec_response_flash_read);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_READ, flash_command_read);


int flash_command_write(uint8_t *data, int *resp_size)
{
	struct ec_params_flash_write *p =
			(struct ec_params_flash_write *)data;

	if (p->size > sizeof(p->data))
		return EC_RES_ERROR;

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

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

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_WRITE, flash_command_write);


int flash_command_erase(uint8_t *data, int *resp_size)
{
	struct ec_params_flash_erase *p =
			(struct ec_params_flash_erase *)data;

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

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

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_ERASE, flash_command_erase);


int flash_command_wp_enable(uint8_t *data, int *resp_size)
{
	struct ec_params_flash_wp_enable *p =
			(struct ec_params_flash_wp_enable *)data;

	return flash_lock_protect(p->enable_wp ? 1 : 0);
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_WP_ENABLE,
		     flash_command_wp_enable);


int flash_command_wp_get_state(uint8_t *data, int *resp_size)
{
	struct ec_response_flash_wp_enable *p =
			(struct ec_response_flash_wp_enable *)data;

	if (flash_get_protect_lock() & FLASH_PROTECT_LOCK_SET)
		p->enable_wp = 1;
	else
		p->enable_wp = 0;

	*resp_size = sizeof(struct ec_response_flash_wp_enable);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_WP_GET_STATE,
		     flash_command_wp_get_state);


int flash_command_wp_set_range(uint8_t *data, int *resp_size)
{
	struct ec_params_flash_wp_range *p =
			(struct ec_params_flash_wp_range *)data;
	enum ec_status ret;

	if (p->size)
		ret = flash_set_protect(p->offset, p->size, 1);
	else
		ret = flash_set_protect(0, flash_get_size(), 0);

	return ret;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_WP_SET_RANGE,
		     flash_command_wp_set_range);


int flash_command_wp_get_range(uint8_t *data, int *resp_size)
{
	struct ec_response_flash_wp_range *p =
			(struct ec_response_flash_wp_range *)data;
	int pbsize = flash_get_protect_block_size();
	int banks = flash_get_size() / pbsize;
	const uint8_t *blocks;
	int i;
	int min = -1, max = banks - 1;  /* the enclosed range for protected. */

	blocks = flash_get_protect_array();
	for (i = 0; i < banks; i++) {
		if (min == -1) {
			/* Looking for the first protected bank. */
			if (blocks[i] & (FLASH_PROTECT_PERSISTENT |
					 FLASH_PROTECT_UNTIL_REBOOT)) {
				min = i;
			}
		} else if (i < max) {
			/* Looking for the unprotected bank. */
			if (!(blocks[i] & (FLASH_PROTECT_PERSISTENT |
					   FLASH_PROTECT_UNTIL_REBOOT))) {
				max = i - 1;
			}
		}
	}

	/* TODO(crosbug.com/p/9492): return multiple region of ranges(). */
	if (min == -1) {
		/* None of bank is protected. */
		p->offset = 0;
		p->size = 0;
	} else {
		p->offset = min * pbsize;
		p->size = (max - min + 1) * pbsize;
	}

	*resp_size = sizeof(struct ec_response_flash_wp_range);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_FLASH_WP_GET_RANGE,
		     flash_command_wp_get_range);