forked from aa15032261/apple_set_os-loader
-
Notifications
You must be signed in to change notification settings - Fork 7
/
bootx64_silent.c
196 lines (165 loc) · 5.24 KB
/
bootx64_silent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <efi.h>
#include "include/int_graphics.h"
#include "include/int_print.h"
#include "include/int_event.h"
#include "include/int_guid.h"
#include "include/int_dpath.h"
#include "include/pci_db.h"
#define APPLE_SET_OS_VENDOR "Apple Inc."
#define APPLE_SET_OS_VERSION "Mac OS X 10.15"
#pragma pack(1)
typedef struct {
UINT8 Desc;
UINT16 Len;
UINT8 ResType;
UINT8 GenFlag;
UINT8 SpecificFlag;
UINT64 AddrSpaceGranularity;
UINT64 AddrRangeMin;
UINT64 AddrRangeMax;
UINT64 AddrTranslationOffset;
UINT64 AddrLen;
} EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR;
typedef struct {
UINT16 VendorId;
UINT16 DeviceId;
UINT16 Command;
UINT16 Status;
UINT8 RevisionId;
UINT8 ClassCode[3];
UINT8 CacheLineSize;
UINT8 PrimaryLatencyTimer;
UINT8 HeaderType;
UINT8 Bist;
} PCI_COMMON_HEADER;
typedef struct {
UINT32 Bar[6]; // Base Address Registers
UINT32 CardBusCISPtr; // CardBus CIS Pointer
UINT16 SubVendorId; // Subsystem Vendor ID
UINT16 SubSystemId; // Subsystem ID
UINT32 ROMBar; // Expansion ROM Base Address
UINT8 CapabilitiesPtr; // Capabilities Pointer
UINT8 Reserved[3];
UINT32 Reserved1;
UINT8 InterruptLine; // Interrupt Line
UINT8 InterruptPin; // Interrupt Pin
UINT8 MinGnt; // Min_Gnt
UINT8 MaxLat; // Max_Lat
} PCI_DEVICE_HEADER;
typedef struct {
UINT32 CardBusSocketReg; // Cardus Socket/ExCA Base Address Register
UINT8 CapabilitiesPtr; // 14h in pci-cardbus bridge.
UINT8 Reserved;
UINT16 SecondaryStatus; // Secondary Status
UINT8 PciBusNumber; // PCI Bus Number
UINT8 CardBusBusNumber; // CardBus Bus Number
UINT8 SubordinateBusNumber; // Subordinate Bus Number
UINT8 CardBusLatencyTimer; // CardBus Latency Timer
UINT32 MemoryBase0; // Memory Base Register 0
UINT32 MemoryLimit0; // Memory Limit Register 0
UINT32 MemoryBase1;
UINT32 MemoryLimit1;
UINT32 IoBase0;
UINT32 IoLimit0; // I/O Base Register 0
UINT32 IoBase1; // I/O Limit Register 0
UINT32 IoLimit1;
UINT8 InterruptLine; // Interrupt Line
UINT8 InterruptPin; // Interrupt Pin
UINT16 BridgeControl; // Bridge Control
} PCI_CARDBUS_HEADER;
typedef union {
PCI_DEVICE_HEADER Device;
PCI_CARDBUS_HEADER CardBus;
} NON_COMMON_UNION;
typedef struct {
PCI_COMMON_HEADER Common;
NON_COMMON_UNION NonCommon;
UINT32 Data[48];
} PCI_CONFIG_SPACE;
#pragma pack(0)
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable)
{
EFI_STATUS Status;
EFI_BOOT_SERVICES* BS = SystemTable->BootServices;
// get apple_set_os protocol
EFI_HANDLE* AppleSetOsHandleBuf;
UINTN AppleSetOsHandleCount = 0;
EFI_GUID apple_set_os_guid = APPLE_SET_OS_GUID;
Status = BS->LocateHandleBuffer(
ByProtocol,
&apple_set_os_guid,
NULL,
&AppleSetOsHandleCount,
&AppleSetOsHandleBuf
);
// find and load bootx64_original.efi
EFI_LOADED_IMAGE_PROTOCOL* LoadedImage;
EFI_DEVICE_PATH* DevicePath = NULL;
EFI_HANDLE DriverHandle;
EFI_GUID efi_loaded_image_protocol_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
Status = BS->HandleProtocol(ImageHandle, &efi_loaded_image_protocol_guid, (VOID**) &LoadedImage);
if (EFI_ERROR(Status) || LoadedImage == NULL) {
goto halt;
}
DevicePath = _INT_FileDevicePath(
BS,
LoadedImage->DeviceHandle,
L"\\EFI\\Boot\\bootx64_original.efi"
);
if (DevicePath == NULL) {
goto halt;
}
// Attempt to load the driver.
Status = BS->LoadImage(FALSE, ImageHandle, DevicePath, NULL, 0, &DriverHandle);
_INT_FreePool(BS, DevicePath);
DevicePath = NULL;
if (EFI_ERROR(Status)) {
goto halt;
}
Status = BS->OpenProtocol(
DriverHandle,
&efi_loaded_image_protocol_guid,
(VOID**)&LoadedImage,
ImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR(Status)) {
goto halt;
}
// load apple_set_os
for(UINTN i = 0; i < AppleSetOsHandleCount; i++) {
EFI_APPLE_SET_OS_IFACE* SetOsIface = NULL;
Status = BS->OpenProtocol(
AppleSetOsHandleBuf[i],
&apple_set_os_guid,
(VOID**)&SetOsIface,
ImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR(Status)) {
} else {
if (SetOsIface->Version != 0){
Status = SetOsIface->SetOsVendor((CHAR8*) APPLE_SET_OS_VENDOR);
Status = SetOsIface->SetOsVersion((CHAR8*) APPLE_SET_OS_VERSION);
}
}
}
_INT_FreePool(BS, AppleSetOsHandleBuf);
// Load was a success - attempt to start the driver
Status = BS->StartImage(DriverHandle, NULL, NULL);
if (EFI_ERROR(Status)) {
goto halt;
}
BS->Exit(ImageHandle, EFI_UNSUPPORTED, 0, NULL);
return EFI_UNSUPPORTED;
halt:
while (1) {
for (UINT16 j = 0; j < 4000; j++) {
BS->Stall(10000);
}
}
BS->Exit(ImageHandle, EFI_UNSUPPORTED, 0, NULL);
return EFI_UNSUPPORTED;
}