Debian Codegraph @

src/ints/bios_disk.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: bios_disk.cpp,v 1.40 2009-08-23 17:24:54 c2woody Exp $ */
20
21 #include "dosbox.h"
22 #include "callback.h"
23 #include "bios.h"
24 #include "regs.h"
25 #include "mem.h"
26 #include "dos_inc.h" /* for Drives[] */
27 #include "../dos/drives.h"
28 #include "mapper.h"
29
30
31
32 diskGeo DiskGeometryList[] = {
33 { 160, 8, 1, 40, 0},
34 { 180, 9, 1, 40, 0},
35 { 200, 10, 1, 40, 0},
36 { 320, 8, 2, 40, 1},
37 { 360, 9, 2, 40, 1},
38 { 400, 10, 2, 40, 1},
39 { 720, 9, 2, 80, 3},
40 {1200, 15, 2, 80, 2},
41 {1440, 18, 2, 80, 4},
42 {2880, 36, 2, 80, 6},
43 {0, 0, 0, 0, 0}
44 };
45
46 Bitu call_int13;
47 Bitu diskparm0, diskparm1;
48 static Bit8u last_status;
49 static Bit8u last_drive;
50 Bit16u imgDTASeg;
51 RealPt imgDTAPtr;
52 DOS_DTA *imgDTA;
53 bool killRead;
54 static bool swapping_requested;
55
56 void CMOS_SetRegister(Bitu regNr, Bit8u val); //For setting equipment word
57
58 /* 2 floppys and 2 harddrives, max */
59 imageDisk *imageDiskList[MAX_DISK_IMAGES];
60 imageDisk *diskSwap[MAX_SWAPPABLE_DISKS];
61 Bits swapPosition;
62
63 void updateDPT(void) {
64 Bit32u tmpheads, tmpcyl, tmpsect, tmpsize;
65 if(imageDiskList[2] != NULL) {
66 PhysPt dp0physaddr=CALLBACK_PhysPointer(diskparm0);
67 imageDiskList[2]->Get_Geometry(&tmpheads, &tmpcyl, &tmpsect, &tmpsize);
68 phys_writew(dp0physaddr,(Bit16u)tmpcyl);
69 phys_writeb(dp0physaddr+0x2,(Bit8u)tmpheads);
70 phys_writew(dp0physaddr+0x3,0);
71 phys_writew(dp0physaddr+0x5,(Bit16u)-1);
72 phys_writeb(dp0physaddr+0x7,0);
73 phys_writeb(dp0physaddr+0x8,(0xc0 | (((imageDiskList[2]->heads) > 8) << 3)));
74 phys_writeb(dp0physaddr+0x9,0);
75 phys_writeb(dp0physaddr+0xa,0);
76 phys_writeb(dp0physaddr+0xb,0);
77 phys_writew(dp0physaddr+0xc,(Bit16u)tmpcyl);
78 phys_writeb(dp0physaddr+0xe,(Bit8u)tmpsect);
79 }
80 if(imageDiskList[3] != NULL) {
81 PhysPt dp1physaddr=CALLBACK_PhysPointer(diskparm1);
82 imageDiskList[3]->Get_Geometry(&tmpheads, &tmpcyl, &tmpsect, &tmpsize);
83 phys_writew(dp1physaddr,(Bit16u)tmpcyl);
84 phys_writeb(dp1physaddr+0x2,(Bit8u)tmpheads);
85 phys_writeb(dp1physaddr+0xe,(Bit8u)tmpsect);
86 }
87 }
88
89 void swapInDisks(void) {
90 bool allNull = true;
91 Bits diskcount = 0;
92 Bits swapPos = swapPosition;
93 int i;
94
95 /* Check to make sure there's atleast one setup image */
96 for(i=0;i<MAX_SWAPPABLE_DISKS;i++) {
97 if(diskSwap[i]!=NULL) {
98 allNull = false;
99 break;
100 }
101 }
102
103 /* No disks setup... fail */
104 if (allNull) return;
105
106 /* If only one disk is loaded, this loop will load the same disk in dive A and drive B */
107 while(diskcount<2) {
108 if(diskSwap[swapPos] != NULL) {
109 LOG_MSG("Loaded disk %d from swaplist position %d - \"%s\"", diskcount, swapPos, diskSwap[swapPos]->diskname);
110 imageDiskList[diskcount] = diskSwap[swapPos];
111 diskcount++;
112 }
113 swapPos++;
114 if(swapPos>=MAX_SWAPPABLE_DISKS) swapPos=0;
115 }
116 }
117
118 bool getSwapRequest(void) {
119 bool sreq=swapping_requested;
120 swapping_requested = false;
121 return sreq;
122 }
123
124 void swapInNextDisk(bool pressed) {
125 if (!pressed)
126 return;
127 DriveManager::CycleAllDisks();
128 /* Hack/feature: rescan all disks as well */
129 LOG_MSG("Diskcaching reset for normal mounted drives.");
130 for(Bitu i=0;i<DOS_DRIVES;i++) {
131 if (Drives[i]) Drives[i]->EmptyCache();
132 }
133 swapPosition++;
134 if(diskSwap[swapPosition] == NULL) swapPosition = 0;
135 swapInDisks();
136 swapping_requested = true;
137 }
138
139
140 Bit8u imageDisk::Read_Sector(Bit32u head,Bit32u cylinder,Bit32u sector,void * data) {
141 Bit32u sectnum;
142
143 sectnum = ( (cylinder * heads + head) * sectors ) + sector - 1L;
144
145 return Read_AbsoluteSector(sectnum, data);
146 }
147
148 Bit8u imageDisk::Read_AbsoluteSector(Bit32u sectnum, void * data) {
149 Bit32u bytenum;
150
151 bytenum = sectnum * sector_size;
152
153 fseek(diskimg,bytenum,SEEK_SET);
154 fread(data, 1, sector_size, diskimg);
155
156 return 0x00;
157 }
158
159 Bit8u imageDisk::Write_Sector(Bit32u head,Bit32u cylinder,Bit32u sector,void * data) {
160 Bit32u sectnum;
161
162 sectnum = ( (cylinder * heads + head) * sectors ) + sector - 1L;
163
164 return Write_AbsoluteSector(sectnum, data);
165
166 }
167
168
169 Bit8u imageDisk::Write_AbsoluteSector(Bit32u sectnum, void *data) {
170 Bit32u bytenum;
171
172 bytenum = sectnum * sector_size;
173
174 //LOG_MSG("Writing sectors to %ld at bytenum %d", sectnum, bytenum);
175
176 fseek(diskimg,bytenum,SEEK_SET);
177 size_t ret=fwrite(data, sector_size, 1, diskimg);
178
179 return ((ret>0)?0x00:0x05);
180
181 }
182
183 imageDisk::imageDisk(FILE *imgFile, Bit8u *imgName, Bit32u imgSizeK, bool isHardDisk) {
184 heads = 0;
185 cylinders = 0;
186 sectors = 0;
187 sector_size = 512;
188 diskimg = imgFile;
189
190 memset(diskname,0,512);
191 if(strlen((const char *)imgName) > 511) {
192 memcpy(diskname, imgName, 511);
193 } else {
194 strcpy((char *)diskname, (const char *)imgName);
195 }
196
197 active = false;
198 hardDrive = isHardDisk;
199 if(!isHardDisk) {
200 Bit8u i=0;
201 bool founddisk = false;
202 while (DiskGeometryList[i].ksize!=0x0) {
203 if ((DiskGeometryList[i].ksize==imgSizeK) ||
204 (DiskGeometryList[i].ksize+1==imgSizeK)) {
205 if (DiskGeometryList[i].ksize!=imgSizeK)
206 LOG_MSG("ImageLoader: image file with additional data, might not load!");
207 founddisk = true;
208 active = true;
209 floppytype = i;
210 heads = DiskGeometryList[i].headscyl;
211 cylinders = DiskGeometryList[i].cylcount;
212 sectors = DiskGeometryList[i].secttrack;
213 break;
214 }
215 i++;
216 }
217 if(!founddisk) {
218 active = false;
219 } else {
220 Bit16u equipment=mem_readw(BIOS_CONFIGURATION);
221 if(equipment&1) {
222 Bitu numofdisks = (equipment>>6)&3;
223 numofdisks++;
224 if(numofdisks > 1) numofdisks=1;//max 2 floppies at the moment
225 equipment&=~0x00C0;
226 equipment|=(numofdisks<<6);
227 } else equipment|=1;
228 mem_writew(BIOS_CONFIGURATION,equipment);
229 CMOS_SetRegister(0x14, (Bit8u)(equipment&0xff));
230 }
231 }
232 }
233
234 void imageDisk::Set_Geometry(Bit32u setHeads, Bit32u setCyl, Bit32u setSect, Bit32u setSectSize) {
235 heads = setHeads;
236 cylinders = setCyl;
237 sectors = setSect;
238 sector_size = setSectSize;
239 active = true;
240 }
241
242 void imageDisk::Get_Geometry(Bit32u * getHeads, Bit32u *getCyl, Bit32u *getSect, Bit32u *getSectSize) {
243 *getHeads = heads;
244 *getCyl = cylinders;
245 *getSect = sectors;
246 *getSectSize = sector_size;
247 }
248
249 Bit8u imageDisk::GetBiosType(void) {
250 if(!hardDrive) {
251 return (Bit8u)DiskGeometryList[floppytype].biosval;
252 } else return 0;
253 }
254
255 Bit32u imageDisk::getSectSize(void) {
256 return sector_size;
257 }
258
259 static Bitu GetDosDriveNumber(Bitu biosNum) {
260 switch(biosNum) {
261 case 0x0:
262 return 0x0;
263 case 0x1:
264 return 0x1;
265 case 0x80:
266 return 0x2;
267 case 0x81:
268 return 0x3;
269 case 0x82:
270 return 0x4;
271 case 0x83:
272 return 0x5;
273 default:
274 return 0x7f;
275 }
276 }
277
278 static bool driveInactive(Bitu driveNum) {
279 if(driveNum>=(2 + MAX_HDD_IMAGES)) {
280 LOG(LOG_BIOS,LOG_ERROR)("Disk %d non-existant", driveNum);
281 last_status = 0x01;
282 CALLBACK_SCF(true);
283 return true;
284 }
285 if(imageDiskList[driveNum] == NULL) {
286 LOG(LOG_BIOS,LOG_ERROR)("Disk %d not active", driveNum);
287 last_status = 0x01;
288 CALLBACK_SCF(true);
289 return true;
290 }
291 if(!imageDiskList[driveNum]->active) {
292 LOG(LOG_BIOS,LOG_ERROR)("Disk %d not active", driveNum);
293 last_status = 0x01;
294 CALLBACK_SCF(true);
295 return true;
296 }
297 return false;
298 }
299
300
301 static Bitu INT13_DiskHandler(void) {
302 Bit16u segat, bufptr;
303 Bit8u sectbuf[512];
304 Bitu drivenum;
305 Bitu i,t;
306 last_drive = reg_dl;
307 drivenum = GetDosDriveNumber(reg_dl);
308 bool any_images = false;
309 for(i = 0;i < MAX_DISK_IMAGES;i++) {
310 if(imageDiskList[i]) any_images=true;
311 }
312
313 // unconditionally enable the interrupt flag
314 CALLBACK_SIF(true);
315
316 //drivenum = 0;
317 //LOG_MSG("INT13: Function %x called on drive %x (dos drive %d)", reg_ah, reg_dl, drivenum);
318 switch(reg_ah) {
319 case 0x0: /* Reset disk */
320 {
321 /* if there aren't any diskimages (so only localdrives and virtual drives)
322 * always succeed on reset disk. If there are diskimages then and only then
323 * do real checks
324 */
325 if (any_images && driveInactive(drivenum)) {
326 /* driveInactive sets carry flag if the specified drive is not available */
327 if ((machine==MCH_CGA) || (machine==MCH_PCJR)) {
328 /* those bioses call floppy drive reset for invalid drive values */
329 if (((imageDiskList[0]) && (imageDiskList[0]->active)) || ((imageDiskList[1]) && (imageDiskList[1]->active))) {
330 last_status = 0x00;
331 CALLBACK_SCF(false);
332 }
333 }
334 return CBRET_NONE;
335 }
336 last_status = 0x00;
337 CALLBACK_SCF(false);
338 }
339 break;
340 case 0x1: /* Get status of last operation */
341
342 if(last_status != 0x00) {
343 reg_ah = last_status;
344 CALLBACK_SCF(true);
345 } else {
346 reg_ah = 0x00;
347 CALLBACK_SCF(false);
348 }
349 break;
350 case 0x2: /* Read sectors */
351 if (reg_al==0) {
352 reg_ah = 0x01;
353 CALLBACK_SCF(true);
354 return CBRET_NONE;
355 }
356 if (!any_images) {
357 // Inherit the Earth cdrom (uses it as disk test)
358 if (((reg_dl&0x80)==0x80) && (reg_dh==0) && ((reg_cl&0x3f)==1)) {
359 reg_ah = 0;
360 CALLBACK_SCF(false);
361 return CBRET_NONE;
362 }
363 }
364 if (driveInactive(drivenum)) {
365 reg_ah = 0xff;
366 CALLBACK_SCF(true);
367 return CBRET_NONE;
368 }
369
370 segat = SegValue(es);
371 bufptr = reg_bx;
372 for(i=0;i<reg_al;i++) {
373 last_status = imageDiskList[drivenum]->Read_Sector((Bit32u)reg_dh, (Bit32u)(reg_ch | ((reg_cl & 0xc0)<< 2)), (Bit32u)((reg_cl & 63)+i), sectbuf);
374 if((last_status != 0x00) || (killRead)) {
375 LOG_MSG("Error in disk read");
376 killRead = false;
377 reg_ah = 0x04;
378 CALLBACK_SCF(true);
379 return CBRET_NONE;
380 }
381 for(t=0;t<512;t++) {
382 real_writeb(segat,bufptr,sectbuf[t]);
383 bufptr++;
384 }
385 }
386 reg_ah = 0x00;
387 CALLBACK_SCF(false);
388 break;
389 case 0x3: /* Write sectors */
390
391 if(driveInactive(drivenum)) {
392 reg_ah = 0xff;
393 CALLBACK_SCF(true);
394 return CBRET_NONE;
395 }
396
397
398 bufptr = reg_bx;
399 for(i=0;i<reg_al;i++) {
400 for(t=0;t<imageDiskList[drivenum]->getSectSize();t++) {
401 sectbuf[t] = real_readb(SegValue(es),bufptr);
402 bufptr++;
403 }
404
405 last_status = imageDiskList[drivenum]->Write_Sector((Bit32u)reg_dh, (Bit32u)(reg_ch | ((reg_cl & 0xc0) << 2)), (Bit32u)((reg_cl & 63) + i), &sectbuf[0]);
406 if(last_status != 0x00) {
407 CALLBACK_SCF(true);
408 return CBRET_NONE;
409 }
410 }
411 reg_ah = 0x00;
412 CALLBACK_SCF(false);
413 break;
414 case 0x04: /* Verify sectors */
415 if (reg_al==0) {
416 reg_ah = 0x01;
417 CALLBACK_SCF(true);
418 return CBRET_NONE;
419 }
420 if(driveInactive(drivenum)) return CBRET_NONE;
421
422 /* TODO: Finish coding this section */
423 /*
424 segat = SegValue(es);
425 bufptr = reg_bx;
426 for(i=0;i<reg_al;i++) {
427 last_status = imageDiskList[drivenum]->Read_Sector((Bit32u)reg_dh, (Bit32u)(reg_ch | ((reg_cl & 0xc0)<< 2)), (Bit32u)((reg_cl & 63)+i), sectbuf);
428 if(last_status != 0x00) {
429 LOG_MSG("Error in disk read");
430 CALLBACK_SCF(true);
431 return CBRET_NONE;
432 }
433 for(t=0;t<512;t++) {
434 real_writeb(segat,bufptr,sectbuf[t]);
435 bufptr++;
436 }
437 }*/
438 reg_ah = 0x00;
439 //Qbix: The following codes don't match my specs. al should be number of sector verified
440 //reg_al = 0x10; /* CRC verify failed */
441 //reg_al = 0x00; /* CRC verify succeeded */
442 CALLBACK_SCF(false);
443
444 break;
445 case 0x08: /* Get drive parameters */
446 if(driveInactive(drivenum)) {
447 last_status = 0x07;
448 reg_ah = last_status;
449 CALLBACK_SCF(true);
450 return CBRET_NONE;
451 }
452 reg_ax = 0x00;
453 reg_bl = imageDiskList[drivenum]->GetBiosType();
454 Bit32u tmpheads, tmpcyl, tmpsect, tmpsize;
455 imageDiskList[drivenum]->Get_Geometry(&tmpheads, &tmpcyl, &tmpsect, &tmpsize);
456 if (tmpcyl==0) LOG(LOG_BIOS,LOG_ERROR)("INT13 DrivParm: cylinder count zero!");
457 else tmpcyl--; // cylinder count -> max cylinder
458 if (tmpheads==0) LOG(LOG_BIOS,LOG_ERROR)("INT13 DrivParm: head count zero!");
459 else tmpheads--; // head count -> max head
460 reg_ch = (Bit8u)(tmpcyl & 0xff);
461 reg_cl = (Bit8u)(((tmpcyl >> 2) & 0xc0) | (tmpsect & 0x3f));
462 reg_dh = (Bit8u)tmpheads;
463 last_status = 0x00;
464 if (reg_dl&0x80) { // harddisks
465 reg_dl = 0;
466 if(imageDiskList[2] != NULL) reg_dl++;
467 if(imageDiskList[3] != NULL) reg_dl++;
468 } else { // floppy disks
469 reg_dl = 0;
470 if(imageDiskList[0] != NULL) reg_dl++;
471 if(imageDiskList[1] != NULL) reg_dl++;
472 }
473 CALLBACK_SCF(false);
474 break;
475 case 0x11: /* Recalibrate drive */
476 reg_ah = 0x00;
477 CALLBACK_SCF(false);
478 break;
479 case 0x17: /* Set disk type for format */
480 /* Pirates! needs this to load */
481 killRead = true;
482 reg_ah = 0x00;
483 CALLBACK_SCF(false);
484 break;
485 default:
486 LOG(LOG_BIOS,LOG_ERROR)("INT13: Function %x called on drive %x (dos drive %d)", reg_ah, reg_dl, drivenum);
487 reg_ah=0xff;
488 CALLBACK_SCF(true);
489 }
490 return CBRET_NONE;
491 }
492
493
494 void BIOS_SetupDisks(void) {
495 /* TODO Start the time correctly */
496 call_int13=CALLBACK_Allocate();
497 CALLBACK_Setup(call_int13,&INT13_DiskHandler,CB_IRET,"Int 13 Bios disk");
498 RealSetVec(0x13,CALLBACK_RealPointer(call_int13));
499 int i;
500 for(i=0;i<4;i++) {
501 imageDiskList[i] = NULL;
502 }
503
504 for(i=0;i<MAX_SWAPPABLE_DISKS;i++) {
505 diskSwap[i] = NULL;
506 }
507
508 diskparm0 = CALLBACK_Allocate();
509 diskparm1 = CALLBACK_Allocate();
510 swapPosition = 0;
511
512 RealSetVec(0x41,CALLBACK_RealPointer(diskparm0));
513 RealSetVec(0x46,CALLBACK_RealPointer(diskparm1));
514
515 PhysPt dp0physaddr=CALLBACK_PhysPointer(diskparm0);
516 PhysPt dp1physaddr=CALLBACK_PhysPointer(diskparm1);
517 for(i=0;i<16;i++) {
518 phys_writeb(dp0physaddr+i,0);
519 phys_writeb(dp1physaddr+i,0);
520 }
521
522 imgDTASeg = 0;
523
524 /* Setup the Bios Area */
525 mem_writeb(BIOS_HARDDISK_COUNT,2);
526
527 MAPPER_AddHandler(swapInNextDisk,MK_f4,MMOD1,"swapimg","Swap Image");
528 killRead = false;
529 swapping_requested = false;
530 }