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);
78 static void grub2_process_pair(struct conf_context *conf, const char *name,
81 struct device *dev = conf->dc->device->device;
82 struct grub2_state *state = conf->parser_info;
83 struct discover_boot_option *opt = state->opt;
85 if (!name || !conf_param_in_list(state->known_names, name))
88 if (streq(name, "menuentry")) {
91 /* complete any existing option... */
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, "");
99 sep = strchr(value, '\'');
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, "");
114 if (streq(name, "linux")) {
117 sep = strchr(value, ' ');
122 opt->boot_image = create_devpath_resource(opt,
123 conf->dc->device, value);
124 state->desc_image = talloc_strdup(opt, value);
127 opt->option->boot_args = talloc_strdup(opt, sep + 1);
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",
140 pb_log("%s: unknown name: %s\n", __func__, name);
143 static const char *const grub2_conf_files[] = {
148 "/boot/grub/grub.cfg",
149 "/boot/grub/menu.lst",
154 "/BOOT/GRUB/GRUB.CFG",
155 "/BOOT/GRUB/MENU.LST",
159 static const char *grub2_known_names[] = {
166 static int grub2_parse(struct discover_context *dc, char *buf, int len)
168 struct conf_context *conf;
169 struct grub2_state *state;
171 conf = talloc_zero(dc, struct conf_context);
177 conf_init_global_options(conf);
178 conf->get_pair = conf_get_pair_space;
179 conf->process_pair = grub2_process_pair;
180 conf->finish = grub2_finish;
181 conf->parser_info = state = talloc_zero(conf, struct grub2_state);
183 state->known_names = grub2_known_names;
185 conf_parse_buf(conf, buf, len);
191 static struct parser grub2_parser = {
193 .method = CONF_METHOD_LOCAL_FILE,
194 .parse = grub2_parse,
195 .filenames = grub2_conf_files,
196 .resolve_resource = resolve_devpath_resource,
199 register_parser(grub2_parser);