blob: f6828c2555c8f9e65cafd9f30150598750269ffd [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;
55
David Hendricksb64b39a2016-10-11 13:48:06 -070056static int set_wp(int enable); /* FIXME: move set_wp() */
57
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080058struct wp_data {
59 int enable;
60 unsigned int start;
61 unsigned int len;
62};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080063#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
64
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080065/* If software sync is enabled, then we don't try the latest firmware copy
66 * after updating.
67 */
68#define SOFTWARE_SYNC_ENABLED
69
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080070/* 1 if we want the flashrom to call erase_and_write_flash() again. */
71static int need_2nd_pass = 0;
72
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080073/* 1 if we want the flashrom to try jumping to new firmware after update. */
74static int try_latest_firmware = 0;
75
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080076/* The range of each firmware copy from the image file to update.
77 * But re-define the .flags as the valid flag to indicate the firmware is
78 * new or not (if flags = 1).
79 */
80static struct fmap_area fwcopy[4]; // [0] is not used.
81
82/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080083static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070084 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
85 "EC_RO",
86 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080087};
88
Gwendal Grignou94e87d62014-11-25 15:34:15 -080089/*
90 * The names of the different device that can be found in a machine.
91 * Order is important: for backward compatibilty issue,
92 * 'ec' must be 0, 'pd' must be 1.
93 */
94static const char *ec_type[] = {
95 [0] = "ec",
96 [1] = "pd",
97 [2] = "sh",
98};
99
Simon Glassc453a642013-07-01 18:08:53 +0900100/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
101 * the number of regions */
102static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800103
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800104/* Given the range not able to update, mark the corresponding
105 * firmware as old.
106 */
David Hendricksb907de32014-08-11 16:47:09 -0700107static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800108{
109 int i;
110
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800111 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800112 struct fmap_area *fw = &fwcopy[i];
113 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
114 (fw->offset >= addr && (fw->offset < addr + len))) {
115 msg_pdbg("Mark firmware [%s] as old.\n",
116 sections[i]);
117 fw->flags = 0; // mark as old
118 }
119 }
120}
121
122
Souvik Ghosh586968a2016-08-11 17:56:24 -0700123static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900124{
125 struct ec_response_get_version resp;
126 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700127
Souvik Ghosh586968a2016-08-11 17:56:24 -0700128 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700129 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900130 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700131 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900132 return rc;
133 }
134 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700135 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900136 return -1;
137 }
138
139 return resp.current_image;
140}
141
142
Souvik Ghosh586968a2016-08-11 17:56:24 -0700143static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900144 struct ec_response_flash_region_info *info)
145{
146 struct ec_params_flash_region_info req;
147 struct ec_response_flash_region_info resp;
148 int rc;
149
150 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700151 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900152 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
153 &resp, sizeof(resp));
154 if (rc < 0) {
155 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
156 return rc;
157 }
158
159 info->offset = resp.offset;
160 info->size = resp.size;
161 return 0;
162}
163
David Hendricksf9461c72013-07-11 19:02:13 -0700164/**
165 * Get the versions of the command supported by the EC.
166 *
167 * @param cmd Command
168 * @param pmask Destination for version mask; will be set to 0 on
169 * error.
170 * @return 0 if success, <0 if error
171 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700172static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700173{
David Hendricksf9461c72013-07-11 19:02:13 -0700174 struct ec_params_get_cmd_versions pver;
175 struct ec_response_get_cmd_versions rver;
176 int rc;
177
178 *pmask = 0;
179
180 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700181 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700182 &pver, sizeof(pver), &rver, sizeof(rver));
183
184 if (rc < 0)
185 return rc;
186
187 *pmask = rver.version_mask;
188 return rc;
189}
190
191/**
192 * Return non-zero if the EC supports the command and version
193 *
194 * @param cmd Command to check
195 * @param ver Version to check
196 * @return non-zero if command version supported; 0 if not.
197 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700198static int ec_cmd_version_supported(int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700199{
200 uint32_t mask = 0;
201 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700202
David Hendricksac1d25c2016-08-09 17:00:58 -0700203 rc = ec_get_cmd_versions(cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700204 if (rc < 0)
205 return rc;
206
207 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
208}
209
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700210/* returns 0 if successful or <0 to indicate error */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700211static int set_ideal_write_size(void)
David Hendricksf9461c72013-07-11 19:02:13 -0700212{
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700213 int cmd_version, ret;
David Hendricksf9461c72013-07-11 19:02:13 -0700214
David Hendricksac1d25c2016-08-09 17:00:58 -0700215 cmd_version = ec_cmd_version_supported(EC_CMD_FLASH_WRITE,
David Hendricksfb405f12014-08-19 22:42:30 -0700216 EC_VER_FLASH_WRITE);
217 if (cmd_version < 0) {
218 msg_perr("Cannot determine write command version\n");
219 return cmd_version;
220 } else if (cmd_version == 0) {
221 struct ec_response_flash_info info;
David Hendricksf9461c72013-07-11 19:02:13 -0700222
Souvik Ghosh586968a2016-08-11 17:56:24 -0700223 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700224 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700225 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700226 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700227 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700228 }
229
Souvik Ghosh586968a2016-08-11 17:56:24 -0700230 cros_ec_priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE;
David Hendricksfb405f12014-08-19 22:42:30 -0700231 } else {
232 struct ec_response_flash_info_1 info;
233
Souvik Ghosh586968a2016-08-11 17:56:24 -0700234 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700235 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700236 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700237 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700238 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700239 }
240
Souvik Ghosh586968a2016-08-11 17:56:24 -0700241 cros_ec_priv->ideal_write_size = info.write_ideal_size;
David Hendricksf9461c72013-07-11 19:02:13 -0700242 }
243
David Hendricksfb405f12014-08-19 22:42:30 -0700244 return 0;
David Hendricksf9461c72013-07-11 19:02:13 -0700245}
Simon Glass3c01dca2013-07-01 18:07:34 +0900246
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800247/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800248 * then this functions picks a NEW firmware copy and jumps to it. Note that
249 * RO is preferred, then A, finally B.
250 *
251 * Returns 0 for success.
252 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700253static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800254 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800255 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700256 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800257
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800258 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
259 * jump to different firmware copy. The second EC_RES_SUCCESS would
260 * set the OBF=1 and the next command cannot be executed.
261 * Thus, we call EC to jump only if the target is different.
262 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700263 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700264 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800265 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700266 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900267 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800268
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800269 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900270
271 /* Translate target --> EC reboot command parameter */
272 switch (target) {
273 case EC_IMAGE_RO:
274 p.cmd = EC_REBOOT_JUMP_RO;
275 break;
276 case EC_IMAGE_RW:
277 p.cmd = EC_REBOOT_JUMP_RW;
278 break;
279 default:
280 /*
281 * If target is unspecified, set EC reboot command to use
282 * a new image. Also set "target" so that it may be used
283 * to update the priv->current_image if jump is successful.
284 */
285 if (fwcopy[EC_IMAGE_RO].flags) {
286 p.cmd = EC_REBOOT_JUMP_RO;
287 target = EC_IMAGE_RO;
288 } else if (fwcopy[EC_IMAGE_RW].flags) {
289 p.cmd = EC_REBOOT_JUMP_RW;
290 target = EC_IMAGE_RW;
291 } else {
292 p.cmd = EC_IMAGE_UNKNOWN;
293 }
294 break;
295 }
296
David Hendricksb907de32014-08-11 16:47:09 -0700297 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800298 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
299
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700300 if (current_image == p.cmd) {
David Hendricksb907de32014-08-11 16:47:09 -0700301 msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700302 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800303 return 0;
304 }
305
Souvik Ghosh586968a2016-08-11 17:56:24 -0700306 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700307 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800308 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700309 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800310 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800311 } else {
David Hendricksb907de32014-08-11 16:47:09 -0700312 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800313 rc = EC_RES_SUCCESS;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700314 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800315 }
316
317 /* Sleep 1 sec to wait the EC re-init. */
318 usleep(1000000);
319
David Hendricksf9461c72013-07-11 19:02:13 -0700320 /* update max data write size in case we're jumping to an EC
321 * firmware with different protocol */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700322 set_ideal_write_size();
David Hendricksf9461c72013-07-11 19:02:13 -0700323
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800324 return rc;
325}
326
David Hendricksb64b39a2016-10-11 13:48:06 -0700327static int cros_ec_restore_wp(void *data)
328{
329 msg_pdbg("Restoring EC soft WP.\n");
330 return set_wp(1);
331}
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800332
David Hendricksb64b39a2016-10-11 13:48:06 -0700333static int cros_ec_wp_is_enabled(void)
334{
335 struct ec_params_flash_protect p;
336 struct ec_response_flash_protect r;
337 int rc;
338
339 memset(&p, 0, sizeof(p));
340 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
341 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
342 if (rc < 0) {
343 msg_perr("FAILED: Cannot get the write protection status: %d\n",
344 rc);
345 return -1;
346 } else if (rc < sizeof(r)) {
347 msg_perr("FAILED: Too little data returned (expected:%zd, "
348 "actual:%d)\n", sizeof(r), rc);
349 return -1;
350 }
351
352 if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))
353 return 1;
354
355 return 0;
356}
357
358/*
359 * Prepare EC for update:
360 * - Disable soft WP if needed.
361 * - Parse flashmap.
362 * - Jump to RO firmware.
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800363 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700364int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800365 struct fmap *fmap;
David Hendricksb64b39a2016-10-11 13:48:06 -0700366 int i, j, wp_status;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800367
Souvik Ghosh586968a2016-08-11 17:56:24 -0700368 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800369
David Hendricksb64b39a2016-10-11 13:48:06 -0700370 /*
371 * If HW WP is disabled we may still need to disable write protection
372 * that is active on the EC. Otherwise the EC can reject erase/write
373 * commands.
374 *
375 * Failure is OK since HW WP might be enabled or the EC needs to be
376 * rebooted for the change to take effect. We can still update RW
377 * portions.
378 *
379 * If disabled here, EC WP will be restored at the end so that
380 * "--wp-enable" does not need to be run later. This greatly
381 * simplifies logic for developers and scripts.
382 */
383 wp_status = cros_ec_wp_is_enabled();
384 if (wp_status < 0) {
385 return 1;
386 } else if (wp_status == 1) {
387 msg_pdbg("Attempting to disable EC soft WP.\n");
388 if (!set_wp(0)) {
389 msg_pdbg("EC soft WP disabled successfully.\n");
390 if (register_shutdown(cros_ec_restore_wp, NULL))
391 return 1;
392 } else {
393 msg_pdbg("Failed. Hardware WP might in effect or EC "
394 "needs to be rebooted first.\n");
395 }
396 } else {
397 msg_pdbg("EC soft WP is already disabled.\n");
398 }
399
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800400 // Parse the fmap in the image file and cache the firmware ranges.
401 fmap = fmap_find_in_memory(image, size);
402 if (!fmap) return 0;
403
404 // Lookup RO/A/B sections in FMAP.
405 for (i = 0; i < fmap->nareas; i++) {
406 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800407 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700408 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800409 msg_pdbg("Found '%s' in image.\n", fa->name);
410 memcpy(&fwcopy[j], fa, sizeof(*fa));
411 fwcopy[j].flags = 1; // mark as new
412 }
413 }
414 }
415
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800416 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700417 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800418 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700419 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800420}
421
422
423/* Returns >0 if we need 2nd pass of erase_and_write_flash().
424 * <0 if we cannot jump to any firmware copy.
425 * ==0 if no more pass is needed.
426 *
427 * This function also jumps to new-updated firmware copy before return >0.
428 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700429int cros_ec_need_2nd_pass(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700430 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800431
432 if (need_2nd_pass) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700433 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800434 return -1;
435 }
436 }
437
438 return need_2nd_pass;
439}
440
441
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800442/* Returns 0 for success.
443 *
444 * Try latest firmware: B > A > RO
445 *
David Hendricksb907de32014-08-11 16:47:09 -0700446 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800447 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700448 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800449 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700450int cros_ec_finish(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700451 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800452
453 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800454 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700455 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
456 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800457 }
458
459 return 0;
460}
461
462
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700463int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800464 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800465 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800466 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700467 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800468 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700469 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800470
David Hendricks133083b2012-07-17 20:39:38 -0700471 while (offset < readcnt) {
472 count = min(maxlen, readcnt - offset);
473 p.offset = blockaddr + offset;
474 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700475 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700476 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800477 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700478 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700479 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800480 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800481 } else {
482 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800483 }
484
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800485 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700486 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800487 }
488
489 return rc;
490}
491
492
Simon Glassc453a642013-07-01 18:08:53 +0900493/*
494 * returns 0 to indicate area does not overlap current EC image
495 * returns 1 to indicate area overlaps current EC image or error
496 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700497static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900498{
Simon Glassc453a642013-07-01 18:08:53 +0900499 enum ec_current_image image;
500 uint32_t region_offset;
501 uint32_t region_size;
502
Souvik Ghosh586968a2016-08-11 17:56:24 -0700503 image = cros_ec_priv->current_image;
504 region_offset = cros_ec_priv->region[image].offset;
505 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900506
507 if ((addr + len - 1 < region_offset) ||
508 (addr > region_offset + region_size - 1)) {
509 return 0;
510 }
511 return 1;
512}
513
514
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700515int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800516 unsigned int blockaddr,
517 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800518 struct ec_params_flash_erase erase;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800519 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800520
Souvik Ghosh586968a2016-08-11 17:56:24 -0700521 if (in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700522 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900523 need_2nd_pass = 1;
524 return ACCESS_DENIED;
525 }
526
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800527 erase.offset = blockaddr;
528 erase.size = len;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700529 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE,
David Hendricks14935fe2014-08-14 17:38:24 -0700530 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800531 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800532 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700533 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800534 need_2nd_pass = 1;
535 return ACCESS_DENIED;
536 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800537 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700538 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800539 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800540 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800541 } else {
542 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800543 }
544
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800545#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800546 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800547#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800548 return rc;
549}
550
551
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700552int cros_ec_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800553 unsigned int nbytes) {
554 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800555 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800556 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700557 uint8_t *packet;
558
Ken Chang69c31b82014-10-28 15:17:21 +0800559 /*
560 * For chrome-os-partner:33035, to workaround the undersized
561 * outdata buffer issue in kernel.
562 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700563 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700564 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800565 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700566 if (!packet)
567 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800568
569 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800570 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800571 p.offset = addr + i;
572 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900573
Souvik Ghosh586968a2016-08-11 17:56:24 -0700574 if (in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700575 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900576 need_2nd_pass = 1;
577 return ACCESS_DENIED;
578 }
579
David Hendricks2d6db772013-07-10 21:07:48 -0700580 memcpy(packet, &p, sizeof(p));
581 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700582 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700583 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700584
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800585 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800586 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700587 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800588 need_2nd_pass = 1;
589 return ACCESS_DENIED;
590 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800591
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800592 if (rc < 0) break;
593 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800594 }
595
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800596#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800597 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800598#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700599 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800600 return rc;
601}
602
603
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700604static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900605 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800606 int rc;
607
Souvik Ghosh586968a2016-08-11 17:56:24 -0700608 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800609 if (rc < 0) {
610 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
611 return 1;
612 }
613
614 msg_pinfo("Supported write protect range:\n");
615 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900616 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
617 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800618
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800619 return 0;
620}
621
622
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800623/*
624 * Helper function for flash protection.
625 *
626 * On EC API v1, the EC write protection has been simplified to one-bit:
627 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
628 * or disabled. However, this is different from the SPI-style write protect
629 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
630 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
631 *
632 * SRP Range | PROTECT_RO_AT_BOOT
633 * 0 0 | 0
634 * 0 non-zero | 1
635 * 1 0 | 1
636 * 1 non-zero | 1
637 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800638 *
639 * Besides, to make the protection take effect as soon as possible, we
640 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
641 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800642 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700643static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800644 struct ec_params_flash_protect p;
645 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800646 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
647 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
648 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800649 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800650
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800651 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800652 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800653 p.mask = (ro_at_boot_flag | ro_now_flag);
654 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700655 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700656 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800657 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800658 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
659 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800660 return 1;
661 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800662
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800663 /* Read back */
664 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700665 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700666 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800667 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800668 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
669 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800670 return 1;
671 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800672
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800673 if (!enable) {
674 /* The disable case is easier to check. */
675 if (r.flags & ro_at_boot_flag) {
676 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
677 return 1;
678 } else if (r.flags & ro_now_flag) {
679 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
680 need_an_ec_cold_reset = 1;
681 goto exit;
682 }
683
684 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
685 return 0;
686 }
687
688 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
689 if (r.flags & ro_at_boot_flag) {
690 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
691 } else {
692 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
693 return 1;
694 }
695
696 /* Then, we check if the protection has been activated. */
697 if (r.flags & ro_now_flag) {
698 /* Good, RO_NOW is set. */
699 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
700 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
701 struct ec_params_reboot_ec reboot;
702
703 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
704
705 memset(&p, 0, sizeof(p));
706 p.mask = EC_FLASH_PROTECT_ALL_NOW;
707 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700708 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800709 EC_VER_FLASH_PROTECT,
710 &p, sizeof(p), &r, sizeof(r));
711 if (rc < 0) {
712 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
713 return 1;
714 }
715
716 /* Read back */
717 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700718 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800719 EC_VER_FLASH_PROTECT,
720 &p, sizeof(p), &r, sizeof(r));
721 if (rc < 0) {
722 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
723 return 1;
724 }
725
726 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
727 msg_perr("FAILED: ALL_NOW is not set.\n");
728 need_an_ec_cold_reset = 1;
729 goto exit;
730 }
731
732 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
733
734 /*
735 * Our goal is to protect the RO ASAP. The entire protection
736 * is just a workaround for platform not supporting RO_NOW.
737 * It has side-effect that the RW is also protected and leads
738 * the RW update failed. So, we arrange an EC code reset to
739 * unlock RW ASAP.
740 */
741 memset(&reboot, 0, sizeof(reboot));
742 reboot.cmd = EC_REBOOT_COLD;
743 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700744 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700745 0, &reboot, sizeof(reboot), NULL, 0);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800746 if (rc < 0) {
747 msg_perr("WARN: Cannot arrange a cold reset at next "
748 "shutdown to unlock entire protect.\n");
749 msg_perr(" But you can do it manually.\n");
750 } else {
751 msg_pdbg("INFO: A cold reset is arranged at next "
752 "shutdown.\n");
753 }
754
755 } else {
756 msg_perr("FAILED: RO_NOW is not set.\n");
757 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
758 "make write protection active now.\n");
759 need_an_ec_cold_reset = 1;
760 }
761
762exit:
763 if (need_an_ec_cold_reset) {
764 msg_perr("FAILED: You may need a reboot to take effect of "
765 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800766 return 1;
767 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800768
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800769 return 0;
770}
771
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700772static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800773 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900774 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800775 int rc;
776
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800777 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700778 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800779 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800780 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800781 return 1;
782 }
783 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900784 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800785 /* pass */
786 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800787 msg_perr("FAILED: Unsupported write protection range "
788 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800789 msg_perr("Currently supported range:\n");
790 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900791 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
792 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800793 return 1;
794 }
795
David Hendricksac1d25c2016-08-09 17:00:58 -0700796 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800797}
798
799
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700800static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700801 enum wp_mode wp_mode) {
802 int ret;
803
804 switch (wp_mode) {
805 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700806 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700807 break;
808 default:
809 msg_perr("%s():%d Unsupported write-protection mode\n",
810 __func__, __LINE__);
811 ret = 1;
812 break;
813 }
814
815 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800816}
817
818
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700819static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700820 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800821}
822
823
Souvik Ghosh586968a2016-08-11 17:56:24 -0700824static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800825 struct ec_params_flash_protect p;
826 struct ec_response_flash_protect r;
827 int start, len; /* wp range */
828 int enabled;
829 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800830
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800831 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700832 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700833 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800834 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800835 msg_perr("FAILED: Cannot get the write protection status: %d\n",
836 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800837 return 1;
838 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700839 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800840 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800841 return 1;
842 }
843
844 start = len = 0;
845 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900846 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800847
848 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
849 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700850 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800851 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800852 msg_perr("FAILED: Cannot get the WP_RO region info: "
853 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800854 return 1;
855 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900856 start = info.offset;
857 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800858 } else {
859 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
860 __func__);
861 }
862
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800863 /*
864 * If neither RO_NOW or ALL_NOW is set, it means write protect is
865 * NOT active now.
866 */
867 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
868 start = len = 0;
869
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800870 /* Remove the SPI-style messages. */
871 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
872 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
873 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800874 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800875 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800876 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800877 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800878
879 return 0;
880}
881
David Hendrickse5454932013-11-04 18:16:11 -0800882/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700883int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800884{
885 struct ec_params_hello request;
886 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -0800887 int rc = 0;
888
889 /* Say hello to EC. */
890 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
891 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
892 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800893 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -0800894 sizeof(request), &response, sizeof(response));
895 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
896
897 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
898 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
899 "rc=%d, request=0x%x response=0x%x\n",
900 rc, request.in_data, response.out_data);
901 return 1;
902 }
903
904 return 0;
905}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800906
David Hendricksd13d90d2016-08-09 17:00:52 -0700907void cros_ec_set_max_size(struct cros_ec_priv *priv,
908 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700909 struct ec_response_get_protocol_info info;
910 int rc = 0;
911 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800912 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700913 &info, sizeof(info));
914 msg_pdbg("%s: rc:%d\n", __func__, rc);
915
916 if (rc == sizeof(info)) {
917 op->max_data_write = min(op->max_data_write,
918 info.max_request_packet_size -
919 sizeof(struct ec_host_request));
920 op->max_data_read = min(op->max_data_read,
921 info.max_response_packet_size -
922 sizeof(struct ec_host_response));
923 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
924 op->max_data_write, op->max_data_read);
925 }
926}
927
David Hendricks14935fe2014-08-14 17:38:24 -0700928
929/*
David Hendricks052446b2014-09-11 11:26:51 -0700930 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -0700931 *
932 * This function parses programmer parameters from the command line. Since
933 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
934 * run during internal programmer initialization.
935 */
936int cros_ec_parse_param(struct cros_ec_priv *priv)
937{
938 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -0700939
David Hendricksd13d90d2016-08-09 17:00:52 -0700940 p = extract_programmer_param("dev");
David Hendricks14935fe2014-08-14 17:38:24 -0700941 if (p) {
942 unsigned int index;
943 char *endptr = NULL;
944
945 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800946 /*
947 * For backward compatibility, check if the index is
948 * a number: 0: main EC, 1: PD
949 * works only on Samus.
950 */
David Hendricks14935fe2014-08-14 17:38:24 -0700951 index = strtoul(p, &endptr, 10);
952 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
953 msg_perr("Invalid argument: \"%s\"\n", p);
954 return 1;
955 }
956
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800957 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -0700958 msg_perr("%s: Invalid device index\n", __func__);
959 return 1;
960 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800961 priv->dev = ec_type[index];
962 msg_pdbg("Target %s used\n", priv->dev);
963 }
David Hendricks14935fe2014-08-14 17:38:24 -0700964
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800965 p = extract_programmer_param("type");
966 if (p) {
967 unsigned int index;
968 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
969 if (!strcmp(p, ec_type[index]))
970 break;
971 if (index == ARRAY_SIZE(ec_type)) {
972 msg_perr("Invalid argument: \"%s\"\n", p);
973 return 1;
974 }
975 priv->dev = ec_type[index];
976 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -0700977 }
978
Duncan Laurie84328722014-09-10 23:25:01 -0700979 p = extract_programmer_param("block");
980 if (p) {
981 unsigned int block;
982 char *endptr = NULL;
983
984 errno = 0;
985 block = strtoul(p, &endptr, 0);
986 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
987 msg_perr("Invalid argument: \"%s\"\n", p);
988 return 1;
989 }
990
991 if (block <= 0) {
992 msg_perr("%s: Invalid block size\n", __func__);
993 return 1;
994 }
995
996 msg_pdbg("Override block size to 0x%x\n", block);
997 priv->erase_block_size = block;
998 }
999
David Hendricks14935fe2014-08-14 17:38:24 -07001000 return 0;
1001}
1002
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001003int cros_ec_probe_size(struct flashctx *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001004 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +08001005 struct ec_response_flash_info info;
David Hendricksa672b042016-09-19 12:37:36 -07001006 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -07001007 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001008 struct block_eraser *eraser;
1009 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -07001010 .list_ranges = cros_ec_list_ranges,
1011 .set_range = cros_ec_set_range,
1012 .enable = cros_ec_enable_writeprotect,
1013 .disable = cros_ec_disable_writeprotect,
1014 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001015 };
1016
Souvik Ghosh586968a2016-08-11 17:56:24 -07001017 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001018 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +08001019 if (rc < 0) {
1020 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
1021 return 0;
1022 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001023 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +09001024 if (rc < 0) {
1025 msg_perr("%s(): Failed to probe (no current image): %d\n",
1026 __func__, rc);
1027 return 0;
1028 }
Souvik Ghosh586968a2016-08-11 17:56:24 -07001029 cros_ec_priv->current_image = rc;
1030 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001031
1032 flash->total_size = info.flash_size / 1024;
David Hendricksac1d25c2016-08-09 17:00:58 -07001033 flash->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001034 eraser = &flash->block_erasers[0];
Duncan Laurie84328722014-09-10 23:25:01 -07001035
1036 /* Allow overriding the erase block size in case EC is incorrect */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001037 if (cros_ec_priv->erase_block_size > 0)
1038 eraser->eraseblocks[0].size = cros_ec_priv->erase_block_size;
Duncan Laurie84328722014-09-10 23:25:01 -07001039 else
1040 eraser->eraseblocks[0].size = info.erase_block_size;
1041
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001042 eraser->eraseblocks[0].count = info.flash_size /
1043 eraser->eraseblocks[0].size;
1044 flash->wp = &wp;
1045
David Hendricks194b3bb2013-07-16 14:32:26 -07001046 /*
1047 * Some STM32 variants erase bits to 0. For now, assume that this
1048 * applies to STM32L parts.
1049 *
1050 * FIXME: This info will eventually be exposed via some EC command.
1051 * See chrome-os-partner:20973.
1052 */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001053 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001054 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001055 if (rc < 0) {
1056 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1057 return 0;
1058 }
1059 if (!strncmp(chip_info.name, "stm32l", 6))
1060 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
1061
Souvik Ghosh586968a2016-08-11 17:56:24 -07001062 rc = set_ideal_write_size();
David Hendricksfbd5e6d2014-08-21 15:01:43 -07001063 if (rc < 0) {
1064 msg_perr("%s(): Unable to set write size\n", __func__);
1065 return 0;
1066 }
David Hendricksf9461c72013-07-11 19:02:13 -07001067
David Hendricksa672b042016-09-19 12:37:36 -07001068 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1069 0, NULL, 0, &spi_info, sizeof(spi_info));
1070 if (rc < 0) {
1071 static char chip_vendor[32];
1072 static char chip_name[32];
1073
1074 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1075 memcpy(chip_name, chip_info.name, sizeof(chip_name));
1076 flash->vendor = chip_vendor;
1077 flash->name = chip_name;
1078 flash->tested = TEST_OK_PREWU;
1079 } else {
1080 const struct flashchip *f;
1081 uint32_t mfg = spi_info.jedec[0];
1082 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1083
1084 for (f = flashchips; f && f->name; f++) {
1085 if (f->bustype != BUS_SPI)
1086 continue;
1087 if ((f->manufacture_id == mfg) &&
1088 f->model_id == model) {
1089 flash->vendor = f->vendor;
1090 flash->name = f->name;
1091 flash->tested = f->tested;
1092 break;
1093 }
1094 }
1095 }
1096
Simon Glassc453a642013-07-01 18:08:53 +09001097 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1098 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001099 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1100 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001101 if (rc) {
1102 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1103 __func__, rc);
1104 return 0;
1105 }
1106
Souvik Ghosh586968a2016-08-11 17:56:24 -07001107 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1108 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001109 if (rc) {
1110 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1111 __func__, rc);
1112 return 0;
1113 }
1114
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001115 return 1;
1116};