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