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