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