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 | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 41 | #include "gec_lpc_commands.h" |
| 42 | #include "programmer.h" |
| 43 | #include "spi.h" |
| 44 | #include "writeprotect.h" |
| 45 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 46 | /* 1 if we want the flashrom to call erase_and_write_flash() again. */ |
| 47 | static int need_2nd_pass = 0; |
| 48 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 49 | /* 1 if we want the flashrom to try jumping to new firmware after update. */ |
| 50 | static int try_latest_firmware = 0; |
| 51 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 52 | /* 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 | */ |
| 56 | static struct fmap_area fwcopy[4]; // [0] is not used. |
| 57 | |
| 58 | /* The names of enum lpc_current_image to match in FMAP area names. */ |
| 59 | static const char *sections[4] = { |
| 60 | "UNKNOWN SECTION", // EC_LPC_IMAGE_UNKNOWN -- never matches |
| 61 | "RO_SECTION", // EC_LPC_IMAGE_RO |
| 62 | "RW_SECTION_A", // EC_LPC_IMAGE_RW_A |
| 63 | "RW_SECTION_B", // EC_LPC_IMAGE_RW_B |
| 64 | }; |
| 65 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 66 | |
| 67 | #ifdef SUPPORT_CHECKSUM |
| 68 | static verify_checksum(uint8_t* expected, |
| 69 | unsigned int addr, |
| 70 | unsigned int count) { |
| 71 | int rc; |
| 72 | struct lpc_params_flash_checksum csp; |
| 73 | struct lpc_response_flash_checksum csr; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 74 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 75 | uint8_t cs; |
| 76 | int j; |
| 77 | |
| 78 | csp.offset = addr; |
| 79 | csp.size = count; |
| 80 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 81 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_CHECKSUM, |
| 82 | &csp, sizeof(csp), &csr, sizeof(csr)); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 83 | if (rc) { |
| 84 | msg_perr("GEC: verify_checksum() error.\n"); |
| 85 | return rc; |
| 86 | } |
| 87 | |
| 88 | for (cs = 0, j = 0; j < count; ++j) { |
| 89 | BYTE_IN(cs, expected[j]); |
| 90 | } |
| 91 | if (cs != csr.checksum) { |
| 92 | msg_pdbg("GEC: checksum dismatch at 0x%02x " |
| 93 | "(ec: 0x%02x, local: 0x%02x). Retry.\n", |
| 94 | addr, csr.checksum, cs); |
| 95 | msg_pdbg("GEC: "); |
| 96 | for (j = 0; j < count; ++j) { |
| 97 | msg_pdbg("%02x-", expected[j]); |
| 98 | if ((j & 15) == 15) msg_pdbg("\nGEC: "); |
| 99 | } |
| 100 | programmer_delay(1000); |
| 101 | return 1; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | #endif /* SUPPORT_CHECKSUM */ |
| 106 | |
| 107 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 108 | /* Given the range not able to update, mark the corresponding |
| 109 | * firmware as old. |
| 110 | */ |
| 111 | static void gec_invalidate_copy(unsigned int addr, unsigned int len) |
| 112 | { |
| 113 | int i; |
| 114 | |
| 115 | for (i = EC_LPC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) { |
| 116 | struct fmap_area *fw = &fwcopy[i]; |
| 117 | if ((addr >= fw->offset && (addr < fw->offset + fw->size)) || |
| 118 | (fw->offset >= addr && (fw->offset < addr + len))) { |
| 119 | msg_pdbg("Mark firmware [%s] as old.\n", |
| 120 | sections[i]); |
| 121 | fw->flags = 0; // mark as old |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* Asks EC to jump to a firmware copy. If target is EC_LPC_IMAGE_UNKNOWN, |
| 128 | * then this functions picks a NEW firmware copy and jumps to it. Note that |
| 129 | * RO is preferred, then A, finally B. |
| 130 | * |
| 131 | * Returns 0 for success. |
| 132 | */ |
| 133 | static int gec_jump_copy(enum lpc_current_image target) { |
| 134 | struct lpc_params_reboot_ec p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 135 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 136 | int rc; |
| 137 | |
Louis Yung-Chieh Lo | bb128ba | 2012-05-04 22:57:44 +0800 | [diff] [blame] | 138 | memset(&p, 0, sizeof(p)); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 139 | p.target = target != EC_LPC_IMAGE_UNKNOWN ? target : |
| 140 | fwcopy[EC_LPC_IMAGE_RO].flags ? EC_LPC_IMAGE_RO : |
| 141 | fwcopy[EC_LPC_IMAGE_RW_A].flags ? EC_LPC_IMAGE_RW_A : |
| 142 | fwcopy[EC_LPC_IMAGE_RW_B].flags ? EC_LPC_IMAGE_RW_B : |
| 143 | EC_LPC_IMAGE_UNKNOWN; |
| 144 | msg_pdbg("GEC is jumping to [%s]\n", sections[p.target]); |
| 145 | if (p.target == EC_LPC_IMAGE_UNKNOWN) return 1; |
| 146 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 147 | rc = priv->ec_command(EC_LPC_COMMAND_REBOOT_EC, |
| 148 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 149 | if (rc) { |
| 150 | msg_perr("GEC cannot jump to [%s]\n", sections[p.target]); |
| 151 | } else { |
| 152 | msg_pdbg("GEC has jumped to [%s]\n", sections[p.target]); |
| 153 | } |
| 154 | |
| 155 | /* Sleep 1 sec to wait the EC re-init. */ |
| 156 | usleep(1000000); |
| 157 | |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* Given an image, this function parses FMAP and recognize the firmware |
| 163 | * ranges. |
| 164 | */ |
| 165 | int gec_prepare(uint8_t *image, int size) { |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 166 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 167 | struct fmap *fmap; |
| 168 | int i, j; |
| 169 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 170 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 171 | |
| 172 | // Parse the fmap in the image file and cache the firmware ranges. |
| 173 | fmap = fmap_find_in_memory(image, size); |
| 174 | if (!fmap) return 0; |
| 175 | |
| 176 | // Lookup RO/A/B sections in FMAP. |
| 177 | for (i = 0; i < fmap->nareas; i++) { |
| 178 | struct fmap_area *fa = &fmap->areas[i]; |
| 179 | for (j = EC_LPC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) { |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 180 | if (!strcmp(sections[j], (const char *)fa->name)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 181 | msg_pdbg("Found '%s' in image.\n", fa->name); |
| 182 | memcpy(&fwcopy[j], fa, sizeof(*fa)); |
| 183 | fwcopy[j].flags = 1; // mark as new |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 188 | /* Warning: before update, we jump the EC to RO copy. If you want to |
| 189 | * change this behavior, please also check the gec_finish(). |
| 190 | */ |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 191 | return gec_jump_copy(EC_LPC_IMAGE_RO); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* Returns >0 if we need 2nd pass of erase_and_write_flash(). |
| 196 | * <0 if we cannot jump to any firmware copy. |
| 197 | * ==0 if no more pass is needed. |
| 198 | * |
| 199 | * This function also jumps to new-updated firmware copy before return >0. |
| 200 | */ |
| 201 | int gec_need_2nd_pass(void) { |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 202 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
| 203 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 204 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 205 | |
| 206 | if (need_2nd_pass) { |
| 207 | if (gec_jump_copy(EC_LPC_IMAGE_UNKNOWN)) { |
| 208 | return -1; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return need_2nd_pass; |
| 213 | } |
| 214 | |
| 215 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 216 | /* Returns 0 for success. |
| 217 | * |
| 218 | * Try latest firmware: B > A > RO |
| 219 | * |
| 220 | * This function assumes the EC jumps to RO at gec_prepare() so that |
| 221 | * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine |
| 222 | * this code logic if you change the gec_prepare() behavior. |
| 223 | */ |
| 224 | int gec_finish(void) { |
| 225 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
| 226 | |
| 227 | if (!(priv && priv->detected)) return 0; |
| 228 | |
| 229 | if (try_latest_firmware) { |
| 230 | if (fwcopy[EC_LPC_IMAGE_RW_B].flags && |
| 231 | gec_jump_copy(EC_LPC_IMAGE_RW_B) == 0) return 0; |
| 232 | if (fwcopy[EC_LPC_IMAGE_RW_A].flags && |
| 233 | gec_jump_copy(EC_LPC_IMAGE_RW_A) == 0) return 0; |
| 234 | return gec_jump_copy(EC_LPC_IMAGE_RO); |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 241 | int gec_read(struct flashchip *flash, uint8_t *readarr, |
| 242 | unsigned int blockaddr, unsigned int readcnt) { |
| 243 | int i; |
| 244 | int rc = 0; |
| 245 | struct lpc_params_flash_read p; |
| 246 | struct lpc_response_flash_read r; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 247 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 248 | int maxlen = opaque_programmer->max_data_read; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 249 | |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 250 | for (i = 0; i < readcnt; i += maxlen) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 251 | p.offset = blockaddr + i; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 252 | p.size = min(readcnt - i, maxlen); |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 253 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_READ, |
| 254 | &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 255 | if (rc) { |
| 256 | msg_perr("GEC: Flash read error at offset 0x%x\n", |
| 257 | blockaddr + i); |
| 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | #ifdef SUPPORT_CHECKSUM |
| 262 | if (verify_checksum(r.data, blockaddr + i, |
David Hendricks | 0d3fcb5 | 2012-07-08 18:37:43 -0700 | [diff] [blame^] | 263 | min(readcnt - i, sizeof(r.data)))) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 264 | msg_pdbg("GEC: re-read...\n"); |
David Hendricks | 0d3fcb5 | 2012-07-08 18:37:43 -0700 | [diff] [blame^] | 265 | i -= sizeof(r.data); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 266 | continue; |
| 267 | } |
| 268 | #endif |
| 269 | memcpy(readarr + i, r.data, p.size); |
| 270 | } |
| 271 | |
| 272 | return rc; |
| 273 | } |
| 274 | |
| 275 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 276 | int gec_block_erase(struct flashchip *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 277 | unsigned int blockaddr, |
| 278 | unsigned int len) { |
| 279 | struct lpc_params_flash_erase erase; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 280 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 281 | int rc; |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 282 | #ifdef SUPPORT_CHECKSUM |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 283 | uint8_t *blank; |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 284 | #endif |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 285 | |
| 286 | #ifdef SUPPORT_CHECKSUM |
| 287 | re_erase: |
| 288 | #endif |
| 289 | erase.offset = blockaddr; |
| 290 | erase.size = len; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 291 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_ERASE, |
| 292 | &erase, sizeof(erase), NULL, 0); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 293 | if (rc == EC_LPC_RESULT_ACCESS_DENIED) { |
| 294 | // this is active image. |
| 295 | gec_invalidate_copy(blockaddr, len); |
| 296 | need_2nd_pass = 1; |
| 297 | return ACCESS_DENIED; |
| 298 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 299 | if (rc) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 300 | msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n", |
| 301 | blockaddr, rc); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | #ifdef SUPPORT_CHECKSUM |
| 306 | blank = malloc(len); |
| 307 | memset(blank, 0xff, len); |
| 308 | if (verify_checksum(blank, blockaddr, len)) { |
| 309 | msg_pdbg("GEC: Re-erase...\n"); |
| 310 | goto re_erase; |
| 311 | } |
| 312 | #endif |
| 313 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 314 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 315 | return rc; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr, |
| 320 | unsigned int nbytes) { |
| 321 | int i, rc = 0; |
| 322 | unsigned int written = 0; |
| 323 | struct lpc_params_flash_write p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 324 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 325 | int maxlen = opaque_programmer->max_data_write; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 326 | |
| 327 | for (i = 0; i < nbytes; i += written) { |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 328 | written = min(nbytes - i, maxlen); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 329 | p.offset = addr + i; |
| 330 | p.size = written; |
| 331 | memcpy(p.data, &buf[i], written); |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 332 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WRITE, |
| 333 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 334 | if (rc == EC_LPC_RESULT_ACCESS_DENIED) { |
| 335 | // this is active image. |
| 336 | gec_invalidate_copy(addr, nbytes); |
| 337 | need_2nd_pass = 1; |
| 338 | return ACCESS_DENIED; |
| 339 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 340 | |
| 341 | #ifdef SUPPORT_CHECKSUM |
| 342 | if (verify_checksum(&buf[i], addr + i, written)) { |
| 343 | msg_pdbg("GEC: re-write...\n"); |
| 344 | i -= written; |
| 345 | continue; |
| 346 | } |
| 347 | #endif |
| 348 | |
| 349 | if (rc) break; |
| 350 | } |
| 351 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 352 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | static int gec_list_ranges(const struct flashchip *flash) { |
| 358 | msg_pinfo("You can specify any range:\n"); |
| 359 | msg_pinfo(" from: 0x%06x, to: 0x%06x\n", 0, flash->total_size * 1024); |
| 360 | msg_pinfo(" unit: 0x%06x (%dKB)\n", 2048, 2048); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | static int gec_set_range(const struct flashchip *flash, |
| 366 | unsigned int start, unsigned int len) { |
| 367 | struct lpc_params_flash_wp_range p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 368 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 369 | int rc; |
| 370 | |
| 371 | p.offset = start; |
| 372 | p.size = len; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 373 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WP_SET_RANGE, |
| 374 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 375 | if (rc) { |
| 376 | msg_perr("GEC: wp_set_range error: rc=%d\n", rc); |
| 377 | return rc; |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | static int gec_enable_writeprotect(const struct flashchip *flash) { |
| 385 | struct lpc_params_flash_wp_enable p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 386 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 387 | int rc; |
| 388 | |
| 389 | p.enable_wp = 1; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 390 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WP_ENABLE, |
| 391 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 392 | if (rc) { |
| 393 | msg_perr("GEC: wp_enable_wp error: rc=%d\n", rc); |
| 394 | } |
| 395 | |
| 396 | return rc; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static int gec_disable_writeprotect(const struct flashchip *flash) { |
| 401 | struct lpc_params_flash_wp_enable p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 402 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 403 | int rc; |
| 404 | |
| 405 | p.enable_wp = 0; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 406 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WP_ENABLE, |
| 407 | &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 408 | if (rc) { |
| 409 | msg_perr("GEC: wp_disable_wp error: rc=%d\n", rc); |
| 410 | } else { |
| 411 | msg_pinfo("Disabled WP. Reboot EC and de-assert #WP.\n"); |
| 412 | } |
| 413 | |
| 414 | return rc; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | static int gec_wp_status(const struct flashchip *flash) { |
| 419 | int rc; |
| 420 | struct lpc_response_flash_wp_range range; |
| 421 | struct lpc_response_flash_wp_enable en; |
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 | uint8_t value; |
| 424 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 425 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WP_GET_RANGE, |
| 426 | NULL, 0, &range, sizeof(range)); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 427 | if (rc) { |
| 428 | msg_perr("GEC: wp_get_wp_range error: rc=%d\n", rc); |
| 429 | return rc; |
| 430 | } |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 431 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WP_GET_STATE, |
| 432 | NULL, 0, &en, sizeof(en)); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 433 | if (rc) { |
| 434 | msg_perr("GEC: wp_get_wp_state error: rc=%d\n", rc); |
| 435 | return rc; |
| 436 | } |
| 437 | |
| 438 | /* TODO: Fix scripts which rely on SPI-specific terminology. */ |
| 439 | value = (en.enable_wp << 7); |
| 440 | msg_pinfo("WP: status: 0x%02x\n", value); |
| 441 | msg_pinfo("WP: status.srp0: %x\n", en.enable_wp); |
| 442 | msg_pinfo("WP: write protect is %s.\n", |
| 443 | en.enable_wp ? "enabled" : "disabled"); |
| 444 | msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n", |
| 445 | range.offset, range.size); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 451 | int gec_probe_size(struct flashchip *flash) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 452 | int rc; |
| 453 | struct lpc_response_flash_info info; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 454 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 455 | struct block_eraser *eraser; |
| 456 | static struct wp wp = { |
| 457 | .list_ranges = gec_list_ranges, |
| 458 | .set_range = gec_set_range, |
| 459 | .enable = gec_enable_writeprotect, |
| 460 | .disable = gec_disable_writeprotect, |
| 461 | .wp_status = gec_wp_status, |
| 462 | }; |
| 463 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 464 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_INFO, |
| 465 | NULL, 0, &info, sizeof(info)); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 466 | if (rc) return 0; |
| 467 | |
| 468 | flash->total_size = info.flash_size / 1024; |
David Hendricks | 0d3fcb5 | 2012-07-08 18:37:43 -0700 | [diff] [blame^] | 469 | flash->page_size = 64; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 470 | flash->tested = TEST_OK_PREW; |
| 471 | eraser = &flash->block_erasers[0]; |
| 472 | eraser->eraseblocks[0].size = info.erase_block_size; |
| 473 | eraser->eraseblocks[0].count = info.flash_size / |
| 474 | eraser->eraseblocks[0].size; |
| 475 | flash->wp = ℘ |
| 476 | |
| 477 | return 1; |
| 478 | }; |