Root/
Source at commit 573 created 13 years 5 months ago. By meklort, NetbookInstaller module now works. | |
---|---|
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 "ramdisk.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);␊ |
25 | void NBI_loadBootGraphics(void);␊ |
26 | ␊ |
27 | #ifndef OPTION_ROM␊ |
28 | extern long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);␊ |
29 | ␊ |
30 | extern long NetLoadDrivers(char *dirSpec);␊ |
31 | extern long FileLoadDrivers(char *dirSpec, long plugin);␊ |
32 | #endif␊ |
33 | ␊ |
34 | extern char gMacOSVersion;␊ |
35 | ␊ |
36 | ␊ |
37 | extern long LoadDriverMKext(char *fileSpec);␊ |
38 | extern long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
39 | ␊ |
40 | extern long MatchLibraries( void );␊ |
41 | extern long MatchPersonalities( void );␊ |
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 | void NetbookInstaller_start()␊ |
51 | {␊ |
52 | ␉␊ |
53 | ␉config_file_t systemVersion;␊ |
54 | ␉␊ |
55 | ␉char valid = false;␊ |
56 | ␉const char *val;␊ |
57 | ␉int len;␊ |
58 | ␉const char* gPrevMacOSBuildVersion;␊ |
59 | ␉␊ |
60 | ␉␊ |
61 | ␉if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))␊ |
62 | ␉{␊ |
63 | ␉␉valid = true;␊ |
64 | ␉}␊ |
65 | ␉else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))␊ |
66 | ␉{␊ |
67 | ␉␉valid = true;␊ |
68 | ␉}␊ |
69 | ␉␊ |
70 | ␉if (valid)␊ |
71 | ␉{␊ |
72 | ␉␉if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))␊ |
73 | ␉␉{␊ |
74 | ␉␉␉␊ |
75 | ␉␉␉if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))␊ |
76 | ␉␉␉{␊ |
77 | ␉␉␉␉if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))␊ |
78 | ␉␉␉␉{␊ |
79 | ␉␉␉␉␉if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||␊ |
80 | ␉␉␉␉␉ strcmp(val, gPrevMacOSBuildVersion) != 0␊ |
81 | ␉␉␉␉␉ )␊ |
82 | ␉␉␉␉␉{␊ |
83 | ␉␉␉␉␉␉runNetbookInstaller = 1;␊ |
84 | ␉␉␉␉␉␉archCpuType = CPU_TYPE_I386;␊ |
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 | ␉//else printf("Unable to locate Extra/SystemVersion.LastPatched.plist\n");␊ |
107 | ␉␊ |
108 | ␉register_hook_callback("PreBoot", &NBI_PreBoot_hook);␉␉␊ |
109 | ␉␊ |
110 | }␊ |
111 | ␊ |
112 | void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4)␊ |
113 | {␊ |
114 | ␉extern bool usePngImage;␊ |
115 | ␉bool dummyVal = 0;␊ |
116 | ␉if (getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)␊ |
117 | ␉{␊ |
118 | ␉␉if(dummyVal) runNetbookInstaller = 2;␊ |
119 | ␉}␊ |
120 | ␉␊ |
121 | ␉if(runNetbookInstaller)␊ |
122 | ␉{␊ |
123 | ␉␉replace_function("_LoadDrivers", &NBI_LoadDrivers);␊ |
124 | ␉␉replace_function("_md0Ramdisk", &NBI_md0Ramdisk);␉␉␊ |
125 | ␉␉replace_function("_loadBootGraphics", &NBI_loadBootGraphics);␉␉␊ |
126 | ␉␉usePngImage = true;␊ |
127 | ␉}␊ |
128 | }␊ |
129 | ␊ |
130 | ␊ |
131 | void NBI_md0Ramdisk()␊ |
132 | {␊ |
133 | ␉RAMDiskParam ramdiskPtr;␊ |
134 | ␉char filename[512];␊ |
135 | ␉int fh = -1;␊ |
136 | ␉␊ |
137 | ␉// TODO: embed NBI.img in this file␊ |
138 | ␉// If runNetbookInstaller is true, then the system has changed states, patch it ␊ |
139 | ␉sprintf(filename, "%s", "Extra/NetbookInstaller.img");;␊ |
140 | ␉fh = open(filename, 0);␊ |
141 | ␉␊ |
142 | ␉if (fh >= 0)␊ |
143 | ␉{␊ |
144 | ␉␉verbose("Enabling ramdisk %s\n", filename);␊ |
145 | ␉␉␊ |
146 | ␉␉ramdiskPtr.size = file_size(fh);␊ |
147 | ␉␉ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);␊ |
148 | ␉␉␊ |
149 | ␉␉if(ramdiskPtr.size && ramdiskPtr.base)␊ |
150 | ␉␉{␊ |
151 | ␉␉␉// Read new ramdisk image contents in kernel memory.␊ |
152 | ␉␉␉if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)␊ |
153 | ␉␉␉{␊ |
154 | ␉␉␉␉AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);␊ |
155 | ␉␉␉␉Node* node = DT__FindNode("/chosen/memory-map", false);␊ |
156 | ␉␉␉␉if(node != NULL)␊ |
157 | ␉␉␉␉{␊ |
158 | ␉␉␉␉␉DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);␉␉␊ |
159 | ␉␉␉␉}␊ |
160 | ␉␉␉␉else␊ |
161 | ␉␉␉␉{␊ |
162 | ␉␉␉␉␉verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);␊ |
163 | ␉␉␉␉}␊ |
164 | ␉␉␉}␊ |
165 | ␉␉␉else␊ |
166 | ␉␉␉{␊ |
167 | ␉␉␉␉verbose("Unable to read md0 image %s.\n", filename);␊ |
168 | ␉␉␉}␉␉␉␊ |
169 | ␉␉}␊ |
170 | ␉␉else␊ |
171 | ␉␉{␊ |
172 | ␉␉␉verbose("md0 image %s is empty.\n", filename);␊ |
173 | ␉␉}␊ |
174 | ␉␉␊ |
175 | ␉␉close(fh);␊ |
176 | ␉␉␊ |
177 | ␉}␊ |
178 | }␊ |
179 | ␊ |
180 | ␊ |
181 | long NBI_LoadDrivers( char * dirSpec )␊ |
182 | {␊ |
183 | ␉␊ |
184 | char dirSpecExtra[1024];␊ |
185 | ␉␊ |
186 | if ( InitDriverSupport() != 0 )␊ |
187 | return 0;␊ |
188 | ␉␊ |
189 | #ifndef OPTION_ROM␊ |
190 | // Load extra drivers if a hook has been installed.␊ |
191 | if (LoadExtraDrivers_p != NULL)␊ |
192 | {␊ |
193 | (*LoadExtraDrivers_p)(&FileLoadDrivers);␊ |
194 | }␊ |
195 | ␉␊ |
196 | if ( gBootFileType == kNetworkDeviceType )␊ |
197 | {␊ |
198 | if (NetLoadDrivers(dirSpec) != 0) {␊ |
199 | error("Could not load drivers from the network\n");␊ |
200 | return -1;␊ |
201 | }␊ |
202 | }␊ |
203 | else␊ |
204 | #endif␊ |
205 | ␉␉if ( gBootFileType == kBlockDeviceType )␊ |
206 | ␉␉{␊ |
207 | ␉␉␉verbose("Loading Recovery Extensions\n");␊ |
208 | ␉␉␉strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");␊ |
209 | ␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
210 | ␉␉␉␊ |
211 | ␉␉␉␊ |
212 | ␉␉␉// TODO: fix this, the order does matter, and it's not correct now.␊ |
213 | ␉␉␉// Also try to load Extensions from boot helper partitions.␊ |
214 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
215 | ␉␉␉{␊ |
216 | ␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
217 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
218 | ␉␉␉␉{␊ |
219 | ␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
220 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
221 | ␉␉␉␉␉{␊ |
222 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
223 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
224 | ␉␉␉␉␉}␊ |
225 | ␉␉␉␉}␊ |
226 | ␉␉␉}␊ |
227 | ␉␉␉␊ |
228 | ␉␉␉if (gMKextName[0] != '\0')␊ |
229 | ␉␉␉{␊ |
230 | ␉␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gMKextName);␊ |
231 | ␉␉␉␉if ( LoadDriverMKext(gMKextName) != 0 )␊ |
232 | ␉␉␉␉{␊ |
233 | ␉␉␉␉␉error("Could not load %s\n", gMKextName);␊ |
234 | ␉␉␉␉␉return -1;␊ |
235 | ␉␉␉␉}␊ |
236 | ␉␉␉}␊ |
237 | ␉␉␉else␊ |
238 | ␉␉␉{␊ |
239 | ␉␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
240 | ␉␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
241 | ␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
242 | ␉␉␉}␊ |
243 | ␉␉}␊ |
244 | ␉␉else␊ |
245 | ␉␉{␊ |
246 | ␉␉␉return 0;␊ |
247 | ␉␉}␊ |
248 | ␉␊ |
249 | MatchPersonalities();␊ |
250 | ␉␊ |
251 | MatchLibraries();␊ |
252 | ␉␊ |
253 | LoadMatchedModules();␊ |
254 | ␉␊ |
255 | return 0;␊ |
256 | }␊ |
257 | ␊ |
258 | ␊ |
259 | extern int loadPngImage(const char *filename, uint16_t *width, uint16_t *height, uint8_t **imageData);␊ |
260 | ␊ |
261 | //==========================================================================␊ |
262 | // loadBootGraphics␊ |
263 | void NBI_loadBootGraphics(void)␊ |
264 | {␊ |
265 | ␉extern uint8_t *bootImageData;␊ |
266 | ␉extern uint16_t bootImageWidth; ␊ |
267 | ␉extern uint16_t bootImageHeight; ␊ |
268 | ␊ |
269 | ␉extern const char* theme_name;␊ |
270 | ␉extern bool usePngImage;␊ |
271 | ␉␊ |
272 | ␉if (bootImageData != NULL) {␊ |
273 | ␉␉return;␊ |
274 | ␉}␊ |
275 | ␉␊ |
276 | ␉char dirspec[256];␊ |
277 | ␉␊ |
278 | ␉if ((strlen(theme_name) + 24) > sizeof(dirspec)) {␊ |
279 | ␉␉usePngImage = false; ␊ |
280 | ␉␉return;␊ |
281 | ␉}␊ |
282 | ␉sprintf(dirspec, "/Extra/Themes/%s/NBI.png", theme_name);␊ |
283 | ␉if (loadPngImage(dirspec, &bootImageWidth, &bootImageHeight, &bootImageData) != 0) {␊ |
284 | #ifdef EMBED_THEME␊ |
285 | ␉␉if ((loadEmbeddedPngImage(__boot_png, __boot_png_len, &bootImageWidth, &bootImageHeight, &bootImageData)) != 0)␊ |
286 | #endif␊ |
287 | ␉␉␉usePngImage = false; ␊ |
288 | ␉}␊ |
289 | }␊ |
290 |