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