2 #if defined(HAVE_CONFIG_H)
7 #include <talloc/talloc.h>
11 #include "parser-conf.h"
12 #include "parser-utils.h"
15 #include "user-event.h"
17 static const char *pxelinux_prefix = "pxelinux.cfg/";
19 struct pxe_parser_info {
20 struct discover_boot_option *opt;
21 const char *default_name;
24 static void pxe_finish(struct conf_context *conf)
26 struct pxe_parser_info *info = conf->parser_info;
28 discover_context_add_boot_option(conf->dc, info->opt);
31 /* We need a slightly modified version of pb_url_join, to allow for the
32 * pxelinux "::filename" syntax for absolute URLs
34 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
37 struct pb_url *new_url;
42 if (len > 2 && s[0] == ':' && s[1] == ':') {
46 /* ::/path -> /path */
47 tmp = talloc_strdup(ctx, s+2);
50 tmp = talloc_strdup(ctx, s+1);
54 new_url = pb_url_join(ctx, url, tmp);
60 /* strip leading slashes */
61 for (tmp = s; *tmp == '/'; tmp++);
63 new_url = pb_url_join(ctx, url, tmp);
69 static void pxe_process_pair(struct conf_context *ctx,
70 const char *name, char *value)
72 struct pxe_parser_info *parser_info = ctx->parser_info;
73 struct discover_boot_option *opt = parser_info->opt;
76 /* quirk in the syslinux config format: initrd can be separated
78 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
80 value += strlen("initrd=");
86 if (streq(name, "DEFAULT")) {
87 parser_info->default_name = talloc_strdup(parser_info, value);
91 if (streq(name, "LABEL")) {
95 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
97 opt->option->name = talloc_strdup(opt, value);
98 opt->option->id = talloc_asprintf(opt, "%s@%p",
99 ctx->dc->device->device->id, opt);
101 opt->option->is_default = parser_info->default_name &&
102 streq(parser_info->default_name, value);
104 parser_info->opt = opt;
108 /* all other parameters need an option */
112 if (streq(name, "KERNEL")) {
113 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
114 opt->boot_image = create_url_resource(opt, url);
116 } else if (streq(name, "INITRD")) {
117 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
118 opt->initrd = create_url_resource(opt, url);
120 } else if (streq(name, "APPEND")) {
123 opt->option->boot_args = talloc_strdup(opt->option, value);
125 str = strcasestr(value, "INITRD=");
127 str += strlen("INITRD=");
128 end = strchrnul(str, ' ');
131 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
132 opt->initrd = create_url_resource(opt, url);
138 static int pxe_parse(struct discover_context *dc)
140 struct pb_url *pxe_base_url, *url;
141 struct pxe_parser_info *parser_info;
142 char **pxe_conf_files, **filename;
143 struct conf_context *conf;
148 /* Expects dhcp event parameters to support network boot */
152 conf = talloc_zero(dc, struct conf_context);
158 conf->get_pair = conf_get_pair_space;
159 conf->process_pair = pxe_process_pair;
160 conf->finish = pxe_finish;
162 parser_info = talloc_zero(conf, struct pxe_parser_info);
163 conf->parser_info = parser_info;
165 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
170 /* we have a complete URL; use this and we're done. */
171 rc = parser_request_url(dc, dc->conf_url, &buf, &len);
175 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
181 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
185 for (filename = pxe_conf_files; *filename; filename++) {
186 url = pb_url_join(dc, pxe_base_url, *filename);
190 rc = parser_request_url(dc, url, &buf, &len);
191 if (!rc) /* found one, just break */
197 talloc_free(pxe_base_url);
199 /* No configuration file found on the boot server */
203 talloc_free(pxe_conf_files);
206 /* Call the config file parser with the data read from the file */
207 conf_parse_buf(conf, buf, len);
215 talloc_free(pxe_conf_files);
222 static struct parser pxe_parser = {
227 register_parser(pxe_parser);