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