blob: da83969345c2e50e7f9b188ed2938b62ee6d4cbf [file] [log] [blame]
David Hendricksee712472012-05-23 21:50:59 -07001/*
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 Hendricks14935fe2014-08-14 17:38:24 -070034#include <errno.h>
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include "flashchips.h"
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080040#include "fmap.h"
David Hendricksa5c5cf82014-08-11 16:40:17 -070041#include "cros_ec.h"
42#include "cros_ec_lock.h"
43#include "cros_ec_commands.h"
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +080044#include "programmer.h"
45#include "spi.h"
46#include "writeprotect.h"
47
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080048/* FIXME: used for wp hacks */
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <unistd.h>
Souvik Ghosh586968a2016-08-11 17:56:24 -070053
54struct cros_ec_priv *cros_ec_priv;
55
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080056struct wp_data {
57 int enable;
58 unsigned int start;
59 unsigned int len;
60};
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +080061#define WP_STATE_HACK_FILENAME "/mnt/stateful_partition/flashrom_wp_state"
62
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +080063/* If software sync is enabled, then we don't try the latest firmware copy
64 * after updating.
65 */
66#define SOFTWARE_SYNC_ENABLED
67
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080068/* 1 if we want the flashrom to call erase_and_write_flash() again. */
69static int need_2nd_pass = 0;
70
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +080071/* 1 if we want the flashrom to try jumping to new firmware after update. */
72static int try_latest_firmware = 0;
73
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080074/* The range of each firmware copy from the image file to update.
75 * But re-define the .flags as the valid flag to indicate the firmware is
76 * new or not (if flags = 1).
77 */
78static struct fmap_area fwcopy[4]; // [0] is not used.
79
80/* The names of enum lpc_current_image to match in FMAP area names. */
Gwendal Grignou94e87d62014-11-25 15:34:15 -080081static const char *sections[] = {
David Hendricksbf8c4dd2012-07-19 12:13:17 -070082 "UNKNOWN SECTION", // EC_IMAGE_UNKNOWN -- never matches
83 "EC_RO",
84 "EC_RW",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +080085};
86
Gwendal Grignou94e87d62014-11-25 15:34:15 -080087/*
88 * The names of the different device that can be found in a machine.
89 * Order is important: for backward compatibilty issue,
90 * 'ec' must be 0, 'pd' must be 1.
91 */
92static const char *ec_type[] = {
93 [0] = "ec",
94 [1] = "pd",
95 [2] = "sh",
96};
97
Simon Glassc453a642013-07-01 18:08:53 +090098/* EC_FLASH_REGION_WP_RO is the highest numbered region so it also indicates
99 * the number of regions */
100static struct ec_response_flash_region_info regions[EC_FLASH_REGION_WP_RO + 1];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800101
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800102/* Given the range not able to update, mark the corresponding
103 * firmware as old.
104 */
David Hendricksb907de32014-08-11 16:47:09 -0700105static void cros_ec_invalidate_copy(unsigned int addr, unsigned int len)
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800106{
107 int i;
108
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800109 for (i = EC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800110 struct fmap_area *fw = &fwcopy[i];
111 if ((addr >= fw->offset && (addr < fw->offset + fw->size)) ||
112 (fw->offset >= addr && (fw->offset < addr + len))) {
113 msg_pdbg("Mark firmware [%s] as old.\n",
114 sections[i]);
115 fw->flags = 0; // mark as old
116 }
117 }
118}
119
120
Souvik Ghosh586968a2016-08-11 17:56:24 -0700121static int cros_ec_get_current_image(void)
Simon Glass01c11672013-07-01 18:03:33 +0900122{
123 struct ec_response_get_version resp;
124 int rc;
David Hendricksac1d25c2016-08-09 17:00:58 -0700125
Souvik Ghosh586968a2016-08-11 17:56:24 -0700126 rc = cros_ec_priv->ec_command(EC_CMD_GET_VERSION,
David Hendricks14935fe2014-08-14 17:38:24 -0700127 0, NULL, 0, &resp, sizeof(resp));
Simon Glass01c11672013-07-01 18:03:33 +0900128 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700129 msg_perr("CROS_EC cannot get the running copy: rc=%d\n", rc);
Simon Glass01c11672013-07-01 18:03:33 +0900130 return rc;
131 }
132 if (resp.current_image == EC_IMAGE_UNKNOWN) {
David Hendricksb907de32014-08-11 16:47:09 -0700133 msg_perr("CROS_EC gets unknown running copy\n");
Simon Glass01c11672013-07-01 18:03:33 +0900134 return -1;
135 }
136
137 return resp.current_image;
138}
139
140
Souvik Ghosh586968a2016-08-11 17:56:24 -0700141static int cros_ec_get_region_info(enum ec_flash_region region,
Simon Glass3c01dca2013-07-01 18:07:34 +0900142 struct ec_response_flash_region_info *info)
143{
144 struct ec_params_flash_region_info req;
145 struct ec_response_flash_region_info resp;
146 int rc;
147
148 req.region = region;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700149 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_REGION_INFO,
Simon Glass3c01dca2013-07-01 18:07:34 +0900150 EC_VER_FLASH_REGION_INFO, &req, sizeof(req),
151 &resp, sizeof(resp));
152 if (rc < 0) {
153 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
154 return rc;
155 }
156
157 info->offset = resp.offset;
158 info->size = resp.size;
159 return 0;
160}
161
David Hendricksf9461c72013-07-11 19:02:13 -0700162/**
163 * Get the versions of the command supported by the EC.
164 *
165 * @param cmd Command
166 * @param pmask Destination for version mask; will be set to 0 on
167 * error.
168 * @return 0 if success, <0 if error
169 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700170static int ec_get_cmd_versions(int cmd, uint32_t *pmask)
David Hendricksf9461c72013-07-11 19:02:13 -0700171{
David Hendricksf9461c72013-07-11 19:02:13 -0700172 struct ec_params_get_cmd_versions pver;
173 struct ec_response_get_cmd_versions rver;
174 int rc;
175
176 *pmask = 0;
177
178 pver.cmd = cmd;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700179 rc = cros_ec_priv->ec_command(EC_CMD_GET_CMD_VERSIONS, 0,
David Hendricksf9461c72013-07-11 19:02:13 -0700180 &pver, sizeof(pver), &rver, sizeof(rver));
181
182 if (rc < 0)
183 return rc;
184
185 *pmask = rver.version_mask;
186 return rc;
187}
188
189/**
190 * Return non-zero if the EC supports the command and version
191 *
192 * @param cmd Command to check
193 * @param ver Version to check
194 * @return non-zero if command version supported; 0 if not.
195 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700196static int ec_cmd_version_supported(int cmd, int ver)
David Hendricksf9461c72013-07-11 19:02:13 -0700197{
198 uint32_t mask = 0;
199 int rc;
David Hendricksd13d90d2016-08-09 17:00:52 -0700200
David Hendricksac1d25c2016-08-09 17:00:58 -0700201 rc = ec_get_cmd_versions(cmd, &mask);
David Hendricksf9461c72013-07-11 19:02:13 -0700202 if (rc < 0)
203 return rc;
204
205 return (mask & EC_VER_MASK(ver)) ? 1 : 0;
206}
207
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700208/* returns 0 if successful or <0 to indicate error */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700209static int set_ideal_write_size(void)
David Hendricksf9461c72013-07-11 19:02:13 -0700210{
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700211 int cmd_version, ret;
David Hendricksf9461c72013-07-11 19:02:13 -0700212
David Hendricksac1d25c2016-08-09 17:00:58 -0700213 cmd_version = ec_cmd_version_supported(EC_CMD_FLASH_WRITE,
David Hendricksfb405f12014-08-19 22:42:30 -0700214 EC_VER_FLASH_WRITE);
215 if (cmd_version < 0) {
216 msg_perr("Cannot determine write command version\n");
217 return cmd_version;
218 } else if (cmd_version == 0) {
219 struct ec_response_flash_info info;
David Hendricksf9461c72013-07-11 19:02:13 -0700220
Souvik Ghosh586968a2016-08-11 17:56:24 -0700221 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700222 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700223 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700224 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700225 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700226 }
227
Souvik Ghosh586968a2016-08-11 17:56:24 -0700228 cros_ec_priv->ideal_write_size = EC_FLASH_WRITE_VER0_SIZE;
David Hendricksfb405f12014-08-19 22:42:30 -0700229 } else {
230 struct ec_response_flash_info_1 info;
231
Souvik Ghosh586968a2016-08-11 17:56:24 -0700232 ret = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricksfb405f12014-08-19 22:42:30 -0700233 cmd_version, NULL, 0, &info, sizeof(info));
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700234 if (ret < 0) {
David Hendricksfb405f12014-08-19 22:42:30 -0700235 msg_perr("%s(): Cannot get flash info.\n", __func__);
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700236 return ret;
David Hendricksfb405f12014-08-19 22:42:30 -0700237 }
238
Souvik Ghosh586968a2016-08-11 17:56:24 -0700239 cros_ec_priv->ideal_write_size = info.write_ideal_size;
David Hendricksf9461c72013-07-11 19:02:13 -0700240 }
241
David Hendricksfb405f12014-08-19 22:42:30 -0700242 return 0;
David Hendricksf9461c72013-07-11 19:02:13 -0700243}
Simon Glass3c01dca2013-07-01 18:07:34 +0900244
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800245/* Asks EC to jump to a firmware copy. If target is EC_IMAGE_UNKNOWN,
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800246 * then this functions picks a NEW firmware copy and jumps to it. Note that
247 * RO is preferred, then A, finally B.
248 *
249 * Returns 0 for success.
250 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700251static int cros_ec_jump_copy(enum ec_current_image target) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800252 struct ec_params_reboot_ec p;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800253 int rc;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700254 int current_image;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800255
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800256 /* Since the EC may return EC_RES_SUCCESS twice if the EC doesn't
257 * jump to different firmware copy. The second EC_RES_SUCCESS would
258 * set the OBF=1 and the next command cannot be executed.
259 * Thus, we call EC to jump only if the target is different.
260 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700261 current_image = cros_ec_get_current_image();
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700262 if (current_image < 0)
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800263 return 1;
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700264 if (current_image == target)
Simon Glassc453a642013-07-01 18:08:53 +0900265 return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800266
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800267 memset(&p, 0, sizeof(p));
Simon Glassc453a642013-07-01 18:08:53 +0900268
269 /* Translate target --> EC reboot command parameter */
270 switch (target) {
271 case EC_IMAGE_RO:
272 p.cmd = EC_REBOOT_JUMP_RO;
273 break;
274 case EC_IMAGE_RW:
275 p.cmd = EC_REBOOT_JUMP_RW;
276 break;
277 default:
278 /*
279 * If target is unspecified, set EC reboot command to use
280 * a new image. Also set "target" so that it may be used
281 * to update the priv->current_image if jump is successful.
282 */
283 if (fwcopy[EC_IMAGE_RO].flags) {
284 p.cmd = EC_REBOOT_JUMP_RO;
285 target = EC_IMAGE_RO;
286 } else if (fwcopy[EC_IMAGE_RW].flags) {
287 p.cmd = EC_REBOOT_JUMP_RW;
288 target = EC_IMAGE_RW;
289 } else {
290 p.cmd = EC_IMAGE_UNKNOWN;
291 }
292 break;
293 }
294
David Hendricksb907de32014-08-11 16:47:09 -0700295 msg_pdbg("CROS_EC is jumping to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800296 if (p.cmd == EC_IMAGE_UNKNOWN) return 1;
297
Vadim Bendebury9fa26e82013-09-19 13:56:32 -0700298 if (current_image == p.cmd) {
David Hendricksb907de32014-08-11 16:47:09 -0700299 msg_pdbg("CROS_EC is already in [%s]\n", sections[p.cmd]);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700300 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800301 return 0;
302 }
303
Souvik Ghosh586968a2016-08-11 17:56:24 -0700304 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700305 0, &p, sizeof(p), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800306 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700307 msg_perr("CROS_EC cannot jump to [%s]:%d\n",
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800308 sections[p.cmd], rc);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800309 } else {
David Hendricksb907de32014-08-11 16:47:09 -0700310 msg_pdbg("CROS_EC has jumped to [%s]\n", sections[p.cmd]);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800311 rc = EC_RES_SUCCESS;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700312 cros_ec_priv->current_image = target;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800313 }
314
315 /* Sleep 1 sec to wait the EC re-init. */
316 usleep(1000000);
317
David Hendricksf9461c72013-07-11 19:02:13 -0700318 /* update max data write size in case we're jumping to an EC
319 * firmware with different protocol */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700320 set_ideal_write_size();
David Hendricksf9461c72013-07-11 19:02:13 -0700321
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800322 return rc;
323}
324
325
326/* Given an image, this function parses FMAP and recognize the firmware
327 * ranges.
328 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700329int cros_ec_prepare(uint8_t *image, int size) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800330 struct fmap *fmap;
331 int i, j;
332
Souvik Ghosh586968a2016-08-11 17:56:24 -0700333 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800334
335 // Parse the fmap in the image file and cache the firmware ranges.
336 fmap = fmap_find_in_memory(image, size);
337 if (!fmap) return 0;
338
339 // Lookup RO/A/B sections in FMAP.
340 for (i = 0; i < fmap->nareas; i++) {
341 struct fmap_area *fa = &fmap->areas[i];
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800342 for (j = EC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) {
David Hendricks5b06c882012-05-20 18:27:25 -0700343 if (!strcmp(sections[j], (const char *)fa->name)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800344 msg_pdbg("Found '%s' in image.\n", fa->name);
345 memcpy(&fwcopy[j], fa, sizeof(*fa));
346 fwcopy[j].flags = 1; // mark as new
347 }
348 }
349 }
350
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800351 /* Warning: before update, we jump the EC to RO copy. If you want to
David Hendricksb907de32014-08-11 16:47:09 -0700352 * change this behavior, please also check the cros_ec_finish().
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800353 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700354 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800355}
356
357
358/* Returns >0 if we need 2nd pass of erase_and_write_flash().
359 * <0 if we cannot jump to any firmware copy.
360 * ==0 if no more pass is needed.
361 *
362 * This function also jumps to new-updated firmware copy before return >0.
363 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700364int cros_ec_need_2nd_pass(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700365 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800366
367 if (need_2nd_pass) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700368 if (cros_ec_jump_copy(EC_IMAGE_UNKNOWN)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800369 return -1;
370 }
371 }
372
373 return need_2nd_pass;
374}
375
376
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800377/* Returns 0 for success.
378 *
379 * Try latest firmware: B > A > RO
380 *
David Hendricksb907de32014-08-11 16:47:09 -0700381 * This function assumes the EC jumps to RO at cros_ec_prepare() so that
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800382 * the fwcopy[RO].flags is old (0) and A/B are new. Please also refine
David Hendricksb907de32014-08-11 16:47:09 -0700383 * this code logic if you change the cros_ec_prepare() behavior.
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800384 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700385int cros_ec_finish(void) {
Souvik Ghosh586968a2016-08-11 17:56:24 -0700386 if (!(cros_ec_priv && cros_ec_priv->detected)) return 0;
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800387
388 if (try_latest_firmware) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800389 if (fwcopy[EC_IMAGE_RW].flags &&
David Hendricksac1d25c2016-08-09 17:00:58 -0700390 cros_ec_jump_copy(EC_IMAGE_RW) == 0) return 0;
391 return cros_ec_jump_copy(EC_IMAGE_RO);
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800392 }
393
394 return 0;
395}
396
397
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700398int cros_ec_read(struct flashctx *flash, uint8_t *readarr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800399 unsigned int blockaddr, unsigned int readcnt) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800400 int rc = 0;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800401 struct ec_params_flash_read p;
David Hendricksac1d25c2016-08-09 17:00:58 -0700402 int maxlen = opaque_programmer->max_data_read;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800403 uint8_t buf[maxlen];
David Hendricks133083b2012-07-17 20:39:38 -0700404 int offset = 0, count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800405
David Hendricks133083b2012-07-17 20:39:38 -0700406 while (offset < readcnt) {
407 count = min(maxlen, readcnt - offset);
408 p.offset = blockaddr + offset;
409 p.size = count;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700410 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_READ,
David Hendricks14935fe2014-08-14 17:38:24 -0700411 0, &p, sizeof(p), buf, count);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800412 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700413 msg_perr("CROS_EC: Flash read error at offset 0x%x\n",
David Hendricks133083b2012-07-17 20:39:38 -0700414 blockaddr + offset);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800415 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800416 } else {
417 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800418 }
419
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800420 memcpy(readarr + offset, buf, count);
David Hendricks133083b2012-07-17 20:39:38 -0700421 offset += count;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800422 }
423
424 return rc;
425}
426
427
Simon Glassc453a642013-07-01 18:08:53 +0900428/*
429 * returns 0 to indicate area does not overlap current EC image
430 * returns 1 to indicate area overlaps current EC image or error
431 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700432static int in_current_image(unsigned int addr, unsigned int len)
Simon Glassc453a642013-07-01 18:08:53 +0900433{
Simon Glassc453a642013-07-01 18:08:53 +0900434 enum ec_current_image image;
435 uint32_t region_offset;
436 uint32_t region_size;
437
Souvik Ghosh586968a2016-08-11 17:56:24 -0700438 image = cros_ec_priv->current_image;
439 region_offset = cros_ec_priv->region[image].offset;
440 region_size = cros_ec_priv->region[image].size;
Simon Glassc453a642013-07-01 18:08:53 +0900441
442 if ((addr + len - 1 < region_offset) ||
443 (addr > region_offset + region_size - 1)) {
444 return 0;
445 }
446 return 1;
447}
448
449
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700450int cros_ec_block_erase(struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800451 unsigned int blockaddr,
452 unsigned int len) {
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800453 struct ec_params_flash_erase erase;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800454 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800455
Souvik Ghosh586968a2016-08-11 17:56:24 -0700456 if (in_current_image(blockaddr, len)) {
David Hendricksb907de32014-08-11 16:47:09 -0700457 cros_ec_invalidate_copy(blockaddr, len);
Simon Glassc453a642013-07-01 18:08:53 +0900458 need_2nd_pass = 1;
459 return ACCESS_DENIED;
460 }
461
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800462 erase.offset = blockaddr;
463 erase.size = len;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700464 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_ERASE,
David Hendricks14935fe2014-08-14 17:38:24 -0700465 0, &erase, sizeof(erase), NULL, 0);
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800466 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800467 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700468 cros_ec_invalidate_copy(blockaddr, len);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800469 need_2nd_pass = 1;
470 return ACCESS_DENIED;
471 }
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800472 if (rc < 0) {
David Hendricksb907de32014-08-11 16:47:09 -0700473 msg_perr("CROS_EC: Flash erase error at address 0x%x, rc=%d\n",
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800474 blockaddr, rc);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800475 return rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800476 } else {
477 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800478 }
479
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800480#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800481 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800482#endif
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800483 return rc;
484}
485
486
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700487int cros_ec_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800488 unsigned int nbytes) {
489 int i, rc = 0;
Ken Chang69c31b82014-10-28 15:17:21 +0800490 unsigned int written = 0, real_write_size;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800491 struct ec_params_flash_write p;
David Hendricks2d6db772013-07-10 21:07:48 -0700492 uint8_t *packet;
493
Ken Chang69c31b82014-10-28 15:17:21 +0800494 /*
495 * For chrome-os-partner:33035, to workaround the undersized
496 * outdata buffer issue in kernel.
497 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700498 real_write_size = min(opaque_programmer->max_data_write,
Souvik Ghosh586968a2016-08-11 17:56:24 -0700499 cros_ec_priv->ideal_write_size);
Ken Chang69c31b82014-10-28 15:17:21 +0800500 packet = malloc(sizeof(p) + real_write_size);
David Hendricks2d6db772013-07-10 21:07:48 -0700501 if (!packet)
502 return -1;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800503
504 for (i = 0; i < nbytes; i += written) {
Ken Chang69c31b82014-10-28 15:17:21 +0800505 written = min(nbytes - i, real_write_size);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800506 p.offset = addr + i;
507 p.size = written;
Simon Glassc453a642013-07-01 18:08:53 +0900508
Souvik Ghosh586968a2016-08-11 17:56:24 -0700509 if (in_current_image(p.offset, p.size)) {
David Hendricksb907de32014-08-11 16:47:09 -0700510 cros_ec_invalidate_copy(addr, nbytes);
Simon Glassc453a642013-07-01 18:08:53 +0900511 need_2nd_pass = 1;
512 return ACCESS_DENIED;
513 }
514
David Hendricks2d6db772013-07-10 21:07:48 -0700515 memcpy(packet, &p, sizeof(p));
516 memcpy(packet + sizeof(p), &buf[i], written);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700517 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_WRITE,
David Hendricks14935fe2014-08-14 17:38:24 -0700518 0, packet, sizeof(p) + p.size, NULL, 0);
David Hendricks2d6db772013-07-10 21:07:48 -0700519
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800520 if (rc == -EC_RES_ACCESS_DENIED) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800521 // this is active image.
David Hendricksb907de32014-08-11 16:47:09 -0700522 cros_ec_invalidate_copy(addr, nbytes);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +0800523 need_2nd_pass = 1;
524 return ACCESS_DENIED;
525 }
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800526
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800527 if (rc < 0) break;
528 rc = EC_RES_SUCCESS;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800529 }
530
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800531#ifndef SOFTWARE_SYNC_ENABLED
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +0800532 try_latest_firmware = 1;
Louis Yung-Chieh Loef88ec32012-09-20 10:39:35 +0800533#endif
David Hendricks2d6db772013-07-10 21:07:48 -0700534 free(packet);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800535 return rc;
536}
537
538
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700539static int cros_ec_list_ranges(const struct flashctx *flash) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900540 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800541 int rc;
542
Souvik Ghosh586968a2016-08-11 17:56:24 -0700543 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800544 if (rc < 0) {
545 msg_perr("Cannot get the WP_RO region info: %d\n", rc);
546 return 1;
547 }
548
549 msg_pinfo("Supported write protect range:\n");
550 msg_pinfo(" disable: start=0x%06x len=0x%06x\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900551 msg_pinfo(" enable: start=0x%06x len=0x%06x\n", info.offset,
552 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800553
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800554 return 0;
555}
556
557
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800558/*
559 * Helper function for flash protection.
560 *
561 * On EC API v1, the EC write protection has been simplified to one-bit:
562 * EC_FLASH_PROTECT_RO_AT_BOOT, which means the state is either enabled
563 * or disabled. However, this is different from the SPI-style write protect
564 * behavior. Thus, we re-define the flashrom command (SPI-style) so that
565 * either SRP or range is non-zero, the EC_FLASH_PROTECT_RO_AT_BOOT is set.
566 *
567 * SRP Range | PROTECT_RO_AT_BOOT
568 * 0 0 | 0
569 * 0 non-zero | 1
570 * 1 0 | 1
571 * 1 non-zero | 1
572 *
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800573 *
574 * Besides, to make the protection take effect as soon as possible, we
575 * try to set EC_FLASH_PROTECT_RO_NOW at the same time. However, not
576 * every EC supports RO_NOW, thus we then try to protect the entire chip.
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800577 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700578static int set_wp(int enable) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800579 struct ec_params_flash_protect p;
580 struct ec_response_flash_protect r;
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800581 const int ro_at_boot_flag = EC_FLASH_PROTECT_RO_AT_BOOT;
582 const int ro_now_flag = EC_FLASH_PROTECT_RO_NOW;
583 int need_an_ec_cold_reset = 0;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800584 int rc;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800585
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800586 /* Try to set RO_AT_BOOT and RO_NOW first */
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800587 memset(&p, 0, sizeof(p));
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800588 p.mask = (ro_at_boot_flag | ro_now_flag);
589 p.flags = enable ? (ro_at_boot_flag | ro_now_flag) : 0;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700590 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700591 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800592 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800593 msg_perr("FAILED: Cannot set the RO_AT_BOOT and RO_NOW: %d\n",
594 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800595 return 1;
596 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800597
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800598 /* Read back */
599 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700600 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700601 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800602 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800603 msg_perr("FAILED: Cannot get RO_AT_BOOT and RO_NOW: %d\n",
604 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800605 return 1;
606 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800607
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800608 if (!enable) {
609 /* The disable case is easier to check. */
610 if (r.flags & ro_at_boot_flag) {
611 msg_perr("FAILED: RO_AT_BOOT is not clear.\n");
612 return 1;
613 } else if (r.flags & ro_now_flag) {
614 msg_perr("FAILED: RO_NOW is asserted unexpectedly.\n");
615 need_an_ec_cold_reset = 1;
616 goto exit;
617 }
618
619 msg_pdbg("INFO: RO_AT_BOOT is clear.\n");
620 return 0;
621 }
622
623 /* Check if RO_AT_BOOT is set. If not, fail in anyway. */
624 if (r.flags & ro_at_boot_flag) {
625 msg_pdbg("INFO: RO_AT_BOOT has been set.\n");
626 } else {
627 msg_perr("FAILED: RO_AT_BOOT is not set.\n");
628 return 1;
629 }
630
631 /* Then, we check if the protection has been activated. */
632 if (r.flags & ro_now_flag) {
633 /* Good, RO_NOW is set. */
634 msg_pdbg("INFO: RO_NOW is set. WP is active now.\n");
635 } else if (r.writable_flags & EC_FLASH_PROTECT_ALL_NOW) {
636 struct ec_params_reboot_ec reboot;
637
638 msg_pdbg("WARN: RO_NOW is not set. Trying ALL_NOW.\n");
639
640 memset(&p, 0, sizeof(p));
641 p.mask = EC_FLASH_PROTECT_ALL_NOW;
642 p.flags = EC_FLASH_PROTECT_ALL_NOW;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700643 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800644 EC_VER_FLASH_PROTECT,
645 &p, sizeof(p), &r, sizeof(r));
646 if (rc < 0) {
647 msg_perr("FAILED: Cannot set ALL_NOW: %d\n", rc);
648 return 1;
649 }
650
651 /* Read back */
652 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700653 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800654 EC_VER_FLASH_PROTECT,
655 &p, sizeof(p), &r, sizeof(r));
656 if (rc < 0) {
657 msg_perr("FAILED:Cannot get ALL_NOW: %d\n", rc);
658 return 1;
659 }
660
661 if (!(r.flags & EC_FLASH_PROTECT_ALL_NOW)) {
662 msg_perr("FAILED: ALL_NOW is not set.\n");
663 need_an_ec_cold_reset = 1;
664 goto exit;
665 }
666
667 msg_pdbg("INFO: ALL_NOW has been set. WP is active now.\n");
668
669 /*
670 * Our goal is to protect the RO ASAP. The entire protection
671 * is just a workaround for platform not supporting RO_NOW.
672 * It has side-effect that the RW is also protected and leads
673 * the RW update failed. So, we arrange an EC code reset to
674 * unlock RW ASAP.
675 */
676 memset(&reboot, 0, sizeof(reboot));
677 reboot.cmd = EC_REBOOT_COLD;
678 reboot.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;
Souvik Ghosh586968a2016-08-11 17:56:24 -0700679 rc = cros_ec_priv->ec_command(EC_CMD_REBOOT_EC,
David Hendricks14935fe2014-08-14 17:38:24 -0700680 0, &reboot, sizeof(reboot), NULL, 0);
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800681 if (rc < 0) {
682 msg_perr("WARN: Cannot arrange a cold reset at next "
683 "shutdown to unlock entire protect.\n");
684 msg_perr(" But you can do it manually.\n");
685 } else {
686 msg_pdbg("INFO: A cold reset is arranged at next "
687 "shutdown.\n");
688 }
689
690 } else {
691 msg_perr("FAILED: RO_NOW is not set.\n");
692 msg_perr("FAILED: The PROTECT_RO_AT_BOOT is set, but cannot "
693 "make write protection active now.\n");
694 need_an_ec_cold_reset = 1;
695 }
696
697exit:
698 if (need_an_ec_cold_reset) {
699 msg_perr("FAILED: You may need a reboot to take effect of "
700 "PROTECT_RO_AT_BOOT.\n");
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800701 return 1;
702 }
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800703
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800704 return 0;
705}
706
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700707static int cros_ec_set_range(const struct flashctx *flash,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800708 unsigned int start, unsigned int len) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900709 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800710 int rc;
711
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800712 /* Check if the given range is supported */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700713 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800714 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800715 msg_perr("FAILED: Cannot get the WP_RO region info: %d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800716 return 1;
717 }
718 if ((!start && !len) || /* list supported ranges */
Simon Glass3c01dca2013-07-01 18:07:34 +0900719 ((start == info.offset) && (len == info.size))) {
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800720 /* pass */
721 } else {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800722 msg_perr("FAILED: Unsupported write protection range "
723 "(0x%06x,0x%06x)\n\n", start, len);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800724 msg_perr("Currently supported range:\n");
725 msg_perr(" disable: (0x%06x,0x%06x)\n", 0, 0);
Simon Glass3c01dca2013-07-01 18:07:34 +0900726 msg_perr(" enable: (0x%06x,0x%06x)\n", info.offset,
727 info.size);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800728 return 1;
729 }
730
David Hendricksac1d25c2016-08-09 17:00:58 -0700731 return set_wp(!!len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800732}
733
734
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700735static int cros_ec_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -0700736 enum wp_mode wp_mode) {
737 int ret;
738
739 switch (wp_mode) {
740 case WP_MODE_HARDWARE:
David Hendricksac1d25c2016-08-09 17:00:58 -0700741 ret = set_wp(1);
David Hendricks1c09f802012-10-03 11:03:48 -0700742 break;
743 default:
744 msg_perr("%s():%d Unsupported write-protection mode\n",
745 __func__, __LINE__);
746 ret = 1;
747 break;
748 }
749
750 return ret;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800751}
752
753
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700754static int cros_ec_disable_writeprotect(const struct flashctx *flash) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700755 return set_wp(0);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800756}
757
758
Souvik Ghosh586968a2016-08-11 17:56:24 -0700759static int cros_ec_wp_status(const struct flashctx *flash) {;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800760 struct ec_params_flash_protect p;
761 struct ec_response_flash_protect r;
762 int start, len; /* wp range */
763 int enabled;
764 int rc;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800765
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800766 memset(&p, 0, sizeof(p));
Souvik Ghosh586968a2016-08-11 17:56:24 -0700767 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_PROTECT,
David Hendricks14935fe2014-08-14 17:38:24 -0700768 EC_VER_FLASH_PROTECT, &p, sizeof(p), &r, sizeof(r));
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800769 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800770 msg_perr("FAILED: Cannot get the write protection status: %d\n",
771 rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800772 return 1;
773 } else if (rc < sizeof(r)) {
David Hendricksf797dde2012-10-30 11:39:12 -0700774 msg_perr("FAILED: Too little data returned (expected:%zd, "
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800775 "actual:%d)\n", sizeof(r), rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800776 return 1;
777 }
778
779 start = len = 0;
780 if (r.flags & EC_FLASH_PROTECT_RO_AT_BOOT) {
Simon Glass3c01dca2013-07-01 18:07:34 +0900781 struct ec_response_flash_region_info info;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800782
783 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is set.\n",
784 __func__);
Souvik Ghosh586968a2016-08-11 17:56:24 -0700785 rc = cros_ec_get_region_info(EC_FLASH_REGION_WP_RO, &info);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800786 if (rc < 0) {
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800787 msg_perr("FAILED: Cannot get the WP_RO region info: "
788 "%d\n", rc);
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800789 return 1;
790 }
Simon Glass3c01dca2013-07-01 18:07:34 +0900791 start = info.offset;
792 len = info.size;
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800793 } else {
794 msg_pdbg("%s(): EC_FLASH_PROTECT_RO_AT_BOOT is clear.\n",
795 __func__);
796 }
797
Louis Yung-Chieh Loca052c42012-08-24 14:12:21 +0800798 /*
799 * If neither RO_NOW or ALL_NOW is set, it means write protect is
800 * NOT active now.
801 */
802 if (!(r.flags & (EC_FLASH_PROTECT_RO_NOW | EC_FLASH_PROTECT_ALL_NOW)))
803 start = len = 0;
804
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800805 /* Remove the SPI-style messages. */
806 enabled = r.flags & EC_FLASH_PROTECT_RO_AT_BOOT ? 1 : 0;
807 msg_pinfo("WP: status: 0x%02x\n", enabled ? 0x80 : 0x00);
808 msg_pinfo("WP: status.srp0: %x\n", enabled);
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800809 msg_pinfo("WP: write protect is %s.\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800810 enabled ? "enabled" : "disabled");
Louis Yung-Chieh Lo05b7a7b2012-08-06 19:10:39 +0800811 msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n",
Louis Yung-Chieh Lo3e6da212012-08-13 17:21:01 +0800812 start, len);
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800813
814 return 0;
815}
816
David Hendrickse5454932013-11-04 18:16:11 -0800817/* perform basic "hello" test to see if we can talk to the EC */
David Hendricksb907de32014-08-11 16:47:09 -0700818int cros_ec_test(struct cros_ec_priv *priv)
David Hendrickse5454932013-11-04 18:16:11 -0800819{
820 struct ec_params_hello request;
821 struct ec_response_hello response;
David Hendrickse5454932013-11-04 18:16:11 -0800822 int rc = 0;
823
824 /* Say hello to EC. */
825 request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */
826 msg_pdbg("%s: sending HELLO request with 0x%08x\n",
827 __func__, request.in_data);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800828 rc = priv->ec_command(EC_CMD_HELLO, 0, &request,
David Hendrickse5454932013-11-04 18:16:11 -0800829 sizeof(request), &response, sizeof(response));
830 msg_pdbg("%s: response: 0x%08x\n", __func__, response.out_data);
831
832 if (rc < 0 || response.out_data != 0xf1e2d3c4) {
833 msg_pdbg("response.out_data is not 0xf1e2d3c4.\n"
834 "rc=%d, request=0x%x response=0x%x\n",
835 rc, request.in_data, response.out_data);
836 return 1;
837 }
838
839 return 0;
840}
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800841
David Hendricksd13d90d2016-08-09 17:00:52 -0700842void cros_ec_set_max_size(struct cros_ec_priv *priv,
843 struct opaque_programmer *op) {
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700844 struct ec_response_get_protocol_info info;
845 int rc = 0;
846 msg_pdbg("%s: sending protoinfo command\n", __func__);
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800847 rc = priv->ec_command(EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0,
Puthikorn Voravootivatc0993cf2014-08-28 16:04:58 -0700848 &info, sizeof(info));
849 msg_pdbg("%s: rc:%d\n", __func__, rc);
850
851 if (rc == sizeof(info)) {
852 op->max_data_write = min(op->max_data_write,
853 info.max_request_packet_size -
854 sizeof(struct ec_host_request));
855 op->max_data_read = min(op->max_data_read,
856 info.max_response_packet_size -
857 sizeof(struct ec_host_response));
858 msg_pdbg("%s: max_write:%d max_read:%d\n", __func__,
859 op->max_data_write, op->max_data_read);
860 }
861}
862
David Hendricks14935fe2014-08-14 17:38:24 -0700863
864/*
David Hendricks052446b2014-09-11 11:26:51 -0700865 * Returns 0 to indicate success, non-zero otherwise
David Hendricks14935fe2014-08-14 17:38:24 -0700866 *
867 * This function parses programmer parameters from the command line. Since
868 * CrOS EC hangs off the "internal programmer" (AP, PCH, etc) this gets
869 * run during internal programmer initialization.
870 */
871int cros_ec_parse_param(struct cros_ec_priv *priv)
872{
873 char *p;
Souvik Ghoshf1608b42016-06-30 16:03:55 -0700874
David Hendricksd13d90d2016-08-09 17:00:52 -0700875 p = extract_programmer_param("dev");
David Hendricks14935fe2014-08-14 17:38:24 -0700876 if (p) {
877 unsigned int index;
878 char *endptr = NULL;
879
880 errno = 0;
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800881 /*
882 * For backward compatibility, check if the index is
883 * a number: 0: main EC, 1: PD
884 * works only on Samus.
885 */
David Hendricks14935fe2014-08-14 17:38:24 -0700886 index = strtoul(p, &endptr, 10);
887 if (errno || (endptr != (p + 1)) || (strlen(p) > 1)) {
888 msg_perr("Invalid argument: \"%s\"\n", p);
889 return 1;
890 }
891
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800892 if (index > 1) {
David Hendricks14935fe2014-08-14 17:38:24 -0700893 msg_perr("%s: Invalid device index\n", __func__);
894 return 1;
895 }
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800896 priv->dev = ec_type[index];
897 msg_pdbg("Target %s used\n", priv->dev);
898 }
David Hendricks14935fe2014-08-14 17:38:24 -0700899
Gwendal Grignou94e87d62014-11-25 15:34:15 -0800900 p = extract_programmer_param("type");
901 if (p) {
902 unsigned int index;
903 for (index = 0; index < ARRAY_SIZE(ec_type); index++)
904 if (!strcmp(p, ec_type[index]))
905 break;
906 if (index == ARRAY_SIZE(ec_type)) {
907 msg_perr("Invalid argument: \"%s\"\n", p);
908 return 1;
909 }
910 priv->dev = ec_type[index];
911 msg_pdbg("Target %s used\n", priv->dev);
David Hendricks14935fe2014-08-14 17:38:24 -0700912 }
913
Duncan Laurie84328722014-09-10 23:25:01 -0700914 p = extract_programmer_param("block");
915 if (p) {
916 unsigned int block;
917 char *endptr = NULL;
918
919 errno = 0;
920 block = strtoul(p, &endptr, 0);
921 if (errno || (strlen(p) > 10) || (endptr != (p + strlen(p)))) {
922 msg_perr("Invalid argument: \"%s\"\n", p);
923 return 1;
924 }
925
926 if (block <= 0) {
927 msg_perr("%s: Invalid block size\n", __func__);
928 return 1;
929 }
930
931 msg_pdbg("Override block size to 0x%x\n", block);
932 priv->erase_block_size = block;
933 }
934
David Hendricks14935fe2014-08-14 17:38:24 -0700935 return 0;
936}
937
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700938int cros_ec_probe_size(struct flashctx *flash) {
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800939 int rc;
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800940 struct ec_response_flash_info info;
David Hendricksa672b042016-09-19 12:37:36 -0700941 struct ec_response_flash_spi_info spi_info;
David Hendricks194b3bb2013-07-16 14:32:26 -0700942 struct ec_response_get_chip_info chip_info;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800943 struct block_eraser *eraser;
944 static struct wp wp = {
David Hendricksb907de32014-08-11 16:47:09 -0700945 .list_ranges = cros_ec_list_ranges,
946 .set_range = cros_ec_set_range,
947 .enable = cros_ec_enable_writeprotect,
948 .disable = cros_ec_disable_writeprotect,
949 .wp_status = cros_ec_wp_status,
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800950 };
951
Souvik Ghosh586968a2016-08-11 17:56:24 -0700952 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -0700953 0, NULL, 0, &info, sizeof(info));
Louis Yung-Chieh Lof779a7b2012-07-30 18:20:39 +0800954 if (rc < 0) {
955 msg_perr("%s(): FLASH_INFO returns %d.\n", __func__, rc);
956 return 0;
957 }
Souvik Ghosh586968a2016-08-11 17:56:24 -0700958 rc = cros_ec_get_current_image();
Simon Glass01c11672013-07-01 18:03:33 +0900959 if (rc < 0) {
960 msg_perr("%s(): Failed to probe (no current image): %d\n",
961 __func__, rc);
962 return 0;
963 }
Souvik Ghosh586968a2016-08-11 17:56:24 -0700964 cros_ec_priv->current_image = rc;
965 cros_ec_priv->region = &regions[0];
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800966
967 flash->total_size = info.flash_size / 1024;
David Hendricksac1d25c2016-08-09 17:00:58 -0700968 flash->page_size = opaque_programmer->max_data_read;
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800969 eraser = &flash->block_erasers[0];
Duncan Laurie84328722014-09-10 23:25:01 -0700970
971 /* Allow overriding the erase block size in case EC is incorrect */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700972 if (cros_ec_priv->erase_block_size > 0)
973 eraser->eraseblocks[0].size = cros_ec_priv->erase_block_size;
Duncan Laurie84328722014-09-10 23:25:01 -0700974 else
975 eraser->eraseblocks[0].size = info.erase_block_size;
976
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +0800977 eraser->eraseblocks[0].count = info.flash_size /
978 eraser->eraseblocks[0].size;
979 flash->wp = &wp;
980
David Hendricks194b3bb2013-07-16 14:32:26 -0700981 /*
982 * Some STM32 variants erase bits to 0. For now, assume that this
983 * applies to STM32L parts.
984 *
985 * FIXME: This info will eventually be exposed via some EC command.
986 * See chrome-os-partner:20973.
987 */
Souvik Ghosh586968a2016-08-11 17:56:24 -0700988 rc = cros_ec_priv->ec_command(EC_CMD_GET_CHIP_INFO,
David Hendricks14935fe2014-08-14 17:38:24 -0700989 0, NULL, 0, &chip_info, sizeof(chip_info));
David Hendricks194b3bb2013-07-16 14:32:26 -0700990 if (rc < 0) {
991 msg_perr("%s(): CHIP_INFO returned %d.\n", __func__, rc);
992 return 0;
993 }
994 if (!strncmp(chip_info.name, "stm32l", 6))
995 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
996
Souvik Ghosh586968a2016-08-11 17:56:24 -0700997 rc = set_ideal_write_size();
David Hendricksfbd5e6d2014-08-21 15:01:43 -0700998 if (rc < 0) {
999 msg_perr("%s(): Unable to set write size\n", __func__);
1000 return 0;
1001 }
David Hendricksf9461c72013-07-11 19:02:13 -07001002
David Hendricksa672b042016-09-19 12:37:36 -07001003 rc = cros_ec_priv->ec_command(EC_CMD_FLASH_SPI_INFO,
1004 0, NULL, 0, &spi_info, sizeof(spi_info));
1005 if (rc < 0) {
1006 static char chip_vendor[32];
1007 static char chip_name[32];
1008
1009 memcpy(chip_vendor, chip_info.vendor, sizeof(chip_vendor));
1010 memcpy(chip_name, chip_info.name, sizeof(chip_name));
1011 flash->vendor = chip_vendor;
1012 flash->name = chip_name;
1013 flash->tested = TEST_OK_PREWU;
1014 } else {
1015 const struct flashchip *f;
1016 uint32_t mfg = spi_info.jedec[0];
1017 uint32_t model = (spi_info.jedec[1] << 8) | spi_info.jedec[2];
1018
1019 for (f = flashchips; f && f->name; f++) {
1020 if (f->bustype != BUS_SPI)
1021 continue;
1022 if ((f->manufacture_id == mfg) &&
1023 f->model_id == model) {
1024 flash->vendor = f->vendor;
1025 flash->name = f->name;
1026 flash->tested = f->tested;
1027 break;
1028 }
1029 }
1030 }
1031
Simon Glassc453a642013-07-01 18:08:53 +09001032 /* FIXME: EC_IMAGE_* is ordered differently from EC_FLASH_REGION_*,
1033 * so we need to be careful about using these enums as array indices */
Souvik Ghosh586968a2016-08-11 17:56:24 -07001034 rc = cros_ec_get_region_info(EC_FLASH_REGION_RO,
1035 &cros_ec_priv->region[EC_IMAGE_RO]);
Simon Glassc453a642013-07-01 18:08:53 +09001036 if (rc) {
1037 msg_perr("%s(): Failed to probe (cannot find RO region): %d\n",
1038 __func__, rc);
1039 return 0;
1040 }
1041
Souvik Ghosh586968a2016-08-11 17:56:24 -07001042 rc = cros_ec_get_region_info(EC_FLASH_REGION_RW,
1043 &cros_ec_priv->region[EC_IMAGE_RW]);
Simon Glassc453a642013-07-01 18:08:53 +09001044 if (rc) {
1045 msg_perr("%s(): Failed to probe (cannot find RW region): %d\n",
1046 __func__, rc);
1047 return 0;
1048 }
1049
Louis Yung-Chieh Loedb0cba2011-12-09 17:06:54 +08001050 return 1;
1051};