Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | */ |
| 5 | |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 6 | #include <command.h> |
Ion Agorria | 1c38703 | 2024-01-05 09:22:06 +0200 | [diff] [blame] | 7 | #include <console.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 8 | #include <env.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 9 | #include <fastboot.h> |
| 10 | #include <fastboot-internal.h> |
| 11 | #include <fb_mmc.h> |
| 12 | #include <fb_nand.h> |
| 13 | #include <part.h> |
| 14 | #include <stdlib.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 15 | #include <vsprintf.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 16 | #include <linux/printk.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * image_size - final fastboot image size |
| 20 | */ |
| 21 | static u32 image_size; |
| 22 | |
| 23 | /** |
| 24 | * fastboot_bytes_received - number of bytes received in the current download |
| 25 | */ |
| 26 | static u32 fastboot_bytes_received; |
| 27 | |
| 28 | /** |
| 29 | * fastboot_bytes_expected - number of bytes expected in the current download |
| 30 | */ |
| 31 | static u32 fastboot_bytes_expected; |
| 32 | |
| 33 | static void okay(char *, char *); |
| 34 | static void getvar(char *, char *); |
| 35 | static void download(char *, char *); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 36 | static void flash(char *, char *); |
| 37 | static void erase(char *, char *); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 38 | static void reboot_bootloader(char *, char *); |
Roman Kovalivskyi | b30b97b | 2020-07-28 23:35:33 +0300 | [diff] [blame] | 39 | static void reboot_fastbootd(char *, char *); |
| 40 | static void reboot_recovery(char *, char *); |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 41 | static void oem_format(char *, char *); |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 42 | static void oem_partconf(char *, char *); |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 43 | static void oem_bootbus(char *, char *); |
Ion Agorria | e64262d | 2024-01-05 09:22:11 +0200 | [diff] [blame] | 44 | static void oem_console(char *, char *); |
Alexey Romanov | a0ae790 | 2024-04-18 13:01:29 +0300 | [diff] [blame] | 45 | static void oem_board(char *, char *); |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 46 | static void run_ucmd(char *, char *); |
| 47 | static void run_acmd(char *, char *); |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 48 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 49 | static const struct { |
| 50 | const char *command; |
| 51 | void (*dispatch)(char *cmd_parameter, char *response); |
| 52 | } commands[FASTBOOT_COMMAND_COUNT] = { |
| 53 | [FASTBOOT_COMMAND_GETVAR] = { |
| 54 | .command = "getvar", |
| 55 | .dispatch = getvar |
| 56 | }, |
| 57 | [FASTBOOT_COMMAND_DOWNLOAD] = { |
| 58 | .command = "download", |
| 59 | .dispatch = download |
| 60 | }, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 61 | [FASTBOOT_COMMAND_FLASH] = { |
| 62 | .command = "flash", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 63 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_FLASH, (flash), (NULL)) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 64 | }, |
| 65 | [FASTBOOT_COMMAND_ERASE] = { |
| 66 | .command = "erase", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 67 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_FLASH, (erase), (NULL)) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 68 | }, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 69 | [FASTBOOT_COMMAND_BOOT] = { |
| 70 | .command = "boot", |
| 71 | .dispatch = okay |
| 72 | }, |
| 73 | [FASTBOOT_COMMAND_CONTINUE] = { |
| 74 | .command = "continue", |
| 75 | .dispatch = okay |
| 76 | }, |
| 77 | [FASTBOOT_COMMAND_REBOOT] = { |
| 78 | .command = "reboot", |
| 79 | .dispatch = okay |
| 80 | }, |
| 81 | [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = { |
| 82 | .command = "reboot-bootloader", |
| 83 | .dispatch = reboot_bootloader |
| 84 | }, |
Roman Kovalivskyi | b30b97b | 2020-07-28 23:35:33 +0300 | [diff] [blame] | 85 | [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = { |
| 86 | .command = "reboot-fastboot", |
| 87 | .dispatch = reboot_fastbootd |
| 88 | }, |
| 89 | [FASTBOOT_COMMAND_REBOOT_RECOVERY] = { |
| 90 | .command = "reboot-recovery", |
| 91 | .dispatch = reboot_recovery |
| 92 | }, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 93 | [FASTBOOT_COMMAND_SET_ACTIVE] = { |
| 94 | .command = "set_active", |
| 95 | .dispatch = okay |
| 96 | }, |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 97 | [FASTBOOT_COMMAND_OEM_FORMAT] = { |
| 98 | .command = "oem format", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 99 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT, (oem_format), (NULL)) |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 100 | }, |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 101 | [FASTBOOT_COMMAND_OEM_PARTCONF] = { |
| 102 | .command = "oem partconf", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 103 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF, (oem_partconf), (NULL)) |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 104 | }, |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 105 | [FASTBOOT_COMMAND_OEM_BOOTBUS] = { |
| 106 | .command = "oem bootbus", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 107 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS, (oem_bootbus), (NULL)) |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 108 | }, |
Sean Anderson | 421bec0 | 2022-12-16 13:20:16 -0500 | [diff] [blame] | 109 | [FASTBOOT_COMMAND_OEM_RUN] = { |
| 110 | .command = "oem run", |
| 111 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_OEM_RUN, (run_ucmd), (NULL)) |
| 112 | }, |
Ion Agorria | e64262d | 2024-01-05 09:22:11 +0200 | [diff] [blame] | 113 | [FASTBOOT_COMMAND_OEM_CONSOLE] = { |
| 114 | .command = "oem console", |
| 115 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_CONSOLE, (oem_console), (NULL)) |
| 116 | }, |
Alexey Romanov | a0ae790 | 2024-04-18 13:01:29 +0300 | [diff] [blame] | 117 | [FASTBOOT_COMMAND_OEM_BOARD] = { |
| 118 | .command = "oem board", |
| 119 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_OEM_BOARD, (oem_board), (NULL)) |
| 120 | }, |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 121 | [FASTBOOT_COMMAND_UCMD] = { |
| 122 | .command = "UCmd", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 123 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT, (run_ucmd), (NULL)) |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 124 | }, |
| 125 | [FASTBOOT_COMMAND_ACMD] = { |
| 126 | .command = "ACmd", |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 127 | .dispatch = CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT, (run_acmd), (NULL)) |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 128 | }, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * fastboot_handle_command - Handle fastboot command |
| 133 | * |
| 134 | * @cmd_string: Pointer to command string |
| 135 | * @response: Pointer to fastboot response buffer |
| 136 | * |
| 137 | * Return: Executed command, or -1 if not recognized |
| 138 | */ |
| 139 | int fastboot_handle_command(char *cmd_string, char *response) |
| 140 | { |
| 141 | int i; |
| 142 | char *cmd_parameter; |
| 143 | |
| 144 | cmd_parameter = cmd_string; |
| 145 | strsep(&cmd_parameter, ":"); |
| 146 | |
| 147 | for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) { |
| 148 | if (!strcmp(commands[i].command, cmd_string)) { |
| 149 | if (commands[i].dispatch) { |
| 150 | commands[i].dispatch(cmd_parameter, |
| 151 | response); |
| 152 | return i; |
| 153 | } else { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 154 | pr_err("command %s not supported.\n", cmd_string); |
| 155 | fastboot_fail("Unsupported command", response); |
| 156 | return -1; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pr_err("command %s not recognized.\n", cmd_string); |
| 162 | fastboot_fail("unrecognized command", response); |
| 163 | return -1; |
| 164 | } |
| 165 | |
Ion Agorria | 1c38703 | 2024-01-05 09:22:06 +0200 | [diff] [blame] | 166 | void fastboot_multiresponse(int cmd, char *response) |
| 167 | { |
| 168 | switch (cmd) { |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 169 | case FASTBOOT_COMMAND_GETVAR: |
| 170 | fastboot_getvar_all(response); |
| 171 | break; |
Ion Agorria | e64262d | 2024-01-05 09:22:11 +0200 | [diff] [blame] | 172 | case FASTBOOT_COMMAND_OEM_CONSOLE: |
| 173 | if (CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_CONSOLE)) { |
| 174 | char buf[FASTBOOT_RESPONSE_LEN] = { 0 }; |
| 175 | |
| 176 | if (console_record_isempty()) { |
| 177 | console_record_reset(); |
| 178 | fastboot_okay(NULL, response); |
| 179 | } else { |
| 180 | int ret = console_record_readline(buf, sizeof(buf) - 5); |
| 181 | |
| 182 | if (ret < 0) |
| 183 | fastboot_fail("Error reading console", response); |
| 184 | else |
| 185 | fastboot_response("INFO", response, "%s", buf); |
| 186 | } |
| 187 | break; |
| 188 | } |
Andre Przywara | 1e465ae | 2025-03-27 15:33:01 +0000 | [diff] [blame] | 189 | fallthrough; |
Ion Agorria | 1c38703 | 2024-01-05 09:22:06 +0200 | [diff] [blame] | 190 | default: |
| 191 | fastboot_fail("Unknown multiresponse command", response); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 196 | /** |
| 197 | * okay() - Send bare OKAY response |
| 198 | * |
| 199 | * @cmd_parameter: Pointer to command parameter |
| 200 | * @response: Pointer to fastboot response buffer |
| 201 | * |
| 202 | * Send a bare OKAY fastboot response. This is used where the command is |
| 203 | * valid, but all the work is done after the response has been sent (e.g. |
| 204 | * boot, reboot etc.) |
| 205 | */ |
| 206 | static void okay(char *cmd_parameter, char *response) |
| 207 | { |
| 208 | fastboot_okay(NULL, response); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * getvar() - Read a config/version variable |
| 213 | * |
| 214 | * @cmd_parameter: Pointer to command parameter |
| 215 | * @response: Pointer to fastboot response buffer |
| 216 | */ |
| 217 | static void getvar(char *cmd_parameter, char *response) |
| 218 | { |
| 219 | fastboot_getvar(cmd_parameter, response); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * fastboot_download() - Start a download transfer from the client |
| 224 | * |
| 225 | * @cmd_parameter: Pointer to command parameter |
| 226 | * @response: Pointer to fastboot response buffer |
| 227 | */ |
| 228 | static void download(char *cmd_parameter, char *response) |
| 229 | { |
| 230 | char *tmp; |
| 231 | |
| 232 | if (!cmd_parameter) { |
| 233 | fastboot_fail("Expected command parameter", response); |
| 234 | return; |
| 235 | } |
| 236 | fastboot_bytes_received = 0; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 237 | fastboot_bytes_expected = hextoul(cmd_parameter, &tmp); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 238 | if (fastboot_bytes_expected == 0) { |
| 239 | fastboot_fail("Expected nonzero image size", response); |
| 240 | return; |
| 241 | } |
| 242 | /* |
| 243 | * Nothing to download yet. Response is of the form: |
| 244 | * [DATA|FAIL]$cmd_parameter |
| 245 | * |
| 246 | * where cmd_parameter is an 8 digit hexadecimal number |
| 247 | */ |
| 248 | if (fastboot_bytes_expected > fastboot_buf_size) { |
| 249 | fastboot_fail(cmd_parameter, response); |
| 250 | } else { |
| 251 | printf("Starting download of %d bytes\n", |
| 252 | fastboot_bytes_expected); |
| 253 | fastboot_response("DATA", response, "%s", cmd_parameter); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * fastboot_data_remaining() - return bytes remaining in current transfer |
| 259 | * |
| 260 | * Return: Number of bytes left in the current download |
| 261 | */ |
| 262 | u32 fastboot_data_remaining(void) |
| 263 | { |
| 264 | return fastboot_bytes_expected - fastboot_bytes_received; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * fastboot_data_download() - Copy image data to fastboot_buf_addr. |
| 269 | * |
| 270 | * @fastboot_data: Pointer to received fastboot data |
| 271 | * @fastboot_data_len: Length of received fastboot data |
| 272 | * @response: Pointer to fastboot response buffer |
| 273 | * |
| 274 | * Copies image data from fastboot_data to fastboot_buf_addr. Writes to |
| 275 | * response. fastboot_bytes_received is updated to indicate the number |
| 276 | * of bytes that have been transferred. |
| 277 | * |
| 278 | * On completion sets image_size and ${filesize} to the total size of the |
| 279 | * downloaded image. |
| 280 | */ |
| 281 | void fastboot_data_download(const void *fastboot_data, |
| 282 | unsigned int fastboot_data_len, |
| 283 | char *response) |
| 284 | { |
| 285 | #define BYTES_PER_DOT 0x20000 |
| 286 | u32 pre_dot_num, now_dot_num; |
| 287 | |
| 288 | if (fastboot_data_len == 0 || |
| 289 | (fastboot_bytes_received + fastboot_data_len) > |
| 290 | fastboot_bytes_expected) { |
| 291 | fastboot_fail("Received invalid data length", |
| 292 | response); |
| 293 | return; |
| 294 | } |
| 295 | /* Download data to fastboot_buf_addr */ |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 296 | memcpy(fastboot_buf_addr + fastboot_bytes_received, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 297 | fastboot_data, fastboot_data_len); |
| 298 | |
| 299 | pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT; |
| 300 | fastboot_bytes_received += fastboot_data_len; |
| 301 | now_dot_num = fastboot_bytes_received / BYTES_PER_DOT; |
| 302 | |
| 303 | if (pre_dot_num != now_dot_num) { |
| 304 | putc('.'); |
| 305 | if (!(now_dot_num % 74)) |
| 306 | putc('\n'); |
| 307 | } |
| 308 | *response = '\0'; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * fastboot_data_complete() - Mark current transfer complete |
| 313 | * |
| 314 | * @response: Pointer to fastboot response buffer |
| 315 | * |
| 316 | * Set image_size and ${filesize} to the total size of the downloaded image. |
| 317 | */ |
| 318 | void fastboot_data_complete(char *response) |
| 319 | { |
| 320 | /* Download complete. Respond with "OKAY" */ |
| 321 | fastboot_okay(NULL, response); |
| 322 | printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received); |
| 323 | image_size = fastboot_bytes_received; |
| 324 | env_set_hex("filesize", image_size); |
| 325 | fastboot_bytes_expected = 0; |
| 326 | fastboot_bytes_received = 0; |
| 327 | } |
| 328 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 329 | /** |
| 330 | * flash() - write the downloaded image to the indicated partition. |
| 331 | * |
| 332 | * @cmd_parameter: Pointer to partition name |
| 333 | * @response: Pointer to fastboot response buffer |
| 334 | * |
| 335 | * Writes the previously downloaded image to the partition indicated by |
| 336 | * cmd_parameter. Writes to response. |
| 337 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 338 | static void __maybe_unused flash(char *cmd_parameter, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 339 | { |
Simon Glass | 091a071 | 2023-02-05 17:54:12 -0700 | [diff] [blame] | 340 | if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 341 | fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, |
| 342 | image_size, response); |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 343 | |
Simon Glass | 06799593 | 2023-02-05 17:54:13 -0700 | [diff] [blame] | 344 | if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND)) |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 345 | fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, |
| 346 | image_size, response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
| 350 | * erase() - erase the indicated partition. |
| 351 | * |
| 352 | * @cmd_parameter: Pointer to partition name |
| 353 | * @response: Pointer to fastboot response buffer |
| 354 | * |
| 355 | * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes |
| 356 | * to response. |
| 357 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 358 | static void __maybe_unused erase(char *cmd_parameter, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 359 | { |
Simon Glass | 091a071 | 2023-02-05 17:54:12 -0700 | [diff] [blame] | 360 | if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 361 | fastboot_mmc_erase(cmd_parameter, response); |
| 362 | |
Simon Glass | 06799593 | 2023-02-05 17:54:13 -0700 | [diff] [blame] | 363 | if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND)) |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 364 | fastboot_nand_erase(cmd_parameter, response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 365 | } |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 366 | |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 367 | /** |
| 368 | * run_ucmd() - Execute the UCmd command |
| 369 | * |
| 370 | * @cmd_parameter: Pointer to command parameter |
| 371 | * @response: Pointer to fastboot response buffer |
| 372 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 373 | static void __maybe_unused run_ucmd(char *cmd_parameter, char *response) |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 374 | { |
| 375 | if (!cmd_parameter) { |
| 376 | pr_err("missing slot suffix\n"); |
| 377 | fastboot_fail("missing command", response); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (run_command(cmd_parameter, 0)) |
| 382 | fastboot_fail("", response); |
| 383 | else |
| 384 | fastboot_okay(NULL, response); |
| 385 | } |
| 386 | |
| 387 | static char g_a_cmd_buff[64]; |
| 388 | |
| 389 | void fastboot_acmd_complete(void) |
| 390 | { |
| 391 | run_command(g_a_cmd_buff, 0); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * run_acmd() - Execute the ACmd command |
| 396 | * |
| 397 | * @cmd_parameter: Pointer to command parameter |
| 398 | * @response: Pointer to fastboot response buffer |
| 399 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 400 | static void __maybe_unused run_acmd(char *cmd_parameter, char *response) |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 401 | { |
| 402 | if (!cmd_parameter) { |
| 403 | pr_err("missing slot suffix\n"); |
| 404 | fastboot_fail("missing command", response); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | if (strlen(cmd_parameter) > sizeof(g_a_cmd_buff)) { |
| 409 | pr_err("too long command\n"); |
| 410 | fastboot_fail("too long command", response); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | strcpy(g_a_cmd_buff, cmd_parameter); |
| 415 | fastboot_okay(NULL, response); |
| 416 | } |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 417 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 418 | /** |
| 419 | * reboot_bootloader() - Sets reboot bootloader flag. |
| 420 | * |
| 421 | * @cmd_parameter: Pointer to command parameter |
| 422 | * @response: Pointer to fastboot response buffer |
| 423 | */ |
| 424 | static void reboot_bootloader(char *cmd_parameter, char *response) |
| 425 | { |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 426 | if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER)) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 427 | fastboot_fail("Cannot set reboot flag", response); |
| 428 | else |
| 429 | fastboot_okay(NULL, response); |
| 430 | } |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 431 | |
Roman Kovalivskyi | b30b97b | 2020-07-28 23:35:33 +0300 | [diff] [blame] | 432 | /** |
| 433 | * reboot_fastbootd() - Sets reboot fastboot flag. |
| 434 | * |
| 435 | * @cmd_parameter: Pointer to command parameter |
| 436 | * @response: Pointer to fastboot response buffer |
| 437 | */ |
| 438 | static void reboot_fastbootd(char *cmd_parameter, char *response) |
| 439 | { |
| 440 | if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD)) |
| 441 | fastboot_fail("Cannot set fastboot flag", response); |
| 442 | else |
| 443 | fastboot_okay(NULL, response); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * reboot_recovery() - Sets reboot recovery flag. |
| 448 | * |
| 449 | * @cmd_parameter: Pointer to command parameter |
| 450 | * @response: Pointer to fastboot response buffer |
| 451 | */ |
| 452 | static void reboot_recovery(char *cmd_parameter, char *response) |
| 453 | { |
| 454 | if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY)) |
| 455 | fastboot_fail("Cannot set recovery flag", response); |
| 456 | else |
| 457 | fastboot_okay(NULL, response); |
| 458 | } |
| 459 | |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 460 | /** |
| 461 | * oem_format() - Execute the OEM format command |
| 462 | * |
| 463 | * @cmd_parameter: Pointer to command parameter |
| 464 | * @response: Pointer to fastboot response buffer |
| 465 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 466 | static void __maybe_unused oem_format(char *cmd_parameter, char *response) |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 467 | { |
| 468 | char cmdbuf[32]; |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 469 | const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC, |
| 470 | CONFIG_FASTBOOT_FLASH_MMC_DEV, -1); |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 471 | |
| 472 | if (!env_get("partitions")) { |
| 473 | fastboot_fail("partitions not set", response); |
| 474 | } else { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 475 | sprintf(cmdbuf, "gpt write mmc %x $partitions", mmc_dev); |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 476 | if (run_command(cmdbuf, 0)) |
| 477 | fastboot_fail("", response); |
| 478 | else |
| 479 | fastboot_okay(NULL, response); |
| 480 | } |
| 481 | } |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 482 | |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 483 | /** |
| 484 | * oem_partconf() - Execute the OEM partconf command |
| 485 | * |
| 486 | * @cmd_parameter: Pointer to command parameter |
| 487 | * @response: Pointer to fastboot response buffer |
| 488 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 489 | static void __maybe_unused oem_partconf(char *cmd_parameter, char *response) |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 490 | { |
| 491 | char cmdbuf[32]; |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 492 | const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC, |
| 493 | CONFIG_FASTBOOT_FLASH_MMC_DEV, -1); |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 494 | |
| 495 | if (!cmd_parameter) { |
| 496 | fastboot_fail("Expected command parameter", response); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | /* execute 'mmc partconfg' command with cmd_parameter arguments*/ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 501 | snprintf(cmdbuf, sizeof(cmdbuf), "mmc partconf %x %s 0", mmc_dev, cmd_parameter); |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 502 | printf("Execute: %s\n", cmdbuf); |
| 503 | if (run_command(cmdbuf, 0)) |
| 504 | fastboot_fail("Cannot set oem partconf", response); |
| 505 | else |
| 506 | fastboot_okay(NULL, response); |
| 507 | } |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 508 | |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 509 | /** |
| 510 | * oem_bootbus() - Execute the OEM bootbus command |
| 511 | * |
| 512 | * @cmd_parameter: Pointer to command parameter |
| 513 | * @response: Pointer to fastboot response buffer |
| 514 | */ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 515 | static void __maybe_unused oem_bootbus(char *cmd_parameter, char *response) |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 516 | { |
| 517 | char cmdbuf[32]; |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 518 | const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC, |
| 519 | CONFIG_FASTBOOT_FLASH_MMC_DEV, -1); |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 520 | |
| 521 | if (!cmd_parameter) { |
| 522 | fastboot_fail("Expected command parameter", response); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | /* execute 'mmc bootbus' command with cmd_parameter arguments*/ |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 527 | snprintf(cmdbuf, sizeof(cmdbuf), "mmc bootbus %x %s", mmc_dev, cmd_parameter); |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 528 | printf("Execute: %s\n", cmdbuf); |
| 529 | if (run_command(cmdbuf, 0)) |
| 530 | fastboot_fail("Cannot set oem bootbus", response); |
| 531 | else |
| 532 | fastboot_okay(NULL, response); |
| 533 | } |
Ion Agorria | e64262d | 2024-01-05 09:22:11 +0200 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * oem_console() - Execute the OEM console command |
| 537 | * |
| 538 | * @cmd_parameter: Pointer to command parameter |
| 539 | * @response: Pointer to fastboot response buffer |
| 540 | */ |
| 541 | static void __maybe_unused oem_console(char *cmd_parameter, char *response) |
| 542 | { |
| 543 | if (cmd_parameter) |
| 544 | console_in_puts(cmd_parameter); |
| 545 | |
| 546 | if (console_record_isempty()) |
| 547 | fastboot_fail("Empty console", response); |
| 548 | else |
| 549 | fastboot_response(FASTBOOT_MULTIRESPONSE_START, response, NULL); |
| 550 | } |
Alexey Romanov | a0ae790 | 2024-04-18 13:01:29 +0300 | [diff] [blame] | 551 | |
| 552 | /** |
| 553 | * fastboot_oem_board() - Execute the OEM board command. This is default |
| 554 | * weak implementation, which may be overwritten in board/ files. |
| 555 | * |
| 556 | * @cmd_parameter: Pointer to command parameter |
| 557 | * @data: Pointer to fastboot input buffer |
| 558 | * @size: Size of the fastboot input buffer |
| 559 | * @response: Pointer to fastboot response buffer |
| 560 | */ |
| 561 | void __weak fastboot_oem_board(char *cmd_parameter, void *data, u32 size, char *response) |
| 562 | { |
| 563 | fastboot_fail("oem board function not defined", response); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * oem_board() - Execute the OEM board command |
| 568 | * |
| 569 | * @cmd_parameter: Pointer to command parameter |
| 570 | * @response: Pointer to fastboot response buffer |
| 571 | */ |
| 572 | static void __maybe_unused oem_board(char *cmd_parameter, char *response) |
| 573 | { |
| 574 | fastboot_oem_board(cmd_parameter, (void *)fastboot_buf_addr, image_size, response); |
| 575 | } |