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