-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcros_ec_dev.c
More file actions
612 lines (530 loc) · 16.1 KB
/
cros_ec_dev.c
File metadata and controls
612 lines (530 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
* This file is part of the flashrom project.
*
* Copyright (C) 2012 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Google or the names of contributors or
* licensors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
* GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE
* FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
* GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
* OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
* LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/ioctl.h>
#include <linux/types.h>
#include "flash.h"
#include "cros_ec_commands.h"
#include "cros_ec.h"
#include "programmer.h"
#define CROS_EC_DEV_PREFIX "/dev/cros_"
#define CROS_EC_COMMAND_RETRIES 50
int cros_ec_fd; /* File descriptor for kernel device */
/* The names of the different device that can be found in a machine. */
static const char *ec_type[] = {
"ec",
"pd",
"sh",
"fp",
"tp",
};
/*
* @version: Command version number (often 0)
* @command: Command to send (EC_CMD_...)
* @outdata: Outgoing data to EC
* @outsize: Outgoing length in bytes
* @indata: Where to put the incoming data from EC
* @insize: Incoming length in bytes (filled in by EC)
* @result: EC's response to the command (separate from communication failure)
*/
struct cros_ec_command {
uint32_t version;
uint32_t command;
const uint8_t *outdata;
uint32_t outsize;
uint8_t *indata;
uint32_t insize;
uint32_t result;
};
#define CROS_EC_DEV_IOC ':'
#define CROS_EC_DEV_IOCXCMD _IOWR(':', 0, struct cros_ec_command)
/*
* @version: Command version number (often 0)
* @command: Command to send (EC_CMD_...)
* @outsize: Outgoing length in bytes
* @insize: Max number of bytes to accept from EC
* @result: EC's response to the command (separate from communication failure)
* @data: Where to put the incoming data from EC and outgoing data to EC
*/
struct cros_ec_command_v2 {
uint32_t version;
uint32_t command;
uint32_t outsize;
uint32_t insize;
uint32_t result;
uint8_t data[0];
};
#define CROS_EC_DEV_IOC_V2 0xEC
#define CROS_EC_DEV_IOCXCMD_V2 _IOWR(CROS_EC_DEV_IOC_V2, 0, \
struct cros_ec_command_v2)
#define CROS_EC_DEV_RETRY 3
/* ec device interface v1 (used with Chrome OS v3.18 and earlier) */
/**
* Wait for a command to complete, then return the response
*
* This is called when we get an EAGAIN response from the EC. We need to
* send EC_CMD_GET_COMMS_STATUS commands until the EC indicates it is
* finished the command that we originally sent.
*
* returns 0 if command is successful, <0 to indicate timeout or error
*/
static int command_wait_for_response(void)
{
struct ec_response_get_comms_status status;
struct cros_ec_command cmd;
int ret;
int i;
cmd.version = 0;
cmd.command = EC_CMD_GET_COMMS_STATUS;
cmd.outdata = NULL;
cmd.outsize = 0;
cmd.indata = (uint8_t *)&status;
cmd.insize = sizeof(status);
/* FIXME: magic delay until we fix the underlying problem (probably in
the kernel driver) */
usleep(10 * 1000);
for (i = 1; i <= CROS_EC_COMMAND_RETRIES; i++) {
ret = ioctl(cros_ec_fd, CROS_EC_DEV_IOCXCMD, &cmd, sizeof(cmd));
if (ret < 0) {
msg_perr("%s(): CrOS EC command failed: %d, errno=%d\n",
__func__, ret, errno);
ret = -EC_RES_ERROR;
break;
}
if (cmd.result) {
msg_perr("%s(): CrOS EC command failed: result=%d\n",
__func__, cmd.result);
ret = -cmd.result;
break;
}
if (!(status.flags & EC_COMMS_STATUS_PROCESSING)) {
ret = -EC_RES_SUCCESS;
break;
}
usleep(1000);
}
return ret;
}
/*
* __cros_ec_command_dev - Issue command to CROS_EC device
*
* @command: command code
* @outdata: data to send to EC
* @outsize: number of bytes in outbound payload
* @indata: (unallocated) buffer to store data received from EC
* @insize: number of bytes in inbound payload
*
* This uses the kernel Chrome OS EC driver to communicate with the EC.
*
* The outdata and indata buffers contain payload data (if any); command
* and response codes as well as checksum data are handled transparently by
* this function.
*
* Returns >=0 for success, or negative if other error.
*/
static int __cros_ec_command_dev(int command, int version,
const void *outdata, int outsize,
void *indata, int insize)
{
struct cros_ec_command cmd;
int ret;
cmd.version = version;
cmd.command = command;
cmd.outdata = outdata;
cmd.outsize = outsize;
cmd.indata = indata;
cmd.insize = insize;
ret = ioctl(cros_ec_fd, CROS_EC_DEV_IOCXCMD, &cmd, sizeof(cmd));
if (ret < 0 && errno == EAGAIN) {
ret = command_wait_for_response();
cmd.result = 0;
}
if (ret < 0) {
msg_perr("%s(): Command 0x%02x failed: %d, errno=%d\n",
__func__, command, ret, errno);
return -EC_RES_ERROR;
}
if (cmd.result) {
msg_pdbg("%s(): Command 0x%02x returned result: %d\n",
__func__, command, cmd.result);
return -cmd.result;
}
return ret;
}
/*
* ec device interface v2
* (used with upstream kernel as well as with Chrome OS v4.4 and later)
*/
static int command_wait_for_response_v2(void)
{
uint8_t s_cmd_buf[sizeof(struct cros_ec_command_v2) +
sizeof(struct ec_response_get_comms_status)];
struct ec_response_get_comms_status *status;
struct cros_ec_command_v2 *s_cmd;
int ret;
int i;
s_cmd = (struct cros_ec_command_v2 *)s_cmd_buf;
status = (struct ec_response_get_comms_status *)s_cmd->data;
s_cmd->version = 0;
s_cmd->command = EC_CMD_GET_COMMS_STATUS;
s_cmd->outsize = 0;
s_cmd->insize = sizeof(*status);
/*
* FIXME: magic delay until we fix the underlying problem (probably in
* the kernel driver)
*/
usleep(10 * 1000);
for (i = 1; i <= CROS_EC_COMMAND_RETRIES; i++) {
ret = ioctl(cros_ec_fd, CROS_EC_DEV_IOCXCMD_V2, s_cmd_buf,
sizeof(s_cmd_buf));
if (ret < 0) {
msg_perr("%s(): CrOS EC command failed: %d, errno=%d\n",
__func__, ret, errno);
ret = -EC_RES_ERROR;
break;
}
if (s_cmd->result) {
msg_perr("%s(): CrOS EC command failed: result=%d\n",
__func__, s_cmd->result);
ret = -s_cmd->result;
break;
}
if (!(status->flags & EC_COMMS_STATUS_PROCESSING)) {
ret = -EC_RES_SUCCESS;
break;
}
usleep(1000);
}
return ret;
}
static int __cros_ec_command_dev_v2(int command, int version,
const void *outdata, int outsize,
void *indata, int insize)
{
struct cros_ec_command_v2 *s_cmd;
int size = sizeof(struct cros_ec_command_v2) + max(outsize, insize);
int ret;
assert(outsize == 0 || outdata != NULL);
assert(insize == 0 || indata != NULL);
s_cmd = malloc(size);
if (s_cmd == NULL)
return -EC_RES_ERROR;
s_cmd->command = command;
s_cmd->version = version;
s_cmd->result = 0xff;
s_cmd->outsize = outsize;
s_cmd->insize = insize;
memcpy(s_cmd->data, outdata, outsize);
ret = ioctl(cros_ec_fd, CROS_EC_DEV_IOCXCMD_V2, s_cmd, size);
if (ret < 0 && errno == EAGAIN) {
ret = command_wait_for_response_v2();
s_cmd->result = 0;
}
if (ret < 0) {
msg_perr("%s(): Command 0x%02x failed: %d, errno=%d\n",
__func__, command, ret, errno);
free(s_cmd);
return -EC_RES_ERROR;
}
if (s_cmd->result) {
msg_pdbg("%s(): Command 0x%02x returned result: %d\n",
__func__, command, s_cmd->result);
free(s_cmd);
return -s_cmd->result;
}
memcpy(indata, s_cmd->data, min(ret, insize));
free(s_cmd);
return min(ret, insize);
}
/*
* Attempt to communicate with kernel using old ioctl format.
* If it returns ENOTTY, assume that this kernel uses the new format.
*/
static int ec_dev_is_v2()
{
struct ec_params_hello h_req = {
.in_data = 0xa0b0c0d0
};
struct ec_response_hello h_resp;
struct cros_ec_command s_cmd = { };
int r;
s_cmd.command = EC_CMD_HELLO;
s_cmd.result = 0xff;
s_cmd.outsize = sizeof(h_req);
s_cmd.outdata = (uint8_t *)&h_req;
s_cmd.insize = sizeof(h_resp);
s_cmd.indata = (uint8_t *)&h_resp;
r = ioctl(cros_ec_fd, CROS_EC_DEV_IOCXCMD, &s_cmd, sizeof(s_cmd));
if (r < 0 && errno == ENOTTY)
return 1;
return 0;
}
static int (*__cros_ec_command_dev_fn)(int command, int version,
const void *outdata, int outsize, void *indata, int insize);
/*
* cros_ec_command_dev - Issue command to CROS_EC device with retry
*
* @command: command code
* @outdata: data to send to EC
* @outsize: number of bytes in outbound payload
* @indata: (unallocated) buffer to store data received from EC
* @insize: number of bytes in inbound payload
*
* This uses the kernel Chrome OS EC driver to communicate with the EC.
*
* The outdata and indata buffers contain payload data (if any); command
* and response codes as well as checksum data are handled transparently by
* this function.
*
* Returns >=0 for success, or negative if other error.
*/
static int cros_ec_command_dev(int command, int version,
const void *outdata, int outsize,
void *indata, int insize)
{
int ret = EC_RES_ERROR;
int attempt;
for (attempt = 0; attempt < CROS_EC_DEV_RETRY; attempt++) {
ret = __cros_ec_command_dev_fn(command, version, outdata,
outsize, indata, insize);
if (ret >= 0)
return ret;
}
return ret;
}
static void cros_ec_set_max_size(struct cros_ec_priv *priv, struct opaque_master *op)
{
struct ec_response_get_protocol_info info;
int rc = 0;
msg_pdbg("%s: sending protoinfo command\n", __func__);
rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
&info, sizeof(info));
msg_pdbg("%s: rc:%d\n", __func__, rc);
/*
* Use V3 large size only if v2 protocol is not supported.
* When v2 is supported, we may be using a kernel without v3 support,
* leading to sending larger commands the kernel can support.
*/
if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
/* Allow overriding the max response size in case EC is incorrect */
if (priv->max_response_size)
info.max_response_packet_size = priv->max_response_size;
op->max_data_write = info.max_request_packet_size -
sizeof(struct ec_host_request);
op->max_data_read = info.max_response_packet_size -
sizeof(struct ec_host_response);
/*
* Due to a bug in NPCX SPI code (chromium:725580),
* The EC may responds 163 when it meant 160; it should not
* have included header and footer.
*/
op->max_data_read &= ~3;
msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
op->max_data_write, op->max_data_read);
}
}
/*
* Returns 0 to indicate success, non-zero otherwise
*
* This function parses programmer parameters from the command line. Since
* CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
* run during internal programmer initialization.
*/
static int cros_ec_parse_param(struct cros_ec_priv *priv)
{
char *p;
p = extract_programmer_param_str("type");
if (p) {
unsigned int index;
for (index = 0; index < ARRAY_SIZE(ec_type); index++)
if (!strcmp(p, ec_type[index]))
break;
if (index == ARRAY_SIZE(ec_type)) {
msg_perr("Invalid argument: \"%s\"\n", p);
free(p);
return 1;
}
priv->dev = ec_type[index];
msg_pdbg("Target %s used\n", priv->dev);
}
free(p);
p = extract_programmer_param_str("block");
if (p) {
unsigned int block;
char *endptr = NULL;
errno = 0;
block = strtoul(p, &endptr, 0);
if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
msg_perr("Invalid argument: \"%s\"\n", p);
free(p);
return 1;
}
if (block <= 0) {
msg_perr("%s: Invalid block size\n", __func__);
free(p);
return 1;
}
msg_pdbg("Override block size to 0x%x\n", block);
priv->erase_block_size = block;
}
free(p);
p = extract_programmer_param_str("max_response_size");
if (p) {
unsigned int max_response_size;
char *endptr = NULL;
errno = 0;
max_response_size = strtoul(p, &endptr, 0);
if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
msg_perr("Invalid max_response_size: \"%s\"\n", p);
free(p);
return 1;
}
if (max_response_size <= 0) {
msg_perr("%s: Invalid max_response_size\n", __func__);
free(p);
return 1;
}
msg_pdbg("Override max response size to 0x%x\n", max_response_size);
priv->max_response_size = max_response_size;
}
free(p);
return 0;
}
/* perform basic "hello" test to see if we can talk to the EC */
static int cros_ec_test(struct cros_ec_priv *priv)
{
struct ec_params_hello request;
struct ec_response_hello response;
int rc = 0;
/* Say hello to EC. */
request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
msg_pdbg("%s: sending HELLO request with 0x%08x\n",
__func__, request.in_data);
rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
sizeof(request), &response, sizeof(response));
msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
if (rc < 0 || response.out_data != 0xf1e2d3c4) {
msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
"rc=%d, request=0x%x response=0x%x\n",
rc, request.in_data, response.out_data);
return 1;
}
return 0;
}
static struct cros_ec_priv cros_ec_dev_priv = {
.detected = 0,
.current_image = EC_IMAGE_UNKNOWN,
.region = NULL,
.ec_command = cros_ec_command_dev,
.dev = "ec",
.ideal_write_size = 0,
.erase_block_size = 0,
.max_response_size = 0
};
static struct opaque_master opaque_master_cros_ec_dev = {
.max_data_read = 128,
.max_data_write = 128,
.probe = cros_ec_probe_size,
.read = cros_ec_read,
.write = cros_ec_write,
.erase = cros_ec_block_erase,
.wp_read_cfg = cros_ec_wp_read_cfg,
.wp_write_cfg = cros_ec_wp_write_cfg,
.wp_get_ranges = cros_ec_wp_get_available_ranges,
.data = NULL
};
static int cros_ec_dev_shutdown(void *data)
{
close(cros_ec_fd);
return 0;
}
/* ugly singleton to work around cros layering violations in action_descriptor.c */
static int ec_alias_path = 0;
int programming_ec(void)
{
return ec_alias_path;
}
int cros_ec_probe_dev(void)
{
char dev_path[32];
if (cros_ec_parse_param(&cros_ec_dev_priv))
return 1;
snprintf(dev_path, sizeof(dev_path), "%s%s",
CROS_EC_DEV_PREFIX, cros_ec_dev_priv.dev);
msg_pdbg("%s: probing for CROS_EC at %s\n", __func__, dev_path);
cros_ec_fd = open(dev_path, O_RDWR);
if (cros_ec_fd < 0)
return cros_ec_fd;
if (ec_dev_is_v2())
__cros_ec_command_dev_fn = __cros_ec_command_dev_v2;
else
__cros_ec_command_dev_fn = __cros_ec_command_dev;
if (cros_ec_test(&cros_ec_dev_priv))
return 1;
/* Programmer is EC so toggle ec-alias path detection on. */
ec_alias_path = 1;
cros_ec_set_max_size(&cros_ec_dev_priv, &opaque_master_cros_ec_dev);
internal_buses_supported &= ~(BUS_LPC|BUS_SPI);
msg_pdbg("CROS_EC detected at %s\n", dev_path);
register_opaque_master(&opaque_master_cros_ec_dev, NULL);
register_shutdown(cros_ec_dev_shutdown, NULL);
cros_ec_dev_priv.detected = 1;
cros_ec_priv = &cros_ec_dev_priv;
return 0;
}
const struct programmer_entry programmer_cros_ec = {
.name = "ec",
.type = OTHER,
.devs.note = "Google EC.\n",
.init = cros_ec_probe_dev,
.map_flash_region = fallback_map,
.unmap_flash_region = fallback_unmap,
.delay = internal_delay,
/*
* "ec" implies in-system programming on a live system, so
* handle with paranoia to catch errors early. If something goes
* wrong then hopefully the system will still be recoverable.
*/
.paranoid = 1,
};