X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fyaboot-parser.c;h=f2f34c10b18e32636998dfa5e0980821e20736e6;hp=7fedf20f13963434abe1729f118aabc262244fbf;hb=38d7d1a97d46aacf67675038c927e579bb589310;hpb=e4f5bd235894c11823ac1befe8c8c43063cad026 diff --git a/discover/yaboot-parser.c b/discover/yaboot-parser.c index 7fedf20..f2f34c1 100644 --- a/discover/yaboot-parser.c +++ b/discover/yaboot-parser.c @@ -9,16 +9,42 @@ #include "types/types.h" #include "parser-conf.h" #include "parser-utils.h" -#include "paths.h" +#include "resource.h" struct yaboot_state { - struct discover_boot_option *opt; - const char *desc_image; + char *desc_image; char *desc_initrd; int globals_done; const char *const *known_names; + + /* current option data */ + struct discover_boot_option *opt; + const char *initrd_size; + const char *literal; + const char *ramdisk; + const char *root; + bool read_only; + bool read_write; }; +static struct discover_boot_option *state_start_new_option( + struct conf_context *conf, + struct yaboot_state *state) +{ + state->desc_initrd = NULL; + + state->opt = discover_boot_option_create(conf->dc, conf->dc->device); + state->opt->option->boot_args = talloc_strdup(state->opt->option, ""); + + /* old allocated values will get freed with the state */ + state->initrd_size = conf_get_global_option(conf, "initrd_size"); + state->literal = conf_get_global_option(conf, "literal"); + state->ramdisk = conf_get_global_option(conf, "ramdisk"); + state->root = conf_get_global_option(conf, "root"); + + return state->opt; +} + static void yaboot_finish(struct conf_context *conf) { struct yaboot_state *state = conf->parser_info; @@ -37,30 +63,86 @@ static void yaboot_finish(struct conf_context *conf) assert(opt->name); assert(opt->boot_args); + /* populate the boot option from state data */ + if (state->initrd_size) { + opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s", + state->initrd_size, opt->boot_args); + } + + if (state->ramdisk) { + opt->boot_args = talloc_asprintf(opt, "ramdisk=%s %s", + state->initrd_size, opt->boot_args); + } + + if (state->root) { + opt->boot_args = talloc_asprintf(opt, "root=%s %s", + state->root, opt->boot_args); + } + + if (state->read_only && state->read_write) { + pb_log("boot option %s specified both 'ro' and 'rw', " + "using 'rw'\n", opt->name); + state->read_only = false; + } + + if (state->read_only || state->read_write) { + opt->boot_args = talloc_asprintf(opt, "%s %s", + state->read_only ? "ro" : "rw", + opt->boot_args); + } + + if (state->literal) { + opt->boot_args = talloc_strdup(opt, state->literal); + } + opt->description = talloc_asprintf(opt, "%s %s %s", state->desc_image, (state->desc_initrd ? state->desc_initrd : ""), - opt->boot_args); + opt->boot_args ? opt->boot_args : ""); talloc_free(state->desc_initrd); - state->desc_initrd = NULL; conf_strip_str(opt->boot_args); conf_strip_str(opt->description); - /* opt is persistent, so must be associated with device */ - discover_context_add_boot_option(conf->dc, state->opt); +} - state->opt = discover_boot_option_create(conf->dc, conf->dc->device); - state->opt->option->boot_args = talloc_strdup(state->opt->option, ""); +static struct resource *create_yaboot_devpath_resource( + struct discover_boot_option *opt, + struct conf_context *conf, + const char *path, char **desc_str) +{ + const char *g_boot = conf_get_global_option(conf, "boot"); + const char *g_part = conf_get_global_option(conf, "partition"); + struct resource *res; + char *devpath; + + if (g_boot && g_part) { + devpath = talloc_asprintf(conf, + "%s%s:%s", g_boot, g_part, path); + } else if (g_boot) { + devpath = talloc_asprintf(conf, "%s:%s", g_boot, path); + } else { + devpath = talloc_strdup(conf, path); + } + + res = create_devpath_resource(opt, conf->dc->device, devpath); + + if (desc_str) + *desc_str = devpath; + else + talloc_free(devpath); + + return res; } + static void yaboot_process_pair(struct conf_context *conf, const char *name, char *value) { struct yaboot_state *state = conf->parser_info; - struct boot_option *opt = state->opt->option; + struct discover_boot_option *opt = state->opt; struct fixed_pair { const char *image; const char *initrd; @@ -91,35 +173,16 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name, /* image */ if (streq(name, "image")) { - const char *g_boot = conf_get_global_option(conf, "boot"); - const char *g_part = conf_get_global_option(conf, "partition"); /* First finish any previous image. */ - - if (opt->boot_image_file) + if (opt) yaboot_finish(conf); /* Then start the new image. */ + opt = state_start_new_option(conf, state); - if (g_boot && g_part) { - char* dev = talloc_asprintf(NULL, "%s%s", g_boot, - g_part); - - opt->boot_image_file = resolve_path(opt, - value, dev); - state->desc_image = talloc_asprintf(opt, - "%s%s", dev, value); - talloc_free(dev); - } else if (g_boot) { - opt->boot_image_file = resolve_path(opt, - value, g_boot); - state->desc_image = talloc_asprintf(opt, - "%s%s", g_boot, value); - } else { - opt->boot_image_file = resolve_path(opt, - value, conf->dc->device->device_path); - state->desc_image = talloc_strdup(opt, value); - } + opt->boot_image = create_yaboot_devpath_resource(opt, + conf, value, &state->desc_image); return; } @@ -135,32 +198,35 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name, if (suse_fp) { /* First finish any previous image. */ - - if (opt->boot_image_file) + if (opt) yaboot_finish(conf); /* Then start the new image. */ + opt = state_start_new_option(conf, state); if (*value == '/') { - opt->boot_image_file = resolve_path(opt, - value, conf->dc->device->device_path); - state->desc_image = talloc_strdup(opt, value); + opt->boot_image = create_yaboot_devpath_resource(opt, + conf, value, &state->desc_image); } else { - opt->boot_image_file = resolve_path(opt, - suse_fp->image, conf->dc->device->device_path); - state->desc_image = talloc_strdup(opt, - suse_fp->image); - - opt->initrd_file = resolve_path(opt, - suse_fp->initrd, conf->dc->device->device_path); - state->desc_initrd = talloc_asprintf(state, "initrd=%s", - suse_fp->initrd); + char *tmp; + + opt->boot_image = create_yaboot_devpath_resource(opt, + conf, suse_fp->image, + &state->desc_image); + + opt->initrd = create_yaboot_devpath_resource(opt, + conf, suse_fp->initrd, &tmp); + + state->desc_initrd = talloc_asprintf(opt, + "initrd=%s", tmp); + talloc_free(tmp); } return; } - if (!opt->boot_image_file) { + /* all other processing requires an image */ + if (!opt) { pb_log("%s: unknown name: %s\n", __func__, name); return; } @@ -168,86 +234,56 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name, /* initrd */ if (streq(name, "initrd")) { - const char *g_boot = conf_get_global_option(conf, "boot"); - const char *g_part = conf_get_global_option(conf, "partition"); - - if (g_boot && g_part) { - char* dev = talloc_asprintf(NULL, "%s%s", g_boot, - g_part); - - opt->initrd_file = resolve_path(opt, - value, dev); - state->desc_initrd = talloc_asprintf(state, - "initrd=%s%s", dev, value); - talloc_free(dev); - } else if (g_boot) { - opt->initrd_file = resolve_path(opt, - value, g_boot); - state->desc_initrd = talloc_asprintf(state, - "initrd=%s%s", g_boot, value); - } else { - opt->initrd_file = resolve_path(opt, - value, conf->dc->device->device_path); - state->desc_initrd = talloc_asprintf(state, "initrd=%s", - value); - } + opt->initrd = create_yaboot_devpath_resource(opt, conf, + value, &state->desc_image); + return; } /* label */ if (streq(name, "label")) { - opt->id = talloc_asprintf(opt, "%s#%s", + opt->option->id = talloc_asprintf(opt->option, "%s#%s", conf->dc->device->device->id, value); - opt->name = talloc_strdup(opt, value); + opt->option->name = talloc_strdup(opt->option, value); return; } /* args */ if (streq(name, "append")) { - opt->boot_args = talloc_asprintf_append( - opt->boot_args, "%s ", value); + opt->option->boot_args = talloc_asprintf_append( + opt->option->boot_args, "%s ", value); return; } if (streq(name, "initrd-size")) { - opt->boot_args = talloc_asprintf_append( - opt->boot_args, "ramdisk_size=%s ", value); + state->initrd_size = talloc_strdup(state, value); return; } if (streq(name, "literal")) { - if (*opt->boot_args) { - pb_log("%s: literal over writes '%s'\n", __func__, - opt->boot_args); - talloc_free(opt->boot_args); - } - talloc_asprintf(opt, "%s ", value); + state->literal = talloc_strdup(state, value); return; } if (streq(name, "ramdisk")) { - opt->boot_args = talloc_asprintf_append( - opt->boot_args, "ramdisk=%s ", value); + state->ramdisk = talloc_strdup(state, value); return; } if (streq(name, "read-only")) { - opt->boot_args = talloc_asprintf_append( - opt->boot_args, "ro "); + state->read_only = true; return; } if (streq(name, "read-write")) { - opt->boot_args = talloc_asprintf_append( - opt->boot_args, "rw "); + state->read_write = true; return; } if (streq(name, "root")) { - opt->boot_args = talloc_asprintf_append( - opt->boot_args, "root=%s ", value); + state->root = talloc_strdup(state, value); return; } @@ -255,10 +291,14 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name, } static struct conf_global_option yaboot_global_options[] = { + { .name = "root" }, { .name = "boot" }, { .name = "initrd" }, + { .name = "initrd_size" }, { .name = "partition" }, { .name = "video" }, + { .name = "literal" }, + { .name = "ramdisk" }, { .name = NULL }, }; @@ -312,10 +352,7 @@ static int yaboot_parse(struct discover_context *dc, char *buf, int len) state->known_names = yaboot_known_names; - /* opt is persistent, so must be associated with device */ - - state->opt = discover_boot_option_create(conf->dc, conf->dc->device); - state->opt->option->boot_args = talloc_strdup(state->opt->option, ""); + state->opt = NULL; conf_parse_buf(conf, buf, len); @@ -323,8 +360,12 @@ static int yaboot_parse(struct discover_context *dc, char *buf, int len) return 1; } -struct parser __yaboot_parser = { - .name = "yaboot", - .parse = yaboot_parse, - .filenames = yaboot_conf_files, +static struct parser yaboot_parser = { + .name = "yaboot", + .method = CONF_METHOD_LOCAL_FILE, + .parse = yaboot_parse, + .filenames = yaboot_conf_files, + .resolve_resource = resolve_devpath_resource, }; + +register_parser(yaboot_parser);