1 // SPDX-License-Identifier: GPL-2.0+
3 * Chromium OS cros_ec driver - sandbox emulation
5 * Copyright (c) 2013 The Chromium OS Authors.
11 #include <ec_commands.h>
16 #include <u-boot/sha256.h>
18 #include <asm/malloc.h>
19 #include <asm/state.h>
22 #include <linux/input.h>
25 * Ultimately it shold be possible to connect an Chrome OS EC emulation
26 * to U-Boot and remove all of this code. But this provides a test
27 * environment for bringing up chromeos_sandbox and demonstrating its
30 * This emulation includes the following:
32 * 1. Emulation of the keyboard, by converting keypresses received from SDL
33 * into key scan data, passed back from the EC as key scan messages. The
34 * key layout is read from the device tree.
36 * 2. Emulation of vboot context - so this can be read/written as required.
38 * 3. Save/restore of EC state, so that the vboot context, flash memory
39 * contents and current image can be preserved across boots. This is important
40 * since the EC is supposed to continue running even if the AP resets.
42 * 4. Some event support, in particular allowing Escape to be pressed on boot
43 * to enter recovery mode. The EC passes this to U-Boot through the normal
46 * 5. Flash read/write/erase support, so that software sync works. The
47 * protect messages are supported but no protection is implemented.
49 * 6. Hashing of the EC image, again to support software sync.
51 * Other features can be added, although a better path is probably to link
52 * the EC image in with U-Boot (Vic has demonstrated a prototype for this).
55 #define KEYBOARD_ROWS 8
56 #define KEYBOARD_COLS 13
58 /* A single entry of the key matrix */
59 struct ec_keymatrix_entry {
60 int row; /* key matrix row */
61 int col; /* key matrix column */
62 int keycode; /* corresponding linux key code */
66 VSTORE_SLOT_COUNT = 4,
67 PWM_CHANNEL_COUNT = 4,
72 u8 data[EC_VSTORE_SLOT_SIZE];
75 struct ec_pwm_channel {
76 uint duty; /* not ns, EC_PWM_MAX_DUTY = 100% */
80 * struct ec_state - Information about the EC state
82 * @vbnv_context: Vboot context data stored by EC
83 * @ec_config: FDT config information about the EC (e.g. flashmap)
84 * @flash_data: Contents of flash memory
85 * @flash_data_len: Size of flash memory
86 * @current_image: Current image the EC is running
87 * @matrix_count: Number of keys to decode in matrix
88 * @matrix: Information about keyboard matrix
89 * @keyscan: Current keyscan information (bit set for each row/column pressed)
90 * @recovery_req: Keyboard recovery requested
91 * @test_flags: Flags that control behaviour for tests
92 * @slot_locked: Locked vstore slots (mask)
93 * @pwm: Information per PWM channel
96 u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2];
97 struct fdt_cros_ec ec_config;
100 enum ec_current_image current_image;
102 struct ec_keymatrix_entry *matrix; /* the key matrix info */
103 uint8_t keyscan[KEYBOARD_COLS];
106 struct vstore_slot slot[VSTORE_SLOT_COUNT];
107 struct ec_pwm_channel pwm[PWM_CHANNEL_COUNT];
111 * cros_ec_read_state() - read the sandbox EC state from the state file
113 * If data is available, then blob and node will provide access to it. If
114 * not this function sets up an empty EC.
116 * @param blob: Pointer to device tree blob, or NULL if no data to read
117 * @param node: Node offset to read from
119 static int cros_ec_read_state(const void *blob, int node)
121 struct ec_state *ec = &s_state;
125 /* Set everything to defaults */
126 ec->current_image = EC_IMAGE_RO;
130 /* Read the data if available */
131 ec->current_image = fdtdec_get_int(blob, node, "current-image",
133 prop = fdt_getprop(blob, node, "vbnv-context", &len);
134 if (prop && len == sizeof(ec->vbnv_context))
135 memcpy(ec->vbnv_context, prop, len);
137 prop = fdt_getprop(blob, node, "flash-data", &len);
139 ec->flash_data_len = len;
140 ec->flash_data = malloc(len);
143 memcpy(ec->flash_data, prop, len);
144 debug("%s: Loaded EC flash data size %#x\n", __func__, len);
151 * cros_ec_write_state() - Write out our state to the state file
153 * The caller will ensure that there is a node ready for the state. The node
154 * may already contain the old state, in which case it is overridden.
156 * @param blob: Device tree blob holding state
157 * @param node: Node to write our state into
159 static int cros_ec_write_state(void *blob, int node)
161 struct ec_state *ec = g_state;
166 /* We are guaranteed enough space to write basic properties */
167 fdt_setprop_u32(blob, node, "current-image", ec->current_image);
168 fdt_setprop(blob, node, "vbnv-context", ec->vbnv_context,
169 sizeof(ec->vbnv_context));
171 return state_setprop(node, "flash-data", ec->flash_data,
172 ec->ec_config.flash.length);
175 SANDBOX_STATE_IO(cros_ec, "google,cros-ec", cros_ec_read_state,
176 cros_ec_write_state);
179 * Return the number of bytes used in the specified image.
181 * This is the actual size of code+data in the image, as opposed to the
182 * amount of space reserved in flash for that image. This code is similar to
183 * that used by the real EC code base.
185 * @param ec Current emulated EC state
186 * @param entry Flash map entry containing the image to check
187 * @return actual image size in bytes, 0 if the image contains no content or
190 static int get_image_used(struct ec_state *ec, struct fmap_entry *entry)
195 * Scan backwards looking for 0xea byte, which is by definition the
196 * last byte of the image. See ec.lds.S for how this is inserted at
197 * the end of the image.
199 for (size = entry->length - 1;
200 size > 0 && ec->flash_data[entry->offset + size] != 0xea;
204 return size ? size + 1 : 0; /* 0xea byte IS part of the image */
208 * Read the key matrix from the device tree
210 * Keymap entries in the fdt take the form of 0xRRCCKKKK where
211 * RR=Row CC=Column KKKK=Key Code
213 * @param ec Current emulated EC state
214 * @param node Keyboard node of device tree containing keyscan information
215 * @return 0 if ok, -1 on error
217 static int keyscan_read_fdt_matrix(struct ec_state *ec, ofnode node)
223 cell = ofnode_get_property(node, "linux,keymap", &len);
224 ec->matrix_count = len / 4;
225 ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix));
227 debug("%s: Out of memory for key matrix\n", __func__);
231 /* Now read the data */
232 for (upto = 0; upto < ec->matrix_count; upto++) {
233 struct ec_keymatrix_entry *matrix = &ec->matrix[upto];
236 word = fdt32_to_cpu(*cell++);
237 matrix->row = word >> 24;
238 matrix->col = (word >> 16) & 0xff;
239 matrix->keycode = word & 0xffff;
241 /* Hard-code some sanity limits for now */
242 if (matrix->row >= KEYBOARD_ROWS ||
243 matrix->col >= KEYBOARD_COLS) {
244 debug("%s: Matrix pos out of range (%d,%d)\n",
245 __func__, matrix->row, matrix->col);
250 if (upto != ec->matrix_count) {
251 debug("%s: Read mismatch from key matrix\n", __func__);
259 * Return the next keyscan message contents
261 * @param ec Current emulated EC state
262 * @param scan Place to put keyscan bytes for the keyscan message (must hold
263 * enough space for a full keyscan)
264 * @return number of bytes of valid scan data
266 static int cros_ec_keyscan(struct ec_state *ec, uint8_t *scan)
268 const struct ec_keymatrix_entry *matrix;
269 int bytes = KEYBOARD_COLS;
270 int key[8]; /* allow up to 8 keys to be pressed at once */
274 memset(ec->keyscan, '\0', bytes);
275 count = sandbox_sdl_scan_keys(key, ARRAY_SIZE(key));
277 /* Look up keycode in matrix */
278 for (i = 0, matrix = ec->matrix; i < ec->matrix_count; i++, matrix++) {
282 for (found = false, j = 0; j < count; j++) {
283 if (matrix->keycode == key[j])
288 debug("%d: %d,%d\n", matrix->keycode, matrix->row,
290 ec->keyscan[matrix->col] |= 1 << matrix->row;
294 memcpy(scan, ec->keyscan, bytes);
299 * Process an emulated EC command
301 * @param ec Current emulated EC state
302 * @param req_hdr Pointer to request header
303 * @param req_data Pointer to body of request
304 * @param resp_hdr Pointer to place to put response header
305 * @param resp_data Pointer to place to put response data, if any
306 * @return length of response data, or 0 for no response data, or -1 on error
308 static int process_cmd(struct ec_state *ec,
309 struct ec_host_request *req_hdr, const void *req_data,
310 struct ec_host_response *resp_hdr, void *resp_data)
314 /* TODO(sjg@chromium.org): Check checksums */
315 debug("EC command %#0x\n", req_hdr->command);
317 switch (req_hdr->command) {
319 const struct ec_params_hello *req = req_data;
320 struct ec_response_hello *resp = resp_data;
322 resp->out_data = req->in_data + 0x01020304;
323 if (ec->test_flags & CROSECT_BREAK_HELLO)
328 case EC_CMD_GET_VERSION: {
329 struct ec_response_get_version *resp = resp_data;
331 strcpy(resp->version_string_ro, "sandbox_ro");
332 strcpy(resp->version_string_rw, "sandbox_rw");
333 resp->current_image = ec->current_image;
334 debug("Current image %d\n", resp->current_image);
338 case EC_CMD_VBNV_CONTEXT: {
339 const struct ec_params_vbnvcontext *req = req_data;
340 struct ec_response_vbnvcontext *resp = resp_data;
343 case EC_VBNV_CONTEXT_OP_READ:
344 /* TODO(sjg@chromium.org): Support full-size context */
345 memcpy(resp->block, ec->vbnv_context,
349 case EC_VBNV_CONTEXT_OP_WRITE:
350 /* TODO(sjg@chromium.org): Support full-size context */
351 memcpy(ec->vbnv_context, req->block,
356 printf(" ** Unknown vbnv_context command %#02x\n",
362 case EC_CMD_REBOOT_EC: {
363 const struct ec_params_reboot_ec *req = req_data;
365 printf("Request reboot type %d\n", req->cmd);
367 case EC_REBOOT_DISABLE_JUMP:
370 case EC_REBOOT_JUMP_RW:
371 ec->current_image = EC_IMAGE_RW;
375 puts(" ** Unknown type");
380 case EC_CMD_HOST_EVENT_GET_B: {
381 struct ec_response_host_event_mask *resp = resp_data;
384 if (ec->recovery_req) {
385 resp->mask |= EC_HOST_EVENT_MASK(
386 EC_HOST_EVENT_KEYBOARD_RECOVERY);
388 if (ec->test_flags & CROSECT_LID_OPEN)
390 EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN);
394 case EC_CMD_HOST_EVENT_CLEAR_B: {
395 const struct ec_params_host_event_mask *req = req_data;
397 if (req->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN))
398 ec->test_flags &= ~CROSECT_LID_OPEN;
402 case EC_CMD_VBOOT_HASH: {
403 const struct ec_params_vboot_hash *req = req_data;
404 struct ec_response_vboot_hash *resp = resp_data;
405 struct fmap_entry *entry;
408 entry = &ec->ec_config.region[EC_FLASH_REGION_ACTIVE];
411 case EC_VBOOT_HASH_RECALC:
412 case EC_VBOOT_HASH_GET:
413 size = SHA256_SUM_LEN;
414 len = get_image_used(ec, entry);
415 ret = hash_block("sha256",
416 ec->flash_data + entry->offset,
417 len, resp->hash_digest, &size);
419 printf(" ** hash_block() failed\n");
422 resp->status = EC_VBOOT_HASH_STATUS_DONE;
423 resp->hash_type = EC_VBOOT_HASH_TYPE_SHA256;
424 resp->digest_size = size;
426 resp->offset = entry->offset;
431 printf(" ** EC_CMD_VBOOT_HASH: Unknown command %d\n",
437 case EC_CMD_FLASH_PROTECT: {
438 const struct ec_params_flash_protect *req = req_data;
439 struct ec_response_flash_protect *resp = resp_data;
440 uint32_t expect = EC_FLASH_PROTECT_ALL_NOW |
441 EC_FLASH_PROTECT_ALL_AT_BOOT;
443 printf("mask=%#x, flags=%#x\n", req->mask, req->flags);
444 if (req->flags == expect || req->flags == 0) {
445 resp->flags = req->flags ? EC_FLASH_PROTECT_ALL_NOW :
447 resp->valid_flags = EC_FLASH_PROTECT_ALL_NOW;
448 resp->writable_flags = 0;
451 puts(" ** unexpected flash protect request\n");
456 case EC_CMD_FLASH_REGION_INFO: {
457 const struct ec_params_flash_region_info *req = req_data;
458 struct ec_response_flash_region_info *resp = resp_data;
459 struct fmap_entry *entry;
461 switch (req->region) {
462 case EC_FLASH_REGION_RO:
463 case EC_FLASH_REGION_ACTIVE:
464 case EC_FLASH_REGION_WP_RO:
465 entry = &ec->ec_config.region[req->region];
466 resp->offset = entry->offset;
467 resp->size = entry->length;
469 printf("EC flash region %d: offset=%#x, size=%#x\n",
470 req->region, resp->offset, resp->size);
473 printf("** Unknown flash region %d\n", req->region);
478 case EC_CMD_FLASH_ERASE: {
479 const struct ec_params_flash_erase *req = req_data;
481 memset(ec->flash_data + req->offset,
482 ec->ec_config.flash_erase_value,
487 case EC_CMD_FLASH_WRITE: {
488 const struct ec_params_flash_write *req = req_data;
490 memcpy(ec->flash_data + req->offset, req + 1, req->size);
494 case EC_CMD_MKBP_STATE:
495 len = cros_ec_keyscan(ec, resp_data);
497 case EC_CMD_ENTERING_MODE:
500 case EC_CMD_GET_NEXT_EVENT: {
501 struct ec_response_get_next_event *resp = resp_data;
503 resp->event_type = EC_MKBP_EVENT_KEY_MATRIX;
504 cros_ec_keyscan(ec, resp->data.key_matrix);
508 case EC_CMD_GET_SKU_ID: {
509 struct ec_sku_id_info *resp = resp_data;
515 case EC_CMD_GET_FEATURES: {
516 struct ec_response_get_features *resp = resp_data;
518 resp->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FLASH) |
519 EC_FEATURE_MASK_0(EC_FEATURE_I2C) |
520 EC_FEATURE_MASK_0(EC_FEATURE_VSTORE);
522 EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS) |
523 EC_FEATURE_MASK_1(EC_FEATURE_ISH);
527 case EC_CMD_VSTORE_INFO: {
528 struct ec_response_vstore_info *resp = resp_data;
531 resp->slot_count = VSTORE_SLOT_COUNT;
532 resp->slot_locked = 0;
533 for (i = 0; i < VSTORE_SLOT_COUNT; i++) {
534 if (ec->slot[i].locked)
535 resp->slot_locked |= 1 << i;
540 case EC_CMD_VSTORE_WRITE: {
541 const struct ec_params_vstore_write *req = req_data;
542 struct vstore_slot *slot;
544 if (req->slot >= EC_VSTORE_SLOT_MAX)
546 slot = &ec->slot[req->slot];
548 memcpy(slot->data, req->data, EC_VSTORE_SLOT_SIZE);
552 case EC_CMD_VSTORE_READ: {
553 const struct ec_params_vstore_read *req = req_data;
554 struct ec_response_vstore_read *resp = resp_data;
555 struct vstore_slot *slot;
557 if (req->slot >= EC_VSTORE_SLOT_MAX)
559 slot = &ec->slot[req->slot];
560 memcpy(resp->data, slot->data, EC_VSTORE_SLOT_SIZE);
564 case EC_CMD_PWM_GET_DUTY: {
565 const struct ec_params_pwm_get_duty *req = req_data;
566 struct ec_response_pwm_get_duty *resp = resp_data;
567 struct ec_pwm_channel *pwm;
569 if (req->pwm_type != EC_PWM_TYPE_GENERIC)
571 if (req->index >= PWM_CHANNEL_COUNT)
573 pwm = &ec->pwm[req->index];
574 resp->duty = pwm->duty;
578 case EC_CMD_PWM_SET_DUTY: {
579 const struct ec_params_pwm_set_duty *req = req_data;
580 struct ec_pwm_channel *pwm;
582 if (req->pwm_type != EC_PWM_TYPE_GENERIC)
584 if (req->index >= PWM_CHANNEL_COUNT)
586 pwm = &ec->pwm[req->index];
587 pwm->duty = req->duty;
592 printf(" ** Unknown EC command %#02x\n", req_hdr->command);
599 int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
601 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
602 struct ec_state *ec = dev_get_priv(dev->dev);
603 struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout;
604 const void *req_data = req_hdr + 1;
605 struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din;
606 void *resp_data = resp_hdr + 1;
609 len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data);
613 resp_hdr->struct_version = 3;
614 resp_hdr->result = EC_RES_SUCCESS;
615 resp_hdr->data_len = len;
616 resp_hdr->reserved = 0;
617 len += sizeof(*resp_hdr);
618 resp_hdr->checksum = 0;
619 resp_hdr->checksum = (uint8_t)
620 -cros_ec_calc_checksum((const uint8_t *)resp_hdr, len);
625 void cros_ec_check_keyboard(struct udevice *dev)
627 struct ec_state *ec = dev_get_priv(dev);
630 printf("Press keys for EC to detect on reset (ESC=recovery)...");
631 start = get_timer(0);
632 while (get_timer(start) < 1000)
635 if (!sandbox_sdl_key_pressed(KEY_ESC)) {
636 ec->recovery_req = true;
637 printf(" - EC requests recovery\n");
641 /* Return the byte of EC switch states */
642 static int cros_ec_sandbox_get_switches(struct udevice *dev)
644 struct ec_state *ec = dev_get_priv(dev);
646 return ec->test_flags & CROSECT_LID_OPEN ? EC_SWITCH_LID_OPEN : 0;
649 void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags)
651 struct ec_state *ec = dev_get_priv(dev);
653 ec->test_flags = flags;
656 int sandbox_cros_ec_get_pwm_duty(struct udevice *dev, uint index, uint *duty)
658 struct ec_state *ec = dev_get_priv(dev);
659 struct ec_pwm_channel *pwm;
661 if (index >= PWM_CHANNEL_COUNT)
663 pwm = &ec->pwm[index];
669 int cros_ec_probe(struct udevice *dev)
671 struct ec_state *ec = dev_get_priv(dev);
672 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
673 struct udevice *keyb_dev;
677 memcpy(ec, &s_state, sizeof(*ec));
678 err = cros_ec_decode_ec_flash(dev, &ec->ec_config);
680 debug("%s: Cannot device EC flash\n", __func__);
684 node = ofnode_null();
685 for (device_find_first_child(dev, &keyb_dev);
687 device_find_next_child(&keyb_dev)) {
688 if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) {
689 node = dev_ofnode(keyb_dev);
693 if (!ofnode_valid(node)) {
694 debug("%s: No cros_ec keyboard found\n", __func__);
695 } else if (keyscan_read_fdt_matrix(ec, node)) {
696 debug("%s: Could not read key matrix\n", __func__);
700 /* If we loaded EC data, check that the length matches */
701 if (ec->flash_data &&
702 ec->flash_data_len != ec->ec_config.flash.length) {
703 printf("EC data length is %x, expected %x, discarding data\n",
704 ec->flash_data_len, ec->ec_config.flash.length);
705 free(ec->flash_data);
706 ec->flash_data = NULL;
709 /* Otherwise allocate the memory */
710 if (!ec->flash_data) {
711 ec->flash_data_len = ec->ec_config.flash.length;
712 ec->flash_data = malloc(ec->flash_data_len);
719 return cros_ec_register(dev);
722 struct dm_cros_ec_ops cros_ec_ops = {
723 .packet = cros_ec_sandbox_packet,
724 .get_switches = cros_ec_sandbox_get_switches,
727 static const struct udevice_id cros_ec_ids[] = {
728 { .compatible = "google,cros-ec-sandbox" },
732 U_BOOT_DRIVER(google_cros_ec_sandbox) = {
733 .name = "google_cros_ec_sandbox",
734 .id = UCLASS_CROS_EC,
735 .of_match = cros_ec_ids,
736 .probe = cros_ec_probe,
737 .priv_auto = sizeof(struct ec_state),