Chameleon

Chameleon Svn Source Tree

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

Source at commit 1166 created 13 years 10 days ago.
By meklort, Fixed recent Makefile changes. Please do not use /Users/evan/SourceCode/tmp/chameleon/trunk or VPATH. the *only* time you should use /Users/evan/SourceCode/tmp/chameleon/trunk is when setting the SRCROOT variable. Also note that very soon make pkg is going to be removed. The pkg build script in trunk is very out of date. Instead please use the package maker at http://forge.voodooprojects.org/p/chameleonApplications/. Once this is ready for trunk it will be merged.
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
68if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
69{
70valid = true;
71}
72else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
73{
74valid = true;
75}
76
77if (valid)
78{
79if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))
80{
81
82if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))
83{
84if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))
85{
86if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||
87 strcmp(val, gPrevMacOSBuildVersion) != 0
88 )
89{
90runNetbookInstaller = 1;
91}
92else
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
113if (!runNetbookInstaller && getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)
114{
115if(dummyVal) runNetbookInstaller = 2;
116}
117
118if(runNetbookInstaller)
119{
120
121replace_function("_LoadDrivers", &NBI_LoadDrivers);
122if(runNetbookInstaller == 1)
123{
124replace_function("_md0Ramdisk", &NBI_md0Ramdisk);
125}
126
127// Force arch=i386 + -v
128archCpuType = CPU_TYPE_I386;
129gVerboseMode = true;
130}
131}
132
133
134void NBI_md0Ramdisk()
135{
136RAMDiskParam ramdiskPtr;
137char filename[512];
138int fh = -1;
139
140// TODO: embed NBI.img in this file
141// If runNetbookInstaller is true, then the system has changed states, patch it
142sprintf(filename, "%s", "Extra/NetbookInstaller.img");;
143fh = open(filename, 0);
144
145if (fh >= 0)
146{
147verbose("Enabling ramdisk %s\n", filename);
148
149ramdiskPtr.size = file_size(fh);
150ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
151
152if(ramdiskPtr.size && ramdiskPtr.base)
153{
154// Read new ramdisk image contents in kernel memory.
155if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
156{
157AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);
158Node* node = DT__FindNode("/chosen/memory-map", false);
159if(node != NULL)
160{
161DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
162}
163else
164{
165verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
166}
167}
168else
169{
170verbose("Unable to read md0 image %s.\n", filename);
171}
172}
173else
174{
175verbose("md0 image %s is empty.\n", filename);
176}
177
178close(fh);
179
180}
181}
182
183
184long NBI_LoadDrivers( char * dirSpec )
185{
186
187 char dirSpecExtra[1024];
188
189 if ( InitDriverSupport() != 0 )
190 return 0;
191
192#ifndef OPTION_ROM
193 // Load extra drivers if a hook has been installed.
194 if (LoadExtraDrivers_p != NULL)
195 {
196 (*LoadExtraDrivers_p)(&FileLoadDrivers);
197 }
198
199 if ( gBootFileType == kNetworkDeviceType )
200 {
201 if (NetLoadDrivers(dirSpec) != 0) {
202 error("Could not load drivers from the network\n");
203 return -1;
204 }
205 }
206 else
207#endif
208if ( gBootFileType == kBlockDeviceType )
209{
210verbose("Loading Recovery Extensions\n");
211strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");
212FileLoadDrivers(dirSpecExtra, 0);
213
214
215// TODO: fix this, the order does matter, and it's not correct now.
216// Also try to load Extensions from boot helper partitions.
217if (gBootVolume->flags & kBVFlagBooter)
218{
219strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
220if (FileLoadDrivers(dirSpecExtra, 0) != 0)
221{
222strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
223if (FileLoadDrivers(dirSpecExtra, 0) != 0)
224{
225strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
226FileLoadDrivers(dirSpecExtra, 0);
227}
228}
229}
230
231if (gMKextName[0] != '\0')
232{
233verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
234if ( LoadDriverMKext(gMKextName) != 0 )
235{
236error("Could not load %s\n", gMKextName);
237return -1;
238}
239}
240else
241{
242strcpy(gExtensionsSpec, dirSpec);
243strcat(gExtensionsSpec, "System/Library/");
244FileLoadDrivers(gExtensionsSpec, 0);
245}
246}
247else
248{
249return 0;
250}
251
252 MatchPersonalities();
253
254 MatchLibraries();
255
256 LoadMatchedModules();
257
258 return 0;
259}
260

Archive Download this file

Revision: 1166