cros_ec_sandbox.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Chromium OS cros_ec driver - sandbox emulation
  3. *
  4. * Copyright (c) 2013 The Chromium OS Authors.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <cros_ec.h>
  10. #include <dm.h>
  11. #include <ec_commands.h>
  12. #include <errno.h>
  13. #include <hash.h>
  14. #include <malloc.h>
  15. #include <os.h>
  16. #include <u-boot/sha256.h>
  17. #include <spi.h>
  18. #include <asm/state.h>
  19. #include <asm/sdl.h>
  20. #include <linux/input.h>
  21. /*
  22. * Ultimately it shold be possible to connect an Chrome OS EC emulation
  23. * to U-Boot and remove all of this code. But this provides a test
  24. * environment for bringing up chromeos_sandbox and demonstrating its
  25. * utility.
  26. *
  27. * This emulation includes the following:
  28. *
  29. * 1. Emulation of the keyboard, by converting keypresses received from SDL
  30. * into key scan data, passed back from the EC as key scan messages. The
  31. * key layout is read from the device tree.
  32. *
  33. * 2. Emulation of vboot context - so this can be read/written as required.
  34. *
  35. * 3. Save/restore of EC state, so that the vboot context, flash memory
  36. * contents and current image can be preserved across boots. This is important
  37. * since the EC is supposed to continue running even if the AP resets.
  38. *
  39. * 4. Some event support, in particular allowing Escape to be pressed on boot
  40. * to enter recovery mode. The EC passes this to U-Boot through the normal
  41. * event message.
  42. *
  43. * 5. Flash read/write/erase support, so that software sync works. The
  44. * protect messages are supported but no protection is implemented.
  45. *
  46. * 6. Hashing of the EC image, again to support software sync.
  47. *
  48. * Other features can be added, although a better path is probably to link
  49. * the EC image in with U-Boot (Vic has demonstrated a prototype for this).
  50. */
  51. DECLARE_GLOBAL_DATA_PTR;
  52. #define KEYBOARD_ROWS 8
  53. #define KEYBOARD_COLS 13
  54. /* A single entry of the key matrix */
  55. struct ec_keymatrix_entry {
  56. int row; /* key matrix row */
  57. int col; /* key matrix column */
  58. int keycode; /* corresponding linux key code */
  59. };
  60. /**
  61. * struct ec_state - Information about the EC state
  62. *
  63. * @vbnv_context: Vboot context data stored by EC
  64. * @ec_config: FDT config information about the EC (e.g. flashmap)
  65. * @flash_data: Contents of flash memory
  66. * @flash_data_len: Size of flash memory
  67. * @current_image: Current image the EC is running
  68. * @matrix_count: Number of keys to decode in matrix
  69. * @matrix: Information about keyboard matrix
  70. * @keyscan: Current keyscan information (bit set for each row/column pressed)
  71. * @recovery_req: Keyboard recovery requested
  72. */
  73. struct ec_state {
  74. uint8_t vbnv_context[EC_VBNV_BLOCK_SIZE];
  75. struct fdt_cros_ec ec_config;
  76. uint8_t *flash_data;
  77. int flash_data_len;
  78. enum ec_current_image current_image;
  79. int matrix_count;
  80. struct ec_keymatrix_entry *matrix; /* the key matrix info */
  81. uint8_t keyscan[KEYBOARD_COLS];
  82. bool recovery_req;
  83. } s_state, *g_state;
  84. /**
  85. * cros_ec_read_state() - read the sandbox EC state from the state file
  86. *
  87. * If data is available, then blob and node will provide access to it. If
  88. * not this function sets up an empty EC.
  89. *
  90. * @param blob: Pointer to device tree blob, or NULL if no data to read
  91. * @param node: Node offset to read from
  92. */
  93. static int cros_ec_read_state(const void *blob, int node)
  94. {
  95. struct ec_state *ec = &s_state;
  96. const char *prop;
  97. int len;
  98. /* Set everything to defaults */
  99. ec->current_image = EC_IMAGE_RO;
  100. if (!blob)
  101. return 0;
  102. /* Read the data if available */
  103. ec->current_image = fdtdec_get_int(blob, node, "current-image",
  104. EC_IMAGE_RO);
  105. prop = fdt_getprop(blob, node, "vbnv-context", &len);
  106. if (prop && len == sizeof(ec->vbnv_context))
  107. memcpy(ec->vbnv_context, prop, len);
  108. prop = fdt_getprop(blob, node, "flash-data", &len);
  109. if (prop) {
  110. ec->flash_data_len = len;
  111. ec->flash_data = os_malloc(len);
  112. if (!ec->flash_data)
  113. return -ENOMEM;
  114. memcpy(ec->flash_data, prop, len);
  115. debug("%s: Loaded EC flash data size %#x\n", __func__, len);
  116. }
  117. return 0;
  118. }
  119. /**
  120. * cros_ec_write_state() - Write out our state to the state file
  121. *
  122. * The caller will ensure that there is a node ready for the state. The node
  123. * may already contain the old state, in which case it is overridden.
  124. *
  125. * @param blob: Device tree blob holding state
  126. * @param node: Node to write our state into
  127. */
  128. static int cros_ec_write_state(void *blob, int node)
  129. {
  130. struct ec_state *ec = g_state;
  131. /* We are guaranteed enough space to write basic properties */
  132. fdt_setprop_u32(blob, node, "current-image", ec->current_image);
  133. fdt_setprop(blob, node, "vbnv-context", ec->vbnv_context,
  134. sizeof(ec->vbnv_context));
  135. return state_setprop(node, "flash-data", ec->flash_data,
  136. ec->ec_config.flash.length);
  137. }
  138. SANDBOX_STATE_IO(cros_ec, "google,cros-ec", cros_ec_read_state,
  139. cros_ec_write_state);
  140. /**
  141. * Return the number of bytes used in the specified image.
  142. *
  143. * This is the actual size of code+data in the image, as opposed to the
  144. * amount of space reserved in flash for that image. This code is similar to
  145. * that used by the real EC code base.
  146. *
  147. * @param ec Current emulated EC state
  148. * @param entry Flash map entry containing the image to check
  149. * @return actual image size in bytes, 0 if the image contains no content or
  150. * error.
  151. */
  152. static int get_image_used(struct ec_state *ec, struct fmap_entry *entry)
  153. {
  154. int size;
  155. /*
  156. * Scan backwards looking for 0xea byte, which is by definition the
  157. * last byte of the image. See ec.lds.S for how this is inserted at
  158. * the end of the image.
  159. */
  160. for (size = entry->length - 1;
  161. size > 0 && ec->flash_data[entry->offset + size] != 0xea;
  162. size--)
  163. ;
  164. return size ? size + 1 : 0; /* 0xea byte IS part of the image */
  165. }
  166. /**
  167. * Read the key matrix from the device tree
  168. *
  169. * Keymap entries in the fdt take the form of 0xRRCCKKKK where
  170. * RR=Row CC=Column KKKK=Key Code
  171. *
  172. * @param ec Current emulated EC state
  173. * @param blob Device tree blob containing keyscan information
  174. * @param node Keyboard node of device tree containing keyscan information
  175. * @return 0 if ok, -1 on error
  176. */
  177. static int keyscan_read_fdt_matrix(struct ec_state *ec, const void *blob,
  178. int node)
  179. {
  180. const u32 *cell;
  181. int upto;
  182. int len;
  183. cell = fdt_getprop(blob, node, "linux,keymap", &len);
  184. ec->matrix_count = len / 4;
  185. ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix));
  186. if (!ec->matrix) {
  187. debug("%s: Out of memory for key matrix\n", __func__);
  188. return -1;
  189. }
  190. /* Now read the data */
  191. for (upto = 0; upto < ec->matrix_count; upto++) {
  192. struct ec_keymatrix_entry *matrix = &ec->matrix[upto];
  193. u32 word;
  194. word = fdt32_to_cpu(*cell++);
  195. matrix->row = word >> 24;
  196. matrix->col = (word >> 16) & 0xff;
  197. matrix->keycode = word & 0xffff;
  198. /* Hard-code some sanity limits for now */
  199. if (matrix->row >= KEYBOARD_ROWS ||
  200. matrix->col >= KEYBOARD_COLS) {
  201. debug("%s: Matrix pos out of range (%d,%d)\n",
  202. __func__, matrix->row, matrix->col);
  203. return -1;
  204. }
  205. }
  206. if (upto != ec->matrix_count) {
  207. debug("%s: Read mismatch from key matrix\n", __func__);
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. /**
  213. * Return the next keyscan message contents
  214. *
  215. * @param ec Current emulated EC state
  216. * @param scan Place to put keyscan bytes for the keyscan message (must hold
  217. * enough space for a full keyscan)
  218. * @return number of bytes of valid scan data
  219. */
  220. static int cros_ec_keyscan(struct ec_state *ec, uint8_t *scan)
  221. {
  222. const struct ec_keymatrix_entry *matrix;
  223. int bytes = KEYBOARD_COLS;
  224. int key[8]; /* allow up to 8 keys to be pressed at once */
  225. int count;
  226. int i;
  227. memset(ec->keyscan, '\0', bytes);
  228. count = sandbox_sdl_scan_keys(key, ARRAY_SIZE(key));
  229. /* Look up keycode in matrix */
  230. for (i = 0, matrix = ec->matrix; i < ec->matrix_count; i++, matrix++) {
  231. bool found;
  232. int j;
  233. for (found = false, j = 0; j < count; j++) {
  234. if (matrix->keycode == key[j])
  235. found = true;
  236. }
  237. if (found) {
  238. debug("%d: %d,%d\n", matrix->keycode, matrix->row,
  239. matrix->col);
  240. ec->keyscan[matrix->col] |= 1 << matrix->row;
  241. }
  242. }
  243. memcpy(scan, ec->keyscan, bytes);
  244. return bytes;
  245. }
  246. /**
  247. * Process an emulated EC command
  248. *
  249. * @param ec Current emulated EC state
  250. * @param req_hdr Pointer to request header
  251. * @param req_data Pointer to body of request
  252. * @param resp_hdr Pointer to place to put response header
  253. * @param resp_data Pointer to place to put response data, if any
  254. * @return length of response data, or 0 for no response data, or -1 on error
  255. */
  256. static int process_cmd(struct ec_state *ec,
  257. struct ec_host_request *req_hdr, const void *req_data,
  258. struct ec_host_response *resp_hdr, void *resp_data)
  259. {
  260. int len;
  261. /* TODO(sjg@chromium.org): Check checksums */
  262. debug("EC command %#0x\n", req_hdr->command);
  263. switch (req_hdr->command) {
  264. case EC_CMD_HELLO: {
  265. const struct ec_params_hello *req = req_data;
  266. struct ec_response_hello *resp = resp_data;
  267. resp->out_data = req->in_data + 0x01020304;
  268. len = sizeof(*resp);
  269. break;
  270. }
  271. case EC_CMD_GET_VERSION: {
  272. struct ec_response_get_version *resp = resp_data;
  273. strcpy(resp->version_string_ro, "sandbox_ro");
  274. strcpy(resp->version_string_rw, "sandbox_rw");
  275. resp->current_image = ec->current_image;
  276. debug("Current image %d\n", resp->current_image);
  277. len = sizeof(*resp);
  278. break;
  279. }
  280. case EC_CMD_VBNV_CONTEXT: {
  281. const struct ec_params_vbnvcontext *req = req_data;
  282. struct ec_response_vbnvcontext *resp = resp_data;
  283. switch (req->op) {
  284. case EC_VBNV_CONTEXT_OP_READ:
  285. memcpy(resp->block, ec->vbnv_context,
  286. sizeof(resp->block));
  287. len = sizeof(*resp);
  288. break;
  289. case EC_VBNV_CONTEXT_OP_WRITE:
  290. memcpy(ec->vbnv_context, resp->block,
  291. sizeof(resp->block));
  292. len = 0;
  293. break;
  294. default:
  295. printf(" ** Unknown vbnv_context command %#02x\n",
  296. req->op);
  297. return -1;
  298. }
  299. break;
  300. }
  301. case EC_CMD_REBOOT_EC: {
  302. const struct ec_params_reboot_ec *req = req_data;
  303. printf("Request reboot type %d\n", req->cmd);
  304. switch (req->cmd) {
  305. case EC_REBOOT_DISABLE_JUMP:
  306. len = 0;
  307. break;
  308. case EC_REBOOT_JUMP_RW:
  309. ec->current_image = EC_IMAGE_RW;
  310. len = 0;
  311. break;
  312. default:
  313. puts(" ** Unknown type");
  314. return -1;
  315. }
  316. break;
  317. }
  318. case EC_CMD_HOST_EVENT_GET_B: {
  319. struct ec_response_host_event_mask *resp = resp_data;
  320. resp->mask = 0;
  321. if (ec->recovery_req) {
  322. resp->mask |= EC_HOST_EVENT_MASK(
  323. EC_HOST_EVENT_KEYBOARD_RECOVERY);
  324. }
  325. len = sizeof(*resp);
  326. break;
  327. }
  328. case EC_CMD_VBOOT_HASH: {
  329. const struct ec_params_vboot_hash *req = req_data;
  330. struct ec_response_vboot_hash *resp = resp_data;
  331. struct fmap_entry *entry;
  332. int ret, size;
  333. entry = &ec->ec_config.region[EC_FLASH_REGION_RW];
  334. switch (req->cmd) {
  335. case EC_VBOOT_HASH_RECALC:
  336. case EC_VBOOT_HASH_GET:
  337. size = SHA256_SUM_LEN;
  338. len = get_image_used(ec, entry);
  339. ret = hash_block("sha256",
  340. ec->flash_data + entry->offset,
  341. len, resp->hash_digest, &size);
  342. if (ret) {
  343. printf(" ** hash_block() failed\n");
  344. return -1;
  345. }
  346. resp->status = EC_VBOOT_HASH_STATUS_DONE;
  347. resp->hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  348. resp->digest_size = size;
  349. resp->reserved0 = 0;
  350. resp->offset = entry->offset;
  351. resp->size = len;
  352. len = sizeof(*resp);
  353. break;
  354. default:
  355. printf(" ** EC_CMD_VBOOT_HASH: Unknown command %d\n",
  356. req->cmd);
  357. return -1;
  358. }
  359. break;
  360. }
  361. case EC_CMD_FLASH_PROTECT: {
  362. const struct ec_params_flash_protect *req = req_data;
  363. struct ec_response_flash_protect *resp = resp_data;
  364. uint32_t expect = EC_FLASH_PROTECT_ALL_NOW |
  365. EC_FLASH_PROTECT_ALL_AT_BOOT;
  366. printf("mask=%#x, flags=%#x\n", req->mask, req->flags);
  367. if (req->flags == expect || req->flags == 0) {
  368. resp->flags = req->flags ? EC_FLASH_PROTECT_ALL_NOW :
  369. 0;
  370. resp->valid_flags = EC_FLASH_PROTECT_ALL_NOW;
  371. resp->writable_flags = 0;
  372. len = sizeof(*resp);
  373. } else {
  374. puts(" ** unexpected flash protect request\n");
  375. return -1;
  376. }
  377. break;
  378. }
  379. case EC_CMD_FLASH_REGION_INFO: {
  380. const struct ec_params_flash_region_info *req = req_data;
  381. struct ec_response_flash_region_info *resp = resp_data;
  382. struct fmap_entry *entry;
  383. switch (req->region) {
  384. case EC_FLASH_REGION_RO:
  385. case EC_FLASH_REGION_RW:
  386. case EC_FLASH_REGION_WP_RO:
  387. entry = &ec->ec_config.region[req->region];
  388. resp->offset = entry->offset;
  389. resp->size = entry->length;
  390. len = sizeof(*resp);
  391. printf("EC flash region %d: offset=%#x, size=%#x\n",
  392. req->region, resp->offset, resp->size);
  393. break;
  394. default:
  395. printf("** Unknown flash region %d\n", req->region);
  396. return -1;
  397. }
  398. break;
  399. }
  400. case EC_CMD_FLASH_ERASE: {
  401. const struct ec_params_flash_erase *req = req_data;
  402. memset(ec->flash_data + req->offset,
  403. ec->ec_config.flash_erase_value,
  404. req->size);
  405. len = 0;
  406. break;
  407. }
  408. case EC_CMD_FLASH_WRITE: {
  409. const struct ec_params_flash_write *req = req_data;
  410. memcpy(ec->flash_data + req->offset, req + 1, req->size);
  411. len = 0;
  412. break;
  413. }
  414. case EC_CMD_MKBP_STATE:
  415. len = cros_ec_keyscan(ec, resp_data);
  416. break;
  417. case EC_CMD_ENTERING_MODE:
  418. len = 0;
  419. break;
  420. default:
  421. printf(" ** Unknown EC command %#02x\n", req_hdr->command);
  422. return -1;
  423. }
  424. return len;
  425. }
  426. int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
  427. {
  428. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  429. struct ec_state *ec = dev_get_priv(dev->dev);
  430. struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout;
  431. const void *req_data = req_hdr + 1;
  432. struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din;
  433. void *resp_data = resp_hdr + 1;
  434. int len;
  435. len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data);
  436. if (len < 0)
  437. return len;
  438. resp_hdr->struct_version = 3;
  439. resp_hdr->result = EC_RES_SUCCESS;
  440. resp_hdr->data_len = len;
  441. resp_hdr->reserved = 0;
  442. len += sizeof(*resp_hdr);
  443. resp_hdr->checksum = 0;
  444. resp_hdr->checksum = (uint8_t)
  445. -cros_ec_calc_checksum((const uint8_t *)resp_hdr, len);
  446. return in_bytes;
  447. }
  448. void cros_ec_check_keyboard(struct cros_ec_dev *dev)
  449. {
  450. struct ec_state *ec = dev_get_priv(dev->dev);
  451. ulong start;
  452. printf("Press keys for EC to detect on reset (ESC=recovery)...");
  453. start = get_timer(0);
  454. while (get_timer(start) < 1000)
  455. ;
  456. putc('\n');
  457. if (!sandbox_sdl_key_pressed(KEY_ESC)) {
  458. ec->recovery_req = true;
  459. printf(" - EC requests recovery\n");
  460. }
  461. }
  462. int cros_ec_probe(struct udevice *dev)
  463. {
  464. struct ec_state *ec = dev->priv;
  465. struct cros_ec_dev *cdev = dev->uclass_priv;
  466. const void *blob = gd->fdt_blob;
  467. struct udevice *keyb_dev;
  468. int node;
  469. int err;
  470. memcpy(ec, &s_state, sizeof(*ec));
  471. err = cros_ec_decode_ec_flash(blob, dev->of_offset, &ec->ec_config);
  472. if (err)
  473. return err;
  474. node = -1;
  475. for (device_find_first_child(dev, &keyb_dev);
  476. keyb_dev;
  477. device_find_next_child(&keyb_dev)) {
  478. if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) {
  479. node = keyb_dev->of_offset;
  480. break;
  481. }
  482. }
  483. if (node < 0) {
  484. debug("%s: No cros_ec keyboard found\n", __func__);
  485. } else if (keyscan_read_fdt_matrix(ec, blob, node)) {
  486. debug("%s: Could not read key matrix\n", __func__);
  487. return -1;
  488. }
  489. /* If we loaded EC data, check that the length matches */
  490. if (ec->flash_data &&
  491. ec->flash_data_len != ec->ec_config.flash.length) {
  492. printf("EC data length is %x, expected %x, discarding data\n",
  493. ec->flash_data_len, ec->ec_config.flash.length);
  494. os_free(ec->flash_data);
  495. ec->flash_data = NULL;
  496. }
  497. /* Otherwise allocate the memory */
  498. if (!ec->flash_data) {
  499. ec->flash_data_len = ec->ec_config.flash.length;
  500. ec->flash_data = os_malloc(ec->flash_data_len);
  501. if (!ec->flash_data)
  502. return -ENOMEM;
  503. }
  504. cdev->dev = dev;
  505. g_state = ec;
  506. return cros_ec_register(dev);
  507. }
  508. struct dm_cros_ec_ops cros_ec_ops = {
  509. .packet = cros_ec_sandbox_packet,
  510. };
  511. static const struct udevice_id cros_ec_ids[] = {
  512. { .compatible = "google,cros-ec-sandbox" },
  513. { }
  514. };
  515. U_BOOT_DRIVER(cros_ec_sandbox) = {
  516. .name = "cros_ec_sandbox",
  517. .id = UCLASS_CROS_EC,
  518. .of_match = cros_ec_ids,
  519. .probe = cros_ec_probe,
  520. .priv_auto_alloc_size = sizeof(struct ec_state),
  521. .ops = &cros_ec_ops,
  522. };