]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/util/grub-install.c
keep up with 1.0.67 (#1464)
[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 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1169 efi_file = "grubriscv32.efi";
1170 break;
1171 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1172 efi_file = "grubriscv64.efi";
1173 break;
1174 default:
1175 efi_file = "grub.efi";
1176 break;
1177 }
1178 }
1179 t = grub_util_path_concat (3, efidir, "EFI", efi_distributor);
1180 free (efidir);
1181 efidir = t;
1182 grub_install_mkdir_p (efidir);
1183 }
1184
1185 if (platform == GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
1186 {
1187 int is_guess = 0;
1188 if (!macppcdir)
1189 {
1190 char *d;
1191
1192 is_guess = 1;
1193 d = grub_util_path_concat (2, bootdir, "macppc");
1194 if (!grub_util_is_directory (d))
1195 {
1196 free (d);
1197 d = grub_util_path_concat (2, bootdir, "efi");
1198 }
1199 /* Find the Mac HFS(+) System Partition. */
1200 if (!grub_util_is_directory (d))
1201 {
1202 free (d);
1203 d = grub_util_path_concat (2, bootdir, "EFI");
1204 }
1205 if (!grub_util_is_directory (d))
1206 {
1207 free (d);
1208 d = 0;
1209 }
1210 if (d)
1211 macppcdir = d;
1212 }
1213 if (macppcdir)
1214 {
1215 char **macppcdir_device_names = NULL;
1216 grub_device_t macppcdir_grub_dev = NULL;
1217 char *macppcdir_grub_devname;
1218 grub_fs_t fs;
1219
1220 macppcdir_device_names = grub_guess_root_devices (macppcdir);
1221 if (!macppcdir_device_names || !macppcdir_device_names[0])
1222 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1223 macppcdir);
1224
1225 for (curdev = macppcdir_device_names; *curdev; curdev++)
1226 grub_util_pull_device (*curdev);
1227
1228 macppcdir_grub_devname = grub_util_get_grub_dev (macppcdir_device_names[0]);
1229 if (!macppcdir_grub_devname)
1230 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1231 macppcdir_device_names[0]);
1232
1233 macppcdir_grub_dev = grub_device_open (macppcdir_grub_devname);
1234 if (! macppcdir_grub_dev)
1235 grub_util_error ("%s", grub_errmsg);
1236
1237 fs = grub_fs_probe (macppcdir_grub_dev);
1238 if (! fs)
1239 grub_util_error ("%s", grub_errmsg);
1240
1241 if (grub_strcmp (fs->name, "hfs") != 0
1242 && grub_strcmp (fs->name, "hfsplus") != 0
1243 && !is_guess)
1244 grub_util_error (_("filesystem on %s is neither HFS nor HFS+"),
1245 macppcdir);
1246 if (grub_strcmp (fs->name, "hfs") == 0
1247 || grub_strcmp (fs->name, "hfsplus") == 0)
1248 {
1249 install_device = macppcdir_device_names[0];
1250 is_prep = 0;
1251 }
1252 }
1253 }
1254
1255 grub_install_copy_files (grub_install_source_directory,
1256 grubdir, platform);
1257
1258 char *envfile = grub_util_path_concat (2, grubdir, "grubenv");
1259 if (!grub_util_is_regular (envfile))
1260 grub_util_create_envblk_file (envfile);
1261
1262 size_t ndev = 0;
1263
1264 /* Write device to a variable so we don't have to traverse /dev every time. */
1265 grub_devices = grub_guess_root_devices (grubdir);
1266 if (!grub_devices || !grub_devices[0])
1267 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1268 grubdir);
1269
1270 for (curdev = grub_devices; *curdev; curdev++)
1271 {
1272 grub_util_pull_device (*curdev);
1273 ndev++;
1274 }
1275
1276 grub_drives = xmalloc (sizeof (grub_drives[0]) * (ndev + 1));
1277
1278 for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
1279 curdrive++)
1280 {
1281 *curdrive = grub_util_get_grub_dev (*curdev);
1282 if (! *curdrive)
1283 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1284 *curdev);
1285 }
1286 *curdrive = 0;
1287
1288 grub_dev = grub_device_open (grub_drives[0]);
1289 if (! grub_dev)
1290 grub_util_error ("%s", grub_errmsg);
1291
1292 grub_fs = grub_fs_probe (grub_dev);
1293 if (! grub_fs)
1294 grub_util_error ("%s", grub_errmsg);
1295
1296 grub_install_push_module (grub_fs->name);
1297
1298 if (grub_dev->disk)
1299 probe_mods (grub_dev->disk);
1300
1301 for (curdrive = grub_drives + 1; *curdrive; curdrive++)
1302 {
1303 grub_device_t dev = grub_device_open (*curdrive);
1304 if (!dev)
1305 continue;
1306 if (dev->disk)
1307 probe_mods (dev->disk);
1308 grub_device_close (dev);
1309 }
1310
1311 if (!config.is_cryptodisk_enabled && have_cryptodisk)
1312 grub_util_error (_("attempt to install to encrypted disk without cryptodisk enabled. "
1313 "Set `%s' in file `%s'"), "GRUB_ENABLE_CRYPTODISK=y",
1314 grub_util_get_config_filename ());
1315
1316 if (disk_module && grub_strcmp (disk_module, "ata") == 0)
1317 grub_install_push_module ("pata");
1318 else if (disk_module && grub_strcmp (disk_module, "native") == 0)
1319 {
1320 grub_install_push_module ("pata");
1321 grub_install_push_module ("ahci");
1322 grub_install_push_module ("ohci");
1323 grub_install_push_module ("uhci");
1324 grub_install_push_module ("ehci");
1325 grub_install_push_module ("usbms");
1326 }
1327 else if (disk_module && disk_module[0])
1328 grub_install_push_module (disk_module);
1329
1330 relative_grubdir = grub_make_system_path_relative_to_its_root (grubdir);
1331 if (relative_grubdir[0] == '\0')
1332 {
1333 free (relative_grubdir);
1334 relative_grubdir = xstrdup ("/");
1335 }
1336
1337 char *platname = grub_install_get_platform_name (platform);
1338 char *platdir;
1339 {
1340 char *t = grub_util_path_concat (2, grubdir,
1341 platname);
1342 platdir = grub_canonicalize_file_name (t);
1343 if (!platdir)
1344 grub_util_error (_("failed to get canonical path of `%s'"),
1345 t);
1346 free (t);
1347 }
1348 load_cfg = grub_util_path_concat (2, platdir,
1349 "load.cfg");
1350
1351 grub_util_unlink (load_cfg);
1352
1353 if (debug_image && debug_image[0])
1354 {
1355 load_cfg_f = grub_util_fopen (load_cfg, "wb");
1356 have_load_cfg = 1;
1357 fprintf (load_cfg_f, "set debug='%s'\n",
1358 debug_image);
1359 }
1360 char *prefix_drive = NULL;
1361 char *install_drive = NULL;
1362
1363 if (install_device)
1364 {
1365 if (install_device[0] == '('
1366 && install_device[grub_strlen (install_device) - 1] == ')')
1367 {
1368 size_t len = grub_strlen (install_device) - 2;
1369 install_drive = xmalloc (len + 1);
1370 memcpy (install_drive, install_device + 1, len);
1371 install_drive[len] = '\0';
1372 }
1373 else
1374 {
1375 grub_util_pull_device (install_device);
1376 install_drive = grub_util_get_grub_dev (install_device);
1377 if (!install_drive)
1378 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1379 install_device);
1380 }
1381 }
1382
1383 if (!have_abstractions)
1384 {
1385 if ((disk_module && grub_strcmp (disk_module, "biosdisk") != 0)
1386 || grub_drives[1]
1387 || (!install_drive
1388 && platform != GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
1389 || (install_drive && !is_same_disk (grub_drives[0], install_drive))
1390 || !have_bootdev (platform))
1391 {
1392 char *uuid = NULL;
1393 /* generic method (used on coreboot and ata mod). */
1394 if (!force_file_id
1395 && grub_fs->fs_uuid && grub_fs->fs_uuid (grub_dev, &uuid))
1396 {
1397 grub_print_error ();
1398 grub_errno = 0;
1399 uuid = NULL;
1400 }
1401
1402 if (!load_cfg_f)
1403 load_cfg_f = grub_util_fopen (load_cfg, "wb");
1404 have_load_cfg = 1;
1405 if (uuid)
1406 {
1407 fprintf (load_cfg_f, "search.fs_uuid %s root ",
1408 uuid);
1409 grub_install_push_module ("search_fs_uuid");
1410 }
1411 else
1412 {
1413 char *rndstr = get_rndstr ();
1414 char *fl = grub_util_path_concat (3, grubdir,
1415 "uuid", rndstr);
1416 char *fldir = grub_util_path_concat (2, grubdir,
1417 "uuid");
1418 char *relfl;
1419 FILE *flf;
1420 grub_install_mkdir_p (fldir);
1421 flf = grub_util_fopen (fl, "w");
1422 if (!flf)
1423 grub_util_error (_("Can't create file: %s"), strerror (errno));
1424 fclose (flf);
1425 relfl = grub_make_system_path_relative_to_its_root (fl);
1426 fprintf (load_cfg_f, "search.file %s root ",
1427 relfl);
1428 grub_install_push_module ("search_fs_file");
1429 }
1430 for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
1431 curdrive++)
1432 {
1433 const char *map;
1434 char *g = NULL;
1435 grub_device_t dev;
1436 if (curdrive == grub_drives)
1437 dev = grub_dev;
1438 else
1439 dev = grub_device_open (*curdrive);
1440 if (!dev)
1441 continue;
1442
1443 if (dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID)
1444 {
1445 grub_util_fprint_full_disk_name (load_cfg_f,
1446 dev->disk->name,
1447 dev);
1448 fprintf (load_cfg_f, " ");
1449 if (dev != grub_dev)
1450 grub_device_close (dev);
1451 continue;
1452 }
1453
1454 map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
1455
1456 if (map)
1457 {
1458 grub_util_fprint_full_disk_name (load_cfg_f, map, dev);
1459 fprintf (load_cfg_f, " ");
1460 }
1461
1462
1463 if (disk_module && disk_module[0]
1464 && grub_strcmp (disk_module, "biosdisk") != 0)
1465 g = grub_util_guess_baremetal_drive (*curdev);
1466 else
1467 switch (platform)
1468 {
1469 case GRUB_INSTALL_PLATFORM_I386_PC:
1470 g = grub_util_guess_bios_drive (*curdev);
1471 break;
1472 case GRUB_INSTALL_PLATFORM_I386_EFI:
1473 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1474 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1475 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1476 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1477 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1478 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1479 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1480 g = grub_util_guess_efi_drive (*curdev);
1481 break;
1482 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1483 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1484 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1485 {
1486 const char * ofpath = grub_util_devname_to_ofpath (*curdev);
1487 g = xasprintf ("ieee1275/%s", ofpath);
1488 break;
1489 }
1490 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1491 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1492 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1493 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1494 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1495 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1496 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1497 g = grub_util_guess_baremetal_drive (*curdev);
1498 break;
1499 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1500 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1501 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1502 case GRUB_INSTALL_PLATFORM_I386_XEN:
1503 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1504 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1505 grub_util_warn ("%s", _("no hints available for your platform. Expect reduced performance"));
1506 break;
1507 /* pacify warning. */
1508 case GRUB_INSTALL_PLATFORM_MAX:
1509 break;
1510 }
1511 if (g)
1512 {
1513 grub_util_fprint_full_disk_name (load_cfg_f, g, dev);
1514 fprintf (load_cfg_f, " ");
1515 free (g);
1516 }
1517 if (dev != grub_dev)
1518 grub_device_close (dev);
1519 }
1520 fprintf (load_cfg_f, "\n");
1521 char *escaped_relpath = escape (relative_grubdir);
1522 fprintf (load_cfg_f, "set prefix=($root)'%s'\n",
1523 escaped_relpath);
1524 }
1525 else
1526 {
1527 /* We need to hardcode the partition number in the core image's prefix. */
1528 char *p;
1529 for (p = grub_drives[0]; *p; )
1530 {
1531 if (*p == '\\' && p[1])
1532 {
1533 p += 2;
1534 continue;
1535 }
1536 if (*p == ',' || *p == '\0')
1537 break;
1538 p++;
1539 }
1540 prefix_drive = xasprintf ("(%s)", p);
1541 }
1542 }
1543 else
1544 {
1545 if (config.is_cryptodisk_enabled)
1546 {
1547 if (grub_dev->disk)
1548 probe_cryptodisk_uuid (grub_dev->disk);
1549
1550 for (curdrive = grub_drives + 1; *curdrive; curdrive++)
1551 {
1552 grub_device_t dev = grub_device_open (*curdrive);
1553 if (!dev)
1554 continue;
1555 if (dev->disk)
1556 probe_cryptodisk_uuid (dev->disk);
1557 grub_device_close (dev);
1558 }
1559 }
1560 prefix_drive = xasprintf ("(%s)", grub_drives[0]);
1561 }
1562
1563 char mkimage_target[200];
1564 const char *core_name = NULL;
1565
1566 switch (platform)
1567 {
1568 case GRUB_INSTALL_PLATFORM_I386_EFI:
1569 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1570 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1571 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1572 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1573 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1574 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1575 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1576 core_name = "core.efi";
1577 snprintf (mkimage_target, sizeof (mkimage_target),
1578 "%s-%s",
1579 grub_install_get_platform_cpu (platform),
1580 grub_install_get_platform_platform (platform));
1581 break;
1582 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1583 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1584 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1585 core_name = "core.elf";
1586 snprintf (mkimage_target, sizeof (mkimage_target),
1587 "%s-%s-elf",
1588 grub_install_get_platform_cpu (platform),
1589 grub_install_get_platform_platform (platform));
1590 break;
1591
1592 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1593 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1594 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1595 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1596 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1597 case GRUB_INSTALL_PLATFORM_I386_XEN:
1598 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1599 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1600 core_name = "core.elf";
1601 snprintf (mkimage_target, sizeof (mkimage_target),
1602 "%s-%s",
1603 grub_install_get_platform_cpu (platform),
1604 grub_install_get_platform_platform (platform));
1605 break;
1606
1607
1608 case GRUB_INSTALL_PLATFORM_I386_PC:
1609 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1610 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1611 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1612 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1613 snprintf (mkimage_target, sizeof (mkimage_target),
1614 "%s-%s",
1615 grub_install_get_platform_cpu (platform),
1616 grub_install_get_platform_platform (platform));
1617 core_name = "core.img";
1618 break;
1619 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1620 strcpy (mkimage_target, "sparc64-ieee1275-raw");
1621 core_name = "core.img";
1622 break;
1623 /* pacify warning. */
1624 case GRUB_INSTALL_PLATFORM_MAX:
1625 break;
1626 }
1627
1628 if (!core_name)
1629 grub_util_error ("%s", _("You've found a bug"));
1630
1631 if (load_cfg_f)
1632 fclose (load_cfg_f);
1633
1634 char *imgfile = grub_util_path_concat (2, platdir,
1635 core_name);
1636 char *prefix = xasprintf ("%s%s", prefix_drive ? : "",
1637 relative_grubdir);
1638 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
1639 /*prefix */ prefix,
1640 /* output */ imgfile,
1641 /* memdisk */ NULL,
1642 have_load_cfg ? load_cfg : NULL,
1643 /* image target */ mkimage_target, 0);
1644 /* Backward-compatibility kludges. */
1645 switch (platform)
1646 {
1647 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1648 {
1649 char *dst = grub_util_path_concat (2, bootdir, "grub.elf");
1650 grub_install_copy_file (imgfile, dst, 1);
1651 free (dst);
1652 }
1653 break;
1654
1655 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1656 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1657 {
1658 char *dst = grub_util_path_concat (2, grubdir, "grub");
1659 grub_install_copy_file (imgfile, dst, 1);
1660 free (dst);
1661 }
1662 break;
1663
1664 case GRUB_INSTALL_PLATFORM_I386_EFI:
1665 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1666 {
1667 char *dst = grub_util_path_concat (2, platdir, "grub.efi");
1668 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
1669 /* prefix */ "",
1670 /* output */ dst,
1671 /* memdisk */ NULL,
1672 have_load_cfg ? load_cfg : NULL,
1673 /* image target */ mkimage_target, 0);
1674 }
1675 break;
1676 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1677 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1678 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1679 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1680 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1681 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1682 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1683 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1684 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1685 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1686 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1687 case GRUB_INSTALL_PLATFORM_I386_PC:
1688 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1689 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1690 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1691 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1692 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1693 case GRUB_INSTALL_PLATFORM_I386_XEN:
1694 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1695 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1696 break;
1697 /* pacify warning. */
1698 case GRUB_INSTALL_PLATFORM_MAX:
1699 break;
1700 }
1701
1702 /* Perform the platform-dependent install */
1703
1704 switch (platform)
1705 {
1706 case GRUB_INSTALL_PLATFORM_I386_PC:
1707 {
1708 char *boot_img_src = grub_util_path_concat (2,
1709 grub_install_source_directory,
1710 "boot.img");
1711 char *boot_img = grub_util_path_concat (2, platdir,
1712 "boot.img");
1713 grub_install_copy_file (boot_img_src, boot_img, 1);
1714
1715 grub_util_info ("%sgrub-bios-setup %s %s %s %s %s --directory='%s' --device-map='%s' '%s'",
1716 /* TRANSLATORS: This is a prefix in the log to indicate that usually
1717 a command would be executed but due to an option was skipped. */
1718 install_bootsector ? "" : _("NOT RUNNING: "),
1719 allow_floppy ? "--allow-floppy " : "",
1720 verbosity ? "--verbose " : "",
1721 force ? "--force " : "",
1722 !fs_probe ? "--skip-fs-probe" : "",
1723 !add_rs_codes ? "--no-rs-codes" : "",
1724 platdir,
1725 device_map,
1726 install_device);
1727
1728 /* Now perform the installation. */
1729 if (install_bootsector)
1730 grub_util_bios_setup (platdir, "boot.img", "core.img",
1731 install_drive, force,
1732 fs_probe, allow_floppy, add_rs_codes);
1733 break;
1734 }
1735 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1736 {
1737 char *boot_img_src = grub_util_path_concat (2,
1738 grub_install_source_directory,
1739 "boot.img");
1740 char *boot_img = grub_util_path_concat (2, platdir,
1741 "boot.img");
1742 grub_install_copy_file (boot_img_src, boot_img, 1);
1743
1744 grub_util_info ("%sgrub-sparc64-setup %s %s %s %s --directory='%s' --device-map='%s' '%s'",
1745 install_bootsector ? "" : "NOT RUNNING: ",
1746 allow_floppy ? "--allow-floppy " : "",
1747 verbosity ? "--verbose " : "",
1748 force ? "--force " : "",
1749 !fs_probe ? "--skip-fs-probe" : "",
1750 platdir,
1751 device_map,
1752 install_drive);
1753
1754 /* Now perform the installation. */
1755 if (install_bootsector)
1756 grub_util_sparc_setup (platdir, "boot.img", "core.img",
1757 install_drive, force,
1758 fs_probe, allow_floppy,
1759 0 /* unused */ );
1760 break;
1761 }
1762
1763 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1764 if (macppcdir)
1765 {
1766 char *core_services = grub_util_path_concat (4, macppcdir,
1767 "System", "Library",
1768 "CoreServices");
1769 char *mach_kernel = grub_util_path_concat (2, macppcdir,
1770 "mach_kernel");
1771 char *grub_elf, *bootx;
1772 FILE *f;
1773 grub_device_t ins_dev;
1774 char *grub_chrp = grub_util_path_concat (2,
1775 grub_install_source_directory,
1776 "grub.chrp");
1777
1778 grub_install_mkdir_p (core_services);
1779
1780 bootx = grub_util_path_concat (2, core_services, "BootX");
1781 grub_install_copy_file (grub_chrp, bootx, 1);
1782
1783 grub_elf = grub_util_path_concat (2, core_services, "grub.elf");
1784 grub_install_copy_file (imgfile, grub_elf, 1);
1785
1786 f = grub_util_fopen (mach_kernel, "a+");
1787 if (!f)
1788 grub_util_error (_("Can't create file: %s"), strerror (errno));
1789 fclose (f);
1790
1791 fill_core_services (core_services);
1792
1793 ins_dev = grub_device_open (install_drive);
1794
1795 bless (ins_dev, core_services, 0);
1796
1797 if (update_nvram)
1798 {
1799 const char *dev;
1800 int partno;
1801
1802 partno = ins_dev->disk->partition
1803 ? ins_dev->disk->partition->number + 1 : 0;
1804 dev = grub_util_get_os_disk (install_device);
1805 grub_install_register_ieee1275 (0, dev, partno,
1806 "\\\\BootX");
1807 }
1808 grub_device_close (ins_dev);
1809 free (grub_elf);
1810 free (bootx);
1811 free (mach_kernel);
1812 free (grub_chrp);
1813 break;
1814 }
1815 /* If a install device is defined, copy the core.elf to PReP partition. */
1816 if (is_prep && install_device && install_device[0])
1817 {
1818 grub_device_t ins_dev;
1819 ins_dev = grub_device_open (install_drive);
1820 if (!ins_dev || !is_prep_partition (ins_dev))
1821 {
1822 grub_util_error ("%s", _("the chosen partition is not a PReP partition"));
1823 }
1824 if (is_prep_empty (ins_dev))
1825 {
1826 if (write_to_disk (ins_dev, imgfile))
1827 grub_util_error ("%s", _("failed to copy Grub to the PReP partition"));
1828 }
1829 else
1830 {
1831 char *s = xasprintf ("dd if=/dev/zero of=%s", install_device);
1832 grub_util_error (_("the PReP partition is not empty. If you are sure you want to use it, run dd to clear it: `%s'"),
1833 s);
1834 }
1835 grub_device_close (ins_dev);
1836 if (update_nvram)
1837 grub_install_register_ieee1275 (1, grub_util_get_os_disk (install_device),
1838 0, NULL);
1839 break;
1840 }
1841 /* fallthrough. */
1842 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1843 if (update_nvram)
1844 {
1845 const char *dev;
1846 char *relpath;
1847 int partno;
1848 relpath = grub_make_system_path_relative_to_its_root (imgfile);
1849 partno = grub_dev->disk->partition
1850 ? grub_dev->disk->partition->number + 1 : 0;
1851 dev = grub_util_get_os_disk (grub_devices[0]);
1852 grub_install_register_ieee1275 (0, dev,
1853 partno, relpath);
1854 }
1855 break;
1856 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1857 grub_install_sgi_setup (install_device, imgfile, "grub");
1858 break;
1859
1860 case GRUB_INSTALL_PLATFORM_I386_EFI:
1861 if (!efidir_is_mac)
1862 {
1863 char *dst = grub_util_path_concat (2, efidir, "grub.efi");
1864 /* For old macs. Suggested by Peter Jones. */
1865 grub_install_copy_file (imgfile, dst, 1);
1866 free (dst);
1867 }
1868 /* Fallthrough. */
1869 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1870 if (efidir_is_mac)
1871 {
1872 char *boot_efi;
1873 char *core_services = grub_util_path_concat (4, efidir,
1874 "System", "Library",
1875 "CoreServices");
1876 char *mach_kernel = grub_util_path_concat (2, efidir,
1877 "mach_kernel");
1878 FILE *f;
1879 grub_device_t ins_dev;
1880
1881 grub_install_mkdir_p (core_services);
1882
1883 boot_efi = grub_util_path_concat (2, core_services, "boot.efi");
1884 grub_install_copy_file (imgfile, boot_efi, 1);
1885
1886 f = grub_util_fopen (mach_kernel, "r+");
1887 if (!f)
1888 grub_util_error (_("Can't create file: %s"), strerror (errno));
1889 fclose (f);
1890
1891 fill_core_services(core_services);
1892
1893 ins_dev = grub_device_open (install_drive);
1894
1895 bless (ins_dev, boot_efi, 1);
1896 if (!removable && update_nvram)
1897 {
1898 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1899 int ret;
1900 ret = grub_install_register_efi (efidir_grub_dev,
1901 "\\System\\Library\\CoreServices",
1902 efi_distributor);
1903 if (ret)
1904 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
1905 strerror (ret));
1906 }
1907
1908 grub_device_close (ins_dev);
1909 free (boot_efi);
1910 free (mach_kernel);
1911 break;
1912 }
1913 /* FALLTHROUGH */
1914 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1915 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1916 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI:
1917 case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
1918 case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
1919 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1920 {
1921 char *dst = grub_util_path_concat (2, efidir, efi_file);
1922 grub_install_copy_file (imgfile, dst, 1);
1923 free (dst);
1924 }
1925 if (!removable && update_nvram)
1926 {
1927 char * efifile_path;
1928 char * part;
1929 int ret;
1930
1931 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1932 if (!efi_distributor || efi_distributor[0] == '\0')
1933 grub_util_error ("%s", _("EFI bootloader id isn't specified."));
1934 efifile_path = xasprintf ("\\EFI\\%s\\%s",
1935 efi_distributor,
1936 efi_file);
1937 part = (efidir_grub_dev->disk->partition
1938 ? grub_partition_get_name (efidir_grub_dev->disk->partition)
1939 : 0);
1940 grub_util_info ("Registering with EFI: distributor = `%s',"
1941 " path = `%s', ESP at %s%s%s",
1942 efi_distributor, efifile_path,
1943 efidir_grub_dev->disk->name,
1944 (part ? ",": ""), (part ? : ""));
1945 grub_free (part);
1946 ret = grub_install_register_efi (efidir_grub_dev,
1947 efifile_path, efi_distributor);
1948 if (ret)
1949 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
1950 strerror (ret));
1951 }
1952 break;
1953
1954 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1955 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1956 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1957 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1958 case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
1959 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1960 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1961 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1962 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1963 case GRUB_INSTALL_PLATFORM_I386_XEN:
1964 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1965 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
1966 grub_util_warn ("%s",
1967 _("WARNING: no platform-specific install was performed"));
1968 break;
1969 /* pacify warning. */
1970 case GRUB_INSTALL_PLATFORM_MAX:
1971 break;
1972 }
1973
1974 fprintf (stderr, "%s\n", _("Installation finished. No error reported."));
1975
1976 /* Free resources. */
1977 grub_gcry_fini_all ();
1978 grub_fini_all ();
1979
1980 return 0;
1981 }