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(struct kboot_context *ctx, const char *name)
124 for (i = 0; global_options[i].name ;i++)
125 if (!strcmp(name, global_options[i].name))
126 return global_options[i].value;
131 static int parse_option(struct kboot_context *kboot_ctx, char *opt_name,
134 char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;
135 struct boot_option *opt;
137 root = initrd = cmdline = NULL;
139 /* remove quotes around the value */
140 while (*config == '"' || *config == '\'')
143 pos = config + strlen(config) - 1;
144 while (*pos == '"' || *pos == '\'')
150 pos = strchr(config, ' ');
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);
157 /* if there's no space, it's only a kernel image with no params */
159 opt->boot_image_file = resolve_path(opt, config,
160 kboot_ctx->discover->device_path);
161 opt->description = talloc_strdup(opt, config);
166 opt->boot_image_file = resolve_path(opt, config,
167 kboot_ctx->discover->device_path);
169 cmdline = talloc_array(opt, char, buf_size);
173 pos = get_param_pair(pos, &name, &value, ' ');
176 strcat(cmdline, " ");
177 strcat(cmdline, value);
179 } else if (streq(name, "initrd")) {
182 } else if (streq(name, "root")) {
186 strcat(cmdline, " ");
188 strcat(cmdline, name);
193 root = get_global_option(kboot_ctx, "root");
195 initrd = get_global_option(kboot_ctx, "initrd");
198 tmp = talloc_asprintf(opt, "initrd=%s %s", initrd, cmdline);
199 talloc_free(cmdline);
202 opt->initrd_file = resolve_path(opt, initrd,
203 kboot_ctx->discover->device_path);
207 tmp = talloc_asprintf(opt, "root=%s %s", root, cmdline);
208 talloc_free(cmdline);
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);
218 opt->boot_args = cmdline;
220 opt->description = talloc_asprintf(opt, "%s %s",
221 config, opt->boot_args);
224 device_add_boot_option(kboot_ctx->discover->device, opt);
228 static void parse_buf(struct kboot_context *kboot_ctx)
230 char *pos, *name, *value;
232 for (pos = kboot_ctx->buf; pos;) {
233 pos = get_param_pair(pos, &name, &value, '\n');
235 if (name == NULL || param_is_ignored(name))
241 if (check_for_global_option(kboot_ctx, name, value))
244 parse_option(kboot_ctx, name, value);
249 static int parse(struct discover_context *ctx)
251 struct kboot_context *kboot_ctx;
259 kboot_ctx = talloc_zero(ctx, struct kboot_context);
260 kboot_ctx->discover = ctx;
262 filepath = resolve_path(kboot_ctx, "/etc/kboot.conf", ctx->device_path);
264 fd = open(filepath, O_RDONLY);
268 if (fstat(fd, &stat))
271 kboot_ctx->buf = talloc_array(kboot_ctx, char, stat.st_size + 1);
273 len = read(fd, kboot_ctx->buf, stat.st_size);
276 kboot_ctx->buf[len] = 0;
278 if (!ctx->device->icon_file)
279 ctx->device->icon_file = talloc_strdup(ctx,
280 generic_icon_file(guess_device_type(ctx)));
282 parse_buf(kboot_ctx);
289 talloc_free(kboot_ctx);
293 define_parser(kboot, 98, parse);