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