blob: dde3cda78f6e625d8a7e0942b3552ed6755c3f14 [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>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000018
19/**
20 * fastboot_buf_addr - base address of the fastboot download buffer
21 */
22void *fastboot_buf_addr;
23
24/**
25 * fastboot_buf_size - size of the fastboot download buffer
26 */
27u32 fastboot_buf_size;
28
29/**
30 * fastboot_progress_callback - callback executed during long operations
31 */
32void (*fastboot_progress_callback)(const char *msg);
Alex Kiernand2df2ab2018-05-29 15:30:41 +000033
34/**
35 * fastboot_response() - Writes a response of the form "$tag$reason".
36 *
37 * @tag: The first part of the response
38 * @response: Pointer to fastboot response buffer
39 * @format: printf style format string
40 */
41void fastboot_response(const char *tag, char *response,
42 const char *format, ...)
43{
44 va_list args;
45
46 strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
47 if (format) {
48 va_start(args, format);
49 vsnprintf(response + strlen(response),
50 FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
51 format, args);
52 va_end(args);
53 }
54}
55
56/**
57 * fastboot_fail() - Write a FAIL response of the form "FAIL$reason".
58 *
59 * @reason: Pointer to returned reason string
60 * @response: Pointer to fastboot response buffer
61 */
62void fastboot_fail(const char *reason, char *response)
63{
64 fastboot_response("FAIL", response, "%s", reason);
65}
66
67/**
68 * fastboot_okay() - Write an OKAY response of the form "OKAY$reason".
69 *
70 * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY"
71 * @response: Pointer to fastboot response buffer
72 */
73void fastboot_okay(const char *reason, char *response)
74{
75 if (reason)
76 fastboot_response("OKAY", response, "%s", reason);
77 else
78 fastboot_response("OKAY", response, NULL);
79}
Alex Kiernan8a65bd62018-05-29 15:30:46 +000080
81/**
82 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
83 *
84 * Set flag which indicates that we should reboot into the bootloader
85 * following the reboot that fastboot executes after this function.
86 *
87 * This function should be overridden in your board file with one
88 * which sets whatever flag your board specific Android bootloader flow
89 * requires in order to re-enter the bootloader.
90 */
Roman Kovalivskyi851737a2020-07-28 23:35:32 +030091int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
Alex Kiernan8a65bd62018-05-29 15:30:46 +000092{
Roman Kovalivskyia362ce22021-01-26 22:54:56 +020093 static const char * const boot_cmds[] = {
94 [FASTBOOT_REBOOT_REASON_BOOTLOADER] = "bootonce-bootloader",
95 [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot",
96 [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery"
97 };
Patrick Delaunayd0379902022-12-15 10:15:50 +010098 const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
99 CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
100
Simon Glass7cb10e52023-02-05 17:54:12 -0700101 if (!IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100102 return -EINVAL;
Roman Kovalivskyia362ce22021-01-26 22:54:56 +0200103
104 if (reason >= FASTBOOT_REBOOT_REASONS_COUNT)
105 return -EINVAL;
106
Patrick Delaunayd0379902022-12-15 10:15:50 +0100107 return bcb_write_reboot_reason(mmc_dev, "misc", boot_cmds[reason]);
Alex Kiernan8a65bd62018-05-29 15:30:46 +0000108}
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000109
110/**
111 * fastboot_get_progress_callback() - Return progress callback
112 *
113 * Return: Pointer to function called during long operations
114 */
115void (*fastboot_get_progress_callback(void))(const char *)
116{
117 return fastboot_progress_callback;
118}
119
120/**
121 * fastboot_boot() - Execute fastboot boot command
122 *
123 * If ${fastboot_bootcmd} is set, run that command to execute the boot
124 * process, if that returns, then exit the fastboot server and return
125 * control to the caller.
126 *
127 * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset
128 * the board.
129 */
130void fastboot_boot(void)
131{
132 char *s;
133
134 s = env_get("fastboot_bootcmd");
135 if (s) {
136 run_command(s, CMD_FLAG_ENV);
137 } else {
Neil Armstrongf402d262019-02-20 11:36:12 +0100138 static char boot_addr_start[20];
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000139 static char *const bootm_args[] = {
140 "bootm", boot_addr_start, NULL
141 };
142
143 snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
144 "0x%p", fastboot_buf_addr);
145 printf("Booting kernel at %s...\n\n\n", boot_addr_start);
146
147 do_bootm(NULL, 0, 2, bootm_args);
148
149 /*
150 * This only happens if image is somehow faulty so we start
151 * over. We deliberately leave this policy to the invocation
152 * of fastbootcmd if that's what's being run
153 */
154 do_reset(NULL, 0, 0, NULL);
155 }
156}
157
158/**
159 * fastboot_set_progress_callback() - set progress callback
160 *
161 * @progress: Pointer to progress callback
162 *
163 * Set a callback which is invoked periodically during long running operations
164 * (flash and erase). This can be used (for example) by the UDP transport to
165 * send INFO responses to keep the client alive whilst those commands are
166 * executing.
167 */
168void fastboot_set_progress_callback(void (*progress)(const char *msg))
169{
170 fastboot_progress_callback = progress;
171}
172
173/*
174 * fastboot_init() - initialise new fastboot protocol session
175 *
176 * @buf_addr: Pointer to download buffer, or NULL for default
177 * @buf_size: Size of download buffer, or zero for default
178 */
179void fastboot_init(void *buf_addr, u32 buf_size)
180{
181 fastboot_buf_addr = buf_addr ? buf_addr :
182 (void *)CONFIG_FASTBOOT_BUF_ADDR;
183 fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE;
184 fastboot_set_progress_callback(NULL);
185}