VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/floppy.c

Last change on this file was 106071, checked in by vboxsync, 7 weeks ago

BIOS: Remove no longer needed DMA write workaround for floppy and AHCI.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.5 KB
Line 
1/* $Id: floppy.c 106071 2024-09-17 12:08:24Z vboxsync $ */
2/** @file
3 * PC BIOS - ???
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 * --------------------------------------------------------------------
27 *
28 * This code is based on:
29 *
30 * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
31 *
32 * Copyright (C) 2002 MandrakeSoft S.A.
33 *
34 * MandrakeSoft S.A.
35 * 43, rue d'Aboukir
36 * 75002 Paris - France
37 * http://www.linux-mandrake.com/
38 * http://www.mandrakesoft.com/
39 *
40 * This library is free software; you can redistribute it and/or
41 * modify it under the terms of the GNU Lesser General Public
42 * License as published by the Free Software Foundation; either
43 * version 2 of the License, or (at your option) any later version.
44 *
45 * This library is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48 * Lesser General Public License for more details.
49 *
50 * You should have received a copy of the GNU Lesser General Public
51 * License along with this library; if not, write to the Free Software
52 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
53 *
54 */
55
56/*
57 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
58 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
59 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
60 * a choice of LGPL license versions is made available with the language indicating
61 * that LGPLv2 or any later version may be used, or where a choice of which version
62 * of the LGPL is applied is otherwise unspecified.
63 */
64
65
66#include <stdint.h>
67#include "inlines.h"
68#include "biosint.h"
69
70extern uint16_t get_floppy_dpt(uint8_t drive_type);
71
72//////////////////////
73// FLOPPY functions //
74//////////////////////
75
76inline void set_diskette_ret_status(uint8_t value)
77{
78 write_byte(0x0040, 0x0041, value);
79}
80
81void set_diskette_current_cyl(uint8_t drive, uint8_t cyl)
82{
83 if (drive > 1)
84 BX_PANIC("set_diskette_current_cyl: drive > 1\n");
85 write_byte(0x0040, 0x0094+drive, cyl);
86}
87
88#if 1 //BX_SUPPORT_FLOPPY
89
90#if DEBUG_INT13_FL
91# define BX_DEBUG_INT13_FL(...) BX_DEBUG(__VA_ARGS__)
92#else
93# define BX_DEBUG_INT13_FL(...)
94#endif
95
96#define BX_FLOPPY_ON_CNT 37 /* 2 seconds */
97
98extern int diskette_param_table; /* At a fixed location. */
99
100#ifndef VBOX_WITH_FLOPPY_IRQ_POLLING
101
102/**
103 * Wait for the 7th bit of 0040:003e to be set by int0e_handler.
104 * @returns first 7 bits of byte 0040:003e, interrupts disabled.
105 */
106uint8_t floppy_wait_for_interrupt(void)
107{
108 int_disable();
109 for (;;)
110 {
111 uint8_t val8 = read_byte(0x0040, 0x003e);
112 if (val8 & 0x80)
113 return val8 & ~0x7f;
114 int_enable_hlt_disable();
115 }
116}
117
118/**
119 * Wait for the 7th bit of 0040:003e to be set by int0e_handler or 0040:0040 to
120 * be cleared by the timer, clearing the interrupt flag on success.
121 *
122 * @returns 0 on timeout with interrupts enabled.
123 * All 8 bits at 0040:003e on interrupt with interrupts disabled (i.e.
124 * non-zero), after first clearing the 7th bit at 0040:003e.
125 */
126uint8_t floppy_wait_for_interrupt_or_timeout(void)
127{
128 int_disable();
129 for (;;)
130 {
131 uint8_t val8 = read_byte(0x0040, 0x0040);
132 if (val8 == 0) {
133 int_enable();
134 return 0;
135 }
136
137 val8 = read_byte(0x0040, 0x003e);
138 if (val8 & 0x80) {
139 write_byte(0x0040, 0x003e, val8 & 0x7f);
140 return val8;
141 }
142 int_enable_hlt_disable();
143 }
144}
145
146#endif /* !VBOX_WITH_FLOPPY_IRQ_POLLING */
147
148void floppy_reset_controller(uint16_t drive)
149{
150 uint8_t val8;
151
152 // Reset controller
153 val8 = inb(0x03f2);
154 outb(0x03f2, val8 & ~0x04);
155 outb(0x03f2, val8 | 0x04);
156
157 // Wait for controller to come out of reset
158 do {
159 val8 = inb(0x3f4);
160 } while ( (val8 & 0xc0) != 0x80 );
161
162 // Mark media in drive as unknown
163 val8 = read_byte(0x0040, 0x0090 + drive);
164 val8 &= ~0x10;
165 write_byte(0x0040, 0x90 + drive, val8);
166
167}
168
169void floppy_prepare_controller(uint16_t drive)
170{
171 uint8_t val8, dor, prev_reset;
172
173 // set 40:3e bit 7 to 0
174 val8 = read_byte(0x0040, 0x003e);
175 val8 &= 0x7f;
176 write_byte(0x0040, 0x003e, val8);
177
178 // turn on motor of selected drive, DMA & int enabled, normal operation
179 prev_reset = inb(0x03f2) & 0x04;
180 if (drive)
181 dor = 0x20;
182 else
183 dor = 0x10;
184 dor |= 0x0c;
185 dor |= drive;
186 outb(0x03f2, dor);
187
188 // reset the disk motor timeout value of INT 08
189 write_byte(0x0040,0x0040, BX_FLOPPY_ON_CNT);
190
191 // program data rate
192 val8 = read_byte(0x0040, 0x008b);
193 val8 >>= 6;
194 outb(0x03f7, val8);
195
196 // wait for drive readiness
197 do {
198 val8 = inb(0x3f4);
199 } while ( (val8 & 0xc0) != 0x80 );
200
201 if (prev_reset == 0) {
202#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
203 // turn on interrupts
204 int_enable();
205 // wait on 40:3e bit 7 to become 1
206 do {
207 val8 = read_byte(0x0040, 0x003e);
208 } while ( (val8 & 0x80) == 0 );
209 val8 &= 0x7f;
210 int_disable();
211#else
212 val8 = floppy_wait_for_interrupt(); /* (7th bit cleared in ret val) */
213#endif
214 write_byte(0x0040, 0x003e, val8);
215 }
216}
217
218bx_bool floppy_media_known(uint16_t drive)
219{
220 uint8_t val8;
221 uint16_t media_state_offset;
222
223 val8 = read_byte(0x0040, 0x003e); // diskette recal status
224 if (drive)
225 val8 >>= 1;
226 val8 &= 0x01;
227 if (val8 == 0)
228 return 0;
229
230 media_state_offset = 0x0090;
231 if (drive)
232 media_state_offset += 1;
233
234 val8 = read_byte(0x0040, media_state_offset);
235 val8 = (val8 >> 4) & 0x01;
236 if (val8 == 0)
237 return 0;
238
239 // checks passed, return KNOWN
240 return 1;
241}
242
243bx_bool floppy_read_id(uint16_t drive)
244{
245#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
246 uint8_t val8;
247#endif
248 int i;
249
250 floppy_prepare_controller(drive);
251
252 // send Read ID command (2 bytes) to controller
253 outb(0x03f5, 0x4a); // 4a: Read ID (MFM)
254 outb(0x03f5, drive); // 0=drive0, 1=drive1, head always 0
255
256#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
257 // turn on interrupts
258 int_enable();
259
260 // wait on 40:3e bit 7 to become 1
261 do {
262 val8 = (read_byte(0x0040, 0x003e) & 0x80);
263 } while ( val8 == 0 );
264
265 val8 = 0; // separate asm from while() loop
266 // turn off interrupts
267 int_disable();
268#else
269 floppy_wait_for_interrupt();
270#endif
271
272 // read 7 return status bytes from controller
273 for (i = 0; i < 7; ++i)
274 write_byte(0x0040, 0x0042 + i, inb(0x3f5));
275
276 if ((read_byte(0x0040, 0x0042 + 0) & 0xc0) != 0)
277 return 0;
278 else
279 return 1;
280}
281
282bx_bool floppy_drive_recal(uint16_t drive)
283{
284 uint8_t val8;
285 uint16_t curr_cyl_offset;
286
287 floppy_prepare_controller(drive);
288
289 // send Recalibrate command (2 bytes) to controller
290 outb(0x03f5, 0x07); // 07: Recalibrate
291 outb(0x03f5, drive); // 0=drive0, 1=drive1
292
293#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
294 // turn on interrupts
295 int_enable();
296
297 // wait on 40:3e bit 7 to become 1
298 do {
299 val8 = (read_byte(0x0040, 0x003e) & 0x80);
300 } while ( val8 == 0 );
301
302 val8 = 0; // separate asm from while() loop
303 // turn off interrupts
304 int_disable();
305
306 // set 40:3e bit 7 to 0, and calibrated bit
307 val8 = read_byte(0x0040, 0x003e);
308 val8 &= 0x7f;
309#else
310 val8 = floppy_wait_for_interrupt(); /* (7th bit cleared in ret val) */
311
312 // set 40:3e bit 7 to 0, and calibrated bit
313#endif
314 if (drive) {
315 val8 |= 0x02; // Drive 1 calibrated
316 curr_cyl_offset = 0x0095;
317 } else {
318 val8 |= 0x01; // Drive 0 calibrated
319 curr_cyl_offset = 0x0094;
320 }
321 write_byte(0x0040, 0x003e, val8);
322 write_byte(0x0040, curr_cyl_offset, 0); // current cylinder is 0
323
324 return 1;
325}
326
327
328bx_bool floppy_media_sense(uint16_t drive)
329{
330 bx_bool retval;
331 uint16_t media_state_offset;
332 uint8_t drive_type, config_data, media_state;
333
334 if (floppy_drive_recal(drive) == 0)
335 return 0;
336
337 // Try the diskette data rates in the following order:
338 // 1 Mbps -> 500 Kbps -> 300 Kbps -> 250 Kbps
339 // The 1 Mbps rate is only tried for 2.88M drives.
340
341 // ** config_data **
342 // Bitfields for diskette media control:
343 // Bit(s) Description (Table M0028)
344 // 7-6 last data rate set by controller
345 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
346 // 5-4 last diskette drive step rate selected
347 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
348 // 3-2 {data rate at start of operation}
349 // 1-0 reserved
350
351 // ** media_state **
352 // Bitfields for diskette drive media state:
353 // Bit(s) Description (Table M0030)
354 // 7-6 data rate
355 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
356 // 5 double stepping required (e.g. 360kB in 1.2MB)
357 // 4 media type established
358 // 3 drive capable of supporting 4MB media
359 // 2-0 on exit from BIOS, contains
360 // 000 trying 360kB in 360kB
361 // 001 trying 360kB in 1.2MB
362 // 010 trying 1.2MB in 1.2MB
363 // 011 360kB in 360kB established
364 // 100 360kB in 1.2MB established
365 // 101 1.2MB in 1.2MB established
366 // 110 reserved
367 // 111 all other formats/drives
368
369 /// @todo break out drive type determination
370 drive_type = inb_cmos(0x10);
371 if (drive == 0)
372 drive_type >>= 4;
373 else
374 drive_type &= 0x0f;
375 if ( drive_type == 1 ) {
376 // 360K 5.25" drive
377 config_data = 0x00; // 0000 0000
378 media_state = 0x15; // 0001 0101
379 retval = 1;
380 }
381 else if ( drive_type == 2 ) {
382 // 1.2 MB 5.25" drive
383 config_data = 0x00; // 0000 0000
384 media_state = 0x35; // 0011 0101 // need double stepping??? (bit 5)
385 retval = 1;
386 }
387 else if ( drive_type == 3 ) {
388 // 720K 3.5" drive
389 config_data = 0x00; // 0000 0000 ???
390 media_state = 0x17; // 0001 0111
391 retval = 1;
392 }
393 else if ( drive_type == 4 ) {
394 // 1.44 MB 3.5" drive
395 config_data = 0x00; // 0000 0000
396 media_state = 0x17; // 0001 0111
397 retval = 1;
398 }
399 else if ( drive_type == 5 ) {
400 // 2.88 MB 3.5" drive
401 config_data = 0xCC; // 1100 1100
402 media_state = 0xD7; // 1101 0111
403 retval = 1;
404 }
405 // Extended floppy size uses special cmos setting
406 else if ( drive_type == 14 || drive_type == 15 ) {
407 // 15.6 MB 3.5" (fake) || 63.5 MB 3.5" (fake) - report same as 2.88 MB.
408 config_data = 0xCC; // 1100 1100
409 media_state = 0xD7; // 1101 0111
410 retval = 1;
411 }
412 else {
413 // not recognized
414 config_data = 0x00; // 0000 0000
415 media_state = 0x00; // 0000 0000
416 retval = 0;
417 }
418
419 write_byte(0x0040, 0x008B, config_data);
420 while (!floppy_read_id(drive)) {
421 if ((config_data & 0xC0) == 0x80) {
422 // If even 250 Kbps failed, we can't do much
423 break;
424 }
425 switch (config_data & 0xC0) {
426 case 0xC0: // 1 Mbps
427 config_data = config_data & 0x3F | 0x00;
428 break;
429 case 0x00: // 500 Kbps
430 config_data = config_data & 0x3F | 0x40;
431 break;
432 case 0x40: // 300 Kbps
433 config_data = config_data & 0x3F | 0x80;
434 break;
435 }
436 write_byte(0x0040, 0x008B, config_data);
437 }
438
439 if (drive == 0)
440 media_state_offset = 0x0090;
441 else
442 media_state_offset = 0x0091;
443 write_byte(0x0040, 0x008B, config_data);
444 write_byte(0x0040, media_state_offset, media_state);
445
446 return retval;
447}
448
449
450bx_bool floppy_drive_exists(uint16_t drive)
451{
452 uint8_t drive_type;
453
454 // check CMOS to see if drive exists
455 /// @todo break out drive type determination
456 drive_type = inb_cmos(0x10);
457 if (drive == 0)
458 drive_type >>= 4;
459 else
460 drive_type &= 0x0f;
461 return drive_type != 0;
462}
463
464/// @todo put in a header
465#define AX r.gr.u.r16.ax
466#define BX r.gr.u.r16.bx
467#define CX r.gr.u.r16.cx
468#define DX r.gr.u.r16.dx
469#define SI r.gr.u.r16.si // not used
470#define DI r.gr.u.r16.di
471#define BP r.gr.u.r16.bp // not used
472#define ELDX r.gr.u.r16.sp
473#define DS r.ds // not used
474#define ES r.es
475#define FLAGS r.ra.flags.u.r16.flags
476
477void BIOSCALL int13_diskette_function(disk_regs_t r)
478{
479 uint8_t drive, num_sectors, track, sector, head;
480 uint16_t base_address, base_count, base_es;
481 uint8_t page, mode_register, val8, media_state;
482 uint8_t drive_type, num_floppies;
483 uint16_t last_addr;
484 int i;
485
486 BX_DEBUG_INT13_FL("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", __func__, AX, BX, CX, DX, ES);
487
488 SET_IF(); /* INT 13h always returns with interrupts enabled. */
489
490 switch ( GET_AH() ) {
491 case 0x00: // diskette controller reset
492 BX_DEBUG_INT13_FL("floppy f00\n");
493 drive = GET_ELDL();
494 if (drive > 1) {
495 SET_AH(1); // invalid param
496 set_diskette_ret_status(1);
497 SET_CF();
498 return;
499 }
500 /// @todo break out drive type determination
501 drive_type = inb_cmos(0x10);
502 if (drive == 0)
503 drive_type >>= 4;
504 else
505 drive_type &= 0x0f;
506 if (drive_type == 0) {
507 SET_AH(0x80); // drive not responding
508 set_diskette_ret_status(0x80);
509 SET_CF();
510 return;
511 }
512
513 // force re-calibration etc.
514 write_byte(0x0040, 0x003e, 0);
515
516 SET_AH(0);
517 set_diskette_ret_status(0);
518 CLEAR_CF(); // successful
519 set_diskette_current_cyl(drive, 0); // current cylinder
520 return;
521
522 case 0x01: // Read Diskette Status
523 CLEAR_CF();
524 val8 = read_byte(0x0000, 0x0441);
525 SET_AH(val8);
526 if (val8) {
527 SET_CF();
528 }
529 return;
530
531 case 0x02: // Read Diskette Sectors
532 case 0x03: // Write Diskette Sectors
533 case 0x04: // Verify Diskette Sectors
534 num_sectors = GET_AL();
535 track = GET_CH();
536 sector = GET_CL();
537 head = GET_DH();
538 drive = GET_ELDL();
539
540 if ( (drive > 1) || (head > 1) ||
541 (num_sectors == 0) || (num_sectors > 72) ) {
542 BX_INFO("%s: drive>1 || head>1 ...\n", __func__);
543 SET_AH(1);
544 set_diskette_ret_status(1);
545 SET_AL(0); // no sectors read
546 SET_CF(); // error occurred
547 return;
548 }
549
550 // see if drive exists
551 if (floppy_drive_exists(drive) == 0) {
552 BX_DEBUG_INT13_FL("failed (not ready)\n");
553 SET_AH(0x80); // not responding
554 set_diskette_ret_status(0x80);
555 SET_AL(0); // no sectors read
556 SET_CF(); // error occurred
557 return;
558 }
559
560 // see if media in drive, and type is known
561 if (floppy_media_known(drive) == 0) {
562 if (floppy_media_sense(drive) == 0) {
563 BX_DEBUG_INT13_FL("media not found\n");
564 SET_AH(0x0C); // Media type not found
565 set_diskette_ret_status(0x0C);
566 SET_AL(0); // no sectors read
567 SET_CF(); // error occurred
568 return;
569 }
570 }
571
572 if (GET_AH() == 0x02) {
573 // Read Diskette Sectors
574
575 //-----------------------------------
576 // set up DMA controller for transfer
577 //-----------------------------------
578
579 // es:bx = pointer to where to place information from diskette
580 // port 04: DMA-1 base and current address, channel 2
581 // port 05: DMA-1 base and current count, channel 2
582 /// @todo merge/factor out pointer normalization
583 page = (ES >> 12); // upper 4 bits
584 base_es = (ES << 4); // lower 16bits contributed by ES
585 base_address = base_es + BX; // lower 16 bits of address
586 // contributed by ES:BX
587 if ( base_address < base_es ) {
588 // in case of carry, adjust page by 1
589 page++;
590 }
591 base_count = (num_sectors * 512) - 1;
592
593 // check for 64K boundary overrun
594 last_addr = base_address + base_count;
595 if (last_addr < base_address) {
596 SET_AH(0x09);
597 set_diskette_ret_status(0x09);
598 SET_AL(0); // no sectors read
599 SET_CF(); // error occurred
600 return;
601 }
602
603 BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
604 outb(0x000a, 0x06);
605
606 BX_DEBUG_INT13_FL("clear flip-flop\n");
607 outb(0x000c, 0x00); // clear flip-flop
608 outb(0x0004, base_address);
609 outb(0x0004, base_address>>8);
610 BX_DEBUG_INT13_FL("clear flip-flop\n");
611 outb(0x000c, 0x00); // clear flip-flop
612 outb(0x0005, base_count);
613 outb(0x0005, base_count>>8);
614 BX_DEBUG_INT13_FL("xfer buf %x bytes at %x:%x\n",
615 base_count + 1, page, base_address);
616
617 // port 0b: DMA-1 Mode Register
618 mode_register = 0x46; // single mode, increment, autoinit disable,
619 // transfer type=write, channel 2
620 BX_DEBUG_INT13_FL("setting mode register\n");
621 outb(0x000b, mode_register);
622
623 BX_DEBUG_INT13_FL("setting page register\n");
624 // port 81: DMA-1 Page Register, channel 2
625 outb(0x0081, page);
626
627 BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
628 outb(0x000a, 0x02); // unmask channel 2
629
630 //--------------------------------------
631 // set up floppy controller for transfer
632 //--------------------------------------
633 floppy_prepare_controller(drive);
634
635 // send read-normal-data command (9 bytes) to controller
636 outb(0x03f5, 0xe6); // e6: read normal data
637 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
638 outb(0x03f5, track);
639 outb(0x03f5, head);
640 outb(0x03f5, sector);
641 outb(0x03f5, 2); // 512 byte sector size
642 outb(0x03f5, sector + num_sectors - 1); // last sector to read on track
643 outb(0x03f5, 0); // Gap length
644 outb(0x03f5, 0xff); // Gap length
645 BX_DEBUG_INT13_FL("read initiated\n");
646
647#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
648 // turn on interrupts
649 int_enable();
650
651 // wait on 40:3e bit 7 to become 1 or timeout (latter isn't armed so it won't happen)
652 do {
653 val8 = read_byte(0x0040, 0x0040);
654 if (val8 == 0) {
655 BX_DEBUG_INT13_FL("failed (not ready)\n");
656 floppy_reset_controller(drive);
657 SET_AH(0x80); // drive not ready (timeout)
658 set_diskette_ret_status(0x80);
659 SET_AL(0); // no sectors read
660 SET_CF(); // error occurred
661 return;
662 }
663 val8 = (read_byte(0x0040, 0x003e) & 0x80);
664 } while ( val8 == 0 );
665
666 val8 = 0; // separate asm from while() loop
667 // turn off interrupts
668 int_disable();
669
670 // set 40:3e bit 7 to 0
671 val8 = read_byte(0x0040, 0x003e);
672 val8 &= 0x7f;
673 write_byte(0x0040, 0x003e, val8);
674
675#else
676 val8 = floppy_wait_for_interrupt_or_timeout();
677 if (val8 == 0) { /* Note! Interrupts enabled in this branch. */
678 BX_DEBUG_INT13_FL("failed (not ready)\n");
679 floppy_reset_controller(drive);
680 SET_AH(0x80); // drive not ready (timeout)
681 set_diskette_ret_status(0x80);
682 SET_AL(0); // no sectors read
683 SET_CF(); // error occurred
684 return;
685 }
686#endif
687
688 // check port 3f4 for accessibility to status bytes
689 val8 = inb(0x3f4);
690 if ( (val8 & 0xc0) != 0xc0 )
691 BX_PANIC("%s: ctrl not ready\n", __func__);
692
693 // read 7 return status bytes from controller and store in BDA
694 for (i = 0; i < 7; ++i)
695 write_byte(0x0040, 0x0042 + i, inb(0x3f5));
696
697 if ((read_byte(0x0040, 0x0042 + 0) & 0xc0) != 0) {
698 BX_DEBUG_INT13_FL("failed (FDC failure)\n");
699 floppy_reset_controller(drive);
700 SET_AH(0x20);
701 set_diskette_ret_status(0x20);
702 SET_AL(0); // no sectors read
703 SET_CF(); // error occurred
704 return;
705 }
706
707 BX_DEBUG_INT13_FL("success!\n");
708 // ??? should track be new val from return_status[3] ?
709 set_diskette_current_cyl(drive, track);
710 // AL = number of sectors read (same value as passed)
711 SET_AH(0x00); // success
712 CLEAR_CF(); // success
713 return;
714 } else if (GET_AH() == 0x03) {
715 // Write Diskette Sectors
716
717 //-----------------------------------
718 // set up DMA controller for transfer
719 //-----------------------------------
720
721 // es:bx = pointer to where to place information from diskette
722 // port 04: DMA-1 base and current address, channel 2
723 // port 05: DMA-1 base and current count, channel 2
724 /// @todo merge/factor out pointer normalization
725 page = (ES >> 12); // upper 4 bits
726 base_es = (ES << 4); // lower 16bits contributed by ES
727 base_address = base_es + BX; // lower 16 bits of address
728 // contributed by ES:BX
729 if ( base_address < base_es ) {
730 // in case of carry, adjust page by 1
731 page++;
732 }
733 base_count = (num_sectors * 512) - 1;
734
735 // check for 64K boundary overrun
736 last_addr = base_address + base_count;
737 if (last_addr < base_address) {
738 SET_AH(0x09);
739 set_diskette_ret_status(0x09);
740 SET_AL(0); // no sectors read
741 SET_CF(); // error occurred
742 return;
743 }
744
745 BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
746 outb(0x000a, 0x06);
747
748 outb(0x000c, 0x00); // clear flip-flop
749 outb(0x0004, base_address);
750 outb(0x0004, base_address>>8);
751 outb(0x000c, 0x00); // clear flip-flop
752 outb(0x0005, base_count);
753 outb(0x0005, base_count>>8);
754 BX_DEBUG_INT13_FL("xfer buf %x bytes at %x:%x\n",
755 base_count, page, base_address);
756
757 // port 0b: DMA-1 Mode Register
758 mode_register = 0x4a; // single mode, increment, autoinit disable,
759 // transfer type=read, channel 2
760 outb(0x000b, mode_register);
761
762 // port 81: DMA-1 Page Register, channel 2
763 outb(0x0081, page);
764
765 BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
766 outb(0x000a, 0x02);
767
768 //--------------------------------------
769 // set up floppy controller for transfer
770 //--------------------------------------
771 floppy_prepare_controller(drive);
772
773 // send write-normal-data command (9 bytes) to controller
774 outb(0x03f5, 0xc5); // c5: write normal data
775 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
776 outb(0x03f5, track);
777 outb(0x03f5, head);
778 outb(0x03f5, sector);
779 outb(0x03f5, 2); // 512 byte sector size
780 outb(0x03f5, sector + num_sectors - 1); // last sector to write on track
781 outb(0x03f5, 0); // Gap length
782 outb(0x03f5, 0xff); // Gap length
783
784#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
785 // turn on interrupts
786 int_enable();
787
788 // wait on 40:3e bit 7 to become 1
789 do {
790 val8 = read_byte(0x0040, 0x0040);
791 if (val8 == 0) {
792 floppy_reset_controller(drive);
793 SET_AH(0x80); // drive not ready (timeout)
794 set_diskette_ret_status(0x80);
795 SET_AL(0); // no sectors written
796 SET_CF(); // error occurred
797 return;
798 }
799 val8 = (read_byte(0x0040, 0x003e) & 0x80);
800 } while ( val8 == 0 );
801
802 val8 = 0; // separate asm from while() loop @todo: why??
803 // turn off interrupts
804 int_disable();
805
806 // set 40:3e bit 7 to 0
807 val8 = read_byte(0x0040, 0x003e);
808 val8 &= 0x7f;
809 write_byte(0x0040, 0x003e, val8);
810#else
811 val8 = floppy_wait_for_interrupt_or_timeout();
812 if (val8 == 0) { /* Note! Interrupts enabled in this branch. */
813 floppy_reset_controller(drive);
814 SET_AH(0x80); // drive not ready (timeout)
815 set_diskette_ret_status(0x80);
816 SET_AL(0); // no sectors written
817 SET_CF(); // error occurred
818 return;
819 }
820#endif
821
822 // check port 3f4 for accessibility to status bytes
823 val8 = inb(0x3f4);
824 if ( (val8 & 0xc0) != 0xc0 )
825 BX_PANIC("%s: ctrl not ready\n", __func__);
826
827 // read 7 return status bytes from controller and store in BDA
828 for (i = 0; i < 7; ++i)
829 write_byte(0x0040, 0x0042 + i, inb(0x3f5));
830
831 if ((read_byte(0x0040, 0x0042 + 0) & 0xc0) != 0) {
832 if ((read_byte(0x0040, 0x0042 + 1) & 0x02) != 0) {
833 // diskette not writable.
834 // AH=status code=0x03 (tried to write on write-protected disk)
835 // AL=number of sectors written=0
836 AX = 0x0300;
837 } else {
838 // Some other problem occurred.
839 AX = 0x0100;
840 }
841 SET_CF();
842 return;
843 }
844
845 // ??? should track be new val from return_status[3] ?
846 set_diskette_current_cyl(drive, track);
847 // AL = number of sectors read (same value as passed)
848 SET_AH(0x00); // success
849 CLEAR_CF(); // success
850 return;
851 } else { // if (ah == 0x04)
852 // Verify Diskette Sectors
853
854 // ??? should track be new val from return_status[3] ?
855 set_diskette_current_cyl(drive, track);
856 // AL = number of sectors verified (same value as passed)
857 CLEAR_CF(); // success
858 SET_AH(0x00); // success
859 return;
860 }
861 break;
862
863 case 0x05: // format diskette track
864 BX_DEBUG_INT13_FL("floppy f05\n");
865
866 num_sectors = GET_AL();
867 track = GET_CH();
868 head = GET_DH();
869 drive = GET_ELDL();
870
871 if ((drive > 1) || (head > 1) || (track > 79) ||
872 (num_sectors == 0) || (num_sectors > 18)) {
873 SET_AH(1);
874 set_diskette_ret_status(1);
875 SET_CF(); // error occurred
876 }
877
878 // see if drive exists
879 if (floppy_drive_exists(drive) == 0) {
880 SET_AH(0x80); // drive not responding
881 set_diskette_ret_status(0x80);
882 SET_CF(); // error occurred
883 return;
884 }
885
886 // see if media in drive, and type is known
887 if (floppy_media_known(drive) == 0) {
888 if (floppy_media_sense(drive) == 0) {
889 SET_AH(0x0C); // Media type not found
890 set_diskette_ret_status(0x0C);
891 SET_AL(0); // no sectors read
892 SET_CF(); // error occurred
893 return;
894 }
895 }
896
897 // set up DMA controller for transfer
898 /// @todo merge/factor out pointer normalization
899 page = (ES >> 12); // upper 4 bits
900 base_es = (ES << 4); // lower 16bits contributed by ES
901 base_address = base_es + BX; // lower 16 bits of address
902 // contributed by ES:BX
903 if ( base_address < base_es ) {
904 // in case of carry, adjust page by 1
905 page++;
906 }
907 base_count = (num_sectors * 4) - 1;
908
909 // check for 64K boundary overrun
910 last_addr = base_address + base_count;
911 if (last_addr < base_address) {
912 SET_AH(0x09);
913 set_diskette_ret_status(0x09);
914 SET_AL(0); // no sectors read
915 SET_CF(); // error occurred
916 return;
917 }
918
919 outb(0x000a, 0x06);
920 outb(0x000c, 0x00); // clear flip-flop
921 outb(0x0004, base_address);
922 outb(0x0004, base_address>>8);
923 outb(0x000c, 0x00); // clear flip-flop
924 outb(0x0005, base_count);
925 outb(0x0005, base_count>>8);
926 mode_register = 0x4a; // single mode, increment, autoinit disable,
927 // transfer type=read, channel 2
928 outb(0x000b, mode_register);
929 // port 81: DMA-1 Page Register, channel 2
930 outb(0x0081, page);
931 outb(0x000a, 0x02);
932
933 // set up floppy controller for transfer
934 floppy_prepare_controller(drive);
935
936 // send seek command to controller
937 outb(0x03f5, 0x0f); // 0f: seek
938 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
939 outb(0x03f5, track);
940
941 // send format-track command (6 bytes) to controller
942 outb(0x03f5, 0x4d); // 4d: format track
943 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
944 outb(0x03f5, 2); // 512 byte sector size
945 outb(0x03f5, num_sectors); // number of sectors per track
946 outb(0x03f5, 0); // Gap length
947 outb(0x03f5, 0xf6); // Fill byte
948
949#ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
950 // turn on interrupts
951 int_enable();
952
953 // wait on 40:3e bit 7 to become 1
954 do {
955 val8 = read_byte(0x0040, 0x0040);
956 if (val8 == 0) {
957 floppy_reset_controller(drive);
958 SET_AH(0x80); // drive not ready (timeout)
959 set_diskette_ret_status(0x80);
960 SET_CF(); // error occurred
961 return;
962 }
963 val8 = (read_byte(0x0040, 0x003e) & 0x80);
964 } while ( val8 == 0 );
965
966 val8 = 0; // separate asm from while() loop
967 // turn off interrupts
968 int_disable();
969
970 // set 40:3e bit 7 to 0
971 val8 = read_byte(0x0040, 0x003e);
972 val8 &= 0x7f;
973 write_byte(0x0040, 0x003e, val8);
974#else
975 val8 = floppy_wait_for_interrupt_or_timeout();
976 if (val8 == 0) { /* Note! Interrupts enabled in this branch. */
977 floppy_reset_controller(drive);
978 SET_AH(0x80); // drive not ready (timeout)
979 set_diskette_ret_status(0x80);
980 SET_CF(); // error occurred
981 return;
982 }
983#endif
984
985 // check port 3f4 for accessibility to status bytes
986 val8 = inb(0x3f4);
987 if ( (val8 & 0xc0) != 0xc0 )
988 BX_PANIC("%s: ctrl not ready\n", __func__);
989
990 // read 7 return status bytes from controller and store in BDA
991 for (i = 0; i < 7; ++i)
992 write_byte(0x0040, 0x0042 + i, inb(0x3f5));
993
994 if ((read_byte(0x0040, 0x0042 + 0) & 0xc0) != 0) {
995 if ((read_byte(0x0040, 0x0042 + 1) & 0x02) != 0) {
996 // diskette not writable.
997 // AH=status code=0x03 (tried to write on write-protected disk)
998 // AL=number of sectors written=0
999 AX = 0x0300;
1000 SET_CF();
1001 return;
1002 } else {
1003 BX_PANIC("%s: write error\n", __func__);
1004 }
1005 }
1006
1007 SET_AH(0);
1008 set_diskette_ret_status(0);
1009 set_diskette_current_cyl(drive, 0);
1010 CLEAR_CF(); // successful
1011 return;
1012
1013
1014 case 0x08: // read diskette drive parameters
1015 BX_DEBUG_INT13_FL("floppy f08\n");
1016 drive = GET_ELDL();
1017
1018 if (drive > 1) {
1019 AX = 0;
1020 BX = 0;
1021 CX = 0;
1022 DX = 0;
1023 ES = 0;
1024 DI = 0;
1025 SET_DL(num_floppies);
1026 SET_CF();
1027 return;
1028 }
1029
1030 /// @todo break out drive type determination
1031 drive_type = inb_cmos(0x10);
1032 num_floppies = 0;
1033 if (drive_type & 0xf0)
1034 num_floppies++;
1035 if (drive_type & 0x0f)
1036 num_floppies++;
1037
1038 if (drive == 0)
1039 drive_type >>= 4;
1040 else
1041 drive_type &= 0x0f;
1042
1043 SET_BH(0);
1044 SET_BL(drive_type);
1045 SET_AH(0);
1046 SET_AL(0);
1047 SET_DL(num_floppies);
1048 SET_DH(1); // max head #
1049
1050 switch (drive_type) {
1051 case 0: // none
1052 CX = 0;
1053 SET_DH(0); // max head #
1054 break;
1055
1056 case 1: // 360KB, 5.25"
1057 CX = 0x2709; // 40 tracks, 9 sectors
1058 break;
1059
1060 case 2: // 1.2MB, 5.25"
1061 CX = 0x4f0f; // 80 tracks, 15 sectors
1062 break;
1063
1064 case 3: // 720KB, 3.5"
1065 CX = 0x4f09; // 80 tracks, 9 sectors
1066 break;
1067
1068 case 4: // 1.44MB, 3.5"
1069 CX = 0x4f12; // 80 tracks, 18 sectors
1070 break;
1071
1072 case 5: // 2.88MB, 3.5"
1073 CX = 0x4f24; // 80 tracks, 36 sectors
1074 break;
1075
1076 case 14: // 15.6 MB 3.5" (fake)
1077 CX = 0xfe3f; // 255 tracks, 63 sectors
1078 break;
1079
1080 case 15: // 63.5 MB 3.5" (fake)
1081 CX = 0xfeff; // 255 tracks, 255 sectors - This works because the cylinder
1082 break; // and sectors limits/encoding aren't checked by the BIOS
1083 // due to copy protection schemes and such stuff.
1084
1085 default: // ?
1086 BX_PANIC("%s: bad floppy type\n", __func__);
1087 }
1088
1089 /* set es & di to point to 11 byte diskette param table in ROM */
1090 ES = 0xF000; /// @todo any way to make this relocatable?
1091 DI = get_floppy_dpt(drive_type);
1092 CLEAR_CF(); // success
1093 /* disk status not changed upon success */
1094 return;
1095
1096 case 0x15: // read diskette drive type
1097 BX_DEBUG_INT13_FL("floppy f15\n");
1098 drive = GET_ELDL();
1099 if (drive > 1) {
1100 SET_AH(0); // only 2 drives supported
1101 // set_diskette_ret_status here ???
1102 SET_CF();
1103 return;
1104 }
1105 /// @todo break out drive type determination
1106 drive_type = inb_cmos(0x10);
1107 if (drive == 0)
1108 drive_type >>= 4;
1109 else
1110 drive_type &= 0x0f;
1111 CLEAR_CF(); // successful, not present
1112 if (drive_type==0) {
1113 SET_AH(0); // drive not present
1114 } else if (drive_type > 1) {
1115 SET_AH(2); // drive present, supports change line
1116 } else {
1117 SET_AH(1); // drive present, does not support change line
1118 }
1119
1120 return;
1121
1122 case 0x16: // get diskette change line status
1123 BX_DEBUG_INT13_FL("floppy f16\n");
1124 drive = GET_ELDL();
1125 if (drive > 1) {
1126 SET_AH(0x01); // invalid drive
1127 set_diskette_ret_status(0x01);
1128 SET_CF();
1129 return;
1130 }
1131
1132 SET_AH(0x06); // change line not supported
1133 set_diskette_ret_status(0x06);
1134 SET_CF();
1135 return;
1136
1137 case 0x17: // set diskette type for format(old)
1138 BX_DEBUG_INT13_FL("floppy f17\n");
1139 // NOTE: 1.44M diskette not supported by this function, use INT14h/18h instead.
1140 // Drive number (0 or 1) values allowed
1141 drive = GET_ELDL();
1142
1143 // Format type (AL)
1144 // 00 - NOT USED
1145 // 01 - DISKETTE 360K IN 360K DRIVE
1146 // 02 - DISKETTE 360K IN 1.2M DRIVE
1147 // 03 - DISKETTE 1.2M IN 1.2M DRIVE
1148 // 04 - DISKETTE 720K IN 720K DRIVE
1149 val8 = GET_AL();
1150
1151 BX_DEBUG_INT13_FL("floppy f17 - drive: %d, format type: %d\n", drive, val8);
1152
1153 if (drive > 1) {
1154 SET_AH(0x01); // invalid drive
1155 set_diskette_ret_status(0x01); // bad parameter
1156 SET_CF();
1157 return;
1158 }
1159
1160 // see if drive exists
1161 if (floppy_drive_exists(drive) == 0) {
1162 SET_AH(0x80); // not responding/time out
1163 set_diskette_ret_status(0x80);
1164 SET_CF();
1165 return;
1166 }
1167
1168 // Get current drive state. Set 'base_address' to media status offset address
1169 base_address = (drive) ? 0x0091 : 0x0090;
1170 media_state = read_byte(0x0040, base_address);
1171
1172 // Mask out (clear) bits 4-7 (4:media type established, 5:double stepping, 6-7:data rate)
1173 media_state &= 0x0f;
1174
1175 switch (val8) {
1176 case 1:
1177 // 360K media in 360K drive
1178 media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
1179 break;
1180 case 2:
1181 // 360K media in 1.2M drive
1182 media_state |= 0x70; // 0111 0000 (media type established, double stepping, 300 kbps)
1183 break;
1184 case 3:
1185 // 1.2M media in 1.2M drive
1186 media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
1187 break;
1188 case 4:
1189 // 720K media in 720K drive
1190 media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
1191 break;
1192 default:
1193 // bad parameter
1194 SET_AH(0x01); // invalid format mode parameter
1195 set_diskette_ret_status(0x01);
1196 SET_CF();
1197 return;
1198 }
1199
1200 // Update media status
1201 write_byte(0x0040, base_address, media_state);
1202 BX_DEBUG_INT13_FL("floppy f17 - media status set to: %02x\n", media_state);
1203
1204 // return success!
1205 SET_AH(0);
1206 set_diskette_ret_status(0);
1207 CLEAR_CF();
1208 return;
1209
1210 case 0x18: // set diskette type for format(new)
1211 BX_DEBUG_INT13_FL("floppy f18\n");
1212 // Set Media Type for Format. Verifies that the device supports a specific geometry.
1213 // Unlike INT13h/17h, this service supports higher capacity drives (1.44M and 2.88M).
1214 // Drive number (0 or 1) values allowed
1215 drive = GET_ELDL();
1216
1217 val8 = GET_CL();
1218 num_sectors = val8 & 0x3f; // max sector number per cylinder
1219 track = ((val8 >> 6) << 8) + GET_CH(); // max cylinder number (max cylinders - 1)
1220
1221 BX_DEBUG_INT13_FL("floppy f18 - drive: %d, max cylinder/track number: %d, sectors-per-tracks: %d\n",
1222 drive, track, num_sectors);
1223
1224 if (drive > 1) {
1225 SET_AH(0x01); // invalid drive
1226 set_diskette_ret_status(0x01);
1227 SET_CF();
1228 return;
1229 }
1230
1231 // see if drive exists
1232 if (floppy_drive_exists(drive) == 0) {
1233 SET_AH(0x80); // not responding/time out
1234 set_diskette_ret_status(0x80);
1235 SET_CF();
1236 return;
1237 }
1238
1239 // see if media in drive, and media type is known
1240 if (floppy_media_known(drive) == 0) {
1241 if (floppy_media_sense(drive) == 0) {
1242 SET_AH(0x0C); // drive/media type unknown
1243 set_diskette_ret_status(0x0C);
1244 SET_CF();
1245 return;
1246 }
1247 }
1248
1249 /// @todo break out drive type determination
1250 drive_type = inb_cmos(0x10);
1251 if (drive == 0)
1252 drive_type >>= 4;
1253 else
1254 drive_type &= 0x0f;
1255
1256 // Get current drive state. Set 'base_address' to media status offset address
1257 base_address = (drive) ? 0x0091 : 0x0090;
1258 media_state = read_byte(0x0040, base_address);
1259
1260 // Mask out (clear) bits 4-7 (4:media type established, 5:double stepping, 6-7:data rate)
1261 media_state &= 0x0f;
1262
1263 switch (drive_type) {
1264 case 1: // 360KB, 5.25"
1265 if (track == 39 && num_sectors == 9)
1266 media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
1267
1268 break;
1269 case 2: // 1.2MB, 5.25"
1270 if (track == 39 && num_sectors == 9) { // 360K disk in 1.2M drive
1271 media_state |= 0x70; // 0111 0000 (media type established, double stepping, 300 kbps)
1272 } else if (track == 79 && num_sectors == 15) { // 1.2M disk in 1.2M drive
1273 media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
1274 }
1275 break;
1276 case 3: // 720KB, 3.5"
1277 if (track == 79 && num_sectors == 9)
1278 media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
1279
1280 break;
1281 case 4: // 1.44MB, 3.5"
1282 if (track == 79) {
1283 if (num_sectors == 9) { // 720K disk in 1.44M drive
1284 media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
1285 } else if (num_sectors == 18) { // 1.44M disk in 1.44M drive
1286 media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
1287 }
1288 }
1289 break;
1290 case 5: // 2.88MB, 3.5"
1291 if (track == 79) {
1292 if (num_sectors == 9) { // 720K disk in 2.88M drive
1293 media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
1294 } else if (num_sectors == 18) { // 1.44M disk in 2.88M drive
1295 media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
1296 } else if (num_sectors == 36) { // 2.88M disk in 2.88M drive
1297 media_state |= 0xD0; // 1101 0000 (media type established, 1 Mbps)
1298 }
1299 }
1300 break;
1301 default:
1302 break;
1303 }
1304
1305 // Error if bit 4 (media type established) has not just been set above.
1306 if (((media_state >> 4) & 0x01) == 0) {
1307 // Error - assume requested tracks/sectors-per-track not supported
1308 // for current drive type - or drive type is unknown!
1309 SET_AH(0x0C);
1310 set_diskette_ret_status(0x0C);
1311 SET_CF();
1312 return;
1313 }
1314
1315 // Update media status
1316 write_byte(0x0040, base_address, media_state);
1317
1318 // set es & di to point to 11 byte diskette param table in ROM
1319 ES = 0xF000; /// @todo any way to make this relocatable?
1320 DI = get_floppy_dpt(drive_type);
1321
1322 // return success!
1323 SET_AH(0);
1324 set_diskette_ret_status(0);
1325 CLEAR_CF();
1326 return;
1327
1328 default:
1329 BX_INFO("%s: unsupported AH=%02x\n", __func__, GET_AH());
1330
1331 // if ( (ah==0x20) || ((ah>=0x41) && (ah<=0x49)) || (ah==0x4e) ) {
1332 SET_AH(0x01); // ???
1333 set_diskette_ret_status(1);
1334 SET_CF();
1335 return;
1336 // }
1337 }
1338}
1339
1340#else // #if BX_SUPPORT_FLOPPY
1341
1342void BIOSCALL int13_diskette_function(disk_regs_t r)
1343{
1344 uint8_t val8;
1345
1346 switch ( GET_AH() ) {
1347
1348 case 0x01: // Read Diskette Status
1349 CLEAR_CF();
1350 val8 = read_byte(0x0000, 0x0441);
1351 SET_AH(val8);
1352 if (val8) {
1353 SET_CF();
1354 }
1355 return;
1356
1357 default:
1358 SET_CF();
1359 write_byte(0x0000, 0x0441, 0x01);
1360 SET_AH(0x01);
1361 }
1362}
1363
1364#endif // #if BX_SUPPORT_FLOPPY
1365
1366/* Avoid saving general registers already saved by caller (PUSHA). */
1367#pragma aux int13_diskette_function modify [di si cx dx bx];
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette