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"
36 struct discover_boot_option *opt;
39 const char *const *known_names;
42 static void grub2_finish(struct conf_context *conf)
44 struct device *dev = conf->dc->device->device;
45 struct grub2_state *state = conf->parser_info;
46 struct boot_option *opt;
48 if (!state->desc_image) {
49 pb_log("%s: %s: no image found\n", __func__, dev->id);
54 opt = state->opt->option;
58 assert(opt->boot_args);
60 opt->description = talloc_asprintf(opt, "%s %s %s",
62 (state->desc_initrd ? state->desc_initrd : ""),
65 talloc_free(state->desc_initrd);
66 state->desc_initrd = NULL;
68 conf_strip_str(opt->boot_args);
69 conf_strip_str(opt->description);
71 /* opt is persistent, so must be associated with device */
73 discover_context_add_boot_option(conf->dc, state->opt);
75 state->opt = discover_boot_option_create(conf->dc, conf->dc->device);
76 opt = state->opt->option;
77 opt->boot_args = talloc_strdup(opt, "");
79 talloc_free(state->desc_image);
80 state->desc_image = NULL;
83 static void grub2_process_pair(struct conf_context *conf, const char *name,
86 struct device *dev = conf->dc->device->device;
87 struct grub2_state *state = conf->parser_info;
88 struct discover_boot_option *opt = state->opt;
90 if (!name || !conf_param_in_list(state->known_names, name))
93 if (streq(name, "menuentry")) {
98 /* Then start the new image. */
100 sep = strchr(value, '\'');
105 opt->option->id = talloc_asprintf(opt->option,
106 "%s#%s", dev->id, value);
107 opt->option->name = talloc_strdup(opt->option, value);
112 if (streq(name, "linux")) {
115 sep = strchr(value, ' ');
120 opt->boot_image = create_devpath_resource(opt,
121 conf->dc->device, value);
122 state->desc_image = talloc_strdup(opt, value);
125 opt->option->boot_args = talloc_strdup(opt, sep + 1);
130 if (streq(name, "initrd")) {
131 opt->initrd = create_devpath_resource(opt,
132 conf->dc->device, value);
133 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
138 pb_log("%s: unknown name: %s\n", __func__, name);
141 static const char *const grub2_conf_files[] = {
146 "/boot/grub/grub.cfg",
147 "/boot/grub/menu.lst",
152 "/BOOT/GRUB/GRUB.CFG",
153 "/BOOT/GRUB/MENU.LST",
157 static const char *grub2_known_names[] = {
164 static int grub2_parse(struct discover_context *dc, char *buf, int len)
166 struct conf_context *conf;
167 struct grub2_state *state;
169 conf = talloc_zero(dc, struct conf_context);
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);
181 state->known_names = grub2_known_names;
183 /* opt is persistent, so must be associated with device */
185 state->opt = discover_boot_option_create(dc, dc->device);
186 state->opt->option->boot_args = talloc_strdup(state->opt->option, "");
188 conf_parse_buf(conf, buf, len);
194 static struct parser grub2_parser = {
196 .method = CONF_METHOD_LOCAL_FILE,
197 .parse = grub2_parse,
198 .filenames = grub2_conf_files,
199 .resolve_resource = resolve_devpath_resource,
202 register_parser(grub2_parser);