]> git.ozlabs.org Git - petitboot/blobdiff - discover/yaboot-parser.c
discover/yaboot: fix device parsing
[petitboot] / discover / yaboot-parser.c
index 39fba63055b12f08332e88ddfc30e899306d8050..81ab83d548b9e8e93979500a6bbce9bc792954f2 100644 (file)
 #include "resource.h"
 
 struct yaboot_state {
-       struct discover_boot_option *opt;
-       char *desc_image;
-       char *desc_initrd;
        int globals_done;
        const char *const *known_names;
+
+       /* current option data */
+       struct discover_boot_option *opt;
+       const char *device;
+       const char *partition;
+       const char *boot_image;
+       const char *initrd;
+       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->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->device = conf_get_global_option(conf, "device");
+       state->partition = conf_get_global_option(conf, "partition");
+       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 struct resource *create_yaboot_devpath_resource(
+               struct yaboot_state *state,
+               struct conf_context *conf,
+               const char *path)
+{
+       struct discover_boot_option *opt = state->opt;
+       const char *dev, *part;
+       struct resource *res;
+       char *devpath;
+
+       dev = state->device;
+       part = state->partition;
+
+       if (!dev)
+               dev = conf_get_global_option(conf, "device");
+       if (!part)
+               part = conf_get_global_option(conf, "partition");
+
+       if (strchr(path, ':')) {
+               devpath = talloc_strdup(conf, path);
+
+       } else if (dev && part) {
+               devpath = talloc_asprintf(conf,
+                               "%s%s:%s", dev, part, path);
+       } else if (dev) {
+               devpath = talloc_asprintf(conf, "%s:%s", dev, path);
+       } else {
+               devpath = talloc_strdup(conf, path);
+       }
+
+       res = create_devpath_resource(opt, conf->dc->device, devpath);
+
+       talloc_free(devpath);
+
+       return res;
+}
+
 static void yaboot_finish(struct conf_context *conf)
 {
        struct yaboot_state *state = conf->parser_info;
-       struct device *dev = conf->dc->device->device;
        struct boot_option *opt;
 
-       if (!state->desc_image) {
-               pb_log("%s: %s: no image found\n", __func__, dev->id);
-               return;
-       }
-
        assert(state->opt);
 
        opt = state->opt->option;
@@ -37,49 +96,55 @@ static void yaboot_finish(struct conf_context *conf)
        assert(opt->name);
        assert(opt->boot_args);
 
-       opt->description = talloc_asprintf(opt, "%s %s %s",
-               state->desc_image,
-               (state->desc_initrd ? state->desc_initrd : ""),
-               opt->boot_args);
+       /* populate the boot option from state data */
+       state->opt->boot_image = create_yaboot_devpath_resource(state,
+                               conf, state->boot_image);
+       if (state->initrd) {
+               state->opt->initrd = create_yaboot_devpath_resource(state,
+                               conf, state->initrd);
+       }
 
-       talloc_free(state->desc_initrd);
-       state->desc_initrd = NULL;
+       if (state->initrd_size) {
+               opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
+                                       state->initrd_size, opt->boot_args);
+       }
 
-       conf_strip_str(opt->boot_args);
-       conf_strip_str(opt->description);
+       if (state->ramdisk) {
+               opt->boot_args = talloc_asprintf(opt, "ramdisk=%s %s",
+                                       state->initrd_size, opt->boot_args);
+       }
 
-       discover_context_add_boot_option(conf->dc, state->opt);
-}
+       if (state->root) {
+               opt->boot_args = talloc_asprintf(opt, "root=%s %s",
+                                       state->root, opt->boot_args);
+       }
 
-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 (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 (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);
+       if (state->read_only || state->read_write) {
+               opt->boot_args = talloc_asprintf(opt, "%s %s",
+                                       state->read_only ? "ro" : "rw",
+                                       opt->boot_args);
        }
 
-       res = create_devpath_resource(opt, conf->dc->device, devpath);
+       if (state->literal) {
+               opt->boot_args = talloc_strdup(opt, state->literal);
+       }
 
-       if (desc_str)
-               *desc_str = devpath;
-       else
-               talloc_free(devpath);
+       opt->description = talloc_asprintf(opt, "%s %s %s",
+               state->boot_image,
+               (state->initrd ? state->initrd : ""),
+               opt->boot_args ? opt->boot_args : "");
 
-       return res;
-}
+       conf_strip_str(opt->boot_args);
+       conf_strip_str(opt->description);
 
