10 #include <sys/types.h>
13 #include <talloc/talloc.h>
16 #include "pb-protocol/pb-protocol.h"
19 #include "parser-utils.h"
20 #include "device-handler.h"
24 struct kboot_context {
25 struct discover_context *discover;
29 struct global_option {
36 static int param_is_ignored(const char *param)
38 static const char *ignored_options[] =
39 { "message", "timeout", "default", NULL };
42 for (str = ignored_options; *str; str++)
43 if (streq(*str, param))
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.
53 * Returns the next byte to process, or null if we've hit the end of the
57 static char *get_param_pair(char *str, char **name_out, char **value_out,
60 char *sep, *tmp, *name, *value;
62 /* terminate the value */
63 tmp = strchr(str, terminator);
69 sep = strchr(str, '=');
73 return tmp ? tmp + 1 : NULL;
76 /* terminate the name */
79 /* remove leading spaces */
80 for (name = str; isspace(*name); name++);
81 for (value = sep + 1; isspace(*value); value++);
83 /* .. and trailing ones.. */
84 for (sep--; isspace(*sep); sep--)
86 for (sep = value + strlen(value) - 1; isspace(*sep); sep--)
92 return tmp ? tmp + 1 : NULL;
95 static struct global_option global_options[] = {
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.
106 static int check_for_global_option(struct kboot_context *ctx,
107 const char *name, const char *value)
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);
120 static char *get_global_option(
121 struct kboot_context *ctx __attribute__((unused)),
126 for (i = 0; global_options[i].name ;i++)
127 if (!strcmp(name, global_options[i].name))
128 return global_options[i].value;
133 static int parse_option(struct kboot_context *kboot_ctx, char *opt_name,
136 char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;
137 struct boot_option *opt;
139 root = initrd = cmdline = NULL;
141 /* remove quotes around the value */
142 while (*config == '"' || *config == '\'')
145 pos = config + strlen(config) - 1;
146 while (*pos == '"' || *pos == '\'')
152 pos = strchr(config, ' ');
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);
159 /* if there's no space, it's only a kernel image with no params */
161 opt->boot_image_file = resolve_path(opt, config,
162 kboot_ctx->discover->device_path);
163 opt->description = talloc_strdup(opt, config);
168 opt->boot_image_file = resolve_path(opt, config,
169 kboot_ctx->discover->device_path);
171 cmdline = talloc_array(opt, char, buf_size);
175 pos = get_param_pair(pos, &name, &value, ' ');
178 strcat(cmdline, " ");
179 strcat(cmdline, value);
181 } else if (streq(name, "initrd")) {
184 } else if (streq(name, "root")) {
188 strcat(cmdline, " ");
190 strcat(cmdline, name);
195 root = get_global_option(kboot_ctx, "root");
197 initrd = get_global_option(kboot_ctx, "initrd");
200 tmp = talloc_asprintf(opt, "initrd=%s %s", initrd, cmdline);
201 talloc_free(cmdline);
204 opt->initrd_file = resolve_path(opt, initrd,
205 kboot_ctx->discover->device_path);
209 tmp = talloc_asprintf(opt, "root=%s %s", root, cmdline);
210 talloc_free(cmdline);
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);
220 opt->boot_args = cmdline;
222 opt->description = talloc_asprintf(opt, "%s %s",
223 config, opt->boot_args);
226 device_add_boot_option(kboot_ctx->discover->device, opt);
230 static void parse_buf(struct kboot_context *kboot_ctx)
232 char *pos, *name, *value;
234 for (pos = kboot_ctx->buf; pos;) {
235 pos = get_param_pair(pos, &name, &value, '\n');
237 if (name == NULL || param_is_ignored(name))
243 if (check_for_global_option(kboot_ctx, name, value))
246 parse_option(kboot_ctx, name, value);
251 static int parse(struct discover_context *ctx)
253 struct kboot_context *kboot_ctx;
261 kboot_ctx = talloc_zero(ctx, struct kboot_context);
262 kboot_ctx->discover = ctx;
264 filepath = resolve_path(kboot_ctx, "/etc/kboot.conf", ctx->device_path);
266 fd = open(filepath, O_RDONLY);
270 if (fstat(fd, &stat))
273 kboot_ctx->buf = talloc_array(kboot_ctx, char, stat.st_size + 1);
275 len = read(fd, kboot_ctx->buf, stat.st_size);
278 kboot_ctx->buf[len] = 0;
280 if (!ctx->device->icon_file)
281 ctx->device->icon_file = talloc_strdup(ctx,
282 generic_icon_file(guess_device_type(ctx)));
284 parse_buf(kboot_ctx);
291 talloc_free(kboot_ctx);
295 define_parser(kboot, 98, parse);