cros_ec_sandbox.c 15 KB

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