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