discover: Change parsers to emit resources rather than filenames
[petitboot] / discover / yaboot-parser.c
index 5224bb3dd23ca5162806a226742761552c864b20..0a66e8d588241d69b66a9fc3d1119edce7f3c38a 100644 (file)
@@ -6,16 +6,15 @@
 
 #include "log/log.h"
 #include "talloc/talloc.h"
-#include "pb-protocol/pb-protocol.h"
+#include "types/types.h"
 #include "parser-conf.h"
 #include "parser-utils.h"
-#include "paths.h"
+#include "resource.h"
 
 struct yaboot_state {
-       struct boot_option *opt;
-       const char *desc_image;
+       struct discover_boot_option *opt;
+       char *desc_image;
        char *desc_initrd;
-       int found_suse;
        int globals_done;
        const char *const *known_names;
 };
@@ -23,39 +22,87 @@ struct yaboot_state {
 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__,
-                       conf->dc->device->id);
+               pb_log("%s: %s: no image found\n", __func__, dev->id);
                return;
        }
 
        assert(state->opt);
-       assert(state->opt->name);
-       assert(state->opt->boot_args);
 
-       state->opt->description = talloc_asprintf(state->opt, "%s %s %s",
+       opt = state->opt->option;
+       assert(opt);
+       assert(opt->name);
+       assert(opt->boot_args);
+
+       opt->description = talloc_asprintf(opt, "%s %s %s",
                state->desc_image,
                (state->desc_initrd ? state->desc_initrd : ""),
-               state->opt->boot_args);
+               opt->boot_args);
 
        talloc_free(state->desc_initrd);
        state->desc_initrd = NULL;
 
-       conf_strip_str(state->opt->boot_args);
-       conf_strip_str(state->opt->description);
+       conf_strip_str(opt->boot_args);
+       conf_strip_str(opt->description);
 
        /* opt is persistent, so must be associated with device */
 
-       device_add_boot_option(conf->dc->device, state->opt);
-       state->opt = talloc_zero(conf->dc->device, struct boot_option);
-       state->opt->boot_args = talloc_strdup(state->opt, "");
+       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 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(conf->dc, 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 discover_boot_option *opt = state->opt;
+       struct fixed_pair {
+               const char *image;
+               const char *initrd;
+       };
+       static const struct fixed_pair suse_fp32 = {
+               .image = "/suseboot/vmlinux32",
+               .initrd = "/suseboot/initrd32",
+       };
+       static const struct fixed_pair suse_fp64 = {
+               .image = "/suseboot/vmlinux64",
+               .initrd = "/suseboot/initrd64",
+       };
+       const struct fixed_pair *suse_fp;
 
        /* fixup for bare values */
 
@@ -73,38 +120,57 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        /* image */
 
        if (streq(name, "image")) {
-               if (state->opt->boot_image_file)
+
+               /* First finish any previous image. */
+               if (opt->boot_image)
                        yaboot_finish(conf);
 
-               state->opt->boot_image_file = resolve_path(state->opt, value,
-                       conf->dc->device_path);
-               state->desc_image = talloc_strdup(state->opt, value);
+               /* Then start the new image. */
+               opt->boot_image = create_yaboot_devpath_resource(conf,
+                               value, &state->desc_image);
+
                return;
        }
 
-       if (streq(name, "image[32bit]") || streq(name, "image[64bit]")) {
-               state->found_suse = 1;
+       /* Special processing for SUSE install CD. */
+
+       if (streq(name, "image[32bit]"))
+               suse_fp = &suse_fp32;
+       else if (streq(name, "image[64bit]"))
+               suse_fp = &suse_fp64;
+       else
+               suse_fp = NULL;
 
-               if (state->opt->boot_image_file)
+       if (suse_fp) {
+               /* First finish any previous image. */
+
+               if (opt->boot_image)
                        yaboot_finish(conf);
 
+               /* Then start the new image. */
+
                if (*value == '/') {
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               value, conf->dc->device_path);
-                       state->desc_image = talloc_strdup(state->opt, value);
+                       opt->boot_image = create_yaboot_devpath_resource(
+                                       conf, value, &state->desc_image);
                } else {
-                       char *s;
-                       asprintf(&s, "/suseboot/%s", value);
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               s, conf->dc->device_path);
-                       state->desc_image = talloc_strdup(state->opt, s);
-                       free(s);
+                       char *tmp;
+
+                       opt->boot_image = create_yaboot_devpath_resource(
+                                       conf, suse_fp->image,
+                                       &state->desc_image);
+
+                       opt->initrd = create_yaboot_devpath_resource(
+                                       conf, suse_fp->initrd, &tmp);
+
+                       state->desc_initrd = talloc_asprintf(opt,
+                               "initrd=%s", tmp);
+                       talloc_free(tmp);
                }
 
                return;
        }
 
