blob: 3714d637b30a6a664b76f81604e1c871e9b37750 [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));
117 if (rc || c.current_image == EC_IMAGE_UNKNOWN) {
118 msg_perr("GEC cannot get the running copy: rc=%d\n", rc);
119 return 1;
120 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800121
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800122 memset(&p, 0, sizeof(p));
123 p.cmd = target != EC_IMAGE_UNKNOWN ? target :
124 fwcopy[EC_IMAGE_RO].flags ? EC_IMAGE_RO :
125 fwcopy[EC_IMAGE_RW].flags ? EC_IMAGE_RW :
126 EC_IMAGE_UNKNOWN;
127 msg_pdbg("GEC is jumping to [%s]\n", sections[p.cmd]);
128 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
129
130 if (c.current_image == p.cmd) {
131 msg_pdbg("GEC is already in [%s]\n", sections[p.cmd]);
132 return 0;
133 }
134
135 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700136 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800137 if (rc < 0) {
138 msg_perr("GEC cannot jump to [%s]:%d\n",
139 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800140 } else {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800141 msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]);
142 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800143 }
144
145 /* Sleep 1 sec to wait the EC re-init. */
146 usleep(1000000);
147
148 return rc;
149}
150
151
152/* Given an image, this function parses FMAP and recognize the firmware
153 * ranges.
154 */
155int gec_prepare(uint8_t *image, int size) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700156 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800157 struct fmap *fmap;
158 int i, j;
159
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800160 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800161
162 // Parse the fmap in the image file and cache the firmware ranges.
163 fmap = fmap_find_in_memory(image, size);
164 if (!fmap) return 0;
165
166 // Lookup RO/A/B sections in FMAP.
167 for (i = 0; i < fmap->nareas; i++) {
168 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800169 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700170 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800171 msg_pdbg("Found '%s' in image.\n", fa->name);
172 memcpy(&fwcopy[j], fa, sizeof(*fa));
173 fwcopy[j].flags = 1; // mark as new
174 }
175 }
176 }
177
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800178 /* Warning: before update, we jump the EC to RO copy. If you want to
179 * change this behavior, please also check the gec_finish().
180 */
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800181 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800182}
183
184
185/* Returns >0 if we need 2nd pass of erase_and_write_flash().
186 * <0 if we cannot jump to any firmware copy.
187 * ==0 if no more pass is needed.
188 *
189 * This function also jumps to new-updated firmware copy before return >0.
190 */
191int gec_need_2nd_pass(void) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700192 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
193
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800194 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800195
196 if (need_2nd_pass) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800197 if (gec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800198 return -1;
199 }
200 }
201
202 return need_2nd_pass;
203}
204
205
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800206/* Returns 0 for success.
207 *
208 * Try latest firmware: B > A > RO
209 *
210 * This function assumes the EC jumps to RO at gec_prepare() so that
211 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
212 * this code logic if you change the gec_prepare() behavior.
213 */
214int gec_finish(void) {
215 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
216
217 if (!(priv && priv->detected)) return 0;
218
219 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800220 if (fwcopy[EC_IMAGE_RW].flags &&
221 gec_jump_copy(EC_IMAGE_RW) == 0) return 0;
222 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800223 }
224
225 return 0;
226}
227
228
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800229int gec_read(struct flashchip *flash, uint8_t *readarr,
230 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800231 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800232 struct ec_params_flash_read p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700233 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700234 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800235 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700236 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800237
David Hendricks133083b2012-07-17 20:39:38 -0700238 while (offset < readcnt) {
239 count = min(maxlen, readcnt - offset);
240 p.offset = blockaddr + offset;
241 p.size = count;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800242 rc = priv->ec_command(EC_CMD_FLASH_READ, 0,
243 &p, sizeof(p), buf, count);
244 if (rc < 0) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800245 msg_perr("GEC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700246 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800247 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800248 } else {
249 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800250 }
251
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800252 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700253 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800254 }
255
256 return rc;
257}
258
259
David Hendricks7cfbd022012-05-20 17:25:51 -0700260int gec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800261 unsigned int blockaddr,
262 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800263 struct ec_params_flash_erase erase;
David Hendricks7cfbd022012-05-20 17:25:51 -0700264 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800265 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800266
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800267 erase.offset = blockaddr;
268 erase.size = len;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800269 rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700270 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800271 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800272 // this is active image.
273 gec_invalidate_copy(blockaddr, len);
274 need_2nd_pass = 1;
275 return ACCESS_DENIED;
276 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800277 if (rc < 0) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800278 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
279 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800280 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800281 } else {
282 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800283 }
284
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800285 try_latest_firmware = 1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800286 return rc;
287}
288
289
290int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
291 unsigned int nbytes) {
292 int i, rc = 0;
293 unsigned int written = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800294 struct ec_params_flash_write p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700295 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700296 int maxlen = opaque_programmer->max_data_write;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800297
298 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700299 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800300 p.offset = addr + i;
301 p.size = written;
302 memcpy(p.data, &buf[i], written);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800303 rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700304 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800305 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800306 // this is active image.
307 gec_invalidate_copy(addr, nbytes);
308 need_2nd_pass = 1;
309 return ACCESS_DENIED;
310 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800311
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800312 if (rc < 0) break;
313 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800314 }
315
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800316 try_latest_firmware = 1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800317 return rc;
318}
319
320
321static int gec_list_ranges(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800322 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
323 struct ec_params_flash_region_info p;
324 struct ec_response_flash_region_info r;
325 int rc;
326
327 p.region = EC_FLASH_REGION_WP_RO;
328 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
329 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
330 if (rc < 0) {
331 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
332 return 1;
333 }
334
335 msg_pinfo("Supported write protect range:\n");
336 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
337 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", r.offset, r.size);
338
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800339 return 0;
340}
341
342
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800343/*
344 * Helper function for flash protection.
345 *
346 * On EC API v1, the EC write protection has been simplified to one-bit:
347 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
348 * or disabled. However, this is different from the SPI-style write protect
349 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
350 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
351 *
352 * SRP Range | PROTECT_RO_AT_BOOT
353 * 0 0 | 0
354 * 0 non-zero | 1
355 * 1 0 | 1
356 * 1 non-zero | 1
357 *
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800358 */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800359static int set_wp(int enable) {
360 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
361 struct ec_params_flash_protect p;
362 struct ec_response_flash_protect r;
363 int mask = EC_FLASH_PROTECT_RO_AT_BOOT;
364 int flag = enable ? EC_FLASH_PROTECT_RO_AT_BOOT : 0;
365 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800366
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800367 memset(&p, 0, sizeof(p));
368 p.mask = mask;
369 p.flags = flag;
370 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
371 &p, sizeof(p), &r, sizeof(r));
372 if (rc < 0) {
373 msg_perr("Cannot set the write protection status: %d\n", rc);
374 return 1;
375 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800376
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800377 /* Read back */
378 memset(&p, 0, sizeof(p));
379 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
380 &p, sizeof(p), &r, sizeof(r));
381 if (rc < 0) {
382 msg_perr("Cannot get the write protection status: %d\n", rc);
383 return 1;
384 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800385
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800386 if ((r.flags & mask) != flag) {
387 msg_perr("WP status is not expected (0x%x). actual: 0x%x\n",
388 flag, r.flags & mask);
389 return 1;
390 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800391
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800392 return 0;
393}
394
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800395static int gec_set_range(const struct flashchip *flash,
396 unsigned int start, unsigned int len) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800397 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
398 struct ec_params_flash_region_info p;
399 struct ec_response_flash_region_info r;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800400 int rc;
401
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800402 /* Check if the given range is supported */
403 p.region = EC_FLASH_REGION_WP_RO;
404 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
405 EC_VER_FLASH_REGION_INFO, &p, sizeof(p), &r, sizeof(r));
406 if (rc < 0) {
407 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
408 return 1;
409 }
410 if ((!start && !len) || /* list supported ranges */
411 ((start == r.offset) && (len == r.size))) {
412 /* pass */
413 } else {
414 msg_perr("Unsupported write protection range (0x%06x,0x%06x)\n",
415 start, len);
416 msg_perr("Currently supported range:\n");
417 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
418 msg_perr(" enable: (0x%06x,0x%06x)\n", r.offset, r.size);
419 return 1;
420 }
421
422 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800423}
424
425
426static int gec_enable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800427 return set_wp(1);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800428}
429
430
431static int gec_disable_writeprotect(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800432 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800433}
434
435
436static int gec_wp_status(const struct flashchip *flash) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800437 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
438 struct ec_params_flash_protect p;
439 struct ec_response_flash_protect r;
440 int start, len; /* wp range */
441 int enabled;
442 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800443
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800444 memset(&p, 0, sizeof(p));
445 rc = priv->ec_command(EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
446 &p, sizeof(p), &r, sizeof(r));
447 if (rc < 0) {
448 msg_perr("Cannot get the write protection status: %d\n", rc);
449 return 1;
450 } else if (rc < sizeof(r)) {
451 msg_perr("Too little data returned (expected:%d, actual:%d)\n",
452 sizeof(r), rc);
453 return 1;
454 }
455
456 start = len = 0;
457 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
458 struct ec_params_flash_region_info reg_p;
459 struct ec_response_flash_region_info reg_r;
460
461 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
462 __func__);
463 reg_p.region = EC_FLASH_REGION_WP_RO;
464 rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO,
465 EC_VER_FLASH_REGION_INFO,
466 &reg_p, sizeof(reg_p), &reg_r, sizeof(reg_r));
467 if (rc < 0) {
468 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
469 return 1;
470 }
471 start = reg_r.offset;
472 len = reg_r.size;
473 } else {
474 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
475 __func__);
476 }
477
478 /* Remove the SPI-style messages. */
479 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
480 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
481 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800482 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800483 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800484 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800485 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800486
487 return 0;
488}
489
490
David Hendricks7cfbd022012-05-20 17:25:51 -0700491int gec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800492 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800493 struct ec_response_flash_info info;
David Hendricks7cfbd022012-05-20 17:25:51 -0700494 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800495 struct block_eraser *eraser;
496 static struct wp wp = {
497 .list_ranges = gec_list_ranges,
498 .set_range = gec_set_range,
499 .enable = gec_enable_writeprotect,
500 .disable = gec_disable_writeprotect,
501 .wp_status = gec_wp_status,
502 };
503
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800504 rc = priv->ec_command(EC_CMD_FLASH_INFO, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700505 NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800506 if (rc < 0) {
507 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
508 return 0;
509 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800510
511 flash->total_size = info.flash_size / 1024;
David Hendricks0d3fcb52012-07-08 18:37:43 -0700512 flash->page_size = 64;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800513 flash->tested = TEST_OK_PREW;
514 eraser = &flash->block_erasers[0];
515 eraser->eraseblocks[0].size = info.erase_block_size;
516 eraser->eraseblocks[0].count = info.flash_size /
517 eraser->eraseblocks[0].size;
518 flash->wp = &wp;
519
520 return 1;
521};