10 #include <sys/types.h>
13 #include "udev-helper.h"
18 static const char *mountpoint;
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 static int parse_option(struct boot_option *opt, char *config)
81 char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;
83 root = initrd = cmdline = NULL;
85 /* remove quotes around the value */
86 while (*config == '"' || *config == '\'')
89 pos = config + strlen(config) - 1;
90 while (*pos == '"' || *pos == '\'')
96 pos = strchr(config, ' ');
98 /* if there's no space, it's only a kernel image with no params */
100 opt->boot_image_file = join_paths(mountpoint, config);
101 opt->description = strdup(config);
106 opt->boot_image_file = join_paths(mountpoint, config);
108 cmdline = malloc(buf_size);
111 pos = get_param_pair(pos, &name, &value, ' ');
114 strcat(cmdline, " ");
115 strcat(cmdline, value);
117 } else if (streq(name, "initrd")) {
120 } else if (streq(name, "root")) {
125 strcat(cmdline, name);
130 asprintf(&tmp, "initrd=%s %s", initrd, cmdline);
134 opt->initrd_file = join_paths(mountpoint, initrd);
138 asprintf(&tmp, "root=%s %s", root, cmdline);
142 } else if (!initrd) {
143 /* if there's an initrd but no root, fake up /dev/ram0 */
144 asprintf(&tmp, "root=/dev/ram0 %s", cmdline);
149 printf("kboot cmdline: %s", cmdline);
150 opt->boot_args = cmdline;
152 asprintf(&opt->description, "%s %s", opt->boot_image_file, cmdline);
157 static void parse_buf(struct device *dev, char *buf)
159 char *pos, *name, *value;
162 for (pos = buf; pos;) {
163 struct boot_option opt;
165 pos = get_param_pair(pos, &name, &value, '\n');
167 printf("kboot param: '%s' = '%s'\n", name, value);
169 if (name == NULL || param_is_ignored(name))
172 memset(&opt, 0, sizeof(opt));
173 opt.name = strdup(name);
175 if (parse_option(&opt, value))
178 add_boot_option(&opt);
184 static int parse(const char *devicepath, const char *_mountpoint)
186 char *filepath, *buf;
191 mountpoint = _mountpoint;
193 filepath = join_paths(mountpoint, "/etc/kboot.conf");
195 fd = open(filepath, O_RDONLY);
199 if (fstat(fd, &stat))
202 buf = malloc(stat.st_size + 1);
206 len = read(fd, buf, stat.st_size);
211 dev = malloc(sizeof(*dev));
212 memset(dev, 0, sizeof(*dev));
213 dev->id = strdup(devicepath);
214 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
229 struct parser kboot_parser = {
230 .name = "kboot.conf parser",