-       if (!state->opt->boot_image_file) {
+       if (!opt->boot_image) {
                pb_log("%s: unknown name: %s\n", __func__, name);
                return;
        }
@@ -112,77 +178,66 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        /* initrd */
 
        if (streq(name, "initrd")) {
-               if (!state->found_suse || (*value == '/')) {
-                       state->opt->initrd_file = resolve_path(state->opt,
-                               value, conf->dc->device_path);
-                       state->desc_initrd = talloc_asprintf(state, "initrd=%s",
-                               value);
-               } else {
-                       char *s;
-                       asprintf(&s, "/suseboot/%s", value);
-                       state->opt->initrd_file = resolve_path(state->opt,
-                               s, conf->dc->device_path);
-                       state->desc_initrd = talloc_asprintf(state, "initrd=%s",
-                               s);
-                       free(s);
-               }
+               opt->initrd = create_yaboot_devpath_resource(conf,
+                               value, &state->desc_image);
+
                return;
        }
 
        /* label */
 
        if (streq(name, "label")) {
-               state->opt->id = talloc_asprintf(state->opt, "%s#%s",
-                       conf->dc->device->id, value);
-               state->opt->name = talloc_strdup(state->opt, value);
+               opt->option->id = talloc_asprintf(opt->option, "%s#%s",
+                       conf->dc->device->device->id, value);
+               opt->option->name = talloc_strdup(opt->option, value);
                return;
        }
 
        /* args */
 
        if (streq(name, "append")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "%s ", value);
+               opt->option->boot_args = talloc_asprintf_append(
+                       opt->option->boot_args, "%s ", value);
                return;
        }
 
        if (streq(name, "initrd-size")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "ramdisk_size=%s ", value);
+               opt->option->boot_args = talloc_asprintf_append(
+                       opt->option->boot_args, "ramdisk_size=%s ", value);
                return;
        }
 
        if (streq(name, "literal")) {
-               if (*state->opt->boot_args) {
+               if (*opt->option->boot_args) {
                        pb_log("%s: literal over writes '%s'\n", __func__,
-                               state->opt->boot_args);
-                       talloc_free(state->opt->boot_args);
+                               opt->option->boot_args);
+                       talloc_free(opt->option->boot_args);
                }
-               talloc_asprintf(state->opt, "%s ", value);
+               talloc_asprintf(opt->option, "%s ", value);
                return;
        }
 
        if (streq(name, "ramdisk")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "ramdisk=%s ", value);
+               opt->option->boot_args = talloc_asprintf_append(
+                       opt->option->boot_args, "ramdisk=%s ", value);
                return;
        }
 
        if (streq(name, "read-only")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "ro ");
+               opt->option->boot_args = talloc_asprintf_append(
+                       opt->option->boot_args, "ro ");
                return;
        }
 
        if (streq(name, "read-write")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "rw ");
+               opt->option->boot_args = talloc_asprintf_append(
+                       opt->option->boot_args, "rw ");
                return;
        }
 
        if (streq(name, "root")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "root=%s ", value);
+               opt->option->boot_args = talloc_asprintf_append(
+                       opt->option->boot_args, "root=%s ", value);
                return;
        }
 
@@ -227,21 +282,20 @@ static const char *yaboot_known_names[] = {
        NULL
 };
 
-static int yaboot_parse(struct discover_context *dc)
+static int yaboot_parse(struct discover_context *dc, char *buf, int len)
 {
        struct conf_context *conf;
        struct yaboot_state *state;
-       int rc;
 
        conf = talloc_zero(dc, struct conf_context);
 
        if (!conf)
-               return -1;
+               return 0;
 
        conf->dc = dc;
        conf->global_options = yaboot_global_options,
        conf_init_global_options(conf);
-       conf->conf_files = yaboot_conf_files,
+       conf->get_pair = conf_get_pair_equal;
        conf->process_pair = yaboot_process_pair;
        conf->finish = yaboot_finish;
        conf->parser_info = state = talloc_zero(conf, struct yaboot_state);
@@ -250,13 +304,17 @@ static int yaboot_parse(struct discover_context *dc)
 
        /* opt is persistent, so must be associated with device */
 
-       state->opt = talloc_zero(conf->dc->device, struct boot_option);
-       state->opt->boot_args = talloc_strdup(state->opt, "");
+       state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
+       state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
 
-       rc = conf_parse(conf);
+       conf_parse_buf(conf, buf, len);
 
        talloc_free(conf);
-       return rc;
+       return 1;
 }
 
-define_parser(yaboot, 99, yaboot_parse);
+struct parser __yaboot_parser = {
+       .name           = "yaboot",
+       .parse          = yaboot_parse,
+       .filenames      = yaboot_conf_files,
+};