David Hendricks | ee71247 | 2012-05-23 21:50:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <unistd.h> |
| 39 | #include "flashchips.h" |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 40 | #include "fmap.h" |
Louis Yung-Chieh Lo | c050524 | 2012-08-09 23:11:32 +0800 | [diff] [blame] | 41 | #include "gec_lock.h" |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 42 | #include "gec_ec_commands.h" |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 43 | #include "programmer.h" |
| 44 | #include "spi.h" |
| 45 | #include "writeprotect.h" |
| 46 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 47 | /* FIXME: used for wp hacks */ |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <fcntl.h> |
| 51 | #include <unistd.h> |
| 52 | struct wp_data { |
| 53 | int enable; |
| 54 | unsigned int start; |
| 55 | unsigned int len; |
| 56 | }; |
| 57 | static struct wp_data fake_wp; |
| 58 | #define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state" |
| 59 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 60 | /* 1 if we want the flashrom to call erase_and_write_flash() again. */ |
| 61 | static int need_2nd_pass = 0; |
| 62 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 63 | /* 1 if we want the flashrom to try jumping to new firmware after update. */ |
| 64 | static int try_latest_firmware = 0; |
| 65 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 66 | /* 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 | */ |
| 70 | static 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 Hendricks | bf8c4dd | 2012-07-19 12:13:17 -0700 | [diff] [blame] | 73 | static const char *sections[3] = { |
| 74 | "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches |
| 75 | "EC_RO", |
| 76 | "EC_RW", |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 77 | }; |
| 78 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 79 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 80 | /* Given the range not able to update, mark the corresponding |
| 81 | * firmware as old. |
| 82 | */ |
| 83 | static void gec_invalidate_copy(unsigned int addr, unsigned int len) |
| 84 | { |
| 85 | int i; |
| 86 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 87 | for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 88 | 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 Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 99 | /* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN, |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 100 | * 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 Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 105 | static int gec_jump_copy(enum ec_current_image target) { |
| 106 | struct ec_response_get_version c; |
| 107 | struct ec_params_reboot_ec p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 108 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 109 | int rc; |
| 110 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 111 | /* 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 Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 121 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 122 | 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 Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 136 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 137 | if (rc < 0) { |
| 138 | msg_perr("GEC cannot jump to [%s]:%d\n", |
| 139 | sections[p.cmd], rc); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 140 | } else { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 141 | msg_pdbg("GEC has jumped to [%s]\n", sections[p.cmd]); |
| 142 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 143 | } |
| 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 | */ |
| 155 | int gec_prepare(uint8_t *image, int size) { |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 156 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 157 | struct fmap *fmap; |
| 158 | int i, j; |
| 159 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 160 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 161 | |
| 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 Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 169 | for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) { |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 170 | if (!strcmp(sections[j], (const char *)fa->name)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 171 | 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 Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 178 | /* 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 Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 181 | return gec_jump_copy(EC_IMAGE_RO); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 182 | } |
| 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 | */ |
| 191 | int gec_need_2nd_pass(void) { |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 192 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
| 193 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 194 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 195 | |
| 196 | if (need_2nd_pass) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 197 | if (gec_jump_copy(EC_IMAGE_UNKNOWN)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 198 | return -1; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return need_2nd_pass; |
| 203 | } |
| 204 | |
| 205 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 206 | /* 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 | */ |
| 214 | int 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 Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 220 | 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 Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 229 | int gec_read(struct flashchip *flash, uint8_t *readarr, |
| 230 | unsigned int blockaddr, unsigned int readcnt) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 231 | int rc = 0; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 232 | struct ec_params_flash_read p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 233 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 234 | int maxlen = opaque_programmer->max_data_read; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 235 | uint8_t buf[maxlen]; |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 236 | int offset = 0, count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 237 | |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 238 | while (offset < readcnt) { |
| 239 | count = min(maxlen, readcnt - offset); |
| 240 | p.offset = blockaddr + offset; |
| 241 | p.size = count; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 242 | rc = priv->ec_command(EC_CMD_FLASH_READ, 0, |
| 243 | &p, sizeof(p), buf, count); |
| 244 | if (rc < 0) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 245 | msg_perr("GEC: Flash read error at offset 0x%x\n", |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 246 | blockaddr + offset); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 247 | return rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 248 | } else { |
| 249 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 250 | } |
| 251 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 252 | memcpy(readarr + offset, buf, count); |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 253 | offset += count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 260 | int gec_block_erase(struct flashchip *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 261 | unsigned int blockaddr, |
| 262 | unsigned int len) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 263 | struct ec_params_flash_erase erase; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 264 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 265 | int rc; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 266 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 267 | erase.offset = blockaddr; |
| 268 | erase.size = len; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 269 | rc = priv->ec_command(EC_CMD_FLASH_ERASE, 0, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 270 | &erase, sizeof(erase), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 271 | if (rc == -EC_RES_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 272 | // this is active image. |
| 273 | gec_invalidate_copy(blockaddr, len); |
| 274 | need_2nd_pass = 1; |
| 275 | return ACCESS_DENIED; |
| 276 | } |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 277 | if (rc < 0) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 278 | msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n", |
| 279 | blockaddr, rc); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 280 | return rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 281 | } else { |
| 282 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 283 | } |
| 284 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 285 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 286 | return rc; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | int 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 Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 294 | struct ec_params_flash_write p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 295 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 296 | int maxlen = opaque_programmer->max_data_write; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 297 | |
| 298 | for (i = 0; i < nbytes; i += written) { |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 299 | written = min(nbytes - i, maxlen); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 300 | p.offset = addr + i; |
| 301 | p.size = written; |
| 302 | memcpy(p.data, &buf[i], written); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 303 | rc = priv->ec_command(EC_CMD_FLASH_WRITE, 0, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 304 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 305 | if (rc == -EC_RES_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 306 | // this is active image. |
| 307 | gec_invalidate_copy(addr, nbytes); |
| 308 | need_2nd_pass = 1; |
| 309 | return ACCESS_DENIED; |
| 310 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 311 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 312 | if (rc < 0) break; |
| 313 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 314 | } |
| 315 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 316 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 317 | return rc; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | static int gec_list_ranges(const struct flashchip *flash) { |
| 322 | msg_pinfo("You can specify any range:\n"); |
| 323 | msg_pinfo(" from: 0x%06x, to: 0x%06x\n", 0, flash->total_size * 1024); |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 324 | msg_pinfo(" unit: 0x%06x (%dKB)\n", 2048, 2); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 329 | /* Temporary solution before the real EC WP is ready. This is a hack to |
| 330 | * avoid breaking factory procedure. |
| 331 | * This should be removed after crosbug.com/p/11320 and 11219 are fixed. |
| 332 | */ |
| 333 | static int load_fake_wp(void) { |
| 334 | int fd; |
| 335 | |
| 336 | if ((fd = open(WP_STATE_HACK_FILENAME, O_RDONLY)) == -1) |
| 337 | goto read_err; |
| 338 | |
| 339 | if (read(fd, &fake_wp, sizeof(fake_wp)) != sizeof(fake_wp)) |
| 340 | goto read_err; |
| 341 | |
| 342 | close(fd); |
| 343 | return 0; |
| 344 | |
| 345 | read_err: |
| 346 | memset(&fake_wp, 0 , sizeof(fake_wp)); |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static int save_fake_wp(void) { |
| 351 | int fd; |
| 352 | |
| 353 | if ((fd = open(WP_STATE_HACK_FILENAME, |
| 354 | O_CREAT | O_TRUNC | O_RDWR, 0644)) == -1) |
| 355 | return -1; |
| 356 | |
| 357 | if (write(fd, &fake_wp, sizeof(fake_wp)) != sizeof(fake_wp)) |
| 358 | return -1; |
| 359 | |
| 360 | close(fd); |
| 361 | return 0; |
| 362 | } |
| 363 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 364 | static int gec_set_range(const struct flashchip *flash, |
| 365 | unsigned int start, unsigned int len) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 366 | /* TODO: update to latest ec_commands.h and reimplement. */ |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 367 | int rc; |
| 368 | |
| 369 | rc = system("crossystem wpsw_cur?1"); |
| 370 | if (rc) /* change-able only when WP pin is de-asserted. */ |
| 371 | return -1; |
| 372 | load_fake_wp(); |
| 373 | fake_wp.start = start; |
| 374 | fake_wp.len = len; |
| 375 | return save_fake_wp(); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
| 379 | static int gec_enable_writeprotect(const struct flashchip *flash) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 380 | /* TODO: update to latest ec_commands.h and reimplement. */ |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 381 | load_fake_wp(); |
| 382 | fake_wp.enable = 1; |
| 383 | return save_fake_wp(); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
| 387 | static int gec_disable_writeprotect(const struct flashchip *flash) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 388 | /* TODO: update to latest ec_commands.h and reimplement. */ |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 389 | int rc; |
| 390 | |
| 391 | rc = system("crossystem wpsw_cur?1"); |
| 392 | if (rc) /* change-able only when WP pin is de-asserted. */ |
| 393 | return -1; |
| 394 | load_fake_wp(); |
| 395 | fake_wp.enable = 0; |
| 396 | return save_fake_wp(); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
| 400 | static int gec_wp_status(const struct flashchip *flash) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 401 | /* |
| 402 | * TODO: update to latest ec_commands.h and reimplement. For now, |
| 403 | * just claim chip is unprotected. |
| 404 | */ |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 405 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 406 | load_fake_wp(); |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 407 | msg_pinfo("WP: status: 0x%02x\n", 0); |
| 408 | msg_pinfo("WP: status.srp0: %x\n", 0); |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 409 | msg_pinfo("WP: write protect is %s.\n", |
| 410 | fake_wp.enable ? "enabled" : "disabled"); |
| 411 | msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n", |
| 412 | fake_wp.start, fake_wp.len); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 413 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 414 | /* TODO: Fix scripts which rely on SPI-specific terminology. */ |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 419 | int gec_probe_size(struct flashchip *flash) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 420 | int rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 421 | struct ec_response_flash_info info; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 422 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 423 | struct block_eraser *eraser; |
| 424 | static struct wp wp = { |
| 425 | .list_ranges = gec_list_ranges, |
| 426 | .set_range = gec_set_range, |
| 427 | .enable = gec_enable_writeprotect, |
| 428 | .disable = gec_disable_writeprotect, |
| 429 | .wp_status = gec_wp_status, |
| 430 | }; |
| 431 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 432 | rc = priv->ec_command(EC_CMD_FLASH_INFO, 0, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 433 | NULL, 0, &info, sizeof(info)); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame^] | 434 | if (rc < 0) { |
| 435 | msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc); |
| 436 | return 0; |
| 437 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 438 | |
| 439 | flash->total_size = info.flash_size / 1024; |
David Hendricks | 0d3fcb5 | 2012-07-08 18:37:43 -0700 | [diff] [blame] | 440 | flash->page_size = 64; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 441 | flash->tested = TEST_OK_PREW; |
| 442 | eraser = &flash->block_erasers[0]; |
| 443 | eraser->eraseblocks[0].size = info.erase_block_size; |
| 444 | eraser->eraseblocks[0].count = info.flash_size / |
| 445 | eraser->eraseblocks[0].size; |
| 446 | flash->wp = ℘ |
| 447 | |
| 448 | return 1; |
| 449 | }; |