Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-install.c
blobcedc1880901121083892466c7c732d0d9fe06c43
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
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/>.
19 #include <config.h>
20 #include <grub/types.h>
21 #include <grub/emu/misc.h>
22 #include <grub/util/misc.h>
23 #include <grub/misc.h>
24 #include <grub/device.h>
25 #include <grub/disk.h>
26 #include <grub/file.h>
27 #include <grub/fs.h>
28 #include <grub/env.h>
29 #include <grub/term.h>
30 #include <grub/mm.h>
31 #include <grub/lib/hexdump.h>
32 #include <grub/crypto.h>
33 #include <grub/command.h>
34 #include <grub/i18n.h>
35 #include <grub/zfs/zfs.h>
36 #include <grub/util/install.h>
37 #include <grub/emu/getroot.h>
38 #include <grub/diskfilter.h>
39 #include <grub/cryptodisk.h>
40 #include <grub/legacy_parse.h>
41 #include <grub/gpt_partition.h>
42 #include <grub/emu/config.h>
43 #include <grub/util/ofpath.h>
44 #include <grub/hfsplus.h>
46 #include <string.h>
48 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
49 #pragma GCC diagnostic ignored "-Wmissing-declarations"
50 #include <argp.h>
51 #pragma GCC diagnostic error "-Wmissing-prototypes"
52 #pragma GCC diagnostic error "-Wmissing-declarations"
54 #include "progname.h"
56 static char *target;
57 static int removable = 0;
58 static int recheck = 0;
59 static int update_nvram = 1;
60 static char *install_device = NULL;
61 static char *debug_image = NULL;
62 static char *rootdir = NULL;
63 static char *bootdir = NULL;
64 static int allow_floppy = 0;
65 static int force_file_id = 0;
66 static char *disk_module = NULL;
67 static char *efidir = NULL;
68 static char *macppcdir = NULL;
69 static int force = 0;
70 static int have_abstractions = 0;
71 static int have_cryptodisk = 0;
72 static char * bootloader_id;
73 static int have_load_cfg = 0;
74 static FILE * load_cfg_f = NULL;
75 static char *load_cfg;
76 static int install_bootsector = 1;
77 static char *label_font;
78 static char *label_color;
79 static char *label_bgcolor;
80 static char *product_version;
81 static int add_rs_codes = 1;
83 enum
85 OPTION_BOOT_DIRECTORY = 0x301,
86 OPTION_ROOT_DIRECTORY,
87 OPTION_TARGET,
88 OPTION_SETUP,
89 OPTION_MKRELPATH,
90 OPTION_MKDEVICEMAP,
91 OPTION_PROBE,
92 OPTION_EDITENV,
93 OPTION_ALLOW_FLOPPY,
94 OPTION_RECHECK,
95 OPTION_FORCE,
96 OPTION_FORCE_FILE_ID,
97 OPTION_NO_NVRAM,
98 OPTION_REMOVABLE,
99 OPTION_BOOTLOADER_ID,
100 OPTION_EFI_DIRECTORY,
101 OPTION_FONT,
102 OPTION_DEBUG,
103 OPTION_DEBUG_IMAGE,
104 OPTION_NO_FLOPPY,
105 OPTION_DISK_MODULE,
106 OPTION_NO_BOOTSECTOR,
107 OPTION_NO_RS_CODES,
108 OPTION_MACPPC_DIRECTORY,
109 OPTION_LABEL_FONT,
110 OPTION_LABEL_COLOR,
111 OPTION_LABEL_BGCOLOR,
112 OPTION_PRODUCT_VERSION
115 static int fs_probe = 1;
117 static error_t
118 argp_parser (int key, char *arg, struct argp_state *state)
120 if (grub_install_parse (key, arg))
121 return 0;
122 switch (key)
124 case OPTION_FORCE_FILE_ID:
125 force_file_id = 1;
126 return 0;
127 case 's':
128 fs_probe = 0;
129 return 0;
131 case OPTION_SETUP:
132 if (!grub_strstr (arg, "setup"))
133 install_bootsector = 0;
134 return 0;
136 case OPTION_PRODUCT_VERSION:
137 free (product_version);
138 product_version = xstrdup (arg);
139 return 0;
140 case OPTION_LABEL_FONT:
141 free (label_font);
142 label_font = xstrdup (arg);
143 return 0;
145 case OPTION_LABEL_COLOR:
146 free (label_color);
147 label_color = xstrdup (arg);
148 return 0;
150 case OPTION_LABEL_BGCOLOR:
151 free (label_bgcolor);
152 label_bgcolor = xstrdup (arg);
153 return 0;
155 /* Accept and ignore for compatibility. */
156 case OPTION_FONT:
157 case OPTION_MKRELPATH:
158 case OPTION_PROBE:
159 case OPTION_EDITENV:
160 case OPTION_MKDEVICEMAP:
161 case OPTION_NO_FLOPPY:
162 return 0;
163 case OPTION_ROOT_DIRECTORY:
164 /* Accept for compatibility. */
165 free (rootdir);
166 rootdir = xstrdup (arg);
167 return 0;
169 case OPTION_BOOT_DIRECTORY:
170 free (bootdir);
171 bootdir = xstrdup (arg);
172 return 0;
174 case OPTION_MACPPC_DIRECTORY:
175 free (macppcdir);
176 macppcdir = xstrdup (arg);
177 return 0;
179 case OPTION_EFI_DIRECTORY:
180 free (efidir);
181 efidir = xstrdup (arg);
182 return 0;
184 case OPTION_DISK_MODULE:
185 free (disk_module);
186 disk_module = xstrdup (arg);
187 return 0;
189 case OPTION_TARGET:
190 free (target);
191 target = xstrdup (arg);
192 return 0;
194 case OPTION_DEBUG_IMAGE:
195 free (debug_image);
196 debug_image = xstrdup (arg);
197 return 0;
199 case OPTION_NO_NVRAM:
200 update_nvram = 0;
201 return 0;
203 case OPTION_FORCE:
204 force = 1;
205 return 0;
207 case OPTION_RECHECK:
208 recheck = 1;
209 return 0;
211 case OPTION_REMOVABLE:
212 removable = 1;
213 return 0;
215 case OPTION_ALLOW_FLOPPY:
216 allow_floppy = 1;
217 return 0;
219 case OPTION_NO_BOOTSECTOR:
220 install_bootsector = 0;
221 return 0;
223 case OPTION_NO_RS_CODES:
224 add_rs_codes = 0;
225 return 0;
227 case OPTION_DEBUG:
228 verbosity++;
229 return 0;
231 case OPTION_BOOTLOADER_ID:
232 free (bootloader_id);
233 bootloader_id = xstrdup (arg);
234 return 0;
236 case ARGP_KEY_ARG:
237 if (install_device)
238 grub_util_error ("%s", _("More than one install device?"));
239 install_device = xstrdup (arg);
240 return 0;
242 default:
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},
305 {0, 0, 0, 0, 0, 0}
308 static const char *
309 get_default_platform (void)
311 #ifdef __powerpc__
312 return "powerpc-ieee1275";
313 #elif defined (__sparc__) || defined (__sparc64__)
314 return "sparc64-ieee1275";
315 #elif defined (__MIPSEL__)
316 return "mipsel-loongson";
317 #elif defined (__MIPSEB__)
318 return "mips-arc";
319 #elif defined (__ia64__)
320 return "ia64-efi";
321 #elif defined (__arm__)
322 return "arm-uboot";
323 #elif defined (__aarch64__)
324 return "arm64-efi";
325 #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__)
326 return grub_install_get_default_x86_platform ();
327 #else
328 return NULL;
329 #endif
332 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
334 static char *
335 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
337 switch (key)
339 case OPTION_BOOT_DIRECTORY:
340 return xasprintf (text, GRUB_DIR_NAME, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME);
341 case OPTION_TARGET:
343 char *plats = grub_install_get_platforms_string ();
344 char *ret;
345 ret = xasprintf (text, get_default_platform (), plats);
346 free (plats);
347 return ret;
349 case ARGP_KEY_HELP_POST_DOC:
350 return xasprintf (text, program_name, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME);
351 default:
352 return grub_install_help_filter (key, text, input);
356 #pragma GCC diagnostic error "-Wformat-nonliteral"
358 /* TRANSLATORS: INSTALL_DEVICE isn't an identifier and is the DEVICE you
359 install to. */
360 struct argp argp = {
361 options, argp_parser, N_("[OPTION] [INSTALL_DEVICE]"),
362 N_("Install GRUB on your drive.")"\v"
363 N_("INSTALL_DEVICE must be system device filename.\n"
364 "%s copies GRUB images into %s. On some platforms, it"
365 " may also install GRUB into the boot sector."),
366 NULL, help_filter, NULL
369 static int
370 probe_raid_level (grub_disk_t disk)
372 /* disk might be NULL in the case of a LVM physical volume with no LVM
373 signature. Ignore such cases here. */
374 if (!disk)
375 return -1;
377 if (disk->dev->id != GRUB_DISK_DEVICE_DISKFILTER_ID)
378 return -1;
380 if (disk->name[0] != 'm' || disk->name[1] != 'd')
381 return -1;
383 if (!((struct grub_diskfilter_lv *) disk->data)->segments)
384 return -1;
385 return ((struct grub_diskfilter_lv *) disk->data)->segments->type;
388 static void
389 push_partmap_module (const char *map, void *data __attribute__ ((unused)))
391 char buf[50];
393 if (strcmp (map, "openbsd") == 0 || strcmp (map, "netbsd") == 0)
395 grub_install_push_module ("part_bsd");
396 return;
399 snprintf (buf, sizeof (buf), "part_%s", map);
400 grub_install_push_module (buf);
403 static void
404 push_cryptodisk_module (const char *mod, void *data __attribute__ ((unused)))
406 grub_install_push_module (mod);
409 static void
410 probe_mods (grub_disk_t disk)
412 grub_partition_t part;
413 grub_disk_memberlist_t list = NULL, tmp;
414 int raid_level;
416 if (disk->partition == NULL)
417 grub_util_info ("no partition map found for %s", disk->name);
419 for (part = disk->partition; part; part = part->parent)
420 push_partmap_module (part->partmap->name, NULL);
422 if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID)
424 grub_diskfilter_get_partmap (disk, push_partmap_module, NULL);
425 have_abstractions = 1;
428 if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
429 && (grub_memcmp (disk->name, "lvm/", sizeof ("lvm/") - 1) == 0 ||
430 grub_memcmp (disk->name, "lvmid/", sizeof ("lvmid/") - 1) == 0))
431 grub_install_push_module ("lvm");
433 if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
434 && grub_memcmp (disk->name, "ldm/", sizeof ("ldm/") - 1) == 0)
435 grub_install_push_module ("ldm");
437 if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
439 grub_util_cryptodisk_get_abstraction (disk,
440 push_cryptodisk_module, NULL);
441 have_abstractions = 1;
442 have_cryptodisk = 1;
445 raid_level = probe_raid_level (disk);
446 if (raid_level >= 0)
448 grub_install_push_module ("diskfilter");
449 if (disk->dev->raidname)
450 grub_install_push_module (disk->dev->raidname (disk));
452 if (raid_level == 5)
453 grub_install_push_module ("raid5rec");
454 if (raid_level == 6)
455 grub_install_push_module ("raid6rec");
457 /* In case of LVM/RAID, check the member devices as well. */
458 if (disk->dev->memberlist)
459 list = disk->dev->memberlist (disk);
460 while (list)
462 probe_mods (list->disk);
463 tmp = list->next;
464 free (list);
465 list = tmp;
469 static int
470 have_bootdev (enum grub_install_plat pl)
472 switch (pl)
474 case GRUB_INSTALL_PLATFORM_I386_PC:
475 case GRUB_INSTALL_PLATFORM_I386_EFI:
476 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
477 case GRUB_INSTALL_PLATFORM_IA64_EFI:
478 case GRUB_INSTALL_PLATFORM_ARM_EFI:
479 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
480 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
481 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
482 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
483 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
484 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
485 return 1;
487 case GRUB_INSTALL_PLATFORM_I386_QEMU:
488 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
489 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
490 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
491 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
493 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
494 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
496 case GRUB_INSTALL_PLATFORM_I386_XEN:
497 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
498 return 0;
500 /* pacify warning. */
501 case GRUB_INSTALL_PLATFORM_MAX:
502 return 0;
504 return 0;
507 static void
508 probe_cryptodisk_uuid (grub_disk_t disk)
510 grub_disk_memberlist_t list = NULL, tmp;
512 /* In case of LVM/RAID, check the member devices as well. */
513 if (disk->dev->memberlist)
515 list = disk->dev->memberlist (disk);
517 while (list)
519 probe_cryptodisk_uuid (list->disk);
520 tmp = list->next;
521 free (list);
522 list = tmp;
524 if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
526 const char *uuid = grub_util_cryptodisk_get_uuid (disk);
527 if (!load_cfg_f)
528 load_cfg_f = grub_util_fopen (load_cfg, "wb");
529 have_load_cfg = 1;
531 fprintf (load_cfg_f, "cryptomount -u %s\n",
532 uuid);
536 static int
537 is_same_disk (const char *a, const char *b)
539 while (1)
541 if ((*a == ',' || *a == '\0') && (*b == ',' || *b == '\0'))
542 return 1;
543 if (*a != *b)
544 return 0;
545 if (*a == '\\')
547 if (a[1] != b[1])
548 return 0;
549 a += 2;
550 b += 2;
551 continue;
553 a++;
554 b++;
558 static char *
559 get_rndstr (void)
561 grub_uint8_t rnd[15];
562 const size_t sz = sizeof (rnd) * GRUB_CHAR_BIT / 5;
563 char * ret = xmalloc (sz + 1);
564 size_t i;
565 if (grub_get_random (rnd, sizeof (rnd)))
566 grub_util_error ("%s", _("couldn't retrieve random data"));
567 for (i = 0; i < sz; i++)
569 grub_size_t b = i * 5;
570 grub_uint8_t r;
571 grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
572 grub_size_t f2;
573 if (f1 > 5)
574 f1 = 5;
575 f2 = 5 - f1;
576 r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
577 if (f2)
578 r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
579 if (r < 10)
580 ret[i] = '0' + r;
581 else
582 ret[i] = 'a' + (r - 10);
584 ret[sz] = '\0';
585 return ret;
588 static char *
589 escape (const char *in)
591 char *ptr;
592 char *ret;
593 int overhead = 0;
595 for (ptr = (char*)in; *ptr; ptr++)
596 if (*ptr == '\'')
597 overhead += 3;
598 ret = grub_malloc (ptr - in + overhead + 1);
599 if (!ret)
600 return NULL;
602 grub_strchrsub (ret, in, '\'', "'\\''");
603 return ret;
606 static struct grub_util_config config;
608 static void
609 device_map_check_duplicates (const char *dev_map)
611 FILE *fp;
612 char buf[1024]; /* XXX */
613 size_t alloced = 8;
614 size_t filled = 0;
615 char **d;
616 size_t i;
618 if (dev_map[0] == '\0')
619 return;
621 fp = grub_util_fopen (dev_map, "r");
622 if (! fp)
623 return;
625 d = xmalloc (alloced * sizeof (d[0]));
627 while (fgets (buf, sizeof (buf), fp))
629 char *p = buf;
630 char *e;
632 /* Skip leading spaces. */
633 while (*p && grub_isspace (*p))
634 p++;
636 /* If the first character is `#' or NUL, skip this line. */
637 if (*p == '\0' || *p == '#')
638 continue;
640 if (*p != '(')
641 continue;
643 p++;
645 e = p;
646 p = strchr (p, ')');
647 if (! p)
648 continue;
650 if (filled >= alloced)
652 alloced *= 2;
653 d = xrealloc (d, alloced * sizeof (d[0]));
656 *p = '\0';
658 d[filled++] = xstrdup (e);
661 fclose (fp);
663 qsort (d, filled, sizeof (d[0]), grub_qsort_strcmp);
665 for (i = 0; i + 1 < filled; i++)
666 if (strcmp (d[i], d[i+1]) == 0)
668 grub_util_error (_("the drive %s is defined multiple times in the device map %s"),
669 d[i], dev_map);
672 for (i = 0; i < filled; i++)
673 free (d[i]);
675 free (d);
678 static grub_err_t
679 write_to_disk (grub_device_t dev, const char *fn)
681 char *core_img;
682 size_t core_size;
683 grub_err_t err;
685 core_size = grub_util_get_image_size (fn);
687 core_img = grub_util_read_image (fn);
689 grub_util_info ("writing `%s' to `%s'", fn, dev->disk->name);
690 err = grub_disk_write (dev->disk, 0, 0,
691 core_size, core_img);
692 free (core_img);
693 return err;
696 static int
697 is_prep_partition (grub_device_t dev)
699 if (!dev->disk)
700 return 0;
701 if (!dev->disk->partition)
702 return 0;
703 if (strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
704 return (dev->disk->partition->msdostype == 0x41);
706 if (strcmp (dev->disk->partition->partmap->name, "gpt") == 0)
708 struct grub_gpt_partentry gptdata;
709 grub_partition_t p = dev->disk->partition;
710 int ret = 0;
711 dev->disk->partition = dev->disk->partition->parent;
713 if (grub_disk_read (dev->disk, p->offset, p->index,
714 sizeof (gptdata), &gptdata) == 0)
716 const grub_gpt_part_type_t template = {
717 grub_cpu_to_le32_compile_time (0x9e1a2d38),
718 grub_cpu_to_le16_compile_time (0xc612),
719 grub_cpu_to_le16_compile_time (0x4316),
720 { 0xaa, 0x26, 0x8b, 0x49, 0x52, 0x1e, 0x5a, 0x8b }
723 ret = grub_memcmp (&template, &gptdata.type,
724 sizeof (template)) == 0;
726 dev->disk->partition = p;
727 return ret;
730 return 0;
733 static int
734 is_prep_empty (grub_device_t dev)
736 grub_disk_addr_t dsize, addr;
737 grub_uint32_t buffer[32768];
739 dsize = grub_disk_get_size (dev->disk);
740 for (addr = 0; addr < dsize;
741 addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
743 grub_size_t sz = sizeof (buffer);
744 grub_uint32_t *ptr;
746 if (sizeof (buffer) / GRUB_DISK_SECTOR_SIZE > dsize - addr)
747 sz = (dsize - addr) * GRUB_DISK_SECTOR_SIZE;
748 grub_disk_read (dev->disk, addr, 0, sz, buffer);
750 if (addr == 0 && grub_memcmp (buffer, ELFMAG, SELFMAG) == 0)
751 return 1;
753 for (ptr = buffer; ptr < buffer + sz / sizeof (*buffer); ptr++)
754 if (*ptr)
755 return 0;
758 return 1;
761 static void
762 bless (grub_device_t dev, const char *path, int x86)
764 struct stat st;
765 grub_err_t err;
767 grub_util_info ("blessing %s", path);
769 if (stat (path, &st) < 0)
770 grub_util_error (N_("cannot stat `%s': %s"),
771 path, strerror (errno));
773 err = grub_mac_bless_inode (dev, st.st_ino, S_ISDIR (st.st_mode), x86);
774 if (err)
775 grub_util_error ("%s", grub_errmsg);
776 grub_util_info ("blessed\n");
779 static void
780 fill_core_services (const char *core_services)
782 char *label;
783 FILE *f;
784 char *label_text;
785 char *label_string = xasprintf ("%s %s", bootloader_id, product_version);
786 char *sysv_plist;
788 label = grub_util_path_concat (2, core_services, ".disk_label");
789 grub_util_info ("rendering label %s", label_string);
790 grub_util_render_label (label_font, label_bgcolor ? : "white",
791 label_color ? : "black", label_string, label);
792 grub_util_info ("label rendered");
793 free (label);
794 label_text = grub_util_path_concat (2, core_services, ".disk_label.contentDetails");
795 f = grub_util_fopen (label_text, "wb");
796 fprintf (f, "%s\n", label_string);
797 fclose (f);
798 free (label_string);
799 free (label_text);
801 sysv_plist = grub_util_path_concat (2, core_services, "SystemVersion.plist");
802 f = grub_util_fopen (sysv_plist, "wb");
803 fprintf (f,
804 "<plist version=\"1.0\">\n"
805 "<dict>\n"
806 " <key>ProductBuildVersion</key>\n"
807 " <string></string>\n"
808 " <key>ProductName</key>\n"
809 " <string>%s</string>\n"
810 " <key>ProductVersion</key>\n"
811 " <string>%s</string>\n"
812 "</dict>\n"
813 "</plist>\n", bootloader_id, product_version);
814 fclose (f);
815 free (sysv_plist);
819 main (int argc, char *argv[])
821 int is_efi = 0;
822 const char *efi_distributor = NULL;
823 const char *efi_file = NULL;
824 char **grub_devices;
825 grub_fs_t grub_fs;
826 grub_device_t grub_dev = NULL;
827 enum grub_install_plat platform;
828 char *grubdir, *device_map;
829 char **curdev, **curdrive;
830 char **grub_drives;
831 char *relative_grubdir;
832 char **efidir_device_names = NULL;
833 grub_device_t efidir_grub_dev = NULL;
834 char *efidir_grub_devname;
835 int efidir_is_mac = 0;
836 int is_prep = 0;
837 const char *pkgdatadir;
839 grub_util_host_init (&argc, &argv);
840 product_version = xstrdup (PACKAGE_VERSION);
841 pkgdatadir = grub_util_get_pkgdatadir ();
842 label_font = grub_util_path_concat (2, pkgdatadir, "unicode.pf2");
844 argp_parse (&argp, argc, argv, 0, 0, 0);
846 if (verbosity > 1)
847 grub_env_set ("debug", "all");
849 grub_util_load_config (&config);
851 if (!bootloader_id && config.grub_distributor)
853 char *ptr;
854 bootloader_id = xstrdup (config.grub_distributor);
855 for (ptr = bootloader_id; *ptr && *ptr != ' '; ptr++)
856 if (*ptr >= 'A' && *ptr <= 'Z')
857 *ptr = *ptr - 'A' + 'a';
858 *ptr = '\0';
860 if (!bootloader_id || bootloader_id[0] == '\0')
862 free (bootloader_id);
863 bootloader_id = xstrdup ("grub");
866 if (!grub_install_source_directory)
868 if (!target)
870 const char * t;
871 t = get_default_platform ();
872 if (!t)
873 grub_util_error ("%s",
874 _("Unable to determine your platform."
875 " Use --target.")
877 target = xstrdup (t);
879 grub_install_source_directory
880 = grub_util_path_concat (2, grub_util_get_pkglibdir (), target);
883 platform = grub_install_get_target (grub_install_source_directory);
886 char *platname = grub_install_get_platform_name (platform);
887 fprintf (stderr, _("Installing for %s platform.\n"), platname);
888 free (platname);
891 switch (platform)
893 case GRUB_INSTALL_PLATFORM_I386_PC:
894 if (!disk_module)
895 disk_module = xstrdup ("biosdisk");
896 break;
897 case GRUB_INSTALL_PLATFORM_I386_EFI:
898 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
899 case GRUB_INSTALL_PLATFORM_ARM_EFI:
900 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
901 case GRUB_INSTALL_PLATFORM_IA64_EFI:
902 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
903 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
904 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
905 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
906 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
907 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
908 case GRUB_INSTALL_PLATFORM_I386_XEN:
909 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
910 break;
912 case GRUB_INSTALL_PLATFORM_I386_QEMU:
913 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
914 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
915 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
916 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
917 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
918 disk_module = xstrdup ("native");
919 break;
921 /* pacify warning. */
922 case GRUB_INSTALL_PLATFORM_MAX:
923 break;
926 switch (platform)
928 case GRUB_INSTALL_PLATFORM_I386_PC:
929 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
930 if (!install_device)
931 grub_util_error ("%s", _("install device isn't specified"));
932 break;
933 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
934 if (install_device)
935 is_prep = 1;
936 break;
937 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
938 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
939 break;
940 case GRUB_INSTALL_PLATFORM_I386_EFI:
941 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
942 case GRUB_INSTALL_PLATFORM_ARM_EFI:
943 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
944 case GRUB_INSTALL_PLATFORM_IA64_EFI:
945 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
946 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
947 case GRUB_INSTALL_PLATFORM_I386_QEMU:
948 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
949 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
950 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
951 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
952 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
953 case GRUB_INSTALL_PLATFORM_I386_XEN:
954 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
955 free (install_device);
956 install_device = NULL;
957 break;
959 /* pacify warning. */
960 case GRUB_INSTALL_PLATFORM_MAX:
961 break;
964 if (!bootdir)
965 bootdir = grub_util_path_concat (3, "/", rootdir, GRUB_BOOT_DIR_NAME);
968 char * t = grub_util_path_concat (2, bootdir, GRUB_DIR_NAME);
969 grub_install_mkdir_p (t);
970 grubdir = canonicalize_file_name (t);
971 if (!grubdir)
972 grub_util_error (_("failed to get canonical path of `%s'"), t);
973 free (t);
975 device_map = grub_util_path_concat (2, grubdir, "device.map");
977 if (recheck)
978 grub_util_unlink (device_map);
980 device_map_check_duplicates (device_map);
981 grub_util_biosdisk_init (device_map);
983 /* Initialize all modules. */
984 grub_init_all ();
985 grub_gcry_init_all ();
986 grub_hostfs_init ();
987 grub_host_init ();
989 switch (platform)
991 case GRUB_INSTALL_PLATFORM_I386_EFI:
992 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
993 case GRUB_INSTALL_PLATFORM_ARM_EFI:
994 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
995 case GRUB_INSTALL_PLATFORM_IA64_EFI:
996 is_efi = 1;
997 break;
998 default:
999 is_efi = 0;
1000 break;
1002 /* pacify warning. */
1003 case GRUB_INSTALL_PLATFORM_MAX:
1004 break;
1007 /* Find the EFI System Partition. */
1009 if (is_efi)
1011 grub_fs_t fs;
1012 free (install_device);
1013 install_device = NULL;
1014 if (!efidir)
1016 char *d = grub_util_path_concat (2, bootdir, "efi");
1017 char *dr = NULL;
1018 if (!grub_util_is_directory (d))
1020 free (d);
1021 d = grub_util_path_concat (2, bootdir, "EFI");
1024 The EFI System Partition may have been given directly using
1025 --root-directory.
1027 if (!grub_util_is_directory (d)
1028 && rootdir && grub_strcmp (rootdir, "/") != 0)
1030 free (d);
1031 d = xstrdup (rootdir);
1033 if (grub_util_is_directory (d))
1034 dr = grub_make_system_path_relative_to_its_root (d);
1035 /* Is it a mount point? */
1036 if (dr && dr[0] == '\0')
1037 efidir = d;
1038 else
1039 free (d);
1040 free (dr);
1042 if (!efidir)
1043 grub_util_error ("%s", _("cannot find EFI directory"));
1044 efidir_device_names = grub_guess_root_devices (efidir);
1045 if (!efidir_device_names || !efidir_device_names[0])
1046 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1047 efidir);
1048 install_device = efidir_device_names[0];
1050 for (curdev = efidir_device_names; *curdev; curdev++)
1051 grub_util_pull_device (*curdev);
1053 efidir_grub_devname = grub_util_get_grub_dev (efidir_device_names[0]);
1054 if (!efidir_grub_devname)
1055 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1056 efidir_device_names[0]);
1058 efidir_grub_dev = grub_device_open (efidir_grub_devname);
1059 if (! efidir_grub_dev)
1060 grub_util_error ("%s", grub_errmsg);
1062 fs = grub_fs_probe (efidir_grub_dev);
1063 if (! fs)
1064 grub_util_error ("%s", grub_errmsg);
1066 efidir_is_mac = 0;
1068 if (grub_strcmp (fs->name, "hfs") == 0
1069 || grub_strcmp (fs->name, "hfsplus") == 0)
1070 efidir_is_mac = 1;
1072 if (!efidir_is_mac && grub_strcmp (fs->name, "fat") != 0)
1073 grub_util_error (_("%s doesn't look like an EFI partition.\n"), efidir);
1075 /* The EFI specification requires that an EFI System Partition must
1076 contain an "EFI" subdirectory, and that OS loaders are stored in
1077 subdirectories below EFI. Vendors are expected to pick names that do
1078 not collide with other vendors. To minimise collisions, we use the
1079 name of our distributor if possible.
1081 char *t;
1082 efi_distributor = bootloader_id;
1083 if (removable)
1085 /* The specification makes stricter requirements of removable
1086 devices, in order that only one image can be automatically loaded
1087 from them. The image must always reside under /EFI/BOOT, and it
1088 must have a specific file name depending on the architecture.
1090 efi_distributor = "BOOT";
1091 switch (platform)
1093 case GRUB_INSTALL_PLATFORM_I386_EFI:
1094 efi_file = "BOOTIA32.EFI";
1095 break;
1096 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1097 efi_file = "BOOTX64.EFI";
1098 break;
1099 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1100 efi_file = "BOOTIA64.EFI";
1101 break;
1102 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1103 efi_file = "BOOTARM.EFI";
1104 break;
1105 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1106 efi_file = "BOOTAA64.EFI";
1107 break;
1108 default:
1109 grub_util_error ("%s", _("You've found a bug"));
1110 break;
1113 else
1115 /* It is convenient for each architecture to have a different
1116 efi_file, so that different versions can be installed in parallel.
1118 switch (platform)
1120 case GRUB_INSTALL_PLATFORM_I386_EFI:
1121 efi_file = "grubia32.efi";
1122 break;
1123 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1124 efi_file = "grubx64.efi";
1125 break;
1126 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1127 efi_file = "grubia64.efi";
1128 break;
1129 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1130 efi_file = "grubarm.efi";
1131 break;
1132 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1133 efi_file = "grubaa64.efi";
1134 break;
1135 default:
1136 efi_file = "grub.efi";
1137 break;
1140 t = grub_util_path_concat (3, efidir, "EFI", efi_distributor);
1141 free (efidir);
1142 efidir = t;
1143 grub_install_mkdir_p (efidir);
1146 if (platform == GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
1148 int is_guess = 0;
1149 if (!macppcdir)
1151 char *d;
1153 is_guess = 1;
1154 d = grub_util_path_concat (2, bootdir, "macppc");
1155 if (!grub_util_is_directory (d))
1157 free (d);
1158 d = grub_util_path_concat (2, bootdir, "efi");
1160 /* Find the Mac HFS(+) System Partition. */
1161 if (!grub_util_is_directory (d))
1163 free (d);
1164 d = grub_util_path_concat (2, bootdir, "EFI");
1166 if (!grub_util_is_directory (d))
1168 free (d);
1169 d = 0;
1171 if (d)
1172 macppcdir = d;
1174 if (macppcdir)
1176 char **macppcdir_device_names = NULL;
1177 grub_device_t macppcdir_grub_dev = NULL;
1178 char *macppcdir_grub_devname;
1179 grub_fs_t fs;
1181 macppcdir_device_names = grub_guess_root_devices (macppcdir);
1182 if (!macppcdir_device_names || !macppcdir_device_names[0])
1183 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1184 macppcdir);
1186 for (curdev = macppcdir_device_names; *curdev; curdev++)
1187 grub_util_pull_device (*curdev);
1189 macppcdir_grub_devname = grub_util_get_grub_dev (macppcdir_device_names[0]);
1190 if (!macppcdir_grub_devname)
1191 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1192 macppcdir_device_names[0]);
1194 macppcdir_grub_dev = grub_device_open (macppcdir_grub_devname);
1195 if (! macppcdir_grub_dev)
1196 grub_util_error ("%s", grub_errmsg);
1198 fs = grub_fs_probe (macppcdir_grub_dev);
1199 if (! fs)
1200 grub_util_error ("%s", grub_errmsg);
1202 if (grub_strcmp (fs->name, "hfs") != 0
1203 && grub_strcmp (fs->name, "hfsplus") != 0
1204 && !is_guess)
1205 grub_util_error (_("filesystem on %s is neither HFS nor HFS+"),
1206 macppcdir);
1207 if (grub_strcmp (fs->name, "hfs") == 0
1208 || grub_strcmp (fs->name, "hfsplus") == 0)
1210 install_device = macppcdir_device_names[0];
1211 is_prep = 0;
1216 grub_install_copy_files (grub_install_source_directory,
1217 grubdir, platform);
1219 char *envfile = grub_util_path_concat (2, grubdir, "grubenv");
1220 if (!grub_util_is_regular (envfile))
1221 grub_util_create_envblk_file (envfile);
1223 size_t ndev = 0;
1225 /* Write device to a variable so we don't have to traverse /dev every time. */
1226 grub_devices = grub_guess_root_devices (grubdir);
1227 if (!grub_devices || !grub_devices[0])
1228 grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
1229 grubdir);
1231 for (curdev = grub_devices; *curdev; curdev++)
1233 grub_util_pull_device (*curdev);
1234 ndev++;
1237 grub_drives = xmalloc (sizeof (grub_drives[0]) * (ndev + 1));
1239 for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
1240 curdrive++)
1242 *curdrive = grub_util_get_grub_dev (*curdev);
1243 if (! *curdrive)
1244 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1245 *curdev);
1247 *curdrive = 0;
1249 grub_dev = grub_device_open (grub_drives[0]);
1250 if (! grub_dev)
1251 grub_util_error ("%s", grub_errmsg);
1253 grub_fs = grub_fs_probe (grub_dev);
1254 if (! grub_fs)
1255 grub_util_error ("%s", grub_errmsg);
1257 grub_install_push_module (grub_fs->name);
1259 if (grub_dev->disk)
1260 probe_mods (grub_dev->disk);
1262 for (curdrive = grub_drives + 1; *curdrive; curdrive++)
1264 grub_device_t dev = grub_device_open (*curdrive);
1265 if (!dev)
1266 continue;
1267 if (dev->disk)
1268 probe_mods (dev->disk);
1269 grub_device_close (dev);
1272 if (!config.is_cryptodisk_enabled && have_cryptodisk)
1273 grub_util_error (_("attempt to install to encrypted disk without cryptodisk enabled. "
1274 "Set `%s' in file `%s'."), "GRUB_ENABLE_CRYPTODISK=y",
1275 grub_util_get_config_filename ());
1277 if (disk_module && grub_strcmp (disk_module, "ata") == 0)
1278 grub_install_push_module ("pata");
1279 else if (disk_module && grub_strcmp (disk_module, "native") == 0)
1281 grub_install_push_module ("pata");
1282 grub_install_push_module ("ahci");
1283 grub_install_push_module ("ohci");
1284 grub_install_push_module ("uhci");
1285 grub_install_push_module ("usbms");
1287 else if (disk_module && disk_module[0])
1288 grub_install_push_module (disk_module);
1290 relative_grubdir = grub_make_system_path_relative_to_its_root (grubdir);
1291 if (relative_grubdir[0] == '\0')
1293 free (relative_grubdir);
1294 relative_grubdir = xstrdup ("/");
1297 char *platname = grub_install_get_platform_name (platform);
1298 char *platdir;
1300 char *t = grub_util_path_concat (2, grubdir,
1301 platname);
1302 platdir = canonicalize_file_name (t);
1303 if (!platdir)
1304 grub_util_error (_("failed to get canonical path of `%s'"),
1306 free (t);
1308 load_cfg = grub_util_path_concat (2, platdir,
1309 "load.cfg");
1311 grub_util_unlink (load_cfg);
1313 if (debug_image && debug_image[0])
1315 load_cfg_f = grub_util_fopen (load_cfg, "wb");
1316 have_load_cfg = 1;
1317 fprintf (load_cfg_f, "set debug='%s'\n",
1318 debug_image);
1320 char *prefix_drive = NULL;
1321 char *install_drive = NULL;
1323 if (install_device)
1325 if (install_device[0] == '('
1326 && install_device[grub_strlen (install_device) - 1] == ')')
1328 size_t len = grub_strlen (install_device) - 2;
1329 install_drive = xmalloc (len + 1);
1330 memcpy (install_drive, install_device + 1, len);
1331 install_drive[len] = '\0';
1333 else
1335 grub_util_pull_device (install_device);
1336 install_drive = grub_util_get_grub_dev (install_device);
1337 if (!install_drive)
1338 grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
1339 install_device);
1343 if (!have_abstractions)
1345 if ((disk_module && grub_strcmp (disk_module, "biosdisk") != 0)
1346 || grub_drives[1]
1347 || (!install_drive
1348 && platform != GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
1349 || (install_drive && !is_same_disk (grub_drives[0], install_drive))
1350 || !have_bootdev (platform))
1352 char *uuid = NULL;
1353 /* generic method (used on coreboot and ata mod). */
1354 if (!force_file_id && grub_fs->uuid && grub_fs->uuid (grub_dev,
1355 &uuid))
1357 grub_print_error ();
1358 grub_errno = 0;
1359 uuid = NULL;
1362 if (!load_cfg_f)
1363 load_cfg_f = grub_util_fopen (load_cfg, "wb");
1364 have_load_cfg = 1;
1365 if (uuid)
1367 fprintf (load_cfg_f, "search.fs_uuid %s root ",
1368 uuid);
1369 grub_install_push_module ("search_fs_uuid");
1371 else
1373 char *rndstr = get_rndstr ();
1374 char *fl = grub_util_path_concat (3, grubdir,
1375 "uuid", rndstr);
1376 char *fldir = grub_util_path_concat (2, grubdir,
1377 "uuid");
1378 char *relfl;
1379 FILE *flf;
1380 grub_install_mkdir_p (fldir);
1381 flf = grub_util_fopen (fl, "w");
1382 if (!flf)
1383 grub_util_error (_("Can't create file: %s"), strerror (errno));
1384 fclose (flf);
1385 relfl = grub_make_system_path_relative_to_its_root (fl);
1386 fprintf (load_cfg_f, "search.file %s root ",
1387 relfl);
1388 grub_install_push_module ("search_fs_file");
1390 for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
1391 curdrive++)
1393 const char *map;
1394 char *g = NULL;
1395 grub_device_t dev;
1396 if (curdrive == grub_drives)
1397 dev = grub_dev;
1398 else
1399 dev = grub_device_open (*curdrive);
1400 if (!dev)
1401 continue;
1403 if (dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID)
1405 grub_util_fprint_full_disk_name (load_cfg_f,
1406 dev->disk->name,
1407 dev);
1408 fprintf (load_cfg_f, " ");
1409 if (dev != grub_dev)
1410 grub_device_close (dev);
1411 continue;
1414 map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
1416 if (map)
1418 grub_util_fprint_full_disk_name (load_cfg_f, map, dev);
1419 fprintf (load_cfg_f, " ");
1423 if (disk_module && disk_module[0]
1424 && grub_strcmp (disk_module, "biosdisk") != 0)
1425 g = grub_util_guess_baremetal_drive (*curdev);
1426 else
1427 switch (platform)
1429 case GRUB_INSTALL_PLATFORM_I386_PC:
1430 g = grub_util_guess_bios_drive (*curdev);
1431 break;
1432 case GRUB_INSTALL_PLATFORM_I386_EFI:
1433 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1434 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1435 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1436 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1437 g = grub_util_guess_efi_drive (*curdev);
1438 break;
1439 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1440 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1441 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1443 const char * ofpath = grub_util_devname_to_ofpath (*curdev);
1444 g = xasprintf ("ieee1275/%s", ofpath);
1445 break;
1447 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1448 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1449 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1450 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1451 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1452 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1453 g = grub_util_guess_baremetal_drive (*curdev);
1454 break;
1455 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1456 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1457 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1458 case GRUB_INSTALL_PLATFORM_I386_XEN:
1459 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1460 grub_util_warn ("%s", _("no hints available for your platform. Expect reduced performance"));
1461 break;
1462 /* pacify warning. */
1463 case GRUB_INSTALL_PLATFORM_MAX:
1464 break;
1466 if (g)
1468 grub_util_fprint_full_disk_name (load_cfg_f, g, dev);
1469 fprintf (load_cfg_f, " ");
1471 if (dev != grub_dev)
1472 grub_device_close (dev);
1474 fprintf (load_cfg_f, "\n");
1475 char *escaped_relpath = escape (relative_grubdir);
1476 fprintf (load_cfg_f, "set prefix=($root)'%s'\n",
1477 escaped_relpath);
1479 else
1481 /* We need to hardcode the partition number in the core image's prefix. */
1482 char *p;
1483 for (p = grub_drives[0]; *p; )
1485 if (*p == '\\' && p[1])
1487 p += 2;
1488 continue;
1490 if (*p == ',' || *p == '\0')
1491 break;
1492 p++;
1494 prefix_drive = xasprintf ("(%s)", p);
1497 else
1499 if (config.is_cryptodisk_enabled)
1501 if (grub_dev->disk)
1502 probe_cryptodisk_uuid (grub_dev->disk);
1504 for (curdrive = grub_drives + 1; *curdrive; curdrive++)
1506 grub_device_t dev = grub_device_open (*curdrive);
1507 if (!dev)
1508 continue;
1509 if (dev->disk)
1510 probe_cryptodisk_uuid (dev->disk);
1511 grub_device_close (dev);
1514 prefix_drive = xasprintf ("(%s)", grub_drives[0]);
1517 char mkimage_target[200];
1518 const char *core_name = NULL;
1520 switch (platform)
1522 case GRUB_INSTALL_PLATFORM_I386_EFI:
1523 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1524 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1525 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1526 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1527 core_name = "core.efi";
1528 snprintf (mkimage_target, sizeof (mkimage_target),
1529 "%s-%s",
1530 grub_install_get_platform_cpu (platform),
1531 grub_install_get_platform_platform (platform));
1532 break;
1533 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1534 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1535 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1536 core_name = "core.elf";
1537 snprintf (mkimage_target, sizeof (mkimage_target),
1538 "%s-%s-elf",
1539 grub_install_get_platform_cpu (platform),
1540 grub_install_get_platform_platform (platform));
1541 break;
1543 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1544 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1545 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1546 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1547 case GRUB_INSTALL_PLATFORM_I386_XEN:
1548 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1549 core_name = "core.elf";
1550 snprintf (mkimage_target, sizeof (mkimage_target),
1551 "%s-%s",
1552 grub_install_get_platform_cpu (platform),
1553 grub_install_get_platform_platform (platform));
1554 break;
1557 case GRUB_INSTALL_PLATFORM_I386_PC:
1558 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1559 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1560 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1561 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1562 snprintf (mkimage_target, sizeof (mkimage_target),
1563 "%s-%s",
1564 grub_install_get_platform_cpu (platform),
1565 grub_install_get_platform_platform (platform));
1566 core_name = "core.img";
1567 break;
1568 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1569 strcpy (mkimage_target, "sparc64-ieee1275-raw");
1570 core_name = "core.img";
1571 break;
1572 /* pacify warning. */
1573 case GRUB_INSTALL_PLATFORM_MAX:
1574 break;
1577 if (!core_name)
1578 grub_util_error ("%s", _("You've found a bug"));
1580 if (load_cfg_f)
1581 fclose (load_cfg_f);
1583 char *imgfile = grub_util_path_concat (2, platdir,
1584 core_name);
1585 char *prefix = xasprintf ("%s%s", prefix_drive ? : "",
1586 relative_grubdir);
1587 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
1588 /*prefix */ prefix,
1589 /* output */ imgfile,
1590 /* memdisk */ NULL,
1591 have_load_cfg ? load_cfg : NULL,
1592 /* image target */ mkimage_target, 0);
1593 /* Backward-compatibility kludges. */
1594 switch (platform)
1596 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1598 char *dst = grub_util_path_concat (2, bootdir, "grub.elf");
1599 grub_install_copy_file (imgfile, dst, 1);
1600 free (dst);
1602 break;
1604 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1605 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1607 char *dst = grub_util_path_concat (2, grubdir, "grub");
1608 grub_install_copy_file (imgfile, dst, 1);
1609 free (dst);
1611 break;
1613 case GRUB_INSTALL_PLATFORM_I386_EFI:
1614 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1616 char *dst = grub_util_path_concat (2, platdir, "grub.efi");
1617 grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
1618 /* prefix */ "",
1619 /* output */ dst,
1620 /* memdisk */ NULL,
1621 have_load_cfg ? load_cfg : NULL,
1622 /* image target */ mkimage_target, 0);
1624 break;
1625 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1626 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1627 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1628 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1629 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1630 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1631 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1632 case GRUB_INSTALL_PLATFORM_I386_PC:
1633 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1634 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1635 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1636 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1637 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1638 case GRUB_INSTALL_PLATFORM_I386_XEN:
1639 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1640 break;
1641 /* pacify warning. */
1642 case GRUB_INSTALL_PLATFORM_MAX:
1643 break;
1646 /* Perform the platform-dependent install */
1648 switch (platform)
1650 case GRUB_INSTALL_PLATFORM_I386_PC:
1652 char *boot_img_src = grub_util_path_concat (2,
1653 grub_install_source_directory,
1654 "boot.img");
1655 char *boot_img = grub_util_path_concat (2, platdir,
1656 "boot.img");
1657 grub_install_copy_file (boot_img_src, boot_img, 1);
1659 grub_util_info ("%sgrub-bios-setup %s %s %s %s %s --directory='%s' --device-map='%s' '%s'",
1660 /* TRANSLATORS: This is a prefix in the log to indicate that usually
1661 a command would be executed but due to an option was skipped. */
1662 install_bootsector ? "" : _("NOT RUNNING: "),
1663 allow_floppy ? "--allow-floppy " : "",
1664 verbosity ? "--verbose " : "",
1665 force ? "--force " : "",
1666 !fs_probe ? "--skip-fs-probe" : "",
1667 !add_rs_codes ? "--no-rs-codes" : "",
1668 platdir,
1669 device_map,
1670 install_device);
1672 /* Now perform the installation. */
1673 if (install_bootsector)
1674 grub_util_bios_setup (platdir, "boot.img", "core.img",
1675 install_drive, force,
1676 fs_probe, allow_floppy, add_rs_codes);
1677 break;
1679 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
1681 char *boot_img_src = grub_util_path_concat (2,
1682 grub_install_source_directory,
1683 "boot.img");
1684 char *boot_img = grub_util_path_concat (2, platdir,
1685 "boot.img");
1686 grub_install_copy_file (boot_img_src, boot_img, 1);
1688 grub_util_info ("%sgrub-sparc64-setup %s %s %s %s --directory='%s' --device-map='%s' '%s'",
1689 install_bootsector ? "" : "NOT RUNNING: ",
1690 allow_floppy ? "--allow-floppy " : "",
1691 verbosity ? "--verbose " : "",
1692 force ? "--force " : "",
1693 !fs_probe ? "--skip-fs-probe" : "",
1694 platdir,
1695 device_map,
1696 install_drive);
1698 /* Now perform the installation. */
1699 if (install_bootsector)
1700 grub_util_sparc_setup (platdir, "boot.img", "core.img",
1701 install_device, force,
1702 fs_probe, allow_floppy,
1703 0 /* unused */ );
1704 break;
1707 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
1708 if (macppcdir)
1710 char *core_services = grub_util_path_concat (4, macppcdir,
1711 "System", "Library",
1712 "CoreServices");
1713 char *mach_kernel = grub_util_path_concat (2, macppcdir,
1714 "mach_kernel");
1715 char *grub_elf, *bootx;
1716 FILE *f;
1717 grub_device_t ins_dev;
1718 char *grub_chrp = grub_util_path_concat (2,
1719 grub_install_source_directory,
1720 "grub.chrp");
1722 grub_install_mkdir_p (core_services);
1724 bootx = grub_util_path_concat (2, core_services, "BootX");
1725 grub_install_copy_file (grub_chrp, bootx, 1);
1727 grub_elf = grub_util_path_concat (2, core_services, "grub.elf");
1728 grub_install_copy_file (imgfile, grub_elf, 1);
1730 f = grub_util_fopen (mach_kernel, "r+");
1731 if (!f)
1732 grub_util_error (_("Can't create file: %s"), strerror (errno));
1733 fclose (f);
1735 fill_core_services (core_services);
1737 ins_dev = grub_device_open (install_drive);
1739 bless (ins_dev, core_services, 0);
1741 if (update_nvram)
1743 const char *dev;
1744 int partno;
1746 partno = ins_dev->disk->partition
1747 ? ins_dev->disk->partition->number + 1 : 0;
1748 dev = grub_util_get_os_disk (install_device);
1749 grub_install_register_ieee1275 (0, dev, partno,
1750 "\\\\BootX");
1752 grub_device_close (ins_dev);
1753 free (grub_elf);
1754 free (bootx);
1755 free (mach_kernel);
1756 free (grub_chrp);
1757 break;
1759 /* If a install device is defined, copy the core.elf to PReP partition. */
1760 if (is_prep && install_device && install_device[0])
1762 grub_device_t ins_dev;
1763 ins_dev = grub_device_open (install_drive);
1764 if (!ins_dev || !is_prep_partition (ins_dev))
1766 grub_util_error ("%s", _("the chosen partition is not a PReP partition"));
1768 if (is_prep_empty (ins_dev))
1770 if (write_to_disk (ins_dev, imgfile))
1771 grub_util_error ("%s", _("failed to copy Grub to the PReP partition"));
1773 else
1775 char *s = xasprintf ("dd if=/dev/zero of=%s", install_device);
1776 grub_util_error (_("the PReP partition is not empty. If you are sure you want to use it, run dd to clear it: `%s'"),
1779 grub_device_close (ins_dev);
1780 if (update_nvram)
1781 grub_install_register_ieee1275 (1, grub_util_get_os_disk (install_device),
1782 0, NULL);
1783 break;
1785 /* fallthrough. */
1786 case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
1787 if (update_nvram)
1789 const char *dev;
1790 char *relpath;
1791 int partno;
1792 relpath = grub_make_system_path_relative_to_its_root (imgfile);
1793 partno = grub_dev->disk->partition
1794 ? grub_dev->disk->partition->number + 1 : 0;
1795 dev = grub_util_get_os_disk (grub_devices[0]);
1796 grub_install_register_ieee1275 (0, dev,
1797 partno, relpath);
1799 break;
1800 case GRUB_INSTALL_PLATFORM_MIPS_ARC:
1801 grub_install_sgi_setup (install_device, imgfile, "grub");
1802 break;
1804 case GRUB_INSTALL_PLATFORM_I386_EFI:
1805 if (!efidir_is_mac)
1807 char *dst = grub_util_path_concat (2, efidir, "grub.efi");
1808 /* For old macs. Suggested by Peter Jones. */
1809 grub_install_copy_file (imgfile, dst, 1);
1810 free (dst);
1812 /* Fallthrough. */
1813 case GRUB_INSTALL_PLATFORM_X86_64_EFI:
1814 if (efidir_is_mac)
1816 char *boot_efi;
1817 char *core_services = grub_util_path_concat (4, efidir,
1818 "System", "Library",
1819 "CoreServices");
1820 char *mach_kernel = grub_util_path_concat (2, efidir,
1821 "mach_kernel");
1822 FILE *f;
1823 grub_device_t ins_dev;
1825 grub_install_mkdir_p (core_services);
1827 boot_efi = grub_util_path_concat (2, core_services, "boot.efi");
1828 grub_install_copy_file (imgfile, boot_efi, 1);
1830 f = grub_util_fopen (mach_kernel, "r+");
1831 if (!f)
1832 grub_util_error (_("Can't create file: %s"), strerror (errno));
1833 fclose (f);
1835 fill_core_services(core_services);
1837 ins_dev = grub_device_open (install_drive);
1839 bless (ins_dev, boot_efi, 1);
1840 if (!removable && update_nvram)
1842 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1843 grub_install_register_efi (efidir_grub_dev,
1844 "\\System\\Library\\CoreServices",
1845 efi_distributor);
1848 grub_device_close (ins_dev);
1849 free (boot_efi);
1850 free (mach_kernel);
1851 break;
1853 case GRUB_INSTALL_PLATFORM_ARM_EFI:
1854 case GRUB_INSTALL_PLATFORM_ARM64_EFI:
1855 case GRUB_INSTALL_PLATFORM_IA64_EFI:
1857 char *dst = grub_util_path_concat (2, efidir, efi_file);
1858 grub_install_copy_file (imgfile, dst, 1);
1859 free (dst);
1861 if (!removable && update_nvram)
1863 char * efifile_path;
1864 char * part;
1866 /* Try to make this image bootable using the EFI Boot Manager, if available. */
1867 if (!efi_distributor || efi_distributor[0] == '\0')
1868 grub_util_error ("%s", _("EFI bootloader id isn't specified."));
1869 efifile_path = xasprintf ("\\EFI\\%s\\%s",
1870 efi_distributor,
1871 efi_file);
1872 part = (efidir_grub_dev->disk->partition
1873 ? grub_partition_get_name (efidir_grub_dev->disk->partition)
1874 : 0);
1875 grub_util_info ("Registering with EFI: distributor = `%s',"
1876 " path = `%s', ESP at %s%s%s",
1877 efi_distributor, efifile_path,
1878 efidir_grub_dev->disk->name,
1879 (part ? ",": ""), (part ? : ""));
1880 grub_free (part);
1881 grub_install_register_efi (efidir_grub_dev,
1882 efifile_path, efi_distributor);
1884 break;
1886 case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
1887 case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
1888 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
1889 case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
1890 case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
1891 case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
1892 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
1893 case GRUB_INSTALL_PLATFORM_I386_QEMU:
1894 case GRUB_INSTALL_PLATFORM_I386_XEN:
1895 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
1896 grub_util_warn ("%s",
1897 _("WARNING: no platform-specific install was performed"));
1898 break;
1899 /* pacify warning. */
1900 case GRUB_INSTALL_PLATFORM_MAX:
1901 break;
1904 fprintf (stderr, "%s\n", _("Installation finished. No error reported."));
1906 /* Free resources. */
1907 grub_gcry_fini_all ();
1908 grub_fini_all ();
1910 return 0;