blob: a514b501a102d69f7199ae5909fb6f4b61a281f6 [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
Gwendal Grignoua36ff502015-03-23 16:36:47 -070060/* For region larger use async version for FLASH_ERASE */
61#define FLASH_SMALL_REGION_THRESHOLD (16 * 1024)
62
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080063/* 1 if we want the flashrom to call erase_and_write_flash() again. */
64static int need_2nd_pass = 0;
65
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +080066/* 1 if EC firmware has RWSIG enabled. */
67static int rwsig_enabled = 0;
68
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080069/* The range of each firmware copy from the image file to update.
70 * But re-define the .flags as the valid flag to indicate the firmware is
71 * new or not (if flags = 1).
72 */
73static struct fmap_area fwcopy[4]; // [0] is not used.
74
75/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080076static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070077 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
78 "EC_RO",
79 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080080};
81
Gwendal Grignoua36ff502015-03-23 16:36:47 -070082static struct ec_response_flash_region_info regions[EC_FLASH_REGION_COUNT];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080083
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +080084/*
85 * Delay after reboot before EC can respond to host command.
86 * This value should be large enough for EC to initialize, but no larger than
87 * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be
88 * able to abort RWSIG jump and stay in RO.
89 */
90#define EC_INIT_DELAY 800000
91
92/*
93 * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW.
94 */
95#define EC_RWSIG_JUMP_TO_RW_DELAY 3000000
96
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080097/* Given the range not able to update, mark the corresponding
98 * firmware as old.
99 */
David Hendricksb907de32014-08-11 16:47:09 -0700100static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800101{
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000102 unsigned i;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800103
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800104 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800105 struct fmap_area *fw = &fwcopy[i];
106 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
107 (fw->offset >= addr && (fw->offset < addr + len))) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -0700108 msg_pdbg(" OLD[%s]", sections[i]);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800109 fw->flags = 0; // mark as old
110 }
111 }
112}
113
114
Souvik Ghosh586968a2016-08-11 17:56:24 -0700115static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900116{
117 struct ec_response_get_version resp;
118 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700119
Souvik Ghosh586968a2016-08-11 17:56:24 -0700120 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700121 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900122 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700123 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900124 return rc;
125 }
126 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700127 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900128 return -1;
129 }
130
131 return resp.current_image;
132}
133
134
Souvik Ghosh586968a2016-08-11 17:56:24 -0700135static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900136 struct ec_response_flash_region_info *info)
137{
138 struct ec_params_flash_region_info req;
139 struct ec_response_flash_region_info resp;
140 int rc;
141
142 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700143 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900144 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
145 &resp, sizeof(resp));
146 if (rc < 0) {
147 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
148 return rc;
149 }
150
151 info->offset = resp.offset;
152 info->size = resp.size;
153 return 0;
154}
155
David Hendricksf9461c72013-07-11 19:02:13 -0700156/**
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800157 * Check if a feature is supported by EC.
158 *
159 * @param feature feature code
160 * @return < 0 if error, 0 not supported, > 0 supported
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700161 *
162 * NOTE: Once it successfully runs, the feature bits are cached. So, if you
163 * want to query a feature that can be different per copy, you need to
164 * cache features per image copy.
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800165 */
166static int ec_check_features(int feature)
167{
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700168 static struct ec_response_get_features r;
169 int rc = 0;
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800170
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000171 if (feature < 0 || feature >= (int)sizeof(r.flags) * 8)
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800172 return -1;
173
Daisuke Nojiri40592e42018-04-04 16:38:54 -0700174 /* We don't cache return code. We retry regardless the return code. */
175 if (r.flags[0] == 0)
176 rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES,
177 0, NULL, 0, &r, sizeof(r));
178
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800179 if (rc < 0)
180 return rc;
181
Daisuke Nojirif8ab92f2018-04-04 10:13:38 -0700182 return !!(r.flags[feature / 32] & (1 << (feature % 32)));
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800183}
184
185/**
186 * Disable EC rwsig jump.
187 *
188 * @return 0 if success, <0 if error
189 */
190static int ec_rwsig_abort()
191{
192 struct ec_params_rwsig_action p;
193
194 p.action = RWSIG_ACTION_ABORT;
195 return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION,
196 0, &p, sizeof(p), NULL, 0);
197}
198
199/**
David Hendricksf9461c72013-07-11 19:02:13 -0700200 * Get the versions of the command supported by the EC.
201 *
202 * @param cmd Command
203 * @param pmask Destination for version mask; will be set to 0 on
204 * error.
205 * @return 0 if success, <0 if error
206 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700207static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700208{
David Hendricksf9461c72013-07-11 19:02:13 -0700209 struct ec_params_get_cmd_versions pver;
210 struct ec_response_get_cmd_versions rver;
211 int rc;
212
213 *pmask = 0;
214
215 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700216 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700217 &pver, sizeof(pver), &rver, sizeof(rver));
218
219 if (rc < 0)
220 return rc;
221
222 *pmask = rver.version_mask;
223 return rc;
224}
225
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800226/* Perform a cold reboot.
227 *
228 * @param flags flags to pass to EC_CMD_REBOOT_EC.
229 * @return 0 for success, < 0 for command failure.
230 */
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000231static int cros_ec_cold_reboot(int flags)
232{
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800233 struct ec_params_reboot_ec p;
234
235 memset(&p, 0, sizeof(p));
236 p.cmd = EC_REBOOT_COLD;
237 p.flags = flags;
238 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
239 NULL, 0);
240}
241
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800242/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800243 * then this functions picks a NEW firmware copy and jumps to it. Note that
244 * RO is preferred, then A, finally B.
245 *
246 * Returns 0 for success.
247 */
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000248static int cros_ec_jump_copy(enum ec_current_image target)
249{
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800250 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800251 int rc;
Edward O'Callaghan07a297d2020-04-06 12:57:27 +1000252 enum ec_current_image current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800253
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800254 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
255 * jump to different firmware copy. The second EC_RES_SUCCESS would
256 * set the OBF=1 and the next command cannot be executed.
257 * Thus, we call EC to jump only if the target is different.
258 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700259 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700260 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800261 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700262 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900263 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800264
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800265 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900266
267 /* Translate target --> EC reboot command parameter */
268 switch (target) {
269 case EC_IMAGE_RO:
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700270 /*
271 * Do a cold reset instead of JUMP_RO so board enabling
272 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
273 * This is true for EC enabling RWSIG, where
274 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
275 */
276 if (rwsig_enabled)
277 p.cmd = EC_REBOOT_COLD;
278 else
279 p.cmd = EC_REBOOT_JUMP_RO;
Simon Glassc453a642013-07-01 18:08:53 +0900280 break;
281 case EC_IMAGE_RW:
282 p.cmd = EC_REBOOT_JUMP_RW;
283 break;
284 default:
285 /*
286 * If target is unspecified, set EC reboot command to use
287 * a new image. Also set "target" so that it may be used
288 * to update the priv->current_image if jump is successful.
289 */
290 if (fwcopy[EC_IMAGE_RO].flags) {
291 p.cmd = EC_REBOOT_JUMP_RO;
292 target = EC_IMAGE_RO;
293 } else if (fwcopy[EC_IMAGE_RW].flags) {
294 p.cmd = EC_REBOOT_JUMP_RW;
295 target = EC_IMAGE_RW;
296 } else {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700297 return 1;
Simon Glassc453a642013-07-01 18:08:53 +0900298 }
299 break;
300 }
301
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700302 if (p.cmd == EC_REBOOT_COLD)
303 msg_pdbg("Doing a cold reboot instead of JUMP_RO/RW.\n");
304 else
305 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800306
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700307 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800308 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700309 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800310 return 0;
311 }
312
Souvik Ghosh586968a2016-08-11 17:56:24 -0700313 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700314 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800315 if (rc < 0) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700316 msg_perr("CROS_EC cannot jump/reboot to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800317 sections[target], rc);
318 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800319 }
320
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800321 /* Sleep until EC can respond to host command, but just before
322 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
323 usleep(EC_INIT_DELAY);
324
325 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
326 if (target == EC_IMAGE_RO && rwsig_enabled) {
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700327 msg_pdbg("Aborting RWSIG jump.\n");
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800328 ec_rwsig_abort();
329 }
330
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700331 msg_pdbg("CROS_EC jumped/rebooted to [%s]\n", sections[target]);
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800332 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800333
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700334 return EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800335}
336
David Hendricksb64b39a2016-10-11 13:48:06 -0700337static int cros_ec_restore_wp(void *data)
338{
339 msg_pdbg("Restoring EC soft WP.\n");
340 return set_wp(1);
341}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800342
David Hendricksb64b39a2016-10-11 13:48:06 -0700343static int cros_ec_wp_is_enabled(void)
344{
345 struct ec_params_flash_protect p;
346 struct ec_response_flash_protect r;
347 int rc;
348
349 memset(&p, 0, sizeof(p));
350 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
351 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
352 if (rc < 0) {
353 msg_perr("FAILED: Cannot get the write protection status: %d\n",
354 rc);
355 return -1;
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000356 } else if (rc < (int)sizeof(r)) {
David Hendricksb64b39a2016-10-11 13:48:06 -0700357 msg_perr("FAILED: Too little data returned (expected:%zd, "
358 "actual:%d)\n", sizeof(r), rc);
359 return -1;
360 }
361
362 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
363 return 1;
364
365 return 0;
366}
367
368/*
369 * Prepare EC for update:
370 * - Disable soft WP if needed.
371 * - Parse flashmap.
372 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800373 */
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000374int cros_ec_prepare(uint8_t *image, int size)
375{
Edward O'Callaghand8ddde52020-07-30 11:15:45 +1000376 struct fmap *fmap = NULL;
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000377 unsigned i, j;
378 int wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800379
Souvik Ghosh586968a2016-08-11 17:56:24 -0700380 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800381
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800382 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
383 rwsig_enabled = 1;
384 msg_pdbg("EC has RWSIG enabled.\n");
385 }
386
David Hendricksb64b39a2016-10-11 13:48:06 -0700387 /*
388 * If HW WP is disabled we may still need to disable write protection
389 * that is active on the EC. Otherwise the EC can reject erase/write
390 * commands.
391 *
392 * Failure is OK since HW WP might be enabled or the EC needs to be
393 * rebooted for the change to take effect. We can still update RW
394 * portions.
395 *
396 * If disabled here, EC WP will be restored at the end so that
397 * "--wp-enable" does not need to be run later. This greatly
398 * simplifies logic for developers and scripts.
399 */
400 wp_status = cros_ec_wp_is_enabled();
401 if (wp_status < 0) {
402 return 1;
403 } else if (wp_status == 1) {
404 msg_pdbg("Attempting to disable EC soft WP.\n");
405 if (!set_wp(0)) {
406 msg_pdbg("EC soft WP disabled successfully.\n");
407 if (register_shutdown(cros_ec_restore_wp, NULL))
408 return 1;
409 } else {
410 msg_pdbg("Failed. Hardware WP might in effect or EC "
411 "needs to be rebooted first.\n");
412 }
413 } else {
414 msg_pdbg("EC soft WP is already disabled.\n");
415 }
416
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800417 // Parse the fmap in the image file and cache the firmware ranges.
Edward O'Callaghana02ec342020-07-24 20:14:31 +1000418 if (!fmap_read_from_buffer(&fmap, image, size)) {
Nicolas Boichata7a062b2018-07-18 15:18:41 +0800419 // Lookup RO/A/B sections in FMAP.
420 for (i = 0; i < fmap->nareas; i++) {
421 struct fmap_area *fa = &fmap->areas[i];
422 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
423 if (!strcmp(sections[j],
424 (const char *)fa->name)) {
425 msg_pdbg("Found '%s' in image.\n",
426 fa->name);
427 memcpy(&fwcopy[j], fa, sizeof(*fa));
428 fwcopy[j].flags = 1; // mark as new
429 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800430 }
431 }
Edward O'Callaghand8ddde52020-07-30 11:15:45 +1000432 free(fmap);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800433 }
434
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700435 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0) {
436 msg_pwarn("Skip jumping to RO\n");
437 return 0;
438 }
439 /* Warning: before update, we jump the EC to RO copy. If you
440 * want to change this behavior, please also check the
441 * cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800442 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700443 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800444}
445
446
447/* Returns >0 if we need 2nd pass of erase_and_write_flash().
448 * <0 if we cannot jump to any firmware copy.
449 * ==0 if no more pass is needed.
450 *
451 * This function also jumps to new-updated firmware copy before return >0.
452 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700453int cros_ec_need_2nd_pass(void)
454{
455 if (!(cros_ec_priv && cros_ec_priv->detected))
456 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800457
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700458 if (!need_2nd_pass)
459 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800460
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700461 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) > 0)
462 /* EC_RES_ACCESS_DENIED is returned when the block is either
463 * protected or unsafe. Thus, theoretically, we shouldn't reach
464 * here because everywhere is safe for EXEC_IN_RAM chips and
465 * WP is disabled before erase/write cycle starts.
466 * We can still let the 2nd pass run (and it will probably
467 * fail again).
468 */
469 return 1;
470
471 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN))
472 return -1;
473
474 return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800475}
476
477
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800478/* Returns 0 for success.
479 *
480 * Try latest firmware: B > A > RO
481 *
David Hendricksb907de32014-08-11 16:47:09 -0700482 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800483 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700484 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800485 */
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700486int cros_ec_finish(void)
487{
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000488 if (!(cros_ec_priv && cros_ec_priv->detected))
489 return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800490
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800491 /* For EC with RWSIG enabled. We need a cold reboot to enable
492 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
493 */
494 if (rwsig_enabled) {
495 int rc;
496
497 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
498 rc = cros_ec_cold_reboot(0);
499 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
500 return rc;
501 }
502
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800503 return 0;
504}
505
506
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700507int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Daisuke Nojiri790efaa2018-09-07 14:54:01 -0700508 unsigned int blockaddr, unsigned int readcnt)
509{
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800510 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800511 struct ec_params_flash_read p;
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100512 int maxlen = flash->mst->opaque.max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800513 uint8_t buf[maxlen];
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000514 unsigned offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800515
David Hendricks133083b2012-07-17 20:39:38 -0700516 while (offset < readcnt) {
517 count = min(maxlen, readcnt - offset);
518 p.offset = blockaddr + offset;
519 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700520 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700521 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800522 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700523 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700524 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800525 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800526 } else {
527 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800528 }
529
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800530 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700531 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800532 }
533
534 return rc;
535}
536
537
Simon Glassc453a642013-07-01 18:08:53 +0900538/*
539 * returns 0 to indicate area does not overlap current EC image
540 * returns 1 to indicate area overlaps current EC image or error
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700541 *
542 * We can't get rid of this. The ECs should know what region is safe to erase
543 * or write. We should let them decide (and return EC_RES_ACCESS_DENIED).
544 * Not all existing EC firmware can do so.
Simon Glassc453a642013-07-01 18:08:53 +0900545 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700546static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900547{
Simon Glassc453a642013-07-01 18:08:53 +0900548 enum ec_current_image image;
549 uint32_t region_offset;
550 uint32_t region_size;
551
Souvik Ghosh586968a2016-08-11 17:56:24 -0700552 image = cros_ec_priv->current_image;
553 region_offset = cros_ec_priv->region[image].offset;
554 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900555
556 if ((addr + len - 1 < region_offset) ||
557 (addr > region_offset + region_size - 1)) {
558 return 0;
559 }
560 return 1;
561}
562
563
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000564int cros_ec_block_erase(struct flashctx *flash, unsigned int blockaddr,
565 unsigned int len)
566{
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700567 struct ec_params_flash_erase_v1 erase;
568 uint32_t mask;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700569 int rc, cmd_version, timeout=0;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800570
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700571 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
572 in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700573 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900574 need_2nd_pass = 1;
Edward O'Callaghan0a92ce22020-12-09 17:10:37 +1100575 return SPI_ACCESS_DENIED;
Simon Glassc453a642013-07-01 18:08:53 +0900576 }
577
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700578 erase.params.offset = blockaddr;
579 erase.params.size = len;
580 rc = ec_get_cmd_versions(EC_CMD_FLASH_ERASE, &mask);
581 if (rc < 0) {
582 msg_perr("Cannot determine erase command version\n");
583 return 0;
584 }
585 cmd_version = 31 - __builtin_clz(mask);
586
587 if (cmd_version == 0) {
588 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, 0,
589 &erase.params,
590 sizeof(struct ec_params_flash_erase), NULL, 0);
591 if (rc == -EC_RES_ACCESS_DENIED) {
592 // this is active image.
593 cros_ec_invalidate_copy(blockaddr, len);
594 need_2nd_pass = 1;
Edward O'Callaghan0a92ce22020-12-09 17:10:37 +1100595 return SPI_ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700596 }
597 if (rc < 0) {
598 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
599 blockaddr, rc);
600 return rc;
601 }
602 goto end_flash_erase;
603 }
604
605 if (len >= FLASH_SMALL_REGION_THRESHOLD) {
606 erase.cmd = FLASH_ERASE_SECTOR_ASYNC;
607 } else {
608 erase.cmd = FLASH_ERASE_SECTOR;
609 }
610 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
611 &erase, sizeof(erase), NULL, 0);
612 switch (rc) {
613 case 0:
614 break;
615 case -EC_RES_ACCESS_DENIED:
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800616 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700617 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800618 need_2nd_pass = 1;
Edward O'Callaghan0a92ce22020-12-09 17:10:37 +1100619 return SPI_ACCESS_DENIED;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700620 case -EC_RES_BUSY:
621 msg_perr("CROS_EC: Flash erase command "
622 " already in progress\n");
Edward O'Callaghan1557e652020-04-06 12:59:37 +1000623 return rc;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700624 default:
625 return rc;
626 }
627 if (len < FLASH_SMALL_REGION_THRESHOLD)
628 goto end_flash_erase;
629
630 /* Wait for the erase command to complete */
631 rc = -EC_RES_BUSY;
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700632
633/* wait up to 10s to erase a flash sector */
634#define CROS_EC_ERASE_ASYNC_TIMEOUT 10000000
635/* wait .5 second between queries. */
636#define CROS_EC_ERASE_ASYNC_WAIT 500000
637
638 while (rc < 0 && timeout < CROS_EC_ERASE_ASYNC_TIMEOUT) {
639 usleep(CROS_EC_ERASE_ASYNC_WAIT);
640 timeout += CROS_EC_ERASE_ASYNC_WAIT;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700641 erase.cmd = FLASH_ERASE_GET_RESULT;
642 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, cmd_version,
643 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800644 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800645 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700646 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800647 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800648 return rc;
649 }
650
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700651end_flash_erase:
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700652 if (rc > 0) {
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700653 /*
654 * Can happen if the command with retried with
655 * EC_CMD_GET_COMMS_STATUS
656 */
Gwendal Grignoud42cf5a2017-05-22 22:48:53 -0700657 rc = -EC_RES_SUCCESS;
Gwendal Grignoua36ff502015-03-23 16:36:47 -0700658 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800659 return rc;
660}
661
662
Patrick Georgiab8353e2017-02-03 18:32:01 +0100663int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000664 unsigned int nbytes)
665{
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000666 unsigned i;
667 int rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800668 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800669 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700670 uint8_t *packet;
671
Ken Chang69c31b82014-10-28 15:17:21 +0800672 /*
Eric Yilun Lina3f47c42020-07-01 11:59:39 +0800673 * For b:35542013, to workaround the undersized
Ken Chang69c31b82014-10-28 15:17:21 +0800674 * outdata buffer issue in kernel.
Eric Yilun Lina3f47c42020-07-01 11:59:39 +0800675 * chunk size should exclude the packet header ec_params_flash_write.
Ken Chang69c31b82014-10-28 15:17:21 +0800676 */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100677 real_write_size = min(flash->mst->opaque.max_data_write - sizeof(p),
Eric Yilun Lina3f47c42020-07-01 11:59:39 +0800678 cros_ec_priv->ideal_write_size);
679 assert(real_write_size > 0);
680
Ken Chang69c31b82014-10-28 15:17:21 +0800681 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700682 if (!packet)
683 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800684
685 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800686 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800687 p.offset = addr + i;
688 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900689
Daisuke Nojiricfd7dfc2018-04-04 10:43:30 -0700690 if (ec_check_features(EC_FEATURE_EXEC_IN_RAM) <= 0 &&
691 in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700692 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900693 need_2nd_pass = 1;
Edward O'Callaghan0a92ce22020-12-09 17:10:37 +1100694 return SPI_ACCESS_DENIED;
Simon Glassc453a642013-07-01 18:08:53 +0900695 }
696
David Hendricks2d6db772013-07-10 21:07:48 -0700697 memcpy(packet, &p, sizeof(p));
698 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700699 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700700 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700701
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800702 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800703 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700704 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800705 need_2nd_pass = 1;
Edward O'Callaghan0a92ce22020-12-09 17:10:37 +1100706 return SPI_ACCESS_DENIED;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800707 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800708
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800709 if (rc < 0) break;
710 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800711 }
712
David Hendricks2d6db772013-07-10 21:07:48 -0700713 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800714 return rc;
715}
716
717
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000718static int cros_ec_list_ranges(const struct flashctx *flash)
719{
Simon Glass3c01dca2013-07-01 18:07:34 +0900720 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800721 int rc;
722
Souvik Ghosh586968a2016-08-11 17:56:24 -0700723 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800724 if (rc < 0) {
725 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
726 return 1;
727 }
728
729 msg_pinfo("Supported write protect range:\n");
730 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900731 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
732 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800733
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800734 return 0;
735}
736
737
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800738/*
739 * Helper function for flash protection.
740 *
741 * On EC API v1, the EC write protection has been simplified to one-bit:
742 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
743 * or disabled. However, this is different from the SPI-style write protect
744 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
745 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
746 *
747 * SRP Range | PROTECT_RO_AT_BOOT
748 * 0 0 | 0
749 * 0 non-zero | 1
750 * 1 0 | 1
751 * 1 non-zero | 1
752 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800753 *
754 * Besides, to make the protection take effect as soon as possible, we
755 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
756 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800757 */
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000758static int set_wp(int enable)
759{
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800760 struct ec_params_flash_protect p;
761 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800762 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
763 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
764 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800765 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800766
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800767 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800768 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800769 p.mask = (ro_at_boot_flag | ro_now_flag);
770 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700771 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700772 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800773 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800774 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
775 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800776 return 1;
777 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800778
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800779 /* Read back */
780 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700781 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700782 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800783 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800784 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
785 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800786 return 1;
787 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800788
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800789 if (!enable) {
790 /* The disable case is easier to check. */
791 if (r.flags & ro_at_boot_flag) {
792 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
793 return 1;
794 } else if (r.flags & ro_now_flag) {
795 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
796 need_an_ec_cold_reset = 1;
797 goto exit;
798 }
799
800 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
801 return 0;
802 }
803
804 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
805 if (r.flags & ro_at_boot_flag) {
806 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
807 } else {
808 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
809 return 1;
810 }
811
812 /* Then, we check if the protection has been activated. */
813 if (r.flags & ro_now_flag) {
814 /* Good, RO_NOW is set. */
815 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
816 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800817 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
818
819 memset(&p, 0, sizeof(p));
820 p.mask = EC_FLASH_PROTECT_ALL_NOW;
821 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700822 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800823 EC_VER_FLASH_PROTECT,
824 &p, sizeof(p), &r, sizeof(r));
825 if (rc < 0) {
826 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
827 return 1;
828 }
829
830 /* Read back */
831 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700832 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800833 EC_VER_FLASH_PROTECT,
834 &p, sizeof(p), &r, sizeof(r));
835 if (rc < 0) {
836 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
837 return 1;
838 }
839
840 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
841 msg_perr("FAILED: ALL_NOW is not set.\n");
842 need_an_ec_cold_reset = 1;
843 goto exit;
844 }
845
846 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
847
848 /*
849 * Our goal is to protect the RO ASAP. The entire protection
850 * is just a workaround for platform not supporting RO_NOW.
851 * It has side-effect that the RW is also protected and leads
852 * the RW update failed. So, we arrange an EC code reset to
853 * unlock RW ASAP.
854 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800855 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800856 if (rc < 0) {
857 msg_perr("WARN: Cannot arrange a cold reset at next "
858 "shutdown to unlock entire protect.\n");
859 msg_perr(" But you can do it manually.\n");
860 } else {
861 msg_pdbg("INFO: A cold reset is arranged at next "
862 "shutdown.\n");
863 }
864
865 } else {
866 msg_perr("FAILED: RO_NOW is not set.\n");
867 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
868 "make write protection active now.\n");
869 need_an_ec_cold_reset = 1;
870 }
871
872exit:
873 if (need_an_ec_cold_reset) {
874 msg_perr("FAILED: You may need a reboot to take effect of "
875 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800876 return 1;
877 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800878
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800879 return 0;
880}
881
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000882static int cros_ec_set_range(const struct flashctx *flash, unsigned int start, unsigned int len)
883{
Simon Glass3c01dca2013-07-01 18:07:34 +0900884 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800885 int rc;
886
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800887 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700888 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800889 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800890 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800891 return 1;
892 }
893 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900894 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800895 /* pass */
896 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800897 msg_perr("FAILED: Unsupported write protection range "
898 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800899 msg_perr("Currently supported range:\n");
900 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900901 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
902 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800903 return 1;
904 }
905
David Hendricks393deec2016-11-23 16:15:05 -0800906 if (ignore_wp_range_command)
907 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700908 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800909}
910
911
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000912static int cros_ec_enable_writeprotect(const struct flashctx *flash, enum wp_mode wp_mode)
913{
David Hendricks1c09f802012-10-03 11:03:48 -0700914 int ret;
915
916 switch (wp_mode) {
917 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700918 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700919 break;
920 default:
921 msg_perr("%s():%d Unsupported write-protection mode\n",
922 __func__, __LINE__);
923 ret = 1;
924 break;
925 }
926
927 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800928}
929
930
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000931static int cros_ec_disable_writeprotect(const struct flashctx *flash)
932{
David Hendricks393deec2016-11-23 16:15:05 -0800933 /* --wp-range implicitly enables write protection on CrOS EC, so force
934 it not to if --wp-disable is what the user really wants. */
935 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700936 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800937}
938
939
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000940static int cros_ec_wp_status(const struct flashctx *flash)
941{
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800942 struct ec_params_flash_protect p;
943 struct ec_response_flash_protect r;
944 int start, len; /* wp range */
945 int enabled;
946 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800947
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800948 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700949 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700950 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800951 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800952 msg_perr("FAILED: Cannot get the write protection status: %d\n",
953 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800954 return 1;
Edward O'Callaghan04315fc2020-04-06 12:56:07 +1000955 } else if (rc < (int)sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700956 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800957 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800958 return 1;
959 }
960
961 start = len = 0;
962 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900963 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800964
965 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
966 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700967 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800968 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800969 msg_perr("FAILED: Cannot get the WP_RO region info: "
970 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800971 return 1;
972 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900973 start = info.offset;
974 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800975 } else {
976 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
977 __func__);
978 }
979
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800980 /*
981 * If neither RO_NOW or ALL_NOW is set, it means write protect is
982 * NOT active now.
983 */
984 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
985 start = len = 0;
986
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800987 /* Remove the SPI-style messages. */
988 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
989 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
990 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800991 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800992 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800993 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800994 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800995
996 return 0;
997}
998
Edward O'Callaghan3186f942020-07-28 19:08:39 +1000999int cros_ec_probe_size(struct flashctx *flash)
1000{
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001001 int rc = 0, cmd_version;
David Hendricksa672b042016-09-19 12:37:36 -07001002 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001003 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001004 struct block_eraser *eraser;
1005 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001006 .list_ranges = cros_ec_list_ranges,
1007 .set_range = cros_ec_set_range,
1008 .enable = cros_ec_enable_writeprotect,
1009 .disable = cros_ec_disable_writeprotect,
1010 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001011 };
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001012 uint32_t mask;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001013
Souvik Ghosh586968a2016-08-11 17:56:24 -07001014 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001015 if (rc < 0) {
1016 msg_perr("%s(): Failed to probe (no current image): %d\n",
1017 __func__, rc);
1018 return 0;
1019 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001020 cros_ec_priv->current_image = rc;
1021 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001022
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001023 rc = ec_get_cmd_versions(EC_CMD_FLASH_INFO, &mask);
1024 if (rc < 0) {
1025 msg_perr("Cannot determine write command version\n");
1026 return 0;
1027 }
1028 cmd_version = 31 - __builtin_clz(mask);
1029
Patrick Georgif3fa2992017-02-02 16:24:44 +01001030 eraser = &flash->chip->block_erasers[0];
Patrick Georgif3fa2992017-02-02 16:24:44 +01001031 flash->chip->wp = &wp;
Edward O'Callaghanc66827e2020-10-09 12:22:04 +11001032 flash->chip->page_size = flash->mst->opaque.max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001033
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001034 if (cmd_version < 2) {
1035 struct ec_response_flash_info_1 info;
1036 /* Request general information about flash (v1 or below). */
1037 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, cmd_version,
1038 NULL, 0, &info,
1039 (cmd_version > 0 ? sizeof(info) :
1040 sizeof(struct ec_response_flash_info)));
1041 if (rc < 0) {
1042 msg_perr("%s(): FLASH_INFO v%d returns %d.\n", __func__,
1043 cmd_version, rc);
1044 return 0;
1045 }
1046 if (cmd_version == 0) {
1047 cros_ec_priv->ideal_write_size =
1048 EC_FLASH_WRITE_VER0_SIZE;
1049 } else {
1050 cros_ec_priv->ideal_write_size = info.write_ideal_size;
1051 if (info.flags & EC_FLASH_INFO_ERASE_TO_0)
1052 flash->chip->feature_bits |=
Alan Greendbeec2b2019-09-16 14:36:52 +10001053 FEATURE_ERASED_ZERO;
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001054 }
1055 flash->chip->total_size = info.flash_size / 1024;
1056
1057 /* Allow overriding the erase block size in case EC is incorrect */
1058 if (cros_ec_priv->erase_block_size > 0)
1059 eraser->eraseblocks[0].size =
1060 cros_ec_priv->erase_block_size;
1061 else
1062 eraser->eraseblocks[0].size = info.erase_block_size;
1063
1064 eraser->eraseblocks[0].count = info.flash_size /
1065 eraser->eraseblocks[0].size;
1066 } else {
1067 struct ec_response_flash_info_2 info_2;
1068 struct ec_params_flash_info_2 params_2;
1069 struct ec_response_flash_info_2 *info_2_p = &info_2;
1070 int size_info_v2 = sizeof(info_2), i;
1071
1072 params_2.num_banks_desc = 0;
1073 /*
1074 * Call FLASH_INFO twice, second time with all banks
1075 * information.
1076 */
1077 for (i = 0; i < 2; i++) {
1078 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
1079 cmd_version, &params_2,
1080 sizeof(params_2),
1081 info_2_p, size_info_v2);
1082 if (rc < 0) {
1083 msg_perr("%s(): FLASH_INFO(%d) v%d returns %d.\n",
1084 __func__,
1085 params_2.num_banks_desc,
1086 cmd_version, rc);
1087 if (info_2_p != &info_2)
1088 free(info_2_p);
1089 return 0;
1090 } else if (i > 0) {
1091 break;
1092 }
1093 params_2.num_banks_desc = info_2_p->num_banks_total;
1094 size_info_v2 += info_2_p->num_banks_total *
1095 sizeof(struct ec_flash_bank);
1096
1097 info_2_p = malloc(size_info_v2);
1098 if (!info_2_p) {
1099 msg_perr("%s(): malloc of %d banks failed\n",
1100 __func__, info_2_p->num_banks_total);
1101 return 0;
1102 }
1103 }
1104 flash->chip->total_size = info_2_p->flash_size / 1024;
1105 for (i = 0; i < info_2_p->num_banks_desc; i++) {
1106 /* Allow overriding the erase block size in case EC is incorrect */
1107 eraser->eraseblocks[i].size =
1108 (cros_ec_priv->erase_block_size > 0 ?
1109 cros_ec_priv->erase_block_size :
Edward O'Callaghan4b9ef6a2020-04-06 12:58:40 +10001110 (unsigned) 1 << info_2_p->banks[i].erase_size_exp);
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001111 eraser->eraseblocks[i].count =
1112 info_2_p->banks[i].count <<
1113 (info_2_p->banks[i].size_exp -
1114 info_2_p->banks[i].erase_size_exp);
1115 }
1116 cros_ec_priv->ideal_write_size = info_2_p->write_ideal_size;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001117#if 0
1118 /*
1119 * TODO(b/38506987)Comment out, as some firmware were not
1120 * setting this flag properly.
1121 */
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001122 if (info_2_p->flags & EC_FLASH_INFO_ERASE_TO_0)
Alan Greendbeec2b2019-09-16 14:36:52 +10001123 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
Gwendal Grignou7f31f632017-05-22 16:30:19 -07001124#endif
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001125 free(info_2_p);
1126 }
Vadim Bendeburyadbd7062018-06-19 21:36:45 -07001127 eraser->block_erase = cros_ec_block_erase;
David Hendricks194b3bb2013-07-16 14:32:26 -07001128 /*
1129 * Some STM32 variants erase bits to 0. For now, assume that this
1130 * applies to STM32L parts.
1131 *
1132 * FIXME: This info will eventually be exposed via some EC command.
1133 * See chrome-os-partner:20973.
1134 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001135 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001136 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001137 if (rc < 0) {
1138 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1139 return 0;
1140 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001141 if (!strncmp(chip_info.name, "stm32l1", 7))
Alan Greendbeec2b2019-09-16 14:36:52 +10001142 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001143
Gwendal Grignoua36ff502015-03-23 16:36:47 -07001144
David Hendricksf9461c72013-07-11 19:02:13 -07001145
David Hendricksa672b042016-09-19 12:37:36 -07001146 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1147 0, NULL, 0, &spi_info, sizeof(spi_info));
1148 if (rc < 0) {
1149 static char chip_vendor[32];
1150 static char chip_name[32];
1151
1152 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1153 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001154 flash->chip->vendor = chip_vendor;
1155 flash->chip->name = chip_name;
Alan Greenb2fe0472019-07-30 14:33:28 +10001156 flash->chip->tested = TEST_OK_PREW;
David Hendricksa672b042016-09-19 12:37:36 -07001157 } else {
1158 const struct flashchip *f;
1159 uint32_t mfg = spi_info.jedec[0];
1160 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1161
1162 for (f = flashchips; f && f->name; f++) {
1163 if (f->bustype != BUS_SPI)
1164 continue;
1165 if ((f->manufacture_id == mfg) &&
1166 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001167 flash->chip->vendor = f->vendor;
1168 flash->chip->name = f->name;
1169 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001170 break;
1171 }
1172 }
1173 }
1174
Simon Glassc453a642013-07-01 18:08:53 +09001175 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1176 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001177 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1178 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001179 if (rc) {
1180 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1181 __func__, rc);
1182 return 0;
1183 }
1184
Souvik Ghosh586968a2016-08-11 17:56:24 -07001185 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1186 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001187 if (rc) {
1188 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1189 __func__, rc);
1190 return 0;
1191 }
1192
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001193 return 1;
1194};