grub-install.c 53 KB

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