blob: 6488c04a9a95bd34d58fb65bb6f4da5806a8c97c [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"
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};
57static struct wp_data fake_wp;
58#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
59
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080060/* 1 if we want the flashrom to call erase_and_write_flash() again. */
61static int need_2nd_pass = 0;
62
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080063/* 1 if we want the flashrom to try jumping to new firmware after update. */
64static int try_latest_firmware = 0;
65
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080066/* The range of each firmware copy from the image file to update.
67 * But re-define the .flags as the valid flag to indicate the firmware is
68 * new or not (if flags = 1).
69 */
70static struct fmap_area fwcopy[4]; // [0] is not used.
71
72/* The names of enum lpc_current_image to match in FMAP area names. */
David Hendricksbf8c4dd2012-07-19 12:13:17 -070073static const char *sections[3] = {
74 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
75 "EC_RO",
76 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080077};
78
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080079
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080080/* Given the range not able to update, mark the corresponding
81 * firmware as old.
82 */
83static void gec_invalidate_copy(unsigned int addr, unsigned int len)
84{
85 int i;
86
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080087 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080088 struct fmap_area *fw = &fwcopy[i];
89 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
90 (fw->offset >= addr && (fw->offset < addr + len))) {
91 msg_pdbg("Mark firmware [%s] as old.\n",
92 sections[i]);
93 fw->flags = 0; // mark as old
94 }
95 }
96}
97
98
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +080099/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800100 * then this functions picks a NEW firmware copy and jumps to it. Note that
101 * RO is preferred, then A, finally B.
102 *
103 * Returns 0 for success.
104 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800105static int gec_jump_copy(enum ec_current_image target) {
106 struct ec_response_get_version c;
107 struct ec_params_reboot_ec p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700108 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800109 int rc;
110
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800111 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
112 * jump to different firmware copy. The second EC_RES_SUCCESS would
113 * set the OBF=1 and the next command cannot be executed.
114 * Thus, we call EC to jump only if the target is different.
115 */
116 rc = priv->ec_command(EC_CMD_GET_VERSION, 0, NULL, 0, &c, sizeof(c));
Louis Yung-Chieh Lo5c97c9f2012-08-16 13:33:36 +0800117 if (rc < 0 || c.current_image == EC_IMAGE_UNKNOWN) {
118 msg_perr("GEC cannot get the running copy: rc=%d image=%s\n",
119 rc, sections[c.current_image]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800120 return 1;
121 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800122
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800123 memset(&p, 0, sizeof(p));
124 p.cmd = target != EC_IMAGE_UNKNOWN ? target :
125 fwcopy[EC_IMAGE_RO].flags ? EC_IMAGE_RO :
126 fwcopy[EC_IMAGE_RW].flags ? EC_IMAGE_RW :
127 EC_IMAGE_UNKNOWN;
128 msg_pdbg("GEC is jumping to [%s]\n", sections[p.cmd]);
129 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
130
131 if (c.current_image == p.cmd) {
132 msg_pdbg("GEC is already in [%s]\n", sections[p.cmd]);
133 return 0;
134 }
135
136 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700137 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800138 if (rc < 0) {
139 msg_perr("GEC cannot jump to [%s]:%d\n",
140 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800141 } else {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800142 msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]);
143 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800144 }
145
146 /* Sleep 1 sec to wait the EC re-init. */
147 usleep(1000000);
148
149 return rc;
150}
151
152
153/* Given an image, this function parses FMAP and recognize the firmware
154 * ranges.
155 */
156int gec_prepare(uint8_t *image, int size) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700157 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800158 struct fmap *fmap;
159 int i, j;
160
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800161 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800162
163 // Parse the fmap in the image file and cache the firmware ranges.
164 fmap = fmap_find_in_memory(image, size);
165 if (!fmap) return 0;
166
167 // Lookup RO/A/B sections in FMAP.
168 for (i = 0; i < fmap->nareas; i++) {
169 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800170 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700171 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800172 msg_pdbg("Found '%s' in image.\n", fa->name);
173 memcpy(&fwcopy[j], fa, sizeof(*fa));
174 fwcopy[j].flags = 1; // mark as new
175 }
176 }
177 }
178
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800179 /* Warning: before update, we jump the EC to RO copy. If you want to
180 * change this behavior, please also check the gec_finish().
181 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800182 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800183}
184
185
186/* Returns >0 if we need 2nd pass of erase_and_write_flash().
187 * <0 if we cannot jump to any firmware copy.
188 * ==0 if no more pass is needed.
189 *
190 * This function also jumps to new-updated firmware copy before return >0.
191 */
192int gec_need_2nd_pass(void) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700193 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
194
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800195 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800196
197 if (need_2nd_pass) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800198 if (gec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800199 return -1;
200 }
201 }
202
203 return need_2nd_pass;
204}
205
206
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800207/* Returns 0 for success.
208 *
209 * Try latest firmware: B > A > RO
210 *
211 * This function assumes the EC jumps to RO at gec_prepare() so that
212 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
213 * this code logic if you change the gec_prepare() behavior.
214 */
215int gec_finish(void) {
216 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
217
218 if (!(priv && priv->detected)) return 0;
219
220 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800221 if (fwcopy[EC_IMAGE_RW].flags &&
222 gec_jump_copy(EC_IMAGE_RW) == 0) return 0;
223 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800224 }
225
226 return 0;
227}
228
229
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800230int gec_read(struct flashchip *flash, uint8_t *readarr,
231 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800232 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800233 struct ec_params_flash_read p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700234 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700235 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800236 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700237 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800238
David Hendricks133083b2012-07-17 20:39:38 -0700239 while (offset < readcnt) {
240 count = min(maxlen, readcnt - offset);
241 p.offset = blockaddr + offset;
242 p.size = count;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800243 rc = priv->ec_command(EC_CMD_FLASH_READ, 0,
244 &p, sizeof(p), buf, count);
245 if (rc < 0) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800246 msg_perr("GEC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700247 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800248 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800249 } else {
250 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800251 }
252
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800253 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700254 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800255 }
256
257 return rc;
258}
259
260
David Hendricks7cfbd022012-05-20 17:25:51 -0700261int gec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800262 unsigned int blockaddr,
263 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800264 struct ec_params_flash_erase erase;
David Hendricks7cfbd022012-05-20 17:25:51 -0700265 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800266 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800267
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800268 erase.offset = blockaddr;
269 erase.size = len;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800270 rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700271 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800272 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800273 // this is active image.
274 gec_invalidate_copy(blockaddr, len);
275 need_2nd_pass = 1;
276 return ACCESS_DENIED;
277 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800278 if (rc < 0) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800279 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
280 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800281 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800282 } else {
283 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800284 }
285
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800286 try_latest_firmware = 1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800287 return rc;
288}
289
290
291int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
292 unsigned int nbytes) {
293 int i, rc = 0;
294 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800295 struct ec_params_flash_write p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700296 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700297 int maxlen = opaque_programmer->max_data_write;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800298
299 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700300 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800301 p.offset = addr + i;
302 p.size = written;
303 memcpy(p.data, &buf[i], written);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800304 rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700305 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800306 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800307 // this is active image.
308 gec_invalidate_copy(addr, nbytes);
309 need_2nd_pass = 1;
310 return ACCESS_DENIED;
311 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800312
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800313 if (rc < 0) break;
314 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800315 }
316
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800317 try_latest_firmware = 1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800318 return rc;
319}
320
321
322static int gec_list_ranges(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800323 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
324 struct ec_params_flash_region_info p;
325 struct ec_response_flash_region_info r;
326 int rc;
327
328 p.region = EC_FLASH_REGION_WP_RO;
329 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
330 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
331 if (rc < 0) {
332 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
333 return 1;
334 }
335
336 msg_pinfo("Supported write protect range:\n");
337 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
338 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", r.offset, r.size);
339
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800340 return 0;
341}
342
343
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800344/*
345 * Helper function for flash protection.
346 *
347 * On EC API v1, the EC write protection has been simplified to one-bit:
348 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
349 * or disabled. However, this is different from the SPI-style write protect
350 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
351 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
352 *
353 * SRP Range | PROTECT_RO_AT_BOOT
354 * 0 0 | 0
355 * 0 non-zero | 1
356 * 1 0 | 1
357 * 1 non-zero | 1
358 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800359 *
360 * Besides, to make the protection take effect as soon as possible, we
361 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
362 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800363 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800364static int set_wp(int enable) {
365 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
366 struct ec_params_flash_protect p;
367 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800368 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
369 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
370 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800371 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800372
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800373 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800374 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800375 p.mask = (ro_at_boot_flag | ro_now_flag);
376 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800377 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
378 &p, sizeof(p), &r, sizeof(r));
379 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800380 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
381 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800382 return 1;
383 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800384
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800385 /* Read back */
386 memset(&p, 0, sizeof(p));
387 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
388 &p, sizeof(p), &r, sizeof(r));
389 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800390 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
391 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800392 return 1;
393 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800394
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800395 if (!enable) {
396 /* The disable case is easier to check. */
397 if (r.flags & ro_at_boot_flag) {
398 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
399 return 1;
400 } else if (r.flags & ro_now_flag) {
401 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
402 need_an_ec_cold_reset = 1;
403 goto exit;
404 }
405
406 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
407 return 0;
408 }
409
410 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
411 if (r.flags & ro_at_boot_flag) {
412 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
413 } else {
414 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
415 return 1;
416 }
417
418 /* Then, we check if the protection has been activated. */
419 if (r.flags & ro_now_flag) {
420 /* Good, RO_NOW is set. */
421 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
422 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
423 struct ec_params_reboot_ec reboot;
424
425 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
426
427 memset(&p, 0, sizeof(p));
428 p.mask = EC_FLASH_PROTECT_ALL_NOW;
429 p.flags = EC_FLASH_PROTECT_ALL_NOW;
430 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
431 EC_VER_FLASH_PROTECT,
432 &p, sizeof(p), &r, sizeof(r));
433 if (rc < 0) {
434 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
435 return 1;
436 }
437
438 /* Read back */
439 memset(&p, 0, sizeof(p));
440 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
441 EC_VER_FLASH_PROTECT,
442 &p, sizeof(p), &r, sizeof(r));
443 if (rc < 0) {
444 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
445 return 1;
446 }
447
448 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
449 msg_perr("FAILED: ALL_NOW is not set.\n");
450 need_an_ec_cold_reset = 1;
451 goto exit;
452 }
453
454 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
455
456 /*
457 * Our goal is to protect the RO ASAP. The entire protection
458 * is just a workaround for platform not supporting RO_NOW.
459 * It has side-effect that the RW is also protected and leads
460 * the RW update failed. So, we arrange an EC code reset to
461 * unlock RW ASAP.
462 */
463 memset(&reboot, 0, sizeof(reboot));
464 reboot.cmd = EC_REBOOT_COLD;
465 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
466 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
467 &reboot, sizeof(reboot), NULL, 0);
468 if (rc < 0) {
469 msg_perr("WARN: Cannot arrange a cold reset at next "
470 "shutdown to unlock entire protect.\n");
471 msg_perr(" But you can do it manually.\n");
472 } else {
473 msg_pdbg("INFO: A cold reset is arranged at next "
474 "shutdown.\n");
475 }
476
477 } else {
478 msg_perr("FAILED: RO_NOW is not set.\n");
479 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
480 "make write protection active now.\n");
481 need_an_ec_cold_reset = 1;
482 }
483
484exit:
485 if (need_an_ec_cold_reset) {
486 msg_perr("FAILED: You may need a reboot to take effect of "
487 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800488 return 1;
489 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800490
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800491 return 0;
492}
493
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800494static int gec_set_range(const struct flashchip *flash,
495 unsigned int start, unsigned int len) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800496 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
497 struct ec_params_flash_region_info p;
498 struct ec_response_flash_region_info r;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800499 int rc;
500
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800501 /* Check if the given range is supported */
502 p.region = EC_FLASH_REGION_WP_RO;
503 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
504 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
505 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800506 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800507 return 1;
508 }
509 if ((!start && !len) || /* list supported ranges */
510 ((start == r.offset) && (len == r.size))) {
511 /* pass */
512 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800513 msg_perr("FAILED: Unsupported write protection range "
514 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800515 msg_perr("Currently supported range:\n");
516 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
517 msg_perr(" enable: (0x%06x,0x%06x)\n", r.offset, r.size);
518 return 1;
519 }
520
521 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800522}
523
524
525static int gec_enable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800526 return set_wp(1);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800527}
528
529
530static int gec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800531 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800532}
533
534
535static int gec_wp_status(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800536 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
537 struct ec_params_flash_protect p;
538 struct ec_response_flash_protect r;
539 int start, len; /* wp range */
540 int enabled;
541 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800542
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800543 memset(&p, 0, sizeof(p));
544 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
545 &p, sizeof(p), &r, sizeof(r));
546 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800547 msg_perr("FAILED: Cannot get the write protection status: %d\n",
548 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800549 return 1;
550 } else if (rc < sizeof(r)) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800551 msg_perr("FAILED: Too little data returned (expected:%d, "
552 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800553 return 1;
554 }
555
556 start = len = 0;
557 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
558 struct ec_params_flash_region_info reg_p;
559 struct ec_response_flash_region_info reg_r;
560
561 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
562 __func__);
563 reg_p.region = EC_FLASH_REGION_WP_RO;
564 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
565 EC_VER_FLASH_REGION_INFO,
566 &reg_p, sizeof(reg_p), &reg_r, sizeof(reg_r));
567 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800568 msg_perr("FAILED: Cannot get the WP_RO region info: "
569 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800570 return 1;
571 }
572 start = reg_r.offset;
573 len = reg_r.size;
574 } else {
575 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
576 __func__);
577 }
578
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800579 /*
580 * If neither RO_NOW or ALL_NOW is set, it means write protect is
581 * NOT active now.
582 */
583 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
584 start = len = 0;
585
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800586 /* Remove the SPI-style messages. */
587 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
588 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
589 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800590 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800591 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800592 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800593 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800594
595 return 0;
596}
597
598
David Hendricks7cfbd022012-05-20 17:25:51 -0700599int gec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800600 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800601 struct ec_response_flash_info info;
David Hendricks7cfbd022012-05-20 17:25:51 -0700602 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800603 struct block_eraser *eraser;
604 static struct wp wp = {
605 .list_ranges = gec_list_ranges,
606 .set_range = gec_set_range,
607 .enable = gec_enable_writeprotect,
608 .disable = gec_disable_writeprotect,
609 .wp_status = gec_wp_status,
610 };
611
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800612 rc = priv->ec_command(EC_CMD_FLASH_INFO, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700613 NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800614 if (rc < 0) {
615 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
616 return 0;
617 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800618
619 flash->total_size = info.flash_size / 1024;
David Hendricks0d3fcb52012-07-08 18:37:43 -0700620 flash->page_size = 64;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800621 flash->tested = TEST_OK_PREW;
622 eraser = &flash->block_erasers[0];
623 eraser->eraseblocks[0].size = info.erase_block_size;
624 eraser->eraseblocks[0].count = info.flash_size /
625 eraser->eraseblocks[0].size;
626 flash->wp = &wp;
627
628 return 1;
629};