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