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