blob: 51cdce20cc258f757ba5989b492115e39d8b993f [file] [log] [blame]
David Hendricksee712472012-05-23 21:50:59 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2012 The Chromium OS Authors. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Google or the names of contributors or
18 * licensors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind.
22 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
25 * GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE
26 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
27 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28 * GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
29 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
30 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
31 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
32 * EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 */
David Hendricks14935fe2014-08-14 17:38:24 -070034#include <errno.h>
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080040#include "fmap.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070041#include "cros_ec.h"
42#include "cros_ec_lock.h"
43#include "cros_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080044#include "programmer.h"
45#include "spi.h"
46#include "writeprotect.h"
47
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080048/* FIXME: used for wp hacks */
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <unistd.h>
Souvik Ghosh586968a2016-08-11 17:56:24 -070053
54struct cros_ec_priv *cros_ec_priv;
David Hendricks393deec2016-11-23 16:15:05 -080055static int ignore_wp_range_command = 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -070056
David Hendricksb64b39a2016-10-11 13:48:06 -070057static int set_wp(int enable); /* FIXME: move set_wp() */
58
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080059struct wp_data {
60 int enable;
61 unsigned int start;
62 unsigned int len;
63};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080064#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
65
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080066/* If software sync is enabled, then we don't try the latest firmware copy
67 * after updating.
68 */
69#define SOFTWARE_SYNC_ENABLED
70
Gwendal Grignoua36ff502015-03-23 16:36:47 -070071/* For region larger use async version for FLASH_ERASE */
72#define FLASH_SMALL_REGION_THRESHOLD (16 * 1024)
73
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080074/* 1 if we want the flashrom to call erase_and_write_flash() again. */
75static int need_2nd_pass = 0;
76
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080077/* 1 if we want the flashrom to try jumping to new firmware after update. */
78static int try_latest_firmware = 0;
79
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +080080/* 1 if EC firmware has RWSIG enabled. */
81static int rwsig_enabled = 0;
82
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080083/* The range of each firmware copy from the image file to update.
84 * But re-define the .flags as the valid flag to indicate the firmware is
85 * new or not (if flags = 1).
86 */
87static struct fmap_area fwcopy[4]; // [0] is not used.
88
89/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080090static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070091 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
92 "EC_RO",
93 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080094};
95
Gwendal Grignou94e87d62014-11-25 15:34:15 -080096/*
97 * The names of the different device that can be found in a machine.
98 * Order is important: for backward compatibilty issue,
99 * 'ec' must be 0, 'pd' must be 1.
100 */
101static const char *ec_type[] = {
102 [0] = "ec",
103 [1] = "pd",
104 [2] = "sh",
Vincent Palatin4faff9a2017-03-17 17:27:39 +0100105 [3] = "fp",
Wei-Ning Huang78397842017-05-05 21:45:47 +0800106 [4] = "tp",
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800107};
108
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700109static struct ec_response_flash_region_info regions[EC_FLASH_REGION_COUNT];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800110
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800111/*
112 * Delay after reboot before EC can respond to host command.
113 * This value should be large enough for EC to initialize, but no larger than
114 * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be
115 * able to abort RWSIG jump and stay in RO.
116 */
117#define EC_INIT_DELAY 800000
118
119/*
120 * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW.
121 */
122#define EC_RWSIG_JUMP_TO_RW_DELAY 3000000
123
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800124/* Given the range not able to update, mark the corresponding
125 * firmware as old.
126 */
David Hendricksb907de32014-08-11 16:47:09 -0700127static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800128{
129 int i;
130
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800131 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800132 struct fmap_area *fw = &fwcopy[i];
133 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
134 (fw->offset >= addr && (fw->offset < addr + len))) {
135 msg_pdbg("Mark firmware [%s] as old.\n",
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700136 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 }
463 /* Warning: before update, we jump the EC to RO copy. If you
464 * want to change this behavior, please also check the
465 * cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800466 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700467 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800468}
469
470
471/* Returns >0 if we need 2nd pass of erase_and_write_flash().
472 * <0 if we cannot jump to any firmware copy.
473 * ==0 if no more pass is needed.
474 *
475 * This function also jumps to new-updated firmware copy before return >0.
476 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700477int cros_ec_need_2nd_pass(void)
478{
479 if (!(cros_ec_priv && cros_ec_priv->detected))
480 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800481
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700482 if (!need_2nd_pass)
483 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800484
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700485 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0)
486 /* EC_RES_ACCESS_DENIED is returned when the block is either
487 * protected or unsafe. Thus, theoretically, we shouldn't reach
488 * here because everywhere is safe for EXEC_IN_RAM chips and
489 * WP is disabled before erase/write cycle starts.
490 * We can still let the 2nd pass run (and it will probably
491 * fail again).
492 */
493 return 1;
494
495 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN))
496 return -1;
497
498 return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800499}
500
501
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800502/* Returns 0 for success.
503 *
504 * Try latest firmware: B > A > RO
505 *
David Hendricksb907de32014-08-11 16:47:09 -0700506 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800507 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700508 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800509 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700510int cros_ec_finish(void)
511{
Souvik Ghosh586968a2016-08-11 17:56:24 -0700512 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800513
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800514 /* For EC with RWSIG enabled. We need a cold reboot to enable
515 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
516 */
517 if (rwsig_enabled) {
518 int rc;
519
520 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
521 rc = cros_ec_cold_reboot(0);
522 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
523 return rc;
524 }
525
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800526 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800527 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700528 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
529 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800530 }
531
532 return 0;
533}
534
535
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700536int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700537 unsigned int blockaddr, unsigned int readcnt)
538{
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800539 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800540 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700541 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800542 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700543 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800544
David Hendricks133083b2012-07-17 20:39:38 -0700545 while (offset < readcnt) {
546 count = min(maxlen, readcnt - offset);
547 p.offset = blockaddr + offset;
548 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700549 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700550 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800551 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700552 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700553 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800554 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800555 } else {
556 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800557 }
558
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800559 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700560 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800561 }
562
563 return rc;
564}
565
566
Simon Glassc453a642013-07-01 18:08:53 +0900567/*
568 * returns 0 to indicate area does not overlap current EC image
569 * returns 1 to indicate area overlaps current EC image or error
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700570 *
571 * We can't get rid of this. The ECs should know what region is safe to erase
572 * or write. We should let them decide (and return EC_RES_ACCESS_DENIED).
573 * Not all existing EC firmware can do so.
Simon Glassc453a642013-07-01 18:08:53 +0900574 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700575static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900576{
Simon Glassc453a642013-07-01 18:08:53 +0900577 enum ec_current_image image;
578 uint32_t region_offset;
579 uint32_t region_size;
580
Souvik Ghosh586968a2016-08-11 17:56:24 -0700581 image = cros_ec_priv->current_image;
582 region_offset = cros_ec_priv->region[image].offset;
583 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900584
585 if ((addr + len - 1 < region_offset) ||
586 (addr > region_offset + region_size - 1)) {
587 return 0;
588 }
589 return 1;
590}
591
592
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700593int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800594 unsigned int blockaddr,
595 unsigned int len) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700596 struct ec_params_flash_erase_v1 erase;
597 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700598 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800599
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700600 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
601 in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700602 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900603 need_2nd_pass = 1;
604 return ACCESS_DENIED;
605 }
606
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700607 erase.params.offset = blockaddr;
608 erase.params.size = len;
609 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
610 if (rc < 0) {
611 msg_perr("Cannot determine erase command version\n");
612 return 0;
613 }
614 cmd_version = 31 - __builtin_clz(mask);
615
616 if (cmd_version == 0) {
617 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
618 &erase.params,
619 sizeof(struct ec_params_flash_erase), NULL, 0);
620 if (rc == -EC_RES_ACCESS_DENIED) {
621 // this is active image.
622 cros_ec_invalidate_copy(blockaddr, len);
623 need_2nd_pass = 1;
624 return ACCESS_DENIED;
625 }
626 if (rc < 0) {
627 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
628 blockaddr, rc);
629 return rc;
630 }
631 goto end_flash_erase;
632 }
633
634 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
635 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
636 } else {
637 erase.cmd = FLASH_ERASE_SECTOR;
638 }
639 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
640 &erase, sizeof(erase), NULL, 0);
641 switch (rc) {
642 case 0:
643 break;
644 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800645 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700646 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800647 need_2nd_pass = 1;
648 return ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700649 case -EC_RES_BUSY:
650 msg_perr("CROS_EC: Flash erase command "
651 " already in progress\n");
652 default:
653 return rc;
654 }
655 if (len < FLASH_SMALL_REGION_THRESHOLD)
656 goto end_flash_erase;
657
658 /* Wait for the erase command to complete */
659 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700660
661/* wait up to 10s to erase a flash sector */
662#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
663/* wait .5 second between queries. */
664#define CROS_EC_ERASE_ASYNC_WAIT 500000
665
666 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
667 usleep(CROS_EC_ERASE_ASYNC_WAIT);
668 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700669 erase.cmd = FLASH_ERASE_GET_RESULT;
670 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
671 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800672 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800673 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700674 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800675 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800676 return rc;
677 }
678
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700679end_flash_erase:
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800680#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800681 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800682#endif
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700683 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700684 /*
685 * Can happen if the command with retried with
686 * EC_CMD_GET_COMMS_STATUS
687 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700688 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700689 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800690 return rc;
691}
692
693
Patrick Georgiab8353e2017-02-03 18:32:01 +0100694int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800695 unsigned int nbytes) {
696 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800697 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800698 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700699 uint8_t *packet;
700
Ken Chang69c31b82014-10-28 15:17:21 +0800701 /*
702 * For chrome-os-partner:33035, to workaround the undersized
703 * outdata buffer issue in kernel.
704 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700705 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700706 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800707 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700708 if (!packet)
709 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800710
711 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800712 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800713 p.offset = addr + i;
714 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900715
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700716 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
717 in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700718 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900719 need_2nd_pass = 1;
720 return ACCESS_DENIED;
721 }
722
David Hendricks2d6db772013-07-10 21:07:48 -0700723 memcpy(packet, &p, sizeof(p));
724 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700725 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700726 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700727
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800728 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800729 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700730 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800731 need_2nd_pass = 1;
732 return ACCESS_DENIED;
733 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800734
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800735 if (rc < 0) break;
736 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800737 }
738
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800739#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800740 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800741#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700742 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800743 return rc;
744}
745
746
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700747static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900748 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800749 int rc;
750
Souvik Ghosh586968a2016-08-11 17:56:24 -0700751 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800752 if (rc < 0) {
753 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
754 return 1;
755 }
756
757 msg_pinfo("Supported write protect range:\n");
758 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900759 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
760 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800761
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800762 return 0;
763}
764
765
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800766/*
767 * Helper function for flash protection.
768 *
769 * On EC API v1, the EC write protection has been simplified to one-bit:
770 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
771 * or disabled. However, this is different from the SPI-style write protect
772 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
773 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
774 *
775 * SRP Range | PROTECT_RO_AT_BOOT
776 * 0 0 | 0
777 * 0 non-zero | 1
778 * 1 0 | 1
779 * 1 non-zero | 1
780 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800781 *
782 * Besides, to make the protection take effect as soon as possible, we
783 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
784 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800785 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700786static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800787 struct ec_params_flash_protect p;
788 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800789 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
790 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
791 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800792 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800793
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800794 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800795 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800796 p.mask = (ro_at_boot_flag | ro_now_flag);
797 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700798 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700799 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800800 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800801 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
802 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800803 return 1;
804 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800805
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800806 /* Read back */
807 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700808 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700809 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800810 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800811 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
812 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800813 return 1;
814 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800815
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800816 if (!enable) {
817 /* The disable case is easier to check. */
818 if (r.flags & ro_at_boot_flag) {
819 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
820 return 1;
821 } else if (r.flags & ro_now_flag) {
822 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
823 need_an_ec_cold_reset = 1;
824 goto exit;
825 }
826
827 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
828 return 0;
829 }
830
831 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
832 if (r.flags & ro_at_boot_flag) {
833 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
834 } else {
835 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
836 return 1;
837 }
838
839 /* Then, we check if the protection has been activated. */
840 if (r.flags & ro_now_flag) {
841 /* Good, RO_NOW is set. */
842 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
843 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800844 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
845
846 memset(&p, 0, sizeof(p));
847 p.mask = EC_FLASH_PROTECT_ALL_NOW;
848 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700849 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800850 EC_VER_FLASH_PROTECT,
851 &p, sizeof(p), &r, sizeof(r));
852 if (rc < 0) {
853 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
854 return 1;
855 }
856
857 /* Read back */
858 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700859 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800860 EC_VER_FLASH_PROTECT,
861 &p, sizeof(p), &r, sizeof(r));
862 if (rc < 0) {
863 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
864 return 1;
865 }
866
867 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
868 msg_perr("FAILED: ALL_NOW is not set.\n");
869 need_an_ec_cold_reset = 1;
870 goto exit;
871 }
872
873 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
874
875 /*
876 * Our goal is to protect the RO ASAP. The entire protection
877 * is just a workaround for platform not supporting RO_NOW.
878 * It has side-effect that the RW is also protected and leads
879 * the RW update failed. So, we arrange an EC code reset to
880 * unlock RW ASAP.
881 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800882 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800883 if (rc < 0) {
884 msg_perr("WARN: Cannot arrange a cold reset at next "
885 "shutdown to unlock entire protect.\n");
886 msg_perr(" But you can do it manually.\n");
887 } else {
888 msg_pdbg("INFO: A cold reset is arranged at next "
889 "shutdown.\n");
890 }
891
892 } else {
893 msg_perr("FAILED: RO_NOW is not set.\n");
894 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
895 "make write protection active now.\n");
896 need_an_ec_cold_reset = 1;
897 }
898
899exit:
900 if (need_an_ec_cold_reset) {
901 msg_perr("FAILED: You may need a reboot to take effect of "
902 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800903 return 1;
904 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800905
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800906 return 0;
907}
908
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700909static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800910 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900911 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800912 int rc;
913
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800914 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700915 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800916 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800917 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800918 return 1;
919 }
920 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900921 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800922 /* pass */
923 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800924 msg_perr("FAILED: Unsupported write protection range "
925 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800926 msg_perr("Currently supported range:\n");
927 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900928 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
929 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800930 return 1;
931 }
932
David Hendricks393deec2016-11-23 16:15:05 -0800933 if (ignore_wp_range_command)
934 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700935 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800936}
937
938
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700939static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700940 enum wp_mode wp_mode) {
941 int ret;
942
943 switch (wp_mode) {
944 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700945 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700946 break;
947 default:
948 msg_perr("%s():%d Unsupported write-protection mode\n",
949 __func__, __LINE__);
950 ret = 1;
951 break;
952 }
953
954 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800955}
956
957
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700958static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800959 /* --wp-range implicitly enables write protection on CrOS EC, so force
960 it not to if --wp-disable is what the user really wants. */
961 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700962 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800963}
964
965
Souvik Ghosh586968a2016-08-11 17:56:24 -0700966static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800967 struct ec_params_flash_protect p;
968 struct ec_response_flash_protect r;
969 int start, len; /* wp range */
970 int enabled;
971 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800972
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800973 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700974 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700975 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800976 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800977 msg_perr("FAILED: Cannot get the write protection status: %d\n",
978 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800979 return 1;
980 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700981 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800982 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800983 return 1;
984 }
985
986 start = len = 0;
987 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900988 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800989
990 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
991 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700992 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800993 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800994 msg_perr("FAILED: Cannot get the WP_RO region info: "
995 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800996 return 1;
997 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900998 start = info.offset;
999 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001000 } else {
1001 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
1002 __func__);
1003 }
1004
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +08001005 /*
1006 * If neither RO_NOW or ALL_NOW is set, it means write protect is
1007 * NOT active now.
1008 */
1009 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
1010 start = len = 0;
1011
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001012 /* Remove the SPI-style messages. */
1013 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
1014 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
1015 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001016 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001017 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001018 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001019 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001020
1021 return 0;
1022}
1023
David Hendrickse5454932013-11-04 18:16:11 -08001024/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -07001025int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -08001026{
1027 struct ec_params_hello request;
1028 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -08001029 int rc = 0;
1030
1031 /* Say hello to EC. */
1032 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
1033 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
1034 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001035 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001036 sizeof(request), &response, sizeof(response));
1037 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1038
1039 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1040 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1041 "rc=%d, request=0x%x response=0x%x\n",
1042 rc, request.in_data, response.out_data);
1043 return 1;
1044 }
1045
1046 return 0;
1047}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001048
David Hendricksd13d90d2016-08-09 17:00:52 -07001049void cros_ec_set_max_size(struct cros_ec_priv *priv,
1050 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001051 struct ec_response_get_protocol_info info;
1052 int rc = 0;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001053
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001054 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001055 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001056 &info, sizeof(info));
1057 msg_pdbg("%s: rc:%d\n", __func__, rc);
1058
Gwendal Grignoucf540ef2017-08-10 12:10:06 -07001059 /*
1060 * Use V3 large size only if v2 protocol is not supported.
1061 * When v2 is supported, we may be using a kernel without v3 support,
1062 * leading to sending larger commands the kernel can support.
1063 */
1064 if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001065 op->max_data_write = info.max_request_packet_size -
1066 sizeof(struct ec_host_request);
1067 op->max_data_read = info.max_response_packet_size -
1068 sizeof(struct ec_host_response);
Gwendal Grignouef9062f2017-05-31 17:38:31 -07001069 /*
1070 * Due to a bug in NPCX SPI code (chromium:725580),
1071 * The EC may responds 163 when it meant 160; it should not
1072 * have included header and footer.
1073 */
1074 op->max_data_read &= ~3;
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001075 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1076 op->max_data_write, op->max_data_read);
1077 }
1078}
1079
David Hendricks14935fe2014-08-14 17:38:24 -07001080
1081/*
David Hendricks052446b2014-09-11 11:26:51 -07001082 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001083 *
1084 * This function parses programmer parameters from the command line. Since
1085 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1086 * run during internal programmer initialization.
1087 */
1088int cros_ec_parse_param(struct cros_ec_priv *priv)
1089{
David Hendricks98b3c572016-11-30 01:50:08 +00001090 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001091
David Hendricks98b3c572016-11-30 01:50:08 +00001092 p = extract_programmer_param("dev");
1093 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -07001094 unsigned int index;
1095 char *endptr = NULL;
1096
1097 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001098 /*
1099 * For backward compatibility, check if the index is
1100 * a number: 0: main EC, 1: PD
1101 * works only on Samus.
1102 */
David Hendricks98b3c572016-11-30 01:50:08 +00001103 index = strtoul(p, &endptr, 10);
1104 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
1105 msg_perr("Invalid argument: \"%s\"\n", p);
1106 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001107 }
1108
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001109 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -07001110 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001111 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001112 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001113 priv->dev = ec_type[index];
1114 msg_pdbg("Target %s used\n", priv->dev);
1115 }
David Hendricks14935fe2014-08-14 17:38:24 -07001116
David Hendricks98b3c572016-11-30 01:50:08 +00001117 p = extract_programmer_param("type");
1118 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001119 unsigned int index;
1120 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001121 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001122 break;
1123 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001124 msg_perr("Invalid argument: \"%s\"\n", p);
1125 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001126 }
1127 priv->dev = ec_type[index];
1128 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001129 }
1130
David Hendricks98b3c572016-11-30 01:50:08 +00001131 p = extract_programmer_param("block");
1132 if (p) {
1133 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001134 char *endptr = NULL;
1135
1136 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001137 block = strtoul(p, &endptr, 0);
1138 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1139 msg_perr("Invalid argument: \"%s\"\n", p);
1140 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001141 }
1142
David Hendricks98b3c572016-11-30 01:50:08 +00001143 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001144 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001145 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001146 }
1147
David Hendricks98b3c572016-11-30 01:50:08 +00001148 msg_pdbg("Override block size to 0x%x\n", block);
1149 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001150 }
1151
David Hendricks98b3c572016-11-30 01:50:08 +00001152 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001153}
1154
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001155int cros_ec_probe_size(struct flashctx *flash) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001156 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001157 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001158 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001159 struct block_eraser *eraser;
1160 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001161 .list_ranges = cros_ec_list_ranges,
1162 .set_range = cros_ec_set_range,
1163 .enable = cros_ec_enable_writeprotect,
1164 .disable = cros_ec_disable_writeprotect,
1165 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001166 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001167 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001168
Souvik Ghosh586968a2016-08-11 17:56:24 -07001169 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001170 if (rc < 0) {
1171 msg_perr("%s(): Failed to probe (no current image): %d\n",
1172 __func__, rc);
1173 return 0;
1174 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001175 cros_ec_priv->current_image = rc;
1176 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001177
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001178 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1179 if (rc < 0) {
1180 msg_perr("Cannot determine write command version\n");
1181 return 0;
1182 }
1183 cmd_version = 31 - __builtin_clz(mask);
1184
Patrick Georgif3fa2992017-02-02 16:24:44 +01001185 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001186 flash->chip->wp = &wp;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001187 flash->chip->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001188
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001189 if (cmd_version < 2) {
1190 struct ec_response_flash_info_1 info;
1191 /* Request general information about flash (v1 or below). */
1192 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1193 NULL, 0, &info,
1194 (cmd_version > 0 ? sizeof(info) :
1195 sizeof(struct ec_response_flash_info)));
1196 if (rc < 0) {
1197 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1198 cmd_version, rc);
1199 return 0;
1200 }
1201 if (cmd_version == 0) {
1202 cros_ec_priv->ideal_write_size =
1203 EC_FLASH_WRITE_VER0_SIZE;
1204 } else {
1205 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1206 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1207 flash->chip->feature_bits |=
1208 FEATURE_ERASE_TO_ZERO;
1209 }
1210 flash->chip->total_size = info.flash_size / 1024;
1211
1212 /* Allow overriding the erase block size in case EC is incorrect */
1213 if (cros_ec_priv->erase_block_size > 0)
1214 eraser->eraseblocks[0].size =
1215 cros_ec_priv->erase_block_size;
1216 else
1217 eraser->eraseblocks[0].size = info.erase_block_size;
1218
1219 eraser->eraseblocks[0].count = info.flash_size /
1220 eraser->eraseblocks[0].size;
1221 } else {
1222 struct ec_response_flash_info_2 info_2;
1223 struct ec_params_flash_info_2 params_2;
1224 struct ec_response_flash_info_2 *info_2_p = &info_2;
1225 int size_info_v2 = sizeof(info_2), i;
1226
1227 params_2.num_banks_desc = 0;
1228 /*
1229 * Call FLASH_INFO twice, second time with all banks
1230 * information.
1231 */
1232 for (i = 0; i < 2; i++) {
1233 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1234 cmd_version, &params_2,
1235 sizeof(params_2),
1236 info_2_p, size_info_v2);
1237 if (rc < 0) {
1238 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1239 __func__,
1240 params_2.num_banks_desc,
1241 cmd_version, rc);
1242 if (info_2_p != &info_2)
1243 free(info_2_p);
1244 return 0;
1245 } else if (i > 0) {
1246 break;
1247 }
1248 params_2.num_banks_desc = info_2_p->num_banks_total;
1249 size_info_v2 += info_2_p->num_banks_total *
1250 sizeof(struct ec_flash_bank);
1251
1252 info_2_p = malloc(size_info_v2);
1253 if (!info_2_p) {
1254 msg_perr("%s(): malloc of %d banks failed\n",
1255 __func__, info_2_p->num_banks_total);
1256 return 0;
1257 }
1258 }
1259 flash->chip->total_size = info_2_p->flash_size / 1024;
1260 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1261 /* Allow overriding the erase block size in case EC is incorrect */
1262 eraser->eraseblocks[i].size =
1263 (cros_ec_priv->erase_block_size > 0 ?
1264 cros_ec_priv->erase_block_size :
1265 1 << info_2_p->banks[i].erase_size_exp);
1266 eraser->eraseblocks[i].count =
1267 info_2_p->banks[i].count <<
1268 (info_2_p->banks[i].size_exp -
1269 info_2_p->banks[i].erase_size_exp);
1270 }
1271 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001272#if 0
1273 /*
1274 * TODO(b/38506987)Comment out, as some firmware were not
1275 * setting this flag properly.
1276 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001277 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
1278 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001279#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001280 free(info_2_p);
1281 }
Vadim Bendeburyadbd7062018-06-19 21:36:45 -07001282 eraser->block_erase = cros_ec_block_erase;
David Hendricks194b3bb2013-07-16 14:32:26 -07001283 /*
1284 * Some STM32 variants erase bits to 0. For now, assume that this
1285 * applies to STM32L parts.
1286 *
1287 * FIXME: This info will eventually be exposed via some EC command.
1288 * See chrome-os-partner:20973.
1289 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001290 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001291 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001292 if (rc < 0) {
1293 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1294 return 0;
1295 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001296 if (!strncmp(chip_info.name, "stm32l1", 7))
Patrick Georgif3fa2992017-02-02 16:24:44 +01001297 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001298
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001299
David Hendricksf9461c72013-07-11 19:02:13 -07001300
David Hendricksa672b042016-09-19 12:37:36 -07001301 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1302 0, NULL, 0, &spi_info, sizeof(spi_info));
1303 if (rc < 0) {
1304 static char chip_vendor[32];
1305 static char chip_name[32];
1306
1307 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1308 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001309 flash->chip->vendor = chip_vendor;
1310 flash->chip->name = chip_name;
1311 flash->chip->tested = TEST_OK_PREWU;
David Hendricksa672b042016-09-19 12:37:36 -07001312 } else {
1313 const struct flashchip *f;
1314 uint32_t mfg = spi_info.jedec[0];
1315 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1316
1317 for (f = flashchips; f && f->name; f++) {
1318 if (f->bustype != BUS_SPI)
1319 continue;
1320 if ((f->manufacture_id == mfg) &&
1321 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001322 flash->chip->vendor = f->vendor;
1323 flash->chip->name = f->name;
1324 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001325 break;
1326 }
1327 }
1328 }
1329
Simon Glassc453a642013-07-01 18:08:53 +09001330 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1331 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001332 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1333 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001334 if (rc) {
1335 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1336 __func__, rc);
1337 return 0;
1338 }
1339
Souvik Ghosh586968a2016-08-11 17:56:24 -07001340 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1341 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001342 if (rc) {
1343 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1344 __func__, rc);
1345 return 0;
1346 }
1347
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001348 return 1;
1349};