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