blob: 070e692fb8e0db0aafe8d14889847718f1bb538d [file] [log] [blame]
David Hendricksee712472012-05-23 21:50:59 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2012 The Chromium OS Authors. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Google or the names of contributors or
18 * licensors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind.
22 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
25 * GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE
26 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
27 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28 * GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
29 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
30 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
31 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
32 * EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 */
David Hendricks14935fe2014-08-14 17:38:24 -070034#include <errno.h>
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080040#include "fmap.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070041#include "cros_ec.h"
42#include "cros_ec_lock.h"
43#include "cros_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080044#include "programmer.h"
45#include "spi.h"
46#include "writeprotect.h"
47
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080048/* FIXME: used for wp hacks */
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <unistd.h>
Souvik Ghosh586968a2016-08-11 17:56:24 -070053
54struct cros_ec_priv *cros_ec_priv;
David Hendricks393deec2016-11-23 16:15:05 -080055static int ignore_wp_range_command = 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -070056
David Hendricksb64b39a2016-10-11 13:48:06 -070057static int set_wp(int enable); /* FIXME: move set_wp() */
58
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080059struct wp_data {
60 int enable;
61 unsigned int start;
62 unsigned int len;
63};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080064#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
65
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080066/* If software sync is enabled, then we don't try the latest firmware copy
67 * after updating.
68 */
69#define SOFTWARE_SYNC_ENABLED
70
Gwendal Grignoua36ff502015-03-23 16:36:47 -070071/* For region larger use async version for FLASH_ERASE */
72#define FLASH_SMALL_REGION_THRESHOLD (16 * 1024)
73
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080074/* 1 if we want the flashrom to call erase_and_write_flash() again. */
75static int need_2nd_pass = 0;
76
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080077/* 1 if we want the flashrom to try jumping to new firmware after update. */
78static int try_latest_firmware = 0;
79
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +080080/* 1 if EC firmware has RWSIG enabled. */
81static int rwsig_enabled = 0;
82
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080083/* The range of each firmware copy from the image file to update.
84 * But re-define the .flags as the valid flag to indicate the firmware is
85 * new or not (if flags = 1).
86 */
87static struct fmap_area fwcopy[4]; // [0] is not used.
88
89/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080090static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070091 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
92 "EC_RO",
93 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080094};
95
Gwendal Grignou94e87d62014-11-25 15:34:15 -080096/*
97 * The names of the different device that can be found in a machine.
98 * Order is important: for backward compatibilty issue,
99 * 'ec' must be 0, 'pd' must be 1.
100 */
101static const char *ec_type[] = {
102 [0] = "ec",
103 [1] = "pd",
104 [2] = "sh",
Vincent Palatin4faff9a2017-03-17 17:27:39 +0100105 [3] = "fp",
Wei-Ning Huang78397842017-05-05 21:45:47 +0800106 [4] = "tp",
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800107};
108
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700109static struct ec_response_flash_region_info regions[EC_FLASH_REGION_COUNT];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800110
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800111/*
112 * Delay after reboot before EC can respond to host command.
113 * This value should be large enough for EC to initialize, but no larger than
114 * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be
115 * able to abort RWSIG jump and stay in RO.
116 */
117#define EC_INIT_DELAY 800000
118
119/*
120 * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW.
121 */
122#define EC_RWSIG_JUMP_TO_RW_DELAY 3000000
123
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800124/* Given the range not able to update, mark the corresponding
125 * firmware as old.
126 */
David Hendricksb907de32014-08-11 16:47:09 -0700127static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800128{
129 int i;
130
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800131 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800132 struct fmap_area *fw = &fwcopy[i];
133 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
134 (fw->offset >= addr && (fw->offset < addr + len))) {
135 msg_pdbg("Mark firmware [%s] as old.\n",
136 sections[i]);
137 fw->flags = 0; // mark as old
138 }
139 }
140}
141
142
Souvik Ghosh586968a2016-08-11 17:56:24 -0700143static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900144{
145 struct ec_response_get_version resp;
146 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700147
Souvik Ghosh586968a2016-08-11 17:56:24 -0700148 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700149 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900150 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700151 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900152 return rc;
153 }
154 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700155 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900156 return -1;
157 }
158
159 return resp.current_image;
160}
161
162
Souvik Ghosh586968a2016-08-11 17:56:24 -0700163static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900164 struct ec_response_flash_region_info *info)
165{
166 struct ec_params_flash_region_info req;
167 struct ec_response_flash_region_info resp;
168 int rc;
169
170 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700171 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900172 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
173 &resp, sizeof(resp));
174 if (rc < 0) {
175 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
176 return rc;
177 }
178
179 info->offset = resp.offset;
180 info->size = resp.size;
181 return 0;
182}
183
David Hendricksf9461c72013-07-11 19:02:13 -0700184/**
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800185 * Check if a feature is supported by EC.
186 *
187 * @param feature feature code
188 * @return < 0 if error, 0 not supported, > 0 supported
189 */
190static int ec_check_features(int feature)
191{
192 struct ec_response_get_features r;
193 int rc;
194
195 if (feature < 0 || feature >= sizeof(r.flags) * 8)
196 return -1;
197
198 rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES,
199 0, NULL, 0, &r, sizeof(r));
200 if (rc < 0)
201 return rc;
202
203 return !!(r.flags[feature / 32] & EC_FEATURE_MASK_0(feature));
204}
205
206/**
207 * Disable EC rwsig jump.
208 *
209 * @return 0 if success, <0 if error
210 */
211static int ec_rwsig_abort()
212{
213 struct ec_params_rwsig_action p;
214
215 p.action = RWSIG_ACTION_ABORT;
216 return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION,
217 0, &p, sizeof(p), NULL, 0);
218}
219
220/**
David Hendricksf9461c72013-07-11 19:02:13 -0700221 * Get the versions of the command supported by the EC.
222 *
223 * @param cmd Command
224 * @param pmask Destination for version mask; will be set to 0 on
225 * error.
226 * @return 0 if success, <0 if error
227 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700228static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700229{
David Hendricksf9461c72013-07-11 19:02:13 -0700230 struct ec_params_get_cmd_versions pver;
231 struct ec_response_get_cmd_versions rver;
232 int rc;
233
234 *pmask = 0;
235
236 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700237 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700238 &pver, sizeof(pver), &rver, sizeof(rver));
239
240 if (rc < 0)
241 return rc;
242
243 *pmask = rver.version_mask;
244 return rc;
245}
246
247/**
248 * Return non-zero if the EC supports the command and version
249 *
250 * @param cmd Command to check
251 * @param ver Version to check
252 * @return non-zero if command version supported; 0 if not.
253 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700254static int ec_cmd_version_supported(int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700255{
256 uint32_t mask = 0;
257 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700258
David Hendricksac1d25c2016-08-09 17:00:58 -0700259 rc = ec_get_cmd_versions(cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700260 if (rc < 0)
261 return rc;
262
263 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
264}
265
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800266/* Perform a cold reboot.
267 *
268 * @param flags flags to pass to EC_CMD_REBOOT_EC.
269 * @return 0 for success, < 0 for command failure.
270 */
271static int cros_ec_cold_reboot(int flags) {
272 struct ec_params_reboot_ec p;
273
274 memset(&p, 0, sizeof(p));
275 p.cmd = EC_REBOOT_COLD;
276 p.flags = flags;
277 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
278 NULL, 0);
279}
280
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800281/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800282 * then this functions picks a NEW firmware copy and jumps to it. Note that
283 * RO is preferred, then A, finally B.
284 *
285 * Returns 0 for success.
286 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700287static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800288 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800289 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700290 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800291
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800292 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
293 * jump to different firmware copy. The second EC_RES_SUCCESS would
294 * set the OBF=1 and the next command cannot be executed.
295 * Thus, we call EC to jump only if the target is different.
296 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700297 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700298 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800299 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700300 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900301 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800302
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800303 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900304
305 /* Translate target --> EC reboot command parameter */
306 switch (target) {
307 case EC_IMAGE_RO:
308 p.cmd = EC_REBOOT_JUMP_RO;
309 break;
310 case EC_IMAGE_RW:
311 p.cmd = EC_REBOOT_JUMP_RW;
312 break;
313 default:
314 /*
315 * If target is unspecified, set EC reboot command to use
316 * a new image. Also set "target" so that it may be used
317 * to update the priv->current_image if jump is successful.
318 */
319 if (fwcopy[EC_IMAGE_RO].flags) {
320 p.cmd = EC_REBOOT_JUMP_RO;
321 target = EC_IMAGE_RO;
322 } else if (fwcopy[EC_IMAGE_RW].flags) {
323 p.cmd = EC_REBOOT_JUMP_RW;
324 target = EC_IMAGE_RW;
325 } else {
326 p.cmd = EC_IMAGE_UNKNOWN;
327 }
328 break;
329 }
330
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800331 /*
332 * Do a cold reset instead of JUMP_RO so board enabling
333 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
334 * This is true for EC enabling RWSIG, where
335 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
336 */
337 if (target == EC_IMAGE_RO && rwsig_enabled) {
338 p.cmd = EC_REBOOT_COLD;
339 msg_pdbg("RWSIG enabled: doing a cold reboot instead of "
340 "JUMP_RO.\n");
341 }
342
343 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800344 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
345
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700346 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800347 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700348 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800349 return 0;
350 }
351
Souvik Ghosh586968a2016-08-11 17:56:24 -0700352 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700353 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800354 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700355 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800356 sections[target], rc);
357 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800358 }
359
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800360 /* Sleep until EC can respond to host command, but just before
361 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
362 usleep(EC_INIT_DELAY);
363
364 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
365 if (target == EC_IMAGE_RO && rwsig_enabled) {
366 msg_pdbg("RWSIG enabled: aborting RWSIG jump.\n");
367 ec_rwsig_abort();
368 }
369
370 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[target]);
371 rc = EC_RES_SUCCESS;
372 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800373
374 return rc;
375}
376
David Hendricksb64b39a2016-10-11 13:48:06 -0700377static int cros_ec_restore_wp(void *data)
378{
379 msg_pdbg("Restoring EC soft WP.\n");
380 return set_wp(1);
381}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800382
David Hendricksb64b39a2016-10-11 13:48:06 -0700383static int cros_ec_wp_is_enabled(void)
384{
385 struct ec_params_flash_protect p;
386 struct ec_response_flash_protect r;
387 int rc;
388
389 memset(&p, 0, sizeof(p));
390 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
391 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
392 if (rc < 0) {
393 msg_perr("FAILED: Cannot get the write protection status: %d\n",
394 rc);
395 return -1;
396 } else if (rc < sizeof(r)) {
397 msg_perr("FAILED: Too little data returned (expected:%zd, "
398 "actual:%d)\n", sizeof(r), rc);
399 return -1;
400 }
401
402 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
403 return 1;
404
405 return 0;
406}
407
408/*
409 * Prepare EC for update:
410 * - Disable soft WP if needed.
411 * - Parse flashmap.
412 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800413 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700414int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800415 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700416 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800417
Souvik Ghosh586968a2016-08-11 17:56:24 -0700418 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800419
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800420 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
421 rwsig_enabled = 1;
422 msg_pdbg("EC has RWSIG enabled.\n");
423 }
424
David Hendricksb64b39a2016-10-11 13:48:06 -0700425 /*
426 * If HW WP is disabled we may still need to disable write protection
427 * that is active on the EC. Otherwise the EC can reject erase/write
428 * commands.
429 *
430 * Failure is OK since HW WP might be enabled or the EC needs to be
431 * rebooted for the change to take effect. We can still update RW
432 * portions.
433 *
434 * If disabled here, EC WP will be restored at the end so that
435 * "--wp-enable" does not need to be run later. This greatly
436 * simplifies logic for developers and scripts.
437 */
438 wp_status = cros_ec_wp_is_enabled();
439 if (wp_status < 0) {
440 return 1;
441 } else if (wp_status == 1) {
442 msg_pdbg("Attempting to disable EC soft WP.\n");
443 if (!set_wp(0)) {
444 msg_pdbg("EC soft WP disabled successfully.\n");
445 if (register_shutdown(cros_ec_restore_wp, NULL))
446 return 1;
447 } else {
448 msg_pdbg("Failed. Hardware WP might in effect or EC "
449 "needs to be rebooted first.\n");
450 }
451 } else {
452 msg_pdbg("EC soft WP is already disabled.\n");
453 }
454
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800455 // Parse the fmap in the image file and cache the firmware ranges.
456 fmap = fmap_find_in_memory(image, size);
457 if (!fmap) return 0;
458
459 // Lookup RO/A/B sections in FMAP.
460 for (i = 0; i < fmap->nareas; i++) {
461 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800462 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700463 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800464 msg_pdbg("Found '%s' in image.\n", fa->name);
465 memcpy(&fwcopy[j], fa, sizeof(*fa));
466 fwcopy[j].flags = 1; // mark as new
467 }
468 }
469 }
470
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800471 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700472 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800473 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700474 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800475}
476
477
478/* Returns >0 if we need 2nd pass of erase_and_write_flash().
479 * <0 if we cannot jump to any firmware copy.
480 * ==0 if no more pass is needed.
481 *
482 * This function also jumps to new-updated firmware copy before return >0.
483 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700484int cros_ec_need_2nd_pass(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700485 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800486
487 if (need_2nd_pass) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700488 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800489 return -1;
490 }
491 }
492
493 return need_2nd_pass;
494}
495
496
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800497/* Returns 0 for success.
498 *
499 * Try latest firmware: B > A > RO
500 *
David Hendricksb907de32014-08-11 16:47:09 -0700501 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800502 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700503 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800504 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700505int cros_ec_finish(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700506 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800507
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800508 /* For EC with RWSIG enabled. We need a cold reboot to enable
509 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
510 */
511 if (rwsig_enabled) {
512 int rc;
513
514 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
515 rc = cros_ec_cold_reboot(0);
516 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
517 return rc;
518 }
519
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800520 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800521 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700522 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
523 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800524 }
525
526 return 0;
527}
528
529
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700530int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800531 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800532 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800533 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700534 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800535 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700536 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800537
David Hendricks133083b2012-07-17 20:39:38 -0700538 while (offset < readcnt) {
539 count = min(maxlen, readcnt - offset);
540 p.offset = blockaddr + offset;
541 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700542 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700543 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800544 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700545 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700546 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800547 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800548 } else {
549 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800550 }
551
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800552 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700553 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800554 }
555
556 return rc;
557}
558
559
Simon Glassc453a642013-07-01 18:08:53 +0900560/*
561 * returns 0 to indicate area does not overlap current EC image
562 * returns 1 to indicate area overlaps current EC image or error
563 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700564static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900565{
Simon Glassc453a642013-07-01 18:08:53 +0900566 enum ec_current_image image;
567 uint32_t region_offset;
568 uint32_t region_size;
569
Souvik Ghosh586968a2016-08-11 17:56:24 -0700570 image = cros_ec_priv->current_image;
571 region_offset = cros_ec_priv->region[image].offset;
572 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900573
574 if ((addr + len - 1 < region_offset) ||
575 (addr > region_offset + region_size - 1)) {
576 return 0;
577 }
578 return 1;
579}
580
581
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700582int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800583 unsigned int blockaddr,
584 unsigned int len) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700585 struct ec_params_flash_erase_v1 erase;
586 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700587 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800588
Souvik Ghosh586968a2016-08-11 17:56:24 -0700589 if (in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700590 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900591 need_2nd_pass = 1;
592 return ACCESS_DENIED;
593 }
594
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700595 erase.params.offset = blockaddr;
596 erase.params.size = len;
597 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
598 if (rc < 0) {
599 msg_perr("Cannot determine erase command version\n");
600 return 0;
601 }
602 cmd_version = 31 - __builtin_clz(mask);
603
604 if (cmd_version == 0) {
605 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
606 &erase.params,
607 sizeof(struct ec_params_flash_erase), NULL, 0);
608 if (rc == -EC_RES_ACCESS_DENIED) {
609 // this is active image.
610 cros_ec_invalidate_copy(blockaddr, len);
611 need_2nd_pass = 1;
612 return ACCESS_DENIED;
613 }
614 if (rc < 0) {
615 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
616 blockaddr, rc);
617 return rc;
618 }
619 goto end_flash_erase;
620 }
621
622 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
623 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
624 } else {
625 erase.cmd = FLASH_ERASE_SECTOR;
626 }
627 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
628 &erase, sizeof(erase), NULL, 0);
629 switch (rc) {
630 case 0:
631 break;
632 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800633 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700634 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800635 need_2nd_pass = 1;
636 return ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700637 case -EC_RES_BUSY:
638 msg_perr("CROS_EC: Flash erase command "
639 " already in progress\n");
640 default:
641 return rc;
642 }
643 if (len < FLASH_SMALL_REGION_THRESHOLD)
644 goto end_flash_erase;
645
646 /* Wait for the erase command to complete */
647 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700648
649/* wait up to 10s to erase a flash sector */
650#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
651/* wait .5 second between queries. */
652#define CROS_EC_ERASE_ASYNC_WAIT 500000
653
654 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
655 usleep(CROS_EC_ERASE_ASYNC_WAIT);
656 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700657 erase.cmd = FLASH_ERASE_GET_RESULT;
658 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
659 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800660 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800661 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700662 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800663 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800664 return rc;
665 }
666
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700667end_flash_erase:
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800668#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800669 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800670#endif
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700671 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700672 /*
673 * Can happen if the command with retried with
674 * EC_CMD_GET_COMMS_STATUS
675 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700676 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700677 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800678 return rc;
679}
680
681
Patrick Georgiab8353e2017-02-03 18:32:01 +0100682int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800683 unsigned int nbytes) {
684 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800685 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800686 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700687 uint8_t *packet;
688
Ken Chang69c31b82014-10-28 15:17:21 +0800689 /*
690 * For chrome-os-partner:33035, to workaround the undersized
691 * outdata buffer issue in kernel.
692 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700693 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700694 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800695 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700696 if (!packet)
697 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800698
699 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800700 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800701 p.offset = addr + i;
702 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900703
Souvik Ghosh586968a2016-08-11 17:56:24 -0700704 if (in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700705 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900706 need_2nd_pass = 1;
707 return ACCESS_DENIED;
708 }
709
David Hendricks2d6db772013-07-10 21:07:48 -0700710 memcpy(packet, &p, sizeof(p));
711 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700712 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700713 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700714
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800715 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800716 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700717 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800718 need_2nd_pass = 1;
719 return ACCESS_DENIED;
720 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800721
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800722 if (rc < 0) break;
723 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800724 }
725
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800726#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800727 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800728#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700729 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800730 return rc;
731}
732
733
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700734static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900735 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800736 int rc;
737
Souvik Ghosh586968a2016-08-11 17:56:24 -0700738 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800739 if (rc < 0) {
740 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
741 return 1;
742 }
743
744 msg_pinfo("Supported write protect range:\n");
745 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900746 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
747 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800748
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800749 return 0;
750}
751
752
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800753/*
754 * Helper function for flash protection.
755 *
756 * On EC API v1, the EC write protection has been simplified to one-bit:
757 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
758 * or disabled. However, this is different from the SPI-style write protect
759 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
760 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
761 *
762 * SRP Range | PROTECT_RO_AT_BOOT
763 * 0 0 | 0
764 * 0 non-zero | 1
765 * 1 0 | 1
766 * 1 non-zero | 1
767 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800768 *
769 * Besides, to make the protection take effect as soon as possible, we
770 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
771 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800772 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700773static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800774 struct ec_params_flash_protect p;
775 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800776 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
777 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
778 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800779 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800780
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800781 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800782 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800783 p.mask = (ro_at_boot_flag | ro_now_flag);
784 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700785 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700786 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800787 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800788 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
789 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800790 return 1;
791 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800792
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800793 /* Read back */
794 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700795 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700796 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800797 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800798 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
799 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800800 return 1;
801 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800802
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800803 if (!enable) {
804 /* The disable case is easier to check. */
805 if (r.flags & ro_at_boot_flag) {
806 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
807 return 1;
808 } else if (r.flags & ro_now_flag) {
809 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
810 need_an_ec_cold_reset = 1;
811 goto exit;
812 }
813
814 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
815 return 0;
816 }
817
818 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
819 if (r.flags & ro_at_boot_flag) {
820 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
821 } else {
822 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
823 return 1;
824 }
825
826 /* Then, we check if the protection has been activated. */
827 if (r.flags & ro_now_flag) {
828 /* Good, RO_NOW is set. */
829 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
830 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800831 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
832
833 memset(&p, 0, sizeof(p));
834 p.mask = EC_FLASH_PROTECT_ALL_NOW;
835 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700836 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800837 EC_VER_FLASH_PROTECT,
838 &p, sizeof(p), &r, sizeof(r));
839 if (rc < 0) {
840 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
841 return 1;
842 }
843
844 /* Read back */
845 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700846 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800847 EC_VER_FLASH_PROTECT,
848 &p, sizeof(p), &r, sizeof(r));
849 if (rc < 0) {
850 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
851 return 1;
852 }
853
854 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
855 msg_perr("FAILED: ALL_NOW is not set.\n");
856 need_an_ec_cold_reset = 1;
857 goto exit;
858 }
859
860 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
861
862 /*
863 * Our goal is to protect the RO ASAP. The entire protection
864 * is just a workaround for platform not supporting RO_NOW.
865 * It has side-effect that the RW is also protected and leads
866 * the RW update failed. So, we arrange an EC code reset to
867 * unlock RW ASAP.
868 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800869 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800870 if (rc < 0) {
871 msg_perr("WARN: Cannot arrange a cold reset at next "
872 "shutdown to unlock entire protect.\n");
873 msg_perr(" But you can do it manually.\n");
874 } else {
875 msg_pdbg("INFO: A cold reset is arranged at next "
876 "shutdown.\n");
877 }
878
879 } else {
880 msg_perr("FAILED: RO_NOW is not set.\n");
881 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
882 "make write protection active now.\n");
883 need_an_ec_cold_reset = 1;
884 }
885
886exit:
887 if (need_an_ec_cold_reset) {
888 msg_perr("FAILED: You may need a reboot to take effect of "
889 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800890 return 1;
891 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800892
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800893 return 0;
894}
895
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700896static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800897 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900898 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800899 int rc;
900
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800901 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700902 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800903 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800904 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800905 return 1;
906 }
907 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900908 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800909 /* pass */
910 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800911 msg_perr("FAILED: Unsupported write protection range "
912 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800913 msg_perr("Currently supported range:\n");
914 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900915 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
916 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800917 return 1;
918 }
919
David Hendricks393deec2016-11-23 16:15:05 -0800920 if (ignore_wp_range_command)
921 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700922 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800923}
924
925
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700926static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700927 enum wp_mode wp_mode) {
928 int ret;
929
930 switch (wp_mode) {
931 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700932 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700933 break;
934 default:
935 msg_perr("%s():%d Unsupported write-protection mode\n",
936 __func__, __LINE__);
937 ret = 1;
938 break;
939 }
940
941 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800942}
943
944
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700945static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800946 /* --wp-range implicitly enables write protection on CrOS EC, so force
947 it not to if --wp-disable is what the user really wants. */
948 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700949 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800950}
951
952
Souvik Ghosh586968a2016-08-11 17:56:24 -0700953static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800954 struct ec_params_flash_protect p;
955 struct ec_response_flash_protect r;
956 int start, len; /* wp range */
957 int enabled;
958 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800959
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800960 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700961 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700962 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800963 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800964 msg_perr("FAILED: Cannot get the write protection status: %d\n",
965 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800966 return 1;
967 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700968 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800969 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800970 return 1;
971 }
972
973 start = len = 0;
974 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900975 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800976
977 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
978 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700979 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800980 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800981 msg_perr("FAILED: Cannot get the WP_RO region info: "
982 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800983 return 1;
984 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900985 start = info.offset;
986 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800987 } else {
988 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
989 __func__);
990 }
991
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800992 /*
993 * If neither RO_NOW or ALL_NOW is set, it means write protect is
994 * NOT active now.
995 */
996 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
997 start = len = 0;
998
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800999 /* Remove the SPI-style messages. */
1000 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
1001 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
1002 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001003 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001004 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001005 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001006 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001007
1008 return 0;
1009}
1010
David Hendrickse5454932013-11-04 18:16:11 -08001011/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -07001012int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -08001013{
1014 struct ec_params_hello request;
1015 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -08001016 int rc = 0;
1017
1018 /* Say hello to EC. */
1019 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
1020 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
1021 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001022 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001023 sizeof(request), &response, sizeof(response));
1024 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1025
1026 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1027 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1028 "rc=%d, request=0x%x response=0x%x\n",
1029 rc, request.in_data, response.out_data);
1030 return 1;
1031 }
1032
1033 return 0;
1034}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001035
David Hendricksd13d90d2016-08-09 17:00:52 -07001036void cros_ec_set_max_size(struct cros_ec_priv *priv,
1037 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001038 struct ec_response_get_protocol_info info;
1039 int rc = 0;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001040
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001041 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001042 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001043 &info, sizeof(info));
1044 msg_pdbg("%s: rc:%d\n", __func__, rc);
1045
Gwendal Grignoucf540ef2017-08-10 12:10:06 -07001046 /*
1047 * Use V3 large size only if v2 protocol is not supported.
1048 * When v2 is supported, we may be using a kernel without v3 support,
1049 * leading to sending larger commands the kernel can support.
1050 */
1051 if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001052 op->max_data_write = info.max_request_packet_size -
1053 sizeof(struct ec_host_request);
1054 op->max_data_read = info.max_response_packet_size -
1055 sizeof(struct ec_host_response);
Gwendal Grignouef9062f2017-05-31 17:38:31 -07001056 /*
1057 * Due to a bug in NPCX SPI code (chromium:725580),
1058 * The EC may responds 163 when it meant 160; it should not
1059 * have included header and footer.
1060 */
1061 op->max_data_read &= ~3;
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001062 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1063 op->max_data_write, op->max_data_read);
1064 }
1065}
1066
David Hendricks14935fe2014-08-14 17:38:24 -07001067
1068/*
David Hendricks052446b2014-09-11 11:26:51 -07001069 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001070 *
1071 * This function parses programmer parameters from the command line. Since
1072 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1073 * run during internal programmer initialization.
1074 */
1075int cros_ec_parse_param(struct cros_ec_priv *priv)
1076{
David Hendricks98b3c572016-11-30 01:50:08 +00001077 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001078
David Hendricks98b3c572016-11-30 01:50:08 +00001079 p = extract_programmer_param("dev");
1080 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -07001081 unsigned int index;
1082 char *endptr = NULL;
1083
1084 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001085 /*
1086 * For backward compatibility, check if the index is
1087 * a number: 0: main EC, 1: PD
1088 * works only on Samus.
1089 */
David Hendricks98b3c572016-11-30 01:50:08 +00001090 index = strtoul(p, &endptr, 10);
1091 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
1092 msg_perr("Invalid argument: \"%s\"\n", p);
1093 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001094 }
1095
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001096 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -07001097 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001098 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001099 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001100 priv->dev = ec_type[index];
1101 msg_pdbg("Target %s used\n", priv->dev);
1102 }
David Hendricks14935fe2014-08-14 17:38:24 -07001103
David Hendricks98b3c572016-11-30 01:50:08 +00001104 p = extract_programmer_param("type");
1105 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001106 unsigned int index;
1107 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001108 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001109 break;
1110 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001111 msg_perr("Invalid argument: \"%s\"\n", p);
1112 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001113 }
1114 priv->dev = ec_type[index];
1115 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001116 }
1117
David Hendricks98b3c572016-11-30 01:50:08 +00001118 p = extract_programmer_param("block");
1119 if (p) {
1120 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001121 char *endptr = NULL;
1122
1123 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001124 block = strtoul(p, &endptr, 0);
1125 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1126 msg_perr("Invalid argument: \"%s\"\n", p);
1127 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001128 }
1129
David Hendricks98b3c572016-11-30 01:50:08 +00001130 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001131 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001132 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001133 }
1134
David Hendricks98b3c572016-11-30 01:50:08 +00001135 msg_pdbg("Override block size to 0x%x\n", block);
1136 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001137 }
1138
David Hendricks98b3c572016-11-30 01:50:08 +00001139 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001140}
1141
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001142int cros_ec_probe_size(struct flashctx *flash) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001143 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001144 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001145 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001146 struct block_eraser *eraser;
1147 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001148 .list_ranges = cros_ec_list_ranges,
1149 .set_range = cros_ec_set_range,
1150 .enable = cros_ec_enable_writeprotect,
1151 .disable = cros_ec_disable_writeprotect,
1152 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001153 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001154 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001155
Souvik Ghosh586968a2016-08-11 17:56:24 -07001156 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001157 if (rc < 0) {
1158 msg_perr("%s(): Failed to probe (no current image): %d\n",
1159 __func__, rc);
1160 return 0;
1161 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001162 cros_ec_priv->current_image = rc;
1163 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001164
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001165 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1166 if (rc < 0) {
1167 msg_perr("Cannot determine write command version\n");
1168 return 0;
1169 }
1170 cmd_version = 31 - __builtin_clz(mask);
1171
Patrick Georgif3fa2992017-02-02 16:24:44 +01001172 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001173 flash->chip->wp = &wp;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001174 flash->chip->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001175
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001176 if (cmd_version < 2) {
1177 struct ec_response_flash_info_1 info;
1178 /* Request general information about flash (v1 or below). */
1179 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1180 NULL, 0, &info,
1181 (cmd_version > 0 ? sizeof(info) :
1182 sizeof(struct ec_response_flash_info)));
1183 if (rc < 0) {
1184 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1185 cmd_version, rc);
1186 return 0;
1187 }
1188 if (cmd_version == 0) {
1189 cros_ec_priv->ideal_write_size =
1190 EC_FLASH_WRITE_VER0_SIZE;
1191 } else {
1192 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1193 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1194 flash->chip->feature_bits |=
1195 FEATURE_ERASE_TO_ZERO;
1196 }
1197 flash->chip->total_size = info.flash_size / 1024;
1198
1199 /* Allow overriding the erase block size in case EC is incorrect */
1200 if (cros_ec_priv->erase_block_size > 0)
1201 eraser->eraseblocks[0].size =
1202 cros_ec_priv->erase_block_size;
1203 else
1204 eraser->eraseblocks[0].size = info.erase_block_size;
1205
1206 eraser->eraseblocks[0].count = info.flash_size /
1207 eraser->eraseblocks[0].size;
1208 } else {
1209 struct ec_response_flash_info_2 info_2;
1210 struct ec_params_flash_info_2 params_2;
1211 struct ec_response_flash_info_2 *info_2_p = &info_2;
1212 int size_info_v2 = sizeof(info_2), i;
1213
1214 params_2.num_banks_desc = 0;
1215 /*
1216 * Call FLASH_INFO twice, second time with all banks
1217 * information.
1218 */
1219 for (i = 0; i < 2; i++) {
1220 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1221 cmd_version, &params_2,
1222 sizeof(params_2),
1223 info_2_p, size_info_v2);
1224 if (rc < 0) {
1225 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1226 __func__,
1227 params_2.num_banks_desc,
1228 cmd_version, rc);
1229 if (info_2_p != &info_2)
1230 free(info_2_p);
1231 return 0;
1232 } else if (i > 0) {
1233 break;
1234 }
1235 params_2.num_banks_desc = info_2_p->num_banks_total;
1236 size_info_v2 += info_2_p->num_banks_total *
1237 sizeof(struct ec_flash_bank);
1238
1239 info_2_p = malloc(size_info_v2);
1240 if (!info_2_p) {
1241 msg_perr("%s(): malloc of %d banks failed\n",
1242 __func__, info_2_p->num_banks_total);
1243 return 0;
1244 }
1245 }
1246 flash->chip->total_size = info_2_p->flash_size / 1024;
1247 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1248 /* Allow overriding the erase block size in case EC is incorrect */
1249 eraser->eraseblocks[i].size =
1250 (cros_ec_priv->erase_block_size > 0 ?
1251 cros_ec_priv->erase_block_size :
1252 1 << info_2_p->banks[i].erase_size_exp);
1253 eraser->eraseblocks[i].count =
1254 info_2_p->banks[i].count <<
1255 (info_2_p->banks[i].size_exp -
1256 info_2_p->banks[i].erase_size_exp);
1257 }
1258 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001259#if 0
1260 /*
1261 * TODO(b/38506987)Comment out, as some firmware were not
1262 * setting this flag properly.
1263 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001264 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
1265 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001266#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001267 free(info_2_p);
1268 }
David Hendricks194b3bb2013-07-16 14:32:26 -07001269 /*
1270 * Some STM32 variants erase bits to 0. For now, assume that this
1271 * applies to STM32L parts.
1272 *
1273 * FIXME: This info will eventually be exposed via some EC command.
1274 * See chrome-os-partner:20973.
1275 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001276 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001277 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001278 if (rc < 0) {
1279 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1280 return 0;
1281 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001282 if (!strncmp(chip_info.name, "stm32l1", 7))
Patrick Georgif3fa2992017-02-02 16:24:44 +01001283 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001284
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001285
David Hendricksf9461c72013-07-11 19:02:13 -07001286
David Hendricksa672b042016-09-19 12:37:36 -07001287 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1288 0, NULL, 0, &spi_info, sizeof(spi_info));
1289 if (rc < 0) {
1290 static char chip_vendor[32];
1291 static char chip_name[32];
1292
1293 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1294 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001295 flash->chip->vendor = chip_vendor;
1296 flash->chip->name = chip_name;
1297 flash->chip->tested = TEST_OK_PREWU;
David Hendricksa672b042016-09-19 12:37:36 -07001298 } else {
1299 const struct flashchip *f;
1300 uint32_t mfg = spi_info.jedec[0];
1301 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1302
1303 for (f = flashchips; f && f->name; f++) {
1304 if (f->bustype != BUS_SPI)
1305 continue;
1306 if ((f->manufacture_id == mfg) &&
1307 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001308 flash->chip->vendor = f->vendor;
1309 flash->chip->name = f->name;
1310 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001311 break;
1312 }
1313 }
1314 }
1315
Simon Glassc453a642013-07-01 18:08:53 +09001316 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1317 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001318 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1319 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001320 if (rc) {
1321 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1322 __func__, rc);
1323 return 0;
1324 }
1325
Souvik Ghosh586968a2016-08-11 17:56:24 -07001326 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1327 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001328 if (rc) {
1329 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1330 __func__, rc);
1331 return 0;
1332 }
1333
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001334 return 1;
1335};