]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/util/grub-install.c
Fix the order issue in TreeView mode. (#3218)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / util / grub-install.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013 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 #include <config.h>
20 #include <grub/types.h>
21 #include <grub/emu/misc.h>
22 #include <grub/util/misc.h>
23 #include <grub/misc.h>
24 #include <grub/device.h>
25 #include <grub/disk.h>
26 #include <grub/file.h>
27 #include <grub/fs.h>
28 #include <grub/env.h>
29 #include <grub/term.h>
30 #include <grub/mm.h>
31 #include <grub/lib/hexdump.h>
32 #include <grub/crypto.h>
33 #include <grub/command.h>
34 #include <grub/i18n.h>
35 #include <grub/zfs/zfs.h>
36 #include <grub/util/install.h>
37 #include <grub/emu/getroot.h>
38 #include <grub/diskfilter.h>
39 #include <grub/cryptodisk.h>
40 #include <grub/legacy_parse.h>
41 #include <grub/gpt_partition.h>
42 #include <grub/emu/config.h>
43 #include <grub/util/ofpath.h>
44 #include <grub/hfsplus.h>
45
46 #include <string.h>
47
48 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
49 #pragma GCC diagnostic ignored "-Wmissing-declarations"
50 #include <argp.h>
51 #pragma GCC diagnostic error "-Wmissing-prototypes"
52 #pragma GCC diagnostic error "-Wmissing-declarations"
53
54 #include "progname.h"
55
56 static char *target;
57 static int removable = 0;
58 static int recheck = 0;
59 static int update_nvram = 1;
60 static char *install_device = NULL;
61 static char *debug_image = NULL;
62 static char *rootdir = NULL;
63 static char *bootdir = NULL;
64 static int allow_floppy = 0;
65 static int force_file_id = 0;
66 static char *disk_module = NULL;
67 static char *efidir = NULL;
68 static char *macppcdir = NULL;
69 static int force = 0;
70 static int have_abstractions = 0;
71 static int have_cryptodisk = 0;
72 static char * bootloader_id;
73 static int have_load_cfg = 0;
74 static FILE * load_cfg_f = NULL;
75 static char *load_cfg;
76 static int install_bootsector = 1;
77 static char *label_font;
78 static char *label_color;
79 static char *label_bgcolor;
80 static char *product_version;
81 static int add_rs_codes = 1;
82
83 enum
84 {
85 OPTION_BOOT_DIRECTORY = 0x301,
86 OPTION_ROOT_DIRECTORY,
87 OPTION_TARGET,
88 OPTION_SETUP,
89 OPTION_MKRELPATH,
90 OPTION_MKDEVICEMAP,
91 OPTION_PROBE,
92 OPTION_EDITENV,
93 OPTION_ALLOW_FLOPPY,
94 OPTION_RECHECK,
95 OPTION_FORCE,
96 OPTION_FORCE_FILE_ID,
97 OPTION_NO_NVRAM,
98 OPTION_REMOVABLE,
99 OPTION_BOOTLOADER_ID,
100 OPTION_EFI_DIRECTORY,
101 OPTION_FONT,
102 OPTION_DEBUG,
103 OPTION_DEBUG_IMAGE,
104 OPTION_NO_FLOPPY,
105 OPTION_DISK_MODULE,
106 OPTION_NO_BOOTSECTOR,
107 OPTION_NO_RS_CODES,
108 OPTION_MACPPC_DIRECTORY,
109 OPTION_LABEL_FONT,
110 OPTION_LABEL_COLOR,
111 OPTION_LABEL_BGCOLOR,
112 OPTION_PRODUCT_VERSION
113 };
114
115 static int fs_probe = 1;
116
117 static error_t
118 argp_parser (int key, char *arg, struct argp_state *state)
119 {
120 if (grub_install_parse (key, arg))
121 return 0;
122 switch (key)
123 {
124 case OPTION_FORCE_FILE_ID:
125 force_file_id = 1;
126 return 0;
127 case 's':
128 fs_probe = 0;
129 return 0;
130
131 case OPTION_SETUP:
132 if (!grub_strstr (arg, "setup"))
133 install_bootsector = 0;
134 return 0;
135
136 case OPTION_PRODUCT_VERSION:
137 free (product_version);
138 product_version = xstrdup (arg);
139 return 0;
140 case OPTION_LABEL_FONT:
141 free (label_font);
142 label_font = xstrdup (arg);
143 return 0;
144
145 case OPTION_LABEL_COLOR:
146 free (label_color);
147 label_color = xstrdup (arg);
148 return 0;
149
150 case OPTION_LABEL_BGCOLOR:
151 free (label_bgcolor);
152 label_bgcolor = xstrdup (arg);
153 return 0;
154
155 /* Accept and ignore for compatibility. */
156 case OPTION_FONT:
157 case OPTION_MKRELPATH:
158 case OPTION_PROBE:
159 case OPTION_EDITENV:
160 case OPTION_MKDEVICEMAP:
161 case OPTION_NO_FLOPPY:
162 return 0;
163 case OPTION_ROOT_DIRECTORY:
164 /* Accept for compatibility. */
165 free (rootdir);
166 rootdir = xstrdup (arg);
167 return 0;
168
169 case OPTION_BOOT_DIRECTORY:
170 free (bootdir);
171 bootdir = xstrdup (arg);
172 return 0;
173
174 case OPTION_MACPPC_DIRECTORY:
175 free (macppcdir);
176 macppcdir = xstrdup (arg);
177 return 0;
178
179 case OPTION_EFI_DIRECTORY:
180 free (efidir);
181 efidir = xstrdup (arg);
182 return 0;
183
184 case OPTION_DISK_MODULE:
185 free (disk_module);
186 disk_module = xstrdup (arg);
187 return 0;
188
189 case OPTION_TARGET:
190 free (target);
191 target = xstrdup (arg);
192 return 0;
193
194 case OPTION_DEBUG_IMAGE:
195 free (debug_image);
196 debug_image = xstrdup (arg);
197 return 0;
198
199 case OPTION_NO_NVRAM:
200 update_nvram = 0;
201 return 0;
202
203 case OPTION_FORCE:
204 force = 1;
205 return 0;
206
207 case OPTION_RECHECK:
208 recheck = 1;
209 return 0;
210
211 case OPTION_REMOVABLE:
212 removable = 1;
213 return 0;
214
215 case OPTION_ALLOW_FLOPPY:
216 allow_floppy = 1;
217 return 0;
218
219 case OPTION_NO_BOOTSECTOR:
220 install_bootsector = 0;
221 return 0;
222
223 case OPTION_NO_RS_CODES:
224 add_rs_codes = 0;
225 return 0;
226
227 case OPTION_DEBUG:
228 verbosity++;
229 return 0;
230
231 case OPTION_BOOTLOADER_ID:
232 free (bootloader_id);
233 bootloader_id = xstrdup (arg);
234 return 0;
235
236 case ARGP_KEY_ARG:
237 if (install_device)
238 grub_util_error ("%s", _("More than one install device?"));
239 install_device = xstrdup (arg);
240 return 0;
241
242 default:
243 return ARGP_ERR_UNKNOWN;
244 }
245 }
246
247
248 static struct argp_option options[] = {
249 GRUB_INSTALL_OPTIONS,
250 {"boot-directory", OPTION_BOOT_DIRECTORY, N_("DIR"),
251 0, N_("install GRUB images under the directory DIR/%s instead of the %s directory"), 2},
252 {"root-directory", OPTION_ROOT_DIRECTORY, N_("DIR"),
253 OPTION_HIDDEN, 0, 2},
254 {"font", OPTION_FONT, N_("FILE"),
255 OPTION_HIDDEN, 0, 2},
256 {"target", OPTION_TARGET, N_("TARGET"),
257 /* TRANSLATORS: "TARGET" as in "target platform". */
258 0, N_("install GRUB for TARGET platform [default=%s]; available targets: %s"), 2},
259 {"grub-setup", OPTION_SETUP, "FILE", OPTION_HIDDEN, 0, 2},
260 {"grub-mkrelpath", OPTION_MKRELPATH, "FILE", OPTION_HIDDEN, 0, 2},
261 {"grub-mkdevicemap", OPTION_MKDEVICEMAP, "FILE", OPTION_HIDDEN, 0, 2},
262 {"grub-probe", OPTION_PROBE, "FILE", OPTION_HIDDEN, 0, 2},
263 {"grub-editenv", OPTION_EDITENV, "FILE", OPTION_HIDDEN, 0, 2},
264 {"allow-floppy", OPTION_ALLOW_FLOPPY, 0, 0,
265 /* TRANSLATORS: "may break" doesn't just mean that option wouldn't have any
266 effect but that it will make the resulting install unbootable from HDD. */
267 N_("make the drive also bootable as floppy (default for fdX devices)."
268 " May break on some BIOSes."), 2},
269 {"recheck", OPTION_RECHECK, 0, 0,
270 N_("delete device map if it already exists"), 2},
271 {"force", OPTION_FORCE, 0, 0,
272 N_("install even if problems are detected"), 2},
273 {"force-file-id", OPTION_FORCE_FILE_ID, 0, 0,
274 N_("use identifier file even if UUID is available"), 2},
275 {"disk-module", OPTION_DISK_MODULE, N_("MODULE"), 0,
276 N_("disk module to use (biosdisk or native). "
277 "This option is only available on BIOS target."), 2},
278 {"no-nvram", OPTION_NO_NVRAM, 0, 0,
279 N_("don't update the `boot-device'/`Boot*' NVRAM variables. "
280 "This option is only available on EFI and IEEE1275 targets."), 2},
281 {"skip-fs-probe",'s',0, 0,
282 N_("do not probe for filesystems in DEVICE"), 0},
283 {"no-bootsector", OPTION_NO_BOOTSECTOR, 0, 0,
284 N_("do not install bootsector"), 0},
285 {"no-rs-codes", OPTION_NO_RS_CODES, 0, 0,
286 N_("Do not apply any reed-solomon codes when embedding core.img. "
287 "This option is only available on x86 BIOS targets."), 0},
288
289 {"debug", OPTION_DEBUG, 0, OPTION_HIDDEN, 0, 2},
290 {"no-floppy", OPTION_NO_FLOPPY, 0, OPTION_HIDDEN, 0, 2},
291 {"debug-image", OPTION_DEBUG_IMAGE, N_("STRING"), OPTION_HIDDEN, 0, 2},
292 {"removable", OPTION_REMOVABLE, 0, 0,
293 N_("the installation device is removable. "
294 "This option is only available on EFI."), 2},
295 {"bootloader-id", OPTION_BOOTLOADER_ID, N_("ID"), 0,
296 N_("the ID of bootloader. This option is only available on EFI and Macs."), 2},
297 {"efi-directory", OPTION_EFI_DIRECTORY, N_("DIR"), 0,
298 N_("use DIR as the EFI System Partition root."), 2},
299 {"macppc-directory", OPTION_MACPPC_DIRECTORY, N_("DIR"), 0,
300 N_("use DIR for PPC MAC install."), 2},
301 {"label-font", OPTION_LABEL_FONT, N_("FILE"), 0, N_("use FILE as font for label"), 2},
302 {"label-color", OPTION_LABEL_COLOR, N_("COLOR"), 0, N_("use COLOR for label"), 2},
303 {"label-bgcolor", OPTION_LABEL_BGCOLOR, N_("COLOR"), 0, N_("use COLOR for label background"), 2},
304 {"product-version", OPTION_PRODUCT_VERSION, N_("STRING"), 0, N_("use STRING as product version"), 2},
305 {0, 0, 0, 0, 0, 0}
306 };
307
308 static const char *
309 get_default_platform (void)
310 {
311 #ifdef __powerpc__
312 return "powerpc-ieee1275";
313 #elif defined (__sparc__) || defined (__sparc64__)
314 return "sparc64-ieee1275";
315 #elif defined (__MIPSEL__)
316 #if _MIPS_SIM == _ABI64
317 return "mips64el-efi";
318 #else
319 return "mipsel-loongson";
320 #endif
321 #elif defined (__MIPSEB__)
322 return "mips-arc";
323 #elif defined (__ia64__)
324 return "ia64-efi";
325 #elif defined (__arm__)
326 return grub_install_get_default_arm_platform ();
327 #elif defined (__aarch64__)
328 return "arm64-efi";
329 #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__)
330 return grub_install_get_default_x86_platform ();
331 #else
332 return NULL;
333 #endif
334 }
335
336 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
337
338 static char *
339 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
340 {
341 switch (key)
342 {
343 case OPTION_BOOT_DIRECTORY:
344 return xasprintf (text, GRUB_DIR_NAME, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME);
345 case OPTION_TARGET:
346 {
347 char *plats = grub_install_get_platforms_string ();
348 char *ret;
349 ret = xasprintf (text, get_default_platform (), plats);
350 free (plats);
351 return ret;
352 }
353 case ARGP_KEY_HELP_POST_DOC:
354 return xasprintf (text, program_name, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME);
355 default:
356 return grub_install_help_filter (key, text, input);
357 }
358 }
359
360 #pragma GCC diagnostic error "-Wformat-nonliteral"
361
362 /* TRANSLATORS: INSTALL_DEVICE isn't an identifier and is the DEVICE you
363 install to. */
364 struct argp argp = {
365 options, argp_parser, N_("[OPTION] [INSTALL_DEVICE]"),
366 N_("Install GRUB on your drive.")"\v"
367 N_("INSTALL_DEVICE must be system device filename.\n"
368 "%s copies GRUB images into %s. On some platforms, it"
369 " may also install GRUB into the boot sector."),
370 NULL, help_filter, NULL
371 };
372
373 static int
374 probe_raid_level (grub_disk_t disk)
375 {
376 /* disk might be NULL in the case of a LVM physical volume with no LVM
377 signature. Ignore such cases here. */
378 if (!disk)
379 return -1;
380
381 if (disk->dev->id != GRUB_DISK_DEVICE_DISKFILTER_ID)
382 return -1;
383
384 if (disk->name[0] != 'm' || disk->name[1] != 'd')
385 return -1;
386
387 if (!((struct grub_diskfilter_lv *) disk->data)->segments)
388 return -1;
389 return ((struct grub_diskfilter_lv *) disk->data)->segments->type;
390 }
391
392 static void
393 push_partmap_module (const char *map, void *data __attribute__ ((unused)))
394 {
395 char buf[50];
396
397 if (strcmp (map, "openbsd") == 0 || strcmp (map, "netbsd") == 0)
398 {
399 grub_install_push_module ("part_bsd");
400 return;
401 }
402
403 snprintf (buf, sizeof (buf), "part_%s", map);
404 grub_install_push_module (buf);
405 }
406
407 static void
408 push_cryptodisk_module (const char *mod, void *data __attribute__ ((unused)))
409 {
410 grub_install_push_module (mod);
411 }
412
413 static void
414 probe_mods (grub_disk_t disk)
415 {
416 grub_partition_t part;
417 grub_disk_memberlist_t list = NULL, tmp;
418 int raid_level;
419
420 if (disk->partition == NULL)
421 grub_util_info ("no partition map found for %s", disk->name);
422
423 for (part = disk->partition; part; part = part->parent)
424 push_partmap_module (part->partmap->name, NULL);
425
426 if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID)
427 {
428 grub_diskfilter_get_partmap (disk, push_partmap_module, NULL);
429 have_abstractions = 1;
430 }
431
432 if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
433 && (grub_memcmp (disk->name, "lvm/", sizeof ("lvm/") - 1) == 0 ||
434 grub_memcmp (disk->name, "lvmid/", sizeof ("lvmid/") - 1) == 0))
435 grub_install_push_module ("lvm");
436
437 if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
438 && grub_memcmp (disk->name, "ldm/", sizeof ("ldm/") - 1) == 0)
439 grub_install_push_module ("ldm");
440
441 if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
442 {
443 grub_util_cryptodisk_get_abstraction (disk,
444 push_cryptodisk_module, NULL);
445 have_abstractions = 1;
446 have_cryptodisk = 1;
447 }
448
449 raid_level = probe_raid_level (disk);
450 if (raid_level >= 0)
451 {
452 grub_install_push_module ("diskfilter");
453 if (disk->dev->disk_raidname)
454 grub_install_push_module (disk->dev->disk_raidname (disk));
455 }
456 if (raid_level == 5)
457 grub_install_push_module ("raid5rec");
458 if (raid_level == 6)
459 grub_install_push_module ("raid6rec");
460
461 /* In case of LVM/RAID, check the member devices as well. */
462 if (disk->dev->disk_memberlist)
463 list = disk->dev->disk_memberlist (disk);
464 while (list)
465 {
466 probe_mods (list->disk);
467 tmp = list->next;
468 free (list);
469 list = tmp;
470 }
471 }
472
473 static int
474 have_bootdev (enum grub_install_plat pl)
475 {
476 switch (pl)
477 {
478 case GRUB_INSTALL_PLATFORM_I386_PC:
479 case GRUB_INSTALL_PLATFORM_I386_EFI:
480 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
481 case GRUB_INSTALL_PLATFORM_IA64_EFI:
482 case GRUB_INSTALL_PLATFORM_ARM_EFI:
483 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
484 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
485 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
486 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
487 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
488 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
489 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
490 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
491 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
492 return 1;
493
494 case GRUB_INSTALL_PLATFORM_I386_QEMU:
495 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
496 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
497 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
498 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
499 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
500
501 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
502 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
503
504 case GRUB_INSTALL_PLATFORM_I386_XEN:
505 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
506 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
507 return 0;
508
509 /* pacify warning. */
510 case GRUB_INSTALL_PLATFORM_MAX:
511 return 0;
512 }
513 return 0;
514 }
515
516 static void
517 probe_cryptodisk_uuid (grub_disk_t disk)
518 {
519 grub_disk_memberlist_t list = NULL, tmp;
520
521 /* In case of LVM/RAID, check the member devices as well. */
522 if (disk->dev->disk_memberlist)
523 {
524 list = disk->dev->disk_memberlist (disk);
525 }
526 while (list)
527 {
528 probe_cryptodisk_uuid (list->disk);
529 tmp = list->next;
530 free (list);
531 list = tmp;
532 }
533 if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
534 {
535 const char *uuid = grub_util_cryptodisk_get_uuid (disk);
536 if (!load_cfg_f)
537 load_cfg_f = grub_util_fopen (load_cfg, "wb");
538 have_load_cfg = 1;
539
540 fprintf (load_cfg_f, "cryptomount -u %s\n",
541 uuid);
542 }
543 }
544
545 static int
546 is_same_disk (const char *a, const char *b)
547 {
548 while (1)
549 {
550 if ((*a == ',' || *a == '\0') && (*b == ',' || *b == '\0'))
551 return 1;
552 if (*a != *b)
553 return 0;
554 if (*a == '\\')
555 {
556 if (a[1] != b[1])
557 return 0;
558 a += 2;
559 b += 2;
560 continue;
561 }
562 a++;
563 b++;
564 }
565 }
566
567 static char *
568 get_rndstr (void)
569 {
570 grub_uint8_t rnd[15];
571 const size_t sz = sizeof (rnd) * GRUB_CHAR_BIT / 5;
572 char * ret = xmalloc (sz + 1);
573 size_t i;
574 if (grub_get_random (rnd, sizeof (rnd)))
575 grub_util_error ("%s", _("couldn't retrieve random data"));
576 for (i = 0; i < sz; i++)
577 {
578 grub_size_t b = i * 5;
579 grub_uint8_t r;
580 grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
581 grub_size_t f2;
582 if (f1 > 5)
583 f1 = 5;
584 f2 = 5 - f1;
585 r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
586 if (f2)
587 r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
588 if (r < 10)
589 ret[i] = '0' + r;
590 else
591 ret[i] = 'a' + (r - 10);
592 }
593 ret[sz] = '\0';
594 return ret;
595 }
596
597 static char *
598 escape (const char *in)
599 {
600 char *ptr;
601 char *ret;
602 int overhead = 0;
603
604 for (ptr = (char*)in; *ptr; ptr++)
605 if (*ptr == '\'')
606 overhead += 3;
607 ret = grub_malloc (ptr - in + overhead + 1);
608 if (!ret)
609 return NULL;
610
611 grub_strchrsub (ret, in, '\'', "'\\''");
612 return ret;
613 }
614
615 static struct grub_util_config config;
616
617 static void
618 device_map_check_duplicates (const char *dev_map)
619 {
620 FILE *fp;
621 char buf[1024]; /* XXX */
622 size_t alloced = 8;
623 size_t filled = 0;
624 char **d;
625 size_t i;
626
627 if (dev_map[0] == '\0')
628 return;
629
630 fp = grub_util_fopen (dev_map, "r");
631 if (! fp)
632 return;
633
634 d = xmalloc (alloced * sizeof (d[0]));
635
636 while (fgets (buf, sizeof (buf), fp))
637 {
638 char *p = buf;
639 char *e;
640
641 /* Skip leading spaces. */
642 while (*p && grub_isspace (*p))
643 p++;
644
645 /* If the first character is `#' or NUL, skip this line. */
646 if (*p == '\0' || *p == '#')
647 continue;
648
649 if (*p != '(')
650 continue;
651
652 p++;
653
654 e = p;
655 p = strchr (p, ')');
656 if (! p)
657 continue;
658
659 if (filled >= alloced)
660 {
661 alloced *= 2;
662 d = xrealloc (d, alloced * sizeof (d[0]));
663 }
664
665 *p = '\0';
666
667 d[filled++] = xstrdup (e);
668 }
669
670 fclose (fp);
671
672 qsort (d, filled, sizeof (d[0]), grub_qsort_strcmp);
673
674 for (i = 0; i + 1 < filled; i++)
675 if (strcmp (d[i], d[i+1]) == 0)
676 {
677 grub_util_error (_("the drive %s is defined multiple times in the device map %s"),
678 d[i], dev_map);
679 }
680
681 for (i = 0; i < filled; i++)
682 free (d[i]);
683
684 free (d);
685 }
686
687 static grub_err_t
688 write_to_disk (grub_device_t dev, const char *fn)
689 {
690 char *core_img;
691 size_t core_size;
692 grub_err_t err;
693
694 core_size = grub_util_get_image_size (fn);
695
696 core_img = grub_util_read_image (fn);
697
698 grub_util_info ("writing `%s' to `%s'", fn, dev->disk->name);
699 err = grub_disk_write (dev->disk, 0, 0,
700 core_size, core_img);
701 free (core_img);
702 return err;
703 }
704
705 static int
706 is_prep_partition (grub_device_t dev)
707 {
708 if (!dev->disk)
709 return 0;
710 if (!dev->disk->partition)
711 return 0;
712 if (strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
713 return (dev->disk->partition->msdostype == 0x41);
714
715 if (strcmp (dev->disk->partition->partmap->name, "gpt") == 0)
716 {
717 struct grub_gpt_partentry gptdata;
718 grub_partition_t p = dev->disk->partition;
719 int ret = 0;
720 dev->disk->partition = dev->disk->partition->parent;
721
722 if (grub_disk_read (dev->disk, p->offset, p->index,
723 sizeof (gptdata), &gptdata) == 0)
724 {
725 const grub_gpt_part_guid_t template = {
726 grub_cpu_to_le32_compile_time (0x9e1a2d38),
727 grub_cpu_to_le16_compile_time (0xc612),
728 grub_cpu_to_le16_compile_time (0x4316),
729 { 0xaa, 0x26, 0x8b, 0x49, 0x52, 0x1e, 0x5a, 0x8b }
730 };
731
732 ret = grub_memcmp (&template, &gptdata.type,
733 sizeof (template)) == 0;
734 }
735 dev->disk->partition = p;
736 return ret;
737 }
738
739 return 0;
740 }
741
742 static int
743 is_prep_empty (grub_device_t dev)
744 {
745 grub_disk_addr_t dsize, addr;
746 grub_uint32_t buffer[32768];
747
748 dsize = grub_disk_get_size (dev->disk);
749 for (addr = 0; addr < dsize;
750 addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
751 {
752 grub_size_t sz = sizeof (buffer);
753 grub_uint32_t *ptr;
754
755 if (sizeof (buffer) / GRUB_DISK_SECTOR_SIZE > dsize - addr)
756 sz = (dsize - addr) * GRUB_DISK_SECTOR_SIZE;
757 grub_disk_read (dev->disk, addr, 0, sz, buffer);
758
759 if (addr == 0 && grub_memcmp (buffer, ELFMAG, SELFMAG) == 0)
760 return 1;
761
762 for (ptr = buffer; ptr < buffer + sz / sizeof (*buffer); ptr++)
763 if (*ptr)
764 return 0;
765 }
766
767 return 1;
768 }
769
770 static void
771 bless (grub_device_t dev, const char *path, int x86)
772 {
773 struct stat st;
774 grub_err_t err;
775
776 grub_util_info ("blessing %s", path);
777
778 if (stat (path, &st) < 0)
779 grub_util_error (N_("cannot stat `%s': %s"),
780 path, strerror (errno));
781
782 err = grub_mac_bless_inode (dev, st.st_ino, S_ISDIR (st.st_mode), x86);
783 if (err)
784 grub_util_error ("%s", grub_errmsg);
785 grub_util_info ("blessed");
786 }
787
788 static void
789 fill_core_services (const char *core_services)
790 {
791 char *label;
792 FILE *f;
793 char *label_text;
794 char *label_string = xasprintf ("%s %s", bootloader_id, product_version);
795 char *sysv_plist;
796
797 label = grub_util_path_concat (2, core_services, ".disk_label");
798 grub_util_info ("rendering label %s", label_string);
799 grub_util_render_label (label_font, label_bgcolor ? : "white",
800 label_color ? : "black", label_string, label);
801 grub_util_info ("label rendered");
802 free (label);
803 label_text = grub_util_path_concat (2, core_services, ".disk_label.contentDetails");
804 f = grub_util_fopen (label_text, "wb");
805 fprintf (f, "%s\n", label_string);
806 fclose (f);
807 free (label_string);
808 free (label_text);
809
810 sysv_plist = grub_util_path_concat (2, core_services, "SystemVersion.plist");
811 f = grub_util_fopen (sysv_plist, "wb");
812 fprintf (f,
813 "<plist version=\"1.0\">\n"
814 "<dict>\n"
815 " <key>ProductBuildVersion</key>\n"
816 " <string></string>\n"
817 " <key>ProductName</key>\n"
818 " <string>%s</string>\n"
819 " <key>ProductVersion</key>\n"
820 " <string>%s</string>\n"
821 "</dict>\n"
822 "</plist>\n", bootloader_id, product_version);
823 fclose (f);
824 free (sysv_plist);
825 }
826
827 int
828 main (int argc, char *argv[])
829 {
830 int is_efi = 0;
831 const char *efi_distributor = NULL;
832 const char *efi_file = NULL;
833 char **grub_devices;
834 grub_fs_t grub_fs;
835 grub_device_t grub_dev = NULL;
836 enum grub_install_plat platform;
837 char *grubdir, *device_map;
838 char **curdev, **curdrive;
839 char **grub_drives;
840 char *relative_grubdir;
841 char **efidir_device_names = NULL;
842 grub_device_t efidir_grub_dev = NULL;
843 char *efidir_grub_devname;
844 int efidir_is_mac = 0;
845 int is_prep = 0;
846 const char *pkgdatadir;
847
848 grub_util_host_init (&argc, &argv);
849 product_version = xstrdup (PACKAGE_VERSION);
850 pkgdatadir = grub_util_get_pkgdatadir ();
851 label_font = grub_util_path_concat (2, pkgdatadir, "unicode.pf2");
852
853 argp_parse (&argp, argc, argv, 0, 0, 0);
854
855 if (verbosity > 1)
856 grub_env_set ("debug", "all");
857
858 grub_util_load_config (&config);
859
860 if (!bootloader_id && config.grub_distributor)
861 {
862 char *ptr;
863 bootloader_id = xstrdup (config.grub_distributor);
864 for (ptr = bootloader_id; *ptr && *ptr != ' '; ptr++)
865 if (*ptr >= 'A' && *ptr <= 'Z')
866 *ptr = *ptr - 'A' + 'a';
867 *ptr = '\0';
868 }
869 if (!bootloader_id || bootloader_id[0] == '\0')
870 {
871 free (bootloader_id);
872 bootloader_id = xstrdup ("grub");
873 }
874
875 if (!grub_install_source_directory)
876 {
877 if (!target)
878 {
879 const char * t;
880 t = get_default_platform ();
881 if (!t)
882 grub_util_error ("%s",
883 _("Unable to determine your platform."
884 " Use --target.")
885 );
886 target = xstrdup (t);
887 }
888 grub_install_source_directory
889 = grub_util_path_concat (2, grub_util_get_pkglibdir (), target);
890 }
891
892 platform = grub_install_get_target (grub_install_source_directory);
893
894 {
895 char *platname = grub_install_get_platform_name (platform);
896 fprintf (stderr, _("Installing for %s platform.\n"), platname);
897 free (platname);
898 }
899
900 switch (platform)
901 {
902 case GRUB_INSTALL_PLATFORM_I386_PC:
903 if (!disk_module)
904 disk_module = xstrdup ("biosdisk");
905 break;
906 case GRUB_INSTALL_PLATFORM_I386_EFI:
907 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
908 case GRUB_INSTALL_PLATFORM_ARM_EFI:
909 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
910 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
911 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
912 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
913 case GRUB_INSTALL_PLATFORM_IA64_EFI:
914 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
915 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
916 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
917 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
918 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
919 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
920 case GRUB_INSTALL_PLATFORM_I386_XEN:
921 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
922 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
923 break;
924
925 case GRUB_INSTALL_PLATFORM_I386_QEMU:
926 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
927 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
928 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
929 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
930 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
931 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
932 disk_module = xstrdup ("native");
933 break;
934
935 /* pacify warning. */
936 case GRUB_INSTALL_PLATFORM_MAX:
937 break;
938 }
939
940 switch (platform)
941 {
942 case GRUB_INSTALL_PLATFORM_I386_PC:
943 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
944 if (!install_device)
945 grub_util_error ("%s", _("install device isn't specified"));
946 break;
947 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
948 if (install_device)
949 is_prep = 1;
950 break;
951 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
952 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
953 break;
954 case GRUB_INSTALL_PLATFORM_I386_EFI:
955 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
956 case GRUB_INSTALL_PLATFORM_ARM_EFI:
957 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
958 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
959 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
960 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
961 case GRUB_INSTALL_PLATFORM_IA64_EFI:
962 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
963 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
964 case GRUB_INSTALL_PLATFORM_I386_QEMU:
965 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
966 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
967 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
968 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
969 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
970 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
971 case GRUB_INSTALL_PLATFORM_I386_XEN:
972 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
973 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
974 free (install_device);
975 install_device = NULL;
976 break;
977
978 /* pacify warning. */
979 case GRUB_INSTALL_PLATFORM_MAX:
980 break;
981 }
982
983 if (!bootdir)
984 bootdir = grub_util_path_concat (3, "/", rootdir, GRUB_BOOT_DIR_NAME);
985
986 {
987 char * t = grub_util_path_concat (2, bootdir, GRUB_DIR_NAME);
988 grub_install_mkdir_p (t);
989 grubdir = grub_canonicalize_file_name (t);
990 if (!grubdir)
991 grub_util_error (_("failed to get canonical path of `%s'"), t);
992 free (t);
993 }
994 device_map = grub_util_path_concat (2, grubdir, "device.map");
995
996 if (recheck)
997 grub_util_unlink (device_map);
998
999 device_map_check_duplicates (device_map);
1000 grub_util_biosdisk_init (device_map);
1001
1002 /* Initialize all modules. */
1003 grub_init_all ();
1004 grub_gcry_init_all ();
1005 grub_hostfs_init ();
1006 grub_host_init ();
1007
1008 switch (platform)
1009 {
1010 case GRUB_INSTALL_PLATFORM_I386_EFI:
1011 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1012 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1013 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1014 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1015 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1016 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1017 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1018 is_efi = 1;
1019 break;
1020 default:
1021 is_efi = 0;
1022 break;
1023
1024 /* pacify warning. */
1025 case GRUB_INSTALL_PLATFORM_MAX:
1026 break;
1027 }
1028
1029 /* Find the EFI System Partition. */
1030
1031 if (is_efi)
1032 {
1033 grub_fs_t fs;
1034 free (install_device);
1035 install_device = NULL;
1036 if (!efidir)
1037 {
1038 char *d = grub_util_path_concat (2, bootdir, "efi");
1039 char *dr = NULL;
1040 if (!grub_util_is_directory (d))
1041 {
1042 free (d);
1043 d = grub_util_path_concat (2, bootdir, "EFI");
1044 }
1045 /*
1046 The EFI System Partition may have been given directly using
1047 --root-directory.
1048 */
1049 if (!grub_util_is_directory (d)
1050 && rootdir && grub_strcmp (rootdir, "/") != 0)
1051 {
1052 free (d);
1053 d = xstrdup (rootdir);
1054 }
1055 if (grub_util_is_directory (d))
1056 dr = grub_make_system_path_relative_to_its_root (d);
1057 /* Is it a mount point? */
1058 if (dr && dr[0] == '\0')
1059 efidir = d;
1060 else
1061 free (d);
1062 free (dr);
1063 }
1064 if (!efidir)
1065 grub_util_error ("%s", _("cannot find EFI directory"));
1066 efidir_device_names = grub_guess_root_devices (efidir);
1067 if (!efidir_device_names || !efidir_device_names[0])
1068 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1069 efidir);
1070 install_device = efidir_device_names[0];
1071
1072 for (curdev = efidir_device_names; *curdev; curdev++)
1073 grub_util_pull_device (*curdev);
1074
1075 efidir_grub_devname = grub_util_get_grub_dev (efidir_device_names[0]);
1076 if (!efidir_grub_devname)
1077 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1078 efidir_device_names[0]);
1079
1080 efidir_grub_dev = grub_device_open (efidir_grub_devname);
1081 if (! efidir_grub_dev)
1082 grub_util_error ("%s", grub_errmsg);
1083
1084 fs = grub_fs_probe (efidir_grub_dev);
1085 if (! fs)
1086 grub_util_error ("%s", grub_errmsg);
1087
1088 efidir_is_mac = 0;
1089
1090 if (grub_strcmp (fs->name, "hfs") == 0
1091 || grub_strcmp (fs->name, "hfsplus") == 0)
1092 efidir_is_mac = 1;
1093
1094 if (!efidir_is_mac && grub_strcmp (fs->name, "fat") != 0)
1095 grub_util_error (_("%s doesn't look like an EFI partition"), efidir);
1096
1097 /* The EFI specification requires that an EFI System Partition must
1098 contain an "EFI" subdirectory, and that OS loaders are stored in
1099 subdirectories below EFI. Vendors are expected to pick names that do
1100 not collide with other vendors. To minimise collisions, we use the
1101 name of our distributor if possible.
1102 */
1103 char *t;
1104 efi_distributor = bootloader_id;
1105 if (removable)
1106 {
1107 /* The specification makes stricter requirements of removable
1108 devices, in order that only one image can be automatically loaded
1109 from them. The image must always reside under /EFI/BOOT, and it
1110 must have a specific file name depending on the architecture.
1111 */
1112 efi_distributor = "BOOT";
1113 switch (platform)
1114 {
1115 case GRUB_INSTALL_PLATFORM_I386_EFI:
1116 efi_file = "BOOTIA32.EFI";
1117 break;
1118 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1119 efi_file = "BOOTX64.EFI";
1120 break;
1121 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1122 efi_file = "BOOTIA64.EFI";
1123 break;
1124 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1125 efi_file = "BOOTARM.EFI";
1126 break;
1127 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1128 efi_file = "BOOTAA64.EFI";
1129 break;
1130 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1131 efi_file = "BOOTMIPS64EL.EFI";
1132 break;
1133 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1134 efi_file = "BOOTRISCV32.EFI";
1135 break;
1136 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1137 efi_file = "BOOTRISCV64.EFI";
1138 break;
1139 default:
1140 grub_util_error ("%s", _("You've found a bug"));
1141 break;
1142 }
1143 }
1144 else
1145 {
1146 /* It is convenient for each architecture to have a different
1147 efi_file, so that different versions can be installed in parallel.
1148 */
1149 switch (platform)
1150 {
1151 case GRUB_INSTALL_PLATFORM_I386_EFI:
1152 efi_file = "grubia32.efi";
1153 break;
1154 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1155 efi_file = "grubx64.efi";
1156 break;
1157 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1158 efi_file = "grubia64.efi";
1159 break;
1160 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1161 efi_file = "grubarm.efi";
1162 break;
1163 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1164 efi_file = "grubaa64.efi";
1165 break;
1166 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1167 efi_file = "grubmips64el.efi";
1168 break;
1169 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1170 efi_file = "grubriscv32.efi";
1171 break;
1172 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1173 efi_file = "grubriscv64.efi";
1174 break;
1175 default:
1176 efi_file = "grub.efi";
1177 break;
1178 }
1179 }
1180 t = grub_util_path_concat (3, efidir, "EFI", efi_distributor);
1181 free (efidir);
1182 efidir = t;
1183 grub_install_mkdir_p (efidir);
1184 }
1185
1186 if (platform == GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
1187 {
1188 int is_guess = 0;
1189 if (!macppcdir)
1190 {
1191 char *d;
1192
1193 is_guess = 1;
1194 d = grub_util_path_concat (2, bootdir, "macppc");
1195 if (!grub_util_is_directory (d))
1196 {
1197 free (d);
1198 d = grub_util_path_concat (2, bootdir, "efi");
1199 }
1200 /* Find the Mac HFS(+) System Partition. */
1201 if (!grub_util_is_directory (d))
1202 {
1203 free (d);
1204 d = grub_util_path_concat (2, bootdir, "EFI");
1205 }
1206 if (!grub_util_is_directory (d))
1207 {
1208 free (d);
1209 d = 0;
1210 }
1211 if (d)
1212 macppcdir = d;
1213 }
1214 if (macppcdir)
1215 {
1216 char **macppcdir_device_names = NULL;
1217 grub_device_t macppcdir_grub_dev = NULL;
1218 char *macppcdir_grub_devname;
1219 grub_fs_t fs;
1220
1221 macppcdir_device_names = grub_guess_root_devices (macppcdir);
1222 if (!macppcdir_device_names || !macppcdir_device_names[0])
1223 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1224 macppcdir);
1225
1226 for (curdev = macppcdir_device_names; *curdev; curdev++)
1227 grub_util_pull_device (*curdev);
1228
1229 macppcdir_grub_devname = grub_util_get_grub_dev (macppcdir_device_names[0]);
1230 if (!macppcdir_grub_devname)
1231 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1232 macppcdir_device_names[0]);
1233
1234 macppcdir_grub_dev = grub_device_open (macppcdir_grub_devname);
1235 if (! macppcdir_grub_dev)
1236 grub_util_error ("%s", grub_errmsg);
1237
1238 fs = grub_fs_probe (macppcdir_grub_dev);
1239 if (! fs)
1240 grub_util_error ("%s", grub_errmsg);
1241
1242 if (grub_strcmp (fs->name, "hfs") != 0
1243 && grub_strcmp (fs->name, "hfsplus") != 0
1244 && !is_guess)
1245 grub_util_error (_("filesystem on %s is neither HFS nor HFS+"),
1246 macppcdir);
1247 if (grub_strcmp (fs->name, "hfs") == 0
1248 || grub_strcmp (fs->name, "hfsplus") == 0)
1249 {
1250 install_device = macppcdir_device_names[0];
1251 is_prep = 0;
1252 }
1253 }
1254 }
1255
1256 grub_install_copy_files (grub_install_source_directory,
1257 grubdir, platform);
1258
1259 char *envfile = grub_util_path_concat (2, grubdir, "grubenv");
1260 if (!grub_util_is_regular (envfile))
1261 grub_util_create_envblk_file (envfile);
1262
1263 size_t ndev = 0;
1264
1265 /* Write device to a variable so we don't have to traverse /dev every time. */
1266 grub_devices = grub_guess_root_devices (grubdir);
1267 if (!grub_devices || !grub_devices[0])
1268 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1269 grubdir);
1270
1271 for (curdev = grub_devices; *curdev; curdev++)
1272 {
1273 grub_util_pull_device (*curdev);
1274 ndev++;
1275 }
1276
1277 grub_drives = xmalloc (sizeof (grub_drives[0]) * (ndev + 1));
1278
1279 for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
1280 curdrive++)
1281 {
1282 *curdrive = grub_util_get_grub_dev (*curdev);
1283 if (! *curdrive)
1284 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1285 *curdev);
1286 }
1287 *curdrive = 0;
1288
1289 grub_dev = grub_device_open (grub_drives[0]);
1290 if (! grub_dev)
1291 grub_util_error ("%s", grub_errmsg);
1292
1293 grub_fs = grub_fs_probe (grub_dev);
1294 if (! grub_fs)
1295 grub_util_error ("%s", grub_errmsg);
1296
1297 grub_install_push_module (grub_fs->name);
1298
1299 if (grub_dev->disk)
1300 probe_mods (grub_dev->disk);
1301
1302 for (curdrive = grub_drives + 1; *curdrive; curdrive++)
1303 {
1304 grub_device_t dev = grub_device_open (*curdrive);
1305 if (!dev)
1306 continue;
1307 if (dev->disk)
1308 probe_mods (dev->disk);
1309 grub_device_close (dev);
1310 }
1311
1312 if (!config.is_cryptodisk_enabled && have_cryptodisk)
1313 grub_util_error (_("attempt to install to encrypted disk without cryptodisk enabled. "
1314 "Set `%s' in file `%s'"), "GRUB_ENABLE_CRYPTODISK=y",
1315 grub_util_get_config_filename ());
1316
1317 if (disk_module && grub_strcmp (disk_module, "ata") == 0)
1318 grub_install_push_module ("pata");
1319 else if (disk_module && grub_strcmp (disk_module, "native") == 0)
1320 {
1321 grub_install_push_module ("pata");
1322 grub_install_push_module ("ahci");
1323 grub_install_push_module ("ohci");
1324 grub_install_push_module ("uhci");
1325 grub_install_push_module ("ehci");
1326 grub_install_push_module ("usbms");
1327 }
1328 else if (disk_module && disk_module[0])
1329 grub_install_push_module (disk_module);
1330
1331 relative_grubdir = grub_make_system_path_relative_to_its_root (grubdir);
1332 if (relative_grubdir[0] == '\0')
1333 {
1334 free (relative_grubdir);
1335 relative_grubdir = xstrdup ("/");
1336 }
1337
1338 char *platname = grub_install_get_platform_name (platform);
1339 char *platdir;
1340 {
1341 char *t = grub_util_path_concat (2, grubdir,
1342 platname);
1343 platdir = grub_canonicalize_file_name (t);
1344 if (!platdir)
1345 grub_util_error (_("failed to get canonical path of `%s'"),
1346 t);
1347 free (t);
1348 }
1349 load_cfg = grub_util_path_concat (2, platdir,
1350 "load.cfg");
1351
1352 grub_util_unlink (load_cfg);
1353
1354 if (debug_image && debug_image[0])
1355 {
1356 load_cfg_f = grub_util_fopen (load_cfg, "wb");
1357 have_load_cfg = 1;
1358 fprintf (load_cfg_f, "set debug='%s'\n",
1359 debug_image);
1360 }
1361 char *prefix_drive = NULL;
1362 char *install_drive = NULL;
1363
1364 if (install_device)
1365 {
1366 if (install_device[0] == '('
1367 && install_device[grub_strlen (install_device) - 1] == ')')
1368 {
1369 size_t len = grub_strlen (install_device) - 2;
1370 install_drive = xmalloc (len + 1);
1371 memcpy (install_drive, install_device + 1, len);
1372 install_drive[len] = '\0';
1373 }
1374 else
1375 {
1376 grub_util_pull_device (install_device);
1377 install_drive = grub_util_get_grub_dev (install_device);
1378 if (!install_drive)
1379 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1380 install_device);
1381 }
1382 }
1383
1384 if (!have_abstractions)
1385 {
1386 if ((disk_module && grub_strcmp (disk_module, "biosdisk") != 0)
1387 || grub_drives[1]
1388 || (!install_drive
1389 && platform != GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
1390 || (install_drive && !is_same_disk (grub_drives[0], install_drive))
1391 || !have_bootdev (platform))
1392 {
1393 char *uuid = NULL;
1394 /* generic method (used on coreboot and ata mod). */
1395 if (!force_file_id
1396 && grub_fs->fs_uuid && grub_fs->fs_uuid (grub_dev, &uuid))
1397 {
1398 grub_print_error ();
1399 grub_errno = 0;
1400 uuid = NULL;
1401 }
1402
1403 if (!load_cfg_f)
1404 load_cfg_f = grub_util_fopen (load_cfg, "wb");
1405 have_load_cfg = 1;
1406 if (uuid)
1407 {
1408 fprintf (load_cfg_f, "search.fs_uuid %s root ",
1409 uuid);
1410 grub_install_push_module ("search_fs_uuid");
1411 }
1412 else
1413 {
1414 char *rndstr = get_rndstr ();
1415 char *fl = grub_util_path_concat (3, grubdir,
1416 "uuid", rndstr);
1417 char *fldir = grub_util_path_concat (2, grubdir,
1418 "uuid");
1419 char *relfl;
1420 FILE *flf;
1421 grub_install_mkdir_p (fldir);
1422 flf = grub_util_fopen (fl, "w");
1423 if (!flf)
1424 grub_util_error (_("Can't create file: %s"), strerror (errno));
1425 fclose (flf);
1426 relfl = grub_make_system_path_relative_to_its_root (fl);
1427 fprintf (load_cfg_f, "search.file %s root ",
1428 relfl);
1429 grub_install_push_module ("search_fs_file");
1430 }
1431 for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
1432 curdrive++)
1433 {
1434 const char *map;
1435 char *g = NULL;
1436 grub_device_t dev;
1437 if (curdrive == grub_drives)
1438 dev = grub_dev;
1439 else
1440 dev = grub_device_open (*curdrive);
1441 if (!dev)
1442 continue;
1443
1444 if (dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID)
1445 {
1446 grub_util_fprint_full_disk_name (load_cfg_f,
1447 dev->disk->name,
1448 dev);
1449 fprintf (load_cfg_f, " ");
1450 if (dev != grub_dev)
1451 grub_device_close (dev);
1452 continue;
1453 }
1454
1455 map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
1456
1457 if (map)
1458 {
1459 grub_util_fprint_full_disk_name (load_cfg_f, map, dev);
1460 fprintf (load_cfg_f, " ");
1461 }
1462
1463
1464 if (disk_module && disk_module[0]
1465 && grub_strcmp (disk_module, "biosdisk") != 0)
1466 g = grub_util_guess_baremetal_drive (*curdev);
1467 else
1468 switch (platform)
1469 {
1470 case GRUB_INSTALL_PLATFORM_I386_PC:
1471 g = grub_util_guess_bios_drive (*curdev);
1472 break;
1473 case GRUB_INSTALL_PLATFORM_I386_EFI:
1474 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1475 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1476 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1477 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1478 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1479 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1480 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1481 g = grub_util_guess_efi_drive (*curdev);
1482 break;
1483 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1484 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1485 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1486 {
1487 const char * ofpath = grub_util_devname_to_ofpath (*curdev);
1488 g = xasprintf ("ieee1275/%s", ofpath);
1489 break;
1490 }
1491 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1492 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1493 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1494 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1495 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1496 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1497 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1498 g = grub_util_guess_baremetal_drive (*curdev);
1499 break;
1500 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1501 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1502 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1503 case GRUB_INSTALL_PLATFORM_I386_XEN:
1504 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1505 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1506 grub_util_warn ("%s", _("no hints available for your platform. Expect reduced performance"));
1507 break;
1508 /* pacify warning. */
1509 case GRUB_INSTALL_PLATFORM_MAX:
1510 break;
1511 }
1512 if (g)
1513 {
1514 grub_util_fprint_full_disk_name (load_cfg_f, g, dev);
1515 fprintf (load_cfg_f, " ");
1516 free (g);
1517 }
1518 if (dev != grub_dev)
1519 grub_device_close (dev);
1520 }
1521 fprintf (load_cfg_f, "\n");
1522 char *escaped_relpath = escape (relative_grubdir);
1523 fprintf (load_cfg_f, "set prefix=($root)'%s'\n",
1524 escaped_relpath);
1525 }
1526 else
1527 {
1528 /* We need to hardcode the partition number in the core image's prefix. */
1529 char *p;
1530 for (p = grub_drives[0]; *p; )
1531 {
1532 if (*p == '\\' && p[1])
1533 {
1534 p += 2;
1535 continue;
1536 }
1537 if (*p == ',' || *p == '\0')
1538 break;
1539 p++;
1540 }
1541 prefix_drive = xasprintf ("(%s)", p);
1542 }
1543 }
1544 else
1545 {
1546 if (config.is_cryptodisk_enabled)
1547 {
1548 if (grub_dev->disk)
1549 probe_cryptodisk_uuid (grub_dev->disk);
1550
1551 for (curdrive = grub_drives + 1; *curdrive; curdrive++)
1552 {
1553 grub_device_t dev = grub_device_open (*curdrive);
1554 if (!dev)
1555 continue;
1556 if (dev->disk)
1557 probe_cryptodisk_uuid (dev->disk);
1558 grub_device_close (dev);
1559 }
1560 }
1561 prefix_drive = xasprintf ("(%s)", grub_drives[0]);
1562 }
1563
1564 char mkimage_target[200];
1565 const char *core_name = NULL;
1566
1567 switch (platform)
1568 {
1569 case GRUB_INSTALL_PLATFORM_I386_EFI:
1570 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1571 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1572 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1573 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1574 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1575 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1576 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1577 core_name = "core.efi";
1578 snprintf (mkimage_target, sizeof (mkimage_target),
1579 "%s-%s",
1580 grub_install_get_platform_cpu (platform),
1581 grub_install_get_platform_platform (platform));
1582 break;
1583 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1584 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1585 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1586 core_name = "core.elf";
1587 snprintf (mkimage_target, sizeof (mkimage_target),
1588 "%s-%s-elf",
1589 grub_install_get_platform_cpu (platform),
1590 grub_install_get_platform_platform (platform));
1591 break;
1592
1593 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1594 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1595 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1596 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1597 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1598 case GRUB_INSTALL_PLATFORM_I386_XEN:
1599 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1600 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1601 core_name = "core.elf";
1602 snprintf (mkimage_target, sizeof (mkimage_target),
1603 "%s-%s",
1604 grub_install_get_platform_cpu (platform),
1605 grub_install_get_platform_platform (platform));
1606 break;
1607
1608
1609 case GRUB_INSTALL_PLATFORM_I386_PC:
1610 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1611 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1612 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1613 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1614 snprintf (mkimage_target, sizeof (mkimage_target),
1615 "%s-%s",
1616 grub_install_get_platform_cpu (platform),
1617 grub_install_get_platform_platform (platform));
1618 core_name = "core.img";
1619 break;
1620 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1621 strcpy (mkimage_target, "sparc64-ieee1275-raw");
1622 core_name = "core.img";
1623 break;
1624 /* pacify warning. */
1625 case GRUB_INSTALL_PLATFORM_MAX:
1626 break;
1627 }
1628
1629 if (!core_name)
1630 grub_util_error ("%s", _("You've found a bug"));
1631
1632 if (load_cfg_f)
1633 fclose (load_cfg_f);
1634
1635 char *imgfile = grub_util_path_concat (2, platdir,
1636 core_name);
1637 char *prefix = xasprintf ("%s%s", prefix_drive ? : "",
1638 relative_grubdir);
1639 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
1640 /*prefix */ prefix,
1641 /* output */ imgfile,
1642 /* memdisk */ NULL,
1643 have_load_cfg ? load_cfg : NULL,
1644 /* image target */ mkimage_target, 0);
1645 /* Backward-compatibility kludges. */
1646 switch (platform)
1647 {
1648 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1649 {
1650 char *dst = grub_util_path_concat (2, bootdir, "grub.elf");
1651 grub_install_copy_file (imgfile, dst, 1);
1652 free (dst);
1653 }
1654 break;
1655
1656 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1657 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1658 {
1659 char *dst = grub_util_path_concat (2, grubdir, "grub");
1660 grub_install_copy_file (imgfile, dst, 1);
1661 free (dst);
1662 }
1663 break;
1664
1665 case GRUB_INSTALL_PLATFORM_I386_EFI:
1666 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1667 {
1668 char *dst = grub_util_path_concat (2, platdir, "grub.efi");
1669 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
1670 /* prefix */ "",
1671 /* output */ dst,
1672 /* memdisk */ NULL,
1673 have_load_cfg ? load_cfg : NULL,
1674 /* image target */ mkimage_target, 0);
1675 }
1676 break;
1677 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1678 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1679 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1680 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1681 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1682 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1683 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1684 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1685 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1686 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1687 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1688 case GRUB_INSTALL_PLATFORM_I386_PC:
1689 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1690 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1691 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1692 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1693 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1694 case GRUB_INSTALL_PLATFORM_I386_XEN:
1695 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1696 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1697 break;
1698 /* pacify warning. */
1699 case GRUB_INSTALL_PLATFORM_MAX:
1700 break;
1701 }
1702
1703 /* Perform the platform-dependent install */
1704
1705 switch (platform)
1706 {
1707 case GRUB_INSTALL_PLATFORM_I386_PC:
1708 {
1709 char *boot_img_src = grub_util_path_concat (2,
1710 grub_install_source_directory,
1711 "boot.img");
1712 char *boot_img = grub_util_path_concat (2, platdir,
1713 "boot.img");
1714 grub_install_copy_file (boot_img_src, boot_img, 1);
1715
1716 grub_util_info ("%sgrub-bios-setup %s %s %s %s %s --directory='%s' --device-map='%s' '%s'",
1717 /* TRANSLATORS: This is a prefix in the log to indicate that usually
1718 a command would be executed but due to an option was skipped. */
1719 install_bootsector ? "" : _("NOT RUNNING: "),
1720 allow_floppy ? "--allow-floppy " : "",
1721 verbosity ? "--verbose " : "",
1722 force ? "--force " : "",
1723 !fs_probe ? "--skip-fs-probe" : "",
1724 !add_rs_codes ? "--no-rs-codes" : "",
1725 platdir,
1726 device_map,
1727 install_device);
1728
1729 /* Now perform the installation. */
1730 if (install_bootsector)
1731 grub_util_bios_setup (platdir, "boot.img", "core.img",
1732 install_drive, force,
1733 fs_probe, allow_floppy, add_rs_codes);
1734 break;
1735 }
1736 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1737 {
1738 char *boot_img_src = grub_util_path_concat (2,
1739 grub_install_source_directory,
1740 "boot.img");
1741 char *boot_img = grub_util_path_concat (2, platdir,
1742 "boot.img");
1743 grub_install_copy_file (boot_img_src, boot_img, 1);
1744
1745 grub_util_info ("%sgrub-sparc64-setup %s %s %s %s --directory='%s' --device-map='%s' '%s'",
1746 install_bootsector ? "" : "NOT RUNNING: ",
1747 allow_floppy ? "--allow-floppy " : "",
1748 verbosity ? "--verbose " : "",
1749 force ? "--force " : "",
1750 !fs_probe ? "--skip-fs-probe" : "",
1751 platdir,
1752 device_map,
1753 install_drive);
1754
1755 /* Now perform the installation. */
1756 if (install_bootsector)
1757 grub_util_sparc_setup (platdir, "boot.img", "core.img",
1758 install_drive, force,
1759 fs_probe, allow_floppy,
1760 0 /* unused */ );
1761 break;
1762 }
1763
1764 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1765 if (macppcdir)
1766 {
1767 char *core_services = grub_util_path_concat (4, macppcdir,
1768 "System", "Library",
1769 "CoreServices");
1770 char *mach_kernel = grub_util_path_concat (2, macppcdir,
1771 "mach_kernel");
1772 char *grub_elf, *bootx;
1773 FILE *f;
1774 grub_device_t ins_dev;
1775 char *grub_chrp = grub_util_path_concat (2,
1776 grub_install_source_directory,
1777 "grub.chrp");
1778
1779 grub_install_mkdir_p (core_services);
1780
1781 bootx = grub_util_path_concat (2, core_services, "BootX");
1782 grub_install_copy_file (grub_chrp, bootx, 1);
1783
1784 grub_elf = grub_util_path_concat (2, core_services, "grub.elf");
1785 grub_install_copy_file (imgfile, grub_elf, 1);
1786
1787 f = grub_util_fopen (mach_kernel, "a+");
1788 if (!f)
1789 grub_util_error (_("Can't create file: %s"), strerror (errno));
1790 fclose (f);
1791
1792 fill_core_services (core_services);
1793
1794 ins_dev = grub_device_open (install_drive);
1795
1796 bless (ins_dev, core_services, 0);
1797
1798 if (update_nvram)
1799 {
1800 const char *dev;
1801 int partno;
1802
1803 partno = ins_dev->disk->partition
1804 ? ins_dev->disk->partition->number + 1 : 0;
1805 dev = grub_util_get_os_disk (install_device);
1806 grub_install_register_ieee1275 (0, dev, partno,
1807 "\\\\BootX");
1808 }
1809 grub_device_close (ins_dev);
1810 free (grub_elf);
1811 free (bootx);
1812 free (mach_kernel);
1813 free (grub_chrp);
1814 break;
1815 }
1816 /* If a install device is defined, copy the core.elf to PReP partition. */
1817 if (is_prep && install_device && install_device[0])
1818 {
1819 grub_device_t ins_dev;
1820 ins_dev = grub_device_open (install_drive);
1821 if (!ins_dev || !is_prep_partition (ins_dev))
1822 {
1823 grub_util_error ("%s", _("the chosen partition is not a PReP partition"));
1824 }
1825 if (is_prep_empty (ins_dev))
1826 {
1827 if (write_to_disk (ins_dev, imgfile))
1828 grub_util_error ("%s", _("failed to copy Grub to the PReP partition"));
1829 }
1830 else
1831 {
1832 char *s = xasprintf ("dd if=/dev/zero of=%s", install_device);
1833 grub_util_error (_("the PReP partition is not empty. If you are sure you want to use it, run dd to clear it: `%s'"),
1834 s);
1835 }
1836 grub_device_close (ins_dev);
1837 if (update_nvram)
1838 grub_install_register_ieee1275 (1, grub_util_get_os_disk (install_device),
1839 0, NULL);
1840 break;
1841 }
1842 /* fallthrough. */
1843 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1844 if (update_nvram)
1845 {
1846 const char *dev;
1847 char *relpath;
1848 int partno;
1849 relpath = grub_make_system_path_relative_to_its_root (imgfile);
1850 partno = grub_dev->disk->partition
1851 ? grub_dev->disk->partition->number + 1 : 0;
1852 dev = grub_util_get_os_disk (grub_devices[0]);
1853 grub_install_register_ieee1275 (0, dev,
1854 partno, relpath);
1855 }
1856 break;
1857 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1858 grub_install_sgi_setup (install_device, imgfile, "grub");
1859 break;
1860
1861 case GRUB_INSTALL_PLATFORM_I386_EFI:
1862 if (!efidir_is_mac)
1863 {
1864 char *dst = grub_util_path_concat (2, efidir, "grub.efi");
1865 /* For old macs. Suggested by Peter Jones. */
1866 grub_install_copy_file (imgfile, dst, 1);
1867 free (dst);
1868 }
1869 /* Fallthrough. */
1870 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1871 if (efidir_is_mac)
1872 {
1873 char *boot_efi;
1874 char *core_services = grub_util_path_concat (4, efidir,
1875 "System", "Library",
1876 "CoreServices");
1877 char *mach_kernel = grub_util_path_concat (2, efidir,
1878 "mach_kernel");
1879 FILE *f;
1880 grub_device_t ins_dev;
1881
1882 grub_install_mkdir_p (core_services);
1883
1884 boot_efi = grub_util_path_concat (2, core_services, "boot.efi");
1885 grub_install_copy_file (imgfile, boot_efi, 1);
1886
1887 f = grub_util_fopen (mach_kernel, "r+");
1888 if (!f)
1889 grub_util_error (_("Can't create file: %s"), strerror (errno));
1890 fclose (f);
1891
1892 fill_core_services(core_services);
1893
1894 ins_dev = grub_device_open (install_drive);
1895
1896 bless (ins_dev, boot_efi, 1);
1897 if (!removable && update_nvram)
1898 {
1899 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1900 int ret;
1901 ret = grub_install_register_efi (efidir_grub_dev,
1902 "\\System\\Library\\CoreServices",
1903 efi_distributor);
1904 if (ret)
1905 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
1906 strerror (ret));
1907 }
1908
1909 grub_device_close (ins_dev);
1910 free (boot_efi);
1911 free (mach_kernel);
1912 break;
1913 }
1914 /* FALLTHROUGH */
1915 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1916 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1917 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1918 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1919 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1920 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1921 {
1922 char *dst = grub_util_path_concat (2, efidir, efi_file);
1923 grub_install_copy_file (imgfile, dst, 1);
1924 free (dst);
1925 }
1926 if (!removable && update_nvram)
1927 {
1928 char * efifile_path;
1929 char * part;
1930 int ret;
1931
1932 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1933 if (!efi_distributor || efi_distributor[0] == '\0')
1934 grub_util_error ("%s", _("EFI bootloader id isn't specified."));
1935 efifile_path = xasprintf ("\\EFI\\%s\\%s",
1936 efi_distributor,
1937 efi_file);
1938 part = (efidir_grub_dev->disk->partition
1939 ? grub_partition_get_name (efidir_grub_dev->disk->partition)
1940 : 0);
1941 grub_util_info ("Registering with EFI: distributor = `%s',"
1942 " path = `%s', ESP at %s%s%s",
1943 efi_distributor, efifile_path,
1944 efidir_grub_dev->disk->name,
1945 (part ? ",": ""), (part ? : ""));
1946 grub_free (part);
1947 ret = grub_install_register_efi (efidir_grub_dev,
1948 efifile_path, efi_distributor);
1949 if (ret)
1950 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
1951 strerror (ret));
1952 }
1953 break;
1954
1955 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1956 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1957 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1958 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1959 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1960 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1961 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1962 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1963 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1964 case GRUB_INSTALL_PLATFORM_I386_XEN:
1965 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1966 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1967 grub_util_warn ("%s",
1968 _("WARNING: no platform-specific install was performed"));
1969 break;
1970 /* pacify warning. */
1971 case GRUB_INSTALL_PLATFORM_MAX:
1972 break;
1973 }
1974
1975 fprintf (stderr, "%s\n", _("Installation finished. No error reported."));
1976
1977 /* Free resources. */
1978 grub_gcry_fini_all ();
1979 grub_fini_all ();
1980
1981 return 0;
1982 }