blob: 4e9d9b719c6f89d3c844ec6d906ce0b09f1d1bdc [file] [log] [blame]
Alex Kiernand2df2ab2018-05-29 15:30:41 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2008 - 2009
4 * Windriver, <www.windriver.com>
5 * Tom Rix <Tom.Rix@windriver.com>
6 *
7 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Copyright 2014 Linaro, Ltd.
10 * Rob Herring <robh@kernel.org>
11 */
12
Roman Kovalivskyia362ce22021-01-26 22:54:56 +020013#include <bcb.h>
Alex Kiernand2df2ab2018-05-29 15:30:41 +000014#include <common.h>
Simon Glass288b29e2019-11-14 12:57:43 -070015#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060016#include <env.h>
Alex Kiernand2df2ab2018-05-29 15:30:41 +000017#include <fastboot.h>
Dmitrii Merkurevc8acbbb2023-04-12 19:49:31 +010018#include <net.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000019
20/**
21 * fastboot_buf_addr - base address of the fastboot download buffer
22 */
23void *fastboot_buf_addr;
24
25/**
26 * fastboot_buf_size - size of the fastboot download buffer
27 */
28u32 fastboot_buf_size;
29
30/**
31 * fastboot_progress_callback - callback executed during long operations
32 */
33void (*fastboot_progress_callback)(const char *msg);
Alex Kiernand2df2ab2018-05-29 15:30:41 +000034
35/**
36 * fastboot_response() - Writes a response of the form "$tag$reason".
37 *
38 * @tag: The first part of the response
39 * @response: Pointer to fastboot response buffer
40 * @format: printf style format string
41 */
42void fastboot_response(const char *tag, char *response,
43 const char *format, ...)
44{
45 va_list args;
46
47 strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
48 if (format) {
49 va_start(args, format);
50 vsnprintf(response + strlen(response),
51 FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
52 format, args);
53 va_end(args);
54 }
55}
56
57/**
58 * fastboot_fail() - Write a FAIL response of the form "FAIL$reason".
59 *
60 * @reason: Pointer to returned reason string
61 * @response: Pointer to fastboot response buffer
62 */
63void fastboot_fail(const char *reason, char *response)
64{
65 fastboot_response("FAIL", response, "%s", reason);
66}
67
68/**
69 * fastboot_okay() - Write an OKAY response of the form "OKAY$reason".
70 *
71 * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY"
72 * @response: Pointer to fastboot response buffer
73 */
74void fastboot_okay(const char *reason, char *response)
75{
76 if (reason)
77 fastboot_response("OKAY", response, "%s", reason);
78 else
79 fastboot_response("OKAY", response, NULL);
80}
Alex Kiernan8a65bd62018-05-29 15:30:46 +000081
82/**
83 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
84 *
85 * Set flag which indicates that we should reboot into the bootloader
86 * following the reboot that fastboot executes after this function.
87 *
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.
91 */
Roman Kovalivskyi851737a2020-07-28 23:35:32 +030092int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
Alex Kiernan8a65bd62018-05-29 15:30:46 +000093{
Roman Kovalivskyia362ce22021-01-26 22:54:56 +020094 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"
98 };
Patrick Delaunayd0379902022-12-15 10:15:50 +010099 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
100 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
101
Simon Glass7cb10e52023-02-05 17:54:12 -0700102 if (!IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100103 return -EINVAL;
Roman Kovalivskyia362ce22021-01-26 22:54:56 +0200104
105 if (reason >= FASTBOOT_REBOOT_REASONS_COUNT)
106 return -EINVAL;
107
Patrick Delaunayd0379902022-12-15 10:15:50 +0100108 return bcb_write_reboot_reason(mmc_dev, "misc", boot_cmds[reason]);
Alex Kiernan8a65bd62018-05-29 15:30:46 +0000109}
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000110
111/**
112 * fastboot_get_progress_callback() - Return progress callback
113 *
114 * Return: Pointer to function called during long operations
115 */
116void (*fastboot_get_progress_callback(void))(const char *)
117{
118 return fastboot_progress_callback;
119}
120
121/**
122 * fastboot_boot() - Execute fastboot boot command
123 *
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.
127 *
128 * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset
129 * the board.
130 */
131void fastboot_boot(void)
132{
133 char *s;
134
135 s = env_get("fastboot_bootcmd");
136 if (s) {
137 run_command(s, CMD_FLAG_ENV);
Samuel Holland9e3eb4a2023-02-20 00:14:58 -0600138 } else if (IS_ENABLED(CONFIG_CMD_BOOTM)) {
Neil Armstrongf402d262019-02-20 11:36:12 +0100139 static char boot_addr_start[20];
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000140 static char *const bootm_args[] = {
141 "bootm", boot_addr_start, NULL
142 };
143
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);
147
148 do_bootm(NULL, 0, 2, bootm_args);
149
150 /*
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
154 */
155 do_reset(NULL, 0, 0, NULL);
156 }
157}
158
159/**
Dmitrii Merkurevc8acbbb2023-04-12 19:49:31 +0100160 * fastboot_handle_boot() - Shared implementation of system reaction to
161 * fastboot commands
162 *
163 * Making desceisions about device boot state (stay in fastboot, reboot
164 * to bootloader, reboot to OS, etc).
165 */
166void fastboot_handle_boot(int command, bool success)
167{
168 if (!success)
169 return;
170
171 switch (command) {
172 case FASTBOOT_COMMAND_BOOT:
173 fastboot_boot();
174 net_set_state(NETLOOP_SUCCESS);
175 break;
176
177 case FASTBOOT_COMMAND_CONTINUE:
178 net_set_state(NETLOOP_SUCCESS);
179 break;
180
181 case FASTBOOT_COMMAND_REBOOT:
182 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
183 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
184 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
185 do_reset(NULL, 0, 0, NULL);
186 break;
187 }
188}
189
190/**
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000191 * fastboot_set_progress_callback() - set progress callback
192 *
193 * @progress: Pointer to progress callback
194 *
195 * Set a callback which is invoked periodically during long running operations
196 * (flash and erase). This can be used (for example) by the UDP transport to
197 * send INFO responses to keep the client alive whilst those commands are
198 * executing.
199 */
200void fastboot_set_progress_callback(void (*progress)(const char *msg))
201{
202 fastboot_progress_callback = progress;
203}
204
205/*
206 * fastboot_init() - initialise new fastboot protocol session
207 *
208 * @buf_addr: Pointer to download buffer, or NULL for default
209 * @buf_size: Size of download buffer, or zero for default
210 */
211void fastboot_init(void *buf_addr, u32 buf_size)
212{
213 fastboot_buf_addr = buf_addr ? buf_addr :
214 (void *)CONFIG_FASTBOOT_BUF_ADDR;
215 fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE;
216 fastboot_set_progress_callback(NULL);
217}