1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
12 #define READ_ONLY BIT(31)
15 * Mapping between EFI variables and u-boot variables:
17 * efi_$guid_$varname = {attributes}(type)value
21 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
22 * "{ro,boot,run}(blob)0000000000000000"
23 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
26 * The attributes are a comma separated list of these possible
30 * + boot - boot-services access
31 * + run - runtime access
33 * NOTE: with current implementation, no variables are available after
34 * ExitBootServices, and all are persisted (if possible).
36 * If not specified, the attributes default to "{boot}".
38 * The required type is one of:
40 * + utf8 - raw utf8 string
41 * + blob - arbitrary length hex string
43 * Maybe a utf16 type would be useful to for a string value to be auto
47 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
49 static int hex(int ch
)
51 if (ch
>= 'a' && ch
<= 'f')
53 if (ch
>= '0' && ch
<= '9')
55 if (ch
>= 'A' && ch
<= 'F')
60 static int hex2mem(u8
*mem
, const char *hexstr
, int size
)
65 for (i
= 0; i
< size
; i
++) {
69 nibble
= hex(*hexstr
);
76 nibble
= hex(*hexstr
);
80 *mem
= (*mem
<< 4) | nibble
;
88 static char *mem2hex(char *hexstr
, const u8
*mem
, int count
)
90 static const char hexchars
[] = "0123456789abcdef";
94 *hexstr
++ = hexchars
[ch
>> 4];
95 *hexstr
++ = hexchars
[ch
& 0xf];
101 static efi_status_t
efi_to_native(char **native
, const u16
*variable_name
,
107 len
= PREFIX_LEN
+ utf16_utf8_strlen(variable_name
) + 1;
108 *native
= malloc(len
);
110 return EFI_OUT_OF_RESOURCES
;
113 pos
+= sprintf(pos
, "efi_%pUl_", vendor
);
114 utf16_utf8_strcpy(&pos
, variable_name
);
119 static const char *prefix(const char *str
, const char *prefix
)
121 size_t n
= strlen(prefix
);
122 if (!strncmp(prefix
, str
, n
))
127 /* parse attributes part of variable value, if present: */
128 static const char *parse_attr(const char *str
, u32
*attrp
)
134 *attrp
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
138 while (*str
== sep
) {
143 if ((s
= prefix(str
, "ro"))) {
145 } else if ((s
= prefix(str
, "boot"))) {
146 attr
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
147 } else if ((s
= prefix(str
, "run"))) {
148 attr
|= EFI_VARIABLE_RUNTIME_ACCESS
;
150 printf("invalid attribute: %s\n", str
);
165 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetVariable.28.29 */
166 efi_status_t EFIAPI
efi_get_variable(u16
*variable_name
, efi_guid_t
*vendor
,
167 u32
*attributes
, efi_uintn_t
*data_size
,
172 unsigned long in_size
;
176 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name
, vendor
, attributes
,
179 if (!variable_name
|| !vendor
|| !data_size
)
180 return EFI_EXIT(EFI_INVALID_PARAMETER
);
182 ret
= efi_to_native(&native_name
, variable_name
, vendor
);
184 return EFI_EXIT(ret
);
186 debug("%s: get '%s'\n", __func__
, native_name
);
188 val
= env_get(native_name
);
191 return EFI_EXIT(EFI_NOT_FOUND
);
193 val
= parse_attr(val
, &attr
);
195 in_size
= *data_size
;
197 if ((s
= prefix(val
, "(blob)"))) {
198 unsigned len
= strlen(s
);
200 /* number of hexadecimal digits must be even */
202 return EFI_EXIT(EFI_DEVICE_ERROR
);
204 /* two characters per byte: */
209 return EFI_EXIT(EFI_BUFFER_TOO_SMALL
);
212 return EFI_EXIT(EFI_INVALID_PARAMETER
);
214 if (hex2mem(data
, s
, len
) != len
)
215 return EFI_EXIT(EFI_DEVICE_ERROR
);
217 debug("%s: got value: \"%s\"\n", __func__
, s
);
218 } else if ((s
= prefix(val
, "(utf8)"))) {
219 unsigned len
= strlen(s
) + 1;
224 return EFI_EXIT(EFI_BUFFER_TOO_SMALL
);
227 return EFI_EXIT(EFI_INVALID_PARAMETER
);
229 memcpy(data
, s
, len
);
230 ((char *)data
)[len
] = '\0';
232 debug("%s: got value: \"%s\"\n", __func__
, (char *)data
);
234 debug("%s: invalid value: '%s'\n", __func__
, val
);
235 return EFI_EXIT(EFI_DEVICE_ERROR
);
239 *attributes
= attr
& EFI_VARIABLE_MASK
;
241 return EFI_EXIT(EFI_SUCCESS
);
244 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetNextVariableName.28.29 */
245 efi_status_t EFIAPI
efi_get_next_variable_name(efi_uintn_t
*variable_name_size
,
249 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size
, variable_name
, vendor
);
251 return EFI_EXIT(EFI_DEVICE_ERROR
);
254 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#SetVariable.28.29 */
255 efi_status_t EFIAPI
efi_set_variable(u16
*variable_name
, efi_guid_t
*vendor
,
256 u32 attributes
, efi_uintn_t data_size
,
259 char *native_name
= NULL
, *val
= NULL
, *s
;
260 efi_status_t ret
= EFI_SUCCESS
;
263 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name
, vendor
, attributes
,
266 if (!variable_name
|| !vendor
) {
267 ret
= EFI_INVALID_PARAMETER
;
271 ret
= efi_to_native(&native_name
, variable_name
, vendor
);
275 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
277 if ((data_size
== 0) || !(attributes
& ACCESS_ATTR
)) {
278 /* delete the variable: */
279 env_set(native_name
, NULL
);
284 val
= env_get(native_name
);
286 parse_attr(val
, &attr
);
288 if (attr
& READ_ONLY
) {
289 /* We should not free val */
291 ret
= EFI_WRITE_PROTECTED
;
296 val
= malloc(2 * data_size
+ strlen("{ro,run,boot}(blob)") + 1);
298 return EFI_EXIT(EFI_OUT_OF_RESOURCES
);
302 /* store attributes: */
303 attributes
&= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
);
304 s
+= sprintf(s
, "{");
306 u32 attr
= 1 << (ffs(attributes
) - 1);
308 if (attr
== EFI_VARIABLE_BOOTSERVICE_ACCESS
)
309 s
+= sprintf(s
, "boot");
310 else if (attr
== EFI_VARIABLE_RUNTIME_ACCESS
)
311 s
+= sprintf(s
, "run");
315 s
+= sprintf(s
, ",");
317 s
+= sprintf(s
, "}");
320 s
+= sprintf(s
, "(blob)");
321 s
= mem2hex(s
, data
, data_size
);
324 debug("%s: setting: %s=%s\n", __func__
, native_name
, val
);
326 if (env_set(native_name
, val
))
327 ret
= EFI_DEVICE_ERROR
;
333 return EFI_EXIT(ret
);