4 * Copyright (c) 2017 Rob Clark
6 * SPDX-License-Identifier: GPL-2.0+
11 #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 MAX_VAR_NAME 31
49 #define MAX_NATIVE_VAR_NAME \
50 (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx_") + \
51 (MAX_VAR_NAME * MAX_UTF8_PER_UTF16))
53 static int hex(unsigned char ch)
55 if (ch >= 'a' && ch <= 'f')
57 if (ch >= '0' && ch <= '9')
59 if (ch >= 'A' && ch <= 'F')
64 static const char *hex2mem(u8 *mem, const char *hexstr, int count)
66 memset(mem, 0, count/2);
73 if (!count || !*hexstr)
76 nibble = hex(*hexstr);
84 if (!count || !*hexstr)
87 nibble = hex(*hexstr);
91 *mem = (*mem << 4) | nibble;
104 static char *mem2hex(char *hexstr, const u8 *mem, int count)
106 static const char hexchars[] = "0123456789abcdef";
108 while (count-- > 0) {
110 *hexstr++ = hexchars[ch >> 4];
111 *hexstr++ = hexchars[ch & 0xf];
117 static efi_status_t efi_to_native(char *native, s16 *variable_name,
122 len = utf16_strlen((u16 *)variable_name);
123 if (len >= MAX_VAR_NAME)
124 return EFI_DEVICE_ERROR;
126 native += sprintf(native, "efi_%pUl_", vendor);
127 native = (char *)utf16_to_utf8((u8 *)native, (u16 *)variable_name, len);
133 static const char *prefix(const char *str, const char *prefix)
135 size_t n = strlen(prefix);
136 if (!strncmp(prefix, str, n))
141 /* parse attributes part of variable value, if present: */
142 static const char *parse_attr(const char *str, u32 *attrp)
148 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
152 while (*str == sep) {
157 if ((s = prefix(str, "ro"))) {
159 } else if ((s = prefix(str, "boot"))) {
160 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
161 } else if ((s = prefix(str, "run"))) {
162 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
164 printf("invalid attribute: %s\n", str);
179 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetVariable.28.29 */
180 efi_status_t EFIAPI efi_get_variable(s16 *variable_name,
181 efi_guid_t *vendor, u32 *attributes,
182 unsigned long *data_size, void *data)
184 char native_name[MAX_NATIVE_VAR_NAME + 1];
186 unsigned long in_size;
190 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
193 if (!variable_name || !vendor || !data_size)
194 return EFI_EXIT(EFI_INVALID_PARAMETER);
196 ret = efi_to_native(native_name, variable_name, vendor);
198 return EFI_EXIT(ret);
200 debug("%s: get '%s'\n", __func__, native_name);
202 val = env_get(native_name);
204 return EFI_EXIT(EFI_NOT_FOUND);
206 val = parse_attr(val, &attr);
208 in_size = *data_size;
210 if ((s = prefix(val, "(blob)"))) {
211 unsigned len = strlen(s);
213 /* two characters per byte: */
214 len = DIV_ROUND_UP(len, 2);
218 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
221 return EFI_EXIT(EFI_INVALID_PARAMETER);
223 if (hex2mem(data, s, len * 2))
224 return EFI_EXIT(EFI_DEVICE_ERROR);
226 debug("%s: got value: \"%s\"\n", __func__, s);
227 } else if ((s = prefix(val, "(utf8)"))) {
228 unsigned len = strlen(s) + 1;
233 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
236 return EFI_EXIT(EFI_INVALID_PARAMETER);
238 memcpy(data, s, len);
239 ((char *)data)[len] = '\0';
241 debug("%s: got value: \"%s\"\n", __func__, (char *)data);
243 debug("%s: invalid value: '%s'\n", __func__, val);
244 return EFI_EXIT(EFI_DEVICE_ERROR);
248 *attributes = attr & EFI_VARIABLE_MASK;
250 return EFI_EXIT(EFI_SUCCESS);
253 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetNextVariableName.28.29 */
254 efi_status_t EFIAPI efi_get_next_variable(
255 unsigned long *variable_name_size,
256 s16 *variable_name, efi_guid_t *vendor)
258 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
260 return EFI_EXIT(EFI_DEVICE_ERROR);
263 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#SetVariable.28.29 */
264 efi_status_t EFIAPI efi_set_variable(s16 *variable_name,
265 efi_guid_t *vendor, u32 attributes,
266 unsigned long data_size, void *data)
268 char native_name[MAX_NATIVE_VAR_NAME + 1];
269 efi_status_t ret = EFI_SUCCESS;
273 EFI_ENTRY("\"%ls\" %pUl %x %lu %p", variable_name, vendor, attributes,
276 if (!variable_name || !vendor)
277 return EFI_EXIT(EFI_INVALID_PARAMETER);
279 ret = efi_to_native(native_name, variable_name, vendor);
281 return EFI_EXIT(ret);
283 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
285 if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
286 /* delete the variable: */
287 env_set(native_name, NULL);
288 return EFI_EXIT(EFI_SUCCESS);
291 val = env_get(native_name);
293 parse_attr(val, &attr);
295 if (attr & READ_ONLY)
296 return EFI_EXIT(EFI_WRITE_PROTECTED);
299 val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
301 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
305 /* store attributes: */
306 attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
307 s += sprintf(s, "{");
309 u32 attr = 1 << (ffs(attributes) - 1);
311 if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
312 s += sprintf(s, "boot");
313 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
314 s += sprintf(s, "run");
318 s += sprintf(s, ",");
320 s += sprintf(s, "}");
323 s += sprintf(s, "(blob)");
324 s = mem2hex(s, data, data_size);
327 debug("%s: setting: %s=%s\n", __func__, native_name, val);
329 if (env_set(native_name, val))
330 ret = EFI_DEVICE_ERROR;
334 return EFI_EXIT(ret);