blob: 260ed9befa59f5526ea696c3b282422fa36200b5 [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
David Hendricks14935fe2014-08-14 17:38:24 -070065#define DEV(priv) (priv->dev_index << 14)
66
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080067/* 1 if we want the flashrom to call erase_and_write_flash() again. */
68static int need_2nd_pass = 0;
69
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080070/* 1 if we want the flashrom to try jumping to new firmware after update. */
71static int try_latest_firmware = 0;
72
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080073/* The range of each firmware copy from the image file to update.
74 * But re-define the .flags as the valid flag to indicate the firmware is
75 * new or not (if flags = 1).
76 */
77static struct fmap_area fwcopy[4]; // [0] is not used.
78
79/* The names of enum lpc_current_image to match in FMAP area names. */
David Hendricksbf8c4dd2012-07-19 12:13:17 -070080static const char *sections[3] = {
81 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
82 "EC_RO",
83 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080084};
85
Simon Glassc453a642013-07-01 18:08:53 +090086/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
87 * the number of regions */
88static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080089
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080090/* Given the range not able to update, mark the corresponding
91 * firmware as old.
92 */
David Hendricksb907de32014-08-11 16:47:09 -070093static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080094{
95 int i;
96
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080097 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080098 struct fmap_area *fw = &fwcopy[i];
99 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
100 (fw->offset >= addr && (fw->offset < addr + len))) {
101 msg_pdbg("Mark firmware [%s] as old.\n",
102 sections[i]);
103 fw->flags = 0; // mark as old
104 }
105 }
106}
107
108
David Hendricksb907de32014-08-11 16:47:09 -0700109static int cros_ec_get_current_image(struct cros_ec_priv *priv)
Simon Glass01c11672013-07-01 18:03:33 +0900110{
111 struct ec_response_get_version resp;
112 int rc;
113
David Hendricks14935fe2014-08-14 17:38:24 -0700114 rc = priv->ec_command(EC_CMD_GET_VERSION | DEV(priv),
115 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900116 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700117 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900118 return rc;
119 }
120 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700121 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900122 return -1;
123 }
124
125 return resp.current_image;
126}
127
128
David Hendricksb907de32014-08-11 16:47:09 -0700129static int cros_ec_get_region_info(struct cros_ec_priv *priv,
Simon Glass3c01dca2013-07-01 18:07:34 +0900130 enum ec_flash_region region,
131 struct ec_response_flash_region_info *info)
132{
133 struct ec_params_flash_region_info req;
134 struct ec_response_flash_region_info resp;
135 int rc;
136
137 req.region = region;
David Hendricks14935fe2014-08-14 17:38:24 -0700138 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO | DEV(priv),
Simon Glass3c01dca2013-07-01 18:07:34 +0900139 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
140 &resp, sizeof(resp));
141 if (rc < 0) {
142 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
143 return rc;
144 }
145
146 info->offset = resp.offset;
147 info->size = resp.size;
148 return 0;
149}
150
David Hendricksf9461c72013-07-11 19:02:13 -0700151/**
152 * Get the versions of the command supported by the EC.
153 *
154 * @param cmd Command
155 * @param pmask Destination for version mask; will be set to 0 on
156 * error.
157 * @return 0 if success, <0 if error
158 */
159static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
160{
David Hendricksb907de32014-08-11 16:47:09 -0700161 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricksf9461c72013-07-11 19:02:13 -0700162 struct ec_params_get_cmd_versions pver;
163 struct ec_response_get_cmd_versions rver;
164 int rc;
165
166 *pmask = 0;
167
168 pver.cmd = cmd;
David Hendricks14935fe2014-08-14 17:38:24 -0700169 rc = priv->ec_command(EC_CMD_GET_CMD_VERSIONS | DEV(priv), 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700170 &pver, sizeof(pver), &rver, sizeof(rver));
171
172 if (rc < 0)
173 return rc;
174
175 *pmask = rver.version_mask;
176 return rc;
177}
178
179/**
180 * Return non-zero if the EC supports the command and version
181 *
182 * @param cmd Command to check
183 * @param ver Version to check
184 * @return non-zero if command version supported; 0 if not.
185 */
186static int ec_cmd_version_supported(int cmd, int ver)
187{
188 uint32_t mask = 0;
189 int rc;
190
191 rc = ec_get_cmd_versions(cmd, &mask);
192 if (rc < 0)
193 return rc;
194
195 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
196}
197
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700198/* returns 0 if successful or <0 to indicate error */
199static int set_ideal_write_size(struct cros_ec_priv *priv)
David Hendricksf9461c72013-07-11 19:02:13 -0700200{
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700201 int cmd_version, ret;
David Hendricksf9461c72013-07-11 19:02:13 -0700202
David Hendricksfb405f12014-08-19 22:42:30 -0700203 cmd_version = ec_cmd_version_supported(EC_CMD_FLASH_WRITE,
204 EC_VER_FLASH_WRITE);
205 if (cmd_version < 0) {
206 msg_perr("Cannot determine write command version\n");
207 return cmd_version;
208 } else if (cmd_version == 0) {
209 struct ec_response_flash_info info;
David Hendricksf9461c72013-07-11 19:02:13 -0700210
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700211 ret = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv),
David Hendricksfb405f12014-08-19 22:42:30 -0700212 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700213 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700214 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700215 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700216 }
217
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700218 priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE;
David Hendricksfb405f12014-08-19 22:42:30 -0700219 } else {
220 struct ec_response_flash_info_1 info;
221
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700222 ret = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv),
David Hendricksfb405f12014-08-19 22:42:30 -0700223 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700224 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700225 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700226 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700227 }
228
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700229 priv->ideal_write_size = info.write_ideal_size;
David Hendricksf9461c72013-07-11 19:02:13 -0700230 }
231
David Hendricksfb405f12014-08-19 22:42:30 -0700232 return 0;
David Hendricksf9461c72013-07-11 19:02:13 -0700233}
Simon Glass3c01dca2013-07-01 18:07:34 +0900234
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800235/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800236 * then this functions picks a NEW firmware copy and jumps to it. Note that
237 * RO is preferred, then A, finally B.
238 *
239 * Returns 0 for success.
240 */
David Hendricksb907de32014-08-11 16:47:09 -0700241static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800242 struct ec_params_reboot_ec p;
David Hendricksb907de32014-08-11 16:47:09 -0700243 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800244 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700245 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800246
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800247 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
248 * jump to different firmware copy. The second EC_RES_SUCCESS would
249 * set the OBF=1 and the next command cannot be executed.
250 * Thus, we call EC to jump only if the target is different.
251 */
David Hendricksb907de32014-08-11 16:47:09 -0700252 current_image = cros_ec_get_current_image(priv);
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700253 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800254 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700255 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900256 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800257
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800258 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900259
260 /* Translate target --> EC reboot command parameter */
261 switch (target) {
262 case EC_IMAGE_RO:
263 p.cmd = EC_REBOOT_JUMP_RO;
264 break;
265 case EC_IMAGE_RW:
266 p.cmd = EC_REBOOT_JUMP_RW;
267 break;
268 default:
269 /*
270 * If target is unspecified, set EC reboot command to use
271 * a new image. Also set "target" so that it may be used
272 * to update the priv->current_image if jump is successful.
273 */
274 if (fwcopy[EC_IMAGE_RO].flags) {
275 p.cmd = EC_REBOOT_JUMP_RO;
276 target = EC_IMAGE_RO;
277 } else if (fwcopy[EC_IMAGE_RW].flags) {
278 p.cmd = EC_REBOOT_JUMP_RW;
279 target = EC_IMAGE_RW;
280 } else {
281 p.cmd = EC_IMAGE_UNKNOWN;
282 }
283 break;
284 }
285
David Hendricksb907de32014-08-11 16:47:09 -0700286 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800287 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
288
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700289 if (current_image == p.cmd) {
David Hendricksb907de32014-08-11 16:47:09 -0700290 msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]);
Simon Glassc453a642013-07-01 18:08:53 +0900291 priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800292 return 0;
293 }
294
David Hendricks14935fe2014-08-14 17:38:24 -0700295 rc = priv->ec_command(EC_CMD_REBOOT_EC | DEV(priv),
296 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800297 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700298 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800299 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800300 } else {
David Hendricksb907de32014-08-11 16:47:09 -0700301 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800302 rc = EC_RES_SUCCESS;
Simon Glass01c11672013-07-01 18:03:33 +0900303 priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800304 }
305
306 /* Sleep 1 sec to wait the EC re-init. */
307 usleep(1000000);
308
David Hendricksf9461c72013-07-11 19:02:13 -0700309 /* update max data write size in case we're jumping to an EC
310 * firmware with different protocol */
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700311 set_ideal_write_size(priv);
David Hendricksf9461c72013-07-11 19:02:13 -0700312
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800313 return rc;
314}
315
316
317/* Given an image, this function parses FMAP and recognize the firmware
318 * ranges.
319 */
David Hendricksb907de32014-08-11 16:47:09 -0700320int cros_ec_prepare(uint8_t *image, int size) {
321 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800322 struct fmap *fmap;
323 int i, j;
324
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800325 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800326
327 // Parse the fmap in the image file and cache the firmware ranges.
328 fmap = fmap_find_in_memory(image, size);
329 if (!fmap) return 0;
330
331 // Lookup RO/A/B sections in FMAP.
332 for (i = 0; i < fmap->nareas; i++) {
333 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800334 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700335 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800336 msg_pdbg("Found '%s' in image.\n", fa->name);
337 memcpy(&fwcopy[j], fa, sizeof(*fa));
338 fwcopy[j].flags = 1; // mark as new
339 }
340 }
341 }
342
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800343 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700344 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800345 */
David Hendricksb907de32014-08-11 16:47:09 -0700346 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800347}
348
349
350/* Returns >0 if we need 2nd pass of erase_and_write_flash().
351 * <0 if we cannot jump to any firmware copy.
352 * ==0 if no more pass is needed.
353 *
354 * This function also jumps to new-updated firmware copy before return >0.
355 */
David Hendricksb907de32014-08-11 16:47:09 -0700356int cros_ec_need_2nd_pass(void) {
357 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricks7cfbd022012-05-20 17:25:51 -0700358
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800359 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800360
361 if (need_2nd_pass) {
David Hendricksb907de32014-08-11 16:47:09 -0700362 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800363 return -1;
364 }
365 }
366
367 return need_2nd_pass;
368}
369
370
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800371/* Returns 0 for success.
372 *
373 * Try latest firmware: B > A > RO
374 *
David Hendricksb907de32014-08-11 16:47:09 -0700375 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800376 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700377 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800378 */
David Hendricksb907de32014-08-11 16:47:09 -0700379int cros_ec_finish(void) {
380 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800381
382 if (!(priv && priv->detected)) return 0;
383
384 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800385 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksb907de32014-08-11 16:47:09 -0700386 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
387 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800388 }
389
390 return 0;
391}
392
393
David Hendricksb907de32014-08-11 16:47:09 -0700394int cros_ec_read(struct flashchip *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800395 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800396 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800397 struct ec_params_flash_read p;
David Hendricksb907de32014-08-11 16:47:09 -0700398 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700399 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800400 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700401 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800402
David Hendricks133083b2012-07-17 20:39:38 -0700403 while (offset < readcnt) {
404 count = min(maxlen, readcnt - offset);
405 p.offset = blockaddr + offset;
406 p.size = count;
David Hendricks14935fe2014-08-14 17:38:24 -0700407 rc = priv->ec_command(EC_CMD_FLASH_READ | DEV(priv),
408 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800409 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700410 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700411 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800412 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800413 } else {
414 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800415 }
416
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800417 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700418 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800419 }
420
421 return rc;
422}
423
424
Simon Glassc453a642013-07-01 18:08:53 +0900425/*
426 * returns 0 to indicate area does not overlap current EC image
427 * returns 1 to indicate area overlaps current EC image or error
428 */
David Hendricksb907de32014-08-11 16:47:09 -0700429static int in_current_image(struct cros_ec_priv *priv,
Simon Glassc453a642013-07-01 18:08:53 +0900430 unsigned int addr, unsigned int len)
431{
432 int ret;
433 enum ec_current_image image;
434 uint32_t region_offset;
435 uint32_t region_size;
436
437 image = priv->current_image;
438 region_offset = priv->region[image].offset;
439 region_size = priv->region[image].size;
440
441 if ((addr + len - 1 < region_offset) ||
442 (addr > region_offset + region_size - 1)) {
443 return 0;
444 }
445 return 1;
446}
447
448
David Hendricksb907de32014-08-11 16:47:09 -0700449int cros_ec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800450 unsigned int blockaddr,
451 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800452 struct ec_params_flash_erase erase;
David Hendricksb907de32014-08-11 16:47:09 -0700453 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800454 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800455
Simon Glassc453a642013-07-01 18:08:53 +0900456 if (in_current_image(priv, blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700457 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900458 need_2nd_pass = 1;
459 return ACCESS_DENIED;
460 }
461
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800462 erase.offset = blockaddr;
463 erase.size = len;
David Hendricks14935fe2014-08-14 17:38:24 -0700464 rc = priv->ec_command(EC_CMD_FLASH_ERASE | DEV(priv),
465 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800466 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800467 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700468 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800469 need_2nd_pass = 1;
470 return ACCESS_DENIED;
471 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800472 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700473 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800474 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800475 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800476 } else {
477 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800478 }
479
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800480#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800481 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800482#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800483 return rc;
484}
485
486
David Hendricksb907de32014-08-11 16:47:09 -0700487int cros_ec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800488 unsigned int nbytes) {
489 int i, rc = 0;
490 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800491 struct ec_params_flash_write p;
David Hendricksb907de32014-08-11 16:47:09 -0700492 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricks2d6db772013-07-10 21:07:48 -0700493 uint8_t *packet;
494
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700495 packet = malloc(sizeof(p) + priv->ideal_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700496 if (!packet)
497 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800498
499 for (i = 0; i < nbytes; i += written) {
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700500 written = min(nbytes - i, priv->ideal_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800501 p.offset = addr + i;
502 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900503
504 if (in_current_image(priv, p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700505 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900506 need_2nd_pass = 1;
507 return ACCESS_DENIED;
508 }
509
David Hendricks2d6db772013-07-10 21:07:48 -0700510 memcpy(packet, &p, sizeof(p));
511 memcpy(packet + sizeof(p), &buf[i], written);
David Hendricks14935fe2014-08-14 17:38:24 -0700512 rc = priv->ec_command(EC_CMD_FLASH_WRITE | DEV(priv),
513 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700514
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800515 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800516 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700517 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800518 need_2nd_pass = 1;
519 return ACCESS_DENIED;
520 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800521
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800522 if (rc < 0) break;
523 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800524 }
525
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800526#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800527 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800528#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700529 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800530 return rc;
531}
532
533
David Hendricksb907de32014-08-11 16:47:09 -0700534static int cros_ec_list_ranges(const struct flashchip *flash) {
535 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900536 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800537 int rc;
538
David Hendricksb907de32014-08-11 16:47:09 -0700539 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800540 if (rc < 0) {
541 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
542 return 1;
543 }
544
545 msg_pinfo("Supported write protect range:\n");
546 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900547 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
548 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800549
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800550 return 0;
551}
552
553
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800554/*
555 * Helper function for flash protection.
556 *
557 * On EC API v1, the EC write protection has been simplified to one-bit:
558 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
559 * or disabled. However, this is different from the SPI-style write protect
560 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
561 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
562 *
563 * SRP Range | PROTECT_RO_AT_BOOT
564 * 0 0 | 0
565 * 0 non-zero | 1
566 * 1 0 | 1
567 * 1 non-zero | 1
568 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800569 *
570 * Besides, to make the protection take effect as soon as possible, we
571 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
572 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800573 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800574static int set_wp(int enable) {
David Hendricksb907de32014-08-11 16:47:09 -0700575 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800576 struct ec_params_flash_protect p;
577 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800578 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
579 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
580 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800581 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800582
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800583 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800584 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800585 p.mask = (ro_at_boot_flag | ro_now_flag);
586 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
David Hendricks14935fe2014-08-14 17:38:24 -0700587 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
588 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800589 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800590 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
591 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800592 return 1;
593 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800594
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800595 /* Read back */
596 memset(&p, 0, sizeof(p));
David Hendricks14935fe2014-08-14 17:38:24 -0700597 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
598 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800599 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800600 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
601 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800602 return 1;
603 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800604
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800605 if (!enable) {
606 /* The disable case is easier to check. */
607 if (r.flags & ro_at_boot_flag) {
608 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
609 return 1;
610 } else if (r.flags & ro_now_flag) {
611 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
612 need_an_ec_cold_reset = 1;
613 goto exit;
614 }
615
616 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
617 return 0;
618 }
619
620 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
621 if (r.flags & ro_at_boot_flag) {
622 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
623 } else {
624 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
625 return 1;
626 }
627
628 /* Then, we check if the protection has been activated. */
629 if (r.flags & ro_now_flag) {
630 /* Good, RO_NOW is set. */
631 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
632 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
633 struct ec_params_reboot_ec reboot;
634
635 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
636
637 memset(&p, 0, sizeof(p));
638 p.mask = EC_FLASH_PROTECT_ALL_NOW;
639 p.flags = EC_FLASH_PROTECT_ALL_NOW;
David Hendricks14935fe2014-08-14 17:38:24 -0700640 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800641 EC_VER_FLASH_PROTECT,
642 &p, sizeof(p), &r, sizeof(r));
643 if (rc < 0) {
644 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
645 return 1;
646 }
647
648 /* Read back */
649 memset(&p, 0, sizeof(p));
David Hendricks14935fe2014-08-14 17:38:24 -0700650 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800651 EC_VER_FLASH_PROTECT,
652 &p, sizeof(p), &r, sizeof(r));
653 if (rc < 0) {
654 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
655 return 1;
656 }
657
658 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
659 msg_perr("FAILED: ALL_NOW is not set.\n");
660 need_an_ec_cold_reset = 1;
661 goto exit;
662 }
663
664 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
665
666 /*
667 * Our goal is to protect the RO ASAP. The entire protection
668 * is just a workaround for platform not supporting RO_NOW.
669 * It has side-effect that the RW is also protected and leads
670 * the RW update failed. So, we arrange an EC code reset to
671 * unlock RW ASAP.
672 */
673 memset(&reboot, 0, sizeof(reboot));
674 reboot.cmd = EC_REBOOT_COLD;
675 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
David Hendricks14935fe2014-08-14 17:38:24 -0700676 rc = priv->ec_command(EC_CMD_REBOOT_EC | DEV(priv),
677 0, &reboot, sizeof(reboot), NULL, 0);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800678 if (rc < 0) {
679 msg_perr("WARN: Cannot arrange a cold reset at next "
680 "shutdown to unlock entire protect.\n");
681 msg_perr(" But you can do it manually.\n");
682 } else {
683 msg_pdbg("INFO: A cold reset is arranged at next "
684 "shutdown.\n");
685 }
686
687 } else {
688 msg_perr("FAILED: RO_NOW is not set.\n");
689 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
690 "make write protection active now.\n");
691 need_an_ec_cold_reset = 1;
692 }
693
694exit:
695 if (need_an_ec_cold_reset) {
696 msg_perr("FAILED: You may need a reboot to take effect of "
697 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800698 return 1;
699 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800700
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800701 return 0;
702}
703
David Hendricksb907de32014-08-11 16:47:09 -0700704static int cros_ec_set_range(const struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800705 unsigned int start, unsigned int len) {
David Hendricksb907de32014-08-11 16:47:09 -0700706 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900707 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800708 int rc;
709
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800710 /* Check if the given range is supported */
David Hendricksb907de32014-08-11 16:47:09 -0700711 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800712 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800713 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800714 return 1;
715 }
716 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900717 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800718 /* pass */
719 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800720 msg_perr("FAILED: Unsupported write protection range "
721 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800722 msg_perr("Currently supported range:\n");
723 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900724 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
725 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800726 return 1;
727 }
728
729 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800730}
731
732
David Hendricksb907de32014-08-11 16:47:09 -0700733static int cros_ec_enable_writeprotect(const struct flashchip *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700734 enum wp_mode wp_mode) {
735 int ret;
736
737 switch (wp_mode) {
738 case WP_MODE_HARDWARE:
739 ret = set_wp(1);
740 break;
741 default:
742 msg_perr("%s():%d Unsupported write-protection mode\n",
743 __func__, __LINE__);
744 ret = 1;
745 break;
746 }
747
748 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800749}
750
751
David Hendricksb907de32014-08-11 16:47:09 -0700752static int cros_ec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800753 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800754}
755
756
David Hendricksb907de32014-08-11 16:47:09 -0700757static int cros_ec_wp_status(const struct flashchip *flash) {
758 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800759 struct ec_params_flash_protect p;
760 struct ec_response_flash_protect r;
761 int start, len; /* wp range */
762 int enabled;
763 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800764
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800765 memset(&p, 0, sizeof(p));
David Hendricks14935fe2014-08-14 17:38:24 -0700766 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
767 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800768 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800769 msg_perr("FAILED: Cannot get the write protection status: %d\n",
770 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800771 return 1;
772 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700773 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800774 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800775 return 1;
776 }
777
778 start = len = 0;
779 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900780 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800781
782 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
783 __func__);
David Hendricksb907de32014-08-11 16:47:09 -0700784 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800785 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800786 msg_perr("FAILED: Cannot get the WP_RO region info: "
787 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800788 return 1;
789 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900790 start = info.offset;
791 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800792 } else {
793 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
794 __func__);
795 }
796
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800797 /*
798 * If neither RO_NOW or ALL_NOW is set, it means write protect is
799 * NOT active now.
800 */
801 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
802 start = len = 0;
803
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800804 /* Remove the SPI-style messages. */
805 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
806 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
807 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800808 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800809 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800810 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800811 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800812
813 return 0;
814}
815
David Hendrickse5454932013-11-04 18:16:11 -0800816/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700817int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800818{
819 struct ec_params_hello request;
820 struct ec_response_hello response;
821 struct ec_response_proto_version proto;
822 int rc = 0;
823
824 /* Say hello to EC. */
825 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
826 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
827 __func__, request.in_data);
David Hendricks14935fe2014-08-14 17:38:24 -0700828 rc = priv->ec_command(EC_CMD_HELLO | DEV(priv), 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -0800829 sizeof(request), &response, sizeof(response));
830 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
831
832 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
833 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
834 "rc=%d, request=0x%x response=0x%x\n",
835 rc, request.in_data, response.out_data);
836 return 1;
837 }
838
839 return 0;
840}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800841
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700842void cros_ec_set_max_size(struct cros_ec_priv *priv,
843 struct opaque_programmer *op) {
844 struct ec_response_get_protocol_info info;
845 int rc = 0;
846 msg_pdbg("%s: sending protoinfo command\n", __func__);
847 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO | DEV(priv), 0, NULL, 0,
848 &info, sizeof(info));
849 msg_pdbg("%s: rc:%d\n", __func__, rc);
850
851 if (rc == sizeof(info)) {
852 op->max_data_write = min(op->max_data_write,
853 info.max_request_packet_size -
854 sizeof(struct ec_host_request));
855 op->max_data_read = min(op->max_data_read,
856 info.max_response_packet_size -
857 sizeof(struct ec_host_response));
858 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
859 op->max_data_write, op->max_data_read);
860 }
861}
862
David Hendricks14935fe2014-08-14 17:38:24 -0700863
864/*
David Hendricks052446b2014-09-11 11:26:51 -0700865 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -0700866 *
867 * This function parses programmer parameters from the command line. Since
868 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
869 * run during internal programmer initialization.
870 */
871int cros_ec_parse_param(struct cros_ec_priv *priv)
872{
873 char *p;
874
875 p = extract_programmer_param("dev");
876 if (p) {
877 unsigned int index;
878 char *endptr = NULL;
879
880 errno = 0;
881 index = strtoul(p, &endptr, 10);
882 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
883 msg_perr("Invalid argument: \"%s\"\n", p);
884 return 1;
885 }
886
887 if (index > 3) {
888 msg_perr("%s: Invalid device index\n", __func__);
889 return 1;
890 }
891
892 priv->dev_index = index;
893 }
894
895 return 0;
896}
897
David Hendricksb907de32014-08-11 16:47:09 -0700898int cros_ec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800899 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800900 struct ec_response_flash_info info;
David Hendricks194b3bb2013-07-16 14:32:26 -0700901 struct ec_response_get_chip_info chip_info;
David Hendricksb907de32014-08-11 16:47:09 -0700902 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800903 struct block_eraser *eraser;
904 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -0700905 .list_ranges = cros_ec_list_ranges,
906 .set_range = cros_ec_set_range,
907 .enable = cros_ec_enable_writeprotect,
908 .disable = cros_ec_disable_writeprotect,
909 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800910 };
911
David Hendricks14935fe2014-08-14 17:38:24 -0700912 if (priv->dev_index > 0) {
913 if (cros_ec_test(priv)) {
914 msg_perr("%s: Failed to say \"hello\" to device %d\n",
915 __func__, priv->dev_index);
916 return 1;
917 }
918 }
919
920 rc = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv),
921 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800922 if (rc < 0) {
923 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
924 return 0;
925 }
David Hendricksb907de32014-08-11 16:47:09 -0700926 rc = cros_ec_get_current_image(priv);
Simon Glass01c11672013-07-01 18:03:33 +0900927 if (rc < 0) {
928 msg_perr("%s(): Failed to probe (no current image): %d\n",
929 __func__, rc);
930 return 0;
931 }
932 priv->current_image = rc;
Simon Glassc453a642013-07-01 18:08:53 +0900933 priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800934
935 flash->total_size = info.flash_size / 1024;
Simon Glass144fc2c2013-07-16 09:33:35 -0600936 flash->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800937 flash->tested = TEST_OK_PREW;
938 eraser = &flash->block_erasers[0];
939 eraser->eraseblocks[0].size = info.erase_block_size;
940 eraser->eraseblocks[0].count = info.flash_size /
941 eraser->eraseblocks[0].size;
942 flash->wp = &wp;
943
David Hendricks194b3bb2013-07-16 14:32:26 -0700944 /*
945 * Some STM32 variants erase bits to 0. For now, assume that this
946 * applies to STM32L parts.
947 *
948 * FIXME: This info will eventually be exposed via some EC command.
949 * See chrome-os-partner:20973.
950 */
David Hendricks14935fe2014-08-14 17:38:24 -0700951 rc = priv->ec_command(EC_CMD_GET_CHIP_INFO | DEV(priv),
952 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -0700953 if (rc < 0) {
954 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
955 return 0;
956 }
957 if (!strncmp(chip_info.name, "stm32l", 6))
958 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
959
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700960 rc = set_ideal_write_size(priv);
961 if (rc < 0) {
962 msg_perr("%s(): Unable to set write size\n", __func__);
963 return 0;
964 }
David Hendricksf9461c72013-07-11 19:02:13 -0700965
Simon Glassc453a642013-07-01 18:08:53 +0900966 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
967 * so we need to be careful about using these enums as array indices */
David Hendricksb907de32014-08-11 16:47:09 -0700968 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RO,
Simon Glassc453a642013-07-01 18:08:53 +0900969 &priv->region[EC_IMAGE_RO]);
970 if (rc) {
971 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
972 __func__, rc);
973 return 0;
974 }
975
David Hendricksb907de32014-08-11 16:47:09 -0700976 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RW,
Simon Glassc453a642013-07-01 18:08:53 +0900977 &priv->region[EC_IMAGE_RW]);
978 if (rc) {
979 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
980 __func__, rc);
981 return 0;
982 }
983
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800984 return 1;
985};