blob: 6f621df074aee4047536cca088f8c9c2424c2feb [file] [log] [blame]
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (C) 2016 The Android Open Source Project
4 */
5
6#include <common.h>
Simon Glass288b29e2019-11-14 12:57:43 -07007#include <command.h>
Ion Agorria85fcd692024-01-05 09:22:06 +02008#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -06009#include <env.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000010#include <fastboot.h>
11#include <fastboot-internal.h>
12#include <fb_mmc.h>
13#include <fb_nand.h>
14#include <part.h>
15#include <stdlib.h>
Simon Glass1e94b462023-09-14 18:21:46 -060016#include <linux/printk.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000017
18/**
19 * image_size - final fastboot image size
20 */
21static u32 image_size;
22
23/**
24 * fastboot_bytes_received - number of bytes received in the current download
25 */
26static u32 fastboot_bytes_received;
27
28/**
29 * fastboot_bytes_expected - number of bytes expected in the current download
30 */
31static u32 fastboot_bytes_expected;
32
33static void okay(char *, char *);
34static void getvar(char *, char *);
35static void download(char *, char *);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000036static void flash(char *, char *);
37static void erase(char *, char *);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000038static void reboot_bootloader(char *, char *);
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +030039static void reboot_fastbootd(char *, char *);
40static void reboot_recovery(char *, char *);
Alex Kiernan3845b902018-05-29 15:30:54 +000041static void oem_format(char *, char *);
Patrick Delaunayb2f6b972021-01-27 14:46:48 +010042static void oem_partconf(char *, char *);
Patrick Delaunay0c0394b2021-01-27 14:46:49 +010043static void oem_bootbus(char *, char *);
Heiko Schocherbc820d52021-02-10 09:29:03 +010044static void run_ucmd(char *, char *);
45static void run_acmd(char *, char *);
Heiko Schocherbc820d52021-02-10 09:29:03 +010046
Alex Kiernanf73a7df2018-05-29 15:30:53 +000047static const struct {
48 const char *command;
49 void (*dispatch)(char *cmd_parameter, char *response);
50} commands[FASTBOOT_COMMAND_COUNT] = {
51 [FASTBOOT_COMMAND_GETVAR] = {
52 .command = "getvar",
53 .dispatch = getvar
54 },
55 [FASTBOOT_COMMAND_DOWNLOAD] = {
56 .command = "download",
57 .dispatch = download
58 },
Alex Kiernanf73a7df2018-05-29 15:30:53 +000059 [FASTBOOT_COMMAND_FLASH] = {
60 .command = "flash",
Patrick Delaunayd0379902022-12-15 10:15:50 +010061 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_FLASH, (flash), (NULL))
Alex Kiernanf73a7df2018-05-29 15:30:53 +000062 },
63 [FASTBOOT_COMMAND_ERASE] = {
64 .command = "erase",
Patrick Delaunayd0379902022-12-15 10:15:50 +010065 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_FLASH, (erase), (NULL))
Alex Kiernanf73a7df2018-05-29 15:30:53 +000066 },
Alex Kiernanf73a7df2018-05-29 15:30:53 +000067 [FASTBOOT_COMMAND_BOOT] = {
68 .command = "boot",
69 .dispatch = okay
70 },
71 [FASTBOOT_COMMAND_CONTINUE] = {
72 .command = "continue",
73 .dispatch = okay
74 },
75 [FASTBOOT_COMMAND_REBOOT] = {
76 .command = "reboot",
77 .dispatch = okay
78 },
79 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
80 .command = "reboot-bootloader",
81 .dispatch = reboot_bootloader
82 },
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +030083 [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = {
84 .command = "reboot-fastboot",
85 .dispatch = reboot_fastbootd
86 },
87 [FASTBOOT_COMMAND_REBOOT_RECOVERY] = {
88 .command = "reboot-recovery",
89 .dispatch = reboot_recovery
90 },
Alex Kiernanf73a7df2018-05-29 15:30:53 +000091 [FASTBOOT_COMMAND_SET_ACTIVE] = {
92 .command = "set_active",
93 .dispatch = okay
94 },
Alex Kiernan3845b902018-05-29 15:30:54 +000095 [FASTBOOT_COMMAND_OEM_FORMAT] = {
96 .command = "oem format",
Patrick Delaunayd0379902022-12-15 10:15:50 +010097 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT, (oem_format), (NULL))
Alex Kiernan3845b902018-05-29 15:30:54 +000098 },
Patrick Delaunayb2f6b972021-01-27 14:46:48 +010099 [FASTBOOT_COMMAND_OEM_PARTCONF] = {
100 .command = "oem partconf",
Patrick Delaunayd0379902022-12-15 10:15:50 +0100101 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF, (oem_partconf), (NULL))
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100102 },
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100103 [FASTBOOT_COMMAND_OEM_BOOTBUS] = {
104 .command = "oem bootbus",
Patrick Delaunayd0379902022-12-15 10:15:50 +0100105 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS, (oem_bootbus), (NULL))
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100106 },
Sean Andersonf3d914c2022-12-16 13:20:16 -0500107 [FASTBOOT_COMMAND_OEM_RUN] = {
108 .command = "oem run",
109 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_OEM_RUN, (run_ucmd), (NULL))
110 },
Heiko Schocherbc820d52021-02-10 09:29:03 +0100111 [FASTBOOT_COMMAND_UCMD] = {
112 .command = "UCmd",
Patrick Delaunayd0379902022-12-15 10:15:50 +0100113 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT, (run_ucmd), (NULL))
Heiko Schocherbc820d52021-02-10 09:29:03 +0100114 },
115 [FASTBOOT_COMMAND_ACMD] = {
116 .command = "ACmd",
Patrick Delaunayd0379902022-12-15 10:15:50 +0100117 .dispatch = CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT, (run_acmd), (NULL))
Heiko Schocherbc820d52021-02-10 09:29:03 +0100118 },
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000119};
120
121/**
122 * fastboot_handle_command - Handle fastboot command
123 *
124 * @cmd_string: Pointer to command string
125 * @response: Pointer to fastboot response buffer
126 *
127 * Return: Executed command, or -1 if not recognized
128 */
129int fastboot_handle_command(char *cmd_string, char *response)
130{
131 int i;
132 char *cmd_parameter;
133
134 cmd_parameter = cmd_string;
135 strsep(&cmd_parameter, ":");
136
137 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
138 if (!strcmp(commands[i].command, cmd_string)) {
139 if (commands[i].dispatch) {
140 commands[i].dispatch(cmd_parameter,
141 response);
142 return i;
143 } else {
Patrick Delaunayd0379902022-12-15 10:15:50 +0100144 pr_err("command %s not supported.\n", cmd_string);
145 fastboot_fail("Unsupported command", response);
146 return -1;
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000147 }
148 }
149 }
150
151 pr_err("command %s not recognized.\n", cmd_string);
152 fastboot_fail("unrecognized command", response);
153 return -1;
154}
155
Ion Agorria85fcd692024-01-05 09:22:06 +0200156void fastboot_multiresponse(int cmd, char *response)
157{
158 switch (cmd) {
Ion Agorria475aa9a2024-01-05 09:22:07 +0200159 case FASTBOOT_COMMAND_GETVAR:
160 fastboot_getvar_all(response);
161 break;
Ion Agorria85fcd692024-01-05 09:22:06 +0200162 default:
163 fastboot_fail("Unknown multiresponse command", response);
164 break;
165 }
166}
167
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000168/**
169 * okay() - Send bare OKAY response
170 *
171 * @cmd_parameter: Pointer to command parameter
172 * @response: Pointer to fastboot response buffer
173 *
174 * Send a bare OKAY fastboot response. This is used where the command is
175 * valid, but all the work is done after the response has been sent (e.g.
176 * boot, reboot etc.)
177 */
178static void okay(char *cmd_parameter, char *response)
179{
180 fastboot_okay(NULL, response);
181}
182
183/**
184 * getvar() - Read a config/version variable
185 *
186 * @cmd_parameter: Pointer to command parameter
187 * @response: Pointer to fastboot response buffer
188 */
189static void getvar(char *cmd_parameter, char *response)
190{
191 fastboot_getvar(cmd_parameter, response);
192}
193
194/**
195 * fastboot_download() - Start a download transfer from the client
196 *
197 * @cmd_parameter: Pointer to command parameter
198 * @response: Pointer to fastboot response buffer
199 */
200static void download(char *cmd_parameter, char *response)
201{
202 char *tmp;
203
204 if (!cmd_parameter) {
205 fastboot_fail("Expected command parameter", response);
206 return;
207 }
208 fastboot_bytes_received = 0;
Simon Glass7e5f4602021-07-24 09:03:29 -0600209 fastboot_bytes_expected = hextoul(cmd_parameter, &tmp);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000210 if (fastboot_bytes_expected == 0) {
211 fastboot_fail("Expected nonzero image size", response);
212 return;
213 }
214 /*
215 * Nothing to download yet. Response is of the form:
216 * [DATA|FAIL]$cmd_parameter
217 *
218 * where cmd_parameter is an 8 digit hexadecimal number
219 */
220 if (fastboot_bytes_expected > fastboot_buf_size) {
221 fastboot_fail(cmd_parameter, response);
222 } else {
223 printf("Starting download of %d bytes\n",
224 fastboot_bytes_expected);
225 fastboot_response("DATA", response, "%s", cmd_parameter);
226 }
227}
228
229/**
230 * fastboot_data_remaining() - return bytes remaining in current transfer
231 *
232 * Return: Number of bytes left in the current download
233 */
234u32 fastboot_data_remaining(void)
235{
236 return fastboot_bytes_expected - fastboot_bytes_received;
237}
238
239/**
240 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
241 *
242 * @fastboot_data: Pointer to received fastboot data
243 * @fastboot_data_len: Length of received fastboot data
244 * @response: Pointer to fastboot response buffer
245 *
246 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
247 * response. fastboot_bytes_received is updated to indicate the number
248 * of bytes that have been transferred.
249 *
250 * On completion sets image_size and ${filesize} to the total size of the
251 * downloaded image.
252 */
253void fastboot_data_download(const void *fastboot_data,
254 unsigned int fastboot_data_len,
255 char *response)
256{
257#define BYTES_PER_DOT 0x20000
258 u32 pre_dot_num, now_dot_num;
259
260 if (fastboot_data_len == 0 ||
261 (fastboot_bytes_received + fastboot_data_len) >
262 fastboot_bytes_expected) {
263 fastboot_fail("Received invalid data length",
264 response);
265 return;
266 }
267 /* Download data to fastboot_buf_addr */
268 memcpy(fastboot_buf_addr + fastboot_bytes_received,
269 fastboot_data, fastboot_data_len);
270
271 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
272 fastboot_bytes_received += fastboot_data_len;
273 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
274
275 if (pre_dot_num != now_dot_num) {
276 putc('.');
277 if (!(now_dot_num % 74))
278 putc('\n');
279 }
280 *response = '\0';
281}
282
283/**
284 * fastboot_data_complete() - Mark current transfer complete
285 *
286 * @response: Pointer to fastboot response buffer
287 *
288 * Set image_size and ${filesize} to the total size of the downloaded image.
289 */
290void fastboot_data_complete(char *response)
291{
292 /* Download complete. Respond with "OKAY" */
293 fastboot_okay(NULL, response);
294 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
295 image_size = fastboot_bytes_received;
296 env_set_hex("filesize", image_size);
297 fastboot_bytes_expected = 0;
298 fastboot_bytes_received = 0;
299}
300
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000301/**
302 * flash() - write the downloaded image to the indicated partition.
303 *
304 * @cmd_parameter: Pointer to partition name
305 * @response: Pointer to fastboot response buffer
306 *
307 * Writes the previously downloaded image to the partition indicated by
308 * cmd_parameter. Writes to response.
309 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100310static void __maybe_unused flash(char *cmd_parameter, char *response)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000311{
Simon Glass7cb10e52023-02-05 17:54:12 -0700312 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100313 fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr,
314 image_size, response);
315
Simon Glassc6228ed2023-02-05 17:54:13 -0700316 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100317 fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr,
318 image_size, response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000319}
320
321/**
322 * erase() - erase the indicated partition.
323 *
324 * @cmd_parameter: Pointer to partition name
325 * @response: Pointer to fastboot response buffer
326 *
327 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
328 * to response.
329 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100330static void __maybe_unused erase(char *cmd_parameter, char *response)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000331{
Simon Glass7cb10e52023-02-05 17:54:12 -0700332 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100333 fastboot_mmc_erase(cmd_parameter, response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000334
Simon Glassc6228ed2023-02-05 17:54:13 -0700335 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100336 fastboot_nand_erase(cmd_parameter, response);
337}
338
Heiko Schocherbc820d52021-02-10 09:29:03 +0100339/**
340 * run_ucmd() - Execute the UCmd command
341 *
342 * @cmd_parameter: Pointer to command parameter
343 * @response: Pointer to fastboot response buffer
344 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100345static void __maybe_unused run_ucmd(char *cmd_parameter, char *response)
Heiko Schocherbc820d52021-02-10 09:29:03 +0100346{
347 if (!cmd_parameter) {
348 pr_err("missing slot suffix\n");
349 fastboot_fail("missing command", response);
350 return;
351 }
352
353 if (run_command(cmd_parameter, 0))
354 fastboot_fail("", response);
355 else
356 fastboot_okay(NULL, response);
357}
358
359static char g_a_cmd_buff[64];
360
361void fastboot_acmd_complete(void)
362{
363 run_command(g_a_cmd_buff, 0);
364}
365
366/**
367 * run_acmd() - Execute the ACmd command
368 *
369 * @cmd_parameter: Pointer to command parameter
370 * @response: Pointer to fastboot response buffer
371 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100372static void __maybe_unused run_acmd(char *cmd_parameter, char *response)
Heiko Schocherbc820d52021-02-10 09:29:03 +0100373{
374 if (!cmd_parameter) {
375 pr_err("missing slot suffix\n");
376 fastboot_fail("missing command", response);
377 return;
378 }
379
380 if (strlen(cmd_parameter) > sizeof(g_a_cmd_buff)) {
381 pr_err("too long command\n");
382 fastboot_fail("too long command", response);
383 return;
384 }
385
386 strcpy(g_a_cmd_buff, cmd_parameter);
387 fastboot_okay(NULL, response);
388}
Heiko Schocherbc820d52021-02-10 09:29:03 +0100389
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000390/**
391 * reboot_bootloader() - Sets reboot bootloader flag.
392 *
393 * @cmd_parameter: Pointer to command parameter
394 * @response: Pointer to fastboot response buffer
395 */
396static void reboot_bootloader(char *cmd_parameter, char *response)
397{
Roman Kovalivskyi851737a2020-07-28 23:35:32 +0300398 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER))
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000399 fastboot_fail("Cannot set reboot flag", response);
400 else
401 fastboot_okay(NULL, response);
402}
Alex Kiernan3845b902018-05-29 15:30:54 +0000403
Roman Kovalivskyi2b2a7712020-07-28 23:35:33 +0300404/**
405 * reboot_fastbootd() - Sets reboot fastboot flag.
406 *
407 * @cmd_parameter: Pointer to command parameter
408 * @response: Pointer to fastboot response buffer
409 */
410static void reboot_fastbootd(char *cmd_parameter, char *response)
411{
412 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD))
413 fastboot_fail("Cannot set fastboot flag", response);
414 else
415 fastboot_okay(NULL, response);
416}
417
418/**
419 * reboot_recovery() - Sets reboot recovery flag.
420 *
421 * @cmd_parameter: Pointer to command parameter
422 * @response: Pointer to fastboot response buffer
423 */
424static void reboot_recovery(char *cmd_parameter, char *response)
425{
426 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY))
427 fastboot_fail("Cannot set recovery flag", response);
428 else
429 fastboot_okay(NULL, response);
430}
431
Alex Kiernan3845b902018-05-29 15:30:54 +0000432/**
433 * oem_format() - Execute the OEM format command
434 *
435 * @cmd_parameter: Pointer to command parameter
436 * @response: Pointer to fastboot response buffer
437 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100438static void __maybe_unused oem_format(char *cmd_parameter, char *response)
Alex Kiernan3845b902018-05-29 15:30:54 +0000439{
440 char cmdbuf[32];
Patrick Delaunayd0379902022-12-15 10:15:50 +0100441 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
442 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
Alex Kiernan3845b902018-05-29 15:30:54 +0000443
444 if (!env_get("partitions")) {
445 fastboot_fail("partitions not set", response);
446 } else {
Patrick Delaunayd0379902022-12-15 10:15:50 +0100447 sprintf(cmdbuf, "gpt write mmc %x $partitions", mmc_dev);
Alex Kiernan3845b902018-05-29 15:30:54 +0000448 if (run_command(cmdbuf, 0))
449 fastboot_fail("", response);
450 else
451 fastboot_okay(NULL, response);
452 }
453}
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100454
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100455/**
456 * oem_partconf() - Execute the OEM partconf command
457 *
458 * @cmd_parameter: Pointer to command parameter
459 * @response: Pointer to fastboot response buffer
460 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100461static void __maybe_unused oem_partconf(char *cmd_parameter, char *response)
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100462{
463 char cmdbuf[32];
Patrick Delaunayd0379902022-12-15 10:15:50 +0100464 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
465 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100466
467 if (!cmd_parameter) {
468 fastboot_fail("Expected command parameter", response);
469 return;
470 }
471
472 /* execute 'mmc partconfg' command with cmd_parameter arguments*/
Patrick Delaunayd0379902022-12-15 10:15:50 +0100473 snprintf(cmdbuf, sizeof(cmdbuf), "mmc partconf %x %s 0", mmc_dev, cmd_parameter);
Patrick Delaunayb2f6b972021-01-27 14:46:48 +0100474 printf("Execute: %s\n", cmdbuf);
475 if (run_command(cmdbuf, 0))
476 fastboot_fail("Cannot set oem partconf", response);
477 else
478 fastboot_okay(NULL, response);
479}
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100480
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100481/**
482 * oem_bootbus() - Execute the OEM bootbus command
483 *
484 * @cmd_parameter: Pointer to command parameter
485 * @response: Pointer to fastboot response buffer
486 */
Patrick Delaunayd0379902022-12-15 10:15:50 +0100487static void __maybe_unused oem_bootbus(char *cmd_parameter, char *response)
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100488{
489 char cmdbuf[32];
Patrick Delaunayd0379902022-12-15 10:15:50 +0100490 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
491 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100492
493 if (!cmd_parameter) {
494 fastboot_fail("Expected command parameter", response);
495 return;
496 }
497
498 /* execute 'mmc bootbus' command with cmd_parameter arguments*/
Patrick Delaunayd0379902022-12-15 10:15:50 +0100499 snprintf(cmdbuf, sizeof(cmdbuf), "mmc bootbus %x %s", mmc_dev, cmd_parameter);
Patrick Delaunay0c0394b2021-01-27 14:46:49 +0100500 printf("Execute: %s\n", cmdbuf);
501 if (run_command(cmdbuf, 0))
502 fastboot_fail("Cannot set oem bootbus", response);
503 else
504 fastboot_okay(NULL, response);
505}