]> git.ozlabs.org Git - petitboot/blob - discover/grub2/grub2.c
discover/grub2: Hook up flex/bison parser to discover server
[petitboot] / discover / grub2 / grub2.c
1
2 #include <assert.h>
3
4 #include <talloc/talloc.h>
5
6 #include <discover/resource.h>
7 #include <discover/parser.h>
8 #include <discover/parser-utils.h>
9
10 #include "grub2.h"
11 #include "parser.h"
12 #include "lexer.h"
13
14 static const char *const grub2_conf_files[] = {
15         "/grub.cfg",
16         "/menu.lst",
17         "/grub/grub.cfg",
18         "/grub2/grub.cfg",
19         "/grub/menu.lst",
20         "/boot/grub/grub.cfg",
21         "/boot/grub2/grub.cfg",
22         "/boot/grub/menu.lst",
23         "/GRUB.CFG",
24         "/MENU.LST",
25         "/GRUB/GRUB.CFG",
26         "/GRUB2/GRUB.CFG",
27         "/GRUB/MENU.LST",
28         "/BOOT/GRUB/GRUB.CFG",
29         "/BOOT/GRUB/MENU.LST",
30         NULL
31 };
32
33 struct grub2_resource_info {
34         struct grub2_root *root;
35         char *path;
36 };
37
38 /* we use slightly different resources for grub2 */
39 struct resource *create_grub2_resource(void *ctx,
40                 struct discover_device *orig_device,
41                 struct grub2_root *root, const char *path)
42 {
43         struct grub2_resource_info *info;
44         struct resource *res;
45
46         res = talloc(ctx, struct resource);
47
48         if (root) {
49                 info = talloc(res, struct grub2_resource_info);
50                 info->root = root;
51                 talloc_reference(info, root);
52                 info->path = talloc_strdup(info, path);
53
54                 res->resolved = false;
55                 res->info = info;
56
57         } else
58                 resolve_resource_against_device(res, orig_device, path);
59
60         return res;
61 }
62
63 bool resolve_grub2_resource(struct device_handler *handler,
64                 struct resource *res)
65 {
66         struct grub2_resource_info *info = res->info;
67         struct discover_device *dev;
68
69         assert(!res->resolved);
70
71         dev = device_lookup_by_uuid(handler, info->root->uuid);
72
73         if (!dev)
74                 return false;
75
76         resolve_resource_against_device(res, dev, info->path);
77         talloc_free(info);
78
79         return true;
80 }
81
82 static int grub2_parse(struct discover_context *dc, char *buf, int len)
83 {
84         struct grub2_parser *parser;
85
86         parser = grub2_parser_create(dc);
87
88         grub2_parser_parse(parser, buf, len);
89
90         talloc_free(parser);
91
92         return 1;
93 }
94
95 static struct parser grub2_parser = {
96         .name                   = "grub2",
97         .method                 = CONF_METHOD_LOCAL_FILE,
98         .parse                  = grub2_parse,
99         .filenames              = grub2_conf_files,
100         .resolve_resource       = resolve_grub2_resource,
101 };
102
103 register_parser(grub2_parser);