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