Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/modules/NetbookInstaller/NBI.c

Source at commit 569 created 4 years 26 days ago.
By meklort, NBI postboot installer + recovery mode module (as usual, untested)
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 "ramdisk.h"
17#include "drivers.h"
18#include "modules.h"
19
20intrunNetbookInstaller = 0;
21
22long NBI_LoadDrivers( char * dirSpec );
23void NBI_md0Ramdisk();
24
25
26
27#ifndef OPTION_ROM
28extern long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);
29
30extern long NetLoadDrivers(char *dirSpec);
31extern long FileLoadDrivers(char *dirSpec, long plugin);
32#endif
33
34extern char gMacOSVersion;
35
36
37extern long LoadDriverMKext(char *fileSpec);
38extern long LoadDriverPList(char *dirSpec, char *name, long bundleType);
39
40extern long MatchLibraries( void );
41extern long MatchPersonalities( void );
42extern long LoadMatchedModules( void );
43extern long InitDriverSupport(void);
44extern char * gExtensionsSpec;
45extern char * gDriverSpec;
46extern char * gFileSpec;
47extern char * gTempSpec;
48extern char * gFileName;
49
50void NetbookInstaller_start()
51{
52
53config_file_t systemVersion;
54
55char valid = false;
56const char *val;
57int len;
58const char* gPrevMacOSBuildVersion;
59
60
61if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
62{
63valid = true;
64}
65else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
66{
67valid = true;
68}
69
70if (valid)
71{
72if (getValueForKey("ProductBuildVersion", &val, &len, &systemVersion))
73{
74
75if (!loadConfigFile("Extra/SystemVersion.LastPatched.plist", &systemVersion))
76{
77if(getValueForKey("ProductBuildVersion", &gPrevMacOSBuildVersion, &len, &systemVersion))
78{
79if(strlen(val) != strlen(gPrevMacOSBuildVersion) ||
80 strcmp(val, gPrevMacOSBuildVersion) != 0
81 )
82{
83runNetbookInstaller = 1;
84archCpuType = CPU_TYPE_I386;
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//else printf("Unable to locate Extra/SystemVersion.LastPatched.plist\n");
107
108char dummyVal = 0;
109if (getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)
110{
111if(dummyVal) runNetbookInstaller = 2;
112}
113
114if(runNetbookInstaller)
115{
116replace_function("_LoadDrivers", &NBI_LoadDrivers);
117replace_function("_md0Ramdisk", &NBI_md0Ramdisk);
118}
119
120
121}
122
123void NBI_md0Ramdisk()
124{
125RAMDiskParam ramdiskPtr;
126char filename[512];
127int fh = -1;
128
129// TODO: embed NBI.img in this file
130// If runNetbookInstaller is true, then the system has changed states, patch it
131sprintf(filename, "%s", "Extra/NetbookInstaller.img");;
132fh = open(filename, 0);
133
134if (fh >= 0)
135{
136verbose("Enabling ramdisk %s\n", filename);
137
138ramdiskPtr.size = file_size(fh);
139ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
140
141if(ramdiskPtr.size && ramdiskPtr.base)
142{
143// Read new ramdisk image contents in kernel memory.
144if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
145{
146AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);
147Node* node = DT__FindNode("/chosen/memory-map", false);
148if(node != NULL)
149{
150DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
151}
152else
153{
154verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
155}
156}
157else
158{
159verbose("Unable to read md0 image %s.\n", filename);
160}
161}
162else
163{
164verbose("md0 image %s is empty.\n", filename);
165}
166
167close(fh);
168
169}
170}
171
172
173long NBI_LoadDrivers( char * dirSpec )
174{
175
176 char dirSpecExtra[1024];
177
178 if ( InitDriverSupport() != 0 )
179 return 0;
180
181#ifndef OPTION_ROM
182 // Load extra drivers if a hook has been installed.
183 if (LoadExtraDrivers_p != NULL)
184 {
185 (*LoadExtraDrivers_p)(&FileLoadDrivers);
186 }
187
188 if ( gBootFileType == kNetworkDeviceType )
189 {
190 if (NetLoadDrivers(dirSpec) != 0) {
191 error("Could not load drivers from the network\n");
192 return -1;
193 }
194 }
195 else
196#endif
197if ( gBootFileType == kBlockDeviceType )
198{
199verbose("Loading Recovery Extensions\n");
200strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");
201FileLoadDrivers(dirSpecExtra, 0);
202
203
204// TODO: fix this, the order does matter, and it's not correct now.
205// Also try to load Extensions from boot helper partitions.
206if (gBootVolume->flags & kBVFlagBooter)
207{
208strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
209if (FileLoadDrivers(dirSpecExtra, 0) != 0)
210{
211strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
212if (FileLoadDrivers(dirSpecExtra, 0) != 0)
213{
214strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
215FileLoadDrivers(dirSpecExtra, 0);
216}
217}
218}
219
220if (gMKextName[0] != '\0')
221{
222verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
223if ( LoadDriverMKext(gMKextName) != 0 )
224{
225error("Could not load %s\n", gMKextName);
226return -1;
227}
228}
229else
230{
231strcpy(gExtensionsSpec, dirSpec);
232strcat(gExtensionsSpec, "System/Library/");
233FileLoadDrivers(gExtensionsSpec, 0);
234}
235}
236else
237{
238return 0;
239}
240
241 MatchPersonalities();
242
243 MatchLibraries();
244
245 LoadMatchedModules();
246
247 return 0;
248}
249
250
251

Archive Download this file

Revision: 569