Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-install-common.c
blob37c1a98b93a83db6f1671fa500abf88b91c7326e
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.
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.
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.
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/>.
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>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <errno.h>
48 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
50 char *
51 grub_install_help_filter (int key, const char *text,
52 void *input __attribute__ ((unused)))
54 switch (key)
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;
72 #pragma GCC diagnostic error "-Wformat-nonliteral"
74 static int (*compress_func) (const char *src, const char *dest) = NULL;
75 char *grub_install_copy_buffer;
77 int
78 grub_install_copy_file (const char *src,
79 const char *dst,
80 int is_needed)
82 grub_util_fd_t in, out;
83 ssize_t r;
85 grub_util_info ("copying `%s' -> `%s'", src, dst);
87 in = grub_util_fd_open (src, GRUB_UTIL_FD_O_RDONLY);
88 if (!GRUB_UTIL_FD_IS_VALID (in))
90 if (is_needed)
91 grub_util_error (_("cannot open `%s': %s"), src, grub_util_fd_strerror ());
92 else
93 grub_util_info (_("cannot open `%s': %s"), src, grub_util_fd_strerror ());
94 return 0;
96 out = grub_util_fd_open (dst, GRUB_UTIL_FD_O_WRONLY
97 | GRUB_UTIL_FD_O_CREATTRUNC);
98 if (!GRUB_UTIL_FD_IS_VALID (out))
100 grub_util_error (_("cannot open `%s': %s"), dst,
101 grub_util_fd_strerror ());
102 grub_util_fd_close (in);
103 return 0;
106 if (!grub_install_copy_buffer)
107 grub_install_copy_buffer = xmalloc (GRUB_INSTALL_COPY_BUFFER_SIZE);
109 while (1)
111 r = grub_util_fd_read (in, grub_install_copy_buffer, GRUB_INSTALL_COPY_BUFFER_SIZE);
112 if (r <= 0)
113 break;
114 grub_util_fd_write (out, grub_install_copy_buffer, r);
116 grub_util_fd_sync (out);
117 grub_util_fd_close (in);
118 grub_util_fd_close (out);
120 if (r < 0)
121 grub_util_error (_("cannot copy `%s' to `%s': %s"),
122 src, dst, grub_util_fd_strerror ());
124 return 1;
127 static int
128 grub_install_compress_file (const char *in_name,
129 const char *out_name,
130 int is_needed)
132 int ret;
134 if (!compress_func)
135 ret = grub_install_copy_file (in_name, out_name, is_needed);
136 else
138 grub_util_info ("compressing `%s' -> `%s'", in_name, out_name);
139 ret = !compress_func (in_name, out_name);
140 if (!ret && is_needed)
141 grub_util_warn (_("can't compress `%s' to `%s'"), in_name, out_name);
144 if (!ret && is_needed)
145 grub_util_error (_("cannot copy `%s' to `%s': %s"),
146 in_name, out_name, grub_util_fd_strerror ());
148 return ret;
151 static int
152 is_path_separator (char c)
154 #if defined (__MINGW32__) || defined (__CYGWIN__)
155 if (c == '\\')
156 return 1;
157 #endif
158 if (c == '/')
159 return 1;
160 return 0;
163 void
164 grub_install_mkdir_p (const char *dst)
166 char *t = xstrdup (dst);
167 char *p;
168 for (p = t; *p; p++)
170 if (is_path_separator (*p))
172 char s = *p;
173 *p = '\0';
174 grub_util_mkdir (t);
175 *p = s;
178 grub_util_mkdir (t);
179 free (t);
182 static void
183 clean_grub_dir (const char *di)
185 grub_util_fd_dir_t d;
186 grub_util_fd_dirent_t de;
188 d = grub_util_fd_opendir (di);
189 if (!d)
190 grub_util_error (_("cannot open directory `%s': %s"),
191 di, grub_util_fd_strerror ());
193 while ((de = grub_util_fd_readdir (d)))
195 const char *ext = strrchr (de->d_name, '.');
196 if ((ext && (strcmp (ext, ".mod") == 0
197 || strcmp (ext, ".lst") == 0
198 || strcmp (ext, ".img") == 0
199 || strcmp (ext, ".mo") == 0)
200 && strcmp (de->d_name, "menu.lst") != 0)
201 || strcmp (de->d_name, "efiemu32.o") == 0
202 || strcmp (de->d_name, "efiemu64.o") == 0)
204 char *x = grub_util_path_concat (2, di, de->d_name);
205 if (grub_util_unlink (x) < 0)
206 grub_util_error (_("cannot delete `%s': %s"), x,
207 grub_util_fd_strerror ());
208 free (x);
211 grub_util_fd_closedir (d);
214 struct install_list
216 int is_default;
217 char **entries;
218 size_t n_entries;
219 size_t n_alloc;
222 struct install_list install_modules = { 1, 0, 0, 0 };
223 struct install_list modules = { 1, 0, 0, 0 };
224 struct install_list install_locales = { 1, 0, 0, 0 };
225 struct install_list install_fonts = { 1, 0, 0, 0 };
226 struct install_list install_themes = { 1, 0, 0, 0 };
227 char *grub_install_source_directory = NULL;
228 char *grub_install_locale_directory = NULL;
229 char *grub_install_themes_directory = NULL;
231 void
232 grub_install_push_module (const char *val)
234 modules.is_default = 0;
235 if (modules.n_entries + 1 >= modules.n_alloc)
237 modules.n_alloc <<= 1;
238 if (modules.n_alloc < 16)
239 modules.n_alloc = 16;
240 modules.entries = xrealloc (modules.entries,
241 modules.n_alloc * sizeof (*modules.entries));
243 modules.entries[modules.n_entries++] = xstrdup (val);
244 modules.entries[modules.n_entries] = NULL;
247 void
248 grub_install_pop_module (void)
250 modules.n_entries--;
251 free (modules.entries[modules.n_entries]);
252 modules.entries[modules.n_entries] = NULL;
256 static void
257 handle_install_list (struct install_list *il, const char *val,
258 int default_all)
260 const char *ptr;
261 char **ce;
262 il->is_default = 0;
263 free (il->entries);
264 il->entries = NULL;
265 il->n_entries = 0;
266 if (strcmp (val, "all") == 0 && default_all)
268 il->is_default = 1;
269 return;
271 ptr = val;
272 while (1)
274 while (*ptr && grub_isspace (*ptr))
275 ptr++;
276 if (!*ptr)
277 break;
278 while (*ptr && !grub_isspace (*ptr))
279 ptr++;
280 il->n_entries++;
282 il->n_alloc = il->n_entries + 1;
283 il->entries = xmalloc (il->n_alloc * sizeof (il->entries[0]));
284 ptr = val;
285 for (ce = il->entries; ; ce++)
287 const char *bptr;
288 while (*ptr && grub_isspace (*ptr))
289 ptr++;
290 if (!*ptr)
291 break;
292 bptr = ptr;
293 while (*ptr && !grub_isspace (*ptr))
294 ptr++;
295 *ce = xmalloc (ptr - bptr + 1);
296 memcpy (*ce, bptr, ptr - bptr);
297 (*ce)[ptr - bptr] = '\0';
299 *ce = NULL;
302 static char **pubkeys;
303 static size_t npubkeys;
304 static grub_compression_t compression;
307 grub_install_parse (int key, char *arg)
309 switch (key)
311 case 'C':
312 if (grub_strcmp (arg, "xz") == 0)
314 #ifdef HAVE_LIBLZMA
315 compression = GRUB_COMPRESSION_XZ;
316 #else
317 grub_util_error ("%s",
318 _("grub-mkimage is compiled without XZ support"));
319 #endif
321 else if (grub_strcmp (arg, "none") == 0)
322 compression = GRUB_COMPRESSION_NONE;
323 else if (grub_strcmp (arg, "auto") == 0)
324 compression = GRUB_COMPRESSION_AUTO;
325 else
326 grub_util_error (_("Unknown compression format %s"), arg);
327 return 1;
328 case 'k':
329 pubkeys = xrealloc (pubkeys,
330 sizeof (pubkeys[0])
331 * (npubkeys + 1));
332 pubkeys[npubkeys++] = xstrdup (arg);
333 return 1;
335 case GRUB_INSTALL_OPTIONS_VERBOSITY:
336 verbosity++;
337 return 1;
339 case GRUB_INSTALL_OPTIONS_DIRECTORY:
340 case GRUB_INSTALL_OPTIONS_DIRECTORY2:
341 free (grub_install_source_directory);
342 grub_install_source_directory = xstrdup (arg);
343 return 1;
344 case GRUB_INSTALL_OPTIONS_LOCALE_DIRECTORY:
345 free (grub_install_locale_directory);
346 grub_install_locale_directory = xstrdup (arg);
347 return 1;
348 case GRUB_INSTALL_OPTIONS_THEMES_DIRECTORY:
349 free (grub_install_themes_directory);
350 grub_install_themes_directory = xstrdup (arg);
351 return 1;
352 case GRUB_INSTALL_OPTIONS_INSTALL_MODULES:
353 handle_install_list (&install_modules, arg, 0);
354 return 1;
355 case GRUB_INSTALL_OPTIONS_MODULES:
356 handle_install_list (&modules, arg, 0);
357 return 1;
358 case GRUB_INSTALL_OPTIONS_INSTALL_LOCALES:
359 handle_install_list (&install_locales, arg, 0);
360 return 1;
361 case GRUB_INSTALL_OPTIONS_INSTALL_THEMES:
362 handle_install_list (&install_themes, arg, 0);
363 return 1;
364 case GRUB_INSTALL_OPTIONS_INSTALL_FONTS:
365 handle_install_list (&install_fonts, arg, 0);
366 return 1;
367 case GRUB_INSTALL_OPTIONS_INSTALL_COMPRESS:
368 if (strcmp (arg, "no") == 0
369 || strcmp (arg, "none") == 0)
371 compress_func = NULL;
372 return 1;
374 if (strcmp (arg, "gz") == 0)
376 compress_func = grub_install_compress_gzip;
377 return 1;
379 if (strcmp (arg, "xz") == 0)
381 compress_func = grub_install_compress_xz;
382 return 1;
384 if (strcmp (arg, "lzo") == 0)
386 compress_func = grub_install_compress_lzop;
387 return 1;
389 grub_util_error (_("Unrecognized compression `%s'"), arg);
390 case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE:
391 return 1;
392 default:
393 return 0;
397 static int
398 decompressors (void)
400 if (compress_func == grub_install_compress_gzip)
402 grub_install_push_module ("gzio");
403 return 1;
405 if (compress_func == grub_install_compress_xz)
407 grub_install_push_module ("xzio");
408 grub_install_push_module ("gcry_crc");
409 return 2;
411 if (compress_func == grub_install_compress_lzop)
413 grub_install_push_module ("lzopio");
414 grub_install_push_module ("adler32");
415 grub_install_push_module ("gcry_crc");
416 return 3;
418 return 0;
421 void
422 grub_install_make_image_wrap_file (const char *dir, const char *prefix,
423 FILE *fp, const char *outname,
424 char *memdisk_path,
425 char *config_path,
426 const char *mkimage_target, int note)
428 const struct grub_install_image_target_desc *tgt;
429 const char *const compnames[] =
431 [GRUB_COMPRESSION_AUTO] = "auto",
432 [GRUB_COMPRESSION_NONE] = "none",
433 [GRUB_COMPRESSION_XZ] = "xz",
434 [GRUB_COMPRESSION_LZMA] = "lzma",
436 grub_size_t slen = 1;
437 char *s, *p;
438 char **pk, **md;
439 int dc = decompressors ();
441 if (memdisk_path)
442 slen += 20 + grub_strlen (memdisk_path);
443 if (config_path)
444 slen += 20 + grub_strlen (config_path);
446 for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
447 slen += 20 + grub_strlen (*pk);
449 for (md = modules.entries; *md; md++)
451 slen += 10 + grub_strlen (*md);
454 p = s = xmalloc (slen);
455 if (memdisk_path)
457 p = grub_stpcpy (p, "--memdisk '");
458 p = grub_stpcpy (p, memdisk_path);
459 *p++ = '\'';
460 *p++ = ' ';
462 if (config_path)
464 p = grub_stpcpy (p, "--config '");
465 p = grub_stpcpy (p, config_path);
466 *p++ = '\'';
467 *p++ = ' ';
469 for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
471 p = grub_stpcpy (p, "--pubkey '");
472 p = grub_stpcpy (p, *pk);
473 *p++ = '\'';
474 *p++ = ' ';
477 for (md = modules.entries; *md; md++)
479 *p++ = '\'';
480 p = grub_stpcpy (p, *md);
481 *p++ = '\'';
482 *p++ = ' ';
485 *p = '\0';
487 grub_util_info ("grub-mkimage --directory '%s' --prefix '%s'"
488 " --output '%s' "
489 "--format '%s' --compression '%s' %s %s\n",
490 dir, prefix,
491 outname, mkimage_target,
492 compnames[compression], note ? "--note" : "", s);
493 free (s);
495 tgt = grub_install_get_image_target (mkimage_target);
496 if (!tgt)
497 grub_util_error (_("unknown target format %s\n"), mkimage_target);
499 grub_install_generate_image (dir, prefix, fp, outname,
500 modules.entries, memdisk_path,
501 pubkeys, npubkeys, config_path, tgt,
502 note, compression);
503 while (dc--)
504 grub_install_pop_module ();
507 void
508 grub_install_make_image_wrap (const char *dir, const char *prefix,
509 const char *outname, char *memdisk_path,
510 char *config_path,
511 const char *mkimage_target, int note)
513 FILE *fp;
515 fp = grub_util_fopen (outname, "wb");
516 if (! fp)
517 grub_util_error (_("cannot open `%s': %s"), outname,
518 strerror (errno));
519 grub_install_make_image_wrap_file (dir, prefix, fp, outname,
520 memdisk_path, config_path,
521 mkimage_target, note);
522 grub_util_file_sync (fp);
523 fclose (fp);
526 static void
527 copy_by_ext (const char *srcd,
528 const char *dstd,
529 const char *extf,
530 int req)
532 grub_util_fd_dir_t d;
533 grub_util_fd_dirent_t de;
535 d = grub_util_fd_opendir (srcd);
536 if (!d && !req)
537 return;
538 if (!d)
539 grub_util_error (_("cannot open directory `%s': %s"),
540 srcd, grub_util_fd_strerror ());
542 while ((de = grub_util_fd_readdir (d)))
544 const char *ext = strrchr (de->d_name, '.');
545 if (ext && strcmp (ext, extf) == 0)
547 char *srcf = grub_util_path_concat (2, srcd, de->d_name);
548 char *dstf = grub_util_path_concat (2, dstd, de->d_name);
549 grub_install_compress_file (srcf, dstf, 1);
550 free (srcf);
551 free (dstf);
554 grub_util_fd_closedir (d);
557 static void
558 copy_all (const char *srcd,
559 const char *dstd)
561 grub_util_fd_dir_t d;
562 grub_util_fd_dirent_t de;
564 d = grub_util_fd_opendir (srcd);
565 if (!d)
566 grub_util_error (_("cannot open directory `%s': %s"),
567 srcd, grub_util_fd_strerror ());
569 while ((de = grub_util_fd_readdir (d)))
571 char *srcf;
572 char *dstf;
573 if (strcmp (de->d_name, ".") == 0
574 || strcmp (de->d_name, "..") == 0)
575 continue;
576 srcf = grub_util_path_concat (2, srcd, de->d_name);
577 if (grub_util_is_special_file (srcf)
578 || grub_util_is_directory (srcf))
579 continue;
580 dstf = grub_util_path_concat (2, dstd, de->d_name);
581 grub_install_compress_file (srcf, dstf, 1);
582 free (srcf);
583 free (dstf);
585 grub_util_fd_closedir (d);
588 static const char *
589 get_localedir (void)
591 if (grub_install_locale_directory)
592 return grub_install_locale_directory;
593 else
594 return grub_util_get_localedir ();
597 static void
598 copy_locales (const char *dstd)
600 grub_util_fd_dir_t d;
601 grub_util_fd_dirent_t de;
602 const char *locale_dir = get_localedir ();
604 d = grub_util_fd_opendir (locale_dir);
605 if (!d)
607 grub_util_warn (_("cannot open directory `%s': %s"),
608 locale_dir, grub_util_fd_strerror ());
609 return;
612 while ((de = grub_util_fd_readdir (d)))
614 char *srcf;
615 char *dstf;
616 char *ext;
617 if (strcmp (de->d_name, ".") == 0)
618 continue;
619 if (strcmp (de->d_name, "..") == 0)
620 continue;
621 ext = grub_strrchr (de->d_name, '.');
622 if (ext && (grub_strcmp (ext, ".mo") == 0
623 || grub_strcmp (ext, ".gmo") == 0))
625 srcf = grub_util_path_concat (2, locale_dir, de->d_name);
626 dstf = grub_util_path_concat (2, dstd, de->d_name);
627 ext = grub_strrchr (dstf, '.');
628 grub_strcpy (ext, ".mo");
630 else
632 srcf = grub_util_path_concat_ext (4, locale_dir, de->d_name,
633 "LC_MESSAGES", PACKAGE, ".mo");
634 dstf = grub_util_path_concat_ext (2, dstd, de->d_name, ".mo");
636 grub_install_compress_file (srcf, dstf, 0);
637 free (srcf);
638 free (dstf);
640 grub_util_fd_closedir (d);
643 static struct
645 const char *cpu;
646 const char *platform;
647 } platforms[GRUB_INSTALL_PLATFORM_MAX] =
649 [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" },
650 [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" },
651 [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" },
652 [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" },
653 [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" },
654 [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" },
655 [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" },
656 [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" },
657 [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" },
658 [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" },
659 [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" },
660 [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" },
661 [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" },
662 [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" },
663 [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" },
664 [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" },
665 [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" },
666 [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" },
667 [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" },
668 [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" },
671 char *
672 grub_install_get_platforms_string (void)
674 char **arr = xmalloc (sizeof (char *) * ARRAY_SIZE (platforms));
675 int platform_strins_len = 0;
676 char *platforms_string;
677 char *ptr;
678 unsigned i;
679 for (i = 0; i < ARRAY_SIZE (platforms); i++)
681 arr[i] = xasprintf ("%s-%s", platforms[i].cpu,
682 platforms[i].platform);
683 platform_strins_len += strlen (arr[i]) + 2;
685 ptr = platforms_string = xmalloc (platform_strins_len);
686 qsort (arr, ARRAY_SIZE (platforms), sizeof (char *), grub_qsort_strcmp);
687 for (i = 0; i < ARRAY_SIZE (platforms); i++)
689 strcpy (ptr, arr[i]);
690 ptr += strlen (arr[i]);
691 *ptr++ = ',';
692 *ptr++ = ' ';
693 free (arr[i]);
695 ptr[-2] = 0;
696 free (arr);
698 return platforms_string;
701 char *
702 grub_install_get_platform_name (enum grub_install_plat platid)
704 return xasprintf ("%s-%s", platforms[platid].cpu,
705 platforms[platid].platform);
708 const char *
709 grub_install_get_platform_cpu (enum grub_install_plat platid)
711 return platforms[platid].cpu;
714 const char *
715 grub_install_get_platform_platform (enum grub_install_plat platid)
717 return platforms[platid].platform;
721 void
722 grub_install_copy_files (const char *src,
723 const char *dst,
724 enum grub_install_plat platid)
726 char *dst_platform, *dst_locale, *dst_fonts;
727 const char *pkgdatadir = grub_util_get_pkgdatadir ();
728 char *themes_dir;
731 char *platform;
732 platform = xasprintf ("%s-%s", platforms[platid].cpu,
733 platforms[platid].platform);
734 dst_platform = grub_util_path_concat (2, dst, platform);
735 free (platform);
737 dst_locale = grub_util_path_concat (2, dst, "locale");
738 dst_fonts = grub_util_path_concat (2, dst, "fonts");
739 grub_install_mkdir_p (dst_platform);
740 grub_install_mkdir_p (dst_locale);
741 clean_grub_dir (dst);
742 clean_grub_dir (dst_platform);
743 clean_grub_dir (dst_locale);
745 if (install_modules.is_default)
746 copy_by_ext (src, dst_platform, ".mod", 1);
747 else
749 struct grub_util_path_list *path_list, *p;
751 path_list = grub_util_resolve_dependencies (src, "moddep.lst",
752 install_modules.entries);
753 for (p = path_list; p; p = p->next)
755 const char *srcf = p->name;
756 const char *dir;
757 char *dstf;
759 dir = grub_strrchr (srcf, '/');
760 if (dir)
761 dir++;
762 else
763 dir = srcf;
764 dstf = grub_util_path_concat (2, dst_platform, dir);
765 grub_install_compress_file (srcf, dstf, 1);
766 free (dstf);
770 const char *pkglib_DATA[] = {"efiemu32.o", "efiemu64.o",
771 "moddep.lst", "command.lst",
772 "fs.lst", "partmap.lst",
773 "parttool.lst",
774 "video.lst", "crypto.lst",
775 "terminal.lst", "modinfo.sh" };
776 size_t i;
778 for (i = 0; i < ARRAY_SIZE (pkglib_DATA); i++)
780 char *srcf = grub_util_path_concat (2, src, pkglib_DATA[i]);
781 char *dstf = grub_util_path_concat (2, dst_platform, pkglib_DATA[i]);
782 if (i == 0 || i == 1)
783 grub_install_compress_file (srcf, dstf, 0);
784 else
785 grub_install_compress_file (srcf, dstf, 1);
786 free (srcf);
787 free (dstf);
790 if (install_locales.is_default)
792 char *srcd = grub_util_path_concat (2, src, "po");
793 copy_by_ext (srcd, dst_locale, ".mo", 0);
794 copy_locales (dst_locale);
795 free (srcd);
797 else
799 const char *locale_dir = get_localedir ();
801 for (i = 0; i < install_locales.n_entries; i++)
803 char *srcf = grub_util_path_concat_ext (3, src,
804 "po",
805 install_locales.entries[i],
806 ".mo");
807 char *dstf = grub_util_path_concat_ext (2, dst_locale,
808 install_locales.entries[i],
809 ".mo");
810 if (grub_install_compress_file (srcf, dstf, 0))
812 free (srcf);
813 free (dstf);
814 continue;
816 free (srcf);
817 srcf = grub_util_path_concat_ext (4,
818 locale_dir,
819 install_locales.entries[i],
820 "LC_MESSAGES",
821 PACKAGE,
822 ".mo");
823 if (grub_install_compress_file (srcf, dstf, 0))
825 free (srcf);
826 free (dstf);
827 continue;
829 grub_util_error (_("cannot find locale `%s'"),
830 install_locales.entries[i]);
834 if (install_themes.is_default)
836 install_themes.is_default = 0;
837 install_themes.n_entries = 1;
838 install_themes.entries = xmalloc (2 * sizeof (install_themes.entries[0]));
839 install_themes.entries[0] = xstrdup ("starfield");
840 install_themes.entries[1] = NULL;
843 if (grub_install_themes_directory)
844 themes_dir = xstrdup (grub_install_themes_directory);
845 else
846 themes_dir = grub_util_path_concat (2, grub_util_get_pkgdatadir (),
847 "themes");
849 for (i = 0; i < install_themes.n_entries; i++)
851 char *srcf = grub_util_path_concat (3, themes_dir,
852 install_themes.entries[i],
853 "theme.txt");
854 if (grub_util_is_regular (srcf))
856 char *srcd = grub_util_path_concat (2, themes_dir,
857 install_themes.entries[i]);
858 char *dstd = grub_util_path_concat (3, dst, "themes",
859 install_themes.entries[i]);
860 grub_install_mkdir_p (dstd);
861 copy_all (srcd, dstd);
862 free (srcd);
863 free (dstd);
865 free (srcf);
868 free (themes_dir);
870 if (install_fonts.is_default)
872 install_fonts.is_default = 0;
873 install_fonts.n_entries = 1;
874 install_fonts.entries = xmalloc (2 * sizeof (install_fonts.entries[0]));
875 install_fonts.entries[0] = xstrdup ("unicode");
876 install_fonts.entries[1] = NULL;
879 grub_install_mkdir_p (dst_fonts);
881 for (i = 0; i < install_fonts.n_entries; i++)
883 char *srcf = grub_util_path_concat_ext (2, pkgdatadir,
884 install_fonts.entries[i],
885 ".pf2");
886 char *dstf = grub_util_path_concat_ext (2, dst_fonts,
887 install_fonts.entries[i],
888 ".pf2");
890 grub_install_compress_file (srcf, dstf, 0);
891 free (srcf);
892 free (dstf);
895 free (dst_platform);
896 free (dst_locale);
897 free (dst_fonts);
900 enum grub_install_plat
901 grub_install_get_target (const char *src)
903 char *fn;
904 grub_util_fd_t f;
905 char buf[2048];
906 ssize_t r;
907 char *c, *pl, *p;
908 size_t i;
909 fn = grub_util_path_concat (2, src, "modinfo.sh");
910 f = grub_util_fd_open (fn, GRUB_UTIL_FD_O_RDONLY);
911 if (!GRUB_UTIL_FD_IS_VALID (f))
912 grub_util_error (_("%s doesn't exist. Please specify --target or --directory"),
913 fn);
914 r = grub_util_fd_read (f, buf, sizeof (buf) - 1);
915 if (r < 0)
916 grub_util_error (_("cannot read `%s': %s"), fn, strerror (errno));
917 grub_util_fd_close (f);
918 buf[r] = '\0';
919 c = strstr (buf, "grub_modinfo_target_cpu=");
920 if (!c || (c != buf && !grub_isspace (*(c-1))))
921 grub_util_error (_("invalid modinfo file `%s'"), fn);
922 pl = strstr (buf, "grub_modinfo_platform=");
923 if (!pl || (pl != buf && !grub_isspace (*(pl-1))))
924 grub_util_error (_("invalid modinfo file `%s'"), fn);
925 c += sizeof ("grub_modinfo_target_cpu=") - 1;
926 pl += sizeof ("grub_modinfo_platform=") - 1;
927 for (p = c; *p && !grub_isspace (*p); p++);
928 *p = '\0';
929 for (p = pl; *p && !grub_isspace (*p); p++);
930 *p = '\0';
932 for (i = 0; i < ARRAY_SIZE (platforms); i++)
933 if (strcmp (platforms[i].cpu, c) == 0
934 && strcmp (platforms[i].platform, pl) == 0)
936 free (fn);
937 return i;
939 grub_util_error (_("Unknown platform `%s-%s'"), c, pl);
943 void
944 grub_util_unlink_recursive (const char *name)
946 grub_util_fd_dir_t d;
947 grub_util_fd_dirent_t de;
949 d = grub_util_fd_opendir (name);
951 while ((de = grub_util_fd_readdir (d)))
953 char *fp;
954 if (strcmp (de->d_name, ".") == 0)
955 continue;
956 if (strcmp (de->d_name, "..") == 0)
957 continue;
958 fp = grub_util_path_concat (2, name, de->d_name);
959 if (grub_util_is_special_file (fp))
961 free (fp);
962 continue;
964 if (grub_util_is_regular (fp))
965 grub_util_unlink (fp);
966 else if (grub_util_is_directory (fp))
967 grub_util_unlink_recursive (fp);
968 free (fp);
970 grub_util_rmdir (name);
971 grub_util_fd_closedir (d);