]> git.ozlabs.org Git - petitboot/blobdiff - discover/yaboot-parser.c
ui/ncurses: Always provide a key definition for backtab
[petitboot] / discover / yaboot-parser.c
index f2f34c10b18e32636998dfa5e0980821e20736e6..4dd1ea5be9648214f7a14b64b837839723fb9e60 100644 (file)
@@ -1,8 +1,11 @@
-#define _GNU_SOURCE
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "log/log.h"
 #include "talloc/talloc.h"
 
 #include "log/log.h"
 #include "talloc/talloc.h"
 #include "resource.h"
 
 struct yaboot_state {
 #include "resource.h"
 
 struct yaboot_state {
-       char *desc_image;
-       char *desc_initrd;
        int globals_done;
        int globals_done;
-       const char *const *known_names;
 
        /* current option data */
        struct discover_boot_option *opt;
 
        /* 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 *initrd_size;
        const char *literal;
        const char *ramdisk;
@@ -31,12 +35,12 @@ static struct discover_boot_option *state_start_new_option(
                struct conf_context *conf,
                struct yaboot_state *state)
 {
                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->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->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");
@@ -45,18 +49,62 @@ static struct discover_boot_option *state_start_new_option(
        return state->opt;
 }
 
        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;
 static void yaboot_finish(struct conf_context *conf)
 {
        struct yaboot_state *state = conf->parser_info;
-       struct device *dev = conf->dc->device->device;
+       const char *default_label;
        struct boot_option *opt;
 
        struct boot_option *opt;
 
-       if (!state->desc_image) {
-               pb_log("%s: %s: no image found\n", __func__, dev->id);
+       if (!state->opt)
                return;
                return;
-       }
-
-       assert(state->opt);
 
        opt = state->opt->option;
        assert(opt);
 
        opt = state->opt->option;
        assert(opt);
@@ -64,6 +112,13 @@ static void yaboot_finish(struct conf_context *conf)
        assert(opt->boot_args);
 
        /* populate the boot option from state data */
        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);
+       }
+
        if (state->initrd_size) {
                opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
                                        state->initrd_size, opt->boot_args);
        if (state->initrd_size) {
                opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
                                        state->initrd_size, opt->boot_args);
@@ -96,48 +151,21 @@ static void yaboot_finish(struct conf_context *conf)
        }
 
        opt->description = talloc_asprintf(opt, "%s %s %s",
        }
 
        opt->description = talloc_asprintf(opt, "%s %s %s",
-               state->desc_image,
-               (state->desc_initrd ? state->desc_initrd : ""),
+               state->boot_image,
+               (state->initrd ? state->initrd : ""),
                opt->boot_args ? opt->boot_args : "");
 
                opt->boot_args ? opt->boot_args : "");
 
-       talloc_free(state->desc_initrd);
-
        conf_strip_str(opt->boot_args);
        conf_strip_str(opt->description);
 
        conf_strip_str(opt->boot_args);
        conf_strip_str(opt->description);
 
-       discover_context_add_boot_option(conf->dc, state->opt);
-}
+       default_label = conf_get_global_option(conf, "default");
+       if (default_label &&
+                       !strcasecmp(state->opt->option->name, default_label))
+               state->opt->option->is_default = true;
 
 
-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;
+       discover_context_add_boot_option(conf->dc, state->opt);
 }
 
 }
 
