Root/
Source at commit 569 created 13 years 6 months ago. By meklort, NBI postboot installer + recovery mode module (as usual, untested) | |
---|---|
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 | ␊ |
25 | ␊ |
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 | ␉char dummyVal = 0;␊ |
109 | ␉if (getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)␊ |
110 | ␉{␊ |
111 | ␉␉if(dummyVal) runNetbookInstaller = 2;␊ |
112 | ␉}␊ |
113 | ␉␊ |
114 | ␉if(runNetbookInstaller)␊ |
115 | ␉{␊ |
116 | ␉␉replace_function("_LoadDrivers", &NBI_LoadDrivers);␊ |
117 | ␉␉replace_function("_md0Ramdisk", &NBI_md0Ramdisk);␊ |
118 | ␉}␊ |
119 | ␉␊ |
120 | ␉␊ |
121 | }␊ |
122 | ␊ |
123 | void NBI_md0Ramdisk()␊ |
124 | {␊ |
125 | ␉RAMDiskParam ramdiskPtr;␊ |
126 | ␉char filename[512];␊ |
127 | ␉int fh = -1;␊ |
128 | ␉␊ |
129 | ␉// TODO: embed NBI.img in this file␊ |
130 | ␉// If runNetbookInstaller is true, then the system has changed states, patch it ␊ |
131 | ␉sprintf(filename, "%s", "Extra/NetbookInstaller.img");;␊ |
132 | ␉fh = open(filename, 0);␊ |
133 | ␉␊ |
134 | ␉if (fh >= 0)␊ |
135 | ␉{␊ |
136 | ␉␉verbose("Enabling ramdisk %s\n", filename);␊ |
137 | ␉␉␊ |
138 | ␉␉ramdiskPtr.size = file_size(fh);␊ |
139 | ␉␉ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);␊ |
140 | ␉␉␊ |
141 | ␉␉if(ramdiskPtr.size && ramdiskPtr.base)␊ |
142 | ␉␉{␊ |
143 | ␉␉␉// Read new ramdisk image contents in kernel memory.␊ |
144 | ␉␉␉if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)␊ |
145 | ␉␉␉{␊ |
146 | ␉␉␉␉AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);␊ |
147 | ␉␉␉␉Node* node = DT__FindNode("/chosen/memory-map", false);␊ |
148 | ␉␉␉␉if(node != NULL)␊ |
149 | ␉␉␉␉{␊ |
150 | ␉␉␉␉␉DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);␉␉␊ |
151 | ␉␉␉␉}␊ |
152 | ␉␉␉␉else␊ |
153 | ␉␉␉␉{␊ |
154 | ␉␉␉␉␉verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);␊ |
155 | ␉␉␉␉}␊ |
156 | ␉␉␉}␊ |
157 | ␉␉␉else␊ |
158 | ␉␉␉{␊ |
159 | ␉␉␉␉verbose("Unable to read md0 image %s.\n", filename);␊ |
160 | ␉␉␉}␉␉␉␊ |
161 | ␉␉}␊ |
162 | ␉␉else␊ |
163 | ␉␉{␊ |
164 | ␉␉␉verbose("md0 image %s is empty.\n", filename);␊ |
165 | ␉␉}␊ |
166 | ␉␉␊ |
167 | ␉␉close(fh);␊ |
168 | ␉␉␊ |
169 | ␉}␊ |
170 | }␊ |
171 | ␊ |
172 | ␊ |
173 | long NBI_LoadDrivers( char * dirSpec )␊ |
174 | {␊ |
175 | ␉␊ |
176 | char dirSpecExtra[1024];␊ |
177 | ␉␊ |
178 | if ( InitDriverSupport() != 0 )␊ |
179 | return 0;␊ |
180 | ␉␊ |
181 | #ifndef OPTION_ROM␊ |
182 | // Load extra drivers if a hook has been installed.␊ |
183 | if (LoadExtraDrivers_p != NULL)␊ |
184 | {␊ |
185 | (*LoadExtraDrivers_p)(&FileLoadDrivers);␊ |
186 | }␊ |
187 | ␉␊ |
188 | if ( gBootFileType == kNetworkDeviceType )␊ |
189 | {␊ |
190 | if (NetLoadDrivers(dirSpec) != 0) {␊ |
191 | error("Could not load drivers from the network\n");␊ |
192 | return -1;␊ |
193 | }␊ |
194 | }␊ |
195 | else␊ |
196 | #endif␊ |
197 | ␉␉if ( gBootFileType == kBlockDeviceType )␊ |
198 | ␉␉{␊ |
199 | ␉␉␉verbose("Loading Recovery Extensions\n");␊ |
200 | ␉␉␉strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");␊ |
201 | ␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
202 | ␉␉␉␊ |
203 | ␉␉␉␊ |
204 | ␉␉␉// TODO: fix this, the order does matter, and it's not correct now.␊ |
205 | ␉␉␉// Also try to load Extensions from boot helper partitions.␊ |
206 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
207 | ␉␉␉{␊ |
208 | ␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
209 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
210 | ␉␉␉␉{␊ |
211 | ␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
212 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
213 | ␉␉␉␉␉{␊ |
214 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
215 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
216 | ␉␉␉␉␉}␊ |
217 | ␉␉␉␉}␊ |
218 | ␉␉␉}␊ |
219 | ␉␉␉␊ |
220 | ␉␉␉if (gMKextName[0] != '\0')␊ |
221 | ␉␉␉{␊ |
222 | ␉␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gMKextName);␊ |
223 | ␉␉␉␉if ( LoadDriverMKext(gMKextName) != 0 )␊ |
224 | ␉␉␉␉{␊ |
225 | ␉␉␉␉␉error("Could not load %s\n", gMKextName);␊ |
226 | ␉␉␉␉␉return -1;␊ |
227 | ␉␉␉␉}␊ |
228 | ␉␉␉}␊ |
229 | ␉␉␉else␊ |
230 | ␉␉␉{␊ |
231 | ␉␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
232 | ␉␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
233 | ␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
234 | ␉␉␉}␊ |
235 | ␉␉}␊ |
236 | ␉␉else␊ |
237 | ␉␉{␊ |
238 | ␉␉␉return 0;␊ |
239 | ␉␉}␊ |
240 | ␉␊ |
241 | MatchPersonalities();␊ |
242 | ␉␊ |
243 | MatchLibraries();␊ |
244 | ␉␊ |
245 | LoadMatchedModules();␊ |
246 | ␉␊ |
247 | return 0;␊ |
248 | }␊ |
249 | ␊ |
250 | ␊ |
251 |