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