Debian Codegraph @

grub-core/osdep/unix/efivar.c

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013,2019 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* Contains portions derived from efibootmgr, licensed as follows:
20 *
21 * Copyright (C) 2001-2004 Dell, Inc. <[email protected]>
22 * Copyright 2015-2016 Red Hat, Inc. <[email protected]>
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 */
29
30 #include <config.h>
31
32 #ifdef HAVE_EFIVAR
33
34 #include <grub/util/install.h>
35 #include <grub/emu/hostdisk.h>
36 #include <grub/util/misc.h>
37 #include <grub/list.h>
38 #include <grub/misc.h>
39 #include <grub/emu/exec.h>
40 #include <sys/types.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include <efiboot.h>
47 #include <efivar.h>
48
49 struct efi_variable {
50 struct efi_variable *next;
51 struct efi_variable **prev;
52 char *name;
53 efi_guid_t guid;
54 uint8_t *data;
55 size_t data_size;
56 uint32_t attributes;
57 int num;
58 };
59
60 /* Boot option attributes. */
61 #define LOAD_OPTION_ACTIVE 0x00000001
62
63 /* GUIDs. */
64 #define BLKX_UNKNOWN_GUID \
65 EFI_GUID (0x47c7b225, 0xc42a, 0x11d2, 0x8e57, 0x00, 0xa0, 0xc9, 0x69, \
66 0x72, 0x3b)
67
68 /* Log all errors recorded by libefivar/libefiboot. */
69 static void
70 show_efi_errors (void)
71 {
72 int i;
73 int saved_errno = errno;
74
75 for (i = 0; ; ++i)
76 {
77 char *filename, *function, *message = NULL;
78 int line, error = 0, rc;
79
80 rc = efi_error_get (i, &filename, &function, &line, &message, &error);
81 if (rc < 0)
82 /* Give up. The caller is going to log an error anyway. */
83 break;
84 if (rc == 0)
85 /* No more errors. */
86 break;
87 grub_util_warn ("%s: %s: %s", function, message, strerror (error));
88 }
89
90 efi_error_clear ();
91 errno = saved_errno;
92 }
93
94 static struct efi_variable *
95 new_efi_variable (void)
96 {
97 struct efi_variable *new = xmalloc (sizeof (*new));
98 memset (new, 0, sizeof (*new));
99 return new;
100 }
101
102 static struct efi_variable *
103 new_boot_variable (void)
104 {
105 struct efi_variable *new = new_efi_variable ();
106 new->guid = EFI_GLOBAL_GUID;
107 new->attributes = EFI_VARIABLE_NON_VOLATILE |
108 EFI_VARIABLE_BOOTSERVICE_ACCESS |
109 EFI_VARIABLE_RUNTIME_ACCESS;
110 return new;
111 }
112
113 static void
114 free_efi_variable (struct efi_variable *entry)
115 {
116 if (entry)
117 {
118 free (entry->name);
119 free (entry->data);
120 free (entry);
121 }
122 }
123
124 static int
125 read_efi_variable (const char *name, struct efi_variable **entry)
126 {
127 struct efi_variable *new = new_efi_variable ();
128 int rc;
129
130 rc = efi_get_variable (EFI_GLOBAL_GUID, name,
131 &new->data, &new->data_size, &new->attributes);
132 if (rc < 0)
133 {
134 free_efi_variable (new);
135 new = NULL;
136 }
137
138 if (new)
139 {
140 /* Latest Apple firmware sets the high bit which appears invalid
141 to the Linux kernel if we write it back, so let's zero it out if it
142 is set since it would be invalid to set it anyway. */
143 new->attributes = new->attributes & ~(1 << 31);
144
145 new->name = xstrdup (name);
146 new->guid = EFI_GLOBAL_GUID;
147 }
148
149 *entry = new;
150 return rc;
151 }
152
153 /* Set an EFI variable, but only if it differs from the current value.
154 Some firmware implementations are liable to fill up flash space if we set
155 variables unnecessarily, so try to keep write activity to a minimum. */
156 static int
157 set_efi_variable (const char *name, struct efi_variable *entry)
158 {
159 struct efi_variable *old = NULL;
160 int rc = 0;
161
162 read_efi_variable (name, &old);
163 efi_error_clear ();
164 if (old && old->attributes == entry->attributes &&
165 old->data_size == entry->data_size &&
166 memcmp (old->data, entry->data, entry->data_size) == 0)
167 grub_util_info ("skipping unnecessary update of EFI variable %s", name);
168 else
169 {
170 rc = efi_set_variable (EFI_GLOBAL_GUID, name,
171 entry->data, entry->data_size, entry->attributes,
172 0644);
173 if (rc < 0)
174 grub_util_warn (_("Cannot set EFI variable %s"), name);
175 }
176 free_efi_variable (old);
177 return rc;
178 }
179
180 static int
181 cmpvarbyname (const void *p1, const void *p2)
182 {
183 const struct efi_variable *var1 = *(const struct efi_variable **)p1;
184 const struct efi_variable *var2 = *(const struct efi_variable **)p2;
185 return strcmp (var1->name, var2->name);
186 }
187
188 static int
189 read_boot_variables (struct efi_variable **varlist)
190 {
191 int rc;
192 efi_guid_t *guid = NULL;
193 char *name = NULL;
194 struct efi_variable **newlist = NULL;
195 int nentries = 0;
196 int i;
197
198 while ((rc = efi_get_next_variable_name (&guid, &name)) > 0)
199 {
200 const char *snum = name + sizeof ("Boot") - 1;
201 struct efi_variable *var = NULL;
202 unsigned int num;
203
204 if (memcmp (guid, &efi_guid_global, sizeof (efi_guid_global)) != 0 ||
205 strncmp (name, "Boot", sizeof ("Boot") - 1) != 0 ||
206 !grub_isxdigit (snum[0]) || !grub_isxdigit (snum[1]) ||
207 !grub_isxdigit (snum[2]) || !grub_isxdigit (snum[3]))
208 continue;
209
210 rc = read_efi_variable (name, &var);
211 if (rc < 0)
212 break;
213
214 if (sscanf (var->name, "Boot%04X-%*s", &num) == 1 && num < 65536)
215 var->num = num;
216
217 newlist = xrealloc (newlist, (++nentries) * sizeof (*newlist));
218 newlist[nentries - 1] = var;
219 }
220 if (rc == 0 && newlist)
221 {
222 qsort (newlist, nentries, sizeof (*newlist), cmpvarbyname);
223 for (i = nentries - 1; i >= 0; --i)
224 grub_list_push (GRUB_AS_LIST_P (varlist), GRUB_AS_LIST (newlist[i]));
225 }
226 else if (newlist)
227 {
228 for (i = 0; i < nentries; ++i)
229 free_efi_variable (newlist[i]);
230 free (newlist);
231 }
232 return rc;
233 }
234
235 #define GET_ORDER(data, i) \
236 ((uint16_t) ((data)[(i) * 2]) + ((data)[(i) * 2 + 1] << 8))
237 #define SET_ORDER(data, i, num) \
238 do { \
239 (data)[(i) * 2] = (num) & 0xFF; \
240 (data)[(i) * 2 + 1] = ((num) >> 8) & 0xFF; \
241 } while (0)
242
243 static void
244 remove_from_boot_order (struct efi_variable *order, uint16_t num)
245 {
246 unsigned int old_i, new_i;
247
248 /* We've got an array (in order->data) of the order. Squeeze out any
249 instance of the entry we're deleting by shifting the remainder down. */
250 for (old_i = 0, new_i = 0;
251 old_i < order->data_size / sizeof (uint16_t);
252 ++old_i)
253 {
254 uint16_t old_num = GET_ORDER (order->data, old_i);
255 if (old_num != num)
256 {
257 if (new_i != old_i)
258 SET_ORDER (order->data, new_i, old_num);
259 ++new_i;
260 }
261 }
262
263 order->data_size = new_i * sizeof (uint16_t);
264 }
265
266 static void
267 add_to_boot_order (struct efi_variable *order, uint16_t num)
268 {
269 int i;
270 size_t new_data_size;
271 uint8_t *new_data;
272
273 /* Check whether this entry is already in the boot order. If it is, leave
274 it alone. */
275 for (i = 0; i < order->data_size / sizeof (uint16_t); ++i)
276 if (GET_ORDER (order->data, i) == num)
277 return;
278
279 new_data_size = order->data_size + sizeof (uint16_t);
280 new_data = xmalloc (new_data_size);
281 SET_ORDER (new_data, 0, num);
282 memcpy (new_data + sizeof (uint16_t), order->data, order->data_size);
283 free (order->data);
284 order->data = new_data;
285 order->data_size = new_data_size;
286 }
287
288 static int
289 find_free_boot_num (struct efi_variable *entries)
290 {
291 int num_vars = 0, i;
292 struct efi_variable *entry;
293
294 FOR_LIST_ELEMENTS (entry, entries)
295 ++num_vars;
296
297 if (num_vars == 0)
298 return 0;
299
300 /* O(n^2), but n is small and this is easy. */
301 for (i = 0; i < num_vars; ++i)
302 {
303 int found = 0;
304 FOR_LIST_ELEMENTS (entry, entries)
305 {
306 if (entry->num == i)
307 {
308 found = 1;
309 break;
310 }
311 }
312 if (!found)
313 return i;
314 }
315
316 return i;
317 }
318
319 static int
320 get_edd_version (void)
321 {
322 efi_guid_t blkx_guid = BLKX_UNKNOWN_GUID;
323 uint8_t *data = NULL;
324 size_t data_size = 0;
325 uint32_t attributes;
326 efidp_header *path;
327 int rc;
328
329 rc = efi_get_variable (blkx_guid, "blk0", &data, &data_size, &attributes);
330 if (rc < 0)
331 return rc;
332
333 path = (efidp_header *) data;
334 if (path->type == 2 && path->subtype == 1)
335 return 3;
336 return 1;
337 }
338
339 static struct efi_variable *
340 make_boot_variable (int num, const char *disk, int part, const char *loader,
341 const char *label)
342 {
343 struct efi_variable *entry = new_boot_variable ();
344 uint32_t options;
345 uint32_t edd10_devicenum;
346 ssize_t dp_needed, loadopt_needed;
347 efidp dp = NULL;
348
349 options = EFIBOOT_ABBREV_HD;
350 switch (get_edd_version ()) {
351 case 1:
352 options = EFIBOOT_ABBREV_EDD10;
353 break;
354 case 3:
355 options = EFIBOOT_ABBREV_NONE;
356 break;
357 }
358
359 /* This may not be the right disk; but it's probably only an issue on very
360 old hardware anyway. */
361 edd10_devicenum = 0x80;
362
363 dp_needed = efi_generate_file_device_path_from_esp (NULL, 0, disk, part,
364 loader, options,
365 edd10_devicenum);
366 if (dp_needed < 0)
367 goto err;
368
369 dp = xmalloc (dp_needed);
370 dp_needed = efi_generate_file_device_path_from_esp ((uint8_t *) dp,
371 dp_needed, disk, part,
372 loader, options,
373 edd10_devicenum);
374 if (dp_needed < 0)
375 goto err;
376
377 loadopt_needed = efi_loadopt_create (NULL, 0, LOAD_OPTION_ACTIVE,
378 dp, dp_needed, (unsigned char *) label,
379 NULL, 0);
380 if (loadopt_needed < 0)
381 goto err;
382 entry->data_size = loadopt_needed;
383 entry->data = xmalloc (entry->data_size);
384 loadopt_needed = efi_loadopt_create (entry->data, entry->data_size,
385 LOAD_OPTION_ACTIVE, dp, dp_needed,
386 (unsigned char *) label, NULL, 0);
387 if (loadopt_needed < 0)
388 goto err;
389
390 entry->name = xasprintf ("Boot%04X", num);
391 entry->num = num;
392
393 return entry;
394
395 err:
396 free_efi_variable (entry);
397 free (dp);
398 return NULL;
399 }
400
401 int
402 grub_install_efivar_register_efi (grub_device_t efidir_grub_dev,
403 const char *efifile_path,
404 const char *efi_distributor)
405 {
406 const char *efidir_disk;
407 int efidir_part;
408 struct efi_variable *entries = NULL, *entry;
409 struct efi_variable *order;
410 int entry_num = -1;
411 int rc;
412
413 efidir_disk = grub_util_biosdisk_get_osdev (efidir_grub_dev->disk);
414 efidir_part = efidir_grub_dev->disk->partition ? efidir_grub_dev->disk->partition->number + 1 : 1;
415
416 #ifdef __linux__
417 /*
418 * Linux uses efivarfs (mounted on /sys/firmware/efi/efivars) to access the
419 * EFI variable store. Some legacy systems may still use the deprecated
420 * efivars interface (accessed through /sys/firmware/efi/vars). Where both
421 * are present, libefivar will use the former in preference, so attempting
422 * to load efivars will not interfere with later operations.
423 */
424 grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL },
425 NULL, NULL, "/dev/null");
426 #endif
427
428 if (!efi_variables_supported ())
429 {
430 grub_util_warn ("%s",
431 _("EFI variables are not supported on this system."));
432 /* Let the user continue. Perhaps they can still arrange to boot GRUB
433 manually. */
434 return 0;
435 }
436
437 rc = read_boot_variables (&entries);
438 if (rc < 0)
439 {
440 grub_util_warn ("%s", _("Cannot read EFI Boot* variables"));
441 goto err;
442 }
443 rc = read_efi_variable ("BootOrder", &order);
444 if (rc < 0)
445 {
446 order = new_boot_variable ();
447 order->name = xstrdup ("BootOrder");
448 efi_error_clear ();
449 }
450
451 /* Delete old entries from the same distributor. */
452 FOR_LIST_ELEMENTS (entry, entries)
453 {
454 efi_load_option *load_option = (efi_load_option *) entry->data;
455 const char *label;
456
457 if (entry->num < 0)
458 continue;
459 label = (const char *) efi_loadopt_desc (load_option, entry->data_size);
460 if (strcasecmp (label, efi_distributor) != 0)
461 continue;
462
463 /* To avoid problems with some firmware implementations, reuse the first
464 matching variable we find rather than deleting and recreating it. */
465 if (entry_num == -1)
466 entry_num = entry->num;
467 else
468 {
469 grub_util_info ("deleting superfluous EFI variable %s (%s)",
470 entry->name, label);
471 rc = efi_del_variable (EFI_GLOBAL_GUID, entry->name);
472 if (rc < 0)
473 {
474 grub_util_warn (_("Cannot delete EFI variable %s"), entry->name);
475 goto err;
476 }
477 }
478
479 remove_from_boot_order (order, (uint16_t) entry->num);
480 }
481
482 if (entry_num == -1)
483 entry_num = find_free_boot_num (entries);
484 entry = make_boot_variable (entry_num, efidir_disk, efidir_part,
485 efifile_path, efi_distributor);
486 if (!entry)
487 goto err;
488
489 grub_util_info ("setting EFI variable %s", entry->name);
490 rc = set_efi_variable (entry->name, entry);
491 if (rc < 0)
492 goto err;
493
494 add_to_boot_order (order, (uint16_t) entry_num);
495
496 grub_util_info ("setting EFI variable BootOrder");
497 rc = set_efi_variable ("BootOrder", order);
498 if (rc < 0)
499 goto err;
500
501 return 0;
502
503 err:
504 show_efi_errors ();
505 return errno;
506 }
507
508 #endif /* HAVE_EFIVAR */