]> git.ozlabs.org Git - petitboot/blob - discover/pxe-parser.c
4812c374d1991b74ba938e9fd935154982a6b3d3
[petitboot] / discover / pxe-parser.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <talloc/talloc.h>
9 #include <url/url.h>
10 #include <log/log.h>
11
12 #include "parser.h"
13 #include "parser-conf.h"
14 #include "parser-utils.h"
15 #include "resource.h"
16 #include "paths.h"
17 #include "event.h"
18 #include "user-event.h"
19 #include "network.h"
20
21 static const char *pxelinux_prefix = "pxelinux.cfg/";
22
23 struct pxe_parser_info {
24         struct discover_boot_option *opt;
25         const char *default_name;
26 };
27
28 static void pxe_finish(struct conf_context *conf)
29 {
30         struct pxe_parser_info *info = conf->parser_info;
31         if (info->opt)
32                 discover_context_add_boot_option(conf->dc, info->opt);
33 }
34
35 /* We need a slightly modified version of pb_url_join, to allow for the
36  * pxelinux "::filename" syntax for absolute URLs
37  */
38 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
39                 const char *s)
40 {
41         struct pb_url *new_url;
42         int len;
43
44         len = strlen(s);
45
46         if (len > 2 && s[0] == ':' && s[1] == ':') {
47                 char *tmp;
48
49                 if (s[2] == '/') {
50                         /* ::/path -> /path */
51                         tmp = talloc_strdup(ctx, s+2);
52                 } else {
53                         /* ::path -> /path */
54                         tmp = talloc_strdup(ctx, s+1);
55                         tmp[0] = '/';
56                 }
57
58                 new_url = pb_url_join(ctx, url, tmp);
59
60                 talloc_free(tmp);
61
62         } else {
63                 const char *tmp;
64                 /* strip leading slashes */
65                 for (tmp = s; *tmp == '/'; tmp++)
66                         ;
67                 new_url = pb_url_join(ctx, url, tmp);
68         }
69
70         return new_url;
71 }
72
73 static void pxe_append_string(struct discover_boot_option *opt,
74                 const char *str)
75 {
76         if (opt->option->boot_args)
77                 opt->option->boot_args = talloc_asprintf_append(
78                                 opt->option->boot_args, " %s", str);
79         else
80                 opt->option->boot_args = talloc_strdup(opt->option, str);
81 }
82
83 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
84 {
85         if (!mac)
86                 return NULL;
87
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]);
91 }
92
93 static void pxe_process_sysappend(struct discover_context *ctx,
94                 struct discover_boot_option *opt,
95                 unsigned long val)
96 {
97         struct event *event = ctx->event;
98         char *str = NULL;
99
100         if (!event)
101                 return;
102
103         if (val & 0x2) {
104                 uint8_t *mac = find_mac_by_name(ctx, ctx->network,
105                                         event->device);
106                 str = pxe_sysappend_mac(ctx, mac);
107                 if (str) {
108                         pxe_append_string(opt, str);
109                         talloc_free(str);
110                 }
111                 val &= ~0x2;
112         }
113
114         if (val)
115                 pb_log("pxe: unsupported features requested in "
116                                 "ipappend/sysappend: 0x%04lx", val);
117
118 }
119
120 static void pxe_process_pair(struct conf_context *ctx,
121                 const char *name, char *value)
122 {
123         struct pxe_parser_info *parser_info = ctx->parser_info;
124         struct discover_boot_option *opt = parser_info->opt;
125         struct pb_url *url;
126
127         /* quirk in the syslinux config format: initrd can be separated
128          * by an '=' */
129         if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
130                 name = "initrd";
131                 value += strlen("initrd=");
132         }
133
134         if (!name)
135                 return;
136
137         if (streq(name, "DEFAULT")) {
138                 parser_info->default_name = talloc_strdup(parser_info, value);
139                 return;
140         }
141
142         if (streq(name, "LABEL")) {
143                 if (opt)
144                         pxe_finish(ctx);
145
146                 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
147
148                 opt->option->name = talloc_strdup(opt, value);
149                 opt->option->id = talloc_asprintf(opt, "%s@%p",
150                                 ctx->dc->device->device->id, opt);
151
152                 opt->option->is_default = parser_info->default_name &&
153                                         streq(parser_info->default_name, value);
154
155                 parser_info->opt = opt;
156                 return;
157         }
158
159         /* all other parameters need an option */
160         if (!opt)
161                 return;
162
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);
166
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);
170
171         } else if (streq(name, "APPEND")) {
172                 char *str, *end;
173
174                 pxe_append_string(opt, value);
175
176                 str = strcasestr(value, "INITRD=");
177                 if (str) {
178                         str += strlen("INITRD=");
179                         end = strchrnul(str, ' ');
180                         *end = '\0';
181
182                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
183                         opt->initrd = create_url_resource(opt, url);
184                 }
185         } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
186                 unsigned long type;
187                 char *end;
188
189                 type = strtoul(value, &end, 10);
190                 if (end != value && !(*end))
191                         pxe_process_sysappend(ctx->dc, opt, type);
192
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);
196         }
197
198 }
199
200 static int pxe_parse(struct discover_context *dc)
201 {
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;
206         bool complete_url;
207         int len, rc;
208         char *buf;
209
210         /* Expects dhcp event parameters to support network boot */
211         if (!dc->event)
212                 return -1;
213
214         conf = talloc_zero(dc, struct conf_context);
215
216         if (!conf)
217                 goto out;
218
219         conf->dc = dc;
220         conf->get_pair = conf_get_pair_space;
221         conf->process_pair = pxe_process_pair;
222         conf->finish = pxe_finish;
223
224         parser_info = talloc_zero(conf, struct pxe_parser_info);
225         conf->parser_info = parser_info;
226
227         dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
228         if (!dc->conf_url)
229                 goto out_conf;
230
231         if (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);
234                 if (rc)
235                         goto out_conf;
236         } else {
237                 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
238                 if (!pxe_conf_files)
239                         goto out_conf;
240
241                 rc = -1;
242
243                 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
244                 if (!pxe_base_url)
245                         goto out_pxe_conf;
246
247                 for (filename = pxe_conf_files; *filename; filename++) {
248                         url = pb_url_join(dc, pxe_base_url, *filename);
249                         if (!url)
250                                 continue;
251
252                         rc = parser_request_url(dc, url, &buf, &len);
253                         if (!rc) /* found one, just break */
254                                 break;
255
256                         talloc_free(url);
257                 }
258
259                 talloc_free(pxe_base_url);
260
261                 /* No configuration file found on the boot server */
262                 if (rc)
263                         goto out_pxe_conf;
264
265                 talloc_free(pxe_conf_files);
266         }
267
268         /* Call the config file parser with the data read from the file */
269         conf_parse_buf(conf, buf, len);
270
271         talloc_free(buf);
272         talloc_free(conf);
273
274         return 0;
275
276 out_pxe_conf:
277         talloc_free(pxe_conf_files);
278 out_conf:
279         talloc_free(conf);
280 out:
281         return -1;
282 }
283
284 static struct parser pxe_parser = {
285         .name                   = "pxe",
286         .parse                  = pxe_parse,
287 };
288
289 register_parser(pxe_parser);