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