1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
8 #include <fastboot-internal.h>
15 * image_size - final fastboot image size
17 static u32 image_size;
20 * fastboot_bytes_received - number of bytes received in the current download
22 static u32 fastboot_bytes_received;
25 * fastboot_bytes_expected - number of bytes expected in the current download
27 static u32 fastboot_bytes_expected;
29 static void okay(char *, char *);
30 static void getvar(char *, char *);
31 static void download(char *, char *);
32 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
33 static void flash(char *, char *);
34 static void erase(char *, char *);
36 static void reboot_bootloader(char *, char *);
37 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
38 static void oem_format(char *, char *);
43 void (*dispatch)(char *cmd_parameter, char *response);
44 } commands[FASTBOOT_COMMAND_COUNT] = {
45 [FASTBOOT_COMMAND_GETVAR] = {
49 [FASTBOOT_COMMAND_DOWNLOAD] = {
50 .command = "download",
53 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
54 [FASTBOOT_COMMAND_FLASH] = {
58 [FASTBOOT_COMMAND_ERASE] = {
63 [FASTBOOT_COMMAND_BOOT] = {
67 [FASTBOOT_COMMAND_CONTINUE] = {
68 .command = "continue",
71 [FASTBOOT_COMMAND_REBOOT] = {
75 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
76 .command = "reboot-bootloader",
77 .dispatch = reboot_bootloader
79 [FASTBOOT_COMMAND_SET_ACTIVE] = {
80 .command = "set_active",
83 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
84 [FASTBOOT_COMMAND_OEM_FORMAT] = {
85 .command = "oem format",
86 .dispatch = oem_format,
92 * fastboot_handle_command - Handle fastboot command
94 * @cmd_string: Pointer to command string
95 * @response: Pointer to fastboot response buffer
97 * Return: Executed command, or -1 if not recognized
99 int fastboot_handle_command(char *cmd_string, char *response)
104 cmd_parameter = cmd_string;
105 strsep(&cmd_parameter, ":");
107 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
108 if (!strcmp(commands[i].command, cmd_string)) {
109 if (commands[i].dispatch) {
110 commands[i].dispatch(cmd_parameter,
119 pr_err("command %s not recognized.\n", cmd_string);
120 fastboot_fail("unrecognized command", response);
125 * okay() - Send bare OKAY response
127 * @cmd_parameter: Pointer to command parameter
128 * @response: Pointer to fastboot response buffer
130 * Send a bare OKAY fastboot response. This is used where the command is
131 * valid, but all the work is done after the response has been sent (e.g.
134 static void okay(char *cmd_parameter, char *response)
136 fastboot_okay(NULL, response);
140 * getvar() - Read a config/version variable
142 * @cmd_parameter: Pointer to command parameter
143 * @response: Pointer to fastboot response buffer
145 static void getvar(char *cmd_parameter, char *response)
147 fastboot_getvar(cmd_parameter, response);
151 * fastboot_download() - Start a download transfer from the client
153 * @cmd_parameter: Pointer to command parameter
154 * @response: Pointer to fastboot response buffer
156 static void download(char *cmd_parameter, char *response)
160 if (!cmd_parameter) {
161 fastboot_fail("Expected command parameter", response);
164 fastboot_bytes_received = 0;
165 fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
166 if (fastboot_bytes_expected == 0) {
167 fastboot_fail("Expected nonzero image size", response);
171 * Nothing to download yet. Response is of the form:
172 * [DATA|FAIL]$cmd_parameter
174 * where cmd_parameter is an 8 digit hexadecimal number
176 if (fastboot_bytes_expected > fastboot_buf_size) {
177 fastboot_fail(cmd_parameter, response);
179 printf("Starting download of %d bytes\n",
180 fastboot_bytes_expected);
181 fastboot_response("DATA", response, "%s", cmd_parameter);
186 * fastboot_data_remaining() - return bytes remaining in current transfer
188 * Return: Number of bytes left in the current download
190 u32 fastboot_data_remaining(void)
192 return fastboot_bytes_expected - fastboot_bytes_received;
196 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
198 * @fastboot_data: Pointer to received fastboot data
199 * @fastboot_data_len: Length of received fastboot data
200 * @response: Pointer to fastboot response buffer
202 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
203 * response. fastboot_bytes_received is updated to indicate the number
204 * of bytes that have been transferred.
206 * On completion sets image_size and ${filesize} to the total size of the
209 void fastboot_data_download(const void *fastboot_data,
210 unsigned int fastboot_data_len,
213 #define BYTES_PER_DOT 0x20000
214 u32 pre_dot_num, now_dot_num;
216 if (fastboot_data_len == 0 ||
217 (fastboot_bytes_received + fastboot_data_len) >
218 fastboot_bytes_expected) {
219 fastboot_fail("Received invalid data length",
223 /* Download data to fastboot_buf_addr */
224 memcpy(fastboot_buf_addr + fastboot_bytes_received,
225 fastboot_data, fastboot_data_len);
227 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
228 fastboot_bytes_received += fastboot_data_len;
229 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
231 if (pre_dot_num != now_dot_num) {
233 if (!(now_dot_num % 74))
240 * fastboot_data_complete() - Mark current transfer complete
242 * @response: Pointer to fastboot response buffer
244 * Set image_size and ${filesize} to the total size of the downloaded image.
246 void fastboot_data_complete(char *response)
248 /* Download complete. Respond with "OKAY" */
249 fastboot_okay(NULL, response);
250 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
251 image_size = fastboot_bytes_received;
252 env_set_hex("filesize", image_size);
253 fastboot_bytes_expected = 0;
254 fastboot_bytes_received = 0;
257 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
259 * flash() - write the downloaded image to the indicated partition.
261 * @cmd_parameter: Pointer to partition name
262 * @response: Pointer to fastboot response buffer
264 * Writes the previously downloaded image to the partition indicated by
265 * cmd_parameter. Writes to response.
267 static void flash(char *cmd_parameter, char *response)
269 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
270 fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
273 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
274 fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
280 * erase() - erase the indicated partition.
282 * @cmd_parameter: Pointer to partition name
283 * @response: Pointer to fastboot response buffer
285 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
288 static void erase(char *cmd_parameter, char *response)
290 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
291 fastboot_mmc_erase(cmd_parameter, response);
293 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
294 fastboot_nand_erase(cmd_parameter, response);
300 * reboot_bootloader() - Sets reboot bootloader flag.
302 * @cmd_parameter: Pointer to command parameter
303 * @response: Pointer to fastboot response buffer
305 static void reboot_bootloader(char *cmd_parameter, char *response)
307 if (fastboot_set_reboot_flag())
308 fastboot_fail("Cannot set reboot flag", response);
310 fastboot_okay(NULL, response);
313 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
315 * oem_format() - Execute the OEM format command
317 * @cmd_parameter: Pointer to command parameter
318 * @response: Pointer to fastboot response buffer
320 static void oem_format(char *cmd_parameter, char *response)
324 if (!env_get("partitions")) {
325 fastboot_fail("partitions not set", response);
327 sprintf(cmdbuf, "gpt write mmc %x $partitions",
328 CONFIG_FASTBOOT_FLASH_MMC_DEV);
329 if (run_command(cmdbuf, 0))
330 fastboot_fail("", response);
332 fastboot_okay(NULL, response);