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

Archive Download this file

Revision: 789