blob: e330de522b85abc8112f2c13aa06de46a22328c9 [file] [log] [blame]
David Hendricksee712472012-05-23 21:50:59 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2012 The Chromium OS Authors. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Google or the names of contributors or
18 * licensors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind.
22 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
25 * GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE
26 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
27 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28 * GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
29 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
30 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
31 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
32 * EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 */
David Hendricks14935fe2014-08-14 17:38:24 -070034#include <errno.h>
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080040#include "fmap.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070041#include "cros_ec.h"
42#include "cros_ec_lock.h"
43#include "cros_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080044#include "programmer.h"
45#include "spi.h"
46#include "writeprotect.h"
47
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080048/* FIXME: used for wp hacks */
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <unistd.h>
Souvik Ghosh586968a2016-08-11 17:56:24 -070053
54struct cros_ec_priv *cros_ec_priv;
David Hendricks393deec2016-11-23 16:15:05 -080055static int ignore_wp_range_command = 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -070056
David Hendricksb64b39a2016-10-11 13:48:06 -070057static int set_wp(int enable); /* FIXME: move set_wp() */
58
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080059struct wp_data {
60 int enable;
61 unsigned int start;
62 unsigned int len;
63};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080064#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
65
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080066/* If software sync is enabled, then we don't try the latest firmware copy
67 * after updating.
68 */
69#define SOFTWARE_SYNC_ENABLED
70
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080071/* 1 if we want the flashrom to call erase_and_write_flash() again. */
72static int need_2nd_pass = 0;
73
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080074/* 1 if we want the flashrom to try jumping to new firmware after update. */
75static int try_latest_firmware = 0;
76
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +080077/* 1 if EC firmware has RWSIG enabled. */
78static int rwsig_enabled = 0;
79
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080080/* The range of each firmware copy from the image file to update.
81 * But re-define the .flags as the valid flag to indicate the firmware is
82 * new or not (if flags = 1).
83 */
84static struct fmap_area fwcopy[4]; // [0] is not used.
85
86/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080087static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070088 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
89 "EC_RO",
90 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080091};
92
Gwendal Grignou94e87d62014-11-25 15:34:15 -080093/*
94 * The names of the different device that can be found in a machine.
95 * Order is important: for backward compatibilty issue,
96 * 'ec' must be 0, 'pd' must be 1.
97 */
98static const char *ec_type[] = {
99 [0] = "ec",
100 [1] = "pd",
101 [2] = "sh",
Vincent Palatin4faff9a2017-03-17 17:27:39 +0100102 [3] = "fp",
Wei-Ning Huang78397842017-05-05 21:45:47 +0800103 [4] = "tp",
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800104};
105
Simon Glassc453a642013-07-01 18:08:53 +0900106/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
107 * the number of regions */
108static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800109
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800110/*
111 * Delay after reboot before EC can respond to host command.
112 * This value should be large enough for EC to initialize, but no larger than
113 * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be
114 * able to abort RWSIG jump and stay in RO.
115 */
116#define EC_INIT_DELAY 800000
117
118/*
119 * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW.
120 */
121#define EC_RWSIG_JUMP_TO_RW_DELAY 3000000
122
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800123/* Given the range not able to update, mark the corresponding
124 * firmware as old.
125 */
David Hendricksb907de32014-08-11 16:47:09 -0700126static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800127{
128 int i;
129
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800130 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800131 struct fmap_area *fw = &fwcopy[i];
132 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
133 (fw->offset >= addr && (fw->offset < addr + len))) {
134 msg_pdbg("Mark firmware [%s] as old.\n",
135 sections[i]);
136 fw->flags = 0; // mark as old
137 }
138 }
139}
140
141
Souvik Ghosh586968a2016-08-11 17:56:24 -0700142static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900143{
144 struct ec_response_get_version resp;
145 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700146
Souvik Ghosh586968a2016-08-11 17:56:24 -0700147 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700148 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900149 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700150 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900151 return rc;
152 }
153 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700154 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900155 return -1;
156 }
157
158 return resp.current_image;
159}
160
161
Souvik Ghosh586968a2016-08-11 17:56:24 -0700162static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900163 struct ec_response_flash_region_info *info)
164{
165 struct ec_params_flash_region_info req;
166 struct ec_response_flash_region_info resp;
167 int rc;
168
169 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700170 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900171 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
172 &resp, sizeof(resp));
173 if (rc < 0) {
174 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
175 return rc;
176 }
177
178 info->offset = resp.offset;
179 info->size = resp.size;
180 return 0;
181}
182
David Hendricksf9461c72013-07-11 19:02:13 -0700183/**
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800184 * Check if a feature is supported by EC.
185 *
186 * @param feature feature code
187 * @return < 0 if error, 0 not supported, > 0 supported
188 */
189static int ec_check_features(int feature)
190{
191 struct ec_response_get_features r;
192 int rc;
193
194 if (feature < 0 || feature >= sizeof(r.flags) * 8)
195 return -1;
196
197 rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES,
198 0, NULL, 0, &r, sizeof(r));
199 if (rc < 0)
200 return rc;
201
202 return !!(r.flags[feature / 32] & EC_FEATURE_MASK_0(feature));
203}
204
205/**
206 * Disable EC rwsig jump.
207 *
208 * @return 0 if success, <0 if error
209 */
210static int ec_rwsig_abort()
211{
212 struct ec_params_rwsig_action p;
213
214 p.action = RWSIG_ACTION_ABORT;
215 return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION,
216 0, &p, sizeof(p), NULL, 0);
217}
218
219/**
David Hendricksf9461c72013-07-11 19:02:13 -0700220 * Get the versions of the command supported by the EC.
221 *
222 * @param cmd Command
223 * @param pmask Destination for version mask; will be set to 0 on
224 * error.
225 * @return 0 if success, <0 if error
226 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700227static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700228{
David Hendricksf9461c72013-07-11 19:02:13 -0700229 struct ec_params_get_cmd_versions pver;
230 struct ec_response_get_cmd_versions rver;
231 int rc;
232
233 *pmask = 0;
234
235 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700236 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700237 &pver, sizeof(pver), &rver, sizeof(rver));
238
239 if (rc < 0)
240 return rc;
241
242 *pmask = rver.version_mask;
243 return rc;
244}
245
246/**
247 * Return non-zero if the EC supports the command and version
248 *
249 * @param cmd Command to check
250 * @param ver Version to check
251 * @return non-zero if command version supported; 0 if not.
252 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700253static int ec_cmd_version_supported(int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700254{
255 uint32_t mask = 0;
256 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700257
David Hendricksac1d25c2016-08-09 17:00:58 -0700258 rc = ec_get_cmd_versions(cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700259 if (rc < 0)
260 return rc;
261
262 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
263}
264
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700265/* returns 0 if successful or <0 to indicate error */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700266static int set_ideal_write_size(void)
David Hendricksf9461c72013-07-11 19:02:13 -0700267{
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700268 int cmd_version, ret;
David Hendricksf9461c72013-07-11 19:02:13 -0700269
David Hendricksac1d25c2016-08-09 17:00:58 -0700270 cmd_version = ec_cmd_version_supported(EC_CMD_FLASH_WRITE,
David Hendricksfb405f12014-08-19 22:42:30 -0700271 EC_VER_FLASH_WRITE);
272 if (cmd_version < 0) {
273 msg_perr("Cannot determine write command version\n");
274 return cmd_version;
275 } else if (cmd_version == 0) {
276 struct ec_response_flash_info info;
David Hendricksf9461c72013-07-11 19:02:13 -0700277
Souvik Ghosh586968a2016-08-11 17:56:24 -0700278 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700279 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700280 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700281 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700282 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700283 }
284
Souvik Ghosh586968a2016-08-11 17:56:24 -0700285 cros_ec_priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE;
David Hendricksfb405f12014-08-19 22:42:30 -0700286 } else {
287 struct ec_response_flash_info_1 info;
288
Souvik Ghosh586968a2016-08-11 17:56:24 -0700289 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700290 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700291 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700292 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700293 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700294 }
295
Souvik Ghosh586968a2016-08-11 17:56:24 -0700296 cros_ec_priv->ideal_write_size = info.write_ideal_size;
David Hendricksf9461c72013-07-11 19:02:13 -0700297 }
298
David Hendricksfb405f12014-08-19 22:42:30 -0700299 return 0;
David Hendricksf9461c72013-07-11 19:02:13 -0700300}
Simon Glass3c01dca2013-07-01 18:07:34 +0900301
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800302/* Perform a cold reboot.
303 *
304 * @param flags flags to pass to EC_CMD_REBOOT_EC.
305 * @return 0 for success, < 0 for command failure.
306 */
307static int cros_ec_cold_reboot(int flags) {
308 struct ec_params_reboot_ec p;
309
310 memset(&p, 0, sizeof(p));
311 p.cmd = EC_REBOOT_COLD;
312 p.flags = flags;
313 return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p),
314 NULL, 0);
315}
316
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800317/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800318 * then this functions picks a NEW firmware copy and jumps to it. Note that
319 * RO is preferred, then A, finally B.
320 *
321 * Returns 0 for success.
322 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700323static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800324 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800325 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700326 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800327
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800328 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
329 * jump to different firmware copy. The second EC_RES_SUCCESS would
330 * set the OBF=1 and the next command cannot be executed.
331 * Thus, we call EC to jump only if the target is different.
332 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700333 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700334 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800335 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700336 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900337 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800338
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800339 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900340
341 /* Translate target --> EC reboot command parameter */
342 switch (target) {
343 case EC_IMAGE_RO:
344 p.cmd = EC_REBOOT_JUMP_RO;
345 break;
346 case EC_IMAGE_RW:
347 p.cmd = EC_REBOOT_JUMP_RW;
348 break;
349 default:
350 /*
351 * If target is unspecified, set EC reboot command to use
352 * a new image. Also set "target" so that it may be used
353 * to update the priv->current_image if jump is successful.
354 */
355 if (fwcopy[EC_IMAGE_RO].flags) {
356 p.cmd = EC_REBOOT_JUMP_RO;
357 target = EC_IMAGE_RO;
358 } else if (fwcopy[EC_IMAGE_RW].flags) {
359 p.cmd = EC_REBOOT_JUMP_RW;
360 target = EC_IMAGE_RW;
361 } else {
362 p.cmd = EC_IMAGE_UNKNOWN;
363 }
364 break;
365 }
366
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800367 /*
368 * Do a cold reset instead of JUMP_RO so board enabling
369 * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag.
370 * This is true for EC enabling RWSIG, where
371 * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW.
372 */
373 if (target == EC_IMAGE_RO && rwsig_enabled) {
374 p.cmd = EC_REBOOT_COLD;
375 msg_pdbg("RWSIG enabled: doing a cold reboot instead of "
376 "JUMP_RO.\n");
377 }
378
379 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800380 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
381
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700382 if (current_image == p.cmd) {
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800383 msg_pdbg("CROS_EC is already in [%s]\n", sections[target]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700384 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800385 return 0;
386 }
387
Souvik Ghosh586968a2016-08-11 17:56:24 -0700388 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700389 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800390 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700391 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800392 sections[target], rc);
393 return rc;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800394 }
395
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800396 /* Sleep until EC can respond to host command, but just before
397 * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */
398 usleep(EC_INIT_DELAY);
399
400 /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */
401 if (target == EC_IMAGE_RO && rwsig_enabled) {
402 msg_pdbg("RWSIG enabled: aborting RWSIG jump.\n");
403 ec_rwsig_abort();
404 }
405
406 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[target]);
407 rc = EC_RES_SUCCESS;
408 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800409
David Hendricksf9461c72013-07-11 19:02:13 -0700410 /* update max data write size in case we're jumping to an EC
411 * firmware with different protocol */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700412 set_ideal_write_size();
David Hendricksf9461c72013-07-11 19:02:13 -0700413
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800414 return rc;
415}
416
David Hendricksb64b39a2016-10-11 13:48:06 -0700417static int cros_ec_restore_wp(void *data)
418{
419 msg_pdbg("Restoring EC soft WP.\n");
420 return set_wp(1);
421}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800422
David Hendricksb64b39a2016-10-11 13:48:06 -0700423static int cros_ec_wp_is_enabled(void)
424{
425 struct ec_params_flash_protect p;
426 struct ec_response_flash_protect r;
427 int rc;
428
429 memset(&p, 0, sizeof(p));
430 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
431 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
432 if (rc < 0) {
433 msg_perr("FAILED: Cannot get the write protection status: %d\n",
434 rc);
435 return -1;
436 } else if (rc < sizeof(r)) {
437 msg_perr("FAILED: Too little data returned (expected:%zd, "
438 "actual:%d)\n", sizeof(r), rc);
439 return -1;
440 }
441
442 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
443 return 1;
444
445 return 0;
446}
447
448/*
449 * Prepare EC for update:
450 * - Disable soft WP if needed.
451 * - Parse flashmap.
452 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800453 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700454int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800455 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700456 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800457
Souvik Ghosh586968a2016-08-11 17:56:24 -0700458 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800459
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800460 if (ec_check_features(EC_FEATURE_RWSIG) > 0) {
461 rwsig_enabled = 1;
462 msg_pdbg("EC has RWSIG enabled.\n");
463 }
464
David Hendricksb64b39a2016-10-11 13:48:06 -0700465 /*
466 * If HW WP is disabled we may still need to disable write protection
467 * that is active on the EC. Otherwise the EC can reject erase/write
468 * commands.
469 *
470 * Failure is OK since HW WP might be enabled or the EC needs to be
471 * rebooted for the change to take effect. We can still update RW
472 * portions.
473 *
474 * If disabled here, EC WP will be restored at the end so that
475 * "--wp-enable" does not need to be run later. This greatly
476 * simplifies logic for developers and scripts.
477 */
478 wp_status = cros_ec_wp_is_enabled();
479 if (wp_status < 0) {
480 return 1;
481 } else if (wp_status == 1) {
482 msg_pdbg("Attempting to disable EC soft WP.\n");
483 if (!set_wp(0)) {
484 msg_pdbg("EC soft WP disabled successfully.\n");
485 if (register_shutdown(cros_ec_restore_wp, NULL))
486 return 1;
487 } else {
488 msg_pdbg("Failed. Hardware WP might in effect or EC "
489 "needs to be rebooted first.\n");
490 }
491 } else {
492 msg_pdbg("EC soft WP is already disabled.\n");
493 }
494
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800495 // Parse the fmap in the image file and cache the firmware ranges.
496 fmap = fmap_find_in_memory(image, size);
497 if (!fmap) return 0;
498
499 // Lookup RO/A/B sections in FMAP.
500 for (i = 0; i < fmap->nareas; i++) {
501 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800502 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700503 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800504 msg_pdbg("Found '%s' in image.\n", fa->name);
505 memcpy(&fwcopy[j], fa, sizeof(*fa));
506 fwcopy[j].flags = 1; // mark as new
507 }
508 }
509 }
510
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800511 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700512 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800513 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700514 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800515}
516
517
518/* Returns >0 if we need 2nd pass of erase_and_write_flash().
519 * <0 if we cannot jump to any firmware copy.
520 * ==0 if no more pass is needed.
521 *
522 * This function also jumps to new-updated firmware copy before return >0.
523 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700524int cros_ec_need_2nd_pass(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700525 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800526
527 if (need_2nd_pass) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700528 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800529 return -1;
530 }
531 }
532
533 return need_2nd_pass;
534}
535
536
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800537/* Returns 0 for success.
538 *
539 * Try latest firmware: B > A > RO
540 *
David Hendricksb907de32014-08-11 16:47:09 -0700541 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800542 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700543 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800544 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700545int cros_ec_finish(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700546 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800547
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800548 /* For EC with RWSIG enabled. We need a cold reboot to enable
549 * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed.
550 */
551 if (rwsig_enabled) {
552 int rc;
553
554 msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n");
555 rc = cros_ec_cold_reboot(0);
556 usleep(EC_RWSIG_JUMP_TO_RW_DELAY);
557 return rc;
558 }
559
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800560 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800561 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700562 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
563 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800564 }
565
566 return 0;
567}
568
569
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700570int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800571 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800572 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800573 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700574 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800575 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700576 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800577
David Hendricks133083b2012-07-17 20:39:38 -0700578 while (offset < readcnt) {
579 count = min(maxlen, readcnt - offset);
580 p.offset = blockaddr + offset;
581 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700582 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700583 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800584 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700585 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700586 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800587 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800588 } else {
589 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800590 }
591
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800592 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700593 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800594 }
595
596 return rc;
597}
598
599
Simon Glassc453a642013-07-01 18:08:53 +0900600/*
601 * returns 0 to indicate area does not overlap current EC image
602 * returns 1 to indicate area overlaps current EC image or error
603 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700604static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900605{
Simon Glassc453a642013-07-01 18:08:53 +0900606 enum ec_current_image image;
607 uint32_t region_offset;
608 uint32_t region_size;
609
Souvik Ghosh586968a2016-08-11 17:56:24 -0700610 image = cros_ec_priv->current_image;
611 region_offset = cros_ec_priv->region[image].offset;
612 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900613
614 if ((addr + len - 1 < region_offset) ||
615 (addr > region_offset + region_size - 1)) {
616 return 0;
617 }
618 return 1;
619}
620
621
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700622int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800623 unsigned int blockaddr,
624 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800625 struct ec_params_flash_erase erase;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800626 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800627
Souvik Ghosh586968a2016-08-11 17:56:24 -0700628 if (in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700629 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900630 need_2nd_pass = 1;
631 return ACCESS_DENIED;
632 }
633
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800634 erase.offset = blockaddr;
635 erase.size = len;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700636 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE,
David Hendricks14935fe2014-08-14 17:38:24 -0700637 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800638 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800639 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700640 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800641 need_2nd_pass = 1;
642 return ACCESS_DENIED;
643 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800644 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700645 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800646 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800647 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800648 } else {
649 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800650 }
651
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800652#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800653 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800654#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800655 return rc;
656}
657
658
Patrick Georgiab8353e2017-02-03 18:32:01 +0100659int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800660 unsigned int nbytes) {
661 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800662 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800663 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700664 uint8_t *packet;
665
Ken Chang69c31b82014-10-28 15:17:21 +0800666 /*
667 * For chrome-os-partner:33035, to workaround the undersized
668 * outdata buffer issue in kernel.
669 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700670 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700671 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800672 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700673 if (!packet)
674 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800675
676 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800677 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800678 p.offset = addr + i;
679 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900680
Souvik Ghosh586968a2016-08-11 17:56:24 -0700681 if (in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700682 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900683 need_2nd_pass = 1;
684 return ACCESS_DENIED;
685 }
686
David Hendricks2d6db772013-07-10 21:07:48 -0700687 memcpy(packet, &p, sizeof(p));
688 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700689 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700690 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700691
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800692 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800693 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700694 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800695 need_2nd_pass = 1;
696 return ACCESS_DENIED;
697 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800698
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800699 if (rc < 0) break;
700 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800701 }
702
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800703#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800704 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800705#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700706 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800707 return rc;
708}
709
710
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700711static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900712 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800713 int rc;
714
Souvik Ghosh586968a2016-08-11 17:56:24 -0700715 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800716 if (rc < 0) {
717 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
718 return 1;
719 }
720
721 msg_pinfo("Supported write protect range:\n");
722 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900723 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
724 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800725
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800726 return 0;
727}
728
729
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800730/*
731 * Helper function for flash protection.
732 *
733 * On EC API v1, the EC write protection has been simplified to one-bit:
734 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
735 * or disabled. However, this is different from the SPI-style write protect
736 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
737 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
738 *
739 * SRP Range | PROTECT_RO_AT_BOOT
740 * 0 0 | 0
741 * 0 non-zero | 1
742 * 1 0 | 1
743 * 1 non-zero | 1
744 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800745 *
746 * Besides, to make the protection take effect as soon as possible, we
747 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
748 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800749 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700750static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800751 struct ec_params_flash_protect p;
752 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800753 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
754 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
755 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800756 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800757
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800758 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800759 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800760 p.mask = (ro_at_boot_flag | ro_now_flag);
761 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700762 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700763 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800764 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800765 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
766 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800767 return 1;
768 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800769
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800770 /* Read back */
771 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700772 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700773 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800774 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800775 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
776 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800777 return 1;
778 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800779
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800780 if (!enable) {
781 /* The disable case is easier to check. */
782 if (r.flags & ro_at_boot_flag) {
783 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
784 return 1;
785 } else if (r.flags & ro_now_flag) {
786 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
787 need_an_ec_cold_reset = 1;
788 goto exit;
789 }
790
791 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
792 return 0;
793 }
794
795 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
796 if (r.flags & ro_at_boot_flag) {
797 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
798 } else {
799 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
800 return 1;
801 }
802
803 /* Then, we check if the protection has been activated. */
804 if (r.flags & ro_now_flag) {
805 /* Good, RO_NOW is set. */
806 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
807 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800808 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
809
810 memset(&p, 0, sizeof(p));
811 p.mask = EC_FLASH_PROTECT_ALL_NOW;
812 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700813 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800814 EC_VER_FLASH_PROTECT,
815 &p, sizeof(p), &r, sizeof(r));
816 if (rc < 0) {
817 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
818 return 1;
819 }
820
821 /* Read back */
822 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700823 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800824 EC_VER_FLASH_PROTECT,
825 &p, sizeof(p), &r, sizeof(r));
826 if (rc < 0) {
827 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
828 return 1;
829 }
830
831 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
832 msg_perr("FAILED: ALL_NOW is not set.\n");
833 need_an_ec_cold_reset = 1;
834 goto exit;
835 }
836
837 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
838
839 /*
840 * Our goal is to protect the RO ASAP. The entire protection
841 * is just a workaround for platform not supporting RO_NOW.
842 * It has side-effect that the RW is also protected and leads
843 * the RW update failed. So, we arrange an EC code reset to
844 * unlock RW ASAP.
845 */
Wei-Ning Huang70ebbd42017-05-05 21:50:41 +0800846 rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800847 if (rc < 0) {
848 msg_perr("WARN: Cannot arrange a cold reset at next "
849 "shutdown to unlock entire protect.\n");
850 msg_perr(" But you can do it manually.\n");
851 } else {
852 msg_pdbg("INFO: A cold reset is arranged at next "
853 "shutdown.\n");
854 }
855
856 } else {
857 msg_perr("FAILED: RO_NOW is not set.\n");
858 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
859 "make write protection active now.\n");
860 need_an_ec_cold_reset = 1;
861 }
862
863exit:
864 if (need_an_ec_cold_reset) {
865 msg_perr("FAILED: You may need a reboot to take effect of "
866 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800867 return 1;
868 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800869
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800870 return 0;
871}
872
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700873static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800874 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900875 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800876 int rc;
877
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800878 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700879 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800880 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800881 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800882 return 1;
883 }
884 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900885 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800886 /* pass */
887 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800888 msg_perr("FAILED: Unsupported write protection range "
889 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800890 msg_perr("Currently supported range:\n");
891 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900892 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
893 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800894 return 1;
895 }
896
David Hendricks393deec2016-11-23 16:15:05 -0800897 if (ignore_wp_range_command)
898 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700899 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800900}
901
902
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700903static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700904 enum wp_mode wp_mode) {
905 int ret;
906
907 switch (wp_mode) {
908 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700909 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700910 break;
911 default:
912 msg_perr("%s():%d Unsupported write-protection mode\n",
913 __func__, __LINE__);
914 ret = 1;
915 break;
916 }
917
918 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800919}
920
921
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700922static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800923 /* --wp-range implicitly enables write protection on CrOS EC, so force
924 it not to if --wp-disable is what the user really wants. */
925 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700926 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800927}
928
929
Souvik Ghosh586968a2016-08-11 17:56:24 -0700930static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800931 struct ec_params_flash_protect p;
932 struct ec_response_flash_protect r;
933 int start, len; /* wp range */
934 int enabled;
935 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800936
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800937 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700938 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700939 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800940 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800941 msg_perr("FAILED: Cannot get the write protection status: %d\n",
942 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800943 return 1;
944 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700945 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800946 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800947 return 1;
948 }
949
950 start = len = 0;
951 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900952 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800953
954 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
955 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700956 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800957 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800958 msg_perr("FAILED: Cannot get the WP_RO region info: "
959 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800960 return 1;
961 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900962 start = info.offset;
963 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800964 } else {
965 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
966 __func__);
967 }
968
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800969 /*
970 * If neither RO_NOW or ALL_NOW is set, it means write protect is
971 * NOT active now.
972 */
973 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
974 start = len = 0;
975
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800976 /* Remove the SPI-style messages. */
977 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
978 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
979 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800980 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800981 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800982 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800983 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800984
985 return 0;
986}
987
David Hendrickse5454932013-11-04 18:16:11 -0800988/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700989int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800990{
991 struct ec_params_hello request;
992 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -0800993 int rc = 0;
994
995 /* Say hello to EC. */
996 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
997 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
998 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800999 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -08001000 sizeof(request), &response, sizeof(response));
1001 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
1002
1003 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
1004 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
1005 "rc=%d, request=0x%x response=0x%x\n",
1006 rc, request.in_data, response.out_data);
1007 return 1;
1008 }
1009
1010 return 0;
1011}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001012
David Hendricksd13d90d2016-08-09 17:00:52 -07001013void cros_ec_set_max_size(struct cros_ec_priv *priv,
1014 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001015 struct ec_response_get_protocol_info info;
1016 int rc = 0;
1017 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001018 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -07001019 &info, sizeof(info));
1020 msg_pdbg("%s: rc:%d\n", __func__, rc);
1021
1022 if (rc == sizeof(info)) {
1023 op->max_data_write = min(op->max_data_write,
1024 info.max_request_packet_size -
1025 sizeof(struct ec_host_request));
1026 op->max_data_read = min(op->max_data_read,
1027 info.max_response_packet_size -
1028 sizeof(struct ec_host_response));
1029 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
1030 op->max_data_write, op->max_data_read);
1031 }
1032}
1033
David Hendricks14935fe2014-08-14 17:38:24 -07001034
1035/*
David Hendricks052446b2014-09-11 11:26:51 -07001036 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -07001037 *
1038 * This function parses programmer parameters from the command line. Since
1039 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
1040 * run during internal programmer initialization.
1041 */
1042int cros_ec_parse_param(struct cros_ec_priv *priv)
1043{
David Hendricks98b3c572016-11-30 01:50:08 +00001044 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -07001045
David Hendricks98b3c572016-11-30 01:50:08 +00001046 p = extract_programmer_param("dev");
1047 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -07001048 unsigned int index;
1049 char *endptr = NULL;
1050
1051 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001052 /*
1053 * For backward compatibility, check if the index is
1054 * a number: 0: main EC, 1: PD
1055 * works only on Samus.
1056 */
David Hendricks98b3c572016-11-30 01:50:08 +00001057 index = strtoul(p, &endptr, 10);
1058 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
1059 msg_perr("Invalid argument: \"%s\"\n", p);
1060 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001061 }
1062
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001063 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -07001064 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001065 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -07001066 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001067 priv->dev = ec_type[index];
1068 msg_pdbg("Target %s used\n", priv->dev);
1069 }
David Hendricks14935fe2014-08-14 17:38:24 -07001070
David Hendricks98b3c572016-11-30 01:50:08 +00001071 p = extract_programmer_param("type");
1072 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001073 unsigned int index;
1074 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +00001075 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001076 break;
1077 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +00001078 msg_perr("Invalid argument: \"%s\"\n", p);
1079 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001080 }
1081 priv->dev = ec_type[index];
1082 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -07001083 }
1084
David Hendricks98b3c572016-11-30 01:50:08 +00001085 p = extract_programmer_param("block");
1086 if (p) {
1087 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -07001088 char *endptr = NULL;
1089
1090 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +00001091 block = strtoul(p, &endptr, 0);
1092 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
1093 msg_perr("Invalid argument: \"%s\"\n", p);
1094 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001095 }
1096
David Hendricks98b3c572016-11-30 01:50:08 +00001097 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -07001098 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001099 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001100 }
1101
David Hendricks98b3c572016-11-30 01:50:08 +00001102 msg_pdbg("Override block size to 0x%x\n", block);
1103 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001104 }
1105
David Hendricks98b3c572016-11-30 01:50:08 +00001106 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001107}
1108
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001109int cros_ec_probe_size(struct flashctx *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001110 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +08001111 struct ec_response_flash_info info;
David Hendricksa672b042016-09-19 12:37:36 -07001112 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001113 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001114 struct block_eraser *eraser;
1115 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001116 .list_ranges = cros_ec_list_ranges,
1117 .set_range = cros_ec_set_range,
1118 .enable = cros_ec_enable_writeprotect,
1119 .disable = cros_ec_disable_writeprotect,
1120 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001121 };
1122
Souvik Ghosh586968a2016-08-11 17:56:24 -07001123 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001124 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +08001125 if (rc < 0) {
1126 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
1127 return 0;
1128 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001129 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001130 if (rc < 0) {
1131 msg_perr("%s(): Failed to probe (no current image): %d\n",
1132 __func__, rc);
1133 return 0;
1134 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001135 cros_ec_priv->current_image = rc;
1136 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001137
Patrick Georgif3fa2992017-02-02 16:24:44 +01001138 flash->chip->total_size = info.flash_size / 1024;
1139 flash->chip->page_size = opaque_programmer->max_data_read;
1140 eraser = &flash->chip->block_erasers[0];
Duncan Laurie84328722014-09-10 23:25:01 -07001141
1142 /* Allow overriding the erase block size in case EC is incorrect */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001143 if (cros_ec_priv->erase_block_size > 0)
1144 eraser->eraseblocks[0].size = cros_ec_priv->erase_block_size;
Duncan Laurie84328722014-09-10 23:25:01 -07001145 else
1146 eraser->eraseblocks[0].size = info.erase_block_size;
1147
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001148 eraser->eraseblocks[0].count = info.flash_size /
1149 eraser->eraseblocks[0].size;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001150 flash->chip->wp = &wp;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001151
David Hendricks194b3bb2013-07-16 14:32:26 -07001152 /*
1153 * Some STM32 variants erase bits to 0. For now, assume that this
1154 * applies to STM32L parts.
1155 *
1156 * FIXME: This info will eventually be exposed via some EC command.
1157 * See chrome-os-partner:20973.
1158 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001159 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001160 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001161 if (rc < 0) {
1162 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1163 return 0;
1164 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001165 if (!strncmp(chip_info.name, "stm32l1", 7))
Patrick Georgif3fa2992017-02-02 16:24:44 +01001166 flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO;
David Hendricks194b3bb2013-07-16 14:32:26 -07001167
Souvik Ghosh586968a2016-08-11 17:56:24 -07001168 rc = set_ideal_write_size();
David Hendricksfbd5e6d2014-08-21 15:01:43 -07001169 if (rc < 0) {
1170 msg_perr("%s(): Unable to set write size\n", __func__);
1171 return 0;
1172 }
David Hendricksf9461c72013-07-11 19:02:13 -07001173
David Hendricksa672b042016-09-19 12:37:36 -07001174 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1175 0, NULL, 0, &spi_info, sizeof(spi_info));
1176 if (rc < 0) {
1177 static char chip_vendor[32];
1178 static char chip_name[32];
1179
1180 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1181 memcpy(chip_name, chip_info.name, sizeof(chip_name));
Patrick Georgif3fa2992017-02-02 16:24:44 +01001182 flash->chip->vendor = chip_vendor;
1183 flash->chip->name = chip_name;
1184 flash->chip->tested = TEST_OK_PREWU;
David Hendricksa672b042016-09-19 12:37:36 -07001185 } else {
1186 const struct flashchip *f;
1187 uint32_t mfg = spi_info.jedec[0];
1188 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1189
1190 for (f = flashchips; f && f->name; f++) {
1191 if (f->bustype != BUS_SPI)
1192 continue;
1193 if ((f->manufacture_id == mfg) &&
1194 f->model_id == model) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001195 flash->chip->vendor = f->vendor;
1196 flash->chip->name = f->name;
1197 flash->chip->tested = f->tested;
David Hendricksa672b042016-09-19 12:37:36 -07001198 break;
1199 }
1200 }
1201 }
1202
Simon Glassc453a642013-07-01 18:08:53 +09001203 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1204 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001205 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1206 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001207 if (rc) {
1208 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1209 __func__, rc);
1210 return 0;
1211 }
1212
Souvik Ghosh586968a2016-08-11 17:56:24 -07001213 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1214 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001215 if (rc) {
1216 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1217 __func__, rc);
1218 return 0;
1219 }
1220
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001221 return 1;
1222};