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