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> |
Heinrich Schuchardt | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 12 | #include <efi_variable.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.h> |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 14 | #include <exports.h> |
| 15 | #include <hexdump.h> |
| 16 | #include <malloc.h> |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 17 | #include <mapmem.h> |
Heinrich Schuchardt | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 18 | #include <rtc.h> |
Simon Glass | ba06b3c | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 19 | #include <uuid.h> |
AKASHI Takahiro | 49d81fd | 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 | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 39 | {EFI_VARIABLE_READ_ONLY, "RO"}, |
AKASHI Takahiro | 49d81fd | 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 | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 47 | * @verbose: if true, dump data |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 48 | * |
| 49 | * Show information encoded in one UEFI variable |
| 50 | */ |
AKASHI Takahiro | 051aa89 | 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 | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 52 | { |
| 53 | u32 attributes; |
| 54 | u8 *data; |
Heinrich Schuchardt | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 55 | u64 time; |
| 56 | struct rtc_time tm; |
AKASHI Takahiro | 49d81fd | 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 | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 63 | ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time); |
AKASHI Takahiro | 49d81fd | 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 | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 69 | ret = efi_get_variable_int(name, guid, &attributes, &size, |
| 70 | data, &time); |
AKASHI Takahiro | 49d81fd | 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 | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 79 | rtc_to_tm(time, &tm); |
Heinrich Schuchardt | 983a5a2 | 2022-01-16 14:53:37 +0100 | [diff] [blame] | 80 | printf("%ls:\n %pUl (%pUs)\n", name, guid, guid); |
Heinrich Schuchardt | 2a79c35 | 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 | 49d81fd | 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 | 49d81fd | 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 | 051aa89 | 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 | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 96 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 97 | out: |
| 98 | free(data); |
| 99 | } |
| 100 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 101 | static bool match_name(int argc, char *const argv[], u16 *var_name16) |
AKASHI Takahiro | 051aa89 | 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 | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 129 | /** |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 130 | * efi_dump_var_all() - show information about all the UEFI variables |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 131 | * |
AKASHI Takahiro | 051aa89 | 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 | 49d81fd | 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 | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 139 | static int efi_dump_var_all(int argc, char *const argv[], |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 140 | const efi_guid_t *guid_p, bool verbose) |
AKASHI Takahiro | 49d81fd | 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 | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 146 | bool match = false; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 147 | |
AKASHI Takahiro | 49d81fd | 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 | f4c1a14 | 2021-09-09 07:39:40 +0200 | [diff] [blame] | 156 | ret = efi_get_next_variable_name_int(&size, var_name16, |
| 157 | &guid); |
AKASHI Takahiro | 49d81fd | 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 | f4c1a14 | 2021-09-09 07:39:40 +0200 | [diff] [blame] | 168 | ret = efi_get_next_variable_name_int(&size, var_name16, |
| 169 | &guid); |
AKASHI Takahiro | 49d81fd | 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 | c70f448 | 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 | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 180 | efi_dump_single_var(var_name16, &guid, verbose); |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 181 | } |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 182 | } |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 183 | free(var_name16); |
| 184 | |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 185 | if (!match && argc == 1) |
| 186 | printf("Error: \"%s\" not defined\n", argv[0]); |
| 187 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 188 | return CMD_RET_SUCCESS; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * do_env_print_efi() - show information about UEFI variables |
| 193 | * |
| 194 | * @cmdtp: Command table |
| 195 | * @flag: Command flag |
| 196 | * @argc: Number of arguments |
| 197 | * @argv: Argument array |
| 198 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
| 199 | * |
| 200 | * This function is for "env print -e" or "printenv -e" command: |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 201 | * => env print -e [-n] [-guid <guid> | -all] [var [...]] |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 202 | * If one or more variable names are specified, show information |
| 203 | * named UEFI variables, otherwise show all the UEFI variables. |
| 204 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 205 | int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc, |
| 206 | char *const argv[]) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 207 | { |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 208 | const efi_guid_t *guid_p = NULL; |
AKASHI Takahiro | f1eb346 | 2021-07-03 15:43:17 +0900 | [diff] [blame] | 209 | efi_guid_t guid; |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 210 | bool verbose = true; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 211 | efi_status_t ret; |
| 212 | |
| 213 | /* Initialize EFI drivers */ |
| 214 | ret = efi_init_obj_list(); |
| 215 | if (ret != EFI_SUCCESS) { |
| 216 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 217 | ret & ~EFI_ERROR_MASK); |
| 218 | return CMD_RET_FAILURE; |
| 219 | } |
| 220 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 221 | for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { |
| 222 | if (!strcmp(argv[0], "-guid")) { |
| 223 | if (argc == 1) |
| 224 | return CMD_RET_USAGE; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 225 | argc--; |
| 226 | argv++; |
| 227 | if (uuid_str_to_bin(argv[0], guid.b, |
| 228 | UUID_STR_FORMAT_GUID)) |
| 229 | return CMD_RET_USAGE; |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 230 | guid_p = (const efi_guid_t *)guid.b; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 231 | } else if (!strcmp(argv[0], "-n")) { |
| 232 | verbose = false; |
| 233 | } else { |
| 234 | return CMD_RET_USAGE; |
| 235 | } |
| 236 | } |
| 237 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 238 | /* enumerate and show all UEFI variables */ |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 239 | return efi_dump_var_all(argc, argv, guid_p, verbose); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * append_value() - encode UEFI variable's value |
| 244 | * @bufp: Buffer of encoded UEFI variable's value |
| 245 | * @sizep: Size of buffer |
| 246 | * @data: data to be encoded into the value |
| 247 | * Return: 0 on success, -1 otherwise |
| 248 | * |
| 249 | * Interpret a given data string and append it to buffer. |
| 250 | * Buffer will be realloc'ed if necessary. |
| 251 | * |
| 252 | * Currently supported formats are: |
| 253 | * =0x0123...: Hexadecimal number |
| 254 | * =H0123...: Hexadecimal-byte array |
| 255 | * ="...", =S"..." or <string>: |
| 256 | * String |
| 257 | */ |
| 258 | static int append_value(char **bufp, size_t *sizep, char *data) |
| 259 | { |
| 260 | char *tmp_buf = NULL, *new_buf = NULL, *value; |
| 261 | unsigned long len = 0; |
| 262 | |
| 263 | if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */ |
| 264 | union { |
| 265 | u8 u8; |
| 266 | u16 u16; |
| 267 | u32 u32; |
| 268 | u64 u64; |
| 269 | } tmp_data; |
| 270 | unsigned long hex_value; |
| 271 | void *hex_ptr; |
| 272 | |
| 273 | data += 3; |
| 274 | len = strlen(data); |
| 275 | if ((len & 0x1)) /* not multiple of two */ |
| 276 | return -1; |
| 277 | |
| 278 | len /= 2; |
| 279 | if (len > 8) |
| 280 | return -1; |
| 281 | else if (len > 4) |
| 282 | len = 8; |
| 283 | else if (len > 2) |
| 284 | len = 4; |
| 285 | |
| 286 | /* convert hex hexadecimal number */ |
| 287 | if (strict_strtoul(data, 16, &hex_value) < 0) |
| 288 | return -1; |
| 289 | |
| 290 | tmp_buf = malloc(len); |
| 291 | if (!tmp_buf) |
| 292 | return -1; |
| 293 | |
| 294 | if (len == 1) { |
| 295 | tmp_data.u8 = hex_value; |
| 296 | hex_ptr = &tmp_data.u8; |
| 297 | } else if (len == 2) { |
| 298 | tmp_data.u16 = hex_value; |
| 299 | hex_ptr = &tmp_data.u16; |
| 300 | } else if (len == 4) { |
| 301 | tmp_data.u32 = hex_value; |
| 302 | hex_ptr = &tmp_data.u32; |
| 303 | } else { |
| 304 | tmp_data.u64 = hex_value; |
| 305 | hex_ptr = &tmp_data.u64; |
| 306 | } |
| 307 | memcpy(tmp_buf, hex_ptr, len); |
| 308 | value = tmp_buf; |
| 309 | |
| 310 | } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */ |
| 311 | data += 2; |
| 312 | len = strlen(data); |
| 313 | if (len & 0x1) /* not multiple of two */ |
| 314 | return -1; |
| 315 | |
| 316 | len /= 2; |
| 317 | tmp_buf = malloc(len); |
| 318 | if (!tmp_buf) |
| 319 | return -1; |
| 320 | |
Heinrich Schuchardt | 45203e0 | 2019-04-30 07:14:09 +0200 | [diff] [blame] | 321 | if (hex2bin((u8 *)tmp_buf, data, len) < 0) { |
| 322 | printf("Error: illegal hexadecimal string\n"); |
| 323 | free(tmp_buf); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 324 | return -1; |
Heinrich Schuchardt | 45203e0 | 2019-04-30 07:14:09 +0200 | [diff] [blame] | 325 | } |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 326 | |
| 327 | value = tmp_buf; |
| 328 | } else { /* string */ |
| 329 | if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) { |
| 330 | if (data[1] == '"') |
| 331 | data += 2; |
| 332 | else |
| 333 | data += 3; |
| 334 | value = data; |
| 335 | len = strlen(data) - 1; |
| 336 | if (data[len] != '"') |
| 337 | return -1; |
| 338 | } else { |
| 339 | value = data; |
| 340 | len = strlen(data); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | new_buf = realloc(*bufp, *sizep + len); |
| 345 | if (!new_buf) |
| 346 | goto out; |
| 347 | |
| 348 | memcpy(new_buf + *sizep, value, len); |
| 349 | *bufp = new_buf; |
| 350 | *sizep += len; |
| 351 | |
| 352 | out: |
| 353 | free(tmp_buf); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /** |
AKASHI Takahiro | 6810caf | 2019-10-04 13:46:25 +0900 | [diff] [blame] | 359 | * do_env_set_efi() - set UEFI variable |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 360 | * |
| 361 | * @cmdtp: Command table |
| 362 | * @flag: Command flag |
| 363 | * @argc: Number of arguments |
| 364 | * @argv: Argument array |
| 365 | * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE |
| 366 | * |
| 367 | * This function is for "env set -e" or "setenv -e" command: |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 368 | * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v] |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 369 | * [-i address,size] var, or |
| 370 | * var [value ...] |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 371 | * Encode values specified and set given UEFI variable. |
| 372 | * If no value is specified, delete the variable. |
| 373 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 374 | int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc, |
| 375 | char *const argv[]) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 376 | { |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 377 | char *var_name, *value, *ep; |
| 378 | ulong addr; |
| 379 | efi_uintn_t size; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 380 | efi_guid_t guid; |
AKASHI Takahiro | 4b27a76 | 2019-06-04 15:52:11 +0900 | [diff] [blame] | 381 | u32 attributes; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 382 | bool default_guid, verbose, value_on_memory; |
| 383 | u16 *var_name16 = NULL, *p; |
| 384 | size_t len; |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 385 | efi_status_t ret; |
| 386 | |
| 387 | if (argc == 1) |
| 388 | return CMD_RET_USAGE; |
| 389 | |
| 390 | /* Initialize EFI drivers */ |
| 391 | ret = efi_init_obj_list(); |
| 392 | if (ret != EFI_SUCCESS) { |
| 393 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 394 | ret & ~EFI_ERROR_MASK); |
| 395 | return CMD_RET_FAILURE; |
| 396 | } |
| 397 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 398 | /* |
| 399 | * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 400 | * EFI_VARIABLE_RUNTIME_ACCESS; |
| 401 | */ |
| 402 | value = NULL; |
| 403 | size = 0; |
| 404 | attributes = 0; |
| 405 | guid = efi_global_variable_guid; |
| 406 | default_guid = true; |
| 407 | verbose = false; |
| 408 | value_on_memory = false; |
| 409 | for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { |
| 410 | if (!strcmp(argv[0], "-guid")) { |
| 411 | if (argc == 1) |
| 412 | return CMD_RET_USAGE; |
| 413 | |
| 414 | argc--; |
| 415 | argv++; |
| 416 | if (uuid_str_to_bin(argv[0], guid.b, |
| 417 | UUID_STR_FORMAT_GUID)) { |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 418 | return CMD_RET_USAGE; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 419 | } |
| 420 | default_guid = false; |
| 421 | } else if (!strcmp(argv[0], "-bs")) { |
| 422 | attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 423 | } else if (!strcmp(argv[0], "-rt")) { |
| 424 | attributes |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 425 | } else if (!strcmp(argv[0], "-nv")) { |
| 426 | attributes |= EFI_VARIABLE_NON_VOLATILE; |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 427 | } else if (!strcmp(argv[0], "-at")) { |
| 428 | attributes |= |
| 429 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 430 | } else if (!strcmp(argv[0], "-a")) { |
| 431 | attributes |= EFI_VARIABLE_APPEND_WRITE; |
| 432 | } else if (!strcmp(argv[0], "-i")) { |
| 433 | /* data comes from memory */ |
| 434 | if (argc == 1) |
| 435 | return CMD_RET_USAGE; |
| 436 | |
| 437 | argc--; |
| 438 | argv++; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 439 | addr = hextoul(argv[0], &ep); |
Heinrich Schuchardt | 2b3fbcb | 2020-08-24 08:27:49 +0200 | [diff] [blame] | 440 | if (*ep != ':') |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 441 | return CMD_RET_USAGE; |
| 442 | |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 443 | /* 0 should be allowed for delete */ |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 444 | size = hextoul(++ep, NULL); |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 445 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 446 | value_on_memory = true; |
| 447 | } else if (!strcmp(argv[0], "-v")) { |
| 448 | verbose = true; |
| 449 | } else { |
| 450 | return CMD_RET_USAGE; |
| 451 | } |
| 452 | } |
| 453 | if (!argc) |
| 454 | return CMD_RET_USAGE; |
| 455 | |
| 456 | var_name = argv[0]; |
AKASHI Takahiro | f757d04 | 2020-04-14 11:51:46 +0900 | [diff] [blame] | 457 | if (default_guid) { |
| 458 | if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") || |
| 459 | !strcmp(var_name, "dbt")) |
| 460 | guid = efi_guid_image_security_database; |
| 461 | else |
| 462 | guid = efi_global_variable_guid; |
| 463 | } |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 464 | |
| 465 | if (verbose) { |
Heinrich Schuchardt | 983a5a2 | 2022-01-16 14:53:37 +0100 | [diff] [blame] | 466 | printf("GUID: %pUl (%pUs)\n", &guid, &guid); |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 467 | printf("Attributes: 0x%x\n", attributes); |
AKASHI Takahiro | 4b27a76 | 2019-06-04 15:52:11 +0900 | [diff] [blame] | 468 | } |
| 469 | |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 470 | /* for value */ |
| 471 | if (value_on_memory) |
| 472 | value = map_sysmem(addr, 0); |
| 473 | else if (argc > 1) |
| 474 | for (argc--, argv++; argc > 0; argc--, argv++) |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 475 | if (append_value(&value, &size, argv[0]) < 0) { |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 476 | printf("## Failed to process an argument, %s\n", |
| 477 | argv[0]); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 478 | ret = CMD_RET_FAILURE; |
| 479 | goto out; |
| 480 | } |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 481 | |
| 482 | if (size && verbose) { |
| 483 | printf("Value:\n"); |
| 484 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, |
| 485 | 16, 1, value, size, true); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | len = utf8_utf16_strnlen(var_name, strlen(var_name)); |
| 489 | var_name16 = malloc((len + 1) * 2); |
| 490 | if (!var_name16) { |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 491 | printf("## Out of memory\n"); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 492 | ret = CMD_RET_FAILURE; |
| 493 | goto out; |
| 494 | } |
| 495 | p = var_name16; |
| 496 | utf8_utf16_strncpy(&p, var_name, len + 1); |
| 497 | |
Heinrich Schuchardt | 2a79c35 | 2020-06-24 19:55:13 +0200 | [diff] [blame] | 498 | ret = efi_set_variable_int(var_name16, &guid, attributes, size, value, |
| 499 | true); |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 500 | unmap_sysmem(value); |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 501 | if (ret == EFI_SUCCESS) { |
| 502 | ret = CMD_RET_SUCCESS; |
| 503 | } else { |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 504 | const char *msg; |
| 505 | |
| 506 | switch (ret) { |
| 507 | case EFI_NOT_FOUND: |
| 508 | msg = " (not found)"; |
| 509 | break; |
| 510 | case EFI_WRITE_PROTECTED: |
| 511 | msg = " (read only)"; |
| 512 | break; |
| 513 | case EFI_INVALID_PARAMETER: |
| 514 | msg = " (invalid parameter)"; |
| 515 | break; |
| 516 | case EFI_SECURITY_VIOLATION: |
| 517 | msg = " (validation failed)"; |
| 518 | break; |
| 519 | case EFI_OUT_OF_RESOURCES: |
| 520 | msg = " (out of memory)"; |
| 521 | break; |
| 522 | default: |
| 523 | msg = ""; |
| 524 | break; |
| 525 | } |
| 526 | printf("## Failed to set EFI variable%s\n", msg); |
AKASHI Takahiro | 8190b4a | 2019-05-28 09:00:35 +0900 | [diff] [blame] | 527 | ret = CMD_RET_FAILURE; |
| 528 | } |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 529 | out: |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 530 | if (value_on_memory) |
| 531 | unmap_sysmem(value); |
| 532 | else |
| 533 | free(value); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 534 | free(var_name16); |
| 535 | |
| 536 | return ret; |
| 537 | } |