grub-install-common.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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 += sizeof (" --memdisk ''") + grub_strlen (memdisk_path);
  564. if (config_path)
  565. slen += sizeof (" --config ''") + grub_strlen (config_path);
  566. if (dtb)
  567. slen += sizeof (" --dtb ''") + grub_strlen (dtb);
  568. if (sbat)
  569. slen += sizeof (" --sbat ''") + grub_strlen (sbat);
  570. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  571. slen += sizeof (" --pubkey ''") + grub_strlen (*pk);
  572. for (md = modules.entries; *md; md++)
  573. slen += sizeof (" ''") + grub_strlen (*md);
  574. p = s = xmalloc (slen);
  575. if (memdisk_path)
  576. {
  577. *p++ = ' ';
  578. p = grub_stpcpy (p, "--memdisk '");
  579. p = grub_stpcpy (p, memdisk_path);
  580. *p++ = '\'';
  581. }
  582. if (config_path)
  583. {
  584. *p++ = ' ';
  585. p = grub_stpcpy (p, "--config '");
  586. p = grub_stpcpy (p, config_path);
  587. *p++ = '\'';
  588. }
  589. if (dtb)
  590. {
  591. *p++ = ' ';
  592. p = grub_stpcpy (p, "--dtb '");
  593. p = grub_stpcpy (p, dtb);
  594. *p++ = '\'';
  595. }
  596. if (sbat)
  597. {
  598. *p++ = ' ';
  599. p = grub_stpcpy (p, "--sbat '");
  600. p = grub_stpcpy (p, sbat);
  601. *p++ = '\'';
  602. }
  603. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  604. {
  605. *p++ = ' ';
  606. p = grub_stpcpy (p, "--pubkey '");
  607. p = grub_stpcpy (p, *pk);
  608. *p++ = '\'';
  609. }
  610. for (md = modules.entries; *md; md++)
  611. {
  612. *p++ = ' ';
  613. *p++ = '\'';
  614. p = grub_stpcpy (p, *md);
  615. *p++ = '\'';
  616. }
  617. *p = '\0';
  618. grub_util_info ("grub-mkimage --directory '%s' --prefix '%s' --output '%s'"
  619. " --format '%s' --compression '%s'%s%s%s\n",
  620. dir, prefix, outname,
  621. mkimage_target, compnames[compression],
  622. note ? " --note" : "",
  623. disable_shim_lock ? " --disable-shim-lock" : "", s);
  624. free (s);
  625. tgt = grub_install_get_image_target (mkimage_target);
  626. if (!tgt)
  627. grub_util_error (_("unknown target format %s"), mkimage_target);
  628. grub_install_generate_image (dir, prefix, fp, outname,
  629. modules.entries, memdisk_path,
  630. pubkeys, npubkeys, config_path, tgt,
  631. note, compression, dtb, sbat,
  632. disable_shim_lock);
  633. while (dc--)
  634. grub_install_pop_module ();
  635. }
  636. void
  637. grub_install_make_image_wrap (const char *dir, const char *prefix,
  638. const char *outname, char *memdisk_path,
  639. char *config_path,
  640. const char *mkimage_target, int note)
  641. {
  642. FILE *fp;
  643. fp = grub_util_fopen (outname, "wb");
  644. if (! fp)
  645. grub_util_error (_("cannot open `%s': %s"), outname,
  646. strerror (errno));
  647. grub_install_make_image_wrap_file (dir, prefix, fp, outname,
  648. memdisk_path, config_path,
  649. mkimage_target, note);
  650. if (grub_util_file_sync (fp) < 0)
  651. grub_util_error (_("cannot sync `%s': %s"), outname, strerror (errno));
  652. fclose (fp);
  653. }
  654. static void
  655. copy_by_ext (const char *srcd,
  656. const char *dstd,
  657. const char *extf,
  658. int req)
  659. {
  660. grub_util_fd_dir_t d;
  661. grub_util_fd_dirent_t de;
  662. d = grub_util_fd_opendir (srcd);
  663. if (!d && !req)
  664. return;
  665. if (!d)
  666. grub_util_error (_("cannot open directory `%s': %s"),
  667. srcd, grub_util_fd_strerror ());
  668. while ((de = grub_util_fd_readdir (d)))
  669. {
  670. const char *ext = strrchr (de->d_name, '.');
  671. if (ext && strcmp (ext, extf) == 0)
  672. {
  673. char *srcf = grub_util_path_concat (2, srcd, de->d_name);
  674. char *dstf = grub_util_path_concat (2, dstd, de->d_name);
  675. grub_install_compress_file (srcf, dstf, 1);
  676. free (srcf);
  677. free (dstf);
  678. }
  679. }
  680. grub_util_fd_closedir (d);
  681. }
  682. static void
  683. copy_all (const char *srcd,
  684. const char *dstd)
  685. {
  686. grub_util_fd_dir_t d;
  687. grub_util_fd_dirent_t de;
  688. d = grub_util_fd_opendir (srcd);
  689. if (!d)
  690. grub_util_error (_("cannot open directory `%s': %s"),
  691. srcd, grub_util_fd_strerror ());
  692. while ((de = grub_util_fd_readdir (d)))
  693. {
  694. char *srcf;
  695. char *dstf;
  696. if (strcmp (de->d_name, ".") == 0
  697. || strcmp (de->d_name, "..") == 0)
  698. continue;
  699. srcf = grub_util_path_concat (2, srcd, de->d_name);
  700. if (grub_util_is_special_file (srcf)
  701. || grub_util_is_directory (srcf))
  702. {
  703. free (srcf);
  704. continue;
  705. }
  706. dstf = grub_util_path_concat (2, dstd, de->d_name);
  707. grub_install_compress_file (srcf, dstf, 1);
  708. free (srcf);
  709. free (dstf);
  710. }
  711. grub_util_fd_closedir (d);
  712. }
  713. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  714. static const char *
  715. get_localedir (void)
  716. {
  717. if (grub_install_locale_directory)
  718. return grub_install_locale_directory;
  719. else
  720. return grub_util_get_localedir ();
  721. }
  722. static void
  723. copy_locales (const char *dstd)
  724. {
  725. grub_util_fd_dir_t d;
  726. grub_util_fd_dirent_t de;
  727. const char *locale_dir = get_localedir ();
  728. d = grub_util_fd_opendir (locale_dir);
  729. if (!d)
  730. {
  731. grub_util_warn (_("cannot open directory `%s': %s"),
  732. locale_dir, grub_util_fd_strerror ());
  733. return;
  734. }
  735. while ((de = grub_util_fd_readdir (d)))
  736. {
  737. char *srcf;
  738. char *dstf;
  739. char *ext;
  740. if (strcmp (de->d_name, ".") == 0)
  741. continue;
  742. if (strcmp (de->d_name, "..") == 0)
  743. continue;
  744. ext = grub_strrchr (de->d_name, '.');
  745. if (ext && (grub_strcmp (ext, ".mo") == 0
  746. || grub_strcmp (ext, ".gmo") == 0))
  747. {
  748. srcf = grub_util_path_concat (2, locale_dir, de->d_name);
  749. dstf = grub_util_path_concat (2, dstd, de->d_name);
  750. ext = grub_strrchr (dstf, '.');
  751. grub_strcpy (ext, ".mo");
  752. }
  753. else
  754. {
  755. srcf = grub_util_path_concat_ext (4, locale_dir, de->d_name,
  756. "LC_MESSAGES", PACKAGE, ".mo");
  757. dstf = grub_util_path_concat_ext (2, dstd, de->d_name, ".mo");
  758. }
  759. grub_install_compress_file (srcf, dstf, 0);
  760. free (srcf);
  761. free (dstf);
  762. }
  763. grub_util_fd_closedir (d);
  764. }
  765. #endif
  766. static void
  767. grub_install_copy_nls(const char *src __attribute__ ((unused)),
  768. const char *dst __attribute__ ((unused)))
  769. {
  770. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  771. char *dst_locale;
  772. dst_locale = grub_util_path_concat (2, dst, "locale");
  773. grub_install_mkdir_p (dst_locale);
  774. clean_grub_dir (dst_locale);
  775. if (install_locales.is_default)
  776. {
  777. char *srcd = grub_util_path_concat (2, src, "po");
  778. copy_by_ext (srcd, dst_locale, ".mo", 0);
  779. copy_locales (dst_locale);
  780. free (srcd);
  781. }
  782. else
  783. {
  784. size_t i;
  785. const char *locale_dir = get_localedir ();
  786. for (i = 0; i < install_locales.n_entries; i++)
  787. {
  788. char *srcf = grub_util_path_concat_ext (3, src, "po",
  789. install_locales.entries[i],
  790. ".mo");
  791. char *dstf = grub_util_path_concat_ext (2, dst_locale,
  792. install_locales.entries[i],
  793. ".mo");
  794. if (grub_install_compress_file (srcf, dstf, 0))
  795. {
  796. free (srcf);
  797. free (dstf);
  798. continue;
  799. }
  800. free (srcf);
  801. srcf = grub_util_path_concat_ext (4, locale_dir,
  802. install_locales.entries[i],
  803. "LC_MESSAGES", PACKAGE, ".mo");
  804. if (grub_install_compress_file (srcf, dstf, 0) == 0)
  805. grub_util_error (_("cannot find locale `%s'"),
  806. install_locales.entries[i]);
  807. free (srcf);
  808. free (dstf);
  809. }
  810. }
  811. free (dst_locale);
  812. #endif
  813. }
  814. static struct
  815. {
  816. const char *cpu;
  817. const char *platform;
  818. } platforms[GRUB_INSTALL_PLATFORM_MAX] =
  819. {
  820. [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" },
  821. [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" },
  822. [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" },
  823. [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" },
  824. [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" },
  825. [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" },
  826. [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" },
  827. [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" },
  828. [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" },
  829. [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386", "xen_pvh" },
  830. [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" },
  831. [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" },
  832. [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" },
  833. [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" },
  834. [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" },
  835. [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" },
  836. [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" },
  837. [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" },
  838. [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" },
  839. [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" },
  840. [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" },
  841. [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" },
  842. [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64", "efi" },
  843. [GRUB_INSTALL_PLATFORM_RISCV32_EFI] = { "riscv32", "efi" },
  844. [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64", "efi" },
  845. };
  846. char *
  847. grub_install_get_platforms_string (void)
  848. {
  849. char **arr = xmalloc (sizeof (char *) * ARRAY_SIZE (platforms));
  850. int platform_strins_len = 0;
  851. char *platforms_string;
  852. char *ptr;
  853. unsigned i;
  854. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  855. {
  856. arr[i] = xasprintf ("%s-%s", platforms[i].cpu,
  857. platforms[i].platform);
  858. platform_strins_len += strlen (arr[i]) + 2;
  859. }
  860. ptr = platforms_string = xmalloc (platform_strins_len);
  861. qsort (arr, ARRAY_SIZE (platforms), sizeof (char *), grub_qsort_strcmp);
  862. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  863. {
  864. strcpy (ptr, arr[i]);
  865. ptr += strlen (arr[i]);
  866. *ptr++ = ',';
  867. *ptr++ = ' ';
  868. free (arr[i]);
  869. }
  870. ptr[-2] = 0;
  871. free (arr);
  872. return platforms_string;
  873. }
  874. char *
  875. grub_install_get_platform_name (enum grub_install_plat platid)
  876. {
  877. return xasprintf ("%s-%s", platforms[platid].cpu,
  878. platforms[platid].platform);
  879. }
  880. const char *
  881. grub_install_get_platform_cpu (enum grub_install_plat platid)
  882. {
  883. return platforms[platid].cpu;
  884. }
  885. const char *
  886. grub_install_get_platform_platform (enum grub_install_plat platid)
  887. {
  888. return platforms[platid].platform;
  889. }
  890. void
  891. grub_install_copy_files (const char *src,
  892. const char *dst,
  893. enum grub_install_plat platid)
  894. {
  895. char *dst_platform, *dst_fonts;
  896. const char *pkgdatadir = grub_util_get_pkgdatadir ();
  897. char *themes_dir;
  898. {
  899. char *platform;
  900. platform = xasprintf ("%s-%s", platforms[platid].cpu,
  901. platforms[platid].platform);
  902. dst_platform = grub_util_path_concat (2, dst, platform);
  903. free (platform);
  904. }
  905. dst_fonts = grub_util_path_concat (2, dst, "fonts");
  906. grub_install_mkdir_p (dst_platform);
  907. clean_grub_dir (dst);
  908. clean_grub_dir (dst_platform);
  909. grub_install_copy_nls(src, dst);
  910. if (install_modules.is_default)
  911. copy_by_ext (src, dst_platform, ".mod", 1);
  912. else
  913. {
  914. struct grub_util_path_list *path_list, *p;
  915. path_list = grub_util_resolve_dependencies (src, "moddep.lst",
  916. install_modules.entries);
  917. for (p = path_list; p; p = p->next)
  918. {
  919. const char *srcf = p->name;
  920. const char *dir;
  921. char *dstf;
  922. dir = grub_strrchr (srcf, '/');
  923. if (dir)
  924. dir++;
  925. else
  926. dir = srcf;
  927. dstf = grub_util_path_concat (2, dst_platform, dir);
  928. grub_install_compress_file (srcf, dstf, 1);
  929. free (dstf);
  930. }
  931. grub_util_free_path_list (path_list);
  932. }
  933. const char *pkglib_DATA[] = {"efiemu32.o", "efiemu64.o",
  934. "moddep.lst", "command.lst",
  935. "fs.lst", "partmap.lst",
  936. "parttool.lst",
  937. "video.lst", "crypto.lst",
  938. "terminal.lst", "modinfo.sh" };
  939. size_t i;
  940. for (i = 0; i < ARRAY_SIZE (pkglib_DATA); i++)
  941. {
  942. char *srcf = grub_util_path_concat (2, src, pkglib_DATA[i]);
  943. char *dstf = grub_util_path_concat (2, dst_platform, pkglib_DATA[i]);
  944. if (i == 0 || i == 1)
  945. grub_install_compress_file (srcf, dstf, 0);
  946. else
  947. grub_install_compress_file (srcf, dstf, 1);
  948. free (srcf);
  949. free (dstf);
  950. }
  951. if (install_themes.is_default)
  952. {
  953. install_themes.is_default = 0;
  954. install_themes.n_entries = 1;
  955. install_themes.entries = xmalloc (2 * sizeof (install_themes.entries[0]));
  956. install_themes.entries[0] = xstrdup ("starfield");
  957. install_themes.entries[1] = NULL;
  958. }
  959. if (grub_install_themes_directory)
  960. themes_dir = xstrdup (grub_install_themes_directory);
  961. else
  962. themes_dir = grub_util_path_concat (2, grub_util_get_pkgdatadir (),
  963. "themes");
  964. for (i = 0; i < install_themes.n_entries; i++)
  965. {
  966. char *srcf = grub_util_path_concat (3, themes_dir,
  967. install_themes.entries[i],
  968. "theme.txt");
  969. if (grub_util_is_regular (srcf))
  970. {
  971. char *srcd = grub_util_path_concat (2, themes_dir,
  972. install_themes.entries[i]);
  973. char *dstd = grub_util_path_concat (3, dst, "themes",
  974. install_themes.entries[i]);
  975. grub_install_mkdir_p (dstd);
  976. copy_all (srcd, dstd);
  977. free (srcd);
  978. free (dstd);
  979. }
  980. free (srcf);
  981. }
  982. free (themes_dir);
  983. if (install_fonts.is_default)
  984. {
  985. install_fonts.is_default = 0;
  986. install_fonts.n_entries = 1;
  987. install_fonts.entries = xmalloc (2 * sizeof (install_fonts.entries[0]));
  988. install_fonts.entries[0] = xstrdup ("unicode");
  989. install_fonts.entries[1] = NULL;
  990. }
  991. grub_install_mkdir_p (dst_fonts);
  992. for (i = 0; i < install_fonts.n_entries; i++)
  993. {
  994. char *srcf = grub_util_path_concat_ext (2, pkgdatadir,
  995. install_fonts.entries[i],
  996. ".pf2");
  997. char *dstf = grub_util_path_concat_ext (2, dst_fonts,
  998. install_fonts.entries[i],
  999. ".pf2");
  1000. grub_install_compress_file (srcf, dstf, 0);
  1001. free (srcf);
  1002. free (dstf);
  1003. }
  1004. free (dst_platform);
  1005. free (dst_fonts);
  1006. }
  1007. enum grub_install_plat
  1008. grub_install_get_target (const char *src)
  1009. {
  1010. char *fn;
  1011. grub_util_fd_t f;
  1012. char buf[8192];
  1013. ssize_t r;
  1014. char *c, *pl, *p;
  1015. size_t i;
  1016. fn = grub_util_path_concat (2, src, "modinfo.sh");
  1017. f = grub_util_fd_open (fn, GRUB_UTIL_FD_O_RDONLY);
  1018. if (!GRUB_UTIL_FD_IS_VALID (f))
  1019. grub_util_error (_("%s doesn't exist. Please specify --target or --directory"),
  1020. fn);
  1021. r = grub_util_fd_read (f, buf, sizeof (buf) - 1);
  1022. if (r < 0)
  1023. grub_util_error (_("cannot read `%s': %s"), fn, strerror (errno));
  1024. grub_util_fd_close (f);
  1025. buf[r] = '\0';
  1026. c = strstr (buf, "grub_modinfo_target_cpu=");
  1027. if (!c || (c != buf && !grub_isspace (*(c-1))))
  1028. grub_util_error (_("invalid modinfo file `%s'"), fn);
  1029. pl = strstr (buf, "grub_modinfo_platform=");
  1030. if (!pl || (pl != buf && !grub_isspace (*(pl-1))))
  1031. grub_util_error (_("invalid modinfo file `%s'"), fn);
  1032. c += sizeof ("grub_modinfo_target_cpu=") - 1;
  1033. pl += sizeof ("grub_modinfo_platform=") - 1;
  1034. for (p = c; *p && !grub_isspace (*p); p++);
  1035. *p = '\0';
  1036. for (p = pl; *p && !grub_isspace (*p); p++);
  1037. *p = '\0';
  1038. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  1039. if (strcmp (platforms[i].cpu, c) == 0
  1040. && strcmp (platforms[i].platform, pl) == 0)
  1041. {
  1042. free (fn);
  1043. return i;
  1044. }
  1045. grub_util_error (_("Unknown platform `%s-%s'"), c, pl);
  1046. }
  1047. void
  1048. grub_util_unlink_recursive (const char *name)
  1049. {
  1050. grub_util_fd_dir_t d;
  1051. grub_util_fd_dirent_t de;
  1052. d = grub_util_fd_opendir (name);
  1053. while ((de = grub_util_fd_readdir (d)))
  1054. {
  1055. char *fp;
  1056. if (strcmp (de->d_name, ".") == 0)
  1057. continue;
  1058. if (strcmp (de->d_name, "..") == 0)
  1059. continue;
  1060. fp = grub_util_path_concat (2, name, de->d_name);
  1061. if (grub_util_is_special_file (fp))
  1062. {
  1063. free (fp);
  1064. continue;
  1065. }
  1066. if (grub_util_is_regular (fp))
  1067. grub_util_unlink (fp);
  1068. else if (grub_util_is_directory (fp))
  1069. grub_util_unlink_recursive (fp);
  1070. free (fp);
  1071. }
  1072. grub_util_rmdir (name);
  1073. grub_util_fd_closedir (d);
  1074. }