summaryrefslogtreecommitdiff
path: root/test/flash.c
blob: 6f822d88ec0d2ce26aed437f2e135c00ae957ed0 (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
/* 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.
 */

/* Console commands to trigger flash host commands */

#include "board.h"
#include "console.h"
#include "ec_commands.h"
#include "host_command.h"
#include "uart.h"
#include "util.h"

static int ro_image_size(int argc, char **argv)
{
	uart_printf("RO image size = 0x%x\n", CONFIG_SECTION_RO_SIZE);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(rosize, ro_image_size,
			NULL,
			"Report size of RO image",
			NULL);

static int hc_flash_info(int argc, char **argv)
{
	uint8_t data[EC_PARAM_SIZE];
	enum ec_status res;
	int resp_size;
	struct ec_response_flash_info *r =
		(struct ec_response_flash_info *)data;

	res = host_command_process(EC_CMD_FLASH_INFO, data, &resp_size);
	if (res != EC_RES_SUCCESS)
		return EC_ERROR_UNKNOWN;
	uart_printf("flash_size = %d\n", r->flash_size);
	uart_printf("write_block_size = %d\n", r->write_block_size);
	uart_printf("erase_block_size = %d\n", r->erase_block_size);
	uart_printf("protect_block_size = %d\n", r->protect_block_size);

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(hcflashinfo, hc_flash_info,
			NULL, NULL, NULL);

static int hc_flash_read(int argc, char **argv)
{
	uint8_t data[EC_PARAM_SIZE];
	enum ec_status res;
	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;
	char *e;
	int i, size;

	if (argc != 3)
		return EC_ERROR_PARAM_COUNT;

	p->offset = strtoi(argv[1], &e, 0);
	if (*e)
		return EC_ERROR_PARAM1;
	size = strtoi(argv[2], &e, 0);
	p->size = size;
	if (*e)
		return EC_ERROR_PARAM2;

	res = host_command_process(EC_CMD_FLASH_READ, data, &resp_size);
	if (res != EC_RES_SUCCESS)
		return EC_ERROR_UNKNOWN;
	for (i = 0; i < size; ++i) {
		uart_printf("%02x", r->data[i]);
		if ((i & 31) == 31)
			uart_puts("\n");
	}
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(hcflashread, hc_flash_read,
			NULL, NULL, NULL);

static int hc_flash_write(int argc, char **argv)
{
	uint8_t data[EC_PARAM_SIZE];
	enum ec_status res;
	int resp_size;
	struct ec_params_flash_write *p =
		(struct ec_params_flash_write *)data;
	char *e;
	int i, size;
	int seed, mult, add;

	if (argc != 6)
		return EC_ERROR_PARAM_COUNT;

	p->offset = strtoi(argv[1], &e, 0);
	if (*e)
		return EC_ERROR_PARAM1;
	size = strtoi(argv[2], &e, 0);
	p->size = size;
	if (*e)
		return EC_ERROR_PARAM2;
	seed = strtoi(argv[3], &e, 0);
	if (*e)
		return EC_ERROR_PARAM3;
	mult = strtoi(argv[4], &e, 0);
	if (*e)
		return EC_ERROR_PARAM4;
	add = strtoi(argv[5], &e, 0);
	if (*e)
		return EC_ERROR_PARAM5;

	for (i = 0; i < size; ++i) {
		p->data[i] = (uint8_t)(seed & 0xff);
		seed = seed * mult + add;
	}

	res = host_command_process(EC_CMD_FLASH_WRITE, data, &resp_size);
	if (res != EC_RES_SUCCESS)
		return EC_ERROR_UNKNOWN;
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(hcflashwrite, hc_flash_write,
			NULL, NULL, NULL);

static int hc_flash_erase(int argc, char **argv)
{
	uint8_t data[EC_PARAM_SIZE];
	enum ec_status res;
	int resp_size;
	struct ec_params_flash_erase *p =
		(struct ec_params_flash_erase *)data;
	char *e;
	int size;

	if (argc != 3)
		return EC_ERROR_PARAM_COUNT;

	p->offset = strtoi(argv[1], &e, 0);
	if (*e)
		return EC_ERROR_PARAM1;
	size = strtoi(argv[2], &e, 0);
	p->size = size;
	if (*e)
		return EC_ERROR_PARAM2;

	res = host_command_process(EC_CMD_FLASH_ERASE, data, &resp_size);
	if (res != EC_RES_SUCCESS)
		return EC_ERROR_UNKNOWN;
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(hcflasherase, hc_flash_erase,
			NULL, NULL, NULL);