blob: e76af8ecc320b954b1ab2cf2cde514343f093909 [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
13#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060014#include <env.h>
Alex Kiernaned6a41442018-05-29 15:30:41 +000015#include <fastboot.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000016#include <net/fastboot.h>
Alex Kiernaned6a41442018-05-29 15:30:41 +000017
18/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +000019 * fastboot_buf_addr - base address of the fastboot download buffer
20 */
21void *fastboot_buf_addr;
22
23/**
24 * fastboot_buf_size - size of the fastboot download buffer
25 */
26u32 fastboot_buf_size;
27
28/**
29 * fastboot_progress_callback - callback executed during long operations
30 */
31void (*fastboot_progress_callback)(const char *msg);
32
33/**
Alex Kiernaned6a41442018-05-29 15:30:41 +000034 * fastboot_response() - Writes a response of the form "$tag$reason".
35 *
36 * @tag: The first part of the response
37 * @response: Pointer to fastboot response buffer
38 * @format: printf style format string
39 */
40void fastboot_response(const char *tag, char *response,
41 const char *format, ...)
42{
43 va_list args;
44
45 strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
46 if (format) {
47 va_start(args, format);
48 vsnprintf(response + strlen(response),
49 FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
50 format, args);
51 va_end(args);
52 }
53}
54
55/**
56 * fastboot_fail() - Write a FAIL response of the form "FAIL$reason".
57 *
58 * @reason: Pointer to returned reason string
59 * @response: Pointer to fastboot response buffer
60 */
61void fastboot_fail(const char *reason, char *response)
62{
63 fastboot_response("FAIL", response, "%s", reason);
64}
65
66/**
67 * fastboot_okay() - Write an OKAY response of the form "OKAY$reason".
68 *
69 * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY"
70 * @response: Pointer to fastboot response buffer
71 */
72void fastboot_okay(const char *reason, char *response)
73{
74 if (reason)
75 fastboot_response("OKAY", response, "%s", reason);
76 else
77 fastboot_response("OKAY", response, NULL);
78}
Alex Kiernan5512c432018-05-29 15:30:46 +000079
80/**
81 * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
82 *
83 * Set flag which indicates that we should reboot into the bootloader
84 * following the reboot that fastboot executes after this function.
85 *
86 * This function should be overridden in your board file with one
87 * which sets whatever flag your board specific Android bootloader flow
88 * requires in order to re-enter the bootloader.
89 */
90int __weak fastboot_set_reboot_flag(void)
91{
92 return -ENOSYS;
93}
Alex Kiernand5aa57c2018-05-29 15:30:53 +000094
95/**
96 * fastboot_get_progress_callback() - Return progress callback
97 *
98 * Return: Pointer to function called during long operations
99 */
100void (*fastboot_get_progress_callback(void))(const char *)
101{
102 return fastboot_progress_callback;
103}
104
105/**
106 * fastboot_boot() - Execute fastboot boot command
107 *
108 * If ${fastboot_bootcmd} is set, run that command to execute the boot
109 * process, if that returns, then exit the fastboot server and return
110 * control to the caller.
111 *
112 * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset
113 * the board.
114 */
115void fastboot_boot(void)
116{
117 char *s;
118
119 s = env_get("fastboot_bootcmd");
120 if (s) {
121 run_command(s, CMD_FLAG_ENV);
122 } else {
Neil Armstrong2577bb92019-02-20 11:36:12 +0100123 static char boot_addr_start[20];
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000124 static char *const bootm_args[] = {
125 "bootm", boot_addr_start, NULL
126 };
127
128 snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
129 "0x%p", fastboot_buf_addr);
130 printf("Booting kernel at %s...\n\n\n", boot_addr_start);
131
132 do_bootm(NULL, 0, 2, bootm_args);
133
134 /*
135 * This only happens if image is somehow faulty so we start
136 * over. We deliberately leave this policy to the invocation
137 * of fastbootcmd if that's what's being run
138 */
139 do_reset(NULL, 0, 0, NULL);
140 }
141}
142
143/**
144 * fastboot_set_progress_callback() - set progress callback
145 *
146 * @progress: Pointer to progress callback
147 *
148 * Set a callback which is invoked periodically during long running operations
149 * (flash and erase). This can be used (for example) by the UDP transport to
150 * send INFO responses to keep the client alive whilst those commands are
151 * executing.
152 */
153void fastboot_set_progress_callback(void (*progress)(const char *msg))
154{
155 fastboot_progress_callback = progress;
156}
157
158/*
159 * fastboot_init() - initialise new fastboot protocol session
160 *
161 * @buf_addr: Pointer to download buffer, or NULL for default
162 * @buf_size: Size of download buffer, or zero for default
163 */
164void fastboot_init(void *buf_addr, u32 buf_size)
165{
166 fastboot_buf_addr = buf_addr ? buf_addr :
167 (void *)CONFIG_FASTBOOT_BUF_ADDR;
168 fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE;
169 fastboot_set_progress_callback(NULL);
170}