Chameleon

Chameleon Svn Source Tree

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

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

Archive Download this file

Revision: 1143