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
132safe_set_env(envarchCpuType, CPU_TYPE_I386);
133safe_set_env(envgVerboseMode, true);
134}
135}
136
137void NBI_md0Ramdisk_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)
138{
139NBI_md0Ramdisk();
140}
141
142typedef struct RAMDiskParam
143{
144ppnum_t base;
145unsigned int size;
146} RAMDiskParam;
147
148void NBI_md0Ramdisk()
149{
150RAMDiskParam ramdiskPtr;
151char filename[512];
152int fh = -1;
153
154// TODO: embed NBI.img in this file
155// If runNetbookInstaller is true, then the system has changed states, patch it
156sprintf(filename, "%s", "Extra/NetbookInstaller.img");;
157fh = open(filename);
158
159if (fh >= 0)
160{
161verbose("Enabling ramdisk %s\n", filename);
162
163ramdiskPtr.size = file_size(fh);
164ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
165
166if(ramdiskPtr.size && ramdiskPtr.base)
167{
168// Read new ramdisk image contents in kernel memory.
169if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
170{
171
172 AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size);
173
174Node* node = DT__FindNode("/chosen/memory-map", false);
175if(node != NULL)
176{
177DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
178}
179else
180{
181verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
182}
183}
184else
185{
186verbose("Unable to read md0 image %s.\n", filename);
187}
188}
189else
190{
191verbose("md0 image %s is empty.\n", filename);
192}
193
194close(fh);
195
196}
197}
198
199
200long NBI_LoadDrivers( char * dirSpec )
201{
202
203 char dirSpecExtra[1024];
204
205 if ( InitDriverSupport() != 0 )
206 return 0;
207
208int step = 0;
209execute_hook("ramDiskLoadDrivers", &step, NULL, NULL, NULL, NULL, NULL);
210#ifdef NBP_SUPPORT
211 if ( get_env(envgBootFileType) == kNetworkDeviceType )
212 {
213 if (NetLoadDrivers(dirSpec) != 0)
214{
215 error("Could not load drivers from the network\n");
216 return -1;
217 }
218 }
219 else
220#endif
221if ( get_env(envgBootFileType) == 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 (((BVRef)(uint32_t)get_env(envgBootVolume))->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 char * MKextName = (char*)(uint32_t)get_env(envMKextName);
245
246if (MKextName[0] != '\0')
247{
248verbose("LoadDrivers: Loading from [%s]\n", MKextName);
249if ( LoadDriverMKext(MKextName) != 0 )
250{
251error("Could not load %s\n", MKextName);
252return -1;
253}
254}
255else
256{
257 char * ExtensionsSpec = (char*)(uint32_t)get_env(envDriverExtSpec);
258
259strcpy(ExtensionsSpec, dirSpec);
260strcat(ExtensionsSpec, "System/Library/");
261FileLoadDrivers(ExtensionsSpec, 0);
262}
263}
264else
265{
266return 0;
267}
268#if UNUSED
269 MatchPersonalities();
270#endif
271 MatchLibraries();
272
273 LoadMatchedModules();
274
275 return 0;
276}
277

Archive Download this file

Revision: 1977