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 long LoadDriverMKext(char *fileSpec);
33extern long LoadDriverPList(char *dirSpec, char *name, long bundleType);
34
35extern long MatchLibraries( void );
36#if UNUSED
37extern long MatchPersonalities( void );
38#endif
39extern long LoadMatchedModules( void );
40extern long InitDriverSupport(void);
41extern char * gExtensionsSpec;
42extern char * gDriverSpec;
43extern char * gFileSpec;
44extern char * gTempSpec;
45extern char * gFileName;
46
47#define kEnableNBI"EnableNBIModule"
48
49void NetbookInstaller_start()
50{
51bool enable = true;
52getBoolForKey(kEnableNBI, &enable, &bootInfo->bootConfig) ;
53
54if (enable)
55register_hook_callback("PreBoot", &NBI_PreBoot_hook);
56
57}
58
59void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)
60{
61
62bool dummyVal = 0;
63config_file_t systemVersion;
64
65char valid = false;
66const char *val;
67int len;
68const char* gPrevMacOSBuildVersion;
69
70
71if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
72{
73valid = true;
74}
75else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
76{
77valid = true;
78}
79
80if (valid)
81{
82if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))
83{
84
85if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))
86{
87if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))
88{
89if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||
90 strcmp(val, gPrevMacOSBuildVersion) != 0
91 )
92{
93runNetbookInstaller = 1;
94}
95else
96{
97// Only allow restore from hibernation if the system hasn't changed, probably a bad idea though
98//char* val="/private/var/vm/sleepimage";
99
100// Do this first to be sure that root volume is mounted
101//ret = GetFileInfo(0, val, &flags, &sleeptime);
102
103//printf("System version has not changed\n");
104//runNetbookInstaller = 0;
105
106}
107
108}
109}
110}
111}
112
113
114
115
116
117if (!runNetbookInstaller && getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)
118{
119if(dummyVal) runNetbookInstaller = 2;
120}
121
122if(runNetbookInstaller)
123{
124
125replace_function("_LoadDrivers", &NBI_LoadDrivers);
126
127if(runNetbookInstaller == 1 )
128{
129if (execute_hook("isRamDiskRegistred", NULL, NULL, NULL, NULL, NULL, NULL) == EFI_SUCCESS)
130{
131replace_function("_md0Ramdisk", &NBI_md0Ramdisk);
132}
133else
134{
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{
226 error("Could not load drivers from the network\n");
227 return -1;
228 }
229 }
230 else
231#endif
232if ( gBootFileType == kBlockDeviceType )
233{
234verbose("Loading Recovery Extensions\n");
235strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");
236FileLoadDrivers(dirSpecExtra, 0);
237
238#ifdef BOOT_HELPER_SUPPORT
239// TODO: fix this, the order does matter, and it's not correct now.
240// Also try to load Extensions from boot helper partitions.
241if (gBootVolume->flags & kBVFlagBooter)
242{
243strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
244if (FileLoadDrivers(dirSpecExtra, 0) != 0)
245{
246strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
247if (FileLoadDrivers(dirSpecExtra, 0) != 0)
248{
249strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
250FileLoadDrivers(dirSpecExtra, 0);
251}
252}
253}
254#endif
255
256if (gMKextName[0] != '\0')
257{
258verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
259if ( LoadDriverMKext(gMKextName) != 0 )
260{
261error("Could not load %s\n", gMKextName);
262return -1;
263}
264}
265else
266{
267strcpy(gExtensionsSpec, dirSpec);
268strcat(gExtensionsSpec, "System/Library/");
269FileLoadDrivers(gExtensionsSpec, 0);
270}
271}
272else
273{
274return 0;
275}
276#if UNUSED
277 MatchPersonalities();
278#endif
279 MatchLibraries();
280
281 LoadMatchedModules();
282
283 return 0;
284}
285

Archive Download this file

Revision: 1735