blob: 4f4beb0750f38bbc6a52c0c0ffb5715b836250ee [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>
53struct wp_data {
54 int enable;
55 unsigned int start;
56 unsigned int len;
57};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080058#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
59
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080060/* If software sync is enabled, then we don't try the latest firmware copy
61 * after updating.
62 */
63#define SOFTWARE_SYNC_ENABLED
64
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080065/* 1 if we want the flashrom to call erase_and_write_flash() again. */
66static int need_2nd_pass = 0;
67
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080068/* 1 if we want the flashrom to try jumping to new firmware after update. */
69static int try_latest_firmware = 0;
70
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080071/* The range of each firmware copy from the image file to update.
72 * But re-define the .flags as the valid flag to indicate the firmware is
73 * new or not (if flags = 1).
74 */
75static struct fmap_area fwcopy[4]; // [0] is not used.
76
77/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080078static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070079 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
80 "EC_RO",
81 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080082};
83
Gwendal Grignou94e87d62014-11-25 15:34:15 -080084/*
85 * The names of the different device that can be found in a machine.
86 * Order is important: for backward compatibilty issue,
87 * 'ec' must be 0, 'pd' must be 1.
88 */
89static const char *ec_type[] = {
90 [0] = "ec",
91 [1] = "pd",
92 [2] = "sh",
93};
94
Simon Glassc453a642013-07-01 18:08:53 +090095/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
96 * the number of regions */
97static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080098
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080099/* Given the range not able to update, mark the corresponding
100 * firmware as old.
101 */
David Hendricksb907de32014-08-11 16:47:09 -0700102static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800103{
104 int i;
105
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800106 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800107 struct fmap_area *fw = &fwcopy[i];
108 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
109 (fw->offset >= addr && (fw->offset < addr + len))) {
110 msg_pdbg("Mark firmware [%s] as old.\n",
111 sections[i]);
112 fw->flags = 0; // mark as old
113 }
114 }
115}
116
117
David Hendricksd13d90d2016-08-09 17:00:52 -0700118static int cros_ec_get_current_image(struct cros_ec_priv *priv)
Simon Glass01c11672013-07-01 18:03:33 +0900119{
120 struct ec_response_get_version resp;
121 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700122 rc = priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700123 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900124 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700125 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900126 return rc;
127 }
128 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700129 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900130 return -1;
131 }
132
133 return resp.current_image;
134}
135
136
David Hendricksd13d90d2016-08-09 17:00:52 -0700137static int cros_ec_get_region_info(struct cros_ec_priv *priv,
138 enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900139 struct ec_response_flash_region_info *info)
140{
141 struct ec_params_flash_region_info req;
142 struct ec_response_flash_region_info resp;
143 int rc;
144
145 req.region = region;
David Hendricksd13d90d2016-08-09 17:00:52 -0700146 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900147 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
148 &resp, sizeof(resp));
149 if (rc < 0) {
150 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
151 return rc;
152 }
153
154 info->offset = resp.offset;
155 info->size = resp.size;
156 return 0;
157}
158
David Hendricksf9461c72013-07-11 19:02:13 -0700159/**
160 * Get the versions of the command supported by the EC.
161 *
162 * @param cmd Command
163 * @param pmask Destination for version mask; will be set to 0 on
164 * error.
165 * @return 0 if success, <0 if error
166 */
David Hendricksd13d90d2016-08-09 17:00:52 -0700167static int ec_get_cmd_versions(struct cros_ec_priv *priv, int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700168{
David Hendricksf9461c72013-07-11 19:02:13 -0700169 struct ec_params_get_cmd_versions pver;
170 struct ec_response_get_cmd_versions rver;
171 int rc;
172
173 *pmask = 0;
174
175 pver.cmd = cmd;
David Hendricksd13d90d2016-08-09 17:00:52 -0700176 rc = priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700177 &pver, sizeof(pver), &rver, sizeof(rver));
178
179 if (rc < 0)
180 return rc;
181
182 *pmask = rver.version_mask;
183 return rc;
184}
185
186/**
187 * Return non-zero if the EC supports the command and version
188 *
189 * @param cmd Command to check
190 * @param ver Version to check
191 * @return non-zero if command version supported; 0 if not.
192 */
David Hendricksd13d90d2016-08-09 17:00:52 -0700193static int ec_cmd_version_supported(struct cros_ec_priv *priv, int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700194{
195 uint32_t mask = 0;
196 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700197
198 rc = ec_get_cmd_versions(priv, cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700199 if (rc < 0)
200 return rc;
201
202 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
203}
204
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700205/* returns 0 if successful or <0 to indicate error */
David Hendricksd13d90d2016-08-09 17:00:52 -0700206static int set_ideal_write_size(struct cros_ec_priv *priv)
David Hendricksf9461c72013-07-11 19:02:13 -0700207{
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700208 int cmd_version, ret;
David Hendricksf9461c72013-07-11 19:02:13 -0700209
David Hendricksd13d90d2016-08-09 17:00:52 -0700210 cmd_version = ec_cmd_version_supported(priv, EC_CMD_FLASH_WRITE,
David Hendricksfb405f12014-08-19 22:42:30 -0700211 EC_VER_FLASH_WRITE);
212 if (cmd_version < 0) {
213 msg_perr("Cannot determine write command version\n");
214 return cmd_version;
215 } else if (cmd_version == 0) {
216 struct ec_response_flash_info info;
David Hendricksf9461c72013-07-11 19:02:13 -0700217
David Hendricksd13d90d2016-08-09 17:00:52 -0700218 ret = priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700219 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700220 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700221 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700222 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700223 }
224
David Hendricksd13d90d2016-08-09 17:00:52 -0700225 priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE;
David Hendricksfb405f12014-08-19 22:42:30 -0700226 } else {
227 struct ec_response_flash_info_1 info;
228
David Hendricksd13d90d2016-08-09 17:00:52 -0700229 ret = priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700230 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700231 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700232 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700233 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700234 }
235
David Hendricksd13d90d2016-08-09 17:00:52 -0700236 priv->ideal_write_size = info.write_ideal_size;
David Hendricksf9461c72013-07-11 19:02:13 -0700237 }
238
David Hendricksfb405f12014-08-19 22:42:30 -0700239 return 0;
David Hendricksf9461c72013-07-11 19:02:13 -0700240}
Simon Glass3c01dca2013-07-01 18:07:34 +0900241
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800242/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800243 * then this functions picks a NEW firmware copy and jumps to it. Note that
244 * RO is preferred, then A, finally B.
245 *
246 * Returns 0 for success.
247 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700248static int cros_ec_jump_copy(struct flashctx *flash, enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800249 struct ec_params_reboot_ec p;
David Hendricksd13d90d2016-08-09 17:00:52 -0700250 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800251 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700252 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800253
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800254 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
255 * jump to different firmware copy. The second EC_RES_SUCCESS would
256 * set the OBF=1 and the next command cannot be executed.
257 * Thus, we call EC to jump only if the target is different.
258 */
David Hendricksd13d90d2016-08-09 17:00:52 -0700259 current_image = cros_ec_get_current_image(priv);
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700260 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800261 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700262 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900263 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800264
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800265 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900266
267 /* Translate target --> EC reboot command parameter */
268 switch (target) {
269 case EC_IMAGE_RO:
270 p.cmd = EC_REBOOT_JUMP_RO;
271 break;
272 case EC_IMAGE_RW:
273 p.cmd = EC_REBOOT_JUMP_RW;
274 break;
275 default:
276 /*
277 * If target is unspecified, set EC reboot command to use
278 * a new image. Also set "target" so that it may be used
279 * to update the priv->current_image if jump is successful.
280 */
281 if (fwcopy[EC_IMAGE_RO].flags) {
282 p.cmd = EC_REBOOT_JUMP_RO;
283 target = EC_IMAGE_RO;
284 } else if (fwcopy[EC_IMAGE_RW].flags) {
285 p.cmd = EC_REBOOT_JUMP_RW;
286 target = EC_IMAGE_RW;
287 } else {
288 p.cmd = EC_IMAGE_UNKNOWN;
289 }
290 break;
291 }
292
David Hendricksb907de32014-08-11 16:47:09 -0700293 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800294 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
295
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700296 if (current_image == p.cmd) {
David Hendricksb907de32014-08-11 16:47:09 -0700297 msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]);
David Hendricksd13d90d2016-08-09 17:00:52 -0700298 priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800299 return 0;
300 }
301
David Hendricksd13d90d2016-08-09 17:00:52 -0700302 rc = priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700303 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800304 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700305 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800306 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800307 } else {
David Hendricksb907de32014-08-11 16:47:09 -0700308 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800309 rc = EC_RES_SUCCESS;
David Hendricksd13d90d2016-08-09 17:00:52 -0700310 priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800311 }
312
313 /* Sleep 1 sec to wait the EC re-init. */
314 usleep(1000000);
315
David Hendricksf9461c72013-07-11 19:02:13 -0700316 /* update max data write size in case we're jumping to an EC
317 * firmware with different protocol */
David Hendricksd13d90d2016-08-09 17:00:52 -0700318 set_ideal_write_size(priv);
David Hendricksf9461c72013-07-11 19:02:13 -0700319
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800320 return rc;
321}
322
323
324/* Given an image, this function parses FMAP and recognize the firmware
325 * ranges.
326 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700327int cros_ec_prepare(struct flashctx *flash, uint8_t *image, int size) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700328 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800329 struct fmap *fmap;
330 int i, j;
331
David Hendricksd13d90d2016-08-09 17:00:52 -0700332 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800333
334 // Parse the fmap in the image file and cache the firmware ranges.
335 fmap = fmap_find_in_memory(image, size);
336 if (!fmap) return 0;
337
338 // Lookup RO/A/B sections in FMAP.
339 for (i = 0; i < fmap->nareas; i++) {
340 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800341 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700342 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800343 msg_pdbg("Found '%s' in image.\n", fa->name);
344 memcpy(&fwcopy[j], fa, sizeof(*fa));
345 fwcopy[j].flags = 1; // mark as new
346 }
347 }
348 }
349
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800350 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700351 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800352 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700353 return cros_ec_jump_copy(flash, EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800354}
355
356
357/* Returns >0 if we need 2nd pass of erase_and_write_flash().
358 * <0 if we cannot jump to any firmware copy.
359 * ==0 if no more pass is needed.
360 *
361 * This function also jumps to new-updated firmware copy before return >0.
362 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700363int cros_ec_need_2nd_pass(struct flashctx *flash) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700364 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
365
366 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800367
368 if (need_2nd_pass) {
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700369 if (cros_ec_jump_copy(flash, EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800370 return -1;
371 }
372 }
373
374 return need_2nd_pass;
375}
376
377
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800378/* Returns 0 for success.
379 *
380 * Try latest firmware: B > A > RO
381 *
David Hendricksb907de32014-08-11 16:47:09 -0700382 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800383 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700384 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800385 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700386int cros_ec_finish(struct flashctx *flash) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700387 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
388
389 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800390
391 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800392 if (fwcopy[EC_IMAGE_RW].flags &&
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700393 cros_ec_jump_copy(flash, EC_IMAGE_RW) == 0) return 0;
394 return cros_ec_jump_copy(flash, EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800395 }
396
397 return 0;
398}
399
400
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700401int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800402 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800403 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800404 struct ec_params_flash_read p;
David Hendricksd13d90d2016-08-09 17:00:52 -0700405 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700406 int maxlen = flash->pgm->opaque.max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800407 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700408 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800409
David Hendricks133083b2012-07-17 20:39:38 -0700410 while (offset < readcnt) {
411 count = min(maxlen, readcnt - offset);
412 p.offset = blockaddr + offset;
413 p.size = count;
David Hendricksd13d90d2016-08-09 17:00:52 -0700414 rc = priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700415 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800416 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700417 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700418 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800419 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800420 } else {
421 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800422 }
423
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800424 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700425 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800426 }
427
428 return rc;
429}
430
431
Simon Glassc453a642013-07-01 18:08:53 +0900432/*
433 * returns 0 to indicate area does not overlap current EC image
434 * returns 1 to indicate area overlaps current EC image or error
435 */
David Hendricksd13d90d2016-08-09 17:00:52 -0700436static int in_current_image(struct cros_ec_priv *priv,
437 unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900438{
Simon Glassc453a642013-07-01 18:08:53 +0900439 enum ec_current_image image;
440 uint32_t region_offset;
441 uint32_t region_size;
442
David Hendricksd13d90d2016-08-09 17:00:52 -0700443 image = priv->current_image;
444 region_offset = priv->region[image].offset;
445 region_size = priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900446
447 if ((addr + len - 1 < region_offset) ||
448 (addr > region_offset + region_size - 1)) {
449 return 0;
450 }
451 return 1;
452}
453
454
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800456 unsigned int blockaddr,
457 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800458 struct ec_params_flash_erase erase;
David Hendricksd13d90d2016-08-09 17:00:52 -0700459 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800460 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800461
David Hendricksd13d90d2016-08-09 17:00:52 -0700462 if (in_current_image(priv, blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700463 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900464 need_2nd_pass = 1;
465 return ACCESS_DENIED;
466 }
467
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800468 erase.offset = blockaddr;
469 erase.size = len;
David Hendricksd13d90d2016-08-09 17:00:52 -0700470 rc = priv->ec_command(EC_CMD_FLASH_ERASE,
David Hendricks14935fe2014-08-14 17:38:24 -0700471 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800472 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800473 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700474 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800475 need_2nd_pass = 1;
476 return ACCESS_DENIED;
477 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800478 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700479 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800480 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800481 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800482 } else {
483 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800484 }
485
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800486#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800487 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800488#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800489 return rc;
490}
491
492
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700493int cros_ec_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800494 unsigned int nbytes) {
495 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800496 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800497 struct ec_params_flash_write p;
David Hendricksd13d90d2016-08-09 17:00:52 -0700498 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
David Hendricks2d6db772013-07-10 21:07:48 -0700499 uint8_t *packet;
500
Ken Chang69c31b82014-10-28 15:17:21 +0800501 /*
502 * For chrome-os-partner:33035, to workaround the undersized
503 * outdata buffer issue in kernel.
504 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700505 real_write_size = min(flash->pgm->opaque.max_data_write,
David Hendricksd13d90d2016-08-09 17:00:52 -0700506 priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800507 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700508 if (!packet)
509 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800510
511 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800512 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800513 p.offset = addr + i;
514 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900515
David Hendricksd13d90d2016-08-09 17:00:52 -0700516 if (in_current_image(priv, p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700517 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900518 need_2nd_pass = 1;
519 return ACCESS_DENIED;
520 }
521
David Hendricks2d6db772013-07-10 21:07:48 -0700522 memcpy(packet, &p, sizeof(p));
523 memcpy(packet + sizeof(p), &buf[i], written);
David Hendricksd13d90d2016-08-09 17:00:52 -0700524 rc = priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700525 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700526
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800527 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800528 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700529 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800530 need_2nd_pass = 1;
531 return ACCESS_DENIED;
532 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800533
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800534 if (rc < 0) break;
535 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800536 }
537
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800538#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800539 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800540#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700541 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800542 return rc;
543}
544
545
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700546static int cros_ec_list_ranges(const struct flashctx *flash) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700547 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900548 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800549 int rc;
550
David Hendricksd13d90d2016-08-09 17:00:52 -0700551 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800552 if (rc < 0) {
553 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
554 return 1;
555 }
556
557 msg_pinfo("Supported write protect range:\n");
558 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900559 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
560 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800561
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800562 return 0;
563}
564
565
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800566/*
567 * Helper function for flash protection.
568 *
569 * On EC API v1, the EC write protection has been simplified to one-bit:
570 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
571 * or disabled. However, this is different from the SPI-style write protect
572 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
573 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
574 *
575 * SRP Range | PROTECT_RO_AT_BOOT
576 * 0 0 | 0
577 * 0 non-zero | 1
578 * 1 0 | 1
579 * 1 non-zero | 1
580 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800581 *
582 * Besides, to make the protection take effect as soon as possible, we
583 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
584 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800585 */
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700586static int set_wp(const struct flashctx *flash, int enable) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700587 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800588 struct ec_params_flash_protect p;
589 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800590 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
591 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
592 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800593 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800594
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800595 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800596 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800597 p.mask = (ro_at_boot_flag | ro_now_flag);
598 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
David Hendricksd13d90d2016-08-09 17:00:52 -0700599 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700600 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800601 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800602 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
603 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800604 return 1;
605 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800606
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800607 /* Read back */
608 memset(&p, 0, sizeof(p));
David Hendricksd13d90d2016-08-09 17:00:52 -0700609 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700610 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800611 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800612 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
613 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800614 return 1;
615 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800616
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800617 if (!enable) {
618 /* The disable case is easier to check. */
619 if (r.flags & ro_at_boot_flag) {
620 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
621 return 1;
622 } else if (r.flags & ro_now_flag) {
623 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
624 need_an_ec_cold_reset = 1;
625 goto exit;
626 }
627
628 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
629 return 0;
630 }
631
632 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
633 if (r.flags & ro_at_boot_flag) {
634 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
635 } else {
636 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
637 return 1;
638 }
639
640 /* Then, we check if the protection has been activated. */
641 if (r.flags & ro_now_flag) {
642 /* Good, RO_NOW is set. */
643 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
644 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
645 struct ec_params_reboot_ec reboot;
646
647 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
648
649 memset(&p, 0, sizeof(p));
650 p.mask = EC_FLASH_PROTECT_ALL_NOW;
651 p.flags = EC_FLASH_PROTECT_ALL_NOW;
David Hendricksd13d90d2016-08-09 17:00:52 -0700652 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800653 EC_VER_FLASH_PROTECT,
654 &p, sizeof(p), &r, sizeof(r));
655 if (rc < 0) {
656 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
657 return 1;
658 }
659
660 /* Read back */
661 memset(&p, 0, sizeof(p));
David Hendricksd13d90d2016-08-09 17:00:52 -0700662 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800663 EC_VER_FLASH_PROTECT,
664 &p, sizeof(p), &r, sizeof(r));
665 if (rc < 0) {
666 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
667 return 1;
668 }
669
670 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
671 msg_perr("FAILED: ALL_NOW is not set.\n");
672 need_an_ec_cold_reset = 1;
673 goto exit;
674 }
675
676 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
677
678 /*
679 * Our goal is to protect the RO ASAP. The entire protection
680 * is just a workaround for platform not supporting RO_NOW.
681 * It has side-effect that the RW is also protected and leads
682 * the RW update failed. So, we arrange an EC code reset to
683 * unlock RW ASAP.
684 */
685 memset(&reboot, 0, sizeof(reboot));
686 reboot.cmd = EC_REBOOT_COLD;
687 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
David Hendricksd13d90d2016-08-09 17:00:52 -0700688 rc = priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700689 0, &reboot, sizeof(reboot), NULL, 0);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800690 if (rc < 0) {
691 msg_perr("WARN: Cannot arrange a cold reset at next "
692 "shutdown to unlock entire protect.\n");
693 msg_perr(" But you can do it manually.\n");
694 } else {
695 msg_pdbg("INFO: A cold reset is arranged at next "
696 "shutdown.\n");
697 }
698
699 } else {
700 msg_perr("FAILED: RO_NOW is not set.\n");
701 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
702 "make write protection active now.\n");
703 need_an_ec_cold_reset = 1;
704 }
705
706exit:
707 if (need_an_ec_cold_reset) {
708 msg_perr("FAILED: You may need a reboot to take effect of "
709 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800710 return 1;
711 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800712
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800713 return 0;
714}
715
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700716static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800717 unsigned int start, unsigned int len) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700718 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900719 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800720 int rc;
721
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800722 /* Check if the given range is supported */
David Hendricksd13d90d2016-08-09 17:00:52 -0700723 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800724 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800725 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800726 return 1;
727 }
728 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900729 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800730 /* pass */
731 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800732 msg_perr("FAILED: Unsupported write protection range "
733 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800734 msg_perr("Currently supported range:\n");
735 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900736 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
737 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800738 return 1;
739 }
740
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700741 return set_wp(flash, !!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800742}
743
744
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700745static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700746 enum wp_mode wp_mode) {
747 int ret;
748
749 switch (wp_mode) {
750 case WP_MODE_HARDWARE:
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700751 ret = set_wp(flash, 1);
David Hendricks1c09f802012-10-03 11:03:48 -0700752 break;
753 default:
754 msg_perr("%s():%d Unsupported write-protection mode\n",
755 __func__, __LINE__);
756 ret = 1;
757 break;
758 }
759
760 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800761}
762
763
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700764static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700765 return set_wp(flash, 0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800766}
767
768
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700769static int cros_ec_wp_status(const struct flashctx *flash) {
David Hendricksd13d90d2016-08-09 17:00:52 -0700770 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800771 struct ec_params_flash_protect p;
772 struct ec_response_flash_protect r;
773 int start, len; /* wp range */
774 int enabled;
775 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800776
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800777 memset(&p, 0, sizeof(p));
David Hendricksd13d90d2016-08-09 17:00:52 -0700778 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700779 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800780 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800781 msg_perr("FAILED: Cannot get the write protection status: %d\n",
782 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800783 return 1;
784 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700785 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800786 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800787 return 1;
788 }
789
790 start = len = 0;
791 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900792 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800793
794 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
795 __func__);
David Hendricksd13d90d2016-08-09 17:00:52 -0700796 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800797 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800798 msg_perr("FAILED: Cannot get the WP_RO region info: "
799 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800800 return 1;
801 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900802 start = info.offset;
803 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800804 } else {
805 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
806 __func__);
807 }
808
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800809 /*
810 * If neither RO_NOW or ALL_NOW is set, it means write protect is
811 * NOT active now.
812 */
813 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
814 start = len = 0;
815
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800816 /* Remove the SPI-style messages. */
817 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
818 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
819 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800820 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800821 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800822 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800823 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800824
825 return 0;
826}
827
David Hendrickse5454932013-11-04 18:16:11 -0800828/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700829int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800830{
831 struct ec_params_hello request;
832 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -0800833 int rc = 0;
834
835 /* Say hello to EC. */
836 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
837 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
838 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800839 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -0800840 sizeof(request), &response, sizeof(response));
841 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
842
843 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
844 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
845 "rc=%d, request=0x%x response=0x%x\n",
846 rc, request.in_data, response.out_data);
847 return 1;
848 }
849
850 return 0;
851}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800852
David Hendricksd13d90d2016-08-09 17:00:52 -0700853void cros_ec_set_max_size(struct cros_ec_priv *priv,
854 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700855 struct ec_response_get_protocol_info info;
856 int rc = 0;
857 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800858 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700859 &info, sizeof(info));
860 msg_pdbg("%s: rc:%d\n", __func__, rc);
861
862 if (rc == sizeof(info)) {
863 op->max_data_write = min(op->max_data_write,
864 info.max_request_packet_size -
865 sizeof(struct ec_host_request));
866 op->max_data_read = min(op->max_data_read,
867 info.max_response_packet_size -
868 sizeof(struct ec_host_response));
869 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
870 op->max_data_write, op->max_data_read);
871 }
872}
873
David Hendricks14935fe2014-08-14 17:38:24 -0700874
875/*
David Hendricks052446b2014-09-11 11:26:51 -0700876 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -0700877 *
878 * This function parses programmer parameters from the command line. Since
879 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
880 * run during internal programmer initialization.
881 */
882int cros_ec_parse_param(struct cros_ec_priv *priv)
883{
884 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -0700885
David Hendricksd13d90d2016-08-09 17:00:52 -0700886 p = extract_programmer_param("dev");
David Hendricks14935fe2014-08-14 17:38:24 -0700887 if (p) {
888 unsigned int index;
889 char *endptr = NULL;
890
891 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800892 /*
893 * For backward compatibility, check if the index is
894 * a number: 0: main EC, 1: PD
895 * works only on Samus.
896 */
David Hendricks14935fe2014-08-14 17:38:24 -0700897 index = strtoul(p, &endptr, 10);
898 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
899 msg_perr("Invalid argument: \"%s\"\n", p);
900 return 1;
901 }
902
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800903 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -0700904 msg_perr("%s: Invalid device index\n", __func__);
905 return 1;
906 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800907 priv->dev = ec_type[index];
908 msg_pdbg("Target %s used\n", priv->dev);
909 }
David Hendricks14935fe2014-08-14 17:38:24 -0700910
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800911 p = extract_programmer_param("type");
912 if (p) {
913 unsigned int index;
914 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
915 if (!strcmp(p, ec_type[index]))
916 break;
917 if (index == ARRAY_SIZE(ec_type)) {
918 msg_perr("Invalid argument: \"%s\"\n", p);
919 return 1;
920 }
921 priv->dev = ec_type[index];
922 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -0700923 }
924
Duncan Laurie84328722014-09-10 23:25:01 -0700925 p = extract_programmer_param("block");
926 if (p) {
927 unsigned int block;
928 char *endptr = NULL;
929
930 errno = 0;
931 block = strtoul(p, &endptr, 0);
932 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
933 msg_perr("Invalid argument: \"%s\"\n", p);
934 return 1;
935 }
936
937 if (block <= 0) {
938 msg_perr("%s: Invalid block size\n", __func__);
939 return 1;
940 }
941
942 msg_pdbg("Override block size to 0x%x\n", block);
943 priv->erase_block_size = block;
944 }
945
David Hendricks14935fe2014-08-14 17:38:24 -0700946 return 0;
947}
948
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700949int cros_ec_probe_size(struct flashctx *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800950 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800951 struct ec_response_flash_info info;
David Hendricks194b3bb2013-07-16 14:32:26 -0700952 struct ec_response_get_chip_info chip_info;
David Hendricksd13d90d2016-08-09 17:00:52 -0700953 struct cros_ec_priv *priv = (struct cros_ec_priv *)flash->pgm->opaque.data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800954 struct block_eraser *eraser;
955 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -0700956 .list_ranges = cros_ec_list_ranges,
957 .set_range = cros_ec_set_range,
958 .enable = cros_ec_enable_writeprotect,
959 .disable = cros_ec_disable_writeprotect,
960 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800961 };
962
David Hendricksd13d90d2016-08-09 17:00:52 -0700963 rc = priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -0700964 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800965 if (rc < 0) {
966 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
967 return 0;
968 }
David Hendricksd13d90d2016-08-09 17:00:52 -0700969 rc = cros_ec_get_current_image(priv);
Simon Glass01c11672013-07-01 18:03:33 +0900970 if (rc < 0) {
971 msg_perr("%s(): Failed to probe (no current image): %d\n",
972 __func__, rc);
973 return 0;
974 }
David Hendricksd13d90d2016-08-09 17:00:52 -0700975 priv->current_image = rc;
976 priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800977
978 flash->total_size = info.flash_size / 1024;
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700979 flash->page_size = flash->pgm->opaque.max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800980 flash->tested = TEST_OK_PREW;
981 eraser = &flash->block_erasers[0];
Duncan Laurie84328722014-09-10 23:25:01 -0700982
983 /* Allow overriding the erase block size in case EC is incorrect */
David Hendricksd13d90d2016-08-09 17:00:52 -0700984 if (priv->erase_block_size > 0)
985 eraser->eraseblocks[0].size = priv->erase_block_size;
Duncan Laurie84328722014-09-10 23:25:01 -0700986 else
987 eraser->eraseblocks[0].size = info.erase_block_size;
988
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800989 eraser->eraseblocks[0].count = info.flash_size /
990 eraser->eraseblocks[0].size;
991 flash->wp = &wp;
992
David Hendricks194b3bb2013-07-16 14:32:26 -0700993 /*
994 * Some STM32 variants erase bits to 0. For now, assume that this
995 * applies to STM32L parts.
996 *
997 * FIXME: This info will eventually be exposed via some EC command.
998 * See chrome-os-partner:20973.
999 */
David Hendricksd13d90d2016-08-09 17:00:52 -07001000 rc = priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -07001001 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -07001002 if (rc < 0) {
1003 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
1004 return 0;
1005 }
1006 if (!strncmp(chip_info.name, "stm32l", 6))
1007 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
1008
David Hendricksd13d90d2016-08-09 17:00:52 -07001009 rc = set_ideal_write_size(priv);
David Hendricksfbd5e6d2014-08-21 15:01:43 -07001010 if (rc < 0) {
1011 msg_perr("%s(): Unable to set write size\n", __func__);
1012 return 0;
1013 }
David Hendricksf9461c72013-07-11 19:02:13 -07001014
Simon Glassc453a642013-07-01 18:08:53 +09001015 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1016 * so we need to be careful about using these enums as array indices */
David Hendricksd13d90d2016-08-09 17:00:52 -07001017 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RO,
1018 &priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001019 if (rc) {
1020 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1021 __func__, rc);
1022 return 0;
1023 }
1024
David Hendricksd13d90d2016-08-09 17:00:52 -07001025 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RW,
1026 &priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001027 if (rc) {
1028 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1029 __func__, rc);
1030 return 0;
1031 }
1032
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001033 return 1;
1034};