1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
9 #include <fastboot-internal.h>
16 * image_size - final fastboot image size
18 static u32 image_size;
21 * fastboot_bytes_received - number of bytes received in the current download
23 static u32 fastboot_bytes_received;
26 * fastboot_bytes_expected - number of bytes expected in the current download
28 static u32 fastboot_bytes_expected;
30 static void okay(char *, char *);
31 static void getvar(char *, char *);
32 static void download(char *, char *);
33 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
34 static void flash(char *, char *);
35 static void erase(char *, char *);
37 static void reboot_bootloader(char *, char *);
38 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
39 static void oem_format(char *, char *);
44 void (*dispatch)(char *cmd_parameter, char *response);
45 } commands[FASTBOOT_COMMAND_COUNT] = {
46 [FASTBOOT_COMMAND_GETVAR] = {
50 [FASTBOOT_COMMAND_DOWNLOAD] = {
51 .command = "download",
54 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
55 [FASTBOOT_COMMAND_FLASH] = {
59 [FASTBOOT_COMMAND_ERASE] = {
64 [FASTBOOT_COMMAND_BOOT] = {
68 [FASTBOOT_COMMAND_CONTINUE] = {
69 .command = "continue",
72 [FASTBOOT_COMMAND_REBOOT] = {
76 [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
77 .command = "reboot-bootloader",
78 .dispatch = reboot_bootloader
80 [FASTBOOT_COMMAND_SET_ACTIVE] = {
81 .command = "set_active",
84 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
85 [FASTBOOT_COMMAND_OEM_FORMAT] = {
86 .command = "oem format",
87 .dispatch = oem_format,
93 * fastboot_handle_command - Handle fastboot command
95 * @cmd_string: Pointer to command string
96 * @response: Pointer to fastboot response buffer
98 * Return: Executed command, or -1 if not recognized
100 int fastboot_handle_command(char *cmd_string, char *response)
105 cmd_parameter = cmd_string;
106 strsep(&cmd_parameter, ":");
108 for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
109 if (!strcmp(commands[i].command, cmd_string)) {
110 if (commands[i].dispatch) {
111 commands[i].dispatch(cmd_parameter,
120 pr_err("command %s not recognized.\n", cmd_string);
121 fastboot_fail("unrecognized command", response);
126 * okay() - Send bare OKAY response
128 * @cmd_parameter: Pointer to command parameter
129 * @response: Pointer to fastboot response buffer
131 * Send a bare OKAY fastboot response. This is used where the command is
132 * valid, but all the work is done after the response has been sent (e.g.
135 static void okay(char *cmd_parameter, char *response)
137 fastboot_okay(NULL, response);
141 * getvar() - Read a config/version variable
143 * @cmd_parameter: Pointer to command parameter
144 * @response: Pointer to fastboot response buffer
146 static void getvar(char *cmd_parameter, char *response)
148 fastboot_getvar(cmd_parameter, response);
152 * fastboot_download() - Start a download transfer from the client
154 * @cmd_parameter: Pointer to command parameter
155 * @response: Pointer to fastboot response buffer
157 static void download(char *cmd_parameter, char *response)
161 if (!cmd_parameter) {
162 fastboot_fail("Expected command parameter", response);
165 fastboot_bytes_received = 0;
166 fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
167 if (fastboot_bytes_expected == 0) {
168 fastboot_fail("Expected nonzero image size", response);
172 * Nothing to download yet. Response is of the form:
173 * [DATA|FAIL]$cmd_parameter
175 * where cmd_parameter is an 8 digit hexadecimal number
177 if (fastboot_bytes_expected > fastboot_buf_size) {
178 fastboot_fail(cmd_parameter, response);
180 printf("Starting download of %d bytes\n",
181 fastboot_bytes_expected);
182 fastboot_response("DATA", response, "%s", cmd_parameter);
187 * fastboot_data_remaining() - return bytes remaining in current transfer
189 * Return: Number of bytes left in the current download
191 u32 fastboot_data_remaining(void)
193 return fastboot_bytes_expected - fastboot_bytes_received;
197 * fastboot_data_download() - Copy image data to fastboot_buf_addr.
199 * @fastboot_data: Pointer to received fastboot data
200 * @fastboot_data_len: Length of received fastboot data
201 * @response: Pointer to fastboot response buffer
203 * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
204 * response. fastboot_bytes_received is updated to indicate the number
205 * of bytes that have been transferred.
207 * On completion sets image_size and ${filesize} to the total size of the
210 void fastboot_data_download(const void *fastboot_data,
211 unsigned int fastboot_data_len,
214 #define BYTES_PER_DOT 0x20000
215 u32 pre_dot_num, now_dot_num;
217 if (fastboot_data_len == 0 ||
218 (fastboot_bytes_received + fastboot_data_len) >
219 fastboot_bytes_expected) {
220 fastboot_fail("Received invalid data length",
224 /* Download data to fastboot_buf_addr */
225 memcpy(fastboot_buf_addr + fastboot_bytes_received,
226 fastboot_data, fastboot_data_len);
228 pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
229 fastboot_bytes_received += fastboot_data_len;
230 now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
232 if (pre_dot_num != now_dot_num) {
234 if (!(now_dot_num % 74))
241 * fastboot_data_complete() - Mark current transfer complete
243 * @response: Pointer to fastboot response buffer
245 * Set image_size and ${filesize} to the total size of the downloaded image.
247 void fastboot_data_complete(char *response)
249 /* Download complete. Respond with "OKAY" */
250 fastboot_okay(NULL, response);
251 printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
252 image_size = fastboot_bytes_received;
253 env_set_hex("filesize", image_size);
254 fastboot_bytes_expected = 0;
255 fastboot_bytes_received = 0;
258 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
260 * flash() - write the downloaded image to the indicated partition.
262 * @cmd_parameter: Pointer to partition name
263 * @response: Pointer to fastboot response buffer
265 * Writes the previously downloaded image to the partition indicated by
266 * cmd_parameter. Writes to response.
268 static void flash(char *cmd_parameter, char *response)
270 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
271 fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
274 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
275 fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
281 * erase() - erase the indicated partition.
283 * @cmd_parameter: Pointer to partition name
284 * @response: Pointer to fastboot response buffer
286 * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
289 static void erase(char *cmd_parameter, char *response)
291 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
292 fastboot_mmc_erase(cmd_parameter, response);
294 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
295 fastboot_nand_erase(cmd_parameter, response);
301 * reboot_bootloader() - Sets reboot bootloader flag.
303 * @cmd_parameter: Pointer to command parameter
304 * @response: Pointer to fastboot response buffer
306 static void reboot_bootloader(char *cmd_parameter, char *response)
308 if (fastboot_set_reboot_flag())
309 fastboot_fail("Cannot set reboot flag", response);
311 fastboot_okay(NULL, response);
314 #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
316 * oem_format() - Execute the OEM format command
318 * @cmd_parameter: Pointer to command parameter
319 * @response: Pointer to fastboot response buffer
321 static void oem_format(char *cmd_parameter, char *response)
325 if (!env_get("partitions")) {
326 fastboot_fail("partitions not set", response);
328 sprintf(cmdbuf, "gpt write mmc %x $partitions",
329 CONFIG_FASTBOOT_FLASH_MMC_DEV);
330 if (run_command(cmdbuf, 0))
331 fastboot_fail("", response);
333 fastboot_okay(NULL, response);