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