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

Archive Download this file

Revision: 1840