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