Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 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> |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 11 | */ |
| 12 | #ifndef _FASTBOOT_H_ |
| 13 | #define _FASTBOOT_H_ |
| 14 | |
Alex Kiernan | 1df9890 | 2018-05-29 15:30:47 +0000 | [diff] [blame] | 15 | #define FASTBOOT_VERSION "0.4" |
| 16 | |
Ion Agorria | 1c38703 | 2024-01-05 09:22:06 +0200 | [diff] [blame] | 17 | /* |
| 18 | * Signals u-boot fastboot code to send multiple responses by |
| 19 | * calling response generating function repeatedly until a OKAY/FAIL |
| 20 | * is generated as final response. |
| 21 | * |
| 22 | * This status code is only used internally to signal, must NOT |
| 23 | * be sent to host. |
| 24 | */ |
| 25 | #define FASTBOOT_MULTIRESPONSE_START ("MORE") |
| 26 | |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 27 | /* The 64 defined bytes plus \0 */ |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 28 | #define FASTBOOT_COMMAND_LEN (64 + 1) |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 29 | #define FASTBOOT_RESPONSE_LEN (64 + 1) |
| 30 | |
Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 31 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 32 | * All known commands to fastboot |
| 33 | */ |
| 34 | enum { |
| 35 | FASTBOOT_COMMAND_GETVAR = 0, |
| 36 | FASTBOOT_COMMAND_DOWNLOAD, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 37 | FASTBOOT_COMMAND_FLASH, |
| 38 | FASTBOOT_COMMAND_ERASE, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 39 | FASTBOOT_COMMAND_BOOT, |
| 40 | FASTBOOT_COMMAND_CONTINUE, |
| 41 | FASTBOOT_COMMAND_REBOOT, |
| 42 | FASTBOOT_COMMAND_REBOOT_BOOTLOADER, |
Roman Kovalivskyi | b30b97b | 2020-07-28 23:35:33 +0300 | [diff] [blame] | 43 | FASTBOOT_COMMAND_REBOOT_FASTBOOTD, |
| 44 | FASTBOOT_COMMAND_REBOOT_RECOVERY, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 45 | FASTBOOT_COMMAND_SET_ACTIVE, |
Alex Kiernan | c86cde9 | 2018-05-29 15:30:54 +0000 | [diff] [blame] | 46 | FASTBOOT_COMMAND_OEM_FORMAT, |
Patrick Delaunay | 6168770 | 2021-01-27 14:46:48 +0100 | [diff] [blame] | 47 | FASTBOOT_COMMAND_OEM_PARTCONF, |
Patrick Delaunay | 67af29a | 2021-01-27 14:46:49 +0100 | [diff] [blame] | 48 | FASTBOOT_COMMAND_OEM_BOOTBUS, |
Sean Anderson | 421bec0 | 2022-12-16 13:20:16 -0500 | [diff] [blame] | 49 | FASTBOOT_COMMAND_OEM_RUN, |
Ion Agorria | e64262d | 2024-01-05 09:22:11 +0200 | [diff] [blame] | 50 | FASTBOOT_COMMAND_OEM_CONSOLE, |
Alexey Romanov | a0ae790 | 2024-04-18 13:01:29 +0300 | [diff] [blame] | 51 | FASTBOOT_COMMAND_OEM_BOARD, |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 52 | FASTBOOT_COMMAND_ACMD, |
| 53 | FASTBOOT_COMMAND_UCMD, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 54 | FASTBOOT_COMMAND_COUNT |
| 55 | }; |
| 56 | |
| 57 | /** |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 58 | * Reboot reasons |
| 59 | */ |
| 60 | enum fastboot_reboot_reason { |
| 61 | FASTBOOT_REBOOT_REASON_BOOTLOADER, |
Roman Kovalivskyi | b30b97b | 2020-07-28 23:35:33 +0300 | [diff] [blame] | 62 | FASTBOOT_REBOOT_REASON_FASTBOOTD, |
| 63 | FASTBOOT_REBOOT_REASON_RECOVERY, |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 64 | FASTBOOT_REBOOT_REASONS_COUNT |
| 65 | }; |
| 66 | |
| 67 | /** |
Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 68 | * fastboot_response() - Writes a response of the form "$tag$reason". |
| 69 | * |
| 70 | * @tag: The first part of the response |
| 71 | * @response: Pointer to fastboot response buffer |
| 72 | * @format: printf style format string |
| 73 | */ |
| 74 | void fastboot_response(const char *tag, char *response, |
| 75 | const char *format, ...) |
| 76 | __attribute__ ((format (__printf__, 3, 4))); |
| 77 | |
| 78 | /** |
| 79 | * fastboot_fail() - Write a FAIL response of the form "FAIL$reason". |
| 80 | * |
| 81 | * @reason: Pointer to returned reason string |
| 82 | * @response: Pointer to fastboot response buffer |
| 83 | */ |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 84 | void fastboot_fail(const char *reason, char *response); |
Alex Kiernan | ed6a4144 | 2018-05-29 15:30:41 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * fastboot_okay() - Write an OKAY response of the form "OKAY$reason". |
| 88 | * |
| 89 | * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY" |
| 90 | * @response: Pointer to fastboot response buffer |
| 91 | */ |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 92 | void fastboot_okay(const char *reason, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader |
| 96 | * |
| 97 | * Set flag which indicates that we should reboot into the bootloader |
| 98 | * following the reboot that fastboot executes after this function. |
| 99 | * |
| 100 | * This function should be overridden in your board file with one |
| 101 | * which sets whatever flag your board specific Android bootloader flow |
| 102 | * requires in order to re-enter the bootloader. |
| 103 | */ |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 104 | int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * fastboot_set_progress_callback() - set progress callback |
| 108 | * |
| 109 | * @progress: Pointer to progress callback |
| 110 | * |
| 111 | * Set a callback which is invoked periodically during long running operations |
| 112 | * (flash and erase). This can be used (for example) by the UDP transport to |
| 113 | * send INFO responses to keep the client alive whilst those commands are |
| 114 | * executing. |
| 115 | */ |
| 116 | void fastboot_set_progress_callback(void (*progress)(const char *msg)); |
| 117 | |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 118 | /* |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 119 | * fastboot_init() - initialise new fastboot protocol session |
| 120 | * |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 121 | * @buf_addr: Pointer to download buffer, or NULL for default |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 122 | * @buf_size: Size of download buffer, or zero for default |
| 123 | */ |
Tom Rini | 793921e | 2024-04-18 08:29:35 -0600 | [diff] [blame] | 124 | void fastboot_init(void *buf_addr, u32 buf_size); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * fastboot_boot() - Execute fastboot boot command |
| 128 | * |
| 129 | * If ${fastboot_bootcmd} is set, run that command to execute the boot |
| 130 | * process, if that returns, then exit the fastboot server and return |
| 131 | * control to the caller. |
| 132 | * |
| 133 | * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset |
| 134 | * the board. |
| 135 | */ |
| 136 | void fastboot_boot(void); |
| 137 | |
| 138 | /** |
Dmitrii Merkurev | 0788290 | 2023-04-12 19:49:31 +0100 | [diff] [blame] | 139 | * fastboot_handle_boot() - Shared implementation of system reaction to |
| 140 | * fastboot commands |
| 141 | * |
| 142 | * Making desceisions about device boot state (stay in fastboot, reboot |
| 143 | * to bootloader, reboot to OS, etc). |
| 144 | */ |
| 145 | void fastboot_handle_boot(int command, bool success); |
| 146 | |
| 147 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 148 | * fastboot_handle_command() - Handle fastboot command |
| 149 | * |
| 150 | * @cmd_string: Pointer to command string |
| 151 | * @response: Pointer to fastboot response buffer |
| 152 | * |
| 153 | * Return: Executed command, or -1 if not recognized |
| 154 | */ |
| 155 | int fastboot_handle_command(char *cmd_string, char *response); |
| 156 | |
| 157 | /** |
| 158 | * fastboot_data_remaining() - return bytes remaining in current transfer |
| 159 | * |
| 160 | * Return: Number of bytes left in the current download |
| 161 | */ |
| 162 | u32 fastboot_data_remaining(void); |
| 163 | |
| 164 | /** |
| 165 | * fastboot_data_download() - Copy image data to fastboot_buf_addr. |
| 166 | * |
| 167 | * @fastboot_data: Pointer to received fastboot data |
| 168 | * @fastboot_data_len: Length of received fastboot data |
| 169 | * @response: Pointer to fastboot response buffer |
| 170 | * |
| 171 | * Copies image data from fastboot_data to fastboot_buf_addr. Writes to |
| 172 | * response. fastboot_bytes_received is updated to indicate the number |
| 173 | * of bytes that have been transferred. |
| 174 | */ |
| 175 | void fastboot_data_download(const void *fastboot_data, |
| 176 | unsigned int fastboot_data_len, char *response); |
| 177 | |
| 178 | /** |
| 179 | * fastboot_data_complete() - Mark current transfer complete |
| 180 | * |
| 181 | * @response: Pointer to fastboot response buffer |
| 182 | * |
| 183 | * Set image_size and ${filesize} to the total size of the downloaded image. |
| 184 | */ |
| 185 | void fastboot_data_complete(char *response); |
| 186 | |
Ion Agorria | 1c38703 | 2024-01-05 09:22:06 +0200 | [diff] [blame] | 187 | /** |
| 188 | * fastboot_handle_multiresponse() - Called for each response to send |
| 189 | * |
| 190 | * @cmd: Command id that requested multiresponse |
| 191 | * @response: Pointer to fastboot response buffer |
| 192 | */ |
| 193 | void fastboot_multiresponse(int cmd, char *response); |
| 194 | |
Heiko Schocher | 3a99448 | 2021-02-10 09:29:03 +0100 | [diff] [blame] | 195 | void fastboot_acmd_complete(void); |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 196 | #endif /* _FASTBOOT_H_ */ |