1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
10 #include <fastboot-internal.h>
17 * image_size - final fastboot image size
19 static u32 image_size;
22 * fastboot_bytes_received - number of bytes received in the current download
24 static u32 fastboot_bytes_received;
27 * fastboot_bytes_expected - number of bytes expected in the current download
29 static u32 fastboot_bytes_expected;
31 static void okay(char *, char *);
32 static void getvar(char *, char *);
33 static void download(char *, char *);
34 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
35 static void flash(char *, char *);
36 static void erase(char *, char *);
38 static void reboot_bootloader(char *, char *);
39 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
40 static void oem_format(char *, char *);
45 void (*dispatch)(char *cmd_parameter, char *response);
46 } commands[FASTBOOT_COMMAND_COUNT] = {
47 [FASTBOOT_COMMAND_GETVAR] = {
51 [FASTBOOT_COMMAND_DOWNLOAD] = {
52 .command = "download",
55 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
56 [FASTBOOT_COMMAND_FLASH] = {
60 [FASTBOOT_COMMAND_ERASE] = {
65 [FASTBOOT_COMMAND_BOOT] = {
69 [FASTBOOT_COMMAND_CONTINUE] = {
70 .command = "continue",
73 [FASTBOOT_COMMAND_REBOOT] = {
77 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
78 .command = "reboot-bootloader",
79 .dispatch = reboot_bootloader
81 [FASTBOOT_COMMAND_SET_ACTIVE] = {
82 .command = "set_active",
85 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
86 [FASTBOOT_COMMAND_OEM_FORMAT] = {
87 .command = "oem format",
88 .dispatch = oem_format,
94 * fastboot_handle_command - Handle fastboot command
96 * @cmd_string: Pointer to command string
97 * @response: Pointer to fastboot response buffer
99 * Return: Executed command, or -1 if not recognized
101 int fastboot_handle_command(char *cmd_string, char *response)
106 cmd_parameter = cmd_string;
107 strsep(&cmd_parameter, ":");
109 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
110 if (!strcmp(commands[i].command, cmd_string)) {
111 if (commands[i].dispatch) {
112 commands[i].dispatch(cmd_parameter,
121 pr_err("command %s not recognized.\n", cmd_string);
122 fastboot_fail("unrecognized command", response);
127 * okay() - Send bare OKAY response
129 * @cmd_parameter: Pointer to command parameter
130 * @response: Pointer to fastboot response buffer
132 * Send a bare OKAY fastboot response. This is used where the command is
133 * valid, but all the work is done after the response has been sent (e.g.
136 static void okay(char *cmd_parameter, char *response)
138 fastboot_okay(NULL, response);
142 * getvar() - Read a config/version variable
144 * @cmd_parameter: Pointer to command parameter
145 * @response: Pointer to fastboot response buffer
147 static void getvar(char *cmd_parameter, char *response)
149 fastboot_getvar(cmd_parameter, response);
153 * fastboot_download() - Start a download transfer from the client
155 * @cmd_parameter: Pointer to command parameter
156 * @response: Pointer to fastboot response buffer
158 static void download(char *cmd_parameter, char *response)
162 if (!cmd_parameter) {
163 fastboot_fail("Expected command parameter", response);
166 fastboot_bytes_received = 0;
167 fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
168 if (fastboot_bytes_expected == 0) {
169 fastboot_fail("Expected nonzero image size", response);
173 * Nothing to download yet. Response is of the form:
174 * [DATA|FAIL]$cmd_parameter
176 * where cmd_parameter is an 8 digit hexadecimal number
178 if (fastboot_bytes_expected > fastboot_buf_size) {
179 fastboot_fail(cmd_parameter, response);
181 printf("Starting download of %d bytes\n",
182 fastboot_bytes_expected);
183 fastboot_response("DATA", response, "%s", cmd_parameter);
188 * fastboot_data_remaining() - return bytes remaining in current transfer
190 * Return: Number of bytes left in the current download
192 u32 fastboot_data_remaining(void)
194 return fastboot_bytes_expected - fastboot_bytes_received;
198 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
200 * @fastboot_data: Pointer to received fastboot data
201 * @fastboot_data_len: Length of received fastboot data
202 * @response: Pointer to fastboot response buffer
204 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
205 * response. fastboot_bytes_received is updated to indicate the number
206 * of bytes that have been transferred.
208 * On completion sets image_size and ${filesize} to the total size of the
211 void fastboot_data_download(const void *fastboot_data,
212 unsigned int fastboot_data_len,
215 #define BYTES_PER_DOT 0x20000
216 u32 pre_dot_num, now_dot_num;
218 if (fastboot_data_len == 0 ||
219 (fastboot_bytes_received + fastboot_data_len) >
220 fastboot_bytes_expected) {
221 fastboot_fail("Received invalid data length",
225 /* Download data to fastboot_buf_addr */
226 memcpy(fastboot_buf_addr + fastboot_bytes_received,
227 fastboot_data, fastboot_data_len);
229 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
230 fastboot_bytes_received += fastboot_data_len;
231 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
233 if (pre_dot_num != now_dot_num) {
235 if (!(now_dot_num % 74))
242 * fastboot_data_complete() - Mark current transfer complete
244 * @response: Pointer to fastboot response buffer
246 * Set image_size and ${filesize} to the total size of the downloaded image.
248 void fastboot_data_complete(char *response)
250 /* Download complete. Respond with "OKAY" */
251 fastboot_okay(NULL, response);
252 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
253 image_size = fastboot_bytes_received;
254 env_set_hex("filesize", image_size);
255 fastboot_bytes_expected = 0;
256 fastboot_bytes_received = 0;
259 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
261 * flash() - write the downloaded image to the indicated partition.
263 * @cmd_parameter: Pointer to partition name
264 * @response: Pointer to fastboot response buffer
266 * Writes the previously downloaded image to the partition indicated by
267 * cmd_parameter. Writes to response.
269 static void flash(char *cmd_parameter, char *response)
271 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
272 fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
275 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
276 fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
282 * erase() - erase the indicated partition.
284 * @cmd_parameter: Pointer to partition name
285 * @response: Pointer to fastboot response buffer
287 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
290 static void erase(char *cmd_parameter, char *response)
292 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
293 fastboot_mmc_erase(cmd_parameter, response);
295 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
296 fastboot_nand_erase(cmd_parameter, response);
302 * reboot_bootloader() - Sets reboot bootloader flag.
304 * @cmd_parameter: Pointer to command parameter
305 * @response: Pointer to fastboot response buffer
307 static void reboot_bootloader(char *cmd_parameter, char *response)
309 if (fastboot_set_reboot_flag())
310 fastboot_fail("Cannot set reboot flag", response);
312 fastboot_okay(NULL, response);
315 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
317 * oem_format() - Execute the OEM format command
319 * @cmd_parameter: Pointer to command parameter
320 * @response: Pointer to fastboot response buffer
322 static void oem_format(char *cmd_parameter, char *response)
326 if (!env_get("partitions")) {
327 fastboot_fail("partitions not set", response);
329 sprintf(cmdbuf, "gpt write mmc %x $partitions",
330 CONFIG_FASTBOOT_FLASH_MMC_DEV);
331 if (run_command(cmdbuf, 0))
332 fastboot_fail("", response);
334 fastboot_okay(NULL, response);