1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
10 #include <fastboot-internal.h>
18 * image_size - final fastboot image size
20 static u32 image_size;
23 * fastboot_bytes_received - number of bytes received in the current download
25 static u32 fastboot_bytes_received;
28 * fastboot_bytes_expected - number of bytes expected in the current download
30 static u32 fastboot_bytes_expected;
32 static void okay(char *, char *);
33 static void getvar(char *, char *);
34 static void download(char *, char *);
35 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
36 static void flash(char *, char *);
37 static void erase(char *, char *);
39 static void reboot_bootloader(char *, char *);
40 static void reboot_fastbootd(char *, char *);
41 static void reboot_recovery(char *, char *);
42 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
43 static void oem_format(char *, char *);
45 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF)
46 static void oem_partconf(char *, char *);
48 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS)
49 static void oem_bootbus(char *, char *);
52 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
53 static void run_ucmd(char *, char *);
54 static void run_acmd(char *, char *);
59 void (*dispatch)(char *cmd_parameter, char *response);
60 } commands[FASTBOOT_COMMAND_COUNT] = {
61 [FASTBOOT_COMMAND_GETVAR] = {
65 [FASTBOOT_COMMAND_DOWNLOAD] = {
66 .command = "download",
69 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
70 [FASTBOOT_COMMAND_FLASH] = {
74 [FASTBOOT_COMMAND_ERASE] = {
79 [FASTBOOT_COMMAND_BOOT] = {
83 [FASTBOOT_COMMAND_CONTINUE] = {
84 .command = "continue",
87 [FASTBOOT_COMMAND_REBOOT] = {
91 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
92 .command = "reboot-bootloader",
93 .dispatch = reboot_bootloader
95 [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = {
96 .command = "reboot-fastboot",
97 .dispatch = reboot_fastbootd
99 [FASTBOOT_COMMAND_REBOOT_RECOVERY] = {
100 .command = "reboot-recovery",
101 .dispatch = reboot_recovery
103 [FASTBOOT_COMMAND_SET_ACTIVE] = {
104 .command = "set_active",
107 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
108 [FASTBOOT_COMMAND_OEM_FORMAT] = {
109 .command = "oem format",
110 .dispatch = oem_format,
113 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF)
114 [FASTBOOT_COMMAND_OEM_PARTCONF] = {
115 .command = "oem partconf",
116 .dispatch = oem_partconf,
119 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS)
120 [FASTBOOT_COMMAND_OEM_BOOTBUS] = {
121 .command = "oem bootbus",
122 .dispatch = oem_bootbus,
125 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
126 [FASTBOOT_COMMAND_UCMD] = {
128 .dispatch = run_ucmd,
130 [FASTBOOT_COMMAND_ACMD] = {
132 .dispatch = run_acmd,
138 * fastboot_handle_command - Handle fastboot command
140 * @cmd_string: Pointer to command string
141 * @response: Pointer to fastboot response buffer
143 * Return: Executed command, or -1 if not recognized
145 int fastboot_handle_command(char *cmd_string, char *response)
150 cmd_parameter = cmd_string;
151 strsep(&cmd_parameter, ":");
153 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
154 if (!strcmp(commands[i].command, cmd_string)) {
155 if (commands[i].dispatch) {
156 commands[i].dispatch(cmd_parameter,
165 pr_err("command %s not recognized.\n", cmd_string);
166 fastboot_fail("unrecognized command", response);
171 * okay() - Send bare OKAY response
173 * @cmd_parameter: Pointer to command parameter
174 * @response: Pointer to fastboot response buffer
176 * Send a bare OKAY fastboot response. This is used where the command is
177 * valid, but all the work is done after the response has been sent (e.g.
180 static void okay(char *cmd_parameter, char *response)
182 fastboot_okay(NULL, response);
186 * getvar() - Read a config/version variable
188 * @cmd_parameter: Pointer to command parameter
189 * @response: Pointer to fastboot response buffer
191 static void getvar(char *cmd_parameter, char *response)
193 fastboot_getvar(cmd_parameter, response);
197 * fastboot_download() - Start a download transfer from the client
199 * @cmd_parameter: Pointer to command parameter
200 * @response: Pointer to fastboot response buffer
202 static void download(char *cmd_parameter, char *response)
206 if (!cmd_parameter) {
207 fastboot_fail("Expected command parameter", response);
210 fastboot_bytes_received = 0;
211 fastboot_bytes_expected = hextoul(cmd_parameter, &tmp);
212 if (fastboot_bytes_expected == 0) {
213 fastboot_fail("Expected nonzero image size", response);
217 * Nothing to download yet. Response is of the form:
218 * [DATA|FAIL]$cmd_parameter
220 * where cmd_parameter is an 8 digit hexadecimal number
222 if (fastboot_bytes_expected > fastboot_buf_size) {
223 fastboot_fail(cmd_parameter, response);
225 printf("Starting download of %d bytes\n",
226 fastboot_bytes_expected);
227 fastboot_response("DATA", response, "%s", cmd_parameter);
232 * fastboot_data_remaining() - return bytes remaining in current transfer
234 * Return: Number of bytes left in the current download
236 u32 fastboot_data_remaining(void)
238 return fastboot_bytes_expected - fastboot_bytes_received;
242 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
244 * @fastboot_data: Pointer to received fastboot data
245 * @fastboot_data_len: Length of received fastboot data
246 * @response: Pointer to fastboot response buffer
248 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
249 * response. fastboot_bytes_received is updated to indicate the number
250 * of bytes that have been transferred.
252 * On completion sets image_size and ${filesize} to the total size of the
255 void fastboot_data_download(const void *fastboot_data,
256 unsigned int fastboot_data_len,
259 #define BYTES_PER_DOT 0x20000
260 u32 pre_dot_num, now_dot_num;
262 if (fastboot_data_len == 0 ||
263 (fastboot_bytes_received + fastboot_data_len) >
264 fastboot_bytes_expected) {
265 fastboot_fail("Received invalid data length",
269 /* Download data to fastboot_buf_addr */
270 memcpy(fastboot_buf_addr + fastboot_bytes_received,
271 fastboot_data, fastboot_data_len);
273 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
274 fastboot_bytes_received += fastboot_data_len;
275 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
277 if (pre_dot_num != now_dot_num) {
279 if (!(now_dot_num % 74))
286 * fastboot_data_complete() - Mark current transfer complete
288 * @response: Pointer to fastboot response buffer
290 * Set image_size and ${filesize} to the total size of the downloaded image.
292 void fastboot_data_complete(char *response)
294 /* Download complete. Respond with "OKAY" */
295 fastboot_okay(NULL, response);
296 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
297 image_size = fastboot_bytes_received;
298 env_set_hex("filesize", image_size);
299 fastboot_bytes_expected = 0;
300 fastboot_bytes_received = 0;
303 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
305 * flash() - write the downloaded image to the indicated partition.
307 * @cmd_parameter: Pointer to partition name
308 * @response: Pointer to fastboot response buffer
310 * Writes the previously downloaded image to the partition indicated by
311 * cmd_parameter. Writes to response.
313 static void flash(char *cmd_parameter, char *response)
315 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
316 fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
319 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
320 fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
326 * erase() - erase the indicated partition.
328 * @cmd_parameter: Pointer to partition name
329 * @response: Pointer to fastboot response buffer
331 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
334 static void erase(char *cmd_parameter, char *response)
336 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
337 fastboot_mmc_erase(cmd_parameter, response);
339 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
340 fastboot_nand_erase(cmd_parameter, response);
345 #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
347 * run_ucmd() - Execute the UCmd command
349 * @cmd_parameter: Pointer to command parameter
350 * @response: Pointer to fastboot response buffer
352 static void run_ucmd(char *cmd_parameter, char *response)
354 if (!cmd_parameter) {
355 pr_err("missing slot suffix\n");
356 fastboot_fail("missing command", response);
360 if (run_command(cmd_parameter, 0))
361 fastboot_fail("", response);
363 fastboot_okay(NULL, response);
366 static char g_a_cmd_buff[64];
368 void fastboot_acmd_complete(void)
370 run_command(g_a_cmd_buff, 0);
374 * run_acmd() - Execute the ACmd command
376 * @cmd_parameter: Pointer to command parameter
377 * @response: Pointer to fastboot response buffer
379 static void run_acmd(char *cmd_parameter, char *response)
381 if (!cmd_parameter) {
382 pr_err("missing slot suffix\n");
383 fastboot_fail("missing command", response);
387 if (strlen(cmd_parameter) > sizeof(g_a_cmd_buff)) {
388 pr_err("too long command\n");
389 fastboot_fail("too long command", response);
393 strcpy(g_a_cmd_buff, cmd_parameter);
394 fastboot_okay(NULL, response);
399 * reboot_bootloader() - Sets reboot bootloader flag.
401 * @cmd_parameter: Pointer to command parameter
402 * @response: Pointer to fastboot response buffer
404 static void reboot_bootloader(char *cmd_parameter, char *response)
406 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER))
407 fastboot_fail("Cannot set reboot flag", response);
409 fastboot_okay(NULL, response);
413 * reboot_fastbootd() - Sets reboot fastboot flag.
415 * @cmd_parameter: Pointer to command parameter
416 * @response: Pointer to fastboot response buffer
418 static void reboot_fastbootd(char *cmd_parameter, char *response)
420 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD))
421 fastboot_fail("Cannot set fastboot flag", response);
423 fastboot_okay(NULL, response);
427 * reboot_recovery() - Sets reboot recovery flag.
429 * @cmd_parameter: Pointer to command parameter
430 * @response: Pointer to fastboot response buffer
432 static void reboot_recovery(char *cmd_parameter, char *response)
434 if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY))
435 fastboot_fail("Cannot set recovery flag", response);
437 fastboot_okay(NULL, response);
440 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
442 * oem_format() - Execute the OEM format command
444 * @cmd_parameter: Pointer to command parameter
445 * @response: Pointer to fastboot response buffer
447 static void oem_format(char *cmd_parameter, char *response)
451 if (!env_get("partitions")) {
452 fastboot_fail("partitions not set", response);
454 sprintf(cmdbuf, "gpt write mmc %x $partitions",
455 CONFIG_FASTBOOT_FLASH_MMC_DEV);
456 if (run_command(cmdbuf, 0))
457 fastboot_fail("", response);
459 fastboot_okay(NULL, response);
464 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF)
466 * oem_partconf() - Execute the OEM partconf command
468 * @cmd_parameter: Pointer to command parameter
469 * @response: Pointer to fastboot response buffer
471 static void oem_partconf(char *cmd_parameter, char *response)
475 if (!cmd_parameter) {
476 fastboot_fail("Expected command parameter", response);
480 /* execute 'mmc partconfg' command with cmd_parameter arguments*/
481 snprintf(cmdbuf, sizeof(cmdbuf), "mmc partconf %x %s 0",
482 CONFIG_FASTBOOT_FLASH_MMC_DEV, cmd_parameter);
483 printf("Execute: %s\n", cmdbuf);
484 if (run_command(cmdbuf, 0))
485 fastboot_fail("Cannot set oem partconf", response);
487 fastboot_okay(NULL, response);
491 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS)
493 * oem_bootbus() - Execute the OEM bootbus command
495 * @cmd_parameter: Pointer to command parameter
496 * @response: Pointer to fastboot response buffer
498 static void oem_bootbus(char *cmd_parameter, char *response)
502 if (!cmd_parameter) {
503 fastboot_fail("Expected command parameter", response);
507 /* execute 'mmc bootbus' command with cmd_parameter arguments*/
508 snprintf(cmdbuf, sizeof(cmdbuf), "mmc bootbus %x %s",
509 CONFIG_FASTBOOT_FLASH_MMC_DEV, cmd_parameter);
510 printf("Execute: %s\n", cmdbuf);
511 if (run_command(cmdbuf, 0))
512 fastboot_fail("Cannot set oem bootbus", response);
514 fastboot_okay(NULL, response);