Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2008 - 2009 |
| 4 | * Windriver, <www.windriver.com> |
| 5 | * Tom Rix <Tom.Rix@windriver.com> |
| 6 | * |
| 7 | * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
| 9 | * Copyright 2014 Linaro, Ltd. |
| 10 | * Rob Herring <robh@kernel.org> |
| 11 | */ |
| 12 | |
Roman Kovalivskyi | 13240bc | 2021-01-26 22:54:56 +0200 | [diff] [blame] | 13 | #include <bcb.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 14 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 15 | #include <env.h> |
Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 16 | #include <fastboot.h> |
Dmitrii Merkurev | 0788290 | 2023-04-12 19:49:31 +0100 | [diff] [blame] | 17 | #include <net.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 18 | #include <vsprintf.h> |
Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 21 | * fastboot_buf_addr - base address of the fastboot download buffer |
| 22 | */ |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 23 | void *fastboot_buf_addr; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * fastboot_buf_size - size of the fastboot download buffer |
| 27 | */ |
| 28 | u32 fastboot_buf_size; |
| 29 | |
| 30 | /** |
| 31 | * fastboot_progress_callback - callback executed during long operations |
| 32 | */ |
| 33 | void (*fastboot_progress_callback)(const char *msg); |
| 34 | |
| 35 | /** |
Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 36 | * fastboot_response() - Writes a response of the form "$tag$reason". |
| 37 | * |
| 38 | * @tag: The first part of the response |
| 39 | * @response: Pointer to fastboot response buffer |
| 40 | * @format: printf style format string |
| 41 | */ |
| 42 | void fastboot_response(const char *tag, char *response, |
| 43 | const char *format, ...) |
| 44 | { |
| 45 | va_list args; |
| 46 | |
| 47 | strlcpy(response, tag, FASTBOOT_RESPONSE_LEN); |
| 48 | if (format) { |
| 49 | va_start(args, format); |
| 50 | vsnprintf(response + strlen(response), |
| 51 | FASTBOOT_RESPONSE_LEN - strlen(response) - 1, |
| 52 | format, args); |
| 53 | va_end(args); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * fastboot_fail() - Write a FAIL response of the form "FAIL$reason". |
| 59 | * |
| 60 | * @reason: Pointer to returned reason string |
| 61 | * @response: Pointer to fastboot response buffer |
| 62 | */ |
| 63 | void fastboot_fail(const char *reason, char *response) |
| 64 | { |
| 65 | fastboot_response("FAIL", response, "%s", reason); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * fastboot_okay() - Write an OKAY response of the form "OKAY$reason". |
| 70 | * |
| 71 | * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY" |
| 72 | * @response: Pointer to fastboot response buffer |
| 73 | */ |
| 74 | void fastboot_okay(const char *reason, char *response) |
| 75 | { |
| 76 | if (reason) |
| 77 | fastboot_response("OKAY", response, "%s", reason); |
| 78 | else |
| 79 | fastboot_response("OKAY", response, NULL); |
| 80 | } |
Alex Kiernan | 5512c43 | 2018-05-29 15:30:46 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader |
| 84 | * |
| 85 | * Set flag which indicates that we should reboot into the bootloader |
| 86 | * following the reboot that fastboot executes after this function. |
| 87 | * |
| 88 | * This function should be overridden in your board file with one |
| 89 | * which sets whatever flag your board specific Android bootloader flow |
| 90 | * requires in order to re-enter the bootloader. |
| 91 | */ |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 92 | int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) |
Alex Kiernan | 5512c43 | 2018-05-29 15:30:46 +0000 | [diff] [blame] | 93 | { |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 94 | int ret; |
Roman Kovalivskyi | 13240bc | 2021-01-26 22:54:56 +0200 | [diff] [blame] | 95 | static const char * const boot_cmds[] = { |
| 96 | [FASTBOOT_REBOOT_REASON_BOOTLOADER] = "bootonce-bootloader", |
| 97 | [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot", |
| 98 | [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery" |
| 99 | }; |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 100 | const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC, |
| 101 | CONFIG_FASTBOOT_FLASH_MMC_DEV, -1); |
| 102 | |
Simon Glass | 091a071 | 2023-02-05 17:54:12 -0700 | [diff] [blame] | 103 | if (!IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 104 | return -EINVAL; |
Roman Kovalivskyi | 13240bc | 2021-01-26 22:54:56 +0200 | [diff] [blame] | 105 | |
| 106 | if (reason >= FASTBOOT_REBOOT_REASONS_COUNT) |
| 107 | return -EINVAL; |
| 108 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 109 | ret = bcb_find_partition_and_load("mmc", mmc_dev, "misc"); |
| 110 | if (ret) |
| 111 | goto out; |
| 112 | |
| 113 | ret = bcb_set(BCB_FIELD_COMMAND, boot_cmds[reason]); |
| 114 | if (ret) |
| 115 | goto out; |
| 116 | |
| 117 | ret = bcb_store(); |
| 118 | out: |
| 119 | bcb_reset(); |
| 120 | return ret; |
Alex Kiernan | 5512c43 | 2018-05-29 15:30:46 +0000 | [diff] [blame] | 121 | } |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * fastboot_get_progress_callback() - Return progress callback |
| 125 | * |
| 126 | * Return: Pointer to function called during long operations |
| 127 | */ |
| 128 | void (*fastboot_get_progress_callback(void))(const char *) |
| 129 | { |
| 130 | return fastboot_progress_callback; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * fastboot_boot() - Execute fastboot boot command |
| 135 | * |
| 136 | * If ${fastboot_bootcmd} is set, run that command to execute the boot |
| 137 | * process, if that returns, then exit the fastboot server and return |
| 138 | * control to the caller. |
| 139 | * |
| 140 | * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset |
| 141 | * the board. |
| 142 | */ |
| 143 | void fastboot_boot(void) |
| 144 | { |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 145 | char *s; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 146 | |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 147 | s = env_get("fastboot_bootcmd"); |
| 148 | if (s) { |
| 149 | run_command(s, CMD_FLAG_ENV); |
| 150 | } else if (IS_ENABLED(CONFIG_CMD_BOOTM)) { |
| 151 | static char boot_addr_start[20]; |
| 152 | static char *const bootm_args[] = { |
| 153 | "bootm", boot_addr_start, NULL |
| 154 | }; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 155 | |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 156 | snprintf(boot_addr_start, sizeof(boot_addr_start) - 1, |
| 157 | "0x%p", fastboot_buf_addr); |
| 158 | printf("Booting kernel at %s...\n\n\n", boot_addr_start); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 159 | |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 160 | do_bootm(NULL, 0, 2, bootm_args); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * This only happens if image is somehow faulty so we start |
| 164 | * over. We deliberately leave this policy to the invocation |
| 165 | * of fastbootcmd if that's what's being run |
| 166 | */ |
| 167 | do_reset(NULL, 0, 0, NULL); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
Dmitrii Merkurev | 0788290 | 2023-04-12 19:49:31 +0100 | [diff] [blame] | 172 | * fastboot_handle_boot() - Shared implementation of system reaction to |
| 173 | * fastboot commands |
| 174 | * |
| 175 | * Making desceisions about device boot state (stay in fastboot, reboot |
| 176 | * to bootloader, reboot to OS, etc). |
| 177 | */ |
| 178 | void fastboot_handle_boot(int command, bool success) |
| 179 | { |
| 180 | if (!success) |
| 181 | return; |
| 182 | |
| 183 | switch (command) { |
| 184 | case FASTBOOT_COMMAND_BOOT: |
| 185 | fastboot_boot(); |
| 186 | net_set_state(NETLOOP_SUCCESS); |
| 187 | break; |
| 188 | |
| 189 | case FASTBOOT_COMMAND_CONTINUE: |
| 190 | net_set_state(NETLOOP_SUCCESS); |
| 191 | break; |
| 192 | |
| 193 | case FASTBOOT_COMMAND_REBOOT: |
| 194 | case FASTBOOT_COMMAND_REBOOT_BOOTLOADER: |
| 195 | case FASTBOOT_COMMAND_REBOOT_FASTBOOTD: |
| 196 | case FASTBOOT_COMMAND_REBOOT_RECOVERY: |
| 197 | do_reset(NULL, 0, 0, NULL); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 203 | * fastboot_set_progress_callback() - set progress callback |
| 204 | * |
| 205 | * @progress: Pointer to progress callback |
| 206 | * |
| 207 | * Set a callback which is invoked periodically during long running operations |
| 208 | * (flash and erase). This can be used (for example) by the UDP transport to |
| 209 | * send INFO responses to keep the client alive whilst those commands are |
| 210 | * executing. |
| 211 | */ |
| 212 | void fastboot_set_progress_callback(void (*progress)(const char *msg)) |
| 213 | { |
| 214 | fastboot_progress_callback = progress; |
| 215 | } |
| 216 | |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 217 | /* |
| 218 | * fastboot_init() - initialise new fastboot protocol session |
| 219 | * |
| 220 | * @buf_addr: Pointer to download buffer, or NULL for default |
| 221 | * @buf_size: Size of download buffer, or zero for default |
| 222 | */ |
| 223 | void fastboot_init(void *buf_addr, u32 buf_size) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 224 | { |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 225 | fastboot_buf_addr = buf_addr ? buf_addr : |
| 226 | (void *)CONFIG_FASTBOOT_BUF_ADDR; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 227 | fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE; |
| 228 | fastboot_set_progress_callback(NULL); |
| 229 | } |