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);
24//void 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)
133{
134replace_function("_md0Ramdisk", &NBI_md0Ramdisk);
135}
136else
137{
138register_hook_callback("md0Ramdisk", NBI_md0Ramdisk_hook);
139}
140
141}
142
143// Force arch=i386 + -v
144archCpuType = CPU_TYPE_I386;
145gVerboseMode = true;
146}
147}
148
149void NBI_md0Ramdisk_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)
150{
151NBI_md0Ramdisk();
152}
153
154typedef struct RAMDiskParam
155{
156ppnum_t base;
157unsigned int size;
158} RAMDiskParam;
159
160void NBI_md0Ramdisk()
161{
162RAMDiskParam ramdiskPtr;
163char filename[512];
164int fh = -1;
165
166// TODO: embed NBI.img in this file
167// If runNetbookInstaller is true, then the system has changed states, patch it
168sprintf(filename, "%s", "Extra/NetbookInstaller.img");;
169fh = open(filename);
170
171if (fh >= 0)
172{
173verbose("Enabling ramdisk %s\n", filename);
174
175ramdiskPtr.size = file_size(fh);
176ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
177
178if(ramdiskPtr.size && ramdiskPtr.base)
179{
180// Read new ramdisk image contents in kernel memory.
181if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
182{
183#if UNUSED
184 AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);
185#else
186 AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size);
187#endif
188Node* node = DT__FindNode("/chosen/memory-map", false);
189if(node != NULL)
190{
191DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
192}
193else
194{
195verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
196}
197}
198else
199{
200verbose("Unable to read md0 image %s.\n", filename);
201}
202}
203else
204{
205verbose("md0 image %s is empty.\n", filename);
206}
207
208close(fh);
209
210}
211}
212
213
214long NBI_LoadDrivers( char * dirSpec )
215{
216
217 char dirSpecExtra[1024];
218
219 if ( InitDriverSupport() != 0 )
220 return 0;
221
222int step = 0;
223execute_hook("ramDiskLoadDrivers", &step, NULL, NULL, NULL, NULL, NULL);
224#ifdef NBP_SUPPORT
225 if ( gBootFileType == kNetworkDeviceType )
226 {
227 if (NetLoadDrivers(dirSpec) != 0)
228{
229 error("Could not load drivers from the network\n");
230 return -1;
231 }
232 }
233 else
234#endif
235if ( gBootFileType == kBlockDeviceType )
236{
237verbose("Loading Recovery Extensions\n");
238strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");
239FileLoadDrivers(dirSpecExtra, 0);
240
241#ifdef BOOT_HELPER_SUPPORT
242// TODO: fix this, the order does matter, and it's not correct now.
243// Also try to load Extensions from boot helper partitions.
244if (gBootVolume->flags & kBVFlagBooter)
245{
246strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
247if (FileLoadDrivers(dirSpecExtra, 0) != 0)
248{
249strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
250if (FileLoadDrivers(dirSpecExtra, 0) != 0)
251{
252strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
253FileLoadDrivers(dirSpecExtra, 0);
254}
255}
256}
257#endif
258
259if (gMKextName[0] != '\0')
260{
261verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
262if ( LoadDriverMKext(gMKextName) != 0 )
263{
264error("Could not load %s\n", gMKextName);
265return -1;
266}
267}
268else
269{
270strcpy(gExtensionsSpec, dirSpec);
271strcat(gExtensionsSpec, "System/Library/");
272FileLoadDrivers(gExtensionsSpec, 0);
273}
274}
275else
276{
277return 0;
278}
279#if UNUSED
280 MatchPersonalities();
281#endif
282 MatchLibraries();
283
284 LoadMatchedModules();
285
286 return 0;
287}
288

Archive Download this file

Revision: 1468