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 | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 6 | #include <env.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 7 | #include <fastboot.h> |
| 8 | #include <fastboot-internal.h> |
| 9 | #include <fb_mmc.h> |
| 10 | #include <fb_nand.h> |
| 11 | #include <fs.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 12 | #include <part.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 13 | #include <version.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 14 | #include <vsprintf.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 15 | #include <linux/printk.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 16 | |
| 17 | static void getvar_version(char *var_parameter, char *response); |
Sam Protsenko | ac21e74 | 2019-07-03 19:34:07 +0300 | [diff] [blame] | 18 | static void getvar_version_bootloader(char *var_parameter, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 19 | static void getvar_downloadsize(char *var_parameter, char *response); |
| 20 | static void getvar_serialno(char *var_parameter, char *response); |
| 21 | static void getvar_version_baseband(char *var_parameter, char *response); |
| 22 | static void getvar_product(char *var_parameter, char *response); |
Eugeniu Rosca | fb614d4 | 2019-04-09 21:11:40 +0200 | [diff] [blame] | 23 | static void getvar_platform(char *var_parameter, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 24 | static void getvar_current_slot(char *var_parameter, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 25 | static void getvar_has_slot(char *var_parameter, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 26 | static void getvar_partition_type(char *part_name, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 27 | static void getvar_partition_size(char *part_name, char *response); |
Sam Protsenko | 0d74825 | 2019-07-03 19:00:22 +0300 | [diff] [blame] | 28 | static void getvar_is_userspace(char *var_parameter, char *response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 29 | |
| 30 | static const struct { |
| 31 | const char *variable; |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 32 | bool list; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 33 | void (*dispatch)(char *var_parameter, char *response); |
| 34 | } getvar_dispatch[] = { |
| 35 | { |
| 36 | .variable = "version", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 37 | .dispatch = getvar_version, |
| 38 | .list = true, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 39 | }, { |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 40 | .variable = "version-bootloader", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 41 | .dispatch = getvar_version_bootloader, |
| 42 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 43 | }, { |
| 44 | .variable = "downloadsize", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 45 | .dispatch = getvar_downloadsize, |
| 46 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 47 | }, { |
| 48 | .variable = "max-download-size", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 49 | .dispatch = getvar_downloadsize, |
| 50 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 51 | }, { |
| 52 | .variable = "serialno", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 53 | .dispatch = getvar_serialno, |
| 54 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 55 | }, { |
| 56 | .variable = "version-baseband", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 57 | .dispatch = getvar_version_baseband, |
| 58 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 59 | }, { |
| 60 | .variable = "product", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 61 | .dispatch = getvar_product, |
| 62 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 63 | }, { |
Eugeniu Rosca | fb614d4 | 2019-04-09 21:11:40 +0200 | [diff] [blame] | 64 | .variable = "platform", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 65 | .dispatch = getvar_platform, |
| 66 | .list = true |
Eugeniu Rosca | fb614d4 | 2019-04-09 21:11:40 +0200 | [diff] [blame] | 67 | }, { |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 68 | .variable = "current-slot", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 69 | .dispatch = getvar_current_slot, |
| 70 | .list = true |
Simon Glass | 3eaba079 | 2023-02-05 15:39:53 -0700 | [diff] [blame] | 71 | #if IS_ENABLED(CONFIG_FASTBOOT_FLASH) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 72 | }, { |
Eugeniu Rosca | 2ef97cf | 2019-03-26 17:46:14 +0100 | [diff] [blame] | 73 | .variable = "has-slot", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 74 | .dispatch = getvar_has_slot, |
| 75 | .list = false |
Igor Opaniuk | 73dac9b | 2019-06-13 21:11:09 +0300 | [diff] [blame] | 76 | #endif |
Simon Glass | 091a071 | 2023-02-05 17:54:12 -0700 | [diff] [blame] | 77 | #if IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 78 | }, { |
| 79 | .variable = "partition-type", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 80 | .dispatch = getvar_partition_type, |
| 81 | .list = false |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 82 | #endif |
Simon Glass | 3eaba079 | 2023-02-05 15:39:53 -0700 | [diff] [blame] | 83 | #if IS_ENABLED(CONFIG_FASTBOOT_FLASH) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 84 | }, { |
| 85 | .variable = "partition-size", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 86 | .dispatch = getvar_partition_size, |
| 87 | .list = false |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 88 | #endif |
Sam Protsenko | 0d74825 | 2019-07-03 19:00:22 +0300 | [diff] [blame] | 89 | }, { |
| 90 | .variable = "is-userspace", |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 91 | .dispatch = getvar_is_userspace, |
| 92 | .list = true |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 96 | /** |
| 97 | * Get partition number and size for any storage type. |
| 98 | * |
| 99 | * Can be used to check if partition with specified name exists. |
| 100 | * |
| 101 | * If error occurs, this function guarantees to fill @p response with fail |
| 102 | * string. @p response can be rewritten in caller, if needed. |
| 103 | * |
| 104 | * @param[in] part_name Info for which partition name to look for |
| 105 | * @param[in,out] response Pointer to fastboot response buffer |
Gary Bisson | f867428 | 2020-08-27 10:51:14 +0200 | [diff] [blame] | 106 | * @param[out] size If not NULL, will contain partition size |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 107 | * Return: Partition number or negative value on error |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 108 | */ |
| 109 | static int getvar_get_part_info(const char *part_name, char *response, |
| 110 | size_t *size) |
| 111 | { |
| 112 | int r; |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 113 | struct blk_desc *dev_desc; |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 114 | struct disk_partition disk_part; |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 115 | struct part_info *part_info; |
| 116 | |
Simon Glass | 091a071 | 2023-02-05 17:54:12 -0700 | [diff] [blame] | 117 | if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 118 | r = fastboot_mmc_get_part_info(part_name, &dev_desc, &disk_part, |
| 119 | response); |
| 120 | if (r >= 0 && size) |
| 121 | *size = disk_part.size * disk_part.blksz; |
Simon Glass | 06799593 | 2023-02-05 17:54:13 -0700 | [diff] [blame] | 122 | } else if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND)) { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 123 | r = fastboot_nand_get_part_info(part_name, &part_info, response); |
| 124 | if (r >= 0 && size) |
| 125 | *size = part_info->size; |
| 126 | } else { |
| 127 | fastboot_fail("this storage is not supported in bootloader", response); |
| 128 | r = -ENODEV; |
| 129 | } |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 130 | |
| 131 | return r; |
| 132 | } |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 133 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 134 | static void getvar_version(char *var_parameter, char *response) |
| 135 | { |
| 136 | fastboot_okay(FASTBOOT_VERSION, response); |
| 137 | } |
| 138 | |
Sam Protsenko | ac21e74 | 2019-07-03 19:34:07 +0300 | [diff] [blame] | 139 | static void getvar_version_bootloader(char *var_parameter, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 140 | { |
| 141 | fastboot_okay(U_BOOT_VERSION, response); |
| 142 | } |
| 143 | |
| 144 | static void getvar_downloadsize(char *var_parameter, char *response) |
| 145 | { |
| 146 | fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size); |
| 147 | } |
| 148 | |
| 149 | static void getvar_serialno(char *var_parameter, char *response) |
| 150 | { |
| 151 | const char *tmp = env_get("serial#"); |
| 152 | |
| 153 | if (tmp) |
| 154 | fastboot_okay(tmp, response); |
| 155 | else |
| 156 | fastboot_fail("Value not set", response); |
| 157 | } |
| 158 | |
| 159 | static void getvar_version_baseband(char *var_parameter, char *response) |
| 160 | { |
| 161 | fastboot_okay("N/A", response); |
| 162 | } |
| 163 | |
| 164 | static void getvar_product(char *var_parameter, char *response) |
| 165 | { |
| 166 | const char *board = env_get("board"); |
| 167 | |
| 168 | if (board) |
| 169 | fastboot_okay(board, response); |
| 170 | else |
| 171 | fastboot_fail("Board not set", response); |
| 172 | } |
| 173 | |
Eugeniu Rosca | fb614d4 | 2019-04-09 21:11:40 +0200 | [diff] [blame] | 174 | static void getvar_platform(char *var_parameter, char *response) |
| 175 | { |
| 176 | const char *p = env_get("platform"); |
| 177 | |
| 178 | if (p) |
| 179 | fastboot_okay(p, response); |
| 180 | else |
| 181 | fastboot_fail("platform not set", response); |
| 182 | } |
| 183 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 184 | static void getvar_current_slot(char *var_parameter, char *response) |
| 185 | { |
Sam Protsenko | fd7467e | 2019-06-13 00:49:45 +0300 | [diff] [blame] | 186 | /* A/B not implemented, for now always return "a" */ |
| 187 | fastboot_okay("a", response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 190 | static void __maybe_unused getvar_has_slot(char *part_name, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 191 | { |
Igor Opaniuk | 73dac9b | 2019-06-13 21:11:09 +0300 | [diff] [blame] | 192 | char part_name_wslot[PART_NAME_LEN]; |
| 193 | size_t len; |
| 194 | int r; |
| 195 | |
| 196 | if (!part_name || part_name[0] == '\0') |
| 197 | goto fail; |
| 198 | |
| 199 | /* part_name_wslot = part_name + "_a" */ |
| 200 | len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3); |
Matthias Schiffer | 02482ee | 2023-07-14 13:24:50 +0200 | [diff] [blame] | 201 | if (len >= PART_NAME_LEN - 3) |
Igor Opaniuk | 73dac9b | 2019-06-13 21:11:09 +0300 | [diff] [blame] | 202 | goto fail; |
| 203 | strcat(part_name_wslot, "_a"); |
| 204 | |
| 205 | r = getvar_get_part_info(part_name_wslot, response, NULL); |
| 206 | if (r >= 0) { |
| 207 | fastboot_okay("yes", response); /* part exists and slotted */ |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | r = getvar_get_part_info(part_name, response, NULL); |
| 212 | if (r >= 0) |
| 213 | fastboot_okay("no", response); /* part exists but not slotted */ |
| 214 | |
| 215 | /* At this point response is filled with okay or fail string */ |
| 216 | return; |
| 217 | |
| 218 | fail: |
| 219 | fastboot_fail("invalid partition name", response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 222 | static void __maybe_unused getvar_partition_type(char *part_name, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 223 | { |
| 224 | int r; |
| 225 | struct blk_desc *dev_desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 226 | struct disk_partition part_info; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 227 | |
| 228 | r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info, |
| 229 | response); |
| 230 | if (r >= 0) { |
| 231 | r = fs_set_blk_dev_with_part(dev_desc, r); |
| 232 | if (r < 0) |
| 233 | fastboot_fail("failed to set partition", response); |
| 234 | else |
| 235 | fastboot_okay(fs_get_type_name(), response); |
| 236 | } |
| 237 | } |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 238 | |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 239 | static void __maybe_unused getvar_partition_size(char *part_name, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 240 | { |
| 241 | int r; |
| 242 | size_t size; |
| 243 | |
Sam Protsenko | 6646bbd | 2019-06-13 21:11:08 +0300 | [diff] [blame] | 244 | r = getvar_get_part_info(part_name, response, &size); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 245 | if (r >= 0) |
| 246 | fastboot_response("OKAY", response, "0x%016zx", size); |
| 247 | } |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 248 | |
Sam Protsenko | 0d74825 | 2019-07-03 19:00:22 +0300 | [diff] [blame] | 249 | static void getvar_is_userspace(char *var_parameter, char *response) |
| 250 | { |
| 251 | fastboot_okay("no", response); |
| 252 | } |
| 253 | |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 254 | static int current_all_dispatch; |
| 255 | void fastboot_getvar_all(char *response) |
| 256 | { |
| 257 | /* |
| 258 | * Find a dispatch getvar that can be listed and send |
| 259 | * it as INFO until we reach the end. |
| 260 | */ |
| 261 | while (current_all_dispatch < ARRAY_SIZE(getvar_dispatch)) { |
| 262 | if (!getvar_dispatch[current_all_dispatch].list) { |
| 263 | current_all_dispatch++; |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | char envstr[FASTBOOT_RESPONSE_LEN] = { 0 }; |
| 268 | |
| 269 | getvar_dispatch[current_all_dispatch].dispatch(NULL, envstr); |
| 270 | |
| 271 | char *envstr_start = envstr; |
| 272 | |
| 273 | if (!strncmp("OKAY", envstr, 4) || !strncmp("FAIL", envstr, 4)) |
| 274 | envstr_start += 4; |
| 275 | |
| 276 | fastboot_response("INFO", response, "%s: %s", |
| 277 | getvar_dispatch[current_all_dispatch].variable, |
| 278 | envstr_start); |
| 279 | |
| 280 | current_all_dispatch++; |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | fastboot_response("OKAY", response, NULL); |
| 285 | current_all_dispatch = 0; |
| 286 | } |
| 287 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 288 | /** |
| 289 | * fastboot_getvar() - Writes variable indicated by cmd_parameter to response. |
| 290 | * |
| 291 | * @cmd_parameter: Pointer to command parameter |
| 292 | * @response: Pointer to fastboot response buffer |
| 293 | * |
| 294 | * Look up cmd_parameter first as an environment variable of the form |
| 295 | * fastboot.<cmd_parameter>, if that exists return use its value to set |
| 296 | * response. |
| 297 | * |
| 298 | * Otherwise lookup the name of variable and execute the appropriate |
| 299 | * function to return the requested value. |
| 300 | */ |
| 301 | void fastboot_getvar(char *cmd_parameter, char *response) |
| 302 | { |
| 303 | if (!cmd_parameter) { |
| 304 | fastboot_fail("missing var", response); |
Ion Agorria | 99fc645 | 2024-01-05 09:22:07 +0200 | [diff] [blame] | 305 | } else if (!strncmp("all", cmd_parameter, 3) && strlen(cmd_parameter) == 3) { |
| 306 | current_all_dispatch = 0; |
| 307 | fastboot_response(FASTBOOT_MULTIRESPONSE_START, response, NULL); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 308 | } else { |
| 309 | #define FASTBOOT_ENV_PREFIX "fastboot." |
| 310 | int i; |
| 311 | char *var_parameter = cmd_parameter; |
| 312 | char envstr[FASTBOOT_RESPONSE_LEN]; |
| 313 | const char *s; |
| 314 | |
| 315 | snprintf(envstr, sizeof(envstr) - 1, |
| 316 | FASTBOOT_ENV_PREFIX "%s", cmd_parameter); |
| 317 | s = env_get(envstr); |
| 318 | if (s) { |
| 319 | fastboot_response("OKAY", response, "%s", s); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | strsep(&var_parameter, ":"); |
| 324 | for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) { |
| 325 | if (!strcmp(getvar_dispatch[i].variable, |
| 326 | cmd_parameter)) { |
| 327 | getvar_dispatch[i].dispatch(var_parameter, |
| 328 | response); |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | pr_warn("WARNING: unknown variable: %s\n", cmd_parameter); |
| 333 | fastboot_fail("Variable not implemented", response); |
| 334 | } |
| 335 | } |