AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Integrate UEFI variables to u-boot env interface |
| 4 | * |
| 5 | * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited |
| 6 | */ |
| 7 | |
| 8 | #include <charset.h> |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <efi_loader.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 12 | #include <env.h> |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 13 | #include <exports.h> |
| 14 | #include <hexdump.h> |
| 15 | #include <malloc.h> |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 16 | #include <mapmem.h> |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | |
| 19 | /* |
| 20 | * From efi_variable.c, |
| 21 | * |
| 22 | * Mapping between UEFI variables and u-boot variables: |
| 23 | * |
| 24 | * efi_$guid_$varname = {attributes}(type)value |
| 25 | */ |
| 26 | |
| 27 | static const struct { |
| 28 | u32 mask; |
| 29 | char *text; |
| 30 | } efi_var_attrs[] = { |
| 31 | {EFI_VARIABLE_NON_VOLATILE, "NV"}, |
| 32 | {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"}, |
| 33 | {EFI_VARIABLE_RUNTIME_ACCESS, "RT"}, |
| 34 | {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"}, |
| 35 | {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"}, |
| 36 | }; |
| 37 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 38 | static const struct { |
| 39 | efi_guid_t guid; |
| 40 | char *text; |
| 41 | } efi_guid_text[] = { |
| 42 | /* signature database */ |
| 43 | {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"}, |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 44 | {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"}, |
| 45 | /* certificate type */ |
| 46 | {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"}, |
| 47 | {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"}, |
| 48 | {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"}, |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ |
| 52 | static char unknown_guid[37]; |
| 53 | |
| 54 | /** |
| 55 | * efi_guid_to_str() - convert guid to readable name |
| 56 | * |
| 57 | * @guid: GUID |
| 58 | * Return: string for GUID |
| 59 | * |
| 60 | * convert guid to readable name |
| 61 | */ |
| 62 | static const char *efi_guid_to_str(const efi_guid_t *guid) |
| 63 | { |
| 64 | int i; |
| 65 | |
| 66 | for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++) |
| 67 | if (!guidcmp(guid, &efi_guid_text[i].guid)) |
| 68 | return efi_guid_text[i].text; |
| 69 | |
| 70 | uuid_bin_to_str((unsigned char *)guid->b, unknown_guid, |
| 71 | UUID_STR_FORMAT_GUID); |
| 72 | |
| 73 | return unknown_guid; |
| 74 | } |
| 75 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 76 | /** |
| 77 | * efi_dump_single_var() - show information about a UEFI variable |
| 78 | * |
| 79 | * @name: Name of the variable |
| 80 | * @guid: Vendor GUID |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 81 | * @verbose: if true, dump data |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 82 | * |
| 83 | * Show information encoded in one UEFI variable |
| 84 | */ |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 85 | static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 86 | { |
| 87 | u32 attributes; |
| 88 | u8 *data; |
| 89 | efi_uintn_t size; |
| 90 | int count, i; |
| 91 | efi_status_t ret; |
| 92 | |
| 93 | data = NULL; |
| 94 | size = 0; |
| 95 | ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data)); |
| 96 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 97 | data = malloc(size); |
| 98 | if (!data) |
| 99 | goto out; |
| 100 | |
| 101 | ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, |
| 102 | data)); |
| 103 | } |
| 104 | if (ret == EFI_NOT_FOUND) { |
| 105 | printf("Error: \"%ls\" not defined\n", name); |
| 106 | goto out; |
| 107 | } |
| 108 | if (ret != EFI_SUCCESS) |
| 109 | goto out; |
| 110 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 111 | printf("%ls:\n %s:", name, efi_guid_to_str(guid)); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 112 | for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++) |
| 113 | if (attributes & efi_var_attrs[i].mask) { |
| 114 | if (count) |
| 115 | putc('|'); |
| 116 | else |
| 117 | putc(' '); |
| 118 | count++; |
| 119 | puts(efi_var_attrs[i].text); |
| 120 | } |
| 121 | printf(", DataSize = 0x%zx\n", size); |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 122 | if (verbose) |
| 123 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 124 | data, size, true); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 125 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 126 | out: |
| 127 | free(data); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * efi_dump_vars() - show information about named UEFI variables |
| 132 | * |
| 133 | * @argc: Number of arguments (variables) |
| 134 | * @argv: Argument (variable name) array |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 135 | * @verbose: if true, dump data |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 136 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
| 137 | * |
| 138 | * Show information encoded in named UEFI variables |
| 139 | */ |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 140 | static int efi_dump_vars(int argc, char * const argv[], |
| 141 | const efi_guid_t *guid, bool verbose) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 142 | { |
| 143 | u16 *var_name16, *p; |
| 144 | efi_uintn_t buf_size, size; |
| 145 | |
| 146 | buf_size = 128; |
| 147 | var_name16 = malloc(buf_size); |
| 148 | if (!var_name16) |
| 149 | return CMD_RET_FAILURE; |
| 150 | |
| 151 | for (; argc > 0; argc--, argv++) { |
| 152 | size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16); |
| 153 | if (buf_size < size) { |
| 154 | buf_size = size; |
| 155 | p = realloc(var_name16, buf_size); |
| 156 | if (!p) { |
| 157 | free(var_name16); |
| 158 | return CMD_RET_FAILURE; |
| 159 | } |
| 160 | var_name16 = p; |
| 161 | } |
| 162 | |
| 163 | p = var_name16; |
| 164 | utf8_utf16_strcpy(&p, argv[0]); |
| 165 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 166 | efi_dump_single_var(var_name16, guid, verbose); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | free(var_name16); |
| 170 | |
| 171 | return CMD_RET_SUCCESS; |
| 172 | } |
| 173 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 174 | static bool match_name(int argc, char * const argv[], u16 *var_name16) |
| 175 | { |
| 176 | char *buf, *p; |
| 177 | size_t buflen; |
| 178 | int i; |
| 179 | bool result = false; |
| 180 | |
| 181 | buflen = utf16_utf8_strlen(var_name16) + 1; |
| 182 | buf = calloc(1, buflen); |
| 183 | if (!buf) |
| 184 | return result; |
| 185 | |
| 186 | p = buf; |
| 187 | utf16_utf8_strcpy(&p, var_name16); |
| 188 | |
| 189 | for (i = 0; i < argc; argc--, argv++) { |
| 190 | if (!strcmp(buf, argv[i])) { |
| 191 | result = true; |
| 192 | goto out; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | out: |
| 197 | free(buf); |
| 198 | |
| 199 | return result; |
| 200 | } |
| 201 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 202 | /** |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 203 | * efi_dump_var_all() - show information about all the UEFI variables |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 204 | * |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 205 | * @argc: Number of arguments (variables) |
| 206 | * @argv: Argument (variable name) array |
| 207 | * @verbose: if true, dump data |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 208 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
| 209 | * |
| 210 | * Show information encoded in all the UEFI variables |
| 211 | */ |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 212 | static int efi_dump_var_all(int argc, char * const argv[], |
| 213 | const efi_guid_t *guid_p, bool verbose) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 214 | { |
| 215 | u16 *var_name16, *p; |
| 216 | efi_uintn_t buf_size, size; |
| 217 | efi_guid_t guid; |
| 218 | efi_status_t ret; |
| 219 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 220 | if (argc && guid_p) |
| 221 | /* simplified case */ |
| 222 | return efi_dump_vars(argc, argv, guid_p, verbose); |
| 223 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 224 | buf_size = 128; |
| 225 | var_name16 = malloc(buf_size); |
| 226 | if (!var_name16) |
| 227 | return CMD_RET_FAILURE; |
| 228 | |
| 229 | var_name16[0] = 0; |
| 230 | for (;;) { |
| 231 | size = buf_size; |
| 232 | ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16, |
| 233 | &guid)); |
| 234 | if (ret == EFI_NOT_FOUND) |
| 235 | break; |
| 236 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 237 | buf_size = size; |
| 238 | p = realloc(var_name16, buf_size); |
| 239 | if (!p) { |
| 240 | free(var_name16); |
| 241 | return CMD_RET_FAILURE; |
| 242 | } |
| 243 | var_name16 = p; |
| 244 | ret = EFI_CALL(efi_get_next_variable_name(&size, |
| 245 | var_name16, |
| 246 | &guid)); |
| 247 | } |
| 248 | if (ret != EFI_SUCCESS) { |
| 249 | free(var_name16); |
| 250 | return CMD_RET_FAILURE; |
| 251 | } |
| 252 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 253 | if ((!guid_p || !guidcmp(guid_p, &guid)) && |
| 254 | (!argc || match_name(argc, argv, var_name16))) |
| 255 | efi_dump_single_var(var_name16, &guid, verbose); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | free(var_name16); |
| 259 | |
| 260 | return CMD_RET_SUCCESS; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * do_env_print_efi() - show information about UEFI variables |
| 265 | * |
| 266 | * @cmdtp: Command table |
| 267 | * @flag: Command flag |
| 268 | * @argc: Number of arguments |
| 269 | * @argv: Argument array |
| 270 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
| 271 | * |
| 272 | * This function is for "env print -e" or "printenv -e" command: |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 273 | * => env print -e [-n] [-guid <guid> | -all] [var [...]] |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 274 | * If one or more variable names are specified, show information |
| 275 | * named UEFI variables, otherwise show all the UEFI variables. |
| 276 | */ |
| 277 | int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 278 | { |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 279 | efi_guid_t guid; |
| 280 | const efi_guid_t *guid_p; |
| 281 | bool default_guid, guid_any, verbose; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 282 | efi_status_t ret; |
| 283 | |
| 284 | /* Initialize EFI drivers */ |
| 285 | ret = efi_init_obj_list(); |
| 286 | if (ret != EFI_SUCCESS) { |
| 287 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 288 | ret & ~EFI_ERROR_MASK); |
| 289 | return CMD_RET_FAILURE; |
| 290 | } |
| 291 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 292 | default_guid = true; |
| 293 | guid_any = false; |
| 294 | verbose = true; |
| 295 | for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { |
| 296 | if (!strcmp(argv[0], "-guid")) { |
| 297 | if (argc == 1) |
| 298 | return CMD_RET_USAGE; |
| 299 | |
| 300 | /* -a already specified */ |
| 301 | if (!default_guid & guid_any) |
| 302 | return CMD_RET_USAGE; |
| 303 | |
| 304 | argc--; |
| 305 | argv++; |
| 306 | if (uuid_str_to_bin(argv[0], guid.b, |
| 307 | UUID_STR_FORMAT_GUID)) |
| 308 | return CMD_RET_USAGE; |
| 309 | default_guid = false; |
| 310 | } else if (!strcmp(argv[0], "-all")) { |
| 311 | /* -guid already specified */ |
| 312 | if (!default_guid && !guid_any) |
| 313 | return CMD_RET_USAGE; |
| 314 | |
| 315 | guid_any = true; |
| 316 | default_guid = false; |
| 317 | } else if (!strcmp(argv[0], "-n")) { |
| 318 | verbose = false; |
| 319 | } else { |
| 320 | return CMD_RET_USAGE; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (guid_any) |
| 325 | guid_p = NULL; |
| 326 | else if (default_guid) |
| 327 | guid_p = &efi_global_variable_guid; |
| 328 | else |
| 329 | guid_p = (const efi_guid_t *)guid.b; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 330 | |
| 331 | /* enumerate and show all UEFI variables */ |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 332 | return efi_dump_var_all(argc, argv, guid_p, verbose); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * append_value() - encode UEFI variable's value |
| 337 | * @bufp: Buffer of encoded UEFI variable's value |
| 338 | * @sizep: Size of buffer |
| 339 | * @data: data to be encoded into the value |
| 340 | * Return: 0 on success, -1 otherwise |
| 341 | * |
| 342 | * Interpret a given data string and append it to buffer. |
| 343 | * Buffer will be realloc'ed if necessary. |
| 344 | * |
| 345 | * Currently supported formats are: |
| 346 | * =0x0123...: Hexadecimal number |
| 347 | * =H0123...: Hexadecimal-byte array |
| 348 | * ="...", =S"..." or <string>: |
| 349 | * String |
| 350 | */ |
| 351 | static int append_value(char **bufp, size_t *sizep, char *data) |
| 352 | { |
| 353 | char *tmp_buf = NULL, *new_buf = NULL, *value; |
| 354 | unsigned long len = 0; |
| 355 | |
| 356 | if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */ |
| 357 | union { |
| 358 | u8 u8; |
| 359 | u16 u16; |
| 360 | u32 u32; |
| 361 | u64 u64; |
| 362 | } tmp_data; |
| 363 | unsigned long hex_value; |
| 364 | void *hex_ptr; |
| 365 | |
| 366 | data += 3; |
| 367 | len = strlen(data); |
| 368 | if ((len & 0x1)) /* not multiple of two */ |
| 369 | return -1; |
| 370 | |
| 371 | len /= 2; |
| 372 | if (len > 8) |
| 373 | return -1; |
| 374 | else if (len > 4) |
| 375 | len = 8; |
| 376 | else if (len > 2) |
| 377 | len = 4; |
| 378 | |
| 379 | /* convert hex hexadecimal number */ |
| 380 | if (strict_strtoul(data, 16, &hex_value) < 0) |
| 381 | return -1; |
| 382 | |
| 383 | tmp_buf = malloc(len); |
| 384 | if (!tmp_buf) |
| 385 | return -1; |
| 386 | |
| 387 | if (len == 1) { |
| 388 | tmp_data.u8 = hex_value; |
| 389 | hex_ptr = &tmp_data.u8; |
| 390 | } else if (len == 2) { |
| 391 | tmp_data.u16 = hex_value; |
| 392 | hex_ptr = &tmp_data.u16; |
| 393 | } else if (len == 4) { |
| 394 | tmp_data.u32 = hex_value; |
| 395 | hex_ptr = &tmp_data.u32; |
| 396 | } else { |
| 397 | tmp_data.u64 = hex_value; |
| 398 | hex_ptr = &tmp_data.u64; |
| 399 | } |
| 400 | memcpy(tmp_buf, hex_ptr, len); |
| 401 | value = tmp_buf; |
| 402 | |
| 403 | } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */ |
| 404 | data += 2; |
| 405 | len = strlen(data); |
| 406 | if (len & 0x1) /* not multiple of two */ |
| 407 | return -1; |
| 408 | |
| 409 | len /= 2; |
| 410 | tmp_buf = malloc(len); |
| 411 | if (!tmp_buf) |
| 412 | return -1; |
| 413 | |
Heinrich Schuchardt | 45203e0 | 2019-04-30 07:14:09 +0200 | [diff] [blame] | 414 | if (hex2bin((u8 *)tmp_buf, data, len) < 0) { |
| 415 | printf("Error: illegal hexadecimal string\n"); |
| 416 | free(tmp_buf); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 417 | return -1; |
Heinrich Schuchardt | 45203e0 | 2019-04-30 07:14:09 +0200 | [diff] [blame] | 418 | } |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 419 | |
| 420 | value = tmp_buf; |
| 421 | } else { /* string */ |
| 422 | if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) { |
| 423 | if (data[1] == '"') |
| 424 | data += 2; |
| 425 | else |
| 426 | data += 3; |
| 427 | value = data; |
| 428 | len = strlen(data) - 1; |
| 429 | if (data[len] != '"') |
| 430 | return -1; |
| 431 | } else { |
| 432 | value = data; |
| 433 | len = strlen(data); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | new_buf = realloc(*bufp, *sizep + len); |
| 438 | if (!new_buf) |
| 439 | goto out; |
| 440 | |
| 441 | memcpy(new_buf + *sizep, value, len); |
| 442 | *bufp = new_buf; |
| 443 | *sizep += len; |
| 444 | |
| 445 | out: |
| 446 | free(tmp_buf); |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /** |
AKASHI Takahiro | 6810caf | 2019-10-04 13:46:25 +0900 | [diff] [blame] | 452 | * do_env_set_efi() - set UEFI variable |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 453 | * |
| 454 | * @cmdtp: Command table |
| 455 | * @flag: Command flag |
| 456 | * @argc: Number of arguments |
| 457 | * @argv: Argument array |
| 458 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
| 459 | * |
| 460 | * This function is for "env set -e" or "setenv -e" command: |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 461 | * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v] |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 462 | * [-i address,size] var, or |
| 463 | * var [value ...] |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 464 | * Encode values specified and set given UEFI variable. |
| 465 | * If no value is specified, delete the variable. |
| 466 | */ |
| 467 | int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 468 | { |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 469 | char *var_name, *value, *ep; |
| 470 | ulong addr; |
| 471 | efi_uintn_t size; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 472 | efi_guid_t guid; |
AKASHI Takahiro | 4b27a76 | 2019-06-04 15:52:11 +0900 | [diff] [blame] | 473 | u32 attributes; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 474 | bool default_guid, verbose, value_on_memory; |
| 475 | u16 *var_name16 = NULL, *p; |
| 476 | size_t len; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 477 | efi_status_t ret; |
| 478 | |
| 479 | if (argc == 1) |
| 480 | return CMD_RET_USAGE; |
| 481 | |
| 482 | /* Initialize EFI drivers */ |
| 483 | ret = efi_init_obj_list(); |
| 484 | if (ret != EFI_SUCCESS) { |
| 485 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 486 | ret & ~EFI_ERROR_MASK); |
| 487 | return CMD_RET_FAILURE; |
| 488 | } |
| 489 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 490 | /* |
| 491 | * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 492 | * EFI_VARIABLE_RUNTIME_ACCESS; |
| 493 | */ |
| 494 | value = NULL; |
| 495 | size = 0; |
| 496 | attributes = 0; |
| 497 | guid = efi_global_variable_guid; |
| 498 | default_guid = true; |
| 499 | verbose = false; |
| 500 | value_on_memory = false; |
| 501 | for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { |
| 502 | if (!strcmp(argv[0], "-guid")) { |
| 503 | if (argc == 1) |
| 504 | return CMD_RET_USAGE; |
| 505 | |
| 506 | argc--; |
| 507 | argv++; |
| 508 | if (uuid_str_to_bin(argv[0], guid.b, |
| 509 | UUID_STR_FORMAT_GUID)) { |
| 510 | printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n"); |
| 511 | return CMD_RET_FAILURE; |
| 512 | } |
| 513 | default_guid = false; |
| 514 | } else if (!strcmp(argv[0], "-bs")) { |
| 515 | attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 516 | } else if (!strcmp(argv[0], "-rt")) { |
| 517 | attributes |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 518 | } else if (!strcmp(argv[0], "-nv")) { |
| 519 | attributes |= EFI_VARIABLE_NON_VOLATILE; |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 520 | } else if (!strcmp(argv[0], "-at")) { |
| 521 | attributes |= |
| 522 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 523 | } else if (!strcmp(argv[0], "-a")) { |
| 524 | attributes |= EFI_VARIABLE_APPEND_WRITE; |
| 525 | } else if (!strcmp(argv[0], "-i")) { |
| 526 | /* data comes from memory */ |
| 527 | if (argc == 1) |
| 528 | return CMD_RET_USAGE; |
| 529 | |
| 530 | argc--; |
| 531 | argv++; |
| 532 | addr = simple_strtoul(argv[0], &ep, 16); |
| 533 | if (*ep != ',') |
| 534 | return CMD_RET_USAGE; |
| 535 | |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 536 | /* 0 should be allowed for delete */ |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 537 | size = simple_strtoul(++ep, NULL, 16); |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 538 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 539 | value_on_memory = true; |
| 540 | } else if (!strcmp(argv[0], "-v")) { |
| 541 | verbose = true; |
| 542 | } else { |
| 543 | return CMD_RET_USAGE; |
| 544 | } |
| 545 | } |
| 546 | if (!argc) |
| 547 | return CMD_RET_USAGE; |
| 548 | |
| 549 | var_name = argv[0]; |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 550 | if (default_guid) { |
| 551 | if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") || |
| 552 | !strcmp(var_name, "dbt")) |
| 553 | guid = efi_guid_image_security_database; |
| 554 | else |
| 555 | guid = efi_global_variable_guid; |
| 556 | } |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 557 | |
| 558 | if (verbose) { |
| 559 | printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *) |
| 560 | &guid)); |
| 561 | printf("Attributes: 0x%x\n", attributes); |
AKASHI Takahiro | 4b27a76 | 2019-06-04 15:52:11 +0900 | [diff] [blame] | 562 | } |
| 563 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 564 | /* for value */ |
| 565 | if (value_on_memory) |
| 566 | value = map_sysmem(addr, 0); |
| 567 | else if (argc > 1) |
| 568 | for (argc--, argv++; argc > 0; argc--, argv++) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 569 | if (append_value(&value, &size, argv[0]) < 0) { |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 570 | printf("## Failed to process an argument, %s\n", |
| 571 | argv[0]); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 572 | ret = CMD_RET_FAILURE; |
| 573 | goto out; |
| 574 | } |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 575 | |
| 576 | if (size && verbose) { |
| 577 | printf("Value:\n"); |
| 578 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, |
| 579 | 16, 1, value, size, true); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | len = utf8_utf16_strnlen(var_name, strlen(var_name)); |
| 583 | var_name16 = malloc((len + 1) * 2); |
| 584 | if (!var_name16) { |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 585 | printf("## Out of memory\n"); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 586 | ret = CMD_RET_FAILURE; |
| 587 | goto out; |
| 588 | } |
| 589 | p = var_name16; |
| 590 | utf8_utf16_strncpy(&p, var_name, len + 1); |
| 591 | |
AKASHI Takahiro | 4b27a76 | 2019-06-04 15:52:11 +0900 | [diff] [blame] | 592 | ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes, |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 593 | size, value)); |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 594 | unmap_sysmem(value); |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 595 | if (ret == EFI_SUCCESS) { |
| 596 | ret = CMD_RET_SUCCESS; |
| 597 | } else { |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 598 | const char *msg; |
| 599 | |
| 600 | switch (ret) { |
| 601 | case EFI_NOT_FOUND: |
| 602 | msg = " (not found)"; |
| 603 | break; |
| 604 | case EFI_WRITE_PROTECTED: |
| 605 | msg = " (read only)"; |
| 606 | break; |
| 607 | case EFI_INVALID_PARAMETER: |
| 608 | msg = " (invalid parameter)"; |
| 609 | break; |
| 610 | case EFI_SECURITY_VIOLATION: |
| 611 | msg = " (validation failed)"; |
| 612 | break; |
| 613 | case EFI_OUT_OF_RESOURCES: |
| 614 | msg = " (out of memory)"; |
| 615 | break; |
| 616 | default: |
| 617 | msg = ""; |
| 618 | break; |
| 619 | } |
| 620 | printf("## Failed to set EFI variable%s\n", msg); |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 621 | ret = CMD_RET_FAILURE; |
| 622 | } |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 623 | out: |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 624 | if (value_on_memory) |
| 625 | unmap_sysmem(value); |
| 626 | else |
| 627 | free(value); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 628 | free(var_name16); |
| 629 | |
| 630 | return ret; |
| 631 | } |