]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bfeba017 MF |
2 | /* |
3 | * Chromium OS cros_ec driver | |
4 | * | |
5 | * Copyright (c) 2016 The Chromium OS Authors. | |
6 | * Copyright (c) 2016 National Instruments Corp | |
bfeba017 MF |
7 | */ |
8 | ||
9 | #include <common.h> | |
10 | #include <command.h> | |
11 | #include <cros_ec.h> | |
12 | #include <dm.h> | |
b79fdc76 | 13 | #include <flash.h> |
bfeba017 MF |
14 | #include <dm/device-internal.h> |
15 | #include <dm/uclass-internal.h> | |
16 | ||
17 | /* Note: depends on enum ec_current_image */ | |
18 | static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"}; | |
19 | ||
a2558e87 MF |
20 | /** |
21 | * Decode a flash region parameter | |
22 | * | |
23 | * @param argc Number of params remaining | |
24 | * @param argv List of remaining parameters | |
25 | * @return flash region (EC_FLASH_REGION_...) or -1 on error | |
26 | */ | |
09140113 | 27 | static int cros_ec_decode_region(int argc, char *const argv[]) |
a2558e87 MF |
28 | { |
29 | if (argc > 0) { | |
30 | if (0 == strcmp(*argv, "rw")) | |
6f1c0430 | 31 | return EC_FLASH_REGION_ACTIVE; |
a2558e87 MF |
32 | else if (0 == strcmp(*argv, "ro")) |
33 | return EC_FLASH_REGION_RO; | |
34 | ||
35 | debug("%s: Invalid region '%s'\n", __func__, *argv); | |
36 | } else { | |
37 | debug("%s: Missing region parameter\n", __func__); | |
38 | } | |
39 | ||
40 | return -1; | |
41 | } | |
42 | ||
bfeba017 MF |
43 | /** |
44 | * Perform a flash read or write command | |
45 | * | |
46 | * @param dev CROS-EC device to read/write | |
47 | * @param is_write 1 do to a write, 0 to do a read | |
48 | * @param argc Number of arguments | |
49 | * @param argv Arguments (2 is region, 3 is address) | |
50 | * @return 0 for ok, 1 for a usage error or -ve for ec command error | |
51 | * (negative EC_RES_...) | |
52 | */ | |
6322a7b6 | 53 | static int do_read_write(struct udevice *dev, int is_write, int argc, |
09140113 | 54 | char *const argv[]) |
bfeba017 MF |
55 | { |
56 | uint32_t offset, size = -1U, region_size; | |
57 | unsigned long addr; | |
58 | char *endp; | |
59 | int region; | |
60 | int ret; | |
61 | ||
62 | region = cros_ec_decode_region(argc - 2, argv + 2); | |
63 | if (region == -1) | |
64 | return 1; | |
65 | if (argc < 4) | |
66 | return 1; | |
67 | addr = simple_strtoul(argv[3], &endp, 16); | |
68 | if (*argv[3] == 0 || *endp != 0) | |
69 | return 1; | |
70 | if (argc > 4) { | |
71 | size = simple_strtoul(argv[4], &endp, 16); | |
72 | if (*argv[4] == 0 || *endp != 0) | |
73 | return 1; | |
74 | } | |
75 | ||
76 | ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size); | |
77 | if (ret) { | |
78 | debug("%s: Could not read region info\n", __func__); | |
79 | return ret; | |
80 | } | |
81 | if (size == -1U) | |
82 | size = region_size; | |
83 | ||
84 | ret = is_write ? | |
85 | cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) : | |
86 | cros_ec_flash_read(dev, (uint8_t *)addr, offset, size); | |
87 | if (ret) { | |
88 | debug("%s: Could not %s region\n", __func__, | |
89 | is_write ? "write" : "read"); | |
90 | return ret; | |
91 | } | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
09140113 SG |
96 | static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc, |
97 | char *const argv[]) | |
bfeba017 | 98 | { |
6322a7b6 | 99 | struct udevice *dev; |
bfeba017 MF |
100 | const char *cmd; |
101 | int ret = 0; | |
102 | ||
103 | if (argc < 2) | |
104 | return CMD_RET_USAGE; | |
105 | ||
106 | cmd = argv[1]; | |
107 | if (0 == strcmp("init", cmd)) { | |
108 | /* Remove any existing device */ | |
6322a7b6 | 109 | ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev); |
bfeba017 | 110 | if (!ret) |
6322a7b6 SG |
111 | device_remove(dev, DM_REMOVE_NORMAL); |
112 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev); | |
bfeba017 MF |
113 | if (ret) { |
114 | printf("Could not init cros_ec device (err %d)\n", ret); | |
115 | return 1; | |
116 | } | |
117 | return 0; | |
118 | } | |
119 | ||
6322a7b6 | 120 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev); |
bfeba017 MF |
121 | if (ret) { |
122 | printf("Cannot get cros-ec device (err=%d)\n", ret); | |
123 | return 1; | |
124 | } | |
bfeba017 MF |
125 | if (0 == strcmp("id", cmd)) { |
126 | char id[MSG_BYTES]; | |
127 | ||
128 | if (cros_ec_read_id(dev, id, sizeof(id))) { | |
129 | debug("%s: Could not read KBC ID\n", __func__); | |
130 | return 1; | |
131 | } | |
132 | printf("%s\n", id); | |
133 | } else if (0 == strcmp("info", cmd)) { | |
134 | struct ec_response_mkbp_info info; | |
135 | ||
136 | if (cros_ec_info(dev, &info)) { | |
137 | debug("%s: Could not read KBC info\n", __func__); | |
138 | return 1; | |
139 | } | |
140 | printf("rows = %u\n", info.rows); | |
141 | printf("cols = %u\n", info.cols); | |
bfeba017 MF |
142 | } else if (0 == strcmp("curimage", cmd)) { |
143 | enum ec_current_image image; | |
144 | ||
145 | if (cros_ec_read_current_image(dev, &image)) { | |
146 | debug("%s: Could not read KBC image\n", __func__); | |
147 | return 1; | |
148 | } | |
149 | printf("%d\n", image); | |
150 | } else if (0 == strcmp("hash", cmd)) { | |
151 | struct ec_response_vboot_hash hash; | |
152 | int i; | |
153 | ||
a12ef7e2 | 154 | if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) { |
bfeba017 MF |
155 | debug("%s: Could not read KBC hash\n", __func__); |
156 | return 1; | |
157 | } | |
158 | ||
159 | if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256) | |
160 | printf("type: SHA-256\n"); | |
161 | else | |
162 | printf("type: %d\n", hash.hash_type); | |
163 | ||
164 | printf("offset: 0x%08x\n", hash.offset); | |
165 | printf("size: 0x%08x\n", hash.size); | |
166 | ||
167 | printf("digest: "); | |
168 | for (i = 0; i < hash.digest_size; i++) | |
169 | printf("%02x", hash.hash_digest[i]); | |
170 | printf("\n"); | |
171 | } else if (0 == strcmp("reboot", cmd)) { | |
172 | int region; | |
173 | enum ec_reboot_cmd cmd; | |
174 | ||
175 | if (argc >= 3 && !strcmp(argv[2], "cold")) { | |
176 | cmd = EC_REBOOT_COLD; | |
177 | } else { | |
178 | region = cros_ec_decode_region(argc - 2, argv + 2); | |
179 | if (region == EC_FLASH_REGION_RO) | |
180 | cmd = EC_REBOOT_JUMP_RO; | |
6f1c0430 | 181 | else if (region == EC_FLASH_REGION_ACTIVE) |
bfeba017 MF |
182 | cmd = EC_REBOOT_JUMP_RW; |
183 | else | |
184 | return CMD_RET_USAGE; | |
185 | } | |
186 | ||
187 | if (cros_ec_reboot(dev, cmd, 0)) { | |
188 | debug("%s: Could not reboot KBC\n", __func__); | |
189 | return 1; | |
190 | } | |
191 | } else if (0 == strcmp("events", cmd)) { | |
192 | uint32_t events; | |
193 | ||
194 | if (cros_ec_get_host_events(dev, &events)) { | |
195 | debug("%s: Could not read host events\n", __func__); | |
196 | return 1; | |
197 | } | |
198 | printf("0x%08x\n", events); | |
199 | } else if (0 == strcmp("clrevents", cmd)) { | |
200 | uint32_t events = 0x7fffffff; | |
201 | ||
202 | if (argc >= 3) | |
203 | events = simple_strtol(argv[2], NULL, 0); | |
204 | ||
205 | if (cros_ec_clear_host_events(dev, events)) { | |
206 | debug("%s: Could not clear host events\n", __func__); | |
207 | return 1; | |
208 | } | |
209 | } else if (0 == strcmp("read", cmd)) { | |
210 | ret = do_read_write(dev, 0, argc, argv); | |
211 | if (ret > 0) | |
212 | return CMD_RET_USAGE; | |
213 | } else if (0 == strcmp("write", cmd)) { | |
214 | ret = do_read_write(dev, 1, argc, argv); | |
215 | if (ret > 0) | |
216 | return CMD_RET_USAGE; | |
217 | } else if (0 == strcmp("erase", cmd)) { | |
218 | int region = cros_ec_decode_region(argc - 2, argv + 2); | |
219 | uint32_t offset, size; | |
220 | ||
221 | if (region == -1) | |
222 | return CMD_RET_USAGE; | |
223 | if (cros_ec_flash_offset(dev, region, &offset, &size)) { | |
224 | debug("%s: Could not read region info\n", __func__); | |
225 | ret = -1; | |
226 | } else { | |
227 | ret = cros_ec_flash_erase(dev, offset, size); | |
228 | if (ret) { | |
229 | debug("%s: Could not erase region\n", | |
230 | __func__); | |
231 | } | |
232 | } | |
233 | } else if (0 == strcmp("regioninfo", cmd)) { | |
234 | int region = cros_ec_decode_region(argc - 2, argv + 2); | |
235 | uint32_t offset, size; | |
236 | ||
237 | if (region == -1) | |
238 | return CMD_RET_USAGE; | |
239 | ret = cros_ec_flash_offset(dev, region, &offset, &size); | |
240 | if (ret) { | |
241 | debug("%s: Could not read region info\n", __func__); | |
242 | } else { | |
243 | printf("Region: %s\n", region == EC_FLASH_REGION_RO ? | |
244 | "RO" : "RW"); | |
245 | printf("Offset: %x\n", offset); | |
246 | printf("Size: %x\n", size); | |
247 | } | |
248 | } else if (0 == strcmp("flashinfo", cmd)) { | |
249 | struct ec_response_flash_info p; | |
250 | ||
251 | ret = cros_ec_read_flashinfo(dev, &p); | |
252 | if (!ret) { | |
253 | printf("Flash size: %u\n", p.flash_size); | |
254 | printf("Write block size: %u\n", p.write_block_size); | |
255 | printf("Erase block size: %u\n", p.erase_block_size); | |
256 | } | |
257 | } else if (0 == strcmp("vbnvcontext", cmd)) { | |
258 | uint8_t block[EC_VBNV_BLOCK_SIZE]; | |
259 | char buf[3]; | |
260 | int i, len; | |
261 | unsigned long result; | |
262 | ||
263 | if (argc <= 2) { | |
6322a7b6 SG |
264 | ret = cros_ec_read_nvdata(dev, block, |
265 | EC_VBNV_BLOCK_SIZE); | |
bfeba017 MF |
266 | if (!ret) { |
267 | printf("vbnv_block: "); | |
268 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) | |
269 | printf("%02x", block[i]); | |
270 | putc('\n'); | |
271 | } | |
272 | } else { | |
273 | /* | |
274 | * TODO(clchiou): Move this to a utility function as | |
275 | * cmd_spi might want to call it. | |
276 | */ | |
277 | memset(block, 0, EC_VBNV_BLOCK_SIZE); | |
278 | len = strlen(argv[2]); | |
279 | buf[2] = '\0'; | |
280 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) { | |
281 | if (i * 2 >= len) | |
282 | break; | |
283 | buf[0] = argv[2][i * 2]; | |
284 | if (i * 2 + 1 >= len) | |
285 | buf[1] = '0'; | |
286 | else | |
287 | buf[1] = argv[2][i * 2 + 1]; | |
288 | strict_strtoul(buf, 16, &result); | |
289 | block[i] = result; | |
290 | } | |
6322a7b6 SG |
291 | ret = cros_ec_write_nvdata(dev, block, |
292 | EC_VBNV_BLOCK_SIZE); | |
bfeba017 MF |
293 | } |
294 | if (ret) { | |
295 | debug("%s: Could not %s VbNvContext\n", __func__, | |
296 | argc <= 2 ? "read" : "write"); | |
297 | } | |
298 | } else if (0 == strcmp("test", cmd)) { | |
299 | int result = cros_ec_test(dev); | |
300 | ||
301 | if (result) | |
302 | printf("Test failed with error %d\n", result); | |
303 | else | |
304 | puts("Test passed\n"); | |
305 | } else if (0 == strcmp("version", cmd)) { | |
306 | struct ec_response_get_version *p; | |
307 | char *build_string; | |
308 | ||
309 | ret = cros_ec_read_version(dev, &p); | |
310 | if (!ret) { | |
311 | /* Print versions */ | |
312 | printf("RO version: %1.*s\n", | |
313 | (int)sizeof(p->version_string_ro), | |
314 | p->version_string_ro); | |
315 | printf("RW version: %1.*s\n", | |
316 | (int)sizeof(p->version_string_rw), | |
317 | p->version_string_rw); | |
318 | printf("Firmware copy: %s\n", | |
319 | (p->current_image < | |
320 | ARRAY_SIZE(ec_current_image_name) ? | |
321 | ec_current_image_name[p->current_image] : | |
322 | "?")); | |
323 | ret = cros_ec_read_build_info(dev, &build_string); | |
324 | if (!ret) | |
325 | printf("Build info: %s\n", build_string); | |
326 | } | |
327 | } else if (0 == strcmp("ldo", cmd)) { | |
328 | uint8_t index, state; | |
329 | char *endp; | |
330 | ||
331 | if (argc < 3) | |
332 | return CMD_RET_USAGE; | |
333 | index = simple_strtoul(argv[2], &endp, 10); | |
334 | if (*argv[2] == 0 || *endp != 0) | |
335 | return CMD_RET_USAGE; | |
336 | if (argc > 3) { | |
337 | state = simple_strtoul(argv[3], &endp, 10); | |
338 | if (*argv[3] == 0 || *endp != 0) | |
339 | return CMD_RET_USAGE; | |
6322a7b6 | 340 | ret = cros_ec_set_ldo(dev, index, state); |
bfeba017 | 341 | } else { |
6322a7b6 | 342 | ret = cros_ec_get_ldo(dev, index, &state); |
bfeba017 MF |
343 | if (!ret) { |
344 | printf("LDO%d: %s\n", index, | |
345 | state == EC_LDO_STATE_ON ? | |
346 | "on" : "off"); | |
347 | } | |
348 | } | |
349 | ||
350 | if (ret) { | |
351 | debug("%s: Could not access LDO%d\n", __func__, index); | |
352 | return ret; | |
353 | } | |
354 | } else { | |
355 | return CMD_RET_USAGE; | |
356 | } | |
357 | ||
358 | if (ret < 0) { | |
359 | printf("Error: CROS-EC command failed (error %d)\n", ret); | |
360 | ret = 1; | |
361 | } | |
362 | ||
363 | return ret; | |
364 | } | |
365 | ||
366 | U_BOOT_CMD( | |
367 | crosec, 6, 1, do_cros_ec, | |
368 | "CROS-EC utility command", | |
369 | "init Re-init CROS-EC (done on startup automatically)\n" | |
370 | "crosec id Read CROS-EC ID\n" | |
371 | "crosec info Read CROS-EC info\n" | |
372 | "crosec curimage Read CROS-EC current image\n" | |
373 | "crosec hash Read CROS-EC hash\n" | |
374 | "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" | |
375 | "crosec events Read CROS-EC host events\n" | |
376 | "crosec clrevents [mask] Clear CROS-EC host events\n" | |
377 | "crosec regioninfo <ro|rw> Read image info\n" | |
378 | "crosec flashinfo Read flash info\n" | |
379 | "crosec erase <ro|rw> Erase EC image\n" | |
380 | "crosec read <ro|rw> <addr> [<size>] Read EC image\n" | |
381 | "crosec write <ro|rw> <addr> [<size>] Write EC image\n" | |
382 | "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" | |
383 | "crosec ldo <idx> [<state>] Switch/Read LDO state\n" | |
384 | "crosec test run tests on cros_ec\n" | |
385 | "crosec version Read CROS-EC version" | |
386 | ); |