]> git.ozlabs.org Git - petitboot/blob - discover/pxe-parser.c
discover/pxe: check for a valid boot option before adding
[petitboot] / discover / pxe-parser.c
1
2 #define _GNU_SOURCE
3 #include <string.h>
4
5 #include <talloc/talloc.h>
6 #include <url/url.h>
7
8 #include "parser.h"
9 #include "parser-conf.h"
10 #include "parser-utils.h"
11 #include "resource.h"
12
13 static void pxe_finish(struct conf_context *conf)
14 {
15         if (conf->parser_info)
16                 discover_context_add_boot_option(conf->dc, conf->parser_info);
17 }
18
19 static void pxe_process_pair(struct conf_context *ctx,
20                 const char *name, char *value)
21 {
22         struct discover_boot_option *opt = ctx->parser_info;
23         struct pb_url *url;
24
25         /* quirk in the syslinux config format: initrd can be separated
26          * by an '=' */
27         if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
28                 name = "initrd";
29                 value += strlen("initrd=");
30         }
31
32         if (!name)
33                 return;
34
35         if (streq(name, "LABEL")) {
36                 if (opt)
37                         pxe_finish(ctx);
38
39                 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
40                 ctx->parser_info = opt;
41
42                 opt->option->name = talloc_strdup(opt, value);
43                 opt->option->id = talloc_asprintf(opt, "%s@%p",
44                                 ctx->dc->device->device->id, opt);
45                 return;
46         }
47
48         /* all other parameters need an option */
49         if (!opt)
50                 return;
51
52         if (streq(name, "KERNEL")) {
53                 url = pb_url_join(ctx->dc, ctx->dc->conf_url, value);
54                 opt->boot_image = create_url_resource(opt, url);
55
56         } else if (streq(name, "INITRD")) {
57                 url = pb_url_join(ctx->dc, ctx->dc->conf_url, value);
58                 opt->initrd = create_url_resource(opt, url);
59
60         } else if (streq(name, "APPEND")) {
61                 char *str, *end;
62
63                 opt->option->boot_args = talloc_strdup(opt->option, value);
64
65                 str = strcasestr(value, "INITRD=");
66                 if (str) {
67                         str += strlen("INITRD=");
68                         end = strchrnul(str, ' ');
69                         *end = '\0';
70
71                         url = pb_url_join(ctx->dc, ctx->dc->conf_url, str);
72                         opt->initrd = create_url_resource(opt, url);
73                 }
74         }
75
76 }
77
78 static int pxe_parse(struct discover_context *dc, char *buf, int len)
79 {
80         struct conf_context *conf;
81
82         conf = talloc_zero(dc, struct conf_context);
83
84         if (!conf)
85                 return 0;
86
87         conf->dc = dc;
88         conf->get_pair = conf_get_pair_space;
89         conf->process_pair = pxe_process_pair;
90         conf->finish = pxe_finish;
91
92         conf_parse_buf(conf, buf, len);
93
94         talloc_free(conf);
95         return 1;
96 }
97
98 static struct parser pxe_parser = {
99         .name                   = "pxe",
100         .parse                  = pxe_parse,
101         .method                 = CONF_METHOD_DHCP,
102 };
103
104 register_parser(pxe_parser);