]> git.ozlabs.org Git - petitboot/blob - discover/kboot-parser.c
f7f75e07ceae60212f3ccef5cea34d7ed3ef93aa
[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         char* args_sigfile_default = talloc_asprintf(d_opt,
100                 "%s.cmdline.sig", value);
101         d_opt->args_sig_file = create_devpath_resource(d_opt,
102                                 conf->dc->device, args_sigfile_default);
103         talloc_free(args_sigfile_default);
104
105         if (root) {
106                 opt->boot_args = talloc_asprintf(opt, "root=%s %s", root, args);
107                 talloc_free(args);
108         } else
109                 opt->boot_args = args;
110
111         opt->description = talloc_asprintf(opt, "%s %s", value,
112                         opt->boot_args);
113
114         if (initrd) {
115                 d_opt->initrd = create_devpath_resource(d_opt,
116                                 conf->dc->device, initrd);
117                 opt->description = talloc_asprintf_append(opt->description,
118                                 " initrd=%s", initrd);
119         }
120
121         if (dtb) {
122                 d_opt->dtb = create_devpath_resource(d_opt,
123                                 conf->dc->device, dtb);
124                 opt->description = talloc_asprintf_append(opt->description,
125                                 " dtb=%s", dtb);
126         }
127
128         conf_strip_str(opt->boot_args);
129         conf_strip_str(opt->description);
130
131         discover_context_add_boot_option(conf->dc, d_opt);
132 }
133
134 static struct conf_global_option kboot_global_options[] = {
135         { .name = "dtb" },
136         { .name = "initrd" },
137         { .name = "root" },
138         { .name = "video" },
139         { .name = NULL }
140 };
141
142 static const char *const kboot_conf_files[] = {
143         "/kboot.conf",
144         "/kboot.cnf",
145         "/etc/kboot.conf",
146         "/etc/kboot.cnf",
147         "/KBOOT.CONF",
148         "/KBOOT.CNF",
149         "/ETC/KBOOT.CONF",
150         "/ETC/KBOOT.CNF",
151         NULL
152 };
153
154 static const char *const kboot_ignored_names[] = {
155         "default",
156         "delay",
157         "message",
158         "timeout",
159         NULL
160 };
161
162 static int kboot_parse(struct discover_context *dc)
163 {
164         struct conf_context *conf;
165         const char * const *filename;
166         char *buf;
167         int len, rc;
168
169         /* Support block device boot only at present */
170         if (dc->event)
171                 return -1;
172
173         conf = talloc_zero(dc, struct conf_context);
174
175         if (!conf)
176                 return -1;
177
178         conf->dc = dc;
179         conf->global_options = kboot_global_options,
180         conf_init_global_options(conf);
181         conf->get_pair = conf_get_pair_equal;
182         conf->process_pair = kboot_process_pair;
183         conf->parser_info = (void *)kboot_ignored_names;
184
185         for (filename = kboot_conf_files; *filename; filename++) {
186                 rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
187                 if (rc)
188                         continue;
189
190                 conf_parse_buf(conf, buf, len);
191                 talloc_free(buf);
192         }
193
194         talloc_free(conf);
195         return 0;
196 }
197
198 static struct parser kboot_parser = {
199         .name                   = "kboot",
200         .parse                  = kboot_parse,
201         .resolve_resource       = resolve_devpath_resource,
202 };
203
204 register_parser(kboot_parser);