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 grub2_state *state = conf->parser_info;
46 if (!state->desc_image) {
47 pb_log("%s: %s: no image found\n", __func__,
48 conf->dc->device->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 device_add_boot_option(conf->dc->device, state->opt);
70 state->opt = talloc_zero(conf->dc->device, struct boot_option);
71 state->opt->boot_args = talloc_strdup(state->opt, "");
73 talloc_free(state->desc_image);
74 state->desc_image = NULL;
77 static void grub2_process_pair(struct conf_context *conf, const char *name,
80 struct grub2_state *state = conf->parser_info;
82 if (!name || !conf_param_in_list(state->known_names, name))
85 if (streq(name, "menuentry")) {
90 /* Then start the new image. */
92 sep = strchr(value, '\'');
97 state->opt->id = talloc_asprintf(state->opt, "%s#%s",
98 conf->dc->device->id, value);
99 state->opt->name = talloc_strdup(state->opt, value);
104 if (streq(name, "linux")) {
107 sep = strchr(value, ' ');
112 state->opt->boot_image_file = resolve_path(state->opt,
113 value, conf->dc->device_path);
114 state->desc_image = talloc_strdup(state->opt, value);
117 state->opt->boot_args = talloc_strdup(state->opt,
123 if (streq(name, "initrd")) {
124 state->opt->initrd_file = resolve_path(state->opt,
125 value, conf->dc->device_path);
126 state->desc_initrd = talloc_asprintf(state, "initrd=%s",
131 pb_log("%s: unknown name: %s\n", __func__, name);
134 static const char *const grub2_conf_files[] = {
139 "/boot/grub/grub.cfg",
140 "/boot/grub/menu.lst",
145 "/BOOT/GRUB/GRUB.CFG",
146 "/BOOT/GRUB/MENU.LST",
150 static const char *grub2_known_names[] = {
157 static int grub2_parse(struct discover_context *dc)
159 struct conf_context *conf;
160 struct grub2_state *state;
163 conf = talloc_zero(dc, struct conf_context);
169 conf_init_global_options(conf);
170 conf->conf_files = grub2_conf_files,
171 conf->get_pair = conf_get_pair_space;
172 conf->process_pair = grub2_process_pair;
173 conf->finish = grub2_finish;
174 conf->parser_info = state = talloc_zero(conf, struct grub2_state);
176 state->known_names = grub2_known_names;
178 /* opt is persistent, so must be associated with device */
180 state->opt = talloc_zero(conf->dc->device, struct boot_option);
181 state->opt->boot_args = talloc_strdup(state->opt, "");
183 rc = conf_parse(conf);
189 define_parser(grub2, grub2_parse);