Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2 | /* |
| 3 | * EFI utils |
| 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <charset.h> |
| 11 | #include <efi_loader.h> |
Heinrich Schuchardt | 2d8aedc | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 12 | #include <hexdump.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 14 | #include <search.h> |
| 15 | #include <uuid.h> |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 16 | |
| 17 | #define READ_ONLY BIT(31) |
| 18 | |
| 19 | /* |
| 20 | * Mapping between EFI variables and u-boot variables: |
| 21 | * |
| 22 | * efi_$guid_$varname = {attributes}(type)value |
| 23 | * |
| 24 | * For example: |
| 25 | * |
| 26 | * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported= |
| 27 | * "{ro,boot,run}(blob)0000000000000000" |
| 28 | * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder= |
| 29 | * "(blob)00010000" |
| 30 | * |
| 31 | * The attributes are a comma separated list of these possible |
| 32 | * attributes: |
| 33 | * |
| 34 | * + ro - read-only |
| 35 | * + boot - boot-services access |
| 36 | * + run - runtime access |
| 37 | * |
| 38 | * NOTE: with current implementation, no variables are available after |
| 39 | * ExitBootServices, and all are persisted (if possible). |
| 40 | * |
| 41 | * If not specified, the attributes default to "{boot}". |
| 42 | * |
| 43 | * The required type is one of: |
| 44 | * |
| 45 | * + utf8 - raw utf8 string |
| 46 | * + blob - arbitrary length hex string |
| 47 | * |
| 48 | * Maybe a utf16 type would be useful to for a string value to be auto |
| 49 | * converted to utf16? |
| 50 | */ |
| 51 | |
Heinrich Schuchardt | 44389cb | 2018-09-23 04:08:09 +0200 | [diff] [blame] | 52 | #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_")) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 53 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 54 | /** |
| 55 | * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot |
| 56 | * variable name |
| 57 | * |
| 58 | * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring |
| 59 | * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by |
| 60 | * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'. |
| 61 | * |
| 62 | * @native: pointer to pointer to U-Boot variable name |
| 63 | * @variable_name: UEFI variable name |
| 64 | * @vendor: vendor GUID |
| 65 | * Return: status code |
| 66 | */ |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 67 | static efi_status_t efi_to_native(char **native, const u16 *variable_name, |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 68 | const efi_guid_t *vendor) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 69 | { |
| 70 | size_t len; |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 71 | char *pos; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 72 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 73 | len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1; |
| 74 | *native = malloc(len); |
| 75 | if (!*native) |
| 76 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 77 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 78 | pos = *native; |
| 79 | pos += sprintf(pos, "efi_%pUl_", vendor); |
| 80 | utf16_utf8_strcpy(&pos, variable_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 81 | |
| 82 | return EFI_SUCCESS; |
| 83 | } |
| 84 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 85 | /** |
| 86 | * prefix() - skip over prefix |
| 87 | * |
| 88 | * Skip over a prefix string. |
| 89 | * |
| 90 | * @str: string with prefix |
| 91 | * @prefix: prefix string |
| 92 | * Return: string without prefix, or NULL if prefix not found |
| 93 | */ |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 94 | static const char *prefix(const char *str, const char *prefix) |
| 95 | { |
| 96 | size_t n = strlen(prefix); |
| 97 | if (!strncmp(prefix, str, n)) |
| 98 | return str + n; |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 102 | /** |
| 103 | * parse_attr() - decode attributes part of variable value |
| 104 | * |
| 105 | * Convert the string encoded attributes of a UEFI variable to a bit mask. |
| 106 | * TODO: Several attributes are not supported. |
| 107 | * |
| 108 | * @str: value of U-Boot variable |
| 109 | * @attrp: pointer to UEFI attributes |
| 110 | * Return: pointer to remainder of U-Boot variable value |
| 111 | */ |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 112 | static const char *parse_attr(const char *str, u32 *attrp) |
| 113 | { |
| 114 | u32 attr = 0; |
| 115 | char sep = '{'; |
| 116 | |
| 117 | if (*str != '{') { |
| 118 | *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 119 | return str; |
| 120 | } |
| 121 | |
| 122 | while (*str == sep) { |
| 123 | const char *s; |
| 124 | |
| 125 | str++; |
| 126 | |
| 127 | if ((s = prefix(str, "ro"))) { |
| 128 | attr |= READ_ONLY; |
AKASHI Takahiro | 7f33433 | 2019-06-04 15:52:06 +0900 | [diff] [blame] | 129 | } else if ((s = prefix(str, "nv"))) { |
| 130 | attr |= EFI_VARIABLE_NON_VOLATILE; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 131 | } else if ((s = prefix(str, "boot"))) { |
| 132 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 133 | } else if ((s = prefix(str, "run"))) { |
| 134 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 135 | } else { |
| 136 | printf("invalid attribute: %s\n", str); |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | str = s; |
| 141 | sep = ','; |
| 142 | } |
| 143 | |
| 144 | str++; |
| 145 | |
| 146 | *attrp = attr; |
| 147 | |
| 148 | return str; |
| 149 | } |
| 150 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 151 | /** |
Heinrich Schuchardt | 0f042f9 | 2019-06-20 12:03:53 +0200 | [diff] [blame] | 152 | * efi_get_variable() - retrieve value of a UEFI variable |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 153 | * |
| 154 | * This function implements the GetVariable runtime service. |
| 155 | * |
| 156 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 157 | * details. |
| 158 | * |
| 159 | * @variable_name: name of the variable |
| 160 | * @vendor: vendor GUID |
| 161 | * @attributes: attributes of the variable |
| 162 | * @data_size: size of the buffer to which the variable value is copied |
| 163 | * @data: buffer to which the variable value is copied |
| 164 | * Return: status code |
| 165 | */ |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 166 | efi_status_t EFIAPI efi_get_variable(u16 *variable_name, |
| 167 | const efi_guid_t *vendor, u32 *attributes, |
| 168 | efi_uintn_t *data_size, void *data) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 169 | { |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 170 | char *native_name; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 171 | efi_status_t ret; |
| 172 | unsigned long in_size; |
| 173 | const char *val, *s; |
| 174 | u32 attr; |
| 175 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 176 | EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 177 | data_size, data); |
| 178 | |
| 179 | if (!variable_name || !vendor || !data_size) |
| 180 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 181 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 182 | ret = efi_to_native(&native_name, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 183 | if (ret) |
| 184 | return EFI_EXIT(ret); |
| 185 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 186 | EFI_PRINT("get '%s'\n", native_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 187 | |
| 188 | val = env_get(native_name); |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 189 | free(native_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 190 | if (!val) |
| 191 | return EFI_EXIT(EFI_NOT_FOUND); |
| 192 | |
| 193 | val = parse_attr(val, &attr); |
| 194 | |
| 195 | in_size = *data_size; |
| 196 | |
| 197 | if ((s = prefix(val, "(blob)"))) { |
Heinrich Schuchardt | 2d8aedc | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 198 | size_t len = strlen(s); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 199 | |
Ivan Gorinov | d3ea6b7 | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 200 | /* number of hexadecimal digits must be even */ |
| 201 | if (len & 1) |
| 202 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 203 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 204 | /* two characters per byte: */ |
Ivan Gorinov | d3ea6b7 | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 205 | len /= 2; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 206 | *data_size = len; |
| 207 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 208 | if (in_size < len) { |
| 209 | ret = EFI_BUFFER_TOO_SMALL; |
| 210 | goto out; |
| 211 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 212 | |
| 213 | if (!data) |
| 214 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 215 | |
Heinrich Schuchardt | 2d8aedc | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 216 | if (hex2bin(data, s, len)) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 217 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 218 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 219 | EFI_PRINT("got value: \"%s\"\n", s); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 220 | } else if ((s = prefix(val, "(utf8)"))) { |
| 221 | unsigned len = strlen(s) + 1; |
| 222 | |
| 223 | *data_size = len; |
| 224 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 225 | if (in_size < len) { |
| 226 | ret = EFI_BUFFER_TOO_SMALL; |
| 227 | goto out; |
| 228 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 229 | |
| 230 | if (!data) |
| 231 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 232 | |
| 233 | memcpy(data, s, len); |
| 234 | ((char *)data)[len] = '\0'; |
| 235 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 236 | EFI_PRINT("got value: \"%s\"\n", (char *)data); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 237 | } else { |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 238 | EFI_PRINT("invalid value: '%s'\n", val); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 239 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 240 | } |
| 241 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 242 | out: |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 243 | if (attributes) |
| 244 | *attributes = attr & EFI_VARIABLE_MASK; |
| 245 | |
Heinrich Schuchardt | 05f728f | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 246 | return EFI_EXIT(ret); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 247 | } |
| 248 | |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 249 | static char *efi_variables_list; |
| 250 | static char *efi_cur_variable; |
| 251 | |
| 252 | /** |
| 253 | * parse_uboot_variable() - parse a u-boot variable and get uefi-related |
| 254 | * information |
| 255 | * @variable: whole data of u-boot variable (ie. name=value) |
| 256 | * @variable_name_size: size of variable_name buffer in byte |
| 257 | * @variable_name: name of uefi variable in u16, null-terminated |
| 258 | * @vendor: vendor's guid |
| 259 | * @attributes: attributes |
| 260 | * |
| 261 | * A uefi variable is encoded into a u-boot variable as described above. |
| 262 | * This function parses such a u-boot variable and retrieve uefi-related |
| 263 | * information into respective parameters. In return, variable_name_size |
| 264 | * is the size of variable name including NULL. |
| 265 | * |
| 266 | * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 267 | * the entire variable list has been returned, |
| 268 | * otherwise non-zero status code |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 269 | */ |
| 270 | static efi_status_t parse_uboot_variable(char *variable, |
| 271 | efi_uintn_t *variable_name_size, |
| 272 | u16 *variable_name, |
| 273 | const efi_guid_t *vendor, |
| 274 | u32 *attributes) |
| 275 | { |
| 276 | char *guid, *name, *end, c; |
| 277 | unsigned long name_len; |
| 278 | u16 *p; |
| 279 | |
| 280 | guid = strchr(variable, '_'); |
| 281 | if (!guid) |
| 282 | return EFI_INVALID_PARAMETER; |
| 283 | guid++; |
| 284 | name = strchr(guid, '_'); |
| 285 | if (!name) |
| 286 | return EFI_INVALID_PARAMETER; |
| 287 | name++; |
| 288 | end = strchr(name, '='); |
| 289 | if (!end) |
| 290 | return EFI_INVALID_PARAMETER; |
| 291 | |
| 292 | name_len = end - name; |
| 293 | if (*variable_name_size < (name_len + 1)) { |
| 294 | *variable_name_size = name_len + 1; |
| 295 | return EFI_BUFFER_TOO_SMALL; |
| 296 | } |
| 297 | end++; /* point to value */ |
| 298 | |
| 299 | /* variable name */ |
| 300 | p = variable_name; |
| 301 | utf8_utf16_strncpy(&p, name, name_len); |
| 302 | variable_name[name_len] = 0; |
| 303 | *variable_name_size = name_len + 1; |
| 304 | |
| 305 | /* guid */ |
| 306 | c = *(name - 1); |
| 307 | *(name - 1) = '\0'; /* guid need be null-terminated here */ |
| 308 | uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID); |
| 309 | *(name - 1) = c; |
| 310 | |
| 311 | /* attributes */ |
| 312 | parse_attr(end, attributes); |
| 313 | |
| 314 | return EFI_SUCCESS; |
| 315 | } |
| 316 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 317 | /** |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 318 | * efi_get_next_variable_name() - enumerate the current variable names |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 319 | * |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 320 | * @variable_name_size: size of variable_name buffer in byte |
| 321 | * @variable_name: name of uefi variable's name in u16 |
| 322 | * @vendor: vendor's guid |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 323 | * |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 324 | * This function implements the GetNextVariableName service. |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 325 | * |
| 326 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 327 | * details. |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 328 | * |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 329 | * Return: status code |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 330 | */ |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 331 | efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, |
| 332 | u16 *variable_name, |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 333 | const efi_guid_t *vendor) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 334 | { |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 335 | char *native_name, *variable; |
| 336 | ssize_t name_len, list_len; |
| 337 | char regex[256]; |
| 338 | char * const regexlist[] = {regex}; |
| 339 | u32 attributes; |
| 340 | int i; |
| 341 | efi_status_t ret; |
| 342 | |
Rob Clark | 238f88c | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 343 | EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 344 | |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 345 | if (!variable_name_size || !variable_name || !vendor) |
Heinrich Schuchardt | 793a625 | 2019-03-19 18:36:21 +0100 | [diff] [blame] | 346 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 347 | |
| 348 | if (variable_name[0]) { |
| 349 | /* check null-terminated string */ |
| 350 | for (i = 0; i < *variable_name_size; i++) |
| 351 | if (!variable_name[i]) |
| 352 | break; |
| 353 | if (i >= *variable_name_size) |
| 354 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 355 | |
| 356 | /* search for the last-returned variable */ |
| 357 | ret = efi_to_native(&native_name, variable_name, vendor); |
| 358 | if (ret) |
| 359 | return EFI_EXIT(ret); |
| 360 | |
| 361 | name_len = strlen(native_name); |
| 362 | for (variable = efi_variables_list; variable && *variable;) { |
| 363 | if (!strncmp(variable, native_name, name_len) && |
| 364 | variable[name_len] == '=') |
| 365 | break; |
| 366 | |
| 367 | variable = strchr(variable, '\n'); |
| 368 | if (variable) |
| 369 | variable++; |
| 370 | } |
| 371 | |
| 372 | free(native_name); |
| 373 | if (!(variable && *variable)) |
| 374 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 375 | |
| 376 | /* next variable */ |
| 377 | variable = strchr(variable, '\n'); |
| 378 | if (variable) |
| 379 | variable++; |
| 380 | if (!(variable && *variable)) |
| 381 | return EFI_EXIT(EFI_NOT_FOUND); |
| 382 | } else { |
| 383 | /* |
| 384 | *new search: free a list used in the previous search |
| 385 | */ |
| 386 | free(efi_variables_list); |
| 387 | efi_variables_list = NULL; |
| 388 | efi_cur_variable = NULL; |
| 389 | |
| 390 | snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*"); |
| 391 | list_len = hexport_r(&env_htab, '\n', |
| 392 | H_MATCH_REGEX | H_MATCH_KEY, |
| 393 | &efi_variables_list, 0, 1, regexlist); |
Heinrich Schuchardt | fd73848 | 2019-01-22 20:10:46 +0100 | [diff] [blame] | 394 | /* 1 indicates that no match was found */ |
| 395 | if (list_len <= 1) |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 396 | return EFI_EXIT(EFI_NOT_FOUND); |
| 397 | |
| 398 | variable = efi_variables_list; |
| 399 | } |
| 400 | |
| 401 | ret = parse_uboot_variable(variable, variable_name_size, variable_name, |
| 402 | vendor, &attributes); |
| 403 | |
| 404 | return EFI_EXIT(ret); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 407 | /** |
Heinrich Schuchardt | 0f042f9 | 2019-06-20 12:03:53 +0200 | [diff] [blame] | 408 | * efi_set_variable() - set value of a UEFI variable |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 409 | * |
| 410 | * This function implements the SetVariable runtime service. |
| 411 | * |
| 412 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 413 | * details. |
| 414 | * |
| 415 | * @variable_name: name of the variable |
| 416 | * @vendor: vendor GUID |
| 417 | * @attributes: attributes of the variable |
| 418 | * @data_size: size of the buffer with the variable value |
| 419 | * @data: buffer with the variable value |
| 420 | * Return: status code |
| 421 | */ |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 422 | efi_status_t EFIAPI efi_set_variable(u16 *variable_name, |
| 423 | const efi_guid_t *vendor, u32 attributes, |
Heinrich Schuchardt | 73e2ab0 | 2018-12-30 21:03:15 +0100 | [diff] [blame] | 424 | efi_uintn_t data_size, const void *data) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 425 | { |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 426 | char *native_name = NULL, *val = NULL, *s; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 427 | const char *old_val; |
| 428 | size_t old_size; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 429 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 430 | u32 attr; |
| 431 | |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 432 | EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 433 | data_size, data); |
| 434 | |
Heinrich Schuchardt | adba396 | 2019-06-12 23:28:42 +0200 | [diff] [blame] | 435 | if (!variable_name || !*variable_name || !vendor || |
| 436 | ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) && |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 437 | !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) { |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 438 | ret = EFI_INVALID_PARAMETER; |
| 439 | goto out; |
| 440 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 441 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 442 | ret = efi_to_native(&native_name, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 443 | if (ret) |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 444 | goto out; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 445 | |
| 446 | #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS) |
| 447 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 448 | old_val = env_get(native_name); |
| 449 | if (old_val) { |
| 450 | old_val = parse_attr(old_val, &attr); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 451 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 452 | /* check read-only first */ |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 453 | if (attr & READ_ONLY) { |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 454 | ret = EFI_WRITE_PROTECTED; |
| 455 | goto out; |
| 456 | } |
AKASHI Takahiro | 186b5a4 | 2019-05-24 15:59:03 +0900 | [diff] [blame] | 457 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 458 | if ((data_size == 0) || !(attributes & ACCESS_ATTR)) { |
| 459 | /* delete the variable: */ |
| 460 | env_set(native_name, NULL); |
| 461 | ret = EFI_SUCCESS; |
| 462 | goto out; |
| 463 | } |
| 464 | |
| 465 | /* attributes won't be changed */ |
| 466 | if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE)) { |
AKASHI Takahiro | 186b5a4 | 2019-05-24 15:59:03 +0900 | [diff] [blame] | 467 | ret = EFI_INVALID_PARAMETER; |
| 468 | goto out; |
| 469 | } |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 470 | |
| 471 | if (attributes & EFI_VARIABLE_APPEND_WRITE) { |
| 472 | if (!prefix(old_val, "(blob)")) { |
| 473 | return EFI_DEVICE_ERROR; |
| 474 | goto out; |
| 475 | } |
| 476 | old_size = strlen(old_val); |
| 477 | } else { |
| 478 | old_size = 0; |
| 479 | } |
| 480 | } else { |
| 481 | if ((data_size == 0) || !(attributes & ACCESS_ATTR) || |
| 482 | (attributes & EFI_VARIABLE_APPEND_WRITE)) { |
| 483 | /* delete, but nothing to do */ |
| 484 | ret = EFI_NOT_FOUND; |
| 485 | goto out; |
| 486 | } |
| 487 | |
| 488 | old_size = 0; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 489 | } |
| 490 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 491 | val = malloc(old_size + 2 * data_size |
| 492 | + strlen("{ro,run,boot,nv}(blob)") + 1); |
Heinrich Schuchardt | bd095f5 | 2018-10-02 05:30:05 +0200 | [diff] [blame] | 493 | if (!val) { |
| 494 | ret = EFI_OUT_OF_RESOURCES; |
| 495 | goto out; |
| 496 | } |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 497 | |
| 498 | s = val; |
| 499 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 500 | /* store attributes */ |
AKASHI Takahiro | 7f33433 | 2019-06-04 15:52:06 +0900 | [diff] [blame] | 501 | attributes &= (EFI_VARIABLE_NON_VOLATILE | |
| 502 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 503 | EFI_VARIABLE_RUNTIME_ACCESS); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 504 | s += sprintf(s, "{"); |
| 505 | while (attributes) { |
| 506 | u32 attr = 1 << (ffs(attributes) - 1); |
| 507 | |
AKASHI Takahiro | 7f33433 | 2019-06-04 15:52:06 +0900 | [diff] [blame] | 508 | if (attr == EFI_VARIABLE_NON_VOLATILE) |
| 509 | s += sprintf(s, "nv"); |
| 510 | else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 511 | s += sprintf(s, "boot"); |
| 512 | else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) |
| 513 | s += sprintf(s, "run"); |
| 514 | |
| 515 | attributes &= ~attr; |
| 516 | if (attributes) |
| 517 | s += sprintf(s, ","); |
| 518 | } |
| 519 | s += sprintf(s, "}"); |
| 520 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame^] | 521 | if (old_size) |
| 522 | /* APPEND_WRITE */ |
| 523 | s += sprintf(s, old_val); |
| 524 | else |
| 525 | s += sprintf(s, "(blob)"); |
| 526 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 527 | /* store payload: */ |
Heinrich Schuchardt | 807899d | 2019-01-18 18:54:26 +0100 | [diff] [blame] | 528 | s = bin2hex(s, data, data_size); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 529 | *s = '\0'; |
| 530 | |
Heinrich Schuchardt | 55111c3 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 531 | EFI_PRINT("setting: %s=%s\n", native_name, val); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 532 | |
| 533 | if (env_set(native_name, val)) |
| 534 | ret = EFI_DEVICE_ERROR; |
| 535 | |
Heinrich Schuchardt | af1a920 | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 536 | out: |
| 537 | free(native_name); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 538 | free(val); |
| 539 | |
| 540 | return EFI_EXIT(ret); |
| 541 | } |
Heinrich Schuchardt | 9b19dae | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 542 | |
| 543 | /** |
| 544 | * efi_query_variable_info() - get information about EFI variables |
| 545 | * |
| 546 | * This function implements the QueryVariableInfo() runtime service. |
| 547 | * |
| 548 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 549 | * details. |
| 550 | * |
| 551 | * @attributes: bitmask to select variables to be |
| 552 | * queried |
| 553 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 554 | * selected variable types |
| 555 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 556 | * selected variable types |
| 557 | * @maximum_variable_size: maximum size of a variable of the |
| 558 | * selected type |
| 559 | * Returns: status code |
| 560 | */ |
| 561 | efi_status_t __efi_runtime EFIAPI efi_query_variable_info( |
| 562 | u32 attributes, |
| 563 | u64 *maximum_variable_storage_size, |
| 564 | u64 *remaining_variable_storage_size, |
| 565 | u64 *maximum_variable_size) |
| 566 | { |
| 567 | return EFI_UNSUPPORTED; |
| 568 | } |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 569 | |
| 570 | /** |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 571 | * efi_get_variable_runtime() - runtime implementation of GetVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 572 | * |
| 573 | * @variable_name: name of the variable |
| 574 | * @vendor: vendor GUID |
| 575 | * @attributes: attributes of the variable |
| 576 | * @data_size: size of the buffer to which the variable value is copied |
| 577 | * @data: buffer to which the variable value is copied |
| 578 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 579 | */ |
| 580 | static efi_status_t __efi_runtime EFIAPI |
| 581 | efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 582 | u32 *attributes, efi_uintn_t *data_size, void *data) |
| 583 | { |
| 584 | return EFI_UNSUPPORTED; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * efi_get_next_variable_name_runtime() - runtime implementation of |
| 589 | * GetNextVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 590 | * |
| 591 | * @variable_name_size: size of variable_name buffer in byte |
| 592 | * @variable_name: name of uefi variable's name in u16 |
| 593 | * @vendor: vendor's guid |
| 594 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 595 | */ |
| 596 | static efi_status_t __efi_runtime EFIAPI |
| 597 | efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size, |
| 598 | u16 *variable_name, const efi_guid_t *vendor) |
| 599 | { |
| 600 | return EFI_UNSUPPORTED; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * efi_set_variable_runtime() - runtime implementation of SetVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 605 | * |
| 606 | * @variable_name: name of the variable |
| 607 | * @vendor: vendor GUID |
| 608 | * @attributes: attributes of the variable |
| 609 | * @data_size: size of the buffer with the variable value |
| 610 | * @data: buffer with the variable value |
| 611 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 612 | */ |
| 613 | static efi_status_t __efi_runtime EFIAPI |
| 614 | efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 615 | u32 attributes, efi_uintn_t data_size, |
| 616 | const void *data) |
| 617 | { |
| 618 | return EFI_UNSUPPORTED; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * efi_variables_boot_exit_notify() - notify ExitBootServices() is called |
| 623 | */ |
| 624 | void efi_variables_boot_exit_notify(void) |
| 625 | { |
| 626 | efi_runtime_services.get_variable = efi_get_variable_runtime; |
| 627 | efi_runtime_services.get_next_variable_name = |
| 628 | efi_get_next_variable_name_runtime; |
| 629 | efi_runtime_services.set_variable = efi_set_variable_runtime; |
| 630 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 631 | } |
| 632 | |
| 633 | /** |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 634 | * efi_init_variables() - initialize variable services |
| 635 | * |
| 636 | * Return: status code |
| 637 | */ |
| 638 | efi_status_t efi_init_variables(void) |
| 639 | { |
| 640 | return EFI_SUCCESS; |
| 641 | } |