grub-install-common.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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/util/resolve.h>
  37. #include <grub/emu/hostfile.h>
  38. #include <grub/emu/config.h>
  39. #include <grub/emu/hostfile.h>
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <errno.h>
  45. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  46. char *
  47. grub_install_help_filter (int key, const char *text,
  48. void *input __attribute__ ((unused)))
  49. {
  50. switch (key)
  51. {
  52. case GRUB_INSTALL_OPTIONS_INSTALL_THEMES:
  53. return xasprintf(text, "starfield");
  54. case GRUB_INSTALL_OPTIONS_INSTALL_FONTS:
  55. return xasprintf(text, "unicode");
  56. case GRUB_INSTALL_OPTIONS_DIRECTORY:
  57. case GRUB_INSTALL_OPTIONS_DIRECTORY2:
  58. return xasprintf(text, grub_util_get_pkglibdir ());
  59. case GRUB_INSTALL_OPTIONS_LOCALE_DIRECTORY:
  60. return xasprintf(text, grub_util_get_localedir ());
  61. case GRUB_INSTALL_OPTIONS_THEMES_DIRECTORY:
  62. return grub_util_path_concat (2, grub_util_get_pkgdatadir (), "themes");
  63. default:
  64. return (char *) text;
  65. }
  66. }
  67. #pragma GCC diagnostic error "-Wformat-nonliteral"
  68. static int (*compress_func) (const char *src, const char *dest) = NULL;
  69. char *grub_install_copy_buffer;
  70. static char *dtb;
  71. int
  72. grub_install_copy_file (const char *src,
  73. const char *dst,
  74. int is_needed)
  75. {
  76. grub_util_fd_t in, out;
  77. ssize_t r;
  78. grub_util_info ("copying `%s' -> `%s'", src, dst);
  79. in = grub_util_fd_open (src, GRUB_UTIL_FD_O_RDONLY);
  80. if (!GRUB_UTIL_FD_IS_VALID (in))
  81. {
  82. if (is_needed)
  83. grub_util_error (_("cannot open `%s': %s"), src, grub_util_fd_strerror ());
  84. else
  85. grub_util_info (_("cannot open `%s': %s"), src, grub_util_fd_strerror ());
  86. return 0;
  87. }
  88. out = grub_util_fd_open (dst, GRUB_UTIL_FD_O_WRONLY
  89. | GRUB_UTIL_FD_O_CREATTRUNC);
  90. if (!GRUB_UTIL_FD_IS_VALID (out))
  91. {
  92. grub_util_error (_("cannot open `%s': %s"), dst,
  93. grub_util_fd_strerror ());
  94. grub_util_fd_close (in);
  95. return 0;
  96. }
  97. if (!grub_install_copy_buffer)
  98. grub_install_copy_buffer = xmalloc (GRUB_INSTALL_COPY_BUFFER_SIZE);
  99. while (1)
  100. {
  101. r = grub_util_fd_read (in, grub_install_copy_buffer, GRUB_INSTALL_COPY_BUFFER_SIZE);
  102. if (r <= 0)
  103. break;
  104. r = grub_util_fd_write (out, grub_install_copy_buffer, r);
  105. if (r <= 0)
  106. break;
  107. }
  108. if (grub_util_fd_sync (out) < 0)
  109. r = -1;
  110. if (grub_util_fd_close (in) < 0)
  111. r = -1;
  112. if (grub_util_fd_close (out) < 0)
  113. r = -1;
  114. if (r < 0)
  115. grub_util_error (_("cannot copy `%s' to `%s': %s"),
  116. src, dst, grub_util_fd_strerror ());
  117. return 1;
  118. }
  119. static int
  120. grub_install_compress_file (const char *in_name,
  121. const char *out_name,
  122. int is_needed)
  123. {
  124. int ret;
  125. if (!compress_func)
  126. ret = grub_install_copy_file (in_name, out_name, is_needed);
  127. else
  128. {
  129. grub_util_info ("compressing `%s' -> `%s'", in_name, out_name);
  130. ret = !compress_func (in_name, out_name);
  131. if (!ret && is_needed)
  132. grub_util_warn (_("can't compress `%s' to `%s'"), in_name, out_name);
  133. }
  134. if (!ret && is_needed)
  135. grub_util_error (_("cannot copy `%s' to `%s': %s"),
  136. in_name, out_name, grub_util_fd_strerror ());
  137. return ret;
  138. }
  139. static int
  140. is_path_separator (char c)
  141. {
  142. #if defined (__MINGW32__) || defined (__CYGWIN__)
  143. if (c == '\\')
  144. return 1;
  145. #endif
  146. if (c == '/')
  147. return 1;
  148. return 0;
  149. }
  150. void
  151. grub_install_mkdir_p (const char *dst)
  152. {
  153. char *t = xstrdup (dst);
  154. char *p;
  155. for (p = t; *p; p++)
  156. {
  157. if (is_path_separator (*p) && p != t)
  158. {
  159. char s = *p;
  160. *p = '\0';
  161. grub_util_mkdir (t);
  162. if (!grub_util_is_directory (t))
  163. grub_util_error (_("failed to make directory: '%s'"), t);
  164. *p = s;
  165. }
  166. }
  167. grub_util_mkdir (t);
  168. if (!grub_util_is_directory (t))
  169. grub_util_error (_("failed to make directory: '%s'"), t);
  170. free (t);
  171. }
  172. static int
  173. strcmp_ext (const char *a, const char *b, const char *ext)
  174. {
  175. char *bsuffix = grub_util_path_concat_ext (1, b, ext);
  176. int r = strcmp (a, bsuffix);
  177. free (bsuffix);
  178. return r;
  179. }
  180. enum clean_grub_dir_mode
  181. {
  182. CLEAN_NEW,
  183. CLEAN_BACKUP,
  184. CREATE_BACKUP,
  185. RESTORE_BACKUP
  186. };
  187. #ifdef HAVE_ATEXIT
  188. static size_t backup_dirs_size = 0;
  189. static char **backup_dirs = NULL;
  190. static pid_t backup_process = 0;
  191. static int grub_install_backup_ponr = 0;
  192. void
  193. grub_set_install_backup_ponr (void)
  194. {
  195. grub_install_backup_ponr = 1;
  196. }
  197. #endif
  198. static void
  199. clean_grub_dir_real (const char *di, enum clean_grub_dir_mode mode)
  200. {
  201. grub_util_fd_dir_t d;
  202. grub_util_fd_dirent_t de;
  203. const char *suffix = "";
  204. if ((mode == CLEAN_BACKUP) || (mode == RESTORE_BACKUP))
  205. suffix = "~";
  206. d = grub_util_fd_opendir (di);
  207. if (!d)
  208. {
  209. if (mode == CLEAN_BACKUP)
  210. return;
  211. grub_util_error (_("cannot open directory `%s': %s"),
  212. di, grub_util_fd_strerror ());
  213. }
  214. while ((de = grub_util_fd_readdir (d)))
  215. {
  216. const char *ext = strrchr (de->d_name, '.');
  217. if ((ext && (strcmp_ext (ext, ".mod", suffix) == 0
  218. || strcmp_ext (ext, ".lst", suffix) == 0
  219. || strcmp_ext (ext, ".img", suffix) == 0
  220. || strcmp_ext (ext, ".efi", suffix) == 0
  221. || strcmp_ext (ext, ".mo", suffix) == 0)
  222. && strcmp_ext (de->d_name, "menu.lst", suffix) != 0)
  223. || strcmp_ext (de->d_name, "modinfo.sh", suffix) == 0
  224. || strcmp_ext (de->d_name, "efiemu32.o", suffix) == 0
  225. || strcmp_ext (de->d_name, "efiemu64.o", suffix) == 0)
  226. {
  227. char *srcf = grub_util_path_concat (2, di, de->d_name);
  228. if (mode == CREATE_BACKUP)
  229. {
  230. char *dstf = grub_util_path_concat_ext (2, di, de->d_name, "~");
  231. if (grub_util_rename (srcf, dstf) < 0)
  232. grub_util_error (_("cannot backup `%s': %s"), srcf,
  233. grub_util_fd_strerror ());
  234. free (dstf);
  235. }
  236. else if (mode == RESTORE_BACKUP)
  237. {
  238. char *dstf = grub_util_path_concat (2, di, de->d_name);
  239. dstf[strlen (dstf) - 1] = '\0';
  240. if (grub_util_rename (srcf, dstf) < 0)
  241. grub_util_error (_("cannot restore `%s': %s"), dstf,
  242. grub_util_fd_strerror ());
  243. free (dstf);
  244. }
  245. else
  246. {
  247. if (grub_util_unlink (srcf) < 0)
  248. grub_util_error (_("cannot delete `%s': %s"), srcf,
  249. grub_util_fd_strerror ());
  250. }
  251. free (srcf);
  252. }
  253. }
  254. grub_util_fd_closedir (d);
  255. }
  256. #ifdef HAVE_ATEXIT
  257. static void
  258. restore_backup_atexit (void)
  259. {
  260. size_t i;
  261. /*
  262. * Some child inherited atexit() handler, did not clear it, and called it.
  263. * Thus skip clean or restore logic.
  264. */
  265. if (backup_process != getpid ())
  266. return;
  267. for (i = 0; i < backup_dirs_size; i++)
  268. {
  269. /*
  270. * If past point of no return simply clean the backups. Otherwise
  271. * cleanup newly installed files, and restore the backups.
  272. */
  273. if (grub_install_backup_ponr)
  274. clean_grub_dir_real (backup_dirs[i], CLEAN_BACKUP);
  275. else
  276. {
  277. clean_grub_dir_real (backup_dirs[i], CLEAN_NEW);
  278. clean_grub_dir_real (backup_dirs[i], RESTORE_BACKUP);
  279. }
  280. free (backup_dirs[i]);
  281. }
  282. backup_dirs_size = 0;
  283. free (backup_dirs);
  284. }
  285. static void
  286. append_to_backup_dirs (const char *dir)
  287. {
  288. backup_dirs = xrealloc (backup_dirs, sizeof (char *) * (backup_dirs_size + 1));
  289. backup_dirs[backup_dirs_size] = xstrdup (dir);
  290. backup_dirs_size++;
  291. if (!backup_process)
  292. {
  293. atexit (restore_backup_atexit);
  294. backup_process = getpid ();
  295. }
  296. }
  297. #else
  298. static void
  299. append_to_backup_dirs (const char *dir __attribute__ ((unused)))
  300. {
  301. }
  302. #endif
  303. static void
  304. clean_grub_dir (const char *di)
  305. {
  306. clean_grub_dir_real (di, CLEAN_BACKUP);
  307. clean_grub_dir_real (di, CREATE_BACKUP);
  308. append_to_backup_dirs (di);
  309. }
  310. struct install_list
  311. {
  312. int is_default;
  313. char **entries;
  314. size_t n_entries;
  315. size_t n_alloc;
  316. };
  317. struct install_list install_modules = { 1, 0, 0, 0 };
  318. struct install_list modules = { 1, 0, 0, 0 };
  319. struct install_list install_locales = { 1, 0, 0, 0 };
  320. struct install_list install_fonts = { 1, 0, 0, 0 };
  321. struct install_list install_themes = { 1, 0, 0, 0 };
  322. char *grub_install_source_directory = NULL;
  323. char *grub_install_locale_directory = NULL;
  324. char *grub_install_themes_directory = NULL;
  325. int
  326. grub_install_is_short_mbrgap_supported (void)
  327. {
  328. int i, j;
  329. static const char *whitelist[] =
  330. {
  331. "part_msdos", "biosdisk", "affs", "afs", "bfs", "archelp",
  332. "cpio", "cpio_be", "newc", "odc", "ext2", "fat", "exfat",
  333. "f2fs", "fshelp", "hfs", "hfsplus", "iso9660", "jfs", "minix",
  334. "minix2", "minix3", "minix_be", "minix2_be", "nilfs2", "ntfs",
  335. "ntfscomp", "reiserfs", "romfs", "sfs", "tar", "udf", "ufs1",
  336. "ufs1_be", "ufs2", "xfs"
  337. };
  338. for (i = 0; i < modules.n_entries; i++) {
  339. for (j = 0; j < ARRAY_SIZE (whitelist); j++)
  340. if (strcmp(modules.entries[i], whitelist[j]) == 0)
  341. break;
  342. if (j == ARRAY_SIZE (whitelist))
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. void
  348. grub_install_push_module (const char *val)
  349. {
  350. modules.is_default = 0;
  351. if (modules.n_entries + 1 >= modules.n_alloc)
  352. {
  353. modules.n_alloc <<= 1;
  354. if (modules.n_alloc < 16)
  355. modules.n_alloc = 16;
  356. modules.entries = xrealloc (modules.entries,
  357. modules.n_alloc * sizeof (*modules.entries));
  358. }
  359. modules.entries[modules.n_entries++] = xstrdup (val);
  360. modules.entries[modules.n_entries] = NULL;
  361. }
  362. void
  363. grub_install_pop_module (void)
  364. {
  365. modules.n_entries--;
  366. free (modules.entries[modules.n_entries]);
  367. modules.entries[modules.n_entries] = NULL;
  368. }
  369. static void
  370. handle_install_list (struct install_list *il, const char *val,
  371. int default_all)
  372. {
  373. const char *ptr;
  374. char **ce;
  375. il->is_default = 0;
  376. free (il->entries);
  377. il->entries = NULL;
  378. il->n_entries = 0;
  379. if (strcmp (val, "all") == 0 && default_all)
  380. {
  381. il->is_default = 1;
  382. return;
  383. }
  384. ptr = val;
  385. while (1)
  386. {
  387. while (*ptr && grub_isspace (*ptr))
  388. ptr++;
  389. if (!*ptr)
  390. break;
  391. while (*ptr && !grub_isspace (*ptr))
  392. ptr++;
  393. il->n_entries++;
  394. }
  395. il->n_alloc = il->n_entries + 1;
  396. il->entries = xcalloc (il->n_alloc, sizeof (il->entries[0]));
  397. ptr = val;
  398. for (ce = il->entries; ; ce++)
  399. {
  400. const char *bptr;
  401. while (*ptr && grub_isspace (*ptr))
  402. ptr++;
  403. if (!*ptr)
  404. break;
  405. bptr = ptr;
  406. while (*ptr && !grub_isspace (*ptr))
  407. ptr++;
  408. *ce = xmalloc (ptr - bptr + 1);
  409. memcpy (*ce, bptr, ptr - bptr);
  410. (*ce)[ptr - bptr] = '\0';
  411. }
  412. *ce = NULL;
  413. }
  414. static char **pubkeys;
  415. static size_t npubkeys;
  416. static char *sbat;
  417. static int disable_shim_lock;
  418. static grub_compression_t compression;
  419. static int disable_cli;
  420. int
  421. grub_install_parse (int key, char *arg)
  422. {
  423. switch (key)
  424. {
  425. case GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS:
  426. if (grub_strcmp (arg, "xz") == 0)
  427. {
  428. #ifdef HAVE_LIBLZMA
  429. compression = GRUB_COMPRESSION_XZ;
  430. #else
  431. grub_util_error ("%s",
  432. _("grub-mkimage is compiled without XZ support"));
  433. #endif
  434. }
  435. else if (grub_strcmp (arg, "none") == 0)
  436. compression = GRUB_COMPRESSION_NONE;
  437. else if (grub_strcmp (arg, "auto") == 0)
  438. compression = GRUB_COMPRESSION_AUTO;
  439. else
  440. grub_util_error (_("Unknown compression format %s"), arg);
  441. return 1;
  442. case 'k':
  443. pubkeys = xrealloc (pubkeys,
  444. sizeof (pubkeys[0])
  445. * (npubkeys + 1));
  446. pubkeys[npubkeys++] = xstrdup (arg);
  447. return 1;
  448. case GRUB_INSTALL_OPTIONS_SBAT:
  449. if (sbat)
  450. free (sbat);
  451. sbat = xstrdup (arg);
  452. return 1;
  453. case GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK:
  454. disable_shim_lock = 1;
  455. return 1;
  456. case GRUB_INSTALL_OPTIONS_DISABLE_CLI:
  457. disable_cli = 1;
  458. return 1;
  459. case GRUB_INSTALL_OPTIONS_VERBOSITY:
  460. verbosity++;
  461. return 1;
  462. case GRUB_INSTALL_OPTIONS_DIRECTORY:
  463. case GRUB_INSTALL_OPTIONS_DIRECTORY2:
  464. free (grub_install_source_directory);
  465. grub_install_source_directory = xstrdup (arg);
  466. return 1;
  467. case GRUB_INSTALL_OPTIONS_LOCALE_DIRECTORY:
  468. free (grub_install_locale_directory);
  469. grub_install_locale_directory = xstrdup (arg);
  470. return 1;
  471. case GRUB_INSTALL_OPTIONS_THEMES_DIRECTORY:
  472. free (grub_install_themes_directory);
  473. grub_install_themes_directory = xstrdup (arg);
  474. return 1;
  475. case GRUB_INSTALL_OPTIONS_INSTALL_MODULES:
  476. handle_install_list (&install_modules, arg, 0);
  477. return 1;
  478. case GRUB_INSTALL_OPTIONS_MODULES:
  479. handle_install_list (&modules, arg, 0);
  480. return 1;
  481. case GRUB_INSTALL_OPTIONS_INSTALL_LOCALES:
  482. handle_install_list (&install_locales, arg, 0);
  483. return 1;
  484. case GRUB_INSTALL_OPTIONS_INSTALL_THEMES:
  485. handle_install_list (&install_themes, arg, 0);
  486. return 1;
  487. case GRUB_INSTALL_OPTIONS_INSTALL_FONTS:
  488. handle_install_list (&install_fonts, arg, 0);
  489. return 1;
  490. case GRUB_INSTALL_OPTIONS_DTB:
  491. if (dtb)
  492. free (dtb);
  493. dtb = xstrdup (arg);
  494. return 1;
  495. case GRUB_INSTALL_OPTIONS_INSTALL_COMPRESS:
  496. if (strcmp (arg, "no") == 0
  497. || strcmp (arg, "none") == 0)
  498. {
  499. compress_func = NULL;
  500. return 1;
  501. }
  502. if (strcmp (arg, "gz") == 0)
  503. {
  504. compress_func = grub_install_compress_gzip;
  505. return 1;
  506. }
  507. if (strcmp (arg, "xz") == 0)
  508. {
  509. compress_func = grub_install_compress_xz;
  510. return 1;
  511. }
  512. if (strcmp (arg, "lzo") == 0)
  513. {
  514. compress_func = grub_install_compress_lzop;
  515. return 1;
  516. }
  517. grub_util_error (_("Unrecognized compression `%s'"), arg);
  518. case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE:
  519. return 1;
  520. default:
  521. return 0;
  522. }
  523. }
  524. static int
  525. decompressors (void)
  526. {
  527. if (compress_func == grub_install_compress_gzip)
  528. {
  529. grub_install_push_module ("gzio");
  530. return 1;
  531. }
  532. if (compress_func == grub_install_compress_xz)
  533. {
  534. grub_install_push_module ("xzio");
  535. grub_install_push_module ("gcry_crc");
  536. return 2;
  537. }
  538. if (compress_func == grub_install_compress_lzop)
  539. {
  540. grub_install_push_module ("lzopio");
  541. grub_install_push_module ("adler32");
  542. grub_install_push_module ("gcry_crc");
  543. return 3;
  544. }
  545. return 0;
  546. }
  547. void
  548. grub_install_make_image_wrap_file (const char *dir, const char *prefix,
  549. FILE *fp, const char *outname,
  550. char *memdisk_path,
  551. char *config_path,
  552. const char *mkimage_target, int note)
  553. {
  554. const struct grub_install_image_target_desc *tgt;
  555. const char *const compnames[] =
  556. {
  557. [GRUB_COMPRESSION_AUTO] = "auto",
  558. [GRUB_COMPRESSION_NONE] = "none",
  559. [GRUB_COMPRESSION_XZ] = "xz",
  560. [GRUB_COMPRESSION_LZMA] = "lzma",
  561. };
  562. grub_size_t slen = 1;
  563. char *s, *p;
  564. char **pk, **md;
  565. int dc = decompressors ();
  566. if (memdisk_path)
  567. slen += sizeof (" --memdisk ''") + grub_strlen (memdisk_path);
  568. if (config_path)
  569. slen += sizeof (" --config ''") + grub_strlen (config_path);
  570. if (dtb)
  571. slen += sizeof (" --dtb ''") + grub_strlen (dtb);
  572. if (sbat)
  573. slen += sizeof (" --sbat ''") + grub_strlen (sbat);
  574. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  575. slen += sizeof (" --pubkey ''") + grub_strlen (*pk);
  576. for (md = modules.entries; *md; md++)
  577. slen += sizeof (" ''") + grub_strlen (*md);
  578. p = s = xmalloc (slen);
  579. if (memdisk_path)
  580. {
  581. *p++ = ' ';
  582. p = grub_stpcpy (p, "--memdisk '");
  583. p = grub_stpcpy (p, memdisk_path);
  584. *p++ = '\'';
  585. }
  586. if (config_path)
  587. {
  588. *p++ = ' ';
  589. p = grub_stpcpy (p, "--config '");
  590. p = grub_stpcpy (p, config_path);
  591. *p++ = '\'';
  592. }
  593. if (dtb)
  594. {
  595. *p++ = ' ';
  596. p = grub_stpcpy (p, "--dtb '");
  597. p = grub_stpcpy (p, dtb);
  598. *p++ = '\'';
  599. }
  600. if (sbat)
  601. {
  602. *p++ = ' ';
  603. p = grub_stpcpy (p, "--sbat '");
  604. p = grub_stpcpy (p, sbat);
  605. *p++ = '\'';
  606. }
  607. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  608. {
  609. *p++ = ' ';
  610. p = grub_stpcpy (p, "--pubkey '");
  611. p = grub_stpcpy (p, *pk);
  612. *p++ = '\'';
  613. }
  614. for (md = modules.entries; *md; md++)
  615. {
  616. *p++ = ' ';
  617. *p++ = '\'';
  618. p = grub_stpcpy (p, *md);
  619. *p++ = '\'';
  620. }
  621. *p = '\0';
  622. grub_util_info ("grub-mkimage --directory '%s' --prefix '%s' --output '%s'"
  623. " --format '%s' --compression '%s'%s%s%s%s\n",
  624. dir, prefix, outname,
  625. mkimage_target, compnames[compression],
  626. note ? " --note" : "",
  627. disable_shim_lock ? " --disable-shim-lock" : "",
  628. disable_cli ? " --disable-cli" : "", s);
  629. free (s);
  630. tgt = grub_install_get_image_target (mkimage_target);
  631. if (!tgt)
  632. grub_util_error (_("unknown target format %s"), mkimage_target);
  633. grub_install_generate_image (dir, prefix, fp, outname,
  634. modules.entries, memdisk_path,
  635. pubkeys, npubkeys, config_path, tgt,
  636. note, compression, dtb, sbat,
  637. disable_shim_lock, disable_cli);
  638. while (dc--)
  639. grub_install_pop_module ();
  640. }
  641. void
  642. grub_install_make_image_wrap (const char *dir, const char *prefix,
  643. const char *outname, char *memdisk_path,
  644. char *config_path,
  645. const char *mkimage_target, int note)
  646. {
  647. FILE *fp;
  648. fp = grub_util_fopen (outname, "wb");
  649. if (! fp)
  650. grub_util_error (_("cannot open `%s': %s"), outname,
  651. strerror (errno));
  652. grub_install_make_image_wrap_file (dir, prefix, fp, outname,
  653. memdisk_path, config_path,
  654. mkimage_target, note);
  655. if (grub_util_file_sync (fp) < 0)
  656. grub_util_error (_("cannot sync `%s': %s"), outname, strerror (errno));
  657. fclose (fp);
  658. }
  659. static void
  660. copy_by_ext (const char *srcd,
  661. const char *dstd,
  662. const char *extf,
  663. int req)
  664. {
  665. grub_util_fd_dir_t d;
  666. grub_util_fd_dirent_t de;
  667. d = grub_util_fd_opendir (srcd);
  668. if (!d && !req)
  669. return;
  670. if (!d)
  671. grub_util_error (_("cannot open directory `%s': %s"),
  672. srcd, grub_util_fd_strerror ());
  673. while ((de = grub_util_fd_readdir (d)))
  674. {
  675. const char *ext = strrchr (de->d_name, '.');
  676. if (ext && strcmp (ext, extf) == 0)
  677. {
  678. char *srcf = grub_util_path_concat (2, srcd, de->d_name);
  679. char *dstf = grub_util_path_concat (2, dstd, de->d_name);
  680. grub_install_compress_file (srcf, dstf, 1);
  681. free (srcf);
  682. free (dstf);
  683. }
  684. }
  685. grub_util_fd_closedir (d);
  686. }
  687. static void
  688. copy_all (const char *srcd,
  689. const char *dstd)
  690. {
  691. grub_util_fd_dir_t d;
  692. grub_util_fd_dirent_t de;
  693. d = grub_util_fd_opendir (srcd);
  694. if (!d)
  695. grub_util_error (_("cannot open directory `%s': %s"),
  696. srcd, grub_util_fd_strerror ());
  697. while ((de = grub_util_fd_readdir (d)))
  698. {
  699. char *srcf;
  700. char *dstf;
  701. if (strcmp (de->d_name, ".") == 0
  702. || strcmp (de->d_name, "..") == 0)
  703. continue;
  704. srcf = grub_util_path_concat (2, srcd, de->d_name);
  705. if (grub_util_is_special_file (srcf)
  706. || grub_util_is_directory (srcf))
  707. {
  708. free (srcf);
  709. continue;
  710. }
  711. dstf = grub_util_path_concat (2, dstd, de->d_name);
  712. grub_install_compress_file (srcf, dstf, 1);
  713. free (srcf);
  714. free (dstf);
  715. }
  716. grub_util_fd_closedir (d);
  717. }
  718. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  719. static const char *
  720. get_localedir (void)
  721. {
  722. if (grub_install_locale_directory)
  723. return grub_install_locale_directory;
  724. else
  725. return grub_util_get_localedir ();
  726. }
  727. static void
  728. copy_locales (const char *dstd)
  729. {
  730. grub_util_fd_dir_t d;
  731. grub_util_fd_dirent_t de;
  732. const char *locale_dir = get_localedir ();
  733. d = grub_util_fd_opendir (locale_dir);
  734. if (!d)
  735. {
  736. grub_util_warn (_("cannot open directory `%s': %s"),
  737. locale_dir, grub_util_fd_strerror ());
  738. return;
  739. }
  740. while ((de = grub_util_fd_readdir (d)))
  741. {
  742. char *srcf;
  743. char *dstf;
  744. char *ext;
  745. if (strcmp (de->d_name, ".") == 0)
  746. continue;
  747. if (strcmp (de->d_name, "..") == 0)
  748. continue;
  749. ext = grub_strrchr (de->d_name, '.');
  750. if (ext && (grub_strcmp (ext, ".mo") == 0
  751. || grub_strcmp (ext, ".gmo") == 0))
  752. {
  753. srcf = grub_util_path_concat (2, locale_dir, de->d_name);
  754. dstf = grub_util_path_concat (2, dstd, de->d_name);
  755. ext = grub_strrchr (dstf, '.');
  756. grub_strcpy (ext, ".mo");
  757. }
  758. else
  759. {
  760. srcf = grub_util_path_concat_ext (4, locale_dir, de->d_name,
  761. "LC_MESSAGES", PACKAGE, ".mo");
  762. dstf = grub_util_path_concat_ext (2, dstd, de->d_name, ".mo");
  763. }
  764. grub_install_compress_file (srcf, dstf, 0);
  765. free (srcf);
  766. free (dstf);
  767. }
  768. grub_util_fd_closedir (d);
  769. }
  770. #endif
  771. static void
  772. grub_install_copy_nls(const char *src __attribute__ ((unused)),
  773. const char *dst __attribute__ ((unused)))
  774. {
  775. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  776. char *dst_locale;
  777. dst_locale = grub_util_path_concat (2, dst, "locale");
  778. grub_install_mkdir_p (dst_locale);
  779. clean_grub_dir (dst_locale);
  780. if (install_locales.is_default)
  781. {
  782. char *srcd = grub_util_path_concat (2, src, "po");
  783. copy_by_ext (srcd, dst_locale, ".mo", 0);
  784. copy_locales (dst_locale);
  785. free (srcd);
  786. }
  787. else
  788. {
  789. size_t i;
  790. const char *locale_dir = get_localedir ();
  791. for (i = 0; i < install_locales.n_entries; i++)
  792. {
  793. char *srcf = grub_util_path_concat_ext (3, src, "po",
  794. install_locales.entries[i],
  795. ".mo");
  796. char *dstf = grub_util_path_concat_ext (2, dst_locale,
  797. install_locales.entries[i],
  798. ".mo");
  799. if (grub_install_compress_file (srcf, dstf, 0))
  800. {
  801. free (srcf);
  802. free (dstf);
  803. continue;
  804. }
  805. free (srcf);
  806. srcf = grub_util_path_concat_ext (4, locale_dir,
  807. install_locales.entries[i],
  808. "LC_MESSAGES", PACKAGE, ".mo");
  809. if (grub_install_compress_file (srcf, dstf, 0) == 0)
  810. grub_util_error (_("cannot find locale `%s'"),
  811. install_locales.entries[i]);
  812. free (srcf);
  813. free (dstf);
  814. }
  815. }
  816. free (dst_locale);
  817. #endif
  818. }
  819. static struct
  820. {
  821. const char *cpu;
  822. const char *platform;
  823. } platforms[GRUB_INSTALL_PLATFORM_MAX] =
  824. {
  825. [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" },
  826. [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" },
  827. [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" },
  828. [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" },
  829. [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" },
  830. [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" },
  831. [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" },
  832. [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" },
  833. [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" },
  834. [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386", "xen_pvh" },
  835. [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" },
  836. [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" },
  837. [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" },
  838. [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" },
  839. [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" },
  840. [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" },
  841. [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" },
  842. [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" },
  843. [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" },
  844. [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" },
  845. [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" },
  846. [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" },
  847. [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64", "efi" },
  848. [GRUB_INSTALL_PLATFORM_RISCV32_EFI] = { "riscv32", "efi" },
  849. [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64", "efi" },
  850. };
  851. char *
  852. grub_install_get_platforms_string (void)
  853. {
  854. char **arr = xmalloc (sizeof (char *) * ARRAY_SIZE (platforms));
  855. int platform_strins_len = 0;
  856. char *platforms_string;
  857. char *ptr;
  858. unsigned i;
  859. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  860. {
  861. arr[i] = xasprintf ("%s-%s", platforms[i].cpu,
  862. platforms[i].platform);
  863. platform_strins_len += strlen (arr[i]) + 2;
  864. }
  865. ptr = platforms_string = xmalloc (platform_strins_len);
  866. qsort (arr, ARRAY_SIZE (platforms), sizeof (char *), grub_qsort_strcmp);
  867. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  868. {
  869. strcpy (ptr, arr[i]);
  870. ptr += strlen (arr[i]);
  871. *ptr++ = ',';
  872. *ptr++ = ' ';
  873. free (arr[i]);
  874. }
  875. ptr[-2] = 0;
  876. free (arr);
  877. return platforms_string;
  878. }
  879. char *
  880. grub_install_get_platform_name (enum grub_install_plat platid)
  881. {
  882. return xasprintf ("%s-%s", platforms[platid].cpu,
  883. platforms[platid].platform);
  884. }
  885. const char *
  886. grub_install_get_platform_cpu (enum grub_install_plat platid)
  887. {
  888. return platforms[platid].cpu;
  889. }
  890. const char *
  891. grub_install_get_platform_platform (enum grub_install_plat platid)
  892. {
  893. return platforms[platid].platform;
  894. }
  895. void
  896. grub_install_copy_files (const char *src,
  897. const char *dst,
  898. enum grub_install_plat platid)
  899. {
  900. char *dst_platform, *dst_fonts;
  901. const char *pkgdatadir = grub_util_get_pkgdatadir ();
  902. char *themes_dir;
  903. {
  904. char *platform;
  905. platform = xasprintf ("%s-%s", platforms[platid].cpu,
  906. platforms[platid].platform);
  907. dst_platform = grub_util_path_concat (2, dst, platform);
  908. free (platform);
  909. }
  910. dst_fonts = grub_util_path_concat (2, dst, "fonts");
  911. grub_install_mkdir_p (dst_platform);
  912. clean_grub_dir (dst);
  913. clean_grub_dir (dst_platform);
  914. grub_install_copy_nls(src, dst);
  915. if (install_modules.is_default)
  916. copy_by_ext (src, dst_platform, ".mod", 1);
  917. else
  918. {
  919. struct grub_util_path_list *path_list, *p;
  920. path_list = grub_util_resolve_dependencies (src, "moddep.lst",
  921. install_modules.entries);
  922. for (p = path_list; p; p = p->next)
  923. {
  924. const char *srcf = p->name;
  925. const char *dir;
  926. char *dstf;
  927. dir = grub_strrchr (srcf, '/');
  928. if (dir)
  929. dir++;
  930. else
  931. dir = srcf;
  932. dstf = grub_util_path_concat (2, dst_platform, dir);
  933. grub_install_compress_file (srcf, dstf, 1);
  934. free (dstf);
  935. }
  936. grub_util_free_path_list (path_list);
  937. }
  938. const char *pkglib_DATA[] = {"efiemu32.o", "efiemu64.o",
  939. "moddep.lst", "command.lst",
  940. "fs.lst", "partmap.lst",
  941. "parttool.lst",
  942. "video.lst", "crypto.lst",
  943. "terminal.lst", "modinfo.sh" };
  944. size_t i;
  945. for (i = 0; i < ARRAY_SIZE (pkglib_DATA); i++)
  946. {
  947. char *srcf = grub_util_path_concat (2, src, pkglib_DATA[i]);
  948. char *dstf = grub_util_path_concat (2, dst_platform, pkglib_DATA[i]);
  949. if (i == 0 || i == 1)
  950. grub_install_compress_file (srcf, dstf, 0);
  951. else
  952. grub_install_compress_file (srcf, dstf, 1);
  953. free (srcf);
  954. free (dstf);
  955. }
  956. if (install_themes.is_default)
  957. {
  958. install_themes.is_default = 0;
  959. install_themes.n_entries = 1;
  960. install_themes.entries = xmalloc (2 * sizeof (install_themes.entries[0]));
  961. install_themes.entries[0] = xstrdup ("starfield");
  962. install_themes.entries[1] = NULL;
  963. }
  964. if (grub_install_themes_directory)
  965. themes_dir = xstrdup (grub_install_themes_directory);
  966. else
  967. themes_dir = grub_util_path_concat (2, grub_util_get_pkgdatadir (),
  968. "themes");
  969. for (i = 0; i < install_themes.n_entries; i++)
  970. {
  971. char *srcf = grub_util_path_concat (3, themes_dir,
  972. install_themes.entries[i],
  973. "theme.txt");
  974. if (grub_util_is_regular (srcf))
  975. {
  976. char *srcd = grub_util_path_concat (2, themes_dir,
  977. install_themes.entries[i]);
  978. char *dstd = grub_util_path_concat (3, dst, "themes",
  979. install_themes.entries[i]);
  980. grub_install_mkdir_p (dstd);
  981. copy_all (srcd, dstd);
  982. free (srcd);
  983. free (dstd);
  984. }
  985. free (srcf);
  986. }
  987. free (themes_dir);
  988. if (install_fonts.is_default)
  989. {
  990. install_fonts.is_default = 0;
  991. install_fonts.n_entries = 1;
  992. install_fonts.entries = xmalloc (2 * sizeof (install_fonts.entries[0]));
  993. install_fonts.entries[0] = xstrdup ("unicode");
  994. install_fonts.entries[1] = NULL;
  995. }
  996. grub_install_mkdir_p (dst_fonts);
  997. for (i = 0; i < install_fonts.n_entries; i++)
  998. {
  999. char *srcf = grub_util_path_concat_ext (2, pkgdatadir,
  1000. install_fonts.entries[i],
  1001. ".pf2");
  1002. char *dstf = grub_util_path_concat_ext (2, dst_fonts,
  1003. install_fonts.entries[i],
  1004. ".pf2");
  1005. grub_install_compress_file (srcf, dstf, 0);
  1006. free (srcf);
  1007. free (dstf);
  1008. }
  1009. free (dst_platform);
  1010. free (dst_fonts);
  1011. }
  1012. enum grub_install_plat
  1013. grub_install_get_target (const char *src)
  1014. {
  1015. char *fn;
  1016. grub_util_fd_t f;
  1017. char buf[8192];
  1018. ssize_t r;
  1019. char *c, *pl, *p;
  1020. size_t i;
  1021. fn = grub_util_path_concat (2, src, "modinfo.sh");
  1022. f = grub_util_fd_open (fn, GRUB_UTIL_FD_O_RDONLY);
  1023. if (!GRUB_UTIL_FD_IS_VALID (f))
  1024. grub_util_error (_("%s doesn't exist. Please specify --target or --directory"),
  1025. fn);
  1026. r = grub_util_fd_read (f, buf, sizeof (buf) - 1);
  1027. if (r < 0)
  1028. grub_util_error (_("cannot read `%s': %s"), fn, strerror (errno));
  1029. grub_util_fd_close (f);
  1030. buf[r] = '\0';
  1031. c = strstr (buf, "grub_modinfo_target_cpu=");
  1032. if (!c || (c != buf && !grub_isspace (*(c-1))))
  1033. grub_util_error (_("invalid modinfo file `%s'"), fn);
  1034. pl = strstr (buf, "grub_modinfo_platform=");
  1035. if (!pl || (pl != buf && !grub_isspace (*(pl-1))))
  1036. grub_util_error (_("invalid modinfo file `%s'"), fn);
  1037. c += sizeof ("grub_modinfo_target_cpu=") - 1;
  1038. pl += sizeof ("grub_modinfo_platform=") - 1;
  1039. for (p = c; *p && !grub_isspace (*p); p++);
  1040. *p = '\0';
  1041. for (p = pl; *p && !grub_isspace (*p); p++);
  1042. *p = '\0';
  1043. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  1044. if (strcmp (platforms[i].cpu, c) == 0
  1045. && strcmp (platforms[i].platform, pl) == 0)
  1046. {
  1047. free (fn);
  1048. return i;
  1049. }
  1050. grub_util_error (_("Unknown platform `%s-%s'"), c, pl);
  1051. }
  1052. void
  1053. grub_util_unlink_recursive (const char *name)
  1054. {
  1055. grub_util_fd_dir_t d;
  1056. grub_util_fd_dirent_t de;
  1057. d = grub_util_fd_opendir (name);
  1058. while ((de = grub_util_fd_readdir (d)))
  1059. {
  1060. char *fp;
  1061. if (strcmp (de->d_name, ".") == 0)
  1062. continue;
  1063. if (strcmp (de->d_name, "..") == 0)
  1064. continue;
  1065. fp = grub_util_path_concat (2, name, de->d_name);
  1066. if (grub_util_is_special_file (fp))
  1067. {
  1068. free (fp);
  1069. continue;
  1070. }
  1071. if (grub_util_is_regular (fp))
  1072. grub_util_unlink (fp);
  1073. else if (grub_util_is_directory (fp))
  1074. grub_util_unlink_recursive (fp);
  1075. free (fp);
  1076. }
  1077. grub_util_rmdir (name);
  1078. grub_util_fd_closedir (d);
  1079. }