blob: 595954542a6e740af984492127da8e955b389e39 [file] [log] [blame]
Alex Kiernaned6a41442018-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 Kovalivskyi13240bc2021-01-26 22:54:56 +020013#include <bcb.h>
Simon Glass39465c12023-12-14 21:19:06 -070014#include <bootm.h>
Alex Kiernaned6a41442018-05-29 15:30:41 +000015#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -070016#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060017#include <env.h>
Alex Kiernaned6a41442018-05-29 15:30:41 +000018#include <fastboot.h>
Dmitrii Merkurev07882902023-04-12 19:49:31 +010019#include <net.h>
Alex Kiernaned6a41442018-05-29 15:30:41 +000020
21/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +000022 * fastboot_buf_addr - base address of the fastboot download buffer
23 */
Simon Glassae4eb812023-12-14 21:19:04 -070024ulong fastboot_buf_addr;
Alex Kiernand5aa57c2018-05-29 15:30:53 +000025
26/**
27 * fastboot_buf_size - size of the fastboot download buffer
28 */
29u32 fastboot_buf_size;
30
31/**
32 * fastboot_progress_callback - callback executed during long operations
33 */
34void (*fastboot_progress_callback)(const char *msg);
35
36/**
Alex Kiernaned6a41442018-05-29 15:30:41 +000037 * fastboot_response() - Writes a response of the form "$tag$reason".
38 *
39 * @tag: The first part of the response
40 * @response: Pointer to fastboot response buffer
41 * @format: printf style format string
42 */
43void fastboot_response(const char *tag, char *response,
44 const char *format, ...)
45{
46 va_list args;
47
48 strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
49 if (format) {
50 va_start(args, format);
51 vsnprintf(response + strlen(response),
52 FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
53 format, args);
54 va_end(args);
55 }
56}
57
58/**
59 * fastboot_fail() - Write a FAIL response of the form "FAIL$reason".
60 *
61 * @reason: Pointer to returned reason string
62 * @response: Pointer to fastboot response buffer
63 */
64void fastboot_fail(const char *reason, char *response)
65{
66 fastboot_response("FAIL", response, "%s", reason);
67}
68
69/**
70 * fastboot_okay() - Write an OKAY response of the form "OKAY$reason".
71 *
72 * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY"
73 * @response: Pointer to fastboot response buffer
74 */
75void fastboot_okay(const char *reason, char *response)
76{
77 if (reason)
78 fastboot_response("OKAY", response, "%s", reason);
79 else
80 fastboot_response("OKAY", response, NULL);
81}
Alex Kiernan5512c432018-05-29 15:30:46 +000082
83/**
84 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
85 *
86 * Set flag which indicates that we should reboot into the bootloader
87 * following the reboot that fastboot executes after this function.
88 *
89 * This function should be overridden in your board file with one
90 * which sets whatever flag your board specific Android bootloader flow
91 * requires in order to re-enter the bootloader.
92 */
Roman Kovalivskyi1bb13422020-07-28 23:35:32 +030093int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
Alex Kiernan5512c432018-05-29 15:30:46 +000094{
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000095 int ret;
Roman Kovalivskyi13240bc2021-01-26 22:54:56 +020096 static const char * const boot_cmds[] = {
97 [FASTBOOT_REBOOT_REASON_BOOTLOADER] = "bootonce-bootloader",
98 [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot",
99 [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery"
100 };
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100101 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
102 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
103
Simon Glass091a0712023-02-05 17:54:12 -0700104 if (!IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100105 return -EINVAL;
Roman Kovalivskyi13240bc2021-01-26 22:54:56 +0200106
107 if (reason >= FASTBOOT_REBOOT_REASONS_COUNT)
108 return -EINVAL;
109
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000110 ret = bcb_find_partition_and_load("mmc", mmc_dev, "misc");
111 if (ret)
112 goto out;
113
114 ret = bcb_set(BCB_FIELD_COMMAND, boot_cmds[reason]);
115 if (ret)
116 goto out;
117
118 ret = bcb_store();
119out:
120 bcb_reset();
121 return ret;
Alex Kiernan5512c432018-05-29 15:30:46 +0000122}
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000123
124/**
125 * fastboot_get_progress_callback() - Return progress callback
126 *
127 * Return: Pointer to function called during long operations
128 */
129void (*fastboot_get_progress_callback(void))(const char *)
130{
131 return fastboot_progress_callback;
132}
133
134/**
135 * fastboot_boot() - Execute fastboot boot command
136 *
137 * If ${fastboot_bootcmd} is set, run that command to execute the boot
138 * process, if that returns, then exit the fastboot server and return
139 * control to the caller.
140 *
141 * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset
142 * the board.
143 */
144void fastboot_boot(void)
145{
Simon Glass39465c12023-12-14 21:19:06 -0700146 char *s = NULL;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000147
Simon Glass39465c12023-12-14 21:19:06 -0700148 if (IS_ENABLED(CONFIG_CMDLINE)) {
149 s = env_get("fastboot_bootcmd");
150 if (s)
151 run_command(s, CMD_FLAG_ENV);
152 }
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000153
Simon Glass39465c12023-12-14 21:19:06 -0700154 if (!s && IS_ENABLED(CONFIG_BOOTM)) {
155 int ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000156
Simon Glass39465c12023-12-14 21:19:06 -0700157 printf("Booting kernel at %lx...\n\n\n", fastboot_buf_addr);
158 ret = bootm_boot_start(fastboot_buf_addr, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000159
160 /*
161 * This only happens if image is somehow faulty so we start
162 * over. We deliberately leave this policy to the invocation
163 * of fastbootcmd if that's what's being run
164 */
165 do_reset(NULL, 0, 0, NULL);
166 }
167}
168
169/**
Dmitrii Merkurev07882902023-04-12 19:49:31 +0100170 * fastboot_handle_boot() - Shared implementation of system reaction to
171 * fastboot commands
172 *
173 * Making desceisions about device boot state (stay in fastboot, reboot
174 * to bootloader, reboot to OS, etc).
175 */
176void fastboot_handle_boot(int command, bool success)
177{
178 if (!success)
179 return;
180
181 switch (command) {
182 case FASTBOOT_COMMAND_BOOT:
183 fastboot_boot();
184 net_set_state(NETLOOP_SUCCESS);
185 break;
186
187 case FASTBOOT_COMMAND_CONTINUE:
188 net_set_state(NETLOOP_SUCCESS);
189 break;
190
191 case FASTBOOT_COMMAND_REBOOT:
192 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
193 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
194 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
195 do_reset(NULL, 0, 0, NULL);
196 break;
197 }
198}
199
200/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000201 * fastboot_set_progress_callback() - set progress callback
202 *
203 * @progress: Pointer to progress callback
204 *
205 * Set a callback which is invoked periodically during long running operations
206 * (flash and erase). This can be used (for example) by the UDP transport to
207 * send INFO responses to keep the client alive whilst those commands are
208 * executing.
209 */
210void fastboot_set_progress_callback(void (*progress)(const char *msg))
211{
212 fastboot_progress_callback = progress;
213}
214
Simon Glassae4eb812023-12-14 21:19:04 -0700215void fastboot_init(ulong buf_addr, u32 buf_size)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000216{
Simon Glassae4eb812023-12-14 21:19:04 -0700217 fastboot_buf_addr = buf_addr ? buf_addr : CONFIG_FASTBOOT_BUF_ADDR;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000218 fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE;
219 fastboot_set_progress_callback(NULL);
220}