VirtualBox

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

Last change on this file since 67075 was 67069, checked in by vboxsync, 8 years ago

BIOS: Reduced INT 13h floppy service stack usage.

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