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