discover: Separate temporary and permanent device data
[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 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
47         if (!state->desc_image) {
48                 pb_log("%s: %s: no image found\n", __func__, dev->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         discover_context_add_boot_option(conf->dc, state->opt);
70
71         state->opt = talloc_zero(conf->dc, struct boot_option);
72         state->opt->boot_args = talloc_strdup(state->opt, "");
73
74         talloc_free(state->desc_image);
75         state->desc_image = 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
84         if (!name || !conf_param_in_list(state->known_names, name))
85                 return;
86
87         if (streq(name, "menuentry")) {
88                 char *sep;
89
90                 grub2_finish(conf);
91
92                 /* Then start the new image. */
93
94                 sep = strchr(value, '\'');
95
96                 if (sep)
97                         *sep = 0;
98
99                 state->opt->id = talloc_asprintf(state->opt, "%s#%s",
100                         dev->id, value);
101                 state->opt->name = talloc_strdup(state->opt, value);
102
103                 return;
104         }
105
106         if (streq(name, "linux")) {
107                 char *sep;
108
109                 sep = strchr(value, ' ');
110
111                 if (sep)
112                         *sep = 0;
113
114                 state->opt->boot_image_file = resolve_path(state->opt,
115                         value, conf->dc->device->device_path);
116                 state->desc_image = talloc_strdup(state->opt, value);
117
118                 if (sep)
119                         state->opt->boot_args = talloc_strdup(state->opt,
120                                 sep + 1);
121
122                 return;
123         }
124
125         if (streq(name, "initrd")) {
126                 state->opt->initrd_file = resolve_path(state->opt,
127                         value, conf->dc->device->device_path);
128                 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
129                         value);
130                 return;
131         }
132
133         pb_log("%s: unknown name: %s\n", __func__, name);
134 }
135
136 static const char *const grub2_conf_files[] = {
137         "/grub.cfg",
138         "/menu.lst",
139         "/grub/grub.cfg",
140         "/grub/menu.lst",
141         "/boot/grub/grub.cfg",
142         "/boot/grub/menu.lst",
143         "/GRUB.CFG",
144         "/MENU.LST",
145         "/GRUB/GRUB.CFG",
146         "/GRUB/MENU.LST",
147         "/BOOT/GRUB/GRUB.CFG",
148         "/BOOT/GRUB/MENU.LST",
149         NULL
150 };
151
152 static const char *grub2_known_names[] = {
153         "menuentry",
154         "linux",
155         "initrd",
156         NULL
157 };
158
159 static int grub2_parse(struct discover_context *dc)
160 {
161         struct conf_context *conf;
162         struct grub2_state *state;
163         int rc;
164
165         conf = talloc_zero(dc, struct conf_context);
166
167         if (!conf)
168                 return 0;
169
170         conf->dc = dc;
171         conf_init_global_options(conf);
172         conf->conf_files = grub2_conf_files,
173         conf->get_pair = conf_get_pair_space;
174         conf->process_pair = grub2_process_pair;
175         conf->finish = grub2_finish;
176         conf->parser_info = state = talloc_zero(conf, struct grub2_state);
177
178         state->known_names = grub2_known_names;
179
180         /* opt is persistent, so must be associated with device */
181
182         state->opt = talloc_zero(conf->dc->device, struct boot_option);
183         state->opt->boot_args = talloc_strdup(state->opt, "");
184
185         rc = conf_parse(conf);
186
187         talloc_free(conf);
188         return rc;
189 }
190
191 define_parser(grub2, grub2_parse);