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