1 | /*␊ |
2 | * Copyright (c) 2009 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | #include <mach-o/fat.h>␊ |
7 | #include <libkern/OSByteOrder.h>␊ |
8 | #include <mach/machine.h>␊ |
9 | ␊ |
10 | #include "libsaio.h"␊ |
11 | #include "boot.h"␊ |
12 | ␊ |
13 | #include "sl.h"␊ |
14 | #include "bootstruct.h"␊ |
15 | #include "platform.h"␊ |
16 | #include "xml.h"␊ |
17 | #include "drivers.h"␊ |
18 | #include "modules.h"␊ |
19 | ␊ |
20 | int␉␉runNetbookInstaller = 0;␊ |
21 | ␊ |
22 | long NBI_LoadDrivers( char * dirSpec );␊ |
23 | void NBI_md0Ramdisk();␊ |
24 | void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6);␊ |
25 | //void NBI_loadBootGraphics(void);␊ |
26 | void NBI_md0Ramdisk_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6);␊ |
27 | ␊ |
28 | #ifdef NBP_SUPPORT␊ |
29 | extern long NetLoadDrivers(char *dirSpec);␊ |
30 | #endif␊ |
31 | extern long FileLoadDrivers(char *dirSpec, long plugin);␊ |
32 | ␊ |
33 | extern long LoadDriverMKext(char *fileSpec);␊ |
34 | extern long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
35 | ␊ |
36 | extern long MatchLibraries( void );␊ |
37 | #if UNUSED␊ |
38 | extern long MatchPersonalities( void );␊ |
39 | #endif␊ |
40 | extern long LoadMatchedModules( void );␊ |
41 | extern long InitDriverSupport(void);␊ |
42 | extern long GetDriverGbl(void);␊ |
43 | ␊ |
44 | void NetbookInstaller_start(void);␊ |
45 | void NetbookInstaller_start(void)␊ |
46 | {␉ ␊ |
47 | ␉register_hook_callback("PreBoot", &NBI_PreBoot_hook);␉␉␊ |
48 | }␊ |
49 | ␊ |
50 | void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)␊ |
51 | {␉␊ |
52 | ␉␊ |
53 | ␉bool dummyVal = 0;␊ |
54 | ␉config_file_t systemVersion;␊ |
55 | ␉␊ |
56 | ␉char valid = false;␊ |
57 | ␉const char *val;␊ |
58 | ␉int len;␊ |
59 | ␉const char* gPrevMacOSBuildVersion;␊ |
60 | ␉␊ |
61 | ␉␊ |
62 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
63 | ␉{␊ |
64 | ␉␉valid = true;␊ |
65 | ␉}␊ |
66 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
67 | ␉{␊ |
68 | ␉␉valid = true;␊ |
69 | ␉}␊ |
70 | ␉␊ |
71 | ␉if (valid)␊ |
72 | ␉{␊ |
73 | ␉␉if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))␊ |
74 | ␉␉{␊ |
75 | ␉␉␉␊ |
76 | ␉␉␉if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))␊ |
77 | ␉␉␉{␊ |
78 | ␉␉␉␉if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))␊ |
79 | ␉␉␉␉{␊ |
80 | ␉␉␉␉␉if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||␊ |
81 | ␉␉␉␉␉ strcmp(val, gPrevMacOSBuildVersion) != 0␊ |
82 | ␉␉␉␉␉ )␊ |
83 | ␉␉␉␉␉{␊ |
84 | ␉␉␉␉␉␉runNetbookInstaller = 1;␊ |
85 | ␉␉␉␉␉}␊ |
86 | ␉␉␉␉␉else ␊ |
87 | ␉␉␉␉␉{␊ |
88 | ␉␉␉␉␉␉// Only allow restore from hibernation if the system hasn't changed, probably a bad idea though␊ |
89 | ␉␉␉␉␉␉//char* val="/private/var/vm/sleepimage";␊ |
90 | ␉␉␉␉␉␉␊ |
91 | ␉␉␉␉␉␉// Do this first to be sure that root volume is mounted␊ |
92 | ␉␉␉␉␉␉//ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
93 | ␉␉␉␉␉␉␊ |
94 | ␉␉␉␉␉␉//printf("System version has not changed\n");␊ |
95 | ␉␉␉␉␉␉//runNetbookInstaller = 0;␊ |
96 | ␉␉␉␉␉␉␊ |
97 | ␉␉␉␉␉}␊ |
98 | ␉␉␉␉␉␊ |
99 | ␉␉␉␉}␊ |
100 | ␉␉␉}␊ |
101 | ␉␉}␊ |
102 | ␉}␊ |
103 | ␉␊ |
104 | ␉␊ |
105 | ␉␊ |
106 | ␉␊ |
107 | ␉␊ |
108 | ␉if (!runNetbookInstaller && getBoolForKey("recovery", &dummyVal, DEFAULT_BOOT_CONFIG) && dummyVal)␊ |
109 | ␉{␊ |
110 | ␉␉if(dummyVal) runNetbookInstaller = 2;␊ |
111 | ␉}␊ |
112 | ␉␊ |
113 | ␉if(runNetbookInstaller)␊ |
114 | ␉{␊ |
115 | ␉␉␊ |
116 | ␉␉replace_system_function("_LoadDrivers", &NBI_LoadDrivers);␊ |
117 | ␉␉␊ |
118 | ␉␉if(runNetbookInstaller == 1 )␊ |
119 | ␉␉{␊ |
120 | ␉␉␉if (execute_hook("isRamDiskRegistred", NULL, NULL, NULL, NULL, NULL, NULL) == EFI_SUCCESS)␊ |
121 | ␉␉␉{␊ |
122 | ␉␉␉␉replace_function_any("_md0Ramdisk", &NBI_md0Ramdisk);␊ |
123 | ␉␉␉} ␊ |
124 | ␉␉␉else␊ |
125 | ␉␉␉{␊ |
126 | ␉␉␉␉register_hook_callback("md0Ramdisk", NBI_md0Ramdisk_hook);␊ |
127 | ␉␉␉}␊ |
128 | ␊ |
129 | ␉␉}␊ |
130 | ␉␉␊ |
131 | ␉␉// Force arch=i386 + -v␊ |
132 | ␉␉//archCpuType = CPU_TYPE_I386;␊ |
133 | ␉␉safe_set_env(envarchCpuType, CPU_TYPE_I386);␊ |
134 | ␉␉safe_set_env(envgVerboseMode, true);␊ |
135 | ␉}␊ |
136 | }␊ |
137 | ␊ |
138 | void NBI_md0Ramdisk_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)␊ |
139 | {␊ |
140 | ␉NBI_md0Ramdisk();␉␊ |
141 | }␊ |
142 | ␊ |
143 | typedef struct RAMDiskParam␊ |
144 | {␊ |
145 | ␉ppnum_t base;␊ |
146 | ␉unsigned int size;␊ |
147 | } RAMDiskParam;␊ |
148 | ␊ |
149 | void NBI_md0Ramdisk()␊ |
150 | {␊ |
151 | ␉RAMDiskParam ramdiskPtr;␊ |
152 | ␉char filename[512];␊ |
153 | ␉int fh = -1;␊ |
154 | ␉␊ |
155 | ␉// TODO: embed NBI.img in this file␊ |
156 | ␉// If runNetbookInstaller is true, then the system has changed states, patch it ␊ |
157 | ␉sprintf(filename, "%s", "Extra/NetbookInstaller.img");;␊ |
158 | ␉fh = open(filename);␊ |
159 | ␉␊ |
160 | ␉if (fh >= 0)␊ |
161 | ␉{␊ |
162 | ␉␉verbose("Enabling ramdisk %s\n", filename);␊ |
163 | ␉␉␊ |
164 | ␉␉ramdiskPtr.size = file_size(fh);␊ |
165 | ␉␉ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);␊ |
166 | ␉␉␊ |
167 | ␉␉if(ramdiskPtr.size && ramdiskPtr.base)␊ |
168 | ␉␉{␊ |
169 | ␉␉␉// Read new ramdisk image contents in kernel memory.␊ |
170 | ␉␉␉if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)␊ |
171 | ␉␉␉{␉␉␉␉␊ |
172 | ␊ |
173 | AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size);␊ |
174 | ␊ |
175 | ␉␉␉␉Node* node = DT__FindNode("/chosen/memory-map", false);␊ |
176 | ␉␉␉␉if(node != NULL)␊ |
177 | ␉␉␉␉{␊ |
178 | ␉␉␉␉␉DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);␉␉␊ |
179 | ␉␉␉␉}␊ |
180 | ␉␉␉␉else␊ |
181 | ␉␉␉␉{␊ |
182 | ␉␉␉␉␉verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);␊ |
183 | ␉␉␉␉}␊ |
184 | ␉␉␉}␊ |
185 | ␉␉␉else␊ |
186 | ␉␉␉{␊ |
187 | ␉␉␉␉verbose("Unable to read md0 image %s.\n", filename);␊ |
188 | ␉␉␉}␉␉␉␊ |
189 | ␉␉}␊ |
190 | ␉␉else␊ |
191 | ␉␉{␊ |
192 | ␉␉␉verbose("md0 image %s is empty.\n", filename);␊ |
193 | ␉␉}␊ |
194 | ␉␉␊ |
195 | ␉␉close(fh);␊ |
196 | ␉␉␊ |
197 | ␉}␊ |
198 | }␊ |
199 | ␊ |
200 | ␊ |
201 | long NBI_LoadDrivers( char * dirSpec )␊ |
202 | {␊ |
203 | ␉␊ |
204 | char dirSpecExtra[1024];␊ |
205 | ␉␊ |
206 | if ( InitDriverSupport() != 0 )␊ |
207 | return 0;␊ |
208 | ␊ |
209 | ␉int step = 0;␊ |
210 | ␉execute_hook("ramDiskLoadDrivers", &step, NULL, NULL, NULL, NULL, NULL);␊ |
211 | #ifdef NBP_SUPPORT␉␊ |
212 | if ( get_env(envgBootFileType) == kNetworkDeviceType )␊ |
213 | {␊ |
214 | if (NetLoadDrivers(dirSpec) != 0)␊ |
215 | ␉␉{␊ |
216 | error("Could not load drivers from the network\n");␊ |
217 | return -1;␊ |
218 | }␊ |
219 | }␊ |
220 | else␊ |
221 | #endif␊ |
222 | ␉␉if ( get_env(envgBootFileType) == kBlockDeviceType )␊ |
223 | ␉␉{␊ |
224 | ␉␉␉verbose("Loading Recovery Extensions\n");␊ |
225 | ␉␉␉strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");␊ |
226 | ␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
227 | ␉␉␉␊ |
228 | #ifdef BOOT_HELPER_SUPPORT␉␉␉␊ |
229 | ␉␉␉// TODO: fix this, the order does matter, and it's not correct now.␊ |
230 | ␉␉␉// Also try to load Extensions from boot helper partitions.␊ |
231 | ␉␉␉if (((BVRef)(uint32_t)get_env(envgBootVolume))->flags & kBVFlagBooter)␊ |
232 | ␉␉␉{␊ |
233 | ␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
234 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
235 | ␉␉␉␉{␊ |
236 | ␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
237 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
238 | ␉␉␉␉␉{␊ |
239 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
240 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
241 | ␉␉␉␉␉}␊ |
242 | ␉␉␉␉}␊ |
243 | ␉␉␉}␊ |
244 | #endif␊ |
245 | char * MKextName = (char*)(uint32_t)get_env(envMKextName);␊ |
246 | ␊ |
247 | ␉␉␉if (MKextName[0] != '\0')␊ |
248 | ␉␉␉{␊ |
249 | ␉␉␉␉verbose("LoadDrivers: Loading from [%s]\n", MKextName);␊ |
250 | ␉␉␉␉if ( LoadDriverMKext(MKextName) != 0 )␊ |
251 | ␉␉␉␉{␊ |
252 | ␉␉␉␉␉error("Could not load %s\n", MKextName);␊ |
253 | ␉␉␉␉␉return -1;␊ |
254 | ␉␉␉␉}␊ |
255 | ␉␉␉}␊ |
256 | ␉␉␉else␊ |
257 | ␉␉␉{␊ |
258 | char * ExtensionsSpec = (char*)(uint32_t)get_env(envDriverExtSpec);␊ |
259 | ␊ |
260 | ␉␉␉␉strcpy(ExtensionsSpec, dirSpec);␊ |
261 | ␉␉␉␉strcat(ExtensionsSpec, "System/Library/");␊ |
262 | ␉␉␉␉FileLoadDrivers(ExtensionsSpec, 0);␊ |
263 | ␉␉␉}␊ |
264 | ␉␉}␊ |
265 | ␉␉else␊ |
266 | ␉␉{␊ |
267 | ␉␉␉return 0;␊ |
268 | ␉␉}␊ |
269 | #if UNUSED␊ |
270 | MatchPersonalities();␊ |
271 | #endif␊ |
272 | MatchLibraries();␊ |
273 | ␉␊ |
274 | LoadMatchedModules();␊ |
275 | ␉␊ |
276 | return 0;␊ |
277 | }␊ |
278 | |