grub-install-common.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. grub_util_fd_write (out, grub_install_copy_buffer, r);
  105. }
  106. grub_util_fd_sync (out);
  107. grub_util_fd_close (in);
  108. grub_util_fd_close (out);
  109. if (r < 0)
  110. grub_util_error (_("cannot copy `%s' to `%s': %s"),
  111. src, dst, grub_util_fd_strerror ());
  112. return 1;
  113. }
  114. static int
  115. grub_install_compress_file (const char *in_name,
  116. const char *out_name,
  117. int is_needed)
  118. {
  119. int ret;
  120. if (!compress_func)
  121. ret = grub_install_copy_file (in_name, out_name, is_needed);
  122. else
  123. {
  124. grub_util_info ("compressing `%s' -> `%s'", in_name, out_name);
  125. ret = !compress_func (in_name, out_name);
  126. if (!ret && is_needed)
  127. grub_util_warn (_("can't compress `%s' to `%s'"), in_name, out_name);
  128. }
  129. if (!ret && is_needed)
  130. grub_util_error (_("cannot copy `%s' to `%s': %s"),
  131. in_name, out_name, grub_util_fd_strerror ());
  132. return ret;
  133. }
  134. static int
  135. is_path_separator (char c)
  136. {
  137. #if defined (__MINGW32__) || defined (__CYGWIN__)
  138. if (c == '\\')
  139. return 1;
  140. #endif
  141. if (c == '/')
  142. return 1;
  143. return 0;
  144. }
  145. void
  146. grub_install_mkdir_p (const char *dst)
  147. {
  148. char *t = xstrdup (dst);
  149. char *p;
  150. for (p = t; *p; p++)
  151. {
  152. if (is_path_separator (*p))
  153. {
  154. char s = *p;
  155. *p = '\0';
  156. grub_util_mkdir (t);
  157. *p = s;
  158. }
  159. }
  160. grub_util_mkdir (t);
  161. free (t);
  162. }
  163. static void
  164. clean_grub_dir (const char *di)
  165. {
  166. grub_util_fd_dir_t d;
  167. grub_util_fd_dirent_t de;
  168. d = grub_util_fd_opendir (di);
  169. if (!d)
  170. grub_util_error (_("cannot open directory `%s': %s"),
  171. di, grub_util_fd_strerror ());
  172. while ((de = grub_util_fd_readdir (d)))
  173. {
  174. const char *ext = strrchr (de->d_name, '.');
  175. if ((ext && (strcmp (ext, ".mod") == 0
  176. || strcmp (ext, ".lst") == 0
  177. || strcmp (ext, ".img") == 0
  178. || strcmp (ext, ".mo") == 0)
  179. && strcmp (de->d_name, "menu.lst") != 0)
  180. || strcmp (de->d_name, "efiemu32.o") == 0
  181. || strcmp (de->d_name, "efiemu64.o") == 0)
  182. {
  183. char *x = grub_util_path_concat (2, di, de->d_name);
  184. if (grub_util_unlink (x) < 0)
  185. grub_util_error (_("cannot delete `%s': %s"), x,
  186. grub_util_fd_strerror ());
  187. free (x);
  188. }
  189. }
  190. grub_util_fd_closedir (d);
  191. }
  192. struct install_list
  193. {
  194. int is_default;
  195. char **entries;
  196. size_t n_entries;
  197. size_t n_alloc;
  198. };
  199. struct install_list install_modules = { 1, 0, 0, 0 };
  200. struct install_list modules = { 1, 0, 0, 0 };
  201. struct install_list install_locales = { 1, 0, 0, 0 };
  202. struct install_list install_fonts = { 1, 0, 0, 0 };
  203. struct install_list install_themes = { 1, 0, 0, 0 };
  204. char *grub_install_source_directory = NULL;
  205. char *grub_install_locale_directory = NULL;
  206. char *grub_install_themes_directory = NULL;
  207. void
  208. grub_install_push_module (const char *val)
  209. {
  210. modules.is_default = 0;
  211. if (modules.n_entries + 1 >= modules.n_alloc)
  212. {
  213. modules.n_alloc <<= 1;
  214. if (modules.n_alloc < 16)
  215. modules.n_alloc = 16;
  216. modules.entries = xrealloc (modules.entries,
  217. modules.n_alloc * sizeof (*modules.entries));
  218. }
  219. modules.entries[modules.n_entries++] = xstrdup (val);
  220. modules.entries[modules.n_entries] = NULL;
  221. }
  222. void
  223. grub_install_pop_module (void)
  224. {
  225. modules.n_entries--;
  226. free (modules.entries[modules.n_entries]);
  227. modules.entries[modules.n_entries] = NULL;
  228. }
  229. static void
  230. handle_install_list (struct install_list *il, const char *val,
  231. int default_all)
  232. {
  233. const char *ptr;
  234. char **ce;
  235. il->is_default = 0;
  236. free (il->entries);
  237. il->entries = NULL;
  238. il->n_entries = 0;
  239. if (strcmp (val, "all") == 0 && default_all)
  240. {
  241. il->is_default = 1;
  242. return;
  243. }
  244. ptr = val;
  245. while (1)
  246. {
  247. while (*ptr && grub_isspace (*ptr))
  248. ptr++;
  249. if (!*ptr)
  250. break;
  251. while (*ptr && !grub_isspace (*ptr))
  252. ptr++;
  253. il->n_entries++;
  254. }
  255. il->n_alloc = il->n_entries + 1;
  256. il->entries = xmalloc (il->n_alloc * sizeof (il->entries[0]));
  257. ptr = val;
  258. for (ce = il->entries; ; ce++)
  259. {
  260. const char *bptr;
  261. while (*ptr && grub_isspace (*ptr))
  262. ptr++;
  263. if (!*ptr)
  264. break;
  265. bptr = ptr;
  266. while (*ptr && !grub_isspace (*ptr))
  267. ptr++;
  268. *ce = xmalloc (ptr - bptr + 1);
  269. memcpy (*ce, bptr, ptr - bptr);
  270. (*ce)[ptr - bptr] = '\0';
  271. }
  272. *ce = NULL;
  273. }
  274. static char **pubkeys;
  275. static size_t npubkeys;
  276. static grub_compression_t compression;
  277. int
  278. grub_install_parse (int key, char *arg)
  279. {
  280. switch (key)
  281. {
  282. case 'C':
  283. if (grub_strcmp (arg, "xz") == 0)
  284. {
  285. #ifdef HAVE_LIBLZMA
  286. compression = GRUB_COMPRESSION_XZ;
  287. #else
  288. grub_util_error ("%s",
  289. _("grub-mkimage is compiled without XZ support"));
  290. #endif
  291. }
  292. else if (grub_strcmp (arg, "none") == 0)
  293. compression = GRUB_COMPRESSION_NONE;
  294. else if (grub_strcmp (arg, "auto") == 0)
  295. compression = GRUB_COMPRESSION_AUTO;
  296. else
  297. grub_util_error (_("Unknown compression format %s"), arg);
  298. return 1;
  299. case 'k':
  300. pubkeys = xrealloc (pubkeys,
  301. sizeof (pubkeys[0])
  302. * (npubkeys + 1));
  303. pubkeys[npubkeys++] = xstrdup (arg);
  304. return 1;
  305. case GRUB_INSTALL_OPTIONS_VERBOSITY:
  306. verbosity++;
  307. return 1;
  308. case GRUB_INSTALL_OPTIONS_DIRECTORY:
  309. case GRUB_INSTALL_OPTIONS_DIRECTORY2:
  310. free (grub_install_source_directory);
  311. grub_install_source_directory = xstrdup (arg);
  312. return 1;
  313. case GRUB_INSTALL_OPTIONS_LOCALE_DIRECTORY:
  314. free (grub_install_locale_directory);
  315. grub_install_locale_directory = xstrdup (arg);
  316. return 1;
  317. case GRUB_INSTALL_OPTIONS_THEMES_DIRECTORY:
  318. free (grub_install_themes_directory);
  319. grub_install_themes_directory = xstrdup (arg);
  320. return 1;
  321. case GRUB_INSTALL_OPTIONS_INSTALL_MODULES:
  322. handle_install_list (&install_modules, arg, 0);
  323. return 1;
  324. case GRUB_INSTALL_OPTIONS_MODULES:
  325. handle_install_list (&modules, arg, 0);
  326. return 1;
  327. case GRUB_INSTALL_OPTIONS_INSTALL_LOCALES:
  328. handle_install_list (&install_locales, arg, 0);
  329. return 1;
  330. case GRUB_INSTALL_OPTIONS_INSTALL_THEMES:
  331. handle_install_list (&install_themes, arg, 0);
  332. return 1;
  333. case GRUB_INSTALL_OPTIONS_INSTALL_FONTS:
  334. handle_install_list (&install_fonts, arg, 0);
  335. return 1;
  336. case GRUB_INSTALL_OPTIONS_DTB:
  337. if (dtb)
  338. free (dtb);
  339. dtb = xstrdup (arg);
  340. return 1;
  341. case GRUB_INSTALL_OPTIONS_INSTALL_COMPRESS:
  342. if (strcmp (arg, "no") == 0
  343. || strcmp (arg, "none") == 0)
  344. {
  345. compress_func = NULL;
  346. return 1;
  347. }
  348. if (strcmp (arg, "gz") == 0)
  349. {
  350. compress_func = grub_install_compress_gzip;
  351. return 1;
  352. }
  353. if (strcmp (arg, "xz") == 0)
  354. {
  355. compress_func = grub_install_compress_xz;
  356. return 1;
  357. }
  358. if (strcmp (arg, "lzo") == 0)
  359. {
  360. compress_func = grub_install_compress_lzop;
  361. return 1;
  362. }
  363. grub_util_error (_("Unrecognized compression `%s'"), arg);
  364. case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE:
  365. return 1;
  366. default:
  367. return 0;
  368. }
  369. }
  370. static int
  371. decompressors (void)
  372. {
  373. if (compress_func == grub_install_compress_gzip)
  374. {
  375. grub_install_push_module ("gzio");
  376. return 1;
  377. }
  378. if (compress_func == grub_install_compress_xz)
  379. {
  380. grub_install_push_module ("xzio");
  381. grub_install_push_module ("gcry_crc");
  382. return 2;
  383. }
  384. if (compress_func == grub_install_compress_lzop)
  385. {
  386. grub_install_push_module ("lzopio");
  387. grub_install_push_module ("adler32");
  388. grub_install_push_module ("gcry_crc");
  389. return 3;
  390. }
  391. return 0;
  392. }
  393. void
  394. grub_install_make_image_wrap_file (const char *dir, const char *prefix,
  395. FILE *fp, const char *outname,
  396. char *memdisk_path,
  397. char *config_path,
  398. const char *mkimage_target, int note)
  399. {
  400. const struct grub_install_image_target_desc *tgt;
  401. const char *const compnames[] =
  402. {
  403. [GRUB_COMPRESSION_AUTO] = "auto",
  404. [GRUB_COMPRESSION_NONE] = "none",
  405. [GRUB_COMPRESSION_XZ] = "xz",
  406. [GRUB_COMPRESSION_LZMA] = "lzma",
  407. };
  408. grub_size_t slen = 1;
  409. char *s, *p;
  410. char **pk, **md;
  411. int dc = decompressors ();
  412. if (memdisk_path)
  413. slen += 20 + grub_strlen (memdisk_path);
  414. if (config_path)
  415. slen += 20 + grub_strlen (config_path);
  416. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  417. slen += 20 + grub_strlen (*pk);
  418. for (md = modules.entries; *md; md++)
  419. {
  420. slen += 10 + grub_strlen (*md);
  421. }
  422. p = s = xmalloc (slen);
  423. if (memdisk_path)
  424. {
  425. p = grub_stpcpy (p, "--memdisk '");
  426. p = grub_stpcpy (p, memdisk_path);
  427. *p++ = '\'';
  428. *p++ = ' ';
  429. }
  430. if (config_path)
  431. {
  432. p = grub_stpcpy (p, "--config '");
  433. p = grub_stpcpy (p, config_path);
  434. *p++ = '\'';
  435. *p++ = ' ';
  436. }
  437. for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
  438. {
  439. p = grub_stpcpy (p, "--pubkey '");
  440. p = grub_stpcpy (p, *pk);
  441. *p++ = '\'';
  442. *p++ = ' ';
  443. }
  444. for (md = modules.entries; *md; md++)
  445. {
  446. *p++ = '\'';
  447. p = grub_stpcpy (p, *md);
  448. *p++ = '\'';
  449. *p++ = ' ';
  450. }
  451. *p = '\0';
  452. grub_util_info ("grub-mkimage --directory '%s' --prefix '%s'"
  453. " --output '%s' "
  454. " --dtb '%s' "
  455. "--format '%s' --compression '%s' %s %s\n",
  456. dir, prefix,
  457. outname, dtb ? : "", mkimage_target,
  458. compnames[compression], note ? "--note" : "", s);
  459. free (s);
  460. tgt = grub_install_get_image_target (mkimage_target);
  461. if (!tgt)
  462. grub_util_error (_("unknown target format %s"), mkimage_target);
  463. grub_install_generate_image (dir, prefix, fp, outname,
  464. modules.entries, memdisk_path,
  465. pubkeys, npubkeys, config_path, tgt,
  466. note, compression, dtb);
  467. while (dc--)
  468. grub_install_pop_module ();
  469. }
  470. void
  471. grub_install_make_image_wrap (const char *dir, const char *prefix,
  472. const char *outname, char *memdisk_path,
  473. char *config_path,
  474. const char *mkimage_target, int note)
  475. {
  476. FILE *fp;
  477. fp = grub_util_fopen (outname, "wb");
  478. if (! fp)
  479. grub_util_error (_("cannot open `%s': %s"), outname,
  480. strerror (errno));
  481. grub_install_make_image_wrap_file (dir, prefix, fp, outname,
  482. memdisk_path, config_path,
  483. mkimage_target, note);
  484. grub_util_file_sync (fp);
  485. fclose (fp);
  486. }
  487. static void
  488. copy_by_ext (const char *srcd,
  489. const char *dstd,
  490. const char *extf,
  491. int req)
  492. {
  493. grub_util_fd_dir_t d;
  494. grub_util_fd_dirent_t de;
  495. d = grub_util_fd_opendir (srcd);
  496. if (!d && !req)
  497. return;
  498. if (!d)
  499. grub_util_error (_("cannot open directory `%s': %s"),
  500. srcd, grub_util_fd_strerror ());
  501. while ((de = grub_util_fd_readdir (d)))
  502. {
  503. const char *ext = strrchr (de->d_name, '.');
  504. if (ext && strcmp (ext, extf) == 0)
  505. {
  506. char *srcf = grub_util_path_concat (2, srcd, de->d_name);
  507. char *dstf = grub_util_path_concat (2, dstd, de->d_name);
  508. grub_install_compress_file (srcf, dstf, 1);
  509. free (srcf);
  510. free (dstf);
  511. }
  512. }
  513. grub_util_fd_closedir (d);
  514. }
  515. static void
  516. copy_all (const char *srcd,
  517. const char *dstd)
  518. {
  519. grub_util_fd_dir_t d;
  520. grub_util_fd_dirent_t de;
  521. d = grub_util_fd_opendir (srcd);
  522. if (!d)
  523. grub_util_error (_("cannot open directory `%s': %s"),
  524. srcd, grub_util_fd_strerror ());
  525. while ((de = grub_util_fd_readdir (d)))
  526. {
  527. char *srcf;
  528. char *dstf;
  529. if (strcmp (de->d_name, ".") == 0
  530. || strcmp (de->d_name, "..") == 0)
  531. continue;
  532. srcf = grub_util_path_concat (2, srcd, de->d_name);
  533. if (grub_util_is_special_file (srcf)
  534. || grub_util_is_directory (srcf))
  535. continue;
  536. dstf = grub_util_path_concat (2, dstd, de->d_name);
  537. grub_install_compress_file (srcf, dstf, 1);
  538. free (srcf);
  539. free (dstf);
  540. }
  541. grub_util_fd_closedir (d);
  542. }
  543. static const char *
  544. get_localedir (void)
  545. {
  546. if (grub_install_locale_directory)
  547. return grub_install_locale_directory;
  548. else
  549. return grub_util_get_localedir ();
  550. }
  551. static void
  552. copy_locales (const char *dstd)
  553. {
  554. grub_util_fd_dir_t d;
  555. grub_util_fd_dirent_t de;
  556. const char *locale_dir = get_localedir ();
  557. d = grub_util_fd_opendir (locale_dir);
  558. if (!d)
  559. {
  560. grub_util_warn (_("cannot open directory `%s': %s"),
  561. locale_dir, grub_util_fd_strerror ());
  562. return;
  563. }
  564. while ((de = grub_util_fd_readdir (d)))
  565. {
  566. char *srcf;
  567. char *dstf;
  568. char *ext;
  569. if (strcmp (de->d_name, ".") == 0)
  570. continue;
  571. if (strcmp (de->d_name, "..") == 0)
  572. continue;
  573. ext = grub_strrchr (de->d_name, '.');
  574. if (ext && (grub_strcmp (ext, ".mo") == 0
  575. || grub_strcmp (ext, ".gmo") == 0))
  576. {
  577. srcf = grub_util_path_concat (2, locale_dir, de->d_name);
  578. dstf = grub_util_path_concat (2, dstd, de->d_name);
  579. ext = grub_strrchr (dstf, '.');
  580. grub_strcpy (ext, ".mo");
  581. }
  582. else
  583. {
  584. srcf = grub_util_path_concat_ext (4, locale_dir, de->d_name,
  585. "LC_MESSAGES", PACKAGE, ".mo");
  586. dstf = grub_util_path_concat_ext (2, dstd, de->d_name, ".mo");
  587. }
  588. grub_install_compress_file (srcf, dstf, 0);
  589. free (srcf);
  590. free (dstf);
  591. }
  592. grub_util_fd_closedir (d);
  593. }
  594. static struct
  595. {
  596. const char *cpu;
  597. const char *platform;
  598. } platforms[GRUB_INSTALL_PLATFORM_MAX] =
  599. {
  600. [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" },
  601. [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" },
  602. [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" },
  603. [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" },
  604. [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" },
  605. [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" },
  606. [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" },
  607. [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" },
  608. [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" },
  609. [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" },
  610. [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" },
  611. [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" },
  612. [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" },
  613. [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" },
  614. [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" },
  615. [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" },
  616. [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" },
  617. [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" },
  618. [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" },
  619. [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" },
  620. [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" },
  621. };
  622. char *
  623. grub_install_get_platforms_string (void)
  624. {
  625. char **arr = xmalloc (sizeof (char *) * ARRAY_SIZE (platforms));
  626. int platform_strins_len = 0;
  627. char *platforms_string;
  628. char *ptr;
  629. unsigned i;
  630. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  631. {
  632. arr[i] = xasprintf ("%s-%s", platforms[i].cpu,
  633. platforms[i].platform);
  634. platform_strins_len += strlen (arr[i]) + 2;
  635. }
  636. ptr = platforms_string = xmalloc (platform_strins_len);
  637. qsort (arr, ARRAY_SIZE (platforms), sizeof (char *), grub_qsort_strcmp);
  638. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  639. {
  640. strcpy (ptr, arr[i]);
  641. ptr += strlen (arr[i]);
  642. *ptr++ = ',';
  643. *ptr++ = ' ';
  644. free (arr[i]);
  645. }
  646. ptr[-2] = 0;
  647. free (arr);
  648. return platforms_string;
  649. }
  650. char *
  651. grub_install_get_platform_name (enum grub_install_plat platid)
  652. {
  653. return xasprintf ("%s-%s", platforms[platid].cpu,
  654. platforms[platid].platform);
  655. }
  656. const char *
  657. grub_install_get_platform_cpu (enum grub_install_plat platid)
  658. {
  659. return platforms[platid].cpu;
  660. }
  661. const char *
  662. grub_install_get_platform_platform (enum grub_install_plat platid)
  663. {
  664. return platforms[platid].platform;
  665. }
  666. void
  667. grub_install_copy_files (const char *src,
  668. const char *dst,
  669. enum grub_install_plat platid)
  670. {
  671. char *dst_platform, *dst_locale, *dst_fonts;
  672. const char *pkgdatadir = grub_util_get_pkgdatadir ();
  673. char *themes_dir;
  674. {
  675. char *platform;
  676. platform = xasprintf ("%s-%s", platforms[platid].cpu,
  677. platforms[platid].platform);
  678. dst_platform = grub_util_path_concat (2, dst, platform);
  679. free (platform);
  680. }
  681. dst_locale = grub_util_path_concat (2, dst, "locale");
  682. dst_fonts = grub_util_path_concat (2, dst, "fonts");
  683. grub_install_mkdir_p (dst_platform);
  684. grub_install_mkdir_p (dst_locale);
  685. clean_grub_dir (dst);
  686. clean_grub_dir (dst_platform);
  687. clean_grub_dir (dst_locale);
  688. if (install_modules.is_default)
  689. copy_by_ext (src, dst_platform, ".mod", 1);
  690. else
  691. {
  692. struct grub_util_path_list *path_list, *p;
  693. path_list = grub_util_resolve_dependencies (src, "moddep.lst",
  694. install_modules.entries);
  695. for (p = path_list; p; p = p->next)
  696. {
  697. const char *srcf = p->name;
  698. const char *dir;
  699. char *dstf;
  700. dir = grub_strrchr (srcf, '/');
  701. if (dir)
  702. dir++;
  703. else
  704. dir = srcf;
  705. dstf = grub_util_path_concat (2, dst_platform, dir);
  706. grub_install_compress_file (srcf, dstf, 1);
  707. free (dstf);
  708. }
  709. grub_util_free_path_list (path_list);
  710. }
  711. const char *pkglib_DATA[] = {"efiemu32.o", "efiemu64.o",
  712. "moddep.lst", "command.lst",
  713. "fs.lst", "partmap.lst",
  714. "parttool.lst",
  715. "video.lst", "crypto.lst",
  716. "terminal.lst", "modinfo.sh" };
  717. size_t i;
  718. for (i = 0; i < ARRAY_SIZE (pkglib_DATA); i++)
  719. {
  720. char *srcf = grub_util_path_concat (2, src, pkglib_DATA[i]);
  721. char *dstf = grub_util_path_concat (2, dst_platform, pkglib_DATA[i]);
  722. if (i == 0 || i == 1)
  723. grub_install_compress_file (srcf, dstf, 0);
  724. else
  725. grub_install_compress_file (srcf, dstf, 1);
  726. free (srcf);
  727. free (dstf);
  728. }
  729. if (install_locales.is_default)
  730. {
  731. char *srcd = grub_util_path_concat (2, src, "po");
  732. copy_by_ext (srcd, dst_locale, ".mo", 0);
  733. copy_locales (dst_locale);
  734. free (srcd);
  735. }
  736. else
  737. {
  738. const char *locale_dir = get_localedir ();
  739. for (i = 0; i < install_locales.n_entries; i++)
  740. {
  741. char *srcf = grub_util_path_concat_ext (3, src,
  742. "po",
  743. install_locales.entries[i],
  744. ".mo");
  745. char *dstf = grub_util_path_concat_ext (2, dst_locale,
  746. install_locales.entries[i],
  747. ".mo");
  748. if (grub_install_compress_file (srcf, dstf, 0))
  749. {
  750. free (srcf);
  751. free (dstf);
  752. continue;
  753. }
  754. free (srcf);
  755. srcf = grub_util_path_concat_ext (4,
  756. locale_dir,
  757. install_locales.entries[i],
  758. "LC_MESSAGES",
  759. PACKAGE,
  760. ".mo");
  761. if (grub_install_compress_file (srcf, dstf, 0))
  762. {
  763. free (srcf);
  764. free (dstf);
  765. continue;
  766. }
  767. grub_util_error (_("cannot find locale `%s'"),
  768. install_locales.entries[i]);
  769. }
  770. }
  771. if (install_themes.is_default)
  772. {
  773. install_themes.is_default = 0;
  774. install_themes.n_entries = 1;
  775. install_themes.entries = xmalloc (2 * sizeof (install_themes.entries[0]));
  776. install_themes.entries[0] = xstrdup ("starfield");
  777. install_themes.entries[1] = NULL;
  778. }
  779. if (grub_install_themes_directory)
  780. themes_dir = xstrdup (grub_install_themes_directory);
  781. else
  782. themes_dir = grub_util_path_concat (2, grub_util_get_pkgdatadir (),
  783. "themes");
  784. for (i = 0; i < install_themes.n_entries; i++)
  785. {
  786. char *srcf = grub_util_path_concat (3, themes_dir,
  787. install_themes.entries[i],
  788. "theme.txt");
  789. if (grub_util_is_regular (srcf))
  790. {
  791. char *srcd = grub_util_path_concat (2, themes_dir,
  792. install_themes.entries[i]);
  793. char *dstd = grub_util_path_concat (3, dst, "themes",
  794. install_themes.entries[i]);
  795. grub_install_mkdir_p (dstd);
  796. copy_all (srcd, dstd);
  797. free (srcd);
  798. free (dstd);
  799. }
  800. free (srcf);
  801. }
  802. free (themes_dir);
  803. if (install_fonts.is_default)
  804. {
  805. install_fonts.is_default = 0;
  806. install_fonts.n_entries = 1;
  807. install_fonts.entries = xmalloc (2 * sizeof (install_fonts.entries[0]));
  808. install_fonts.entries[0] = xstrdup ("unicode");
  809. install_fonts.entries[1] = NULL;
  810. }
  811. grub_install_mkdir_p (dst_fonts);
  812. for (i = 0; i < install_fonts.n_entries; i++)
  813. {
  814. char *srcf = grub_util_path_concat_ext (2, pkgdatadir,
  815. install_fonts.entries[i],
  816. ".pf2");
  817. char *dstf = grub_util_path_concat_ext (2, dst_fonts,
  818. install_fonts.entries[i],
  819. ".pf2");
  820. grub_install_compress_file (srcf, dstf, 0);
  821. free (srcf);
  822. free (dstf);
  823. }
  824. free (dst_platform);
  825. free (dst_locale);
  826. free (dst_fonts);
  827. }
  828. enum grub_install_plat
  829. grub_install_get_target (const char *src)
  830. {
  831. char *fn;
  832. grub_util_fd_t f;
  833. char buf[8192];
  834. ssize_t r;
  835. char *c, *pl, *p;
  836. size_t i;
  837. fn = grub_util_path_concat (2, src, "modinfo.sh");
  838. f = grub_util_fd_open (fn, GRUB_UTIL_FD_O_RDONLY);
  839. if (!GRUB_UTIL_FD_IS_VALID (f))
  840. grub_util_error (_("%s doesn't exist. Please specify --target or --directory"),
  841. fn);
  842. r = grub_util_fd_read (f, buf, sizeof (buf) - 1);
  843. if (r < 0)
  844. grub_util_error (_("cannot read `%s': %s"), fn, strerror (errno));
  845. grub_util_fd_close (f);
  846. buf[r] = '\0';
  847. c = strstr (buf, "grub_modinfo_target_cpu=");
  848. if (!c || (c != buf && !grub_isspace (*(c-1))))
  849. grub_util_error (_("invalid modinfo file `%s'"), fn);
  850. pl = strstr (buf, "grub_modinfo_platform=");
  851. if (!pl || (pl != buf && !grub_isspace (*(pl-1))))
  852. grub_util_error (_("invalid modinfo file `%s'"), fn);
  853. c += sizeof ("grub_modinfo_target_cpu=") - 1;
  854. pl += sizeof ("grub_modinfo_platform=") - 1;
  855. for (p = c; *p && !grub_isspace (*p); p++);
  856. *p = '\0';
  857. for (p = pl; *p && !grub_isspace (*p); p++);
  858. *p = '\0';
  859. for (i = 0; i < ARRAY_SIZE (platforms); i++)
  860. if (strcmp (platforms[i].cpu, c) == 0
  861. && strcmp (platforms[i].platform, pl) == 0)
  862. {
  863. free (fn);
  864. return i;
  865. }
  866. grub_util_error (_("Unknown platform `%s-%s'"), c, pl);
  867. }
  868. void
  869. grub_util_unlink_recursive (const char *name)
  870. {
  871. grub_util_fd_dir_t d;
  872. grub_util_fd_dirent_t de;
  873. d = grub_util_fd_opendir (name);
  874. while ((de = grub_util_fd_readdir (d)))
  875. {
  876. char *fp;
  877. if (strcmp (de->d_name, ".") == 0)
  878. continue;
  879. if (strcmp (de->d_name, "..") == 0)
  880. continue;
  881. fp = grub_util_path_concat (2, name, de->d_name);
  882. if (grub_util_is_special_file (fp))
  883. {
  884. free (fp);
  885. continue;
  886. }
  887. if (grub_util_is_regular (fp))
  888. grub_util_unlink (fp);
  889. else if (grub_util_is_directory (fp))
  890. grub_util_unlink_recursive (fp);
  891. free (fp);
  892. }
  893. grub_util_rmdir (name);
  894. grub_util_fd_closedir (d);
  895. }