]>
Commit | Line | Data |
---|---|---|
36121927 ER |
1 | From ddfb160353df14e9f88affe7498512a553146872 Mon Sep 17 00:00:00 2001 |
2 | From: Fedora Ninjas <grub2-owner@fedoraproject.org> | |
3 | Date: Tue, 22 Jan 2013 06:31:38 +0100 | |
4 | Subject: [PATCH 101/152] blscfg: add blscfg module to parse Boot Loader | |
5 | Specification snippets | |
6 | ||
7 | http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec | |
8 | ||
9 | Works like this: | |
10 | ||
11 | insmod blscfg | |
12 | bls_import | |
13 | ||
14 | Done! You should now have menu items for your snippets in place. | |
15 | ||
16 | Signed-off-by: Peter Jones <grub2-owner@fedoraproject.org> | |
17 | --- | |
18 | grub-core/Makefile.core.def | 8 ++ | |
19 | grub-core/commands/blscfg.c | 201 ++++++++++++++++++++++++++++++++++++++++++++ | |
20 | 2 files changed, 209 insertions(+) | |
21 | create mode 100644 grub-core/commands/blscfg.c | |
22 | ||
23 | diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def | |
24 | index ec46506..7bf1c8a 100644 | |
25 | --- a/grub-core/Makefile.core.def | |
26 | +++ b/grub-core/Makefile.core.def | |
27 | @@ -747,6 +747,14 @@ module = { | |
28 | }; | |
29 | ||
30 | module = { | |
31 | + name = blscfg; | |
32 | + common = commands/blscfg.c; | |
33 | + enable = i386_efi; | |
34 | + enable = x86_64_efi; | |
35 | + enable = i386_pc; | |
36 | +}; | |
37 | + | |
38 | +module = { | |
39 | name = boot; | |
40 | common = commands/boot.c; | |
41 | i386_pc = lib/i386/pc/biosnum.c; | |
42 | diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c | |
43 | new file mode 100644 | |
44 | index 0000000..4274aca | |
45 | --- /dev/null | |
46 | +++ b/grub-core/commands/blscfg.c | |
47 | @@ -0,0 +1,201 @@ | |
48 | +/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/ | |
49 | + | |
50 | +/* bls.c - implementation of the boot loader spec */ | |
51 | + | |
52 | +/* | |
53 | + * GRUB -- GRand Unified Bootloader | |
54 | + * | |
55 | + * GRUB is free software: you can redistribute it and/or modify | |
56 | + * it under the terms of the GNU General Public License as published by | |
57 | + * the Free Software Foundation, either version 3 of the License, or | |
58 | + * (at your option) any later version. | |
59 | + * | |
60 | + * GRUB is distributed in the hope that it will be useful, | |
61 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
62 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
63 | + * GNU General Public License for more details. | |
64 | + * | |
65 | + * You should have received a copy of the GNU General Public License | |
66 | + * along with GRUB. If not, see <http://www.gnu.org/licenses/>. | |
67 | + */ | |
68 | + | |
69 | +#include <grub/types.h> | |
70 | +#include <grub/misc.h> | |
71 | +#include <grub/mm.h> | |
72 | +#include <grub/err.h> | |
73 | +#include <grub/dl.h> | |
74 | +#include <grub/extcmd.h> | |
75 | +#include <grub/i18n.h> | |
76 | +#include <grub/fs.h> | |
77 | +#include <grub/env.h> | |
78 | +#include <grub/file.h> | |
79 | +#include <grub/normal.h> | |
80 | + | |
81 | +GRUB_MOD_LICENSE ("GPLv3+"); | |
82 | + | |
83 | +#ifdef GRUB_MACHINE_EFI | |
84 | +#define GRUB_LINUX_CMD "linuxefi" | |
85 | +#define GRUB_INITRD_CMD "initrdefi" | |
86 | +#define GRUB_BLS_CONFIG_PATH "/EFI/fedora/loader/entries/" | |
87 | +#define GRUB_BOOT_DEVICE "($boot)" | |
88 | +#else | |
89 | +#define GRUB_LINUX_CMD "linux" | |
90 | +#define GRUB_INITRD_CMD "initrd" | |
91 | +#define GRUB_BLS_CONFIG_PATH "/loader/entries/" | |
92 | +#define GRUB_BOOT_DEVICE "($root)" | |
93 | +#endif | |
94 | + | |
95 | +static int parse_entry ( | |
96 | + const char *filename, | |
97 | + const struct grub_dirhook_info *info __attribute__ ((unused)), | |
98 | + void *data __attribute__ ((unused))) | |
99 | +{ | |
100 | + grub_size_t n; | |
101 | + char *p; | |
102 | + grub_file_t f = NULL; | |
103 | + grub_off_t sz; | |
104 | + char *title = NULL, *options = NULL, *clinux = NULL, *initrd = NULL, *src = NULL; | |
105 | + const char *args[2] = { NULL, NULL }; | |
106 | + | |
107 | + if (filename[0] == '.') | |
108 | + return 0; | |
109 | + | |
110 | + n = grub_strlen (filename); | |
111 | + if (n <= 5) | |
112 | + return 0; | |
113 | + | |
114 | + if (grub_strcmp (filename + n - 5, ".conf") != 0) | |
115 | + return 0; | |
116 | + | |
117 | + p = grub_xasprintf (GRUB_BLS_CONFIG_PATH "%s", filename); | |
118 | + | |
119 | + f = grub_file_open (p); | |
120 | + if (!f) | |
121 | + goto finish; | |
122 | + | |
123 | + sz = grub_file_size (f); | |
124 | + if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024) | |
125 | + goto finish; | |
126 | + | |
127 | + for (;;) | |
128 | + { | |
129 | + char *buf; | |
130 | + | |
131 | + buf = grub_file_getline (f); | |
132 | + if (!buf) | |
133 | + break; | |
134 | + | |
135 | + if (grub_strncmp (buf, "title ", 6) == 0) | |
136 | + { | |
137 | + grub_free (title); | |
138 | + title = grub_strdup (buf + 6); | |
139 | + if (!title) | |
140 | + goto finish; | |
141 | + } | |
142 | + else if (grub_strncmp (buf, "options ", 8) == 0) | |
143 | + { | |
144 | + grub_free (options); | |
145 | + options = grub_strdup (buf + 8); | |
146 | + if (!options) | |
147 | + goto finish; | |
148 | + } | |
149 | + else if (grub_strncmp (buf, "linux ", 6) == 0) | |
150 | + { | |
151 | + grub_free (clinux); | |
152 | + clinux = grub_strdup (buf + 6); | |
153 | + if (!clinux) | |
154 | + goto finish; | |
155 | + } | |
156 | + else if (grub_strncmp (buf, "initrd ", 7) == 0) | |
157 | + { | |
158 | + grub_free (initrd); | |
159 | + initrd = grub_strdup (buf + 7); | |
160 | + if (!initrd) | |
161 | + goto finish; | |
162 | + } | |
163 | + | |
164 | + grub_free(buf); | |
165 | + } | |
166 | + | |
167 | + if (!linux) | |
168 | + { | |
169 | + grub_printf ("Skipping file %s with no 'linux' key.", p); | |
170 | + goto finish; | |
171 | + } | |
172 | + | |
173 | + args[0] = title ? title : filename; | |
174 | + | |
175 | + src = grub_xasprintf ("load_video\n" | |
176 | + "set gfx_payload=keep\n" | |
177 | + "insmod gzio\n" | |
178 | + GRUB_LINUX_CMD " %s%s%s%s\n" | |
179 | + "%s%s%s%s", | |
180 | + GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options : "", | |
181 | + initrd ? GRUB_INITRD_CMD " " : "", initrd ? GRUB_BOOT_DEVICE : "", initrd ? initrd : "", initrd ? "\n" : ""); | |
182 | + | |
183 | + grub_normal_add_menu_entry (1, args, NULL, NULL, "bls", NULL, NULL, src, 0); | |
184 | + | |
185 | +finish: | |
186 | + grub_free (p); | |
187 | + grub_free (title); | |
188 | + grub_free (options); | |
189 | + grub_free (clinux); | |
190 | + grub_free (initrd); | |
191 | + grub_free (src); | |
192 | + | |
193 | + if (f) | |
194 | + grub_file_close (f); | |
195 | + | |
196 | + return 0; | |
197 | +} | |
198 | + | |
199 | +static grub_err_t | |
200 | +grub_cmd_bls_import (grub_extcmd_context_t ctxt __attribute__ ((unused)), | |
201 | + int argc __attribute__ ((unused)), | |
202 | + char **args __attribute__ ((unused))) | |
203 | +{ | |
204 | + grub_fs_t fs; | |
205 | + grub_device_t dev; | |
206 | + static grub_err_t r; | |
207 | + const char *devid; | |
208 | + | |
209 | + devid = grub_env_get ("root"); | |
210 | + if (!devid) | |
211 | + return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "root"); | |
212 | + | |
213 | + dev = grub_device_open (devid); | |
214 | + if (!dev) | |
215 | + return grub_errno; | |
216 | + | |
217 | + fs = grub_fs_probe (dev); | |
218 | + if (!fs) | |
219 | + { | |
220 | + r = grub_errno; | |
221 | + goto finish; | |
222 | + } | |
223 | + | |
224 | + r = fs->dir (dev, GRUB_BLS_CONFIG_PATH, parse_entry, NULL); | |
225 | + | |
226 | +finish: | |
227 | + if (dev) | |
228 | + grub_device_close (dev); | |
229 | + | |
230 | + return r; | |
231 | +} | |
232 | + | |
233 | +static grub_extcmd_t cmd; | |
234 | + | |
235 | +GRUB_MOD_INIT(bls) | |
236 | +{ | |
237 | + cmd = grub_register_extcmd ("bls_import", | |
238 | + grub_cmd_bls_import, | |
239 | + 0, | |
240 | + NULL, | |
241 | + N_("Import Boot Loader Specification snippets."), | |
242 | + NULL); | |
243 | +} | |
244 | + | |
245 | +GRUB_MOD_FINI(bls) | |
246 | +{ | |
247 | + grub_unregister_extcmd (cmd); | |
248 | +} | |
249 | -- | |
250 | 1.9.3 | |
251 |