blob: 28cc12f641f2c1a2d8ed0174f070f05e9083c3f6 [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
35#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 Hendricksa9151312013-07-01 12:21:01 -070041#include "gec.h"
Louis Yung-Chieh Loc0505242012-08-09 23:11:32 +080042#include "gec_lock.h"
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080043#include "gec_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080044#include "programmer.h"
45#include "spi.h"
46#include "writeprotect.h"
47
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080048/* FIXME: used for wp hacks */
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <unistd.h>
53struct wp_data {
54 int enable;
55 unsigned int start;
56 unsigned int len;
57};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080058#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
59
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080060/* If software sync is enabled, then we don't try the latest firmware copy
61 * after updating.
62 */
63#define SOFTWARE_SYNC_ENABLED
64
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080065/* 1 if we want the flashrom to call erase_and_write_flash() again. */
66static int need_2nd_pass = 0;
67
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080068/* 1 if we want the flashrom to try jumping to new firmware after update. */
69static int try_latest_firmware = 0;
70
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080071/* The range of each firmware copy from the image file to update.
72 * But re-define the .flags as the valid flag to indicate the firmware is
73 * new or not (if flags = 1).
74 */
75static struct fmap_area fwcopy[4]; // [0] is not used.
76
77/* The names of enum lpc_current_image to match in FMAP area names. */
David Hendricksbf8c4dd2012-07-19 12:13:17 -070078static const char *sections[3] = {
79 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
80 "EC_RO",
81 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080082};
83
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080084
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080085/* Given the range not able to update, mark the corresponding
86 * firmware as old.
87 */
88static void gec_invalidate_copy(unsigned int addr, unsigned int len)
89{
90 int i;
91
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080092 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080093 struct fmap_area *fw = &fwcopy[i];
94 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
95 (fw->offset >= addr && (fw->offset < addr + len))) {
96 msg_pdbg("Mark firmware [%s] as old.\n",
97 sections[i]);
98 fw->flags = 0; // mark as old
99 }
100 }
101}
102
103
Simon Glass01c11672013-07-01 18:03:33 +0900104static int gec_get_current_image(struct gec_priv *priv)
105{
106 struct ec_response_get_version resp;
107 int rc;
108
109 rc = priv->ec_command(EC_CMD_GET_VERSION, 0, NULL, 0, &resp,
110 sizeof(resp));
111 if (rc < 0) {
112 msg_perr("GEC cannot get the running copy: rc=%d\n", rc);
113 return rc;
114 }
115 if (resp.current_image == EC_IMAGE_UNKNOWN) {
116 msg_perr("GEC gets unknown running copy\n");
117 return -1;
118 }
119
120 return resp.current_image;
121}
122
123
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800124/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800125 * then this functions picks a NEW firmware copy and jumps to it. Note that
126 * RO is preferred, then A, finally B.
127 *
128 * Returns 0 for success.
129 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800130static int gec_jump_copy(enum ec_current_image target) {
131 struct ec_response_get_version c;
132 struct ec_params_reboot_ec p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700133 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800134 int rc;
135
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800136 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
137 * jump to different firmware copy. The second EC_RES_SUCCESS would
138 * set the OBF=1 and the next command cannot be executed.
139 * Thus, we call EC to jump only if the target is different.
140 */
Simon Glass01c11672013-07-01 18:03:33 +0900141 rc = gec_get_current_image(priv);
142 if (rc < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800143 return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800144
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800145 memset(&p, 0, sizeof(p));
146 p.cmd = target != EC_IMAGE_UNKNOWN ? target :
147 fwcopy[EC_IMAGE_RO].flags ? EC_IMAGE_RO :
148 fwcopy[EC_IMAGE_RW].flags ? EC_IMAGE_RW :
149 EC_IMAGE_UNKNOWN;
150 msg_pdbg("GEC is jumping to [%s]\n", sections[p.cmd]);
151 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
152
153 if (c.current_image == p.cmd) {
154 msg_pdbg("GEC is already in [%s]\n", sections[p.cmd]);
155 return 0;
156 }
157
158 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700159 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800160 if (rc < 0) {
161 msg_perr("GEC cannot jump to [%s]:%d\n",
162 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800163 } else {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800164 msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]);
165 rc = EC_RES_SUCCESS;
Simon Glass01c11672013-07-01 18:03:33 +0900166 priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800167 }
168
169 /* Sleep 1 sec to wait the EC re-init. */
170 usleep(1000000);
171
172 return rc;
173}
174
175
176/* Given an image, this function parses FMAP and recognize the firmware
177 * ranges.
178 */
179int gec_prepare(uint8_t *image, int size) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700180 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800181 struct fmap *fmap;
182 int i, j;
183
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800184 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800185
186 // Parse the fmap in the image file and cache the firmware ranges.
187 fmap = fmap_find_in_memory(image, size);
188 if (!fmap) return 0;
189
190 // Lookup RO/A/B sections in FMAP.
191 for (i = 0; i < fmap->nareas; i++) {
192 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800193 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700194 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800195 msg_pdbg("Found '%s' in image.\n", fa->name);
196 memcpy(&fwcopy[j], fa, sizeof(*fa));
197 fwcopy[j].flags = 1; // mark as new
198 }
199 }
200 }
201
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800202 /* Warning: before update, we jump the EC to RO copy. If you want to
203 * change this behavior, please also check the gec_finish().
204 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800205 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800206}
207
208
209/* Returns >0 if we need 2nd pass of erase_and_write_flash().
210 * <0 if we cannot jump to any firmware copy.
211 * ==0 if no more pass is needed.
212 *
213 * This function also jumps to new-updated firmware copy before return >0.
214 */
215int gec_need_2nd_pass(void) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700216 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
217
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800218 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800219
220 if (need_2nd_pass) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800221 if (gec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800222 return -1;
223 }
224 }
225
226 return need_2nd_pass;
227}
228
229
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800230/* Returns 0 for success.
231 *
232 * Try latest firmware: B > A > RO
233 *
234 * This function assumes the EC jumps to RO at gec_prepare() so that
235 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
236 * this code logic if you change the gec_prepare() behavior.
237 */
238int gec_finish(void) {
239 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
240
241 if (!(priv && priv->detected)) return 0;
242
243 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800244 if (fwcopy[EC_IMAGE_RW].flags &&
245 gec_jump_copy(EC_IMAGE_RW) == 0) return 0;
246 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800247 }
248
249 return 0;
250}
251
252
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800253int gec_read(struct flashchip *flash, uint8_t *readarr,
254 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800255 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800256 struct ec_params_flash_read p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700257 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700258 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800259 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700260 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800261
David Hendricks133083b2012-07-17 20:39:38 -0700262 while (offset < readcnt) {
263 count = min(maxlen, readcnt - offset);
264 p.offset = blockaddr + offset;
265 p.size = count;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800266 rc = priv->ec_command(EC_CMD_FLASH_READ, 0,
267 &p, sizeof(p), buf, count);
268 if (rc < 0) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800269 msg_perr("GEC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700270 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800271 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800272 } else {
273 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800274 }
275
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800276 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700277 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800278 }
279
280 return rc;
281}
282
283
David Hendricks7cfbd022012-05-20 17:25:51 -0700284int gec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800285 unsigned int blockaddr,
286 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800287 struct ec_params_flash_erase erase;
David Hendricks7cfbd022012-05-20 17:25:51 -0700288 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800289 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800290
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800291 erase.offset = blockaddr;
292 erase.size = len;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800293 rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700294 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800295 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800296 // this is active image.
297 gec_invalidate_copy(blockaddr, len);
298 need_2nd_pass = 1;
299 return ACCESS_DENIED;
300 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800301 if (rc < 0) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800302 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
303 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800304 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800305 } else {
306 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800307 }
308
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800309#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800310 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800311#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800312 return rc;
313}
314
315
316int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
317 unsigned int nbytes) {
318 int i, rc = 0;
319 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800320 struct ec_params_flash_write p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700321 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700322 int maxlen = opaque_programmer->max_data_write;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800323
324 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700325 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800326 p.offset = addr + i;
327 p.size = written;
328 memcpy(p.data, &buf[i], written);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800329 rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700330 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800331 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800332 // this is active image.
333 gec_invalidate_copy(addr, nbytes);
334 need_2nd_pass = 1;
335 return ACCESS_DENIED;
336 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800337
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800338 if (rc < 0) break;
339 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800340 }
341
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800342#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800343 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800344#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800345 return rc;
346}
347
348
349static int gec_list_ranges(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800350 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
351 struct ec_params_flash_region_info p;
352 struct ec_response_flash_region_info r;
353 int rc;
354
355 p.region = EC_FLASH_REGION_WP_RO;
356 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
357 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
358 if (rc < 0) {
359 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
360 return 1;
361 }
362
363 msg_pinfo("Supported write protect range:\n");
364 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
365 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", r.offset, r.size);
366
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800367 return 0;
368}
369
370
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800371/*
372 * Helper function for flash protection.
373 *
374 * On EC API v1, the EC write protection has been simplified to one-bit:
375 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
376 * or disabled. However, this is different from the SPI-style write protect
377 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
378 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
379 *
380 * SRP Range | PROTECT_RO_AT_BOOT
381 * 0 0 | 0
382 * 0 non-zero | 1
383 * 1 0 | 1
384 * 1 non-zero | 1
385 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800386 *
387 * Besides, to make the protection take effect as soon as possible, we
388 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
389 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800390 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800391static int set_wp(int enable) {
392 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
393 struct ec_params_flash_protect p;
394 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800395 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
396 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
397 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800398 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800399
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800400 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800401 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800402 p.mask = (ro_at_boot_flag | ro_now_flag);
403 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800404 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
405 &p, sizeof(p), &r, sizeof(r));
406 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800407 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
408 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800409 return 1;
410 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800411
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800412 /* Read back */
413 memset(&p, 0, sizeof(p));
414 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
415 &p, sizeof(p), &r, sizeof(r));
416 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800417 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
418 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800419 return 1;
420 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800421
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800422 if (!enable) {
423 /* The disable case is easier to check. */
424 if (r.flags & ro_at_boot_flag) {
425 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
426 return 1;
427 } else if (r.flags & ro_now_flag) {
428 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
429 need_an_ec_cold_reset = 1;
430 goto exit;
431 }
432
433 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
434 return 0;
435 }
436
437 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
438 if (r.flags & ro_at_boot_flag) {
439 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
440 } else {
441 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
442 return 1;
443 }
444
445 /* Then, we check if the protection has been activated. */
446 if (r.flags & ro_now_flag) {
447 /* Good, RO_NOW is set. */
448 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
449 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
450 struct ec_params_reboot_ec reboot;
451
452 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
453
454 memset(&p, 0, sizeof(p));
455 p.mask = EC_FLASH_PROTECT_ALL_NOW;
456 p.flags = EC_FLASH_PROTECT_ALL_NOW;
457 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
458 EC_VER_FLASH_PROTECT,
459 &p, sizeof(p), &r, sizeof(r));
460 if (rc < 0) {
461 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
462 return 1;
463 }
464
465 /* Read back */
466 memset(&p, 0, sizeof(p));
467 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
468 EC_VER_FLASH_PROTECT,
469 &p, sizeof(p), &r, sizeof(r));
470 if (rc < 0) {
471 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
472 return 1;
473 }
474
475 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
476 msg_perr("FAILED: ALL_NOW is not set.\n");
477 need_an_ec_cold_reset = 1;
478 goto exit;
479 }
480
481 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
482
483 /*
484 * Our goal is to protect the RO ASAP. The entire protection
485 * is just a workaround for platform not supporting RO_NOW.
486 * It has side-effect that the RW is also protected and leads
487 * the RW update failed. So, we arrange an EC code reset to
488 * unlock RW ASAP.
489 */
490 memset(&reboot, 0, sizeof(reboot));
491 reboot.cmd = EC_REBOOT_COLD;
492 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
493 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
494 &reboot, sizeof(reboot), NULL, 0);
495 if (rc < 0) {
496 msg_perr("WARN: Cannot arrange a cold reset at next "
497 "shutdown to unlock entire protect.\n");
498 msg_perr(" But you can do it manually.\n");
499 } else {
500 msg_pdbg("INFO: A cold reset is arranged at next "
501 "shutdown.\n");
502 }
503
504 } else {
505 msg_perr("FAILED: RO_NOW is not set.\n");
506 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
507 "make write protection active now.\n");
508 need_an_ec_cold_reset = 1;
509 }
510
511exit:
512 if (need_an_ec_cold_reset) {
513 msg_perr("FAILED: You may need a reboot to take effect of "
514 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800515 return 1;
516 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800517
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800518 return 0;
519}
520
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800521static int gec_set_range(const struct flashchip *flash,
522 unsigned int start, unsigned int len) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800523 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
524 struct ec_params_flash_region_info p;
525 struct ec_response_flash_region_info r;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800526 int rc;
527
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800528 /* Check if the given range is supported */
529 p.region = EC_FLASH_REGION_WP_RO;
530 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
531 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
532 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800533 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800534 return 1;
535 }
536 if ((!start && !len) || /* list supported ranges */
537 ((start == r.offset) && (len == r.size))) {
538 /* pass */
539 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800540 msg_perr("FAILED: Unsupported write protection range "
541 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800542 msg_perr("Currently supported range:\n");
543 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
544 msg_perr(" enable: (0x%06x,0x%06x)\n", r.offset, r.size);
545 return 1;
546 }
547
548 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800549}
550
551
David Hendricks1c09f802012-10-03 11:03:48 -0700552static int gec_enable_writeprotect(const struct flashchip *flash,
553 enum wp_mode wp_mode) {
554 int ret;
555
556 switch (wp_mode) {
557 case WP_MODE_HARDWARE:
558 ret = set_wp(1);
559 break;
560 default:
561 msg_perr("%s():%d Unsupported write-protection mode\n",
562 __func__, __LINE__);
563 ret = 1;
564 break;
565 }
566
567 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800568}
569
570
571static int gec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800572 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800573}
574
575
576static int gec_wp_status(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800577 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
578 struct ec_params_flash_protect p;
579 struct ec_response_flash_protect r;
580 int start, len; /* wp range */
581 int enabled;
582 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800583
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800584 memset(&p, 0, sizeof(p));
585 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
586 &p, sizeof(p), &r, sizeof(r));
587 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800588 msg_perr("FAILED: Cannot get the write protection status: %d\n",
589 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800590 return 1;
591 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700592 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800593 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800594 return 1;
595 }
596
597 start = len = 0;
598 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
599 struct ec_params_flash_region_info reg_p;
600 struct ec_response_flash_region_info reg_r;
601
602 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
603 __func__);
604 reg_p.region = EC_FLASH_REGION_WP_RO;
605 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
606 EC_VER_FLASH_REGION_INFO,
607 &reg_p, sizeof(reg_p), &reg_r, sizeof(reg_r));
608 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800609 msg_perr("FAILED: Cannot get the WP_RO region info: "
610 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800611 return 1;
612 }
613 start = reg_r.offset;
614 len = reg_r.size;
615 } else {
616 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
617 __func__);
618 }
619
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800620 /*
621 * If neither RO_NOW or ALL_NOW is set, it means write protect is
622 * NOT active now.
623 */
624 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
625 start = len = 0;
626
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800627 /* Remove the SPI-style messages. */
628 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
629 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
630 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800631 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800632 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800633 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800634 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800635
636 return 0;
637}
638
639
David Hendricks7cfbd022012-05-20 17:25:51 -0700640int gec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800641 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800642 struct ec_response_flash_info info;
David Hendricks7cfbd022012-05-20 17:25:51 -0700643 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800644 struct block_eraser *eraser;
645 static struct wp wp = {
646 .list_ranges = gec_list_ranges,
647 .set_range = gec_set_range,
648 .enable = gec_enable_writeprotect,
649 .disable = gec_disable_writeprotect,
650 .wp_status = gec_wp_status,
651 };
652
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800653 rc = priv->ec_command(EC_CMD_FLASH_INFO, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700654 NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800655 if (rc < 0) {
656 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
657 return 0;
658 }
Simon Glass01c11672013-07-01 18:03:33 +0900659 rc = gec_get_current_image(priv);
660 if (rc < 0) {
661 msg_perr("%s(): Failed to probe (no current image): %d\n",
662 __func__, rc);
663 return 0;
664 }
665 priv->current_image = rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800666
667 flash->total_size = info.flash_size / 1024;
David Hendricks0d3fcb52012-07-08 18:37:43 -0700668 flash->page_size = 64;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800669 flash->tested = TEST_OK_PREW;
670 eraser = &flash->block_erasers[0];
671 eraser->eraseblocks[0].size = info.erase_block_size;
672 eraser->eraseblocks[0].count = info.flash_size /
673 eraser->eraseblocks[0].size;
674 flash->wp = &wp;
675
676 return 1;
677};