cros_ec_sandbox.c 18 KB

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