Chameleon

Chameleon Commit Details

Date:2010-07-27 03:12:52 (13 years 8 months ago)
Author:Tamás Kosárszky
Commit:264
Parents: 263
Message:Fixed resolving of pseudo "rd" and "bt" devices => you should no longer get "Failed to read boot sector from BIOS device 100h Error=1" in case of unmounted preboot ramdisk. Removed unnecessary checks for preboot ramdisk presence.
Changes:
M/trunk/i386/boot2/ramdisk.c
M/trunk/i386/libsaio/sys.c

File differences

trunk/i386/libsaio/sys.c
9797
9898
9999
100
101
102
103
104
105
100106
101107
102108
......
110116
111117
112118
113
114
115
116
117119
118120
119121
......
513515
514516
515517
516
517
518
519
520
521
522
523
524
525
526
518
527519
528520
529521
......
10141006
10151007
10161008
1009
10171010
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
10321011
10331012
10341013
......
10511030
10521031
10531032
1054
10551033
10561034
10571035
10581036
1059
1037
10601038
1061
1039
10621040
1063
1064
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
10651057
1066
1058
1059
10671060
1068
1069
1070
1071
1072
1073
1074
1061
10751062
1076
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
10771073
10781074
10791075
{ 0, 0 }
};
// Pseudo BIOS devices
enum {
kPseudoBIOSDevRAMDisk = 0x100,
kPseudoBIOSDevBooter = 0x101
};
/*
* Max number of file descriptors.
*/
BVRef gBIOSBootVolume = NULL;
BVRef gBootVolume;
// zef - ramdisk variables
//extern BVRef gRAMDiskVolume;
//extern bool gRAMDiskBTAliased;
//static BVRef getBootVolumeRef( const char * path, const char ** outPath );
static BVRef newBootVolumeRef( int biosdev, int partno );
}
}
}
// turbo - bt(0,0) hook
if ((dp->biosdev + unit) == 0x101) {
// zef - use the ramdisk if available and the alias is active.
if (gRAMDiskVolume != NULL && gRAMDiskBTAliased) {
bvr = gRAMDiskVolume;
} else {
bvr = gBIOSBootVolume;
}
} else {
bvr = newBootVolumeRef(dp->biosdev + unit, partition);
}
bvr = newBootVolumeRef(dp->biosdev + unit, partition);
return open_bvr(bvr, path, flags);
}
}
if (*cp == RP) cp++;
biosdev = dp->biosdev + unit;
bvr = newBootVolumeRef(biosdev, part);
// turbo - bt(0,0) hook
if (biosdev == 0x101)
{
// zef - use the ramdisk if available and the alias is active.
if (gRAMDiskVolume != NULL && gRAMDiskBTAliased)
bvr = gRAMDiskVolume;
else
bvr = gBIOSBootVolume;
}
else
{
bvr = newBootVolumeRef(biosdev, part);
}
if(bvr == NULL)
return NULL;
}
}
//==========================================================================
// Function name is a misnomer as scanBootVolumes usually calls diskScanBootVolumes
// which caches the information. So it's only allocated on the first run.
static BVRef newBootVolumeRef( int biosdev, int partno )
{
BVRef bvr, bvr1, bvrChain;
BVRef bvr, bvr1, bvrChain;
// Fetch the volume list from the device.
bvr = bvr1 = NULL;
scanBootVolumes( biosdev, NULL );
bvrChain = getBVChainForBIOSDev(biosdev);
// Try resolving "rd" and "bt" devices first.
if (biosdev == kPseudoBIOSDevRAMDisk)
{
if (gRAMDiskVolume)
bvr1 = gRAMDiskVolume;
}
else if (biosdev == kPseudoBIOSDevBooter)
{
if (gRAMDiskVolume != NULL && gRAMDiskBTAliased)
bvr1 = gRAMDiskVolume;
else
bvr1 = gBIOSBootVolume;
}
else
{
// Fetch the volume list from the device.
// Look for a perfect match based on device and partition number.
scanBootVolumes( biosdev, NULL );
bvrChain = getBVChainForBIOSDev(biosdev);
for ( bvr1 = NULL, bvr = bvrChain; bvr; bvr = bvr->next )
{
if ( ( bvr->flags & kBVFlagNativeBoot ) == 0 ) continue;
bvr1 = bvr;
if ( bvr->part_no == partno ) break;
}
// Look for a perfect match based on device and partition number.
return bvr ? bvr : bvr1;
for ( bvr1 = NULL, bvr = bvrChain; bvr; bvr = bvr->next )
{
if ( ( bvr->flags & kBVFlagNativeBoot ) == 0 ) continue;
bvr1 = bvr;
if ( bvr->part_no == partno ) break;
}
}
return bvr ? bvr : bvr1;
}
//==========================================================================
trunk/i386/boot2/ramdisk.c
1616
1717
1818
19
2019
2120
2221
......
3534
3635
3736
38
39
40
41
42
43
37
38
39
4440
4541
4642
4743
48
4944
5045
5146
52
53
47
5448
5549
56
57
58
59
60
61
62
50
51
52
6353
6454
6555
6656
67
6857
6958
70
59
7160
7261
7362
7463
7564
7665
77
66
7867
7968
8069
......
8574
8675
8776
88
8977
9078
9179
9280
9381
94
9582
96
83
84
9785
98
99
100
86
87
88
89
10190
10291
103
104
92
10593
106
94
10795
10896
10997
110
11198
11299
113
114
115
116
117
118
119
120
121
122
123
124
125
126
100
101
102
103
104
105
127106
128
129
130
131
132
133
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
134123
135124
136125
137126
138
139
127
128
140129
141
142
143
144
145
130
131
132
133
134
146135
147
148
149
150
151
152
153
154
155
156
136
137
138
139
140
157141
158
159
160
161
162
163
164
165
142
143
144
145
166146
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
147
148
149
194150
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
151
152
153
154
210155
211
212
213
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
214202
215203
216204
217205
218
219
220
221
222
223
206
207
208
209
210
211
212
213
214
224215
225216
226217
227218
228
229
219
220
230221
231
232
233
222
223
224
234225
235
236
237
226
227
228
238229
239
240
241
242
243
244
245
246
247
248
249
250
230
231
232
233
234
235
236
237
238
239
240
241
242
243
251244
252245
253246
254247
255
256
257
258
259
260
261
262
263
264
248
249
250
251
252
253
254
255
256
257
265258
266259
267260
268261
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
316309
bool gRAMDiskBTAliased = false;
char gRAMDiskFile[512];
// Notify OS X that a ramdisk has been setup. XNU with attach this to /dev/md0
void md0Ramdisk()
{
if(fh < 0)
{
if(gRAMDiskVolume && !gRAMDiskBTAliased)
{
sprintf(filename, "rd(0,0)/Extra/%s", override_filename);
fh = open(filename, 0);
}
sprintf(filename, "rd(0,0)/Extra/%s", override_filename);
fh = open(filename, 0);
if(fh < 0)
{
sprintf(filename, "/Extra/%s", override_filename);
fh = open(filename, 0);
}
}
}
if(fh < 0)
{
// Fallback to Postboot.img
if(gRAMDiskVolume && !gRAMDiskBTAliased)
{
sprintf(filename, "rd(0,0)/Extra/Postboot.img");
fh = open(filename, 0);
}
sprintf(filename, "rd(0,0)/Extra/Postboot.img");
fh = open(filename, 0);
if(fh < 0)
{
sprintf(filename, "/Extra/Postboot.img");// Check /Extra if not in rd(0,0)
fh = open(filename, 0);
}
}
if (fh >= 0)
{
verbose("Enabling ramdisk %s\n", filename);
ramdiskPtr.size = file_size(fh);
ramdiskPtr.base = AllocateKernelMemory(ramdiskPtr.size);
if(ramdiskPtr.size && ramdiskPtr.base)
{
// Read new ramdisk image contents in kernel memory.
if(node != NULL)
{
DT__AddProperty(node, "RAMDisk", sizeof(RAMDiskParam), (void*)&ramdiskPtr);
}
else
{
verbose("Unable to notify Mac OS X of the ramdisk %s.\n", filename);
}
}
else {
else
{
verbose("Unable to read md0 image %s.\n", filename);
}
} else {
}
}
else
{
verbose("md0 image %s is empty.\n", filename);
}
close(fh);
}
}
void umountRAMDisk()
{
if (gRAMDiskMI != NULL)
{
// Release ramdisk BVRef and DiskBVMap.
struct DiskBVMap *oldMap = diskResetBootVolumes(0x100);
CacheReset();
diskFreeMap(oldMap);
// Free multiboot info and module structures.
if ((void *)gRAMDiskMI->mi_mods_addr != NULL) free((void *)gRAMDiskMI->mi_mods_addr);
if (gRAMDiskMI != NULL) free(gRAMDiskMI);
// Reset multiboot structures.
gMI = gRAMDiskMI = NULL;
*gRAMDiskFile = '\0';
if (gRAMDiskMI != NULL)
{
// Release ramdisk BVRef and DiskBVMap.
struct DiskBVMap *oldMap = diskResetBootVolumes(0x100);
CacheReset();
diskFreeMap(oldMap);
// Release ramdisk driver hooks.
p_get_ramdisk_info = NULL;
p_ramdiskReadBytes = NULL;
printf("\nunmounting: done");
}
// Free multiboot info and module structures.
if ((void *)gRAMDiskMI->mi_mods_addr != NULL) free((void *)gRAMDiskMI->mi_mods_addr);
if (gRAMDiskMI != NULL) free(gRAMDiskMI);
// Reset multiboot structures.
gMI = gRAMDiskMI = NULL;
*gRAMDiskFile = '\0';
// Release ramdisk driver hooks.
p_get_ramdisk_info = NULL;
p_ramdiskReadBytes = NULL;
// Reset ramdisk bvr
gRAMDiskVolume = NULL;
printf("\nunmounting: done");
}
}
int mountRAMDisk(const char * param)
{
int fh = 0, ramDiskSize;
int error = 0;
int fh = 0, ramDiskSize;
int error = 0;
// Get file handle for ramdisk file.
fh = open(param, 0);
if (fh != -1)
{
printf("\nreading ramdisk image: %s", param);
// Get file handle for ramdisk file.
fh = open(param, 0);
if (fh != -1)
{
printf("\nreading ramdisk image: %s", param);
ramDiskSize = file_size(fh);
if (ramDiskSize > 0)
{
// Unmount previously mounted image if exists.
umountRAMDisk();
// Read new ramdisk image contents into PREBOOT_DATA area.
if (read(fh, (char *)PREBOOT_DATA, ramDiskSize) != ramDiskSize) error = -1;
}
else error = -1;
ramDiskSize = file_size(fh);
if (ramDiskSize > 0)
{
// Unmount previously mounted image if exists.
umountRAMDisk();
close(fh);
}
else error = -1;
if (error == 0)
{
// Save filename in gRAMDiskFile to display information.
strcpy(gRAMDiskFile, param);
// Read new ramdisk image contents into PREBOOT_DATA area.
if (read(fh, (char *)PREBOOT_DATA, ramDiskSize) != ramDiskSize) error = -1;
}
else error = -1;
// Set gMI as well for the multiboot ramdisk driver hook.
gMI = gRAMDiskMI = malloc(sizeof(multiboot_info));
struct multiboot_module * ramdisk_module = malloc(sizeof(multiboot_module));
// Fill in multiboot info and module structures.
if (gRAMDiskMI != NULL && ramdisk_module != NULL)
{
gRAMDiskMI->mi_mods_count = 1;
gRAMDiskMI->mi_mods_addr = (uint32_t)ramdisk_module;
ramdisk_module->mm_mod_start = PREBOOT_DATA;
ramdisk_module->mm_mod_end = PREBOOT_DATA + ramDiskSize;
// Set ramdisk driver hooks.
p_get_ramdisk_info = &multiboot_get_ramdisk_info;
p_ramdiskReadBytes = &multibootRamdiskReadBytes;
int partCount; // unused
// Save bvr of the mounted image.
gRAMDiskVolume = diskScanBootVolumes(0x100, &partCount);
if(gRAMDiskVolume == NULL)
{
umountRAMDisk();
printf("\nRamdisk contains no partitions.");
}
else
{
char dirSpec[128];
close(fh);
}
else error = -1;
// Reading ramdisk configuration.
strcpy(dirSpec, RAMDISKCONFIG_FILENAME);
if (loadConfigFile(dirSpec, &bootInfo->ramdiskConfig) == 0)
{
getBoolForKey("BTAlias", &gRAMDiskBTAliased, &bootInfo->ramdiskConfig);
}
else
{
printf("\nno ramdisk config...\n");
}
printf("\nmounting: done");
}
}
if (error == 0)
{
// Save filename in gRAMDiskFile to display information.
strcpy(gRAMDiskFile, param);
}
return error;
// Set gMI as well for the multiboot ramdisk driver hook.
gMI = gRAMDiskMI = malloc(sizeof(multiboot_info));
struct multiboot_module * ramdisk_module = malloc(sizeof(multiboot_module));
// Fill in multiboot info and module structures.
if (gRAMDiskMI != NULL && ramdisk_module != NULL)
{
gRAMDiskMI->mi_mods_count = 1;
gRAMDiskMI->mi_mods_addr = (uint32_t)ramdisk_module;
ramdisk_module->mm_mod_start = PREBOOT_DATA;
ramdisk_module->mm_mod_end = PREBOOT_DATA + ramDiskSize;
// Set ramdisk driver hooks.
p_get_ramdisk_info = &multiboot_get_ramdisk_info;
p_ramdiskReadBytes = &multibootRamdiskReadBytes;
int partCount; // unused
// Save bvr of the mounted image.
gRAMDiskVolume = diskScanBootVolumes(0x100, &partCount);
if(gRAMDiskVolume == NULL)
{
umountRAMDisk();
printf("\nRamdisk contains no partitions.");
}
else
{
char dirSpec[128];
// Reading ramdisk configuration.
strcpy(dirSpec, RAMDISKCONFIG_FILENAME);
if (loadConfigFile(dirSpec, &bootInfo->ramdiskConfig) == 0)
{
getBoolForKey("BTAlias", &gRAMDiskBTAliased, &bootInfo->ramdiskConfig);
}
else
{
printf("\nno ramdisk config...\n");
}
printf("\nmounting: done");
}
}
}
return error;
}
void setRAMDiskBTHook(bool mode)
{
gRAMDiskBTAliased = mode;
if (mode) {
printf("\nEnabled bt(0,0) alias.");
} else {
printf("\nDisabled bt(0,0) alias.");
}
gRAMDiskBTAliased = mode;
if (mode)
{
printf("\nEnabled bt(0,0) alias.");
}
else
{
printf("\nDisabled bt(0,0) alias.");
}
}
void showInfoRAMDisk(void)
{
int len;
const char *val;
int len;
const char *val;
if (gRAMDiskMI != NULL)
{
struct multiboot_module * ramdisk_module = (void *)gRAMDiskMI->mi_mods_addr;
if (gRAMDiskMI != NULL)
{
struct multiboot_module * ramdisk_module = (void *)gRAMDiskMI->mi_mods_addr;
printf("\nfile: %s %d", gRAMDiskFile,
ramdisk_module->mm_mod_end - ramdisk_module->mm_mod_start);
printf("\nalias: %s", gRAMDiskBTAliased ? "enabled" : "disabled");
printf("\nfile: %s %d", gRAMDiskFile,
ramdisk_module->mm_mod_end - ramdisk_module->mm_mod_start);
printf("\nalias: %s", gRAMDiskBTAliased ? "enabled" : "disabled");
// Display ramdisk information if available.
if (getValueForKey("Info", &val, &len, &bootInfo->ramdiskConfig))
printf("\ninfo: %s", val);
else
printf("\nramdisk info not available.");
}
else
{
printf("\nNo ramdisk mounted.");
}
// Display ramdisk information if available.
if (getValueForKey("Info", &val, &len, &bootInfo->ramdiskConfig))
{
printf("\ninfo: %s", val);
}
else
{
printf("\nramdisk info not available.");
}
}
else
{
printf("\nNo ramdisk mounted.");
}
}
int loadPrebootRAMDisk()
{
mountRAMDisk("bt(0,0)/Extra/Preboot.dmg");
if (gRAMDiskMI != NULL)
{
printf("\n");
return 0;
}
else
{
return -1;
}
mountRAMDisk("bt(0,0)/Extra/Preboot.dmg");
if (gRAMDiskMI != NULL)
{
printf("\n");
return 0;
}
else
{
return -1;
}
}
void processRAMDiskCommand(char ** argPtr, const char * cmd)
{
char * ptr = *argPtr;
char param[1024];
getNextArg(&ptr, param);
if (strcmp(cmd, "m") == 0)
{
mountRAMDisk(param);
sleep(2);
}
else if (strcmp(cmd, "u") == 0)
{
umountRAMDisk();
sleep(2);
}
else if (strcmp(cmd, "e") == 0)
{
setRAMDiskBTHook(1);
sleep(2);
}
else if (strcmp(cmd, "d") == 0)
{
setRAMDiskBTHook(0);
sleep(2);
}
else if (strcmp(cmd, "i") == 0)
{
setActiveDisplayPage(1);
clearScreenRows(0, 24);
setCursorPosition(0, 0, 1);
showInfoRAMDisk();
printf("\n\nPress any key to continue.\n");
getc();
setActiveDisplayPage(0);
}
else
{
setActiveDisplayPage(1);
clearScreenRows(0, 24);
setCursorPosition(0, 0, 1);
printf("\nusage:\n");
printf("\n?rd i - display ramdisk information");
printf("\n?rd m <filename> - mount ramdisk image\n?rd u - unmount ramdisk image");
printf("\n?rd e - enable bt(0,0) alias\n?rd d - disable bt(0,0) alias");
printf("\n\nPress any key to continue.\n");
getc();
setActiveDisplayPage(0);
}
char * ptr = *argPtr;
char param[1024];
getNextArg(&ptr, param);
if (strcmp(cmd, "m") == 0)
{
mountRAMDisk(param);
sleep(2);
}
else if (strcmp(cmd, "u") == 0)
{
umountRAMDisk();
sleep(2);
}
else if (strcmp(cmd, "e") == 0)
{
setRAMDiskBTHook(true);
sleep(2);
}
else if (strcmp(cmd, "d") == 0)
{
setRAMDiskBTHook(false);
sleep(2);
}
else if (strcmp(cmd, "i") == 0)
{
setActiveDisplayPage(1);
clearScreenRows(0, 24);
setCursorPosition(0, 0, 1);
showInfoRAMDisk();
printf("\n\nPress any key to continue.\n");
getc();
setActiveDisplayPage(0);
}
else
{
setActiveDisplayPage(1);
clearScreenRows(0, 24);
setCursorPosition(0, 0, 1);
printf("\nusage:\n");
printf("\n?rd i - display ramdisk information");
printf("\n?rd m <filename> - mount ramdisk image\n?rd u - unmount ramdisk image");
printf("\n?rd e - enable bt(0,0) alias\n?rd d - disable bt(0,0) alias");
printf("\n\nPress any key to continue.\n");
getc();
setActiveDisplayPage(0);
}
}

Archive Download the corresponding diff file

Revision: 264