grub-mknetdir.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2013 Free Software Foundation, Inc.
  3. *
  4. * GRUB is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * GRUB is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <config.h>
  18. #include <grub/util/install.h>
  19. #include <grub/emu/config.h>
  20. #include <grub/util/misc.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  24. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  25. #include <argp.h>
  26. #pragma GCC diagnostic error "-Wmissing-prototypes"
  27. #pragma GCC diagnostic error "-Wmissing-declarations"
  28. static char *rootdir = NULL, *subdir = NULL;
  29. static char *debug_image = NULL;
  30. enum
  31. {
  32. OPTION_NET_DIRECTORY = 0x301,
  33. OPTION_SUBDIR,
  34. OPTION_DEBUG,
  35. OPTION_DEBUG_IMAGE
  36. };
  37. static struct argp_option options[] = {
  38. GRUB_INSTALL_OPTIONS,
  39. {"net-directory", OPTION_NET_DIRECTORY, N_("DIR"),
  40. 0, N_("root directory of TFTP server"), 2},
  41. {"subdir", OPTION_SUBDIR, N_("DIR"),
  42. 0, N_("relative subdirectory on network server"), 2},
  43. {"debug", OPTION_DEBUG, 0, OPTION_HIDDEN, 0, 2},
  44. {"debug-image", OPTION_DEBUG_IMAGE, N_("STRING"), OPTION_HIDDEN, 0, 2},
  45. {0, 0, 0, 0, 0, 0}
  46. };
  47. static error_t
  48. argp_parser (int key, char *arg, struct argp_state *state)
  49. {
  50. if (grub_install_parse (key, arg))
  51. return 0;
  52. switch (key)
  53. {
  54. case OPTION_NET_DIRECTORY:
  55. free (rootdir);
  56. rootdir = xstrdup (arg);
  57. return 0;
  58. case OPTION_SUBDIR:
  59. free (subdir);
  60. subdir = xstrdup (arg);
  61. return 0;
  62. /* This is an undocumented feature... */
  63. case OPTION_DEBUG:
  64. verbosity++;
  65. return 0;
  66. case OPTION_DEBUG_IMAGE:
  67. free (debug_image);
  68. debug_image = xstrdup (arg);
  69. return 0;
  70. case ARGP_KEY_ARG:
  71. default:
  72. return ARGP_ERR_UNKNOWN;
  73. }
  74. }
  75. struct argp argp = {
  76. options, argp_parser, NULL,
  77. "\v"N_("Prepares GRUB network boot images at net_directory/subdir "
  78. "assuming net_directory being TFTP root."),
  79. NULL, grub_install_help_filter, NULL
  80. };
  81. static char *base;
  82. static const struct
  83. {
  84. const char *mkimage_target;
  85. const char *netmodule;
  86. const char *ext;
  87. } targets[GRUB_INSTALL_PLATFORM_MAX] =
  88. {
  89. [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386-pc-pxe", "pxe", ".0" },
  90. [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64-ieee1275-aout", "ofnet", ".img" },
  91. [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386-ieee1275", "ofnet", ".elf" },
  92. [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc-ieee1275", "ofnet", ".elf" },
  93. [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386-efi", "efinet", ".efi" },
  94. [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64-efi", "efinet", ".efi" },
  95. [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64-efi", "efinet", ".efi" },
  96. [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm-efi", "efinet", ".efi" },
  97. [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" }
  98. };
  99. static void
  100. process_input_dir (const char *input_dir, enum grub_install_plat platform)
  101. {
  102. char *platsub = grub_install_get_platform_name (platform);
  103. char *grubdir = grub_util_path_concat (3, rootdir, subdir, platsub);
  104. char *load_cfg = grub_util_path_concat (2, grubdir, "load.cfg");
  105. char *prefix;
  106. char *output;
  107. char *grub_cfg;
  108. FILE *cfg;
  109. grub_install_copy_files (input_dir, base, platform);
  110. grub_util_unlink (load_cfg);
  111. if (debug_image)
  112. {
  113. FILE *f = grub_util_fopen (load_cfg, "wb");
  114. if (!f)
  115. grub_util_error (_("cannot open `%s': %s"), load_cfg,
  116. strerror (errno));
  117. fprintf (f, "set debug='%s'\n", debug_image);
  118. fclose (f);
  119. }
  120. else
  121. {
  122. free (load_cfg);
  123. load_cfg = 0;
  124. }
  125. prefix = xasprintf ("/%s", subdir);
  126. if (!targets[platform].mkimage_target)
  127. grub_util_error (_("unsupported platform %s"), platsub);
  128. grub_cfg = grub_util_path_concat (2, grubdir, "grub.cfg");
  129. cfg = grub_util_fopen (grub_cfg, "wb");
  130. if (!cfg)
  131. grub_util_error (_("cannot open `%s': %s"), grub_cfg,
  132. strerror (errno));
  133. fprintf (cfg, "source %s/grub.cfg", subdir);
  134. fclose (cfg);
  135. grub_install_push_module (targets[platform].netmodule);
  136. output = grub_util_path_concat_ext (2, grubdir, "core", targets[platform].ext);
  137. grub_install_make_image_wrap (input_dir, prefix, output,
  138. 0, load_cfg,
  139. targets[platform].mkimage_target, 0);
  140. grub_install_pop_module ();
  141. /* TRANSLATORS: First %s is replaced by platform name. Second one by filename. */
  142. printf (_("Netboot directory for %s created. Configure your DHCP server to point to %s\n"),
  143. platsub, output);
  144. free (platsub);
  145. free (output);
  146. free (prefix);
  147. free (grub_cfg);
  148. free (grubdir);
  149. }
  150. int
  151. main (int argc, char *argv[])
  152. {
  153. const char *pkglibdir;
  154. grub_util_host_init (&argc, &argv);
  155. grub_util_disable_fd_syncs ();
  156. rootdir = xstrdup ("/srv/tftp");
  157. pkglibdir = grub_util_get_pkglibdir ();
  158. subdir = grub_util_path_concat (2, GRUB_BOOT_DIR_NAME, GRUB_DIR_NAME);
  159. argp_parse (&argp, argc, argv, 0, 0, 0);
  160. base = grub_util_path_concat (2, rootdir, subdir);
  161. /* Create the GRUB directory if it is not present. */
  162. grub_install_mkdir_p (base);
  163. grub_install_push_module ("tftp");
  164. if (!grub_install_source_directory)
  165. {
  166. enum grub_install_plat plat;
  167. for (plat = 0; plat < GRUB_INSTALL_PLATFORM_MAX; plat++)
  168. if (targets[plat].mkimage_target)
  169. {
  170. char *platdir = grub_util_path_concat (2, pkglibdir,
  171. grub_install_get_platform_name (plat));
  172. grub_util_info ("Looking for `%s'", platdir);
  173. if (!grub_util_is_directory (platdir))
  174. {
  175. free (platdir);
  176. continue;
  177. }
  178. process_input_dir (platdir, plat);
  179. }
  180. }
  181. else
  182. {
  183. enum grub_install_plat plat;
  184. plat = grub_install_get_target (grub_install_source_directory);
  185. process_input_dir (grub_install_source_directory, plat);
  186. }
  187. return 0;
  188. }