~ubuntu-branches/debian/jessie/grub2/jessie

« back to all changes in this revision

Viewing changes to .pc/grub-install-extra-removable.patch/util/grub-install.c

  • Committer: Package Import Robot
  • Author(s): Ian Campbell, Steve McIntyre, Ian Campbell
  • Date: 2014-12-08 08:38:38 UTC
  • mfrom: (99.1.21 sid)
  • Revision ID: package-import@ubuntu.com-20141208083838-n9ornqy1leksg80t
Tags: 2.02~beta2-18
[ Steve McIntyre ]
* Add support for forcing an extra copy of grub-efi to the removable
  media path /boot/efi/EFI/BOOT/BOOT$ARCH.EFI (#767037)

[ Ian Campbell ]
* Add myself to Uploaders.

Show diffs side-by-side

added added

removed removed

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