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 } else if (streq(name, "INITRD")) {
170 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
171 opt->initrd = create_url_resource(opt, url);
173 } else if (streq(name, "APPEND")) {
176 pxe_append_string(opt, value);
178 str = strcasestr(value, "INITRD=");
180 str += strlen("INITRD=");
181 end = strchrnul(str, ' ');
184 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
185 opt->initrd = create_url_resource(opt, url);
187 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
191 type = strtoul(value, &end, 10);
192 if (end != value && !(*end))
193 pxe_process_sysappend(ctx->dc, opt, type);
195 } else if (streq(name, "DTB") || streq(name, "FDT")) {
196 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
197 opt->dtb = create_url_resource(opt, url);
203 * Callback for asynchronous loads from pxe_parse()
204 * @param result Result of load_url_async()
205 * @param data Pointer to associated conf_context
207 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
209 struct conf_context *conf = data;
210 struct device_handler *handler;
211 struct boot_status status = {0};
218 if (!result || result->status != LOAD_OK) {
223 rc = read_file(conf, result->local, &buf, &len);
225 pb_log("Read failed during pxe callback for %s\n", result->local);
229 conf_parse_buf(conf, buf, len);
231 /* We may be called well after the original caller of iterate_parsers(),
232 * commit any new boot options ourselves */
233 handler = talloc_parent(conf);
234 device_handler_discover_context_commit(handler, conf->dc);
236 status.type = BOOT_STATUS_INFO;
238 * TRANSLATORS: the format specifier in this string in an IP address,
241 status.message = talloc_asprintf(conf, _("pxe: parsed config for %s"),
242 conf->dc->conf_url->host);
243 device_handler_boot_status(handler, &status);
247 if (result->cleanup_local)
248 unlink(result->local);
253 * Return a new conf_context and increment the talloc reference count on
254 * the discover_context struct.
255 * @param ctx Parent talloc context
256 * @param orig Original discover_context
257 * @return Pointer to new conf_context
259 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
261 struct pxe_parser_info *info;
262 struct conf_context *conf;
264 conf = talloc_zero(ctx, struct conf_context);
269 conf->get_pair = conf_get_pair_space;
270 conf->process_pair = pxe_process_pair;
271 conf->finish = pxe_finish;
272 info = talloc_zero(conf, struct pxe_parser_info);
277 conf->parser_info = info;
280 * The discover_context may be freed once pxe_parse() returns, but the
281 * callback will still need it. Take a reference so that that it will
282 * persist until the last callback completes.
284 conf->dc = talloc_reference(conf, dc);
289 static int pxe_parse(struct discover_context *dc)
291 struct pb_url *pxe_base_url, *url;
292 char **pxe_conf_files, **filename;
293 struct conf_context *conf = NULL;
294 struct load_url_result *result;
295 void *ctx = talloc_parent(dc);
298 /* Expects dhcp event parameters to support network boot */
302 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
307 * Retrieving PXE configs over the network can take some time depending
308 * on factors such as slow network, malformed paths, bad DNS, and
309 * overzealous firewalls. Instead of blocking the discover server while
310 * we wait for these, spawn an asynchronous job for each URL we can
311 * parse and process the resulting files in a callback. A separate
312 * conf_context is created for each job.
314 conf = copy_context(ctx, dc);
319 /* we have a complete URL; use this and we're done. */
320 result = load_url_async(conf->dc, conf->dc->conf_url,
321 pxe_conf_parse_cb, conf);
323 pb_log("load_url_async fails for %s\n",
328 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
332 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
336 for (filename = pxe_conf_files; *filename; filename++) {
338 conf = copy_context(ctx, dc);
340 url = pb_url_join(conf->dc, pxe_base_url, *filename);
343 result = load_url_async(conf, url, pxe_conf_parse_cb,
346 pb_log("load_url_async fails for %s\n",
347 conf->dc->conf_url->path);
350 /* conf now needed by callback, don't reuse */
354 talloc_free(pxe_base_url);
355 talloc_free(pxe_conf_files);
361 talloc_free(pxe_conf_files);
367 static struct parser pxe_parser = {
372 register_parser(pxe_parser);