grub-install.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  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 grub_install_get_default_arm_platform ();
  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. #elif defined (__loongarch_lp64)
  294. return "loongarch64-efi";
  295. #elif defined (__riscv)
  296. #if __riscv_xlen == 32
  297. return "riscv32-efi";
  298. #elif __riscv_xlen == 64
  299. return "riscv64-efi";
  300. #else
  301. return NULL;
  302. #endif
  303. #else
  304. return NULL;
  305. #endif
  306. }
  307. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  308. static char *
  309. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  310. {
  311. switch (key)
  312. {
  313. case OPTION_BOOT_DIRECTORY:
  314. return xasprintf (text, GRUB_DIR_NAME, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME);
  315. case OPTION_TARGET:
  316. {
  317. char *plats = grub_install_get_platforms_string ();
  318. char *ret;
  319. ret = xasprintf (text, get_default_platform (), plats);
  320. free (plats);
  321. return ret;
  322. }
  323. case ARGP_KEY_HELP_POST_DOC:
  324. return xasprintf (text, program_name, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME);
  325. default:
  326. return grub_install_help_filter (key, text, input);
  327. }
  328. }
  329. #pragma GCC diagnostic error "-Wformat-nonliteral"
  330. /* TRANSLATORS: INSTALL_DEVICE isn't an identifier and is the DEVICE you
  331. install to. */
  332. struct argp argp = {
  333. options, argp_parser, N_("[OPTION] [INSTALL_DEVICE]"),
  334. N_("Install GRUB on your drive.")"\v"
  335. N_("INSTALL_DEVICE must be system device filename.\n"
  336. "%s copies GRUB images into %s. On some platforms, it"
  337. " may also install GRUB into the boot sector."),
  338. NULL, help_filter, NULL
  339. };
  340. static int
  341. probe_raid_level (grub_disk_t disk)
  342. {
  343. /* disk might be NULL in the case of a LVM physical volume with no LVM
  344. signature. Ignore such cases here. */
  345. if (!disk)
  346. return -1;
  347. if (disk->dev->id != GRUB_DISK_DEVICE_DISKFILTER_ID)
  348. return -1;
  349. if (disk->name[0] != 'm' || disk->name[1] != 'd')
  350. return -1;
  351. if (!((struct grub_diskfilter_lv *) disk->data)->segments)
  352. return -1;
  353. return ((struct grub_diskfilter_lv *) disk->data)->segments->type;
  354. }
  355. static void
  356. push_partmap_module (const char *map, void *data __attribute__ ((unused)))
  357. {
  358. char buf[50];
  359. if (strcmp (map, "openbsd") == 0 || strcmp (map, "netbsd") == 0)
  360. {
  361. grub_install_push_module ("part_bsd");
  362. return;
  363. }
  364. snprintf (buf, sizeof (buf), "part_%s", map);
  365. grub_install_push_module (buf);
  366. }
  367. static void
  368. push_cryptodisk_module (const char *mod, void *data __attribute__ ((unused)))
  369. {
  370. grub_install_push_module (mod);
  371. }
  372. static void
  373. probe_mods (grub_disk_t disk)
  374. {
  375. grub_partition_t part;
  376. grub_disk_memberlist_t list = NULL, tmp;
  377. int raid_level;
  378. if (disk->partition == NULL)
  379. grub_util_info ("no partition map found for %s", disk->name);
  380. for (part = disk->partition; part; part = part->parent)
  381. push_partmap_module (part->partmap->name, NULL);
  382. if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID)
  383. {
  384. grub_diskfilter_get_partmap (disk, push_partmap_module, NULL);
  385. have_abstractions = 1;
  386. }
  387. if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
  388. && (grub_memcmp (disk->name, "lvm/", sizeof ("lvm/") - 1) == 0 ||
  389. grub_memcmp (disk->name, "lvmid/", sizeof ("lvmid/") - 1) == 0))
  390. grub_install_push_module ("lvm");
  391. if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
  392. && grub_memcmp (disk->name, "ldm/", sizeof ("ldm/") - 1) == 0)
  393. grub_install_push_module ("ldm");
  394. if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
  395. {
  396. grub_util_cryptodisk_get_abstraction (disk,
  397. push_cryptodisk_module, NULL);
  398. have_abstractions = 1;
  399. have_cryptodisk = 1;
  400. }
  401. raid_level = probe_raid_level (disk);
  402. if (raid_level >= 0)
  403. {
  404. grub_install_push_module ("diskfilter");
  405. if (disk->dev->disk_raidname)
  406. grub_install_push_module (disk->dev->disk_raidname (disk));
  407. }
  408. if (raid_level == 5)
  409. grub_install_push_module ("raid5rec");
  410. if (raid_level == 6)
  411. grub_install_push_module ("raid6rec");
  412. /* In case of LVM/RAID, check the member devices as well. */
  413. if (disk->dev->disk_memberlist)
  414. list = disk->dev->disk_memberlist (disk);
  415. while (list)
  416. {
  417. probe_mods (list->disk);
  418. tmp = list->next;
  419. free (list);
  420. list = tmp;
  421. }
  422. }
  423. static int
  424. have_bootdev (enum grub_install_plat pl)
  425. {
  426. switch (pl)
  427. {
  428. case GRUB_INSTALL_PLATFORM_I386_PC:
  429. case GRUB_INSTALL_PLATFORM_I386_EFI:
  430. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  431. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  432. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  433. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  434. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  435. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  436. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  437. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  438. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  439. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  440. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  441. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  442. return 1;
  443. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  444. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  445. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  446. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  447. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  448. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  449. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  450. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  451. case GRUB_INSTALL_PLATFORM_I386_XEN:
  452. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  453. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  454. return 0;
  455. /* pacify warning. */
  456. case GRUB_INSTALL_PLATFORM_MAX:
  457. return 0;
  458. }
  459. return 0;
  460. }
  461. static void
  462. probe_cryptodisk_uuid (grub_disk_t disk)
  463. {
  464. grub_disk_memberlist_t list = NULL, tmp;
  465. /* In case of LVM/RAID, check the member devices as well. */
  466. if (disk->dev->disk_memberlist)
  467. {
  468. list = disk->dev->disk_memberlist (disk);
  469. }
  470. while (list)
  471. {
  472. probe_cryptodisk_uuid (list->disk);
  473. tmp = list->next;
  474. free (list);
  475. list = tmp;
  476. }
  477. if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
  478. {
  479. const char *uuid = grub_util_cryptodisk_get_uuid (disk);
  480. if (!load_cfg_f)
  481. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  482. have_load_cfg = 1;
  483. fprintf (load_cfg_f, "cryptomount -u %s\n",
  484. uuid);
  485. }
  486. }
  487. static int
  488. is_same_disk (const char *a, const char *b)
  489. {
  490. while (1)
  491. {
  492. if ((*a == ',' || *a == '\0') && (*b == ',' || *b == '\0'))
  493. return 1;
  494. if (*a != *b)
  495. return 0;
  496. if (*a == '\\')
  497. {
  498. if (a[1] != b[1])
  499. return 0;
  500. a += 2;
  501. b += 2;
  502. continue;
  503. }
  504. a++;
  505. b++;
  506. }
  507. }
  508. static char *
  509. get_rndstr (void)
  510. {
  511. grub_uint8_t rnd[15];
  512. const size_t sz = sizeof (rnd) * GRUB_CHAR_BIT / 5;
  513. char * ret = xmalloc (sz + 1);
  514. size_t i;
  515. if (grub_get_random (rnd, sizeof (rnd)))
  516. grub_util_error ("%s", _("couldn't retrieve random data"));
  517. for (i = 0; i < sz; i++)
  518. {
  519. grub_size_t b = i * 5;
  520. grub_uint8_t r;
  521. grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
  522. grub_size_t f2;
  523. if (f1 > 5)
  524. f1 = 5;
  525. f2 = 5 - f1;
  526. r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
  527. if (f2)
  528. r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
  529. if (r < 10)
  530. ret[i] = '0' + r;
  531. else
  532. ret[i] = 'a' + (r - 10);
  533. }
  534. ret[sz] = '\0';
  535. return ret;
  536. }
  537. static char *
  538. escape (const char *in)
  539. {
  540. char *ptr;
  541. char *ret;
  542. int overhead = 0;
  543. for (ptr = (char*)in; *ptr; ptr++)
  544. if (*ptr == '\'')
  545. overhead += 3;
  546. ret = grub_malloc (ptr - in + overhead + 1);
  547. if (!ret)
  548. return NULL;
  549. grub_strchrsub (ret, in, '\'', "'\\''");
  550. return ret;
  551. }
  552. static struct grub_util_config config;
  553. static void
  554. device_map_check_duplicates (const char *dev_map)
  555. {
  556. FILE *fp;
  557. char buf[1024]; /* XXX */
  558. size_t alloced = 8;
  559. size_t filled = 0;
  560. char **d;
  561. size_t i;
  562. if (dev_map[0] == '\0')
  563. return;
  564. fp = grub_util_fopen (dev_map, "r");
  565. if (! fp)
  566. return;
  567. d = xcalloc (alloced, sizeof (d[0]));
  568. while (fgets (buf, sizeof (buf), fp))
  569. {
  570. char *p = buf;
  571. char *e;
  572. /* Skip leading spaces. */
  573. while (*p && grub_isspace (*p))
  574. p++;
  575. /* If the first character is `#' or NUL, skip this line. */
  576. if (*p == '\0' || *p == '#')
  577. continue;
  578. if (*p != '(')
  579. continue;
  580. p++;
  581. e = p;
  582. p = strchr (p, ')');
  583. if (! p)
  584. continue;
  585. if (filled >= alloced)
  586. {
  587. alloced *= 2;
  588. d = xrealloc (d, alloced * sizeof (d[0]));
  589. }
  590. *p = '\0';
  591. d[filled++] = xstrdup (e);
  592. }
  593. fclose (fp);
  594. qsort (d, filled, sizeof (d[0]), grub_qsort_strcmp);
  595. for (i = 0; i + 1 < filled; i++)
  596. if (strcmp (d[i], d[i+1]) == 0)
  597. {
  598. grub_util_error (_("the drive %s is defined multiple times in the device map %s"),
  599. d[i], dev_map);
  600. }
  601. for (i = 0; i < filled; i++)
  602. free (d[i]);
  603. free (d);
  604. }
  605. static grub_err_t
  606. write_to_disk (grub_device_t dev, const char *fn)
  607. {
  608. char *core_img;
  609. size_t core_size;
  610. grub_err_t err;
  611. core_size = grub_util_get_image_size (fn);
  612. core_img = grub_util_read_image (fn);
  613. grub_util_info ("writing `%s' to `%s'", fn, dev->disk->name);
  614. err = grub_disk_write (dev->disk, 0, 0,
  615. core_size, core_img);
  616. free (core_img);
  617. return err;
  618. }
  619. static int
  620. is_prep_partition (grub_device_t dev)
  621. {
  622. if (!dev->disk)
  623. return 0;
  624. if (!dev->disk->partition)
  625. return 0;
  626. if (strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
  627. return (dev->disk->partition->msdostype == 0x41);
  628. if (strcmp (dev->disk->partition->partmap->name, "gpt") == 0)
  629. {
  630. struct grub_gpt_partentry gptdata;
  631. grub_partition_t p = dev->disk->partition;
  632. int ret = 0;
  633. dev->disk->partition = dev->disk->partition->parent;
  634. if (grub_disk_read (dev->disk, p->offset, p->index,
  635. sizeof (gptdata), &gptdata) == 0)
  636. {
  637. const grub_guid_t template = {
  638. grub_cpu_to_le32_compile_time (0x9e1a2d38),
  639. grub_cpu_to_le16_compile_time (0xc612),
  640. grub_cpu_to_le16_compile_time (0x4316),
  641. { 0xaa, 0x26, 0x8b, 0x49, 0x52, 0x1e, 0x5a, 0x8b }
  642. };
  643. ret = grub_memcmp (&template, &gptdata.type,
  644. sizeof (template)) == 0;
  645. }
  646. dev->disk->partition = p;
  647. return ret;
  648. }
  649. return 0;
  650. }
  651. static int
  652. is_prep_empty (grub_device_t dev)
  653. {
  654. grub_disk_addr_t dsize, addr;
  655. grub_uint32_t buffer[32768];
  656. dsize = grub_disk_native_sectors (dev->disk);
  657. for (addr = 0; addr < dsize;
  658. addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
  659. {
  660. grub_size_t sz = sizeof (buffer);
  661. grub_uint32_t *ptr;
  662. if (sizeof (buffer) / GRUB_DISK_SECTOR_SIZE > dsize - addr)
  663. sz = (dsize - addr) * GRUB_DISK_SECTOR_SIZE;
  664. grub_disk_read (dev->disk, addr, 0, sz, buffer);
  665. if (addr == 0 && grub_memcmp (buffer, ELFMAG, SELFMAG) == 0)
  666. return 1;
  667. for (ptr = buffer; ptr < buffer + sz / sizeof (*buffer); ptr++)
  668. if (*ptr)
  669. return 0;
  670. }
  671. return 1;
  672. }
  673. static void
  674. bless (grub_device_t dev, const char *path, int x86)
  675. {
  676. struct stat st;
  677. grub_err_t err;
  678. grub_util_info ("blessing %s", path);
  679. if (stat (path, &st) < 0)
  680. grub_util_error (N_("cannot stat `%s': %s"),
  681. path, strerror (errno));
  682. err = grub_mac_bless_inode (dev, st.st_ino, S_ISDIR (st.st_mode), x86);
  683. if (err)
  684. grub_util_error ("%s", grub_errmsg);
  685. grub_util_info ("blessed");
  686. }
  687. static void
  688. fill_core_services (const char *core_services)
  689. {
  690. char *label;
  691. FILE *f;
  692. char *label_text;
  693. char *label_string = xasprintf ("%s %s", bootloader_id, product_version);
  694. char *sysv_plist;
  695. label = grub_util_path_concat (2, core_services, ".disk_label");
  696. grub_util_info ("rendering label %s", label_string);
  697. grub_util_render_label (label_font, label_bgcolor ? : "white",
  698. label_color ? : "black", label_string, label);
  699. grub_util_info ("label rendered");
  700. free (label);
  701. label_text = grub_util_path_concat (2, core_services, ".disk_label.contentDetails");
  702. f = grub_util_fopen (label_text, "wb");
  703. fprintf (f, "%s\n", label_string);
  704. fclose (f);
  705. free (label_string);
  706. free (label_text);
  707. sysv_plist = grub_util_path_concat (2, core_services, "SystemVersion.plist");
  708. f = grub_util_fopen (sysv_plist, "wb");
  709. fprintf (f,
  710. "<plist version=\"1.0\">\n"
  711. "<dict>\n"
  712. " <key>ProductBuildVersion</key>\n"
  713. " <string></string>\n"
  714. " <key>ProductName</key>\n"
  715. " <string>%s</string>\n"
  716. " <key>ProductVersion</key>\n"
  717. " <string>%s</string>\n"
  718. "</dict>\n"
  719. "</plist>\n", bootloader_id, product_version);
  720. fclose (f);
  721. free (sysv_plist);
  722. }
  723. #ifdef __linux__
  724. static void
  725. try_open (const char *path)
  726. {
  727. FILE *f;
  728. f = grub_util_fopen (path, "r+");
  729. if (f == NULL)
  730. grub_util_error (_("Unable to open %s: %s"), path, strerror (errno));
  731. fclose (f);
  732. }
  733. #endif
  734. int
  735. main (int argc, char *argv[])
  736. {
  737. int is_efi = 0;
  738. const char *efi_distributor = NULL;
  739. const char *efi_file = NULL;
  740. char **grub_devices;
  741. grub_fs_t grub_fs;
  742. grub_device_t grub_dev = NULL;
  743. enum grub_install_plat platform;
  744. char *grubdir, *device_map;
  745. char **curdev, **curdrive;
  746. char **grub_drives;
  747. char *relative_grubdir;
  748. char **efidir_device_names = NULL;
  749. grub_device_t efidir_grub_dev = NULL;
  750. char *efidir_grub_devname;
  751. int efidir_is_mac = 0;
  752. int is_prep = 0;
  753. const char *pkgdatadir;
  754. grub_util_host_init (&argc, &argv);
  755. product_version = xstrdup (PACKAGE_VERSION);
  756. pkgdatadir = grub_util_get_pkgdatadir ();
  757. label_font = grub_util_path_concat (2, pkgdatadir, "unicode.pf2");
  758. argp_parse (&argp, argc, argv, 0, 0, 0);
  759. if (verbosity > 1)
  760. grub_env_set ("debug", "all");
  761. grub_util_load_config (&config);
  762. if (!bootloader_id && config.grub_distributor)
  763. {
  764. char *ptr;
  765. bootloader_id = xstrdup (config.grub_distributor);
  766. for (ptr = bootloader_id; *ptr && *ptr != ' '; ptr++)
  767. if (*ptr >= 'A' && *ptr <= 'Z')
  768. *ptr = *ptr - 'A' + 'a';
  769. *ptr = '\0';
  770. }
  771. if (!bootloader_id || bootloader_id[0] == '\0')
  772. {
  773. free (bootloader_id);
  774. bootloader_id = xstrdup ("grub");
  775. }
  776. if (!grub_install_source_directory)
  777. {
  778. if (!target)
  779. {
  780. const char * t;
  781. t = get_default_platform ();
  782. if (!t)
  783. grub_util_error ("%s",
  784. _("Unable to determine your platform."
  785. " Use --target.")
  786. );
  787. target = xstrdup (t);
  788. }
  789. grub_install_source_directory
  790. = grub_util_path_concat (2, grub_util_get_pkglibdir (), target);
  791. }
  792. platform = grub_install_get_target (grub_install_source_directory);
  793. {
  794. char *platname = grub_install_get_platform_name (platform);
  795. fprintf (stderr, _("Installing for %s platform.\n"), platname);
  796. free (platname);
  797. }
  798. switch (platform)
  799. {
  800. case GRUB_INSTALL_PLATFORM_I386_PC:
  801. if (!disk_module)
  802. disk_module = xstrdup ("biosdisk");
  803. break;
  804. case GRUB_INSTALL_PLATFORM_I386_EFI:
  805. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  806. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  807. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  808. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  809. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  810. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  811. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  812. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  813. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  814. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  815. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  816. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  817. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  818. case GRUB_INSTALL_PLATFORM_I386_XEN:
  819. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  820. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  821. break;
  822. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  823. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  824. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  825. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  826. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  827. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  828. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  829. disk_module = xstrdup ("native");
  830. break;
  831. /* pacify warning. */
  832. case GRUB_INSTALL_PLATFORM_MAX:
  833. break;
  834. }
  835. switch (platform)
  836. {
  837. case GRUB_INSTALL_PLATFORM_I386_PC:
  838. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  839. if (!install_device)
  840. grub_util_error ("%s", _("install device isn't specified"));
  841. break;
  842. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  843. if (install_device)
  844. is_prep = 1;
  845. break;
  846. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  847. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  848. break;
  849. case GRUB_INSTALL_PLATFORM_I386_EFI:
  850. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  851. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  852. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  853. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  854. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  855. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  856. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  857. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  858. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  859. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  860. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  861. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  862. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  863. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  864. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  865. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  866. case GRUB_INSTALL_PLATFORM_I386_XEN:
  867. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  868. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  869. free (install_device);
  870. install_device = NULL;
  871. break;
  872. /* pacify warning. */
  873. case GRUB_INSTALL_PLATFORM_MAX:
  874. break;
  875. }
  876. if (!bootdir)
  877. bootdir = grub_util_path_concat (3, "/", rootdir, GRUB_BOOT_DIR_NAME);
  878. {
  879. char * t = grub_util_path_concat (2, bootdir, GRUB_DIR_NAME);
  880. grub_install_mkdir_p (t);
  881. grubdir = grub_canonicalize_file_name (t);
  882. if (!grubdir)
  883. grub_util_error (_("failed to get canonical path of `%s'"), t);
  884. free (t);
  885. }
  886. device_map = grub_util_path_concat (2, grubdir, "device.map");
  887. if (recheck)
  888. grub_util_unlink (device_map);
  889. device_map_check_duplicates (device_map);
  890. grub_util_biosdisk_init (device_map);
  891. /* Initialize all modules. */
  892. grub_init_all ();
  893. grub_gcry_init_all ();
  894. grub_hostfs_init ();
  895. grub_host_init ();
  896. switch (platform)
  897. {
  898. case GRUB_INSTALL_PLATFORM_I386_EFI:
  899. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  900. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  901. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  902. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  903. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  904. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  905. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  906. is_efi = 1;
  907. break;
  908. default:
  909. is_efi = 0;
  910. break;
  911. /* pacify warning. */
  912. case GRUB_INSTALL_PLATFORM_MAX:
  913. break;
  914. }
  915. switch (platform)
  916. {
  917. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  918. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  919. #ifdef __linux__
  920. /* On Linux, ensure /dev/nvram is _functional_. */
  921. if (update_nvram)
  922. try_open ("/dev/nvram");
  923. #endif
  924. break;
  925. default:
  926. break;
  927. }
  928. /* Find the EFI System Partition. */
  929. if (is_efi)
  930. {
  931. grub_fs_t fs;
  932. free (install_device);
  933. install_device = NULL;
  934. if (!efidir)
  935. {
  936. char *d = grub_util_path_concat (2, bootdir, "efi");
  937. char *dr = NULL;
  938. if (!grub_util_is_directory (d))
  939. {
  940. free (d);
  941. d = grub_util_path_concat (2, bootdir, "EFI");
  942. }
  943. /*
  944. The EFI System Partition may have been given directly using
  945. --root-directory.
  946. */
  947. if (!grub_util_is_directory (d)
  948. && rootdir && grub_strcmp (rootdir, "/") != 0)
  949. {
  950. free (d);
  951. d = xstrdup (rootdir);
  952. }
  953. if (grub_util_is_directory (d))
  954. dr = grub_make_system_path_relative_to_its_root (d);
  955. /* Is it a mount point? */
  956. if (dr && dr[0] == '\0')
  957. efidir = d;
  958. else
  959. free (d);
  960. free (dr);
  961. }
  962. if (!efidir)
  963. grub_util_error ("%s", _("cannot find EFI directory"));
  964. efidir_device_names = grub_guess_root_devices (efidir);
  965. if (!efidir_device_names || !efidir_device_names[0])
  966. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
  967. efidir);
  968. install_device = efidir_device_names[0];
  969. for (curdev = efidir_device_names; *curdev; curdev++)
  970. grub_util_pull_device (*curdev);
  971. efidir_grub_devname = grub_util_get_grub_dev (efidir_device_names[0]);
  972. if (!efidir_grub_devname)
  973. grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
  974. efidir_device_names[0]);
  975. efidir_grub_dev = grub_device_open (efidir_grub_devname);
  976. if (! efidir_grub_dev)
  977. grub_util_error ("%s", grub_errmsg);
  978. fs = grub_fs_probe (efidir_grub_dev);
  979. if (! fs)
  980. grub_util_error ("%s", grub_errmsg);
  981. efidir_is_mac = 0;
  982. if (grub_strcmp (fs->name, "hfs") == 0
  983. || grub_strcmp (fs->name, "hfsplus") == 0)
  984. efidir_is_mac = 1;
  985. if (!efidir_is_mac && grub_strcmp (fs->name, "fat") != 0)
  986. {
  987. if (force)
  988. grub_util_warn (_("%s doesn't look like an EFI partition, system may not boot"), efidir);
  989. else
  990. grub_util_error (_("%s doesn't look like an EFI partition"), efidir);
  991. }
  992. /* The EFI specification requires that an EFI System Partition must
  993. contain an "EFI" subdirectory, and that OS loaders are stored in
  994. subdirectories below EFI. Vendors are expected to pick names that do
  995. not collide with other vendors. To minimise collisions, we use the
  996. name of our distributor if possible.
  997. */
  998. char *t;
  999. efi_distributor = bootloader_id;
  1000. if (removable)
  1001. {
  1002. /* The specification makes stricter requirements of removable
  1003. devices, in order that only one image can be automatically loaded
  1004. from them. The image must always reside under /EFI/BOOT, and it
  1005. must have a specific file name depending on the architecture.
  1006. */
  1007. efi_distributor = "BOOT";
  1008. switch (platform)
  1009. {
  1010. case GRUB_INSTALL_PLATFORM_I386_EFI:
  1011. efi_file = "BOOTIA32.EFI";
  1012. break;
  1013. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  1014. efi_file = "BOOTX64.EFI";
  1015. break;
  1016. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  1017. efi_file = "BOOTIA64.EFI";
  1018. break;
  1019. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  1020. efi_file = "BOOTARM.EFI";
  1021. break;
  1022. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  1023. efi_file = "BOOTAA64.EFI";
  1024. break;
  1025. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  1026. efi_file = "BOOTLOONGARCH64.EFI";
  1027. break;
  1028. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  1029. efi_file = "BOOTRISCV32.EFI";
  1030. break;
  1031. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  1032. efi_file = "BOOTRISCV64.EFI";
  1033. break;
  1034. default:
  1035. grub_util_error ("%s", _("You've found a bug"));
  1036. break;
  1037. }
  1038. }
  1039. else
  1040. {
  1041. /* It is convenient for each architecture to have a different
  1042. efi_file, so that different versions can be installed in parallel.
  1043. */
  1044. switch (platform)
  1045. {
  1046. case GRUB_INSTALL_PLATFORM_I386_EFI:
  1047. efi_file = "grubia32.efi";
  1048. break;
  1049. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  1050. efi_file = "grubx64.efi";
  1051. break;
  1052. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  1053. efi_file = "grubia64.efi";
  1054. break;
  1055. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  1056. efi_file = "grubarm.efi";
  1057. break;
  1058. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  1059. efi_file = "grubaa64.efi";
  1060. break;
  1061. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  1062. efi_file = "grubloongarch64.efi";
  1063. break;
  1064. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  1065. efi_file = "grubriscv32.efi";
  1066. break;
  1067. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  1068. efi_file = "grubriscv64.efi";
  1069. break;
  1070. default:
  1071. efi_file = "grub.efi";
  1072. break;
  1073. }
  1074. }
  1075. t = grub_util_path_concat (3, efidir, "EFI", efi_distributor);
  1076. free (efidir);
  1077. efidir = t;
  1078. grub_install_mkdir_p (efidir);
  1079. }
  1080. if (platform == GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
  1081. {
  1082. int is_guess = 0;
  1083. if (!macppcdir)
  1084. {
  1085. char *d;
  1086. is_guess = 1;
  1087. d = grub_util_path_concat (2, bootdir, "macppc");
  1088. if (!grub_util_is_directory (d))
  1089. {
  1090. free (d);
  1091. d = grub_util_path_concat (2, bootdir, "efi");
  1092. }
  1093. /* Find the Mac HFS(+) System Partition. */
  1094. if (!grub_util_is_directory (d))
  1095. {
  1096. free (d);
  1097. d = grub_util_path_concat (2, bootdir, "EFI");
  1098. }
  1099. if (!grub_util_is_directory (d))
  1100. {
  1101. free (d);
  1102. d = 0;
  1103. }
  1104. if (d)
  1105. macppcdir = d;
  1106. }
  1107. if (macppcdir)
  1108. {
  1109. char **macppcdir_device_names = NULL;
  1110. grub_device_t macppcdir_grub_dev = NULL;
  1111. char *macppcdir_grub_devname;
  1112. grub_fs_t fs;
  1113. macppcdir_device_names = grub_guess_root_devices (macppcdir);
  1114. if (!macppcdir_device_names || !macppcdir_device_names[0])
  1115. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
  1116. macppcdir);
  1117. for (curdev = macppcdir_device_names; *curdev; curdev++)
  1118. grub_util_pull_device (*curdev);
  1119. macppcdir_grub_devname = grub_util_get_grub_dev (macppcdir_device_names[0]);
  1120. if (!macppcdir_grub_devname)
  1121. grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
  1122. macppcdir_device_names[0]);
  1123. macppcdir_grub_dev = grub_device_open (macppcdir_grub_devname);
  1124. if (! macppcdir_grub_dev)
  1125. grub_util_error ("%s", grub_errmsg);
  1126. fs = grub_fs_probe (macppcdir_grub_dev);
  1127. if (! fs)
  1128. grub_util_error ("%s", grub_errmsg);
  1129. if (grub_strcmp (fs->name, "hfs") != 0
  1130. && grub_strcmp (fs->name, "hfsplus") != 0
  1131. && !is_guess)
  1132. grub_util_error (_("filesystem on %s is neither HFS nor HFS+"),
  1133. macppcdir);
  1134. if (grub_strcmp (fs->name, "hfs") == 0
  1135. || grub_strcmp (fs->name, "hfsplus") == 0)
  1136. {
  1137. install_device = macppcdir_device_names[0];
  1138. is_prep = 0;
  1139. }
  1140. }
  1141. }
  1142. size_t ndev = 0;
  1143. /* Write device to a variable so we don't have to traverse /dev every time. */
  1144. grub_devices = grub_guess_root_devices (grubdir);
  1145. if (!grub_devices || !grub_devices[0])
  1146. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"),
  1147. grubdir);
  1148. for (curdev = grub_devices; *curdev; curdev++)
  1149. {
  1150. grub_util_pull_device (*curdev);
  1151. ndev++;
  1152. }
  1153. grub_drives = xcalloc (ndev + 1, sizeof (grub_drives[0]));
  1154. for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
  1155. curdrive++)
  1156. {
  1157. *curdrive = grub_util_get_grub_dev (*curdev);
  1158. if (! *curdrive)
  1159. grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
  1160. *curdev);
  1161. }
  1162. *curdrive = 0;
  1163. grub_dev = grub_device_open (grub_drives[0]);
  1164. if (! grub_dev)
  1165. grub_util_error ("%s", grub_errmsg);
  1166. grub_fs = grub_fs_probe (grub_dev);
  1167. if (! grub_fs)
  1168. grub_util_error ("%s", grub_errmsg);
  1169. grub_install_push_module (grub_fs->name);
  1170. if (grub_dev->disk)
  1171. probe_mods (grub_dev->disk);
  1172. for (curdrive = grub_drives + 1; *curdrive; curdrive++)
  1173. {
  1174. grub_device_t dev = grub_device_open (*curdrive);
  1175. if (!dev)
  1176. continue;
  1177. if (dev->disk)
  1178. probe_mods (dev->disk);
  1179. grub_device_close (dev);
  1180. }
  1181. if (!config.is_cryptodisk_enabled && have_cryptodisk)
  1182. grub_util_error (_("attempt to install to encrypted disk without cryptodisk enabled. "
  1183. "Set `%s' in file `%s'"), "GRUB_ENABLE_CRYPTODISK=y",
  1184. grub_util_get_config_filename ());
  1185. if (disk_module && grub_strcmp (disk_module, "ata") == 0)
  1186. grub_install_push_module ("pata");
  1187. else if (disk_module && grub_strcmp (disk_module, "native") == 0)
  1188. {
  1189. grub_install_push_module ("pata");
  1190. grub_install_push_module ("ahci");
  1191. grub_install_push_module ("ohci");
  1192. grub_install_push_module ("uhci");
  1193. grub_install_push_module ("ehci");
  1194. grub_install_push_module ("usbms");
  1195. }
  1196. else if (disk_module && disk_module[0])
  1197. grub_install_push_module (disk_module);
  1198. relative_grubdir = grub_make_system_path_relative_to_its_root (grubdir);
  1199. if (relative_grubdir[0] == '\0')
  1200. {
  1201. free (relative_grubdir);
  1202. relative_grubdir = xstrdup ("/");
  1203. }
  1204. char *prefix_drive = NULL;
  1205. char *install_drive = NULL;
  1206. if (install_device)
  1207. {
  1208. if (install_device[0] == '('
  1209. && install_device[grub_strlen (install_device) - 1] == ')')
  1210. {
  1211. size_t len = grub_strlen (install_device) - 2;
  1212. install_drive = xmalloc (len + 1);
  1213. memcpy (install_drive, install_device + 1, len);
  1214. install_drive[len] = '\0';
  1215. }
  1216. else
  1217. {
  1218. grub_util_pull_device (install_device);
  1219. install_drive = grub_util_get_grub_dev (install_device);
  1220. if (!install_drive)
  1221. grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
  1222. install_device);
  1223. }
  1224. }
  1225. grub_install_copy_files (grub_install_source_directory,
  1226. grubdir, platform);
  1227. char *envfile = grub_util_path_concat (2, grubdir, "grubenv");
  1228. if (!grub_util_is_regular (envfile))
  1229. grub_util_create_envblk_file (envfile);
  1230. char *platname = grub_install_get_platform_name (platform);
  1231. char *platdir;
  1232. {
  1233. char *t = grub_util_path_concat (2, grubdir,
  1234. platname);
  1235. platdir = grub_canonicalize_file_name (t);
  1236. if (!platdir)
  1237. grub_util_error (_("failed to get canonical path of `%s'"),
  1238. t);
  1239. free (t);
  1240. }
  1241. load_cfg = grub_util_path_concat (2, platdir,
  1242. "load.cfg");
  1243. grub_util_unlink (load_cfg);
  1244. if (debug_image && debug_image[0])
  1245. {
  1246. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  1247. have_load_cfg = 1;
  1248. fprintf (load_cfg_f, "set debug='%s'\n",
  1249. debug_image);
  1250. }
  1251. if (!have_abstractions)
  1252. {
  1253. if ((disk_module && grub_strcmp (disk_module, "biosdisk") != 0)
  1254. || grub_drives[1]
  1255. || (!install_drive
  1256. && platform != GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275)
  1257. || (install_drive && !is_same_disk (grub_drives[0], install_drive))
  1258. || !have_bootdev (platform))
  1259. {
  1260. char *uuid = NULL;
  1261. /* generic method (used on coreboot and ata mod). */
  1262. if (!force_file_id
  1263. && grub_fs->fs_uuid && grub_fs->fs_uuid (grub_dev, &uuid))
  1264. {
  1265. grub_print_error ();
  1266. grub_errno = 0;
  1267. uuid = NULL;
  1268. }
  1269. if (!load_cfg_f)
  1270. load_cfg_f = grub_util_fopen (load_cfg, "wb");
  1271. have_load_cfg = 1;
  1272. if (uuid)
  1273. {
  1274. fprintf (load_cfg_f, "search.fs_uuid %s root ",
  1275. uuid);
  1276. grub_install_push_module ("search_fs_uuid");
  1277. }
  1278. else
  1279. {
  1280. char *rndstr = get_rndstr ();
  1281. char *fl = grub_util_path_concat (3, grubdir,
  1282. "uuid", rndstr);
  1283. char *fldir = grub_util_path_concat (2, grubdir,
  1284. "uuid");
  1285. char *relfl;
  1286. FILE *flf;
  1287. grub_install_mkdir_p (fldir);
  1288. flf = grub_util_fopen (fl, "w");
  1289. if (!flf)
  1290. grub_util_error (_("Can't create file: %s"), strerror (errno));
  1291. fclose (flf);
  1292. relfl = grub_make_system_path_relative_to_its_root (fl);
  1293. fprintf (load_cfg_f, "search.file %s root ",
  1294. relfl);
  1295. grub_install_push_module ("search_fs_file");
  1296. }
  1297. for (curdev = grub_devices, curdrive = grub_drives; *curdev; curdev++,
  1298. curdrive++)
  1299. {
  1300. const char *map;
  1301. char *g = NULL;
  1302. grub_device_t dev;
  1303. if (curdrive == grub_drives)
  1304. dev = grub_dev;
  1305. else
  1306. dev = grub_device_open (*curdrive);
  1307. if (!dev)
  1308. continue;
  1309. if (dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID)
  1310. {
  1311. grub_util_fprint_full_disk_name (load_cfg_f,
  1312. dev->disk->name,
  1313. dev);
  1314. fprintf (load_cfg_f, " ");
  1315. if (dev != grub_dev)
  1316. grub_device_close (dev);
  1317. continue;
  1318. }
  1319. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  1320. if (map)
  1321. {
  1322. grub_util_fprint_full_disk_name (load_cfg_f, map, dev);
  1323. fprintf (load_cfg_f, " ");
  1324. }
  1325. if (disk_module && disk_module[0]
  1326. && grub_strcmp (disk_module, "biosdisk") != 0)
  1327. g = grub_util_guess_baremetal_drive (*curdev);
  1328. else
  1329. switch (platform)
  1330. {
  1331. case GRUB_INSTALL_PLATFORM_I386_PC:
  1332. g = grub_util_guess_bios_drive (*curdev);
  1333. break;
  1334. case GRUB_INSTALL_PLATFORM_I386_EFI:
  1335. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  1336. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  1337. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  1338. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  1339. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  1340. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  1341. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  1342. g = grub_util_guess_efi_drive (*curdev);
  1343. break;
  1344. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  1345. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  1346. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  1347. {
  1348. const char * ofpath = grub_util_devname_to_ofpath (*curdev);
  1349. g = xasprintf ("ieee1275/%s", ofpath);
  1350. break;
  1351. }
  1352. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  1353. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  1354. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  1355. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  1356. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  1357. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  1358. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  1359. g = grub_util_guess_baremetal_drive (*curdev);
  1360. break;
  1361. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  1362. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  1363. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  1364. case GRUB_INSTALL_PLATFORM_I386_XEN:
  1365. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  1366. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  1367. grub_util_warn ("%s", _("no hints available for your platform. Expect reduced performance"));
  1368. break;
  1369. /* pacify warning. */
  1370. case GRUB_INSTALL_PLATFORM_MAX:
  1371. break;
  1372. }
  1373. if (g)
  1374. {
  1375. grub_util_fprint_full_disk_name (load_cfg_f, g, dev);
  1376. fprintf (load_cfg_f, " ");
  1377. free (g);
  1378. }
  1379. if (dev != grub_dev)
  1380. grub_device_close (dev);
  1381. }
  1382. fprintf (load_cfg_f, "\n");
  1383. char *escaped_relpath = escape (relative_grubdir);
  1384. fprintf (load_cfg_f, "set prefix=($root)'%s'\n",
  1385. escaped_relpath);
  1386. }
  1387. else
  1388. {
  1389. /* We need to hardcode the partition number in the core image's prefix. */
  1390. char *p;
  1391. for (p = grub_drives[0]; *p; )
  1392. {
  1393. if (*p == '\\' && p[1])
  1394. {
  1395. p += 2;
  1396. continue;
  1397. }
  1398. if (*p == ',' || *p == '\0')
  1399. break;
  1400. p++;
  1401. }
  1402. prefix_drive = xasprintf ("(%s)", p);
  1403. }
  1404. }
  1405. else
  1406. {
  1407. if (config.is_cryptodisk_enabled)
  1408. {
  1409. if (grub_dev->disk)
  1410. probe_cryptodisk_uuid (grub_dev->disk);
  1411. for (curdrive = grub_drives + 1; *curdrive; curdrive++)
  1412. {
  1413. grub_device_t dev = grub_device_open (*curdrive);
  1414. if (!dev)
  1415. continue;
  1416. if (dev->disk)
  1417. probe_cryptodisk_uuid (dev->disk);
  1418. grub_device_close (dev);
  1419. }
  1420. }
  1421. prefix_drive = xasprintf ("(%s)", grub_drives[0]);
  1422. }
  1423. char mkimage_target[200];
  1424. const char *core_name = NULL;
  1425. switch (platform)
  1426. {
  1427. case GRUB_INSTALL_PLATFORM_I386_EFI:
  1428. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  1429. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  1430. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  1431. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  1432. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  1433. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  1434. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  1435. core_name = "core.efi";
  1436. snprintf (mkimage_target, sizeof (mkimage_target),
  1437. "%s-%s",
  1438. grub_install_get_platform_cpu (platform),
  1439. grub_install_get_platform_platform (platform));
  1440. break;
  1441. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  1442. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  1443. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  1444. core_name = "core.elf";
  1445. snprintf (mkimage_target, sizeof (mkimage_target),
  1446. "%s-%s-elf",
  1447. grub_install_get_platform_cpu (platform),
  1448. grub_install_get_platform_platform (platform));
  1449. break;
  1450. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  1451. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  1452. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  1453. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  1454. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  1455. case GRUB_INSTALL_PLATFORM_I386_XEN:
  1456. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  1457. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  1458. core_name = "core.elf";
  1459. snprintf (mkimage_target, sizeof (mkimage_target),
  1460. "%s-%s",
  1461. grub_install_get_platform_cpu (platform),
  1462. grub_install_get_platform_platform (platform));
  1463. break;
  1464. case GRUB_INSTALL_PLATFORM_I386_PC:
  1465. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  1466. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  1467. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  1468. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  1469. snprintf (mkimage_target, sizeof (mkimage_target),
  1470. "%s-%s",
  1471. grub_install_get_platform_cpu (platform),
  1472. grub_install_get_platform_platform (platform));
  1473. core_name = "core.img";
  1474. break;
  1475. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  1476. strcpy (mkimage_target, "sparc64-ieee1275-raw");
  1477. core_name = "core.img";
  1478. break;
  1479. /* pacify warning. */
  1480. case GRUB_INSTALL_PLATFORM_MAX:
  1481. break;
  1482. }
  1483. if (!core_name)
  1484. grub_util_error ("%s", _("You've found a bug"));
  1485. if (load_cfg_f)
  1486. fclose (load_cfg_f);
  1487. char *imgfile = grub_util_path_concat (2, platdir,
  1488. core_name);
  1489. char *prefix = xasprintf ("%s%s", prefix_drive ? : "",
  1490. relative_grubdir);
  1491. grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
  1492. /*prefix */ prefix,
  1493. /* output */ imgfile,
  1494. /* memdisk */ NULL,
  1495. have_load_cfg ? load_cfg : NULL,
  1496. /* image target */ mkimage_target, 0);
  1497. /* Backward-compatibility kludges. */
  1498. switch (platform)
  1499. {
  1500. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  1501. {
  1502. char *dst = grub_util_path_concat (2, bootdir, "grub.elf");
  1503. grub_install_copy_file (imgfile, dst, 1);
  1504. free (dst);
  1505. }
  1506. break;
  1507. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  1508. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  1509. {
  1510. char *dst = grub_util_path_concat (2, grubdir, "grub");
  1511. grub_install_copy_file (imgfile, dst, 1);
  1512. free (dst);
  1513. }
  1514. break;
  1515. case GRUB_INSTALL_PLATFORM_I386_EFI:
  1516. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  1517. {
  1518. char *dst = grub_util_path_concat (2, platdir, "grub.efi");
  1519. grub_install_make_image_wrap (/* source dir */ grub_install_source_directory,
  1520. /* prefix */ "",
  1521. /* output */ dst,
  1522. /* memdisk */ NULL,
  1523. have_load_cfg ? load_cfg : NULL,
  1524. /* image target */ mkimage_target, 0);
  1525. }
  1526. break;
  1527. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  1528. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  1529. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  1530. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  1531. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  1532. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  1533. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  1534. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  1535. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  1536. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  1537. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  1538. case GRUB_INSTALL_PLATFORM_I386_PC:
  1539. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  1540. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  1541. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  1542. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  1543. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  1544. case GRUB_INSTALL_PLATFORM_I386_XEN:
  1545. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  1546. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  1547. break;
  1548. /* pacify warning. */
  1549. case GRUB_INSTALL_PLATFORM_MAX:
  1550. break;
  1551. }
  1552. /* Perform the platform-dependent install */
  1553. switch (platform)
  1554. {
  1555. case GRUB_INSTALL_PLATFORM_I386_PC:
  1556. {
  1557. char *boot_img_src = grub_util_path_concat (2,
  1558. grub_install_source_directory,
  1559. "boot.img");
  1560. char *boot_img = grub_util_path_concat (2, platdir,
  1561. "boot.img");
  1562. grub_install_copy_file (boot_img_src, boot_img, 1);
  1563. grub_util_info ("%sgrub-bios-setup %s %s %s %s %s --directory='%s' --device-map='%s' '%s'",
  1564. /* TRANSLATORS: This is a prefix in the log to indicate that usually
  1565. a command would be executed but due to an option was skipped. */
  1566. install_bootsector ? "" : _("NOT RUNNING: "),
  1567. allow_floppy ? "--allow-floppy " : "",
  1568. verbosity ? "--verbose " : "",
  1569. force ? "--force " : "",
  1570. !fs_probe ? "--skip-fs-probe" : "",
  1571. !add_rs_codes ? "--no-rs-codes" : "",
  1572. platdir,
  1573. device_map,
  1574. install_device);
  1575. /* Now perform the installation. */
  1576. if (install_bootsector)
  1577. {
  1578. grub_util_bios_setup (platdir, "boot.img", "core.img",
  1579. install_drive, force,
  1580. fs_probe, allow_floppy, add_rs_codes,
  1581. !grub_install_is_short_mbrgap_supported ());
  1582. grub_set_install_backup_ponr ();
  1583. }
  1584. break;
  1585. }
  1586. case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
  1587. {
  1588. char *boot_img_src = grub_util_path_concat (2,
  1589. grub_install_source_directory,
  1590. "boot.img");
  1591. char *boot_img = grub_util_path_concat (2, platdir,
  1592. "boot.img");
  1593. grub_install_copy_file (boot_img_src, boot_img, 1);
  1594. grub_util_info ("%sgrub-sparc64-setup %s %s %s %s --directory='%s' --device-map='%s' '%s'",
  1595. install_bootsector ? "" : "NOT RUNNING: ",
  1596. allow_floppy ? "--allow-floppy " : "",
  1597. verbosity ? "--verbose " : "",
  1598. force ? "--force " : "",
  1599. !fs_probe ? "--skip-fs-probe" : "",
  1600. platdir,
  1601. device_map,
  1602. install_drive);
  1603. /* Now perform the installation. */
  1604. if (install_bootsector)
  1605. {
  1606. grub_util_sparc_setup (platdir, "boot.img", "core.img",
  1607. install_drive, force,
  1608. fs_probe, allow_floppy,
  1609. 0 /* unused */, 0 /* unused */ );
  1610. grub_set_install_backup_ponr ();
  1611. }
  1612. break;
  1613. }
  1614. case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
  1615. if (macppcdir)
  1616. {
  1617. char *core_services = grub_util_path_concat (4, macppcdir,
  1618. "System", "Library",
  1619. "CoreServices");
  1620. char *mach_kernel = grub_util_path_concat (2, macppcdir,
  1621. "mach_kernel");
  1622. char *grub_elf, *bootx;
  1623. FILE *f;
  1624. grub_device_t ins_dev;
  1625. char *grub_chrp = grub_util_path_concat (2,
  1626. grub_install_source_directory,
  1627. "grub.chrp");
  1628. grub_install_mkdir_p (core_services);
  1629. bootx = grub_util_path_concat (2, core_services, "BootX");
  1630. grub_install_copy_file (grub_chrp, bootx, 1);
  1631. grub_elf = grub_util_path_concat (2, core_services, "grub.elf");
  1632. grub_install_copy_file (imgfile, grub_elf, 1);
  1633. grub_set_install_backup_ponr ();
  1634. f = grub_util_fopen (mach_kernel, "a+");
  1635. if (!f)
  1636. grub_util_error (_("Can't create file: %s"), strerror (errno));
  1637. fclose (f);
  1638. fill_core_services (core_services);
  1639. ins_dev = grub_device_open (install_drive);
  1640. if (ins_dev == NULL)
  1641. grub_util_error ("%s", grub_errmsg);
  1642. bless (ins_dev, core_services, 0);
  1643. if (update_nvram)
  1644. {
  1645. const char *dev;
  1646. int partno;
  1647. partno = ins_dev->disk->partition
  1648. ? ins_dev->disk->partition->number + 1 : 0;
  1649. dev = grub_util_get_os_disk (install_device);
  1650. grub_install_register_ieee1275 (0, dev, partno,
  1651. "\\\\BootX");
  1652. }
  1653. grub_device_close (ins_dev);
  1654. free (grub_elf);
  1655. free (bootx);
  1656. free (mach_kernel);
  1657. free (grub_chrp);
  1658. break;
  1659. }
  1660. /* If a install device is defined, copy the core.elf to PReP partition. */
  1661. if (is_prep && install_device && install_device[0])
  1662. {
  1663. grub_device_t ins_dev;
  1664. ins_dev = grub_device_open (install_drive);
  1665. if (!ins_dev || !is_prep_partition (ins_dev))
  1666. {
  1667. grub_util_error ("%s", _("the chosen partition is not a PReP partition"));
  1668. }
  1669. if (is_prep_empty (ins_dev))
  1670. {
  1671. if (write_to_disk (ins_dev, imgfile))
  1672. grub_util_error ("%s", _("failed to copy Grub to the PReP partition"));
  1673. grub_set_install_backup_ponr ();
  1674. }
  1675. else
  1676. {
  1677. char *s = xasprintf ("dd if=/dev/zero of=%s", install_device);
  1678. grub_util_error (_("the PReP partition is not empty. If you are sure you want to use it, run dd to clear it: `%s'"),
  1679. s);
  1680. }
  1681. grub_device_close (ins_dev);
  1682. if (update_nvram)
  1683. grub_install_register_ieee1275 (1, grub_util_get_os_disk (install_device),
  1684. 0, NULL);
  1685. break;
  1686. }
  1687. /* fallthrough. */
  1688. case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
  1689. if (update_nvram)
  1690. {
  1691. const char *dev;
  1692. char *relpath;
  1693. int partno;
  1694. relpath = grub_make_system_path_relative_to_its_root (imgfile);
  1695. partno = grub_dev->disk->partition
  1696. ? grub_dev->disk->partition->number + 1 : 0;
  1697. dev = grub_util_get_os_disk (grub_devices[0]);
  1698. grub_install_register_ieee1275 (0, dev,
  1699. partno, relpath);
  1700. }
  1701. break;
  1702. case GRUB_INSTALL_PLATFORM_MIPS_ARC:
  1703. grub_install_sgi_setup (install_device, imgfile, "grub");
  1704. break;
  1705. case GRUB_INSTALL_PLATFORM_I386_EFI:
  1706. if (!efidir_is_mac)
  1707. {
  1708. char *dst = grub_util_path_concat (2, efidir, "grub.efi");
  1709. /* For old macs. Suggested by Peter Jones. */
  1710. grub_install_copy_file (imgfile, dst, 1);
  1711. free (dst);
  1712. }
  1713. /* Fallthrough. */
  1714. case GRUB_INSTALL_PLATFORM_X86_64_EFI:
  1715. if (efidir_is_mac)
  1716. {
  1717. char *boot_efi;
  1718. char *core_services = grub_util_path_concat (4, efidir,
  1719. "System", "Library",
  1720. "CoreServices");
  1721. char *mach_kernel = grub_util_path_concat (2, efidir,
  1722. "mach_kernel");
  1723. FILE *f;
  1724. grub_device_t ins_dev;
  1725. grub_install_mkdir_p (core_services);
  1726. boot_efi = grub_util_path_concat (2, core_services, "boot.efi");
  1727. grub_install_copy_file (imgfile, boot_efi, 1);
  1728. grub_set_install_backup_ponr ();
  1729. f = grub_util_fopen (mach_kernel, "r+");
  1730. if (!f)
  1731. grub_util_error (_("Can't create file: %s"), strerror (errno));
  1732. fclose (f);
  1733. fill_core_services(core_services);
  1734. ins_dev = grub_device_open (install_drive);
  1735. if (ins_dev == NULL)
  1736. grub_util_error ("%s", grub_errmsg);
  1737. bless (ins_dev, boot_efi, 1);
  1738. if (!removable && update_nvram)
  1739. {
  1740. /* Try to make this image bootable using the EFI Boot Manager, if available. */
  1741. int ret;
  1742. ret = grub_install_register_efi (efidir_grub_dev,
  1743. "\\System\\Library\\CoreServices",
  1744. efi_distributor);
  1745. if (ret)
  1746. grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
  1747. strerror (ret));
  1748. }
  1749. grub_device_close (ins_dev);
  1750. free (boot_efi);
  1751. free (mach_kernel);
  1752. break;
  1753. }
  1754. /* FALLTHROUGH */
  1755. case GRUB_INSTALL_PLATFORM_ARM_EFI:
  1756. case GRUB_INSTALL_PLATFORM_ARM64_EFI:
  1757. case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
  1758. case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
  1759. case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
  1760. case GRUB_INSTALL_PLATFORM_IA64_EFI:
  1761. {
  1762. char *dst = grub_util_path_concat (2, efidir, efi_file);
  1763. grub_install_copy_file (imgfile, dst, 1);
  1764. grub_set_install_backup_ponr ();
  1765. free (dst);
  1766. }
  1767. if (!removable && update_nvram)
  1768. {
  1769. char * efifile_path;
  1770. char * part;
  1771. int ret;
  1772. /* Try to make this image bootable using the EFI Boot Manager, if available. */
  1773. if (!efi_distributor || efi_distributor[0] == '\0')
  1774. grub_util_error ("%s", _("EFI bootloader id isn't specified."));
  1775. efifile_path = xasprintf ("\\EFI\\%s\\%s",
  1776. efi_distributor,
  1777. efi_file);
  1778. part = (efidir_grub_dev->disk->partition
  1779. ? grub_partition_get_name (efidir_grub_dev->disk->partition)
  1780. : 0);
  1781. grub_util_info ("Registering with EFI: distributor = `%s',"
  1782. " path = `%s', ESP at %s%s%s",
  1783. efi_distributor, efifile_path,
  1784. efidir_grub_dev->disk->name,
  1785. (part ? ",": ""), (part ? : ""));
  1786. grub_free (part);
  1787. ret = grub_install_register_efi (efidir_grub_dev,
  1788. efifile_path, efi_distributor);
  1789. if (ret)
  1790. grub_util_error (_("efibootmgr failed to register the boot entry: %s"),
  1791. strerror (ret));
  1792. }
  1793. break;
  1794. case GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON:
  1795. case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
  1796. case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
  1797. case GRUB_INSTALL_PLATFORM_I386_COREBOOT:
  1798. case GRUB_INSTALL_PLATFORM_ARM_COREBOOT:
  1799. case GRUB_INSTALL_PLATFORM_I386_MULTIBOOT:
  1800. case GRUB_INSTALL_PLATFORM_MIPSEL_ARC:
  1801. case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  1802. case GRUB_INSTALL_PLATFORM_I386_QEMU:
  1803. case GRUB_INSTALL_PLATFORM_I386_XEN:
  1804. case GRUB_INSTALL_PLATFORM_X86_64_XEN:
  1805. case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
  1806. grub_util_warn ("%s",
  1807. _("WARNING: no platform-specific install was performed"));
  1808. break;
  1809. /* pacify warning. */
  1810. case GRUB_INSTALL_PLATFORM_MAX:
  1811. break;
  1812. }
  1813. /*
  1814. * Either there are no platform specific code, or it didn't raise
  1815. * ponr. Raise it here, because usually this is already past point
  1816. * of no return. If we leave this flag false, at exit all the modules
  1817. * will be removed from the prefix which would be very confusing.
  1818. */
  1819. grub_set_install_backup_ponr ();
  1820. fprintf (stderr, "%s\n", _("Installation finished. No error reported."));
  1821. /* Free resources. */
  1822. grub_gcry_fini_all ();
  1823. grub_fini_all ();
  1824. return 0;
  1825. }