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";
1168 case GRUB_INSTALL_PLATFORM_RISCV32_EFI
:
1169 efi_file
= "grubriscv32.efi";
1171 case GRUB_INSTALL_PLATFORM_RISCV64_EFI
:
1172 efi_file
= "grubriscv64.efi";
1175 efi_file
= "grub.efi";
1179 t
= grub_util_path_concat (3, efidir
, "EFI", efi_distributor
);
1182 grub_install_mkdir_p (efidir
);
1185 if (platform
== GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
)
1193 d
= grub_util_path_concat (2, bootdir
, "macppc");
1194 if (!grub_util_is_directory (d
))
1197 d
= grub_util_path_concat (2, bootdir
, "efi");
1199 /* Find the Mac HFS(+) System Partition. */
1200 if (!grub_util_is_directory (d
))
1203 d
= grub_util_path_concat (2, bootdir
, "EFI");
1205 if (!grub_util_is_directory (d
))
1215 char **macppcdir_device_names
= NULL
;
1216 grub_device_t macppcdir_grub_dev
= NULL
;
1217 char *macppcdir_grub_devname
;
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?)"),
1225 for (curdev
= macppcdir_device_names
; *curdev
; curdev
++)
1226 grub_util_pull_device (*curdev
);
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]);
1233 macppcdir_grub_dev
= grub_device_open (macppcdir_grub_devname
);
1234 if (! macppcdir_grub_dev
)
1235 grub_util_error ("%s", grub_errmsg
);
1237 fs
= grub_fs_probe (macppcdir_grub_dev
);
1239 grub_util_error ("%s", grub_errmsg
);
1241 if (grub_strcmp (fs
->name
, "hfs") != 0
1242 && grub_strcmp (fs
->name
, "hfsplus") != 0
1244 grub_util_error (_("filesystem on %s is neither HFS nor HFS+"),
1246 if (grub_strcmp (fs
->name
, "hfs") == 0
1247 || grub_strcmp (fs
->name
, "hfsplus") == 0)
1249 install_device
= macppcdir_device_names
[0];
1255 grub_install_copy_files (grub_install_source_directory
,
1258 char *envfile
= grub_util_path_concat (2, grubdir
, "grubenv");
1259 if (!grub_util_is_regular (envfile
))
1260 grub_util_create_envblk_file (envfile
);
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?)"),
1270 for (curdev
= grub_devices
; *curdev
; curdev
++)
1272 grub_util_pull_device (*curdev
);
1276 grub_drives
= xmalloc (sizeof (grub_drives
[0]) * (ndev
+ 1));
1278 for (curdev
= grub_devices
, curdrive
= grub_drives
; *curdev
; curdev
++,
1281 *curdrive
= grub_util_get_grub_dev (*curdev
);
1283 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1288 grub_dev
= grub_device_open (grub_drives
[0]);
1290 grub_util_error ("%s", grub_errmsg
);
1292 grub_fs
= grub_fs_probe (grub_dev
);
1294 grub_util_error ("%s", grub_errmsg
);
1296 grub_install_push_module (grub_fs
->name
);
1299 probe_mods (grub_dev
->disk
);
1301 for (curdrive
= grub_drives
+ 1; *curdrive
; curdrive
++)
1303 grub_device_t dev
= grub_device_open (*curdrive
);
1307 probe_mods (dev
->disk
);
1308 grub_device_close (dev
);
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 ());
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)
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");
1327 else if (disk_module
&& disk_module
[0])
1328 grub_install_push_module (disk_module
);
1330 relative_grubdir
= grub_make_system_path_relative_to_its_root (grubdir
);
1331 if (relative_grubdir
[0] == '\0')
1333 free (relative_grubdir
);
1334 relative_grubdir
= xstrdup ("/");
1337 char *platname
= grub_install_get_platform_name (platform
);
1340 char *t
= grub_util_path_concat (2, grubdir
,
1342 platdir
= grub_canonicalize_file_name (t
);
1344 grub_util_error (_("failed to get canonical path of `%s'"),
1348 load_cfg
= grub_util_path_concat (2, platdir
,
1351 grub_util_unlink (load_cfg
);
1353 if (debug_image
&& debug_image
[0])
1355 load_cfg_f
= grub_util_fopen (load_cfg
, "wb");
1357 fprintf (load_cfg_f
, "set debug='%s'\n",
1360 char *prefix_drive
= NULL
;
1361 char *install_drive
= NULL
;
1365 if (install_device
[0] == '('
1366 && install_device
[grub_strlen (install_device
) - 1] == ')')
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';
1375 grub_util_pull_device (install_device
);
1376 install_drive
= grub_util_get_grub_dev (install_device
);
1378 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1383 if (!have_abstractions
)
1385 if ((disk_module
&& grub_strcmp (disk_module
, "biosdisk") != 0)
1388 && platform
!= GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
)
1389 || (install_drive
&& !is_same_disk (grub_drives
[0], install_drive
))
1390 || !have_bootdev (platform
))
1393 /* generic method (used on coreboot and ata mod). */
1395 && grub_fs
->fs_uuid
&& grub_fs
->fs_uuid (grub_dev
, &uuid
))
1397 grub_print_error ();
1403 load_cfg_f
= grub_util_fopen (load_cfg
, "wb");
1407 fprintf (load_cfg_f
, "search.fs_uuid %s root ",
1409 grub_install_push_module ("search_fs_uuid");
1413 char *rndstr
= get_rndstr ();
1414 char *fl
= grub_util_path_concat (3, grubdir
,
1416 char *fldir
= grub_util_path_concat (2, grubdir
,
1420 grub_install_mkdir_p (fldir
);
1421 flf
= grub_util_fopen (fl
, "w");
1423 grub_util_error (_("Can't create file: %s"), strerror (errno
));
1425 relfl
= grub_make_system_path_relative_to_its_root (fl
);
1426 fprintf (load_cfg_f
, "search.file %s root ",
1428 grub_install_push_module ("search_fs_file");
1430 for (curdev
= grub_devices
, curdrive
= grub_drives
; *curdev
; curdev
++,
1436 if (curdrive
== grub_drives
)
1439 dev
= grub_device_open (*curdrive
);
1443 if (dev
->disk
->dev
->id
!= GRUB_DISK_DEVICE_HOSTDISK_ID
)
1445 grub_util_fprint_full_disk_name (load_cfg_f
,
1448 fprintf (load_cfg_f
, " ");
1449 if (dev
!= grub_dev
)
1450 grub_device_close (dev
);
1454 map
= grub_util_biosdisk_get_compatibility_hint (dev
->disk
);
1458 grub_util_fprint_full_disk_name (load_cfg_f
, map
, dev
);
1459 fprintf (load_cfg_f
, " ");
1463 if (disk_module
&& disk_module
[0]
1464 && grub_strcmp (disk_module
, "biosdisk") != 0)
1465 g
= grub_util_guess_baremetal_drive (*curdev
);
1469 case GRUB_INSTALL_PLATFORM_I386_PC
:
1470 g
= grub_util_guess_bios_drive (*curdev
);
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
);
1482 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
1483 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
1484 case GRUB_INSTALL_PLATFORM_I386_IEEE1275
:
1486 const char * ofpath
= grub_util_devname_to_ofpath (*curdev
);
1487 g
= xasprintf ("ieee1275/%s", ofpath
);
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
);
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"));
1507 /* pacify warning. */
1508 case GRUB_INSTALL_PLATFORM_MAX
:
1513 grub_util_fprint_full_disk_name (load_cfg_f
, g
, dev
);
1514 fprintf (load_cfg_f
, " ");
1517 if (dev
!= grub_dev
)
1518 grub_device_close (dev
);
1520 fprintf (load_cfg_f
, "\n");
1521 char *escaped_relpath
= escape (relative_grubdir
);
1522 fprintf (load_cfg_f
, "set prefix=($root)'%s'\n",
1527 /* We need to hardcode the partition number in the core image's prefix. */
1529 for (p
= grub_drives
[0]; *p
; )
1531 if (*p
== '\\' && p
[1])
1536 if (*p
== ',' || *p
== '\0')
1540 prefix_drive
= xasprintf ("(%s)", p
);
1545 if (config
.is_cryptodisk_enabled
)
1548 probe_cryptodisk_uuid (grub_dev
->disk
);
1550 for (curdrive
= grub_drives
+ 1; *curdrive
; curdrive
++)
1552 grub_device_t dev
= grub_device_open (*curdrive
);
1556 probe_cryptodisk_uuid (dev
->disk
);
1557 grub_device_close (dev
);
1560 prefix_drive
= xasprintf ("(%s)", grub_drives
[0]);
1563 char mkimage_target
[200];
1564 const char *core_name
= NULL
;
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
),
1579 grub_install_get_platform_cpu (platform
),
1580 grub_install_get_platform_platform (platform
));
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
),
1588 grub_install_get_platform_cpu (platform
),
1589 grub_install_get_platform_platform (platform
));
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
),
1603 grub_install_get_platform_cpu (platform
),
1604 grub_install_get_platform_platform (platform
));
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
),
1615 grub_install_get_platform_cpu (platform
),
1616 grub_install_get_platform_platform (platform
));
1617 core_name
= "core.img";
1619 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
1620 strcpy (mkimage_target
, "sparc64-ieee1275-raw");
1621 core_name
= "core.img";
1623 /* pacify warning. */
1624 case GRUB_INSTALL_PLATFORM_MAX
:
1629 grub_util_error ("%s", _("You've found a bug"));
1632 fclose (load_cfg_f
);
1634 char *imgfile
= grub_util_path_concat (2, platdir
,
1636 char *prefix
= xasprintf ("%s%s", prefix_drive
? : "",
1638 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory
,
1640 /* output */ imgfile
,
1642 have_load_cfg
? load_cfg
: NULL
,
1643 /* image target */ mkimage_target
, 0);
1644 /* Backward-compatibility kludges. */
1647 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON
:
1649 char *dst
= grub_util_path_concat (2, bootdir
, "grub.elf");
1650 grub_install_copy_file (imgfile
, dst
, 1);
1655 case GRUB_INSTALL_PLATFORM_I386_IEEE1275
:
1656 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
1658 char *dst
= grub_util_path_concat (2, grubdir
, "grub");
1659 grub_install_copy_file (imgfile
, dst
, 1);
1664 case GRUB_INSTALL_PLATFORM_I386_EFI
:
1665 case GRUB_INSTALL_PLATFORM_X86_64_EFI
:
1667 char *dst
= grub_util_path_concat (2, platdir
, "grub.efi");
1668 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory
,
1672 have_load_cfg
? load_cfg
: NULL
,
1673 /* image target */ mkimage_target
, 0);
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
:
1697 /* pacify warning. */
1698 case GRUB_INSTALL_PLATFORM_MAX
:
1702 /* Perform the platform-dependent install */
1706 case GRUB_INSTALL_PLATFORM_I386_PC
:
1708 char *boot_img_src
= grub_util_path_concat (2,
1709 grub_install_source_directory
,
1711 char *boot_img
= grub_util_path_concat (2, platdir
,
1713 grub_install_copy_file (boot_img_src
, boot_img
, 1);
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" : "",
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
);
1735 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275
:
1737 char *boot_img_src
= grub_util_path_concat (2,
1738 grub_install_source_directory
,
1740 char *boot_img
= grub_util_path_concat (2, platdir
,
1742 grub_install_copy_file (boot_img_src
, boot_img
, 1);
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" : "",
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
,
1763 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275
:
1766 char *core_services
= grub_util_path_concat (4, macppcdir
,
1767 "System", "Library",
1769 char *mach_kernel
= grub_util_path_concat (2, macppcdir
,
1771 char *grub_elf
, *bootx
;
1773 grub_device_t ins_dev
;
1774 char *grub_chrp
= grub_util_path_concat (2,
1775 grub_install_source_directory
,
1778 grub_install_mkdir_p (core_services
);
1780 bootx
= grub_util_path_concat (2, core_services
, "BootX");
1781 grub_install_copy_file (grub_chrp
, bootx
, 1);
1783 grub_elf
= grub_util_path_concat (2, core_services
, "grub.elf");
1784 grub_install_copy_file (imgfile
, grub_elf
, 1);
1786 f
= grub_util_fopen (mach_kernel
, "a+");
1788 grub_util_error (_("Can't create file: %s"), strerror (errno
));
1791 fill_core_services (core_services
);
1793 ins_dev
= grub_device_open (install_drive
);
1795 bless (ins_dev
, core_services
, 0);
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
,
1808 grub_device_close (ins_dev
);
1815 /* If a install device is defined, copy the core.elf to PReP partition. */
1816 if (is_prep
&& install_device
&& install_device
[0])
1818 grub_device_t ins_dev
;
1819 ins_dev
= grub_device_open (install_drive
);
1820 if (!ins_dev
|| !is_prep_partition (ins_dev
))
1822 grub_util_error ("%s", _("the chosen partition is not a PReP partition"));
1824 if (is_prep_empty (ins_dev
))
1826 if (write_to_disk (ins_dev
, imgfile
))
1827 grub_util_error ("%s", _("failed to copy Grub to the PReP partition"));
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'"),
1835 grub_device_close (ins_dev
);
1837 grub_install_register_ieee1275 (1, grub_util_get_os_disk (install_device
),
1842 case GRUB_INSTALL_PLATFORM_I386_IEEE1275
:
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
,
1856 case GRUB_INSTALL_PLATFORM_MIPS_ARC
:
1857 grub_install_sgi_setup (install_device
, imgfile
, "grub");
1860 case GRUB_INSTALL_PLATFORM_I386_EFI
:
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);
1869 case GRUB_INSTALL_PLATFORM_X86_64_EFI
:
1873 char *core_services
= grub_util_path_concat (4, efidir
,
1874 "System", "Library",
1876 char *mach_kernel
= grub_util_path_concat (2, efidir
,
1879 grub_device_t ins_dev
;
1881 grub_install_mkdir_p (core_services
);
1883 boot_efi
= grub_util_path_concat (2, core_services
, "boot.efi");
1884 grub_install_copy_file (imgfile
, boot_efi
, 1);
1886 f
= grub_util_fopen (mach_kernel
, "r+");
1888 grub_util_error (_("Can't create file: %s"), strerror (errno
));
1891 fill_core_services(core_services
);
1893 ins_dev
= grub_device_open (install_drive
);
1895 bless (ins_dev
, boot_efi
, 1);
1896 if (!removable
&& update_nvram
)
1898 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1900 ret
= grub_install_register_efi (efidir_grub_dev
,
1901 "\\System\\Library\\CoreServices",
1904 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
1908 grub_device_close (ins_dev
);
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
:
1921 char *dst
= grub_util_path_concat (2, efidir
, efi_file
);
1922 grub_install_copy_file (imgfile
, dst
, 1);
1925 if (!removable
&& update_nvram
)
1927 char * efifile_path
;
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",
1937 part
= (efidir_grub_dev
->disk
->partition
1938 ? grub_partition_get_name (efidir_grub_dev
->disk
->partition
)
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
? : ""));
1946 ret
= grub_install_register_efi (efidir_grub_dev
,
1947 efifile_path
, efi_distributor
);
1949 grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
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"));
1969 /* pacify warning. */
1970 case GRUB_INSTALL_PLATFORM_MAX
:
1974 fprintf (stderr
, "%s\n", _("Installation finished. No error reported."));
1976 /* Free resources. */
1977 grub_gcry_fini_all ();