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 char gMacOSVersion;␊ |
33 | ␊ |
34 | ␊ |
35 | extern long LoadDriverMKext(char *fileSpec);␊ |
36 | extern long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
37 | ␊ |
38 | extern long MatchLibraries( void );␊ |
39 | extern long MatchPersonalities( void );␊ |
40 | extern long LoadMatchedModules( void );␊ |
41 | extern long InitDriverSupport(void);␊ |
42 | extern char * gExtensionsSpec;␊ |
43 | extern char * gDriverSpec;␊ |
44 | extern char * gFileSpec;␊ |
45 | extern char * gTempSpec;␊ |
46 | extern char * gFileName;␊ |
47 | ␊ |
48 | #define kEnableNBI␉␉␉"EnableNBIModule"␊ |
49 | ␊ |
50 | void NetbookInstaller_start()␊ |
51 | {␊ |
52 | ␉bool enable = true;␊ |
53 | ␉getBoolForKey(kEnableNBI, &enable, &bootInfo->bootConfig) ;␊ |
54 | ␉␊ |
55 | ␉if (enable) ␊ |
56 | ␉register_hook_callback("PreBoot", &NBI_PreBoot_hook);␉␉␊ |
57 | ␉␊ |
58 | }␊ |
59 | ␊ |
60 | void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)␊ |
61 | {␉␊ |
62 | ␉␊ |
63 | ␉bool dummyVal = 0;␊ |
64 | ␉config_file_t systemVersion;␊ |
65 | ␉␊ |
66 | ␉char valid = false;␊ |
67 | ␉const char *val;␊ |
68 | ␉int len;␊ |
69 | ␉const char* gPrevMacOSBuildVersion;␊ |
70 | ␉␊ |
71 | ␉␊ |
72 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
73 | ␉{␊ |
74 | ␉␉valid = true;␊ |
75 | ␉}␊ |
76 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
77 | ␉{␊ |
78 | ␉␉valid = true;␊ |
79 | ␉}␊ |
80 | ␉␊ |
81 | ␉if (valid)␊ |
82 | ␉{␊ |
83 | ␉␉if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))␊ |
84 | ␉␉{␊ |
85 | ␉␉␉␊ |
86 | ␉␉␉if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))␊ |
87 | ␉␉␉{␊ |
88 | ␉␉␉␉if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))␊ |
89 | ␉␉␉␉{␊ |
90 | ␉␉␉␉␉if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||␊ |
91 | ␉␉␉␉␉ strcmp(val, gPrevMacOSBuildVersion) != 0␊ |
92 | ␉␉␉␉␉ )␊ |
93 | ␉␉␉␉␉{␊ |
94 | ␉␉␉␉␉␉runNetbookInstaller = 1;␊ |
95 | ␉␉␉␉␉}␊ |
96 | ␉␉␉␉␉else ␊ |
97 | ␉␉␉␉␉{␊ |
98 | ␉␉␉␉␉␉// Only allow restore from hibernation if the system hasn't changed, probably a bad idea though␊ |
99 | ␉␉␉␉␉␉//char* val="/private/var/vm/sleepimage";␊ |
100 | ␉␉␉␉␉␉␊ |
101 | ␉␉␉␉␉␉// Do this first to be sure that root volume is mounted␊ |
102 | ␉␉␉␉␉␉//ret = GetFileInfo(0, val, &flags, &sleeptime);␊ |
103 | ␉␉␉␉␉␉␊ |
104 | ␉␉␉␉␉␉//printf("System version has not changed\n");␊ |
105 | ␉␉␉␉␉␉//runNetbookInstaller = 0;␊ |
106 | ␉␉␉␉␉␉␊ |
107 | ␉␉␉␉␉}␊ |
108 | ␉␉␉␉␉␊ |
109 | ␉␉␉␉}␊ |
110 | ␉␉␉}␊ |
111 | ␉␉}␊ |
112 | ␉}␊ |
113 | ␉␊ |
114 | ␉␊ |
115 | ␉␊ |
116 | ␉␊ |
117 | ␉␊ |
118 | ␉if (!runNetbookInstaller && getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)␊ |
119 | ␉{␊ |
120 | ␉␉if(dummyVal) runNetbookInstaller = 2;␊ |
121 | ␉}␊ |
122 | ␉␊ |
123 | ␉if(runNetbookInstaller)␊ |
124 | ␉{␊ |
125 | ␉␉␊ |
126 | ␉␉replace_function("_LoadDrivers", &NBI_LoadDrivers);␊ |
127 | ␉␉␊ |
128 | ␉␉if(runNetbookInstaller == 1 )␊ |
129 | ␉␉{␊ |
130 | ␉␉␉if (execute_hook("isRamDiskRegistred", NULL, NULL, NULL, NULL, NULL, NULL) == EFI_SUCCESS) {␊ |
131 | ␉␉␉␉replace_function("_md0Ramdisk", &NBI_md0Ramdisk);␊ |
132 | ␉␉␉} else {␊ |
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 | error("Could not load drivers from the network\n");␊ |
224 | return -1;␊ |
225 | }␊ |
226 | }␊ |
227 | else␊ |
228 | #endif␊ |
229 | ␉␉if ( gBootFileType == kBlockDeviceType )␊ |
230 | ␉␉{␊ |
231 | ␉␉␉verbose("Loading Recovery Extensions\n");␊ |
232 | ␉␉␉strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");␊ |
233 | ␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
234 | ␉␉␉␊ |
235 | #ifdef BOOT_HELPER_SUPPORT␉␉␉␊ |
236 | ␉␉␉// TODO: fix this, the order does matter, and it's not correct now.␊ |
237 | ␉␉␉// Also try to load Extensions from boot helper partitions.␊ |
238 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
239 | ␉␉␉{␊ |
240 | ␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
241 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
242 | ␉␉␉␉{␊ |
243 | ␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
244 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
245 | ␉␉␉␉␉{␊ |
246 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
247 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
248 | ␉␉␉␉␉}␊ |
249 | ␉␉␉␉}␊ |
250 | ␉␉␉}␊ |
251 | #endif␊ |
252 | ␉␉␉␊ |
253 | ␉␉␉if (gMKextName[0] != '\0')␊ |
254 | ␉␉␉{␊ |
255 | ␉␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gMKextName);␊ |
256 | ␉␉␉␉if ( LoadDriverMKext(gMKextName) != 0 )␊ |
257 | ␉␉␉␉{␊ |
258 | ␉␉␉␉␉error("Could not load %s\n", gMKextName);␊ |
259 | ␉␉␉␉␉return -1;␊ |
260 | ␉␉␉␉}␊ |
261 | ␉␉␉}␊ |
262 | ␉␉␉else␊ |
263 | ␉␉␉{␊ |
264 | ␉␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
265 | ␉␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
266 | ␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
267 | ␉␉␉}␊ |
268 | ␉␉}␊ |
269 | ␉␉else␊ |
270 | ␉␉{␊ |
271 | ␉␉␉return 0;␊ |
272 | ␉␉}␊ |
273 | ␉␊ |
274 | MatchPersonalities();␊ |
275 | ␉␊ |
276 | MatchLibraries();␊ |
277 | ␉␊ |
278 | LoadMatchedModules();␊ |
279 | ␉␊ |
280 | return 0;␊ |
281 | }␊ |
282 | |