grub-install-common.c 30 KB

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