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 | struct multiboot_info * gRAMDiskMI = NULL;␊ |
13 | ␊ |
14 | // gRAMDiskVolume holds the bvr for the mounted ramdisk image.␊ |
15 | BVRef gRAMDiskVolume = NULL;␊ |
16 | bool gRAMDiskBTAliased = false;␊ |
17 | char gRAMDiskFile[512];␊ |
18 | ␊ |
19 | void umountRAMDisk()␊ |
20 | {␊ |
21 | if (gRAMDiskMI != NULL)␊ |
22 | {␊ |
23 | // Release ramdisk BVRef and DiskBVMap.␊ |
24 | struct DiskBVMap *oldMap = diskResetBootVolumes(0x100);␊ |
25 | CacheReset();␊ |
26 | diskFreeMap(oldMap);␊ |
27 | ␊ |
28 | // Free multiboot info and module structures.␊ |
29 | if ((void *)gRAMDiskMI->mi_mods_addr != NULL) free((void *)gRAMDiskMI->mi_mods_addr);␊ |
30 | if (gRAMDiskMI != NULL) free(gRAMDiskMI);␊ |
31 | ␊ |
32 | // Reset multiboot structures.␊ |
33 | gMI = gRAMDiskMI = NULL;␊ |
34 | *gRAMDiskFile = '\0';␊ |
35 | ␊ |
36 | // Release ramdisk driver hooks.␊ |
37 | p_get_ramdisk_info = NULL;␊ |
38 | p_ramdiskReadBytes = NULL;␊ |
39 | ␊ |
40 | printf("\nunmounting: done");␊ |
41 | }␊ |
42 | }␊ |
43 | ␊ |
44 | int mountRAMDisk(const char * param)␊ |
45 | {␊ |
46 | int fh = 0, ramDiskSize;␊ |
47 | int error = 0;␊ |
48 | ␊ |
49 | // Get file handle for ramdisk file.␊ |
50 | fh = open(param, 0);␊ |
51 | if (fh != -1)␊ |
52 | {␊ |
53 | ␉printf("\nreading ramdisk image: %s", param);␊ |
54 | ␊ |
55 | ␉ramDiskSize = file_size(fh);␊ |
56 | if (ramDiskSize > 0)␊ |
57 | {␊ |
58 | // Unmount previously mounted image if exists.␊ |
59 | umountRAMDisk();␊ |
60 | ␊ |
61 | // Read new ramdisk image contents into PREBOOT_DATA area.␊ |
62 | if (read(fh, (char *)PREBOOT_DATA, ramDiskSize) != ramDiskSize) error = -1;␊ |
63 | }␊ |
64 | else error = -1;␊ |
65 | ␊ |
66 | close(fh);␊ |
67 | }␊ |
68 | else error = -1;␊ |
69 | ␊ |
70 | if (error == 0)␊ |
71 | {␊ |
72 | // Save filename in gRAMDiskFile to display information.␊ |
73 | strcpy(gRAMDiskFile, param);␊ |
74 | ␊ |
75 | // Set gMI as well for the multiboot ramdisk driver hook.␊ |
76 | gMI = gRAMDiskMI = malloc(sizeof(multiboot_info));␊ |
77 | struct multiboot_module * ramdisk_module = malloc(sizeof(multiboot_module));␊ |
78 | ␊ |
79 | // Fill in multiboot info and module structures.␊ |
80 | if (gRAMDiskMI != NULL && ramdisk_module != NULL)␊ |
81 | {␊ |
82 | gRAMDiskMI->mi_mods_count = 1;␊ |
83 | gRAMDiskMI->mi_mods_addr = (uint32_t)ramdisk_module;␊ |
84 | ramdisk_module->mm_mod_start = PREBOOT_DATA;␊ |
85 | ramdisk_module->mm_mod_end = PREBOOT_DATA + ramDiskSize;␊ |
86 | ␊ |
87 | // Set ramdisk driver hooks.␊ |
88 | p_get_ramdisk_info = &multiboot_get_ramdisk_info;␊ |
89 | p_ramdiskReadBytes = &multibootRamdiskReadBytes;␊ |
90 | ␊ |
91 | int partCount; // unused␊ |
92 | // Save bvr of the mounted image.␊ |
93 | gRAMDiskVolume = diskScanBootVolumes(0x100, &partCount);␊ |
94 | if(gRAMDiskVolume == NULL)␊ |
95 | {␊ |
96 | umountRAMDisk();␊ |
97 | printf("\nRamdisk contains no partitions.");␊ |
98 | }␊ |
99 | else␊ |
100 | {␊ |
101 | char dirSpec[128];␊ |
102 | ␊ |
103 | // Reading ramdisk configuration.␊ |
104 | strcpy(dirSpec, RAMDISKCONFIG_FILENAME);␊ |
105 | ␊ |
106 | if (loadConfigFile(dirSpec, &bootInfo->ramdiskConfig) == 0)␊ |
107 | {␊ |
108 | getBoolForKey("BTAlias", &gRAMDiskBTAliased, &bootInfo->ramdiskConfig);␊ |
109 | }␊ |
110 | else␊ |
111 | {␊ |
112 | printf("\nno ramdisk config...\n");␊ |
113 | }␊ |
114 | ␊ |
115 | printf("\nmounting: done");␊ |
116 | }␊ |
117 | }␊ |
118 | ␊ |
119 | }␊ |
120 | ␊ |
121 | return error;␊ |
122 | }␊ |
123 | ␊ |
124 | void setRAMDiskBTHook(bool mode)␊ |
125 | {␊ |
126 | gRAMDiskBTAliased = mode;␊ |
127 | if (mode) {␊ |
128 | printf("\nEnabled bt(0,0) alias.");␊ |
129 | } else {␊ |
130 | printf("\nDisabled bt(0,0) alias.");␊ |
131 | }␊ |
132 | }␊ |
133 | ␊ |
134 | void showInfoRAMDisk(void)␊ |
135 | {␊ |
136 | int len;␊ |
137 | const char *val;␊ |
138 | ␊ |
139 | if (gRAMDiskMI != NULL)␊ |
140 | {␊ |
141 | struct multiboot_module * ramdisk_module = (void *)gRAMDiskMI->mi_mods_addr;␊ |
142 | ␊ |
143 | printf("\nfile: %s %d", gRAMDiskFile,␊ |
144 | ramdisk_module->mm_mod_end - ramdisk_module->mm_mod_start);␊ |
145 | printf("\nalias: %s", gRAMDiskBTAliased ? "enabled" : "disabled");␊ |
146 | ␊ |
147 | // Display ramdisk information if available.␊ |
148 | ␊ |
149 | if (getValueForKey("Info", &val, &len, &bootInfo->ramdiskConfig))␊ |
150 | printf("\ninfo: %s", val);␊ |
151 | else␊ |
152 | printf("\nramdisk info not available.");␊ |
153 | ␊ |
154 | }␊ |
155 | else␊ |
156 | {␊ |
157 | printf("\nNo ramdisk mounted.");␊ |
158 | }␊ |
159 | }␊ |
160 | ␊ |
161 | int loadPrebootRAMDisk()␊ |
162 | {␊ |
163 | mountRAMDisk("bt(0,0)/Extra/Preboot.dmg");␊ |
164 | if (gRAMDiskMI != NULL)␊ |
165 | {␊ |
166 | printf("\n");␊ |
167 | return 0;␊ |
168 | }␊ |
169 | else␊ |
170 | {␊ |
171 | return -1;␊ |
172 | }␊ |
173 | }␊ |
174 | ␊ |
175 | void processRAMDiskCommand(char ** argPtr, const char * cmd)␊ |
176 | {␊ |
177 | char * ptr = *argPtr;␊ |
178 | char param[1024];␊ |
179 | getNextArg(&ptr, param);␊ |
180 | ␊ |
181 | if (strcmp(cmd, "m") == 0)␊ |
182 | {␊ |
183 | mountRAMDisk(param);␊ |
184 | sleep(2);␊ |
185 | }␊ |
186 | else if (strcmp(cmd, "u") == 0)␊ |
187 | {␊ |
188 | umountRAMDisk();␊ |
189 | sleep(2);␊ |
190 | }␊ |
191 | else if (strcmp(cmd, "e") == 0)␊ |
192 | {␊ |
193 | setRAMDiskBTHook(1);␊ |
194 | sleep(2);␊ |
195 | }␊ |
196 | else if (strcmp(cmd, "d") == 0)␊ |
197 | {␊ |
198 | setRAMDiskBTHook(0);␊ |
199 | sleep(2);␊ |
200 | }␊ |
201 | else if (strcmp(cmd, "i") == 0)␊ |
202 | {␊ |
203 | setActiveDisplayPage(1);␊ |
204 | clearScreenRows(0, 24);␊ |
205 | setCursorPosition(0, 0, 1);␊ |
206 | showInfoRAMDisk();␊ |
207 | printf("\n\nPress any key to continue.\n");␊ |
208 | getc();␊ |
209 | setActiveDisplayPage(0);␊ |
210 | }␊ |
211 | else␊ |
212 | {␊ |
213 | setActiveDisplayPage(1);␊ |
214 | clearScreenRows(0, 24);␊ |
215 | setCursorPosition(0, 0, 1);␊ |
216 | printf("\nusage:\n");␊ |
217 | printf("\n?rd i - display ramdisk information");␊ |
218 | printf("\n?rd m <filename> - mount ramdisk image\n?rd u - unmount ramdisk image");␊ |
219 | printf("\n?rd e - enable bt(0,0) alias\n?rd d - disable bt(0,0) alias");␊ |
220 | printf("\n\nPress any key to continue.\n");␊ |
221 | getc();␊ |
222 | setActiveDisplayPage(0);␊ |
223 | }␊ |
224 | }␊ |
225 | |