common: Drop flash.h from common header
[platform/kernel/u-boot.git] / cmd / cros_ec.c
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 <flash.h>
14 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
16
17 /* Note: depends on enum ec_current_image */
18 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
19
20 /**
21  * Decode a flash region parameter
22  *
23  * @param argc Number of params remaining
24  * @param argv List of remaining parameters
25  * @return flash region (EC_FLASH_REGION_...) or -1 on error
26  */
27 static int cros_ec_decode_region(int argc, char * const argv[])
28 {
29         if (argc > 0) {
30                 if (0 == strcmp(*argv, "rw"))
31                         return EC_FLASH_REGION_ACTIVE;
32                 else if (0 == strcmp(*argv, "ro"))
33                         return EC_FLASH_REGION_RO;
34
35                 debug("%s: Invalid region '%s'\n", __func__, *argv);
36         } else {
37                 debug("%s: Missing region parameter\n", __func__);
38         }
39
40         return -1;
41 }
42
43 /**
44  * Perform a flash read or write command
45  *
46  * @param dev           CROS-EC device to read/write
47  * @param is_write      1 do to a write, 0 to do a read
48  * @param argc          Number of arguments
49  * @param argv          Arguments (2 is region, 3 is address)
50  * @return 0 for ok, 1 for a usage error or -ve for ec command error
51  *      (negative EC_RES_...)
52  */
53 static int do_read_write(struct udevice *dev, int is_write, int argc,
54                          char * const argv[])
55 {
56         uint32_t offset, size = -1U, region_size;
57         unsigned long addr;
58         char *endp;
59         int region;
60         int ret;
61
62         region = cros_ec_decode_region(argc - 2, argv + 2);
63         if (region == -1)
64                 return 1;
65         if (argc < 4)
66                 return 1;
67         addr = simple_strtoul(argv[3], &endp, 16);
68         if (*argv[3] == 0 || *endp != 0)
69                 return 1;
70         if (argc > 4) {
71                 size = simple_strtoul(argv[4], &endp, 16);
72                 if (*argv[4] == 0 || *endp != 0)
73                         return 1;
74         }
75
76         ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
77         if (ret) {
78                 debug("%s: Could not read region info\n", __func__);
79                 return ret;
80         }
81         if (size == -1U)
82                 size = region_size;
83
84         ret = is_write ?
85                 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
86                 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
87         if (ret) {
88                 debug("%s: Could not %s region\n", __func__,
89                       is_write ? "write" : "read");
90                 return ret;
91         }
92
93         return 0;
94 }
95
96 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
97 {
98         struct udevice *dev;
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, &dev);
109                 if (!ret)
110                         device_remove(dev, DM_REMOVE_NORMAL);
111                 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
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, &dev);
120         if (ret) {
121                 printf("Cannot get cros-ec device (err=%d)\n", ret);
122                 return 1;
123         }
124         if (0 == strcmp("id", cmd)) {
125                 char id[MSG_BYTES];
126
127                 if (cros_ec_read_id(dev, id, sizeof(id))) {
128                         debug("%s: Could not read KBC ID\n", __func__);
129                         return 1;
130                 }
131                 printf("%s\n", id);
132         } else if (0 == strcmp("info", cmd)) {
133                 struct ec_response_mkbp_info info;
134
135                 if (cros_ec_info(dev, &info)) {
136                         debug("%s: Could not read KBC info\n", __func__);
137                         return 1;
138                 }
139                 printf("rows     = %u\n", info.rows);
140                 printf("cols     = %u\n", info.cols);
141         } else if (0 == strcmp("curimage", cmd)) {
142                 enum ec_current_image image;
143
144                 if (cros_ec_read_current_image(dev, &image)) {
145                         debug("%s: Could not read KBC image\n", __func__);
146                         return 1;
147                 }
148                 printf("%d\n", image);
149         } else if (0 == strcmp("hash", cmd)) {
150                 struct ec_response_vboot_hash hash;
151                 int i;
152
153                 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
154                         debug("%s: Could not read KBC hash\n", __func__);
155                         return 1;
156                 }
157
158                 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
159                         printf("type:    SHA-256\n");
160                 else
161                         printf("type:    %d\n", hash.hash_type);
162
163                 printf("offset:  0x%08x\n", hash.offset);
164                 printf("size:    0x%08x\n", hash.size);
165
166                 printf("digest:  ");
167                 for (i = 0; i < hash.digest_size; i++)
168                         printf("%02x", hash.hash_digest[i]);
169                 printf("\n");
170         } else if (0 == strcmp("reboot", cmd)) {
171                 int region;
172                 enum ec_reboot_cmd cmd;
173
174                 if (argc >= 3 && !strcmp(argv[2], "cold")) {
175                         cmd = EC_REBOOT_COLD;
176                 } else {
177                         region = cros_ec_decode_region(argc - 2, argv + 2);
178                         if (region == EC_FLASH_REGION_RO)
179                                 cmd = EC_REBOOT_JUMP_RO;
180                         else if (region == EC_FLASH_REGION_ACTIVE)
181                                 cmd = EC_REBOOT_JUMP_RW;
182                         else
183                                 return CMD_RET_USAGE;
184                 }
185
186                 if (cros_ec_reboot(dev, cmd, 0)) {
187                         debug("%s: Could not reboot KBC\n", __func__);
188                         return 1;
189                 }
190         } else if (0 == strcmp("events", cmd)) {
191                 uint32_t events;
192
193                 if (cros_ec_get_host_events(dev, &events)) {
194                         debug("%s: Could not read host events\n", __func__);
195                         return 1;
196                 }
197                 printf("0x%08x\n", events);
198         } else if (0 == strcmp("clrevents", cmd)) {
199                 uint32_t events = 0x7fffffff;
200
201                 if (argc >= 3)
202                         events = simple_strtol(argv[2], NULL, 0);
203
204                 if (cros_ec_clear_host_events(dev, events)) {
205                         debug("%s: Could not clear host events\n", __func__);
206                         return 1;
207                 }
208         } else if (0 == strcmp("read", cmd)) {
209                 ret = do_read_write(dev, 0, argc, argv);
210                 if (ret > 0)
211                         return CMD_RET_USAGE;
212         } else if (0 == strcmp("write", cmd)) {
213                 ret = do_read_write(dev, 1, argc, argv);
214                 if (ret > 0)
215                         return CMD_RET_USAGE;
216         } else if (0 == strcmp("erase", cmd)) {
217                 int region = cros_ec_decode_region(argc - 2, argv + 2);
218                 uint32_t offset, size;
219
220                 if (region == -1)
221                         return CMD_RET_USAGE;
222                 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
223                         debug("%s: Could not read region info\n", __func__);
224                         ret = -1;
225                 } else {
226                         ret = cros_ec_flash_erase(dev, offset, size);
227                         if (ret) {
228                                 debug("%s: Could not erase region\n",
229                                       __func__);
230                         }
231                 }
232         } else if (0 == strcmp("regioninfo", cmd)) {
233                 int region = cros_ec_decode_region(argc - 2, argv + 2);
234                 uint32_t offset, size;
235
236                 if (region == -1)
237                         return CMD_RET_USAGE;
238                 ret = cros_ec_flash_offset(dev, region, &offset, &size);
239                 if (ret) {
240                         debug("%s: Could not read region info\n", __func__);
241                 } else {
242                         printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
243                                         "RO" : "RW");
244                         printf("Offset: %x\n", offset);
245                         printf("Size:   %x\n", size);
246                 }
247         } else if (0 == strcmp("flashinfo", cmd)) {
248                 struct ec_response_flash_info p;
249
250                 ret = cros_ec_read_flashinfo(dev, &p);
251                 if (!ret) {
252                         printf("Flash size:         %u\n", p.flash_size);
253                         printf("Write block size:   %u\n", p.write_block_size);
254                         printf("Erase block size:   %u\n", p.erase_block_size);
255                 }
256         } else if (0 == strcmp("vbnvcontext", cmd)) {
257                 uint8_t block[EC_VBNV_BLOCK_SIZE];
258                 char buf[3];
259                 int i, len;
260                 unsigned long result;
261
262                 if (argc <= 2) {
263                         ret = cros_ec_read_nvdata(dev, block,
264                                                   EC_VBNV_BLOCK_SIZE);
265                         if (!ret) {
266                                 printf("vbnv_block: ");
267                                 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
268                                         printf("%02x", block[i]);
269                                 putc('\n');
270                         }
271                 } else {
272                         /*
273                          * TODO(clchiou): Move this to a utility function as
274                          * cmd_spi might want to call it.
275                          */
276                         memset(block, 0, EC_VBNV_BLOCK_SIZE);
277                         len = strlen(argv[2]);
278                         buf[2] = '\0';
279                         for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
280                                 if (i * 2 >= len)
281                                         break;
282                                 buf[0] = argv[2][i * 2];
283                                 if (i * 2 + 1 >= len)
284                                         buf[1] = '0';
285                                 else
286                                         buf[1] = argv[2][i * 2 + 1];
287                                 strict_strtoul(buf, 16, &result);
288                                 block[i] = result;
289                         }
290                         ret = cros_ec_write_nvdata(dev, block,
291                                                    EC_VBNV_BLOCK_SIZE);
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(dev, index, state);
340                 } else {
341                         ret = cros_ec_get_ldo(dev, 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 );