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" |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 42 | #include "gec_lpc_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 | |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 87 | for (i = EC_LPC_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 | |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 99 | /* Asks EC to jump to a firmware copy. If target is EC_LPC_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 | */ |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 105 | static int gec_jump_copy(enum lpc_current_image target) { |
| 106 | struct lpc_params_reboot_ec p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 107 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 108 | int rc; |
| 109 | |
Louis Yung-Chieh Lo | bb128ba | 2012-05-04 22:57:44 +0800 | [diff] [blame] | 110 | memset(&p, 0, sizeof(p)); |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 111 | p.target = target != EC_LPC_IMAGE_UNKNOWN ? target : |
| 112 | fwcopy[EC_LPC_IMAGE_RO].flags ? EC_LPC_IMAGE_RO : |
| 113 | fwcopy[EC_LPC_IMAGE_RW].flags ? EC_LPC_IMAGE_RW : |
| 114 | EC_LPC_IMAGE_UNKNOWN; |
| 115 | msg_pdbg("GEC is jumping to [%s]\n", sections[p.target]); |
| 116 | if (p.target == EC_LPC_IMAGE_UNKNOWN) return 1; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 117 | |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 118 | rc = priv->ec_command(EC_LPC_COMMAND_REBOOT_EC, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 119 | &p, sizeof(p), NULL, 0); |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 120 | if (rc) { |
| 121 | msg_perr("GEC cannot jump to [%s]\n", sections[p.target]); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 122 | } else { |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 123 | msg_pdbg("GEC has jumped to [%s]\n", sections[p.target]); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* Sleep 1 sec to wait the EC re-init. */ |
| 127 | usleep(1000000); |
| 128 | |
| 129 | return rc; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* Given an image, this function parses FMAP and recognize the firmware |
| 134 | * ranges. |
| 135 | */ |
| 136 | int gec_prepare(uint8_t *image, int size) { |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 137 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 138 | struct fmap *fmap; |
| 139 | int i, j; |
| 140 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 141 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 142 | |
| 143 | // Parse the fmap in the image file and cache the firmware ranges. |
| 144 | fmap = fmap_find_in_memory(image, size); |
| 145 | if (!fmap) return 0; |
| 146 | |
| 147 | // Lookup RO/A/B sections in FMAP. |
| 148 | for (i = 0; i < fmap->nareas; i++) { |
| 149 | struct fmap_area *fa = &fmap->areas[i]; |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 150 | for (j = EC_LPC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) { |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 151 | if (!strcmp(sections[j], (const char *)fa->name)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 152 | msg_pdbg("Found '%s' in image.\n", fa->name); |
| 153 | memcpy(&fwcopy[j], fa, sizeof(*fa)); |
| 154 | fwcopy[j].flags = 1; // mark as new |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 159 | /* Warning: before update, we jump the EC to RO copy. If you want to |
| 160 | * change this behavior, please also check the gec_finish(). |
| 161 | */ |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 162 | return gec_jump_copy(EC_LPC_IMAGE_RO); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
| 166 | /* Returns >0 if we need 2nd pass of erase_and_write_flash(). |
| 167 | * <0 if we cannot jump to any firmware copy. |
| 168 | * ==0 if no more pass is needed. |
| 169 | * |
| 170 | * This function also jumps to new-updated firmware copy before return >0. |
| 171 | */ |
| 172 | int gec_need_2nd_pass(void) { |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 173 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
| 174 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 175 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 176 | |
| 177 | if (need_2nd_pass) { |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 178 | if (gec_jump_copy(EC_LPC_IMAGE_UNKNOWN)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 179 | return -1; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return need_2nd_pass; |
| 184 | } |
| 185 | |
| 186 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 187 | /* Returns 0 for success. |
| 188 | * |
| 189 | * Try latest firmware: B > A > RO |
| 190 | * |
| 191 | * This function assumes the EC jumps to RO at gec_prepare() so that |
| 192 | * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine |
| 193 | * this code logic if you change the gec_prepare() behavior. |
| 194 | */ |
| 195 | int gec_finish(void) { |
| 196 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
| 197 | |
| 198 | if (!(priv && priv->detected)) return 0; |
| 199 | |
| 200 | if (try_latest_firmware) { |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 201 | if (fwcopy[EC_LPC_IMAGE_RW].flags && |
| 202 | gec_jump_copy(EC_LPC_IMAGE_RW) == 0) return 0; |
| 203 | return gec_jump_copy(EC_LPC_IMAGE_RO); |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 210 | int gec_read(struct flashchip *flash, uint8_t *readarr, |
| 211 | unsigned int blockaddr, unsigned int readcnt) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 212 | int rc = 0; |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 213 | struct lpc_params_flash_read p; |
| 214 | struct lpc_response_flash_read r; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 215 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 216 | int maxlen = opaque_programmer->max_data_read; |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 217 | int offset = 0, count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 218 | |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 219 | while (offset < readcnt) { |
| 220 | count = min(maxlen, readcnt - offset); |
| 221 | p.offset = blockaddr + offset; |
| 222 | p.size = count; |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 223 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_READ, |
| 224 | &p, sizeof(p), &r, count); |
| 225 | if (rc) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 226 | msg_perr("GEC: Flash read error at offset 0x%x\n", |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 227 | blockaddr + offset); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 228 | return rc; |
| 229 | } |
| 230 | |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 231 | memcpy(readarr + offset, r.data, count); |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 232 | offset += count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | return rc; |
| 236 | } |
| 237 | |
| 238 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 239 | int gec_block_erase(struct flashchip *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 240 | unsigned int blockaddr, |
| 241 | unsigned int len) { |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 242 | struct lpc_params_flash_erase erase; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 243 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 244 | int rc; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 245 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 246 | erase.offset = blockaddr; |
| 247 | erase.size = len; |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 248 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_ERASE, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 249 | &erase, sizeof(erase), NULL, 0); |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 250 | if (rc == EC_LPC_RESULT_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 251 | // this is active image. |
| 252 | gec_invalidate_copy(blockaddr, len); |
| 253 | need_2nd_pass = 1; |
| 254 | return ACCESS_DENIED; |
| 255 | } |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 256 | if (rc) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 257 | msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n", |
| 258 | blockaddr, rc); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 259 | return rc; |
| 260 | } |
| 261 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 262 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 263 | return rc; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr, |
| 268 | unsigned int nbytes) { |
| 269 | int i, rc = 0; |
| 270 | unsigned int written = 0; |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 271 | struct lpc_params_flash_write p; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 272 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 273 | int maxlen = opaque_programmer->max_data_write; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 274 | |
| 275 | for (i = 0; i < nbytes; i += written) { |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 276 | written = min(nbytes - i, maxlen); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 277 | p.offset = addr + i; |
| 278 | p.size = written; |
| 279 | memcpy(p.data, &buf[i], written); |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 280 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_WRITE, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 281 | &p, sizeof(p), NULL, 0); |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 282 | if (rc == EC_LPC_RESULT_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 283 | // this is active image. |
| 284 | gec_invalidate_copy(addr, nbytes); |
| 285 | need_2nd_pass = 1; |
| 286 | return ACCESS_DENIED; |
| 287 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 288 | |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 289 | if (rc) break; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 290 | } |
| 291 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 292 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 293 | return rc; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static int gec_list_ranges(const struct flashchip *flash) { |
| 298 | msg_pinfo("You can specify any range:\n"); |
| 299 | 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] | 300 | msg_pinfo(" unit: 0x%06x (%dKB)\n", 2048, 2); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 305 | /* Temporary solution before the real EC WP is ready. This is a hack to |
| 306 | * avoid breaking factory procedure. |
| 307 | * This should be removed after crosbug.com/p/11320 and 11219 are fixed. |
| 308 | */ |
| 309 | static int load_fake_wp(void) { |
| 310 | int fd; |
| 311 | |
| 312 | if ((fd = open(WP_STATE_HACK_FILENAME, O_RDONLY)) == -1) |
| 313 | goto read_err; |
| 314 | |
| 315 | if (read(fd, &fake_wp, sizeof(fake_wp)) != sizeof(fake_wp)) |
| 316 | goto read_err; |
| 317 | |
| 318 | close(fd); |
| 319 | return 0; |
| 320 | |
| 321 | read_err: |
| 322 | memset(&fake_wp, 0 , sizeof(fake_wp)); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int save_fake_wp(void) { |
| 327 | int fd; |
| 328 | |
| 329 | if ((fd = open(WP_STATE_HACK_FILENAME, |
| 330 | O_CREAT | O_TRUNC | O_RDWR, 0644)) == -1) |
| 331 | return -1; |
| 332 | |
| 333 | if (write(fd, &fake_wp, sizeof(fake_wp)) != sizeof(fake_wp)) |
| 334 | return -1; |
| 335 | |
| 336 | close(fd); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 340 | static int gec_set_range(const struct flashchip *flash, |
| 341 | unsigned int start, unsigned int len) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 342 | /* TODO: update to latest ec_commands.h and reimplement. */ |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 343 | int rc; |
| 344 | |
| 345 | rc = system("crossystem wpsw_cur?1"); |
| 346 | if (rc) /* change-able only when WP pin is de-asserted. */ |
| 347 | return -1; |
| 348 | load_fake_wp(); |
| 349 | fake_wp.start = start; |
| 350 | fake_wp.len = len; |
| 351 | return save_fake_wp(); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
| 355 | static int gec_enable_writeprotect(const struct flashchip *flash) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 356 | /* TODO: update to latest ec_commands.h and reimplement. */ |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 357 | load_fake_wp(); |
| 358 | fake_wp.enable = 1; |
| 359 | return save_fake_wp(); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
| 363 | static int gec_disable_writeprotect(const struct flashchip *flash) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 364 | /* TODO: update to latest ec_commands.h and reimplement. */ |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 365 | int rc; |
| 366 | |
| 367 | rc = system("crossystem wpsw_cur?1"); |
| 368 | if (rc) /* change-able only when WP pin is de-asserted. */ |
| 369 | return -1; |
| 370 | load_fake_wp(); |
| 371 | fake_wp.enable = 0; |
| 372 | return save_fake_wp(); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | |
| 376 | static int gec_wp_status(const struct flashchip *flash) { |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 377 | /* |
| 378 | * TODO: update to latest ec_commands.h and reimplement. For now, |
| 379 | * just claim chip is unprotected. |
| 380 | */ |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 381 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 382 | load_fake_wp(); |
Randall Spangler | 6e160ef | 2012-07-18 09:36:25 -0700 | [diff] [blame] | 383 | msg_pinfo("WP: status: 0x%02x\n", 0); |
| 384 | msg_pinfo("WP: status.srp0: %x\n", 0); |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 385 | msg_pinfo("WP: write protect is %s.\n", |
| 386 | fake_wp.enable ? "enabled" : "disabled"); |
| 387 | msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n", |
| 388 | fake_wp.start, fake_wp.len); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 389 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 390 | /* TODO: Fix scripts which rely on SPI-specific terminology. */ |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 395 | int gec_probe_size(struct flashchip *flash) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 396 | int rc; |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 397 | struct lpc_response_flash_info info; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 398 | struct gec_priv *priv = (struct gec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 399 | struct block_eraser *eraser; |
| 400 | static struct wp wp = { |
| 401 | .list_ranges = gec_list_ranges, |
| 402 | .set_range = gec_set_range, |
| 403 | .enable = gec_enable_writeprotect, |
| 404 | .disable = gec_disable_writeprotect, |
| 405 | .wp_status = gec_wp_status, |
| 406 | }; |
| 407 | |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 408 | rc = priv->ec_command(EC_LPC_COMMAND_FLASH_INFO, |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 409 | NULL, 0, &info, sizeof(info)); |
David Hendricks | cf6e3a3 | 2012-08-01 16:13:02 -0700 | [diff] [blame] | 410 | if (rc) return 0; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 411 | |
| 412 | flash->total_size = info.flash_size / 1024; |
David Hendricks | 0d3fcb5 | 2012-07-08 18:37:43 -0700 | [diff] [blame] | 413 | flash->page_size = 64; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 414 | flash->tested = TEST_OK_PREW; |
| 415 | eraser = &flash->block_erasers[0]; |
| 416 | eraser->eraseblocks[0].size = info.erase_block_size; |
| 417 | eraser->eraseblocks[0].count = info.flash_size / |
| 418 | eraser->eraseblocks[0].size; |
| 419 | flash->wp = ℘ |
| 420 | |
| 421 | return 1; |
| 422 | }; |