Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2 | /* |
| 3 | * EFI utils |
| 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <malloc.h> |
| 9 | #include <charset.h> |
| 10 | #include <efi_loader.h> |
Heinrich Schuchardt | 6e37fa2 | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 11 | #include <hexdump.h> |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 12 | #include <environment.h> |
| 13 | #include <search.h> |
| 14 | #include <uuid.h> |
Rob Clark | ad644e7 | 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 | 506dc52 | 2018-09-23 04:08:09 +0200 | [diff] [blame] | 51 | #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_")) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 52 | |
Heinrich Schuchardt | 77d4d39 | 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 | dcdb64f | 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 | 0bda81b | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 67 | const efi_guid_t *vendor) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 68 | { |
| 69 | size_t len; |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 70 | char *pos; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 71 | |
Heinrich Schuchardt | dcdb64f | 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 | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 76 | |
Heinrich Schuchardt | dcdb64f | 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 | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 80 | |
| 81 | return EFI_SUCCESS; |
| 82 | } |
| 83 | |
Heinrich Schuchardt | 77d4d39 | 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 | ad644e7 | 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 | 77d4d39 | 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 | ad644e7 | 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; |
| 128 | } else if ((s = prefix(str, "boot"))) { |
| 129 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 130 | } else if ((s = prefix(str, "run"))) { |
| 131 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 132 | } else { |
| 133 | printf("invalid attribute: %s\n", str); |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | str = s; |
| 138 | sep = ','; |
| 139 | } |
| 140 | |
| 141 | str++; |
| 142 | |
| 143 | *attrp = attr; |
| 144 | |
| 145 | return str; |
| 146 | } |
| 147 | |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 148 | /** |
| 149 | * efi_efi_get_variable() - retrieve value of a UEFI variable |
| 150 | * |
| 151 | * This function implements the GetVariable runtime service. |
| 152 | * |
| 153 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 154 | * details. |
| 155 | * |
| 156 | * @variable_name: name of the variable |
| 157 | * @vendor: vendor GUID |
| 158 | * @attributes: attributes of the variable |
| 159 | * @data_size: size of the buffer to which the variable value is copied |
| 160 | * @data: buffer to which the variable value is copied |
| 161 | * Return: status code |
| 162 | */ |
Heinrich Schuchardt | 0bda81b | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 163 | efi_status_t EFIAPI efi_get_variable(u16 *variable_name, |
| 164 | const efi_guid_t *vendor, u32 *attributes, |
| 165 | efi_uintn_t *data_size, void *data) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 166 | { |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 167 | char *native_name; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 168 | efi_status_t ret; |
| 169 | unsigned long in_size; |
| 170 | const char *val, *s; |
| 171 | u32 attr; |
| 172 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 173 | EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes, |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 174 | data_size, data); |
| 175 | |
| 176 | if (!variable_name || !vendor || !data_size) |
| 177 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 178 | |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 179 | ret = efi_to_native(&native_name, variable_name, vendor); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 180 | if (ret) |
| 181 | return EFI_EXIT(ret); |
| 182 | |
Heinrich Schuchardt | cc2ed79 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 183 | EFI_PRINT("get '%s'\n", native_name); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 184 | |
| 185 | val = env_get(native_name); |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 186 | free(native_name); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 187 | if (!val) |
| 188 | return EFI_EXIT(EFI_NOT_FOUND); |
| 189 | |
| 190 | val = parse_attr(val, &attr); |
| 191 | |
| 192 | in_size = *data_size; |
| 193 | |
| 194 | if ((s = prefix(val, "(blob)"))) { |
Heinrich Schuchardt | 6e37fa2 | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 195 | size_t len = strlen(s); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 196 | |
Ivan Gorinov | d73c8bc | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 197 | /* number of hexadecimal digits must be even */ |
| 198 | if (len & 1) |
| 199 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 200 | |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 201 | /* two characters per byte: */ |
Ivan Gorinov | d73c8bc | 2018-05-11 13:18:25 -0700 | [diff] [blame] | 202 | len /= 2; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 203 | *data_size = len; |
| 204 | |
Heinrich Schuchardt | 487d8c7 | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 205 | if (in_size < len) { |
| 206 | ret = EFI_BUFFER_TOO_SMALL; |
| 207 | goto out; |
| 208 | } |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 209 | |
| 210 | if (!data) |
| 211 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 212 | |
Heinrich Schuchardt | 6e37fa2 | 2019-01-18 12:31:54 +0100 | [diff] [blame] | 213 | if (hex2bin(data, s, len)) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 214 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 215 | |
Heinrich Schuchardt | cc2ed79 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 216 | EFI_PRINT("got value: \"%s\"\n", s); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 217 | } else if ((s = prefix(val, "(utf8)"))) { |
| 218 | unsigned len = strlen(s) + 1; |
| 219 | |
| 220 | *data_size = len; |
| 221 | |
Heinrich Schuchardt | 487d8c7 | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 222 | if (in_size < len) { |
| 223 | ret = EFI_BUFFER_TOO_SMALL; |
| 224 | goto out; |
| 225 | } |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 226 | |
| 227 | if (!data) |
| 228 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 229 | |
| 230 | memcpy(data, s, len); |
| 231 | ((char *)data)[len] = '\0'; |
| 232 | |
Heinrich Schuchardt | cc2ed79 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 233 | EFI_PRINT("got value: \"%s\"\n", (char *)data); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 234 | } else { |
Heinrich Schuchardt | cc2ed79 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 235 | EFI_PRINT("invalid value: '%s'\n", val); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 236 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 237 | } |
| 238 | |
Heinrich Schuchardt | 487d8c7 | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 239 | out: |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 240 | if (attributes) |
| 241 | *attributes = attr & EFI_VARIABLE_MASK; |
| 242 | |
Heinrich Schuchardt | 487d8c7 | 2019-05-15 19:32:43 +0200 | [diff] [blame] | 243 | return EFI_EXIT(ret); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 244 | } |
| 245 | |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 246 | static char *efi_variables_list; |
| 247 | static char *efi_cur_variable; |
| 248 | |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 249 | /** |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 250 | * parse_uboot_variable() - parse a u-boot variable and get uefi-related |
| 251 | * information |
| 252 | * @variable: whole data of u-boot variable (ie. name=value) |
| 253 | * @variable_name_size: size of variable_name buffer in byte |
| 254 | * @variable_name: name of uefi variable in u16, null-terminated |
| 255 | * @vendor: vendor's guid |
| 256 | * @attributes: attributes |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 257 | * |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 258 | * A uefi variable is encoded into a u-boot variable as described above. |
| 259 | * This function parses such a u-boot variable and retrieve uefi-related |
| 260 | * information into respective parameters. In return, variable_name_size |
| 261 | * is the size of variable name including NULL. |
| 262 | * |
| 263 | * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when |
| 264 | the entire variable list has been returned, |
| 265 | otherwise non-zero status code |
| 266 | */ |
| 267 | static efi_status_t parse_uboot_variable(char *variable, |
| 268 | efi_uintn_t *variable_name_size, |
| 269 | u16 *variable_name, |
| 270 | const efi_guid_t *vendor, |
| 271 | u32 *attributes) |
| 272 | { |
| 273 | char *guid, *name, *end, c; |
| 274 | unsigned long name_len; |
| 275 | u16 *p; |
| 276 | |
| 277 | guid = strchr(variable, '_'); |
| 278 | if (!guid) |
| 279 | return EFI_INVALID_PARAMETER; |
| 280 | guid++; |
| 281 | name = strchr(guid, '_'); |
| 282 | if (!name) |
| 283 | return EFI_INVALID_PARAMETER; |
| 284 | name++; |
| 285 | end = strchr(name, '='); |
| 286 | if (!end) |
| 287 | return EFI_INVALID_PARAMETER; |
| 288 | |
| 289 | name_len = end - name; |
| 290 | if (*variable_name_size < (name_len + 1)) { |
| 291 | *variable_name_size = name_len + 1; |
| 292 | return EFI_BUFFER_TOO_SMALL; |
| 293 | } |
| 294 | end++; /* point to value */ |
| 295 | |
| 296 | /* variable name */ |
| 297 | p = variable_name; |
| 298 | utf8_utf16_strncpy(&p, name, name_len); |
| 299 | variable_name[name_len] = 0; |
| 300 | *variable_name_size = name_len + 1; |
| 301 | |
| 302 | /* guid */ |
| 303 | c = *(name - 1); |
| 304 | *(name - 1) = '\0'; /* guid need be null-terminated here */ |
| 305 | uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID); |
| 306 | *(name - 1) = c; |
| 307 | |
| 308 | /* attributes */ |
| 309 | parse_attr(end, attributes); |
| 310 | |
| 311 | return EFI_SUCCESS; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * efi_get_next_variable_name() - enumerate the current variable names |
| 316 | * @variable_name_size: size of variable_name buffer in byte |
| 317 | * @variable_name: name of uefi variable's name in u16 |
| 318 | * @vendor: vendor's guid |
| 319 | * |
| 320 | * This function implements the GetNextVariableName service. |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 321 | * |
| 322 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 323 | * details: http://wiki.phoenix.com/wiki/index.php/ |
| 324 | * EFI_RUNTIME_SERVICES#GetNextVariableName.28.29 |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 325 | * |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 326 | * Return: status code |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 327 | */ |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 328 | efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, |
| 329 | u16 *variable_name, |
Heinrich Schuchardt | 0bda81b | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 330 | const efi_guid_t *vendor) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 331 | { |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 332 | char *native_name, *variable; |
| 333 | ssize_t name_len, list_len; |
| 334 | char regex[256]; |
| 335 | char * const regexlist[] = {regex}; |
| 336 | u32 attributes; |
| 337 | int i; |
| 338 | efi_status_t ret; |
| 339 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 340 | EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 341 | |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 342 | if (!variable_name_size || !variable_name || !vendor) |
Heinrich Schuchardt | e7dae58 | 2019-03-19 18:36:21 +0100 | [diff] [blame] | 343 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 344 | |
| 345 | if (variable_name[0]) { |
| 346 | /* check null-terminated string */ |
| 347 | for (i = 0; i < *variable_name_size; i++) |
| 348 | if (!variable_name[i]) |
| 349 | break; |
| 350 | if (i >= *variable_name_size) |
| 351 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 352 | |
| 353 | /* search for the last-returned variable */ |
| 354 | ret = efi_to_native(&native_name, variable_name, vendor); |
| 355 | if (ret) |
| 356 | return EFI_EXIT(ret); |
| 357 | |
| 358 | name_len = strlen(native_name); |
| 359 | for (variable = efi_variables_list; variable && *variable;) { |
| 360 | if (!strncmp(variable, native_name, name_len) && |
| 361 | variable[name_len] == '=') |
| 362 | break; |
| 363 | |
| 364 | variable = strchr(variable, '\n'); |
| 365 | if (variable) |
| 366 | variable++; |
| 367 | } |
| 368 | |
| 369 | free(native_name); |
| 370 | if (!(variable && *variable)) |
| 371 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 372 | |
| 373 | /* next variable */ |
| 374 | variable = strchr(variable, '\n'); |
| 375 | if (variable) |
| 376 | variable++; |
| 377 | if (!(variable && *variable)) |
| 378 | return EFI_EXIT(EFI_NOT_FOUND); |
| 379 | } else { |
| 380 | /* |
| 381 | *new search: free a list used in the previous search |
| 382 | */ |
| 383 | free(efi_variables_list); |
| 384 | efi_variables_list = NULL; |
| 385 | efi_cur_variable = NULL; |
| 386 | |
| 387 | snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*"); |
| 388 | list_len = hexport_r(&env_htab, '\n', |
| 389 | H_MATCH_REGEX | H_MATCH_KEY, |
| 390 | &efi_variables_list, 0, 1, regexlist); |
Heinrich Schuchardt | eefb790 | 2019-01-22 20:10:46 +0100 | [diff] [blame] | 391 | /* 1 indicates that no match was found */ |
| 392 | if (list_len <= 1) |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 393 | return EFI_EXIT(EFI_NOT_FOUND); |
| 394 | |
| 395 | variable = efi_variables_list; |
| 396 | } |
| 397 | |
| 398 | ret = parse_uboot_variable(variable, variable_name_size, variable_name, |
| 399 | vendor, &attributes); |
| 400 | |
| 401 | return EFI_EXIT(ret); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 404 | /** |
| 405 | * efi_efi_set_variable() - set value of a UEFI variable |
| 406 | * |
| 407 | * This function implements the SetVariable runtime service. |
| 408 | * |
| 409 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 410 | * details. |
| 411 | * |
| 412 | * @variable_name: name of the variable |
| 413 | * @vendor: vendor GUID |
| 414 | * @attributes: attributes of the variable |
| 415 | * @data_size: size of the buffer with the variable value |
| 416 | * @data: buffer with the variable value |
| 417 | * Return: status code |
| 418 | */ |
Heinrich Schuchardt | 0bda81b | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 419 | efi_status_t EFIAPI efi_set_variable(u16 *variable_name, |
| 420 | const efi_guid_t *vendor, u32 attributes, |
Heinrich Schuchardt | 452257a | 2018-12-30 21:03:15 +0100 | [diff] [blame] | 421 | efi_uintn_t data_size, const void *data) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 422 | { |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 423 | char *native_name = NULL, *val = NULL, *s; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 424 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 425 | u32 attr; |
| 426 | |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 427 | EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes, |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 428 | data_size, data); |
| 429 | |
AKASHI Takahiro | dbebae5 | 2019-05-24 15:59:02 +0900 | [diff] [blame^] | 430 | /* TODO: implement APPEND_WRITE */ |
| 431 | if (!variable_name || !vendor || |
| 432 | (attributes & EFI_VARIABLE_APPEND_WRITE)) { |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 433 | ret = EFI_INVALID_PARAMETER; |
| 434 | goto out; |
| 435 | } |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 436 | |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 437 | ret = efi_to_native(&native_name, variable_name, vendor); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 438 | if (ret) |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 439 | goto out; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 440 | |
| 441 | #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS) |
| 442 | |
| 443 | if ((data_size == 0) || !(attributes & ACCESS_ATTR)) { |
| 444 | /* delete the variable: */ |
| 445 | env_set(native_name, NULL); |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 446 | ret = EFI_SUCCESS; |
| 447 | goto out; |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | val = env_get(native_name); |
| 451 | if (val) { |
| 452 | parse_attr(val, &attr); |
| 453 | |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 454 | if (attr & READ_ONLY) { |
| 455 | /* We should not free val */ |
| 456 | val = NULL; |
| 457 | ret = EFI_WRITE_PROTECTED; |
| 458 | goto out; |
| 459 | } |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1); |
Heinrich Schuchardt | dadc2bd | 2018-10-02 05:30:05 +0200 | [diff] [blame] | 463 | if (!val) { |
| 464 | ret = EFI_OUT_OF_RESOURCES; |
| 465 | goto out; |
| 466 | } |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 467 | |
| 468 | s = val; |
| 469 | |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 470 | /* |
| 471 | * store attributes |
| 472 | * TODO: several attributes are not supported |
| 473 | */ |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 474 | attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS); |
| 475 | s += sprintf(s, "{"); |
| 476 | while (attributes) { |
| 477 | u32 attr = 1 << (ffs(attributes) - 1); |
| 478 | |
| 479 | if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) |
| 480 | s += sprintf(s, "boot"); |
| 481 | else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) |
| 482 | s += sprintf(s, "run"); |
| 483 | |
| 484 | attributes &= ~attr; |
| 485 | if (attributes) |
| 486 | s += sprintf(s, ","); |
| 487 | } |
| 488 | s += sprintf(s, "}"); |
| 489 | |
| 490 | /* store payload: */ |
| 491 | s += sprintf(s, "(blob)"); |
Heinrich Schuchardt | 8377ee3 | 2019-01-18 18:54:26 +0100 | [diff] [blame] | 492 | s = bin2hex(s, data, data_size); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 493 | *s = '\0'; |
| 494 | |
Heinrich Schuchardt | cc2ed79 | 2019-04-04 21:36:44 +0200 | [diff] [blame] | 495 | EFI_PRINT("setting: %s=%s\n", native_name, val); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 496 | |
| 497 | if (env_set(native_name, val)) |
| 498 | ret = EFI_DEVICE_ERROR; |
| 499 | |
Heinrich Schuchardt | dcdb64f | 2018-08-31 21:31:31 +0200 | [diff] [blame] | 500 | out: |
| 501 | free(native_name); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 502 | free(val); |
| 503 | |
| 504 | return EFI_EXIT(ret); |
| 505 | } |