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