Chameleon

Chameleon Svn Source Tree

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

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

Archive Download this file

Revision: 535