discover/network: In static config mode, only configure defined interfaces
[petitboot] / discover / yaboot-parser.c
1 #define _GNU_SOURCE
2
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7
8 #include "log/log.h"
9 #include "talloc/talloc.h"
10 #include "types/types.h"
11 #include "parser-conf.h"
12 #include "parser-utils.h"
13 #include "resource.h"
14
15 struct yaboot_state {
16         int globals_done;
17         const char *const *known_names;
18
19         /* current option data */
20         struct discover_boot_option *opt;
21         const char *device;
22         const char *partition;
23         const char *boot_image;
24         const char *initrd;
25         const char *initrd_size;
26         const char *literal;
27         const char *ramdisk;
28         const char *root;
29         bool read_only;
30         bool read_write;
31 };
32
33 static struct discover_boot_option *state_start_new_option(
34                 struct conf_context *conf,
35                 struct yaboot_state *state)
36 {
37         state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
38         state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
39
40         /* old allocated values will get freed with the state */
41         state->device = conf_get_global_option(conf, "device");
42         state->partition = conf_get_global_option(conf, "partition");
43         state->initrd_size = conf_get_global_option(conf, "initrd_size");
44         state->literal = conf_get_global_option(conf, "literal");
45         state->ramdisk = conf_get_global_option(conf, "ramdisk");
46         state->root = conf_get_global_option(conf, "root");
47
48         return state->opt;
49 }
50
51 static struct resource *create_yaboot_devpath_resource(
52                 struct yaboot_state *state,
53                 struct conf_context *conf,
54                 const char *path)
55 {
56         struct discover_boot_option *opt = state->opt;
57         const char *dev, *part, *devpos;
58         struct resource *res;
59         char *devpath, *devstr;
60
61         dev = state->device;
62         part = state->partition;
63
64         if (!dev)
65                 dev = conf_get_global_option(conf, "device");
66         if (!part)
67                 part = conf_get_global_option(conf, "partition");
68
69         if (strchr(path, ':')) {
70                 devpath = talloc_strdup(conf, path);
71
72         } else if (dev && part) {
73                 devpos = &dev[strlen(dev) - 1];
74                 if (isdigit(*devpos)) {
75                         while (isdigit(*devpos))
76                                 devpos--;
77
78                         devstr = talloc_strndup(conf, dev, devpos - dev + 1);
79                         devpath = talloc_asprintf(conf, "%s%s:%s", devstr,
80                                         part, path);
81                         talloc_free(devstr);
82                 } else {
83                         devpath = talloc_asprintf(conf,
84                                         "%s%s:%s", dev, part, path);
85                 }
86         } else if (dev) {
87                 devpath = talloc_asprintf(conf, "%s:%s", dev, path);
88         } else {
89                 devpath = talloc_strdup(conf, path);
90         }
91
92         res = create_devpath_resource(opt, conf->dc->device, devpath);
93
94         talloc_free(devpath);
95
96         return res;
97 }
98
99 static void yaboot_finish(struct conf_context *conf)
100 {
101         struct yaboot_state *state = conf->parser_info;
102         const char *default_label;
103         struct boot_option *opt;
104
105         assert(state->opt);
106
107         opt = state->opt->option;
108         assert(opt);
109         assert(opt->name);
110         assert(opt->boot_args);
111
112         /* populate the boot option from state data */
113         state->opt->boot_image = create_yaboot_devpath_resource(state,
114                                 conf, state->boot_image);
115         if (state->initrd) {
116                 state->opt->initrd = create_yaboot_devpath_resource(state,
117                                 conf, state->initrd);
118         }
119
120         if (state->initrd_size) {
121                 opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
122                                         state->initrd_size, opt->boot_args);
123         }
124
125         if (state->ramdisk) {
126                 opt->boot_args = talloc_asprintf(opt, "ramdisk=%s %s",
127                                         state->initrd_size, opt->boot_args);
128         }
129
130         if (state->root) {
131                 opt->boot_args = talloc_asprintf(opt, "root=%s %s",
132                                         state->root, opt->boot_args);
133         }
134
135         if (state->read_only && state->read_write) {
136                 pb_log("boot option %s specified both 'ro' and 'rw', "
137                                 "using 'rw'\n", opt->name);
138                 state->read_only = false;
139         }
140
141         if (state->read_only || state->read_write) {
142                 opt->boot_args = talloc_asprintf(opt, "%s %s",
143                                         state->read_only ? "ro" : "rw",
144                                         opt->boot_args);
145         }
146
147         if (state->literal) {
148                 opt->boot_args = talloc_strdup(opt, state->literal);
149         }
150
151         opt->description = talloc_asprintf(opt, "%s %s %s",
152                 state->boot_image,
153                 (state->initrd ? state->initrd : ""),
154                 opt->boot_args ? opt->boot_args : "");
155
156         conf_strip_str(opt->boot_args);
157         conf_strip_str(opt->description);
158
159         default_label = conf_get_global_option(conf, "default");
160         if (default_label &&
161                         !strcasecmp(state->opt->option->name, default_label))
162                 state->opt->option->is_default = true;
163
164         discover_context_add_boot_option(conf->dc, state->opt);
165 }
166
167 static void yaboot_process_pair(struct conf_context *conf, const char *name,
168                 char *value)
169 {
170         struct yaboot_state *state = conf->parser_info;
171         struct discover_boot_option *opt = state->opt;
172         struct fixed_pair {
173                 const char *image;
174                 const char *initrd;
175         };
176         static const struct fixed_pair suse_fp32 = {
177                 .image = "/suseboot/vmlinux32",
178                 .initrd = "/suseboot/initrd32",
179         };
180         static const struct fixed_pair suse_fp64 = {
181                 .image = "/suseboot/vmlinux64",
182                 .initrd = "/suseboot/initrd64",
183         };
184         const struct fixed_pair *suse_fp;
185
186         /* fixup for bare values */
187
188         if (!name)
189                 name = value;
190
191         if (!state->globals_done && conf_set_global_option(conf, name, value))
192                 return;
193
194         if (!conf_param_in_list(state->known_names, name))
195                 return;
196
197         state->globals_done = 1;
198
199         /* image */
200
201         if (streq(name, "image")) {
202
203                 /* First finish any previous image. */
204                 if (opt)
205                         yaboot_finish(conf);
206
207                 /* Then start the new image. */
208                 opt = state_start_new_option(conf, state);
209
210                 state->boot_image = talloc_strdup(state, value);
211
212                 return;
213         }
214
215         /* Special processing for SUSE install CD. */
216
217         if (streq(name, "image[32bit]"))
218                 suse_fp = &suse_fp32;
219         else if (streq(name, "image[64bit]"))
220                 suse_fp = &suse_fp64;
221         else
222                 suse_fp = NULL;
223
224         if (suse_fp) {
225                 /* First finish any previous image. */
226                 if (opt)
227                         yaboot_finish(conf);
228
229                 /* Then start the new image. */
230                 opt = state_start_new_option(conf, state);
231
232                 if (*value == '/') {
233                         state->boot_image = talloc_strdup(state, value);
234                 } else {
235                         state->boot_image = talloc_strdup(state,
236                                                         suse_fp->image);
237                         state->initrd = talloc_strdup(state, suse_fp->initrd);
238                 }
239
240                 return;
241         }
242
243         /* all other processing requires an image */
244         if (!opt) {
245                 pb_log("%s: unknown name: %s\n", __func__, name);
246                 return;
247         }
248
249         /* initrd */
250         if (streq(name, "initrd")) {
251                 state->initrd = talloc_strdup(state, value);
252                 return;
253         }
254
255         /* label */
256         if (streq(name, "label")) {
257                 opt->option->id = talloc_asprintf(opt->option, "%s#%s",
258                         conf->dc->device->device->id, value);
259                 opt->option->name = talloc_strdup(opt->option, value);
260                 return;
261         }
262
263         /* args */
264         if (streq(name, "device")) {
265                 printf("option device : %s", value);
266                 state->device = talloc_strdup(state, value);
267                 return;
268         }
269
270         if (streq(name, "parititon")) {
271                 state->partition = talloc_strdup(state, value);
272                 return;
273         }
274
275         if (streq(name, "append")) {
276                 opt->option->boot_args = talloc_asprintf_append(
277                         opt->option->boot_args, "%s ", value);
278                 return;
279         }
280
281         if (streq(name, "initrd-size")) {
282                 state->initrd_size = talloc_strdup(state, value);
283                 return;
284         }
285
286         if (streq(name, "literal")) {
287                 state->literal = talloc_strdup(state, value);
288                 return;
289         }
290
291         if (streq(name, "ramdisk")) {
292                 state->ramdisk = talloc_strdup(state, value);
293                 return;
294         }
295
296         if (streq(name, "read-only")) {
297                 state->read_only = true;
298                 return;
299         }
300
301         if (streq(name, "read-write")) {
302                 state->read_write = true;
303                 return;
304         }
305
306         if (streq(name, "root")) {
307                 state->root = talloc_strdup(state, value);
308                 return;
309         }
310
311         pb_log("%s: unknown name: %s\n", __func__, name);
312 }
313
314 static struct conf_global_option yaboot_global_options[] = {
315         { .name = "root" },
316         { .name = "device" },
317         { .name = "partition" },
318         { .name = "initrd" },
319         { .name = "initrd_size" },
320         { .name = "video" },
321         { .name = "literal" },
322         { .name = "ramdisk" },
323         { .name = "default" },
324         { .name = NULL },
325 };
326
327 static const char *const yaboot_conf_files[] = {
328         "/yaboot.conf",
329         "/yaboot.cnf",
330         "/etc/yaboot.conf",
331         "/etc/yaboot.cnf",
332         "/suseboot/yaboot.cnf",
333         "/YABOOT.CONF",
334         "/YABOOT.CNF",
335         "/ETC/YABOOT.CONF",
336         "/ETC/YABOOT.CNF",
337         "/SUSEBOOT/YABOOT.CNF",
338         NULL
339 };
340
341 static const char *yaboot_known_names[] = {
342         "append",
343         "image",
344         "image[64bit]", /* SUSE extension */
345         "image[32bit]", /* SUSE extension */
346         "initrd",
347         "initrd-size",
348         "label",
349         "literal",
350         "ramdisk",
351         "read-only",
352         "read-write",
353         "root",
354         "device",
355         "partition",
356         NULL
357 };
358
359 static int yaboot_parse(struct discover_context *dc)
360 {
361         const char * const *filename;
362         struct yaboot_state *state;
363         struct conf_context *conf;
364         int len, rc;
365         char *buf;
366
367         /* Support block device boot only at present */
368         if (dc->event)
369                 return -1;
370
371         conf = talloc_zero(dc, struct conf_context);
372
373         if (!conf)
374                 return -1;
375
376         conf->dc = dc;
377         conf->global_options = yaboot_global_options,
378         conf_init_global_options(conf);
379         conf->get_pair = conf_get_pair_equal;
380         conf->process_pair = yaboot_process_pair;
381         conf->finish = yaboot_finish;
382         conf->parser_info = state = talloc_zero(conf, struct yaboot_state);
383
384         state->known_names = yaboot_known_names;
385
386         state->opt = NULL;
387
388         for (filename = yaboot_conf_files; *filename; filename++) {
389                 rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
390                 if (rc)
391                         continue;
392
393                 conf_parse_buf(conf, buf, len);
394                 talloc_free(buf);
395         }
396
397         talloc_free(conf);
398         return 0;
399 }
400
401 static struct parser yaboot_parser = {
402         .name                   = "yaboot",
403         .parse                  = yaboot_parse,
404         .resolve_resource       = resolve_devpath_resource,
405 };
406
407 register_parser(yaboot_parser);