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

Archive Download this file

Revision: 2044