discover/yaboot: Clear globals_done when we see an image definition
[petitboot] / discover / yaboot-parser.c
index 1000505b9681df66da2bfff4e17709d503c0175e..eaefbf0041572cfbcd7dd3fdf3a376d7bd657718 100644 (file)
-#define _GNU_SOURCE
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 #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;
-       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, *devpos;
+       struct resource *res;
+       char *devpath, *devstr;
+
+       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) {
+               devpos = &dev[strlen(dev) - 1];
+               if (isdigit(*devpos)) {
+                       while (isdigit(*devpos))
+                               devpos--;
+
+                       devstr = talloc_strndup(conf, dev, devpos - dev + 1);
+                       devpath = talloc_asprintf(conf, "%s%s:%s", devstr,
+                                       part, path);
+                       talloc_free(devstr);
+               } else {
+                       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;
+       const char *default_label;
+       struct boot_option *opt;
 
-       if (!state->desc_image) {
-               pb_log("%s: %s: no image found\n", __func__,
-                       conf->dc->device->id);
+       if (!state->opt)
                return;
+
+       opt = state->opt->option;
+       assert(opt);
+       assert(opt->name);
+       assert(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);
        }
 
-       assert(state->opt);
-       assert(state->opt->name);
-       assert(state->opt->boot_args);
+       if (state->initrd_size) {
+               opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
+                                       state->initrd_size, opt->boot_args);
+       }
 
-       state->opt->description = talloc_asprintf(state->opt, "%s %s %s",
-               state->desc_image,
-               (state->desc_initrd ? state->desc_initrd : ""),
-               state->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);
+       }
 
-       talloc_free(state->desc_initrd);
-       state->desc_initrd = NULL;
+       opt->description = talloc_asprintf(opt, "%s %s %s",
+               state->boot_image,
+               (state->initrd ? state->initrd : ""),
+               opt->boot_args ? opt->boot_args : "");
 
-       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 */
+       default_label = conf_get_global_option(conf, "default");
+       if (default_label &&
+                       !strcasecmp(state->opt->option->name, default_label))
+               state->opt->option->is_default = true;
 
-       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);
 }
 
 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;
@@ -80,40 +197,19 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        if (!conf_param_in_list(state->known_names, name))
                return;
 
-       state->globals_done = 1;
-
        /* 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");
+               /* an image section finishes our global defintions */
+               state->globals_done = 1;
 
                /* First finish any previous image. */
-
-               if (state->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);
-
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               value, dev);
-                       state->desc_image = talloc_asprintf(state->opt,
-                               "%s%s", dev, value);
-                       talloc_free(dev);
-               } else if (g_boot) {
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               value, g_boot);
-                       state->desc_image = talloc_asprintf(state->opt,
-                               "%s%s", g_boot, value);
-               } else {
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               value, conf->dc->device_path);
-                       state->desc_image = talloc_strdup(state->opt, value);
-               }
+               state->boot_image = talloc_strdup(state, value);
 
                return;
        }
@@ -129,119 +225,88 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
 
        if (suse_fp) {
                /* First finish any previous image. */
-
-               if (state->opt->boot_image_file)
+               if (opt)
                        yaboot_finish(conf);
 
                /* Then start the new image. */
+               opt = state_start_new_option(conf, state);
 
                if (*value == '/') {
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               value, conf->dc->device_path);
-                       state->desc_image = talloc_strdup(state->opt, value);
+                       state->boot_image = talloc_strdup(state, value);
                } else {
-                       state->opt->boot_image_file = resolve_path(state->opt,
-                               suse_fp->image, conf->dc->device_path);
-                       state->desc_image = talloc_strdup(state->opt,
-                               suse_fp->image);
-
-                       state->opt->initrd_file = resolve_path(state->opt,
-                               suse_fp->initrd, conf->dc->device_path);
-                       state->desc_initrd = talloc_asprintf(state, "initrd=%s",
-                               suse_fp->initrd);
+                       state->boot_image = talloc_strdup(state,
+                                                       suse_fp->image);
+                       state->initrd = talloc_strdup(state, suse_fp->initrd);
                }
 
                return;
        }
 
