]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/syslinuxcfg.c
probe: Support probing for partition UUID with --part-uuid
[grub2.git] / grub-core / commands / syslinuxcfg.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 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 <grub/types.h>
20 #include <grub/misc.h>
21 #include <grub/extcmd.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/file.h>
26 #include <grub/normal.h>
27 #include <grub/script_sh.h>
28 #include <grub/i18n.h>
29 #include <grub/term.h>
30 #include <grub/syslinux_parse.h>
31 #include <grub/crypto.h>
32 #include <grub/auth.h>
33 #include <grub/disk.h>
34 #include <grub/partition.h>
35
36 GRUB_MOD_LICENSE ("GPLv3+");
37
38 /* Helper for syslinux_file. */
39 static grub_err_t
40 syslinux_file_getline (char **line, int cont __attribute__ ((unused)),
41 void *data __attribute__ ((unused)))
42 {
43 *line = 0;
44 return GRUB_ERR_NONE;
45 }
46
47 static const struct grub_arg_option options[] =
48 {
49 {"root", 'r', 0,
50 N_("root directory of the syslinux disk [default=/]."),
51 N_("DIR"), ARG_TYPE_STRING},
52 {"cwd", 'c', 0,
53 N_("current directory of syslinux [default is parent directory of input file]."),
54 N_("DIR"), ARG_TYPE_STRING},
55 {"isolinux", 'i', 0, N_("assume input is an isolinux configuration file."), 0, 0},
56 {"pxelinux", 'p', 0, N_("assume input is a pxelinux configuration file."), 0, 0},
57 {"syslinux", 's', 0, N_("assume input is a syslinux configuration file."), 0, 0},
58 {0, 0, 0, 0, 0, 0}
59 };
60
61 enum
62 {
63 OPTION_ROOT,
64 OPTION_CWD,
65 OPTION_ISOLINUX,
66 OPTION_PXELINUX,
67 OPTION_SYSLINUX
68 };
69
70 static grub_err_t
71 syslinux_file (grub_extcmd_context_t ctxt, const char *filename)
72 {
73 char *result;
74 const char *root = ctxt->state[OPTION_ROOT].set ? ctxt->state[OPTION_ROOT].arg : "/";
75 const char *cwd = ctxt->state[OPTION_CWD].set ? ctxt->state[OPTION_CWD].arg : NULL;
76 grub_syslinux_flavour_t flav = GRUB_SYSLINUX_UNKNOWN;
77 char *cwdf = NULL;
78 grub_menu_t menu;
79
80 if (ctxt->state[OPTION_ISOLINUX].set)
81 flav = GRUB_SYSLINUX_ISOLINUX;
82 if (ctxt->state[OPTION_PXELINUX].set)
83 flav = GRUB_SYSLINUX_PXELINUX;
84 if (ctxt->state[OPTION_SYSLINUX].set)
85 flav = GRUB_SYSLINUX_SYSLINUX;
86
87 if (!cwd)
88 {
89 char *p;
90 cwdf = grub_strdup (filename);
91 if (!cwdf)
92 return grub_errno;
93 p = grub_strrchr (cwdf, '/');
94 if (!p)
95 {
96 grub_free (cwdf);
97 cwd = "/";
98 cwdf = NULL;
99 }
100 else
101 {
102 *p = '\0';
103 cwd = cwdf;
104 }
105 }
106
107 grub_dprintf ("syslinux",
108 "transforming syslinux config %s, root = %s, cwd = %s\n",
109 filename, root, cwd);
110
111 result = grub_syslinux_config_file (root, root, cwd, cwd, filename, flav);
112 if (!result)
113 return grub_errno;
114
115 grub_dprintf ("syslinux", "syslinux config transformed\n");
116
117 menu = grub_env_get_menu ();
118 if (! menu)
119 {
120 menu = grub_zalloc (sizeof (*menu));
121 if (! menu)
122 {
123 grub_free (result);
124 return grub_errno;
125 }
126
127 grub_env_set_menu (menu);
128 }
129
130 grub_normal_parse_line (result, syslinux_file_getline, NULL);
131 grub_print_error ();
132 grub_free (result);
133 grub_free (cwdf);
134
135 return GRUB_ERR_NONE;
136 }
137
138 static grub_err_t
139 grub_cmd_syslinux_source (grub_extcmd_context_t ctxt,
140 int argc, char **args)
141 {
142 int new_env, extractor;
143 grub_err_t ret;
144
145 if (argc != 1)
146 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
147
148 extractor = (ctxt->extcmd->cmd->name[0] == 'e');
149 new_env = (ctxt->extcmd->cmd->name[extractor ? (sizeof ("extract_syslinux_entries_") - 1)
150 : (sizeof ("syslinux_") - 1)] == 'c');
151
152 if (new_env)
153 grub_cls ();
154
155 if (new_env && !extractor)
156 grub_env_context_open ();
157 if (extractor)
158 grub_env_extractor_open (!new_env);
159
160 ret = syslinux_file (ctxt, args[0]);
161
162 if (new_env)
163 {
164 grub_menu_t menu;
165 menu = grub_env_get_menu ();
166 if (menu && menu->size)
167 grub_show_menu (menu, 1, 0);
168 if (!extractor)
169 grub_env_context_close ();
170 }
171 if (extractor)
172 grub_env_extractor_close (!new_env);
173
174 return ret;
175 }
176
177
178 static grub_extcmd_t cmd_source, cmd_configfile;
179 static grub_extcmd_t cmd_source_extract, cmd_configfile_extract;
180
181 GRUB_MOD_INIT(syslinuxcfg)
182 {
183 cmd_source
184 = grub_register_extcmd ("syslinux_source",
185 grub_cmd_syslinux_source, 0,
186 N_("FILE"),
187 /* TRANSLATORS: "syslinux config" means
188 "config as used by syslinux". */
189 N_("Execute syslinux config in same context"),
190 options);
191 cmd_configfile
192 = grub_register_extcmd ("syslinux_configfile",
193 grub_cmd_syslinux_source, 0,
194 N_("FILE"),
195 N_("Execute syslinux config in new context"),
196 options);
197 cmd_source_extract
198 = grub_register_extcmd ("extract_syslinux_entries_source",
199 grub_cmd_syslinux_source, 0,
200 N_("FILE"),
201 N_("Execute syslinux config in same context taking only menu entries"),
202 options);
203 cmd_configfile_extract
204 = grub_register_extcmd ("extract_syslinux_entries_configfile",
205 grub_cmd_syslinux_source, 0,
206 N_("FILE"),
207 N_("Execute syslinux config in new context taking only menu entries"),
208 options);
209 }
210
211 GRUB_MOD_FINI(syslinuxcfg)
212 {
213 grub_unregister_extcmd (cmd_source);
214 grub_unregister_extcmd (cmd_configfile);
215 grub_unregister_extcmd (cmd_source_extract);
216 grub_unregister_extcmd (cmd_configfile_extract);
217 }