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#define pr_fmt(fmt) "mokvar: " fmt
33
34#include <linux/capability.h>
35#include <linux/efi.h>
36#include <linux/init.h>
37#include <linux/io.h>
38#include <linux/kernel.h>
39#include <linux/kobject.h>
40#include <linux/list.h>
41#include <linux/slab.h>
42
43#include <asm/early_ioremap.h>
44
45
46
47
48
49
50
51
52
53
54
55
56static size_t efi_mokvar_table_size;
57
58
59
60
61
62static struct efi_mokvar_table_entry *efi_mokvar_table_va;
63
64
65
66
67
68
69
70
71
72struct efi_mokvar_sysfs_attr {
73 struct bin_attribute bin_attr;
74 struct list_head node;
75};
76
77static LIST_HEAD(efi_mokvar_sysfs_list);
78static struct kobject *mokvar_kobj;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100void __init efi_mokvar_table_init(void)
101{
102 efi_memory_desc_t md;
103 void *va = NULL;
104 unsigned long cur_offset = 0;
105 unsigned long offset_limit;
106 unsigned long map_size = 0;
107 unsigned long map_size_needed = 0;
108 unsigned long size;
109 struct efi_mokvar_table_entry *mokvar_entry;
110 int err;
111
112 if (!efi_enabled(EFI_MEMMAP))
113 return;
114
115 if (efi.mokvar_table == EFI_INVALID_TABLE_ADDR)
116 return;
117
118
119
120
121 err = efi_mem_desc_lookup(efi.mokvar_table, &md);
122 if (err) {
123 pr_warn("EFI MOKvar config table is not within the EFI memory map\n");
124 return;
125 }
126
127 offset_limit = efi_mem_desc_end(&md) - efi.mokvar_table;
128
129
130
131
132
133
134
135 err = -EINVAL;
136 while (cur_offset + sizeof(*mokvar_entry) <= offset_limit) {
137 mokvar_entry = va + cur_offset;
138 map_size_needed = cur_offset + sizeof(*mokvar_entry);
139 if (map_size_needed > map_size) {
140 if (va)
141 early_memunmap(va, map_size);
142
143
144
145
146
147
148 map_size = min(map_size_needed + 2*EFI_PAGE_SIZE,
149 offset_limit);
150 va = early_memremap(efi.mokvar_table, map_size);
151 if (!va) {
152 pr_err("Failed to map EFI MOKvar config table pa=0x%lx, size=%lu.\n",
153 efi.mokvar_table, map_size);
154 return;
155 }
156 mokvar_entry = va + cur_offset;
157 }
158
159
160 if (mokvar_entry->name[0] == '\0') {
161 if (mokvar_entry->data_size != 0)
162 break;
163 err = 0;
164 break;
165 }
166
167
168 size = strnlen(mokvar_entry->name,
169 sizeof(mokvar_entry->name));
170 if (size >= sizeof(mokvar_entry->name))
171 break;
172
173
174 cur_offset = map_size_needed + mokvar_entry->data_size;
175 }
176
177 if (va)
178 early_memunmap(va, map_size);
179 if (err) {
180 pr_err("EFI MOKvar config table is not valid\n");
181 return;
182 }
183
184 if (md.type == EFI_BOOT_SERVICES_DATA)
185 efi_mem_reserve(efi.mokvar_table, map_size_needed);
186
187 efi_mokvar_table_size = map_size_needed;
188}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206struct efi_mokvar_table_entry *efi_mokvar_entry_next(
207 struct efi_mokvar_table_entry **mokvar_entry)
208{
209 struct efi_mokvar_table_entry *mokvar_cur;
210 struct efi_mokvar_table_entry *mokvar_next;
211 size_t size_cur;
212
213 mokvar_cur = *mokvar_entry;
214 *mokvar_entry = NULL;
215
216 if (efi_mokvar_table_va == NULL)
217 return NULL;
218
219 if (mokvar_cur == NULL) {
220 mokvar_next = efi_mokvar_table_va;
221 } else {
222 if (mokvar_cur->name[0] == '\0')
223 return NULL;
224 size_cur = sizeof(*mokvar_cur) + mokvar_cur->data_size;
225 mokvar_next = (void *)mokvar_cur + size_cur;
226 }
227
228 if (mokvar_next->name[0] == '\0')
229 return NULL;
230
231 *mokvar_entry = mokvar_next;
232 return mokvar_next;
233}
234
235
236
237
238
239
240
241
242
243
244
245
246struct efi_mokvar_table_entry *efi_mokvar_entry_find(const char *name)
247{
248 struct efi_mokvar_table_entry *mokvar_entry = NULL;
249
250 while (efi_mokvar_entry_next(&mokvar_entry)) {
251 if (!strncmp(name, mokvar_entry->name,
252 sizeof(mokvar_entry->name)))
253 return mokvar_entry;
254 }
255 return NULL;
256}
257
258
259
260
261
262
263
264
265
266
267
268static ssize_t efi_mokvar_sysfs_read(struct file *file, struct kobject *kobj,
269 struct bin_attribute *bin_attr, char *buf,
270 loff_t off, size_t count)
271{
272 struct efi_mokvar_table_entry *mokvar_entry = bin_attr->private;
273
274 if (!capable(CAP_SYS_ADMIN))
275 return 0;
276
277 if (off >= mokvar_entry->data_size)
278 return 0;
279 if (count > mokvar_entry->data_size - off)
280 count = mokvar_entry->data_size - off;
281
282 memcpy(buf, mokvar_entry->data + off, count);
283 return count;
284}
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310static int __init efi_mokvar_sysfs_init(void)
311{
312 void *config_va;
313 struct efi_mokvar_table_entry *mokvar_entry = NULL;
314 struct efi_mokvar_sysfs_attr *mokvar_sysfs = NULL;
315 int err = 0;
316
317 if (efi_mokvar_table_size == 0)
318 return -ENOENT;
319
320 config_va = memremap(efi.mokvar_table, efi_mokvar_table_size,
321 MEMREMAP_WB);
322 if (!config_va) {
323 pr_err("Failed to map EFI MOKvar config table\n");
324 return -ENOMEM;
325 }
326 efi_mokvar_table_va = config_va;
327
328 mokvar_kobj = kobject_create_and_add("mok-variables", efi_kobj);
329 if (!mokvar_kobj) {
330 pr_err("Failed to create EFI mok-variables sysfs entry\n");
331 return -ENOMEM;
332 }
333
334 while (efi_mokvar_entry_next(&mokvar_entry)) {
335 mokvar_sysfs = kzalloc(sizeof(*mokvar_sysfs), GFP_KERNEL);
336 if (!mokvar_sysfs) {
337 err = -ENOMEM;
338 break;
339 }
340
341 sysfs_bin_attr_init(&mokvar_sysfs->bin_attr);
342 mokvar_sysfs->bin_attr.private = mokvar_entry;
343 mokvar_sysfs->bin_attr.attr.name = mokvar_entry->name;
344 mokvar_sysfs->bin_attr.attr.mode = 0400;
345 mokvar_sysfs->bin_attr.size = mokvar_entry->data_size;
346 mokvar_sysfs->bin_attr.read = efi_mokvar_sysfs_read;
347
348 err = sysfs_create_bin_file(mokvar_kobj,
349 &mokvar_sysfs->bin_attr);
350 if (err)
351 break;
352
353 list_add_tail(&mokvar_sysfs->node, &efi_mokvar_sysfs_list);
354 }
355
356 if (err) {
357 pr_err("Failed to create some EFI mok-variables sysfs entries\n");
358 kfree(mokvar_sysfs);
359 }
360 return err;
361}
362device_initcall(efi_mokvar_sysfs_init);
363