forked from rhboot/efivar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
efivarfs.c
262 lines (214 loc) · 5.35 KB
/
efivarfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* libefivar - library for the manipulation of EFI variables
* Copyright 2012-2013 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <fcntl.h>
#include <linux/magic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/vfs.h>
#include <unistd.h>
#include "lib.h"
#include "generics.h"
#include "util.h"
#define EFIVARS_PATH "/sys/firmware/efi/efivars/"
#ifndef EFIVARFS_MAGIC
# define EFIVARFS_MAGIC 0xde5e81e4
#endif
typedef struct efi_variable_t {
uint32_t Attributes;
uint8_t Data[];
} __attribute__((packed)) efi_variable_t;
static int
efivarfs_probe(void)
{
if (!access(EFIVARS_PATH, F_OK)) {
int rc = 0;
struct statfs buf;
memset(&buf, '\0', sizeof (buf));
rc = statfs(EFIVARS_PATH, &buf);
if (rc == 0) {
typeof(buf.f_type) magic = EFIVARFS_MAGIC;
if (!memcmp(&buf.f_type, &magic, sizeof (magic)))
return 1;
}
}
return 0;
}
#define make_efivarfs_path(str, guid, name) ({ \
asprintf(str, EFIVARS_PATH "%s-" GUID_FORMAT, \
name, (guid).a, (guid).b, (guid).c, \
bswap_16((guid).d), \
(guid).e[0], (guid).e[1], (guid).e[2], \
(guid).e[3], (guid).e[4], (guid).e[5]); \
})
static int
efivarfs_get_variable_size(efi_guid_t guid, const char *name, size_t *size)
{
char *path = NULL;
int rc = 0;
int ret = -1;
typeof(errno) errno_value;
rc = make_efivarfs_path(&path, guid, name);
if (rc < 0)
goto err;
struct stat statbuf;
rc = stat(path, &statbuf);
if (rc < 0)
goto err;
ret = 0;
/* Compensate for the size of the Attributes field. */
*size = statbuf.st_size - sizeof (uint32_t);
err:
errno_value = errno;
if (path)
free(path);
errno = errno_value;
return ret;
}
static int
efivarfs_get_variable_attributes(efi_guid_t guid, const char *name,
uint32_t *attributes)
{
int ret = -1;
uint8_t *data;
size_t data_size;
uint32_t attribs;
ret = efi_get_variable(guid, name, &data, &data_size, &attribs);
if (ret < 0)
return ret;
*attributes = attribs;
if (data)
free(data);
return ret;
}
static int
efivarfs_get_variable(efi_guid_t guid, const char *name, uint8_t **data,
size_t *data_size, uint32_t *attributes)
{
typeof(errno) errno_value;
int ret = -1;
size_t size = 0;
uint32_t ret_attributes = 0;
uint8_t *ret_data;
char *path;
int rc = make_efivarfs_path(&path, guid, name);
if (rc < 0)
return -1;
int fd = open(path, O_RDONLY);
if (fd < 0)
goto err;
rc = read(fd, &ret_attributes, sizeof (ret_attributes));
if (rc < 0)
goto err;
rc = read_file(fd, &ret_data, &size);
if (rc < 0)
goto err;
*attributes = ret_attributes;
*data = ret_data;
*data_size = size;
ret = 0;
err:
errno_value = errno;
if (fd >= 0)
close(fd);
if (path)
free(path);
errno = errno_value;
return ret;
}
static int
efivarfs_del_variable(efi_guid_t guid, const char *name)
{
char *path;
int rc = make_efivarfs_path(&path, guid, name);
if (rc < 0)
return -1;
rc = unlink(path);
typeof(errno) errno_value = errno;
free(path);
errno = errno_value;
return rc;
}
static int
efivarfs_set_variable(efi_guid_t guid, const char *name, uint8_t *data,
size_t data_size, uint32_t attributes)
{
uint8_t buf[sizeof (attributes) + data_size];
typeof(errno) errno_value;
int ret = -1;
if (strlen(name) > 1024) {
errno = EINVAL;
return -1;
}
char *path;
int rc = make_efivarfs_path(&path, guid, name);
if (rc < 0)
return -1;
int fd = -1;
if (!access(path, F_OK) && !(attributes & EFI_VARIABLE_APPEND_WRITE)) {
rc = efi_del_variable(guid, name);
if (rc < 0)
goto err;
}
fd = open(path, O_WRONLY|O_CREAT, 0600);
if (fd < 0)
goto err;
memcpy(buf, &attributes, sizeof (attributes));
memcpy(buf + sizeof (attributes), data, data_size);
rc = write(fd, buf, sizeof (attributes) + data_size);
if (rc >= 0) {
ret = 0;
} else {
unlink(path);
}
err:
errno_value = errno;
if (path)
free(path);
if (fd >= 0)
close(fd);
errno = errno_value;
return ret;
}
static int
efivarfs_append_variable(efi_guid_t guid, const char *name, uint8_t *data,
size_t data_size, uint32_t attributes)
{
attributes |= EFI_VARIABLE_APPEND_WRITE;
return efivarfs_set_variable(guid, name, data, data_size, attributes);
}
static int
efivarfs_get_next_variable_name(efi_guid_t **guid, char **name)
{
return generic_get_next_variable_name(EFIVARS_PATH, guid, name);
}
struct efi_var_operations efivarfs_ops = {
.probe = efivarfs_probe,
.set_variable = efivarfs_set_variable,
.append_variable = efivarfs_append_variable,
.del_variable = efivarfs_del_variable,
.get_variable = efivarfs_get_variable,
.get_variable_attributes = efivarfs_get_variable_attributes,
.get_variable_size = efivarfs_get_variable_size,
.get_next_variable_name = efivarfs_get_next_variable_name,
};