]> git.ozlabs.org Git - petitboot/blob - discover/yaboot-parser.c
discover/yaboot: fix device parsing
[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         struct boot_option *opt;
91
92         assert(state->opt);
93
94         opt = state->opt->option;
95         assert(opt);
96         assert(opt->name);
97         assert(opt->boot_args);
98
99         /* populate the boot option from state data */
100         state->opt->boot_image = create_yaboot_devpath_resource(state,
101                                 conf, state->boot_image);
102         if (state->initrd) {
103                 state->opt->initrd = create_yaboot_devpath_resource(state,
104                                 conf, state->initrd);
105         }
106
107         if (state->initrd_size) {
108                 opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
109                                         state->initrd_size, opt->boot_args);
110         }
111
112         if (state->ramdisk) {
113                 opt->boot_args = talloc_asprintf(opt, "ramdisk=%s %s",
114                                         state->initrd_size, opt->boot_args);
115         }
116
117         if (state->root) {
118                 opt->boot_args = talloc_asprintf(opt, "root=%s %s",
119                                         state->root, opt->boot_args);
120         }
121
122         if (state->read_only && state->read_write) {
123                 pb_log("boot option %s specified both 'ro' and 'rw', "
124                                 "using 'rw'\n", opt->name);
125                 state->read_only = false;
126         }
127
128         if (state->read_only || state->read_write) {
129                 opt->boot_args = talloc_asprintf(opt, "%s %s",
130                                         state->read_only ? "ro" : "rw",
131                                         opt->boot_args);
132         }
133
134         if (state->literal) {
135                 opt->boot_args = talloc_strdup(opt, state->literal);
136         }
137
138         opt->description = talloc_asprintf(opt, "%s %s %s",
139                 state->boot_image,
140                 (state->initrd ? state->initrd : ""),
141                 opt->boot_args ? opt->boot_args : "");
142
143         conf_strip_str(opt->boot_args);
144         conf_strip_str(opt->description);
145
146         discover_context_add_boot_option(conf->dc, state->opt);
147 }
148
149 static void yaboot_process_pair(struct conf_context *conf, const char *name,
150                 char *value)
151 {
152         struct yaboot_state *state = conf->parser_info;
153         struct discover_boot_option *opt = state->opt;
154         struct fixed_pair {
155                 const char *image;
156                 const char *initrd;
157         };
158         static const struct fixed_pair suse_fp32 = {
159                 .image = "/suseboot/vmlinux32",
160                 .initrd = "/suseboot/initrd32",
161         };
162         static const struct fixed_pair suse_fp64 = {
163                 .image = "/suseboot/vmlinux64",
164                 .initrd = "/suseboot/initrd64",
165         };
166         const struct fixed_pair *suse_fp;
167
168         /* fixup for bare values */
169
170         if (!name)
171                 name = value;
172
173         if (!state->globals_done && conf_set_global_option(conf, name, value))
174                 return;
175
176         if (!conf_param_in_list(state->known_names, name))
177                 return;
178
179         state->globals_done = 1;
180
181         /* image */
182
183         if (streq(name, "image")) {
184
185                 /* First finish any previous image. */
186                 if (opt)
187                         yaboot_finish(conf);
188
189                 /* Then start the new image. */
190                 opt = state_start_new_option(conf, state);
191
192                 state->boot_image = talloc_strdup(state, value);
193
194                 return;
195         }
196
197         /* Special processing for SUSE install CD. */
198
199         if (streq(name, "image[32bit]"))
200                 suse_fp = &suse_fp32;
201         else if (streq(name, "image[64bit]"))
202                 suse_fp = &suse_fp64;
203         else
204                 suse_fp = NULL;
205
206         if (suse_fp) {
207                 /* First finish any previous image. */
208                 if (opt)
209                         yaboot_finish(conf);
210
211                 /* Then start the new image. */
212                 opt = state_start_new_option(conf, state);
213
214                 if (*value == '/') {
215                         state->boot_image = talloc_strdup(state, value);
216                 } else {
217                         state->boot_image = talloc_strdup(state,
218                                                         suse_fp->image);
219                         state->initrd = talloc_strdup(state, suse_fp->initrd);
220                 }
221
222                 return;
223         }
224
225         /* all other processing requires an image */
226         if (!opt) {
227                 pb_log("%s: unknown name: %s\n", __func__, name);
228                 return;
229         }
230
231         /* initrd */
232         if (streq(name, "initrd")) {
233                 state->initrd = talloc_strdup(state, value);
234                 return;
235         }
236
237         /* label */
238         if (streq(name, "label")) {
239                 opt->option->id = talloc_asprintf(opt->option, "%s#%s",
240                         conf->dc->device->device->id, value);
241                 opt->option->name = talloc_strdup(opt->option, value);
242                 return;
243         }
244
245         /* args */
246         if (streq(name, "device")) {
247                 printf("option device : %s", value);
248                 state->device = talloc_strdup(state, value);
249                 return;
250         }
251
252         if (streq(name, "parititon")) {
253                 state->partition = talloc_strdup(state, value);
254                 return;
255         }
256
257         if (streq(name, "append")) {
258                 opt->option->boot_args = talloc_asprintf_append(
259                         opt->option->boot_args, "%s ", value);
260                 return;
261         }
262
263         if (streq(name, "initrd-size")) {
264                 state->initrd_size = talloc_strdup(state, value);
265                 return;
266         }
267
268         if (streq(name, "literal")) {
269                 state->literal = talloc_strdup(state, value);
270                 return;
271         }
272
273         if (streq(name, "ramdisk")) {
274                 state->ramdisk = talloc_strdup(state, value);
275                 return;
276         }
277
278         if (streq(name, "read-only")) {
279                 state->read_only = true;
280                 return;
281         }
282
283         if (streq(name, "read-write")) {
284                 state->read_write = true;
285                 return;
286         }
287
288         if (streq(name, "root")) {
289                 state->root = talloc_strdup(state, value);
290                 return;
291         }
292
293         pb_log("%s: unknown name: %s\n", __func__, name);
294 }
295
296 static struct conf_global_option yaboot_global_options[] = {
297         { .name = "root" },
298         { .name = "device" },
299         { .name = "partition" },
300         { .name = "initrd" },
301         { .name = "initrd_size" },
302         { .name = "video" },
303         { .name = "literal" },
304         { .name = "ramdisk" },
305         { .name = NULL },
306 };
307
308 static const char *const yaboot_conf_files[] = {
309         "/yaboot.conf",
310         "/yaboot.cnf",
311         "/etc/yaboot.conf",
312         "/etc/yaboot.cnf",
313         "/suseboot/yaboot.cnf",
314         "/YABOOT.CONF",
315         "/YABOOT.CNF",
316         "/ETC/YABOOT.CONF",
317         "/ETC/YABOOT.CNF",
318         "/SUSEBOOT/YABOOT.CNF",
319         NULL
320 };
321
322 static const char *yaboot_known_names[] = {
323         "append",
324         "image",
325         "image[64bit]", /* SUSE extension */
326         "image[32bit]", /* SUSE extension */
327         "initrd",
328         "initrd-size",
329         "label",
330         "literal",
331         "ramdisk",
332         "read-only",
333         "read-write",
334         "root",
335         "device",
336         "partition",
337         NULL
338 };
339
340 static int yaboot_parse(struct discover_context *dc, char *buf, int len)
341 {
342         struct conf_context *conf;
343         struct yaboot_state *state;
344
345         conf = talloc_zero(dc, struct conf_context);
346
347         if (!conf)
348                 return 0;
349
350         conf->dc = dc;
351         conf->global_options = yaboot_global_options,
352         conf_init_global_options(conf);
353         conf->get_pair = conf_get_pair_equal;
354         conf->process_pair = yaboot_process_pair;
355         conf->finish = yaboot_finish;
356         conf->parser_info = state = talloc_zero(conf, struct yaboot_state);
357
358         state->known_names = yaboot_known_names;
359
360         state->opt = NULL;
361
362         conf_parse_buf(conf, buf, len);
363
364         talloc_free(conf);
365         return 1;
366 }
367
368 static struct parser yaboot_parser = {
369         .name                   = "yaboot",
370         .method                 = CONF_METHOD_LOCAL_FILE,
371         .parse                  = yaboot_parse,
372         .filenames              = yaboot_conf_files,
373         .resolve_resource       = resolve_devpath_resource,
374 };
375
376 register_parser(yaboot_parser);