]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/util/grub-mknetdir.c
Update Hindi Translation (#1941)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / util / grub-mknetdir.c
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
18 #include <config.h>
19
20 #include <grub/util/install.h>
21 #include <grub/emu/config.h>
22 #include <grub/util/misc.h>
23
24 #include <string.h>
25 #include <errno.h>
26
27 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
28 #pragma GCC diagnostic ignored "-Wmissing-declarations"
29 #include <argp.h>
30 #pragma GCC diagnostic error "-Wmissing-prototypes"
31 #pragma GCC diagnostic error "-Wmissing-declarations"
32
33 static char *rootdir = NULL, *subdir = NULL;
34 static char *debug_image = NULL;
35
36 enum
37 {
38 OPTION_NET_DIRECTORY = 0x301,
39 OPTION_SUBDIR,
40 OPTION_DEBUG,
41 OPTION_DEBUG_IMAGE
42 };
43
44 static struct argp_option options[] = {
45 GRUB_INSTALL_OPTIONS,
46 {"net-directory", OPTION_NET_DIRECTORY, N_("DIR"),
47 0, N_("root directory of TFTP server"), 2},
48 {"subdir", OPTION_SUBDIR, N_("DIR"),
49 0, N_("relative subdirectory on network server"), 2},
50 {"debug", OPTION_DEBUG, 0, OPTION_HIDDEN, 0, 2},
51 {"debug-image", OPTION_DEBUG_IMAGE, N_("STRING"), OPTION_HIDDEN, 0, 2},
52 {0, 0, 0, 0, 0, 0}
53 };
54
55 static error_t
56 argp_parser (int key, char *arg, struct argp_state *state)
57 {
58 if (grub_install_parse (key, arg))
59 return 0;
60 switch (key)
61 {
62 case OPTION_NET_DIRECTORY:
63 free (rootdir);
64 rootdir = xstrdup (arg);
65 return 0;
66 case OPTION_SUBDIR:
67 free (subdir);
68 subdir = xstrdup (arg);
69 return 0;
70 /* This is an undocumented feature... */
71 case OPTION_DEBUG:
72 verbosity++;
73 return 0;
74 case OPTION_DEBUG_IMAGE:
75 free (debug_image);
76 debug_image = xstrdup (arg);
77 return 0;
78
79 case ARGP_KEY_ARG:
80 default:
81 return ARGP_ERR_UNKNOWN;
82 }
83 }
84
85
86 struct argp argp = {
87 options, argp_parser, NULL,
88 "\v"N_("Prepares GRUB network boot images at net_directory/subdir "
89 "assuming net_directory being TFTP root."),
90 NULL, grub_install_help_filter, NULL
91 };
92
93 static char *base;
94
95 static const struct
96 {
97 const char *mkimage_target;
98 const char *netmodule;
99 const char *ext;
100 } targets[GRUB_INSTALL_PLATFORM_MAX] =
101 {
102 [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386-pc-pxe", "pxe", ".0" },
103 [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64-ieee1275-aout", "ofnet", ".img" },
104 [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386-ieee1275", "ofnet", ".elf" },
105 [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc-ieee1275", "ofnet", ".elf" },
106 [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386-efi", "efinet", ".efi" },
107 [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64-efi", "efinet", ".efi" },
108 [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64-efi", "efinet", ".efi" },
109 [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm-efi", "efinet", ".efi" },
110 [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" },
111 [GRUB_INSTALL_PLATFORM_MIPS64EL_EFI] = { "mips64el-efi", "efinet", ".efi" },
112 [GRUB_INSTALL_PLATFORM_RISCV32_EFI] = { "riscv32-efi", "efinet", ".efi" },
113 [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64-efi", "efinet", ".efi" },
114 };
115
116 static void
117 process_input_dir (const char *input_dir, enum grub_install_plat platform)
118 {
119 char *platsub = grub_install_get_platform_name (platform);
120 char *grubdir = grub_util_path_concat (3, rootdir, subdir, platsub);
121 char *load_cfg = grub_util_path_concat (2, grubdir, "load.cfg");
122 char *prefix;
123 char *output;
124 char *grub_cfg;
125 FILE *cfg;
126
127 grub_install_copy_files (input_dir, base, platform);
128 grub_util_unlink (load_cfg);
129
130 if (debug_image)
131 {
132 FILE *f = grub_util_fopen (load_cfg, "wb");
133 if (!f)
134 grub_util_error (_("cannot open `%s': %s"), load_cfg,
135 strerror (errno));
136 fprintf (f, "set debug='%s'\n", debug_image);
137 fclose (f);
138 }
139 else
140 {
141 free (load_cfg);
142 load_cfg = 0;
143 }
144
145 prefix = xasprintf ("/%s", subdir);
146 if (!targets[platform].mkimage_target)
147 grub_util_error (_("unsupported platform %s"), platsub);
148
149 grub_cfg = grub_util_path_concat (2, grubdir, "grub.cfg");
150 cfg = grub_util_fopen (grub_cfg, "wb");
151 if (!cfg)
152 grub_util_error (_("cannot open `%s': %s"), grub_cfg,
153 strerror (errno));
154 fprintf (cfg, "source %s/grub.cfg", subdir);
155 fclose (cfg);
156
157 grub_install_push_module (targets[platform].netmodule);
158
159 output = grub_util_path_concat_ext (2, grubdir, "core", targets[platform].ext);
160 grub_install_make_image_wrap (input_dir, prefix, output,
161 0, load_cfg,
162 targets[platform].mkimage_target, 0);
163 grub_install_pop_module ();
164
165 /* TRANSLATORS: First %s is replaced by platform name. Second one by filename. */
166 printf (_("Netboot directory for %s created. Configure your DHCP server to point to %s\n"),
167 platsub, output);
168
169 free (platsub);
170 free (output);
171 free (prefix);
172 free (grub_cfg);
173 free (grubdir);
174 }
175
176
177 int
178 main (int argc, char *argv[])
179 {
180 const char *pkglibdir;
181
182 grub_util_host_init (&argc, &argv);
183 grub_util_disable_fd_syncs ();
184 rootdir = xstrdup ("/srv/tftp");
185 pkglibdir = grub_util_get_pkglibdir ();
186
187 subdir = grub_util_path_concat (2, GRUB_BOOT_DIR_NAME, GRUB_DIR_NAME);
188
189 argp_parse (&argp, argc, argv, 0, 0, 0);
190
191 base = grub_util_path_concat (2, rootdir, subdir);
192 /* Create the GRUB directory if it is not present. */
193
194 grub_install_mkdir_p (base);
195
196 grub_install_push_module ("tftp");
197
198 if (!grub_install_source_directory)
199 {
200 enum grub_install_plat plat;
201
202 for (plat = 0; plat < GRUB_INSTALL_PLATFORM_MAX; plat++)
203 if (targets[plat].mkimage_target)
204 {
205 char *platdir = grub_util_path_concat (2, pkglibdir,
206 grub_install_get_platform_name (plat));
207
208 grub_util_info ("Looking for `%s'", platdir);
209
210 if (!grub_util_is_directory (platdir))
211 {
212 free (platdir);
213 continue;
214 }
215 process_input_dir (platdir, plat);
216 }
217 }
218 else
219 {
220 enum grub_install_plat plat;
221 plat = grub_install_get_target (grub_install_source_directory);
222 process_input_dir (grub_install_source_directory, plat);
223 }
224 return 0;
225 }