Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/boot2/ramdisk.c

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

Archive Download this file

Revision: 575