ui/ncurses: Use pmenu_item destrutor to free ncurses ITEMs
[petitboot] / discover / grub2 / grub2.c
index ebc6ac7b6d72383d24efc29a1c08bf11cf2b8d6f..5b3009a465f7d43ac8f351c00aac7b1075fbd24b 100644 (file)
@@ -1,15 +1,15 @@
 
 #include <assert.h>
+#include <string.h>
 
 #include <talloc/talloc.h>
+#include <url/url.h>
 
 #include <discover/resource.h>
 #include <discover/parser.h>
 #include <discover/parser-utils.h>
 
 #include "grub2.h"
-#include "parser.h"
-#include "lexer.h"
 
 static const char *const grub2_conf_files[] = {
        "/grub.cfg",
@@ -31,24 +31,30 @@ static const char *const grub2_conf_files[] = {
 };
 
 struct grub2_resource_info {
-       struct grub2_root *root;
+       char *root;
        char *path;
 };
 
 /* we use slightly different resources for grub2 */
-struct resource *create_grub2_resource(void *ctx,
+struct resource *create_grub2_resource(struct discover_boot_option *opt,
                struct discover_device *orig_device,
-               struct grub2_root *root, const char *path)
+               const char *root, const char *path)
 {
        struct grub2_resource_info *info;
        struct resource *res;
 
-       res = talloc(ctx, struct resource);
+       if (strstr(path, "://")) {
+               struct pb_url *url = pb_url_parse(opt, path);
+               if (url)
+                       return create_url_resource(opt, url);
+       }
+
+       res = talloc(opt, struct resource);
 
        if (root) {
                info = talloc(res, struct grub2_resource_info);
-               info->root = root;
                talloc_reference(info, root);
+               info->root = talloc_strdup(info, root);
                info->path = talloc_strdup(info, path);
 
                res->resolved = false;
@@ -68,7 +74,7 @@ bool resolve_grub2_resource(struct device_handler *handler,
 
        assert(!res->resolved);
 
-       dev = device_lookup_by_uuid(handler, info->root->uuid);
+       dev = device_lookup_by_uuid(handler, info->root);
 
        if (!dev)
                return false;
@@ -79,24 +85,36 @@ bool resolve_grub2_resource(struct device_handler *handler,
        return true;
 }
 
-static int grub2_parse(struct discover_context *dc, char *buf, int len)
+static int grub2_parse(struct discover_context *dc)
 {
+       const char * const *filename;
        struct grub2_parser *parser;
+       int len, rc;
+       char *buf;
+
+       /* Support block device boot only at present */
+       if (dc->event)
+               return -1;
 
-       parser = grub2_parser_create(dc);
+       for (filename = grub2_conf_files; *filename; filename++) {
+               rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
+               if (rc)
+                       continue;
 
-       grub2_parser_parse(parser, buf, len);
+               parser = grub2_parser_create(dc);
+               grub2_parser_parse(parser, *filename, buf, len);
+               talloc_free(buf);
+               talloc_free(parser);
+               break;
+       }
 
-       talloc_free(parser);
 
-       return 1;
+       return 0;
 }
 
 static struct parser grub2_parser = {
        .name                   = "grub2",
-       .method                 = CONF_METHOD_LOCAL_FILE,
        .parse                  = grub2_parse,
-       .filenames              = grub2_conf_files,
        .resolve_resource       = resolve_grub2_resource,
 };