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