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 | |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 65 | enum { |
| 66 | VSTORE_SLOT_COUNT = 4, |
Alper Nebi Yasak | 8a8cd4f | 2021-05-19 19:33:31 +0300 | [diff] [blame^] | 67 | PWM_CHANNEL_COUNT = 4, |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct vstore_slot { |
| 71 | bool locked; |
| 72 | u8 data[EC_VSTORE_SLOT_SIZE]; |
| 73 | }; |
| 74 | |
Alper Nebi Yasak | 8a8cd4f | 2021-05-19 19:33:31 +0300 | [diff] [blame^] | 75 | struct ec_pwm_channel { |
| 76 | uint duty; /* not ns, EC_PWM_MAX_DUTY = 100% */ |
| 77 | }; |
| 78 | |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 79 | /** |
| 80 | * struct ec_state - Information about the EC state |
| 81 | * |
| 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 |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 91 | * @test_flags: Flags that control behaviour for tests |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 92 | * @slot_locked: Locked vstore slots (mask) |
Alper Nebi Yasak | 8a8cd4f | 2021-05-19 19:33:31 +0300 | [diff] [blame^] | 93 | * @pwm: Information per PWM channel |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 94 | */ |
| 95 | struct ec_state { |
Simon Glass | 153f252 | 2018-11-06 15:21:22 -0700 | [diff] [blame] | 96 | u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2]; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 97 | struct fdt_cros_ec ec_config; |
| 98 | uint8_t *flash_data; |
| 99 | int flash_data_len; |
| 100 | enum ec_current_image current_image; |
| 101 | int matrix_count; |
| 102 | struct ec_keymatrix_entry *matrix; /* the key matrix info */ |
| 103 | uint8_t keyscan[KEYBOARD_COLS]; |
| 104 | bool recovery_req; |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 105 | uint test_flags; |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 106 | struct vstore_slot slot[VSTORE_SLOT_COUNT]; |
Alper Nebi Yasak | 8a8cd4f | 2021-05-19 19:33:31 +0300 | [diff] [blame^] | 107 | struct ec_pwm_channel pwm[PWM_CHANNEL_COUNT]; |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 108 | } s_state, *g_state; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * cros_ec_read_state() - read the sandbox EC state from the state file |
| 112 | * |
| 113 | * If data is available, then blob and node will provide access to it. If |
| 114 | * not this function sets up an empty EC. |
| 115 | * |
| 116 | * @param blob: Pointer to device tree blob, or NULL if no data to read |
| 117 | * @param node: Node offset to read from |
| 118 | */ |
| 119 | static int cros_ec_read_state(const void *blob, int node) |
| 120 | { |
| 121 | struct ec_state *ec = &s_state; |
| 122 | const char *prop; |
| 123 | int len; |
| 124 | |
| 125 | /* Set everything to defaults */ |
| 126 | ec->current_image = EC_IMAGE_RO; |
| 127 | if (!blob) |
| 128 | return 0; |
| 129 | |
| 130 | /* Read the data if available */ |
| 131 | ec->current_image = fdtdec_get_int(blob, node, "current-image", |
| 132 | EC_IMAGE_RO); |
| 133 | prop = fdt_getprop(blob, node, "vbnv-context", &len); |
| 134 | if (prop && len == sizeof(ec->vbnv_context)) |
| 135 | memcpy(ec->vbnv_context, prop, len); |
| 136 | |
| 137 | prop = fdt_getprop(blob, node, "flash-data", &len); |
| 138 | if (prop) { |
| 139 | ec->flash_data_len = len; |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 140 | ec->flash_data = malloc(len); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 141 | if (!ec->flash_data) |
| 142 | return -ENOMEM; |
| 143 | memcpy(ec->flash_data, prop, len); |
| 144 | debug("%s: Loaded EC flash data size %#x\n", __func__, len); |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * cros_ec_write_state() - Write out our state to the state file |
| 152 | * |
| 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. |
| 155 | * |
| 156 | * @param blob: Device tree blob holding state |
| 157 | * @param node: Node to write our state into |
| 158 | */ |
| 159 | static int cros_ec_write_state(void *blob, int node) |
| 160 | { |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 161 | struct ec_state *ec = g_state; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 162 | |
Simon Glass | c747f8b | 2021-03-15 18:11:08 +1300 | [diff] [blame] | 163 | if (!g_state) |
| 164 | return 0; |
| 165 | |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 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)); |
Simon Glass | c747f8b | 2021-03-15 18:11:08 +1300 | [diff] [blame] | 170 | |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 171 | return state_setprop(node, "flash-data", ec->flash_data, |
| 172 | ec->ec_config.flash.length); |
| 173 | } |
| 174 | |
| 175 | SANDBOX_STATE_IO(cros_ec, "google,cros-ec", cros_ec_read_state, |
| 176 | cros_ec_write_state); |
| 177 | |
| 178 | /** |
| 179 | * Return the number of bytes used in the specified image. |
| 180 | * |
| 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. |
| 184 | * |
| 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 |
| 188 | * error. |
| 189 | */ |
| 190 | static int get_image_used(struct ec_state *ec, struct fmap_entry *entry) |
| 191 | { |
| 192 | int size; |
| 193 | |
| 194 | /* |
| 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. |
| 198 | */ |
| 199 | for (size = entry->length - 1; |
| 200 | size > 0 && ec->flash_data[entry->offset + size] != 0xea; |
| 201 | size--) |
| 202 | ; |
| 203 | |
| 204 | return size ? size + 1 : 0; /* 0xea byte IS part of the image */ |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Read the key matrix from the device tree |
| 209 | * |
| 210 | * Keymap entries in the fdt take the form of 0xRRCCKKKK where |
| 211 | * RR=Row CC=Column KKKK=Key Code |
| 212 | * |
| 213 | * @param ec Current emulated EC state |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 214 | * @param node Keyboard node of device tree containing keyscan information |
| 215 | * @return 0 if ok, -1 on error |
| 216 | */ |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 217 | static int keyscan_read_fdt_matrix(struct ec_state *ec, ofnode node) |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 218 | { |
| 219 | const u32 *cell; |
| 220 | int upto; |
| 221 | int len; |
| 222 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 223 | cell = ofnode_get_property(node, "linux,keymap", &len); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 224 | ec->matrix_count = len / 4; |
| 225 | ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix)); |
| 226 | if (!ec->matrix) { |
| 227 | debug("%s: Out of memory for key matrix\n", __func__); |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | /* Now read the data */ |
| 232 | for (upto = 0; upto < ec->matrix_count; upto++) { |
| 233 | struct ec_keymatrix_entry *matrix = &ec->matrix[upto]; |
| 234 | u32 word; |
| 235 | |
| 236 | word = fdt32_to_cpu(*cell++); |
| 237 | matrix->row = word >> 24; |
| 238 | matrix->col = (word >> 16) & 0xff; |
| 239 | matrix->keycode = word & 0xffff; |
| 240 | |
| 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); |
| 246 | return -1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (upto != ec->matrix_count) { |
| 251 | debug("%s: Read mismatch from key matrix\n", __func__); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Return the next keyscan message contents |
| 260 | * |
| 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 |
| 265 | */ |
| 266 | static int cros_ec_keyscan(struct ec_state *ec, uint8_t *scan) |
| 267 | { |
| 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 */ |
| 271 | int count; |
| 272 | int i; |
| 273 | |
| 274 | memset(ec->keyscan, '\0', bytes); |
| 275 | count = sandbox_sdl_scan_keys(key, ARRAY_SIZE(key)); |
| 276 | |
| 277 | /* Look up keycode in matrix */ |
| 278 | for (i = 0, matrix = ec->matrix; i < ec->matrix_count; i++, matrix++) { |
| 279 | bool found; |
| 280 | int j; |
| 281 | |
| 282 | for (found = false, j = 0; j < count; j++) { |
| 283 | if (matrix->keycode == key[j]) |
| 284 | found = true; |
| 285 | } |
| 286 | |
| 287 | if (found) { |
| 288 | debug("%d: %d,%d\n", matrix->keycode, matrix->row, |
| 289 | matrix->col); |
| 290 | ec->keyscan[matrix->col] |= 1 << matrix->row; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | memcpy(scan, ec->keyscan, bytes); |
| 295 | return bytes; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Process an emulated EC command |
| 300 | * |
| 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 |
| 307 | */ |
| 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) |
| 311 | { |
| 312 | int len; |
| 313 | |
| 314 | /* TODO(sjg@chromium.org): Check checksums */ |
| 315 | debug("EC command %#0x\n", req_hdr->command); |
| 316 | |
| 317 | switch (req_hdr->command) { |
| 318 | case EC_CMD_HELLO: { |
| 319 | const struct ec_params_hello *req = req_data; |
| 320 | struct ec_response_hello *resp = resp_data; |
| 321 | |
| 322 | resp->out_data = req->in_data + 0x01020304; |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 323 | if (ec->test_flags & CROSECT_BREAK_HELLO) |
| 324 | resp->out_data++; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 325 | len = sizeof(*resp); |
| 326 | break; |
| 327 | } |
| 328 | case EC_CMD_GET_VERSION: { |
| 329 | struct ec_response_get_version *resp = resp_data; |
| 330 | |
| 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); |
| 335 | len = sizeof(*resp); |
| 336 | break; |
| 337 | } |
| 338 | case EC_CMD_VBNV_CONTEXT: { |
| 339 | const struct ec_params_vbnvcontext *req = req_data; |
| 340 | struct ec_response_vbnvcontext *resp = resp_data; |
| 341 | |
| 342 | switch (req->op) { |
| 343 | case EC_VBNV_CONTEXT_OP_READ: |
Simon Glass | ea2cf05 | 2018-11-23 21:29:37 -0700 | [diff] [blame] | 344 | /* TODO(sjg@chromium.org): Support full-size context */ |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 345 | memcpy(resp->block, ec->vbnv_context, |
Simon Glass | ea2cf05 | 2018-11-23 21:29:37 -0700 | [diff] [blame] | 346 | EC_VBNV_BLOCK_SIZE); |
| 347 | len = 16; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 348 | break; |
| 349 | case EC_VBNV_CONTEXT_OP_WRITE: |
Simon Glass | ea2cf05 | 2018-11-23 21:29:37 -0700 | [diff] [blame] | 350 | /* TODO(sjg@chromium.org): Support full-size context */ |
| 351 | memcpy(ec->vbnv_context, req->block, |
| 352 | EC_VBNV_BLOCK_SIZE); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 353 | len = 0; |
| 354 | break; |
| 355 | default: |
| 356 | printf(" ** Unknown vbnv_context command %#02x\n", |
| 357 | req->op); |
| 358 | return -1; |
| 359 | } |
| 360 | break; |
| 361 | } |
| 362 | case EC_CMD_REBOOT_EC: { |
| 363 | const struct ec_params_reboot_ec *req = req_data; |
| 364 | |
| 365 | printf("Request reboot type %d\n", req->cmd); |
| 366 | switch (req->cmd) { |
| 367 | case EC_REBOOT_DISABLE_JUMP: |
| 368 | len = 0; |
| 369 | break; |
| 370 | case EC_REBOOT_JUMP_RW: |
| 371 | ec->current_image = EC_IMAGE_RW; |
| 372 | len = 0; |
| 373 | break; |
| 374 | default: |
| 375 | puts(" ** Unknown type"); |
| 376 | return -1; |
| 377 | } |
| 378 | break; |
| 379 | } |
| 380 | case EC_CMD_HOST_EVENT_GET_B: { |
| 381 | struct ec_response_host_event_mask *resp = resp_data; |
| 382 | |
| 383 | resp->mask = 0; |
| 384 | if (ec->recovery_req) { |
| 385 | resp->mask |= EC_HOST_EVENT_MASK( |
| 386 | EC_HOST_EVENT_KEYBOARD_RECOVERY); |
| 387 | } |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame] | 388 | if (ec->test_flags & CROSECT_LID_OPEN) |
| 389 | resp->mask |= |
| 390 | EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 391 | len = sizeof(*resp); |
| 392 | break; |
| 393 | } |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame] | 394 | case EC_CMD_HOST_EVENT_CLEAR_B: { |
| 395 | const struct ec_params_host_event_mask *req = req_data; |
| 396 | |
| 397 | if (req->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN)) |
| 398 | ec->test_flags &= ~CROSECT_LID_OPEN; |
| 399 | len = 0; |
| 400 | break; |
| 401 | } |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 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; |
| 406 | int ret, size; |
| 407 | |
Simon Glass | 2c1e550 | 2018-10-01 12:22:36 -0600 | [diff] [blame] | 408 | entry = &ec->ec_config.region[EC_FLASH_REGION_ACTIVE]; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 409 | |
| 410 | switch (req->cmd) { |
| 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); |
| 418 | if (ret) { |
| 419 | printf(" ** hash_block() failed\n"); |
| 420 | return -1; |
| 421 | } |
| 422 | resp->status = EC_VBOOT_HASH_STATUS_DONE; |
| 423 | resp->hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 424 | resp->digest_size = size; |
| 425 | resp->reserved0 = 0; |
| 426 | resp->offset = entry->offset; |
| 427 | resp->size = len; |
| 428 | len = sizeof(*resp); |
| 429 | break; |
| 430 | default: |
| 431 | printf(" ** EC_CMD_VBOOT_HASH: Unknown command %d\n", |
| 432 | req->cmd); |
| 433 | return -1; |
| 434 | } |
| 435 | break; |
| 436 | } |
| 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; |
| 442 | |
| 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 : |
| 446 | 0; |
| 447 | resp->valid_flags = EC_FLASH_PROTECT_ALL_NOW; |
| 448 | resp->writable_flags = 0; |
| 449 | len = sizeof(*resp); |
| 450 | } else { |
| 451 | puts(" ** unexpected flash protect request\n"); |
| 452 | return -1; |
| 453 | } |
| 454 | break; |
| 455 | } |
| 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; |
| 460 | |
| 461 | switch (req->region) { |
| 462 | case EC_FLASH_REGION_RO: |
Simon Glass | 2c1e550 | 2018-10-01 12:22:36 -0600 | [diff] [blame] | 463 | case EC_FLASH_REGION_ACTIVE: |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 464 | case EC_FLASH_REGION_WP_RO: |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 465 | entry = &ec->ec_config.region[req->region]; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 466 | resp->offset = entry->offset; |
| 467 | resp->size = entry->length; |
| 468 | len = sizeof(*resp); |
| 469 | printf("EC flash region %d: offset=%#x, size=%#x\n", |
| 470 | req->region, resp->offset, resp->size); |
| 471 | break; |
| 472 | default: |
| 473 | printf("** Unknown flash region %d\n", req->region); |
| 474 | return -1; |
| 475 | } |
| 476 | break; |
| 477 | } |
| 478 | case EC_CMD_FLASH_ERASE: { |
| 479 | const struct ec_params_flash_erase *req = req_data; |
| 480 | |
| 481 | memset(ec->flash_data + req->offset, |
| 482 | ec->ec_config.flash_erase_value, |
| 483 | req->size); |
| 484 | len = 0; |
| 485 | break; |
| 486 | } |
| 487 | case EC_CMD_FLASH_WRITE: { |
| 488 | const struct ec_params_flash_write *req = req_data; |
| 489 | |
| 490 | memcpy(ec->flash_data + req->offset, req + 1, req->size); |
| 491 | len = 0; |
| 492 | break; |
| 493 | } |
| 494 | case EC_CMD_MKBP_STATE: |
| 495 | len = cros_ec_keyscan(ec, resp_data); |
| 496 | break; |
Simon Glass | ee67a94 | 2015-05-04 11:31:09 -0600 | [diff] [blame] | 497 | case EC_CMD_ENTERING_MODE: |
Daniel Schwierzeck | bfb744c | 2015-11-06 14:15:37 +0100 | [diff] [blame] | 498 | len = 0; |
Simon Glass | ee67a94 | 2015-05-04 11:31:09 -0600 | [diff] [blame] | 499 | break; |
Alper Nebi Yasak | 5370d83 | 2020-11-14 01:01:42 +0300 | [diff] [blame] | 500 | case EC_CMD_GET_NEXT_EVENT: { |
| 501 | struct ec_response_get_next_event *resp = resp_data; |
| 502 | |
| 503 | resp->event_type = EC_MKBP_EVENT_KEY_MATRIX; |
| 504 | cros_ec_keyscan(ec, resp->data.key_matrix); |
| 505 | len = sizeof(*resp); |
| 506 | break; |
| 507 | } |
Simon Glass | 33909b5 | 2021-01-16 14:52:25 -0700 | [diff] [blame] | 508 | case EC_CMD_GET_SKU_ID: { |
| 509 | struct ec_sku_id_info *resp = resp_data; |
| 510 | |
| 511 | resp->sku_id = 1234; |
| 512 | len = sizeof(*resp); |
| 513 | break; |
| 514 | } |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 515 | case EC_CMD_GET_FEATURES: { |
| 516 | struct ec_response_get_features *resp = resp_data; |
| 517 | |
| 518 | resp->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FLASH) | |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 519 | EC_FEATURE_MASK_0(EC_FEATURE_I2C) | |
| 520 | EC_FEATURE_MASK_0(EC_FEATURE_VSTORE); |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 521 | resp->flags[1] = |
| 522 | EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS) | |
| 523 | EC_FEATURE_MASK_1(EC_FEATURE_ISH); |
| 524 | len = sizeof(*resp); |
| 525 | break; |
| 526 | } |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 527 | case EC_CMD_VSTORE_INFO: { |
| 528 | struct ec_response_vstore_info *resp = resp_data; |
| 529 | int i; |
| 530 | |
| 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; |
| 536 | } |
| 537 | len = sizeof(*resp); |
| 538 | break; |
| 539 | }; |
| 540 | case EC_CMD_VSTORE_WRITE: { |
| 541 | const struct ec_params_vstore_write *req = req_data; |
| 542 | struct vstore_slot *slot; |
| 543 | |
| 544 | if (req->slot >= EC_VSTORE_SLOT_MAX) |
| 545 | return -EINVAL; |
| 546 | slot = &ec->slot[req->slot]; |
| 547 | slot->locked = true; |
| 548 | memcpy(slot->data, req->data, EC_VSTORE_SLOT_SIZE); |
| 549 | len = 0; |
| 550 | break; |
| 551 | } |
| 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; |
| 556 | |
| 557 | if (req->slot >= EC_VSTORE_SLOT_MAX) |
| 558 | return -EINVAL; |
| 559 | slot = &ec->slot[req->slot]; |
| 560 | memcpy(resp->data, slot->data, EC_VSTORE_SLOT_SIZE); |
| 561 | len = sizeof(*resp); |
| 562 | break; |
| 563 | } |
Alper Nebi Yasak | 8a8cd4f | 2021-05-19 19:33:31 +0300 | [diff] [blame^] | 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; |
| 568 | |
| 569 | if (req->pwm_type != EC_PWM_TYPE_GENERIC) |
| 570 | return -EINVAL; |
| 571 | if (req->index >= PWM_CHANNEL_COUNT) |
| 572 | return -EINVAL; |
| 573 | pwm = &ec->pwm[req->index]; |
| 574 | resp->duty = pwm->duty; |
| 575 | len = sizeof(*resp); |
| 576 | break; |
| 577 | } |
| 578 | case EC_CMD_PWM_SET_DUTY: { |
| 579 | const struct ec_params_pwm_set_duty *req = req_data; |
| 580 | struct ec_pwm_channel *pwm; |
| 581 | |
| 582 | if (req->pwm_type != EC_PWM_TYPE_GENERIC) |
| 583 | return -EINVAL; |
| 584 | if (req->index >= PWM_CHANNEL_COUNT) |
| 585 | return -EINVAL; |
| 586 | pwm = &ec->pwm[req->index]; |
| 587 | pwm->duty = req->duty; |
| 588 | len = 0; |
| 589 | break; |
| 590 | } |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 591 | default: |
| 592 | printf(" ** Unknown EC command %#02x\n", req_hdr->command); |
| 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | return len; |
| 597 | } |
| 598 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 599 | int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes) |
| 600 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 601 | struct cros_ec_dev *dev = dev_get_uclass_priv(udev); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 602 | struct ec_state *ec = dev_get_priv(dev->dev); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 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; |
| 607 | int len; |
| 608 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 609 | len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 610 | if (len < 0) |
| 611 | return len; |
| 612 | |
| 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); |
| 621 | |
| 622 | return in_bytes; |
| 623 | } |
| 624 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 625 | void cros_ec_check_keyboard(struct udevice *dev) |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 626 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 627 | struct ec_state *ec = dev_get_priv(dev); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 628 | ulong start; |
| 629 | |
| 630 | printf("Press keys for EC to detect on reset (ESC=recovery)..."); |
| 631 | start = get_timer(0); |
| 632 | while (get_timer(start) < 1000) |
| 633 | ; |
| 634 | putc('\n'); |
| 635 | if (!sandbox_sdl_key_pressed(KEY_ESC)) { |
| 636 | ec->recovery_req = true; |
| 637 | printf(" - EC requests recovery\n"); |
| 638 | } |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 639 | } |
| 640 | |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 641 | /* Return the byte of EC switch states */ |
| 642 | static int cros_ec_sandbox_get_switches(struct udevice *dev) |
| 643 | { |
| 644 | struct ec_state *ec = dev_get_priv(dev); |
| 645 | |
| 646 | return ec->test_flags & CROSECT_LID_OPEN ? EC_SWITCH_LID_OPEN : 0; |
| 647 | } |
| 648 | |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 649 | void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags) |
| 650 | { |
| 651 | struct ec_state *ec = dev_get_priv(dev); |
| 652 | |
| 653 | ec->test_flags = flags; |
| 654 | } |
| 655 | |
Alper Nebi Yasak | 8a8cd4f | 2021-05-19 19:33:31 +0300 | [diff] [blame^] | 656 | int sandbox_cros_ec_get_pwm_duty(struct udevice *dev, uint index, uint *duty) |
| 657 | { |
| 658 | struct ec_state *ec = dev_get_priv(dev); |
| 659 | struct ec_pwm_channel *pwm; |
| 660 | |
| 661 | if (index >= PWM_CHANNEL_COUNT) |
| 662 | return -ENOSPC; |
| 663 | pwm = &ec->pwm[index]; |
| 664 | *duty = pwm->duty; |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 669 | int cros_ec_probe(struct udevice *dev) |
| 670 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 671 | struct ec_state *ec = dev_get_priv(dev); |
| 672 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Simon Glass | 3e5a8b9 | 2016-06-19 17:33:15 -0600 | [diff] [blame] | 673 | struct udevice *keyb_dev; |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 674 | ofnode node; |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 675 | int err; |
| 676 | |
| 677 | memcpy(ec, &s_state, sizeof(*ec)); |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 678 | err = cros_ec_decode_ec_flash(dev, &ec->ec_config); |
| 679 | if (err) { |
| 680 | debug("%s: Cannot device EC flash\n", __func__); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 681 | return err; |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 682 | } |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 683 | |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 684 | node = ofnode_null(); |
Simon Glass | 3e5a8b9 | 2016-06-19 17:33:15 -0600 | [diff] [blame] | 685 | for (device_find_first_child(dev, &keyb_dev); |
| 686 | keyb_dev; |
| 687 | device_find_next_child(&keyb_dev)) { |
| 688 | if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) { |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 689 | node = dev_ofnode(keyb_dev); |
Simon Glass | 3e5a8b9 | 2016-06-19 17:33:15 -0600 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | } |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 693 | if (!ofnode_valid(node)) { |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 694 | debug("%s: No cros_ec keyboard found\n", __func__); |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 695 | } else if (keyscan_read_fdt_matrix(ec, node)) { |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 696 | debug("%s: Could not read key matrix\n", __func__); |
| 697 | return -1; |
| 698 | } |
| 699 | |
| 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); |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 705 | free(ec->flash_data); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 706 | ec->flash_data = NULL; |
| 707 | } |
| 708 | |
| 709 | /* Otherwise allocate the memory */ |
| 710 | if (!ec->flash_data) { |
| 711 | ec->flash_data_len = ec->ec_config.flash.length; |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 712 | ec->flash_data = malloc(ec->flash_data_len); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 713 | if (!ec->flash_data) |
| 714 | return -ENOMEM; |
| 715 | } |
| 716 | |
| 717 | cdev->dev = dev; |
| 718 | g_state = ec; |
| 719 | return cros_ec_register(dev); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 720 | } |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 721 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 722 | struct dm_cros_ec_ops cros_ec_ops = { |
| 723 | .packet = cros_ec_sandbox_packet, |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 724 | .get_switches = cros_ec_sandbox_get_switches, |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | static const struct udevice_id cros_ec_ids[] = { |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 728 | { .compatible = "google,cros-ec-sandbox" }, |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 729 | { } |
| 730 | }; |
| 731 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 732 | U_BOOT_DRIVER(google_cros_ec_sandbox) = { |
| 733 | .name = "google_cros_ec_sandbox", |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 734 | .id = UCLASS_CROS_EC, |
| 735 | .of_match = cros_ec_ids, |
| 736 | .probe = cros_ec_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 737 | .priv_auto = sizeof(struct ec_state), |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 738 | .ops = &cros_ec_ops, |
| 739 | }; |