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 | // SPDX-License-Identifier: GPL-2.0+ #include <charset.h> #include <command.h> #include <efi_loader.h> #include <efi_variable.h> #include <env.h> #include <hexdump.h> #include <malloc.h> #include <uuid.h> /* Set a U-Boot environment variable to the contents of a UEFI variable */ int do_efi_get_env(struct cmd_tbl *cmdtb, int flat, int argc, char *const argv[]) { u16 *var_name = NULL; char *strdata = NULL; efi_uintn_t size = 0; bool var_content_is_utf16_string = false; efi_status_t ret; efi_guid_t guid; u8 *data = NULL; u32 attributes; size_t len; u64 time; u16 *p; ret = efi_init_obj_list(); if (ret != EFI_SUCCESS) { printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", ret & ~EFI_ERROR_MASK); return CMD_RET_FAILURE; } argv++; argc--; if (argc != 3 && argc != 4) return CMD_RET_USAGE; if (argc == 4) { if (strcmp(argv[0], "-s")) return CMD_RET_USAGE; var_content_is_utf16_string = true; argv++; argc--; } len = utf8_utf16_strnlen(argv[0], strlen(argv[0])); var_name = malloc((len + 1) * 2); if (!var_name) { printf("## Out of memory\n"); return CMD_RET_FAILURE; } p = var_name; utf8_utf16_strncpy(&p, argv[0], len + 1); if (uuid_str_to_bin(argv[1], guid.b, UUID_STR_FORMAT_GUID)) { ret = CMD_RET_USAGE; goto out; } ret = efi_get_variable_int(var_name, &guid, &attributes, &size, data, &time); if (ret == EFI_BUFFER_TOO_SMALL) { data = malloc(size); if (!data) { printf("## Out of memory\n"); ret = CMD_RET_FAILURE; goto out; } ret = efi_get_variable_int(var_name, &guid, &attributes, &size, data, &time); } if (ret == EFI_NOT_FOUND) { printf("Error: \"%ls\" not defined\n", var_name); ret = CMD_RET_FAILURE; goto out; } if (ret != EFI_SUCCESS) { printf("Error: Cannot read variable, r = %lu\n", ret & ~EFI_ERROR_MASK); ret = CMD_RET_FAILURE; goto out; } if (var_content_is_utf16_string) { char *p; len = utf16_utf8_strnlen((u16 *)data, size / 2); strdata = malloc(len + 1); if (!strdata) { printf("## Out of memory\n"); ret = CMD_RET_FAILURE; goto out; } p = strdata; utf16_utf8_strncpy(&p, (u16 *)data, size / 2); } else { len = size * 2; strdata = malloc(len + 1); if (!strdata) { printf("## Out of memory\n"); ret = CMD_RET_FAILURE; goto out; } bin2hex(strdata, data, size); } strdata[len] = '\0'; ret = env_set(argv[2], strdata); if (ret) { ret = CMD_RET_FAILURE; goto out; } ret = CMD_RET_SUCCESS; out: free(strdata); free(data); free(var_name); return ret; } U_BOOT_CMD( efigetenv, 5, 4, do_efi_get_env, "set environment variable to content of EFI variable", "[-s] name guid envvar\n" " - set environment variable 'envvar' to the EFI variable 'name'-'guid'\n" " \"-s\": Interpret the EFI variable value as a UTF-16 string\n" ); |