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 | */ |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 34 | #include <errno.h> |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 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" |
David Hendricks | a5c5cf8 | 2014-08-11 16:40:17 -0700 | [diff] [blame] | 41 | #include "cros_ec.h" |
| 42 | #include "cros_ec_lock.h" |
| 43 | #include "cros_ec_commands.h" |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 44 | #include "programmer.h" |
| 45 | #include "spi.h" |
| 46 | #include "writeprotect.h" |
| 47 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 48 | /* FIXME: used for wp hacks */ |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
| 51 | #include <fcntl.h> |
| 52 | #include <unistd.h> |
| 53 | struct wp_data { |
| 54 | int enable; |
| 55 | unsigned int start; |
| 56 | unsigned int len; |
| 57 | }; |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 58 | #define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state" |
| 59 | |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 60 | /* If software sync is enabled, then we don't try the latest firmware copy |
| 61 | * after updating. |
| 62 | */ |
| 63 | #define SOFTWARE_SYNC_ENABLED |
| 64 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 65 | #define DEV(priv) (priv->dev_index << 14) |
| 66 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 67 | /* 1 if we want the flashrom to call erase_and_write_flash() again. */ |
| 68 | static int need_2nd_pass = 0; |
| 69 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 70 | /* 1 if we want the flashrom to try jumping to new firmware after update. */ |
| 71 | static int try_latest_firmware = 0; |
| 72 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 73 | /* The range of each firmware copy from the image file to update. |
| 74 | * But re-define the .flags as the valid flag to indicate the firmware is |
| 75 | * new or not (if flags = 1). |
| 76 | */ |
| 77 | static struct fmap_area fwcopy[4]; // [0] is not used. |
| 78 | |
| 79 | /* 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] | 80 | static const char *sections[3] = { |
| 81 | "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches |
| 82 | "EC_RO", |
| 83 | "EC_RW", |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 84 | }; |
| 85 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 86 | /* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates |
| 87 | * the number of regions */ |
| 88 | static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1]; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 89 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 90 | /* Given the range not able to update, mark the corresponding |
| 91 | * firmware as old. |
| 92 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 93 | static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len) |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 94 | { |
| 95 | int i; |
| 96 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 97 | for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 98 | struct fmap_area *fw = &fwcopy[i]; |
| 99 | if ((addr >= fw->offset && (addr < fw->offset + fw->size)) || |
| 100 | (fw->offset >= addr && (fw->offset < addr + len))) { |
| 101 | msg_pdbg("Mark firmware [%s] as old.\n", |
| 102 | sections[i]); |
| 103 | fw->flags = 0; // mark as old |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 109 | static int cros_ec_get_current_image(struct cros_ec_priv *priv) |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 110 | { |
| 111 | struct ec_response_get_version resp; |
| 112 | int rc; |
| 113 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 114 | rc = priv->ec_command(EC_CMD_GET_VERSION | DEV(priv), |
| 115 | 0, NULL, 0, &resp, sizeof(resp)); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 116 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 117 | msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 118 | return rc; |
| 119 | } |
| 120 | if (resp.current_image == EC_IMAGE_UNKNOWN) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 121 | msg_perr("CROS_EC gets unknown running copy\n"); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | return resp.current_image; |
| 126 | } |
| 127 | |
| 128 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 129 | static int cros_ec_get_region_info(struct cros_ec_priv *priv, |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 130 | enum ec_flash_region region, |
| 131 | struct ec_response_flash_region_info *info) |
| 132 | { |
| 133 | struct ec_params_flash_region_info req; |
| 134 | struct ec_response_flash_region_info resp; |
| 135 | int rc; |
| 136 | |
| 137 | req.region = region; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 138 | rc = priv->ec_command(EC_CMD_FLASH_REGION_INFO | DEV(priv), |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 139 | EC_VER_FLASH_REGION_INFO, &req, sizeof(req), |
| 140 | &resp, sizeof(resp)); |
| 141 | if (rc < 0) { |
| 142 | msg_perr("Cannot get the WP_RO region info: %d\n", rc); |
| 143 | return rc; |
| 144 | } |
| 145 | |
| 146 | info->offset = resp.offset; |
| 147 | info->size = resp.size; |
| 148 | return 0; |
| 149 | } |
| 150 | |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Get the versions of the command supported by the EC. |
| 153 | * |
| 154 | * @param cmd Command |
| 155 | * @param pmask Destination for version mask; will be set to 0 on |
| 156 | * error. |
| 157 | * @return 0 if success, <0 if error |
| 158 | */ |
| 159 | static int ec_get_cmd_versions(int cmd, uint32_t *pmask) |
| 160 | { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 161 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 162 | struct ec_params_get_cmd_versions pver; |
| 163 | struct ec_response_get_cmd_versions rver; |
| 164 | int rc; |
| 165 | |
| 166 | *pmask = 0; |
| 167 | |
| 168 | pver.cmd = cmd; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 169 | rc = priv->ec_command(EC_CMD_GET_CMD_VERSIONS | DEV(priv), 0, |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 170 | &pver, sizeof(pver), &rver, sizeof(rver)); |
| 171 | |
| 172 | if (rc < 0) |
| 173 | return rc; |
| 174 | |
| 175 | *pmask = rver.version_mask; |
| 176 | return rc; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Return non-zero if the EC supports the command and version |
| 181 | * |
| 182 | * @param cmd Command to check |
| 183 | * @param ver Version to check |
| 184 | * @return non-zero if command version supported; 0 if not. |
| 185 | */ |
| 186 | static int ec_cmd_version_supported(int cmd, int ver) |
| 187 | { |
| 188 | uint32_t mask = 0; |
| 189 | int rc; |
| 190 | |
| 191 | rc = ec_get_cmd_versions(cmd, &mask); |
| 192 | if (rc < 0) |
| 193 | return rc; |
| 194 | |
| 195 | return (mask & EC_VER_MASK(ver)) ? 1 : 0; |
| 196 | } |
| 197 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 198 | static int cros_ec_set_max_write_size(void) |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 199 | { |
| 200 | int rc; |
| 201 | struct ec_response_flash_info info; |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 202 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 203 | unsigned int pdata_max_size; |
| 204 | |
| 205 | /* |
| 206 | * Determine whether we can use version 1 of the command with more |
| 207 | * data, or only version 0. |
| 208 | */ |
| 209 | rc = ec_cmd_version_supported(EC_CMD_FLASH_WRITE, EC_VER_FLASH_WRITE); |
| 210 | if (rc < 0) |
| 211 | return rc; |
| 212 | else if (rc == 0) |
| 213 | pdata_max_size = EC_FLASH_WRITE_VER0_SIZE; |
| 214 | else |
| 215 | pdata_max_size = EC_PROTO2_MAX_PARAM_SIZE - 8; |
| 216 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 217 | rc = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv), |
| 218 | 0, NULL, 0, &info, sizeof(info)); |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 219 | if (rc < 0) { |
| 220 | msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc); |
| 221 | return rc; |
| 222 | } |
| 223 | |
| 224 | opaque_programmer->max_data_write = |
| 225 | (pdata_max_size/info.write_block_size) * info.write_block_size; |
| 226 | return rc; |
| 227 | } |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 228 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 229 | /* 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] | 230 | * then this functions picks a NEW firmware copy and jumps to it. Note that |
| 231 | * RO is preferred, then A, finally B. |
| 232 | * |
| 233 | * Returns 0 for success. |
| 234 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 235 | static int cros_ec_jump_copy(enum ec_current_image target) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 236 | struct ec_params_reboot_ec p; |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 237 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 238 | int rc; |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 239 | int current_image; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 240 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 241 | /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't |
| 242 | * jump to different firmware copy. The second EC_RES_SUCCESS would |
| 243 | * set the OBF=1 and the next command cannot be executed. |
| 244 | * Thus, we call EC to jump only if the target is different. |
| 245 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 246 | current_image = cros_ec_get_current_image(priv); |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 247 | if (current_image < 0) |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 248 | return 1; |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 249 | if (current_image == target) |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 250 | return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 251 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 252 | memset(&p, 0, sizeof(p)); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 253 | |
| 254 | /* Translate target --> EC reboot command parameter */ |
| 255 | switch (target) { |
| 256 | case EC_IMAGE_RO: |
| 257 | p.cmd = EC_REBOOT_JUMP_RO; |
| 258 | break; |
| 259 | case EC_IMAGE_RW: |
| 260 | p.cmd = EC_REBOOT_JUMP_RW; |
| 261 | break; |
| 262 | default: |
| 263 | /* |
| 264 | * If target is unspecified, set EC reboot command to use |
| 265 | * a new image. Also set "target" so that it may be used |
| 266 | * to update the priv->current_image if jump is successful. |
| 267 | */ |
| 268 | if (fwcopy[EC_IMAGE_RO].flags) { |
| 269 | p.cmd = EC_REBOOT_JUMP_RO; |
| 270 | target = EC_IMAGE_RO; |
| 271 | } else if (fwcopy[EC_IMAGE_RW].flags) { |
| 272 | p.cmd = EC_REBOOT_JUMP_RW; |
| 273 | target = EC_IMAGE_RW; |
| 274 | } else { |
| 275 | p.cmd = EC_IMAGE_UNKNOWN; |
| 276 | } |
| 277 | break; |
| 278 | } |
| 279 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 280 | msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 281 | if (p.cmd == EC_IMAGE_UNKNOWN) return 1; |
| 282 | |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 283 | if (current_image == p.cmd) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 284 | msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 285 | priv->current_image = target; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 289 | rc = priv->ec_command(EC_CMD_REBOOT_EC | DEV(priv), |
| 290 | 0, &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 291 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 292 | msg_perr("CROS_EC cannot jump to [%s]:%d\n", |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 293 | sections[p.cmd], rc); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 294 | } else { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 295 | msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 296 | rc = EC_RES_SUCCESS; |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 297 | priv->current_image = target; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* Sleep 1 sec to wait the EC re-init. */ |
| 301 | usleep(1000000); |
| 302 | |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 303 | /* update max data write size in case we're jumping to an EC |
| 304 | * firmware with different protocol */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 305 | cros_ec_set_max_write_size(); |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 306 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 307 | return rc; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /* Given an image, this function parses FMAP and recognize the firmware |
| 312 | * ranges. |
| 313 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 314 | int cros_ec_prepare(uint8_t *image, int size) { |
| 315 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 316 | struct fmap *fmap; |
| 317 | int i, j; |
| 318 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 319 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 320 | |
| 321 | // Parse the fmap in the image file and cache the firmware ranges. |
| 322 | fmap = fmap_find_in_memory(image, size); |
| 323 | if (!fmap) return 0; |
| 324 | |
| 325 | // Lookup RO/A/B sections in FMAP. |
| 326 | for (i = 0; i < fmap->nareas; i++) { |
| 327 | struct fmap_area *fa = &fmap->areas[i]; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 328 | for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) { |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 329 | if (!strcmp(sections[j], (const char *)fa->name)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 330 | msg_pdbg("Found '%s' in image.\n", fa->name); |
| 331 | memcpy(&fwcopy[j], fa, sizeof(*fa)); |
| 332 | fwcopy[j].flags = 1; // mark as new |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 337 | /* Warning: before update, we jump the EC to RO copy. If you want to |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 338 | * change this behavior, please also check the cros_ec_finish(). |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 339 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 340 | return cros_ec_jump_copy(EC_IMAGE_RO); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | /* Returns >0 if we need 2nd pass of erase_and_write_flash(). |
| 345 | * <0 if we cannot jump to any firmware copy. |
| 346 | * ==0 if no more pass is needed. |
| 347 | * |
| 348 | * This function also jumps to new-updated firmware copy before return >0. |
| 349 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 350 | int cros_ec_need_2nd_pass(void) { |
| 351 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
David Hendricks | 7cfbd02 | 2012-05-20 17:25:51 -0700 | [diff] [blame] | 352 | |
Louis Yung-Chieh Lo | 0eaa0ca | 2012-05-29 15:28:58 +0800 | [diff] [blame] | 353 | if (!(priv && priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 354 | |
| 355 | if (need_2nd_pass) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 356 | if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 357 | return -1; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return need_2nd_pass; |
| 362 | } |
| 363 | |
| 364 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 365 | /* Returns 0 for success. |
| 366 | * |
| 367 | * Try latest firmware: B > A > RO |
| 368 | * |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 369 | * This function assumes the EC jumps to RO at cros_ec_prepare() so that |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 370 | * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 371 | * this code logic if you change the cros_ec_prepare() behavior. |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 372 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 373 | int cros_ec_finish(void) { |
| 374 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 375 | |
| 376 | if (!(priv && priv->detected)) return 0; |
| 377 | |
| 378 | if (try_latest_firmware) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 379 | if (fwcopy[EC_IMAGE_RW].flags && |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 380 | cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0; |
| 381 | return cros_ec_jump_copy(EC_IMAGE_RO); |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 388 | int cros_ec_read(struct flashchip *flash, uint8_t *readarr, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 389 | unsigned int blockaddr, unsigned int readcnt) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 390 | int rc = 0; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 391 | struct ec_params_flash_read p; |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 392 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 393 | int maxlen = opaque_programmer->max_data_read; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 394 | uint8_t buf[maxlen]; |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 395 | int offset = 0, count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 396 | |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 397 | while (offset < readcnt) { |
| 398 | count = min(maxlen, readcnt - offset); |
| 399 | p.offset = blockaddr + offset; |
| 400 | p.size = count; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 401 | rc = priv->ec_command(EC_CMD_FLASH_READ | DEV(priv), |
| 402 | 0, &p, sizeof(p), buf, count); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 403 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 404 | msg_perr("CROS_EC: Flash read error at offset 0x%x\n", |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 405 | blockaddr + offset); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 406 | return rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 407 | } else { |
| 408 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 409 | } |
| 410 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 411 | memcpy(readarr + offset, buf, count); |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 412 | offset += count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return rc; |
| 416 | } |
| 417 | |
| 418 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 419 | /* |
| 420 | * returns 0 to indicate area does not overlap current EC image |
| 421 | * returns 1 to indicate area overlaps current EC image or error |
| 422 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 423 | static int in_current_image(struct cros_ec_priv *priv, |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 424 | unsigned int addr, unsigned int len) |
| 425 | { |
| 426 | int ret; |
| 427 | enum ec_current_image image; |
| 428 | uint32_t region_offset; |
| 429 | uint32_t region_size; |
| 430 | |
| 431 | image = priv->current_image; |
| 432 | region_offset = priv->region[image].offset; |
| 433 | region_size = priv->region[image].size; |
| 434 | |
| 435 | if ((addr + len - 1 < region_offset) || |
| 436 | (addr > region_offset + region_size - 1)) { |
| 437 | return 0; |
| 438 | } |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 443 | int cros_ec_block_erase(struct flashchip *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 444 | unsigned int blockaddr, |
| 445 | unsigned int len) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 446 | struct ec_params_flash_erase erase; |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 447 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 448 | int rc; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 449 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 450 | if (in_current_image(priv, blockaddr, len)) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 451 | cros_ec_invalidate_copy(blockaddr, len); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 452 | need_2nd_pass = 1; |
| 453 | return ACCESS_DENIED; |
| 454 | } |
| 455 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 456 | erase.offset = blockaddr; |
| 457 | erase.size = len; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 458 | rc = priv->ec_command(EC_CMD_FLASH_ERASE | DEV(priv), |
| 459 | 0, &erase, sizeof(erase), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 460 | if (rc == -EC_RES_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 461 | // this is active image. |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 462 | cros_ec_invalidate_copy(blockaddr, len); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 463 | need_2nd_pass = 1; |
| 464 | return ACCESS_DENIED; |
| 465 | } |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 466 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 467 | msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n", |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 468 | blockaddr, rc); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 469 | return rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 470 | } else { |
| 471 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 472 | } |
| 473 | |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 474 | #ifndef SOFTWARE_SYNC_ENABLED |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 475 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 476 | #endif |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 477 | return rc; |
| 478 | } |
| 479 | |
| 480 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 481 | int cros_ec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 482 | unsigned int nbytes) { |
| 483 | int i, rc = 0; |
| 484 | unsigned int written = 0; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 485 | struct ec_params_flash_write p; |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 486 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 487 | int maxlen = opaque_programmer->max_data_write; |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 488 | uint8_t *packet; |
| 489 | |
| 490 | packet = malloc(sizeof(p) + maxlen); |
| 491 | if (!packet) |
| 492 | return -1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 493 | |
| 494 | for (i = 0; i < nbytes; i += written) { |
David Hendricks | d6a0f66 | 2012-05-29 14:39:50 -0700 | [diff] [blame] | 495 | written = min(nbytes - i, maxlen); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 496 | p.offset = addr + i; |
| 497 | p.size = written; |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 498 | |
| 499 | if (in_current_image(priv, p.offset, p.size)) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 500 | cros_ec_invalidate_copy(addr, nbytes); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 501 | need_2nd_pass = 1; |
| 502 | return ACCESS_DENIED; |
| 503 | } |
| 504 | |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 505 | memcpy(packet, &p, sizeof(p)); |
| 506 | memcpy(packet + sizeof(p), &buf[i], written); |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 507 | rc = priv->ec_command(EC_CMD_FLASH_WRITE | DEV(priv), |
| 508 | 0, packet, sizeof(p) + p.size, NULL, 0); |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 509 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 510 | if (rc == -EC_RES_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 511 | // this is active image. |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 512 | cros_ec_invalidate_copy(addr, nbytes); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 513 | need_2nd_pass = 1; |
| 514 | return ACCESS_DENIED; |
| 515 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 516 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 517 | if (rc < 0) break; |
| 518 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 519 | } |
| 520 | |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 521 | #ifndef SOFTWARE_SYNC_ENABLED |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 522 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 523 | #endif |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 524 | free(packet); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 525 | return rc; |
| 526 | } |
| 527 | |
| 528 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 529 | static int cros_ec_list_ranges(const struct flashchip *flash) { |
| 530 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 531 | struct ec_response_flash_region_info info; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 532 | int rc; |
| 533 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 534 | rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 535 | if (rc < 0) { |
| 536 | msg_perr("Cannot get the WP_RO region info: %d\n", rc); |
| 537 | return 1; |
| 538 | } |
| 539 | |
| 540 | msg_pinfo("Supported write protect range:\n"); |
| 541 | msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0); |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 542 | msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset, |
| 543 | info.size); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 544 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 549 | /* |
| 550 | * Helper function for flash protection. |
| 551 | * |
| 552 | * On EC API v1, the EC write protection has been simplified to one-bit: |
| 553 | * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled |
| 554 | * or disabled. However, this is different from the SPI-style write protect |
| 555 | * behavior. Thus, we re-define the flashrom command (SPI-style) so that |
| 556 | * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set. |
| 557 | * |
| 558 | * SRP Range | PROTECT_RO_AT_BOOT |
| 559 | * 0 0 | 0 |
| 560 | * 0 non-zero | 1 |
| 561 | * 1 0 | 1 |
| 562 | * 1 non-zero | 1 |
| 563 | * |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 564 | * |
| 565 | * Besides, to make the protection take effect as soon as possible, we |
| 566 | * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not |
| 567 | * every EC supports RO_NOW, thus we then try to protect the entire chip. |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 568 | */ |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 569 | static int set_wp(int enable) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 570 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 571 | struct ec_params_flash_protect p; |
| 572 | struct ec_response_flash_protect r; |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 573 | const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT; |
| 574 | const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW; |
| 575 | int need_an_ec_cold_reset = 0; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 576 | int rc; |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 577 | |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 578 | /* Try to set RO_AT_BOOT and RO_NOW first */ |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 579 | memset(&p, 0, sizeof(p)); |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 580 | p.mask = (ro_at_boot_flag | ro_now_flag); |
| 581 | p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 582 | rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv), |
| 583 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 584 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 585 | msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n", |
| 586 | rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 587 | return 1; |
| 588 | } |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 589 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 590 | /* Read back */ |
| 591 | memset(&p, 0, sizeof(p)); |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 592 | rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv), |
| 593 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 594 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 595 | msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n", |
| 596 | rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 597 | return 1; |
| 598 | } |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 599 | |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 600 | if (!enable) { |
| 601 | /* The disable case is easier to check. */ |
| 602 | if (r.flags & ro_at_boot_flag) { |
| 603 | msg_perr("FAILED: RO_AT_BOOT is not clear.\n"); |
| 604 | return 1; |
| 605 | } else if (r.flags & ro_now_flag) { |
| 606 | msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n"); |
| 607 | need_an_ec_cold_reset = 1; |
| 608 | goto exit; |
| 609 | } |
| 610 | |
| 611 | msg_pdbg("INFO: RO_AT_BOOT is clear.\n"); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /* Check if RO_AT_BOOT is set. If not, fail in anyway. */ |
| 616 | if (r.flags & ro_at_boot_flag) { |
| 617 | msg_pdbg("INFO: RO_AT_BOOT has been set.\n"); |
| 618 | } else { |
| 619 | msg_perr("FAILED: RO_AT_BOOT is not set.\n"); |
| 620 | return 1; |
| 621 | } |
| 622 | |
| 623 | /* Then, we check if the protection has been activated. */ |
| 624 | if (r.flags & ro_now_flag) { |
| 625 | /* Good, RO_NOW is set. */ |
| 626 | msg_pdbg("INFO: RO_NOW is set. WP is active now.\n"); |
| 627 | } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) { |
| 628 | struct ec_params_reboot_ec reboot; |
| 629 | |
| 630 | msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n"); |
| 631 | |
| 632 | memset(&p, 0, sizeof(p)); |
| 633 | p.mask = EC_FLASH_PROTECT_ALL_NOW; |
| 634 | p.flags = EC_FLASH_PROTECT_ALL_NOW; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 635 | rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv), |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 636 | EC_VER_FLASH_PROTECT, |
| 637 | &p, sizeof(p), &r, sizeof(r)); |
| 638 | if (rc < 0) { |
| 639 | msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc); |
| 640 | return 1; |
| 641 | } |
| 642 | |
| 643 | /* Read back */ |
| 644 | memset(&p, 0, sizeof(p)); |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 645 | rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv), |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 646 | EC_VER_FLASH_PROTECT, |
| 647 | &p, sizeof(p), &r, sizeof(r)); |
| 648 | if (rc < 0) { |
| 649 | msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc); |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) { |
| 654 | msg_perr("FAILED: ALL_NOW is not set.\n"); |
| 655 | need_an_ec_cold_reset = 1; |
| 656 | goto exit; |
| 657 | } |
| 658 | |
| 659 | msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n"); |
| 660 | |
| 661 | /* |
| 662 | * Our goal is to protect the RO ASAP. The entire protection |
| 663 | * is just a workaround for platform not supporting RO_NOW. |
| 664 | * It has side-effect that the RW is also protected and leads |
| 665 | * the RW update failed. So, we arrange an EC code reset to |
| 666 | * unlock RW ASAP. |
| 667 | */ |
| 668 | memset(&reboot, 0, sizeof(reboot)); |
| 669 | reboot.cmd = EC_REBOOT_COLD; |
| 670 | reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 671 | rc = priv->ec_command(EC_CMD_REBOOT_EC | DEV(priv), |
| 672 | 0, &reboot, sizeof(reboot), NULL, 0); |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 673 | if (rc < 0) { |
| 674 | msg_perr("WARN: Cannot arrange a cold reset at next " |
| 675 | "shutdown to unlock entire protect.\n"); |
| 676 | msg_perr(" But you can do it manually.\n"); |
| 677 | } else { |
| 678 | msg_pdbg("INFO: A cold reset is arranged at next " |
| 679 | "shutdown.\n"); |
| 680 | } |
| 681 | |
| 682 | } else { |
| 683 | msg_perr("FAILED: RO_NOW is not set.\n"); |
| 684 | msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot " |
| 685 | "make write protection active now.\n"); |
| 686 | need_an_ec_cold_reset = 1; |
| 687 | } |
| 688 | |
| 689 | exit: |
| 690 | if (need_an_ec_cold_reset) { |
| 691 | msg_perr("FAILED: You may need a reboot to take effect of " |
| 692 | "PROTECT_RO_AT_BOOT.\n"); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 693 | return 1; |
| 694 | } |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 695 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 699 | static int cros_ec_set_range(const struct flashchip *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 700 | unsigned int start, unsigned int len) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 701 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 702 | struct ec_response_flash_region_info info; |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 703 | int rc; |
| 704 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 705 | /* Check if the given range is supported */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 706 | rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 707 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 708 | msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 709 | return 1; |
| 710 | } |
| 711 | if ((!start && !len) || /* list supported ranges */ |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 712 | ((start == info.offset) && (len == info.size))) { |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 713 | /* pass */ |
| 714 | } else { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 715 | msg_perr("FAILED: Unsupported write protection range " |
| 716 | "(0x%06x,0x%06x)\n\n", start, len); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 717 | msg_perr("Currently supported range:\n"); |
| 718 | msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0); |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 719 | msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset, |
| 720 | info.size); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 721 | return 1; |
| 722 | } |
| 723 | |
| 724 | return set_wp(!!len); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 728 | static int cros_ec_enable_writeprotect(const struct flashchip *flash, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 729 | enum wp_mode wp_mode) { |
| 730 | int ret; |
| 731 | |
| 732 | switch (wp_mode) { |
| 733 | case WP_MODE_HARDWARE: |
| 734 | ret = set_wp(1); |
| 735 | break; |
| 736 | default: |
| 737 | msg_perr("%s():%d Unsupported write-protection mode\n", |
| 738 | __func__, __LINE__); |
| 739 | ret = 1; |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | return ret; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 747 | static int cros_ec_disable_writeprotect(const struct flashchip *flash) { |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 748 | return set_wp(0); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 752 | static int cros_ec_wp_status(const struct flashchip *flash) { |
| 753 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 754 | struct ec_params_flash_protect p; |
| 755 | struct ec_response_flash_protect r; |
| 756 | int start, len; /* wp range */ |
| 757 | int enabled; |
| 758 | int rc; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 759 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 760 | memset(&p, 0, sizeof(p)); |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 761 | rc = priv->ec_command(EC_CMD_FLASH_PROTECT | DEV(priv), |
| 762 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 763 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 764 | msg_perr("FAILED: Cannot get the write protection status: %d\n", |
| 765 | rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 766 | return 1; |
| 767 | } else if (rc < sizeof(r)) { |
David Hendricks | f797dde | 2012-10-30 11:39:12 -0700 | [diff] [blame] | 768 | msg_perr("FAILED: Too little data returned (expected:%zd, " |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 769 | "actual:%d)\n", sizeof(r), rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 770 | return 1; |
| 771 | } |
| 772 | |
| 773 | start = len = 0; |
| 774 | if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) { |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 775 | struct ec_response_flash_region_info info; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 776 | |
| 777 | msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n", |
| 778 | __func__); |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 779 | rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_WP_RO, &info); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 780 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 781 | msg_perr("FAILED: Cannot get the WP_RO region info: " |
| 782 | "%d\n", rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 783 | return 1; |
| 784 | } |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 785 | start = info.offset; |
| 786 | len = info.size; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 787 | } else { |
| 788 | msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n", |
| 789 | __func__); |
| 790 | } |
| 791 | |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 792 | /* |
| 793 | * If neither RO_NOW or ALL_NOW is set, it means write protect is |
| 794 | * NOT active now. |
| 795 | */ |
| 796 | if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))) |
| 797 | start = len = 0; |
| 798 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 799 | /* Remove the SPI-style messages. */ |
| 800 | enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0; |
| 801 | msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00); |
| 802 | msg_pinfo("WP: status.srp0: %x\n", enabled); |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 803 | msg_pinfo("WP: write protect is %s.\n", |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 804 | enabled ? "enabled" : "disabled"); |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 805 | msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n", |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 806 | start, len); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 811 | /* perform basic "hello" test to see if we can talk to the EC */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 812 | int cros_ec_test(struct cros_ec_priv *priv) |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 813 | { |
| 814 | struct ec_params_hello request; |
| 815 | struct ec_response_hello response; |
| 816 | struct ec_response_proto_version proto; |
| 817 | int rc = 0; |
| 818 | |
| 819 | /* Say hello to EC. */ |
| 820 | request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */ |
| 821 | msg_pdbg("%s: sending HELLO request with 0x%08x\n", |
| 822 | __func__, request.in_data); |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 823 | rc = priv->ec_command(EC_CMD_HELLO | DEV(priv), 0, &request, |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 824 | sizeof(request), &response, sizeof(response)); |
| 825 | msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data); |
| 826 | |
| 827 | if (rc < 0 || response.out_data != 0xf1e2d3c4) { |
| 828 | msg_pdbg("response.out_data is not 0xf1e2d3c4.\n" |
| 829 | "rc=%d, request=0x%x response=0x%x\n", |
| 830 | rc, request.in_data, response.out_data); |
| 831 | return 1; |
| 832 | } |
| 833 | |
| 834 | return 0; |
| 835 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 836 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 837 | |
| 838 | /* |
| 839 | * Returns 0 to indicate success, non-zero othersize |
| 840 | * |
| 841 | * This function parses programmer parameters from the command line. Since |
| 842 | * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets |
| 843 | * run during internal programmer initialization. |
| 844 | */ |
| 845 | int cros_ec_parse_param(struct cros_ec_priv *priv) |
| 846 | { |
| 847 | char *p; |
| 848 | |
| 849 | p = extract_programmer_param("dev"); |
| 850 | if (p) { |
| 851 | unsigned int index; |
| 852 | char *endptr = NULL; |
| 853 | |
| 854 | errno = 0; |
| 855 | index = strtoul(p, &endptr, 10); |
| 856 | if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) { |
| 857 | msg_perr("Invalid argument: \"%s\"\n", p); |
| 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | if (index > 3) { |
| 862 | msg_perr("%s: Invalid device index\n", __func__); |
| 863 | return 1; |
| 864 | } |
| 865 | |
| 866 | priv->dev_index = index; |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 872 | int cros_ec_probe_size(struct flashchip *flash) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 873 | int rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 874 | struct ec_response_flash_info info; |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 875 | struct ec_response_get_chip_info chip_info; |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 876 | struct cros_ec_priv *priv = (struct cros_ec_priv *)opaque_programmer->data; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 877 | struct block_eraser *eraser; |
| 878 | static struct wp wp = { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 879 | .list_ranges = cros_ec_list_ranges, |
| 880 | .set_range = cros_ec_set_range, |
| 881 | .enable = cros_ec_enable_writeprotect, |
| 882 | .disable = cros_ec_disable_writeprotect, |
| 883 | .wp_status = cros_ec_wp_status, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 884 | }; |
| 885 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 886 | if (priv->dev_index > 0) { |
| 887 | if (cros_ec_test(priv)) { |
| 888 | msg_perr("%s: Failed to say \"hello\" to device %d\n", |
| 889 | __func__, priv->dev_index); |
| 890 | return 1; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | rc = priv->ec_command(EC_CMD_FLASH_INFO | DEV(priv), |
| 895 | 0, NULL, 0, &info, sizeof(info)); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 896 | if (rc < 0) { |
| 897 | msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc); |
| 898 | return 0; |
| 899 | } |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 900 | rc = cros_ec_get_current_image(priv); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 901 | if (rc < 0) { |
| 902 | msg_perr("%s(): Failed to probe (no current image): %d\n", |
| 903 | __func__, rc); |
| 904 | return 0; |
| 905 | } |
| 906 | priv->current_image = rc; |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 907 | priv->region = ®ions[0]; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 908 | |
| 909 | flash->total_size = info.flash_size / 1024; |
Simon Glass | 144fc2c | 2013-07-16 09:33:35 -0600 | [diff] [blame] | 910 | flash->page_size = opaque_programmer->max_data_read; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 911 | flash->tested = TEST_OK_PREW; |
| 912 | eraser = &flash->block_erasers[0]; |
| 913 | eraser->eraseblocks[0].size = info.erase_block_size; |
| 914 | eraser->eraseblocks[0].count = info.flash_size / |
| 915 | eraser->eraseblocks[0].size; |
| 916 | flash->wp = ℘ |
| 917 | |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 918 | /* |
| 919 | * Some STM32 variants erase bits to 0. For now, assume that this |
| 920 | * applies to STM32L parts. |
| 921 | * |
| 922 | * FIXME: This info will eventually be exposed via some EC command. |
| 923 | * See chrome-os-partner:20973. |
| 924 | */ |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame^] | 925 | rc = priv->ec_command(EC_CMD_GET_CHIP_INFO | DEV(priv), |
| 926 | 0, NULL, 0, &chip_info, sizeof(chip_info)); |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 927 | if (rc < 0) { |
| 928 | msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc); |
| 929 | return 0; |
| 930 | } |
| 931 | if (!strncmp(chip_info.name, "stm32l", 6)) |
| 932 | flash->feature_bits |= FEATURE_ERASE_TO_ZERO; |
| 933 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 934 | cros_ec_set_max_write_size(); |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 935 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 936 | /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*, |
| 937 | * so we need to be careful about using these enums as array indices */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 938 | rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RO, |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 939 | &priv->region[EC_IMAGE_RO]); |
| 940 | if (rc) { |
| 941 | msg_perr("%s(): Failed to probe (cannot find RO region): %d\n", |
| 942 | __func__, rc); |
| 943 | return 0; |
| 944 | } |
| 945 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 946 | rc = cros_ec_get_region_info(priv, EC_FLASH_REGION_RW, |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 947 | &priv->region[EC_IMAGE_RW]); |
| 948 | if (rc) { |
| 949 | msg_perr("%s(): Failed to probe (cannot find RW region): %d\n", |
| 950 | __func__, rc); |
| 951 | return 0; |
| 952 | } |
| 953 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 954 | return 1; |
| 955 | }; |