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/>.
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>
29 #include <grub/term.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>
48 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
51 grub_install_help_filter (int key, const char *text,
52 void *input __attribute__ ((unused)))
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");
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;
79 grub_install_copy_file (const char *src,
83 grub_util_fd_t in, out;
86 grub_util_info ("copying `%s' -> `%s'", src, dst);
88 in = grub_util_fd_open (src, GRUB_UTIL_FD_O_RDONLY);
89 if (!GRUB_UTIL_FD_IS_VALID (in))
92 grub_util_error (_("cannot open `%s': %s"), src, grub_util_fd_strerror ());
94 grub_util_info (_("cannot open `%s': %s"), src, grub_util_fd_strerror ());
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))
101 grub_util_error (_("cannot open `%s': %s"), dst,
102 grub_util_fd_strerror ());
103 grub_util_fd_close (in);
107 if (!grub_install_copy_buffer)
108 grub_install_copy_buffer = xmalloc (GRUB_INSTALL_COPY_BUFFER_SIZE);
112 r = grub_util_fd_read (in, grub_install_copy_buffer, GRUB_INSTALL_COPY_BUFFER_SIZE);
115 grub_util_fd_write (out, grub_install_copy_buffer, r);
117 grub_util_fd_sync (out);
118 grub_util_fd_close (in);
119 grub_util_fd_close (out);
122 grub_util_error (_("cannot copy `%s' to `%s': %s"),
123 src, dst, grub_util_fd_strerror ());
129 grub_install_compress_file (const char *in_name,
130 const char *out_name,
136 ret = grub_install_copy_file (in_name, out_name, is_needed);
139 grub_util_info ("compressing `%s' -> `%s'", in_name, out_name);
140 ret = !compress_func (in_name, out_name);
141 if (!ret && is_needed)
142 grub_util_warn (_("can't compress `%s' to `%s'"), in_name, out_name);
145 if (!ret && is_needed)
146 grub_util_error (_("cannot copy `%s' to `%s': %s"),
147 in_name, out_name, grub_util_fd_strerror ());
153 is_path_separator (char c)
155 #if defined (__MINGW32__) || defined (__CYGWIN__)
165 grub_install_mkdir_p (const char *dst)
167 char *t = xstrdup (dst);
171 if (is_path_separator (*p))
184 clean_grub_dir (const char *di)
186 grub_util_fd_dir_t d;
187 grub_util_fd_dirent_t de;
189 d = grub_util_fd_opendir (di);
191 grub_util_error (_("cannot open directory `%s': %s"),
192 di, grub_util_fd_strerror ());
194 while ((de = grub_util_fd_readdir (d)))
196 const char *ext = strrchr (de->d_name, '.');
197 if ((ext && (strcmp (ext, ".mod") == 0
198 || strcmp (ext, ".lst") == 0
199 || strcmp (ext, ".img") == 0
200 || strcmp (ext, ".mo") == 0)
201 && strcmp (de->d_name, "menu.lst") != 0)
202 || strcmp (de->d_name, "efiemu32.o") == 0
203 || strcmp (de->d_name, "efiemu64.o") == 0)
205 char *x = grub_util_path_concat (2, di, de->d_name);
206 if (grub_util_unlink (x) < 0)
207 grub_util_error (_("cannot delete `%s': %s"), x,
208 grub_util_fd_strerror ());
212 grub_util_fd_closedir (d);
223 struct install_list install_modules = { 1, 0, 0, 0 };
224 struct install_list modules = { 1, 0, 0, 0 };
225 struct install_list install_locales = { 1, 0, 0, 0 };
226 struct install_list install_fonts = { 1, 0, 0, 0 };
227 struct install_list install_themes = { 1, 0, 0, 0 };
228 char *grub_install_source_directory = NULL;
229 char *grub_install_locale_directory = NULL;
230 char *grub_install_themes_directory = NULL;
233 grub_install_push_module (const char *val)
235 modules.is_default = 0;
236 if (modules.n_entries + 1 >= modules.n_alloc)
238 modules.n_alloc <<= 1;
239 if (modules.n_alloc < 16)
240 modules.n_alloc = 16;
241 modules.entries = xrealloc (modules.entries,
242 modules.n_alloc * sizeof (*modules.entries));
244 modules.entries[modules.n_entries++] = xstrdup (val);
245 modules.entries[modules.n_entries] = NULL;
249 grub_install_pop_module (void)
252 free (modules.entries[modules.n_entries]);
253 modules.entries[modules.n_entries] = NULL;
258 handle_install_list (struct install_list *il, const char *val,
267 if (strcmp (val, "all") == 0 && default_all)
275 while (*ptr && grub_isspace (*ptr))
279 while (*ptr && !grub_isspace (*ptr))
283 il->n_alloc = il->n_entries + 1;
284 il->entries = xmalloc (il->n_alloc * sizeof (il->entries[0]));
286 for (ce = il->entries; ; ce++)
289 while (*ptr && grub_isspace (*ptr))
294 while (*ptr && !grub_isspace (*ptr))
296 *ce = xmalloc (ptr - bptr + 1);
297 memcpy (*ce, bptr, ptr - bptr);
298 (*ce)[ptr - bptr] = '\0';
303 static char **pubkeys;
304 static size_t npubkeys;
305 static grub_compression_t compression;
308 grub_install_parse (int key, char *arg)
313 if (grub_strcmp (arg, "xz") == 0)
316 compression = GRUB_COMPRESSION_XZ;
318 grub_util_error ("%s",
319 _("grub-mkimage is compiled without XZ support"));
322 else if (grub_strcmp (arg, "none") == 0)
323 compression = GRUB_COMPRESSION_NONE;
324 else if (grub_strcmp (arg, "auto") == 0)
325 compression = GRUB_COMPRESSION_AUTO;
327 grub_util_error (_("Unknown compression format %s"), arg);
330 pubkeys = xrealloc (pubkeys,
333 pubkeys[npubkeys++] = xstrdup (arg);
336 case GRUB_INSTALL_OPTIONS_VERBOSITY:
340 case GRUB_INSTALL_OPTIONS_DIRECTORY:
341 case GRUB_INSTALL_OPTIONS_DIRECTORY2:
342 free (grub_install_source_directory);
343 grub_install_source_directory = xstrdup (arg);
345 case GRUB_INSTALL_OPTIONS_LOCALE_DIRECTORY:
346 free (grub_install_locale_directory);
347 grub_install_locale_directory = xstrdup (arg);
349 case GRUB_INSTALL_OPTIONS_THEMES_DIRECTORY:
350 free (grub_install_themes_directory);
351 grub_install_themes_directory = xstrdup (arg);
353 case GRUB_INSTALL_OPTIONS_INSTALL_MODULES:
354 handle_install_list (&install_modules, arg, 0);
356 case GRUB_INSTALL_OPTIONS_MODULES:
357 handle_install_list (&modules, arg, 0);
359 case GRUB_INSTALL_OPTIONS_INSTALL_LOCALES:
360 handle_install_list (&install_locales, arg, 0);
362 case GRUB_INSTALL_OPTIONS_INSTALL_THEMES:
363 handle_install_list (&install_themes, arg, 0);
365 case GRUB_INSTALL_OPTIONS_INSTALL_FONTS:
366 handle_install_list (&install_fonts, arg, 0);
368 case GRUB_INSTALL_OPTIONS_DTB:
373 case GRUB_INSTALL_OPTIONS_INSTALL_COMPRESS:
374 if (strcmp (arg, "no") == 0
375 || strcmp (arg, "none") == 0)
377 compress_func = NULL;
380 if (strcmp (arg, "gz") == 0)
382 compress_func = grub_install_compress_gzip;
385 if (strcmp (arg, "xz") == 0)
387 compress_func = grub_install_compress_xz;
390 if (strcmp (arg, "lzo") == 0)
392 compress_func = grub_install_compress_lzop;
395 grub_util_error (_("Unrecognized compression `%s'"), arg);
396 case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE:
406 if (compress_func == grub_install_compress_gzip)
408 grub_install_push_module ("gzio");
411 if (compress_func == grub_install_compress_xz)
413 grub_install_push_module ("xzio");
414 grub_install_push_module ("gcry_crc");
417 if (compress_func == grub_install_compress_lzop)
419 grub_install_push_module ("lzopio");
420 grub_install_push_module ("adler32");
421 grub_install_push_module ("gcry_crc");
428 grub_install_make_image_wrap_file (const char *dir, const char *prefix,
429 FILE *fp, const char *outname,
432 const char *mkimage_target, int note)
434 const struct grub_install_image_target_desc *tgt;
435 const char *const compnames[] =
437 [GRUB_COMPRESSION_AUTO] = "auto",
438 [GRUB_COMPRESSION_NONE] = "none",
439 [GRUB_COMPRESSION_XZ] = "xz",
440 [GRUB_COMPRESSION_LZMA] = "lzma",
442 grub_size_t slen = 1;
445 int dc = decompressors ();
448 slen += 20 + grub_strlen (memdisk_path);
450 slen += 20 + grub_strlen (config_path);
452 for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
453 slen += 20 + grub_strlen (*pk);
455 for (md = modules.entries; *md; md++)
457 slen += 10 + grub_strlen (*md);
460 p = s = xmalloc (slen);
463 p = grub_stpcpy (p, "--memdisk '");
464 p = grub_stpcpy (p, memdisk_path);
470 p = grub_stpcpy (p, "--config '");
471 p = grub_stpcpy (p, config_path);
475 for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
477 p = grub_stpcpy (p, "--pubkey '");
478 p = grub_stpcpy (p, *pk);
483 for (md = modules.entries; *md; md++)
486 p = grub_stpcpy (p, *md);
493 grub_util_info ("grub-mkimage --directory '%s' --prefix '%s'"
496 "--format '%s' --compression '%s' %s %s\n",
498 outname, dtb ? : "", mkimage_target,
499 compnames[compression], note ? "--note" : "", s);
502 tgt = grub_install_get_image_target (mkimage_target);
504 grub_util_error (_("unknown target format %s"), mkimage_target);
506 grub_install_generate_image (dir, prefix, fp, outname,
507 modules.entries, memdisk_path,
508 pubkeys, npubkeys, config_path, tgt,
509 note, compression, dtb);
511 grub_install_pop_module ();
515 grub_install_make_image_wrap (const char *dir, const char *prefix,
516 const char *outname, char *memdisk_path,
518 const char *mkimage_target, int note)
522 fp = grub_util_fopen (outname, "wb");
524 grub_util_error (_("cannot open `%s': %s"), outname,
526 grub_install_make_image_wrap_file (dir, prefix, fp, outname,
527 memdisk_path, config_path,
528 mkimage_target, note);
529 grub_util_file_sync (fp);
534 copy_by_ext (const char *srcd,
539 grub_util_fd_dir_t d;
540 grub_util_fd_dirent_t de;
542 d = grub_util_fd_opendir (srcd);
546 grub_util_error (_("cannot open directory `%s': %s"),
547 srcd, grub_util_fd_strerror ());
549 while ((de = grub_util_fd_readdir (d)))
551 const char *ext = strrchr (de->d_name, '.');
552 if (ext && strcmp (ext, extf) == 0)
554 char *srcf = grub_util_path_concat (2, srcd, de->d_name);
555 char *dstf = grub_util_path_concat (2, dstd, de->d_name);
556 grub_install_compress_file (srcf, dstf, 1);
561 grub_util_fd_closedir (d);
565 copy_all (const char *srcd,
568 grub_util_fd_dir_t d;
569 grub_util_fd_dirent_t de;
571 d = grub_util_fd_opendir (srcd);
573 grub_util_error (_("cannot open directory `%s': %s"),
574 srcd, grub_util_fd_strerror ());
576 while ((de = grub_util_fd_readdir (d)))
580 if (strcmp (de->d_name, ".") == 0
581 || strcmp (de->d_name, "..") == 0)
583 srcf = grub_util_path_concat (2, srcd, de->d_name);
584 if (grub_util_is_special_file (srcf)
585 || grub_util_is_directory (srcf))
587 dstf = grub_util_path_concat (2, dstd, de->d_name);
588 grub_install_compress_file (srcf, dstf, 1);
592 grub_util_fd_closedir (d);
598 if (grub_install_locale_directory)
599 return grub_install_locale_directory;
601 return grub_util_get_localedir ();
605 copy_locales (const char *dstd)
607 grub_util_fd_dir_t d;
608 grub_util_fd_dirent_t de;
609 const char *locale_dir = get_localedir ();
611 d = grub_util_fd_opendir (locale_dir);
614 grub_util_warn (_("cannot open directory `%s': %s"),
615 locale_dir, grub_util_fd_strerror ());
619 while ((de = grub_util_fd_readdir (d)))
624 if (strcmp (de->d_name, ".") == 0)
626 if (strcmp (de->d_name, "..") == 0)
628 ext = grub_strrchr (de->d_name, '.');
629 if (ext && (grub_strcmp (ext, ".mo") == 0
630 || grub_strcmp (ext, ".gmo") == 0))
632 srcf = grub_util_path_concat (2, locale_dir, de->d_name);
633 dstf = grub_util_path_concat (2, dstd, de->d_name);
634 ext = grub_strrchr (dstf, '.');
635 grub_strcpy (ext, ".mo");
639 srcf = grub_util_path_concat_ext (4, locale_dir, de->d_name,
640 "LC_MESSAGES", PACKAGE, ".mo");
641 dstf = grub_util_path_concat_ext (2, dstd, de->d_name, ".mo");
643 grub_install_compress_file (srcf, dstf, 0);
647 grub_util_fd_closedir (d);
653 const char *platform;
654 } platforms[GRUB_INSTALL_PLATFORM_MAX] =
656 [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" },
657 [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" },
658 [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" },
659 [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" },
660 [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" },
661 [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" },
662 [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" },
663 [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" },
664 [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" },
665 [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" },
666 [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" },
667 [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" },
668 [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" },
669 [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" },
670 [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" },
671 [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" },
672 [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" },
673 [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" },
674 [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" },
675 [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" },
676 [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" },
680 grub_install_get_platforms_string (void)
682 char **arr = xmalloc (sizeof (char *) * ARRAY_SIZE (platforms));
683 int platform_strins_len = 0;
684 char *platforms_string;
687 for (i = 0; i < ARRAY_SIZE (platforms); i++)
689 arr[i] = xasprintf ("%s-%s", platforms[i].cpu,
690 platforms[i].platform);
691 platform_strins_len += strlen (arr[i]) + 2;
693 ptr = platforms_string = xmalloc (platform_strins_len);
694 qsort (arr, ARRAY_SIZE (platforms), sizeof (char *), grub_qsort_strcmp);
695 for (i = 0; i < ARRAY_SIZE (platforms); i++)
697 strcpy (ptr, arr[i]);
698 ptr += strlen (arr[i]);
706 return platforms_string;
710 grub_install_get_platform_name (enum grub_install_plat platid)
712 return xasprintf ("%s-%s", platforms[platid].cpu,
713 platforms[platid].platform);
717 grub_install_get_platform_cpu (enum grub_install_plat platid)
719 return platforms[platid].cpu;
723 grub_install_get_platform_platform (enum grub_install_plat platid)
725 return platforms[platid].platform;
730 grub_install_copy_files (const char *src,
732 enum grub_install_plat platid)
734 char *dst_platform, *dst_locale, *dst_fonts;
735 const char *pkgdatadir = grub_util_get_pkgdatadir ();
740 platform = xasprintf ("%s-%s", platforms[platid].cpu,
741 platforms[platid].platform);
742 dst_platform = grub_util_path_concat (2, dst, platform);
745 dst_locale = grub_util_path_concat (2, dst, "locale");
746 dst_fonts = grub_util_path_concat (2, dst, "fonts");
747 grub_install_mkdir_p (dst_platform);
748 grub_install_mkdir_p (dst_locale);
749 clean_grub_dir (dst);
750 clean_grub_dir (dst_platform);
751 clean_grub_dir (dst_locale);
753 if (install_modules.is_default)
754 copy_by_ext (src, dst_platform, ".mod", 1);
757 struct grub_util_path_list *path_list, *p;
759 path_list = grub_util_resolve_dependencies (src, "moddep.lst",
760 install_modules.entries);
761 for (p = path_list; p; p = p->next)
763 const char *srcf = p->name;
767 dir = grub_strrchr (srcf, '/');
772 dstf = grub_util_path_concat (2, dst_platform, dir);
773 grub_install_compress_file (srcf, dstf, 1);
777 grub_util_free_path_list (path_list);
780 const char *pkglib_DATA[] = {"efiemu32.o", "efiemu64.o",
781 "moddep.lst", "command.lst",
782 "fs.lst", "partmap.lst",
784 "video.lst", "crypto.lst",
785 "terminal.lst", "modinfo.sh" };
788 for (i = 0; i < ARRAY_SIZE (pkglib_DATA); i++)
790 char *srcf = grub_util_path_concat (2, src, pkglib_DATA[i]);
791 char *dstf = grub_util_path_concat (2, dst_platform, pkglib_DATA[i]);
792 if (i == 0 || i == 1)
793 grub_install_compress_file (srcf, dstf, 0);
795 grub_install_compress_file (srcf, dstf, 1);
800 if (install_locales.is_default)
802 char *srcd = grub_util_path_concat (2, src, "po");
803 copy_by_ext (srcd, dst_locale, ".mo", 0);
804 copy_locales (dst_locale);
809 const char *locale_dir = get_localedir ();
811 for (i = 0; i < install_locales.n_entries; i++)
813 char *srcf = grub_util_path_concat_ext (3, src,
815 install_locales.entries[i],
817 char *dstf = grub_util_path_concat_ext (2, dst_locale,
818 install_locales.entries[i],
820 if (grub_install_compress_file (srcf, dstf, 0))
827 srcf = grub_util_path_concat_ext (4,
829 install_locales.entries[i],
833 if (grub_install_compress_file (srcf, dstf, 0))
839 grub_util_error (_("cannot find locale `%s'"),
840 install_locales.entries[i]);
844 if (install_themes.is_default)
846 install_themes.is_default = 0;
847 install_themes.n_entries = 1;
848 install_themes.entries = xmalloc (2 * sizeof (install_themes.entries[0]));
849 install_themes.entries[0] = xstrdup ("starfield");
850 install_themes.entries[1] = NULL;
853 if (grub_install_themes_directory)
854 themes_dir = xstrdup (grub_install_themes_directory);
856 themes_dir = grub_util_path_concat (2, grub_util_get_pkgdatadir (),
859 for (i = 0; i < install_themes.n_entries; i++)
861 char *srcf = grub_util_path_concat (3, themes_dir,
862 install_themes.entries[i],
864 if (grub_util_is_regular (srcf))
866 char *srcd = grub_util_path_concat (2, themes_dir,
867 install_themes.entries[i]);
868 char *dstd = grub_util_path_concat (3, dst, "themes",
869 install_themes.entries[i]);
870 grub_install_mkdir_p (dstd);
871 copy_all (srcd, dstd);
880 if (install_fonts.is_default)
882 install_fonts.is_default = 0;
883 install_fonts.n_entries = 1;
884 install_fonts.entries = xmalloc (2 * sizeof (install_fonts.entries[0]));
885 install_fonts.entries[0] = xstrdup ("unicode");
886 install_fonts.entries[1] = NULL;
889 grub_install_mkdir_p (dst_fonts);
891 for (i = 0; i < install_fonts.n_entries; i++)
893 char *srcf = grub_util_path_concat_ext (2, pkgdatadir,
894 install_fonts.entries[i],
896 char *dstf = grub_util_path_concat_ext (2, dst_fonts,
897 install_fonts.entries[i],
900 grub_install_compress_file (srcf, dstf, 0);
910 enum grub_install_plat
911 grub_install_get_target (const char *src)
919 fn = grub_util_path_concat (2, src, "modinfo.sh");
920 f = grub_util_fd_open (fn, GRUB_UTIL_FD_O_RDONLY);
921 if (!GRUB_UTIL_FD_IS_VALID (f))
922 grub_util_error (_("%s doesn't exist. Please specify --target or --directory"),
924 r = grub_util_fd_read (f, buf, sizeof (buf) - 1);
926 grub_util_error (_("cannot read `%s': %s"), fn, strerror (errno));
927 grub_util_fd_close (f);
929 c = strstr (buf, "grub_modinfo_target_cpu=");
930 if (!c || (c != buf && !grub_isspace (*(c-1))))
931 grub_util_error (_("invalid modinfo file `%s'"), fn);
932 pl = strstr (buf, "grub_modinfo_platform=");
933 if (!pl || (pl != buf && !grub_isspace (*(pl-1))))
934 grub_util_error (_("invalid modinfo file `%s'"), fn);
935 c += sizeof ("grub_modinfo_target_cpu=") - 1;
936 pl += sizeof ("grub_modinfo_platform=") - 1;
937 for (p = c; *p && !grub_isspace (*p); p++);
939 for (p = pl; *p && !grub_isspace (*p); p++);
942 for (i = 0; i < ARRAY_SIZE (platforms); i++)
943 if (strcmp (platforms[i].cpu, c) == 0
944 && strcmp (platforms[i].platform, pl) == 0)
949 grub_util_error (_("Unknown platform `%s-%s'"), c, pl);
954 grub_util_unlink_recursive (const char *name)
956 grub_util_fd_dir_t d;
957 grub_util_fd_dirent_t de;
959 d = grub_util_fd_opendir (name);
961 while ((de = grub_util_fd_readdir (d)))
964 if (strcmp (de->d_name, ".") == 0)
966 if (strcmp (de->d_name, "..") == 0)
968 fp = grub_util_path_concat (2, name, de->d_name);
969 if (grub_util_is_special_file (fp))
974 if (grub_util_is_regular (fp))
975 grub_util_unlink (fp);
976 else if (grub_util_is_directory (fp))
977 grub_util_unlink_recursive (fp);
980 grub_util_rmdir (name);
981 grub_util_fd_closedir (d);