summaryrefslogtreecommitdiff
path: root/common/flash_commands.c
blob: c53f8e6d978375c914d1cc44bc028f3a9fe1843f (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
/* 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 KB\n", flash_get_size());
	return EC_SUCCESS;
}


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


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


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


static const struct console_command console_commands[] = {
	{"flasherase", command_flash_erase},
	{"flashinfo", command_flash_info},
	{"flashwrite", command_flash_write},
	{"flashwp", command_flash_wp},
};
static const struct console_group command_group = {
	"Flash", console_commands, ARRAY_SIZE(console_commands)
};



/*****************************************************************************/
/* 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;
}


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

/*****************************************************************************/
/* Initialization */

int flash_commands_init(void)
{
	/* Register our internal commands */
	console_register_commands(&command_group);
	return EC_SUCCESS;
}