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