X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fkboot-parser.c;h=23d48a401f8a82cdff8daf4035f2769d4ccc6971;hp=df2e7620e13ad8ea40a735aaa04da60881f43de1;hb=3f82d6fc10a582ba3f558b69c14ab4ab7234e5ea;hpb=32e6a41f33e5576716b351bd473a27939fe94fa1 diff --git a/discover/kboot-parser.c b/discover/kboot-parser.c index df2e762..23d48a4 100644 --- a/discover/kboot-parser.c +++ b/discover/kboot-parser.c @@ -1,288 +1,151 @@ #define _GNU_SOURCE +#include #include -#include #include -#include -#include -#include -#include -#include +#include "log/log.h" +#include "talloc/talloc.h" +#include "pb-protocol/pb-protocol.h" +#include "parser-conf.h" +#include "parser-utils.h" +#include "paths.h" -#include "parser.h" -#include "params.h" - -#define buf_size 1024 - -static const char *devpath; - -static int param_is_ignored(const char *param) +static void kboot_process_pair(struct conf_context *conf, const char *name, + char *value) { - static const char *ignored_options[] = - { "message", "timeout", "default", NULL }; - const char **str; - - for (str = ignored_options; *str; str++) - if (streq(*str, param)) - return 1; - return 0; -} + const char *const *ignored_names = conf->parser_info; + char *pos; + char *args; + const char *initrd; + const char *root; + struct boot_option *opt; -/** - * Splits a name=value pair, with value terminated by @term (or nul). if there - * is no '=', then only the value is populated, and *name is set to NULL. The - * string is modified in place. - * - * Returns the next byte to process, or null if we've hit the end of the - * string. - * - * */ -static char *get_param_pair(char *str, char **name_out, char **value_out, - char terminator) -{ - char *sep, *tmp, *name, *value; - - /* terminate the value */ - tmp = strchr(str, terminator); - if (tmp) - *tmp = 0; - else - tmp = NULL; - - sep = strchr(str, '='); - if (!sep) { - *name_out = NULL; - *value_out = str; - return tmp ? tmp + 1 : NULL; - } + if (conf_param_in_list(ignored_names, name)) + return; - /* terminate the name */ - *sep = 0; + if (conf_set_global_option(conf, name, value)) + return; - /* remove leading spaces */ - for (name = str; isspace(*name); name++); - for (value = sep + 1; isspace(*value); value++); + /* opt must be associated with dc */ - /* .. and trailing ones.. */ - for (sep--; isspace(*sep); sep--) - *sep = 0; - for (sep = value + strlen(value) - 1; isspace(*sep); sep--) - *sep = 0; + opt = talloc_zero(conf->dc->device, struct boot_option); - *name_out = name; - *value_out = value; + if (!opt) + return; - return tmp ? tmp + 1 : NULL; -} + opt->id = talloc_asprintf(opt, "%s#%s", conf->dc->device->id, name); + opt->name = talloc_strdup(opt, name); -struct global_option { - char *name; - char *value; -}; + args = talloc_strdup(opt, ""); + initrd = conf_get_global_option(conf, "initrd"); + root = conf_get_global_option(conf, "root"); - -static struct global_option global_options[] = { - { .name = "root" }, - { .name = "initrd" }, - { .name = "video" }, - { .name = NULL } -}; - -/* - * Check if an option (name=value) is a global option. If so, store it in - * the global options table, and return 1. Otherwise, return 0. - */ -static int check_for_global_option(const char *name, const char *value) -{ - int i; - - for (i = 0; global_options[i].name ;i++) { - if (!strcmp(name, global_options[i].name)) { - global_options[i].value = strdup(value); - return 1; - } - } - return 0; -} - -static char *get_global_option(const char *name) -{ - int i; - - for (i = 0; global_options[i].name ;i++) - if (!strcmp(name, global_options[i].name)) - return global_options[i].value; - - return NULL; -} - -static int parse_option(struct boot_option *opt, char *config) -{ - char *pos, *name, *value, *root, *initrd, *cmdline, *tmp; - - root = initrd = cmdline = NULL; - - /* remove quotes around the value */ - while (*config == '"' || *config == '\'') - config++; - - pos = config + strlen(config) - 1; - while (*pos == '"' || *pos == '\'') - *(pos--) = 0; - - if (!strlen(pos)) - return 0; - - pos = strchr(config, ' '); + pos = strchr(value, ' '); /* if there's no space, it's only a kernel image with no params */ - if (!pos) { - opt->boot_image_file = resolve_path(config, devpath); - opt->description = strdup(config); - return 1; - } + if (!pos) + goto out_add; *pos = 0; - opt->boot_image_file = resolve_path(config, devpath); - - cmdline = malloc(buf_size); - *cmdline = 0; for (pos++; pos;) { - pos = get_param_pair(pos, &name, &value, ' '); - - if (!name) { - strcat(cmdline, " "); - strcat(cmdline, value); + char *cl_name, *cl_value; - } else if (streq(name, "initrd")) { - initrd = value; + pos = conf_get_param_pair(pos, &cl_name, &cl_value, ' '); - } else if (streq(name, "root")) { - root = value; - - } else { - strcat(cmdline, " "); - *(value - 1) = '='; - strcat(cmdline, name); + if (!cl_name) { + args = talloc_asprintf_append(args, "%s ", cl_value); + continue; } - } - - if (!root) - root = get_global_option("root"); - if (!initrd) - initrd = get_global_option("initrd"); - if (initrd) { - asprintf(&tmp, "initrd=%s %s", initrd, cmdline); - free(cmdline); - cmdline = tmp; + if (streq(cl_name, "initrd")) { + initrd = cl_value; + continue; + } - opt->initrd_file = resolve_path(initrd, devpath); - } + if (streq(cl_name, "root")) { + root = cl_value; + continue; + } - if (root) { - asprintf(&tmp, "root=%s %s", root, cmdline); - free(cmdline); - cmdline = tmp; - - } else if (initrd) { - /* if there's an initrd but no root, fake up /dev/ram0 */ - asprintf(&tmp, "root=/dev/ram0 %s", cmdline); - free(cmdline); - cmdline = tmp; + args = talloc_asprintf_append(args, "%s=%s ", cl_name, + cl_value); } - pb_log("kboot cmdline: %s\n", cmdline); - opt->boot_args = cmdline; - - asprintf(&opt->description, "%s %s", - config, opt->boot_args); - - return 1; -} - -static void parse_buf(struct device *dev, char *buf) -{ - char *pos, *name, *value; - int sent_device = 0; - - for (pos = buf; pos;) { - struct boot_option opt; - - pos = get_param_pair(pos, &name, &value, '\n'); - - pb_log("kboot param: '%s' = '%s'\n", name, value); - - if (name == NULL || param_is_ignored(name)) - continue; +out_add: + opt->boot_image_file = resolve_path(opt, value, conf->dc->device_path); - if (*name == '#') - continue; + if (root) { + opt->boot_args = talloc_asprintf(opt, "root=%s %s", root, args); + talloc_free(args); + } else + opt->boot_args = args; - if (check_for_global_option(name, value)) - continue; + if (initrd) { + opt->initrd_file = resolve_path(opt, initrd, + conf->dc->device_path); - memset(&opt, 0, sizeof(opt)); - opt.name = strdup(name); + opt->description = talloc_asprintf(opt, "%s initrd=%s %s", + value, initrd, opt->boot_args); + } else + opt->description = talloc_asprintf(opt, "%s %s", value, + opt->boot_args); - if (parse_option(&opt, value)) - if (!sent_device++) - add_device(dev); - add_boot_option(&opt); + conf_strip_str(opt->boot_args); + conf_strip_str(opt->description); - free(opt.name); - } + device_add_boot_option(conf->dc->device, opt); } -static int parse(const char *device) -{ - char *filepath, *buf; - int fd, len, rc = 0; - struct stat stat; - struct device *dev; - - devpath = device; - - filepath = resolve_path("/etc/kboot.conf", devpath); +static struct conf_global_option kboot_global_options[] = { + { .name = "initrd" }, + { .name = "root" }, + { .name = "video" }, + { .name = NULL } +}; - fd = open(filepath, O_RDONLY); - if (fd < 0) - goto out_free_path; +static const char *const kboot_conf_files[] = { + "/kboot.conf", + "/kboot.cnf", + "/etc/kboot.conf", + "/etc/kboot.cnf", + "/KBOOT.CONF", + "/KBOOT.CNF", + "/ETC/KBOOT.CONF", + "/ETC/KBOOT.CNF", + NULL +}; - if (fstat(fd, &stat)) - goto out_close; +static const char *const kboot_ignored_names[] = { + "default", + "delay", + "message", + "timeout", + NULL +}; - buf = malloc(stat.st_size + 1); - if (!buf) - goto out_close;; +static int kboot_parse(struct discover_context *dc) +{ + struct conf_context *conf; + int rc; - len = read(fd, buf, stat.st_size); - if (len < 0) - goto out_free_buf; - buf[len] = 0; + conf = talloc_zero(dc, struct conf_context); - dev = malloc(sizeof(*dev)); - memset(dev, 0, sizeof(*dev)); - dev->id = strdup(device); - dev->icon_file = strdup(generic_icon_file(guess_device_type())); + if (!conf) + return -1; - parse_buf(dev, buf); + conf->dc = dc; + conf->global_options = kboot_global_options, + conf_init_global_options(conf); + conf->conf_files = kboot_conf_files, + conf->process_pair = kboot_process_pair; + conf->parser_info = (void *)kboot_ignored_names, - rc = 1; + rc = conf_parse(conf); -out_free_buf: - free(buf); -out_close: - close(fd); -out_free_path: - free(filepath); + talloc_free(conf); return rc; } -struct parser kboot_parser = { - .name = "kboot.conf parser", - .priority = 98, - .parse = parse -}; +define_parser(kboot, 98, kboot_parse);