blob: 579e68e0efbae15f0d1570c2c118b94bb5e09ed0 [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
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080077/* The range of each firmware copy from the image file to update.
78 * But re-define the .flags as the valid flag to indicate the firmware is
79 * new or not (if flags = 1).
80 */
81static struct fmap_area fwcopy[4]; // [0] is not used.
82
83/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080084static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070085 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
86 "EC_RO",
87 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080088};
89
Gwendal Grignou94e87d62014-11-25 15:34:15 -080090/*
91 * The names of the different device that can be found in a machine.
92 * Order is important: for backward compatibilty issue,
93 * 'ec' must be 0, 'pd' must be 1.
94 */
95static const char *ec_type[] = {
96 [0] = "ec",
97 [1] = "pd",
98 [2] = "sh",
Vincent Palatin4faff9a2017-03-17 17:27:39 +010099 [3] = "fp",
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800100};
101
Simon Glassc453a642013-07-01 18:08:53 +0900102/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
103 * the number of regions */
104static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800105
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800106/* Given the range not able to update, mark the corresponding
107 * firmware as old.
108 */
David Hendricksb907de32014-08-11 16:47:09 -0700109static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800110{
111 int i;
112
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800113 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800114 struct fmap_area *fw = &fwcopy[i];
115 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
116 (fw->offset >= addr && (fw->offset < addr + len))) {
117 msg_pdbg("Mark firmware [%s] as old.\n",
118 sections[i]);
119 fw->flags = 0; // mark as old
120 }
121 }
122}
123
124
Souvik Ghosh586968a2016-08-11 17:56:24 -0700125static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900126{
127 struct ec_response_get_version resp;
128 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700129
Souvik Ghosh586968a2016-08-11 17:56:24 -0700130 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700131 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900132 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700133 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900134 return rc;
135 }
136 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700137 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900138 return -1;
139 }
140
141 return resp.current_image;
142}
143
144
Souvik Ghosh586968a2016-08-11 17:56:24 -0700145static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900146 struct ec_response_flash_region_info *info)
147{
148 struct ec_params_flash_region_info req;
149 struct ec_response_flash_region_info resp;
150 int rc;
151
152 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700153 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900154 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
155 &resp, sizeof(resp));
156 if (rc < 0) {
157 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
158 return rc;
159 }
160
161 info->offset = resp.offset;
162 info->size = resp.size;
163 return 0;
164}
165
David Hendricksf9461c72013-07-11 19:02:13 -0700166/**
167 * Get the versions of the command supported by the EC.
168 *
169 * @param cmd Command
170 * @param pmask Destination for version mask; will be set to 0 on
171 * error.
172 * @return 0 if success, <0 if error
173 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700174static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700175{
David Hendricksf9461c72013-07-11 19:02:13 -0700176 struct ec_params_get_cmd_versions pver;
177 struct ec_response_get_cmd_versions rver;
178 int rc;
179
180 *pmask = 0;
181
182 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700183 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700184 &pver, sizeof(pver), &rver, sizeof(rver));
185
186 if (rc < 0)
187 return rc;
188
189 *pmask = rver.version_mask;
190 return rc;
191}
192
193/**
194 * Return non-zero if the EC supports the command and version
195 *
196 * @param cmd Command to check
197 * @param ver Version to check
198 * @return non-zero if command version supported; 0 if not.
199 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700200static int ec_cmd_version_supported(int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700201{
202 uint32_t mask = 0;
203 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700204
David Hendricksac1d25c2016-08-09 17:00:58 -0700205 rc = ec_get_cmd_versions(cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700206 if (rc < 0)
207 return rc;
208
209 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
210}
211
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700212/* returns 0 if successful or <0 to indicate error */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700213static int set_ideal_write_size(void)
David Hendricksf9461c72013-07-11 19:02:13 -0700214{
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700215 int cmd_version, ret;
David Hendricksf9461c72013-07-11 19:02:13 -0700216
David Hendricksac1d25c2016-08-09 17:00:58 -0700217 cmd_version = ec_cmd_version_supported(EC_CMD_FLASH_WRITE,
David Hendricksfb405f12014-08-19 22:42:30 -0700218 EC_VER_FLASH_WRITE);
219 if (cmd_version < 0) {
220 msg_perr("Cannot determine write command version\n");
221 return cmd_version;
222 } else if (cmd_version == 0) {
223 struct ec_response_flash_info info;
David Hendricksf9461c72013-07-11 19:02:13 -0700224
Souvik Ghosh586968a2016-08-11 17:56:24 -0700225 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700226 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700227 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700228 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700229 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700230 }
231
Souvik Ghosh586968a2016-08-11 17:56:24 -0700232 cros_ec_priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE;
David Hendricksfb405f12014-08-19 22:42:30 -0700233 } else {
234 struct ec_response_flash_info_1 info;
235
Souvik Ghosh586968a2016-08-11 17:56:24 -0700236 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700237 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700238 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700239 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700240 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700241 }
242
Souvik Ghosh586968a2016-08-11 17:56:24 -0700243 cros_ec_priv->ideal_write_size = info.write_ideal_size;
David Hendricksf9461c72013-07-11 19:02:13 -0700244 }
245
David Hendricksfb405f12014-08-19 22:42:30 -0700246 return 0;
David Hendricksf9461c72013-07-11 19:02:13 -0700247}
Simon Glass3c01dca2013-07-01 18:07:34 +0900248
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800249/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800250 * then this functions picks a NEW firmware copy and jumps to it. Note that
251 * RO is preferred, then A, finally B.
252 *
253 * Returns 0 for success.
254 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700255static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800256 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800257 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700258 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800259
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800260 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
261 * jump to different firmware copy. The second EC_RES_SUCCESS would
262 * set the OBF=1 and the next command cannot be executed.
263 * Thus, we call EC to jump only if the target is different.
264 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700265 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700266 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800267 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700268 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900269 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800270
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800271 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900272
273 /* Translate target --> EC reboot command parameter */
274 switch (target) {
275 case EC_IMAGE_RO:
276 p.cmd = EC_REBOOT_JUMP_RO;
277 break;
278 case EC_IMAGE_RW:
279 p.cmd = EC_REBOOT_JUMP_RW;
280 break;
281 default:
282 /*
283 * If target is unspecified, set EC reboot command to use
284 * a new image. Also set "target" so that it may be used
285 * to update the priv->current_image if jump is successful.
286 */
287 if (fwcopy[EC_IMAGE_RO].flags) {
288 p.cmd = EC_REBOOT_JUMP_RO;
289 target = EC_IMAGE_RO;
290 } else if (fwcopy[EC_IMAGE_RW].flags) {
291 p.cmd = EC_REBOOT_JUMP_RW;
292 target = EC_IMAGE_RW;
293 } else {
294 p.cmd = EC_IMAGE_UNKNOWN;
295 }
296 break;
297 }
298
David Hendricksb907de32014-08-11 16:47:09 -0700299 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800300 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
301
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700302 if (current_image == p.cmd) {
David Hendricksb907de32014-08-11 16:47:09 -0700303 msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700304 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800305 return 0;
306 }
307
Souvik Ghosh586968a2016-08-11 17:56:24 -0700308 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700309 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800310 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700311 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800312 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800313 } else {
David Hendricksb907de32014-08-11 16:47:09 -0700314 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800315 rc = EC_RES_SUCCESS;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700316 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800317 }
318
319 /* Sleep 1 sec to wait the EC re-init. */
320 usleep(1000000);
321
David Hendricksf9461c72013-07-11 19:02:13 -0700322 /* update max data write size in case we're jumping to an EC
323 * firmware with different protocol */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700324 set_ideal_write_size();
David Hendricksf9461c72013-07-11 19:02:13 -0700325
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800326 return rc;
327}
328
David Hendricksb64b39a2016-10-11 13:48:06 -0700329static int cros_ec_restore_wp(void *data)
330{
331 msg_pdbg("Restoring EC soft WP.\n");
332 return set_wp(1);
333}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800334
David Hendricksb64b39a2016-10-11 13:48:06 -0700335static int cros_ec_wp_is_enabled(void)
336{
337 struct ec_params_flash_protect p;
338 struct ec_response_flash_protect r;
339 int rc;
340
341 memset(&p, 0, sizeof(p));
342 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
343 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
344 if (rc < 0) {
345 msg_perr("FAILED: Cannot get the write protection status: %d\n",
346 rc);
347 return -1;
348 } else if (rc < sizeof(r)) {
349 msg_perr("FAILED: Too little data returned (expected:%zd, "
350 "actual:%d)\n", sizeof(r), rc);
351 return -1;
352 }
353
354 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
355 return 1;
356
357 return 0;
358}
359
360/*
361 * Prepare EC for update:
362 * - Disable soft WP if needed.
363 * - Parse flashmap.
364 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800365 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700366int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800367 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700368 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800369
Souvik Ghosh586968a2016-08-11 17:56:24 -0700370 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800371
David Hendricksb64b39a2016-10-11 13:48:06 -0700372 /*
373 * If HW WP is disabled we may still need to disable write protection
374 * that is active on the EC. Otherwise the EC can reject erase/write
375 * commands.
376 *
377 * Failure is OK since HW WP might be enabled or the EC needs to be
378 * rebooted for the change to take effect. We can still update RW
379 * portions.
380 *
381 * If disabled here, EC WP will be restored at the end so that
382 * "--wp-enable" does not need to be run later. This greatly
383 * simplifies logic for developers and scripts.
384 */
385 wp_status = cros_ec_wp_is_enabled();
386 if (wp_status < 0) {
387 return 1;
388 } else if (wp_status == 1) {
389 msg_pdbg("Attempting to disable EC soft WP.\n");
390 if (!set_wp(0)) {
391 msg_pdbg("EC soft WP disabled successfully.\n");
392 if (register_shutdown(cros_ec_restore_wp, NULL))
393 return 1;
394 } else {
395 msg_pdbg("Failed. Hardware WP might in effect or EC "
396 "needs to be rebooted first.\n");
397 }
398 } else {
399 msg_pdbg("EC soft WP is already disabled.\n");
400 }
401
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800402 // Parse the fmap in the image file and cache the firmware ranges.
403 fmap = fmap_find_in_memory(image, size);
404 if (!fmap) return 0;
405
406 // Lookup RO/A/B sections in FMAP.
407 for (i = 0; i < fmap->nareas; i++) {
408 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800409 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700410 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800411 msg_pdbg("Found '%s' in image.\n", fa->name);
412 memcpy(&fwcopy[j], fa, sizeof(*fa));
413 fwcopy[j].flags = 1; // mark as new
414 }
415 }
416 }
417
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800418 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700419 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800420 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700421 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800422}
423
424
425/* Returns >0 if we need 2nd pass of erase_and_write_flash().
426 * <0 if we cannot jump to any firmware copy.
427 * ==0 if no more pass is needed.
428 *
429 * This function also jumps to new-updated firmware copy before return >0.
430 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700431int cros_ec_need_2nd_pass(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700432 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800433
434 if (need_2nd_pass) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700435 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800436 return -1;
437 }
438 }
439
440 return need_2nd_pass;
441}
442
443
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800444/* Returns 0 for success.
445 *
446 * Try latest firmware: B > A > RO
447 *
David Hendricksb907de32014-08-11 16:47:09 -0700448 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800449 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700450 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800451 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700452int cros_ec_finish(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700453 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800454
455 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800456 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700457 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
458 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800459 }
460
461 return 0;
462}
463
464
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700465int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800466 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800467 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800468 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700469 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800470 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700471 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800472
David Hendricks133083b2012-07-17 20:39:38 -0700473 while (offset < readcnt) {
474 count = min(maxlen, readcnt - offset);
475 p.offset = blockaddr + offset;
476 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700477 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700478 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800479 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700480 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700481 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800482 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800483 } else {
484 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800485 }
486
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800487 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700488 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800489 }
490
491 return rc;
492}
493
494
Simon Glassc453a642013-07-01 18:08:53 +0900495/*
496 * returns 0 to indicate area does not overlap current EC image
497 * returns 1 to indicate area overlaps current EC image or error
498 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700499static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900500{
Simon Glassc453a642013-07-01 18:08:53 +0900501 enum ec_current_image image;
502 uint32_t region_offset;
503 uint32_t region_size;
504
Souvik Ghosh586968a2016-08-11 17:56:24 -0700505 image = cros_ec_priv->current_image;
506 region_offset = cros_ec_priv->region[image].offset;
507 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900508
509 if ((addr + len - 1 < region_offset) ||
510 (addr > region_offset + region_size - 1)) {
511 return 0;
512 }
513 return 1;
514}
515
516
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700517int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800518 unsigned int blockaddr,
519 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800520 struct ec_params_flash_erase erase;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800521 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800522
Souvik Ghosh586968a2016-08-11 17:56:24 -0700523 if (in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700524 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900525 need_2nd_pass = 1;
526 return ACCESS_DENIED;
527 }
528
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800529 erase.offset = blockaddr;
530 erase.size = len;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700531 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE,
David Hendricks14935fe2014-08-14 17:38:24 -0700532 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800533 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800534 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700535 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800536 need_2nd_pass = 1;
537 return ACCESS_DENIED;
538 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800539 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700540 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800541 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800542 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800543 } else {
544 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800545 }
546
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800547#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800548 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800549#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800550 return rc;
551}
552
553
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700554int cros_ec_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800555 unsigned int nbytes) {
556 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800557 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800558 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700559 uint8_t *packet;
560
Ken Chang69c31b82014-10-28 15:17:21 +0800561 /*
562 * For chrome-os-partner:33035, to workaround the undersized
563 * outdata buffer issue in kernel.
564 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700565 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700566 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800567 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700568 if (!packet)
569 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800570
571 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800572 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800573 p.offset = addr + i;
574 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900575
Souvik Ghosh586968a2016-08-11 17:56:24 -0700576 if (in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700577 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900578 need_2nd_pass = 1;
579 return ACCESS_DENIED;
580 }
581
David Hendricks2d6db772013-07-10 21:07:48 -0700582 memcpy(packet, &p, sizeof(p));
583 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700584 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700585 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700586
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800587 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800588 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700589 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800590 need_2nd_pass = 1;
591 return ACCESS_DENIED;
592 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800593
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800594 if (rc < 0) break;
595 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800596 }
597
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800598#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800599 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800600#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700601 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800602 return rc;
603}
604
605
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700606static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900607 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800608 int rc;
609
Souvik Ghosh586968a2016-08-11 17:56:24 -0700610 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800611 if (rc < 0) {
612 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
613 return 1;
614 }
615
616 msg_pinfo("Supported write protect range:\n");
617 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900618 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
619 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800620
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800621 return 0;
622}
623
624
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800625/*
626 * Helper function for flash protection.
627 *
628 * On EC API v1, the EC write protection has been simplified to one-bit:
629 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
630 * or disabled. However, this is different from the SPI-style write protect
631 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
632 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
633 *
634 * SRP Range | PROTECT_RO_AT_BOOT
635 * 0 0 | 0
636 * 0 non-zero | 1
637 * 1 0 | 1
638 * 1 non-zero | 1
639 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800640 *
641 * Besides, to make the protection take effect as soon as possible, we
642 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
643 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800644 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700645static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800646 struct ec_params_flash_protect p;
647 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800648 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
649 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
650 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800651 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800652
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800653 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800654 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800655 p.mask = (ro_at_boot_flag | ro_now_flag);
656 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700657 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700658 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800659 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800660 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
661 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800662 return 1;
663 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800664
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800665 /* Read back */
666 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700667 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700668 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800669 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800670 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
671 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800672 return 1;
673 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800674
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800675 if (!enable) {
676 /* The disable case is easier to check. */
677 if (r.flags & ro_at_boot_flag) {
678 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
679 return 1;
680 } else if (r.flags & ro_now_flag) {
681 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
682 need_an_ec_cold_reset = 1;
683 goto exit;
684 }
685
686 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
687 return 0;
688 }
689
690 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
691 if (r.flags & ro_at_boot_flag) {
692 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
693 } else {
694 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
695 return 1;
696 }
697
698 /* Then, we check if the protection has been activated. */
699 if (r.flags & ro_now_flag) {
700 /* Good, RO_NOW is set. */
701 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
702 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
703 struct ec_params_reboot_ec reboot;
704
705 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
706
707 memset(&p, 0, sizeof(p));
708 p.mask = EC_FLASH_PROTECT_ALL_NOW;
709 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700710 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800711 EC_VER_FLASH_PROTECT,
712 &p, sizeof(p), &r, sizeof(r));
713 if (rc < 0) {
714 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
715 return 1;
716 }
717
718 /* Read back */
719 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700720 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800721 EC_VER_FLASH_PROTECT,
722 &p, sizeof(p), &r, sizeof(r));
723 if (rc < 0) {
724 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
725 return 1;
726 }
727
728 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
729 msg_perr("FAILED: ALL_NOW is not set.\n");
730 need_an_ec_cold_reset = 1;
731 goto exit;
732 }
733
734 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
735
736 /*
737 * Our goal is to protect the RO ASAP. The entire protection
738 * is just a workaround for platform not supporting RO_NOW.
739 * It has side-effect that the RW is also protected and leads
740 * the RW update failed. So, we arrange an EC code reset to
741 * unlock RW ASAP.
742 */
743 memset(&reboot, 0, sizeof(reboot));
744 reboot.cmd = EC_REBOOT_COLD;
745 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700746 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700747 0, &reboot, sizeof(reboot), NULL, 0);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800748 if (rc < 0) {
749 msg_perr("WARN: Cannot arrange a cold reset at next "
750 "shutdown to unlock entire protect.\n");
751 msg_perr(" But you can do it manually.\n");
752 } else {
753 msg_pdbg("INFO: A cold reset is arranged at next "
754 "shutdown.\n");
755 }
756
757 } else {
758 msg_perr("FAILED: RO_NOW is not set.\n");
759 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
760 "make write protection active now.\n");
761 need_an_ec_cold_reset = 1;
762 }
763
764exit:
765 if (need_an_ec_cold_reset) {
766 msg_perr("FAILED: You may need a reboot to take effect of "
767 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800768 return 1;
769 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800770
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800771 return 0;
772}
773
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700774static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800775 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900776 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800777 int rc;
778
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800779 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700780 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800781 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800782 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800783 return 1;
784 }
785 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900786 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800787 /* pass */
788 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800789 msg_perr("FAILED: Unsupported write protection range "
790 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800791 msg_perr("Currently supported range:\n");
792 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900793 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
794 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800795 return 1;
796 }
797
David Hendricks393deec2016-11-23 16:15:05 -0800798 if (ignore_wp_range_command)
799 return 0;
David Hendricksac1d25c2016-08-09 17:00:58 -0700800 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800801}
802
803
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700804static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700805 enum wp_mode wp_mode) {
806 int ret;
807
808 switch (wp_mode) {
809 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700810 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700811 break;
812 default:
813 msg_perr("%s():%d Unsupported write-protection mode\n",
814 __func__, __LINE__);
815 ret = 1;
816 break;
817 }
818
819 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800820}
821
822
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700823static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricks393deec2016-11-23 16:15:05 -0800824 /* --wp-range implicitly enables write protection on CrOS EC, so force
825 it not to if --wp-disable is what the user really wants. */
826 ignore_wp_range_command = 1;
David Hendricksac1d25c2016-08-09 17:00:58 -0700827 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800828}
829
830
Souvik Ghosh586968a2016-08-11 17:56:24 -0700831static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800832 struct ec_params_flash_protect p;
833 struct ec_response_flash_protect r;
834 int start, len; /* wp range */
835 int enabled;
836 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800837
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800838 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700839 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700840 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800841 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800842 msg_perr("FAILED: Cannot get the write protection status: %d\n",
843 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800844 return 1;
845 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700846 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800847 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800848 return 1;
849 }
850
851 start = len = 0;
852 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900853 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800854
855 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
856 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700857 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800858 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800859 msg_perr("FAILED: Cannot get the WP_RO region info: "
860 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800861 return 1;
862 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900863 start = info.offset;
864 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800865 } else {
866 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
867 __func__);
868 }
869
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800870 /*
871 * If neither RO_NOW or ALL_NOW is set, it means write protect is
872 * NOT active now.
873 */
874 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
875 start = len = 0;
876
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800877 /* Remove the SPI-style messages. */
878 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
879 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
880 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800881 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800882 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800883 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800884 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800885
886 return 0;
887}
888
David Hendrickse5454932013-11-04 18:16:11 -0800889/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700890int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800891{
892 struct ec_params_hello request;
893 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -0800894 int rc = 0;
895
896 /* Say hello to EC. */
897 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
898 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
899 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800900 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -0800901 sizeof(request), &response, sizeof(response));
902 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
903
904 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
905 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
906 "rc=%d, request=0x%x response=0x%x\n",
907 rc, request.in_data, response.out_data);
908 return 1;
909 }
910
911 return 0;
912}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800913
David Hendricksd13d90d2016-08-09 17:00:52 -0700914void cros_ec_set_max_size(struct cros_ec_priv *priv,
915 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700916 struct ec_response_get_protocol_info info;
917 int rc = 0;
918 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800919 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700920 &info, sizeof(info));
921 msg_pdbg("%s: rc:%d\n", __func__, rc);
922
923 if (rc == sizeof(info)) {
924 op->max_data_write = min(op->max_data_write,
925 info.max_request_packet_size -
926 sizeof(struct ec_host_request));
927 op->max_data_read = min(op->max_data_read,
928 info.max_response_packet_size -
929 sizeof(struct ec_host_response));
930 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
931 op->max_data_write, op->max_data_read);
932 }
933}
934
David Hendricks14935fe2014-08-14 17:38:24 -0700935
936/*
David Hendricks052446b2014-09-11 11:26:51 -0700937 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -0700938 *
939 * This function parses programmer parameters from the command line. Since
940 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
941 * run during internal programmer initialization.
942 */
943int cros_ec_parse_param(struct cros_ec_priv *priv)
944{
David Hendricks98b3c572016-11-30 01:50:08 +0000945 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -0700946
David Hendricks98b3c572016-11-30 01:50:08 +0000947 p = extract_programmer_param("dev");
948 if (p) {
David Hendricks14935fe2014-08-14 17:38:24 -0700949 unsigned int index;
950 char *endptr = NULL;
951
952 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800953 /*
954 * For backward compatibility, check if the index is
955 * a number: 0: main EC, 1: PD
956 * works only on Samus.
957 */
David Hendricks98b3c572016-11-30 01:50:08 +0000958 index = strtoul(p, &endptr, 10);
959 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
960 msg_perr("Invalid argument: \"%s\"\n", p);
961 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -0700962 }
963
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800964 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -0700965 msg_perr("%s: Invalid device index\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +0000966 return 1;
David Hendricks14935fe2014-08-14 17:38:24 -0700967 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800968 priv->dev = ec_type[index];
969 msg_pdbg("Target %s used\n", priv->dev);
970 }
David Hendricks14935fe2014-08-14 17:38:24 -0700971
David Hendricks98b3c572016-11-30 01:50:08 +0000972 p = extract_programmer_param("type");
973 if (p) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800974 unsigned int index;
975 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
David Hendricks98b3c572016-11-30 01:50:08 +0000976 if (!strcmp(p, ec_type[index]))
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800977 break;
978 if (index == ARRAY_SIZE(ec_type)) {
David Hendricks98b3c572016-11-30 01:50:08 +0000979 msg_perr("Invalid argument: \"%s\"\n", p);
980 return 1;
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800981 }
982 priv->dev = ec_type[index];
983 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -0700984 }
985
David Hendricks98b3c572016-11-30 01:50:08 +0000986 p = extract_programmer_param("block");
987 if (p) {
988 unsigned int block;
Duncan Laurie84328722014-09-10 23:25:01 -0700989 char *endptr = NULL;
990
991 errno = 0;
David Hendricks98b3c572016-11-30 01:50:08 +0000992 block = strtoul(p, &endptr, 0);
993 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
994 msg_perr("Invalid argument: \"%s\"\n", p);
995 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -0700996 }
997
David Hendricks98b3c572016-11-30 01:50:08 +0000998 if (block <= 0) {
Duncan Laurie84328722014-09-10 23:25:01 -0700999 msg_perr("%s: Invalid block size\n", __func__);
David Hendricks98b3c572016-11-30 01:50:08 +00001000 return 1;
Duncan Laurie84328722014-09-10 23:25:01 -07001001 }
1002
David Hendricks98b3c572016-11-30 01:50:08 +00001003 msg_pdbg("Override block size to 0x%x\n", block);
1004 priv->erase_block_size = block;
Duncan Laurie84328722014-09-10 23:25:01 -07001005 }
1006
David Hendricks98b3c572016-11-30 01:50:08 +00001007 return 0;
David Hendricks14935fe2014-08-14 17:38:24 -07001008}
1009
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001010int cros_ec_probe_size(struct flashctx *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001011 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +08001012 struct ec_response_flash_info info;
David Hendricksa672b042016-09-19 12:37:36 -07001013 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001014 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001015 struct block_eraser *eraser;
1016 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001017 .list_ranges = cros_ec_list_ranges,
1018 .set_range = cros_ec_set_range,
1019 .enable = cros_ec_enable_writeprotect,
1020 .disable = cros_ec_disable_writeprotect,
1021 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001022 };
1023
Souvik Ghosh586968a2016-08-11 17:56:24 -07001024 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001025 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +08001026 if (rc < 0) {
1027 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
1028 return 0;
1029 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001030 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001031 if (rc < 0) {
1032 msg_perr("%s(): Failed to probe (no current image): %d\n",
1033 __func__, rc);
1034 return 0;
1035 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001036 cros_ec_priv->current_image = rc;
1037 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001038
1039 flash->total_size = info.flash_size / 1024;
David Hendricksac1d25c2016-08-09 17:00:58 -07001040 flash->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001041 eraser = &flash->block_erasers[0];
Duncan Laurie84328722014-09-10 23:25:01 -07001042
1043 /* Allow overriding the erase block size in case EC is incorrect */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001044 if (cros_ec_priv->erase_block_size > 0)
1045 eraser->eraseblocks[0].size = cros_ec_priv->erase_block_size;
Duncan Laurie84328722014-09-10 23:25:01 -07001046 else
1047 eraser->eraseblocks[0].size = info.erase_block_size;
1048
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001049 eraser->eraseblocks[0].count = info.flash_size /
1050 eraser->eraseblocks[0].size;
1051 flash->wp = &wp;
1052
David Hendricks194b3bb2013-07-16 14:32:26 -07001053 /*
1054 * Some STM32 variants erase bits to 0. For now, assume that this
1055 * applies to STM32L parts.
1056 *
1057 * FIXME: This info will eventually be exposed via some EC command.
1058 * See chrome-os-partner:20973.
1059 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001060 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001061 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001062 if (rc < 0) {
1063 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1064 return 0;
1065 }
Vincent Palatin4faff9a2017-03-17 17:27:39 +01001066 if (!strncmp(chip_info.name, "stm32l1", 7))
David Hendricks194b3bb2013-07-16 14:32:26 -07001067 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
1068
Souvik Ghosh586968a2016-08-11 17:56:24 -07001069 rc = set_ideal_write_size();
David Hendricksfbd5e6d2014-08-21 15:01:43 -07001070 if (rc < 0) {
1071 msg_perr("%s(): Unable to set write size\n", __func__);
1072 return 0;
1073 }
David Hendricksf9461c72013-07-11 19:02:13 -07001074
David Hendricksa672b042016-09-19 12:37:36 -07001075 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1076 0, NULL, 0, &spi_info, sizeof(spi_info));
1077 if (rc < 0) {
1078 static char chip_vendor[32];
1079 static char chip_name[32];
1080
1081 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1082 memcpy(chip_name, chip_info.name, sizeof(chip_name));
1083 flash->vendor = chip_vendor;
1084 flash->name = chip_name;
1085 flash->tested = TEST_OK_PREWU;
1086 } else {
1087 const struct flashchip *f;
1088 uint32_t mfg = spi_info.jedec[0];
1089 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1090
1091 for (f = flashchips; f && f->name; f++) {
1092 if (f->bustype != BUS_SPI)
1093 continue;
1094 if ((f->manufacture_id == mfg) &&
1095 f->model_id == model) {
1096 flash->vendor = f->vendor;
1097 flash->name = f->name;
1098 flash->tested = f->tested;
1099 break;
1100 }
1101 }
1102 }
1103
Simon Glassc453a642013-07-01 18:08:53 +09001104 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1105 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001106 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1107 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001108 if (rc) {
1109 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1110 __func__, rc);
1111 return 0;
1112 }
1113
Souvik Ghosh586968a2016-08-11 17:56:24 -07001114 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1115 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001116 if (rc) {
1117 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1118 __func__, rc);
1119 return 0;
1120 }
1121
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001122 return 1;
1123};