]> git.ozlabs.org Git - petitboot/blob - discover/grub2-parser.c
8a0868a7a72bdba905cf0d379c1c0e08ac3c65b7
[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
35 struct grub2_state {
36         struct discover_boot_option *opt;
37         char *desc_image;
38         char *desc_initrd;
39         const char *const *known_names;
40 };
41
42 static void grub2_finish(struct conf_context *conf)
43 {
44         struct device *dev = conf->dc->device->device;
45         struct grub2_state *state = conf->parser_info;
46         struct boot_option *opt;
47
48         if (!state->desc_image) {
49                 pb_log("%s: %s: no image found\n", __func__, dev->id);
50                 return;
51         }
52
53         assert(state->opt);
54         opt = state->opt->option;
55
56         assert(opt);
57         assert(opt->name);
58         assert(opt->boot_args);
59
60         opt->description = talloc_asprintf(opt, "%s %s %s",
61                 state->desc_image,
62                 (state->desc_initrd ? state->desc_initrd : ""),
63                 opt->boot_args);
64
65         talloc_free(state->desc_initrd);
66         state->desc_initrd = NULL;
67
68         conf_strip_str(opt->boot_args);
69         conf_strip_str(opt->description);
70
71         /* opt is persistent, so must be associated with device */
72
73         discover_context_add_boot_option(conf->dc, state->opt);
74
75         state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
76         opt = state->opt->option;
77         opt->boot_args = talloc_strdup(opt, "");
78
79         talloc_free(state->desc_image);
80         state->desc_image = NULL;
81 }
82
83 static void grub2_process_pair(struct conf_context *conf, const char *name,
84                 char *value)
85 {
86         struct device *dev = conf->dc->device->device;
87         struct grub2_state *state = conf->parser_info;
88         struct boot_option *opt = state->opt->option;
89
90         if (!name || !conf_param_in_list(state->known_names, name))
91                 return;
92
93         if (streq(name, "menuentry")) {
94                 char *sep;
95
96                 grub2_finish(conf);
97
98                 /* Then start the new image. */
99
100                 sep = strchr(value, '\'');
101
102                 if (sep)
103                         *sep = 0;
104
105                 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, value);
106                 opt->name = talloc_strdup(opt, value);
107
108                 return;
109         }
110
111         if (streq(name, "linux")) {
112                 char *sep;
113
114                 sep = strchr(value, ' ');
115
116                 if (sep)
117                         *sep = 0;
118
119                 opt->boot_image_file = resolve_path(opt, value,
120                                         conf->dc->device->device_path);
121                 state->desc_image = talloc_strdup(opt, value);
122
123                 if (sep)
124                         opt->boot_args = talloc_strdup(opt,
125                                 sep + 1);
126
127                 return;
128         }
129
130         if (streq(name, "initrd")) {
131                 opt->initrd_file = resolve_path(opt,
132                         value, conf->dc->device->device_path);
133                 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
134                         value);
135                 return;
136         }
137
138         pb_log("%s: unknown name: %s\n", __func__, name);
139 }
140
141 static const char *const grub2_conf_files[] = {
142         "/grub.cfg",
143         "/menu.lst",
144         "/grub/grub.cfg",
145         "/grub/menu.lst",
146         "/boot/grub/grub.cfg",
147         "/boot/grub/menu.lst",
148         "/GRUB.CFG",
149         "/MENU.LST",
150         "/GRUB/GRUB.CFG",
151         "/GRUB/MENU.LST",
152         "/BOOT/GRUB/GRUB.CFG",
153         "/BOOT/GRUB/MENU.LST",
154         NULL
155 };
156
157 static const char *grub2_known_names[] = {
158         "menuentry",
159         "linux",
160         "initrd",
161         NULL
162 };
163
164 static int grub2_parse(struct discover_context *dc, char *buf, int len)
165 {
166         struct conf_context *conf;
167         struct grub2_state *state;
168
169         conf = talloc_zero(dc, struct conf_context);
170
171         if (!conf)
172                 return 0;
173
174         conf->dc = dc;
175         conf_init_global_options(conf);
176         conf->get_pair = conf_get_pair_space;
177         conf->process_pair = grub2_process_pair;
178         conf->finish = grub2_finish;
179         conf->parser_info = state = talloc_zero(conf, struct grub2_state);
180
181         state->known_names = grub2_known_names;
182
183         /* opt is persistent, so must be associated with device */
184
185         state->opt = discover_boot_option_create(dc, dc->device);
186         state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
187
188         conf_parse_buf(conf, buf, len);
189
190         talloc_free(conf);
191         return 1;
192 }
193
194 struct parser __grub2_parser = {
195         .name           = "grub2",
196         .parse          = grub2_parse,
197         .filenames      = grub2_conf_files,
198 };