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.
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.
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.
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/>.
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>
29 #include <grub/term.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>
48 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
49 #pragma GCC diagnostic ignored "-Wmissing-declarations"
51 #pragma GCC diagnostic error "-Wmissing-prototypes"
52 #pragma GCC diagnostic error "-Wmissing-declarations"
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
;
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;
85 OPTION_BOOT_DIRECTORY
= 0x301,
86 OPTION_ROOT_DIRECTORY
,
100 OPTION_EFI_DIRECTORY
,
106 OPTION_NO_BOOTSECTOR
,
108 OPTION_MACPPC_DIRECTORY
,
111 OPTION_LABEL_BGCOLOR
,
112 OPTION_PRODUCT_VERSION
115 static int fs_probe
= 1;
118 argp_parser (int key
, char *arg
, struct argp_state
*state
)
120 if (grub_install_parse (key
, arg
))
124 case OPTION_FORCE_FILE_ID
:
132 if (!grub_strstr (arg
, "setup"))
133 install_bootsector
= 0;
136 case OPTION_PRODUCT_VERSION
:
137 free (product_version
);
138 product_version
= xstrdup (arg
);
140 case OPTION_LABEL_FONT
:
142 label_font
= xstrdup (arg
);
145 case OPTION_LABEL_COLOR
:
147 label_color
= xstrdup (arg
);
150 case OPTION_LABEL_BGCOLOR
:
151 free (label_bgcolor
);
152 label_bgcolor
= xstrdup (arg
);
155 /* Accept and ignore for compatibility. */
157 case OPTION_MKRELPATH
:
160 case OPTION_MKDEVICEMAP
:
161 case OPTION_NO_FLOPPY
:
163 case OPTION_ROOT_DIRECTORY
:
164 /* Accept for compatibility. */
166 rootdir
= xstrdup (arg
);
169 case OPTION_BOOT_DIRECTORY
:
171 bootdir
= xstrdup (arg
);
174 case OPTION_MACPPC_DIRECTORY
:
176 macppcdir
= xstrdup (arg
);
179 case OPTION_EFI_DIRECTORY
:
181 efidir
= xstrdup (arg
);
184 case OPTION_DISK_MODULE
:
186 disk_module
= xstrdup (arg
);
191 target
= xstrdup (arg
);
194 case OPTION_DEBUG_IMAGE
:
196 debug_image
= xstrdup (arg
);
199 case OPTION_NO_NVRAM
:
211 case OPTION_REMOVABLE
:
215 case OPTION_ALLOW_FLOPPY
:
219 case OPTION_NO_BOOTSECTOR
:
220 install_bootsector
= 0;
223 case OPTION_NO_RS_CODES
:
231 case OPTION_BOOTLOADER_ID
:
232 free (bootloader_id
);
233 bootloader_id
= xstrdup (arg
);
238 grub_util_error ("%s", _("More than one install device?"));
239 install_device
= xstrdup (arg
);
243 return ARGP_ERR_UNKNOWN
;
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},
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},
309 get_default_platform (void)
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";
319 return "mipsel-loongson";
321 #elif defined (__MIPSEB__)
323 #elif defined (__ia64__)
325 #elif defined (__arm__)
326 return grub_install_get_default_arm_platform ();
327 #elif defined (__aarch64__)
329 #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__)
330 return grub_install_get_default_x86_platform ();
336 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
339 help_filter (int key
, const char *text
, void *input
__attribute__ ((unused
)))
343 case OPTION_BOOT_DIRECTORY
:
344 return xasprintf (text
, GRUB_DIR_NAME
, GRUB_BOOT_DIR_NAME
"/" GRUB_DIR_NAME
);
347 char *plats
= grub_install_get_platforms_string ();
349 ret
= xasprintf (text
, get_default_platform (), plats
);
353 case ARGP_KEY_HELP_POST_DOC
:
354 return xasprintf (text
, program_name
, GRUB_BOOT_DIR_NAME
"/" GRUB_DIR_NAME
);
356 return grub_install_help_filter (key
, text
, input
);
360 #pragma GCC diagnostic error "-Wformat-nonliteral"
362 /* TRANSLATORS: INSTALL_DEVICE isn't an identifier and is the DEVICE you
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
374 probe_raid_level (grub_disk_t disk
)
376 /* disk might be NULL in the case of a LVM physical volume with no LVM
377 signature. Ignore such cases here. */
381 if (disk
->dev
->id
!= GRUB_DISK_DEVICE_DISKFILTER_ID
)
384 if (disk
->name
[0] != 'm' || disk
->name
[1] != 'd')
387 if (!((struct grub_diskfilter_lv
*) disk
->data
)->segments
)
389 return ((struct grub_diskfilter_lv
*) disk
->data
)->segments
->type
;
393 push_partmap_module (const char *map
, void *data
__attribute__ ((unused
)))
397 if (strcmp (map
, "openbsd") == 0 || strcmp (map
, "netbsd") == 0)
399 grub_install_push_module ("part_bsd");
403 snprintf (buf
, sizeof (buf
), "part_%s", map
);
404 grub_install_push_module (buf
);
408 push_cryptodisk_module (const char *mod
, void *data
__attribute__ ((unused
)))
410 grub_install_push_module (mod
);
414 probe_mods (grub_disk_t disk
)
416 grub_partition_t part
;
417 grub_disk_memberlist_t list
= NULL
, tmp
;
420 if (disk
->partition
== NULL
)
421 grub_util_info ("no partition map found for %s", disk
->name
);
423 for (part
= disk
->partition
; part
; part
= part
->parent
)
424 push_partmap_module (part
->partmap
->name
, NULL
);
426 if (disk
->dev
->id
== GRUB_DISK_DEVICE_DISKFILTER_ID
)
428 grub_diskfilter_get_partmap (disk
, push_partmap_module
, NULL
);
429 have_abstractions
= 1;
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");
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");
441 if (disk
->dev
->id
== GRUB_DISK_DEVICE_CRYPTODISK_ID
)
443 grub_util_cryptodisk_get_abstraction (disk
,
444 push_cryptodisk_module
, NULL
);
445 have_abstractions
= 1;
449 raid_level
= probe_raid_level (disk
);
452 grub_install_push_module ("diskfilter");
453 if (disk
->dev
->disk_raidname
)
454 grub_install_push_module (disk
->dev
->disk_raidname (disk
));
457 grub_install_push_module ("raid5rec");
459 grub_install_push_module ("raid6rec");
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
);
466 probe_mods (list
->disk
);
474 have_bootdev (enum grub_install_plat pl
)
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
:
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
:
501 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON
:
502 case GRUB_INSTALL_PLATFORM_ARM_UBOOT
:
504 case GRUB_INSTALL_PLATFORM_I386_XEN
:
505 case GRUB_INSTALL_PLATFORM_X86_64_XEN
:
506 case GRUB_INSTALL_PLATFORM_I386_XEN_PVH
:
509 /* pacify warning. */
510 case GRUB_INSTALL_PLATFORM_MAX
:
517 probe_cryptodisk_uuid (grub_disk_t disk
)
519 grub_disk_memberlist_t list
= NULL
, tmp
;
521 /* In case of LVM/RAID, check the member devices as well. */
522 if (disk
->dev
->disk_memberlist
)
524 list
= disk
->dev
->disk_memberlist (disk
);
528 probe_cryptodisk_uuid (list
->disk
);
533 if (disk
->dev
->id
== GRUB_DISK_DEVICE_CRYPTODISK_ID
)
535 const char *uuid
= grub_util_cryptodisk_get_uuid (disk
);
537 load_cfg_f
= grub_util_fopen (load_cfg
, "wb");
540 fprintf (load_cfg_f
, "cryptomount -u %s\n",
546 is_same_disk (const char *a
, const char *b
)
550 if ((*a
== ',' || *a
== '\0') && (*b
== ',' || *b
== '\0'))
570 grub_uint8_t rnd
[15];
571 const size_t sz
= sizeof (rnd
) * GRUB_CHAR_BIT
/ 5;
572 char * ret
= xmalloc (sz
+ 1);
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
++)
578 grub_size_t b
= i
* 5;
580 grub_size_t f1
= GRUB_CHAR_BIT
- b
% GRUB_CHAR_BIT
;
585 r
= (rnd
[b
/ GRUB_CHAR_BIT
] >> (b
% GRUB_CHAR_BIT
)) & ((1 << f1
) - 1);
587 r
|= (rnd
[b
/ GRUB_CHAR_BIT
+ 1] & ((1 << f2
) - 1)) << f1
;
591 ret
[i
] = 'a' + (r
- 10);
598 escape (const char *in
)
604 for (ptr
= (char*)in
; *ptr
; ptr
++)
607 ret
= grub_malloc (ptr
- in
+ overhead
+ 1);
611 grub_strchrsub (ret
, in
, '\'', "'\\''");
615 static struct grub_util_config config
;
618 device_map_check_duplicates (const char *dev_map
)
621 char buf
[1024]; /* XXX */
627 if (dev_map
[0] == '\0')
630 fp
= grub_util_fopen (dev_map
, "r");
634 d
= xmalloc (alloced
* sizeof (d
[0]));
636 while (fgets (buf
, sizeof (buf
), fp
))
641 /* Skip leading spaces. */
642 while (*p
&& grub_isspace (*p
))
645 /* If the first character is `#' or NUL, skip this line. */
646 if (*p
== '\0' || *p
== '#')
659 if (filled
>= alloced
)
662 d
= xrealloc (d
, alloced
* sizeof (d
[0]));
667 d
[filled
++] = xstrdup (e
);
672 qsort (d
, filled
, sizeof (d
[0]), grub_qsort_strcmp
);
674 for (i
= 0; i
+ 1 < filled
; i
++)
675 if (strcmp (d
[i
], d
[i
+1]) == 0)
677 grub_util_error (_("the drive %s is defined multiple times in the device map %s"),
681 for (i
= 0; i
< filled
; i
++)
688 write_to_disk (grub_device_t dev
, const char *fn
)
694 core_size
= grub_util_get_image_size (fn
);
696 core_img
= grub_util_read_image (fn
);
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
);
706 is_prep_partition (grub_device_t dev
)
710 if (!dev
->disk
->partition
)
712 if (strcmp(dev
->disk
->partition
->partmap
->name
, "msdos") == 0)
713 return (dev
->disk
->partition
->msdostype
== 0x41);
715 if (strcmp (dev
->disk
->partition
->partmap
->name
, "gpt") == 0)
717 struct grub_gpt_partentry gptdata
;
718 grub_partition_t p
= dev
->disk
->partition
;
720 dev
->disk
->partition
= dev
->disk
->partition
->parent
;
722 if (grub_disk_read (dev
->disk
, p
->offset
, p
->index
,
723 sizeof (gptdata
), &gptdata
) == 0)
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 }
732 ret
= grub_memcmp (&template, &gptdata
.type
,
733 sizeof (template)) == 0;
735 dev
->disk
->partition
= p
;
743 is_prep_empty (grub_device_t dev
)
745 grub_disk_addr_t dsize
, addr
;
746 grub_uint32_t buffer
[32768];
748 dsize
= grub_disk_get_size (dev
->disk
);
749 for (addr
= 0; addr
< dsize
;
750 addr
+= sizeof (buffer
) / GRUB_DISK_SECTOR_SIZE
)
752 grub_size_t sz
= sizeof (buffer
);
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
);
759 if (addr
== 0 && grub_memcmp (buffer
, ELFMAG
, SELFMAG
) == 0)
762 for (ptr
= buffer
; ptr
< buffer
+ sz
/ sizeof (*buffer
); ptr
++)
771 bless (grub_device_t dev
, const char *path
, int x86
)
776 grub_util_info ("blessing %s", path
);
778 if (stat (path
, &st
) < 0)
779 grub_util_error (N_("cannot stat `%s': %s"),
780 path
, strerror (errno
));
782 err
= grub_mac_bless_inode (dev
, st
.st_ino
, S_ISDIR (st
.st_mode
), x86
);
784 grub_util_error ("%s", grub_errmsg
);
785 grub_util_info ("blessed");
789 fill_core_services (const char *core_services
)
794 char *label_string
= xasprintf ("%s %s", bootloader_id
, product_version
);
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");
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
);
810 sysv_plist
= grub_util_path_concat (2, core_services
, "SystemVersion.plist");
811 f
= grub_util_fopen (sysv_plist
, "wb");
813 "<plist version=\"1.0\">\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"
822 "</plist>\n", bootloader_id
, product_version
);
828 main (int argc
, char *argv
[])
831 const char *efi_distributor
= NULL
;
832 const char *efi_file
= NULL
;
835 grub_device_t grub_dev
= NULL
;
836 enum grub_install_plat platform
;
837 char *grubdir
, *device_map
;
838 char **curdev
, **curdrive
;
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;
846 const char *pkgdatadir
;
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");
853 argp_parse (&argp
, argc
, argv
, 0, 0, 0);
856 grub_env_set ("debug", "all");
858 grub_util_load_config (&config
);
860 if (!bootloader_id
&& config
.grub_distributor
)
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';
869 if (!bootloader_id
|| bootloader_id
[0] == '\0')
871 free (bootloader_id
);
872 bootloader_id
= xstrdup ("grub");
875 if (!grub_install_source_directory
)
880 t
= get_default_platform ();
882 grub_util_error ("%s",
883 _("Unable to determine your platform."
886 target
= xstrdup (t
);
888 grub_install_source_directory
889 = grub_util_path_concat (2, grub_util_get_pkglibdir (), target
);
892 platform
= grub_install_get_target (grub_install_source_directory
);
895 char *platname
= grub_install_get_platform_name (platform
);
896 fprintf (stderr
, _("Installing for %s platform.\n"), platname
);
902 case GRUB_INSTALL_PLATFORM_I386_PC
:
904 disk_module
= xstrdup ("biosdisk");
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
:
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");
935 /* pacify warning. */
936 case GRUB_INSTALL_PLATFORM_MAX
:
942 case GRUB_INSTALL_PLATFORM_I386_PC
:
943 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
945 grub_util_error ("%s", _("install device isn't specified"));
947 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
951 case GRUB_INSTALL_PLATFORM_MIPS_ARC
:
952 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC
:
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
;
978 /* pacify warning. */
979 case GRUB_INSTALL_PLATFORM_MAX
:
984 bootdir
= grub_util_path_concat (3, "/", rootdir
, GRUB_BOOT_DIR_NAME
);
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
);
991 grub_util_error (_("failed to get canonical path of `%s'"), t
);
994 device_map
= grub_util_path_concat (2, grubdir
, "device.map");
997 grub_util_unlink (device_map
);
999 device_map_check_duplicates (device_map
);
1000 grub_util_biosdisk_init (device_map
);
1002 /* Initialize all modules. */
1004 grub_gcry_init_all ();
1005 grub_hostfs_init ();
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
:
1024 /* pacify warning. */
1025 case GRUB_INSTALL_PLATFORM_MAX
:
1029 /* Find the EFI System Partition. */
1034 free (install_device
);
1035 install_device
= NULL
;
1038 char *d
= grub_util_path_concat (2, bootdir
, "efi");
1040 if (!grub_util_is_directory (d
))
1043 d
= grub_util_path_concat (2, bootdir
, "EFI");
1046 The EFI System Partition may have been given directly using
1049 if (!grub_util_is_directory (d
)
1050 && rootdir
&& grub_strcmp (rootdir
, "/") != 0)
1053 d
= xstrdup (rootdir
);
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')
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?)"),
1070 install_device
= efidir_device_names
[0];
1072 for (curdev
= efidir_device_names
; *curdev
; curdev
++)
1073 grub_util_pull_device (*curdev
);
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]);
1080 efidir_grub_dev
= grub_device_open (efidir_grub_devname
);
1081 if (! efidir_grub_dev
)
1082 grub_util_error ("%s", grub_errmsg
);
1084 fs
= grub_fs_probe (efidir_grub_dev
);
1086 grub_util_error ("%s", grub_errmsg
);
1090 if (grub_strcmp (fs
->name
, "hfs") == 0
1091 || grub_strcmp (fs
->name
, "hfsplus") == 0)
1094 if (!efidir_is_mac
&& grub_strcmp (fs
->name
, "fat") != 0)
1095 grub_util_error (_("%s doesn't look like an EFI partition"), efidir
);
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.
1104 efi_distributor
= bootloader_id
;
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.
1112 efi_distributor
= "BOOT";
1115 case GRUB_INSTALL_PLATFORM_I386_EFI
:
1116 efi_file
= "BOOTIA32.EFI";
1118 case GRUB_INSTALL_PLATFORM_X86_64_EFI
:
1119 efi_file
= "BOOTX64.EFI";
1121 case GRUB_INSTALL_PLATFORM_IA64_EFI
:
1122 efi_file
= "BOOTIA64.EFI";
1124 case GRUB_INSTALL_PLATFORM_ARM_EFI
:
1125 efi_file
= "BOOTARM.EFI";
1127 case GRUB_INSTALL_PLATFORM_ARM64_EFI
:
1128 efi_file
= "BOOTAA64.EFI";
1130 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI
:
1131 efi_file
= "BOOTMIPS64EL.EFI";
1133 case GRUB_INSTALL_PLATFORM_RISCV32_EFI
:
1134 efi_file
= "BOOTRISCV32.EFI";
1136 case GRUB_INSTALL_PLATFORM_RISCV64_EFI
:
1137 efi_file
= "BOOTRISCV64.EFI";
1140 grub_util_error ("%s", _("You've found a bug"));
1146 /* It is convenient for each architecture to have a different
1147 efi_file, so that different versions can be installed in parallel.
1151 case GRUB_INSTALL_PLATFORM_I386_EFI
:
1152 efi_file
= "grubia32.efi";
1154 case GRUB_INSTALL_PLATFORM_X86_64_EFI
:
1155 efi_file
= "grubx64.efi";
1157 case GRUB_INSTALL_PLATFORM_IA64_EFI
:
1158 efi_file
= "grubia64.efi";
1160 case GRUB_INSTALL_PLATFORM_ARM_EFI
:
1161 efi_file
= "grubarm.efi";
1163 case GRUB_INSTALL_PLATFORM_ARM64_EFI
:
1164 efi_file
= "grubaa64.efi";
1166 case GRUB_INSTALL_PLATFORM_MIPS64EL_EFI
:
1167 efi_file
= "grubmips64el.efi";
1169 case GRUB_INSTALL_PLATFORM_RISCV32_EFI
:
1170 efi_file
= "grubriscv32.efi";
1172 case GRUB_INSTALL_PLATFORM_RISCV64_EFI
:
1173 efi_file
= "grubriscv64.efi";
1176 efi_file
= "grub.efi";
1180 t
= grub_util_path_concat (3, efidir
, "EFI", efi_distributor
);
1183 grub_install_mkdir_p (efidir
);
1186 if (platform
== GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
)
1194 d
= grub_util_path_concat (2, bootdir
, "macppc");
1195 if (!grub_util_is_directory (d
))
1198 d
= grub_util_path_concat (2, bootdir
, "efi");
1200 /* Find the Mac HFS(+) System Partition. */
1201 if (!grub_util_is_directory (d
))
1204 d
= grub_util_path_concat (2, bootdir
, "EFI");
1206 if (!grub_util_is_directory (d
))
1216 char **macppcdir_device_names
= NULL
;
1217 grub_device_t macppcdir_grub_dev
= NULL
;
1218 char *macppcdir_grub_devname
;
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?)"),
1226 for (curdev
= macppcdir_device_names
; *curdev
; curdev
++)
1227 grub_util_pull_device (*curdev
);
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]);
1234 macppcdir_grub_dev
= grub_device_open (macppcdir_grub_devname
);
1235 if (! macppcdir_grub_dev
)
1236 grub_util_error ("%s", grub_errmsg
);
1238 fs
= grub_fs_probe (macppcdir_grub_dev
);
1240 grub_util_error ("%s", grub_errmsg
);
1242 if (grub_strcmp (fs
->name
, "hfs") != 0
1243 && grub_strcmp (fs
->name
, "hfsplus") != 0
1245 grub_util_error (_("filesystem on %s is neither HFS nor HFS+"),
1247 if (grub_strcmp (fs
->name
, "hfs") == 0
1248 || grub_strcmp (fs
->name
, "hfsplus") == 0)
1250 install_device
= macppcdir_device_names
[0];
1256 grub_install_copy_files (grub_install_source_directory
,
1259 char *envfile
= grub_util_path_concat (2, grubdir
, "grubenv");
1260 if (!grub_util_is_regular (envfile
))
1261 grub_util_create_envblk_file (envfile
);
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?)"),
1271 for (curdev
= grub_devices
; *curdev
; curdev
++)
1273 grub_util_pull_device (*curdev
);
1277 grub_drives
= xmalloc (sizeof (grub_drives
[0]) * (ndev
+ 1));
1279 for (curdev
= grub_devices
, curdrive
= grub_drives
; *curdev
; curdev
++,
1282 *curdrive
= grub_util_get_grub_dev (*curdev
);
1284 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1289 grub_dev
= grub_device_open (grub_drives
[0]);
1291 grub_util_error ("%s", grub_errmsg
);
1293 grub_fs
= grub_fs_probe (grub_dev
);
1295 grub_util_error ("%s", grub_errmsg
);
1297 grub_install_push_module (grub_fs
->name
);
1300 probe_mods (grub_dev
->disk
);
1302 for (curdrive
= grub_drives
+ 1; *curdrive
; curdrive
++)
1304 grub_device_t dev
= grub_device_open (*curdrive
);
1308 probe_mods (dev
->disk
);
1309 grub_device_close (dev
);
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 ());
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)
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");
1328 else if (disk_module
&& disk_module
[0])
1329 grub_install_push_module (disk_module
);
1331 relative_grubdir
= grub_make_system_path_relative_to_its_root (grubdir
);
1332 if (relative_grubdir
[0] == '\0')
1334 free (relative_grubdir
);
1335 relative_grubdir
= xstrdup ("/");
1338 char *platname
= grub_install_get_platform_name (platform
);
1341 char *t
= grub_util_path_concat (2, grubdir
,
1343 platdir
= grub_canonicalize_file_name (t
);
1345 grub_util_error (_("failed to get canonical path of `%s'"),
1349 load_cfg
= grub_util_path_concat (2, platdir
,
1352 grub_util_unlink (load_cfg
);
1354 if (debug_image
&& debug_image
[0])
1356 load_cfg_f
= grub_util_fopen (load_cfg
, "wb");
1358 fprintf (load_cfg_f
, "set debug='%s'\n",
1361 char *prefix_drive
= NULL
;
1362 char *install_drive
= NULL
;
1366 if (install_device
[0] == '('
1367 && install_device
[grub_strlen (install_device
) - 1] == ')')
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';
1376 grub_util_pull_device (install_device
);
1377 install_drive
= grub_util_get_grub_dev (install_device
);
1379 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1384 if (!have_abstractions
)
1386 if ((disk_module
&& grub_strcmp (disk_module
, "biosdisk") != 0)
1389 && platform
!= GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
)
1390 || (install_drive
&& !is_same_disk (grub_drives
[0], install_drive
))
1391 || !have_bootdev (platform
))
1394 /* generic method (used on coreboot and ata mod). */
1396 && grub_fs
->fs_uuid
&& grub_fs
->fs_uuid (grub_dev
, &uuid
))
1398 grub_print_error ();
1404 load_cfg_f
= grub_util_fopen (load_cfg
, "wb");
1408 fprintf (load_cfg_f
, "search.fs_uuid %s root ",
1410 grub_install_push_module ("search_fs_uuid");
1414 char *rndstr
= get_rndstr ();
1415 char *fl
= grub_util_path_concat (3, grubdir
,
1417 char *fldir
= grub_util_path_concat (2, grubdir
,
1421 grub_install_mkdir_p (fldir
);
1422 flf
= grub_util_fopen (fl
, "w");
1424 grub_util_error (_("Can't create file: %s"), strerror (errno
));
1426 relfl
= grub_make_system_path_relative_to_its_root (fl
);
1427 fprintf (load_cfg_f
, "search.file %s root ",
1429 grub_install_push_module ("search_fs_file");
1431 for (curdev
= grub_devices
, curdrive
= grub_drives
; *curdev
; curdev
++,
1437 if (curdrive
== grub_drives
)
1440 dev
= grub_device_open (*curdrive
);
1444 if (dev
->disk
->dev
->id
!= GRUB_DISK_DEVICE_HOSTDISK_ID
)
1446 grub_util_fprint_full_disk_name (load_cfg_f
,
1449 fprintf (load_cfg_f
, " ");
1450 if (dev
!= grub_dev
)
1451 grub_device_close (dev
);
1455 map
= grub_util_biosdisk_get_compatibility_hint (dev
->disk
);
1459 grub_util_fprint_full_disk_name (load_cfg_f
, map
, dev
);
1460 fprintf (load_cfg_f
, " ");
1464 if (disk_module
&& disk_module
[0]
1465 && grub_strcmp (disk_module
, "biosdisk") != 0)
1466 g
= grub_util_guess_baremetal_drive (*curdev
);
1470 case GRUB_INSTALL_PLATFORM_I386_PC
:
1471 g
= grub_util_guess_bios_drive (*curdev
);
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
);
1483 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
1484 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
1485 case GRUB_INSTALL_PLATFORM_I386_IEEE1275
:
1487 const char * ofpath
= grub_util_devname_to_ofpath (*curdev
);
1488 g
= xasprintf ("ieee1275/%s", ofpath
);
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
);
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"));
1508 /* pacify warning. */
1509 case GRUB_INSTALL_PLATFORM_MAX
:
1514 grub_util_fprint_full_disk_name (load_cfg_f
, g
, dev
);
1515 fprintf (load_cfg_f
, " ");
1518 if (dev
!= grub_dev
)
1519 grub_device_close (dev
);
1521 fprintf (load_cfg_f
, "\n");
1522 char *escaped_relpath
= escape (relative_grubdir
);
1523 fprintf (load_cfg_f
, "set prefix=($root)'%s'\n",
1528 /* We need to hardcode the partition number in the core image's prefix. */
1530 for (p
= grub_drives
[0]; *p
; )
1532 if (*p
== '\\' && p
[1])
1537 if (*p
== ',' || *p
== '\0')
1541 prefix_drive
= xasprintf ("(%s)", p
);
1546 if (config
.is_cryptodisk_enabled
)
1549 probe_cryptodisk_uuid (grub_dev
->disk
);
1551 for (curdrive
= grub_drives
+ 1; *curdrive
; curdrive
++)
1553 grub_device_t dev
= grub_device_open (*curdrive
);
1557 probe_cryptodisk_uuid (dev
->disk
);
1558 grub_device_close (dev
);
1561 prefix_drive
= xasprintf ("(%s)", grub_drives
[0]);
1564 char mkimage_target
[200];
1565 const char *core_name
= NULL
;
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
),
1580 grub_install_get_platform_cpu (platform
),
1581 grub_install_get_platform_platform (platform
));
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
),
1589 grub_install_get_platform_cpu (platform
),
1590 grub_install_get_platform_platform (platform
));
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
),
1604 grub_install_get_platform_cpu (platform
),
1605 grub_install_get_platform_platform (platform
));
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
),
1616 grub_install_get_platform_cpu (platform
),
1617 grub_install_get_platform_platform (platform
));
1618 core_name
= "core.img";
1620 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
1621 strcpy (mkimage_target
, "sparc64-ieee1275-raw");
1622 core_name
= "core.img";
1624 /* pacify warning. */
1625 case GRUB_INSTALL_PLATFORM_MAX
:
1630 grub_util_error ("%s", _("You've found a bug"));
1633 fclose (load_cfg_f
);
1635 char *imgfile
= grub_util_path_concat (2, platdir
,
1637 char *prefix
= xasprintf ("%s%s", prefix_drive
? : "",
1639 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory
,
1641 /* output */ imgfile
,
1643 have_load_cfg
? load_cfg
: NULL
,
1644 /* image target */ mkimage_target
, 0);
1645 /* Backward-compatibility kludges. */
1648 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON
:
1650 char *dst
= grub_util_path_concat (2, bootdir
, "grub.elf");
1651 grub_install_copy_file (imgfile
, dst
, 1);
1656 case GRUB_INSTALL_PLATFORM_I386_IEEE1275
:
1657 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
1659 char *dst
= grub_util_path_concat (2, grubdir
, "grub");
1660 grub_install_copy_file (imgfile
, dst
, 1);
1665 case GRUB_INSTALL_PLATFORM_I386_EFI
:
1666 case GRUB_INSTALL_PLATFORM_X86_64_EFI
:
1668 char *dst
= grub_util_path_concat (2, platdir
, "grub.efi");
1669 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory
,
1673 have_load_cfg
? load_cfg
: NULL
,
1674 /* image target */ mkimage_target
, 0);
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
:
1698 /* pacify warning. */
1699 case GRUB_INSTALL_PLATFORM_MAX
:
1703 /* Perform the platform-dependent install */
1707 case GRUB_INSTALL_PLATFORM_I386_PC
:
1709 char *boot_img_src
= grub_util_path_concat (2,
1710 grub_install_source_directory
,
1712 char *boot_img
= grub_util_path_concat (2, platdir
,
1714 grub_install_copy_file (boot_img_src
, boot_img
, 1);
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" : "",
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
);
1736 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
1738 char *boot_img_src
= grub_util_path_concat (2,
1739 grub_install_source_directory
,
1741 char *boot_img
= grub_util_path_concat (2, platdir
,
1743 grub_install_copy_file (boot_img_src
, boot_img
, 1);
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" : "",
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
,
1764 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
1767 char *core_services
= grub_util_path_concat (4, macppcdir
,
1768 "System", "Library",
1770 char *mach_kernel
= grub_util_path_concat (2, macppcdir
,
1772 char *grub_elf
, *bootx
;
1774 grub_device_t ins_dev
;
1775 char *grub_chrp
= grub_util_path_concat (2,
1776 grub_install_source_directory
,
1779 grub_install_mkdir_p (core_services
);
1781 bootx
= grub_util_path_concat (2, core_services
, "BootX");
1782 grub_install_copy_file (grub_chrp
, bootx
, 1);
1784 grub_elf
= grub_util_path_concat (2, core_services
, "grub.elf");
1785 grub_install_copy_file (imgfile
, grub_elf
, 1);
1787 f
= grub_util_fopen (mach_kernel
, "a+");
1789 grub_util_error (_("Can't create file: %s"), strerror (errno
));
1792 fill_core_services (core_services
);
1794 ins_dev
= grub_device_open (install_drive
);
1796 bless (ins_dev
, core_services
, 0);
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
,
1809 grub_device_close (ins_dev
);
1816 /* If a install device is defined, copy the core.elf to PReP partition. */
1817 if (is_prep
&& install_device
&& install_device
[0])
1819 grub_device_t ins_dev
;
1820 ins_dev
= grub_device_open (install_drive
);
1821 if (!ins_dev
|| !is_prep_partition (ins_dev
))
1823 grub_util_error ("%s", _("the chosen partition is not a PReP partition"));
1825 if (is_prep_empty (ins_dev
))
1827 if (write_to_disk (ins_dev
, imgfile
))
1828 grub_util_error ("%s", _("failed to copy Grub to the PReP partition"));
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'"),
1836 grub_device_close (ins_dev
);
1838 grub_install_register_ieee1275 (1, grub_util_get_os_disk (install_device
),
1843 case GRUB_INSTALL_PLATFORM_I386_IEEE1275
:
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
,
1857 case GRUB_INSTALL_PLATFORM_MIPS_ARC
:
1858 grub_install_sgi_setup (install_device
, imgfile
, "grub");
1861 case GRUB_INSTALL_PLATFORM_I386_EFI
:
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);
1870 case GRUB_INSTALL_PLATFORM_X86_64_EFI
:
1874 char *core_services
= grub_util_path_concat (4, efidir
,
1875 "System", "Library",
1877 char *mach_kernel
= grub_util_path_concat (2, efidir
,
1880 grub_device_t ins_dev
;
1882 grub_install_mkdir_p (core_services
);
1884 boot_efi
= grub_util_path_concat (2, core_services
, "boot.efi");
1885 grub_install_copy_file (imgfile
, boot_efi
, 1);
1887 f
= grub_util_fopen (mach_kernel
, "r+");
1889 grub_util_error (_("Can't create file: %s"), strerror (errno
));
1892 fill_core_services(core_services
);
1894 ins_dev
= grub_device_open (install_drive
);
1896 bless (ins_dev
, boot_efi
, 1);
1897 if (!removable
&& update_nvram
)
1899 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1901 ret
= grub_install_register_efi (efidir_grub_dev
,
1902 "\\System\\Library\\CoreServices",
1905 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
1909 grub_device_close (ins_dev
);
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
:
1922 char *dst
= grub_util_path_concat (2, efidir
, efi_file
);
1923 grub_install_copy_file (imgfile
, dst
, 1);
1926 if (!removable
&& update_nvram
)
1928 char * efifile_path
;
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",
1938 part
= (efidir_grub_dev
->disk
->partition
1939 ? grub_partition_get_name (efidir_grub_dev
->disk
->partition
)
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
? : ""));
1947 ret
= grub_install_register_efi (efidir_grub_dev
,
1948 efifile_path
, efi_distributor
);
1950 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
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"));
1970 /* pacify warning. */
1971 case GRUB_INSTALL_PLATFORM_MAX
:
1975 fprintf (stderr
, "%s\n", _("Installation finished. No error reported."));
1977 /* Free resources. */
1978 grub_gcry_fini_all ();