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