blob: 6f9d76f2a2d5154440318eef7dd23170f704eacf [file] [log] [blame]
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * File interface for UEFI variables
4 *
5 * Copyright (c) 2020, Heinrich Schuchardt
6 */
7
8#define LOG_CATEGORY LOGC_EFI
9
10#include <common.h>
11#include <charset.h>
12#include <fs.h>
13#include <log.h>
14#include <malloc.h>
15#include <mapmem.h>
16#include <efi_loader.h>
17#include <efi_variable.h>
18#include <u-boot/crc.h>
19
20#define PART_STR_LEN 10
21
22/**
23 * efi_set_blk_dev_to_system_partition() - select EFI system partition
24 *
25 * Set the EFI system partition as current block device.
26 *
27 * Return: status code
28 */
29static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void)
30{
31 char part_str[PART_STR_LEN];
32 int r;
33
34 if (!efi_system_partition.if_type) {
35 log_err("No EFI system partition\n");
36 return EFI_DEVICE_ERROR;
37 }
38 snprintf(part_str, PART_STR_LEN, "%u:%u",
39 efi_system_partition.devnum, efi_system_partition.part);
40 r = fs_set_blk_dev(blk_get_if_type_name(efi_system_partition.if_type),
41 part_str, FS_TYPE_ANY);
42 if (r) {
43 log_err("Cannot read EFI system partition\n");
44 return EFI_DEVICE_ERROR;
45 }
46 return EFI_SUCCESS;
47}
48
49/**
50 * efi_var_collect() - collect non-volatile variables in buffer
51 *
52 * A buffer is allocated and filled with all non-volatile variables in a
53 * format ready to be written to disk.
54 *
55 * @bufp: pointer to pointer of buffer with collected variables
56 * @lenp: pointer to length of buffer
57 * Return: status code
58 */
59static efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp,
60 loff_t *lenp)
61{
62 size_t len = EFI_VAR_BUF_SIZE;
63 struct efi_var_file *buf;
64 struct efi_var_entry *var, *old_var;
65 size_t old_var_name_length = 2;
66
67 *bufp = NULL; /* Avoid double free() */
68 buf = calloc(1, len);
69 if (!buf)
70 return EFI_OUT_OF_RESOURCES;
71 var = buf->var;
72 old_var = var;
73 for (;;) {
74 efi_uintn_t data_length, var_name_length;
75 u8 *data;
76 efi_status_t ret;
77
78 if ((uintptr_t)buf + len <=
79 (uintptr_t)var->name + old_var_name_length)
80 return EFI_BUFFER_TOO_SMALL;
81
82 var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name;
83 memcpy(var->name, old_var->name, old_var_name_length);
84 guidcpy(&var->guid, &old_var->guid);
85 ret = efi_get_next_variable_name_int(
86 &var_name_length, var->name, &var->guid);
87 if (ret == EFI_NOT_FOUND)
88 break;
89 if (ret != EFI_SUCCESS) {
90 free(buf);
91 return ret;
92 }
93 old_var_name_length = var_name_length;
94 old_var = var;
95
96 data = (u8 *)var->name + old_var_name_length;
97 data_length = (uintptr_t)buf + len - (uintptr_t)data;
98 ret = efi_get_variable_int(var->name, &var->guid,
99 &var->attr, &data_length, data,
100 &var->time);
101 if (ret != EFI_SUCCESS) {
102 free(buf);
103 return ret;
104 }
105 if (!(var->attr & EFI_VARIABLE_NON_VOLATILE))
106 continue;
107 var->length = data_length;
108 var = (struct efi_var_entry *)
109 ALIGN((uintptr_t)data + data_length, 8);
110 }
111
112 buf->reserved = 0;
113 buf->magic = EFI_VAR_FILE_MAGIC;
114 len = (uintptr_t)var - (uintptr_t)buf;
115 buf->crc32 = crc32(0, (u8 *)buf->var,
116 len - sizeof(struct efi_var_file));
117 buf->length = len;
118 *bufp = buf;
119 *lenp = len;
120
121 return EFI_SUCCESS;
122}
123
124/**
125 * efi_var_to_file() - save non-volatile variables as file
126 *
127 * File ubootefi.var is created on the EFI system partion.
128 *
129 * Return: status code
130 */
131efi_status_t efi_var_to_file(void)
132{
133#ifdef CONFIG_EFI_VARIABLE_FILE_STORE
134 efi_status_t ret;
135 struct efi_var_file *buf;
136 loff_t len;
137 loff_t actlen;
138 int r;
139
140 ret = efi_var_collect(&buf, &len);
141 if (ret != EFI_SUCCESS)
142 goto error;
143
144 ret = efi_set_blk_dev_to_system_partition();
145 if (ret != EFI_SUCCESS)
146 goto error;
147
148 r = fs_write(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, len, &actlen);
149 if (r || len != actlen)
150 ret = EFI_DEVICE_ERROR;
151
152error:
153 if (ret != EFI_SUCCESS)
154 log_err("Failed to persist EFI variables\n");
155 free(buf);
156 return ret;
157#else
158 return EFI_SUCCESS;
159#endif
160}
161
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +0200162efi_status_t efi_var_restore(struct efi_var_file *buf)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000163{
164 struct efi_var_entry *var, *last_var;
165 efi_status_t ret;
166
167 if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC ||
168 buf->crc32 != crc32(0, (u8 *)buf->var,
169 buf->length - sizeof(struct efi_var_file))) {
170 log_err("Invalid EFI variables file\n");
171 return EFI_INVALID_PARAMETER;
172 }
173
174 var = buf->var;
175 last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
176 while (var < last_var) {
177 u16 *data = var->name + u16_strlen(var->name) + 1;
178
179 if (var->attr & EFI_VARIABLE_NON_VOLATILE && var->length) {
Heinrich Schuchardt5be59712020-03-24 19:54:53 +0000180 ret = efi_var_mem_ins(var->name, &var->guid, var->attr,
181 var->length, data, 0, NULL,
182 var->time);
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000183 if (ret != EFI_SUCCESS)
184 log_err("Failed to set EFI variable %ls\n",
185 var->name);
186 }
187 var = (struct efi_var_entry *)
188 ALIGN((uintptr_t)data + var->length, 8);
189 }
190 return EFI_SUCCESS;
191}
192
193/**
194 * efi_var_from_file() - read variables from file
195 *
196 * File ubootefi.var is read from the EFI system partitions and the variables
197 * stored in the file are created.
198 *
199 * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
200 * is returned.
201 *
202 * Return: status code
203 */
204efi_status_t efi_var_from_file(void)
205{
206#ifdef CONFIG_EFI_VARIABLE_FILE_STORE
207 struct efi_var_file *buf;
208 loff_t len;
209 efi_status_t ret;
210 int r;
211
212 buf = calloc(1, EFI_VAR_BUF_SIZE);
213 if (!buf) {
214 log_err("Out of memory\n");
215 return EFI_OUT_OF_RESOURCES;
216 }
217
218 ret = efi_set_blk_dev_to_system_partition();
219 if (ret != EFI_SUCCESS)
220 goto error;
221 r = fs_read(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, EFI_VAR_BUF_SIZE,
222 &len);
223 if (r || len < sizeof(struct efi_var_file)) {
224 log_err("Failed to load EFI variables\n");
225 goto error;
226 }
227 if (buf->length != len || efi_var_restore(buf) != EFI_SUCCESS)
228 log_err("Invalid EFI variables file\n");
229error:
230 free(buf);
231#endif
232 return EFI_SUCCESS;
233}