Chameleon

Chameleon Svn Source Tree

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

Source at commit 577 created 13 years 6 months ago.
By meklort, Reenabled multiboot code (ramdisk support)
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.
114gRAMDiskMI = NULL;
115//#ifdef UNUSED
116gMI = gRAMDiskMI;
117//#endif
118
119*gRAMDiskFile = '\0';
120
121// Release ramdisk driver hooks.
122p_get_ramdisk_info = NULL;
123p_ramdiskReadBytes = NULL;
124
125// Reset ramdisk bvr
126gRAMDiskVolume = NULL;
127printf("\nunmounting: done");
128}
129}
130
131int mountRAMDisk(const char * param)
132{
133int fh = 0, ramDiskSize;
134int error = 0;
135
136// Get file handle for ramdisk file.
137fh = open(param, 0);
138if (fh != -1)
139{
140verbose("\nreading ramdisk image: %s", param);
141
142ramDiskSize = file_size(fh);
143if (ramDiskSize > 0)
144{
145// Unmount previously mounted image if exists.
146umountRAMDisk();
147
148// Read new ramdisk image contents into PREBOOT_DATA area.
149if (read(fh, (char *)PREBOOT_DATA, ramDiskSize) != ramDiskSize) error = -1;
150}
151else error = -1;
152
153close(fh);
154}
155else error = -1;
156
157if (error == 0)
158{
159// Save filename in gRAMDiskFile to display information.
160strcpy(gRAMDiskFile, param);
161
162// Set gMI as well for the multiboot ramdisk driver hook.
163gRAMDiskMI = malloc(sizeof(multiboot_info));
164//#ifdef UNUSED
165gMI = gRAMDiskMI;
166//#endif
167struct multiboot_module * ramdisk_module = malloc(sizeof(multiboot_module));
168
169// Fill in multiboot info and module structures.
170if (gRAMDiskMI != NULL && ramdisk_module != NULL)
171{
172gRAMDiskMI->mi_mods_count = 1;
173gRAMDiskMI->mi_mods_addr = (uint32_t)ramdisk_module;
174ramdisk_module->mm_mod_start = PREBOOT_DATA;
175ramdisk_module->mm_mod_end = PREBOOT_DATA + ramDiskSize;
176
177//#ifdef UNUSED
178// Set ramdisk driver hooks.
179p_get_ramdisk_info = &multiboot_get_ramdisk_info;
180p_ramdiskReadBytes = &multibootRamdiskReadBytes;
181//#endif
182int partCount; // unused
183// Save bvr of the mounted image.
184gRAMDiskVolume = diskScanBootVolumes(0x100, &partCount);
185if(gRAMDiskVolume == NULL)
186{
187umountRAMDisk();
188printf("\nRamdisk contains no partitions.\n");
189pause();
190
191}
192else
193{
194char dirSpec[128];
195
196// Reading ramdisk configuration.
197strcpy(dirSpec, RAMDISKCONFIG_FILENAME);
198
199if (loadConfigFile(dirSpec, &bootInfo->ramdiskConfig) == 0)
200{
201getBoolForKey("BTAlias", &gRAMDiskBTAliased, &bootInfo->ramdiskConfig);
202}
203else
204{
205verbose("\nno ramdisk config...\n");
206}
207
208verbose("\nmounting: done");
209}
210}
211}
212return error;
213}
214
215void setRAMDiskBTHook(bool mode)
216{
217gRAMDiskBTAliased = mode;
218if (mode)
219{
220printf("\nEnabled bt(0,0) alias.");
221}
222else
223{
224printf("\nDisabled bt(0,0) alias.");
225}
226}
227
228void showInfoRAMDisk(void)
229{
230int len;
231const char *val;
232
233if (gRAMDiskMI != NULL)
234{
235struct multiboot_module * ramdisk_module = (void *)gRAMDiskMI->mi_mods_addr;
236
237printf("\nfile: %s %d", gRAMDiskFile,
238ramdisk_module->mm_mod_end - ramdisk_module->mm_mod_start);
239printf("\nalias: %s", gRAMDiskBTAliased ? "enabled" : "disabled");
240
241// Display ramdisk information if available.
242if (getValueForKey("Info", &val, &len, &bootInfo->ramdiskConfig))
243{
244printf("\ninfo: %s", val);
245}
246else
247{
248printf("\nramdisk info not available.");
249}
250}
251else
252{
253printf("\nNo ramdisk mounted.");
254}
255}
256
257int loadPrebootRAMDisk()
258{
259mountRAMDisk("bt(0,0)/Extra/Preboot.dmg");
260if (gRAMDiskMI != NULL)
261{
262printf("\n");
263return 0;
264}
265else
266{
267return -1;
268}
269}
270
271void processRAMDiskCommand(char ** argPtr, const char * cmd)
272{
273char * ptr = *argPtr;
274char param[1024];
275getNextArg(&ptr, param);
276
277if (strcmp(cmd, "m") == 0)
278{
279mountRAMDisk(param);
280sleep(2);
281}
282else if (strcmp(cmd, "u") == 0)
283{
284umountRAMDisk();
285sleep(2);
286}
287else if (strcmp(cmd, "e") == 0)
288{
289setRAMDiskBTHook(true);
290sleep(2);
291}
292else if (strcmp(cmd, "d") == 0)
293{
294setRAMDiskBTHook(false);
295sleep(2);
296}
297else if (strcmp(cmd, "i") == 0)
298{
299setActiveDisplayPage(1);
300clearScreenRows(0, 24);
301setCursorPosition(0, 0, 1);
302showInfoRAMDisk();
303printf("\n\nPress any key to continue.\n");
304getc();
305setActiveDisplayPage(0);
306}
307else
308{
309setActiveDisplayPage(1);
310clearScreenRows(0, 24);
311setCursorPosition(0, 0, 1);
312printf("\nusage:\n");
313printf("\n?rd i - display ramdisk information");
314printf("\n?rd m <filename> - mount ramdisk image\n?rd u - unmount ramdisk image");
315printf("\n?rd e - enable bt(0,0) alias\n?rd d - disable bt(0,0) alias");
316printf("\n\nPress any key to continue.\n");
317getc();
318setActiveDisplayPage(0);
319}
320}
321#endif
322

Archive Download this file

Revision: 577