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 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;
47 if (!state->desc_image) {
48 pb_log("%s: %s: no image found\n", __func__, dev->id);
53 assert(state->opt->name);
54 assert(state->opt->boot_args);
56 state->opt->description = talloc_asprintf(state->opt, "%s %s %s",
58 (state->desc_initrd ? state->desc_initrd : ""),
59 state->opt->boot_args);
61 talloc_free(state->desc_initrd);
62 state->desc_initrd = NULL;
64 conf_strip_str(state->opt->boot_args);
65 conf_strip_str(state->opt->description);
67 /* opt is persistent, so must be associated with device */
69 discover_context_add_boot_option(conf->dc, state->opt);
71 state->opt = talloc_zero(conf->dc, struct boot_option);
72 state->opt->boot_args = talloc_strdup(state->opt, "");
74 talloc_free(state->desc_image);
75 state->desc_image = NULL;
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;
84 if (!name || !conf_param_in_list(state->known_names, name))
87 if (streq(name, "menuentry")) {
92 /* Then start the new image. */
94 sep = strchr(value, '\'');
99 state->opt->id = talloc_asprintf(state->opt, "%s#%s",
101 state->opt->name = talloc_strdup(state->opt, value);
106 if (streq(name, "linux")) {
109 sep = strchr(value, ' ');
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);
119 state->opt->boot_args = talloc_strdup(state->opt,
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",
133 pb_log("%s: unknown name: %s\n", __func__, name);
136 static const char *const grub2_conf_files[] = {
141 "/boot/grub/grub.cfg",
142 "/boot/grub/menu.lst",
147 "/BOOT/GRUB/GRUB.CFG",
148 "/BOOT/GRUB/MENU.LST",
152 static const char *grub2_known_names[] = {
159 static int grub2_parse(struct discover_context *dc)
161 struct conf_context *conf;
162 struct grub2_state *state;
165 conf = talloc_zero(dc, struct conf_context);
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);
178 state->known_names = grub2_known_names;
180 /* opt is persistent, so must be associated with device */
182 state->opt = talloc_zero(conf->dc->device, struct boot_option);
183 state->opt->boot_args = talloc_strdup(state->opt, "");
185 rc = conf_parse(conf);
191 define_parser(grub2, grub2_parse);