blob: 4aeb4b1b79f37cb13d09466d3fa9bf284a6d7d3d [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))) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -0700135 msg_pdbg(" OLD[%s]", sections[i]);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800136 fw->flags = 0; // mark as old
137 }
138 }
139}
140
141
Souvik Ghosh586968a2016-08-11 17:56:24 -0700142static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900143{
144 struct ec_response_get_version resp;
145 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700146
Souvik Ghosh586968a2016-08-11 17:56:24 -0700147 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700148 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900149 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700150 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900151 return rc;
152 }
153 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700154 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900155 return -1;
156 }
157
158 return resp.current_image;
159}
160
161
Souvik Ghosh586968a2016-08-11 17:56:24 -0700162static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900163 struct ec_response_flash_region_info *info)
164{
165 struct ec_params_flash_region_info req;
166 struct ec_response_flash_region_info resp;
167 int rc;
168
169 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700170 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900171 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
172 &resp, sizeof(resp));
173 if (rc < 0) {
174 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
175 return rc;
176 }
177
178 info->offset = resp.offset;
179 info->size = resp.size;
180 return 0;
181}
182
David Hendricksf9461c72013-07-11 19:02:13 -0700183/**
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800184 * Check if a feature is supported by EC.
185 *
186 * @param feature feature code
187 * @return < 0 if error, 0 not supported, > 0 supported
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700188 *
189 * NOTE: Once it successfully runs, the feature bits are cached. So, if you
190 * want to query a feature that can be different per copy, you need to
191 * cache features per image copy.
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800192 */
193static int ec_check_features(int feature)
194{
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700195 static struct ec_response_get_features r;
196 int rc = 0;
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800197
198 if (feature < 0 || feature >= sizeof(r.flags) * 8)
199 return -1;
200
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700201 /* We don't cache return code. We retry regardless the return code. */
202 if (r.flags[0] == 0)
203 rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES,
204 0, NULL, 0, &r, sizeof(r));
205
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800206 if (rc < 0)
207 return rc;
208
Daisuke Nojirif8ab92f2018-04-04 10:13:38 -0700209 return !!(r.flags[feature / 32] & (1 << (feature % 32)));
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800210}
211
212/**
213 * Disable EC rwsig jump.
214 *
215 * @return 0 if success, <0 if error
216 */
217static int ec_rwsig_abort()
218{
219 struct ec_params_rwsig_action p;
220
221 p.action = RWSIG_ACTION_ABORT;
222 return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION,
223 0, &p, sizeof(p), NULL, 0);
224}
225
226/**
David Hendricksf9461c72013-07-11 19:02:13 -0700227 * Get the versions of the command supported by the EC.
228 *
229 * @param cmd Command
230 * @param pmask Destination for version mask; will be set to 0 on
231 * error.
232 * @return 0 if success, <0 if error
233 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700234static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700235{
David Hendricksf9461c72013-07-11 19:02:13 -0700236 struct ec_params_get_cmd_versions pver;
237 struct ec_response_get_cmd_versions rver;
238 int rc;
239
240 *pmask = 0;
241
242 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700243 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700244 &pver, sizeof(pver), &rver, sizeof(rver));
245
246 if (rc < 0)
247 return rc;
248
249 *pmask = rver.version_mask;
250 return rc;
251}
252
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800253/* Perform a cold reboot.
254 *
255 * @param flags flags to pass to EC_CMD_REBOOT_EC.
256 * @return 0 for success, < 0 for command failure.
257 */
258static int cros_ec_cold_reboot(int flags) {
259 struct ec_params_reboot_ec p;
260
261 memset(&p, 0, sizeof(p));
262 p.cmd = EC_REBOOT_COLD;
263 p.flags = flags;
264 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
265 NULL, 0);
266}
267
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800268/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800269 * then this functions picks a NEW firmware copy and jumps to it. Note that
270 * RO is preferred, then A, finally B.
271 *
272 * Returns 0 for success.
273 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700274static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800275 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800276 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700277 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800278
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800279 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
280 * jump to different firmware copy. The second EC_RES_SUCCESS would
281 * set the OBF=1 and the next command cannot be executed.
282 * Thus, we call EC to jump only if the target is different.
283 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700284 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700285 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800286 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700287 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900288 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800289
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800290 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900291
292 /* Translate target --> EC reboot command parameter */
293 switch (target) {
294 case EC_IMAGE_RO:
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700295 /*
296 * Do a cold reset instead of JUMP_RO so board enabling
297 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
298 * This is true for EC enabling RWSIG, where
299 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
300 */
301 if (rwsig_enabled)
302 p.cmd = EC_REBOOT_COLD;
303 else
304 p.cmd = EC_REBOOT_JUMP_RO;
Simon Glassc453a642013-07-01 18:08:53 +0900305 break;
306 case EC_IMAGE_RW:
307 p.cmd = EC_REBOOT_JUMP_RW;
308 break;
309 default:
310 /*
311 * If target is unspecified, set EC reboot command to use
312 * a new image. Also set "target" so that it may be used
313 * to update the priv->current_image if jump is successful.
314 */
315 if (fwcopy[EC_IMAGE_RO].flags) {
316 p.cmd = EC_REBOOT_JUMP_RO;
317 target = EC_IMAGE_RO;
318 } else if (fwcopy[EC_IMAGE_RW].flags) {
319 p.cmd = EC_REBOOT_JUMP_RW;
320 target = EC_IMAGE_RW;
321 } else {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700322 return 1;
Simon Glassc453a642013-07-01 18:08:53 +0900323 }
324 break;
325 }
326
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700327 if (p.cmd == EC_REBOOT_COLD)
328 msg_pdbg("Doing a cold reboot instead of JUMP_RO/RW.\n");
329 else
330 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800331
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700332 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800333 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700334 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800335 return 0;
336 }
337
Souvik Ghosh586968a2016-08-11 17:56:24 -0700338 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700339 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800340 if (rc < 0) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700341 msg_perr("CROS_EC cannot jump/reboot to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800342 sections[target], rc);
343 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800344 }
345
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800346 /* Sleep until EC can respond to host command, but just before
347 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
348 usleep(EC_INIT_DELAY);
349
350 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
351 if (target == EC_IMAGE_RO && rwsig_enabled) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700352 msg_pdbg("Aborting RWSIG jump.\n");
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800353 ec_rwsig_abort();
354 }
355
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700356 msg_pdbg("CROS_EC jumped/rebooted to [%s]\n", sections[target]);
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800357 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800358
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700359 return EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800360}
361
David Hendricksb64b39a2016-10-11 13:48:06 -0700362static int cros_ec_restore_wp(void *data)
363{
364 msg_pdbg("Restoring EC soft WP.\n");
365 return set_wp(1);
366}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800367
David Hendricksb64b39a2016-10-11 13:48:06 -0700368static int cros_ec_wp_is_enabled(void)
369{
370 struct ec_params_flash_protect p;
371 struct ec_response_flash_protect r;
372 int rc;
373
374 memset(&p, 0, sizeof(p));
375 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
376 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
377 if (rc < 0) {
378 msg_perr("FAILED: Cannot get the write protection status: %d\n",
379 rc);
380 return -1;
381 } else if (rc < sizeof(r)) {
382 msg_perr("FAILED: Too little data returned (expected:%zd, "
383 "actual:%d)\n", sizeof(r), rc);
384 return -1;
385 }
386
387 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
388 return 1;
389
390 return 0;
391}
392
393/*
394 * Prepare EC for update:
395 * - Disable soft WP if needed.
396 * - Parse flashmap.
397 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800398 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700399int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800400 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700401 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800402
Souvik Ghosh586968a2016-08-11 17:56:24 -0700403 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800404
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800405 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
406 rwsig_enabled = 1;
407 msg_pdbg("EC has RWSIG enabled.\n");
408 }
409
David Hendricksb64b39a2016-10-11 13:48:06 -0700410 /*
411 * If HW WP is disabled we may still need to disable write protection
412 * that is active on the EC. Otherwise the EC can reject erase/write
413 * commands.
414 *
415 * Failure is OK since HW WP might be enabled or the EC needs to be
416 * rebooted for the change to take effect. We can still update RW
417 * portions.
418 *
419 * If disabled here, EC WP will be restored at the end so that
420 * "--wp-enable" does not need to be run later. This greatly
421 * simplifies logic for developers and scripts.
422 */
423 wp_status = cros_ec_wp_is_enabled();
424 if (wp_status < 0) {
425 return 1;
426 } else if (wp_status == 1) {
427 msg_pdbg("Attempting to disable EC soft WP.\n");
428 if (!set_wp(0)) {
429 msg_pdbg("EC soft WP disabled successfully.\n");
430 if (register_shutdown(cros_ec_restore_wp, NULL))
431 return 1;
432 } else {
433 msg_pdbg("Failed. Hardware WP might in effect or EC "
434 "needs to be rebooted first.\n");
435 }
436 } else {
437 msg_pdbg("EC soft WP is already disabled.\n");
438 }
439
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800440 // Parse the fmap in the image file and cache the firmware ranges.
441 fmap = fmap_find_in_memory(image, size);
Nicolas Boichata7a062b2018-07-18 15:18:41 +0800442 if (fmap) {
443 // Lookup RO/A/B sections in FMAP.
444 for (i = 0; i < fmap->nareas; i++) {
445 struct fmap_area *fa = &fmap->areas[i];
446 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
447 if (!strcmp(sections[j],
448 (const char *)fa->name)) {
449 msg_pdbg("Found '%s' in image.\n",
450 fa->name);
451 memcpy(&fwcopy[j], fa, sizeof(*fa));
452 fwcopy[j].flags = 1; // mark as new
453 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800454 }
455 }
456 }
457
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700458 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0) {
459 msg_pwarn("Skip jumping to RO\n");
460 return 0;
461 }
462 /* Warning: before update, we jump the EC to RO copy. If you
463 * want to change this behavior, please also check the
464 * cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800465 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700466 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800467}
468
469
470/* Returns >0 if we need 2nd pass of erase_and_write_flash().
471 * <0 if we cannot jump to any firmware copy.
472 * ==0 if no more pass is needed.
473 *
474 * This function also jumps to new-updated firmware copy before return >0.
475 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700476int cros_ec_need_2nd_pass(void)
477{
478 if (!(cros_ec_priv && cros_ec_priv->detected))
479 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800480
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700481 if (!need_2nd_pass)
482 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800483
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700484 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0)
485 /* EC_RES_ACCESS_DENIED is returned when the block is either
486 * protected or unsafe. Thus, theoretically, we shouldn't reach
487 * here because everywhere is safe for EXEC_IN_RAM chips and
488 * WP is disabled before erase/write cycle starts.
489 * We can still let the 2nd pass run (and it will probably
490 * fail again).
491 */
492 return 1;
493
494 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN))
495 return -1;
496
497 return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800498}
499
500
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800501/* Returns 0 for success.
502 *
503 * Try latest firmware: B > A > RO
504 *
David Hendricksb907de32014-08-11 16:47:09 -0700505 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800506 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700507 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800508 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700509int cros_ec_finish(void)
510{
Souvik Ghosh586968a2016-08-11 17:56:24 -0700511 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800512
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800513 /* For EC with RWSIG enabled. We need a cold reboot to enable
514 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
515 */
516 if (rwsig_enabled) {
517 int rc;
518
519 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
520 rc = cros_ec_cold_reboot(0);
521 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
522 return rc;
523 }
524
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800525 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800526 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700527 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
528 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800529 }
530
531 return 0;
532}
533
534
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700535int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700536 unsigned int blockaddr, unsigned int readcnt)
537{
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800538 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800539 struct ec_params_flash_read p;
Craig Hesling65eb8812019-08-01 09:33:56 -0700540 int maxlen = opaque_master->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800541 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700542 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800543
David Hendricks133083b2012-07-17 20:39:38 -0700544 while (offset < readcnt) {
545 count = min(maxlen, readcnt - offset);
546 p.offset = blockaddr + offset;
547 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700548 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700549 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800550 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700551 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700552 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800553 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800554 } else {
555 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800556 }
557
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800558 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700559 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800560 }
561
562 return rc;
563}
564
565
Simon Glassc453a642013-07-01 18:08:53 +0900566/*
567 * returns 0 to indicate area does not overlap current EC image
568 * returns 1 to indicate area overlaps current EC image or error
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700569 *
570 * We can't get rid of this. The ECs should know what region is safe to erase
571 * or write. We should let them decide (and return EC_RES_ACCESS_DENIED).
572 * Not all existing EC firmware can do so.
Simon Glassc453a642013-07-01 18:08:53 +0900573 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700574static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900575{
Simon Glassc453a642013-07-01 18:08:53 +0900576 enum ec_current_image image;
577 uint32_t region_offset;
578 uint32_t region_size;
579
Souvik Ghosh586968a2016-08-11 17:56:24 -0700580 image = cros_ec_priv->current_image;
581 region_offset = cros_ec_priv->region[image].offset;
582 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900583
584 if ((addr + len - 1 < region_offset) ||
585 (addr > region_offset + region_size - 1)) {
586 return 0;
587 }
588 return 1;
589}
590
591
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700592int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800593 unsigned int blockaddr,
594 unsigned int len) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700595 struct ec_params_flash_erase_v1 erase;
596 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700597 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800598
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700599 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
600 in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700601 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900602 need_2nd_pass = 1;
603 return ACCESS_DENIED;
604 }
605
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700606 erase.params.offset = blockaddr;
607 erase.params.size = len;
608 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
609 if (rc < 0) {
610 msg_perr("Cannot determine erase command version\n");
611 return 0;
612 }
613 cmd_version = 31 - __builtin_clz(mask);
614
615 if (cmd_version == 0) {
616 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
617 &erase.params,
618 sizeof(struct ec_params_flash_erase), NULL, 0);
619 if (rc == -EC_RES_ACCESS_DENIED) {
620 // this is active image.
621 cros_ec_invalidate_copy(blockaddr, len);
622 need_2nd_pass = 1;
623 return ACCESS_DENIED;
624 }
625 if (rc < 0) {
626 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
627 blockaddr, rc);
628 return rc;
629 }
630 goto end_flash_erase;
631 }
632
633 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
634 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
635 } else {
636 erase.cmd = FLASH_ERASE_SECTOR;
637 }
638 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
639 &erase, sizeof(erase), NULL, 0);
640 switch (rc) {
641 case 0:
642 break;
643 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800644 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700645 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800646 need_2nd_pass = 1;
647 return ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700648 case -EC_RES_BUSY:
649 msg_perr("CROS_EC: Flash erase command "
650 " already in progress\n");
651 default:
652 return rc;
653 }
654 if (len < FLASH_SMALL_REGION_THRESHOLD)
655 goto end_flash_erase;
656
657 /* Wait for the erase command to complete */
658 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700659
660/* wait up to 10s to erase a flash sector */
661#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
662/* wait .5 second between queries. */
663#define CROS_EC_ERASE_ASYNC_WAIT 500000
664
665 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
666 usleep(CROS_EC_ERASE_ASYNC_WAIT);
667 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700668 erase.cmd = FLASH_ERASE_GET_RESULT;
669 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
670 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800671 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800672 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700673 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800674 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800675 return rc;
676 }
677
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700678end_flash_erase:
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800679#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800680 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800681#endif
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700682 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700683 /*
684 * Can happen if the command with retried with
685 * EC_CMD_GET_COMMS_STATUS
686 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700687 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700688 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800689 return rc;
690}
691
692
Patrick Georgiab8353e2017-02-03 18:32:01 +0100693int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800694 unsigned int nbytes) {
695 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800696 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800697 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700698 uint8_t *packet;
699
Ken Chang69c31b82014-10-28 15:17:21 +0800700 /*
701 * For chrome-os-partner:33035, to workaround the undersized
702 * outdata buffer issue in kernel.
703 */
Craig Hesling65eb8812019-08-01 09:33:56 -0700704 real_write_size = min(opaque_master->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700705 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800706 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700707 if (!packet)
708 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800709
710 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800711 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800712 p.offset = addr + i;
713 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900714
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700715 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
716 in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700717 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900718 need_2nd_pass = 1;
719 return ACCESS_DENIED;
720 }
721
David Hendricks2d6db772013-07-10 21:07:48 -0700722 memcpy(packet, &p, sizeof(p));
723 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700724 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700725 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700726
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800727 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800728 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700729 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800730 need_2nd_pass = 1;
731 return ACCESS_DENIED;
732 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800733
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800734 if (rc < 0) break;
735 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800736 }
737
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800738#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800739 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800740#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700741 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800742 return rc;
743}
744
745
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700746static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900747 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800748 int rc;
749
Souvik Ghosh586968a2016-08-11 17:56:24 -0700750 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800751 if (rc < 0) {
752 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
753 return 1;
754 }
755
756 msg_pinfo("Supported write protect range:\n");
757 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900758 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
759 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800760
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800761 return 0;
762}
763
764
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800765/*
766 * Helper function for flash protection.
767 *
768 * On EC API v1, the EC write protection has been simplified to one-bit:
769 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
770 * or disabled. However, this is different from the SPI-style write protect
771 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
772 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
773 *
774 * SRP Range | PROTECT_RO_AT_BOOT
775 * 0 0 | 0
776 * 0 non-zero | 1
777 * 1 0 | 1
778 * 1 non-zero | 1
779 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800780 *
781 * Besides, to make the protection take effect as soon as possible, we
782 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
783 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800784 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700785static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800786 struct ec_params_flash_protect p;
787 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800788 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
789 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
790 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800791 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800792
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800793 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800794 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800795 p.mask = (ro_at_boot_flag | ro_now_flag);
796 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700797 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700798 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800799 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800800 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
801 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800802 return 1;
803 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800804
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800805 /* Read back */
806 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700807 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700808 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800809 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800810 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
811 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800812 return 1;
813 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800814
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800815 if (!enable) {
816 /* The disable case is easier to check. */
817 if (r.flags & ro_at_boot_flag) {
818 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
819 return 1;
820 } else if (r.flags & ro_now_flag) {
821 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
822 need_an_ec_cold_reset = 1;
823 goto exit;
824 }
825
826 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
827 return 0;
828 }
829
830 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
831 if (r.flags & ro_at_boot_flag) {
832 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
833 } else {
834 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
835 return 1;
836 }
837
838 /* Then, we check if the protection has been activated. */
839 if (r.flags & ro_now_flag) {
840 /* Good, RO_NOW is set. */
841 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
842 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800843 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
844
845 memset(&p, 0, sizeof(p));
846 p.mask = EC_FLASH_PROTECT_ALL_NOW;
847 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700848 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800849 EC_VER_FLASH_PROTECT,
850 &p, sizeof(p), &r, sizeof(r));
851 if (rc < 0) {
852 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
853 return 1;
854 }
855
856 /* Read back */
857 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700858 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800859 EC_VER_FLASH_PROTECT,
860 &p, sizeof(p), &r, sizeof(r));
861 if (rc < 0) {
862 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
863 return 1;
864 }
865
866 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
867 msg_perr("FAILED: ALL_NOW is not set.\n");
868 need_an_ec_cold_reset = 1;
869 goto exit;
870 }
871
872 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
873
874 /*
875 * Our goal is to protect the RO ASAP. The entire protection
876 * is just a workaround for platform not supporting RO_NOW.
877 * It has side-effect that the RW is also protected and leads
878 * the RW update failed. So, we arrange an EC code reset to
879 * unlock RW ASAP.
880 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800881 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800882 if (rc < 0) {
883 msg_perr("WARN: Cannot arrange a cold reset at next "
884 "shutdown to unlock entire protect.\n");
885 msg_perr(" But you can do it manually.\n");
886 } else {
887 msg_pdbg("INFO: A cold reset is arranged at next "
888 "shutdown.\n");
889 }
890
891 } else {
892 msg_perr("FAILED: RO_NOW is not set.\n");
893 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
894 "make write protection active now.\n");
895 need_an_ec_cold_reset = 1;
896 }
897
898exit:
899 if (need_an_ec_cold_reset) {
900 msg_perr("FAILED: You may need a reboot to take effect of "
901 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800902 return 1;
903 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800904
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800905 return 0;
906}
907
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700908static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800909 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900910 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800911 int rc;
912
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800913 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700914 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800915 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800916 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800917 return 1;
918 }
919 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900920 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800921 /* pass */
922 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800923 msg_perr("FAILED: Unsupported write protection range "
924 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800925 msg_perr("Currently supported range:\n");
926 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900927 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
928 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800929 return 1;
930 }
931
David Hendricks393deec2016-11-23 16:15:05 -0800932 if (ignore_wp_range_command)
933 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700934 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800935}
936
937
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700938static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700939 enum wp_mode wp_mode) {
940 int ret;
941
942 switch (wp_mode) {
943 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700944 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700945 break;
946 default:
947 msg_perr("%s():%d Unsupported write-protection mode\n",
948 __func__, __LINE__);
949 ret = 1;
950 break;
951 }
952
953 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800954}
955
956
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700957static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800958 /* --wp-range implicitly enables write protection on CrOS EC, so force
959 it not to if --wp-disable is what the user really wants. */
960 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700961 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800962}
963
964
Souvik Ghosh586968a2016-08-11 17:56:24 -0700965static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800966 struct ec_params_flash_protect p;
967 struct ec_response_flash_protect r;
968 int start, len; /* wp range */
969 int enabled;
970 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800971
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800972 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700973 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700974 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800975 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800976 msg_perr("FAILED: Cannot get the write protection status: %d\n",
977 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800978 return 1;
979 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700980 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800981 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800982 return 1;
983 }
984
985 start = len = 0;
986 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900987 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800988
989 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
990 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700991 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800992 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800993 msg_perr("FAILED: Cannot get the WP_RO region info: "
994 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800995 return 1;
996 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900997 start = info.offset;
998 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800999 } else {
1000 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
1001 __func__);
1002 }
1003
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +08001004 /*
1005 * If neither RO_NOW or ALL_NOW is set, it means write protect is
1006 * NOT active now.
1007 */
1008 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
1009 start = len = 0;
1010
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001011 /* Remove the SPI-style messages. */
1012 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
1013 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
1014 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001015 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001016 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001017 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001018 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001019
1020 return 0;
1021}
1022
David Hendrickse5454932013-11-04 18:16:11 -08001023/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -07001024int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -08001025{
1026 struct ec_params_hello request;
1027 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -08001028 int rc = 0;
1029
1030 /* Say hello to EC. */
1031 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
1032 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
1033 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001034 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001035 sizeof(request), &response, sizeof(response));
1036 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1037
1038 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1039 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1040 "rc=%d, request=0x%x response=0x%x\n",
1041 rc, request.in_data, response.out_data);
1042 return 1;
1043 }
1044
1045 return 0;
1046}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001047
David Hendricksd13d90d2016-08-09 17:00:52 -07001048void cros_ec_set_max_size(struct cros_ec_priv *priv,
Edward O'Callaghanabd30192019-05-14 15:58:19 +10001049 struct opaque_master *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001050 struct ec_response_get_protocol_info info;
1051 int rc = 0;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001052
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001053 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001054 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001055 &info, sizeof(info));
1056 msg_pdbg("%s: rc:%d\n", __func__, rc);
1057
Gwendal Grignoucf540ef2017-08-10 12:10:06 -07001058 /*
1059 * Use V3 large size only if v2 protocol is not supported.
1060 * When v2 is supported, we may be using a kernel without v3 support,
1061 * leading to sending larger commands the kernel can support.
1062 */
1063 if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001064 op->max_data_write = info.max_request_packet_size -
1065 sizeof(struct ec_host_request);
1066 op->max_data_read = info.max_response_packet_size -
1067 sizeof(struct ec_host_response);
Gwendal Grignouef9062f2017-05-31 17:38:31 -07001068 /*
1069 * Due to a bug in NPCX SPI code (chromium:725580),
1070 * The EC may responds 163 when it meant 160; it should not
1071 * have included header and footer.
1072 */
1073 op->max_data_read &= ~3;
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001074 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1075 op->max_data_write, op->max_data_read);
1076 }
1077}
1078
David Hendricks14935fe2014-08-14 17:38:24 -07001079
1080/*
David Hendricks052446b2014-09-11 11:26:51 -07001081 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001082 *
1083 * This function parses programmer parameters from the command line. Since
1084 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1085 * run during internal programmer initialization.
1086 */
1087int cros_ec_parse_param(struct cros_ec_priv *priv)
1088{
David Hendricks98b3c572016-11-30 01:50:08 +00001089 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001090
David Hendricks98b3c572016-11-30 01:50:08 +00001091 p = extract_programmer_param("dev");
1092 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -07001093 unsigned int index;
1094 char *endptr = NULL;
1095
1096 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001097 /*
1098 * For backward compatibility, check if the index is
1099 * a number: 0: main EC, 1: PD
1100 * works only on Samus.
1101 */
David Hendricks98b3c572016-11-30 01:50:08 +00001102 index = strtoul(p, &endptr, 10);
1103 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
1104 msg_perr("Invalid argument: \"%s\"\n", p);
1105 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001106 }
1107
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001108 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -07001109 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001110 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001111 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001112 priv->dev = ec_type[index];
1113 msg_pdbg("Target %s used\n", priv->dev);
1114 }
David Hendricks14935fe2014-08-14 17:38:24 -07001115
David Hendricks98b3c572016-11-30 01:50:08 +00001116 p = extract_programmer_param("type");
1117 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001118 unsigned int index;
1119 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001120 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001121 break;
1122 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001123 msg_perr("Invalid argument: \"%s\"\n", p);
1124 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001125 }
1126 priv->dev = ec_type[index];
1127 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001128 }
1129
David Hendricks98b3c572016-11-30 01:50:08 +00001130 p = extract_programmer_param("block");
1131 if (p) {
1132 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001133 char *endptr = NULL;
1134
1135 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001136 block = strtoul(p, &endptr, 0);
1137 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1138 msg_perr("Invalid argument: \"%s\"\n", p);
1139 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001140 }
1141
David Hendricks98b3c572016-11-30 01:50:08 +00001142 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001143 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001144 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001145 }
1146
David Hendricks98b3c572016-11-30 01:50:08 +00001147 msg_pdbg("Override block size to 0x%x\n", block);
1148 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001149 }
1150
David Hendricks98b3c572016-11-30 01:50:08 +00001151 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001152}
1153
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001154int cros_ec_probe_size(struct flashctx *flash) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001155 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001156 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001157 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001158 struct block_eraser *eraser;
1159 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001160 .list_ranges = cros_ec_list_ranges,
1161 .set_range = cros_ec_set_range,
1162 .enable = cros_ec_enable_writeprotect,
1163 .disable = cros_ec_disable_writeprotect,
1164 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001165 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001166 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001167
Souvik Ghosh586968a2016-08-11 17:56:24 -07001168 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001169 if (rc < 0) {
1170 msg_perr("%s(): Failed to probe (no current image): %d\n",
1171 __func__, rc);
1172 return 0;
1173 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001174 cros_ec_priv->current_image = rc;
1175 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001176
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001177 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1178 if (rc < 0) {
1179 msg_perr("Cannot determine write command version\n");
1180 return 0;
1181 }
1182 cmd_version = 31 - __builtin_clz(mask);
1183
Patrick Georgif3fa2992017-02-02 16:24:44 +01001184 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001185 flash->chip->wp = &wp;
Craig Hesling65eb8812019-08-01 09:33:56 -07001186 flash->chip->page_size = opaque_master->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001187
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001188 if (cmd_version < 2) {
1189 struct ec_response_flash_info_1 info;
1190 /* Request general information about flash (v1 or below). */
1191 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1192 NULL, 0, &info,
1193 (cmd_version > 0 ? sizeof(info) :
1194 sizeof(struct ec_response_flash_info)));
1195 if (rc < 0) {
1196 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1197 cmd_version, rc);
1198 return 0;
1199 }
1200 if (cmd_version == 0) {
1201 cros_ec_priv->ideal_write_size =
1202 EC_FLASH_WRITE_VER0_SIZE;
1203 } else {
1204 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1205 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1206 flash->chip->feature_bits |=
1207 FEATURE_ERASE_TO_ZERO;
1208 }
1209 flash->chip->total_size = info.flash_size / 1024;
1210
1211 /* Allow overriding the erase block size in case EC is incorrect */
1212 if (cros_ec_priv->erase_block_size > 0)
1213 eraser->eraseblocks[0].size =
1214 cros_ec_priv->erase_block_size;
1215 else
1216 eraser->eraseblocks[0].size = info.erase_block_size;
1217
1218 eraser->eraseblocks[0].count = info.flash_size /
1219 eraser->eraseblocks[0].size;
1220 } else {
1221 struct ec_response_flash_info_2 info_2;
1222 struct ec_params_flash_info_2 params_2;
1223 struct ec_response_flash_info_2 *info_2_p = &info_2;
1224 int size_info_v2 = sizeof(info_2), i;
1225
1226 params_2.num_banks_desc = 0;
1227 /*
1228 * Call FLASH_INFO twice, second time with all banks
1229 * information.
1230 */
1231 for (i = 0; i < 2; i++) {
1232 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1233 cmd_version, &params_2,
1234 sizeof(params_2),
1235 info_2_p, size_info_v2);
1236 if (rc < 0) {
1237 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1238 __func__,
1239 params_2.num_banks_desc,
1240 cmd_version, rc);
1241 if (info_2_p != &info_2)
1242 free(info_2_p);
1243 return 0;
1244 } else if (i > 0) {
1245 break;
1246 }
1247 params_2.num_banks_desc = info_2_p->num_banks_total;
1248 size_info_v2 += info_2_p->num_banks_total *
1249 sizeof(struct ec_flash_bank);
1250
1251 info_2_p = malloc(size_info_v2);
1252 if (!info_2_p) {
1253 msg_perr("%s(): malloc of %d banks failed\n",
1254 __func__, info_2_p->num_banks_total);
1255 return 0;
1256 }
1257 }
1258 flash->chip->total_size = info_2_p->flash_size / 1024;
1259 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1260 /* Allow overriding the erase block size in case EC is incorrect */
1261 eraser->eraseblocks[i].size =
1262 (cros_ec_priv->erase_block_size > 0 ?
1263 cros_ec_priv->erase_block_size :
1264 1 << info_2_p->banks[i].erase_size_exp);
1265 eraser->eraseblocks[i].count =
1266 info_2_p->banks[i].count <<
1267 (info_2_p->banks[i].size_exp -
1268 info_2_p->banks[i].erase_size_exp);
1269 }
1270 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001271#if 0
1272 /*
1273 * TODO(b/38506987)Comment out, as some firmware were not
1274 * setting this flag properly.
1275 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001276 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
1277 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001278#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001279 free(info_2_p);
1280 }
Vadim Bendeburyadbd7062018-06-19 21:36:45 -07001281 eraser->block_erase = cros_ec_block_erase;
David Hendricks194b3bb2013-07-16 14:32:26 -07001282 /*
1283 * Some STM32 variants erase bits to 0. For now, assume that this
1284 * applies to STM32L parts.
1285 *
1286 * FIXME: This info will eventually be exposed via some EC command.
1287 * See chrome-os-partner:20973.
1288 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001289 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001290 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001291 if (rc < 0) {
1292 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1293 return 0;
1294 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001295 if (!strncmp(chip_info.name, "stm32l1", 7))
Patrick Georgif3fa2992017-02-02 16:24:44 +01001296 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001297
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001298
David Hendricksf9461c72013-07-11 19:02:13 -07001299
David Hendricksa672b042016-09-19 12:37:36 -07001300 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1301 0, NULL, 0, &spi_info, sizeof(spi_info));
1302 if (rc < 0) {
1303 static char chip_vendor[32];
1304 static char chip_name[32];
1305
1306 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1307 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001308 flash->chip->vendor = chip_vendor;
1309 flash->chip->name = chip_name;
1310 flash->chip->tested = TEST_OK_PREWU;
David Hendricksa672b042016-09-19 12:37:36 -07001311 } else {
1312 const struct flashchip *f;
1313 uint32_t mfg = spi_info.jedec[0];
1314 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1315
1316 for (f = flashchips; f && f->name; f++) {
1317 if (f->bustype != BUS_SPI)
1318 continue;
1319 if ((f->manufacture_id == mfg) &&
1320 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001321 flash->chip->vendor = f->vendor;
1322 flash->chip->name = f->name;
1323 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001324 break;
1325 }
1326 }
1327 }
1328
Simon Glassc453a642013-07-01 18:08:53 +09001329 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1330 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001331 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1332 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001333 if (rc) {
1334 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1335 __func__, rc);
1336 return 0;
1337 }
1338
Souvik Ghosh586968a2016-08-11 17:56:24 -07001339 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1340 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001341 if (rc) {
1342 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1343 __func__, rc);
1344 return 0;
1345 }
1346
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001347 return 1;
1348};