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