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