]> git.ozlabs.org Git - petitboot/blob - discover/grub2-parser.c
cb9635e64122d766a4fc086e89b52fd13f25fac1
[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 "resource.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 = NULL;
76 }
77
78 static void grub2_process_pair(struct conf_context *conf, const char *name,
79                 char *value)
80 {
81         struct device *dev = conf->dc->device->device;
82         struct grub2_state *state = conf->parser_info;
83         struct discover_boot_option *opt = state->opt;
84
85         if (!name || !conf_param_in_list(state->known_names, name))
86                 return;
87
88         if (streq(name, "menuentry")) {
89                 char *sep;
90
91                 /* complete any existing option... */
92                 if (state->opt)
93                         grub2_finish(conf);
94
95                 /* ... then start the new one */
96                 opt = discover_boot_option_create(conf->dc, conf->dc->device);
97                 opt->option->boot_args = talloc_strdup(opt->option, "");
98
99                 sep = strchr(value, '\'');
100
101                 if (sep)
102                         *sep = 0;
103
104                 opt->option->id = talloc_asprintf(opt->option,
105                                         "%s#%s", dev->id, value);
106                 opt->option->name = talloc_strdup(opt->option, value);
107                 opt->option->boot_args = talloc_strdup(opt, "");
108
109                 state->opt = opt;
110
111                 return;
112         }
113
114         if (streq(name, "linux")) {
115                 char *sep;
116
117                 sep = strchr(value, ' ');
118
119                 if (sep)
120                         *sep = 0;
121
122                 opt->boot_image = create_devpath_resource(opt,
123                                         conf->dc->device, value);
124                 state->desc_image = talloc_strdup(opt, value);
125
126                 if (sep)
127                         opt->option->boot_args = talloc_strdup(opt, sep + 1);
128
129                 return;
130         }
131
132         if (streq(name, "initrd")) {
133                 opt->initrd = create_devpath_resource(opt,
134                                         conf->dc->device, value);
135                 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
136                         value);
137                 return;
138         }
139
140         pb_log("%s: unknown name: %s\n", __func__, name);
141 }
142
143 static const char *const grub2_conf_files[] = {
144         "/grub.cfg",
145         "/menu.lst",
146         "/grub/grub.cfg",
147         "/grub2/grub.cfg",
148         "/grub/menu.lst",
149         "/boot/grub/grub.cfg",
150         "/boot/grub/menu.lst",
151         "/GRUB.CFG",
152         "/MENU.LST",
153         "/GRUB/GRUB.CFG",
154         "/GRUB2/GRUB.CFG",
155         "/GRUB/MENU.LST",
156         "/BOOT/GRUB/GRUB.CFG",
157         "/BOOT/GRUB/MENU.LST",
158         NULL
159 };
160
161 static const char *grub2_known_names[] = {
162         "menuentry",
163         "linux",
164         "initrd",
165         NULL
166 };
167
168 static int grub2_parse(struct discover_context *dc, char *buf, int len)
169 {
170         struct conf_context *conf;
171         struct grub2_state *state;
172
173         conf = talloc_zero(dc, struct conf_context);
174
175         if (!conf)
176                 return 0;
177
178         conf->dc = dc;
179         conf_init_global_options(conf);
180         conf->get_pair = conf_get_pair_space;
181         conf->process_pair = grub2_process_pair;
182         conf->finish = grub2_finish;
183         conf->parser_info = state = talloc_zero(conf, struct grub2_state);
184
185         state->known_names = grub2_known_names;
186
187         conf_parse_buf(conf, buf, len);
188
189         talloc_free(conf);
190         return 1;
191 }
192
193 static struct parser grub2_parser = {
194         .name                   = "grub2",
195         .method                 = CONF_METHOD_LOCAL_FILE,
196         .parse                  = grub2_parse,
197         .filenames              = grub2_conf_files,
198         .resolve_resource       = resolve_devpath_resource,
199 };
200
201 register_parser(grub2_parser);