2 #if defined(HAVE_CONFIG_H)
8 #include <talloc/talloc.h>
11 #include <file/file.h>
12 #include <i18n/i18n.h>
15 #include "parser-conf.h"
16 #include "parser-utils.h"
20 #include "user-event.h"
23 static const char *pxelinux_prefix = "pxelinux.cfg/";
25 struct pxe_parser_info {
26 struct discover_boot_option *opt;
27 const char *default_name;
30 static void pxe_finish(struct conf_context *conf)
32 struct pxe_parser_info *info = conf->parser_info;
34 discover_context_add_boot_option(conf->dc, info->opt);
37 /* We need a slightly modified version of pb_url_join, to allow for the
38 * pxelinux "::filename" syntax for absolute URLs
40 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
43 struct pb_url *new_url;
48 if (len > 2 && s[0] == ':' && s[1] == ':') {
52 /* ::/path -> /path */
53 tmp = talloc_strdup(ctx, s+2);
56 tmp = talloc_strdup(ctx, s+1);
60 new_url = pb_url_join(ctx, url, tmp);
66 /* strip leading slashes */
67 for (tmp = s; *tmp == '/'; tmp++)
69 new_url = pb_url_join(ctx, url, tmp);
75 static void pxe_append_string(struct discover_boot_option *opt,
78 if (opt->option->boot_args)
79 opt->option->boot_args = talloc_asprintf_append(
80 opt->option->boot_args, " %s", str);
82 opt->option->boot_args = talloc_strdup(opt->option, str);
85 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
90 return talloc_asprintf(ctx,
91 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
92 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
95 static void pxe_process_sysappend(struct discover_context *ctx,
96 struct discover_boot_option *opt,
99 struct event *event = ctx->event;
106 uint8_t *mac = find_mac_by_name(ctx, ctx->network,
108 str = pxe_sysappend_mac(ctx, mac);
110 pxe_append_string(opt, str);
117 pb_log("pxe: unsupported features requested in "
118 "ipappend/sysappend: 0x%04lx", val);
122 static void pxe_process_pair(struct conf_context *ctx,
123 const char *name, char *value)
125 struct pxe_parser_info *parser_info = ctx->parser_info;
126 struct discover_boot_option *opt = parser_info->opt;
129 /* quirk in the syslinux config format: initrd can be separated
131 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
133 value += strlen("initrd=");
139 if (streq(name, "DEFAULT")) {
140 parser_info->default_name = talloc_strdup(parser_info, value);
144 if (streq(name, "LABEL")) {
148 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
150 opt->option->name = talloc_strdup(opt, value);
151 opt->option->id = talloc_asprintf(opt, "%s@%p",
152 ctx->dc->device->device->id, opt);
154 opt->option->is_default = parser_info->default_name &&
155 streq(parser_info->default_name, value);
157 parser_info->opt = opt;
161 /* all other parameters need an option */
165 if (streq(name, "KERNEL")) {
166 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
167 opt->boot_image = create_url_resource(opt, url);
169 char* args_sigfile_default = talloc_asprintf(opt,
170 "%s.cmdline.sig", value);
171 url = pxe_url_join(ctx->dc, ctx->dc->conf_url,
172 args_sigfile_default);
173 opt->args_sig_file = create_url_resource(opt, url);
174 talloc_free(args_sigfile_default);
176 } else if (streq(name, "INITRD")) {
177 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
178 opt->initrd = create_url_resource(opt, url);
180 } else if (streq(name, "APPEND")) {
183 pxe_append_string(opt, value);
185 str = strcasestr(value, "INITRD=");
187 str += strlen("INITRD=");
188 end = strchrnul(str, ' ');
191 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
192 opt->initrd = create_url_resource(opt, url);
194 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
198 type = strtoul(value, &end, 10);
199 if (end != value && !(*end))
200 pxe_process_sysappend(ctx->dc, opt, type);
202 } else if (streq(name, "DTB") || streq(name, "FDT")) {
203 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
204 opt->dtb = create_url_resource(opt, url);
210 * Callback for asynchronous loads from pxe_parse()
211 * @param result Result of load_url_async()
212 * @param data Pointer to associated conf_context
214 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
216 struct conf_context *conf = data;
217 struct device_handler *handler;
218 struct boot_status status = {0};
225 if (!result || result->status != LOAD_OK) {
230 rc = read_file(conf, result->local, &buf, &len);
232 pb_log("Read failed during pxe callback for %s\n", result->local);
236 conf_parse_buf(conf, buf, len);
238 /* We may be called well after the original caller of iterate_parsers(),
239 * commit any new boot options ourselves */
240 handler = talloc_parent(conf);
241 device_handler_discover_context_commit(handler, conf->dc);
243 status.type = BOOT_STATUS_INFO;
245 * TRANSLATORS: the format specifier in this string in an IP address,
248 status.message = talloc_asprintf(conf, _("pxe: parsed config for %s"),
249 conf->dc->conf_url->host);
250 device_handler_boot_status(handler, &status);
254 if (result->cleanup_local)
255 unlink(result->local);
260 * Return a new conf_context and increment the talloc reference count on
261 * the discover_context struct.
262 * @param ctx Parent talloc context
263 * @param orig Original discover_context
264 * @return Pointer to new conf_context
266 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
268 struct pxe_parser_info *info;
269 struct conf_context *conf;
271 conf = talloc_zero(ctx, struct conf_context);
276 conf->get_pair = conf_get_pair_space;
277 conf->process_pair = pxe_process_pair;
278 conf->finish = pxe_finish;
279 info = talloc_zero(conf, struct pxe_parser_info);
284 conf->parser_info = info;
287 * The discover_context may be freed once pxe_parse() returns, but the
288 * callback will still need it. Take a reference so that that it will
289 * persist until the last callback completes.
291 conf->dc = talloc_reference(conf, dc);
296 static int pxe_parse(struct discover_context *dc)
298 struct pb_url *pxe_base_url, *url;
299 char **pxe_conf_files, **filename;
300 struct conf_context *conf = NULL;
301 struct load_url_result *result;
302 void *ctx = talloc_parent(dc);
305 /* Expects dhcp event parameters to support network boot */
309 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
314 * Retrieving PXE configs over the network can take some time depending
315 * on factors such as slow network, malformed paths, bad DNS, and
316 * overzealous firewalls. Instead of blocking the discover server while
317 * we wait for these, spawn an asynchronous job for each URL we can
318 * parse and process the resulting files in a callback. A separate
319 * conf_context is created for each job.
321 conf = copy_context(ctx, dc);
326 /* we have a complete URL; use this and we're done. */
327 result = load_url_async(conf->dc, conf->dc->conf_url,
328 pxe_conf_parse_cb, conf);
330 pb_log("load_url_async fails for %s\n",
335 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
339 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
343 for (filename = pxe_conf_files; *filename; filename++) {
345 conf = copy_context(ctx, dc);
347 url = pb_url_join(conf->dc, pxe_base_url, *filename);
350 result = load_url_async(conf, url, pxe_conf_parse_cb,
353 pb_log("load_url_async fails for %s\n",
354 conf->dc->conf_url->path);
357 /* conf now needed by callback, don't reuse */
361 talloc_free(pxe_base_url);
362 talloc_free(pxe_conf_files);
368 talloc_free(pxe_conf_files);
374 static struct parser pxe_parser = {
379 register_parser(pxe_parser);