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 MAX_VAR_NAME 31
48 #define MAX_NATIVE_VAR_NAME \
49 (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx_") + \
50 (MAX_VAR_NAME * MAX_UTF8_PER_UTF16))
52 static int hex(unsigned char ch)
54 if (ch >= 'a' && ch <= 'f')
56 if (ch >= '0' && ch <= '9')
58 if (ch >= 'A' && ch <= 'F')
63 static const char *hex2mem(u8 *mem, const char *hexstr, int count)
65 memset(mem, 0, count/2);
72 if (!count || !*hexstr)
75 nibble = hex(*hexstr);
83 if (!count || !*hexstr)
86 nibble = hex(*hexstr);
90 *mem = (*mem << 4) | nibble;
103 static char *mem2hex(char *hexstr, const u8 *mem, int count)
105 static const char hexchars[] = "0123456789abcdef";
107 while (count-- > 0) {
109 *hexstr++ = hexchars[ch >> 4];
110 *hexstr++ = hexchars[ch & 0xf];
116 static efi_status_t efi_to_native(char *native, s16 *variable_name,
121 len = utf16_strlen((u16 *)variable_name);
122 if (len >= MAX_VAR_NAME)
123 return EFI_DEVICE_ERROR;
125 native += sprintf(native, "efi_%pUl_", vendor);
126 native = (char *)utf16_to_utf8((u8 *)native, (u16 *)variable_name, len);
132 static const char *prefix(const char *str, const char *prefix)
134 size_t n = strlen(prefix);
135 if (!strncmp(prefix, str, n))
140 /* parse attributes part of variable value, if present: */
141 static const char *parse_attr(const char *str, u32 *attrp)
147 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
151 while (*str == sep) {
156 if ((s = prefix(str, "ro"))) {
158 } else if ((s = prefix(str, "boot"))) {
159 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
160 } else if ((s = prefix(str, "run"))) {
161 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
163 printf("invalid attribute: %s\n", str);
178 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetVariable.28.29 */
179 efi_status_t EFIAPI efi_get_variable(s16 *variable_name,
180 efi_guid_t *vendor, u32 *attributes,
181 unsigned long *data_size, void *data)
183 char native_name[MAX_NATIVE_VAR_NAME + 1];
185 unsigned long in_size;
189 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
192 if (!variable_name || !vendor || !data_size)
193 return EFI_EXIT(EFI_INVALID_PARAMETER);
195 ret = efi_to_native(native_name, variable_name, vendor);
197 return EFI_EXIT(ret);
199 debug("%s: get '%s'\n", __func__, native_name);
201 val = env_get(native_name);
203 return EFI_EXIT(EFI_NOT_FOUND);
205 val = parse_attr(val, &attr);
207 in_size = *data_size;
209 if ((s = prefix(val, "(blob)"))) {
210 unsigned len = strlen(s);
212 /* two characters per byte: */
213 len = DIV_ROUND_UP(len, 2);
217 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
220 return EFI_EXIT(EFI_INVALID_PARAMETER);
222 if (hex2mem(data, s, len * 2))
223 return EFI_EXIT(EFI_DEVICE_ERROR);
225 debug("%s: got value: \"%s\"\n", __func__, s);
226 } else if ((s = prefix(val, "(utf8)"))) {
227 unsigned len = strlen(s) + 1;
232 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
235 return EFI_EXIT(EFI_INVALID_PARAMETER);
237 memcpy(data, s, len);
238 ((char *)data)[len] = '\0';
240 debug("%s: got value: \"%s\"\n", __func__, (char *)data);
242 debug("%s: invalid value: '%s'\n", __func__, val);
243 return EFI_EXIT(EFI_DEVICE_ERROR);
247 *attributes = attr & EFI_VARIABLE_MASK;
249 return EFI_EXIT(EFI_SUCCESS);
252 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetNextVariableName.28.29 */
253 efi_status_t EFIAPI efi_get_next_variable(
254 unsigned long *variable_name_size,
255 s16 *variable_name, efi_guid_t *vendor)
257 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
259 return EFI_EXIT(EFI_DEVICE_ERROR);
262 /* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#SetVariable.28.29 */
263 efi_status_t EFIAPI efi_set_variable(s16 *variable_name,
264 efi_guid_t *vendor, u32 attributes,
265 unsigned long data_size, void *data)
267 char native_name[MAX_NATIVE_VAR_NAME + 1];
268 efi_status_t ret = EFI_SUCCESS;
272 EFI_ENTRY("\"%ls\" %pUl %x %lu %p", variable_name, vendor, attributes,
275 if (!variable_name || !vendor)
276 return EFI_EXIT(EFI_INVALID_PARAMETER);
278 ret = efi_to_native(native_name, variable_name, vendor);
280 return EFI_EXIT(ret);
282 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
284 if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
285 /* delete the variable: */
286 env_set(native_name, NULL);
287 return EFI_EXIT(EFI_SUCCESS);
290 val = env_get(native_name);
292 parse_attr(val, &attr);
294 if (attr & READ_ONLY)
295 return EFI_EXIT(EFI_WRITE_PROTECTED);
298 val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
300 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
304 /* store attributes: */
305 attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
306 s += sprintf(s, "{");
308 u32 attr = 1 << (ffs(attributes) - 1);
310 if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
311 s += sprintf(s, "boot");
312 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
313 s += sprintf(s, "run");
317 s += sprintf(s, ",");
319 s += sprintf(s, "}");
322 s += sprintf(s, "(blob)");
323 s = mem2hex(s, data, data_size);
326 debug("%s: setting: %s=%s\n", __func__, native_name, val);
328 if (env_set(native_name, val))
329 ret = EFI_DEVICE_ERROR;
333 return EFI_EXIT(ret);