]> git.proxmox.com Git - grub2.git/blob - grub-core/efiemu/main.c
Move platform-dependent files from $prefix to $prefix/$platform.
[grub2.git] / grub-core / efiemu / main.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2009 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* This is an emulation of EFI runtime services.
20 This allows a more uniform boot on i386 machines.
21 As it emulates only runtime service it isn't able
22 to chainload EFI bootloader on non-EFI system. */
23
24
25 #include <grub/file.h>
26 #include <grub/err.h>
27 #include <grub/normal.h>
28 #include <grub/mm.h>
29 #include <grub/dl.h>
30 #include <grub/misc.h>
31 #include <grub/efiemu/efiemu.h>
32 #include <grub/command.h>
33 #include <grub/i18n.h>
34
35 GRUB_MOD_LICENSE ("GPLv3+");
36
37 /* System table. Two version depending on mode */
38 grub_efi_system_table32_t *grub_efiemu_system_table32 = 0;
39 grub_efi_system_table64_t *grub_efiemu_system_table64 = 0;
40 /* Modules may need to execute some actions after memory allocation happens */
41 static struct grub_efiemu_prepare_hook *efiemu_prepare_hooks = 0;
42 /* Linked list of configuration tables */
43 static struct grub_efiemu_configuration_table *efiemu_config_tables = 0;
44 static int prepared = 0;
45
46 /* Free all allocated space */
47 grub_err_t
48 grub_efiemu_unload (void)
49 {
50 struct grub_efiemu_configuration_table *cur, *d;
51 struct grub_efiemu_prepare_hook *curhook, *d2;
52 grub_efiemu_loadcore_unload ();
53
54 grub_efiemu_mm_unload ();
55
56 for (cur = efiemu_config_tables; cur;)
57 {
58 d = cur->next;
59 if (cur->unload)
60 cur->unload (cur->data);
61 grub_free (cur);
62 cur = d;
63 }
64 efiemu_config_tables = 0;
65
66 for (curhook = efiemu_prepare_hooks; curhook;)
67 {
68 d2 = curhook->next;
69 if (curhook->unload)
70 curhook->unload (curhook->data);
71 grub_free (curhook);
72 curhook = d2;
73 }
74 efiemu_prepare_hooks = 0;
75
76 prepared = 0;
77
78 return GRUB_ERR_NONE;
79 }
80
81 /* Remove previously registered table from the list */
82 grub_err_t
83 grub_efiemu_unregister_configuration_table (grub_efi_guid_t guid)
84 {
85 struct grub_efiemu_configuration_table *cur, *prev;
86
87 /* Special treating if head is to remove */
88 while (efiemu_config_tables
89 && !grub_memcmp (&(efiemu_config_tables->guid), &guid, sizeof (guid)))
90 {
91 if (efiemu_config_tables->unload)
92 efiemu_config_tables->unload (efiemu_config_tables->data);
93 cur = efiemu_config_tables->next;
94 grub_free (efiemu_config_tables);
95 efiemu_config_tables = cur;
96 }
97 if (!efiemu_config_tables)
98 return GRUB_ERR_NONE;
99
100 /* Remove from chain */
101 for (prev = efiemu_config_tables, cur = prev->next; cur;)
102 if (grub_memcmp (&(cur->guid), &guid, sizeof (guid)) == 0)
103 {
104 if (cur->unload)
105 cur->unload (cur->data);
106 prev->next = cur->next;
107 grub_free (cur);
108 cur = prev->next;
109 }
110 else
111 {
112 prev = cur;
113 cur = cur->next;
114 }
115 return GRUB_ERR_NONE;
116 }
117
118 grub_err_t
119 grub_efiemu_register_prepare_hook (grub_err_t (*hook) (void *data),
120 void (*unload) (void *data),
121 void *data)
122 {
123 struct grub_efiemu_prepare_hook *nhook;
124 if (! hook)
125 return grub_error (GRUB_ERR_BAD_ARGUMENT, "you must supply the hook");
126 nhook = (struct grub_efiemu_prepare_hook *) grub_malloc (sizeof (*nhook));
127 if (! nhook)
128 return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't prepare hook");
129 nhook->hook = hook;
130 nhook->unload = unload;
131 nhook->data = data;
132 nhook->next = efiemu_prepare_hooks;
133 efiemu_prepare_hooks = nhook;
134 return GRUB_ERR_NONE;
135 }
136
137 /* Register a configuration table either supplying the address directly
138 or with a hook
139 */
140 grub_err_t
141 grub_efiemu_register_configuration_table (grub_efi_guid_t guid,
142 void * (*get_table) (void *data),
143 void (*unload) (void *data),
144 void *data)
145 {
146 struct grub_efiemu_configuration_table *tbl;
147 grub_err_t err;
148
149 if (! get_table && ! data)
150 return grub_error (GRUB_ERR_BAD_ARGUMENT,
151 "you must set at least get_table or data");
152 err = grub_efiemu_unregister_configuration_table (guid);
153 if (err)
154 return err;
155
156 tbl = (struct grub_efiemu_configuration_table *) grub_malloc (sizeof (*tbl));
157 if (! tbl)
158 return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't register table");
159
160 tbl->guid = guid;
161 tbl->get_table = get_table;
162 tbl->unload = unload;
163 tbl->data = data;
164 tbl->next = efiemu_config_tables;
165 efiemu_config_tables = tbl;
166
167 return GRUB_ERR_NONE;
168 }
169
170 static grub_err_t
171 grub_cmd_efiemu_unload (grub_command_t cmd __attribute__ ((unused)),
172 int argc __attribute__ ((unused)),
173 char *args[] __attribute__ ((unused)))
174 {
175 return grub_efiemu_unload ();
176 }
177
178 static grub_err_t
179 grub_cmd_efiemu_prepare (grub_command_t cmd __attribute__ ((unused)),
180 int argc __attribute__ ((unused)),
181 char *args[] __attribute__ ((unused)))
182 {
183 return grub_efiemu_prepare ();
184 }
185
186 \f
187
188 /* Load the runtime from the file FILENAME. */
189 static grub_err_t
190 grub_efiemu_load_file (const char *filename)
191 {
192 grub_file_t file;
193 grub_err_t err;
194
195 file = grub_file_open (filename);
196 if (! file)
197 return grub_errno;
198
199 err = grub_efiemu_mm_init ();
200 if (err)
201 {
202 grub_file_close (file);
203 grub_efiemu_unload ();
204 return grub_error (grub_errno, "couldn't init memory management");
205 }
206
207 grub_dprintf ("efiemu", "mm initialized\n");
208
209 err = grub_efiemu_loadcore_init (file);
210 if (err)
211 {
212 grub_file_close (file);
213 grub_efiemu_unload ();
214 return err;
215 }
216
217 grub_file_close (file);
218
219 /* For configuration tables entry in system table. */
220 grub_efiemu_request_symbols (1);
221
222 return GRUB_ERR_NONE;
223 }
224
225 grub_err_t
226 grub_efiemu_autocore (void)
227 {
228 const char *prefix;
229 char *filename;
230 const char *suffix;
231 grub_err_t err;
232
233 if (grub_efiemu_sizeof_uintn_t () != 0)
234 return GRUB_ERR_NONE;
235
236 prefix = grub_env_get ("prefix");
237
238 if (! prefix)
239 return grub_error (GRUB_ERR_FILE_NOT_FOUND,
240 "couldn't find efiemu core because prefix "
241 "isn't set");
242
243 suffix = grub_efiemu_get_default_core_name ();
244
245 filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s",
246 prefix, suffix);
247 if (! filename)
248 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
249 "couldn't allocate temporary space");
250
251 err = grub_efiemu_load_file (filename);
252 grub_free (filename);
253 if (err)
254 return err;
255 #ifndef GRUB_MACHINE_EMU
256 err = grub_machine_efiemu_init_tables ();
257 if (err)
258 return err;
259 #endif
260
261 return GRUB_ERR_NONE;
262 }
263
264 grub_err_t
265 grub_efiemu_prepare (void)
266 {
267 grub_err_t err;
268
269 if (prepared)
270 return GRUB_ERR_NONE;
271
272 err = grub_efiemu_autocore ();
273 if (err)
274 return err;
275
276 grub_dprintf ("efiemu", "Preparing %d-bit efiemu\n",
277 8 * grub_efiemu_sizeof_uintn_t ());
278
279 /* Create NVRAM. */
280 grub_efiemu_pnvram ();
281
282 prepared = 1;
283
284 if (grub_efiemu_sizeof_uintn_t () == 4)
285 return grub_efiemu_prepare32 (efiemu_prepare_hooks, efiemu_config_tables);
286 else
287 return grub_efiemu_prepare64 (efiemu_prepare_hooks, efiemu_config_tables);
288 }
289
290
291 static grub_err_t
292 grub_cmd_efiemu_load (grub_command_t cmd __attribute__ ((unused)),
293 int argc, char *args[])
294 {
295 grub_err_t err;
296
297 grub_efiemu_unload ();
298
299 if (argc != 1)
300 return grub_error (GRUB_ERR_BAD_ARGUMENT, "filename required");
301
302 err = grub_efiemu_load_file (args[0]);
303 if (err)
304 return err;
305 #ifndef GRUB_MACHINE_EMU
306 err = grub_machine_efiemu_init_tables ();
307 if (err)
308 return err;
309 #endif
310 return GRUB_ERR_NONE;
311 }
312
313 static grub_command_t cmd_loadcore, cmd_prepare, cmd_unload;
314
315 GRUB_MOD_INIT(efiemu)
316 {
317 cmd_loadcore = grub_register_command ("efiemu_loadcore",
318 grub_cmd_efiemu_load,
319 N_("FILE"),
320 N_("Load and initialize EFI emulator."));
321 cmd_prepare = grub_register_command ("efiemu_prepare",
322 grub_cmd_efiemu_prepare,
323 0,
324 N_("Finalize loading of EFI emulator."));
325 cmd_unload = grub_register_command ("efiemu_unload", grub_cmd_efiemu_unload,
326 0,
327 N_("Unload EFI emulator."));
328 }
329
330 GRUB_MOD_FINI(efiemu)
331 {
332 grub_unregister_command (cmd_loadcore);
333 grub_unregister_command (cmd_prepare);
334 grub_unregister_command (cmd_unload);
335 }