]> git.ozlabs.org Git - petitboot/blob - discover/kboot-parser.c
930fbe49897cd51193762076b08d693783b7cd5a
[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 #include <log/log.h>
15
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(
121                 struct kboot_context *ctx __attribute__((unused)),
122                 const char *name)
123 {
124         int i;
125
126         for (i = 0; global_options[i].name ;i++)
127                 if (!strcmp(name, global_options[i].name))
128                         return global_options[i].value;
129
130         return NULL;
131 }
132
133 static int parse_option(struct kboot_context *kboot_ctx, char *opt_name,
134                 char *config)
135 {
136         char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;
137         struct boot_option *opt;
138
139         root = initrd = cmdline = NULL;
140
141         /* remove quotes around the value */
142         while (*config == '"' || *config == '\'')
143                 config++;
144
145         pos = config + strlen(config) - 1;
146         while (*pos == '"' || *pos == '\'')
147                 *(pos--) = 0;
148
149         if (!strlen(pos))
150                 return 0;
151
152         pos = strchr(config, ' ');
153
154         opt = talloc_zero(kboot_ctx, struct boot_option);
155         opt->id = talloc_asprintf(opt, "%s#%s",
156                         kboot_ctx->discover->device->id, opt_name);
157         opt->name = talloc_strdup(opt, opt_name);
158
159         /* if there's no space, it's only a kernel image with no params */
160         if (!pos) {
161                 opt->boot_image_file = resolve_path(opt, config,
162                                 kboot_ctx->discover->device_path);
163                 opt->description = talloc_strdup(opt, config);
164                 goto out_add;
165         }
166
167         *pos = 0;
168         opt->boot_image_file = resolve_path(opt, config,
169                         kboot_ctx->discover->device_path);
170
171         cmdline = talloc_array(opt, char, buf_size);
172         *cmdline = 0;
173
174         for (pos++; pos;) {
175                 pos = get_param_pair(pos, &name, &value, ' ');
176
177                 if (!name) {
178                         strcat(cmdline, " ");
179                         strcat(cmdline, value);
180
181                 } else if (streq(name, "initrd")) {
182                         initrd = value;
183
184                 } else if (streq(name, "root")) {
185                         root = value;
186
187                 } else {
188                         strcat(cmdline, " ");
189                         *(value - 1) = '=';
190                         strcat(cmdline, name);
191                 }
192         }
193
194         if (!root)
195                 root = get_global_option(kboot_ctx, "root");
196         if (!initrd)
197                 initrd = get_global_option(kboot_ctx, "initrd");
198
199         if (initrd) {
200                 tmp = talloc_asprintf(opt, "initrd=%s %s", initrd, cmdline);
201                 talloc_free(cmdline);
202                 cmdline = tmp;
203
204                 opt->initrd_file = resolve_path(opt, initrd,
205                                 kboot_ctx->discover->device_path);
206         }
207
208         if (root) {
209                 tmp = talloc_asprintf(opt, "root=%s %s", root, cmdline);
210                 talloc_free(cmdline);
211                 cmdline = tmp;
212
213         } else if (initrd) {
214                 /* if there's an initrd but no root, fake up /dev/ram0 */
215                 tmp = talloc_asprintf(opt, "root=/dev/ram0 %s", cmdline);
216                 talloc_free(cmdline);
217                 cmdline = tmp;
218         }
219
220         opt->boot_args = cmdline;
221
222         opt->description = talloc_asprintf(opt, "%s %s",
223                         config, opt->boot_args);
224
225 out_add:
226         device_add_boot_option(kboot_ctx->discover->device, opt);
227         return 1;
228 }
229
230 static void parse_buf(struct kboot_context *kboot_ctx)
231 {
232         char *pos, *name, *value;
233
234         for (pos = kboot_ctx->buf; pos;) {
235                 pos = get_param_pair(pos, &name, &value, '\n');
236
237                 if (name == NULL || param_is_ignored(name))
238                         continue;
239
240                 if (*name == '#')
241                         continue;
242
243                 if (check_for_global_option(kboot_ctx, name, value))
244                         continue;
245
246                 parse_option(kboot_ctx, name, value);
247         }
248 }
249
250
251 static int parse(struct discover_context *ctx)
252 {
253         struct kboot_context *kboot_ctx;
254         char *filepath;
255         int fd, len, rc;
256         struct stat stat;
257
258         rc = 0;
259         fd = -1;
260
261         kboot_ctx = talloc_zero(ctx, struct kboot_context);
262         kboot_ctx->discover = ctx;
263
264         filepath = resolve_path(kboot_ctx, "/etc/kboot.conf", ctx->device_path);
265
266         fd = open(filepath, O_RDONLY);
267         if (fd < 0)
268                 goto out;
269
270         if (fstat(fd, &stat))
271                 goto out;
272
273         kboot_ctx->buf = talloc_array(kboot_ctx, char, stat.st_size + 1);
274
275         len = read(fd, kboot_ctx->buf, stat.st_size);
276         if (len < 0)
277                 goto out;
278         kboot_ctx->buf[len] = 0;
279
280         if (!ctx->device->icon_file)
281                 ctx->device->icon_file = talloc_strdup(ctx,
282                                 generic_icon_file(guess_device_type(ctx)));
283
284         parse_buf(kboot_ctx);
285
286         rc = 1;
287
288 out:
289         if (fd >= 0)
290                 close(fd);
291         talloc_free(kboot_ctx);
292         return rc;
293 }
294
295 define_parser(kboot, 98, parse);