1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
13 #define READ_ONLY BIT(31)
16 * Mapping between EFI variables and u-boot variables:
18 * efi_$guid_$varname = {attributes}(type)value
22 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
23 * "{ro,boot,run}(blob)0000000000000000"
24 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
27 * The attributes are a comma separated list of these possible
31 * + boot - boot-services access
32 * + run - runtime access
34 * NOTE: with current implementation, no variables are available after
35 * ExitBootServices, and all are persisted (if possible).
37 * If not specified, the attributes default to "{boot}".
39 * The required type is one of:
41 * + utf8 - raw utf8 string
42 * + blob - arbitrary length hex string
44 * Maybe a utf16 type would be useful to for a string value to be auto
48 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
50 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
51 const efi_guid_t *vendor)
56 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
57 *native = malloc(len);
59 return EFI_OUT_OF_RESOURCES;
62 pos += sprintf(pos, "efi_%pUl_", vendor);
63 utf16_utf8_strcpy(&pos, variable_name);
68 static const char *prefix(const char *str, const char *prefix)
70 size_t n = strlen(prefix);
71 if (!strncmp(prefix, str, n))
76 /* parse attributes part of variable value, if present: */
77 static const char *parse_attr(const char *str, u32 *attrp)
83 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
92 if ((s = prefix(str, "ro"))) {
94 } else if ((s = prefix(str, "boot"))) {
95 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
96 } else if ((s = prefix(str, "run"))) {
97 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
99 printf("invalid attribute: %s\n", str);
114 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetVariable.28.29 */
115 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
116 const efi_guid_t *vendor, u32 *attributes,
117 efi_uintn_t *data_size, void *data)
121 unsigned long in_size;
125 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
128 if (!variable_name || !vendor || !data_size)
129 return EFI_EXIT(EFI_INVALID_PARAMETER);
131 ret = efi_to_native(&native_name, variable_name, vendor);
133 return EFI_EXIT(ret);
135 debug("%s: get '%s'\n", __func__, native_name);
137 val = env_get(native_name);
140 return EFI_EXIT(EFI_NOT_FOUND);
142 val = parse_attr(val, &attr);
144 in_size = *data_size;
146 if ((s = prefix(val, "(blob)"))) {
147 size_t len = strlen(s);
149 /* number of hexadecimal digits must be even */
151 return EFI_EXIT(EFI_DEVICE_ERROR);
153 /* two characters per byte: */
158 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
161 return EFI_EXIT(EFI_INVALID_PARAMETER);
163 if (hex2bin(data, s, len))
164 return EFI_EXIT(EFI_DEVICE_ERROR);
166 debug("%s: got value: \"%s\"\n", __func__, s);
167 } else if ((s = prefix(val, "(utf8)"))) {
168 unsigned len = strlen(s) + 1;
173 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
176 return EFI_EXIT(EFI_INVALID_PARAMETER);
178 memcpy(data, s, len);
179 ((char *)data)[len] = '\0';
181 debug("%s: got value: \"%s\"\n", __func__, (char *)data);
183 debug("%s: invalid value: '%s'\n", __func__, val);
184 return EFI_EXIT(EFI_DEVICE_ERROR);
188 *attributes = attr & EFI_VARIABLE_MASK;
190 return EFI_EXIT(EFI_SUCCESS);
193 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetNextVariableName.28.29 */
194 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
196 const efi_guid_t *vendor)
198 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
200 return EFI_EXIT(EFI_DEVICE_ERROR);
203 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#SetVariable.28.29 */
204 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
205 const efi_guid_t *vendor, u32 attributes,
206 efi_uintn_t data_size, const void *data)
208 char *native_name = NULL, *val = NULL, *s;
209 efi_status_t ret = EFI_SUCCESS;
212 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
215 if (!variable_name || !vendor) {
216 ret = EFI_INVALID_PARAMETER;
220 ret = efi_to_native(&native_name, variable_name, vendor);
224 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
226 if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
227 /* delete the variable: */
228 env_set(native_name, NULL);
233 val = env_get(native_name);
235 parse_attr(val, &attr);
237 if (attr & READ_ONLY) {
238 /* We should not free val */
240 ret = EFI_WRITE_PROTECTED;
245 val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
247 ret = EFI_OUT_OF_RESOURCES;
253 /* store attributes: */
254 attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
255 s += sprintf(s, "{");
257 u32 attr = 1 << (ffs(attributes) - 1);
259 if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
260 s += sprintf(s, "boot");
261 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
262 s += sprintf(s, "run");
266 s += sprintf(s, ",");
268 s += sprintf(s, "}");
271 s += sprintf(s, "(blob)");
272 s = bin2hex(s, data, data_size);
275 debug("%s: setting: %s=%s\n", __func__, native_name, val);
277 if (env_set(native_name, val))
278 ret = EFI_DEVICE_ERROR;
284 return EFI_EXIT(ret);