parser/grub2: Better menuentry parsing
[petitboot] / discover / grub2-parser.c
index 97b96537482f7b1bc83f31855f052e4ea041138b..bd08ec0a70c750f90d91f4c609bd427125ceb594 100644 (file)
 #include "types/types.h"
 #include "parser-conf.h"
 #include "parser-utils.h"
+#include "paths.h"
 #include "resource.h"
 
+struct grub2_root {
+       char *uuid;
+};
+
 struct grub2_state {
        struct discover_boot_option *opt;
        char *desc_image;
        char *desc_initrd;
        const char *const *known_names;
+       struct grub2_root *root;
 };
 
+struct grub2_resource_info {
+       struct grub2_root *root;
+       char *path;
+};
+
+/* we use slightly different resources for grub2 */
+static struct resource *create_grub2_resource(void *ctx,
+               struct discover_device *orig_device,
+               struct grub2_root *root, const char *path)
+{
+       struct grub2_resource_info *info;
+       struct resource *res;
+
+       res = talloc(ctx, struct resource);
+
+       if (root) {
+               info = talloc(res, struct grub2_resource_info);
+               info->root = root;
+               talloc_reference(info, root);
+               info->path = talloc_strdup(info, path);
+
+               res->resolved = false;
+               res->info = info;
+
+       } else
+               resolve_resource_against_device(res, orig_device, path);
+
+       return res;
+}
+
+static bool resolve_grub2_resource(struct device_handler *handler,
+               struct resource *res)
+{
+       struct grub2_resource_info *info = res->info;
+       struct discover_device *dev;
+
+       assert(!res->resolved);
+
+       dev = device_lookup_by_uuid(handler, info->root->uuid);
+
+       if (!dev)
+               return false;
+
+       resolve_resource_against_device(res, dev, info->path);
+       talloc_free(info);
+
+       return true;
+}
+
 static void grub2_finish(struct conf_context *conf)
 {
        struct device *dev = conf->dc->device->device;
@@ -72,12 +127,7 @@ static void grub2_finish(struct conf_context *conf)
 
        discover_context_add_boot_option(conf->dc, state->opt);
 
-       state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
-       opt = state->opt->option;
-       opt->boot_args = talloc_strdup(opt, "");
-
-       talloc_free(state->desc_image);
-       state->desc_image = NULL;
+       state->opt = NULL;
 }
 
 static void grub2_process_pair(struct conf_context *conf, const char *name,
@@ -91,20 +141,22 @@ static void grub2_process_pair(struct conf_context *conf, const char *name,
                return;
 
        if (streq(name, "menuentry")) {
-               char *sep;
-
-               grub2_finish(conf);
-
-               /* Then start the new image. */
+               /* complete any existing option... */
+               if (state->opt)
+                       grub2_finish(conf);
 
-               sep = strchr(value, '\'');
+               /* ... then start the new one */
+               opt = discover_boot_option_create(conf->dc, conf->dc->device);
+               opt->option->boot_args = talloc_strdup(opt->option, "");
 
-               if (sep)
-                       *sep = 0;
+               value = strtok(value, "\'{\"");
 
                opt->option->id = talloc_asprintf(opt->option,
                                        "%s#%s", dev->id, value);
                opt->option->name = talloc_strdup(opt->option, value);
+               opt->option->boot_args = talloc_strdup(opt, "");
+
+               state->opt = opt;
 
                return;
        }
@@ -117,8 +169,8 @@ static void grub2_process_pair(struct conf_context *conf, const char *name,
                if (sep)
                        *sep = 0;
 
-               opt->boot_image = create_devpath_resource(opt,
-                                       conf->dc->device, value);
+               opt->boot_image = create_grub2_resource(opt, conf->dc->device,
+                                       state->root, value);
                state->desc_image = talloc_strdup(opt, value);
 
                if (sep)
@@ -128,13 +180,42 @@ static void grub2_process_pair(struct conf_context *conf, const char *name,
        }
 
        if (streq(name, "initrd")) {
-               opt->initrd = create_devpath_resource(opt,
-                                       conf->dc->device, value);
+               opt->initrd = create_grub2_resource(opt, conf->dc->device,
+                                       state->root, value);
                state->desc_initrd = talloc_asprintf(state, "initrd=%s",
                        value);
                return;
        }
 
+       if (streq(name, "search")) {
+               struct grub2_root *root;
+               char *uuid;
+
+               if (!strstr(value, "--set=root")) {
+                       pb_log("%s: no root\n", __func__);
+                       return;
+               }
+
+               /* The UUID should be the last argument to the search command.
+                * FIXME: this is a little fragile; would be nice to have some
+                * parser helpers to deal with "command args" parsing
+                */
+               uuid = strrchr(value, ' ');
+               if (!uuid)
+                       return;
+
+               uuid++;
+               pb_log("%s: uuid %s\n", __func__, uuid);
+
+               if (state->root)
+                       talloc_unlink(state, state->root);
+
+               root = talloc(state, struct grub2_root);
+               root->uuid = talloc_strdup(root, uuid);
+               state->root = root;
+               return;
+       }
+
        pb_log("%s: unknown name: %s\n", __func__, name);
 }
 
@@ -142,12 +223,14 @@ static const char *const grub2_conf_files[] = {
        "/grub.cfg",
        "/menu.lst",
        "/grub/grub.cfg",
+       "/grub2/grub.cfg",
        "/grub/menu.lst",
        "/boot/grub/grub.cfg",
        "/boot/grub/menu.lst",
        "/GRUB.CFG",
        "/MENU.LST",
        "/GRUB/GRUB.CFG",
+       "/GRUB2/GRUB.CFG",
        "/GRUB/MENU.LST",
        "/BOOT/GRUB/GRUB.CFG",
        "/BOOT/GRUB/MENU.LST",
@@ -158,6 +241,7 @@ static const char *grub2_known_names[] = {
        "menuentry",
        "linux",
        "initrd",
+       "search",
        NULL
 };
 
@@ -180,11 +264,6 @@ static int grub2_parse(struct discover_context *dc, char *buf, int len)
 
        state->known_names = grub2_known_names;
 
-       /* opt is persistent, so must be associated with device */
-
-       state->opt = discover_boot_option_create(dc, dc->device);
-       state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
-
        conf_parse_buf(conf, buf, len);
 
        talloc_free(conf);
@@ -196,7 +275,7 @@ static struct parser grub2_parser = {
        .method                 = CONF_METHOD_LOCAL_FILE,
        .parse                  = grub2_parse,
        .filenames              = grub2_conf_files,
-       .resolve_resource       = resolve_devpath_resource,
+       .resolve_resource       = resolve_grub2_resource,
 };
 
 register_parser(grub2_parser);