10 #include <sys/types.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);
112 pos = get_param_pair(pos, &name, &value, ' ');
115 strcat(cmdline, " ");
116 strcat(cmdline, value);
118 } else if (streq(name, "initrd")) {
121 } else if (streq(name, "root")) {
125 strcat(cmdline, " ");
127 strcat(cmdline, name);
132 asprintf(&tmp, "initrd=%s %s", initrd, cmdline);
136 opt->initrd_file = join_paths(mountpoint, initrd);
140 asprintf(&tmp, "root=%s %s", root, cmdline);
144 } else if (!initrd) {
145 /* if there's an initrd but no root, fake up /dev/ram0 */
146 asprintf(&tmp, "root=/dev/ram0 %s", cmdline);
151 pb_log("kboot cmdline: %s", cmdline);
152 opt->boot_args = cmdline;
154 asprintf(&opt->description, "%s %s", config, cmdline);
159 static void parse_buf(struct device *dev, char *buf)
161 char *pos, *name, *value;
164 for (pos = buf; pos;) {
165 struct boot_option opt;
167 pos = get_param_pair(pos, &name, &value, '\n');
169 pb_log("kboot param: '%s' = '%s'\n", name, value);
171 if (name == NULL || param_is_ignored(name))
177 memset(&opt, 0, sizeof(opt));
178 opt.name = strdup(name);
180 if (parse_option(&opt, value))
183 add_boot_option(&opt);
189 static int parse(const char *devicepath, const char *_mountpoint)
191 char *filepath, *buf;
196 mountpoint = _mountpoint;
198 filepath = join_paths(mountpoint, "/etc/kboot.conf");
200 fd = open(filepath, O_RDONLY);
204 if (fstat(fd, &stat))
207 buf = malloc(stat.st_size + 1);
211 len = read(fd, buf, stat.st_size);
216 dev = malloc(sizeof(*dev));
217 memset(dev, 0, sizeof(*dev));
218 dev->id = strdup(devicepath);
219 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
234 struct parser kboot_parser = {
235 .name = "kboot.conf parser",