x86: qemu: Fix build warnings with CONFIG_DISTRO_DEFAULTS=n
[oweals/u-boot.git] / drivers / fastboot / fb_common.c
1 // 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>
14 #include <env.h>
15 #include <fastboot.h>
16 #include <net/fastboot.h>
17
18 /**
19  * fastboot_buf_addr - base address of the fastboot download buffer
20  */
21 void *fastboot_buf_addr;
22
23 /**
24  * fastboot_buf_size - size of the fastboot download buffer
25  */
26 u32 fastboot_buf_size;
27
28 /**
29  * fastboot_progress_callback - callback executed during long operations
30  */
31 void (*fastboot_progress_callback)(const char *msg);
32
33 /**
34  * 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  */
40 void 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  */
61 void 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  */
72 void 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 }
79
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  */
90 int __weak fastboot_set_reboot_flag(void)
91 {
92         return -ENOSYS;
93 }
94
95 /**
96  * fastboot_get_progress_callback() - Return progress callback
97  *
98  * Return: Pointer to function called during long operations
99  */
100 void (*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  */
115 void 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 {
123                 static char boot_addr_start[20];
124                 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  */
153 void 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  */
164 void 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 }