discover/grub: Use different paths to search for the BLS directory
[petitboot] / discover / yaboot-parser.c
index 816e0c4a91f941b542791bd31f74184dcdca5288..b06248f535f35eb0c05b3e2bf2437a1516cafbdf 100644 (file)
@@ -1,8 +1,12 @@
-#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 <i18n/i18n.h>
 
 #include "log/log.h"
 #include "talloc/talloc.h"
 #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, *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;
-       struct device *dev = conf->dc->device->device;
+       const char *default_label;
        struct boot_option *opt;
 
-       if (!state->desc_image) {
-               pb_log("%s: %s: no image found\n", __func__, dev->id);
+       if (!state->opt)
                return;
-       }
-
-       assert(state->opt);
 
        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 : ""),
-               opt->boot_args);
+       /* populate the boot option from state data */
+       state->opt->boot_image = create_yaboot_devpath_resource(state,
+                               conf, state->boot_image);
 
-       talloc_free(state->desc_initrd);
-       state->desc_initrd = NULL;
+       char* args_sigfile_default = talloc_asprintf(opt,
+               "%s.cmdline.sig", state->boot_image);
+       state->opt->args_sig_file = create_yaboot_devpath_resource(state,
+                               conf, args_sigfile_default);
+       talloc_free(args_sigfile_default);
 
-       conf_strip_str(opt->boot_args);
-       conf_strip_str(opt->description);
+       if (state->initrd) {
+               state->opt->initrd = create_yaboot_devpath_resource(state,
+                               conf, state->initrd);
+       }
 
-       /* opt is persistent, so must be associated with device */
+       if (state->initrd_size) {
+               opt->boot_args = talloc_asprintf(opt, "ramdisk_size=%s %s",
+                                       state->initrd_size, opt->boot_args);
+       }
 
-       discover_context_add_boot_option(conf->dc, state->opt);
+       if (state->ramdisk) {
+               opt->boot_args = talloc_asprintf(opt, "ramdisk=%s %s",
+                                       state->initrd_size, opt->boot_args);
+       }
 
-       state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
-       state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
-}
+       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 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(conf->dc, 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);
 
+       default_label = conf_get_global_option(conf, "default");
+       if (default_label &&
+                       !strcasecmp(state->opt->option->name, default_label))
+               state->opt->option->is_default = true;
+
+       discover_context_add_boot_option(conf->dc, state->opt);
+       state->opt = NULL;
+}
 
 static void yaboot_process_pair(struct conf_context *conf, const char *name,
                char *value)
@@ -112,22 +202,19 @@ 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 (!conf_param_in_list(state->known_names, name))
-               return;
-
-       state->globals_done = 1;
-
        /* image */
-
        if (streq(name, "image")) {
+               /* an image section finishes our global defintions */
+               state->globals_done = 1;
 
                /* First finish any previous image. */
-               if (opt->boot_image)
+               if (opt)
                        yaboot_finish(conf);
 
                /* Then start the new image. */
-               opt->boot_image = create_yaboot_devpath_resource(conf,
-                               value, &state->desc_image);
+               opt = state_start_new_option(conf, state);
+
+               state->boot_image = talloc_strdup(state, value);
 
                return;
        }
@@ -143,49 +230,36 @@ static void yaboot_process_pair(struct conf_context *conf, const char *name,
 
        if (suse_fp) {
                /* First finish any previous image. */
-
-               if (opt->boot_image)
+               if (opt)
                        yaboot_finish(conf);
 
                /* Then start the new image. */
+               opt = state_start_new_option(conf, state);
 
                if (*value == '/') {
-                       opt->boot_image = create_yaboot_devpath_resource(
-                                       conf, value, &state->desc_image);
+                       state->boot_image = talloc_strdup(state, value);
                } else {
-                       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);
+                       state->boot_image = talloc_strdup(state,
+                                                       suse_fp->image);
+                       state->initrd = talloc_strdup(state, suse_fp->initrd);
                }
 
                return;
        }
 
-       if (!opt->boot_image) {
-               pb_log("%s: unknown name: %s\n", __func__, name);
+       /* all other processing requires an image */
+       if (!opt) {
+               pb_debug("%s: unknown name: %s\n", __func__, name);
                return;
        }
 
        /* initrd */
-
        if (streq(name, "initrd")) {
-               opt->initrd = create_yaboot_devpath_resource(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);
@@ -194,6 +268,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(
@@ -202,95 +286,85 @@ 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;
        }
 
-       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 = "boot" },
-       { .name = "initrd" },
+       { .name = "root" },
+       { .name = "device" },
        { .name = "partition" },
+       { .name = "initrd" },
+       { .name = "initrd_size" },
        { .name = "video" },
+       { .name = "literal" },
+       { .name = "ramdisk" },
+       { .name = "default" },
        { .name = NULL },
 };
 
 static const char *const yaboot_conf_files[] = {
        "/yaboot.conf",
        "/yaboot.cnf",
+       "/etc/lilo.conf",
+       "/etc/silo.conf",
        "/etc/yaboot.conf",
        "/etc/yaboot.cnf",
        "/suseboot/yaboot.cnf",
        "/YABOOT.CONF",
        "/YABOOT.CNF",
+       "/ETC/LILO.CONF",
+       "/ETC/SILO.CONF",
        "/ETC/YABOOT.CONF",
        "/ETC/YABOOT.CNF",
        "/SUSEBOOT/YABOOT.CNF",
        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 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)
-               return 0;
+               return -1;
 
        conf->dc = dc;
        conf->global_options = yaboot_global_options,
@@ -300,24 +374,28 @@ 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);
 
-       state->known_names = yaboot_known_names;
-
-       /* opt is persistent, so must be associated with device */
+       state->opt = NULL;
 
-       state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
-       state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
+       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);
+               conf_parse_buf(conf, buf, len);
+               device_handler_status_dev_info(dc->handler, dc->device,
+                               _("Parsed yaboot configuration from %s"),
+                               *filename);
+               talloc_free(buf);
+       }
 
        talloc_free(conf);
-       return 1;
+       return 0;
 }
 
 static struct parser yaboot_parser = {
-       .name           = "yaboot",
-       .method         = CONF_METHOD_LOCAL_FILE,
-       .parse          = yaboot_parse,
-       .filenames      = yaboot_conf_files,
+       .name                   = "yaboot",
+       .parse                  = yaboot_parse,
+       .resolve_resource       = resolve_devpath_resource,
 };
 
 register_parser(yaboot_parser);