]> git.ozlabs.org Git - petitboot/blob - discover/kboot-parser.c
f8cd61d5b2e66a2cd971265cdcecccd6822d9af3
[petitboot] / discover / kboot-parser.c
1 #define _GNU_SOURCE
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <unistd.h>
8
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13 #include <talloc/talloc.h>
14
15 #include "log.h"
16 #include "pb-protocol/pb-protocol.h"
17 #include "paths.h"
18 #include "params.h"
19 #include "parser-utils.h"
20 #include "device-handler.h"
21
22 #define buf_size 1024
23
24 struct kboot_context {
25         struct discover_context *discover;
26
27         char *buf;
28
29         struct global_option {
30                 char *name;
31                 char *value;
32         } *global_options;
33         int n_global_options;
34 };
35
36 static int param_is_ignored(const char *param)
37 {
38         static const char *ignored_options[] =
39                 { "message", "timeout", "default", NULL };
40         const char **str;
41
42         for (str = ignored_options; *str; str++)
43                 if (streq(*str, param))
44                         return 1;
45         return 0;
46 }
47
48 /**
49  * Splits a name=value pair, with value terminated by @term (or nul). if there
50  * is no '=', then only the value is populated, and *name is set to NULL. The
51  * string is modified in place.
52  *
53  * Returns the next byte to process, or null if we've hit the end of the
54  * string.
55  *
56  * */
57 static char *get_param_pair(char *str, char **name_out, char **value_out,
58                 char terminator)
59 {
60         char *sep, *tmp, *name, *value;
61
62         /* terminate the value */
63         tmp = strchr(str, terminator);
64         if (tmp)
65                 *tmp = 0;
66         else
67                 tmp = NULL;
68
69         sep = strchr(str, '=');
70         if (!sep) {
71                 *name_out = NULL;
72                 *value_out = str;
73                 return tmp ? tmp + 1 : NULL;
74         }
75
76         /* terminate the name */
77         *sep = 0;
78
79         /* remove leading spaces */
80         for (name = str; isspace(*name); name++);
81         for (value = sep + 1; isspace(*value); value++);
82
83         /* .. and trailing ones.. */
84         for (sep--; isspace(*sep); sep--)
85                 *sep = 0;
86         for (sep = value + strlen(value) - 1; isspace(*sep); sep--)
87                 *sep = 0;
88
89         *name_out = name;
90         *value_out = value;
91
92         return tmp ? tmp + 1 : NULL;
93 }
94
95 static struct global_option global_options[] = {
96         { .name = "root" },
97         { .name = "initrd" },
98         { .name = "video" },
99         { .name = NULL }
100 };
101
102 /*
103  * Check if an option (name=value) is a global option. If so, store it in
104  * the global options table, and return 1. Otherwise, return 0.
105  */
106 static int check_for_global_option(struct kboot_context *ctx,
107                 const char *name, const char *value)
108 {
109         int i;
110
111         for (i = 0; i < ctx->n_global_options; i++) {
112                 if (!strcmp(name, ctx->global_options[i].name)) {
113                         global_options[i].value = strdup(value);
114                         break;
115                 }
116         }
117         return 0;
118 }
119
120 static char *get_global_option(struct kboot_context *ctx, const char *name)
121 {
122         int i;
123
124         for (i = 0; global_options[i].name ;i++)
125                 if (!strcmp(name, global_options[i].name))
126                         return global_options[i].value;
127
128         return NULL;
129 }
130
131 static int parse_option(struct kboot_context *kboot_ctx, char *opt_name,
132                 char *config)
133 {
134         char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;
135         struct boot_option *opt;
136
137         root = initrd = cmdline = NULL;
138
139         /* remove quotes around the value */
140         while (*config == '"' || *config == '\'')
141                 config++;
142
143         pos = config + strlen(config) - 1;
144         while (*pos == '"' || *pos == '\'')
145                 *(pos--) = 0;
146
147         if (!strlen(pos))
148                 return 0;
149
150         pos = strchr(config, ' ');
151
152         opt = talloc_zero(kboot_ctx, struct boot_option);
153         opt->id = talloc_asprintf(opt, "%s#%s",
154                         kboot_ctx->discover->device->id, opt_name);
155         opt->name = talloc_strdup(opt, opt_name);
156
157         /* if there's no space, it's only a kernel image with no params */
158         if (!pos) {
159                 opt->boot_image_file = resolve_path(opt, config,
160                                 kboot_ctx->discover->device_path);
161                 opt->description = talloc_strdup(opt, config);
162                 goto out_add;
163         }
164
165         *pos = 0;
166         opt->boot_image_file = resolve_path(opt, config,
167                         kboot_ctx->discover->device_path);
168
169         cmdline = talloc_array(opt, char, buf_size);
170         *cmdline = 0;
171
172         for (pos++; pos;) {
173                 pos = get_param_pair(pos, &name, &value, ' ');
174
175                 if (!name) {
176                         strcat(cmdline, " ");
177                         strcat(cmdline, value);
178
179                 } else if (streq(name, "initrd")) {
180                         initrd = value;
181
182                 } else if (streq(name, "root")) {
183                         root = value;
184
185                 } else {
186                         strcat(cmdline, " ");
187                         *(value - 1) = '=';
188                         strcat(cmdline, name);
189                 }
190         }
191
192         if (!root)
193                 root = get_global_option(kboot_ctx, "root");
194         if (!initrd)
195                 initrd = get_global_option(kboot_ctx, "initrd");
196
197         if (initrd) {
198                 tmp = talloc_asprintf(opt, "initrd=%s %s", initrd, cmdline);
199                 talloc_free(cmdline);
200                 cmdline = tmp;
201
202                 opt->initrd_file = resolve_path(opt, initrd,
203                                 kboot_ctx->discover->device_path);
204         }
205
206         if (root) {
207                 tmp = talloc_asprintf(opt, "root=%s %s", root, cmdline);
208                 talloc_free(cmdline);
209                 cmdline = tmp;
210
211         } else if (initrd) {
212                 /* if there's an initrd but no root, fake up /dev/ram0 */
213                 tmp = talloc_asprintf(opt, "root=/dev/ram0 %s", cmdline);
214                 talloc_free(cmdline);
215                 cmdline = tmp;
216         }
217
218         opt->boot_args = cmdline;
219
220         opt->description = talloc_asprintf(opt, "%s %s",
221                         config, opt->boot_args);
222
223 out_add:
224         device_add_boot_option(kboot_ctx->discover->device, opt);
225         return 1;
226 }
227
228 static void parse_buf(struct kboot_context *kboot_ctx)
229 {
230         char *pos, *name, *value;
231
232         for (pos = kboot_ctx->buf; pos;) {
233                 pos = get_param_pair(pos, &name, &value, '\n');
234
235                 if (name == NULL || param_is_ignored(name))
236                         continue;
237
238                 if (*name == '#')
239                         continue;
240
241                 if (check_for_global_option(kboot_ctx, name, value))
242                         continue;
243
244                 parse_option(kboot_ctx, name, value);
245         }
246 }
247
248
249 static int parse(struct discover_context *ctx)
250 {
251         struct kboot_context *kboot_ctx;
252         char *filepath;
253         int fd, len, rc;
254         struct stat stat;
255
256         rc = 0;
257         fd = -1;
258
259         kboot_ctx = talloc_zero(ctx, struct kboot_context);
260         kboot_ctx->discover = ctx;
261
262         filepath = resolve_path(kboot_ctx, "/etc/kboot.conf", ctx->device_path);
263
264         fd = open(filepath, O_RDONLY);
265         if (fd < 0)
266                 goto out;
267
268         if (fstat(fd, &stat))
269                 goto out;
270
271         kboot_ctx->buf = talloc_array(kboot_ctx, char, stat.st_size + 1);
272
273         len = read(fd, kboot_ctx->buf, stat.st_size);
274         if (len < 0)
275                 goto out;
276         kboot_ctx->buf[len] = 0;
277
278         if (!ctx->device->icon_file)
279                 ctx->device->icon_file = talloc_strdup(ctx,
280                                 generic_icon_file(guess_device_type(ctx)));
281
282         parse_buf(kboot_ctx);
283
284         rc = 1;
285
286 out:
287         if (fd >= 0)
288                 close(fd);
289         talloc_free(kboot_ctx);
290         return rc;
291 }
292
293 define_parser(kboot, 98, parse);