]> git.ozlabs.org Git - petitboot/blob - discover/kboot-parser.c
discover: Enable 'url' pb-events
[petitboot] / discover / kboot-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
9 #include "log/log.h"
10 #include "talloc/talloc.h"
11 #include "types/types.h"
12 #include "parser-conf.h"
13 #include "parser-utils.h"
14 #include "resource.h"
15
16 static void kboot_process_pair(struct conf_context *conf, const char *name,
17                 char *value)
18 {
19         const char *const *ignored_names = conf->parser_info;
20         struct discover_boot_option *d_opt;
21         struct boot_option *opt;
22         char *pos;
23         char *args;
24         const char *initrd;
25         const char *root;
26         const char *dtb;
27
28         /* ignore bare values */
29
30         if (!name)
31                 return;
32
33         if (conf_param_in_list(ignored_names, name))
34                 return;
35
36         if (conf_set_global_option(conf, name, value))
37                 return;
38
39         /* opt must be associated with dc */
40
41         d_opt = talloc_zero(conf->dc, struct discover_boot_option);
42         d_opt->device = conf->dc->device;
43         opt = talloc_zero(d_opt, struct boot_option);
44
45         if (!opt)
46                 return;
47
48         opt->id = talloc_asprintf(opt, "%s#%s", conf->dc->device->device->id,
49                         name);
50         opt->name = talloc_strdup(opt, name);
51         d_opt->option = opt;
52
53         args = talloc_strdup(opt, "");
54         initrd = conf_get_global_option(conf, "initrd");
55         root = conf_get_global_option(conf, "root");
56         dtb = conf_get_global_option(conf, "dtb");
57
58         pos = strchr(value, ' ');
59
60         /* if there's no space, it's only a kernel image with no params */
61
62         if (!pos)
63                 goto out_add;
64         *pos = 0;
65
66         for (pos++; pos;) {
67                 char *cl_name, *cl_value;
68
69                 pos = conf_get_pair_equal(conf, pos, &cl_name, &cl_value, ' ');
70
71                 if (!cl_name) {
72                         args = talloc_asprintf_append(args, "%s ", cl_value);
73                         continue;
74                 }
75
76                 if (streq(cl_name, "initrd")) {
77                         initrd = cl_value;
78                         continue;
79                 }
80
81                 if (streq(cl_name, "root")) {
82                         root = cl_value;
83                         continue;
84                 }
85
86                 if (streq(cl_name, "dtb")) {
87                         dtb = cl_value;
88                         continue;
89                 }
90
91                 args = talloc_asprintf_append(args, "%s=%s ", cl_name,
92                         cl_value);
93         }
94
95 out_add:
96         d_opt->boot_image = create_devpath_resource(d_opt,
97                                 conf->dc->device, value);
98
99         if (root) {
100                 opt->boot_args = talloc_asprintf(opt, "root=%s %s", root, args);
101                 talloc_free(args);
102         } else
103                 opt->boot_args = args;
104
105         opt->description = talloc_asprintf(opt, "%s %s", value,
106                         opt->boot_args);
107
108         if (initrd) {
109                 d_opt->initrd = create_devpath_resource(d_opt,
110                                 conf->dc->device, initrd);
111                 opt->description = talloc_asprintf_append(opt->description,
112                                 " initrd=%s", initrd);
113         }
114
115         if (dtb) {
116                 d_opt->dtb = create_devpath_resource(d_opt,
117                                 conf->dc->device, dtb);
118                 opt->description = talloc_asprintf_append(opt->description,
119                                 " dtb=%s", dtb);
120         }
121
122         conf_strip_str(opt->boot_args);
123         conf_strip_str(opt->description);
124
125         discover_context_add_boot_option(conf->dc, d_opt);
126 }
127
128 static struct conf_global_option kboot_global_options[] = {
129         { .name = "dtb" },
130         { .name = "initrd" },
131         { .name = "root" },
132         { .name = "video" },
133         { .name = NULL }
134 };
135
136 static const char *const kboot_conf_files[] = {
137         "/kboot.conf",
138         "/kboot.cnf",
139         "/etc/kboot.conf",
140         "/etc/kboot.cnf",
141         "/KBOOT.CONF",
142         "/KBOOT.CNF",
143         "/ETC/KBOOT.CONF",
144         "/ETC/KBOOT.CNF",
145         NULL
146 };
147
148 static const char *const kboot_ignored_names[] = {
149         "default",
150         "delay",
151         "message",
152         "timeout",
153         NULL
154 };
155
156 static int kboot_parse(struct discover_context *dc)
157 {
158         struct conf_context *conf;
159         const char * const *filename;
160         char *buf;
161         int len, rc;
162
163         /* Support block device boot only at present */
164         if (dc->event)
165                 return -1;
166
167         conf = talloc_zero(dc, struct conf_context);
168
169         if (!conf)
170                 return -1;
171
172         conf->dc = dc;
173         conf->global_options = kboot_global_options,
174         conf_init_global_options(conf);
175         conf->get_pair = conf_get_pair_equal;
176         conf->process_pair = kboot_process_pair;
177         conf->parser_info = (void *)kboot_ignored_names;
178
179         for (filename = kboot_conf_files; *filename; filename++) {
180                 rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
181                 if (rc)
182                         continue;
183
184                 conf_parse_buf(conf, buf, len);
185                 talloc_free(buf);
186         }
187
188         talloc_free(conf);
189         return 0;
190 }
191
192 static struct parser kboot_parser = {
193         .name                   = "kboot",
194         .parse                  = kboot_parse,
195         .resolve_resource       = resolve_devpath_resource,
196 };
197
198 register_parser(kboot_parser);