blob: 06952c71fe66eef364627e2a035ad67b2836f71d [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
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700189 *
190 * NOTE: Once it successfully runs, the feature bits are cached. So, if you
191 * want to query a feature that can be different per copy, you need to
192 * cache features per image copy.
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800193 */
194static int ec_check_features(int feature)
195{
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700196 static struct ec_response_get_features r;
197 int rc = 0;
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800198
199 if (feature < 0 || feature >= sizeof(r.flags) * 8)
200 return -1;
201
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700202 /* We don't cache return code. We retry regardless the return code. */
203 if (r.flags[0] == 0)
204 rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES,
205 0, NULL, 0, &r, sizeof(r));
206
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800207 if (rc < 0)
208 return rc;
209
Daisuke Nojirif8ab92f2018-04-04 10:13:38 -0700210 return !!(r.flags[feature / 32] & (1 << (feature % 32)));
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800211}
212
213/**
214 * Disable EC rwsig jump.
215 *
216 * @return 0 if success, <0 if error
217 */
218static int ec_rwsig_abort()
219{
220 struct ec_params_rwsig_action p;
221
222 p.action = RWSIG_ACTION_ABORT;
223 return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION,
224 0, &p, sizeof(p), NULL, 0);
225}
226
227/**
David Hendricksf9461c72013-07-11 19:02:13 -0700228 * Get the versions of the command supported by the EC.
229 *
230 * @param cmd Command
231 * @param pmask Destination for version mask; will be set to 0 on
232 * error.
233 * @return 0 if success, <0 if error
234 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700235static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700236{
David Hendricksf9461c72013-07-11 19:02:13 -0700237 struct ec_params_get_cmd_versions pver;
238 struct ec_response_get_cmd_versions rver;
239 int rc;
240
241 *pmask = 0;
242
243 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700244 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700245 &pver, sizeof(pver), &rver, sizeof(rver));
246
247 if (rc < 0)
248 return rc;
249
250 *pmask = rver.version_mask;
251 return rc;
252}
253
254/**
255 * Return non-zero if the EC supports the command and version
256 *
257 * @param cmd Command to check
258 * @param ver Version to check
259 * @return non-zero if command version supported; 0 if not.
260 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700261static int ec_cmd_version_supported(int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700262{
263 uint32_t mask = 0;
264 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700265
David Hendricksac1d25c2016-08-09 17:00:58 -0700266 rc = ec_get_cmd_versions(cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700267 if (rc < 0)
268 return rc;
269
270 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
271}
272
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800273/* Perform a cold reboot.
274 *
275 * @param flags flags to pass to EC_CMD_REBOOT_EC.
276 * @return 0 for success, < 0 for command failure.
277 */
278static int cros_ec_cold_reboot(int flags) {
279 struct ec_params_reboot_ec p;
280
281 memset(&p, 0, sizeof(p));
282 p.cmd = EC_REBOOT_COLD;
283 p.flags = flags;
284 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
285 NULL, 0);
286}
287
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800288/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800289 * then this functions picks a NEW firmware copy and jumps to it. Note that
290 * RO is preferred, then A, finally B.
291 *
292 * Returns 0 for success.
293 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700294static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800295 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800296 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700297 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800298
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800299 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
300 * jump to different firmware copy. The second EC_RES_SUCCESS would
301 * set the OBF=1 and the next command cannot be executed.
302 * Thus, we call EC to jump only if the target is different.
303 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700304 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700305 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800306 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700307 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900308 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800309
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800310 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900311
312 /* Translate target --> EC reboot command parameter */
313 switch (target) {
314 case EC_IMAGE_RO:
315 p.cmd = EC_REBOOT_JUMP_RO;
316 break;
317 case EC_IMAGE_RW:
318 p.cmd = EC_REBOOT_JUMP_RW;
319 break;
320 default:
321 /*
322 * If target is unspecified, set EC reboot command to use
323 * a new image. Also set "target" so that it may be used
324 * to update the priv->current_image if jump is successful.
325 */
326 if (fwcopy[EC_IMAGE_RO].flags) {
327 p.cmd = EC_REBOOT_JUMP_RO;
328 target = EC_IMAGE_RO;
329 } else if (fwcopy[EC_IMAGE_RW].flags) {
330 p.cmd = EC_REBOOT_JUMP_RW;
331 target = EC_IMAGE_RW;
332 } else {
333 p.cmd = EC_IMAGE_UNKNOWN;
334 }
335 break;
336 }
337
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800338 /*
339 * Do a cold reset instead of JUMP_RO so board enabling
340 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
341 * This is true for EC enabling RWSIG, where
342 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
343 */
344 if (target == EC_IMAGE_RO && rwsig_enabled) {
345 p.cmd = EC_REBOOT_COLD;
346 msg_pdbg("RWSIG enabled: doing a cold reboot instead of "
347 "JUMP_RO.\n");
348 }
349
350 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800351 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
352
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700353 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800354 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700355 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800356 return 0;
357 }
358
Souvik Ghosh586968a2016-08-11 17:56:24 -0700359 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700360 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800361 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700362 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800363 sections[target], rc);
364 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800365 }
366
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800367 /* Sleep until EC can respond to host command, but just before
368 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
369 usleep(EC_INIT_DELAY);
370
371 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
372 if (target == EC_IMAGE_RO && rwsig_enabled) {
373 msg_pdbg("RWSIG enabled: aborting RWSIG jump.\n");
374 ec_rwsig_abort();
375 }
376
377 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[target]);
378 rc = EC_RES_SUCCESS;
379 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800380
381 return rc;
382}
383
David Hendricksb64b39a2016-10-11 13:48:06 -0700384static int cros_ec_restore_wp(void *data)
385{
386 msg_pdbg("Restoring EC soft WP.\n");
387 return set_wp(1);
388}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800389
David Hendricksb64b39a2016-10-11 13:48:06 -0700390static int cros_ec_wp_is_enabled(void)
391{
392 struct ec_params_flash_protect p;
393 struct ec_response_flash_protect r;
394 int rc;
395
396 memset(&p, 0, sizeof(p));
397 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
398 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
399 if (rc < 0) {
400 msg_perr("FAILED: Cannot get the write protection status: %d\n",
401 rc);
402 return -1;
403 } else if (rc < sizeof(r)) {
404 msg_perr("FAILED: Too little data returned (expected:%zd, "
405 "actual:%d)\n", sizeof(r), rc);
406 return -1;
407 }
408
409 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
410 return 1;
411
412 return 0;
413}
414
415/*
416 * Prepare EC for update:
417 * - Disable soft WP if needed.
418 * - Parse flashmap.
419 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800420 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700421int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800422 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700423 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800424
Souvik Ghosh586968a2016-08-11 17:56:24 -0700425 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800426
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800427 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
428 rwsig_enabled = 1;
429 msg_pdbg("EC has RWSIG enabled.\n");
430 }
431
David Hendricksb64b39a2016-10-11 13:48:06 -0700432 /*
433 * If HW WP is disabled we may still need to disable write protection
434 * that is active on the EC. Otherwise the EC can reject erase/write
435 * commands.
436 *
437 * Failure is OK since HW WP might be enabled or the EC needs to be
438 * rebooted for the change to take effect. We can still update RW
439 * portions.
440 *
441 * If disabled here, EC WP will be restored at the end so that
442 * "--wp-enable" does not need to be run later. This greatly
443 * simplifies logic for developers and scripts.
444 */
445 wp_status = cros_ec_wp_is_enabled();
446 if (wp_status < 0) {
447 return 1;
448 } else if (wp_status == 1) {
449 msg_pdbg("Attempting to disable EC soft WP.\n");
450 if (!set_wp(0)) {
451 msg_pdbg("EC soft WP disabled successfully.\n");
452 if (register_shutdown(cros_ec_restore_wp, NULL))
453 return 1;
454 } else {
455 msg_pdbg("Failed. Hardware WP might in effect or EC "
456 "needs to be rebooted first.\n");
457 }
458 } else {
459 msg_pdbg("EC soft WP is already disabled.\n");
460 }
461
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800462 // Parse the fmap in the image file and cache the firmware ranges.
463 fmap = fmap_find_in_memory(image, size);
464 if (!fmap) return 0;
465
466 // Lookup RO/A/B sections in FMAP.
467 for (i = 0; i < fmap->nareas; i++) {
468 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800469 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700470 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800471 msg_pdbg("Found '%s' in image.\n", fa->name);
472 memcpy(&fwcopy[j], fa, sizeof(*fa));
473 fwcopy[j].flags = 1; // mark as new
474 }
475 }
476 }
477
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800478 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700479 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800480 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700481 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800482}
483
484
485/* Returns >0 if we need 2nd pass of erase_and_write_flash().
486 * <0 if we cannot jump to any firmware copy.
487 * ==0 if no more pass is needed.
488 *
489 * This function also jumps to new-updated firmware copy before return >0.
490 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700491int cros_ec_need_2nd_pass(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700492 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800493
494 if (need_2nd_pass) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700495 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800496 return -1;
497 }
498 }
499
500 return need_2nd_pass;
501}
502
503
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800504/* Returns 0 for success.
505 *
506 * Try latest firmware: B > A > RO
507 *
David Hendricksb907de32014-08-11 16:47:09 -0700508 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800509 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700510 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800511 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700512int cros_ec_finish(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700513 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800514
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800515 /* For EC with RWSIG enabled. We need a cold reboot to enable
516 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
517 */
518 if (rwsig_enabled) {
519 int rc;
520
521 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
522 rc = cros_ec_cold_reboot(0);
523 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
524 return rc;
525 }
526
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800527 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800528 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700529 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
530 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800531 }
532
533 return 0;
534}
535
536
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700537int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800538 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800539 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800540 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700541 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800542 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700543 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800544
David Hendricks133083b2012-07-17 20:39:38 -0700545 while (offset < readcnt) {
546 count = min(maxlen, readcnt - offset);
547 p.offset = blockaddr + offset;
548 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700549 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700550 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800551 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700552 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700553 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800554 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800555 } else {
556 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800557 }
558
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800559 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700560 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800561 }
562
563 return rc;
564}
565
566
Simon Glassc453a642013-07-01 18:08:53 +0900567/*
568 * returns 0 to indicate area does not overlap current EC image
569 * returns 1 to indicate area overlaps current EC image or error
570 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700571static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900572{
Simon Glassc453a642013-07-01 18:08:53 +0900573 enum ec_current_image image;
574 uint32_t region_offset;
575 uint32_t region_size;
576
Souvik Ghosh586968a2016-08-11 17:56:24 -0700577 image = cros_ec_priv->current_image;
578 region_offset = cros_ec_priv->region[image].offset;
579 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900580
581 if ((addr + len - 1 < region_offset) ||
582 (addr > region_offset + region_size - 1)) {
583 return 0;
584 }
585 return 1;
586}
587
588
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700589int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800590 unsigned int blockaddr,
591 unsigned int len) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700592 struct ec_params_flash_erase_v1 erase;
593 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700594 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800595
Souvik Ghosh586968a2016-08-11 17:56:24 -0700596 if (in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700597 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900598 need_2nd_pass = 1;
599 return ACCESS_DENIED;
600 }
601
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700602 erase.params.offset = blockaddr;
603 erase.params.size = len;
604 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
605 if (rc < 0) {
606 msg_perr("Cannot determine erase command version\n");
607 return 0;
608 }
609 cmd_version = 31 - __builtin_clz(mask);
610
611 if (cmd_version == 0) {
612 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
613 &erase.params,
614 sizeof(struct ec_params_flash_erase), NULL, 0);
615 if (rc == -EC_RES_ACCESS_DENIED) {
616 // this is active image.
617 cros_ec_invalidate_copy(blockaddr, len);
618 need_2nd_pass = 1;
619 return ACCESS_DENIED;
620 }
621 if (rc < 0) {
622 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
623 blockaddr, rc);
624 return rc;
625 }
626 goto end_flash_erase;
627 }
628
629 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
630 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
631 } else {
632 erase.cmd = FLASH_ERASE_SECTOR;
633 }
634 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
635 &erase, sizeof(erase), NULL, 0);
636 switch (rc) {
637 case 0:
638 break;
639 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800640 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700641 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800642 need_2nd_pass = 1;
643 return ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700644 case -EC_RES_BUSY:
645 msg_perr("CROS_EC: Flash erase command "
646 " already in progress\n");
647 default:
648 return rc;
649 }
650 if (len < FLASH_SMALL_REGION_THRESHOLD)
651 goto end_flash_erase;
652
653 /* Wait for the erase command to complete */
654 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700655
656/* wait up to 10s to erase a flash sector */
657#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
658/* wait .5 second between queries. */
659#define CROS_EC_ERASE_ASYNC_WAIT 500000
660
661 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
662 usleep(CROS_EC_ERASE_ASYNC_WAIT);
663 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700664 erase.cmd = FLASH_ERASE_GET_RESULT;
665 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
666 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800667 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800668 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700669 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800670 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800671 return rc;
672 }
673
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700674end_flash_erase:
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800675#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800676 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800677#endif
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700678 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700679 /*
680 * Can happen if the command with retried with
681 * EC_CMD_GET_COMMS_STATUS
682 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700683 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700684 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800685 return rc;
686}
687
688
Patrick Georgiab8353e2017-02-03 18:32:01 +0100689int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800690 unsigned int nbytes) {
691 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800692 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800693 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700694 uint8_t *packet;
695
Ken Chang69c31b82014-10-28 15:17:21 +0800696 /*
697 * For chrome-os-partner:33035, to workaround the undersized
698 * outdata buffer issue in kernel.
699 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700700 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700701 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800702 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700703 if (!packet)
704 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800705
706 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800707 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800708 p.offset = addr + i;
709 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900710
Souvik Ghosh586968a2016-08-11 17:56:24 -0700711 if (in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700712 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900713 need_2nd_pass = 1;
714 return ACCESS_DENIED;
715 }
716
David Hendricks2d6db772013-07-10 21:07:48 -0700717 memcpy(packet, &p, sizeof(p));
718 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700719 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700720 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700721
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800722 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800723 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700724 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800725 need_2nd_pass = 1;
726 return ACCESS_DENIED;
727 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800728
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800729 if (rc < 0) break;
730 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800731 }
732
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800733#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800734 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800735#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700736 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800737 return rc;
738}
739
740
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700741static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900742 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800743 int rc;
744
Souvik Ghosh586968a2016-08-11 17:56:24 -0700745 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800746 if (rc < 0) {
747 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
748 return 1;
749 }
750
751 msg_pinfo("Supported write protect range:\n");
752 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900753 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
754 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800755
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800756 return 0;
757}
758
759
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800760/*
761 * Helper function for flash protection.
762 *
763 * On EC API v1, the EC write protection has been simplified to one-bit:
764 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
765 * or disabled. However, this is different from the SPI-style write protect
766 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
767 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
768 *
769 * SRP Range | PROTECT_RO_AT_BOOT
770 * 0 0 | 0
771 * 0 non-zero | 1
772 * 1 0 | 1
773 * 1 non-zero | 1
774 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800775 *
776 * Besides, to make the protection take effect as soon as possible, we
777 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
778 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800779 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700780static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800781 struct ec_params_flash_protect p;
782 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800783 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
784 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
785 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800786 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800787
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800788 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800789 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800790 p.mask = (ro_at_boot_flag | ro_now_flag);
791 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700792 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700793 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800794 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800795 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
796 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800797 return 1;
798 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800799
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800800 /* Read back */
801 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700802 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700803 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800804 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800805 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
806 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800807 return 1;
808 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800809
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800810 if (!enable) {
811 /* The disable case is easier to check. */
812 if (r.flags & ro_at_boot_flag) {
813 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
814 return 1;
815 } else if (r.flags & ro_now_flag) {
816 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
817 need_an_ec_cold_reset = 1;
818 goto exit;
819 }
820
821 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
822 return 0;
823 }
824
825 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
826 if (r.flags & ro_at_boot_flag) {
827 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
828 } else {
829 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
830 return 1;
831 }
832
833 /* Then, we check if the protection has been activated. */
834 if (r.flags & ro_now_flag) {
835 /* Good, RO_NOW is set. */
836 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
837 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800838 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
839
840 memset(&p, 0, sizeof(p));
841 p.mask = EC_FLASH_PROTECT_ALL_NOW;
842 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700843 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800844 EC_VER_FLASH_PROTECT,
845 &p, sizeof(p), &r, sizeof(r));
846 if (rc < 0) {
847 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
848 return 1;
849 }
850
851 /* Read back */
852 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700853 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800854 EC_VER_FLASH_PROTECT,
855 &p, sizeof(p), &r, sizeof(r));
856 if (rc < 0) {
857 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
858 return 1;
859 }
860
861 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
862 msg_perr("FAILED: ALL_NOW is not set.\n");
863 need_an_ec_cold_reset = 1;
864 goto exit;
865 }
866
867 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
868
869 /*
870 * Our goal is to protect the RO ASAP. The entire protection
871 * is just a workaround for platform not supporting RO_NOW.
872 * It has side-effect that the RW is also protected and leads
873 * the RW update failed. So, we arrange an EC code reset to
874 * unlock RW ASAP.
875 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800876 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800877 if (rc < 0) {
878 msg_perr("WARN: Cannot arrange a cold reset at next "
879 "shutdown to unlock entire protect.\n");
880 msg_perr(" But you can do it manually.\n");
881 } else {
882 msg_pdbg("INFO: A cold reset is arranged at next "
883 "shutdown.\n");
884 }
885
886 } else {
887 msg_perr("FAILED: RO_NOW is not set.\n");
888 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
889 "make write protection active now.\n");
890 need_an_ec_cold_reset = 1;
891 }
892
893exit:
894 if (need_an_ec_cold_reset) {
895 msg_perr("FAILED: You may need a reboot to take effect of "
896 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800897 return 1;
898 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800899
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800900 return 0;
901}
902
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700903static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800904 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900905 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800906 int rc;
907
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800908 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700909 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800910 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800911 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800912 return 1;
913 }
914 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900915 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800916 /* pass */
917 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800918 msg_perr("FAILED: Unsupported write protection range "
919 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800920 msg_perr("Currently supported range:\n");
921 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900922 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
923 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800924 return 1;
925 }
926
David Hendricks393deec2016-11-23 16:15:05 -0800927 if (ignore_wp_range_command)
928 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700929 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800930}
931
932
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700933static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700934 enum wp_mode wp_mode) {
935 int ret;
936
937 switch (wp_mode) {
938 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700939 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700940 break;
941 default:
942 msg_perr("%s():%d Unsupported write-protection mode\n",
943 __func__, __LINE__);
944 ret = 1;
945 break;
946 }
947
948 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800949}
950
951
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700952static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800953 /* --wp-range implicitly enables write protection on CrOS EC, so force
954 it not to if --wp-disable is what the user really wants. */
955 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700956 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800957}
958
959
Souvik Ghosh586968a2016-08-11 17:56:24 -0700960static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800961 struct ec_params_flash_protect p;
962 struct ec_response_flash_protect r;
963 int start, len; /* wp range */
964 int enabled;
965 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800966
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800967 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700968 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700969 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800970 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800971 msg_perr("FAILED: Cannot get the write protection status: %d\n",
972 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800973 return 1;
974 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700975 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800976 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800977 return 1;
978 }
979
980 start = len = 0;
981 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900982 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800983
984 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
985 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700986 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800987 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800988 msg_perr("FAILED: Cannot get the WP_RO region info: "
989 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800990 return 1;
991 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900992 start = info.offset;
993 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800994 } else {
995 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
996 __func__);
997 }
998
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800999 /*
1000 * If neither RO_NOW or ALL_NOW is set, it means write protect is
1001 * NOT active now.
1002 */
1003 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
1004 start = len = 0;
1005
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001006 /* Remove the SPI-style messages. */
1007 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
1008 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
1009 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001010 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001011 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001012 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001013 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001014
1015 return 0;
1016}
1017
David Hendrickse5454932013-11-04 18:16:11 -08001018/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -07001019int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -08001020{
1021 struct ec_params_hello request;
1022 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -08001023 int rc = 0;
1024
1025 /* Say hello to EC. */
1026 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
1027 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
1028 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001029 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001030 sizeof(request), &response, sizeof(response));
1031 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1032
1033 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1034 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1035 "rc=%d, request=0x%x response=0x%x\n",
1036 rc, request.in_data, response.out_data);
1037 return 1;
1038 }
1039
1040 return 0;
1041}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001042
David Hendricksd13d90d2016-08-09 17:00:52 -07001043void cros_ec_set_max_size(struct cros_ec_priv *priv,
1044 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001045 struct ec_response_get_protocol_info info;
1046 int rc = 0;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001047
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001048 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001049 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001050 &info, sizeof(info));
1051 msg_pdbg("%s: rc:%d\n", __func__, rc);
1052
Gwendal Grignoucf540ef2017-08-10 12:10:06 -07001053 /*
1054 * Use V3 large size only if v2 protocol is not supported.
1055 * When v2 is supported, we may be using a kernel without v3 support,
1056 * leading to sending larger commands the kernel can support.
1057 */
1058 if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001059 op->max_data_write = info.max_request_packet_size -
1060 sizeof(struct ec_host_request);
1061 op->max_data_read = info.max_response_packet_size -
1062 sizeof(struct ec_host_response);
Gwendal Grignouef9062f2017-05-31 17:38:31 -07001063 /*
1064 * Due to a bug in NPCX SPI code (chromium:725580),
1065 * The EC may responds 163 when it meant 160; it should not
1066 * have included header and footer.
1067 */
1068 op->max_data_read &= ~3;
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001069 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1070 op->max_data_write, op->max_data_read);
1071 }
1072}
1073
David Hendricks14935fe2014-08-14 17:38:24 -07001074
1075/*
David Hendricks052446b2014-09-11 11:26:51 -07001076 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001077 *
1078 * This function parses programmer parameters from the command line. Since
1079 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1080 * run during internal programmer initialization.
1081 */
1082int cros_ec_parse_param(struct cros_ec_priv *priv)
1083{
David Hendricks98b3c572016-11-30 01:50:08 +00001084 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001085
David Hendricks98b3c572016-11-30 01:50:08 +00001086 p = extract_programmer_param("dev");
1087 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -07001088 unsigned int index;
1089 char *endptr = NULL;
1090
1091 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001092 /*
1093 * For backward compatibility, check if the index is
1094 * a number: 0: main EC, 1: PD
1095 * works only on Samus.
1096 */
David Hendricks98b3c572016-11-30 01:50:08 +00001097 index = strtoul(p, &endptr, 10);
1098 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
1099 msg_perr("Invalid argument: \"%s\"\n", p);
1100 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001101 }
1102
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001103 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -07001104 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001105 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001106 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001107 priv->dev = ec_type[index];
1108 msg_pdbg("Target %s used\n", priv->dev);
1109 }
David Hendricks14935fe2014-08-14 17:38:24 -07001110
David Hendricks98b3c572016-11-30 01:50:08 +00001111 p = extract_programmer_param("type");
1112 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001113 unsigned int index;
1114 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001115 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001116 break;
1117 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001118 msg_perr("Invalid argument: \"%s\"\n", p);
1119 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001120 }
1121 priv->dev = ec_type[index];
1122 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001123 }
1124
David Hendricks98b3c572016-11-30 01:50:08 +00001125 p = extract_programmer_param("block");
1126 if (p) {
1127 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001128 char *endptr = NULL;
1129
1130 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001131 block = strtoul(p, &endptr, 0);
1132 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1133 msg_perr("Invalid argument: \"%s\"\n", p);
1134 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001135 }
1136
David Hendricks98b3c572016-11-30 01:50:08 +00001137 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001138 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001139 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001140 }
1141
David Hendricks98b3c572016-11-30 01:50:08 +00001142 msg_pdbg("Override block size to 0x%x\n", block);
1143 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001144 }
1145
David Hendricks98b3c572016-11-30 01:50:08 +00001146 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001147}
1148
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001149int cros_ec_probe_size(struct flashctx *flash) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001150 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001151 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001152 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001153 struct block_eraser *eraser;
1154 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001155 .list_ranges = cros_ec_list_ranges,
1156 .set_range = cros_ec_set_range,
1157 .enable = cros_ec_enable_writeprotect,
1158 .disable = cros_ec_disable_writeprotect,
1159 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001160 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001161 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001162
Souvik Ghosh586968a2016-08-11 17:56:24 -07001163 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001164 if (rc < 0) {
1165 msg_perr("%s(): Failed to probe (no current image): %d\n",
1166 __func__, rc);
1167 return 0;
1168 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001169 cros_ec_priv->current_image = rc;
1170 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001171
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001172 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1173 if (rc < 0) {
1174 msg_perr("Cannot determine write command version\n");
1175 return 0;
1176 }
1177 cmd_version = 31 - __builtin_clz(mask);
1178
Patrick Georgif3fa2992017-02-02 16:24:44 +01001179 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001180 flash->chip->wp = &wp;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001181 flash->chip->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001182
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001183 if (cmd_version < 2) {
1184 struct ec_response_flash_info_1 info;
1185 /* Request general information about flash (v1 or below). */
1186 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1187 NULL, 0, &info,
1188 (cmd_version > 0 ? sizeof(info) :
1189 sizeof(struct ec_response_flash_info)));
1190 if (rc < 0) {
1191 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1192 cmd_version, rc);
1193 return 0;
1194 }
1195 if (cmd_version == 0) {
1196 cros_ec_priv->ideal_write_size =
1197 EC_FLASH_WRITE_VER0_SIZE;
1198 } else {
1199 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1200 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1201 flash->chip->feature_bits |=
1202 FEATURE_ERASE_TO_ZERO;
1203 }
1204 flash->chip->total_size = info.flash_size / 1024;
1205
1206 /* Allow overriding the erase block size in case EC is incorrect */
1207 if (cros_ec_priv->erase_block_size > 0)
1208 eraser->eraseblocks[0].size =
1209 cros_ec_priv->erase_block_size;
1210 else
1211 eraser->eraseblocks[0].size = info.erase_block_size;
1212
1213 eraser->eraseblocks[0].count = info.flash_size /
1214 eraser->eraseblocks[0].size;
1215 } else {
1216 struct ec_response_flash_info_2 info_2;
1217 struct ec_params_flash_info_2 params_2;
1218 struct ec_response_flash_info_2 *info_2_p = &info_2;
1219 int size_info_v2 = sizeof(info_2), i;
1220
1221 params_2.num_banks_desc = 0;
1222 /*
1223 * Call FLASH_INFO twice, second time with all banks
1224 * information.
1225 */
1226 for (i = 0; i < 2; i++) {
1227 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1228 cmd_version, &params_2,
1229 sizeof(params_2),
1230 info_2_p, size_info_v2);
1231 if (rc < 0) {
1232 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1233 __func__,
1234 params_2.num_banks_desc,
1235 cmd_version, rc);
1236 if (info_2_p != &info_2)
1237 free(info_2_p);
1238 return 0;
1239 } else if (i > 0) {
1240 break;
1241 }
1242 params_2.num_banks_desc = info_2_p->num_banks_total;
1243 size_info_v2 += info_2_p->num_banks_total *
1244 sizeof(struct ec_flash_bank);
1245
1246 info_2_p = malloc(size_info_v2);
1247 if (!info_2_p) {
1248 msg_perr("%s(): malloc of %d banks failed\n",
1249 __func__, info_2_p->num_banks_total);
1250 return 0;
1251 }
1252 }
1253 flash->chip->total_size = info_2_p->flash_size / 1024;
1254 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1255 /* Allow overriding the erase block size in case EC is incorrect */
1256 eraser->eraseblocks[i].size =
1257 (cros_ec_priv->erase_block_size > 0 ?
1258 cros_ec_priv->erase_block_size :
1259 1 << info_2_p->banks[i].erase_size_exp);
1260 eraser->eraseblocks[i].count =
1261 info_2_p->banks[i].count <<
1262 (info_2_p->banks[i].size_exp -
1263 info_2_p->banks[i].erase_size_exp);
1264 }
1265 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001266#if 0
1267 /*
1268 * TODO(b/38506987)Comment out, as some firmware were not
1269 * setting this flag properly.
1270 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001271 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
1272 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001273#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001274 free(info_2_p);
1275 }
David Hendricks194b3bb2013-07-16 14:32:26 -07001276 /*
1277 * Some STM32 variants erase bits to 0. For now, assume that this
1278 * applies to STM32L parts.
1279 *
1280 * FIXME: This info will eventually be exposed via some EC command.
1281 * See chrome-os-partner:20973.
1282 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001283 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001284 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001285 if (rc < 0) {
1286 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1287 return 0;
1288 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001289 if (!strncmp(chip_info.name, "stm32l1", 7))
Patrick Georgif3fa2992017-02-02 16:24:44 +01001290 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001291
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001292
David Hendricksf9461c72013-07-11 19:02:13 -07001293
David Hendricksa672b042016-09-19 12:37:36 -07001294 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1295 0, NULL, 0, &spi_info, sizeof(spi_info));
1296 if (rc < 0) {
1297 static char chip_vendor[32];
1298 static char chip_name[32];
1299
1300 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1301 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001302 flash->chip->vendor = chip_vendor;
1303 flash->chip->name = chip_name;
1304 flash->chip->tested = TEST_OK_PREWU;
David Hendricksa672b042016-09-19 12:37:36 -07001305 } else {
1306 const struct flashchip *f;
1307 uint32_t mfg = spi_info.jedec[0];
1308 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1309
1310 for (f = flashchips; f && f->name; f++) {
1311 if (f->bustype != BUS_SPI)
1312 continue;
1313 if ((f->manufacture_id == mfg) &&
1314 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001315 flash->chip->vendor = f->vendor;
1316 flash->chip->name = f->name;
1317 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001318 break;
1319 }
1320 }
1321 }
1322
Simon Glassc453a642013-07-01 18:08:53 +09001323 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1324 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001325 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1326 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001327 if (rc) {
1328 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1329 __func__, rc);
1330 return 0;
1331 }
1332
Souvik Ghosh586968a2016-08-11 17:56:24 -07001333 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1334 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001335 if (rc) {
1336 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1337 __func__, rc);
1338 return 0;
1339 }
1340
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001341 return 1;
1342};