grub-install-common.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. int
  420. grub_install_parse (int key, char *arg)
  421. {
  422. switch (key)
  423. {
  424. case GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS:
  425. if (grub_strcmp (arg, "xz") == 0)
  426. {
  427. #ifdef HAVE_LIBLZMA
  428. compression = GRUB_COMPRESSION_XZ;
  429. #else
  430. grub_util_error ("%s",
  431. _("grub-mkimage is compiled without XZ support"));
  432. #endif
  433. }
  434. else if (grub_strcmp (arg, "none") == 0)
  435. compression = GRUB_COMPRESSION_NONE;
  436. else if (grub_strcmp (arg, "auto") == 0)
  437. compression = GRUB_COMPRESSION_AUTO;
  438. else
  439. grub_util_error (_("Unknown compression format %s"), arg);
  440. return 1;
  441. case 'k':
  442. pubkeys = xrealloc (pubkeys,
  443. sizeof (pubkeys[0])
  444. * (npubkeys + 1));
  445. pubkeys[npubkeys++] = xstrdup (arg);
  446. return 1;
  447. case GRUB_INSTALL_OPTIONS_SBAT:
  448. if (sbat)
  449. free (sbat);
  450. sbat = xstrdup (arg);
  451. return 1;
  452. case GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK:
  453. disable_shim_lock = 1;
  454. return 1;
  455. case GRUB_INSTALL_OPTIONS_VERBOSITY:
  456. verbosity++;
  457. return 1;
  458. case GRUB_INSTALL_OPTIONS_DIRECTORY:
  459. case GRUB_INSTALL_OPTIONS_DIRECTORY2:
  460. free (grub_install_source_directory);
  461. grub_install_source_directory = xstrdup (arg);
  462. return 1;
  463. case GRUB_INSTALL_OPTIONS_LOCALE_DIRECTORY:
  464. free (grub_install_locale_directory);
  465. grub_install_locale_directory = xstrdup (arg);
  466. return 1;
  467. case GRUB_INSTALL_OPTIONS_THEMES_DIRECTORY:
  468. free (grub_install_themes_directory);
  469. grub_install_themes_directory = xstrdup (arg);
  470. return 1;
  471. case GRUB_INSTALL_OPTIONS_INSTALL_MODULES:
  472. handle_install_list (&install_modules, arg, 0);
  473. return 1;
  474. case GRUB_INSTALL_OPTIONS_MODULES:
  475. handle_install_list (&modules, arg, 0);
  476. return 1;
  477. case GRUB_INSTALL_OPTIONS_INSTALL_LOCALES:
  478. handle_install_list (&install_locales, arg, 0);
  479. return 1;
  480. case GRUB_INSTALL_OPTIONS_INSTALL_THEMES:
  481. handle_install_list (&install_themes, arg, 0);
  482. return 1;
  483. case GRUB_INSTALL_OPTIONS_INSTALL_FONTS:
  484. handle_install_list (&install_fonts, arg, 0);
  485. return 1;
  486. case GRUB_INSTALL_OPTIONS_DTB:
  487. if (dtb)
  488. free (dtb);
  489. dtb = xstrdup (arg);
  490. return 1;
  491. case GRUB_INSTALL_OPTIONS_INSTALL_COMPRESS:
  492. if (strcmp (arg, "no") == 0
  493. || strcmp (arg, "none") == 0)
  494. {
  495. compress_func = NULL;
  496. return 1;
  497. }
  498. if (strcmp (arg, "gz") == 0)
  499. {
  500. compress_func = grub_install_compress_gzip;
  501. return 1;
  502. }
  503. if (strcmp (arg, "xz") == 0)
  504. {
  505. compress_func = grub_install_compress_xz;
  506. return 1;
  507. }
  508. if (strcmp (arg, "lzo") == 0)
  509. {
  510. compress_func = grub_install_compress_lzop;
  511. return 1;
  512. }
  513. grub_util_error (_("Unrecognized compression `%s'"), arg);
  514. case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE:
  515. return 1;
  516. default:
  517. return 0;
  518. }
  519. }
  520. static int
  521. decompressors (void)
  522. {
  523. if (compress_func == grub_install_compress_gzip)
  524. {
  525. grub_install_push_module ("gzio");
  526. return 1;
  527. }
  528. if (compress_func == grub_install_compress_xz)
  529. {
  530. grub_install_push_module ("xzio");
  531. grub_install_push_module ("gcry_crc");
  532. return 2;
  533. }
  534. if (compress_func == grub_install_compress_lzop)
  535. {
  536. grub_install_push_module ("lzopio");
  537. grub_install_push_module ("adler32");
  538. grub_install_push_module ("gcry_crc");
  539. return 3;
  540. }
  541. return 0;
  542. }
  543. void
  544. grub_install_make_image_wrap_file (const char *dir, const char *prefix,
  545. FILE *fp, const char *outname,
  546. char *memdisk_path,
  547. char *config_path,
  548. const char *mkimage_target, int note)
  549. {
  550. const struct grub_install_image_target_desc *tgt;
  551. const char *const compnames[] =
  552. {
  553. [GRUB_COMPRESSION_AUTO] = "auto",
  554. [GRUB_COMPRESSION_NONE] = "none",
  555. [GRUB_COMPRESSION_XZ] = "xz",
  556. [GRUB_COMPRESSION_LZMA] = "lzma",
  557. };
  558. grub_size_t slen = 1;
  559. char *s, *p;
  560. char **pk, **md;
  561. int dc = decompressors ();
  562. if (memdisk_path)
  563. slen += 20 + grub_strlen (memdisk_path);
  564. if (config_path)
  565. slen += 20 + grub_strlen (config_path);
  566. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  567. slen += 20 + grub_strlen (*pk);
  568. for (md = modules.entries; *md; md++)
  569. {
  570. slen += 10 + grub_strlen (*md);
  571. }
  572. p = s = xmalloc (slen);
  573. if (memdisk_path)
  574. {
  575. p = grub_stpcpy (p, "--memdisk '");
  576. p = grub_stpcpy (p, memdisk_path);
  577. *p++ = '\'';
  578. *p++ = ' ';
  579. }
  580. if (config_path)
  581. {
  582. p = grub_stpcpy (p, "--config '");
  583. p = grub_stpcpy (p, config_path);
  584. *p++ = '\'';
  585. *p++ = ' ';
  586. }
  587. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  588. {
  589. p = grub_stpcpy (p, "--pubkey '");
  590. p = grub_stpcpy (p, *pk);
  591. *p++ = '\'';
  592. *p++ = ' ';
  593. }
  594. for (md = modules.entries; *md; md++)
  595. {
  596. *p++ = '\'';
  597. p = grub_stpcpy (p, *md);
  598. *p++ = '\'';
  599. *p++ = ' ';
  600. }
  601. *p = '\0';
  602. grub_util_info ("grub-mkimage --directory '%s' --prefix '%s'"
  603. " --output '%s' "
  604. " --dtb '%s' "
  605. "--sbat '%s' "
  606. "--format '%s' --compression '%s' %s %s %s\n",
  607. dir, prefix,
  608. outname, dtb ? : "", sbat ? : "", mkimage_target,
  609. compnames[compression], note ? "--note" : "",
  610. disable_shim_lock ? "--disable-shim-lock" : "", s);
  611. free (s);
  612. tgt = grub_install_get_image_target (mkimage_target);
  613. if (!tgt)
  614. grub_util_error (_("unknown target format %s"), mkimage_target);
  615. grub_install_generate_image (dir, prefix, fp, outname,
  616. modules.entries, memdisk_path,
  617. pubkeys, npubkeys, config_path, tgt,
  618. note, compression, dtb, sbat,
  619. disable_shim_lock);
  620. while (dc--)
  621. grub_install_pop_module ();
  622. }
  623. void
  624. grub_install_make_image_wrap (const char *dir, const char *prefix,
  625. const char *outname, char *memdisk_path,
  626. char *config_path,
  627. const char *mkimage_target, int note)
  628. {
  629. FILE *fp;
  630. fp = grub_util_fopen (outname, "wb");
  631. if (! fp)
  632. grub_util_error (_("cannot open `%s': %s"), outname,
  633. strerror (errno));
  634. grub_install_make_image_wrap_file (dir, prefix, fp, outname,
  635. memdisk_path, config_path,
  636. mkimage_target, note);
  637. if (grub_util_file_sync (fp) < 0)
  638. grub_util_error (_("cannot sync `%s': %s"), outname, strerror (errno));
  639. fclose (fp);
  640. }
  641. static void
  642. copy_by_ext (const char *srcd,
  643. const char *dstd,
  644. const char *extf,
  645. int req)
  646. {
  647. grub_util_fd_dir_t d;
  648. grub_util_fd_dirent_t de;
  649. d = grub_util_fd_opendir (srcd);
  650. if (!d && !req)
  651. return;
  652. if (!d)
  653. grub_util_error (_("cannot open directory `%s': %s"),
  654. srcd, grub_util_fd_strerror ());
  655. while ((de = grub_util_fd_readdir (d)))
  656. {
  657. const char *ext = strrchr (de->d_name, '.');
  658. if (ext && strcmp (ext, extf) == 0)
  659. {
  660. char *srcf = grub_util_path_concat (2, srcd, de->d_name);
  661. char *dstf = grub_util_path_concat (2, dstd, de->d_name);
  662. grub_install_compress_file (srcf, dstf, 1);
  663. free (srcf);
  664. free (dstf);
  665. }
  666. }
  667. grub_util_fd_closedir (d);
  668. }
  669. static void
  670. copy_all (const char *srcd,
  671. const char *dstd)
  672. {
  673. grub_util_fd_dir_t d;
  674. grub_util_fd_dirent_t de;
  675. d = grub_util_fd_opendir (srcd);
  676. if (!d)
  677. grub_util_error (_("cannot open directory `%s': %s"),
  678. srcd, grub_util_fd_strerror ());
  679. while ((de = grub_util_fd_readdir (d)))
  680. {
  681. char *srcf;
  682. char *dstf;
  683. if (strcmp (de->d_name, ".") == 0
  684. || strcmp (de->d_name, "..") == 0)
  685. continue;
  686. srcf = grub_util_path_concat (2, srcd, de->d_name);
  687. if (grub_util_is_special_file (srcf)
  688. || grub_util_is_directory (srcf))
  689. {
  690. free (srcf);
  691. continue;
  692. }
  693. dstf = grub_util_path_concat (2, dstd, de->d_name);
  694. grub_install_compress_file (srcf, dstf, 1);
  695. free (srcf);
  696. free (dstf);
  697. }
  698. grub_util_fd_closedir (d);
  699. }
  700. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  701. static const char *
  702. get_localedir (void)
  703. {
  704. if (grub_install_locale_directory)
  705. return grub_install_locale_directory;
  706. else
  707. return grub_util_get_localedir ();
  708. }
  709. static void
  710. copy_locales (const char *dstd)
  711. {
  712. grub_util_fd_dir_t d;
  713. grub_util_fd_dirent_t de;
  714. const char *locale_dir = get_localedir ();
  715. d = grub_util_fd_opendir (locale_dir);
  716. if (!d)
  717. {
  718. grub_util_warn (_("cannot open directory `%s': %s"),
  719. locale_dir, grub_util_fd_strerror ());
  720. return;
  721. }
  722. while ((de = grub_util_fd_readdir (d)))
  723. {
  724. char *srcf;
  725. char *dstf;
  726. char *ext;
  727. if (strcmp (de->d_name, ".") == 0)
  728. continue;
  729. if (strcmp (de->d_name, "..") == 0)
  730. continue;
  731. ext = grub_strrchr (de->d_name, '.');
  732. if (ext && (grub_strcmp (ext, ".mo") == 0
  733. || grub_strcmp (ext, ".gmo") == 0))
  734. {
  735. srcf = grub_util_path_concat (2, locale_dir, de->d_name);
  736. dstf = grub_util_path_concat (2, dstd, de->d_name);
  737. ext = grub_strrchr (dstf, '.');
  738. grub_strcpy (ext, ".mo");
  739. }
  740. else
  741. {
  742. srcf = grub_util_path_concat_ext (4, locale_dir, de->d_name,
  743. "LC_MESSAGES", PACKAGE, ".mo");
  744. dstf = grub_util_path_concat_ext (2, dstd, de->d_name, ".mo");
  745. }
  746. grub_install_compress_file (srcf, dstf, 0);
  747. free (srcf);
  748. free (dstf);
  749. }
  750. grub_util_fd_closedir (d);
  751. }
  752. #endif
  753. static void
  754. grub_install_copy_nls(const char *src __attribute__ ((unused)),
  755. const char *dst __attribute__ ((unused)))
  756. {
  757. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  758. char *dst_locale;
  759. dst_locale = grub_util_path_concat (2, dst, "locale");
  760. grub_install_mkdir_p (dst_locale);
  761. clean_grub_dir (dst_locale);
  762. if (install_locales.is_default)
  763. {
  764. char *srcd = grub_util_path_concat (2, src, "po");
  765. copy_by_ext (srcd, dst_locale, ".mo", 0);
  766. copy_locales (dst_locale);
  767. free (srcd);
  768. }
  769. else
  770. {
  771. size_t i;
  772. const char *locale_dir = get_localedir ();
  773. for (i = 0; i < install_locales.n_entries; i++)
  774. {
  775. char *srcf = grub_util_path_concat_ext (3, src, "po",
  776. install_locales.entries[i],
  777. ".mo");
  778. char *dstf = grub_util_path_concat_ext (2, dst_locale,
  779. install_locales.entries[i],
  780. ".mo");
  781. if (grub_install_compress_file (srcf, dstf, 0))
  782. {
  783. free (srcf);
  784. free (dstf);
  785. continue;
  786. }
  787. free (srcf);
  788. srcf = grub_util_path_concat_ext (4, locale_dir,
  789. install_locales.entries[i],
  790. "LC_MESSAGES", PACKAGE, ".mo");
  791. if (grub_install_compress_file (srcf, dstf, 0) == 0)
  792. grub_util_error (_("cannot find locale `%s'"),
  793. install_locales.entries[i]);
  794. free (srcf);
  795. free (dstf);
  796. }
  797. }
  798. free (dst_locale);
  799. #endif
  800. }
  801. static struct
  802. {
  803. const char *cpu;
  804. const char *platform;
  805. } platforms[GRUB_INSTALL_PLATFORM_MAX] =
  806. {
  807. [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" },
  808. [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" },
  809. [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" },
  810. [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" },
  811. [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" },
  812. [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" },
  813. [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" },
  814. [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" },
  815. [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" },
  816. [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386", "xen_pvh" },
  817. [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" },
  818. [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" },
  819. [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" },
  820. [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" },
  821. [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" },
  822. [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" },
  823. [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" },
  824. [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" },
  825. [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" },
  826. [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" },
  827. [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" },
  828. [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" },
  829. [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64", "efi" },
  830. [GRUB_INSTALL_PLATFORM_RISCV32_EFI] = { "riscv32", "efi" },
  831. [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64", "efi" },
  832. };
  833. char *
  834. grub_install_get_platforms_string (void)
  835. {
  836. char **arr = xmalloc (sizeof (char *) * ARRAY_SIZE (platforms));
  837. int platform_strins_len = 0;
  838. char *platforms_string;
  839. char *ptr;
  840. unsigned i;
  841. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  842. {
  843. arr[i] = xasprintf ("%s-%s", platforms[i].cpu,
  844. platforms[i].platform);
  845. platform_strins_len += strlen (arr[i]) + 2;
  846. }
  847. ptr = platforms_string = xmalloc (platform_strins_len);
  848. qsort (arr, ARRAY_SIZE (platforms), sizeof (char *), grub_qsort_strcmp);
  849. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  850. {
  851. strcpy (ptr, arr[i]);
  852. ptr += strlen (arr[i]);
  853. *ptr++ = ',';
  854. *ptr++ = ' ';
  855. free (arr[i]);
  856. }
  857. ptr[-2] = 0;
  858. free (arr);
  859. return platforms_string;
  860. }
  861. char *
  862. grub_install_get_platform_name (enum grub_install_plat platid)
  863. {
  864. return xasprintf ("%s-%s", platforms[platid].cpu,
  865. platforms[platid].platform);
  866. }
  867. const char *
  868. grub_install_get_platform_cpu (enum grub_install_plat platid)
  869. {
  870. return platforms[platid].cpu;
  871. }
  872. const char *
  873. grub_install_get_platform_platform (enum grub_install_plat platid)
  874. {
  875. return platforms[platid].platform;
  876. }
  877. void
  878. grub_install_copy_files (const char *src,
  879. const char *dst,
  880. enum grub_install_plat platid)
  881. {
  882. char *dst_platform, *dst_fonts;
  883. const char *pkgdatadir = grub_util_get_pkgdatadir ();
  884. char *themes_dir;
  885. {
  886. char *platform;
  887. platform = xasprintf ("%s-%s", platforms[platid].cpu,
  888. platforms[platid].platform);
  889. dst_platform = grub_util_path_concat (2, dst, platform);
  890. free (platform);
  891. }
  892. dst_fonts = grub_util_path_concat (2, dst, "fonts");
  893. grub_install_mkdir_p (dst_platform);
  894. clean_grub_dir (dst);
  895. clean_grub_dir (dst_platform);
  896. grub_install_copy_nls(src, dst);
  897. if (install_modules.is_default)
  898. copy_by_ext (src, dst_platform, ".mod", 1);
  899. else
  900. {
  901. struct grub_util_path_list *path_list, *p;
  902. path_list = grub_util_resolve_dependencies (src, "moddep.lst",
  903. install_modules.entries);
  904. for (p = path_list; p; p = p->next)
  905. {
  906. const char *srcf = p->name;
  907. const char *dir;
  908. char *dstf;
  909. dir = grub_strrchr (srcf, '/');
  910. if (dir)
  911. dir++;
  912. else
  913. dir = srcf;
  914. dstf = grub_util_path_concat (2, dst_platform, dir);
  915. grub_install_compress_file (srcf, dstf, 1);
  916. free (dstf);
  917. }
  918. grub_util_free_path_list (path_list);
  919. }
  920. const char *pkglib_DATA[] = {"efiemu32.o", "efiemu64.o",
  921. "moddep.lst", "command.lst",
  922. "fs.lst", "partmap.lst",
  923. "parttool.lst",
  924. "video.lst", "crypto.lst",
  925. "terminal.lst", "modinfo.sh" };
  926. size_t i;
  927. for (i = 0; i < ARRAY_SIZE (pkglib_DATA); i++)
  928. {
  929. char *srcf = grub_util_path_concat (2, src, pkglib_DATA[i]);
  930. char *dstf = grub_util_path_concat (2, dst_platform, pkglib_DATA[i]);
  931. if (i == 0 || i == 1)
  932. grub_install_compress_file (srcf, dstf, 0);
  933. else
  934. grub_install_compress_file (srcf, dstf, 1);
  935. free (srcf);
  936. free (dstf);
  937. }
  938. if (install_themes.is_default)
  939. {
  940. install_themes.is_default = 0;
  941. install_themes.n_entries = 1;
  942. install_themes.entries = xmalloc (2 * sizeof (install_themes.entries[0]));
  943. install_themes.entries[0] = xstrdup ("starfield");
  944. install_themes.entries[1] = NULL;
  945. }
  946. if (grub_install_themes_directory)
  947. themes_dir = xstrdup (grub_install_themes_directory);
  948. else
  949. themes_dir = grub_util_path_concat (2, grub_util_get_pkgdatadir (),
  950. "themes");
  951. for (i = 0; i < install_themes.n_entries; i++)
  952. {
  953. char *srcf = grub_util_path_concat (3, themes_dir,
  954. install_themes.entries[i],
  955. "theme.txt");
  956. if (grub_util_is_regular (srcf))
  957. {
  958. char *srcd = grub_util_path_concat (2, themes_dir,
  959. install_themes.entries[i]);
  960. char *dstd = grub_util_path_concat (3, dst, "themes",
  961. install_themes.entries[i]);
  962. grub_install_mkdir_p (dstd);
  963. copy_all (srcd, dstd);
  964. free (srcd);
  965. free (dstd);
  966. }
  967. free (srcf);
  968. }
  969. free (themes_dir);
  970. if (install_fonts.is_default)
  971. {
  972. install_fonts.is_default = 0;
  973. install_fonts.n_entries = 1;
  974. install_fonts.entries = xmalloc (2 * sizeof (install_fonts.entries[0]));
  975. install_fonts.entries[0] = xstrdup ("unicode");
  976. install_fonts.entries[1] = NULL;
  977. }
  978. grub_install_mkdir_p (dst_fonts);
  979. for (i = 0; i < install_fonts.n_entries; i++)
  980. {
  981. char *srcf = grub_util_path_concat_ext (2, pkgdatadir,
  982. install_fonts.entries[i],
  983. ".pf2");
  984. char *dstf = grub_util_path_concat_ext (2, dst_fonts,
  985. install_fonts.entries[i],
  986. ".pf2");
  987. grub_install_compress_file (srcf, dstf, 0);
  988. free (srcf);
  989. free (dstf);
  990. }
  991. free (dst_platform);
  992. free (dst_fonts);
  993. }
  994. enum grub_install_plat
  995. grub_install_get_target (const char *src)
  996. {
  997. char *fn;
  998. grub_util_fd_t f;
  999. char buf[8192];
  1000. ssize_t r;
  1001. char *c, *pl, *p;
  1002. size_t i;
  1003. fn = grub_util_path_concat (2, src, "modinfo.sh");
  1004. f = grub_util_fd_open (fn, GRUB_UTIL_FD_O_RDONLY);
  1005. if (!GRUB_UTIL_FD_IS_VALID (f))
  1006. grub_util_error (_("%s doesn't exist. Please specify --target or --directory"),
  1007. fn);
  1008. r = grub_util_fd_read (f, buf, sizeof (buf) - 1);
  1009. if (r < 0)
  1010. grub_util_error (_("cannot read `%s': %s"), fn, strerror (errno));
  1011. grub_util_fd_close (f);
  1012. buf[r] = '\0';
  1013. c = strstr (buf, "grub_modinfo_target_cpu=");
  1014. if (!c || (c != buf && !grub_isspace (*(c-1))))
  1015. grub_util_error (_("invalid modinfo file `%s'"), fn);
  1016. pl = strstr (buf, "grub_modinfo_platform=");
  1017. if (!pl || (pl != buf && !grub_isspace (*(pl-1))))
  1018. grub_util_error (_("invalid modinfo file `%s'"), fn);
  1019. c += sizeof ("grub_modinfo_target_cpu=") - 1;
  1020. pl += sizeof ("grub_modinfo_platform=") - 1;
  1021. for (p = c; *p && !grub_isspace (*p); p++);
  1022. *p = '\0';
  1023. for (p = pl; *p && !grub_isspace (*p); p++);
  1024. *p = '\0';
  1025. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  1026. if (strcmp (platforms[i].cpu, c) == 0
  1027. && strcmp (platforms[i].platform, pl) == 0)
  1028. {
  1029. free (fn);
  1030. return i;
  1031. }
  1032. grub_util_error (_("Unknown platform `%s-%s'"), c, pl);
  1033. }
  1034. void
  1035. grub_util_unlink_recursive (const char *name)
  1036. {
  1037. grub_util_fd_dir_t d;
  1038. grub_util_fd_dirent_t de;
  1039. d = grub_util_fd_opendir (name);
  1040. while ((de = grub_util_fd_readdir (d)))
  1041. {
  1042. char *fp;
  1043. if (strcmp (de->d_name, ".") == 0)
  1044. continue;
  1045. if (strcmp (de->d_name, "..") == 0)
  1046. continue;
  1047. fp = grub_util_path_concat (2, name, de->d_name);
  1048. if (grub_util_is_special_file (fp))
  1049. {
  1050. free (fp);
  1051. continue;
  1052. }
  1053. if (grub_util_is_regular (fp))
  1054. grub_util_unlink (fp);
  1055. else if (grub_util_is_directory (fp))
  1056. grub_util_unlink_recursive (fp);
  1057. free (fp);
  1058. }
  1059. grub_util_rmdir (name);
  1060. grub_util_fd_closedir (d);
  1061. }