xref: /openbmc/u-boot/cmd/cros_ec.c (revision 63e22517)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Chromium OS cros_ec driver
4  *
5  * Copyright (c) 2016 The Chromium OS Authors.
6  * Copyright (c) 2016 National Instruments Corp
7  */
8 
9 #include <common.h>
10 #include <command.h>
11 #include <cros_ec.h>
12 #include <dm.h>
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
15 
16 /* Note: depends on enum ec_current_image */
17 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
18 
19 /**
20  * Decode a flash region parameter
21  *
22  * @param argc Number of params remaining
23  * @param argv List of remaining parameters
24  * @return flash region (EC_FLASH_REGION_...) or -1 on error
25  */
26 static int cros_ec_decode_region(int argc, char * const argv[])
27 {
28 	if (argc > 0) {
29 		if (0 == strcmp(*argv, "rw"))
30 			return EC_FLASH_REGION_RW;
31 		else if (0 == strcmp(*argv, "ro"))
32 			return EC_FLASH_REGION_RO;
33 
34 		debug("%s: Invalid region '%s'\n", __func__, *argv);
35 	} else {
36 		debug("%s: Missing region parameter\n", __func__);
37 	}
38 
39 	return -1;
40 }
41 
42 /**
43  * Perform a flash read or write command
44  *
45  * @param dev		CROS-EC device to read/write
46  * @param is_write	1 do to a write, 0 to do a read
47  * @param argc		Number of arguments
48  * @param argv		Arguments (2 is region, 3 is address)
49  * @return 0 for ok, 1 for a usage error or -ve for ec command error
50  *	(negative EC_RES_...)
51  */
52 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
53 			 char * const argv[])
54 {
55 	uint32_t offset, size = -1U, region_size;
56 	unsigned long addr;
57 	char *endp;
58 	int region;
59 	int ret;
60 
61 	region = cros_ec_decode_region(argc - 2, argv + 2);
62 	if (region == -1)
63 		return 1;
64 	if (argc < 4)
65 		return 1;
66 	addr = simple_strtoul(argv[3], &endp, 16);
67 	if (*argv[3] == 0 || *endp != 0)
68 		return 1;
69 	if (argc > 4) {
70 		size = simple_strtoul(argv[4], &endp, 16);
71 		if (*argv[4] == 0 || *endp != 0)
72 			return 1;
73 	}
74 
75 	ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
76 	if (ret) {
77 		debug("%s: Could not read region info\n", __func__);
78 		return ret;
79 	}
80 	if (size == -1U)
81 		size = region_size;
82 
83 	ret = is_write ?
84 		cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
85 		cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
86 	if (ret) {
87 		debug("%s: Could not %s region\n", __func__,
88 		      is_write ? "write" : "read");
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
96 {
97 	struct cros_ec_dev *dev;
98 	struct udevice *udev;
99 	const char *cmd;
100 	int ret = 0;
101 
102 	if (argc < 2)
103 		return CMD_RET_USAGE;
104 
105 	cmd = argv[1];
106 	if (0 == strcmp("init", cmd)) {
107 		/* Remove any existing device */
108 		ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
109 		if (!ret)
110 			device_remove(udev, DM_REMOVE_NORMAL);
111 		ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
112 		if (ret) {
113 			printf("Could not init cros_ec device (err %d)\n", ret);
114 			return 1;
115 		}
116 		return 0;
117 	}
118 
119 	ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
120 	if (ret) {
121 		printf("Cannot get cros-ec device (err=%d)\n", ret);
122 		return 1;
123 	}
124 	dev = dev_get_uclass_priv(udev);
125 	if (0 == strcmp("id", cmd)) {
126 		char id[MSG_BYTES];
127 
128 		if (cros_ec_read_id(dev, id, sizeof(id))) {
129 			debug("%s: Could not read KBC ID\n", __func__);
130 			return 1;
131 		}
132 		printf("%s\n", id);
133 	} else if (0 == strcmp("info", cmd)) {
134 		struct ec_response_mkbp_info info;
135 
136 		if (cros_ec_info(dev, &info)) {
137 			debug("%s: Could not read KBC info\n", __func__);
138 			return 1;
139 		}
140 		printf("rows     = %u\n", info.rows);
141 		printf("cols     = %u\n", info.cols);
142 		printf("switches = %#x\n", info.switches);
143 	} else if (0 == strcmp("curimage", cmd)) {
144 		enum ec_current_image image;
145 
146 		if (cros_ec_read_current_image(dev, &image)) {
147 			debug("%s: Could not read KBC image\n", __func__);
148 			return 1;
149 		}
150 		printf("%d\n", image);
151 	} else if (0 == strcmp("hash", cmd)) {
152 		struct ec_response_vboot_hash hash;
153 		int i;
154 
155 		if (cros_ec_read_hash(dev, &hash)) {
156 			debug("%s: Could not read KBC hash\n", __func__);
157 			return 1;
158 		}
159 
160 		if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
161 			printf("type:    SHA-256\n");
162 		else
163 			printf("type:    %d\n", hash.hash_type);
164 
165 		printf("offset:  0x%08x\n", hash.offset);
166 		printf("size:    0x%08x\n", hash.size);
167 
168 		printf("digest:  ");
169 		for (i = 0; i < hash.digest_size; i++)
170 			printf("%02x", hash.hash_digest[i]);
171 		printf("\n");
172 	} else if (0 == strcmp("reboot", cmd)) {
173 		int region;
174 		enum ec_reboot_cmd cmd;
175 
176 		if (argc >= 3 && !strcmp(argv[2], "cold")) {
177 			cmd = EC_REBOOT_COLD;
178 		} else {
179 			region = cros_ec_decode_region(argc - 2, argv + 2);
180 			if (region == EC_FLASH_REGION_RO)
181 				cmd = EC_REBOOT_JUMP_RO;
182 			else if (region == EC_FLASH_REGION_RW)
183 				cmd = EC_REBOOT_JUMP_RW;
184 			else
185 				return CMD_RET_USAGE;
186 		}
187 
188 		if (cros_ec_reboot(dev, cmd, 0)) {
189 			debug("%s: Could not reboot KBC\n", __func__);
190 			return 1;
191 		}
192 	} else if (0 == strcmp("events", cmd)) {
193 		uint32_t events;
194 
195 		if (cros_ec_get_host_events(dev, &events)) {
196 			debug("%s: Could not read host events\n", __func__);
197 			return 1;
198 		}
199 		printf("0x%08x\n", events);
200 	} else if (0 == strcmp("clrevents", cmd)) {
201 		uint32_t events = 0x7fffffff;
202 
203 		if (argc >= 3)
204 			events = simple_strtol(argv[2], NULL, 0);
205 
206 		if (cros_ec_clear_host_events(dev, events)) {
207 			debug("%s: Could not clear host events\n", __func__);
208 			return 1;
209 		}
210 	} else if (0 == strcmp("read", cmd)) {
211 		ret = do_read_write(dev, 0, argc, argv);
212 		if (ret > 0)
213 			return CMD_RET_USAGE;
214 	} else if (0 == strcmp("write", cmd)) {
215 		ret = do_read_write(dev, 1, argc, argv);
216 		if (ret > 0)
217 			return CMD_RET_USAGE;
218 	} else if (0 == strcmp("erase", cmd)) {
219 		int region = cros_ec_decode_region(argc - 2, argv + 2);
220 		uint32_t offset, size;
221 
222 		if (region == -1)
223 			return CMD_RET_USAGE;
224 		if (cros_ec_flash_offset(dev, region, &offset, &size)) {
225 			debug("%s: Could not read region info\n", __func__);
226 			ret = -1;
227 		} else {
228 			ret = cros_ec_flash_erase(dev, offset, size);
229 			if (ret) {
230 				debug("%s: Could not erase region\n",
231 				      __func__);
232 			}
233 		}
234 	} else if (0 == strcmp("regioninfo", cmd)) {
235 		int region = cros_ec_decode_region(argc - 2, argv + 2);
236 		uint32_t offset, size;
237 
238 		if (region == -1)
239 			return CMD_RET_USAGE;
240 		ret = cros_ec_flash_offset(dev, region, &offset, &size);
241 		if (ret) {
242 			debug("%s: Could not read region info\n", __func__);
243 		} else {
244 			printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
245 					"RO" : "RW");
246 			printf("Offset: %x\n", offset);
247 			printf("Size:   %x\n", size);
248 		}
249 	} else if (0 == strcmp("flashinfo", cmd)) {
250 		struct ec_response_flash_info p;
251 
252 		ret = cros_ec_read_flashinfo(dev, &p);
253 		if (!ret) {
254 			printf("Flash size:         %u\n", p.flash_size);
255 			printf("Write block size:   %u\n", p.write_block_size);
256 			printf("Erase block size:   %u\n", p.erase_block_size);
257 		}
258 	} else if (0 == strcmp("vbnvcontext", cmd)) {
259 		uint8_t block[EC_VBNV_BLOCK_SIZE];
260 		char buf[3];
261 		int i, len;
262 		unsigned long result;
263 
264 		if (argc <= 2) {
265 			ret = cros_ec_read_vbnvcontext(dev, block);
266 			if (!ret) {
267 				printf("vbnv_block: ");
268 				for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
269 					printf("%02x", block[i]);
270 				putc('\n');
271 			}
272 		} else {
273 			/*
274 			 * TODO(clchiou): Move this to a utility function as
275 			 * cmd_spi might want to call it.
276 			 */
277 			memset(block, 0, EC_VBNV_BLOCK_SIZE);
278 			len = strlen(argv[2]);
279 			buf[2] = '\0';
280 			for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
281 				if (i * 2 >= len)
282 					break;
283 				buf[0] = argv[2][i * 2];
284 				if (i * 2 + 1 >= len)
285 					buf[1] = '0';
286 				else
287 					buf[1] = argv[2][i * 2 + 1];
288 				strict_strtoul(buf, 16, &result);
289 				block[i] = result;
290 			}
291 			ret = cros_ec_write_vbnvcontext(dev, block);
292 		}
293 		if (ret) {
294 			debug("%s: Could not %s VbNvContext\n", __func__,
295 			      argc <= 2 ?  "read" : "write");
296 		}
297 	} else if (0 == strcmp("test", cmd)) {
298 		int result = cros_ec_test(dev);
299 
300 		if (result)
301 			printf("Test failed with error %d\n", result);
302 		else
303 			puts("Test passed\n");
304 	} else if (0 == strcmp("version", cmd)) {
305 		struct ec_response_get_version *p;
306 		char *build_string;
307 
308 		ret = cros_ec_read_version(dev, &p);
309 		if (!ret) {
310 			/* Print versions */
311 			printf("RO version:    %1.*s\n",
312 			       (int)sizeof(p->version_string_ro),
313 			       p->version_string_ro);
314 			printf("RW version:    %1.*s\n",
315 			       (int)sizeof(p->version_string_rw),
316 			       p->version_string_rw);
317 			printf("Firmware copy: %s\n",
318 			       (p->current_image <
319 			       ARRAY_SIZE(ec_current_image_name) ?
320 			       ec_current_image_name[p->current_image] :
321 			       "?"));
322 			ret = cros_ec_read_build_info(dev, &build_string);
323 			if (!ret)
324 				printf("Build info:    %s\n", build_string);
325 		}
326 	} else if (0 == strcmp("ldo", cmd)) {
327 		uint8_t index, state;
328 		char *endp;
329 
330 		if (argc < 3)
331 			return CMD_RET_USAGE;
332 		index = simple_strtoul(argv[2], &endp, 10);
333 		if (*argv[2] == 0 || *endp != 0)
334 			return CMD_RET_USAGE;
335 		if (argc > 3) {
336 			state = simple_strtoul(argv[3], &endp, 10);
337 			if (*argv[3] == 0 || *endp != 0)
338 				return CMD_RET_USAGE;
339 			ret = cros_ec_set_ldo(udev, index, state);
340 		} else {
341 			ret = cros_ec_get_ldo(udev, index, &state);
342 			if (!ret) {
343 				printf("LDO%d: %s\n", index,
344 				       state == EC_LDO_STATE_ON ?
345 				       "on" : "off");
346 			}
347 		}
348 
349 		if (ret) {
350 			debug("%s: Could not access LDO%d\n", __func__, index);
351 			return ret;
352 		}
353 	} else {
354 		return CMD_RET_USAGE;
355 	}
356 
357 	if (ret < 0) {
358 		printf("Error: CROS-EC command failed (error %d)\n", ret);
359 		ret = 1;
360 	}
361 
362 	return ret;
363 }
364 
365 U_BOOT_CMD(
366 	crosec,	6,	1,	do_cros_ec,
367 	"CROS-EC utility command",
368 	"init                Re-init CROS-EC (done on startup automatically)\n"
369 	"crosec id                  Read CROS-EC ID\n"
370 	"crosec info                Read CROS-EC info\n"
371 	"crosec curimage            Read CROS-EC current image\n"
372 	"crosec hash                Read CROS-EC hash\n"
373 	"crosec reboot [rw | ro | cold]  Reboot CROS-EC\n"
374 	"crosec events              Read CROS-EC host events\n"
375 	"crosec clrevents [mask]    Clear CROS-EC host events\n"
376 	"crosec regioninfo <ro|rw>  Read image info\n"
377 	"crosec flashinfo           Read flash info\n"
378 	"crosec erase <ro|rw>       Erase EC image\n"
379 	"crosec read <ro|rw> <addr> [<size>]   Read EC image\n"
380 	"crosec write <ro|rw> <addr> [<size>]  Write EC image\n"
381 	"crosec vbnvcontext [hexstring]        Read [write] VbNvContext from EC\n"
382 	"crosec ldo <idx> [<state>] Switch/Read LDO state\n"
383 	"crosec test                run tests on cros_ec\n"
384 	"crosec version             Read CROS-EC version"
385 );
386