blob: a780b5ac4cd2cd8e88591ad07e8ede907a8ed7b6 [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
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800104/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800105 * then this functions picks a NEW firmware copy and jumps to it. Note that
106 * RO is preferred, then A, finally B.
107 *
108 * Returns 0 for success.
109 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800110static int gec_jump_copy(enum ec_current_image target) {
111 struct ec_response_get_version c;
112 struct ec_params_reboot_ec p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700113 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800114 int rc;
115
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800116 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
117 * jump to different firmware copy. The second EC_RES_SUCCESS would
118 * set the OBF=1 and the next command cannot be executed.
119 * Thus, we call EC to jump only if the target is different.
120 */
121 rc = priv->ec_command(EC_CMD_GET_VERSION, 0, NULL, 0, &c, sizeof(c));
Louis Yung-Chieh Lo5c97c9f2012-08-16 13:33:36 +0800122 if (rc < 0 || c.current_image == EC_IMAGE_UNKNOWN) {
123 msg_perr("GEC cannot get the running copy: rc=%d image=%s\n",
124 rc, sections[c.current_image]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800125 return 1;
126 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800127
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800128 memset(&p, 0, sizeof(p));
129 p.cmd = target != EC_IMAGE_UNKNOWN ? target :
130 fwcopy[EC_IMAGE_RO].flags ? EC_IMAGE_RO :
131 fwcopy[EC_IMAGE_RW].flags ? EC_IMAGE_RW :
132 EC_IMAGE_UNKNOWN;
133 msg_pdbg("GEC is jumping to [%s]\n", sections[p.cmd]);
134 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
135
136 if (c.current_image == p.cmd) {
137 msg_pdbg("GEC is already in [%s]\n", sections[p.cmd]);
138 return 0;
139 }
140
141 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700142 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800143 if (rc < 0) {
144 msg_perr("GEC cannot jump to [%s]:%d\n",
145 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800146 } else {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800147 msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]);
148 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800149 }
150
151 /* Sleep 1 sec to wait the EC re-init. */
152 usleep(1000000);
153
154 return rc;
155}
156
157
158/* Given an image, this function parses FMAP and recognize the firmware
159 * ranges.
160 */
161int gec_prepare(uint8_t *image, int size) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700162 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800163 struct fmap *fmap;
164 int i, j;
165
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800166 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800167
168 // Parse the fmap in the image file and cache the firmware ranges.
169 fmap = fmap_find_in_memory(image, size);
170 if (!fmap) return 0;
171
172 // Lookup RO/A/B sections in FMAP.
173 for (i = 0; i < fmap->nareas; i++) {
174 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800175 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700176 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800177 msg_pdbg("Found '%s' in image.\n", fa->name);
178 memcpy(&fwcopy[j], fa, sizeof(*fa));
179 fwcopy[j].flags = 1; // mark as new
180 }
181 }
182 }
183
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800184 /* Warning: before update, we jump the EC to RO copy. If you want to
185 * change this behavior, please also check the gec_finish().
186 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800187 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800188}
189
190
191/* Returns >0 if we need 2nd pass of erase_and_write_flash().
192 * <0 if we cannot jump to any firmware copy.
193 * ==0 if no more pass is needed.
194 *
195 * This function also jumps to new-updated firmware copy before return >0.
196 */
197int gec_need_2nd_pass(void) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700198 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
199
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800200 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800201
202 if (need_2nd_pass) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800203 if (gec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800204 return -1;
205 }
206 }
207
208 return need_2nd_pass;
209}
210
211
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800212/* Returns 0 for success.
213 *
214 * Try latest firmware: B > A > RO
215 *
216 * This function assumes the EC jumps to RO at gec_prepare() so that
217 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
218 * this code logic if you change the gec_prepare() behavior.
219 */
220int gec_finish(void) {
221 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
222
223 if (!(priv && priv->detected)) return 0;
224
225 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800226 if (fwcopy[EC_IMAGE_RW].flags &&
227 gec_jump_copy(EC_IMAGE_RW) == 0) return 0;
228 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800229 }
230
231 return 0;
232}
233
234
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800235int gec_read(struct flashchip *flash, uint8_t *readarr,
236 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800237 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800238 struct ec_params_flash_read p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700239 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700240 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800241 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700242 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800243
David Hendricks133083b2012-07-17 20:39:38 -0700244 while (offset < readcnt) {
245 count = min(maxlen, readcnt - offset);
246 p.offset = blockaddr + offset;
247 p.size = count;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800248 rc = priv->ec_command(EC_CMD_FLASH_READ, 0,
249 &p, sizeof(p), buf, count);
250 if (rc < 0) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800251 msg_perr("GEC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700252 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800253 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800254 } else {
255 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800256 }
257
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800258 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700259 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800260 }
261
262 return rc;
263}
264
265
David Hendricks7cfbd022012-05-20 17:25:51 -0700266int gec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800267 unsigned int blockaddr,
268 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800269 struct ec_params_flash_erase erase;
David Hendricks7cfbd022012-05-20 17:25:51 -0700270 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800271 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800272
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800273 erase.offset = blockaddr;
274 erase.size = len;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800275 rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700276 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800277 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800278 // this is active image.
279 gec_invalidate_copy(blockaddr, len);
280 need_2nd_pass = 1;
281 return ACCESS_DENIED;
282 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800283 if (rc < 0) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800284 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
285 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800286 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800287 } else {
288 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800289 }
290
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800291#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800292 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800293#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800294 return rc;
295}
296
297
298int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
299 unsigned int nbytes) {
300 int i, rc = 0;
301 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800302 struct ec_params_flash_write p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700303 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700304 int maxlen = opaque_programmer->max_data_write;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800305
306 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700307 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800308 p.offset = addr + i;
309 p.size = written;
310 memcpy(p.data, &buf[i], written);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800311 rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700312 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800313 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800314 // this is active image.
315 gec_invalidate_copy(addr, nbytes);
316 need_2nd_pass = 1;
317 return ACCESS_DENIED;
318 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800319
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800320 if (rc < 0) break;
321 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800322 }
323
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800324#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800325 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800326#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800327 return rc;
328}
329
330
331static int gec_list_ranges(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800332 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
333 struct ec_params_flash_region_info p;
334 struct ec_response_flash_region_info r;
335 int rc;
336
337 p.region = EC_FLASH_REGION_WP_RO;
338 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
339 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
340 if (rc < 0) {
341 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
342 return 1;
343 }
344
345 msg_pinfo("Supported write protect range:\n");
346 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
347 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", r.offset, r.size);
348
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800349 return 0;
350}
351
352
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800353/*
354 * Helper function for flash protection.
355 *
356 * On EC API v1, the EC write protection has been simplified to one-bit:
357 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
358 * or disabled. However, this is different from the SPI-style write protect
359 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
360 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
361 *
362 * SRP Range | PROTECT_RO_AT_BOOT
363 * 0 0 | 0
364 * 0 non-zero | 1
365 * 1 0 | 1
366 * 1 non-zero | 1
367 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800368 *
369 * Besides, to make the protection take effect as soon as possible, we
370 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
371 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800372 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800373static int set_wp(int enable) {
374 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
375 struct ec_params_flash_protect p;
376 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800377 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
378 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
379 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800380 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800381
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800382 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800383 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800384 p.mask = (ro_at_boot_flag | ro_now_flag);
385 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800386 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
387 &p, sizeof(p), &r, sizeof(r));
388 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800389 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
390 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800391 return 1;
392 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800393
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800394 /* Read back */
395 memset(&p, 0, sizeof(p));
396 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
397 &p, sizeof(p), &r, sizeof(r));
398 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800399 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
400 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800401 return 1;
402 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800403
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800404 if (!enable) {
405 /* The disable case is easier to check. */
406 if (r.flags & ro_at_boot_flag) {
407 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
408 return 1;
409 } else if (r.flags & ro_now_flag) {
410 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
411 need_an_ec_cold_reset = 1;
412 goto exit;
413 }
414
415 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
416 return 0;
417 }
418
419 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
420 if (r.flags & ro_at_boot_flag) {
421 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
422 } else {
423 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
424 return 1;
425 }
426
427 /* Then, we check if the protection has been activated. */
428 if (r.flags & ro_now_flag) {
429 /* Good, RO_NOW is set. */
430 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
431 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
432 struct ec_params_reboot_ec reboot;
433
434 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
435
436 memset(&p, 0, sizeof(p));
437 p.mask = EC_FLASH_PROTECT_ALL_NOW;
438 p.flags = EC_FLASH_PROTECT_ALL_NOW;
439 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
440 EC_VER_FLASH_PROTECT,
441 &p, sizeof(p), &r, sizeof(r));
442 if (rc < 0) {
443 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
444 return 1;
445 }
446
447 /* Read back */
448 memset(&p, 0, sizeof(p));
449 rc = priv->ec_command(EC_CMD_FLASH_PROTECT,
450 EC_VER_FLASH_PROTECT,
451 &p, sizeof(p), &r, sizeof(r));
452 if (rc < 0) {
453 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
454 return 1;
455 }
456
457 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
458 msg_perr("FAILED: ALL_NOW is not set.\n");
459 need_an_ec_cold_reset = 1;
460 goto exit;
461 }
462
463 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
464
465 /*
466 * Our goal is to protect the RO ASAP. The entire protection
467 * is just a workaround for platform not supporting RO_NOW.
468 * It has side-effect that the RW is also protected and leads
469 * the RW update failed. So, we arrange an EC code reset to
470 * unlock RW ASAP.
471 */
472 memset(&reboot, 0, sizeof(reboot));
473 reboot.cmd = EC_REBOOT_COLD;
474 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
475 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
476 &reboot, sizeof(reboot), NULL, 0);
477 if (rc < 0) {
478 msg_perr("WARN: Cannot arrange a cold reset at next "
479 "shutdown to unlock entire protect.\n");
480 msg_perr(" But you can do it manually.\n");
481 } else {
482 msg_pdbg("INFO: A cold reset is arranged at next "
483 "shutdown.\n");
484 }
485
486 } else {
487 msg_perr("FAILED: RO_NOW is not set.\n");
488 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
489 "make write protection active now.\n");
490 need_an_ec_cold_reset = 1;
491 }
492
493exit:
494 if (need_an_ec_cold_reset) {
495 msg_perr("FAILED: You may need a reboot to take effect of "
496 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800497 return 1;
498 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800499
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800500 return 0;
501}
502
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800503static int gec_set_range(const struct flashchip *flash,
504 unsigned int start, unsigned int len) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800505 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
506 struct ec_params_flash_region_info p;
507 struct ec_response_flash_region_info r;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800508 int rc;
509
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800510 /* Check if the given range is supported */
511 p.region = EC_FLASH_REGION_WP_RO;
512 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
513 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
514 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800515 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800516 return 1;
517 }
518 if ((!start && !len) || /* list supported ranges */
519 ((start == r.offset) && (len == r.size))) {
520 /* pass */
521 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800522 msg_perr("FAILED: Unsupported write protection range "
523 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800524 msg_perr("Currently supported range:\n");
525 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
526 msg_perr(" enable: (0x%06x,0x%06x)\n", r.offset, r.size);
527 return 1;
528 }
529
530 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800531}
532
533
David Hendricks1c09f802012-10-03 11:03:48 -0700534static int gec_enable_writeprotect(const struct flashchip *flash,
535 enum wp_mode wp_mode) {
536 int ret;
537
538 switch (wp_mode) {
539 case WP_MODE_HARDWARE:
540 ret = set_wp(1);
541 break;
542 default:
543 msg_perr("%s():%d Unsupported write-protection mode\n",
544 __func__, __LINE__);
545 ret = 1;
546 break;
547 }
548
549 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800550}
551
552
553static int gec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800554 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800555}
556
557
558static int gec_wp_status(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800559 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
560 struct ec_params_flash_protect p;
561 struct ec_response_flash_protect r;
562 int start, len; /* wp range */
563 int enabled;
564 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800565
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800566 memset(&p, 0, sizeof(p));
567 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
568 &p, sizeof(p), &r, sizeof(r));
569 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800570 msg_perr("FAILED: Cannot get the write protection status: %d\n",
571 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800572 return 1;
573 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700574 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800575 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800576 return 1;
577 }
578
579 start = len = 0;
580 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
581 struct ec_params_flash_region_info reg_p;
582 struct ec_response_flash_region_info reg_r;
583
584 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
585 __func__);
586 reg_p.region = EC_FLASH_REGION_WP_RO;
587 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
588 EC_VER_FLASH_REGION_INFO,
589 &reg_p, sizeof(reg_p), &reg_r, sizeof(reg_r));
590 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800591 msg_perr("FAILED: Cannot get the WP_RO region info: "
592 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800593 return 1;
594 }
595 start = reg_r.offset;
596 len = reg_r.size;
597 } else {
598 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
599 __func__);
600 }
601
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800602 /*
603 * If neither RO_NOW or ALL_NOW is set, it means write protect is
604 * NOT active now.
605 */
606 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
607 start = len = 0;
608
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800609 /* Remove the SPI-style messages. */
610 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
611 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
612 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800613 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800614 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800615 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800616 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800617
618 return 0;
619}
620
621
David Hendricks7cfbd022012-05-20 17:25:51 -0700622int gec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800623 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800624 struct ec_response_flash_info info;
David Hendricks7cfbd022012-05-20 17:25:51 -0700625 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800626 struct block_eraser *eraser;
627 static struct wp wp = {
628 .list_ranges = gec_list_ranges,
629 .set_range = gec_set_range,
630 .enable = gec_enable_writeprotect,
631 .disable = gec_disable_writeprotect,
632 .wp_status = gec_wp_status,
633 };
634
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800635 rc = priv->ec_command(EC_CMD_FLASH_INFO, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700636 NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800637 if (rc < 0) {
638 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
639 return 0;
640 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800641
642 flash->total_size = info.flash_size / 1024;
David Hendricks0d3fcb52012-07-08 18:37:43 -0700643 flash->page_size = 64;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800644 flash->tested = TEST_OK_PREW;
645 eraser = &flash->block_erasers[0];
646 eraser->eraseblocks[0].size = info.erase_block_size;
647 eraser->eraseblocks[0].count = info.flash_size /
648 eraser->eraseblocks[0].size;
649 flash->wp = &wp;
650
651 return 1;
652};