+       discover_context_add_boot_option(conf->dc, state->opt);
+}
 
 static void yaboot_process_pair(struct conf_context *conf, const char *name,
                char *value)
@@ -121,14 +186,10 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                if (opt)
                        yaboot_finish(conf);
 
-               opt = discover_boot_option_create(conf->dc, conf->dc->device);
-               opt->option->boot_args = talloc_strdup(opt->option, "");
-
                /* Then start the new image. */
-               opt->boot_image = create_yaboot_devpath_resource(opt,
-                               conf, value, &state->desc_image);
+               opt = state_start_new_option(conf, state);
 
-               state->opt = opt;
+               state->boot_image = talloc_strdup(state, value);
 
                return;
        }
@@ -148,28 +209,14 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                        yaboot_finish(conf);
 
                /* Then start the new image. */
-
-               opt = discover_boot_option_create(conf->dc, conf->dc->device);
-               opt->option->boot_args = talloc_strdup(opt->option, "");
-
-               state->opt = opt;
+               opt = state_start_new_option(conf, state);
 
                if (*value == '/') {
-                       opt->boot_image = create_yaboot_devpath_resource(opt,
-                                       conf, value, &state->desc_image);
+                       state->boot_image = talloc_strdup(state, value);
                } else {
-                       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);
+                       state->boot_image = talloc_strdup(state,
+                                                       suse_fp->image);
+                       state->initrd = talloc_strdup(state, suse_fp->initrd);
                }
 
                return;
@@ -182,16 +229,12 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        }
 
        /* initrd */
-
        if (streq(name, "initrd")) {
-               opt->initrd = create_yaboot_devpath_resource(opt, conf,
-                               value, &state->desc_image);
-
+               state->initrd = talloc_strdup(state, value);
                return;
        }
 
        /* label */
-
        if (streq(name, "label")) {
                opt->option->id = talloc_asprintf(opt->option, "%s#%s",
                        conf->dc->device->device->id, value);
@@ -200,6 +243,16 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        }
 
        /* args */
+       if (streq(name, "device")) {
+               printf("option device : %s", value);
+               state->device = talloc_strdup(state, value);
+               return;
+       }
+
+       if (streq(name, "parititon")) {
+               state->partition = talloc_strdup(state, value);
+               return;
+       }
 
        if (streq(name, "append")) {
                opt->option->boot_args = talloc_asprintf_append(
@@ -208,42 +261,32 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        }
 
        if (streq(name, "initrd-size")) {
-               opt->option->boot_args = talloc_asprintf_append(
-                       opt->option->boot_args, "ramdisk_size=%s ", value);
+               state->initrd_size = talloc_strdup(state, value);
                return;
        }
 
        if (streq(name, "literal")) {
-               if (*opt->option->boot_args) {
-                       pb_log("%s: literal over writes '%s'\n", __func__,
-                               opt->option->boot_args);
-                       talloc_free(opt->option->boot_args);
-               }
-               talloc_asprintf(opt->option, "%s ", value);
+               state->literal = talloc_strdup(state, value);
                return;
        }
 
        if (streq(name, "ramdisk")) {
-               opt->option->boot_args = talloc_asprintf_append(
-                       opt->option->boot_args, "ramdisk=%s ", value);
+               state->ramdisk = talloc_strdup(state, value);
                return;
        }
 
        if (streq(name, "read-only")) {
-               opt->option->boot_args = talloc_asprintf_append(
-                       opt->option->boot_args, "ro ");
+               state->read_only = true;
                return;
        }
 
        if (streq(name, "read-write")) {
-               opt->option->boot_args = talloc_asprintf_append(
-                       opt->option->boot_args, "rw ");
+               state->read_write = true;
                return;
        }
 
        if (streq(name, "root")) {
-               opt->option->boot_args = talloc_asprintf_append(
-                       opt->option->boot_args, "root=%s ", value);
+               state->root = talloc_strdup(state, value);
                return;
        }
 
@@ -251,10 +294,14 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
 }
 
 static struct conf_global_option yaboot_global_options[] = {
-       { .name = "boot" },
-       { .name = "initrd" },
+       { .name = "root" },
+       { .name = "device" },
        { .name = "partition" },
+       { .name = "initrd" },
+       { .name = "initrd_size" },
        { .name = "video" },
+       { .name = "literal" },
+       { .name = "ramdisk" },
        { .name = NULL },
 };
 
@@ -285,6 +332,8 @@ static const char *yaboot_known_names[] = {
        "read-only",
        "read-write",
        "root",
+       "device",
+       "partition",
        NULL
 };