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