Merge git://git.denx.de/u-boot-marvell
[platform/kernel/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 <bcb.h>
14 #include <common.h>
15 #include <command.h>
16 #include <env.h>
17 #include <fastboot.h>
18 #include <net/fastboot.h>
19
20 /**
21  * fastboot_buf_addr - base address of the fastboot download buffer
22  */
23 void *fastboot_buf_addr;
24
25 /**
26  * fastboot_buf_size - size of the fastboot download buffer
27  */
28 u32 fastboot_buf_size;
29
30 /**
31  * fastboot_progress_callback - callback executed during long operations
32  */
33 void (*fastboot_progress_callback)(const char *msg);
34
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  */
42 void 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  */
63 void 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  */
74 void 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 }
81
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  */
92 int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
93 {
94 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC_DEV)
95         static const char * const boot_cmds[] = {
96                 [FASTBOOT_REBOOT_REASON_BOOTLOADER] = "bootonce-bootloader",
97                 [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot",
98                 [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery"
99         };
100
101         if (reason >= FASTBOOT_REBOOT_REASONS_COUNT)
102                 return -EINVAL;
103
104         return bcb_write_reboot_reason(CONFIG_FASTBOOT_FLASH_MMC_DEV, "misc", boot_cmds[reason]);
105 #else
106     return -EINVAL;
107 #endif
108 }
109
110 /**
111  * fastboot_get_progress_callback() - Return progress callback
112  *
113  * Return: Pointer to function called during long operations
114  */
115 void (*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  */
130 void 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 {
138                 static char boot_addr_start[20];
139                 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  */
168 void 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  */
179 void 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 }