blob: c8cf716953d478d1978f5112a5348cfbf6ddd424 [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 Lo1c4b7842012-07-30 18:20:39 +080041#include "gec_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080042#include "programmer.h"
43#include "spi.h"
44#include "writeprotect.h"
45
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080046/* 1 if we want the flashrom to call erase_and_write_flash() again. */
47static int need_2nd_pass = 0;
48
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080049/* 1 if we want the flashrom to try jumping to new firmware after update. */
50static int try_latest_firmware = 0;
51
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080052/* The range of each firmware copy from the image file to update.
53 * But re-define the .flags as the valid flag to indicate the firmware is
54 * new or not (if flags = 1).
55 */
56static struct fmap_area fwcopy[4]; // [0] is not used.
57
58/* The names of enum lpc_current_image to match in FMAP area names. */
David Hendricksbf8c4dd2012-07-19 12:13:17 -070059static const char *sections[3] = {
60 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
61 "EC_RO",
62 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080063};
64
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080065
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080066/* Given the range not able to update, mark the corresponding
67 * firmware as old.
68 */
69static void gec_invalidate_copy(unsigned int addr, unsigned int len)
70{
71 int i;
72
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +080073 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080074 struct fmap_area *fw = &fwcopy[i];
75 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
76 (fw->offset >= addr && (fw->offset < addr + len))) {
77 msg_pdbg("Mark firmware [%s] as old.\n",
78 sections[i]);
79 fw->flags = 0; // mark as old
80 }
81 }
82}
83
84
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +080085/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080086 * then this functions picks a NEW firmware copy and jumps to it. Note that
87 * RO is preferred, then A, finally B.
88 *
89 * Returns 0 for success.
90 */
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +080091static int gec_jump_copy(enum ec_current_image target) {
92 struct ec_params_reboot_ec p;
David Hendricks7cfbd022012-05-20 17:25:51 -070093 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080094 int rc;
95
Louis Yung-Chieh Lobb128ba2012-05-04 22:57:44 +080096 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +080097 p.cmd = target != EC_IMAGE_UNKNOWN ? target :
98 fwcopy[EC_IMAGE_RO].flags ? EC_IMAGE_RO :
99 fwcopy[EC_IMAGE_RW].flags ? EC_IMAGE_RW :
100 EC_IMAGE_UNKNOWN;
101 msg_pdbg("GEC is jumping to [%s]\n", sections[p.cmd]);
102 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800103
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800104 rc = priv->ec_command(EC_CMD_REBOOT_EC, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700105 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800106 if (rc < 0) {
107 msg_perr("GEC cannot jump to [%s]:%d\n",
108 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800109 } else {
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800110 msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]);
111 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800112 }
113
114 /* Sleep 1 sec to wait the EC re-init. */
115 usleep(1000000);
116
117 return rc;
118}
119
120
121/* Given an image, this function parses FMAP and recognize the firmware
122 * ranges.
123 */
124int gec_prepare(uint8_t *image, int size) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700125 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800126 struct fmap *fmap;
127 int i, j;
128
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800129 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800130
131 // Parse the fmap in the image file and cache the firmware ranges.
132 fmap = fmap_find_in_memory(image, size);
133 if (!fmap) return 0;
134
135 // Lookup RO/A/B sections in FMAP.
136 for (i = 0; i < fmap->nareas; i++) {
137 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800138 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700139 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800140 msg_pdbg("Found '%s' in image.\n", fa->name);
141 memcpy(&fwcopy[j], fa, sizeof(*fa));
142 fwcopy[j].flags = 1; // mark as new
143 }
144 }
145 }
146
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800147 /* Warning: before update, we jump the EC to RO copy. If you want to
148 * change this behavior, please also check the gec_finish().
149 */
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800150 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800151}
152
153
154/* Returns >0 if we need 2nd pass of erase_and_write_flash().
155 * <0 if we cannot jump to any firmware copy.
156 * ==0 if no more pass is needed.
157 *
158 * This function also jumps to new-updated firmware copy before return >0.
159 */
160int gec_need_2nd_pass(void) {
David Hendricks7cfbd022012-05-20 17:25:51 -0700161 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
162
Louis Yung-Chieh Lo0eaa0ca2012-05-29 15:28:58 +0800163 if (!(priv && priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800164
165 if (need_2nd_pass) {
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800166 if (gec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800167 return -1;
168 }
169 }
170
171 return need_2nd_pass;
172}
173
174
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800175/* Returns 0 for success.
176 *
177 * Try latest firmware: B > A > RO
178 *
179 * This function assumes the EC jumps to RO at gec_prepare() so that
180 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
181 * this code logic if you change the gec_prepare() behavior.
182 */
183int gec_finish(void) {
184 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
185
186 if (!(priv && priv->detected)) return 0;
187
188 if (try_latest_firmware) {
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800189 if (fwcopy[EC_IMAGE_RW].flags &&
190 gec_jump_copy(EC_IMAGE_RW) == 0) return 0;
191 return gec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800192 }
193
194 return 0;
195}
196
197
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800198int gec_read(struct flashchip *flash, uint8_t *readarr,
199 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800200 int rc = 0;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800201 struct ec_params_flash_read p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700202 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700203 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800204 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700205 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800206
David Hendricks133083b2012-07-17 20:39:38 -0700207 while (offset < readcnt) {
208 count = min(maxlen, readcnt - offset);
209 p.offset = blockaddr + offset;
210 p.size = count;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800211 rc = priv->ec_command(EC_CMD_FLASH_READ, 0,
212 &p, sizeof(p), buf, count);
213 if (rc < 0) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800214 msg_perr("GEC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700215 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800216 return rc;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800217 } else {
218 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800219 }
220
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800221 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700222 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800223 }
224
225 return rc;
226}
227
228
David Hendricks7cfbd022012-05-20 17:25:51 -0700229int gec_block_erase(struct flashchip *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800230 unsigned int blockaddr,
231 unsigned int len) {
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800232 struct ec_params_flash_erase erase;
David Hendricks7cfbd022012-05-20 17:25:51 -0700233 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800234 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800235
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800236 erase.offset = blockaddr;
237 erase.size = len;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800238 rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700239 &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800240 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800241 // this is active image.
242 gec_invalidate_copy(blockaddr, len);
243 need_2nd_pass = 1;
244 return ACCESS_DENIED;
245 }
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800246 if (rc < 0) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800247 msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n",
248 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800249 return rc;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800250 } else {
251 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800252 }
253
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800254 try_latest_firmware = 1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800255 return rc;
256}
257
258
259int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr,
260 unsigned int nbytes) {
261 int i, rc = 0;
262 unsigned int written = 0;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800263 struct ec_params_flash_write p;
David Hendricks7cfbd022012-05-20 17:25:51 -0700264 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
David Hendricksd6a0f662012-05-29 14:39:50 -0700265 int maxlen = opaque_programmer->max_data_write;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800266
267 for (i = 0; i < nbytes; i += written) {
David Hendricksd6a0f662012-05-29 14:39:50 -0700268 written = min(nbytes - i, maxlen);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800269 p.offset = addr + i;
270 p.size = written;
271 memcpy(p.data, &buf[i], written);
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800272 rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700273 &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800274 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800275 // this is active image.
276 gec_invalidate_copy(addr, nbytes);
277 need_2nd_pass = 1;
278 return ACCESS_DENIED;
279 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800280
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800281 if (rc < 0) break;
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
290static int gec_list_ranges(const struct flashchip *flash) {
291 msg_pinfo("You can specify any range:\n");
292 msg_pinfo(" from: 0x%06x, to: 0x%06x\n", 0, flash->total_size * 1024);
Randall Spangler6e160ef2012-07-18 09:36:25 -0700293 msg_pinfo(" unit: 0x%06x (%dKB)\n", 2048, 2);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800294 return 0;
295}
296
297
298static int gec_set_range(const struct flashchip *flash,
299 unsigned int start, unsigned int len) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800300
Randall Spangler6e160ef2012-07-18 09:36:25 -0700301 /* TODO: update to latest ec_commands.h and reimplement. */
302 msg_perr("GEC: set_range unimplemented\n");
303 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800304}
305
306
307static int gec_enable_writeprotect(const struct flashchip *flash) {
Randall Spangler6e160ef2012-07-18 09:36:25 -0700308 /* TODO: update to latest ec_commands.h and reimplement. */
309 msg_perr("GEC: enable_writeprotect unimplemented\n");
310 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800311}
312
313
314static int gec_disable_writeprotect(const struct flashchip *flash) {
Randall Spangler6e160ef2012-07-18 09:36:25 -0700315 /* TODO: update to latest ec_commands.h and reimplement. */
316 msg_perr("GEC: disable_writeprotect unimplemented\n");
317 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800318}
319
320
321static int gec_wp_status(const struct flashchip *flash) {
Randall Spangler6e160ef2012-07-18 09:36:25 -0700322 /*
323 * TODO: update to latest ec_commands.h and reimplement. For now,
324 * just claim chip is unprotected.
325 */
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800326
327 /* TODO: Fix scripts which rely on SPI-specific terminology. */
Randall Spangler6e160ef2012-07-18 09:36:25 -0700328 msg_pinfo("WP: status: 0x%02x\n", 0);
329 msg_pinfo("WP: status.srp0: %x\n", 0);
330 msg_pinfo("WP: write protect is %s.\n", "disabled");
331 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n", 0, 0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800332
333 return 0;
334}
335
336
David Hendricks7cfbd022012-05-20 17:25:51 -0700337int gec_probe_size(struct flashchip *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800338 int rc;
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800339 struct ec_response_flash_info info;
David Hendricks7cfbd022012-05-20 17:25:51 -0700340 struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800341 struct block_eraser *eraser;
342 static struct wp wp = {
343 .list_ranges = gec_list_ranges,
344 .set_range = gec_set_range,
345 .enable = gec_enable_writeprotect,
346 .disable = gec_disable_writeprotect,
347 .wp_status = gec_wp_status,
348 };
349
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800350 rc = priv->ec_command(EC_CMD_FLASH_INFO, 0,
David Hendricks7cfbd022012-05-20 17:25:51 -0700351 NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lo1c4b7842012-07-30 18:20:39 +0800352 if (rc < 0) {
353 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
354 return 0;
355 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800356
357 flash->total_size = info.flash_size / 1024;
David Hendricks0d3fcb52012-07-08 18:37:43 -0700358 flash->page_size = 64;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800359 flash->tested = TEST_OK_PREW;
360 eraser = &flash->block_erasers[0];
361 eraser->eraseblocks[0].size = info.erase_block_size;
362 eraser->eraseblocks[0].count = info.flash_size /
363 eraser->eraseblocks[0].size;
364 flash->wp = &wp;
365
366 return 1;
367};