1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
10 #include <fastboot-internal.h>
18 * image_size - final fastboot image size
20 static u32 image_size;
23 * fastboot_bytes_received - number of bytes received in the current download
25 static u32 fastboot_bytes_received;
28 * fastboot_bytes_expected - number of bytes expected in the current download
30 static u32 fastboot_bytes_expected;
32 static void okay(char *, char *);
33 static void getvar(char *, char *);
34 static void download(char *, char *);
35 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
36 static void flash(char *, char *);
37 static void erase(char *, char *);
39 static void reboot_bootloader(char *, char *);
40 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
41 static void oem_format(char *, char *);
46 void (*dispatch)(char *cmd_parameter, char *response);
47 } commands[FASTBOOT_COMMAND_COUNT] = {
48 [FASTBOOT_COMMAND_GETVAR] = {
52 [FASTBOOT_COMMAND_DOWNLOAD] = {
53 .command = "download",
56 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
57 [FASTBOOT_COMMAND_FLASH] = {
61 [FASTBOOT_COMMAND_ERASE] = {
66 [FASTBOOT_COMMAND_BOOT] = {
70 [FASTBOOT_COMMAND_CONTINUE] = {
71 .command = "continue",
74 [FASTBOOT_COMMAND_REBOOT] = {
78 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
79 .command = "reboot-bootloader",
80 .dispatch = reboot_bootloader
82 [FASTBOOT_COMMAND_SET_ACTIVE] = {
83 .command = "set_active",
86 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
87 [FASTBOOT_COMMAND_OEM_FORMAT] = {
88 .command = "oem format",
89 .dispatch = oem_format,
95 * fastboot_handle_command - Handle fastboot command
97 * @cmd_string: Pointer to command string
98 * @response: Pointer to fastboot response buffer
100 * Return: Executed command, or -1 if not recognized
102 int fastboot_handle_command(char *cmd_string, char *response)
107 cmd_parameter = cmd_string;
108 strsep(&cmd_parameter, ":");
110 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
111 if (!strcmp(commands[i].command, cmd_string)) {
112 if (commands[i].dispatch) {
113 commands[i].dispatch(cmd_parameter,
122 pr_err("command %s not recognized.\n", cmd_string);
123 fastboot_fail("unrecognized command", response);
128 * okay() - Send bare OKAY response
130 * @cmd_parameter: Pointer to command parameter
131 * @response: Pointer to fastboot response buffer
133 * Send a bare OKAY fastboot response. This is used where the command is
134 * valid, but all the work is done after the response has been sent (e.g.
137 static void okay(char *cmd_parameter, char *response)
139 fastboot_okay(NULL, response);
143 * getvar() - Read a config/version variable
145 * @cmd_parameter: Pointer to command parameter
146 * @response: Pointer to fastboot response buffer
148 static void getvar(char *cmd_parameter, char *response)
150 fastboot_getvar(cmd_parameter, response);
154 * fastboot_download() - Start a download transfer from the client
156 * @cmd_parameter: Pointer to command parameter
157 * @response: Pointer to fastboot response buffer
159 static void download(char *cmd_parameter, char *response)
163 if (!cmd_parameter) {
164 fastboot_fail("Expected command parameter", response);
167 fastboot_bytes_received = 0;
168 fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
169 if (fastboot_bytes_expected == 0) {
170 fastboot_fail("Expected nonzero image size", response);
174 * Nothing to download yet. Response is of the form:
175 * [DATA|FAIL]$cmd_parameter
177 * where cmd_parameter is an 8 digit hexadecimal number
179 if (fastboot_bytes_expected > fastboot_buf_size) {
180 fastboot_fail(cmd_parameter, response);
182 printf("Starting download of %d bytes\n",
183 fastboot_bytes_expected);
184 fastboot_response("DATA", response, "%s", cmd_parameter);
189 * fastboot_data_remaining() - return bytes remaining in current transfer
191 * Return: Number of bytes left in the current download
193 u32 fastboot_data_remaining(void)
195 return fastboot_bytes_expected - fastboot_bytes_received;
199 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
201 * @fastboot_data: Pointer to received fastboot data
202 * @fastboot_data_len: Length of received fastboot data
203 * @response: Pointer to fastboot response buffer
205 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
206 * response. fastboot_bytes_received is updated to indicate the number
207 * of bytes that have been transferred.
209 * On completion sets image_size and ${filesize} to the total size of the
212 void fastboot_data_download(const void *fastboot_data,
213 unsigned int fastboot_data_len,
216 #define BYTES_PER_DOT 0x20000
217 u32 pre_dot_num, now_dot_num;
219 if (fastboot_data_len == 0 ||
220 (fastboot_bytes_received + fastboot_data_len) >
221 fastboot_bytes_expected) {
222 fastboot_fail("Received invalid data length",
226 /* Download data to fastboot_buf_addr */
227 memcpy(fastboot_buf_addr + fastboot_bytes_received,
228 fastboot_data, fastboot_data_len);
230 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
231 fastboot_bytes_received += fastboot_data_len;
232 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
234 if (pre_dot_num != now_dot_num) {
236 if (!(now_dot_num % 74))
243 * fastboot_data_complete() - Mark current transfer complete
245 * @response: Pointer to fastboot response buffer
247 * Set image_size and ${filesize} to the total size of the downloaded image.
249 void fastboot_data_complete(char *response)
251 /* Download complete. Respond with "OKAY" */
252 fastboot_okay(NULL, response);
253 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
254 image_size = fastboot_bytes_received;
255 env_set_hex("filesize", image_size);
256 fastboot_bytes_expected = 0;
257 fastboot_bytes_received = 0;
260 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
262 * flash() - write the downloaded image to the indicated partition.
264 * @cmd_parameter: Pointer to partition name
265 * @response: Pointer to fastboot response buffer
267 * Writes the previously downloaded image to the partition indicated by
268 * cmd_parameter. Writes to response.
270 static void flash(char *cmd_parameter, char *response)
272 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
273 fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
276 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
277 fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
283 * erase() - erase the indicated partition.
285 * @cmd_parameter: Pointer to partition name
286 * @response: Pointer to fastboot response buffer
288 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
291 static void erase(char *cmd_parameter, char *response)
293 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
294 fastboot_mmc_erase(cmd_parameter, response);
296 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
297 fastboot_nand_erase(cmd_parameter, response);
303 * reboot_bootloader() - Sets reboot bootloader flag.
305 * @cmd_parameter: Pointer to command parameter
306 * @response: Pointer to fastboot response buffer
308 static void reboot_bootloader(char *cmd_parameter, char *response)
310 if (fastboot_set_reboot_flag())
311 fastboot_fail("Cannot set reboot flag", response);
313 fastboot_okay(NULL, response);
316 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
318 * oem_format() - Execute the OEM format command
320 * @cmd_parameter: Pointer to command parameter
321 * @response: Pointer to fastboot response buffer
323 static void oem_format(char *cmd_parameter, char *response)
327 if (!env_get("partitions")) {
328 fastboot_fail("partitions not set", response);
330 sprintf(cmdbuf, "gpt write mmc %x $partitions",
331 CONFIG_FASTBOOT_FLASH_MMC_DEV);
332 if (run_command(cmdbuf, 0))
333 fastboot_fail("", response);
335 fastboot_okay(NULL, response);