VirtualBox

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

Last change on this file since 49890 was 49331, checked in by vboxsync, 11 years ago

BIOS: Seek before formatting a track.

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