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

Archive Download this file

Revision: 1047