Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / efiemu / prepare.c
blob2aef8634dec70c11d116d9771f0f268fbf71ac33
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/types.h>
24 #include <grub/efiemu/efiemu.h>
25 #include <grub/crypto.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 struct SUFFIX (grub_efiemu_configuration_table) *conftables = 0;
39 int i;
40 int handle;
41 grub_off_t off;
43 grub_dprintf ("efiemu", "Preparing EfiEmu\n");
45 /* Request space for the list of configuration tables */
46 for (cur = config_tables; cur; cur = cur->next)
47 cntconftables++;
48 conftable_handle
49 = grub_efiemu_request_memalign (GRUB_EFIEMU_PAGESIZE,
50 cntconftables * sizeof (*conftables),
51 GRUB_EFI_RUNTIME_SERVICES_DATA);
53 /* Switch from phase 1 (counting) to phase 2 (real job) */
54 grub_efiemu_alloc_syms ();
55 grub_efiemu_mm_do_alloc ();
56 grub_efiemu_write_sym_markers ();
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 err = grub_efiemu_loadcore_load ();
67 if (err)
69 grub_efiemu_unload ();
70 return err;
73 err = grub_efiemu_resolve_symbol ("efiemu_system_table", &handle, &off);
74 if (err)
76 grub_efiemu_unload ();
77 return err;
80 SUFFIX (grub_efiemu_system_table)
81 = (struct SUFFIX (grub_efi_system_table) *)
82 ((grub_uint8_t *) grub_efiemu_mm_obtain_request (handle) + off);
84 /* Put pointer to the list of configuration tables in system table */
85 grub_efiemu_write_value
86 (&(SUFFIX (grub_efiemu_system_table)->configuration_table), 0,
87 conftable_handle, 0, 1,
88 sizeof (SUFFIX (grub_efiemu_system_table)->configuration_table));
89 SUFFIX(grub_efiemu_system_table)->num_table_entries = cntconftables;
91 /* Fill the list of configuration tables */
92 conftables = (struct SUFFIX (grub_efiemu_configuration_table) *)
93 grub_efiemu_mm_obtain_request (conftable_handle);
94 i = 0;
95 for (cur = config_tables; cur; cur = cur->next, i++)
97 grub_memcpy (&(conftables[i].vendor_guid), &(cur->guid),
98 sizeof (cur->guid));
99 if (cur->get_table)
100 conftables[i].vendor_table = (grub_addr_t) cur->get_table (cur->data);
101 else
102 conftables[i].vendor_table = (grub_addr_t) cur->data;
105 err = SUFFIX (grub_efiemu_crc) ();
106 if (err)
108 grub_efiemu_unload ();
109 return err;
112 grub_dprintf ("efiemu","system_table = %p, conftables = %p (%d entries)\n",
113 SUFFIX (grub_efiemu_system_table), conftables, cntconftables);
115 return GRUB_ERR_NONE;
118 grub_err_t
119 SUFFIX (grub_efiemu_crc) (void)
121 grub_err_t err;
122 int handle;
123 grub_off_t off;
124 struct SUFFIX (grub_efiemu_runtime_services) *runtime_services;
125 grub_uint8_t crc32_context[GRUB_MD_CRC32->contextsize];
127 /* compute CRC32 of runtime_services */
128 err = grub_efiemu_resolve_symbol ("efiemu_runtime_services",
129 &handle, &off);
130 if (err)
131 return err;
133 runtime_services = (struct SUFFIX (grub_efiemu_runtime_services) *)
134 ((grub_uint8_t *) grub_efiemu_mm_obtain_request (handle) + off);
136 runtime_services->hdr.crc32 = 0;
137 GRUB_MD_CRC32->init(crc32_context);
138 GRUB_MD_CRC32->write(crc32_context, runtime_services, runtime_services->hdr.header_size);
139 GRUB_MD_CRC32->final(crc32_context);
141 runtime_services->hdr.crc32 =
142 grub_be_to_cpu32(*(grub_uint32_t*)GRUB_MD_CRC32->read(crc32_context));
144 err = grub_efiemu_resolve_symbol ("efiemu_system_table", &handle, &off);
145 if (err)
146 return err;
148 /* compute CRC32 of system table */
149 SUFFIX (grub_efiemu_system_table)->hdr.crc32 = 0;
150 GRUB_MD_CRC32->init(crc32_context);
151 GRUB_MD_CRC32->write(crc32_context, SUFFIX (grub_efiemu_system_table),
152 SUFFIX (grub_efiemu_system_table)->hdr.header_size);
153 GRUB_MD_CRC32->final(crc32_context);
154 SUFFIX (grub_efiemu_system_table)->hdr.crc32 =
155 grub_be_to_cpu32(*(grub_uint32_t*)GRUB_MD_CRC32->read(crc32_context));
157 grub_dprintf ("efiemu","system_table = %p, runtime_services = %p\n",
158 SUFFIX (grub_efiemu_system_table), runtime_services);
160 return GRUB_ERR_NONE;