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> |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 53 | |
| 54 | struct cros_ec_priv *cros_ec_priv; |
David Hendricks | 393deec | 2016-11-23 16:15:05 -0800 | [diff] [blame] | 55 | static int ignore_wp_range_command = 0; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 56 | |
David Hendricks | b64b39a | 2016-10-11 13:48:06 -0700 | [diff] [blame] | 57 | static int set_wp(int enable); /* FIXME: move set_wp() */ |
| 58 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 59 | struct wp_data { |
| 60 | int enable; |
| 61 | unsigned int start; |
| 62 | unsigned int len; |
| 63 | }; |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 64 | #define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state" |
| 65 | |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 66 | /* If software sync is enabled, then we don't try the latest firmware copy |
| 67 | * after updating. |
| 68 | */ |
| 69 | #define SOFTWARE_SYNC_ENABLED |
| 70 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 71 | /* 1 if we want the flashrom to call erase_and_write_flash() again. */ |
| 72 | static int need_2nd_pass = 0; |
| 73 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 74 | /* 1 if we want the flashrom to try jumping to new firmware after update. */ |
| 75 | static int try_latest_firmware = 0; |
| 76 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 77 | /* 1 if EC firmware has RWSIG enabled. */ |
| 78 | static int rwsig_enabled = 0; |
| 79 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 80 | /* The range of each firmware copy from the image file to update. |
| 81 | * But re-define the .flags as the valid flag to indicate the firmware is |
| 82 | * new or not (if flags = 1). |
| 83 | */ |
| 84 | static struct fmap_area fwcopy[4]; // [0] is not used. |
| 85 | |
| 86 | /* The names of enum lpc_current_image to match in FMAP area names. */ |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 87 | static const char *sections[] = { |
David Hendricks | bf8c4dd | 2012-07-19 12:13:17 -0700 | [diff] [blame] | 88 | "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches |
| 89 | "EC_RO", |
| 90 | "EC_RW", |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 91 | }; |
| 92 | |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 93 | /* |
| 94 | * The names of the different device that can be found in a machine. |
| 95 | * Order is important: for backward compatibilty issue, |
| 96 | * 'ec' must be 0, 'pd' must be 1. |
| 97 | */ |
| 98 | static const char *ec_type[] = { |
| 99 | [0] = "ec", |
| 100 | [1] = "pd", |
| 101 | [2] = "sh", |
Vincent Palatin | 4faff9a | 2017-03-17 17:27:39 +0100 | [diff] [blame] | 102 | [3] = "fp", |
Wei-Ning Huang | 7839784 | 2017-05-05 21:45:47 +0800 | [diff] [blame] | 103 | [4] = "tp", |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 106 | /* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates |
| 107 | * the number of regions */ |
| 108 | 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] | 109 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 110 | /* |
| 111 | * Delay after reboot before EC can respond to host command. |
| 112 | * This value should be large enough for EC to initialize, but no larger than |
| 113 | * CONFIG_RWSIG_JUMP_TIMEOUT. This way for EC using RWSIG task, we will be |
| 114 | * able to abort RWSIG jump and stay in RO. |
| 115 | */ |
| 116 | #define EC_INIT_DELAY 800000 |
| 117 | |
| 118 | /* |
| 119 | * Delay after a cold reboot which allows RWSIG enabled EC to jump to EC_RW. |
| 120 | */ |
| 121 | #define EC_RWSIG_JUMP_TO_RW_DELAY 3000000 |
| 122 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 123 | /* Given the range not able to update, mark the corresponding |
| 124 | * firmware as old. |
| 125 | */ |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 126 | 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] | 127 | { |
| 128 | int i; |
| 129 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 130 | for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 131 | struct fmap_area *fw = &fwcopy[i]; |
| 132 | if ((addr >= fw->offset && (addr < fw->offset + fw->size)) || |
| 133 | (fw->offset >= addr && (fw->offset < addr + len))) { |
| 134 | msg_pdbg("Mark firmware [%s] as old.\n", |
| 135 | sections[i]); |
| 136 | fw->flags = 0; // mark as old |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 142 | static int cros_ec_get_current_image(void) |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 143 | { |
| 144 | struct ec_response_get_version resp; |
| 145 | int rc; |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 146 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 147 | rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 148 | 0, NULL, 0, &resp, sizeof(resp)); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 149 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 150 | 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] | 151 | return rc; |
| 152 | } |
| 153 | if (resp.current_image == EC_IMAGE_UNKNOWN) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 154 | msg_perr("CROS_EC gets unknown running copy\n"); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | return resp.current_image; |
| 159 | } |
| 160 | |
| 161 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 162 | static int cros_ec_get_region_info(enum ec_flash_region region, |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 163 | struct ec_response_flash_region_info *info) |
| 164 | { |
| 165 | struct ec_params_flash_region_info req; |
| 166 | struct ec_response_flash_region_info resp; |
| 167 | int rc; |
| 168 | |
| 169 | req.region = region; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 170 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO, |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 171 | EC_VER_FLASH_REGION_INFO, &req, sizeof(req), |
| 172 | &resp, sizeof(resp)); |
| 173 | if (rc < 0) { |
| 174 | msg_perr("Cannot get the WP_RO region info: %d\n", rc); |
| 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | info->offset = resp.offset; |
| 179 | info->size = resp.size; |
| 180 | return 0; |
| 181 | } |
| 182 | |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 183 | /** |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 184 | * Check if a feature is supported by EC. |
| 185 | * |
| 186 | * @param feature feature code |
| 187 | * @return < 0 if error, 0 not supported, > 0 supported |
| 188 | */ |
| 189 | static int ec_check_features(int feature) |
| 190 | { |
| 191 | struct ec_response_get_features r; |
| 192 | int rc; |
| 193 | |
| 194 | if (feature < 0 || feature >= sizeof(r.flags) * 8) |
| 195 | return -1; |
| 196 | |
| 197 | rc = cros_ec_priv->ec_command(EC_CMD_GET_FEATURES, |
| 198 | 0, NULL, 0, &r, sizeof(r)); |
| 199 | if (rc < 0) |
| 200 | return rc; |
| 201 | |
| 202 | return !!(r.flags[feature / 32] & EC_FEATURE_MASK_0(feature)); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Disable EC rwsig jump. |
| 207 | * |
| 208 | * @return 0 if success, <0 if error |
| 209 | */ |
| 210 | static int ec_rwsig_abort() |
| 211 | { |
| 212 | struct ec_params_rwsig_action p; |
| 213 | |
| 214 | p.action = RWSIG_ACTION_ABORT; |
| 215 | return cros_ec_priv->ec_command(EC_CMD_RWSIG_ACTION, |
| 216 | 0, &p, sizeof(p), NULL, 0); |
| 217 | } |
| 218 | |
| 219 | /** |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 220 | * Get the versions of the command supported by the EC. |
| 221 | * |
| 222 | * @param cmd Command |
| 223 | * @param pmask Destination for version mask; will be set to 0 on |
| 224 | * error. |
| 225 | * @return 0 if success, <0 if error |
| 226 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 227 | static int ec_get_cmd_versions(int cmd, uint32_t *pmask) |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 228 | { |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 229 | struct ec_params_get_cmd_versions pver; |
| 230 | struct ec_response_get_cmd_versions rver; |
| 231 | int rc; |
| 232 | |
| 233 | *pmask = 0; |
| 234 | |
| 235 | pver.cmd = cmd; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 236 | rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0, |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 237 | &pver, sizeof(pver), &rver, sizeof(rver)); |
| 238 | |
| 239 | if (rc < 0) |
| 240 | return rc; |
| 241 | |
| 242 | *pmask = rver.version_mask; |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Return non-zero if the EC supports the command and version |
| 248 | * |
| 249 | * @param cmd Command to check |
| 250 | * @param ver Version to check |
| 251 | * @return non-zero if command version supported; 0 if not. |
| 252 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 253 | static int ec_cmd_version_supported(int cmd, int ver) |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 254 | { |
| 255 | uint32_t mask = 0; |
| 256 | int rc; |
David Hendricks | d13d90d | 2016-08-09 17:00:52 -0700 | [diff] [blame] | 257 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 258 | rc = ec_get_cmd_versions(cmd, &mask); |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 259 | if (rc < 0) |
| 260 | return rc; |
| 261 | |
| 262 | return (mask & EC_VER_MASK(ver)) ? 1 : 0; |
| 263 | } |
| 264 | |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 265 | /* returns 0 if successful or <0 to indicate error */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 266 | static int set_ideal_write_size(void) |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 267 | { |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 268 | int cmd_version, ret; |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 269 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 270 | cmd_version = ec_cmd_version_supported(EC_CMD_FLASH_WRITE, |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 271 | EC_VER_FLASH_WRITE); |
| 272 | if (cmd_version < 0) { |
| 273 | msg_perr("Cannot determine write command version\n"); |
| 274 | return cmd_version; |
| 275 | } else if (cmd_version == 0) { |
| 276 | struct ec_response_flash_info info; |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 277 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 278 | ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 279 | cmd_version, NULL, 0, &info, sizeof(info)); |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 280 | if (ret < 0) { |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 281 | msg_perr("%s(): Cannot get flash info.\n", __func__); |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 282 | return ret; |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 285 | cros_ec_priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE; |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 286 | } else { |
| 287 | struct ec_response_flash_info_1 info; |
| 288 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 289 | ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 290 | cmd_version, NULL, 0, &info, sizeof(info)); |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 291 | if (ret < 0) { |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 292 | msg_perr("%s(): Cannot get flash info.\n", __func__); |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 293 | return ret; |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 296 | cros_ec_priv->ideal_write_size = info.write_ideal_size; |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 297 | } |
| 298 | |
David Hendricks | fb405f1 | 2014-08-19 22:42:30 -0700 | [diff] [blame] | 299 | return 0; |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 300 | } |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 301 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 302 | /* Perform a cold reboot. |
| 303 | * |
| 304 | * @param flags flags to pass to EC_CMD_REBOOT_EC. |
| 305 | * @return 0 for success, < 0 for command failure. |
| 306 | */ |
| 307 | static int cros_ec_cold_reboot(int flags) { |
| 308 | struct ec_params_reboot_ec p; |
| 309 | |
| 310 | memset(&p, 0, sizeof(p)); |
| 311 | p.cmd = EC_REBOOT_COLD; |
| 312 | p.flags = flags; |
| 313 | return cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, 0, &p, sizeof(p), |
| 314 | NULL, 0); |
| 315 | } |
| 316 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 317 | /* 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] | 318 | * then this functions picks a NEW firmware copy and jumps to it. Note that |
| 319 | * RO is preferred, then A, finally B. |
| 320 | * |
| 321 | * Returns 0 for success. |
| 322 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 323 | 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] | 324 | struct ec_params_reboot_ec p; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 325 | int rc; |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 326 | int current_image; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 327 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 328 | /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't |
| 329 | * jump to different firmware copy. The second EC_RES_SUCCESS would |
| 330 | * set the OBF=1 and the next command cannot be executed. |
| 331 | * Thus, we call EC to jump only if the target is different. |
| 332 | */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 333 | current_image = cros_ec_get_current_image(); |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 334 | if (current_image < 0) |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 335 | return 1; |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 336 | if (current_image == target) |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 337 | return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 338 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 339 | memset(&p, 0, sizeof(p)); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 340 | |
| 341 | /* Translate target --> EC reboot command parameter */ |
| 342 | switch (target) { |
| 343 | case EC_IMAGE_RO: |
| 344 | p.cmd = EC_REBOOT_JUMP_RO; |
| 345 | break; |
| 346 | case EC_IMAGE_RW: |
| 347 | p.cmd = EC_REBOOT_JUMP_RW; |
| 348 | break; |
| 349 | default: |
| 350 | /* |
| 351 | * If target is unspecified, set EC reboot command to use |
| 352 | * a new image. Also set "target" so that it may be used |
| 353 | * to update the priv->current_image if jump is successful. |
| 354 | */ |
| 355 | if (fwcopy[EC_IMAGE_RO].flags) { |
| 356 | p.cmd = EC_REBOOT_JUMP_RO; |
| 357 | target = EC_IMAGE_RO; |
| 358 | } else if (fwcopy[EC_IMAGE_RW].flags) { |
| 359 | p.cmd = EC_REBOOT_JUMP_RW; |
| 360 | target = EC_IMAGE_RW; |
| 361 | } else { |
| 362 | p.cmd = EC_IMAGE_UNKNOWN; |
| 363 | } |
| 364 | break; |
| 365 | } |
| 366 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 367 | /* |
| 368 | * Do a cold reset instead of JUMP_RO so board enabling |
| 369 | * EC_FLASH_PROTECT_ALL_NOW at runtime can clear the WP flag. |
| 370 | * This is true for EC enabling RWSIG, where |
| 371 | * EC_FLASH_PROTECT_ALL_NOW is applied before jumping into RW. |
| 372 | */ |
| 373 | if (target == EC_IMAGE_RO && rwsig_enabled) { |
| 374 | p.cmd = EC_REBOOT_COLD; |
| 375 | msg_pdbg("RWSIG enabled: doing a cold reboot instead of " |
| 376 | "JUMP_RO.\n"); |
| 377 | } |
| 378 | |
| 379 | msg_pdbg("CROS_EC is jumping to [%s]\n", sections[target]); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 380 | if (p.cmd == EC_IMAGE_UNKNOWN) return 1; |
| 381 | |
Vadim Bendebury | 9fa26e8 | 2013-09-19 13:56:32 -0700 | [diff] [blame] | 382 | if (current_image == p.cmd) { |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 383 | msg_pdbg("CROS_EC is already in [%s]\n", sections[target]); |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 384 | cros_ec_priv->current_image = target; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 388 | rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 389 | 0, &p, sizeof(p), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 390 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 391 | msg_perr("CROS_EC cannot jump to [%s]:%d\n", |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 392 | sections[target], rc); |
| 393 | return rc; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 396 | /* Sleep until EC can respond to host command, but just before |
| 397 | * CONFIG_RWSIG_JUMP_TIMEOUT if EC is using RWSIG task. */ |
| 398 | usleep(EC_INIT_DELAY); |
| 399 | |
| 400 | /* Abort RWSIG jump for EC that use it. Normal EC will ignore it. */ |
| 401 | if (target == EC_IMAGE_RO && rwsig_enabled) { |
| 402 | msg_pdbg("RWSIG enabled: aborting RWSIG jump.\n"); |
| 403 | ec_rwsig_abort(); |
| 404 | } |
| 405 | |
| 406 | msg_pdbg("CROS_EC has jumped to [%s]\n", sections[target]); |
| 407 | rc = EC_RES_SUCCESS; |
| 408 | cros_ec_priv->current_image = target; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 409 | |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 410 | /* update max data write size in case we're jumping to an EC |
| 411 | * firmware with different protocol */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 412 | set_ideal_write_size(); |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 413 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 414 | return rc; |
| 415 | } |
| 416 | |
David Hendricks | b64b39a | 2016-10-11 13:48:06 -0700 | [diff] [blame] | 417 | static int cros_ec_restore_wp(void *data) |
| 418 | { |
| 419 | msg_pdbg("Restoring EC soft WP.\n"); |
| 420 | return set_wp(1); |
| 421 | } |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 422 | |
David Hendricks | b64b39a | 2016-10-11 13:48:06 -0700 | [diff] [blame] | 423 | static int cros_ec_wp_is_enabled(void) |
| 424 | { |
| 425 | struct ec_params_flash_protect p; |
| 426 | struct ec_response_flash_protect r; |
| 427 | int rc; |
| 428 | |
| 429 | memset(&p, 0, sizeof(p)); |
| 430 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT, |
| 431 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
| 432 | if (rc < 0) { |
| 433 | msg_perr("FAILED: Cannot get the write protection status: %d\n", |
| 434 | rc); |
| 435 | return -1; |
| 436 | } else if (rc < sizeof(r)) { |
| 437 | msg_perr("FAILED: Too little data returned (expected:%zd, " |
| 438 | "actual:%d)\n", sizeof(r), rc); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | if (r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)) |
| 443 | return 1; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Prepare EC for update: |
| 450 | * - Disable soft WP if needed. |
| 451 | * - Parse flashmap. |
| 452 | * - Jump to RO firmware. |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 453 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 454 | int cros_ec_prepare(uint8_t *image, int size) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 455 | struct fmap *fmap; |
David Hendricks | b64b39a | 2016-10-11 13:48:06 -0700 | [diff] [blame] | 456 | int i, j, wp_status; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 457 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 458 | if (!(cros_ec_priv && cros_ec_priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 459 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 460 | if (ec_check_features(EC_FEATURE_RWSIG) > 0) { |
| 461 | rwsig_enabled = 1; |
| 462 | msg_pdbg("EC has RWSIG enabled.\n"); |
| 463 | } |
| 464 | |
David Hendricks | b64b39a | 2016-10-11 13:48:06 -0700 | [diff] [blame] | 465 | /* |
| 466 | * If HW WP is disabled we may still need to disable write protection |
| 467 | * that is active on the EC. Otherwise the EC can reject erase/write |
| 468 | * commands. |
| 469 | * |
| 470 | * Failure is OK since HW WP might be enabled or the EC needs to be |
| 471 | * rebooted for the change to take effect. We can still update RW |
| 472 | * portions. |
| 473 | * |
| 474 | * If disabled here, EC WP will be restored at the end so that |
| 475 | * "--wp-enable" does not need to be run later. This greatly |
| 476 | * simplifies logic for developers and scripts. |
| 477 | */ |
| 478 | wp_status = cros_ec_wp_is_enabled(); |
| 479 | if (wp_status < 0) { |
| 480 | return 1; |
| 481 | } else if (wp_status == 1) { |
| 482 | msg_pdbg("Attempting to disable EC soft WP.\n"); |
| 483 | if (!set_wp(0)) { |
| 484 | msg_pdbg("EC soft WP disabled successfully.\n"); |
| 485 | if (register_shutdown(cros_ec_restore_wp, NULL)) |
| 486 | return 1; |
| 487 | } else { |
| 488 | msg_pdbg("Failed. Hardware WP might in effect or EC " |
| 489 | "needs to be rebooted first.\n"); |
| 490 | } |
| 491 | } else { |
| 492 | msg_pdbg("EC soft WP is already disabled.\n"); |
| 493 | } |
| 494 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 495 | // Parse the fmap in the image file and cache the firmware ranges. |
| 496 | fmap = fmap_find_in_memory(image, size); |
| 497 | if (!fmap) return 0; |
| 498 | |
| 499 | // Lookup RO/A/B sections in FMAP. |
| 500 | for (i = 0; i < fmap->nareas; i++) { |
| 501 | struct fmap_area *fa = &fmap->areas[i]; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 502 | for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) { |
David Hendricks | 5b06c88 | 2012-05-20 18:27:25 -0700 | [diff] [blame] | 503 | if (!strcmp(sections[j], (const char *)fa->name)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 504 | msg_pdbg("Found '%s' in image.\n", fa->name); |
| 505 | memcpy(&fwcopy[j], fa, sizeof(*fa)); |
| 506 | fwcopy[j].flags = 1; // mark as new |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 511 | /* 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] | 512 | * change this behavior, please also check the cros_ec_finish(). |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 513 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 514 | return cros_ec_jump_copy(EC_IMAGE_RO); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
| 518 | /* Returns >0 if we need 2nd pass of erase_and_write_flash(). |
| 519 | * <0 if we cannot jump to any firmware copy. |
| 520 | * ==0 if no more pass is needed. |
| 521 | * |
| 522 | * This function also jumps to new-updated firmware copy before return >0. |
| 523 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 524 | int cros_ec_need_2nd_pass(void) { |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 525 | if (!(cros_ec_priv && cros_ec_priv->detected)) return 0; |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 526 | |
| 527 | if (need_2nd_pass) { |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 528 | if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 529 | return -1; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return need_2nd_pass; |
| 534 | } |
| 535 | |
| 536 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 537 | /* Returns 0 for success. |
| 538 | * |
| 539 | * Try latest firmware: B > A > RO |
| 540 | * |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 541 | * 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] | 542 | * 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] | 543 | * 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] | 544 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 545 | int cros_ec_finish(void) { |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 546 | if (!(cros_ec_priv && cros_ec_priv->detected)) return 0; |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 547 | |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 548 | /* For EC with RWSIG enabled. We need a cold reboot to enable |
| 549 | * EC_FLASH_PROTECT_ALL_NOW and make sure RWSIG check is performed. |
| 550 | */ |
| 551 | if (rwsig_enabled) { |
| 552 | int rc; |
| 553 | |
| 554 | msg_pdbg("RWSIG enabled: doing a cold reboot to enable WP.\n"); |
| 555 | rc = cros_ec_cold_reboot(0); |
| 556 | usleep(EC_RWSIG_JUMP_TO_RW_DELAY); |
| 557 | return rc; |
| 558 | } |
| 559 | |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 560 | if (try_latest_firmware) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 561 | if (fwcopy[EC_IMAGE_RW].flags && |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 562 | cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0; |
| 563 | return cros_ec_jump_copy(EC_IMAGE_RO); |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 570 | int cros_ec_read(struct flashctx *flash, uint8_t *readarr, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 571 | unsigned int blockaddr, unsigned int readcnt) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 572 | int rc = 0; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 573 | struct ec_params_flash_read p; |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 574 | int maxlen = opaque_programmer->max_data_read; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 575 | uint8_t buf[maxlen]; |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 576 | int offset = 0, count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 577 | |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 578 | while (offset < readcnt) { |
| 579 | count = min(maxlen, readcnt - offset); |
| 580 | p.offset = blockaddr + offset; |
| 581 | p.size = count; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 582 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 583 | 0, &p, sizeof(p), buf, count); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 584 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 585 | msg_perr("CROS_EC: Flash read error at offset 0x%x\n", |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 586 | blockaddr + offset); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 587 | return rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 588 | } else { |
| 589 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 592 | memcpy(readarr + offset, buf, count); |
David Hendricks | 133083b | 2012-07-17 20:39:38 -0700 | [diff] [blame] | 593 | offset += count; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return rc; |
| 597 | } |
| 598 | |
| 599 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 600 | /* |
| 601 | * returns 0 to indicate area does not overlap current EC image |
| 602 | * returns 1 to indicate area overlaps current EC image or error |
| 603 | */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 604 | static int in_current_image(unsigned int addr, unsigned int len) |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 605 | { |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 606 | enum ec_current_image image; |
| 607 | uint32_t region_offset; |
| 608 | uint32_t region_size; |
| 609 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 610 | image = cros_ec_priv->current_image; |
| 611 | region_offset = cros_ec_priv->region[image].offset; |
| 612 | region_size = cros_ec_priv->region[image].size; |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 613 | |
| 614 | if ((addr + len - 1 < region_offset) || |
| 615 | (addr > region_offset + region_size - 1)) { |
| 616 | return 0; |
| 617 | } |
| 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 622 | int cros_ec_block_erase(struct flashctx *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 623 | unsigned int blockaddr, |
| 624 | unsigned int len) { |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 625 | struct ec_params_flash_erase erase; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 626 | int rc; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 627 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 628 | if (in_current_image(blockaddr, len)) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 629 | cros_ec_invalidate_copy(blockaddr, len); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 630 | need_2nd_pass = 1; |
| 631 | return ACCESS_DENIED; |
| 632 | } |
| 633 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 634 | erase.offset = blockaddr; |
| 635 | erase.size = len; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 636 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 637 | 0, &erase, sizeof(erase), NULL, 0); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 638 | if (rc == -EC_RES_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 639 | // this is active image. |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 640 | cros_ec_invalidate_copy(blockaddr, len); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 641 | need_2nd_pass = 1; |
| 642 | return ACCESS_DENIED; |
| 643 | } |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 644 | if (rc < 0) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 645 | 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] | 646 | blockaddr, rc); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 647 | return rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 648 | } else { |
| 649 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 650 | } |
| 651 | |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 652 | #ifndef SOFTWARE_SYNC_ENABLED |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 653 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 654 | #endif |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 655 | return rc; |
| 656 | } |
| 657 | |
| 658 | |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 659 | int cros_ec_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 660 | unsigned int nbytes) { |
| 661 | int i, rc = 0; |
Ken Chang | 69c31b8 | 2014-10-28 15:17:21 +0800 | [diff] [blame] | 662 | unsigned int written = 0, real_write_size; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 663 | struct ec_params_flash_write p; |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 664 | uint8_t *packet; |
| 665 | |
Ken Chang | 69c31b8 | 2014-10-28 15:17:21 +0800 | [diff] [blame] | 666 | /* |
| 667 | * For chrome-os-partner:33035, to workaround the undersized |
| 668 | * outdata buffer issue in kernel. |
| 669 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 670 | real_write_size = min(opaque_programmer->max_data_write, |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 671 | cros_ec_priv->ideal_write_size); |
Ken Chang | 69c31b8 | 2014-10-28 15:17:21 +0800 | [diff] [blame] | 672 | packet = malloc(sizeof(p) + real_write_size); |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 673 | if (!packet) |
| 674 | return -1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 675 | |
| 676 | for (i = 0; i < nbytes; i += written) { |
Ken Chang | 69c31b8 | 2014-10-28 15:17:21 +0800 | [diff] [blame] | 677 | written = min(nbytes - i, real_write_size); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 678 | p.offset = addr + i; |
| 679 | p.size = written; |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 680 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 681 | if (in_current_image(p.offset, p.size)) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 682 | cros_ec_invalidate_copy(addr, nbytes); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 683 | need_2nd_pass = 1; |
| 684 | return ACCESS_DENIED; |
| 685 | } |
| 686 | |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 687 | memcpy(packet, &p, sizeof(p)); |
| 688 | memcpy(packet + sizeof(p), &buf[i], written); |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 689 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 690 | 0, packet, sizeof(p) + p.size, NULL, 0); |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 691 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 692 | if (rc == -EC_RES_ACCESS_DENIED) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 693 | // this is active image. |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 694 | cros_ec_invalidate_copy(addr, nbytes); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 695 | need_2nd_pass = 1; |
| 696 | return ACCESS_DENIED; |
| 697 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 698 | |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 699 | if (rc < 0) break; |
| 700 | rc = EC_RES_SUCCESS; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 701 | } |
| 702 | |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 703 | #ifndef SOFTWARE_SYNC_ENABLED |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 704 | try_latest_firmware = 1; |
Louis Yung-Chieh Lo | ef88ec3 | 2012-09-20 10:39:35 +0800 | [diff] [blame] | 705 | #endif |
David Hendricks | 2d6db77 | 2013-07-10 21:07:48 -0700 | [diff] [blame] | 706 | free(packet); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 707 | return rc; |
| 708 | } |
| 709 | |
| 710 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 711 | static int cros_ec_list_ranges(const struct flashctx *flash) { |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 712 | struct ec_response_flash_region_info info; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 713 | int rc; |
| 714 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 715 | rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 716 | if (rc < 0) { |
| 717 | msg_perr("Cannot get the WP_RO region info: %d\n", rc); |
| 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | msg_pinfo("Supported write protect range:\n"); |
| 722 | msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0); |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 723 | msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset, |
| 724 | info.size); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 725 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 730 | /* |
| 731 | * Helper function for flash protection. |
| 732 | * |
| 733 | * On EC API v1, the EC write protection has been simplified to one-bit: |
| 734 | * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled |
| 735 | * or disabled. However, this is different from the SPI-style write protect |
| 736 | * behavior. Thus, we re-define the flashrom command (SPI-style) so that |
| 737 | * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set. |
| 738 | * |
| 739 | * SRP Range | PROTECT_RO_AT_BOOT |
| 740 | * 0 0 | 0 |
| 741 | * 0 non-zero | 1 |
| 742 | * 1 0 | 1 |
| 743 | * 1 non-zero | 1 |
| 744 | * |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 745 | * |
| 746 | * Besides, to make the protection take effect as soon as possible, we |
| 747 | * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not |
| 748 | * 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] | 749 | */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 750 | static int set_wp(int enable) { |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 751 | struct ec_params_flash_protect p; |
| 752 | struct ec_response_flash_protect r; |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 753 | const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT; |
| 754 | const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW; |
| 755 | int need_an_ec_cold_reset = 0; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 756 | int rc; |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 757 | |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 758 | /* Try to set RO_AT_BOOT and RO_NOW first */ |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 759 | memset(&p, 0, sizeof(p)); |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 760 | p.mask = (ro_at_boot_flag | ro_now_flag); |
| 761 | p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 762 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 763 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 764 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 765 | msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n", |
| 766 | rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 767 | return 1; |
| 768 | } |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 769 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 770 | /* Read back */ |
| 771 | memset(&p, 0, sizeof(p)); |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 772 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 773 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 774 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 775 | msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n", |
| 776 | rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 777 | return 1; |
| 778 | } |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 779 | |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 780 | if (!enable) { |
| 781 | /* The disable case is easier to check. */ |
| 782 | if (r.flags & ro_at_boot_flag) { |
| 783 | msg_perr("FAILED: RO_AT_BOOT is not clear.\n"); |
| 784 | return 1; |
| 785 | } else if (r.flags & ro_now_flag) { |
| 786 | msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n"); |
| 787 | need_an_ec_cold_reset = 1; |
| 788 | goto exit; |
| 789 | } |
| 790 | |
| 791 | msg_pdbg("INFO: RO_AT_BOOT is clear.\n"); |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /* Check if RO_AT_BOOT is set. If not, fail in anyway. */ |
| 796 | if (r.flags & ro_at_boot_flag) { |
| 797 | msg_pdbg("INFO: RO_AT_BOOT has been set.\n"); |
| 798 | } else { |
| 799 | msg_perr("FAILED: RO_AT_BOOT is not set.\n"); |
| 800 | return 1; |
| 801 | } |
| 802 | |
| 803 | /* Then, we check if the protection has been activated. */ |
| 804 | if (r.flags & ro_now_flag) { |
| 805 | /* Good, RO_NOW is set. */ |
| 806 | msg_pdbg("INFO: RO_NOW is set. WP is active now.\n"); |
| 807 | } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 808 | msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n"); |
| 809 | |
| 810 | memset(&p, 0, sizeof(p)); |
| 811 | p.mask = EC_FLASH_PROTECT_ALL_NOW; |
| 812 | p.flags = EC_FLASH_PROTECT_ALL_NOW; |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 813 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT, |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 814 | EC_VER_FLASH_PROTECT, |
| 815 | &p, sizeof(p), &r, sizeof(r)); |
| 816 | if (rc < 0) { |
| 817 | msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc); |
| 818 | return 1; |
| 819 | } |
| 820 | |
| 821 | /* Read back */ |
| 822 | memset(&p, 0, sizeof(p)); |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 823 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT, |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 824 | EC_VER_FLASH_PROTECT, |
| 825 | &p, sizeof(p), &r, sizeof(r)); |
| 826 | if (rc < 0) { |
| 827 | msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc); |
| 828 | return 1; |
| 829 | } |
| 830 | |
| 831 | if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) { |
| 832 | msg_perr("FAILED: ALL_NOW is not set.\n"); |
| 833 | need_an_ec_cold_reset = 1; |
| 834 | goto exit; |
| 835 | } |
| 836 | |
| 837 | msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n"); |
| 838 | |
| 839 | /* |
| 840 | * Our goal is to protect the RO ASAP. The entire protection |
| 841 | * is just a workaround for platform not supporting RO_NOW. |
| 842 | * It has side-effect that the RW is also protected and leads |
| 843 | * the RW update failed. So, we arrange an EC code reset to |
| 844 | * unlock RW ASAP. |
| 845 | */ |
Wei-Ning Huang | 70ebbd4 | 2017-05-05 21:50:41 +0800 | [diff] [blame] | 846 | rc = cros_ec_cold_reboot(EC_REBOOT_FLAG_ON_AP_SHUTDOWN); |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 847 | if (rc < 0) { |
| 848 | msg_perr("WARN: Cannot arrange a cold reset at next " |
| 849 | "shutdown to unlock entire protect.\n"); |
| 850 | msg_perr(" But you can do it manually.\n"); |
| 851 | } else { |
| 852 | msg_pdbg("INFO: A cold reset is arranged at next " |
| 853 | "shutdown.\n"); |
| 854 | } |
| 855 | |
| 856 | } else { |
| 857 | msg_perr("FAILED: RO_NOW is not set.\n"); |
| 858 | msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot " |
| 859 | "make write protection active now.\n"); |
| 860 | need_an_ec_cold_reset = 1; |
| 861 | } |
| 862 | |
| 863 | exit: |
| 864 | if (need_an_ec_cold_reset) { |
| 865 | msg_perr("FAILED: You may need a reboot to take effect of " |
| 866 | "PROTECT_RO_AT_BOOT.\n"); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 867 | return 1; |
| 868 | } |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 869 | |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 873 | static int cros_ec_set_range(const struct flashctx *flash, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 874 | unsigned int start, unsigned int len) { |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 875 | struct ec_response_flash_region_info info; |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 876 | int rc; |
| 877 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 878 | /* Check if the given range is supported */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 879 | rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 880 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 881 | 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] | 882 | return 1; |
| 883 | } |
| 884 | if ((!start && !len) || /* list supported ranges */ |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 885 | ((start == info.offset) && (len == info.size))) { |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 886 | /* pass */ |
| 887 | } else { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 888 | msg_perr("FAILED: Unsupported write protection range " |
| 889 | "(0x%06x,0x%06x)\n\n", start, len); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 890 | msg_perr("Currently supported range:\n"); |
| 891 | msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0); |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 892 | msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset, |
| 893 | info.size); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 894 | return 1; |
| 895 | } |
| 896 | |
David Hendricks | 393deec | 2016-11-23 16:15:05 -0800 | [diff] [blame] | 897 | if (ignore_wp_range_command) |
| 898 | return 0; |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 899 | return set_wp(!!len); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 903 | static int cros_ec_enable_writeprotect(const struct flashctx *flash, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 904 | enum wp_mode wp_mode) { |
| 905 | int ret; |
| 906 | |
| 907 | switch (wp_mode) { |
| 908 | case WP_MODE_HARDWARE: |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 909 | ret = set_wp(1); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 910 | break; |
| 911 | default: |
| 912 | msg_perr("%s():%d Unsupported write-protection mode\n", |
| 913 | __func__, __LINE__); |
| 914 | ret = 1; |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | return ret; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 922 | static int cros_ec_disable_writeprotect(const struct flashctx *flash) { |
David Hendricks | 393deec | 2016-11-23 16:15:05 -0800 | [diff] [blame] | 923 | /* --wp-range implicitly enables write protection on CrOS EC, so force |
| 924 | it not to if --wp-disable is what the user really wants. */ |
| 925 | ignore_wp_range_command = 1; |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 926 | return set_wp(0); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 930 | static int cros_ec_wp_status(const struct flashctx *flash) {; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 931 | struct ec_params_flash_protect p; |
| 932 | struct ec_response_flash_protect r; |
| 933 | int start, len; /* wp range */ |
| 934 | int enabled; |
| 935 | int rc; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 936 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 937 | memset(&p, 0, sizeof(p)); |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 938 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 939 | EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r)); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 940 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 941 | msg_perr("FAILED: Cannot get the write protection status: %d\n", |
| 942 | rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 943 | return 1; |
| 944 | } else if (rc < sizeof(r)) { |
David Hendricks | f797dde | 2012-10-30 11:39:12 -0700 | [diff] [blame] | 945 | msg_perr("FAILED: Too little data returned (expected:%zd, " |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 946 | "actual:%d)\n", sizeof(r), rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 947 | return 1; |
| 948 | } |
| 949 | |
| 950 | start = len = 0; |
| 951 | if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) { |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 952 | struct ec_response_flash_region_info info; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 953 | |
| 954 | msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n", |
| 955 | __func__); |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 956 | rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 957 | if (rc < 0) { |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 958 | msg_perr("FAILED: Cannot get the WP_RO region info: " |
| 959 | "%d\n", rc); |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 960 | return 1; |
| 961 | } |
Simon Glass | 3c01dca | 2013-07-01 18:07:34 +0900 | [diff] [blame] | 962 | start = info.offset; |
| 963 | len = info.size; |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 964 | } else { |
| 965 | msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n", |
| 966 | __func__); |
| 967 | } |
| 968 | |
Louis Yung-Chieh Lo | ca052c4 | 2012-08-24 14:12:21 +0800 | [diff] [blame] | 969 | /* |
| 970 | * If neither RO_NOW or ALL_NOW is set, it means write protect is |
| 971 | * NOT active now. |
| 972 | */ |
| 973 | if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW))) |
| 974 | start = len = 0; |
| 975 | |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 976 | /* Remove the SPI-style messages. */ |
| 977 | enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0; |
| 978 | msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00); |
| 979 | msg_pinfo("WP: status.srp0: %x\n", enabled); |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 980 | msg_pinfo("WP: write protect is %s.\n", |
Louis Yung-Chieh Lo | 3e6da21 | 2012-08-13 17:21:01 +0800 | [diff] [blame] | 981 | enabled ? "enabled" : "disabled"); |
Louis Yung-Chieh Lo | 05b7a7b | 2012-08-06 19:10:39 +0800 | [diff] [blame] | 982 | 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] | 983 | start, len); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 988 | /* 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] | 989 | int cros_ec_test(struct cros_ec_priv *priv) |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 990 | { |
| 991 | struct ec_params_hello request; |
| 992 | struct ec_response_hello response; |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 993 | int rc = 0; |
| 994 | |
| 995 | /* Say hello to EC. */ |
| 996 | request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */ |
| 997 | msg_pdbg("%s: sending HELLO request with 0x%08x\n", |
| 998 | __func__, request.in_data); |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 999 | rc = priv->ec_command(EC_CMD_HELLO, 0, &request, |
David Hendricks | e545493 | 2013-11-04 18:16:11 -0800 | [diff] [blame] | 1000 | sizeof(request), &response, sizeof(response)); |
| 1001 | msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data); |
| 1002 | |
| 1003 | if (rc < 0 || response.out_data != 0xf1e2d3c4) { |
| 1004 | msg_pdbg("response.out_data is not 0xf1e2d3c4.\n" |
| 1005 | "rc=%d, request=0x%x response=0x%x\n", |
| 1006 | rc, request.in_data, response.out_data); |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
| 1010 | return 0; |
| 1011 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1012 | |
David Hendricks | d13d90d | 2016-08-09 17:00:52 -0700 | [diff] [blame] | 1013 | void cros_ec_set_max_size(struct cros_ec_priv *priv, |
| 1014 | struct opaque_programmer *op) { |
Puthikorn Voravootivat | c0993cf | 2014-08-28 16:04:58 -0700 | [diff] [blame] | 1015 | struct ec_response_get_protocol_info info; |
| 1016 | int rc = 0; |
| 1017 | msg_pdbg("%s: sending protoinfo command\n", __func__); |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1018 | rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0, |
Puthikorn Voravootivat | c0993cf | 2014-08-28 16:04:58 -0700 | [diff] [blame] | 1019 | &info, sizeof(info)); |
| 1020 | msg_pdbg("%s: rc:%d\n", __func__, rc); |
| 1021 | |
| 1022 | if (rc == sizeof(info)) { |
| 1023 | op->max_data_write = min(op->max_data_write, |
| 1024 | info.max_request_packet_size - |
| 1025 | sizeof(struct ec_host_request)); |
| 1026 | op->max_data_read = min(op->max_data_read, |
| 1027 | info.max_response_packet_size - |
| 1028 | sizeof(struct ec_host_response)); |
| 1029 | msg_pdbg("%s: max_write:%d max_read:%d\n", __func__, |
| 1030 | op->max_data_write, op->max_data_read); |
| 1031 | } |
| 1032 | } |
| 1033 | |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1034 | |
| 1035 | /* |
David Hendricks | 052446b | 2014-09-11 11:26:51 -0700 | [diff] [blame] | 1036 | * Returns 0 to indicate success, non-zero otherwise |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1037 | * |
| 1038 | * This function parses programmer parameters from the command line. Since |
| 1039 | * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets |
| 1040 | * run during internal programmer initialization. |
| 1041 | */ |
| 1042 | int cros_ec_parse_param(struct cros_ec_priv *priv) |
| 1043 | { |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1044 | char *p; |
Souvik Ghosh | f1608b4 | 2016-06-30 16:03:55 -0700 | [diff] [blame] | 1045 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1046 | p = extract_programmer_param("dev"); |
| 1047 | if (p) { |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1048 | unsigned int index; |
| 1049 | char *endptr = NULL; |
| 1050 | |
| 1051 | errno = 0; |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1052 | /* |
| 1053 | * For backward compatibility, check if the index is |
| 1054 | * a number: 0: main EC, 1: PD |
| 1055 | * works only on Samus. |
| 1056 | */ |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1057 | index = strtoul(p, &endptr, 10); |
| 1058 | if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) { |
| 1059 | msg_perr("Invalid argument: \"%s\"\n", p); |
| 1060 | return 1; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1063 | if (index > 1) { |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1064 | msg_perr("%s: Invalid device index\n", __func__); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1065 | return 1; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1066 | } |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1067 | priv->dev = ec_type[index]; |
| 1068 | msg_pdbg("Target %s used\n", priv->dev); |
| 1069 | } |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1070 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1071 | p = extract_programmer_param("type"); |
| 1072 | if (p) { |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1073 | unsigned int index; |
| 1074 | for (index = 0; index < ARRAY_SIZE(ec_type); index++) |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1075 | if (!strcmp(p, ec_type[index])) |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1076 | break; |
| 1077 | if (index == ARRAY_SIZE(ec_type)) { |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1078 | msg_perr("Invalid argument: \"%s\"\n", p); |
| 1079 | return 1; |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1080 | } |
| 1081 | priv->dev = ec_type[index]; |
| 1082 | msg_pdbg("Target %s used\n", priv->dev); |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1085 | p = extract_programmer_param("block"); |
| 1086 | if (p) { |
| 1087 | unsigned int block; |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1088 | char *endptr = NULL; |
| 1089 | |
| 1090 | errno = 0; |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1091 | block = strtoul(p, &endptr, 0); |
| 1092 | if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) { |
| 1093 | msg_perr("Invalid argument: \"%s\"\n", p); |
| 1094 | return 1; |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1097 | if (block <= 0) { |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1098 | msg_perr("%s: Invalid block size\n", __func__); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1099 | return 1; |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1102 | msg_pdbg("Override block size to 0x%x\n", block); |
| 1103 | priv->erase_block_size = block; |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1106 | return 0; |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1109 | int cros_ec_probe_size(struct flashctx *flash) { |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1110 | int rc; |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 1111 | struct ec_response_flash_info info; |
David Hendricks | a672b04 | 2016-09-19 12:37:36 -0700 | [diff] [blame] | 1112 | struct ec_response_flash_spi_info spi_info; |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 1113 | struct ec_response_get_chip_info chip_info; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1114 | struct block_eraser *eraser; |
| 1115 | static struct wp wp = { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 1116 | .list_ranges = cros_ec_list_ranges, |
| 1117 | .set_range = cros_ec_set_range, |
| 1118 | .enable = cros_ec_enable_writeprotect, |
| 1119 | .disable = cros_ec_disable_writeprotect, |
| 1120 | .wp_status = cros_ec_wp_status, |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1121 | }; |
| 1122 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1123 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1124 | 0, NULL, 0, &info, sizeof(info)); |
Louis Yung-Chieh Lo | f779a7b | 2012-07-30 18:20:39 +0800 | [diff] [blame] | 1125 | if (rc < 0) { |
| 1126 | msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc); |
| 1127 | return 0; |
| 1128 | } |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1129 | rc = cros_ec_get_current_image(); |
Simon Glass | 01c1167 | 2013-07-01 18:03:33 +0900 | [diff] [blame] | 1130 | if (rc < 0) { |
| 1131 | msg_perr("%s(): Failed to probe (no current image): %d\n", |
| 1132 | __func__, rc); |
| 1133 | return 0; |
| 1134 | } |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1135 | cros_ec_priv->current_image = rc; |
| 1136 | cros_ec_priv->region = ®ions[0]; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1137 | |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 1138 | flash->chip->total_size = info.flash_size / 1024; |
| 1139 | flash->chip->page_size = opaque_programmer->max_data_read; |
| 1140 | eraser = &flash->chip->block_erasers[0]; |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1141 | |
| 1142 | /* Allow overriding the erase block size in case EC is incorrect */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1143 | if (cros_ec_priv->erase_block_size > 0) |
| 1144 | eraser->eraseblocks[0].size = cros_ec_priv->erase_block_size; |
Duncan Laurie | 8432872 | 2014-09-10 23:25:01 -0700 | [diff] [blame] | 1145 | else |
| 1146 | eraser->eraseblocks[0].size = info.erase_block_size; |
| 1147 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1148 | eraser->eraseblocks[0].count = info.flash_size / |
| 1149 | eraser->eraseblocks[0].size; |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 1150 | flash->chip->wp = ℘ |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1151 | |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 1152 | /* |
| 1153 | * Some STM32 variants erase bits to 0. For now, assume that this |
| 1154 | * applies to STM32L parts. |
| 1155 | * |
| 1156 | * FIXME: This info will eventually be exposed via some EC command. |
| 1157 | * See chrome-os-partner:20973. |
| 1158 | */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1159 | rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO, |
David Hendricks | 14935fe | 2014-08-14 17:38:24 -0700 | [diff] [blame] | 1160 | 0, NULL, 0, &chip_info, sizeof(chip_info)); |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 1161 | if (rc < 0) { |
| 1162 | msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc); |
| 1163 | return 0; |
| 1164 | } |
Vincent Palatin | 4faff9a | 2017-03-17 17:27:39 +0100 | [diff] [blame] | 1165 | if (!strncmp(chip_info.name, "stm32l1", 7)) |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 1166 | flash->chip->feature_bits |= FEATURE_ERASE_TO_ZERO; |
David Hendricks | 194b3bb | 2013-07-16 14:32:26 -0700 | [diff] [blame] | 1167 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1168 | rc = set_ideal_write_size(); |
David Hendricks | fbd5e6d | 2014-08-21 15:01:43 -0700 | [diff] [blame] | 1169 | if (rc < 0) { |
| 1170 | msg_perr("%s(): Unable to set write size\n", __func__); |
| 1171 | return 0; |
| 1172 | } |
David Hendricks | f9461c7 | 2013-07-11 19:02:13 -0700 | [diff] [blame] | 1173 | |
David Hendricks | a672b04 | 2016-09-19 12:37:36 -0700 | [diff] [blame] | 1174 | rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO, |
| 1175 | 0, NULL, 0, &spi_info, sizeof(spi_info)); |
| 1176 | if (rc < 0) { |
| 1177 | static char chip_vendor[32]; |
| 1178 | static char chip_name[32]; |
| 1179 | |
| 1180 | memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor)); |
| 1181 | memcpy(chip_name, chip_info.name, sizeof(chip_name)); |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 1182 | flash->chip->vendor = chip_vendor; |
| 1183 | flash->chip->name = chip_name; |
| 1184 | flash->chip->tested = TEST_OK_PREWU; |
David Hendricks | a672b04 | 2016-09-19 12:37:36 -0700 | [diff] [blame] | 1185 | } else { |
| 1186 | const struct flashchip *f; |
| 1187 | uint32_t mfg = spi_info.jedec[0]; |
| 1188 | uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2]; |
| 1189 | |
| 1190 | for (f = flashchips; f && f->name; f++) { |
| 1191 | if (f->bustype != BUS_SPI) |
| 1192 | continue; |
| 1193 | if ((f->manufacture_id == mfg) && |
| 1194 | f->model_id == model) { |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 1195 | flash->chip->vendor = f->vendor; |
| 1196 | flash->chip->name = f->name; |
| 1197 | flash->chip->tested = f->tested; |
David Hendricks | a672b04 | 2016-09-19 12:37:36 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 1203 | /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*, |
| 1204 | * so we need to be careful about using these enums as array indices */ |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1205 | rc = cros_ec_get_region_info(EC_FLASH_REGION_RO, |
| 1206 | &cros_ec_priv->region[EC_IMAGE_RO]); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 1207 | if (rc) { |
| 1208 | msg_perr("%s(): Failed to probe (cannot find RO region): %d\n", |
| 1209 | __func__, rc); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Souvik Ghosh | 586968a | 2016-08-11 17:56:24 -0700 | [diff] [blame] | 1213 | rc = cros_ec_get_region_info(EC_FLASH_REGION_RW, |
| 1214 | &cros_ec_priv->region[EC_IMAGE_RW]); |
Simon Glass | c453a64 | 2013-07-01 18:08:53 +0900 | [diff] [blame] | 1215 | if (rc) { |
| 1216 | msg_perr("%s(): Failed to probe (cannot find RW region): %d\n", |
| 1217 | __func__, rc); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1221 | return 1; |
| 1222 | }; |