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