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