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

Archive Download this file

Revision: 1972