blob: 8da95f5a39ae3a134a0802fb7066eb369e004db4 [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 */
Eric Yilun Lina3f47c42020-07-01 11:59:39 +080034
35#include <assert.h>
David Hendricks14935fe2014-08-14 17:38:24 -070036#include <errno.h>
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080037#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080042#include "fmap.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070043#include "cros_ec.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070044#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
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
Nikolai Artemiev6c457152020-04-29 11:30:39 +100096/* The names of the different device that can be found in a machine. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080097static const char *ec_type[] = {
Nikolai Artemiev6c457152020-04-29 11:30:39 +100098 "ec",
99 "pd",
100 "sh",
101 "fp",
102 "tp",
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800103};
104
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700105static struct ec_response_flash_region_info regions[EC_FLASH_REGION_COUNT];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800106
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800107/*
108 * Delay after reboot before EC can respond to host command.
109 * This value should be large enough for EC to initialize, but no larger than
110 * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be
111 * able to abort RWSIG jump and stay in RO.
112 */
113#define EC_INIT_DELAY 800000
114
115/*
116 * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW.
117 */
118#define EC_RWSIG_JUMP_TO_RW_DELAY 3000000
119
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800120/* Given the range not able to update, mark the corresponding
121 * firmware as old.
122 */
David Hendricksb907de32014-08-11 16:47:09 -0700123static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800124{
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000125 unsigned i;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800126
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800127 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800128 struct fmap_area *fw = &fwcopy[i];
129 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
130 (fw->offset >= addr && (fw->offset < addr + len))) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -0700131 msg_pdbg(" OLD[%s]", sections[i]);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800132 fw->flags = 0; // mark as old
133 }
134 }
135}
136
137
Souvik Ghosh586968a2016-08-11 17:56:24 -0700138static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900139{
140 struct ec_response_get_version resp;
141 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700142
Souvik Ghosh586968a2016-08-11 17:56:24 -0700143 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700144 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900145 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700146 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900147 return rc;
148 }
149 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700150 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900151 return -1;
152 }
153
154 return resp.current_image;
155}
156
157
Souvik Ghosh586968a2016-08-11 17:56:24 -0700158static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900159 struct ec_response_flash_region_info *info)
160{
161 struct ec_params_flash_region_info req;
162 struct ec_response_flash_region_info resp;
163 int rc;
164
165 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700166 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900167 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
168 &resp, sizeof(resp));
169 if (rc < 0) {
170 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
171 return rc;
172 }
173
174 info->offset = resp.offset;
175 info->size = resp.size;
176 return 0;
177}
178
David Hendricksf9461c72013-07-11 19:02:13 -0700179/**
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800180 * Check if a feature is supported by EC.
181 *
182 * @param feature feature code
183 * @return < 0 if error, 0 not supported, > 0 supported
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700184 *
185 * NOTE: Once it successfully runs, the feature bits are cached. So, if you
186 * want to query a feature that can be different per copy, you need to
187 * cache features per image copy.
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800188 */
189static int ec_check_features(int feature)
190{
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700191 static struct ec_response_get_features r;
192 int rc = 0;
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800193
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000194 if (feature < 0 || feature >= (int)sizeof(r.flags) * 8)
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800195 return -1;
196
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700197 /* We don't cache return code. We retry regardless the return code. */
198 if (r.flags[0] == 0)
199 rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES,
200 0, NULL, 0, &r, sizeof(r));
201
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800202 if (rc < 0)
203 return rc;
204
Daisuke Nojirif8ab92f2018-04-04 10:13:38 -0700205 return !!(r.flags[feature / 32] & (1 << (feature % 32)));
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800206}
207
208/**
209 * Disable EC rwsig jump.
210 *
211 * @return 0 if success, <0 if error
212 */
213static int ec_rwsig_abort()
214{
215 struct ec_params_rwsig_action p;
216
217 p.action = RWSIG_ACTION_ABORT;
218 return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION,
219 0, &p, sizeof(p), NULL, 0);
220}
221
222/**
David Hendricksf9461c72013-07-11 19:02:13 -0700223 * Get the versions of the command supported by the EC.
224 *
225 * @param cmd Command
226 * @param pmask Destination for version mask; will be set to 0 on
227 * error.
228 * @return 0 if success, <0 if error
229 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700230static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700231{
David Hendricksf9461c72013-07-11 19:02:13 -0700232 struct ec_params_get_cmd_versions pver;
233 struct ec_response_get_cmd_versions rver;
234 int rc;
235
236 *pmask = 0;
237
238 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700239 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700240 &pver, sizeof(pver), &rver, sizeof(rver));
241
242 if (rc < 0)
243 return rc;
244
245 *pmask = rver.version_mask;
246 return rc;
247}
248
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800249/* Perform a cold reboot.
250 *
251 * @param flags flags to pass to EC_CMD_REBOOT_EC.
252 * @return 0 for success, < 0 for command failure.
253 */
254static int cros_ec_cold_reboot(int flags) {
255 struct ec_params_reboot_ec p;
256
257 memset(&p, 0, sizeof(p));
258 p.cmd = EC_REBOOT_COLD;
259 p.flags = flags;
260 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
261 NULL, 0);
262}
263
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800264/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800265 * then this functions picks a NEW firmware copy and jumps to it. Note that
266 * RO is preferred, then A, finally B.
267 *
268 * Returns 0 for success.
269 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700270static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800271 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800272 int rc;
Edward O'Callaghan07a297d2020-04-06 12:57:27 +1000273 enum ec_current_image current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800274
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800275 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
276 * jump to different firmware copy. The second EC_RES_SUCCESS would
277 * set the OBF=1 and the next command cannot be executed.
278 * Thus, we call EC to jump only if the target is different.
279 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700280 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700281 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800282 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700283 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900284 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800285
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800286 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900287
288 /* Translate target --> EC reboot command parameter */
289 switch (target) {
290 case EC_IMAGE_RO:
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700291 /*
292 * Do a cold reset instead of JUMP_RO so board enabling
293 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
294 * This is true for EC enabling RWSIG, where
295 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
296 */
297 if (rwsig_enabled)
298 p.cmd = EC_REBOOT_COLD;
299 else
300 p.cmd = EC_REBOOT_JUMP_RO;
Simon Glassc453a642013-07-01 18:08:53 +0900301 break;
302 case EC_IMAGE_RW:
303 p.cmd = EC_REBOOT_JUMP_RW;
304 break;
305 default:
306 /*
307 * If target is unspecified, set EC reboot command to use
308 * a new image. Also set "target" so that it may be used
309 * to update the priv->current_image if jump is successful.
310 */
311 if (fwcopy[EC_IMAGE_RO].flags) {
312 p.cmd = EC_REBOOT_JUMP_RO;
313 target = EC_IMAGE_RO;
314 } else if (fwcopy[EC_IMAGE_RW].flags) {
315 p.cmd = EC_REBOOT_JUMP_RW;
316 target = EC_IMAGE_RW;
317 } else {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700318 return 1;
Simon Glassc453a642013-07-01 18:08:53 +0900319 }
320 break;
321 }
322
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700323 if (p.cmd == EC_REBOOT_COLD)
324 msg_pdbg("Doing a cold reboot instead of JUMP_RO/RW.\n");
325 else
326 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800327
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700328 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800329 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700330 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800331 return 0;
332 }
333
Souvik Ghosh586968a2016-08-11 17:56:24 -0700334 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700335 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800336 if (rc < 0) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700337 msg_perr("CROS_EC cannot jump/reboot to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800338 sections[target], rc);
339 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800340 }
341
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800342 /* Sleep until EC can respond to host command, but just before
343 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
344 usleep(EC_INIT_DELAY);
345
346 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
347 if (target == EC_IMAGE_RO && rwsig_enabled) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700348 msg_pdbg("Aborting RWSIG jump.\n");
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800349 ec_rwsig_abort();
350 }
351
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700352 msg_pdbg("CROS_EC jumped/rebooted to [%s]\n", sections[target]);
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800353 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800354
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700355 return EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800356}
357
David Hendricksb64b39a2016-10-11 13:48:06 -0700358static int cros_ec_restore_wp(void *data)
359{
360 msg_pdbg("Restoring EC soft WP.\n");
361 return set_wp(1);
362}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800363
David Hendricksb64b39a2016-10-11 13:48:06 -0700364static int cros_ec_wp_is_enabled(void)
365{
366 struct ec_params_flash_protect p;
367 struct ec_response_flash_protect r;
368 int rc;
369
370 memset(&p, 0, sizeof(p));
371 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
372 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
373 if (rc < 0) {
374 msg_perr("FAILED: Cannot get the write protection status: %d\n",
375 rc);
376 return -1;
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000377 } else if (rc < (int)sizeof(r)) {
David Hendricksb64b39a2016-10-11 13:48:06 -0700378 msg_perr("FAILED: Too little data returned (expected:%zd, "
379 "actual:%d)\n", sizeof(r), rc);
380 return -1;
381 }
382
383 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
384 return 1;
385
386 return 0;
387}
388
389/*
390 * Prepare EC for update:
391 * - Disable soft WP if needed.
392 * - Parse flashmap.
393 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800394 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700395int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800396 struct fmap *fmap;
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000397 unsigned i, j;
398 int wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800399
Souvik Ghosh586968a2016-08-11 17:56:24 -0700400 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800401
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800402 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
403 rwsig_enabled = 1;
404 msg_pdbg("EC has RWSIG enabled.\n");
405 }
406
David Hendricksb64b39a2016-10-11 13:48:06 -0700407 /*
408 * If HW WP is disabled we may still need to disable write protection
409 * that is active on the EC. Otherwise the EC can reject erase/write
410 * commands.
411 *
412 * Failure is OK since HW WP might be enabled or the EC needs to be
413 * rebooted for the change to take effect. We can still update RW
414 * portions.
415 *
416 * If disabled here, EC WP will be restored at the end so that
417 * "--wp-enable" does not need to be run later. This greatly
418 * simplifies logic for developers and scripts.
419 */
420 wp_status = cros_ec_wp_is_enabled();
421 if (wp_status < 0) {
422 return 1;
423 } else if (wp_status == 1) {
424 msg_pdbg("Attempting to disable EC soft WP.\n");
425 if (!set_wp(0)) {
426 msg_pdbg("EC soft WP disabled successfully.\n");
427 if (register_shutdown(cros_ec_restore_wp, NULL))
428 return 1;
429 } else {
430 msg_pdbg("Failed. Hardware WP might in effect or EC "
431 "needs to be rebooted first.\n");
432 }
433 } else {
434 msg_pdbg("EC soft WP is already disabled.\n");
435 }
436
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800437 // Parse the fmap in the image file and cache the firmware ranges.
438 fmap = fmap_find_in_memory(image, size);
Nicolas Boichata7a062b2018-07-18 15:18:41 +0800439 if (fmap) {
440 // Lookup RO/A/B sections in FMAP.
441 for (i = 0; i < fmap->nareas; i++) {
442 struct fmap_area *fa = &fmap->areas[i];
443 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
444 if (!strcmp(sections[j],
445 (const char *)fa->name)) {
446 msg_pdbg("Found '%s' in image.\n",
447 fa->name);
448 memcpy(&fwcopy[j], fa, sizeof(*fa));
449 fwcopy[j].flags = 1; // mark as new
450 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800451 }
452 }
453 }
454
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700455 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0) {
456 msg_pwarn("Skip jumping to RO\n");
457 return 0;
458 }
459 /* Warning: before update, we jump the EC to RO copy. If you
460 * want to change this behavior, please also check the
461 * cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800462 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700463 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800464}
465
466
467/* Returns >0 if we need 2nd pass of erase_and_write_flash().
468 * <0 if we cannot jump to any firmware copy.
469 * ==0 if no more pass is needed.
470 *
471 * This function also jumps to new-updated firmware copy before return >0.
472 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700473int cros_ec_need_2nd_pass(void)
474{
475 if (!(cros_ec_priv && cros_ec_priv->detected))
476 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800477
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700478 if (!need_2nd_pass)
479 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800480
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700481 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0)
482 /* EC_RES_ACCESS_DENIED is returned when the block is either
483 * protected or unsafe. Thus, theoretically, we shouldn't reach
484 * here because everywhere is safe for EXEC_IN_RAM chips and
485 * WP is disabled before erase/write cycle starts.
486 * We can still let the 2nd pass run (and it will probably
487 * fail again).
488 */
489 return 1;
490
491 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN))
492 return -1;
493
494 return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800495}
496
497
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800498/* Returns 0 for success.
499 *
500 * Try latest firmware: B > A > RO
501 *
David Hendricksb907de32014-08-11 16:47:09 -0700502 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800503 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700504 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800505 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700506int cros_ec_finish(void)
507{
Souvik Ghosh586968a2016-08-11 17:56:24 -0700508 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800509
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800510 /* For EC with RWSIG enabled. We need a cold reboot to enable
511 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
512 */
513 if (rwsig_enabled) {
514 int rc;
515
516 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
517 rc = cros_ec_cold_reboot(0);
518 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
519 return rc;
520 }
521
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800522 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800523 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700524 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
525 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800526 }
527
528 return 0;
529}
530
531
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700532int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700533 unsigned int blockaddr, unsigned int readcnt)
534{
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800535 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800536 struct ec_params_flash_read p;
Craig Hesling65eb8812019-08-01 09:33:56 -0700537 int maxlen = opaque_master->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800538 uint8_t buf[maxlen];
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000539 unsigned offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800540
David Hendricks133083b2012-07-17 20:39:38 -0700541 while (offset < readcnt) {
542 count = min(maxlen, readcnt - offset);
543 p.offset = blockaddr + offset;
544 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700545 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700546 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800547 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700548 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700549 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800550 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800551 } else {
552 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800553 }
554
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800555 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700556 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800557 }
558
559 return rc;
560}
561
562
Simon Glassc453a642013-07-01 18:08:53 +0900563/*
564 * returns 0 to indicate area does not overlap current EC image
565 * returns 1 to indicate area overlaps current EC image or error
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700566 *
567 * We can't get rid of this. The ECs should know what region is safe to erase
568 * or write. We should let them decide (and return EC_RES_ACCESS_DENIED).
569 * Not all existing EC firmware can do so.
Simon Glassc453a642013-07-01 18:08:53 +0900570 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700571static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900572{
Simon Glassc453a642013-07-01 18:08:53 +0900573 enum ec_current_image image;
574 uint32_t region_offset;
575 uint32_t region_size;
576
Souvik Ghosh586968a2016-08-11 17:56:24 -0700577 image = cros_ec_priv->current_image;
578 region_offset = cros_ec_priv->region[image].offset;
579 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900580
581 if ((addr + len - 1 < region_offset) ||
582 (addr > region_offset + region_size - 1)) {
583 return 0;
584 }
585 return 1;
586}
587
588
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700589int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800590 unsigned int blockaddr,
591 unsigned int len) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700592 struct ec_params_flash_erase_v1 erase;
593 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700594 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800595
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700596 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
597 in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700598 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900599 need_2nd_pass = 1;
600 return ACCESS_DENIED;
601 }
602
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700603 erase.params.offset = blockaddr;
604 erase.params.size = len;
605 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
606 if (rc < 0) {
607 msg_perr("Cannot determine erase command version\n");
608 return 0;
609 }
610 cmd_version = 31 - __builtin_clz(mask);
611
612 if (cmd_version == 0) {
613 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
614 &erase.params,
615 sizeof(struct ec_params_flash_erase), NULL, 0);
616 if (rc == -EC_RES_ACCESS_DENIED) {
617 // this is active image.
618 cros_ec_invalidate_copy(blockaddr, len);
619 need_2nd_pass = 1;
620 return ACCESS_DENIED;
621 }
622 if (rc < 0) {
623 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
624 blockaddr, rc);
625 return rc;
626 }
627 goto end_flash_erase;
628 }
629
630 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
631 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
632 } else {
633 erase.cmd = FLASH_ERASE_SECTOR;
634 }
635 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
636 &erase, sizeof(erase), NULL, 0);
637 switch (rc) {
638 case 0:
639 break;
640 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800641 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700642 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800643 need_2nd_pass = 1;
644 return ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700645 case -EC_RES_BUSY:
646 msg_perr("CROS_EC: Flash erase command "
647 " already in progress\n");
Edward O'Callaghan1557e652020-04-06 12:59:37 +1000648 return rc;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700649 default:
650 return rc;
651 }
652 if (len < FLASH_SMALL_REGION_THRESHOLD)
653 goto end_flash_erase;
654
655 /* Wait for the erase command to complete */
656 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700657
658/* wait up to 10s to erase a flash sector */
659#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
660/* wait .5 second between queries. */
661#define CROS_EC_ERASE_ASYNC_WAIT 500000
662
663 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
664 usleep(CROS_EC_ERASE_ASYNC_WAIT);
665 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700666 erase.cmd = FLASH_ERASE_GET_RESULT;
667 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
668 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800669 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800670 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700671 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800672 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800673 return rc;
674 }
675
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700676end_flash_erase:
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800677#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800678 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800679#endif
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700680 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700681 /*
682 * Can happen if the command with retried with
683 * EC_CMD_GET_COMMS_STATUS
684 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700685 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700686 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800687 return rc;
688}
689
690
Patrick Georgiab8353e2017-02-03 18:32:01 +0100691int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800692 unsigned int nbytes) {
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000693 unsigned i;
694 int rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800695 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800696 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700697 uint8_t *packet;
698
Ken Chang69c31b82014-10-28 15:17:21 +0800699 /*
Eric Yilun Lina3f47c42020-07-01 11:59:39 +0800700 * For b:35542013, to workaround the undersized
Ken Chang69c31b82014-10-28 15:17:21 +0800701 * outdata buffer issue in kernel.
Eric Yilun Lina3f47c42020-07-01 11:59:39 +0800702 * chunk size should exclude the packet header ec_params_flash_write.
Ken Chang69c31b82014-10-28 15:17:21 +0800703 */
Eric Yilun Lina3f47c42020-07-01 11:59:39 +0800704 real_write_size = min(opaque_master->max_data_write - sizeof(p),
705 cros_ec_priv->ideal_write_size);
706 assert(real_write_size > 0);
707
Ken Chang69c31b82014-10-28 15:17:21 +0800708 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700709 if (!packet)
710 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800711
712 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800713 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800714 p.offset = addr + i;
715 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900716
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700717 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
718 in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700719 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900720 need_2nd_pass = 1;
721 return ACCESS_DENIED;
722 }
723
David Hendricks2d6db772013-07-10 21:07:48 -0700724 memcpy(packet, &p, sizeof(p));
725 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700726 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700727 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700728
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800729 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800730 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700731 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800732 need_2nd_pass = 1;
733 return ACCESS_DENIED;
734 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800735
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800736 if (rc < 0) break;
737 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800738 }
739
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800740#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800741 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800742#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700743 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800744 return rc;
745}
746
747
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700748static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900749 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800750 int rc;
751
Souvik Ghosh586968a2016-08-11 17:56:24 -0700752 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800753 if (rc < 0) {
754 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
755 return 1;
756 }
757
758 msg_pinfo("Supported write protect range:\n");
759 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900760 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
761 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800762
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800763 return 0;
764}
765
766
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800767/*
768 * Helper function for flash protection.
769 *
770 * On EC API v1, the EC write protection has been simplified to one-bit:
771 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
772 * or disabled. However, this is different from the SPI-style write protect
773 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
774 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
775 *
776 * SRP Range | PROTECT_RO_AT_BOOT
777 * 0 0 | 0
778 * 0 non-zero | 1
779 * 1 0 | 1
780 * 1 non-zero | 1
781 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800782 *
783 * Besides, to make the protection take effect as soon as possible, we
784 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
785 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800786 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700787static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800788 struct ec_params_flash_protect p;
789 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800790 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
791 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
792 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800793 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800794
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800795 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800796 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800797 p.mask = (ro_at_boot_flag | ro_now_flag);
798 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700799 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700800 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800801 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800802 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
803 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800804 return 1;
805 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800806
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800807 /* Read back */
808 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700809 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700810 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800811 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800812 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
813 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800814 return 1;
815 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800816
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800817 if (!enable) {
818 /* The disable case is easier to check. */
819 if (r.flags & ro_at_boot_flag) {
820 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
821 return 1;
822 } else if (r.flags & ro_now_flag) {
823 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
824 need_an_ec_cold_reset = 1;
825 goto exit;
826 }
827
828 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
829 return 0;
830 }
831
832 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
833 if (r.flags & ro_at_boot_flag) {
834 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
835 } else {
836 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
837 return 1;
838 }
839
840 /* Then, we check if the protection has been activated. */
841 if (r.flags & ro_now_flag) {
842 /* Good, RO_NOW is set. */
843 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
844 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800845 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
846
847 memset(&p, 0, sizeof(p));
848 p.mask = EC_FLASH_PROTECT_ALL_NOW;
849 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700850 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800851 EC_VER_FLASH_PROTECT,
852 &p, sizeof(p), &r, sizeof(r));
853 if (rc < 0) {
854 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
855 return 1;
856 }
857
858 /* Read back */
859 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700860 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800861 EC_VER_FLASH_PROTECT,
862 &p, sizeof(p), &r, sizeof(r));
863 if (rc < 0) {
864 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
865 return 1;
866 }
867
868 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
869 msg_perr("FAILED: ALL_NOW is not set.\n");
870 need_an_ec_cold_reset = 1;
871 goto exit;
872 }
873
874 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
875
876 /*
877 * Our goal is to protect the RO ASAP. The entire protection
878 * is just a workaround for platform not supporting RO_NOW.
879 * It has side-effect that the RW is also protected and leads
880 * the RW update failed. So, we arrange an EC code reset to
881 * unlock RW ASAP.
882 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800883 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800884 if (rc < 0) {
885 msg_perr("WARN: Cannot arrange a cold reset at next "
886 "shutdown to unlock entire protect.\n");
887 msg_perr(" But you can do it manually.\n");
888 } else {
889 msg_pdbg("INFO: A cold reset is arranged at next "
890 "shutdown.\n");
891 }
892
893 } else {
894 msg_perr("FAILED: RO_NOW is not set.\n");
895 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
896 "make write protection active now.\n");
897 need_an_ec_cold_reset = 1;
898 }
899
900exit:
901 if (need_an_ec_cold_reset) {
902 msg_perr("FAILED: You may need a reboot to take effect of "
903 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800904 return 1;
905 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800906
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800907 return 0;
908}
909
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700910static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800911 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900912 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800913 int rc;
914
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800915 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700916 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800917 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800918 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800919 return 1;
920 }
921 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900922 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800923 /* pass */
924 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800925 msg_perr("FAILED: Unsupported write protection range "
926 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800927 msg_perr("Currently supported range:\n");
928 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900929 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
930 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800931 return 1;
932 }
933
David Hendricks393deec2016-11-23 16:15:05 -0800934 if (ignore_wp_range_command)
935 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700936 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800937}
938
939
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700940static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700941 enum wp_mode wp_mode) {
942 int ret;
943
944 switch (wp_mode) {
945 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700946 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700947 break;
948 default:
949 msg_perr("%s():%d Unsupported write-protection mode\n",
950 __func__, __LINE__);
951 ret = 1;
952 break;
953 }
954
955 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800956}
957
958
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700959static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800960 /* --wp-range implicitly enables write protection on CrOS EC, so force
961 it not to if --wp-disable is what the user really wants. */
962 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700963 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800964}
965
966
Souvik Ghosh586968a2016-08-11 17:56:24 -0700967static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800968 struct ec_params_flash_protect p;
969 struct ec_response_flash_protect r;
970 int start, len; /* wp range */
971 int enabled;
972 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800973
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800974 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700975 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700976 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800977 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800978 msg_perr("FAILED: Cannot get the write protection status: %d\n",
979 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800980 return 1;
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000981 } else if (rc < (int)sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700982 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800983 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800984 return 1;
985 }
986
987 start = len = 0;
988 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900989 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800990
991 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
992 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700993 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800994 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800995 msg_perr("FAILED: Cannot get the WP_RO region info: "
996 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800997 return 1;
998 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900999 start = info.offset;
1000 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001001 } else {
1002 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
1003 __func__);
1004 }
1005
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +08001006 /*
1007 * If neither RO_NOW or ALL_NOW is set, it means write protect is
1008 * NOT active now.
1009 */
1010 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
1011 start = len = 0;
1012
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001013 /* Remove the SPI-style messages. */
1014 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
1015 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
1016 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001017 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001018 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +08001019 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +08001020 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001021
1022 return 0;
1023}
1024
David Hendrickse5454932013-11-04 18:16:11 -08001025/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -07001026int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -08001027{
1028 struct ec_params_hello request;
1029 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -08001030 int rc = 0;
1031
1032 /* Say hello to EC. */
1033 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
1034 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
1035 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001036 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001037 sizeof(request), &response, sizeof(response));
1038 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1039
1040 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1041 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1042 "rc=%d, request=0x%x response=0x%x\n",
1043 rc, request.in_data, response.out_data);
1044 return 1;
1045 }
1046
1047 return 0;
1048}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001049
David Hendricksd13d90d2016-08-09 17:00:52 -07001050void cros_ec_set_max_size(struct cros_ec_priv *priv,
Edward O'Callaghanabd30192019-05-14 15:58:19 +10001051 struct opaque_master *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001052 struct ec_response_get_protocol_info info;
1053 int rc = 0;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001054
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001055 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001056 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001057 &info, sizeof(info));
1058 msg_pdbg("%s: rc:%d\n", __func__, rc);
1059
Gwendal Grignoucf540ef2017-08-10 12:10:06 -07001060 /*
1061 * Use V3 large size only if v2 protocol is not supported.
1062 * When v2 is supported, we may be using a kernel without v3 support,
1063 * leading to sending larger commands the kernel can support.
1064 */
1065 if (rc == sizeof(info) && ((info.protocol_versions & (1<<2)) == 0)) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001066 op->max_data_write = info.max_request_packet_size -
1067 sizeof(struct ec_host_request);
1068 op->max_data_read = info.max_response_packet_size -
1069 sizeof(struct ec_host_response);
Gwendal Grignouef9062f2017-05-31 17:38:31 -07001070 /*
1071 * Due to a bug in NPCX SPI code (chromium:725580),
1072 * The EC may responds 163 when it meant 160; it should not
1073 * have included header and footer.
1074 */
1075 op->max_data_read &= ~3;
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001076 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1077 op->max_data_write, op->max_data_read);
1078 }
1079}
1080
David Hendricks14935fe2014-08-14 17:38:24 -07001081
1082/*
David Hendricks052446b2014-09-11 11:26:51 -07001083 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001084 *
1085 * This function parses programmer parameters from the command line. Since
1086 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1087 * run during internal programmer initialization.
1088 */
1089int cros_ec_parse_param(struct cros_ec_priv *priv)
1090{
David Hendricks98b3c572016-11-30 01:50:08 +00001091 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001092
David Hendricks98b3c572016-11-30 01:50:08 +00001093 p = extract_programmer_param("type");
1094 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001095 unsigned int index;
1096 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001097 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001098 break;
1099 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001100 msg_perr("Invalid argument: \"%s\"\n", p);
Nikolai Artemiev6c457152020-04-29 11:30:39 +10001101 free(p);
David Hendricks98b3c572016-11-30 01:50:08 +00001102 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001103 }
1104 priv->dev = ec_type[index];
1105 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001106 }
Nikolai Artemiev6c457152020-04-29 11:30:39 +10001107 free(p);
David Hendricks14935fe2014-08-14 17:38:24 -07001108
David Hendricks98b3c572016-11-30 01:50:08 +00001109 p = extract_programmer_param("block");
1110 if (p) {
1111 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001112 char *endptr = NULL;
1113
1114 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001115 block = strtoul(p, &endptr, 0);
1116 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1117 msg_perr("Invalid argument: \"%s\"\n", p);
Nikolai Artemiev6c457152020-04-29 11:30:39 +10001118 free(p);
David Hendricks98b3c572016-11-30 01:50:08 +00001119 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001120 }
1121
David Hendricks98b3c572016-11-30 01:50:08 +00001122 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001123 msg_perr("%s: Invalid block size\n", __func__);
Nikolai Artemiev6c457152020-04-29 11:30:39 +10001124 free(p);
David Hendricks98b3c572016-11-30 01:50:08 +00001125 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001126 }
1127
David Hendricks98b3c572016-11-30 01:50:08 +00001128 msg_pdbg("Override block size to 0x%x\n", block);
1129 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001130 }
Nikolai Artemiev6c457152020-04-29 11:30:39 +10001131 free(p);
Duncan Laurie84328722014-09-10 23:25:01 -07001132
David Hendricks98b3c572016-11-30 01:50:08 +00001133 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001134}
1135
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001136int cros_ec_probe_size(struct flashctx *flash) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001137 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001138 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001139 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001140 struct block_eraser *eraser;
1141 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001142 .list_ranges = cros_ec_list_ranges,
1143 .set_range = cros_ec_set_range,
1144 .enable = cros_ec_enable_writeprotect,
1145 .disable = cros_ec_disable_writeprotect,
1146 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001147 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001148 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001149
Souvik Ghosh586968a2016-08-11 17:56:24 -07001150 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001151 if (rc < 0) {
1152 msg_perr("%s(): Failed to probe (no current image): %d\n",
1153 __func__, rc);
1154 return 0;
1155 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001156 cros_ec_priv->current_image = rc;
1157 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001158
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001159 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1160 if (rc < 0) {
1161 msg_perr("Cannot determine write command version\n");
1162 return 0;
1163 }
1164 cmd_version = 31 - __builtin_clz(mask);
1165
Patrick Georgif3fa2992017-02-02 16:24:44 +01001166 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001167 flash->chip->wp = &wp;
Craig Hesling65eb8812019-08-01 09:33:56 -07001168 flash->chip->page_size = opaque_master->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001169
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001170 if (cmd_version < 2) {
1171 struct ec_response_flash_info_1 info;
1172 /* Request general information about flash (v1 or below). */
1173 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1174 NULL, 0, &info,
1175 (cmd_version > 0 ? sizeof(info) :
1176 sizeof(struct ec_response_flash_info)));
1177 if (rc < 0) {
1178 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1179 cmd_version, rc);
1180 return 0;
1181 }
1182 if (cmd_version == 0) {
1183 cros_ec_priv->ideal_write_size =
1184 EC_FLASH_WRITE_VER0_SIZE;
1185 } else {
1186 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1187 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1188 flash->chip->feature_bits |=
Alan Greendbeec2b2019-09-16 14:36:52 +10001189 FEATURE_ERASED_ZERO;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001190 }
1191 flash->chip->total_size = info.flash_size / 1024;
1192
1193 /* Allow overriding the erase block size in case EC is incorrect */
1194 if (cros_ec_priv->erase_block_size > 0)
1195 eraser->eraseblocks[0].size =
1196 cros_ec_priv->erase_block_size;
1197 else
1198 eraser->eraseblocks[0].size = info.erase_block_size;
1199
1200 eraser->eraseblocks[0].count = info.flash_size /
1201 eraser->eraseblocks[0].size;
1202 } else {
1203 struct ec_response_flash_info_2 info_2;
1204 struct ec_params_flash_info_2 params_2;
1205 struct ec_response_flash_info_2 *info_2_p = &info_2;
1206 int size_info_v2 = sizeof(info_2), i;
1207
1208 params_2.num_banks_desc = 0;
1209 /*
1210 * Call FLASH_INFO twice, second time with all banks
1211 * information.
1212 */
1213 for (i = 0; i < 2; i++) {
1214 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1215 cmd_version, &params_2,
1216 sizeof(params_2),
1217 info_2_p, size_info_v2);
1218 if (rc < 0) {
1219 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1220 __func__,
1221 params_2.num_banks_desc,
1222 cmd_version, rc);
1223 if (info_2_p != &info_2)
1224 free(info_2_p);
1225 return 0;
1226 } else if (i > 0) {
1227 break;
1228 }
1229 params_2.num_banks_desc = info_2_p->num_banks_total;
1230 size_info_v2 += info_2_p->num_banks_total *
1231 sizeof(struct ec_flash_bank);
1232
1233 info_2_p = malloc(size_info_v2);
1234 if (!info_2_p) {
1235 msg_perr("%s(): malloc of %d banks failed\n",
1236 __func__, info_2_p->num_banks_total);
1237 return 0;
1238 }
1239 }
1240 flash->chip->total_size = info_2_p->flash_size / 1024;
1241 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1242 /* Allow overriding the erase block size in case EC is incorrect */
1243 eraser->eraseblocks[i].size =
1244 (cros_ec_priv->erase_block_size > 0 ?
1245 cros_ec_priv->erase_block_size :
Edward O'Callaghan4b9ef6a2020-04-06 12:58:40 +10001246 (unsigned) 1 << info_2_p->banks[i].erase_size_exp);
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001247 eraser->eraseblocks[i].count =
1248 info_2_p->banks[i].count <<
1249 (info_2_p->banks[i].size_exp -
1250 info_2_p->banks[i].erase_size_exp);
1251 }
1252 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001253#if 0
1254 /*
1255 * TODO(b/38506987)Comment out, as some firmware were not
1256 * setting this flag properly.
1257 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001258 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
Alan Greendbeec2b2019-09-16 14:36:52 +10001259 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001260#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001261 free(info_2_p);
1262 }
Vadim Bendeburyadbd7062018-06-19 21:36:45 -07001263 eraser->block_erase = cros_ec_block_erase;
David Hendricks194b3bb2013-07-16 14:32:26 -07001264 /*
1265 * Some STM32 variants erase bits to 0. For now, assume that this
1266 * applies to STM32L parts.
1267 *
1268 * FIXME: This info will eventually be exposed via some EC command.
1269 * See chrome-os-partner:20973.
1270 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001271 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001272 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001273 if (rc < 0) {
1274 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1275 return 0;
1276 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001277 if (!strncmp(chip_info.name, "stm32l1", 7))
Alan Greendbeec2b2019-09-16 14:36:52 +10001278 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001279
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001280
David Hendricksf9461c72013-07-11 19:02:13 -07001281
David Hendricksa672b042016-09-19 12:37:36 -07001282 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1283 0, NULL, 0, &spi_info, sizeof(spi_info));
1284 if (rc < 0) {
1285 static char chip_vendor[32];
1286 static char chip_name[32];
1287
1288 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1289 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001290 flash->chip->vendor = chip_vendor;
1291 flash->chip->name = chip_name;
Alan Greenb2fe0472019-07-30 14:33:28 +10001292 flash->chip->tested = TEST_OK_PREW;
David Hendricksa672b042016-09-19 12:37:36 -07001293 } else {
1294 const struct flashchip *f;
1295 uint32_t mfg = spi_info.jedec[0];
1296 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1297
1298 for (f = flashchips; f && f->name; f++) {
1299 if (f->bustype != BUS_SPI)
1300 continue;
1301 if ((f->manufacture_id == mfg) &&
1302 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001303 flash->chip->vendor = f->vendor;
1304 flash->chip->name = f->name;
1305 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001306 break;
1307 }
1308 }
1309 }
1310
Simon Glassc453a642013-07-01 18:08:53 +09001311 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1312 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001313 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1314 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001315 if (rc) {
1316 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1317 __func__, rc);
1318 return 0;
1319 }
1320
Souvik Ghosh586968a2016-08-11 17:56:24 -07001321 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1322 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001323 if (rc) {
1324 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1325 __func__, rc);
1326 return 0;
1327 }
1328
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001329 return 1;
1330};