Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 520