-       if (!state->opt->boot_image_file) {
+       /* all other processing requires an image */
+       if (!opt) {
                pb_log("%s: unknown name: %s\n", __func__, name);
                return;
        }
 
        /* 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);
-
-                       state->opt->initrd_file = resolve_path(state->opt,
-                               value, dev);
-                       state->desc_initrd = talloc_asprintf(state,
-                               "initrd=%s%s", dev, value);
-                       talloc_free(dev);
-               } else if (g_boot) {
-                       state->opt->initrd_file = resolve_path(state->opt,
-                               value, g_boot);
-                       state->desc_initrd = talloc_asprintf(state,
-                               "initrd=%s%s", g_boot, value);
-               } else {
-                       state->opt->initrd_file = resolve_path(state->opt,
-                               value, conf->dc->device_path);
-                       state->desc_initrd = talloc_asprintf(state, "initrd=%s",
-                               value);
-               }
+               state->initrd = talloc_strdup(state, value);
                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, "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")) {
-               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);
+               state->initrd_size = talloc_strdup(state, value);
                return;
        }
 
        if (streq(name, "literal")) {
-               if (*state->opt->boot_args) {
-                       pb_log("%s: literal over writes '%s'\n", __func__,
-                               state->opt->boot_args);
-                       talloc_free(state->opt->boot_args);
-               }
-               talloc_asprintf(state->opt, "%s ", value);
+               state->literal = talloc_strdup(state, value);
                return;
        }
 
        if (streq(name, "ramdisk")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "ramdisk=%s ", value);
+               state->ramdisk = talloc_strdup(state, value);
                return;
        }
 
        if (streq(name, "read-only")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "ro ");
+               state->read_only = true;
                return;
        }
 
        if (streq(name, "read-write")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "rw ");
+               state->read_write = true;
                return;
        }
 
        if (streq(name, "root")) {
-               state->opt->boot_args = talloc_asprintf_append(
-                       state->opt->boot_args, "root=%s ", value);
+               state->root = talloc_strdup(state, value);
                return;
        }
 
@@ -249,10 +314,15 @@ 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 = "default" },
        { .name = NULL },
 };
 
@@ -283,14 +353,22 @@ static const char *yaboot_known_names[] = {
        "read-only",
        "read-write",
        "root",
+       "device",
+       "partition",
        NULL
 };
 
 static int yaboot_parse(struct discover_context *dc)
 {
-       struct conf_context *conf;
+       const char * const *filename;
        struct yaboot_state *state;
-       int rc;
+       struct conf_context *conf;
+       int len, rc;
+       char *buf;
+
+       /* Support block device boot only at present */
+       if (dc->event)
+               return -1;
 
        conf = talloc_zero(dc, struct conf_context);
 
@@ -300,22 +378,32 @@ static int yaboot_parse(struct discover_context *dc)
        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);
 
        state->known_names = yaboot_known_names;
 
-       /* opt is persistent, so must be associated with device */
+       state->opt = NULL;
 
-       state->opt = talloc_zero(conf->dc->device, struct boot_option);
-       state->opt->boot_args = talloc_strdup(state->opt, "");
+       for (filename = yaboot_conf_files; *filename; filename++) {
+               rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
+               if (rc)
+                       continue;
 
-       rc = conf_parse(conf);
+               conf_parse_buf(conf, buf, len);
+               talloc_free(buf);
+       }
 
        talloc_free(conf);
-       return rc;
+       return 0;
 }
 
-define_parser(yaboot, 99, yaboot_parse);
+static struct parser yaboot_parser = {
+       .name                   = "yaboot",
+       .parse                  = yaboot_parse,
+       .resolve_resource       = resolve_devpath_resource,
+};
+
+register_parser(yaboot_parser);