2 #if defined(HAVE_CONFIG_H)
8 #include <talloc/talloc.h>
13 #include "parser-conf.h"
14 #include "parser-utils.h"
18 #include "user-event.h"
21 static const char *pxelinux_prefix = "pxelinux.cfg/";
23 struct pxe_parser_info {
24 struct discover_boot_option *opt;
25 const char *default_name;
28 static void pxe_finish(struct conf_context *conf)
30 struct pxe_parser_info *info = conf->parser_info;
32 discover_context_add_boot_option(conf->dc, info->opt);
35 /* We need a slightly modified version of pb_url_join, to allow for the
36 * pxelinux "::filename" syntax for absolute URLs
38 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
41 struct pb_url *new_url;
46 if (len > 2 && s[0] == ':' && s[1] == ':') {
50 /* ::/path -> /path */
51 tmp = talloc_strdup(ctx, s+2);
54 tmp = talloc_strdup(ctx, s+1);
58 new_url = pb_url_join(ctx, url, tmp);
64 /* strip leading slashes */
65 for (tmp = s; *tmp == '/'; tmp++)
67 new_url = pb_url_join(ctx, url, tmp);
73 static void pxe_append_string(struct discover_boot_option *opt,
76 if (opt->option->boot_args)
77 opt->option->boot_args = talloc_asprintf_append(
78 opt->option->boot_args, " %s", str);
80 opt->option->boot_args = talloc_strdup(opt->option, str);
83 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
88 return talloc_asprintf(ctx,
89 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
90 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
93 static void pxe_process_sysappend(struct discover_context *ctx,
94 struct discover_boot_option *opt,
97 struct event *event = ctx->event;
104 uint8_t *mac = find_mac_by_name(ctx, ctx->network,
106 str = pxe_sysappend_mac(ctx, mac);
108 pxe_append_string(opt, str);
115 pb_log("pxe: unsupported features requested in "
116 "ipappend/sysappend: 0x%04lx", val);
120 static void pxe_process_pair(struct conf_context *ctx,
121 const char *name, char *value)
123 struct pxe_parser_info *parser_info = ctx->parser_info;
124 struct discover_boot_option *opt = parser_info->opt;
127 /* quirk in the syslinux config format: initrd can be separated
129 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
131 value += strlen("initrd=");
137 if (streq(name, "DEFAULT")) {
138 parser_info->default_name = talloc_strdup(parser_info, value);
142 if (streq(name, "LABEL")) {
146 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
148 opt->option->name = talloc_strdup(opt, value);
149 opt->option->id = talloc_asprintf(opt, "%s@%p",
150 ctx->dc->device->device->id, opt);
152 opt->option->is_default = parser_info->default_name &&
153 streq(parser_info->default_name, value);
155 parser_info->opt = opt;
159 /* all other parameters need an option */
163 if (streq(name, "KERNEL")) {
164 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
165 opt->boot_image = create_url_resource(opt, url);
167 } else if (streq(name, "INITRD")) {
168 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
169 opt->initrd = create_url_resource(opt, url);
171 } else if (streq(name, "APPEND")) {
174 pxe_append_string(opt, value);
176 str = strcasestr(value, "INITRD=");
178 str += strlen("INITRD=");
179 end = strchrnul(str, ' ');
182 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
183 opt->initrd = create_url_resource(opt, url);
185 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
189 type = strtoul(value, &end, 10);
190 if (end != value && !(*end))
191 pxe_process_sysappend(ctx->dc, opt, type);
193 } else if (streq(name, "DTB") || streq(name, "FDT")) {
194 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
195 opt->dtb = create_url_resource(opt, url);
200 static int pxe_parse(struct discover_context *dc)
202 struct pb_url *pxe_base_url, *url;
203 struct pxe_parser_info *parser_info;
204 char **pxe_conf_files, **filename;
205 struct conf_context *conf;
210 /* Expects dhcp event parameters to support network boot */
214 conf = talloc_zero(dc, struct conf_context);
220 conf->get_pair = conf_get_pair_space;
221 conf->process_pair = pxe_process_pair;
222 conf->finish = pxe_finish;
224 parser_info = talloc_zero(conf, struct pxe_parser_info);
225 conf->parser_info = parser_info;
227 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
232 /* we have a complete URL; use this and we're done. */
233 rc = parser_request_url(dc, dc->conf_url, &buf, &len);
237 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
243 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
247 for (filename = pxe_conf_files; *filename; filename++) {
248 url = pb_url_join(dc, pxe_base_url, *filename);
252 rc = parser_request_url(dc, url, &buf, &len);
253 if (!rc) /* found one, just break */
259 talloc_free(pxe_base_url);
261 /* No configuration file found on the boot server */
265 talloc_free(pxe_conf_files);
268 /* Call the config file parser with the data read from the file */
269 conf_parse_buf(conf, buf, len);
277 talloc_free(pxe_conf_files);
284 static struct parser pxe_parser = {
289 register_parser(pxe_parser);