1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2009
4 * Windriver, <www.windriver.com>
5 * Tom Rix <Tom.Rix@windriver.com>
7 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * Copyright 2014 Linaro, Ltd.
10 * Rob Herring <robh@kernel.org>
18 #include <net/fastboot.h>
21 * fastboot_buf_addr - base address of the fastboot download buffer
23 void *fastboot_buf_addr;
26 * fastboot_buf_size - size of the fastboot download buffer
28 u32 fastboot_buf_size;
31 * fastboot_progress_callback - callback executed during long operations
33 void (*fastboot_progress_callback)(const char *msg);
36 * fastboot_response() - Writes a response of the form "$tag$reason".
38 * @tag: The first part of the response
39 * @response: Pointer to fastboot response buffer
40 * @format: printf style format string
42 void fastboot_response(const char *tag, char *response,
43 const char *format, ...)
47 strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
49 va_start(args, format);
50 vsnprintf(response + strlen(response),
51 FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
58 * fastboot_fail() - Write a FAIL response of the form "FAIL$reason".
60 * @reason: Pointer to returned reason string
61 * @response: Pointer to fastboot response buffer
63 void fastboot_fail(const char *reason, char *response)
65 fastboot_response("FAIL", response, "%s", reason);
69 * fastboot_okay() - Write an OKAY response of the form "OKAY$reason".
71 * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY"
72 * @response: Pointer to fastboot response buffer
74 void fastboot_okay(const char *reason, char *response)
77 fastboot_response("OKAY", response, "%s", reason);
79 fastboot_response("OKAY", response, NULL);
83 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
85 * Set flag which indicates that we should reboot into the bootloader
86 * following the reboot that fastboot executes after this function.
88 * This function should be overridden in your board file with one
89 * which sets whatever flag your board specific Android bootloader flow
90 * requires in order to re-enter the bootloader.
92 int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
94 static const char * const boot_cmds[] = {
95 [FASTBOOT_REBOOT_REASON_BOOTLOADER] = "bootonce-bootloader",
96 [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot",
97 [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery"
99 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
100 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
102 if (!IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
105 if (reason >= FASTBOOT_REBOOT_REASONS_COUNT)
108 return bcb_write_reboot_reason(mmc_dev, "misc", boot_cmds[reason]);
112 * fastboot_get_progress_callback() - Return progress callback
114 * Return: Pointer to function called during long operations
116 void (*fastboot_get_progress_callback(void))(const char *)
118 return fastboot_progress_callback;
122 * fastboot_boot() - Execute fastboot boot command
124 * If ${fastboot_bootcmd} is set, run that command to execute the boot
125 * process, if that returns, then exit the fastboot server and return
126 * control to the caller.
128 * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset
131 void fastboot_boot(void)
135 s = env_get("fastboot_bootcmd");
137 run_command(s, CMD_FLAG_ENV);
139 static char boot_addr_start[20];
140 static char *const bootm_args[] = {
141 "bootm", boot_addr_start, NULL
144 snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
145 "0x%p", fastboot_buf_addr);
146 printf("Booting kernel at %s...\n\n\n", boot_addr_start);
148 do_bootm(NULL, 0, 2, bootm_args);
151 * This only happens if image is somehow faulty so we start
152 * over. We deliberately leave this policy to the invocation
153 * of fastbootcmd if that's what's being run
155 do_reset(NULL, 0, 0, NULL);
160 * fastboot_set_progress_callback() - set progress callback
162 * @progress: Pointer to progress callback
164 * Set a callback which is invoked periodically during long running operations
165 * (flash and erase). This can be used (for example) by the UDP transport to
166 * send INFO responses to keep the client alive whilst those commands are
169 void fastboot_set_progress_callback(void (*progress)(const char *msg))
171 fastboot_progress_callback = progress;
175 * fastboot_init() - initialise new fastboot protocol session
177 * @buf_addr: Pointer to download buffer, or NULL for default
178 * @buf_size: Size of download buffer, or zero for default
180 void fastboot_init(void *buf_addr, u32 buf_size)
182 fastboot_buf_addr = buf_addr ? buf_addr :
183 (void *)CONFIG_FASTBOOT_BUF_ADDR;
184 fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE;
185 fastboot_set_progress_callback(NULL);