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