Chameleon

Chameleon Svn Source Tree

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

Source at commit 573 created 10 years 3 months ago.
By meklort, NetbookInstaller module now works.
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();
24void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4);
25void NBI_loadBootGraphics(void);
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
108register_hook_callback("PreBoot", &NBI_PreBoot_hook);
109
110}
111
112void NBI_PreBoot_hook(void* arg1, void* arg2, void* arg3, void* arg4)
113{
114extern bool usePngImage;
115bool dummyVal = 0;
116if (getBoolForKey("recovery", &dummyVal, &bootInfo->bootConfig) && dummyVal)
117{
118if(dummyVal) runNetbookInstaller = 2;
119}
120
121if(runNetbookInstaller)
122{
123replace_function("_LoadDrivers", &NBI_LoadDrivers);
124replace_function("_md0Ramdisk", &NBI_md0Ramdisk);
125replace_function("_loadBootGraphics", &NBI_loadBootGraphics);
126usePngImage = true;
127}
128}
129
130
131void NBI_md0Ramdisk()
132{
133RAMDiskParam ramdiskPtr;
134char filename[512];
135int fh = -1;
136
137// TODO: embed NBI.img in this file
138// If runNetbookInstaller is true, then the system has changed states, patch it
139sprintf(filename, "%s", "Extra/NetbookInstaller.img");;
140fh = open(filename, 0);
141
142if (fh >= 0)
143{
144verbose("Enabling ramdisk %s\n", filename);
145
146ramdiskPtr.size = file_size(fh);
147ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
148
149if(ramdiskPtr.size && ramdiskPtr.base)
150{
151// Read new ramdisk image contents in kernel memory.
152if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
153{
154AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);
155Node* node = DT__FindNode("/chosen/memory-map", false);
156if(node != NULL)
157{
158DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
159}
160else
161{
162verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
163}
164}
165else
166{
167verbose("Unable to read md0 image %s.\n", filename);
168}
169}
170else
171{
172verbose("md0 image %s is empty.\n", filename);
173}
174
175close(fh);
176
177}
178}
179
180
181long NBI_LoadDrivers( char * dirSpec )
182{
183
184 char dirSpecExtra[1024];
185
186 if ( InitDriverSupport() != 0 )
187 return 0;
188
189#ifndef OPTION_ROM
190 // Load extra drivers if a hook has been installed.
191 if (LoadExtraDrivers_p != NULL)
192 {
193 (*LoadExtraDrivers_p)(&FileLoadDrivers);
194 }
195
196 if ( gBootFileType == kNetworkDeviceType )
197 {
198 if (NetLoadDrivers(dirSpec) != 0) {
199 error("Could not load drivers from the network\n");
200 return -1;
201 }
202 }
203 else
204#endif
205if ( gBootFileType == kBlockDeviceType )
206{
207verbose("Loading Recovery Extensions\n");
208strcpy(dirSpecExtra, "/Extra/RecoveryExtensions/");
209FileLoadDrivers(dirSpecExtra, 0);
210
211
212// TODO: fix this, the order does matter, and it's not correct now.
213// Also try to load Extensions from boot helper partitions.
214if (gBootVolume->flags & kBVFlagBooter)
215{
216strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
217if (FileLoadDrivers(dirSpecExtra, 0) != 0)
218{
219strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
220if (FileLoadDrivers(dirSpecExtra, 0) != 0)
221{
222strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
223FileLoadDrivers(dirSpecExtra, 0);
224}
225}
226}
227
228if (gMKextName[0] != '\0')
229{
230verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
231if ( LoadDriverMKext(gMKextName) != 0 )
232{
233error("Could not load %s\n", gMKextName);
234return -1;
235}
236}
237else
238{
239strcpy(gExtensionsSpec, dirSpec);
240strcat(gExtensionsSpec, "System/Library/");
241FileLoadDrivers(gExtensionsSpec, 0);
242}
243}
244else
245{
246return 0;
247}
248
249 MatchPersonalities();
250
251 MatchLibraries();
252
253 LoadMatchedModules();
254
255 return 0;
256}
257
258
259extern int loadPngImage(const char *filename, uint16_t *width, uint16_t *height, uint8_t **imageData);
260
261//==========================================================================
262// loadBootGraphics
263void NBI_loadBootGraphics(void)
264{
265extern uint8_t *bootImageData;
266extern uint16_t bootImageWidth;
267extern uint16_t bootImageHeight;
268
269extern const char* theme_name;
270extern bool usePngImage;
271
272if (bootImageData != NULL) {
273return;
274}
275
276char dirspec[256];
277
278if ((strlen(theme_name) + 24) > sizeof(dirspec)) {
279usePngImage = false;
280return;
281}
282sprintf(dirspec, "/Extra/Themes/%s/NBI.png", theme_name);
283if (loadPngImage(dirspec, &bootImageWidth, &bootImageHeight, &bootImageData) != 0) {
284#ifdef EMBED_THEME
285if ((loadEmbeddedPngImage(__boot_png, __boot_png_len, &bootImageWidth, &bootImageHeight, &bootImageData)) != 0)
286#endif
287usePngImage = false;
288}
289}
290

Archive Download this file

Revision: 573