Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/trunkGraphicsEnablerModules/i386/boot2/ramdisk.c

1/*
2 * Supplemental ramdisk functions for the multiboot ramdisk driver.
3 * Copyright 2009 Tamas Kosarszky. All rights reserved.
4 *
5 */
6
7#include "boot.h"
8#include "bootstruct.h"
9#include "multiboot.h"
10#include "ramdisk.h"
11
12struct multiboot_info * gRAMDiskMI = NULL;
13
14// gRAMDiskVolume holds the bvr for the mounted ramdisk image.
15BVRef gRAMDiskVolume = NULL;
16bool gRAMDiskBTAliased = false;
17char gRAMDiskFile[512];
18
19// Notify OS X that a ramdisk has been setup. XNU with attach this to /dev/md0
20void md0Ramdisk()
21{
22RAMDiskParam ramdiskPtr;
23char filename[512];
24const char* override_filename = 0;
25int fh = -1;
26int len;
27
28if(getValueForKey(kMD0Image, &override_filename, &len,
29 &bootInfo->chameleonConfig))
30{
31// Use user specified md0 file
32sprintf(filename, "%s", override_filename);
33fh = open(filename, 0);
34
35if(fh < 0)
36{
37sprintf(filename, "rd(0,0)/Extra/%s", override_filename);
38fh = open(filename, 0);
39
40if(fh < 0)
41{
42sprintf(filename, "/Extra/%s", override_filename);
43fh = open(filename, 0);
44}
45}
46}
47
48if(fh < 0)
49{
50sprintf(filename, "rd(0,0)/Extra/Postboot.img");
51fh = open(filename, 0);
52
53if(fh < 0)
54{
55sprintf(filename, "/Extra/Postboot.img");// Check /Extra if not in rd(0,0)
56fh = open(filename, 0);
57}
58}
59
60if (fh >= 0)
61{
62verbose("Enabling ramdisk %s\n", filename);
63
64ramdiskPtr.size = file_size(fh);
65ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
66
67if(ramdiskPtr.size && ramdiskPtr.base)
68{
69// Read new ramdisk image contents in kernel memory.
70if (read(fh, (char*) ramdiskPtr.base, ramdiskPtr.size) == ramdiskPtr.size)
71{
72AllocateMemoryRange("RAMDisk", ramdiskPtr.base, ramdiskPtr.size, kBootDriverTypeInvalid);
73Node* node = DT__FindNode("/chosen/memory-map", false);
74if(node != NULL)
75{
76DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
77}
78else
79{
80verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
81}
82}
83else
84{
85verbose("Unable to read md0 image %s.\n", filename);
86}
87}
88else
89{
90verbose("md0 image %s is empty.\n", filename);
91}
92
93close(fh);
94
95}
96}
97
98void umountRAMDisk()
99{
100if (gRAMDiskMI != NULL)
101{
102// Release ramdisk BVRef and DiskBVMap.
103struct DiskBVMap *oldMap = diskResetBootVolumes(0x100);
104CacheReset();
105diskFreeMap(oldMap);
106
107// Free multiboot info and module structures.
108if ((void *)gRAMDiskMI->mi_mods_addr != NULL) free((void *)gRAMDiskMI->mi_mods_addr);
109if (gRAMDiskMI != NULL) free(gRAMDiskMI);
110
111// Reset multiboot structures.
112gMI = gRAMDiskMI = NULL;
113*gRAMDiskFile = '\0';
114
115// Release ramdisk driver hooks.
116p_get_ramdisk_info = NULL;
117p_ramdiskReadBytes = NULL;
118
119// Reset ramdisk bvr
120gRAMDiskVolume = NULL;
121printf("\nunmounting: done");
122}
123}
124
125int mountRAMDisk(const char * param)
126{
127int fh = 0, ramDiskSize;
128int error = 0;
129
130// Get file handle for ramdisk file.
131fh = open(param, 0);
132if (fh != -1)
133{
134printf("\nreading ramdisk image: %s", param);
135
136ramDiskSize = file_size(fh);
137if (ramDiskSize > 0)
138{
139// Unmount previously mounted image if exists.
140umountRAMDisk();
141
142// Read new ramdisk image contents into PREBOOT_DATA area.
143if (read(fh, (char *)PREBOOT_DATA, ramDiskSize) != ramDiskSize) error = -1;
144}
145else error = -1;
146
147close(fh);
148}
149else error = -1;
150
151if (error == 0)
152{
153// Save filename in gRAMDiskFile to display information.
154strcpy(gRAMDiskFile, param);
155
156// Set gMI as well for the multiboot ramdisk driver hook.
157gMI = gRAMDiskMI = malloc(sizeof(multiboot_info));
158struct multiboot_module * ramdisk_module = malloc(sizeof(multiboot_module));
159
160// Fill in multiboot info and module structures.
161if (gRAMDiskMI != NULL && ramdisk_module != NULL)
162{
163gRAMDiskMI->mi_mods_count = 1;
164gRAMDiskMI->mi_mods_addr = (uint32_t)ramdisk_module;
165ramdisk_module->mm_mod_start = PREBOOT_DATA;
166ramdisk_module->mm_mod_end = PREBOOT_DATA + ramDiskSize;
167
168// Set ramdisk driver hooks.
169p_get_ramdisk_info = &multiboot_get_ramdisk_info;
170p_ramdiskReadBytes = &multibootRamdiskReadBytes;
171
172int partCount; // unused
173// Save bvr of the mounted image.
174gRAMDiskVolume = diskScanBootVolumes(0x100, &partCount);
175if(gRAMDiskVolume == NULL)
176{
177umountRAMDisk();
178printf("\nRamdisk contains no partitions.");
179}
180else
181{
182char dirSpec[128];
183
184// Reading ramdisk configuration.
185strcpy(dirSpec, RAMDISKCONFIG_FILENAME);
186
187if (loadConfigFile(dirSpec, &bootInfo->ramdiskConfig) == 0)
188{
189getBoolForKey("BTAlias", &gRAMDiskBTAliased, &bootInfo->ramdiskConfig);
190}
191else
192{
193printf("\nno ramdisk config...\n");
194}
195
196printf("\nmounting: done");
197}
198}
199}
200
201return error;
202}
203
204void setRAMDiskBTHook(bool mode)
205{
206gRAMDiskBTAliased = mode;
207if (mode)
208{
209printf("\nEnabled bt(0,0) alias.");
210}
211else
212{
213printf("\nDisabled bt(0,0) alias.");
214}
215}
216
217void showInfoRAMDisk(void)
218{
219int len;
220const char *val;
221
222if (gRAMDiskMI != NULL)
223{
224struct multiboot_module * ramdisk_module = (void *)gRAMDiskMI->mi_mods_addr;
225
226printf("\nfile: %s %d", gRAMDiskFile,
227ramdisk_module->mm_mod_end - ramdisk_module->mm_mod_start);
228printf("\nalias: %s", gRAMDiskBTAliased ? "enabled" : "disabled");
229
230// Display ramdisk information if available.
231if (getValueForKey("Info", &val, &len, &bootInfo->ramdiskConfig))
232{
233printf("\ninfo: %s", val);
234}
235else
236{
237printf("\nramdisk info not available.");
238}
239}
240else
241{
242printf("\nNo ramdisk mounted.");
243}
244}
245
246int loadPrebootRAMDisk()
247{
248mountRAMDisk("bt(0,0)/Extra/Preboot.dmg");
249if (gRAMDiskMI != NULL)
250{
251printf("\n");
252return 0;
253}
254else
255{
256return -1;
257}
258}
259
260void processRAMDiskCommand(char ** argPtr, const char * cmd)
261{
262char * ptr = *argPtr;
263char param[1024];
264getNextArg(&ptr, param);
265
266if (strcmp(cmd, "m") == 0)
267{
268mountRAMDisk(param);
269sleep(2);
270}
271else if (strcmp(cmd, "u") == 0)
272{
273umountRAMDisk();
274sleep(2);
275}
276else if (strcmp(cmd, "e") == 0)
277{
278setRAMDiskBTHook(true);
279sleep(2);
280}
281else if (strcmp(cmd, "d") == 0)
282{
283setRAMDiskBTHook(false);
284sleep(2);
285}
286else if (strcmp(cmd, "i") == 0)
287{
288setActiveDisplayPage(1);
289clearScreenRows(0, 24);
290setCursorPosition(0, 0, 1);
291showInfoRAMDisk();
292printf("\n\nPress any key to continue.\n");
293getchar();
294setActiveDisplayPage(0);
295}
296else
297{
298setActiveDisplayPage(1);
299clearScreenRows(0, 24);
300setCursorPosition(0, 0, 1);
301printf("\nusage:\n");
302printf("\n?rd i - display ramdisk information");
303printf("\n?rd m <filename> - mount ramdisk image\n?rd u - unmount ramdisk image");
304printf("\n?rd e - enable bt(0,0) alias\n?rd d - disable bt(0,0) alias");
305printf("\n\nPress any key to continue.\n");
306getchar();
307setActiveDisplayPage(0);
308}
309}
310

Archive Download this file

Revision: 1146