discover/yaboot: Allow all image options to be overridden by global options
authorJeremy Kerr <jk@ozlabs.org>
Wed, 7 Aug 2013 02:31:05 +0000 (10:31 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Wed, 7 Aug 2013 02:31:05 +0000 (10:31 +0800)
All of the image options should be overridable by global options.

Instead of building the boot_args during yaboot_process_pair, we add
discovered data into the state struct, then create the boot args from
this data during yaboot_finish

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/yaboot-parser.c
test/parser/Makefile.am
test/parser/test-yaboot-root-override.c [new file with mode: 0644]

index 39fba63055b12f08332e88ddfc30e899306d8050..f2f34c10b18e32636998dfa5e0980821e20736e6 100644 (file)
 #include "resource.h"
 
 struct yaboot_state {
 #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;
        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;
 static void yaboot_finish(struct conf_context *conf)
 {
        struct yaboot_state *state = conf->parser_info;
@@ -37,13 +63,44 @@ static void yaboot_finish(struct conf_context *conf)
        assert(opt->name);
        assert(opt->boot_args);
 
        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->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);
 
        talloc_free(state->desc_initrd);
-       state->desc_initrd = NULL;
 
        conf_strip_str(opt->boot_args);
        conf_strip_str(opt->description);
 
        conf_strip_str(opt->boot_args);
        conf_strip_str(opt->description);
@@ -121,15 +178,12 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                if (opt)
                        yaboot_finish(conf);
 
                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. */
                /* Then start the new image. */
+               opt = state_start_new_option(conf, state);
+
                opt->boot_image = create_yaboot_devpath_resource(opt,
                                conf, value, &state->desc_image);
 
                opt->boot_image = create_yaboot_devpath_resource(opt,
                                conf, value, &state->desc_image);
 
-               state->opt = opt;
-
                return;
        }
 
                return;
        }
 
@@ -148,11 +202,7 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                        yaboot_finish(conf);
 
                /* Then start the new image. */
                        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,
 
                if (*value == '/') {
                        opt->boot_image = create_yaboot_devpath_resource(opt,
@@ -208,42 +258,32 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        }
 
        if (streq(name, "initrd-size")) {
        }
 
        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")) {
                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")) {
                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")) {
                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")) {
                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")) {
                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;
        }
 
                return;
        }
 
@@ -251,10 +291,14 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
 }
 
 static struct conf_global_option yaboot_global_options[] = {
 }
 
 static struct conf_global_option yaboot_global_options[] = {
+       { .name = "root" },
        { .name = "boot" },
        { .name = "initrd" },
        { .name = "boot" },
        { .name = "initrd" },
+       { .name = "initrd_size" },
        { .name = "partition" },
        { .name = "video" },
        { .name = "partition" },
        { .name = "video" },
+       { .name = "literal" },
+       { .name = "ramdisk" },
        { .name = NULL },
 };
 
        { .name = NULL },
 };
 
index bb45a40f7939ea20d6b9ede35242eeb75eb425d9..4319705c576163aa48da7fef0b9d18ac1f850a1c 100644 (file)
@@ -35,6 +35,7 @@ TESTS = \
        test-yaboot-single \
        test-yaboot-external \
        test-yaboot-root-global \
        test-yaboot-single \
        test-yaboot-external \
        test-yaboot-root-global \
+       test-yaboot-root-override \
        test-yaboot-rh8-ppc64 \
        test-pxe-single \
        test-pxe-initrd-in-append
        test-yaboot-rh8-ppc64 \
        test-pxe-single \
        test-pxe-initrd-in-append
diff --git a/test/parser/test-yaboot-root-override.c b/test/parser/test-yaboot-root-override.c
new file mode 100644 (file)
index 0000000..0eea3d4
--- /dev/null
@@ -0,0 +1,35 @@
+
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+default=linux
+root=/dev/sda1
+
+image=/vmlinux
+       label=linux 1
+
+image=/vmlinux
+       label=linux 2
+       root=/dev/sda2
+#endif
+
+void run_test(struct parser_test *test)
+{
+       struct discover_boot_option *opt;
+       struct discover_context *ctx;
+
+       test_read_conf_embedded(test);
+       test_run_parser(test, "yaboot");
+
+       ctx = test->ctx;
+
+       check_boot_option_count(ctx, 2);
+
+       opt = get_boot_option(ctx, 0);
+       check_name(opt, "linux 1");
+       check_args(opt, "root=/dev/sda1");
+
+       opt = get_boot_option(ctx, 1);
+       check_name(opt, "linux 2");
+       check_args(opt, "root=/dev/sda2");
+}