blob: e1081485958c86d6796d0606a091a5e9b992e5d2 [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"
Hung-Te Lin25ffcca2019-10-03 22:57:32 +080041#include "layout.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070042#include "cros_ec.h"
43#include "cros_ec_lock.h"
44#include "cros_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080045#include "programmer.h"
46#include "spi.h"
47#include "writeprotect.h"
48
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080049/* FIXME: used for wp hacks */
50#include <sys/types.h>
51#include <sys/stat.h>
52#include <fcntl.h>
53#include <unistd.h>
Souvik Ghosh586968a2016-08-11 17:56:24 -070054
55struct cros_ec_priv *cros_ec_priv;
David Hendricks393deec2016-11-23 16:15:05 -080056static int ignore_wp_range_command = 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -070057
David Hendricksb64b39a2016-10-11 13:48:06 -070058static int set_wp(int enable); /* FIXME: move set_wp() */
59
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080060struct wp_data {
61 int enable;
62 unsigned int start;
63 unsigned int len;
64};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080065#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
66
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080067/* If software sync is enabled, then we don't try the latest firmware copy
68 * after updating.
69 */
70#define SOFTWARE_SYNC_ENABLED
71
Gwendal Grignoua36ff502015-03-23 16:36:47 -070072/* For region larger use async version for FLASH_ERASE */
73#define FLASH_SMALL_REGION_THRESHOLD (16 * 1024)
74
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080075/* 1 if we want the flashrom to call erase_and_write_flash() again. */
76static int need_2nd_pass = 0;
77
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080078/* 1 if we want the flashrom to try jumping to new firmware after update. */
79static int try_latest_firmware = 0;
80
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +080081/* 1 if EC firmware has RWSIG enabled. */
82static int rwsig_enabled = 0;
83
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080084/* The range of each firmware copy from the image file to update.
85 * But re-define the .flags as the valid flag to indicate the firmware is
86 * new or not (if flags = 1).
87 */
88static struct fmap_area fwcopy[4]; // [0] is not used.
89
90/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080091static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070092 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
93 "EC_RO",
94 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080095};
96
Gwendal Grignou94e87d62014-11-25 15:34:15 -080097/*
98 * The names of the different device that can be found in a machine.
99 * Order is important: for backward compatibilty issue,
100 * 'ec' must be 0, 'pd' must be 1.
101 */
102static const char *ec_type[] = {
103 [0] = "ec",
104 [1] = "pd",
105 [2] = "sh",
Vincent Palatin4faff9a2017-03-17 17:27:39 +0100106 [3] = "fp",
Wei-Ning Huang78397842017-05-05 21:45:47 +0800107 [4] = "tp",
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800108};
109
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700110static struct ec_response_flash_region_info regions[EC_FLASH_REGION_COUNT];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800111
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800112/*
113 * Delay after reboot before EC can respond to host command.
114 * This value should be large enough for EC to initialize, but no larger than
115 * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be
116 * able to abort RWSIG jump and stay in RO.
117 */
118#define EC_INIT_DELAY 800000
119
120/*
121 * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW.
122 */
123#define EC_RWSIG_JUMP_TO_RW_DELAY 3000000
124
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800125/* Given the range not able to update, mark the corresponding
126 * firmware as old.
127 */
David Hendricksb907de32014-08-11 16:47:09 -0700128static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800129{
130 int i;
131
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800132 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800133 struct fmap_area *fw = &fwcopy[i];
134 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
135 (fw->offset >= addr && (fw->offset < addr + len))) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -0700136 msg_pdbg(" OLD[%s]", sections[i]);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800137 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
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800254/* Perform a cold reboot.
255 *
256 * @param flags flags to pass to EC_CMD_REBOOT_EC.
257 * @return 0 for success, < 0 for command failure.
258 */
259static int cros_ec_cold_reboot(int flags) {
260 struct ec_params_reboot_ec p;
261
262 memset(&p, 0, sizeof(p));
263 p.cmd = EC_REBOOT_COLD;
264 p.flags = flags;
265 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
266 NULL, 0);
267}
268
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800269/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800270 * then this functions picks a NEW firmware copy and jumps to it. Note that
271 * RO is preferred, then A, finally B.
272 *
273 * Returns 0 for success.
274 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700275static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800276 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800277 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700278 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800279
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800280 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
281 * jump to different firmware copy. The second EC_RES_SUCCESS would
282 * set the OBF=1 and the next command cannot be executed.
283 * Thus, we call EC to jump only if the target is different.
284 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700285 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700286 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800287 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700288 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900289 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800290
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800291 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900292
293 /* Translate target --> EC reboot command parameter */
294 switch (target) {
295 case EC_IMAGE_RO:
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700296 /*
297 * Do a cold reset instead of JUMP_RO so board enabling
298 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
299 * This is true for EC enabling RWSIG, where
300 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
301 */
302 if (rwsig_enabled)
303 p.cmd = EC_REBOOT_COLD;
304 else
305 p.cmd = EC_REBOOT_JUMP_RO;
Simon Glassc453a642013-07-01 18:08:53 +0900306 break;
307 case EC_IMAGE_RW:
308 p.cmd = EC_REBOOT_JUMP_RW;
309 break;
310 default:
311 /*
312 * If target is unspecified, set EC reboot command to use
313 * a new image. Also set "target" so that it may be used
314 * to update the priv->current_image if jump is successful.
315 */
316 if (fwcopy[EC_IMAGE_RO].flags) {
317 p.cmd = EC_REBOOT_JUMP_RO;
318 target = EC_IMAGE_RO;
319 } else if (fwcopy[EC_IMAGE_RW].flags) {
320 p.cmd = EC_REBOOT_JUMP_RW;
321 target = EC_IMAGE_RW;
322 } else {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700323 return 1;
Simon Glassc453a642013-07-01 18:08:53 +0900324 }
325 break;
326 }
327
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700328 if (p.cmd == EC_REBOOT_COLD)
329 msg_pdbg("Doing a cold reboot instead of JUMP_RO/RW.\n");
330 else
331 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800332
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700333 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800334 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700335 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800336 return 0;
337 }
338
Souvik Ghosh586968a2016-08-11 17:56:24 -0700339 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700340 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800341 if (rc < 0) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700342 msg_perr("CROS_EC cannot jump/reboot to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800343 sections[target], rc);
344 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800345 }
346
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800347 /* Sleep until EC can respond to host command, but just before
348 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
349 usleep(EC_INIT_DELAY);
350
351 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
352 if (target == EC_IMAGE_RO && rwsig_enabled) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700353 msg_pdbg("Aborting RWSIG jump.\n");
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800354 ec_rwsig_abort();
355 }
356
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700357 msg_pdbg("CROS_EC jumped/rebooted to [%s]\n", sections[target]);
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800358 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800359
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700360 return EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800361}
362
David Hendricksb64b39a2016-10-11 13:48:06 -0700363static int cros_ec_restore_wp(void *data)
364{
365 msg_pdbg("Restoring EC soft WP.\n");
366 return set_wp(1);
367}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800368
David Hendricksb64b39a2016-10-11 13:48:06 -0700369static int cros_ec_wp_is_enabled(void)
370{
371 struct ec_params_flash_protect p;
372 struct ec_response_flash_protect r;
373 int rc;
374
375 memset(&p, 0, sizeof(p));
376 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
377 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
378 if (rc < 0) {
379 msg_perr("FAILED: Cannot get the write protection status: %d\n",
380 rc);
381 return -1;
382 } else if (rc < sizeof(r)) {
383 msg_perr("FAILED: Too little data returned (expected:%zd, "
384 "actual:%d)\n", sizeof(r), rc);
385 return -1;
386 }
387
388 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
389 return 1;
390
391 return 0;
392}
393
394/*
395 * Prepare EC for update:
396 * - Disable soft WP if needed.
397 * - Parse flashmap.
398 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800399 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700400int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800401 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700402 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800403
Souvik Ghosh586968a2016-08-11 17:56:24 -0700404 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800405
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800406 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
407 rwsig_enabled = 1;
408 msg_pdbg("EC has RWSIG enabled.\n");
409 }
410
David Hendricksb64b39a2016-10-11 13:48:06 -0700411 /*
412 * If HW WP is disabled we may still need to disable write protection
413 * that is active on the EC. Otherwise the EC can reject erase/write
414 * commands.
415 *
416 * Failure is OK since HW WP might be enabled or the EC needs to be
417 * rebooted for the change to take effect. We can still update RW
418 * portions.
419 *
420 * If disabled here, EC WP will be restored at the end so that
421 * "--wp-enable" does not need to be run later. This greatly
422 * simplifies logic for developers and scripts.
423 */
424 wp_status = cros_ec_wp_is_enabled();
425 if (wp_status < 0) {
426 return 1;
427 } else if (wp_status == 1) {
428 msg_pdbg("Attempting to disable EC soft WP.\n");
429 if (!set_wp(0)) {
430 msg_pdbg("EC soft WP disabled successfully.\n");
431 if (register_shutdown(cros_ec_restore_wp, NULL))
432 return 1;
433 } else {
434 msg_pdbg("Failed. Hardware WP might in effect or EC "
435 "needs to be rebooted first.\n");
436 }
437 } else {
438 msg_pdbg("EC soft WP is already disabled.\n");
439 }
440
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800441 // Parse the fmap in the image file and cache the firmware ranges.
442 fmap = fmap_find_in_memory(image, size);
Nicolas Boichata7a062b2018-07-18 15:18:41 +0800443 if (fmap) {
444 // Lookup RO/A/B sections in FMAP.
445 for (i = 0; i < fmap->nareas; i++) {
446 struct fmap_area *fa = &fmap->areas[i];
447 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
448 if (!strcmp(sections[j],
449 (const char *)fa->name)) {
450 msg_pdbg("Found '%s' in image.\n",
451 fa->name);
452 memcpy(&fwcopy[j], fa, sizeof(*fa));
453 fwcopy[j].flags = 1; // mark as new
454 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800455 }
456 }
457 }
458
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700459 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0) {
460 msg_pwarn("Skip jumping to RO\n");
461 return 0;
462 }
Hung-Te Lin25ffcca2019-10-03 22:57:32 +0800463 /* If not trying latest firmware and doing partial write, we don't have
464 * to always jump to RO (which was designed for update with different
465 * RO/RW sizes).
466 */
467 if (!try_latest_firmware && get_num_include_args()) {
468 msg_pwarn("Skip jumping to RO due to partial write\n");
469 return 0;
470 }
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700471 /* Warning: before update, we jump the EC to RO copy. If you
472 * want to change this behavior, please also check the
473 * cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800474 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700475 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800476}
477
478
479/* Returns >0 if we need 2nd pass of erase_and_write_flash().
480 * <0 if we cannot jump to any firmware copy.
481 * ==0 if no more pass is needed.
482 *
483 * This function also jumps to new-updated firmware copy before return >0.
484 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700485int cros_ec_need_2nd_pass(void)
486{
487 if (!(cros_ec_priv && cros_ec_priv->detected))
488 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800489
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700490 if (!need_2nd_pass)
491 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800492
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700493 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0)
494 /* EC_RES_ACCESS_DENIED is returned when the block is either
495 * protected or unsafe. Thus, theoretically, we shouldn't reach
496 * here because everywhere is safe for EXEC_IN_RAM chips and
497 * WP is disabled before erase/write cycle starts.
498 * We can still let the 2nd pass run (and it will probably
499 * fail again).
500 */
501 return 1;
502
503 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN))
504 return -1;
505
506 return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800507}
508
509
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800510/* Returns 0 for success.
511 *
512 * Try latest firmware: B > A > RO
513 *
David Hendricksb907de32014-08-11 16:47:09 -0700514 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800515 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700516 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800517 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700518int cros_ec_finish(void)
519{
Souvik Ghosh586968a2016-08-11 17:56:24 -0700520 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800521
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800522 /* For EC with RWSIG enabled. We need a cold reboot to enable
523 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
524 */
525 if (rwsig_enabled) {
526 int rc;
527
528 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
529 rc = cros_ec_cold_reboot(0);
530 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
531 return rc;
532 }
533
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800534 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800535 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700536 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
537 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800538 }
539
540 return 0;
541}
542
543
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700544int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700545 unsigned int blockaddr, unsigned int readcnt)
546{
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800547 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800548 struct ec_params_flash_read p;
Craig Hesling65eb8812019-08-01 09:33:56 -0700549 int maxlen = opaque_master->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800550 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700551 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800552
David Hendricks133083b2012-07-17 20:39:38 -0700553 while (offset < readcnt) {
554 count = min(maxlen, readcnt - offset);
555 p.offset = blockaddr + offset;
556 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700557 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700558 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800559 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700560 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700561 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800562 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800563 } else {
564 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800565 }
566
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800567 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700568 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800569 }
570
571 return rc;
572}
573
574
Simon Glassc453a642013-07-01 18:08:53 +0900575/*
576 * returns 0 to indicate area does not overlap current EC image
577 * returns 1 to indicate area overlaps current EC image or error
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700578 *
579 * We can't get rid of this. The ECs should know what region is safe to erase
580 * or write. We should let them decide (and return EC_RES_ACCESS_DENIED).
581 * Not all existing EC firmware can do so.
Simon Glassc453a642013-07-01 18:08:53 +0900582 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700583static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900584{
Simon Glassc453a642013-07-01 18:08:53 +0900585 enum ec_current_image image;
586 uint32_t region_offset;
587 uint32_t region_size;
588
Souvik Ghosh586968a2016-08-11 17:56:24 -0700589 image = cros_ec_priv->current_image;
590 region_offset = cros_ec_priv->region[image].offset;
591 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900592
593 if ((addr + len - 1 < region_offset) ||
594 (addr > region_offset + region_size - 1)) {
595 return 0;
596 }
597 return 1;
598}
599
600
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700601int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800602 unsigned int blockaddr,
603 unsigned int len) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700604 struct ec_params_flash_erase_v1 erase;
605 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700606 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800607
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700608 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
609 in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700610 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900611 need_2nd_pass = 1;
612 return ACCESS_DENIED;
613 }
614
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700615 erase.params.offset = blockaddr;
616 erase.params.size = len;
617 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
618 if (rc < 0) {
619 msg_perr("Cannot determine erase command version\n");
620 return 0;
621 }
622 cmd_version = 31 - __builtin_clz(mask);
623
624 if (cmd_version == 0) {
625 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
626 &erase.params,
627 sizeof(struct ec_params_flash_erase), NULL, 0);
628 if (rc == -EC_RES_ACCESS_DENIED) {
629 // this is active image.
630 cros_ec_invalidate_copy(blockaddr, len);
631 need_2nd_pass = 1;
632 return ACCESS_DENIED;
633 }
634 if (rc < 0) {
635 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
636 blockaddr, rc);
637 return rc;
638 }
639 goto end_flash_erase;
640 }
641
642 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
643 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
644 } else {
645 erase.cmd = FLASH_ERASE_SECTOR;
646 }
647 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
648 &erase, sizeof(erase), NULL, 0);
649 switch (rc) {
650 case 0:
651 break;
652 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800653 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700654 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800655 need_2nd_pass = 1;
656 return ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700657 case -EC_RES_BUSY:
658 msg_perr("CROS_EC: Flash erase command "
659 " already in progress\n");
660 default:
661 return rc;
662 }
663 if (len < FLASH_SMALL_REGION_THRESHOLD)
664 goto end_flash_erase;
665
666 /* Wait for the erase command to complete */
667 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700668
669/* wait up to 10s to erase a flash sector */
670#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
671/* wait .5 second between queries. */
672#define CROS_EC_ERASE_ASYNC_WAIT 500000
673
674 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
675 usleep(CROS_EC_ERASE_ASYNC_WAIT);
676 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700677 erase.cmd = FLASH_ERASE_GET_RESULT;
678 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
679 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800680 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800681 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700682 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800683 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800684 return rc;
685 }
686
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700687end_flash_erase:
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800688#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800689 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800690#endif
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700691 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700692 /*
693 * Can happen if the command with retried with
694 * EC_CMD_GET_COMMS_STATUS
695 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700696 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700697 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800698 return rc;
699}
700
701
Patrick Georgiab8353e2017-02-03 18:32:01 +0100702int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800703 unsigned int nbytes) {
704 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800705 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800706 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700707 uint8_t *packet;
708
Ken Chang69c31b82014-10-28 15:17:21 +0800709 /*
710 * For chrome-os-partner:33035, to workaround the undersized
711 * outdata buffer issue in kernel.
712 */
Craig Hesling65eb8812019-08-01 09:33:56 -0700713 real_write_size = min(opaque_master->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700714 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800715 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700716 if (!packet)
717 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800718
719 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800720 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800721 p.offset = addr + i;
722 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900723
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700724 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
725 in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700726 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900727 need_2nd_pass = 1;
728 return ACCESS_DENIED;
729 }
730
David Hendricks2d6db772013-07-10 21:07:48 -0700731 memcpy(packet, &p, sizeof(p));
732 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700733 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700734 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700735
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800736 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800737 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700738 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800739 need_2nd_pass = 1;
740 return ACCESS_DENIED;
741 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800742
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800743 if (rc < 0) break;
744 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800745 }
746
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800747#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800748 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800749#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700750 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800751 return rc;
752}
753
754
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700755static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900756 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800757 int rc;
758
Souvik Ghosh586968a2016-08-11 17:56:24 -0700759 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800760 if (rc < 0) {
761 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
762 return 1;
763 }
764
765 msg_pinfo("Supported write protect range:\n");
766 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900767 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
768 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800769
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800770 return 0;
771}
772
773
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800774/*
775 * Helper function for flash protection.
776 *
777 * On EC API v1, the EC write protection has been simplified to one-bit:
778 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
779 * or disabled. However, this is different from the SPI-style write protect
780 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
781 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
782 *
783 * SRP Range | PROTECT_RO_AT_BOOT
784 * 0 0 | 0
785 * 0 non-zero | 1
786 * 1 0 | 1
787 * 1 non-zero | 1
788 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800789 *
790 * Besides, to make the protection take effect as soon as possible, we
791 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
792 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800793 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700794static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800795 struct ec_params_flash_protect p;
796 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800797 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
798 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
799 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800800 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800801
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800802 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800803 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800804 p.mask = (ro_at_boot_flag | ro_now_flag);
805 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700806 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700807 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800808 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800809 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
810 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800811 return 1;
812 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800813
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800814 /* Read back */
815 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700816 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700817 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800818 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800819 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
820 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800821 return 1;
822 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800823
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800824 if (!enable) {
825 /* The disable case is easier to check. */
826 if (r.flags & ro_at_boot_flag) {
827 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
828 return 1;
829 } else if (r.flags & ro_now_flag) {
830 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
831 need_an_ec_cold_reset = 1;
832 goto exit;
833 }
834
835 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
836 return 0;
837 }
838
839 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
840 if (r.flags & ro_at_boot_flag) {
841 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
842 } else {
843 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
844 return 1;
845 }
846
847 /* Then, we check if the protection has been activated. */
848 if (r.flags & ro_now_flag) {
849 /* Good, RO_NOW is set. */
850 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
851 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800852 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
853
854 memset(&p, 0, sizeof(p));
855 p.mask = EC_FLASH_PROTECT_ALL_NOW;
856 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700857 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800858 EC_VER_FLASH_PROTECT,
859 &p, sizeof(p), &r, sizeof(r));
860 if (rc < 0) {
861 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
862 return 1;
863 }
864
865 /* Read back */
866 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700867 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800868 EC_VER_FLASH_PROTECT,
869 &p, sizeof(p), &r, sizeof(r));
870 if (rc < 0) {
871 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
872 return 1;
873 }
874
875 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
876 msg_perr("FAILED: ALL_NOW is not set.\n");
877 need_an_ec_cold_reset = 1;
878 goto exit;
879 }
880
881 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
882
883 /*
884 * Our goal is to protect the RO ASAP. The entire protection
885 * is just a workaround for platform not supporting RO_NOW.
886 * It has side-effect that the RW is also protected and leads
887 * the RW update failed. So, we arrange an EC code reset to
888 * unlock RW ASAP.
889 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800890 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800891 if (rc < 0) {
892 msg_perr("WARN: Cannot arrange a cold reset at next "
893 "shutdown to unlock entire protect.\n");
894 msg_perr(" But you can do it manually.\n");
895 } else {
896 msg_pdbg("INFO: A cold reset is arranged at next "
897 "shutdown.\n");
898 }
899
900 } else {
901 msg_perr("FAILED: RO_NOW is not set.\n");
902 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
903 "make write protection active now.\n");
904 need_an_ec_cold_reset = 1;
905 }
906
907exit:
908 if (need_an_ec_cold_reset) {
909 msg_perr("FAILED: You may need a reboot to take effect of "
910 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800911 return 1;
912 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800913
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800914 return 0;
915}
916
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700917static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800918 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900919 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800920 int rc;
921
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800922 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700923 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800924 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800925 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800926 return 1;
927 }
928 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900929 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800930 /* pass */
931 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800932 msg_perr("FAILED: Unsupported write protection range "
933 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800934 msg_perr("Currently supported range:\n");
935 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900936 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
937 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800938 return 1;
939 }
940
David Hendricks393deec2016-11-23 16:15:05 -0800941 if (ignore_wp_range_command)
942 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700943 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800944}
945
946
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700947static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700948 enum wp_mode wp_mode) {
949 int ret;
950
951 switch (wp_mode) {
952 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700953 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700954 break;
955 default:
956 msg_perr("%s():%d Unsupported write-protection mode\n",
957 __func__, __LINE__);
958 ret = 1;
959 break;
960 }
961
962 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800963}
964
965
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700966static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800967 /* --wp-range implicitly enables write protection on CrOS EC, so force
968 it not to if --wp-disable is what the user really wants. */
969 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700970 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800971}
972
973
Souvik Ghosh586968a2016-08-11 17:56:24 -0700974static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800975 struct ec_params_flash_protect p;
976 struct ec_response_flash_protect r;
977 int start, len; /* wp range */
978 int enabled;
979 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800980
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800981 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700982 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700983 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800984 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800985 msg_perr("FAILED: Cannot get the write protection status: %d\n",
986 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800987 return 1;
988 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700989 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800990 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800991 return 1;
992 }
993
994 start = len = 0;
995 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900996 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800997
998 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
999 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -07001000 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001001 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +08001002 msg_perr("FAILED: Cannot get the WP_RO region info: "
1003 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001004 return 1;
1005 }
Simon Glass3c01dca2013-07-01 18:07:34 +09001006 start = info.offset;
1007 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001008 } else {
1009 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
1010 __func__);
1011 }
1012
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +08001013 /*
1014 * If neither RO_NOW or ALL_NOW is set, it means write protect is
1015 * NOT active now.
1016 */
1017 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
1018 start = len = 0;
1019
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001020 /* Remove the SPI-style messages. */
1021 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
1022 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
1023 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001024 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001025 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001026 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001027 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001028
1029 return 0;
1030}
1031
David Hendrickse5454932013-11-04 18:16:11 -08001032/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -07001033int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -08001034{
1035 struct ec_params_hello request;
1036 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -08001037 int rc = 0;
1038
1039 /* Say hello to EC. */
1040 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
1041 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
1042 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001043 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001044 sizeof(request), &response, sizeof(response));
1045 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1046
1047 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1048 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1049 "rc=%d, request=0x%x response=0x%x\n",
1050 rc, request.in_data, response.out_data);
1051 return 1;
1052 }
1053
1054 return 0;
1055}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001056
David Hendricksd13d90d2016-08-09 17:00:52 -07001057void cros_ec_set_max_size(struct cros_ec_priv *priv,
Edward O'Callaghanabd30192019-05-14 15:58:19 +10001058 struct opaque_master *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001059 struct ec_response_get_protocol_info info;
1060 int rc = 0;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001061
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001062 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001063 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001064 &info, sizeof(info));
1065 msg_pdbg("%s: rc:%d\n", __func__, rc);
1066
Gwendal Grignoucf540ef2017-08-10 12:10:06 -07001067 /*
1068 * Use V3 large size only if v2 protocol is not supported.
1069 * When v2 is supported, we may be using a kernel without v3 support,
1070 * leading to sending larger commands the kernel can support.
1071 */
1072 if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001073 op->max_data_write = info.max_request_packet_size -
1074 sizeof(struct ec_host_request);
1075 op->max_data_read = info.max_response_packet_size -
1076 sizeof(struct ec_host_response);
Gwendal Grignouef9062f2017-05-31 17:38:31 -07001077 /*
1078 * Due to a bug in NPCX SPI code (chromium:725580),
1079 * The EC may responds 163 when it meant 160; it should not
1080 * have included header and footer.
1081 */
1082 op->max_data_read &= ~3;
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001083 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1084 op->max_data_write, op->max_data_read);
1085 }
1086}
1087
David Hendricks14935fe2014-08-14 17:38:24 -07001088
1089/*
David Hendricks052446b2014-09-11 11:26:51 -07001090 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001091 *
1092 * This function parses programmer parameters from the command line. Since
1093 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1094 * run during internal programmer initialization.
1095 */
1096int cros_ec_parse_param(struct cros_ec_priv *priv)
1097{
David Hendricks98b3c572016-11-30 01:50:08 +00001098 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001099
David Hendricks98b3c572016-11-30 01:50:08 +00001100 p = extract_programmer_param("dev");
1101 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -07001102 unsigned int index;
1103 char *endptr = NULL;
1104
1105 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001106 /*
1107 * For backward compatibility, check if the index is
1108 * a number: 0: main EC, 1: PD
1109 * works only on Samus.
1110 */
David Hendricks98b3c572016-11-30 01:50:08 +00001111 index = strtoul(p, &endptr, 10);
1112 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
1113 msg_perr("Invalid argument: \"%s\"\n", p);
1114 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001115 }
1116
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001117 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -07001118 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001119 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001120 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001121 priv->dev = ec_type[index];
1122 msg_pdbg("Target %s used\n", priv->dev);
1123 }
David Hendricks14935fe2014-08-14 17:38:24 -07001124
David Hendricks98b3c572016-11-30 01:50:08 +00001125 p = extract_programmer_param("type");
1126 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001127 unsigned int index;
1128 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001129 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001130 break;
1131 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001132 msg_perr("Invalid argument: \"%s\"\n", p);
1133 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001134 }
1135 priv->dev = ec_type[index];
1136 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001137 }
1138
David Hendricks98b3c572016-11-30 01:50:08 +00001139 p = extract_programmer_param("block");
1140 if (p) {
1141 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001142 char *endptr = NULL;
1143
1144 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001145 block = strtoul(p, &endptr, 0);
1146 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1147 msg_perr("Invalid argument: \"%s\"\n", p);
1148 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001149 }
1150
David Hendricks98b3c572016-11-30 01:50:08 +00001151 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001152 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001153 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001154 }
1155
David Hendricks98b3c572016-11-30 01:50:08 +00001156 msg_pdbg("Override block size to 0x%x\n", block);
1157 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001158 }
1159
David Hendricks98b3c572016-11-30 01:50:08 +00001160 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001161}
1162
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001163int cros_ec_probe_size(struct flashctx *flash) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001164 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001165 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001166 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001167 struct block_eraser *eraser;
1168 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001169 .list_ranges = cros_ec_list_ranges,
1170 .set_range = cros_ec_set_range,
1171 .enable = cros_ec_enable_writeprotect,
1172 .disable = cros_ec_disable_writeprotect,
1173 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001174 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001175 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001176
Souvik Ghosh586968a2016-08-11 17:56:24 -07001177 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001178 if (rc < 0) {
1179 msg_perr("%s(): Failed to probe (no current image): %d\n",
1180 __func__, rc);
1181 return 0;
1182 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001183 cros_ec_priv->current_image = rc;
1184 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001185
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001186 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1187 if (rc < 0) {
1188 msg_perr("Cannot determine write command version\n");
1189 return 0;
1190 }
1191 cmd_version = 31 - __builtin_clz(mask);
1192
Patrick Georgif3fa2992017-02-02 16:24:44 +01001193 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001194 flash->chip->wp = &wp;
Craig Hesling65eb8812019-08-01 09:33:56 -07001195 flash->chip->page_size = opaque_master->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001196
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001197 if (cmd_version < 2) {
1198 struct ec_response_flash_info_1 info;
1199 /* Request general information about flash (v1 or below). */
1200 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1201 NULL, 0, &info,
1202 (cmd_version > 0 ? sizeof(info) :
1203 sizeof(struct ec_response_flash_info)));
1204 if (rc < 0) {
1205 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1206 cmd_version, rc);
1207 return 0;
1208 }
1209 if (cmd_version == 0) {
1210 cros_ec_priv->ideal_write_size =
1211 EC_FLASH_WRITE_VER0_SIZE;
1212 } else {
1213 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1214 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1215 flash->chip->feature_bits |=
Alan Greendbeec2b2019-09-16 14:36:52 +10001216 FEATURE_ERASED_ZERO;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001217 }
1218 flash->chip->total_size = info.flash_size / 1024;
1219
1220 /* Allow overriding the erase block size in case EC is incorrect */
1221 if (cros_ec_priv->erase_block_size > 0)
1222 eraser->eraseblocks[0].size =
1223 cros_ec_priv->erase_block_size;
1224 else
1225 eraser->eraseblocks[0].size = info.erase_block_size;
1226
1227 eraser->eraseblocks[0].count = info.flash_size /
1228 eraser->eraseblocks[0].size;
1229 } else {
1230 struct ec_response_flash_info_2 info_2;
1231 struct ec_params_flash_info_2 params_2;
1232 struct ec_response_flash_info_2 *info_2_p = &info_2;
1233 int size_info_v2 = sizeof(info_2), i;
1234
1235 params_2.num_banks_desc = 0;
1236 /*
1237 * Call FLASH_INFO twice, second time with all banks
1238 * information.
1239 */
1240 for (i = 0; i < 2; i++) {
1241 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1242 cmd_version, &params_2,
1243 sizeof(params_2),
1244 info_2_p, size_info_v2);
1245 if (rc < 0) {
1246 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1247 __func__,
1248 params_2.num_banks_desc,
1249 cmd_version, rc);
1250 if (info_2_p != &info_2)
1251 free(info_2_p);
1252 return 0;
1253 } else if (i > 0) {
1254 break;
1255 }
1256 params_2.num_banks_desc = info_2_p->num_banks_total;
1257 size_info_v2 += info_2_p->num_banks_total *
1258 sizeof(struct ec_flash_bank);
1259
1260 info_2_p = malloc(size_info_v2);
1261 if (!info_2_p) {
1262 msg_perr("%s(): malloc of %d banks failed\n",
1263 __func__, info_2_p->num_banks_total);
1264 return 0;
1265 }
1266 }
1267 flash->chip->total_size = info_2_p->flash_size / 1024;
1268 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1269 /* Allow overriding the erase block size in case EC is incorrect */
1270 eraser->eraseblocks[i].size =
1271 (cros_ec_priv->erase_block_size > 0 ?
1272 cros_ec_priv->erase_block_size :
1273 1 << info_2_p->banks[i].erase_size_exp);
1274 eraser->eraseblocks[i].count =
1275 info_2_p->banks[i].count <<
1276 (info_2_p->banks[i].size_exp -
1277 info_2_p->banks[i].erase_size_exp);
1278 }
1279 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001280#if 0
1281 /*
1282 * TODO(b/38506987)Comment out, as some firmware were not
1283 * setting this flag properly.
1284 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001285 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
Alan Greendbeec2b2019-09-16 14:36:52 +10001286 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001287#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001288 free(info_2_p);
1289 }
Vadim Bendeburyadbd7062018-06-19 21:36:45 -07001290 eraser->block_erase = cros_ec_block_erase;
David Hendricks194b3bb2013-07-16 14:32:26 -07001291 /*
1292 * Some STM32 variants erase bits to 0. For now, assume that this
1293 * applies to STM32L parts.
1294 *
1295 * FIXME: This info will eventually be exposed via some EC command.
1296 * See chrome-os-partner:20973.
1297 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001298 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001299 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001300 if (rc < 0) {
1301 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1302 return 0;
1303 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001304 if (!strncmp(chip_info.name, "stm32l1", 7))
Alan Greendbeec2b2019-09-16 14:36:52 +10001305 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001306
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001307
David Hendricksf9461c72013-07-11 19:02:13 -07001308
David Hendricksa672b042016-09-19 12:37:36 -07001309 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1310 0, NULL, 0, &spi_info, sizeof(spi_info));
1311 if (rc < 0) {
1312 static char chip_vendor[32];
1313 static char chip_name[32];
1314
1315 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1316 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001317 flash->chip->vendor = chip_vendor;
1318 flash->chip->name = chip_name;
Alan Greenb2fe0472019-07-30 14:33:28 +10001319 flash->chip->tested = TEST_OK_PREW;
David Hendricksa672b042016-09-19 12:37:36 -07001320 } else {
1321 const struct flashchip *f;
1322 uint32_t mfg = spi_info.jedec[0];
1323 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1324
1325 for (f = flashchips; f && f->name; f++) {
1326 if (f->bustype != BUS_SPI)
1327 continue;
1328 if ((f->manufacture_id == mfg) &&
1329 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001330 flash->chip->vendor = f->vendor;
1331 flash->chip->name = f->name;
1332 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001333 break;
1334 }
1335 }
1336 }
1337
Simon Glassc453a642013-07-01 18:08:53 +09001338 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1339 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001340 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1341 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001342 if (rc) {
1343 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1344 __func__, rc);
1345 return 0;
1346 }
1347
Souvik Ghosh586968a2016-08-11 17:56:24 -07001348 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1349 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001350 if (rc) {
1351 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1352 __func__, rc);
1353 return 0;
1354 }
1355
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001356 return 1;
1357};