-
 static void yaboot_process_pair(struct conf_context *conf, const char *name,
                char *value)
 {
 static void yaboot_process_pair(struct conf_context *conf, const char *name,
                char *value)
 {
@@ -165,14 +193,10 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        if (!state->globals_done && conf_set_global_option(conf, name, value))
                return;
 
        if (!state->globals_done && conf_set_global_option(conf, name, value))
                return;
 
-       if (!conf_param_in_list(state->known_names, name))
-               return;
-
-       state->globals_done = 1;
-
        /* image */
        /* image */
-
        if (streq(name, "image")) {
        if (streq(name, "image")) {
+               /* an image section finishes our global defintions */
+               state->globals_done = 1;
 
                /* First finish any previous image. */
                if (opt)
 
                /* First finish any previous image. */
                if (opt)
@@ -181,8 +205,7 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                /* Then start the new image. */
                opt = state_start_new_option(conf, state);
 
                /* 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);
+               state->boot_image = talloc_strdup(state, value);
 
                return;
        }
 
                return;
        }
@@ -205,21 +228,11 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                opt = state_start_new_option(conf, state);
 
                if (*value == '/') {
                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 {
                } 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;
                }
 
                return;
@@ -227,21 +240,17 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
 
        /* all other processing requires an image */
        if (!opt) {
 
        /* all other processing requires an image */
        if (!opt) {
-               pb_log("%s: unknown name: %s\n", __func__, name);
+               pb_debug("%s: unknown name: %s\n", __func__, name);
                return;
        }
 
        /* initrd */
                return;
        }
 
        /* initrd */
-
        if (streq(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 */
                return;
        }
 
        /* label */
-
        if (streq(name, "label")) {
                opt->option->id = talloc_asprintf(opt->option, "%s#%s",
                        conf->dc->device->device->id, value);
        if (streq(name, "label")) {
                opt->option->id = talloc_asprintf(opt->option, "%s#%s",
                        conf->dc->device->device->id, value);
@@ -250,6 +259,16 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
        }
 
        /* args */
        }
 
        /* 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(
 
        if (streq(name, "append")) {
                opt->option->boot_args = talloc_asprintf_append(
@@ -287,18 +306,19 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
                return;
        }
 
                return;
        }
 
-       pb_log("%s: unknown name: %s\n", __func__, name);
+       pb_debug("%s: unknown name: %s\n", __func__, name);
 }
 
 static struct conf_global_option yaboot_global_options[] = {
        { .name = "root" },
 }
 
 static struct conf_global_option yaboot_global_options[] = {
        { .name = "root" },
-       { .name = "boot" },
+       { .name = "device" },
+       { .name = "partition" },
        { .name = "initrd" },
        { .name = "initrd_size" },
        { .name = "initrd" },
        { .name = "initrd_size" },
-       { .name = "partition" },
        { .name = "video" },
        { .name = "literal" },
        { .name = "ramdisk" },
        { .name = "video" },
        { .name = "literal" },
        { .name = "ramdisk" },
+       { .name = "default" },
        { .name = NULL },
 };
 
        { .name = NULL },
 };
 
@@ -316,31 +336,22 @@ static const char *const yaboot_conf_files[] = {
        NULL
 };
 
        NULL
 };
 
-static const char *yaboot_known_names[] = {
-       "append",
-       "image",
-       "image[64bit]", /* SUSE extension */
-       "image[32bit]", /* SUSE extension */
-       "initrd",
-       "initrd-size",
-       "label",
-       "literal",
-       "ramdisk",
-       "read-only",
-       "read-write",
-       "root",
-       NULL
-};
-
-static int yaboot_parse(struct discover_context *dc, char *buf, int len)
+static int yaboot_parse(struct discover_context *dc)
 {
 {
-       struct conf_context *conf;
+       const char * const *filename;
        struct yaboot_state *state;
        struct yaboot_state *state;
+       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);
 
        if (!conf)
 
        conf = talloc_zero(dc, struct conf_context);
 
        if (!conf)
-               return 0;
+               return -1;
 
        conf->dc = dc;
        conf->global_options = yaboot_global_options,
 
        conf->dc = dc;
        conf->global_options = yaboot_global_options,
@@ -350,21 +361,24 @@ static int yaboot_parse(struct discover_context *dc, char *buf, int len)
        conf->finish = yaboot_finish;
        conf->parser_info = state = talloc_zero(conf, struct yaboot_state);
 
        conf->finish = yaboot_finish;
        conf->parser_info = state = talloc_zero(conf, struct yaboot_state);
 
-       state->known_names = yaboot_known_names;
-
        state->opt = NULL;
 
        state->opt = NULL;
 
-       conf_parse_buf(conf, buf, len);
+       for (filename = yaboot_conf_files; *filename; filename++) {
+               rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
+               if (rc)
+                       continue;
+
+               conf_parse_buf(conf, buf, len);
+               talloc_free(buf);
+       }
 
        talloc_free(conf);
 
        talloc_free(conf);
-       return 1;
+       return 0;
 }
 
 static struct parser yaboot_parser = {
        .name                   = "yaboot",
 }
 
 static struct parser yaboot_parser = {
        .name                   = "yaboot",
-       .method                 = CONF_METHOD_LOCAL_FILE,
        .parse                  = yaboot_parse,
        .parse                  = yaboot_parse,
-       .filenames              = yaboot_conf_files,
        .resolve_resource       = resolve_devpath_resource,
 };
 
        .resolve_resource       = resolve_devpath_resource,
 };