10 #include <sys/types.h>
18 static const char *devpath;
20 static int param_is_ignored(const char *param)
22 static const char *ignored_options[] =
23 { "message", "timeout", "default", NULL };
26 for (str = ignored_options; *str; str++)
27 if (streq(*str, param))
33 * Splits a name=value pair, with value terminated by @term (or nul). if there
34 * is no '=', then only the value is populated, and *name is set to NULL. The
35 * string is modified in place.
37 * Returns the next byte to process, or null if we've hit the end of the
41 static char *get_param_pair(char *str, char **name_out, char **value_out,
44 char *sep, *tmp, *name, *value;
46 /* terminate the value */
47 tmp = strchr(str, terminator);
53 sep = strchr(str, '=');
57 return tmp ? tmp + 1 : NULL;
60 /* terminate the name */
63 /* remove leading spaces */
64 for (name = str; isspace(*name); name++);
65 for (value = sep + 1; isspace(*value); value++);
67 /* .. and trailing ones.. */
68 for (sep--; isspace(*sep); sep--)
70 for (sep = value + strlen(value) - 1; isspace(*sep); sep--)
76 return tmp ? tmp + 1 : NULL;
79 struct global_option {
85 struct global_option global_options[] = {
93 * Check if an option (name=value) is a global option. If so, store it in
94 * the global options table, and return 1. Otherwise, return 0.
96 static int check_for_global_option(const char *name, const char *value)
100 for (i = 0; global_options[i].name ;i++) {
101 if (!strcmp(name, global_options[i].name)) {
102 global_options[i].value = strdup(value);
109 static char *get_global_option(const char *name)
113 for (i = 0; global_options[i].name ;i++)
114 if (!strcmp(name, global_options[i].name))
115 return global_options[i].value;
120 static int parse_option(struct boot_option *opt, char *config)
122 char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;
124 root = initrd = cmdline = NULL;
126 /* remove quotes around the value */
127 while (*config == '"' || *config == '\'')
130 pos = config + strlen(config) - 1;
131 while (*pos == '"' || *pos == '\'')
137 pos = strchr(config, ' ');
139 /* if there's no space, it's only a kernel image with no params */
141 opt->boot_image_file = resolve_path(config, devpath);
142 opt->description = strdup(config);
147 opt->boot_image_file = resolve_path(config, devpath);
149 cmdline = malloc(buf_size);
153 pos = get_param_pair(pos, &name, &value, ' ');
156 strcat(cmdline, " ");
157 strcat(cmdline, value);
159 } else if (streq(name, "initrd")) {
162 } else if (streq(name, "root")) {
166 strcat(cmdline, " ");
168 strcat(cmdline, name);
173 root = get_global_option("root");
175 initrd = get_global_option("initrd");
178 asprintf(&tmp, "initrd=%s %s", initrd, cmdline);
182 opt->initrd_file = resolve_path(initrd, devpath);
186 asprintf(&tmp, "root=%s %s", root, cmdline);
191 /* if there's an initrd but no root, fake up /dev/ram0 */
192 asprintf(&tmp, "root=/dev/ram0 %s", cmdline);
197 pb_log("kboot cmdline: %s\n", cmdline);
198 opt->boot_args = cmdline;
200 asprintf(&opt->description, "%s %s",
201 config, opt->boot_args);
206 static void parse_buf(struct device *dev, char *buf)
208 char *pos, *name, *value;
211 for (pos = buf; pos;) {
212 struct boot_option opt;
214 pos = get_param_pair(pos, &name, &value, '\n');
216 pb_log("kboot param: '%s' = '%s'\n", name, value);
218 if (name == NULL || param_is_ignored(name))
224 if (check_for_global_option(name, value))
227 memset(&opt, 0, sizeof(opt));
228 opt.name = strdup(name);
230 if (parse_option(&opt, value))
233 add_boot_option(&opt);
239 static int parse(const char *device)
241 char *filepath, *buf;
248 filepath = resolve_path("/etc/kboot.conf", devpath);
250 fd = open(filepath, O_RDONLY);
254 if (fstat(fd, &stat))
257 buf = malloc(stat.st_size + 1);
261 len = read(fd, buf, stat.st_size);
266 dev = malloc(sizeof(*dev));
267 memset(dev, 0, sizeof(*dev));
268 dev->id = strdup(device);
269 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
284 struct parser kboot_parser = {
285 .name = "kboot.conf parser",