grub-install-common.c 25 KB

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