2 * Copyright Geoff Levand <geoff@infradead.org>
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.
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.
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
18 #if defined(HAVE_CONFIG_H)
29 #include "talloc/talloc.h"
30 #include "types/types.h"
31 #include "parser-conf.h"
32 #include "parser-utils.h"
41 struct discover_boot_option *opt;
44 const char *const *known_names;
45 struct grub2_root *root;
48 struct grub2_resource_info {
49 struct grub2_root *root;
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)
58 struct grub2_resource_info *info;
61 res = talloc(ctx, struct resource);
64 info = talloc(res, struct grub2_resource_info);
66 talloc_reference(info, root);
67 info->path = talloc_strdup(info, path);
69 res->resolved = false;
73 resolve_resource_against_device(res, orig_device, path);
78 static bool resolve_grub2_resource(struct device_handler *handler,
81 struct grub2_resource_info *info = res->info;
82 struct discover_device *dev;
84 assert(!res->resolved);
86 dev = device_lookup_by_uuid(handler, info->root->uuid);
91 resolve_resource_against_device(res, dev, info->path);
97 static void grub2_finish(struct conf_context *conf)
99 struct device *dev = conf->dc->device->device;
100 struct grub2_state *state = conf->parser_info;
101 struct boot_option *opt;
103 if (!state->desc_image) {
104 pb_log("%s: %s: no image found\n", __func__, dev->id);
109 opt = state->opt->option;
113 assert(opt->boot_args);
115 opt->description = talloc_asprintf(opt, "%s %s %s",
117 (state->desc_initrd ? state->desc_initrd : ""),
120 talloc_free(state->desc_initrd);
121 state->desc_initrd = NULL;
123 conf_strip_str(opt->boot_args);
124 conf_strip_str(opt->description);
126 /* opt is persistent, so must be associated with device */
128 discover_context_add_boot_option(conf->dc, state->opt);
133 static void grub2_process_pair(struct conf_context *conf, const char *name,
136 struct device *dev = conf->dc->device->device;
137 struct grub2_state *state = conf->parser_info;
138 struct discover_boot_option *opt = state->opt;
140 if (!name || !conf_param_in_list(state->known_names, name))
143 if (streq(name, "menuentry")) {
144 /* complete any existing option... */
148 /* ... then start the new one */
149 opt = discover_boot_option_create(conf->dc, conf->dc->device);
150 opt->option->boot_args = talloc_strdup(opt->option, "");
152 value = strtok(value, "\'{\"");
154 opt->option->id = talloc_asprintf(opt->option,
155 "%s#%s", dev->id, value);
156 opt->option->name = talloc_strdup(opt->option, value);
157 opt->option->boot_args = talloc_strdup(opt, "");
164 if (streq(name, "linux") || streq(name, "linux16")) {
167 sep = strchr(value, ' ');
172 opt->boot_image = create_grub2_resource(opt, conf->dc->device,
174 state->desc_image = talloc_strdup(opt, value);
177 opt->option->boot_args = talloc_strdup(opt, sep + 1);
182 if (streq(name, "initrd")) {
183 opt->initrd = create_grub2_resource(opt, conf->dc->device,
185 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
190 if (streq(name, "search")) {
191 struct grub2_root *root;
194 if (!strstr(value, "--set=root")) {
195 pb_log("%s: no root\n", __func__);
199 /* The UUID should be the last argument to the search command.
200 * FIXME: this is a little fragile; would be nice to have some
201 * parser helpers to deal with "command args" parsing
203 uuid = strrchr(value, ' ');
208 pb_log("%s: uuid %s\n", __func__, uuid);
211 talloc_unlink(state, state->root);
213 root = talloc(state, struct grub2_root);
214 root->uuid = talloc_strdup(root, uuid);
219 pb_log("%s: unknown name: %s\n", __func__, name);
222 static const char *const grub2_conf_files[] = {
228 "/boot/grub/grub.cfg",
229 "/boot/grub/menu.lst",
235 "/BOOT/GRUB/GRUB.CFG",
236 "/BOOT/GRUB/MENU.LST",
240 static const char *grub2_known_names[] = {
249 static int grub2_parse(struct discover_context *dc, char *buf, int len)
251 struct conf_context *conf;
252 struct grub2_state *state;
254 conf = talloc_zero(dc, struct conf_context);
260 conf_init_global_options(conf);
261 conf->get_pair = conf_get_pair_space;
262 conf->process_pair = grub2_process_pair;
263 conf->finish = grub2_finish;
264 conf->parser_info = state = talloc_zero(conf, struct grub2_state);
266 state->known_names = grub2_known_names;
268 conf_parse_buf(conf, buf, len);
274 static struct parser grub2_parser = {
276 .method = CONF_METHOD_LOCAL_FILE,
277 .parse = grub2_parse,
278 .filenames = grub2_conf_files,
279 .resolve_resource = resolve_grub2_resource,
282 register_parser(grub2_parser);