1 // SPDX-License-Identifier: GPL-2.0+
3 * Chromium OS cros_ec driver
5 * Copyright (c) 2016 The Chromium OS Authors.
6 * Copyright (c) 2016 National Instruments Corp
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
16 /* Note: depends on enum ec_current_image */
17 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
20 * Decode a flash region parameter
22 * @param argc Number of params remaining
23 * @param argv List of remaining parameters
24 * @return flash region (EC_FLASH_REGION_...) or -1 on error
26 static int cros_ec_decode_region(int argc, char * const argv[])
29 if (0 == strcmp(*argv, "rw"))
30 return EC_FLASH_REGION_ACTIVE;
31 else if (0 == strcmp(*argv, "ro"))
32 return EC_FLASH_REGION_RO;
34 debug("%s: Invalid region '%s'\n", __func__, *argv);
36 debug("%s: Missing region parameter\n", __func__);
43 * Perform a flash read or write command
45 * @param dev CROS-EC device to read/write
46 * @param is_write 1 do to a write, 0 to do a read
47 * @param argc Number of arguments
48 * @param argv Arguments (2 is region, 3 is address)
49 * @return 0 for ok, 1 for a usage error or -ve for ec command error
50 * (negative EC_RES_...)
52 static int do_read_write(struct udevice *dev, int is_write, int argc,
55 uint32_t offset, size = -1U, region_size;
61 region = cros_ec_decode_region(argc - 2, argv + 2);
66 addr = simple_strtoul(argv[3], &endp, 16);
67 if (*argv[3] == 0 || *endp != 0)
70 size = simple_strtoul(argv[4], &endp, 16);
71 if (*argv[4] == 0 || *endp != 0)
75 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
77 debug("%s: Could not read region info\n", __func__);
84 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
85 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
87 debug("%s: Could not %s region\n", __func__,
88 is_write ? "write" : "read");
95 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
102 return CMD_RET_USAGE;
105 if (0 == strcmp("init", cmd)) {
106 /* Remove any existing device */
107 ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev);
109 device_remove(dev, DM_REMOVE_NORMAL);
110 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
112 printf("Could not init cros_ec device (err %d)\n", ret);
118 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
120 printf("Cannot get cros-ec device (err=%d)\n", ret);
123 if (0 == strcmp("id", cmd)) {
126 if (cros_ec_read_id(dev, id, sizeof(id))) {
127 debug("%s: Could not read KBC ID\n", __func__);
131 } else if (0 == strcmp("info", cmd)) {
132 struct ec_response_mkbp_info info;
134 if (cros_ec_info(dev, &info)) {
135 debug("%s: Could not read KBC info\n", __func__);
138 printf("rows = %u\n", info.rows);
139 printf("cols = %u\n", info.cols);
140 } else if (0 == strcmp("curimage", cmd)) {
141 enum ec_current_image image;
143 if (cros_ec_read_current_image(dev, &image)) {
144 debug("%s: Could not read KBC image\n", __func__);
147 printf("%d\n", image);
148 } else if (0 == strcmp("hash", cmd)) {
149 struct ec_response_vboot_hash hash;
152 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
153 debug("%s: Could not read KBC hash\n", __func__);
157 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
158 printf("type: SHA-256\n");
160 printf("type: %d\n", hash.hash_type);
162 printf("offset: 0x%08x\n", hash.offset);
163 printf("size: 0x%08x\n", hash.size);
166 for (i = 0; i < hash.digest_size; i++)
167 printf("%02x", hash.hash_digest[i]);
169 } else if (0 == strcmp("reboot", cmd)) {
171 enum ec_reboot_cmd cmd;
173 if (argc >= 3 && !strcmp(argv[2], "cold")) {
174 cmd = EC_REBOOT_COLD;
176 region = cros_ec_decode_region(argc - 2, argv + 2);
177 if (region == EC_FLASH_REGION_RO)
178 cmd = EC_REBOOT_JUMP_RO;
179 else if (region == EC_FLASH_REGION_ACTIVE)
180 cmd = EC_REBOOT_JUMP_RW;
182 return CMD_RET_USAGE;
185 if (cros_ec_reboot(dev, cmd, 0)) {
186 debug("%s: Could not reboot KBC\n", __func__);
189 } else if (0 == strcmp("events", cmd)) {
192 if (cros_ec_get_host_events(dev, &events)) {
193 debug("%s: Could not read host events\n", __func__);
196 printf("0x%08x\n", events);
197 } else if (0 == strcmp("clrevents", cmd)) {
198 uint32_t events = 0x7fffffff;
201 events = simple_strtol(argv[2], NULL, 0);
203 if (cros_ec_clear_host_events(dev, events)) {
204 debug("%s: Could not clear host events\n", __func__);
207 } else if (0 == strcmp("read", cmd)) {
208 ret = do_read_write(dev, 0, argc, argv);
210 return CMD_RET_USAGE;
211 } else if (0 == strcmp("write", cmd)) {
212 ret = do_read_write(dev, 1, argc, argv);
214 return CMD_RET_USAGE;
215 } else if (0 == strcmp("erase", cmd)) {
216 int region = cros_ec_decode_region(argc - 2, argv + 2);
217 uint32_t offset, size;
220 return CMD_RET_USAGE;
221 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
222 debug("%s: Could not read region info\n", __func__);
225 ret = cros_ec_flash_erase(dev, offset, size);
227 debug("%s: Could not erase region\n",
231 } else if (0 == strcmp("regioninfo", cmd)) {
232 int region = cros_ec_decode_region(argc - 2, argv + 2);
233 uint32_t offset, size;
236 return CMD_RET_USAGE;
237 ret = cros_ec_flash_offset(dev, region, &offset, &size);
239 debug("%s: Could not read region info\n", __func__);
241 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
243 printf("Offset: %x\n", offset);
244 printf("Size: %x\n", size);
246 } else if (0 == strcmp("flashinfo", cmd)) {
247 struct ec_response_flash_info p;
249 ret = cros_ec_read_flashinfo(dev, &p);
251 printf("Flash size: %u\n", p.flash_size);
252 printf("Write block size: %u\n", p.write_block_size);
253 printf("Erase block size: %u\n", p.erase_block_size);
255 } else if (0 == strcmp("vbnvcontext", cmd)) {
256 uint8_t block[EC_VBNV_BLOCK_SIZE];
259 unsigned long result;
262 ret = cros_ec_read_nvdata(dev, block,
265 printf("vbnv_block: ");
266 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
267 printf("%02x", block[i]);
272 * TODO(clchiou): Move this to a utility function as
273 * cmd_spi might want to call it.
275 memset(block, 0, EC_VBNV_BLOCK_SIZE);
276 len = strlen(argv[2]);
278 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
281 buf[0] = argv[2][i * 2];
282 if (i * 2 + 1 >= len)
285 buf[1] = argv[2][i * 2 + 1];
286 strict_strtoul(buf, 16, &result);
289 ret = cros_ec_write_nvdata(dev, block,
293 debug("%s: Could not %s VbNvContext\n", __func__,
294 argc <= 2 ? "read" : "write");
296 } else if (0 == strcmp("test", cmd)) {
297 int result = cros_ec_test(dev);
300 printf("Test failed with error %d\n", result);
302 puts("Test passed\n");
303 } else if (0 == strcmp("version", cmd)) {
304 struct ec_response_get_version *p;
307 ret = cros_ec_read_version(dev, &p);
310 printf("RO version: %1.*s\n",
311 (int)sizeof(p->version_string_ro),
312 p->version_string_ro);
313 printf("RW version: %1.*s\n",
314 (int)sizeof(p->version_string_rw),
315 p->version_string_rw);
316 printf("Firmware copy: %s\n",
318 ARRAY_SIZE(ec_current_image_name) ?
319 ec_current_image_name[p->current_image] :
321 ret = cros_ec_read_build_info(dev, &build_string);
323 printf("Build info: %s\n", build_string);
325 } else if (0 == strcmp("ldo", cmd)) {
326 uint8_t index, state;
330 return CMD_RET_USAGE;
331 index = simple_strtoul(argv[2], &endp, 10);
332 if (*argv[2] == 0 || *endp != 0)
333 return CMD_RET_USAGE;
335 state = simple_strtoul(argv[3], &endp, 10);
336 if (*argv[3] == 0 || *endp != 0)
337 return CMD_RET_USAGE;
338 ret = cros_ec_set_ldo(dev, index, state);
340 ret = cros_ec_get_ldo(dev, index, &state);
342 printf("LDO%d: %s\n", index,
343 state == EC_LDO_STATE_ON ?
349 debug("%s: Could not access LDO%d\n", __func__, index);
353 return CMD_RET_USAGE;
357 printf("Error: CROS-EC command failed (error %d)\n", ret);
365 crosec, 6, 1, do_cros_ec,
366 "CROS-EC utility command",
367 "init Re-init CROS-EC (done on startup automatically)\n"
368 "crosec id Read CROS-EC ID\n"
369 "crosec info Read CROS-EC info\n"
370 "crosec curimage Read CROS-EC current image\n"
371 "crosec hash Read CROS-EC hash\n"
372 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
373 "crosec events Read CROS-EC host events\n"
374 "crosec clrevents [mask] Clear CROS-EC host events\n"
375 "crosec regioninfo <ro|rw> Read image info\n"
376 "crosec flashinfo Read flash info\n"
377 "crosec erase <ro|rw> Erase EC image\n"
378 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
379 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
380 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
381 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
382 "crosec test run tests on cros_ec\n"
383 "crosec version Read CROS-EC version"