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