Moved currently useless parts inside #if 0
[grub2/phcoder.git] / efiemu / prepare.c
blob048893317470c3ec03bd9ef05d4488dd24078efc
1 /* Prepare efiemu. E.g. allocate memory, load the runtime
2 to appropriate place, etc */
3 /*
4 * GRUB -- GRand Unified Bootloader
5 * Copyright (C) 2009 Free Software Foundation, Inc.
7 * GRUB is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GRUB is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/err.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/efiemu/efiemu.h>
25 #include <grub/lib/crc.h>
27 grub_err_t
28 SUFFIX (grub_efiemu_prepare) (struct grub_efiemu_prepare_hook *prepare_hooks,
29 struct grub_efiemu_configuration_table
30 *config_tables)
32 grub_err_t err;
33 int conftable_handle;
34 struct grub_efiemu_configuration_table *cur;
35 struct grub_efiemu_prepare_hook *curhook;
37 int cntconftables = 0;
38 TYPE (grub_efiemu_configuration_table) *conftables = 0;
39 TYPE (grub_efiemu_runtime_services) *runtime_services;
40 int i;
41 int handle;
42 grub_off_t off;
44 grub_dprintf ("efiemu", "Preparing EfiEmu\n");
46 /* Request space for the list of configuration tables */
47 for (cur = config_tables; cur; cur = cur->next)
48 cntconftables++;
49 conftable_handle
50 = grub_efiemu_request_memalign (GRUB_EFIEMU_PAGESIZE,
51 cntconftables * sizeof (*conftables),
52 GRUB_EFI_RUNTIME_SERVICES_DATA);
54 /* Switch from phase 1 (counting) to phase 2 (real job) */
55 grub_efiemu_alloc_syms ();
56 grub_efiemu_mm_do_alloc ();
58 grub_efiemu_system_table32 = 0;
59 grub_efiemu_system_table64 = 0;
61 /* Execute hooks */
62 for (curhook = prepare_hooks; curhook; curhook = curhook->next)
63 curhook->hook (curhook->data);
65 /* Move runtime to its due place */
66 if ((err = grub_efiemu_loadcore_load ()))
68 grub_efiemu_unload ();
69 return err;
72 if ((err = grub_efiemu_resolve_symbol ("efiemu_system_table",
73 &handle, &off)))
75 grub_efiemu_unload ();
76 return err;
79 SUFFIX (grub_efiemu_system_table)
80 = (TYPE (grub_efi_system_table) *)
81 ((grub_uint8_t *)grub_efiemu_mm_obtain_request (handle) + off);
83 /* compute CRC32 of runtime_services */
84 if ((err = grub_efiemu_resolve_symbol ("efiemu_runtime_services",
85 &handle, &off)))
86 return err;
87 runtime_services = (TYPE (grub_efiemu_runtime_services) *)
88 ((grub_uint8_t *)grub_efiemu_mm_obtain_request (handle) + off);
89 runtime_services->hdr.crc32 = 0;
90 runtime_services->hdr.crc32 = grub_getcrc32
91 (0, runtime_services, runtime_services->hdr.header_size);
93 /* Put pointer to the list of configuration tables in system table */
94 grub_efiemu_write_value
95 (&(SUFFIX (grub_efiemu_system_table)->configuration_table), 0,
96 conftable_handle, 0, 1,
97 sizeof (SUFFIX (grub_efiemu_system_table)->configuration_table));
98 SUFFIX(grub_efiemu_system_table)->num_table_entries = cntconftables;
100 /* Fill the list of configuration tables */
101 conftables = (TYPE (grub_efiemu_configuration_table) *)
102 grub_efiemu_mm_obtain_request (conftable_handle);
103 i = 0;
104 for (cur = config_tables; cur; cur = cur->next, i++)
106 grub_memcpy (&(conftables[i].vendor_guid), &(cur->guid),
107 sizeof (cur->guid));
108 if (cur->get_table)
109 conftables[i].vendor_table
110 = PTR_TO_UINT64 (cur->get_table (cur->data));
111 else
112 conftables[i].vendor_table = PTR_TO_UINT64 (cur->data);
115 /* compute CRC32 of system table */
116 SUFFIX (grub_efiemu_system_table)->hdr.crc32 = 0;
117 SUFFIX (grub_efiemu_system_table)->hdr.crc32
118 = grub_getcrc32 (0, SUFFIX (grub_efiemu_system_table),
119 SUFFIX (grub_efiemu_system_table)->hdr.header_size);
121 grub_dprintf ("efiemu","system_table = %p, runtime_services = %p,"
122 " conftables = %p (%d entries)\n",
123 SUFFIX (grub_efiemu_system_table), runtime_services,
124 conftables, cntconftables);
126 return GRUB_ERR_NONE;