]> git.proxmox.com Git - grub2.git/blob - debian/patches/efi-variable-storage-minimise-writes.patch
New upstream release candidate (2.04~rc1)
[grub2.git] / debian / patches / efi-variable-storage-minimise-writes.patch
1 From 3b968756ecc7c4d97d63d65e3fe258a3df9652a0 Mon Sep 17 00:00:00 2001
2 From: Colin Watson <cjwatson@ubuntu.com>
3 Date: Mon, 11 Mar 2019 11:17:43 +0000
4 Subject: Minimise writes to EFI variable storage
5
6 Some UEFI firmware is easily provoked into running out of space in its
7 variable storage. This is usually due to certain kernel drivers (e.g.
8 pstore), but regardless of the cause it can cause grub-install to fail
9 because it currently asks efibootmgr to delete and re-add entries, and
10 the deletion often doesn't result in an immediate garbage collection.
11 Writing variables frequently also increases wear on the NVRAM which may
12 have limited write cycles. For these reasons, it's desirable to find a
13 way to minimise writes while still allowing grub-install to ensure that
14 a suitable boot entry exists.
15
16 Unfortunately, efibootmgr doesn't offer an interface that would let
17 grub-install do this. It doesn't in general make very much effort to
18 minimise writes; it doesn't allow modifying an existing Boot* variable
19 entry, except in certain limited ways; and current versions don't have a
20 way to export the expected variable data so that grub-install can
21 compare it to the current data. While it would be possible (and perhaps
22 desirable?) to add at least some of this to efibootmgr, that would still
23 leave the problem that there isn't a good upstreamable way for
24 grub-install to guarantee that it has a new enough version of
25 efibootmgr. In any case, it's cumbersome and slow for grub-install to
26 have to fork efibootmgr to get things done.
27
28 Fortunately, a few years ago Peter Jones helpfully factored out a
29 substantial part of efibootmgr to the efivar and efiboot libraries, and
30 so it's now possible to have grub-install use those directly. We still
31 have to use some code from efibootmgr, but much less than would
32 previously have been necessary.
33
34 grub-install now reuses existing boot entries where possible, and avoids
35 writing to variables when the new contents are the same as the old
36 contents. In the common upgrade case where nothing needs to change, it
37 no longer writes to NVRAM at all. It's also now slightly faster, since
38 using libefivar is faster than forking efibootmgr.
39
40 Fixes Debian bug #891434.
41
42 Signed-off-by: Colin Watson <cjwatson@ubuntu.com>
43
44 Bug-Debian: https://bugs.debian.org/891434
45 Forwarded: https://lists.gnu.org/archive/html/grub-devel/2019-03/msg00119.html
46 Last-Update: 2019-03-23
47
48 Patch-Name: efi-variable-storage-minimise-writes.patch
49 ---
50 INSTALL | 5 +
51 Makefile.util.def | 20 ++
52 configure.ac | 12 +
53 grub-core/osdep/efivar.c | 3 +
54 grub-core/osdep/unix/efivar.c | 508 ++++++++++++++++++++++++++++++++
55 grub-core/osdep/unix/platform.c | 100 +------
56 include/grub/util/install.h | 5 +
57 util/grub-install.c | 4 +-
58 8 files changed, 562 insertions(+), 95 deletions(-)
59 create mode 100644 grub-core/osdep/efivar.c
60 create mode 100644 grub-core/osdep/unix/efivar.c
61
62 diff --git a/INSTALL b/INSTALL
63 index 8acb40902..342c158e9 100644
64 --- a/INSTALL
65 +++ b/INSTALL
66 @@ -41,6 +41,11 @@ configuring the GRUB.
67 * Other standard GNU/Unix tools
68 * a libc with large file support (e.g. glibc 2.1 or later)
69
70 +On Unix-based systems, you also need:
71 +
72 +* libefivar (recommended)
73 +* libefiboot (recommended; your OS may ship this together with libefivar)
74 +
75 On GNU/Linux, you also need:
76
77 * libdevmapper 1.02.34 or later (recommended)
78 diff --git a/Makefile.util.def b/Makefile.util.def
79 index 8a24b23f0..59e41423b 100644
80 --- a/Makefile.util.def
81 +++ b/Makefile.util.def
82 @@ -558,6 +558,8 @@ program = {
83 common = grub-core/osdep/compress.c;
84 extra_dist = grub-core/osdep/unix/compress.c;
85 extra_dist = grub-core/osdep/basic/compress.c;
86 + common = grub-core/osdep/efivar.c;
87 + extra_dist = grub-core/osdep/unix/efivar.c;
88 common = util/editenv.c;
89 common = grub-core/osdep/blocklist.c;
90 common = grub-core/osdep/config.c;
91 @@ -571,12 +573,15 @@ program = {
92 common = grub-core/kern/emu/argp_common.c;
93 common = grub-core/osdep/init.c;
94
95 + cflags = '$(EFIVAR_CFLAGS)';
96 +
97 ldadd = '$(LIBLZMA)';
98 ldadd = libgrubmods.a;
99 ldadd = libgrubgcry.a;
100 ldadd = libgrubkern.a;
101 ldadd = grub-core/lib/gnulib/libgnu.a;
102 ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
103 + ldadd = '$(EFIVAR_LIBS)';
104
105 condition = COND_HAVE_EXEC;
106 };
107 @@ -605,6 +610,8 @@ program = {
108 extra_dist = grub-core/osdep/basic/no_platform.c;
109 extra_dist = grub-core/osdep/unix/platform.c;
110 common = grub-core/osdep/compress.c;
111 + common = grub-core/osdep/efivar.c;
112 + extra_dist = grub-core/osdep/unix/efivar.c;
113 common = util/editenv.c;
114 common = grub-core/osdep/blocklist.c;
115 common = grub-core/osdep/config.c;
116 @@ -618,12 +625,15 @@ program = {
117 common = grub-core/kern/emu/argp_common.c;
118 common = grub-core/osdep/init.c;
119
120 + cflags = '$(EFIVAR_CFLAGS)';
121 +
122 ldadd = '$(LIBLZMA)';
123 ldadd = libgrubmods.a;
124 ldadd = libgrubgcry.a;
125 ldadd = libgrubkern.a;
126 ldadd = grub-core/lib/gnulib/libgnu.a;
127 ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
128 + ldadd = '$(EFIVAR_LIBS)';
129 };
130
131 program = {
132 @@ -645,6 +655,8 @@ program = {
133 common = grub-core/osdep/platform.c;
134 common = grub-core/osdep/platform_unix.c;
135 common = grub-core/osdep/compress.c;
136 + common = grub-core/osdep/efivar.c;
137 + extra_dist = grub-core/osdep/unix/efivar.c;
138 common = util/editenv.c;
139 common = grub-core/osdep/blocklist.c;
140 common = grub-core/osdep/config.c;
141 @@ -657,12 +669,15 @@ program = {
142 common = grub-core/kern/emu/argp_common.c;
143 common = grub-core/osdep/init.c;
144
145 + cflags = '$(EFIVAR_CFLAGS)';
146 +
147 ldadd = '$(LIBLZMA)';
148 ldadd = libgrubmods.a;
149 ldadd = libgrubgcry.a;
150 ldadd = libgrubkern.a;
151 ldadd = grub-core/lib/gnulib/libgnu.a;
152 ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
153 + ldadd = '$(EFIVAR_LIBS)';
154 };
155
156 program = {
157 @@ -684,6 +699,8 @@ program = {
158 common = grub-core/osdep/platform.c;
159 common = grub-core/osdep/platform_unix.c;
160 common = grub-core/osdep/compress.c;
161 + common = grub-core/osdep/efivar.c;
162 + extra_dist = grub-core/osdep/unix/efivar.c;
163 common = util/editenv.c;
164 common = grub-core/osdep/blocklist.c;
165 common = grub-core/osdep/config.c;
166 @@ -693,12 +710,15 @@ program = {
167 common = grub-core/kern/emu/argp_common.c;
168 common = grub-core/osdep/init.c;
169
170 + cflags = '$(EFIVAR_CFLAGS)';
171 +
172 ldadd = '$(LIBLZMA)';
173 ldadd = libgrubmods.a;
174 ldadd = libgrubgcry.a;
175 ldadd = libgrubkern.a;
176 ldadd = grub-core/lib/gnulib/libgnu.a;
177 ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
178 + ldadd = '$(EFIVAR_LIBS)';
179 };
180
181 script = {
182 diff --git a/configure.ac b/configure.ac
183 index e3dca49fa..800c51ee1 100644
184 --- a/configure.ac
185 +++ b/configure.ac
186 @@ -443,6 +443,18 @@ AC_CHECK_HEADER([util.h], [
187 ])
188 AC_SUBST([LIBUTIL])
189
190 +case "$host_os" in
191 + cygwin | windows* | mingw32* | aros*)
192 + ;;
193 + *)
194 + # For setting EFI variables in grub-install.
195 + PKG_CHECK_MODULES([EFIVAR], [efivar efiboot], [
196 + AC_DEFINE([HAVE_EFIVAR], [1],
197 + [Define to 1 if you have the efivar and efiboot libraries.])
198 + ], [:])
199 + ;;
200 +esac
201 +
202 AC_CACHE_CHECK([whether -Wtrampolines work], [grub_cv_host_cc_wtrampolines], [
203 SAVED_CFLAGS="$CFLAGS"
204 CFLAGS="$HOST_CFLAGS -Wtrampolines -Werror"
205 diff --git a/grub-core/osdep/efivar.c b/grub-core/osdep/efivar.c
206 new file mode 100644
207 index 000000000..d2750e252
208 --- /dev/null
209 +++ b/grub-core/osdep/efivar.c
210 @@ -0,0 +1,3 @@
211 +#if !defined (__MINGW32__) && !defined (__CYGWIN__) && !defined (__AROS__)
212 +#include "unix/efivar.c"
213 +#endif
214 diff --git a/grub-core/osdep/unix/efivar.c b/grub-core/osdep/unix/efivar.c
215 new file mode 100644
216 index 000000000..4a58328b4
217 --- /dev/null
218 +++ b/grub-core/osdep/unix/efivar.c
219 @@ -0,0 +1,508 @@
220 +/*
221 + * GRUB -- GRand Unified Bootloader
222 + * Copyright (C) 2013,2019 Free Software Foundation, Inc.
223 + *
224 + * GRUB is free software: you can redistribute it and/or modify
225 + * it under the terms of the GNU General Public License as published by
226 + * the Free Software Foundation, either version 3 of the License, or
227 + * (at your option) any later version.
228 + *
229 + * GRUB is distributed in the hope that it will be useful,
230 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
231 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
232 + * GNU General Public License for more details.
233 + *
234 + * You should have received a copy of the GNU General Public License
235 + * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
236 + */
237 +
238 +/* Contains portions derived from efibootmgr, licensed as follows:
239 + *
240 + * Copyright (C) 2001-2004 Dell, Inc. <Matt_Domsch@dell.com>
241 + * Copyright 2015-2016 Red Hat, Inc. <pjones@redhat.com>
242 + *
243 + * This program is free software; you can redistribute it and/or modify
244 + * it under the terms of the GNU General Public License as published by
245 + * the Free Software Foundation; either version 2 of the License, or
246 + * (at your option) any later version.
247 + */
248 +
249 +#include <config.h>
250 +
251 +#ifdef HAVE_EFIVAR
252 +
253 +#include <grub/util/install.h>
254 +#include <grub/emu/hostdisk.h>
255 +#include <grub/util/misc.h>
256 +#include <grub/list.h>
257 +#include <grub/misc.h>
258 +#include <grub/emu/exec.h>
259 +#include <sys/types.h>
260 +#include <ctype.h>
261 +#include <errno.h>
262 +#include <stdlib.h>
263 +#include <string.h>
264 +
265 +#include <efiboot.h>
266 +#include <efivar.h>
267 +
268 +struct efi_variable {
269 + struct efi_variable *next;
270 + struct efi_variable **prev;
271 + char *name;
272 + efi_guid_t guid;
273 + uint8_t *data;
274 + size_t data_size;
275 + uint32_t attributes;
276 + int num;
277 +};
278 +
279 +/* Boot option attributes. */
280 +#define LOAD_OPTION_ACTIVE 0x00000001
281 +
282 +/* GUIDs. */
283 +#define BLKX_UNKNOWN_GUID \
284 + EFI_GUID (0x47c7b225, 0xc42a, 0x11d2, 0x8e57, 0x00, 0xa0, 0xc9, 0x69, \
285 + 0x72, 0x3b)
286 +
287 +/* Log all errors recorded by libefivar/libefiboot. */
288 +static void
289 +show_efi_errors (void)
290 +{
291 + int i;
292 + int saved_errno = errno;
293 +
294 + for (i = 0; ; ++i)
295 + {
296 + char *filename, *function, *message = NULL;
297 + int line, error = 0, rc;
298 +
299 + rc = efi_error_get (i, &filename, &function, &line, &message, &error);
300 + if (rc < 0)
301 + /* Give up. The caller is going to log an error anyway. */
302 + break;
303 + if (rc == 0)
304 + /* No more errors. */
305 + break;
306 + grub_util_warn ("%s: %s: %s", function, message, strerror (error));
307 + }
308 +
309 + efi_error_clear ();
310 + errno = saved_errno;
311 +}
312 +
313 +static struct efi_variable *
314 +new_efi_variable (void)
315 +{
316 + struct efi_variable *new = xmalloc (sizeof (*new));
317 + memset (new, 0, sizeof (*new));
318 + return new;
319 +}
320 +
321 +static struct efi_variable *
322 +new_boot_variable (void)
323 +{
324 + struct efi_variable *new = new_efi_variable ();
325 + new->guid = EFI_GLOBAL_GUID;
326 + new->attributes = EFI_VARIABLE_NON_VOLATILE |
327 + EFI_VARIABLE_BOOTSERVICE_ACCESS |
328 + EFI_VARIABLE_RUNTIME_ACCESS;
329 + return new;
330 +}
331 +
332 +static void
333 +free_efi_variable (struct efi_variable *entry)
334 +{
335 + if (entry)
336 + {
337 + free (entry->name);
338 + free (entry->data);
339 + free (entry);
340 + }
341 +}
342 +
343 +static int
344 +read_efi_variable (const char *name, struct efi_variable **entry)
345 +{
346 + struct efi_variable *new = new_efi_variable ();
347 + int rc;
348 +
349 + rc = efi_get_variable (EFI_GLOBAL_GUID, name,
350 + &new->data, &new->data_size, &new->attributes);
351 + if (rc < 0)
352 + {
353 + free_efi_variable (new);
354 + new = NULL;
355 + }
356 +
357 + if (new)
358 + {
359 + /* Latest Apple firmware sets the high bit which appears invalid
360 + to the Linux kernel if we write it back, so let's zero it out if it
361 + is set since it would be invalid to set it anyway. */
362 + new->attributes = new->attributes & ~(1 << 31);
363 +
364 + new->name = xstrdup (name);
365 + new->guid = EFI_GLOBAL_GUID;
366 + }
367 +
368 + *entry = new;
369 + return rc;
370 +}
371 +
372 +/* Set an EFI variable, but only if it differs from the current value.
373 + Some firmware implementations are liable to fill up flash space if we set
374 + variables unnecessarily, so try to keep write activity to a minimum. */
375 +static int
376 +set_efi_variable (const char *name, struct efi_variable *entry)
377 +{
378 + struct efi_variable *old = NULL;
379 + int rc = 0;
380 +
381 + read_efi_variable (name, &old);
382 + efi_error_clear ();
383 + if (old && old->attributes == entry->attributes &&
384 + old->data_size == entry->data_size &&
385 + memcmp (old->data, entry->data, entry->data_size) == 0)
386 + grub_util_info ("skipping unnecessary update of EFI variable %s", name);
387 + else
388 + {
389 + rc = efi_set_variable (EFI_GLOBAL_GUID, name,
390 + entry->data, entry->data_size, entry->attributes,
391 + 0644);
392 + if (rc < 0)
393 + grub_util_warn (_("Cannot set EFI variable %s"), name);
394 + }
395 + free_efi_variable (old);
396 + return rc;
397 +}
398 +
399 +static int
400 +cmpvarbyname (const void *p1, const void *p2)
401 +{
402 + const struct efi_variable *var1 = *(const struct efi_variable **)p1;
403 + const struct efi_variable *var2 = *(const struct efi_variable **)p2;
404 + return strcmp (var1->name, var2->name);
405 +}
406 +
407 +static int
408 +read_boot_variables (struct efi_variable **varlist)
409 +{
410 + int rc;
411 + efi_guid_t *guid = NULL;
412 + char *name = NULL;
413 + struct efi_variable **newlist = NULL;
414 + int nentries = 0;
415 + int i;
416 +
417 + while ((rc = efi_get_next_variable_name (&guid, &name)) > 0)
418 + {
419 + const char *snum = name + sizeof ("Boot") - 1;
420 + struct efi_variable *var = NULL;
421 + unsigned int num;
422 +
423 + if (memcmp (guid, &efi_guid_global, sizeof (efi_guid_global)) != 0 ||
424 + strncmp (name, "Boot", sizeof ("Boot") - 1) != 0 ||
425 + !grub_isxdigit (snum[0]) || !grub_isxdigit (snum[1]) ||
426 + !grub_isxdigit (snum[2]) || !grub_isxdigit (snum[3]))
427 + continue;
428 +
429 + rc = read_efi_variable (name, &var);
430 + if (rc < 0)
431 + break;
432 +
433 + if (sscanf (var->name, "Boot%04X-%*s", &num) == 1 && num < 65536)
434 + var->num = num;
435 +
436 + newlist = xrealloc (newlist, (++nentries) * sizeof (*newlist));
437 + newlist[nentries - 1] = var;
438 + }
439 + if (rc == 0 && newlist)
440 + {
441 + qsort (newlist, nentries, sizeof (*newlist), cmpvarbyname);
442 + for (i = nentries - 1; i >= 0; --i)
443 + grub_list_push (GRUB_AS_LIST_P (varlist), GRUB_AS_LIST (newlist[i]));
444 + }
445 + else if (newlist)
446 + {
447 + for (i = 0; i < nentries; ++i)
448 + free_efi_variable (newlist[i]);
449 + free (newlist);
450 + }
451 + return rc;
452 +}
453 +
454 +#define GET_ORDER(data, i) \
455 + ((uint16_t) ((data)[(i) * 2]) + ((data)[(i) * 2 + 1] << 8))
456 +#define SET_ORDER(data, i, num) \
457 + do { \
458 + (data)[(i) * 2] = (num) & 0xFF; \
459 + (data)[(i) * 2 + 1] = ((num) >> 8) & 0xFF; \
460 + } while (0)
461 +
462 +static void
463 +remove_from_boot_order (struct efi_variable *order, uint16_t num)
464 +{
465 + unsigned int old_i, new_i;
466 +
467 + /* We've got an array (in order->data) of the order. Squeeze out any
468 + instance of the entry we're deleting by shifting the remainder down. */
469 + for (old_i = 0, new_i = 0;
470 + old_i < order->data_size / sizeof (uint16_t);
471 + ++old_i)
472 + {
473 + uint16_t old_num = GET_ORDER (order->data, old_i);
474 + if (old_num != num)
475 + {
476 + if (new_i != old_i)
477 + SET_ORDER (order->data, new_i, old_num);
478 + ++new_i;
479 + }
480 + }
481 +
482 + order->data_size = new_i * sizeof (uint16_t);
483 +}
484 +
485 +static void
486 +add_to_boot_order (struct efi_variable *order, uint16_t num)
487 +{
488 + int i;
489 + size_t new_data_size;
490 + uint8_t *new_data;
491 +
492 + /* Check whether this entry is already in the boot order. If it is, leave
493 + it alone. */
494 + for (i = 0; i < order->data_size / sizeof (uint16_t); ++i)
495 + if (GET_ORDER (order->data, i) == num)
496 + return;
497 +
498 + new_data_size = order->data_size + sizeof (uint16_t);
499 + new_data = xmalloc (new_data_size);
500 + SET_ORDER (new_data, 0, num);
501 + memcpy (new_data + sizeof (uint16_t), order->data, order->data_size);
502 + free (order->data);
503 + order->data = new_data;
504 + order->data_size = new_data_size;
505 +}
506 +
507 +static int
508 +find_free_boot_num (struct efi_variable *entries)
509 +{
510 + int num_vars = 0, i;
511 + struct efi_variable *entry;
512 +
513 + FOR_LIST_ELEMENTS (entry, entries)
514 + ++num_vars;
515 +
516 + if (num_vars == 0)
517 + return 0;
518 +
519 + /* O(n^2), but n is small and this is easy. */
520 + for (i = 0; i < num_vars; ++i)
521 + {
522 + int found = 0;
523 + FOR_LIST_ELEMENTS (entry, entries)
524 + {
525 + if (entry->num == i)
526 + {
527 + found = 1;
528 + break;
529 + }
530 + }
531 + if (!found)
532 + return i;
533 + }
534 +
535 + return i;
536 +}
537 +
538 +static int
539 +get_edd_version (void)
540 +{
541 + efi_guid_t blkx_guid = BLKX_UNKNOWN_GUID;
542 + uint8_t *data = NULL;
543 + size_t data_size = 0;
544 + uint32_t attributes;
545 + efidp_header *path;
546 + int rc;
547 +
548 + rc = efi_get_variable (blkx_guid, "blk0", &data, &data_size, &attributes);
549 + if (rc < 0)
550 + return rc;
551 +
552 + path = (efidp_header *) data;
553 + if (path->type == 2 && path->subtype == 1)
554 + return 3;
555 + return 1;
556 +}
557 +
558 +static struct efi_variable *
559 +make_boot_variable (int num, const char *disk, int part, const char *loader,
560 + const char *label)
561 +{
562 + struct efi_variable *entry = new_boot_variable ();
563 + uint32_t options;
564 + uint32_t edd10_devicenum;
565 + ssize_t dp_needed, loadopt_needed;
566 + efidp dp = NULL;
567 +
568 + options = EFIBOOT_ABBREV_HD;
569 + switch (get_edd_version ()) {
570 + case 1:
571 + options = EFIBOOT_ABBREV_EDD10;
572 + break;
573 + case 3:
574 + options = EFIBOOT_ABBREV_NONE;
575 + break;
576 + }
577 +
578 + /* This may not be the right disk; but it's probably only an issue on very
579 + old hardware anyway. */
580 + edd10_devicenum = 0x80;
581 +
582 + dp_needed = efi_generate_file_device_path_from_esp (NULL, 0, disk, part,
583 + loader, options,
584 + edd10_devicenum);
585 + if (dp_needed < 0)
586 + goto err;
587 +
588 + dp = xmalloc (dp_needed);
589 + dp_needed = efi_generate_file_device_path_from_esp ((uint8_t *) dp,
590 + dp_needed, disk, part,
591 + loader, options,
592 + edd10_devicenum);
593 + if (dp_needed < 0)
594 + goto err;
595 +
596 + loadopt_needed = efi_loadopt_create (NULL, 0, LOAD_OPTION_ACTIVE,
597 + dp, dp_needed, (unsigned char *) label,
598 + NULL, 0);
599 + if (loadopt_needed < 0)
600 + goto err;
601 + entry->data_size = loadopt_needed;
602 + entry->data = xmalloc (entry->data_size);
603 + loadopt_needed = efi_loadopt_create (entry->data, entry->data_size,
604 + LOAD_OPTION_ACTIVE, dp, dp_needed,
605 + (unsigned char *) label, NULL, 0);
606 + if (loadopt_needed < 0)
607 + goto err;
608 +
609 + entry->name = xasprintf ("Boot%04X", num);
610 + entry->num = num;
611 +
612 + return entry;
613 +
614 +err:
615 + free_efi_variable (entry);
616 + free (dp);
617 + return NULL;
618 +}
619 +
620 +int
621 +grub_install_efivar_register_efi (grub_device_t efidir_grub_dev,
622 + const char *efifile_path,
623 + const char *efi_distributor)
624 +{
625 + const char *efidir_disk;
626 + int efidir_part;
627 + struct efi_variable *entries = NULL, *entry;
628 + struct efi_variable *order;
629 + int entry_num = -1;
630 + int rc;
631 +
632 + efidir_disk = grub_util_biosdisk_get_osdev (efidir_grub_dev->disk);
633 + efidir_part = efidir_grub_dev->disk->partition ? efidir_grub_dev->disk->partition->number + 1 : 1;
634 +
635 +#ifdef __linux__
636 + /*
637 + * Linux uses efivarfs (mounted on /sys/firmware/efi/efivars) to access the
638 + * EFI variable store. Some legacy systems may still use the deprecated
639 + * efivars interface (accessed through /sys/firmware/efi/vars). Where both
640 + * are present, libefivar will use the former in preference, so attempting
641 + * to load efivars will not interfere with later operations.
642 + */
643 + grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL },
644 + NULL, NULL, "/dev/null");
645 +#endif
646 +
647 + if (!efi_variables_supported ())
648 + {
649 + grub_util_warn ("%s",
650 + _("EFI variables are not supported on this system."));
651 + /* Let the user continue. Perhaps they can still arrange to boot GRUB
652 + manually. */
653 + return 0;
654 + }
655 +
656 + rc = read_boot_variables (&entries);
657 + if (rc < 0)
658 + {
659 + grub_util_warn ("%s", _("Cannot read EFI Boot* variables"));
660 + goto err;
661 + }
662 + rc = read_efi_variable ("BootOrder", &order);
663 + if (rc < 0)
664 + {
665 + order = new_boot_variable ();
666 + order->name = xstrdup ("BootOrder");
667 + efi_error_clear ();
668 + }
669 +
670 + /* Delete old entries from the same distributor. */
671 + FOR_LIST_ELEMENTS (entry, entries)
672 + {
673 + efi_load_option *load_option = (efi_load_option *) entry->data;
674 + const char *label;
675 +
676 + if (entry->num < 0)
677 + continue;
678 + label = (const char *) efi_loadopt_desc (load_option, entry->data_size);
679 + if (strcasecmp (label, efi_distributor) != 0)
680 + continue;
681 +
682 + /* To avoid problems with some firmware implementations, reuse the first
683 + matching variable we find rather than deleting and recreating it. */
684 + if (entry_num == -1)
685 + entry_num = entry->num;
686 + else
687 + {
688 + grub_util_info ("deleting superfluous EFI variable %s (%s)",
689 + entry->name, label);
690 + rc = efi_del_variable (EFI_GLOBAL_GUID, entry->name);
691 + if (rc < 0)
692 + {
693 + grub_util_warn (_("Cannot delete EFI variable %s"), entry->name);
694 + goto err;
695 + }
696 + }
697 +
698 + remove_from_boot_order (order, (uint16_t) entry->num);
699 + }
700 +
701 + if (entry_num == -1)
702 + entry_num = find_free_boot_num (entries);
703 + entry = make_boot_variable (entry_num, efidir_disk, efidir_part,
704 + efifile_path, efi_distributor);
705 + if (!entry)
706 + goto err;
707 +
708 + grub_util_info ("setting EFI variable %s", entry->name);
709 + rc = set_efi_variable (entry->name, entry);
710 + if (rc < 0)
711 + goto err;
712 +
713 + add_to_boot_order (order, (uint16_t) entry_num);
714 +
715 + grub_util_info ("setting EFI variable BootOrder");
716 + rc = set_efi_variable ("BootOrder", order);
717 + if (rc < 0)
718 + goto err;
719 +
720 + return 0;
721 +
722 +err:
723 + show_efi_errors ();
724 + return errno;
725 +}
726 +
727 +#endif /* HAVE_EFIVAR */
728 diff --git a/grub-core/osdep/unix/platform.c b/grub-core/osdep/unix/platform.c
729 index 9c439326a..b561174ea 100644
730 --- a/grub-core/osdep/unix/platform.c
731 +++ b/grub-core/osdep/unix/platform.c
732 @@ -19,15 +19,12 @@
733 #include <config.h>
734
735 #include <grub/util/install.h>
736 -#include <grub/emu/hostdisk.h>
737 #include <grub/util/misc.h>
738 #include <grub/misc.h>
739 #include <grub/i18n.h>
740 #include <grub/emu/exec.h>
741 #include <sys/types.h>
742 -#include <dirent.h>
743 #include <string.h>
744 -#include <errno.h>
745
746 static char *
747 get_ofpathname (const char *dev)
748 @@ -78,102 +75,19 @@ get_ofpathname (const char *dev)
749 dev);
750 }
751
752 -static int
753 -grub_install_remove_efi_entries_by_distributor (const char *efi_distributor)
754 -{
755 - int fd;
756 - pid_t pid = grub_util_exec_pipe ((const char * []){ "efibootmgr", NULL }, &fd);
757 - char *line = NULL;
758 - size_t len = 0;
759 - int rc = 0;
760 -
761 - if (!pid)
762 - {
763 - grub_util_warn (_("Unable to open stream from %s: %s"),
764 - "efibootmgr", strerror (errno));
765 - return errno;
766 - }
767 -
768 - FILE *fp = fdopen (fd, "r");
769 - if (!fp)
770 - {
771 - grub_util_warn (_("Unable to open stream from %s: %s"),
772 - "efibootmgr", strerror (errno));
773 - return errno;
774 - }
775 -
776 - line = xmalloc (80);
777 - len = 80;
778 - while (1)
779 - {
780 - int ret;
781 - char *bootnum;
782 - ret = getline (&line, &len, fp);
783 - if (ret == -1)
784 - break;
785 - if (grub_memcmp (line, "Boot", sizeof ("Boot") - 1) != 0
786 - || line[sizeof ("Boot") - 1] < '0'
787 - || line[sizeof ("Boot") - 1] > '9')
788 - continue;
789 - if (!strcasestr (line, efi_distributor))
790 - continue;
791 - bootnum = line + sizeof ("Boot") - 1;
792 - bootnum[4] = '\0';
793 - if (!verbosity)
794 - rc = grub_util_exec ((const char * []){ "efibootmgr", "-q",
795 - "-b", bootnum, "-B", NULL });
796 - else
797 - rc = grub_util_exec ((const char * []){ "efibootmgr",
798 - "-b", bootnum, "-B", NULL });
799 - }
800 -
801 - free (line);
802 - return rc;
803 -}
804 -
805 int
806 grub_install_register_efi (grub_device_t efidir_grub_dev,
807 const char *efifile_path,
808 const char *efi_distributor)
809 {
810 - const char * efidir_disk;
811 - int efidir_part;
812 - int ret;
813 - efidir_disk = grub_util_biosdisk_get_osdev (efidir_grub_dev->disk);
814 - efidir_part = efidir_grub_dev->disk->partition ? efidir_grub_dev->disk->partition->number + 1 : 1;
815 -
816 - if (grub_util_exec_redirect_null ((const char * []){ "efibootmgr", "--version", NULL }))
817 - {
818 - /* TRANSLATORS: This message is shown when required executable `%s'
819 - isn't found. */
820 - grub_util_error (_("%s: not found"), "efibootmgr");
821 - }
822 -
823 - /* On Linux, we need the efivars kernel modules. */
824 -#ifdef __linux__
825 - grub_util_exec ((const char * []){ "modprobe", "-q", "efivars", NULL });
826 +#ifdef HAVE_EFIVAR
827 + return grub_install_efivar_register_efi (efidir_grub_dev, efifile_path,
828 + efi_distributor);
829 +#else
830 + grub_util_error ("%s",
831 + _("GRUB was not built with efivar support; "
832 + "cannot register EFI boot entry"));
833 #endif
834 - /* Delete old entries from the same distributor. */
835 - ret = grub_install_remove_efi_entries_by_distributor (efi_distributor);
836 - if (ret)
837 - return ret;
838 -
839 - char *efidir_part_str = xasprintf ("%d", efidir_part);
840 -
841 - if (!verbosity)
842 - ret = grub_util_exec ((const char * []){ "efibootmgr", "-q",
843 - "-c", "-d", efidir_disk,
844 - "-p", efidir_part_str, "-w",
845 - "-L", efi_distributor, "-l",
846 - efifile_path, NULL });
847 - else
848 - ret = grub_util_exec ((const char * []){ "efibootmgr",
849 - "-c", "-d", efidir_disk,
850 - "-p", efidir_part_str, "-w",
851 - "-L", efi_distributor, "-l",
852 - efifile_path, NULL });
853 - free (efidir_part_str);
854 - return ret;
855 }
856
857 void
858 diff --git a/include/grub/util/install.h b/include/grub/util/install.h
859 index 8aeb5c4f2..a521f1663 100644
860 --- a/include/grub/util/install.h
861 +++ b/include/grub/util/install.h
862 @@ -219,6 +219,11 @@ grub_install_get_default_x86_platform (void);
863 const char *
864 grub_install_get_default_powerpc_machtype (void);
865
866 +int
867 +grub_install_efivar_register_efi (grub_device_t efidir_grub_dev,
868 + const char *efifile_path,
869 + const char *efi_distributor);
870 +
871 int
872 grub_install_register_efi (grub_device_t efidir_grub_dev,
873 const char *efifile_path,
874 diff --git a/util/grub-install.c b/util/grub-install.c
875 index 6462d3c70..d66de7f8e 100644
876 --- a/util/grub-install.c
877 +++ b/util/grub-install.c
878 @@ -2059,7 +2059,7 @@ main (int argc, char *argv[])
879 "\\System\\Library\\CoreServices",
880 efi_distributor);
881 if (ret)
882 - grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
883 + grub_util_error (_("failed to register the EFI boot entry: %s"),
884 strerror (ret));
885 }
886
887 @@ -2173,7 +2173,7 @@ main (int argc, char *argv[])
888 ret = grub_install_register_efi (efidir_grub_dev,
889 efifile_path, efi_distributor);
890 if (ret)
891 - grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
892 + grub_util_error (_("failed to register the EFI boot entry: %s"),
893 strerror (ret));
894 }
895 break;