2 #if defined(HAVE_CONFIG_H)
8 #include <talloc/talloc.h>
11 #include <file/file.h>
12 #include <i18n/i18n.h>
14 #include "device-handler.h"
16 #include "parser-conf.h"
17 #include "parser-utils.h"
21 #include "user-event.h"
24 static const char *pxelinux_prefix = "pxelinux.cfg/";
26 static void pxe_conf_parse_cb(struct load_url_result *result, void *data);
28 struct pxe_parser_info {
29 struct discover_boot_option *opt;
30 const char *default_name;
31 char **pxe_conf_files;
32 struct pb_url *pxe_base_url;
36 static void pxe_finish(struct conf_context *conf)
38 struct pxe_parser_info *info = conf->parser_info;
40 discover_context_add_boot_option(conf->dc, info->opt);
43 /* We need a slightly modified version of pb_url_join, to allow for the
44 * pxelinux "::filename" syntax for absolute URLs
46 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
49 struct pb_url *new_url;
54 if (len > 2 && s[0] == ':' && s[1] == ':') {
58 /* ::/path -> /path */
59 tmp = talloc_strdup(ctx, s+2);
62 tmp = talloc_strdup(ctx, s+1);
66 new_url = pb_url_join(ctx, url, tmp);
72 /* strip leading slashes */
73 for (tmp = s; *tmp == '/'; tmp++)
75 new_url = pb_url_join(ctx, url, tmp);
81 static void pxe_append_string(struct discover_boot_option *opt,
84 if (opt->option->boot_args)
85 opt->option->boot_args = talloc_asprintf_append(
86 opt->option->boot_args, " %s", str);
88 opt->option->boot_args = talloc_strdup(opt->option, str);
91 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
96 return talloc_asprintf(ctx,
97 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
98 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
101 static void pxe_process_sysappend(struct discover_context *ctx,
102 struct discover_boot_option *opt,
105 struct event *event = ctx->event;
112 uint8_t *mac = find_mac_by_name(ctx,
113 device_handler_get_network(ctx->handler),
115 str = pxe_sysappend_mac(ctx, mac);
117 pxe_append_string(opt, str);
124 pb_log("pxe: unsupported features requested in "
125 "ipappend/sysappend: 0x%04lx", val);
129 static void pxe_process_pair(struct conf_context *ctx,
130 const char *name, char *value)
132 struct pxe_parser_info *parser_info = ctx->parser_info;
133 struct discover_boot_option *opt = parser_info->opt;
136 /* quirk in the syslinux config format: initrd can be separated
138 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
140 value += strlen("initrd=");
146 if (streq(name, "DEFAULT")) {
147 parser_info->default_name = talloc_strdup(parser_info, value);
151 if (streq(name, "LABEL")) {
155 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
157 opt->option->name = talloc_strdup(opt, value);
158 opt->option->id = talloc_asprintf(opt, "%s@%p",
159 ctx->dc->device->device->id, opt);
161 opt->option->is_default = parser_info->default_name &&
162 streq(parser_info->default_name, value);
164 parser_info->opt = opt;
168 /* all other parameters need an option */
172 if (streq(name, "KERNEL")) {
173 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
174 opt->boot_image = create_url_resource(opt, url);
176 char* args_sigfile_default = talloc_asprintf(opt,
177 "%s.cmdline.sig", value);
178 url = pxe_url_join(ctx->dc, ctx->dc->conf_url,
179 args_sigfile_default);
180 opt->args_sig_file = create_url_resource(opt, url);
181 talloc_free(args_sigfile_default);
183 } else if (streq(name, "INITRD")) {
184 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
185 opt->initrd = create_url_resource(opt, url);
187 } else if (streq(name, "APPEND")) {
190 pxe_append_string(opt, value);
192 str = strcasestr(value, "INITRD=");
194 str += strlen("INITRD=");
195 end = strchrnul(str, ' ');
198 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
199 opt->initrd = create_url_resource(opt, url);
201 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
205 type = strtoul(value, &end, 10);
206 if (end != value && !(*end))
207 pxe_process_sysappend(ctx->dc, opt, type);
209 } else if (streq(name, "DTB") || streq(name, "FDT")) {
210 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
211 opt->dtb = create_url_resource(opt, url);
216 static void pxe_load_next_filename(struct conf_context *conf)
218 struct pxe_parser_info *info = conf->parser_info;
221 if (!info->pxe_conf_files)
224 for (; info->pxe_conf_files[info->current]; info->current++) {
225 url = pb_url_join(conf->dc, info->pxe_base_url,
226 info->pxe_conf_files[info->current]);
230 if (load_url_async(conf, url, pxe_conf_parse_cb, conf))
238 * Callback for asynchronous loads from pxe_parse()
239 * @param result Result of load_url_async()
240 * @param data Pointer to associated conf_context
242 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
244 struct conf_context *conf = data;
245 struct device_handler *handler;
246 struct pxe_parser_info *info;
253 handler = talloc_parent(conf);
255 if (result && result->status == LOAD_OK)
256 rc = read_file(conf, result->local, &buf, &len);
257 if (!result || result->status != LOAD_OK || rc) {
258 /* This load failed so try the next available filename */
259 info = conf->parser_info;
260 if (!info->pxe_conf_files) {
261 device_handler_status_dev_err(handler,
263 _("Failed to download %s"),
264 pb_url_to_string(result->url));
270 pxe_load_next_filename(conf);
271 if (info->pxe_conf_files[info->current] == NULL) {
272 /* Nothing left to try */
273 device_handler_status_dev_err(handler,
275 _("PXE autoconfiguration failed"));
282 * Parse the first successfully downloaded file. We only want to parse
283 * the first because otherwise we could parse options from both a
284 * machine-specific config and a 'fallback' default config
287 conf_parse_buf(conf, buf, len);
289 /* We may be called well after the original caller of iterate_parsers(),
290 * commit any new boot options ourselves */
291 device_handler_discover_context_commit(handler, conf->dc);
294 * TRANSLATORS: the format specifier in this string is a URL
295 * eg. tftp://192.168.1.1/pxelinux.cfg
297 device_handler_status_dev_info(handler, conf->dc->device,
298 _("Parsed PXE config from %s"),
299 pb_url_to_string(conf->dc->conf_url));
303 if (result->cleanup_local)
304 unlink(result->local);
309 * Return a new conf_context and increment the talloc reference count on
310 * the discover_context struct.
311 * @param ctx Parent talloc context
312 * @param orig Original discover_context
313 * @return Pointer to new conf_context
315 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
317 struct pxe_parser_info *info;
318 struct conf_context *conf;
320 conf = talloc_zero(ctx, struct conf_context);
325 conf->get_pair = conf_get_pair_space;
326 conf->process_pair = pxe_process_pair;
327 conf->finish = pxe_finish;
328 info = talloc_zero(conf, struct pxe_parser_info);
333 conf->parser_info = info;
336 * The discover_context may be freed once pxe_parse() returns, but the
337 * callback will still need it. Take a reference so that that it will
338 * persist until the last callback completes.
340 conf->dc = talloc_reference(conf, dc);
345 static int pxe_parse(struct discover_context *dc)
347 struct pb_url *pxe_base_url;
348 struct conf_context *conf = NULL;
349 struct load_url_result *result;
350 void *ctx = talloc_parent(dc);
351 struct pxe_parser_info *info;
352 char **pxe_conf_files;
355 /* Expects dhcp event parameters to support network boot */
359 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
364 * Retrieving PXE configs over the network can take some time depending
365 * on factors such as slow network, malformed paths, bad DNS, and
366 * overzealous firewalls. Instead of blocking the discover server while
367 * we wait for these, spawn an asynchronous job that will attempt to
368 * retrieve each possible URL until it successfully finds one, and
369 * parse and process the resulting file in a callback.
371 conf = copy_context(ctx, dc);
376 device_handler_status_dev_info(conf->dc->handler,
378 _("Requesting config %s"),
379 pb_url_to_string(conf->dc->conf_url));
381 /* we have a complete URL; use this and we're done. */
382 result = load_url_async(conf->dc, conf->dc->conf_url,
383 pxe_conf_parse_cb, conf);
385 pb_log("load_url_async fails for %s\n",
390 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
394 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
398 info = conf->parser_info;
399 info->pxe_conf_files = pxe_conf_files;
400 info->pxe_base_url = pxe_base_url;
402 device_handler_status_dev_info(conf->dc->handler,
404 _("Probing from base %s"),
405 pb_url_to_string(pxe_base_url));
407 pxe_load_next_filename(conf);
413 talloc_free(pxe_conf_files);
419 static struct parser pxe_parser = {
424 register_parser(pxe_parser);