blob: 9953747dc812de0096bb5145b624274c239c2a13 [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 Hendricksb907de32014-08-11 16:47:09 -0700198static int cros_ec_set_max_write_size(void)
David Hendricksf9461c72013-07-11 19:02:13 -0700199{
200 int rc;
201 struct ec_response_flash_info info;
David Hendricksb907de32014-08-11 16:47:09 -0700202 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricksf9461c72013-07-11 19:02:13 -0700203 unsigned int pdata_max_size;
204
205 /*
206 * Determine whether we can use version 1 of the command with more
207 * data, or only version 0.
208 */
209 rc = ec_cmd_version_supported(EC_CMD_FLASH_WRITE, EC_VER_FLASH_WRITE);
210 if (rc < 0)
211 return rc;
212 else if (rc == 0)
213 pdata_max_size = EC_FLASH_WRITE_VER0_SIZE;
214 else
215 pdata_max_size = EC_PROTO2_MAX_PARAM_SIZE - 8;
216
David Hendricks14935fe2014-08-14 17:38:24 -0700217 rc = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv),
218 0, NULL, 0, &info, sizeof(info));
David Hendricksf9461c72013-07-11 19:02:13 -0700219 if (rc < 0) {
220 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
221 return rc;
222 }
223
224 opaque_programmer->max_data_write =
225 (pdata_max_size/info.write_block_size) * info.write_block_size;
226 return rc;
227}
Simon Glass3c01dca2013-07-01 18:07:34 +0900228
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800229/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800230 * then this functions picks a NEW firmware copy and jumps to it. Note that
231 * RO is preferred, then A, finally B.
232 *
233 * Returns 0 for success.
234 */
David Hendricksb907de32014-08-11 16:47:09 -0700235static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800236 struct ec_params_reboot_ec p;
David Hendricksb907de32014-08-11 16:47:09 -0700237 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800238 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700239 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800240
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800241 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
242 * jump to different firmware copy. The second EC_RES_SUCCESS would
243 * set the OBF=1 and the next command cannot be executed.
244 * Thus, we call EC to jump only if the target is different.
245 */
David Hendricksb907de32014-08-11 16:47:09 -0700246 current_image = cros_ec_get_current_image(priv);
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700247 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800248 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700249 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900250 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800251
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800252 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900253
254 /* Translate target --> EC reboot command parameter */
255 switch (target) {
256 case EC_IMAGE_RO:
257 p.cmd = EC_REBOOT_JUMP_RO;
258 break;
259 case EC_IMAGE_RW:
260 p.cmd = EC_REBOOT_JUMP_RW;
261 break;
262 default:
263 /*
264 * If target is unspecified, set EC reboot command to use
265 * a new image. Also set "target" so that it may be used
266 * to update the priv->current_image if jump is successful.
267 */
268 if (fwcopy[EC_IMAGE_RO].flags) {
269 p.cmd = EC_REBOOT_JUMP_RO;
270 target = EC_IMAGE_RO;
271 } else if (fwcopy[EC_IMAGE_RW].flags) {
272 p.cmd = EC_REBOOT_JUMP_RW;
273 target = EC_IMAGE_RW;
274 } else {
275 p.cmd = EC_IMAGE_UNKNOWN;
276 }
277 break;
278 }
279
David Hendricksb907de32014-08-11 16:47:09 -0700280 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800281 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
282
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700283 if (current_image == p.cmd) {
David Hendricksb907de32014-08-11 16:47:09 -0700284 msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]);
Simon Glassc453a642013-07-01 18:08:53 +0900285 priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800286 return 0;
287 }
288
David Hendricks14935fe2014-08-14 17:38:24 -0700289 rc = priv->ec_command(EC_CMD_REBOOT_EC | DEV(priv),
290 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800291 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700292 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800293 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800294 } else {
David Hendricksb907de32014-08-11 16:47:09 -0700295 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800296 rc = EC_RES_SUCCESS;
Simon Glass01c11672013-07-01 18:03:33 +0900297 priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800298 }
299
300 /* Sleep 1 sec to wait the EC re-init. */
301 usleep(1000000);
302
David Hendricksf9461c72013-07-11 19:02:13 -0700303 /* update max data write size in case we're jumping to an EC
304 * firmware with different protocol */
David Hendricksb907de32014-08-11 16:47:09 -0700305 cros_ec_set_max_write_size();
David Hendricksf9461c72013-07-11 19:02:13 -0700306
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800307 return rc;
308}
309
310
311/* Given an image, this function parses FMAP and recognize the firmware
312 * ranges.
313 */
David Hendricksb907de32014-08-11 16:47:09 -0700314int cros_ec_prepare(uint8_t *image, int size) {
315 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800316 struct fmap *fmap;
317 int i, j;
318
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800319 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800320
321 // Parse the fmap in the image file and cache the firmware ranges.
322 fmap = fmap_find_in_memory(image, size);
323 if (!fmap) return 0;
324
325 // Lookup RO/A/B sections in FMAP.
326 for (i = 0; i < fmap->nareas; i++) {
327 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800328 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700329 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800330 msg_pdbg("Found '%s' in image.\n", fa->name);
331 memcpy(&fwcopy[j], fa, sizeof(*fa));
332 fwcopy[j].flags = 1; // mark as new
333 }
334 }
335 }
336
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800337 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700338 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800339 */
David Hendricksb907de32014-08-11 16:47:09 -0700340 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800341}
342
343
344/* Returns >0 if we need 2nd pass of erase_and_write_flash().
345 * <0 if we cannot jump to any firmware copy.
346 * ==0 if no more pass is needed.
347 *
348 * This function also jumps to new-updated firmware copy before return >0.
349 */
David Hendricksb907de32014-08-11 16:47:09 -0700350int cros_ec_need_2nd_pass(void) {
351 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricks7cfbd022012-05-20 17:25:51 -0700352
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800353 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800354
355 if (need_2nd_pass) {
David Hendricksb907de32014-08-11 16:47:09 -0700356 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800357 return -1;
358 }
359 }
360
361 return need_2nd_pass;
362}
363
364
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800365/* Returns 0 for success.
366 *
367 * Try latest firmware: B > A > RO
368 *
David Hendricksb907de32014-08-11 16:47:09 -0700369 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800370 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700371 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800372 */
David Hendricksb907de32014-08-11 16:47:09 -0700373int cros_ec_finish(void) {
374 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800375
376 if (!(priv && priv->detected)) return 0;
377
378 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800379 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksb907de32014-08-11 16:47:09 -0700380 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
381 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800382 }
383
384 return 0;
385}
386
387
David Hendricksb907de32014-08-11 16:47:09 -0700388int cros_ec_read(struct flashchip *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800389 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800390 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800391 struct ec_params_flash_read p;
David Hendricksb907de32014-08-11 16:47:09 -0700392 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700393 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800394 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700395 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800396
David Hendricks133083b2012-07-17 20:39:38 -0700397 while (offset < readcnt) {
398 count = min(maxlen, readcnt - offset);
399 p.offset = blockaddr + offset;
400 p.size = count;
David Hendricks14935fe2014-08-14 17:38:24 -0700401 rc = priv->ec_command(EC_CMD_FLASH_READ | DEV(priv),
402 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800403 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700404 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700405 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800406 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800407 } else {
408 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800409 }
410
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800411 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700412 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800413 }
414
415 return rc;
416}
417
418
Simon Glassc453a642013-07-01 18:08:53 +0900419/*
420 * returns 0 to indicate area does not overlap current EC image
421 * returns 1 to indicate area overlaps current EC image or error
422 */
David Hendricksb907de32014-08-11 16:47:09 -0700423static int in_current_image(struct cros_ec_priv *priv,
Simon Glassc453a642013-07-01 18:08:53 +0900424 unsigned int addr, unsigned int len)
425{
426 int ret;
427 enum ec_current_image image;
428 uint32_t region_offset;
429 uint32_t region_size;
430
431 image = priv->current_image;
432 region_offset = priv->region[image].offset;
433 region_size = priv->region[image].size;
434
435 if ((addr + len - 1 < region_offset) ||
436 (addr > region_offset + region_size - 1)) {
437 return 0;
438 }
439 return 1;
440}
441
442
David Hendricksb907de32014-08-11 16:47:09 -0700443int cros_ec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800444 unsigned int blockaddr,
445 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800446 struct ec_params_flash_erase erase;
David Hendricksb907de32014-08-11 16:47:09 -0700447 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800448 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800449
Simon Glassc453a642013-07-01 18:08:53 +0900450 if (in_current_image(priv, blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700451 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900452 need_2nd_pass = 1;
453 return ACCESS_DENIED;
454 }
455
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800456 erase.offset = blockaddr;
457 erase.size = len;
David Hendricks14935fe2014-08-14 17:38:24 -0700458 rc = priv->ec_command(EC_CMD_FLASH_ERASE | DEV(priv),
459 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800460 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800461 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700462 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800463 need_2nd_pass = 1;
464 return ACCESS_DENIED;
465 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800466 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700467 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800468 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800469 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800470 } else {
471 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800472 }
473
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800474#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800475 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800476#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800477 return rc;
478}
479
480
David Hendricksb907de32014-08-11 16:47:09 -0700481int cros_ec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800482 unsigned int nbytes) {
483 int i, rc = 0;
484 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800485 struct ec_params_flash_write p;
David Hendricksb907de32014-08-11 16:47:09 -0700486 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700487 int maxlen = opaque_programmer->max_data_write;
David Hendricks2d6db772013-07-10 21:07:48 -0700488 uint8_t *packet;
489
490 packet = malloc(sizeof(p) + maxlen);
491 if (!packet)
492 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800493
494 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700495 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800496 p.offset = addr + i;
497 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900498
499 if (in_current_image(priv, p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700500 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900501 need_2nd_pass = 1;
502 return ACCESS_DENIED;
503 }
504
David Hendricks2d6db772013-07-10 21:07:48 -0700505 memcpy(packet, &p, sizeof(p));
506 memcpy(packet + sizeof(p), &buf[i], written);
David Hendricks14935fe2014-08-14 17:38:24 -0700507 rc = priv->ec_command(EC_CMD_FLASH_WRITE | DEV(priv),
508 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700509
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800510 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800511 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700512 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800513 need_2nd_pass = 1;
514 return ACCESS_DENIED;
515 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800516
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800517 if (rc < 0) break;
518 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800519 }
520
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800521#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800522 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800523#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700524 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800525 return rc;
526}
527
528
David Hendricksb907de32014-08-11 16:47:09 -0700529static int cros_ec_list_ranges(const struct flashchip *flash) {
530 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900531 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800532 int rc;
533
David Hendricksb907de32014-08-11 16:47:09 -0700534 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800535 if (rc < 0) {
536 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
537 return 1;
538 }
539
540 msg_pinfo("Supported write protect range:\n");
541 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900542 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
543 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800544
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800545 return 0;
546}
547
548
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800549/*
550 * Helper function for flash protection.
551 *
552 * On EC API v1, the EC write protection has been simplified to one-bit:
553 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
554 * or disabled. However, this is different from the SPI-style write protect
555 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
556 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
557 *
558 * SRP Range | PROTECT_RO_AT_BOOT
559 * 0 0 | 0
560 * 0 non-zero | 1
561 * 1 0 | 1
562 * 1 non-zero | 1
563 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800564 *
565 * Besides, to make the protection take effect as soon as possible, we
566 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
567 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800568 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800569static int set_wp(int enable) {
David Hendricksb907de32014-08-11 16:47:09 -0700570 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800571 struct ec_params_flash_protect p;
572 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800573 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
574 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
575 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800576 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800577
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800578 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800579 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800580 p.mask = (ro_at_boot_flag | ro_now_flag);
581 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
David Hendricks14935fe2014-08-14 17:38:24 -0700582 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
583 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800584 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800585 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
586 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800587 return 1;
588 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800589
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800590 /* Read back */
591 memset(&p, 0, sizeof(p));
David Hendricks14935fe2014-08-14 17:38:24 -0700592 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
593 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800594 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800595 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
596 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800597 return 1;
598 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800599
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800600 if (!enable) {
601 /* The disable case is easier to check. */
602 if (r.flags & ro_at_boot_flag) {
603 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
604 return 1;
605 } else if (r.flags & ro_now_flag) {
606 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
607 need_an_ec_cold_reset = 1;
608 goto exit;
609 }
610
611 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
612 return 0;
613 }
614
615 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
616 if (r.flags & ro_at_boot_flag) {
617 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
618 } else {
619 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
620 return 1;
621 }
622
623 /* Then, we check if the protection has been activated. */
624 if (r.flags & ro_now_flag) {
625 /* Good, RO_NOW is set. */
626 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
627 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
628 struct ec_params_reboot_ec reboot;
629
630 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
631
632 memset(&p, 0, sizeof(p));
633 p.mask = EC_FLASH_PROTECT_ALL_NOW;
634 p.flags = EC_FLASH_PROTECT_ALL_NOW;
David Hendricks14935fe2014-08-14 17:38:24 -0700635 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800636 EC_VER_FLASH_PROTECT,
637 &p, sizeof(p), &r, sizeof(r));
638 if (rc < 0) {
639 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
640 return 1;
641 }
642
643 /* Read back */
644 memset(&p, 0, sizeof(p));
David Hendricks14935fe2014-08-14 17:38:24 -0700645 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800646 EC_VER_FLASH_PROTECT,
647 &p, sizeof(p), &r, sizeof(r));
648 if (rc < 0) {
649 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
650 return 1;
651 }
652
653 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
654 msg_perr("FAILED: ALL_NOW is not set.\n");
655 need_an_ec_cold_reset = 1;
656 goto exit;
657 }
658
659 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
660
661 /*
662 * Our goal is to protect the RO ASAP. The entire protection
663 * is just a workaround for platform not supporting RO_NOW.
664 * It has side-effect that the RW is also protected and leads
665 * the RW update failed. So, we arrange an EC code reset to
666 * unlock RW ASAP.
667 */
668 memset(&reboot, 0, sizeof(reboot));
669 reboot.cmd = EC_REBOOT_COLD;
670 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
David Hendricks14935fe2014-08-14 17:38:24 -0700671 rc = priv->ec_command(EC_CMD_REBOOT_EC | DEV(priv),
672 0, &reboot, sizeof(reboot), NULL, 0);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800673 if (rc < 0) {
674 msg_perr("WARN: Cannot arrange a cold reset at next "
675 "shutdown to unlock entire protect.\n");
676 msg_perr(" But you can do it manually.\n");
677 } else {
678 msg_pdbg("INFO: A cold reset is arranged at next "
679 "shutdown.\n");
680 }
681
682 } else {
683 msg_perr("FAILED: RO_NOW is not set.\n");
684 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
685 "make write protection active now.\n");
686 need_an_ec_cold_reset = 1;
687 }
688
689exit:
690 if (need_an_ec_cold_reset) {
691 msg_perr("FAILED: You may need a reboot to take effect of "
692 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800693 return 1;
694 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800695
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800696 return 0;
697}
698
David Hendricksb907de32014-08-11 16:47:09 -0700699static int cros_ec_set_range(const struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800700 unsigned int start, unsigned int len) {
David Hendricksb907de32014-08-11 16:47:09 -0700701 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900702 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800703 int rc;
704
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800705 /* Check if the given range is supported */
David Hendricksb907de32014-08-11 16:47:09 -0700706 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800707 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800708 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800709 return 1;
710 }
711 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900712 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800713 /* pass */
714 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800715 msg_perr("FAILED: Unsupported write protection range "
716 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800717 msg_perr("Currently supported range:\n");
718 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900719 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
720 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800721 return 1;
722 }
723
724 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800725}
726
727
David Hendricksb907de32014-08-11 16:47:09 -0700728static int cros_ec_enable_writeprotect(const struct flashchip *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700729 enum wp_mode wp_mode) {
730 int ret;
731
732 switch (wp_mode) {
733 case WP_MODE_HARDWARE:
734 ret = set_wp(1);
735 break;
736 default:
737 msg_perr("%s():%d Unsupported write-protection mode\n",
738 __func__, __LINE__);
739 ret = 1;
740 break;
741 }
742
743 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800744}
745
746
David Hendricksb907de32014-08-11 16:47:09 -0700747static int cros_ec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800748 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800749}
750
751
David Hendricksb907de32014-08-11 16:47:09 -0700752static int cros_ec_wp_status(const struct flashchip *flash) {
753 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800754 struct ec_params_flash_protect p;
755 struct ec_response_flash_protect r;
756 int start, len; /* wp range */
757 int enabled;
758 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800759
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800760 memset(&p, 0, sizeof(p));
David Hendricks14935fe2014-08-14 17:38:24 -0700761 rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv),
762 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800763 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800764 msg_perr("FAILED: Cannot get the write protection status: %d\n",
765 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800766 return 1;
767 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700768 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800769 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800770 return 1;
771 }
772
773 start = len = 0;
774 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900775 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800776
777 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
778 __func__);
David Hendricksb907de32014-08-11 16:47:09 -0700779 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
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 WP_RO region info: "
782 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800783 return 1;
784 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900785 start = info.offset;
786 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800787 } else {
788 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
789 __func__);
790 }
791
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800792 /*
793 * If neither RO_NOW or ALL_NOW is set, it means write protect is
794 * NOT active now.
795 */
796 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
797 start = len = 0;
798
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800799 /* Remove the SPI-style messages. */
800 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
801 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
802 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800803 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800804 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800805 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800806 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800807
808 return 0;
809}
810
David Hendrickse5454932013-11-04 18:16:11 -0800811/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700812int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800813{
814 struct ec_params_hello request;
815 struct ec_response_hello response;
816 struct ec_response_proto_version proto;
817 int rc = 0;
818
819 /* Say hello to EC. */
820 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
821 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
822 __func__, request.in_data);
David Hendricks14935fe2014-08-14 17:38:24 -0700823 rc = priv->ec_command(EC_CMD_HELLO | DEV(priv), 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -0800824 sizeof(request), &response, sizeof(response));
825 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
826
827 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
828 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
829 "rc=%d, request=0x%x response=0x%x\n",
830 rc, request.in_data, response.out_data);
831 return 1;
832 }
833
834 return 0;
835}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800836
David Hendricks14935fe2014-08-14 17:38:24 -0700837
838/*
839 * Returns 0 to indicate success, non-zero othersize
840 *
841 * This function parses programmer parameters from the command line. Since
842 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
843 * run during internal programmer initialization.
844 */
845int cros_ec_parse_param(struct cros_ec_priv *priv)
846{
847 char *p;
848
849 p = extract_programmer_param("dev");
850 if (p) {
851 unsigned int index;
852 char *endptr = NULL;
853
854 errno = 0;
855 index = strtoul(p, &endptr, 10);
856 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
857 msg_perr("Invalid argument: \"%s\"\n", p);
858 return 1;
859 }
860
861 if (index > 3) {
862 msg_perr("%s: Invalid device index\n", __func__);
863 return 1;
864 }
865
866 priv->dev_index = index;
867 }
868
869 return 0;
870}
871
David Hendricksb907de32014-08-11 16:47:09 -0700872int cros_ec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800873 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800874 struct ec_response_flash_info info;
David Hendricks194b3bb2013-07-16 14:32:26 -0700875 struct ec_response_get_chip_info chip_info;
David Hendricksb907de32014-08-11 16:47:09 -0700876 struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800877 struct block_eraser *eraser;
878 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -0700879 .list_ranges = cros_ec_list_ranges,
880 .set_range = cros_ec_set_range,
881 .enable = cros_ec_enable_writeprotect,
882 .disable = cros_ec_disable_writeprotect,
883 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800884 };
885
David Hendricks14935fe2014-08-14 17:38:24 -0700886 if (priv->dev_index > 0) {
887 if (cros_ec_test(priv)) {
888 msg_perr("%s: Failed to say \"hello\" to device %d\n",
889 __func__, priv->dev_index);
890 return 1;
891 }
892 }
893
894 rc = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv),
895 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800896 if (rc < 0) {
897 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
898 return 0;
899 }
David Hendricksb907de32014-08-11 16:47:09 -0700900 rc = cros_ec_get_current_image(priv);
Simon Glass01c11672013-07-01 18:03:33 +0900901 if (rc < 0) {
902 msg_perr("%s(): Failed to probe (no current image): %d\n",
903 __func__, rc);
904 return 0;
905 }
906 priv->current_image = rc;
Simon Glassc453a642013-07-01 18:08:53 +0900907 priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800908
909 flash->total_size = info.flash_size / 1024;
Simon Glass144fc2c2013-07-16 09:33:35 -0600910 flash->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800911 flash->tested = TEST_OK_PREW;
912 eraser = &flash->block_erasers[0];
913 eraser->eraseblocks[0].size = info.erase_block_size;
914 eraser->eraseblocks[0].count = info.flash_size /
915 eraser->eraseblocks[0].size;
916 flash->wp = &wp;
917
David Hendricks194b3bb2013-07-16 14:32:26 -0700918 /*
919 * Some STM32 variants erase bits to 0. For now, assume that this
920 * applies to STM32L parts.
921 *
922 * FIXME: This info will eventually be exposed via some EC command.
923 * See chrome-os-partner:20973.
924 */
David Hendricks14935fe2014-08-14 17:38:24 -0700925 rc = priv->ec_command(EC_CMD_GET_CHIP_INFO | DEV(priv),
926 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -0700927 if (rc < 0) {
928 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
929 return 0;
930 }
931 if (!strncmp(chip_info.name, "stm32l", 6))
932 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
933
David Hendricksb907de32014-08-11 16:47:09 -0700934 cros_ec_set_max_write_size();
David Hendricksf9461c72013-07-11 19:02:13 -0700935
Simon Glassc453a642013-07-01 18:08:53 +0900936 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
937 * so we need to be careful about using these enums as array indices */
David Hendricksb907de32014-08-11 16:47:09 -0700938 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RO,
Simon Glassc453a642013-07-01 18:08:53 +0900939 &priv->region[EC_IMAGE_RO]);
940 if (rc) {
941 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
942 __func__, rc);
943 return 0;
944 }
945
David Hendricksb907de32014-08-11 16:47:09 -0700946 rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RW,
Simon Glassc453a642013-07-01 18:08:53 +0900947 &priv->region[EC_IMAGE_RW]);
948 if (rc) {
949 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
950 __func__, rc);
951 return 0;
952 }
953
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800954 return 1;
955};