Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/modules/NetbookInstaller/NBI.c

Source at commit 583 created 13 years 6 months ago.
By meklort, Module updates, mostly bugfixes.
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
20intrunNetbookInstaller = 0;
21
22long NBI_LoadDrivers( char * dirSpec );
23void NBI_md0Ramdisk();
24void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4);
25void NBI_loadBootGraphics(void);
26
27#ifndef OPTION_ROM
28extern long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);
29
30extern long NetLoadDrivers(char *dirSpec);
31extern long FileLoadDrivers(char *dirSpec, long plugin);
32#endif
33
34extern char gMacOSVersion;
35
36
37extern long LoadDriverMKext(char *fileSpec);
38extern long LoadDriverPList(char *dirSpec, char *name, long bundleType);
39
40extern long MatchLibraries( void );
41extern long MatchPersonalities( void );
42extern long LoadMatchedModules( void );
43extern long InitDriverSupport(void);
44extern char * gExtensionsSpec;
45extern char * gDriverSpec;
46extern char * gFileSpec;
47extern char * gTempSpec;
48extern char * gFileName;
49
50void NetbookInstaller_start()
51{
52//else printf("Unable to locate Extra/SystemVersion.LastPatched.plist\n");
53
54register_hook_callback("PreBoot", &NBI_PreBoot_hook);
55
56}
57
58void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4)
59{
60bool dummyVal = 0;
61config_file_t systemVersion;
62
63char valid = false;
64const char *val;
65int len;
66const char* gPrevMacOSBuildVersion;
67
68
69if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
70{
71valid = true;
72}
73else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
74{
75valid = true;
76}
77
78if (valid)
79{
80if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))
81{
82
83if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))
84{
85if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))
86{
87if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||
88 strcmp(val, gPrevMacOSBuildVersion) != 0
89 )
90{
91runNetbookInstaller = 1;
92}
93else
94{
95// Only allow restore from hibernation if the system hasn't changed, probably a bad idea though
96//char* val="/private/var/vm/sleepimage";
97
98// Do this first to be sure that root volume is mounted
99//ret = GetFileInfo(0, val, &flags, &sleeptime);
100
101//printf("System version has not changed\n");
102//runNetbookInstaller = 0;
103
104}
105
106}
107}
108}
109}
110
111
112
113
114
115if (!runNetbookInstaller && getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)
116{
117if(dummyVal) runNetbookInstaller = 2;
118}
119
120if(runNetbookInstaller)
121{
122
123replace_function("_LoadDrivers", &NBI_LoadDrivers);
124if(runNetbookInstaller == 1)
125{
126replace_function("_md0Ramdisk", &NBI_md0Ramdisk);
127}
128
129// Force arch=i386 + -v
130archCpuType = CPU_TYPE_I386;
131gVerboseMode = true;
132}
133}
134
135
136void NBI_md0Ramdisk()
137{
138RAMDiskParam ramdiskPtr;
139char filename[512];
140int fh = -1;
141
142// TODO: embed NBI.img in this file
143// If runNetbookInstaller is true, then the system has changed states, patch it
144sprintf(filename, "%s", "Extra/NetbookInstaller.img");;
145fh = open(filename, 0);
146
147if (fh >= 0)
148{
149verbose("Enabling ramdisk %s\n", filename);
150
151ramdiskPtr.size = file_size(fh);
152ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
153
154if(ramdiskPtr.size && ramdiskPtr.base)
155{
156// Read new ramdisk image contents in kernel memory.
157if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
158{
159AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);
160Node* node = DT__FindNode("/chosen/memory-map", false);
161if(node != NULL)
162{
163DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
164}
165else
166{
167verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
168}
169}
170else
171{
172verbose("Unable to read md0 image %s.\n", filename);
173}
174}
175else
176{
177verbose("md0 image %s is empty.\n", filename);
178}
179
180close(fh);
181
182}
183}
184
185
186long NBI_LoadDrivers( char * dirSpec )
187{
188
189 char dirSpecExtra[1024];
190
191 if ( InitDriverSupport() != 0 )
192 return 0;
193
194#ifndef OPTION_ROM
195 // Load extra drivers if a hook has been installed.
196 if (LoadExtraDrivers_p != NULL)
197 {
198 (*LoadExtraDrivers_p)(&FileLoadDrivers);
199 }
200
201 if ( gBootFileType == kNetworkDeviceType )
202 {
203 if (NetLoadDrivers(dirSpec) != 0) {
204 error("Could not load drivers from the network\n");
205 return -1;
206 }
207 }
208 else
209#endif
210if ( gBootFileType == kBlockDeviceType )
211{
212verbose("Loading Recovery Extensions\n");
213strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");
214FileLoadDrivers(dirSpecExtra, 0);
215
216
217// TODO: fix this, the order does matter, and it's not correct now.
218// Also try to load Extensions from boot helper partitions.
219if (gBootVolume->flags & kBVFlagBooter)
220{
221strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
222if (FileLoadDrivers(dirSpecExtra, 0) != 0)
223{
224strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
225if (FileLoadDrivers(dirSpecExtra, 0) != 0)
226{
227strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
228FileLoadDrivers(dirSpecExtra, 0);
229}
230}
231}
232
233if (gMKextName[0] != '\0')
234{
235verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
236if ( LoadDriverMKext(gMKextName) != 0 )
237{
238error("Could not load %s\n", gMKextName);
239return -1;
240}
241}
242else
243{
244strcpy(gExtensionsSpec, dirSpec);
245strcat(gExtensionsSpec, "System/Library/");
246FileLoadDrivers(gExtensionsSpec, 0);
247}
248}
249else
250{
251return 0;
252}
253
254 MatchPersonalities();
255
256 MatchLibraries();
257
258 LoadMatchedModules();
259
260 return 0;
261}
262

Archive Download this file

Revision: 583