]> git.ozlabs.org Git - petitboot/blob - discover/grub2-parser.c
discover/grub2: handle search commands to specify root filesystems
[petitboot] / discover / grub2-parser.c
1 /*
2  *  Copyright Geoff Levand <geoff@infradead.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #define _GNU_SOURCE
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "log/log.h"
29 #include "talloc/talloc.h"
30 #include "types/types.h"
31 #include "parser-conf.h"
32 #include "parser-utils.h"
33 #include "paths.h"
34 #include "resource.h"
35
36 struct grub2_root {
37         char *uuid;
38 };
39
40 struct grub2_state {
41         struct discover_boot_option *opt;
42         char *desc_image;
43         char *desc_initrd;
44         const char *const *known_names;
45         struct grub2_root *root;
46 };
47
48 struct grub2_resource_info {
49         struct grub2_root *root;
50         char *path;
51 };
52
53 /* we use slightly different resources for grub2 */
54 static struct resource *create_grub2_resource(void *ctx,
55                 struct discover_device *orig_device,
56                 struct grub2_root *root, const char *path)
57 {
58         struct grub2_resource_info *info;
59         struct resource *res;
60
61         res = talloc(ctx, struct resource);
62
63         if (root) {
64                 info = talloc(res, struct grub2_resource_info);
65                 info->root = root;
66                 talloc_reference(info, root);
67                 info->path = talloc_strdup(info, path);
68
69                 res->resolved = false;
70                 res->info = info;
71
72         } else
73                 resolve_resource_against_device(res, orig_device, path);
74
75         return res;
76 }
77
78 static bool resolve_grub2_resource(struct device_handler *handler,
79                 struct resource *res)
80 {
81         struct grub2_resource_info *info = res->info;
82         struct discover_device *dev;
83
84         assert(!res->resolved);
85
86         dev = device_lookup_by_uuid(handler, info->root->uuid);
87
88         if (!dev)
89                 return false;
90
91         resolve_resource_against_device(res, dev, info->path);
92         talloc_free(info);
93
94         return true;
95 }
96
97 static void grub2_finish(struct conf_context *conf)
98 {
99         struct device *dev = conf->dc->device->device;
100         struct grub2_state *state = conf->parser_info;
101         struct boot_option *opt;
102
103         if (!state->desc_image) {
104                 pb_log("%s: %s: no image found\n", __func__, dev->id);
105                 return;
106         }
107
108         assert(state->opt);
109         opt = state->opt->option;
110
111         assert(opt);
112         assert(opt->name);
113         assert(opt->boot_args);
114
115         opt->description = talloc_asprintf(opt, "%s %s %s",
116                 state->desc_image,
117                 (state->desc_initrd ? state->desc_initrd : ""),
118                 opt->boot_args);
119
120         talloc_free(state->desc_initrd);
121         state->desc_initrd = NULL;
122
123         conf_strip_str(opt->boot_args);
124         conf_strip_str(opt->description);
125
126         /* opt is persistent, so must be associated with device */
127
128         discover_context_add_boot_option(conf->dc, state->opt);
129
130         state->opt = NULL;
131 }
132
133 static void grub2_process_pair(struct conf_context *conf, const char *name,
134                 char *value)
135 {
136         struct device *dev = conf->dc->device->device;
137         struct grub2_state *state = conf->parser_info;
138         struct discover_boot_option *opt = state->opt;
139
140         if (!name || !conf_param_in_list(state->known_names, name))
141                 return;
142
143         if (streq(name, "menuentry")) {
144                 char *sep;
145
146                 /* complete any existing option... */
147                 if (state->opt)
148                         grub2_finish(conf);
149
150                 /* ... then start the new one */
151                 opt = discover_boot_option_create(conf->dc, conf->dc->device);
152                 opt->option->boot_args = talloc_strdup(opt->option, "");
153
154                 sep = strchr(value, '\'');
155
156                 if (sep)
157                         *sep = 0;
158
159                 opt->option->id = talloc_asprintf(opt->option,
160                                         "%s#%s", dev->id, value);
161                 opt->option->name = talloc_strdup(opt->option, value);
162                 opt->option->boot_args = talloc_strdup(opt, "");
163
164                 state->opt = opt;
165
166                 return;
167         }
168
169         if (streq(name, "linux")) {
170                 char *sep;
171
172                 sep = strchr(value, ' ');
173
174                 if (sep)
175                         *sep = 0;
176
177                 opt->boot_image = create_grub2_resource(opt, conf->dc->device,
178                                         state->root, value);
179                 state->desc_image = talloc_strdup(opt, value);
180
181                 if (sep)
182                         opt->option->boot_args = talloc_strdup(opt, sep + 1);
183
184                 return;
185         }
186
187         if (streq(name, "initrd")) {
188                 opt->initrd = create_grub2_resource(opt, conf->dc->device,
189                                         state->root, value);
190                 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
191                         value);
192                 return;
193         }
194
195         if (streq(name, "search")) {
196                 struct grub2_root *root;
197                 char *uuid;
198
199                 if (!strstr(value, "--set=root")) {
200                         pb_log("%s: no root\n", __func__);
201                         return;
202                 }
203
204                 /* The UUID should be the last argument to the search command.
205                  * FIXME: this is a little fragile; would be nice to have some
206                  * parser helpers to deal with "command args" parsing
207                  */
208                 uuid = strrchr(value, ' ');
209                 if (!uuid)
210                         return;
211
212                 uuid++;
213                 pb_log("%s: uuid %s\n", __func__, uuid);
214
215                 if (state->root)
216                         talloc_unlink(state, state->root);
217
218                 root = talloc(state, struct grub2_root);
219                 root->uuid = talloc_strdup(root, uuid);
220                 state->root = root;
221                 return;
222         }
223
224         pb_log("%s: unknown name: %s\n", __func__, name);
225 }
226
227 static const char *const grub2_conf_files[] = {
228         "/grub.cfg",
229         "/menu.lst",
230         "/grub/grub.cfg",
231         "/grub2/grub.cfg",
232         "/grub/menu.lst",
233         "/boot/grub/grub.cfg",
234         "/boot/grub/menu.lst",
235         "/GRUB.CFG",
236         "/MENU.LST",
237         "/GRUB/GRUB.CFG",
238         "/GRUB2/GRUB.CFG",
239         "/GRUB/MENU.LST",
240         "/BOOT/GRUB/GRUB.CFG",
241         "/BOOT/GRUB/MENU.LST",
242         NULL
243 };
244
245 static const char *grub2_known_names[] = {
246         "menuentry",
247         "linux",
248         "initrd",
249         "search",
250         NULL
251 };
252
253 static int grub2_parse(struct discover_context *dc, char *buf, int len)
254 {
255         struct conf_context *conf;
256         struct grub2_state *state;
257
258         conf = talloc_zero(dc, struct conf_context);
259
260         if (!conf)
261                 return 0;
262
263         conf->dc = dc;
264         conf_init_global_options(conf);
265         conf->get_pair = conf_get_pair_space;
266         conf->process_pair = grub2_process_pair;
267         conf->finish = grub2_finish;
268         conf->parser_info = state = talloc_zero(conf, struct grub2_state);
269
270         state->known_names = grub2_known_names;
271
272         conf_parse_buf(conf, buf, len);
273
274         talloc_free(conf);
275         return 1;
276 }
277
278 static struct parser grub2_parser = {
279         .name                   = "grub2",
280         .method                 = CONF_METHOD_LOCAL_FILE,
281         .parse                  = grub2_parse,
282         .filenames              = grub2_conf_files,
283         .resolve_resource       = resolve_grub2_resource,
284 };
285
286 register_parser(grub2_parser);