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

Archive Download this file

Revision: 1119