1 | /* $Id: VBoxAppleSim.c 49089 2013-10-14 16:30:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxAppleSim.c - VirtualBox Apple Firmware simulation support
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <Library/BaseMemoryLib.h>
|
---|
32 | #include <Library/DebugLib.h>
|
---|
33 | #include <Library/UefiBootServicesTableLib.h>
|
---|
34 | #include <Library/UefiLib.h>
|
---|
35 |
|
---|
36 | #include <Protocol/DevicePathToText.h>
|
---|
37 |
|
---|
38 | #include <IndustryStandard/Acpi10.h>
|
---|
39 | #include <IndustryStandard/Acpi20.h>
|
---|
40 | #include <IndustryStandard/SmBios.h>
|
---|
41 |
|
---|
42 | #include <Guid/SmBios.h>
|
---|
43 | #include <Guid/Acpi.h>
|
---|
44 | #include <Guid/Mps.h>
|
---|
45 |
|
---|
46 | #include "VBoxPkg.h"
|
---|
47 | #include "DevEFI.h"
|
---|
48 | #include "iprt/asm.h"
|
---|
49 |
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * External functions
|
---|
53 | */
|
---|
54 | EFI_STATUS EFIAPI
|
---|
55 | CpuUpdateDataHub(EFI_BOOT_SERVICES * bs,
|
---|
56 | UINT64 FSBFrequency,
|
---|
57 | UINT64 TSCFrequency,
|
---|
58 | UINT64 CPUFrequency);
|
---|
59 |
|
---|
60 | EFI_STATUS EFIAPI
|
---|
61 | InitializeConsoleSim (IN EFI_HANDLE ImageHandle,
|
---|
62 | IN EFI_SYSTEM_TABLE *SystemTable);
|
---|
63 |
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Internal Functions *
|
---|
67 | */
|
---|
68 | static UINT32
|
---|
69 | GetVmVariable(UINT32 Variable, CHAR8* Buffer, UINT32 Size )
|
---|
70 | {
|
---|
71 | UINT32 VarLen, i;
|
---|
72 |
|
---|
73 |
|
---|
74 | ASMOutU32(EFI_INFO_PORT, Variable);
|
---|
75 | VarLen = ASMInU32(EFI_INFO_PORT);
|
---|
76 |
|
---|
77 | for (i=0; i < VarLen && i < Size; i++)
|
---|
78 | {
|
---|
79 | Buffer[i] = ASMInU8(EFI_INFO_PORT);
|
---|
80 | }
|
---|
81 |
|
---|
82 | return VarLen;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * GUIDs
|
---|
87 | */
|
---|
88 | EFI_GUID gEfiAppleNvramGuid = {
|
---|
89 | 0x4D1EDE05, 0x38C7, 0x4A6A, {0x9C, 0xC6, 0x4B, 0xCC, 0xA8, 0xB3, 0x8C, 0x14 }
|
---|
90 | };
|
---|
91 |
|
---|
92 | EFI_GUID gEfiAppleBootGuid = {
|
---|
93 | 0x7C436110, 0xAB2A, 0x4BBB, {0xA8, 0x80, 0xFE, 0x41, 0x99, 0x5C, 0x9F, 0x82}
|
---|
94 | };
|
---|
95 |
|
---|
96 | EFI_GUID gEfiAppleVarGuid = {
|
---|
97 | 0x91BD12FE, 0xF6C3, 0x44FB, {0xA5, 0xB7, 0x51, 0x22, 0xAB, 0x30, 0x3A, 0xE0}
|
---|
98 | };
|
---|
99 |
|
---|
100 | EFI_GUID gEfiUnknown1ProtocolGuid = {
|
---|
101 | 0xDD8E06AC, 0x00E2, 0x49A9, {0x88, 0x8F, 0xFA, 0x46, 0xDE, 0xD4, 0x0A, 0x52}
|
---|
102 | };
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Typedefs
|
---|
106 | */
|
---|
107 | typedef struct _APPLE_GETVAR_PROTOCOL APPLE_GETVAR_PROTOCOL;
|
---|
108 |
|
---|
109 | typedef
|
---|
110 | EFI_STATUS
|
---|
111 | (EFIAPI *APPLE_GETVAR_PROTOCOL_GET_DEVICE_PROPS) (
|
---|
112 | IN APPLE_GETVAR_PROTOCOL *This,
|
---|
113 | IN CHAR8 *Buffer,
|
---|
114 | IN OUT UINT32 *BufferSize);
|
---|
115 |
|
---|
116 |
|
---|
117 | struct _APPLE_GETVAR_PROTOCOL
|
---|
118 | {
|
---|
119 | UINT64 Magic;
|
---|
120 | EFI_STATUS(EFIAPI *Unknown0)(IN VOID *);
|
---|
121 | EFI_STATUS(EFIAPI *Unknown1)(IN VOID *);
|
---|
122 | EFI_STATUS(EFIAPI *Unknown2)(IN VOID *);
|
---|
123 | APPLE_GETVAR_PROTOCOL_GET_DEVICE_PROPS GetDevProps;
|
---|
124 | };
|
---|
125 |
|
---|
126 |
|
---|
127 | #define IMPL_STUB(iface, num) \
|
---|
128 | EFI_STATUS EFIAPI \
|
---|
129 | iface##Unknown##num(IN VOID *This) \
|
---|
130 | { \
|
---|
131 | Print(L"Unknown%d of %a called", num, #iface); \
|
---|
132 | /*DebugAssert(__FILE__, __LINE__, __FUNCTION__);*/ \
|
---|
133 | return EFI_SUCCESS; \
|
---|
134 | }
|
---|
135 |
|
---|
136 | IMPL_STUB(GetVar, 0)
|
---|
137 | IMPL_STUB(GetVar, 1)
|
---|
138 | IMPL_STUB(GetVar, 2)
|
---|
139 |
|
---|
140 |
|
---|
141 | EFI_STATUS EFIAPI
|
---|
142 | GetDeviceProps(IN APPLE_GETVAR_PROTOCOL *This,
|
---|
143 | IN CHAR8 *Buffer,
|
---|
144 | IN OUT UINT32 *BufferSize)
|
---|
145 | {
|
---|
146 | UINT32 BufLen = *BufferSize, DataLen;
|
---|
147 |
|
---|
148 | DataLen = GetVmVariable(EFI_INFO_INDEX_DEVICE_PROPS, Buffer, BufLen);
|
---|
149 | *BufferSize = DataLen;
|
---|
150 |
|
---|
151 | if (DataLen > BufLen)
|
---|
152 | return EFI_BUFFER_TOO_SMALL;
|
---|
153 |
|
---|
154 | return EFI_SUCCESS;
|
---|
155 | }
|
---|
156 |
|
---|
157 | APPLE_GETVAR_PROTOCOL gPrivateVarHandler =
|
---|
158 | {
|
---|
159 | /* Magic = */ 0, /** @todo figure out what's here on a real system? */
|
---|
160 | GetVarUnknown0,
|
---|
161 | GetVarUnknown1,
|
---|
162 | GetVarUnknown2,
|
---|
163 | GetDeviceProps
|
---|
164 | };
|
---|
165 |
|
---|
166 | EFI_STATUS EFIAPI
|
---|
167 | UnknownHandlerImpl()
|
---|
168 | {
|
---|
169 | Print(L"Unknown called\n");
|
---|
170 | return EFI_SUCCESS;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* array of pointers to function */
|
---|
174 | EFI_STATUS (EFIAPI *gUnknownProtoHandler[])() =
|
---|
175 | {
|
---|
176 | UnknownHandlerImpl,
|
---|
177 | UnknownHandlerImpl,
|
---|
178 | UnknownHandlerImpl,
|
---|
179 | UnknownHandlerImpl,
|
---|
180 | UnknownHandlerImpl,
|
---|
181 | UnknownHandlerImpl,
|
---|
182 | UnknownHandlerImpl,
|
---|
183 | UnknownHandlerImpl,
|
---|
184 | UnknownHandlerImpl,
|
---|
185 | UnknownHandlerImpl,
|
---|
186 | UnknownHandlerImpl,
|
---|
187 | UnknownHandlerImpl,
|
---|
188 | UnknownHandlerImpl,
|
---|
189 | UnknownHandlerImpl,
|
---|
190 | UnknownHandlerImpl,
|
---|
191 | UnknownHandlerImpl,
|
---|
192 | UnknownHandlerImpl,
|
---|
193 | UnknownHandlerImpl
|
---|
194 | };
|
---|
195 |
|
---|
196 | EFI_STATUS EFIAPI
|
---|
197 | SetPrivateVarProto(IN EFI_HANDLE ImageHandle, EFI_BOOT_SERVICES * bs)
|
---|
198 | {
|
---|
199 | EFI_STATUS rc;
|
---|
200 |
|
---|
201 | rc = gBS->InstallMultipleProtocolInterfaces (
|
---|
202 | &ImageHandle,
|
---|
203 | &gEfiAppleVarGuid,
|
---|
204 | &gPrivateVarHandler,
|
---|
205 | NULL
|
---|
206 | );
|
---|
207 | ASSERT_EFI_ERROR (rc);
|
---|
208 |
|
---|
209 | return EFI_SUCCESS;
|
---|
210 | }
|
---|
211 |
|
---|
212 | EFI_STATUS EFIAPI
|
---|
213 | SetProperVariables(IN EFI_HANDLE ImageHandle, EFI_RUNTIME_SERVICES * rs)
|
---|
214 | {
|
---|
215 | EFI_STATUS rc;
|
---|
216 | UINT32 vBackgroundClear = 0x00000000;
|
---|
217 | UINT32 vFwFeatures = 0x80000015;
|
---|
218 | UINT32 vFwFeaturesMask = 0x800003ff;
|
---|
219 |
|
---|
220 | // -legacy acpi=0xffffffff acpi_debug=0xfffffff panic_io_port=0xef11 io=0xfffffffe trace=4096 io=0xffffffef -v serial=2 serialbaud=9600
|
---|
221 | // 0x10 makes kdb default, thus 0x15e for kdb, 0x14e for gdb
|
---|
222 |
|
---|
223 | //static const CHAR8 vBootArgs[] = "debug=0x15e keepsyms=1 acpi=0xffffffff acpi_debug=0xff acpi_level=7 -v -x32 -s"; // or just "debug=0x8 -legacy"
|
---|
224 | // 0x14e for serial output
|
---|
225 | //static const CHAR8 vDefBootArgs[] = "debug=0x146 keepsyms=1 -v -serial=0x1";
|
---|
226 | static const CHAR8 vDefBootArgs[] = "keepsyms=1 -v -serial=0x1";
|
---|
227 | CHAR8 vBootArgs[256];
|
---|
228 | UINT32 BootArgsLen;
|
---|
229 |
|
---|
230 | BootArgsLen = GetVmVariable(EFI_INFO_INDEX_BOOT_ARGS, vBootArgs, sizeof vBootArgs);
|
---|
231 | if (BootArgsLen <= 1)
|
---|
232 | {
|
---|
233 | BootArgsLen = sizeof vDefBootArgs;
|
---|
234 | CopyMem(vBootArgs, vDefBootArgs, BootArgsLen);
|
---|
235 | }
|
---|
236 | rc = rs->SetVariable(L"BackgroundClear",
|
---|
237 | &gEfiAppleNvramGuid,
|
---|
238 | /* EFI_VARIABLE_NON_VOLATILE | */ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
|
---|
239 | sizeof(vBackgroundClear), &vBackgroundClear);
|
---|
240 |
|
---|
241 | rc = rs->SetVariable(L"FirmwareFeatures",
|
---|
242 | &gEfiAppleNvramGuid,
|
---|
243 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
|
---|
244 | sizeof(vFwFeatures), &vFwFeatures);
|
---|
245 |
|
---|
246 | rc = rs->SetVariable(L"FirmwareFeaturesMask",
|
---|
247 | &gEfiAppleNvramGuid,
|
---|
248 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
|
---|
249 | sizeof(vFwFeaturesMask), &vFwFeaturesMask);
|
---|
250 |
|
---|
251 | rc = rs->SetVariable(L"boot-args",
|
---|
252 | &gEfiAppleBootGuid,
|
---|
253 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
|
---|
254 | BootArgsLen, &vBootArgs);
|
---|
255 |
|
---|
256 | return EFI_SUCCESS;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * VBoxInitAppleSim entry point.
|
---|
261 | *
|
---|
262 | * @returns EFI status code.
|
---|
263 | *
|
---|
264 | * @param ImageHandle The image handle.
|
---|
265 | * @param SystemTable The system table pointer.
|
---|
266 | */
|
---|
267 | EFI_STATUS EFIAPI
|
---|
268 | VBoxInitAppleSim(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
|
---|
269 | {
|
---|
270 | EFI_STATUS rc;
|
---|
271 | UINT64 FSBFrequency;
|
---|
272 | UINT64 TSCFrequency;
|
---|
273 | UINT64 CPUFrequency;
|
---|
274 |
|
---|
275 | rc = SetProperVariables(ImageHandle, SystemTable->RuntimeServices);
|
---|
276 | ASSERT_EFI_ERROR (rc);
|
---|
277 |
|
---|
278 | rc = SetPrivateVarProto(ImageHandle, gBS);
|
---|
279 | ASSERT_EFI_ERROR (rc);
|
---|
280 |
|
---|
281 | GetVmVariable(EFI_INFO_INDEX_FSB_FREQUENCY, (CHAR8*)&FSBFrequency, sizeof FSBFrequency);
|
---|
282 | GetVmVariable(EFI_INFO_INDEX_TSC_FREQUENCY, (CHAR8*)&TSCFrequency, sizeof TSCFrequency);
|
---|
283 | GetVmVariable(EFI_INFO_INDEX_CPU_FREQUENCY, (CHAR8*)&CPUFrequency, sizeof CPUFrequency);
|
---|
284 |
|
---|
285 | rc = CpuUpdateDataHub(gBS, FSBFrequency, TSCFrequency, CPUFrequency);
|
---|
286 | ASSERT_EFI_ERROR (rc);
|
---|
287 |
|
---|
288 | rc = InitializeConsoleSim(ImageHandle, SystemTable);
|
---|
289 | ASSERT_EFI_ERROR (rc);
|
---|
290 |
|
---|
291 | rc = gBS->InstallMultipleProtocolInterfaces (
|
---|
292 | &ImageHandle,
|
---|
293 | &gEfiUnknown1ProtocolGuid,
|
---|
294 | gUnknownProtoHandler,
|
---|
295 | NULL
|
---|
296 | );
|
---|
297 | ASSERT_EFI_ERROR (rc);
|
---|
298 |
|
---|
299 | return EFI_SUCCESS;
|
---|
300 | }
|
---|
301 |
|
---|
302 | EFI_STATUS EFIAPI
|
---|
303 | VBoxDeinitAppleSim(IN EFI_HANDLE ImageHandle)
|
---|
304 | {
|
---|
305 | return EFI_SUCCESS;
|
---|
306 | }
|
---|