blob: d18895ea104c12fafada8d6993cc51c7a22f7569 [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 */
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080039#include "fmap.h"
David Hendricksa9151312013-07-01 12:21:01 -070040#include "gec.h"
Louis Yung-Chieh Loc0505242012-08-09 23:11:32 +080041#include "gec_lock.h"
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080042#include "gec_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080043#include "programmer.h"
44#include "spi.h"
45#include "writeprotect.h"
46
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080047/* FIXME: used for wp hacks */
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <fcntl.h>
51#include <unistd.h>
52struct wp_data {
53 int enable;
54 unsigned int start;
55 unsigned int len;
56};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080057#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
58
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080059/* If software sync is enabled, then we don't try the latest firmware copy
60 * after updating.
61 */
62#define SOFTWARE_SYNC_ENABLED
63
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080064/* 1 if we want the flashrom to call erase_and_write_flash() again. */
65static int need_2nd_pass = 0;
66
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080067/* 1 if we want the flashrom to try jumping to new firmware after update. */
68static int try_latest_firmware = 0;
69
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080070/* The range of each firmware copy from the image file to update.
71 * But re-define the .flags as the valid flag to indicate the firmware is
72 * new or not (if flags = 1).
73 */
74static struct fmap_area fwcopy[4]; // [0] is not used.
75
76/* The names of enum lpc_current_image to match in FMAP area names. */
David Hendricksbf8c4dd2012-07-19 12:13:17 -070077static const char *sections[3] = {
78 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
79 "EC_RO",
80 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080081};
82
Simon Glassc453a642013-07-01 18:08:53 +090083/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
84 * the number of regions */
85static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080086
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080087/* Given the range not able to update, mark the corresponding
88 * firmware as old.
89 */
90static void gec_invalidate_copy(unsigned int addr, unsigned int len)
91{
92 int i;
93
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080094 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080095 struct fmap_area *fw = &fwcopy[i];
96 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
97 (fw->offset >= addr && (fw->offset < addr + len))) {
98 msg_pdbg("Mark firmware [%s] as old.\n",
99 sections[i]);
100 fw->flags = 0; // mark as old
101 }
102 }
103}
104
105
Simon Glass01c11672013-07-01 18:03:33 +0900106static int gec_get_current_image(struct gec_priv *priv)
107{
108 struct ec_response_get_version resp;
109 int rc;
110
111 rc = priv->ec_command(EC_CMD_GET_VERSION, 0, NULL, 0, &resp,
112 sizeof(resp));
113 if (rc < 0) {
114 msg_perr("GEC cannot get the running copy: rc=%d\n", rc);
115 return rc;
116 }
117 if (resp.current_image == EC_IMAGE_UNKNOWN) {
118 msg_perr("GEC gets unknown running copy\n");
119 return -1;
120 }
121
122 return resp.current_image;
123}
124
125
Simon Glass3c01dca2013-07-01 18:07:34 +0900126static int gec_get_region_info(struct gec_priv *priv,
127 enum ec_flash_region region,
128 struct ec_response_flash_region_info *info)
129{
130 struct ec_params_flash_region_info req;
131 struct ec_response_flash_region_info resp;
132 int rc;
133
134 req.region = region;
135 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
136 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
137 &resp, sizeof(resp));
138 if (rc < 0) {
139 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
140 return rc;
141 }
142
143 info->offset = resp.offset;
144 info->size = resp.size;
145 return 0;
146}
147
148
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800149/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800150 * then this functions picks a NEW firmware copy and jumps to it. Note that
151 * RO is preferred, then A, finally B.
152 *
153 * Returns 0 for success.
154 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800155static int gec_jump_copy(enum ec_current_image target) {
156 struct ec_response_get_version c;
157 struct ec_params_reboot_ec p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700158 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800159 int rc;
160
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800161 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
162 * jump to different firmware copy. The second EC_RES_SUCCESS would
163 * set the OBF=1 and the next command cannot be executed.
164 * Thus, we call EC to jump only if the target is different.
165 */
Simon Glass01c11672013-07-01 18:03:33 +0900166 rc = gec_get_current_image(priv);
167 if (rc < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800168 return 1;
Simon Glassc453a642013-07-01 18:08:53 +0900169 if (rc == target)
170 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800171
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800172 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900173
174 /* Translate target --> EC reboot command parameter */
175 switch (target) {
176 case EC_IMAGE_RO:
177 p.cmd = EC_REBOOT_JUMP_RO;
178 break;
179 case EC_IMAGE_RW:
180 p.cmd = EC_REBOOT_JUMP_RW;
181 break;
182 default:
183 /*
184 * If target is unspecified, set EC reboot command to use
185 * a new image. Also set "target" so that it may be used
186 * to update the priv->current_image if jump is successful.
187 */
188 if (fwcopy[EC_IMAGE_RO].flags) {
189 p.cmd = EC_REBOOT_JUMP_RO;
190 target = EC_IMAGE_RO;
191 } else if (fwcopy[EC_IMAGE_RW].flags) {
192 p.cmd = EC_REBOOT_JUMP_RW;
193 target = EC_IMAGE_RW;
194 } else {
195 p.cmd = EC_IMAGE_UNKNOWN;
196 }
197 break;
198 }
199
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800200 msg_pdbg("GEC is jumping to [%s]\n", sections[p.cmd]);
201 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
202
203 if (c.current_image == p.cmd) {
204 msg_pdbg("GEC is already in [%s]\n", sections[p.cmd]);
Simon Glassc453a642013-07-01 18:08:53 +0900205 priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800206 return 0;
207 }
208
209 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700210 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800211 if (rc < 0) {
212 msg_perr("GEC cannot jump to [%s]:%d\n",
213 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800214 } else {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800215 msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]);
216 rc = EC_RES_SUCCESS;
Simon Glass01c11672013-07-01 18:03:33 +0900217 priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800218 }
219
220 /* Sleep 1 sec to wait the EC re-init. */
221 usleep(1000000);
222
223 return rc;
224}
225
226
227/* Given an image, this function parses FMAP and recognize the firmware
228 * ranges.
229 */
230int gec_prepare(uint8_t *image, int size) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700231 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800232 struct fmap *fmap;
233 int i, j;
234
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800235 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800236
237 // Parse the fmap in the image file and cache the firmware ranges.
238 fmap = fmap_find_in_memory(image, size);
239 if (!fmap) return 0;
240
241 // Lookup RO/A/B sections in FMAP.
242 for (i = 0; i < fmap->nareas; i++) {
243 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800244 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700245 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800246 msg_pdbg("Found '%s' in image.\n", fa->name);
247 memcpy(&fwcopy[j], fa, sizeof(*fa));
248 fwcopy[j].flags = 1; // mark as new
249 }
250 }
251 }
252
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800253 /* Warning: before update, we jump the EC to RO copy. If you want to
254 * change this behavior, please also check the gec_finish().
255 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800256 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800257}
258
259
260/* Returns >0 if we need 2nd pass of erase_and_write_flash().
261 * <0 if we cannot jump to any firmware copy.
262 * ==0 if no more pass is needed.
263 *
264 * This function also jumps to new-updated firmware copy before return >0.
265 */
266int gec_need_2nd_pass(void) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700267 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
268
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800269 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800270
271 if (need_2nd_pass) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800272 if (gec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800273 return -1;
274 }
275 }
276
277 return need_2nd_pass;
278}
279
280
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800281/* Returns 0 for success.
282 *
283 * Try latest firmware: B > A > RO
284 *
285 * This function assumes the EC jumps to RO at gec_prepare() so that
286 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
287 * this code logic if you change the gec_prepare() behavior.
288 */
289int gec_finish(void) {
290 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
291
292 if (!(priv && priv->detected)) return 0;
293
294 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800295 if (fwcopy[EC_IMAGE_RW].flags &&
296 gec_jump_copy(EC_IMAGE_RW) == 0) return 0;
297 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800298 }
299
300 return 0;
301}
302
303
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800304int gec_read(struct flashchip *flash, uint8_t *readarr,
305 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800306 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800307 struct ec_params_flash_read p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700308 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700309 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800310 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700311 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800312
David Hendricks133083b2012-07-17 20:39:38 -0700313 while (offset < readcnt) {
314 count = min(maxlen, readcnt - offset);
315 p.offset = blockaddr + offset;
316 p.size = count;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800317 rc = priv->ec_command(EC_CMD_FLASH_READ, 0,
318 &p, sizeof(p), buf, count);
319 if (rc < 0) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800320 msg_perr("GEC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700321 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800322 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800323 } else {
324 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800325 }
326
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800327 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700328 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800329 }
330
331 return rc;
332}
333
334
Simon Glassc453a642013-07-01 18:08:53 +0900335/*
336 * returns 0 to indicate area does not overlap current EC image
337 * returns 1 to indicate area overlaps current EC image or error
338 */
339static int in_current_image(struct gec_priv *priv,
340 unsigned int addr, unsigned int len)
341{
342 int ret;
343 enum ec_current_image image;
344 uint32_t region_offset;
345 uint32_t region_size;
346
347 image = priv->current_image;
348 region_offset = priv->region[image].offset;
349 region_size = priv->region[image].size;
350
351 if ((addr + len - 1 < region_offset) ||
352 (addr > region_offset + region_size - 1)) {
353 return 0;
354 }
355 return 1;
356}
357
358
David Hendricks7cfbd022012-05-20 17:25:51 -0700359int gec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800360 unsigned int blockaddr,
361 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800362 struct ec_params_flash_erase erase;
David Hendricks7cfbd022012-05-20 17:25:51 -0700363 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800364 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800365
Simon Glassc453a642013-07-01 18:08:53 +0900366 if (in_current_image(priv, blockaddr, len)) {
367 gec_invalidate_copy(blockaddr, len);
368 need_2nd_pass = 1;
369 return ACCESS_DENIED;
370 }
371
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800372 erase.offset = blockaddr;
373 erase.size = len;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800374 rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700375 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800376 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800377 // this is active image.
378 gec_invalidate_copy(blockaddr, len);
379 need_2nd_pass = 1;
380 return ACCESS_DENIED;
381 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800382 if (rc < 0) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800383 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
384 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800385 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800386 } else {
387 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800388 }
389
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800390#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800391 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800392#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800393 return rc;
394}
395
396
397int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
398 unsigned int nbytes) {
399 int i, rc = 0;
400 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800401 struct ec_params_flash_write p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700402 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700403 int maxlen = opaque_programmer->max_data_write;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800404
405 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700406 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800407 p.offset = addr + i;
408 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900409
410 if (in_current_image(priv, p.offset, p.size)) {
411 gec_invalidate_copy(addr, nbytes);
412 need_2nd_pass = 1;
413 return ACCESS_DENIED;
414 }
415
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800416 memcpy(p.data, &buf[i], written);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800417 rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700418 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800419 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800420 // this is active image.
421 gec_invalidate_copy(addr, nbytes);
422 need_2nd_pass = 1;
423 return ACCESS_DENIED;
424 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800425
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800426 if (rc < 0) break;
427 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800428 }
429
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800430#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800431 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800432#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800433 return rc;
434}
435
436
437static int gec_list_ranges(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800438 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900439 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800440 int rc;
441
Simon Glass3c01dca2013-07-01 18:07:34 +0900442 rc = gec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800443 if (rc < 0) {
444 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
445 return 1;
446 }
447
448 msg_pinfo("Supported write protect range:\n");
449 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900450 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
451 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800452
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800453 return 0;
454}
455
456
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800457/*
458 * Helper function for flash protection.
459 *
460 * On EC API v1, the EC write protection has been simplified to one-bit:
461 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
462 * or disabled. However, this is different from the SPI-style write protect
463 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
464 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
465 *
466 * SRP Range | PROTECT_RO_AT_BOOT
467 * 0 0 | 0
468 * 0 non-zero | 1
469 * 1 0 | 1
470 * 1 non-zero | 1
471 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800472 *
473 * Besides, to make the protection take effect as soon as possible, we
474 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
475 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800476 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800477static int set_wp(int enable) {
478 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
479 struct ec_params_flash_protect p;
480 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800481 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
482 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
483 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800484 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800485
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800486 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800487 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800488 p.mask = (ro_at_boot_flag | ro_now_flag);
489 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800490 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
491 &p, sizeof(p), &r, sizeof(r));
492 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800493 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
494 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800495 return 1;
496 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800497
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800498 /* Read back */
499 memset(&p, 0, sizeof(p));
500 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
501 &p, sizeof(p), &r, sizeof(r));
502 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800503 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
504 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800505 return 1;
506 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800507
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800508 if (!enable) {
509 /* The disable case is easier to check. */
510 if (r.flags & ro_at_boot_flag) {
511 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
512 return 1;
513 } else if (r.flags & ro_now_flag) {
514 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
515 need_an_ec_cold_reset = 1;
516 goto exit;
517 }
518
519 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
520 return 0;
521 }
522
523 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
524 if (r.flags & ro_at_boot_flag) {
525 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
526 } else {
527 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
528 return 1;
529 }
530
531 /* Then, we check if the protection has been activated. */
532 if (r.flags & ro_now_flag) {
533 /* Good, RO_NOW is set. */
534 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
535 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
536 struct ec_params_reboot_ec reboot;
537
538 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
539
540 memset(&p, 0, sizeof(p));
541 p.mask = EC_FLASH_PROTECT_ALL_NOW;
542 p.flags = EC_FLASH_PROTECT_ALL_NOW;
543 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
544 EC_VER_FLASH_PROTECT,
545 &p, sizeof(p), &r, sizeof(r));
546 if (rc < 0) {
547 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
548 return 1;
549 }
550
551 /* Read back */
552 memset(&p, 0, sizeof(p));
553 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
554 EC_VER_FLASH_PROTECT,
555 &p, sizeof(p), &r, sizeof(r));
556 if (rc < 0) {
557 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
558 return 1;
559 }
560
561 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
562 msg_perr("FAILED: ALL_NOW is not set.\n");
563 need_an_ec_cold_reset = 1;
564 goto exit;
565 }
566
567 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
568
569 /*
570 * Our goal is to protect the RO ASAP. The entire protection
571 * is just a workaround for platform not supporting RO_NOW.
572 * It has side-effect that the RW is also protected and leads
573 * the RW update failed. So, we arrange an EC code reset to
574 * unlock RW ASAP.
575 */
576 memset(&reboot, 0, sizeof(reboot));
577 reboot.cmd = EC_REBOOT_COLD;
578 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
579 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
580 &reboot, sizeof(reboot), NULL, 0);
581 if (rc < 0) {
582 msg_perr("WARN: Cannot arrange a cold reset at next "
583 "shutdown to unlock entire protect.\n");
584 msg_perr(" But you can do it manually.\n");
585 } else {
586 msg_pdbg("INFO: A cold reset is arranged at next "
587 "shutdown.\n");
588 }
589
590 } else {
591 msg_perr("FAILED: RO_NOW is not set.\n");
592 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
593 "make write protection active now.\n");
594 need_an_ec_cold_reset = 1;
595 }
596
597exit:
598 if (need_an_ec_cold_reset) {
599 msg_perr("FAILED: You may need a reboot to take effect of "
600 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800601 return 1;
602 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800603
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800604 return 0;
605}
606
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800607static int gec_set_range(const struct flashchip *flash,
608 unsigned int start, unsigned int len) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800609 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Simon Glass3c01dca2013-07-01 18:07:34 +0900610 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800611 int rc;
612
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800613 /* Check if the given range is supported */
Simon Glass3c01dca2013-07-01 18:07:34 +0900614 rc = gec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800615 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800616 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800617 return 1;
618 }
619 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900620 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800621 /* pass */
622 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800623 msg_perr("FAILED: Unsupported write protection range "
624 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800625 msg_perr("Currently supported range:\n");
626 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900627 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
628 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800629 return 1;
630 }
631
632 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800633}
634
635
David Hendricks1c09f802012-10-03 11:03:48 -0700636static int gec_enable_writeprotect(const struct flashchip *flash,
637 enum wp_mode wp_mode) {
638 int ret;
639
640 switch (wp_mode) {
641 case WP_MODE_HARDWARE:
642 ret = set_wp(1);
643 break;
644 default:
645 msg_perr("%s():%d Unsupported write-protection mode\n",
646 __func__, __LINE__);
647 ret = 1;
648 break;
649 }
650
651 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800652}
653
654
655static int gec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800656 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800657}
658
659
660static int gec_wp_status(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800661 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
662 struct ec_params_flash_protect p;
663 struct ec_response_flash_protect r;
664 int start, len; /* wp range */
665 int enabled;
666 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800667
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800668 memset(&p, 0, sizeof(p));
669 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
670 &p, sizeof(p), &r, sizeof(r));
671 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800672 msg_perr("FAILED: Cannot get the write protection status: %d\n",
673 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800674 return 1;
675 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700676 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800677 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800678 return 1;
679 }
680
681 start = len = 0;
682 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900683 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800684
685 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
686 __func__);
Simon Glass3c01dca2013-07-01 18:07:34 +0900687 rc = gec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800688 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800689 msg_perr("FAILED: Cannot get the WP_RO region info: "
690 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800691 return 1;
692 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900693 start = info.offset;
694 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800695 } else {
696 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
697 __func__);
698 }
699
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800700 /*
701 * If neither RO_NOW or ALL_NOW is set, it means write protect is
702 * NOT active now.
703 */
704 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
705 start = len = 0;
706
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800707 /* Remove the SPI-style messages. */
708 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
709 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
710 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800711 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800712 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800713 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800714 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800715
716 return 0;
717}
718
719
David Hendricks7cfbd022012-05-20 17:25:51 -0700720int gec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800721 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800722 struct ec_response_flash_info info;
David Hendricks7cfbd022012-05-20 17:25:51 -0700723 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800724 struct block_eraser *eraser;
725 static struct wp wp = {
726 .list_ranges = gec_list_ranges,
727 .set_range = gec_set_range,
728 .enable = gec_enable_writeprotect,
729 .disable = gec_disable_writeprotect,
730 .wp_status = gec_wp_status,
731 };
732
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800733 rc = priv->ec_command(EC_CMD_FLASH_INFO, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700734 NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800735 if (rc < 0) {
736 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
737 return 0;
738 }
Simon Glass01c11672013-07-01 18:03:33 +0900739 rc = gec_get_current_image(priv);
740 if (rc < 0) {
741 msg_perr("%s(): Failed to probe (no current image): %d\n",
742 __func__, rc);
743 return 0;
744 }
745 priv->current_image = rc;
Simon Glassc453a642013-07-01 18:08:53 +0900746 priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800747
748 flash->total_size = info.flash_size / 1024;
David Hendricks0d3fcb52012-07-08 18:37:43 -0700749 flash->page_size = 64;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800750 flash->tested = TEST_OK_PREW;
751 eraser = &flash->block_erasers[0];
752 eraser->eraseblocks[0].size = info.erase_block_size;
753 eraser->eraseblocks[0].count = info.flash_size /
754 eraser->eraseblocks[0].size;
755 flash->wp = &wp;
756
Simon Glassc453a642013-07-01 18:08:53 +0900757 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
758 * so we need to be careful about using these enums as array indices */
759 rc = gec_get_region_info(priv, EC_FLASH_REGION_RO,
760 &priv->region[EC_IMAGE_RO]);
761 if (rc) {
762 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
763 __func__, rc);
764 return 0;
765 }
766
767 rc = gec_get_region_info(priv, EC_FLASH_REGION_RW,
768 &priv->region[EC_IMAGE_RW]);
769 if (rc) {
770 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
771 __func__, rc);
772 return 0;
773 }
774
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800775 return 1;
776};