1 // 16bit code to access hard drives.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "disk.h" // floppy_13
9 #include "biosvar.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // inb_cmos
12 #include "util.h" // debug_enter
13 #include "ata.h" // ATA_*
15 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
16 #define DEBUGF(fmt, args...)
19 /****************************************************************
21 ****************************************************************/
23 #define DISK_STUB(regs) do { \
24 struct bregs *__regs = (regs); \
26 disk_ret(__regs, DISK_RET_SUCCESS); \
30 checksum_seg(u16 seg, u16 offset, u32 len)
35 sum += GET_FARVAR(seg, *(u8*)(offset+i));
40 basic_access(struct bregs *regs, u8 device, u16 command)
43 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
44 u16 sector = regs->cl & 0x3f;
47 if (count > 128 || count == 0 || sector == 0) {
48 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
50 disk_ret(regs, DISK_RET_EPARAM);
54 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
55 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
56 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
57 u16 nph = GET_EBDA(ata.devices[device].pchs.heads);
58 u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
60 // sanity check on cyl heads, sec
61 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
62 BX_INFO("int13_harddisk: function %02x, parameters out of"
63 " range %04x/%04x/%04x!\n"
64 , regs->ah, cylinder, head, sector);
65 disk_ret(regs, DISK_RET_EPARAM);
71 disk_ret(regs, DISK_RET_SUCCESS);
75 u16 segment = regs->es;
76 u16 offset = regs->bx;
80 if (nph != nlh || npspt != nlspt) {
81 // translate lchs to lba
82 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
84 status = ata_cmd_data(device, command, lba, count, segment, offset);
86 // XXX - see if lba access can always be used.
87 status = ata_cmd_data_chs(device, command
88 , cylinder, head, sector
89 , count, segment, offset);
92 // Set nb of sector transferred
93 regs->al = GET_EBDA(ata.trsfsectors);
96 BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
97 disk_ret(regs, DISK_RET_EBADTRACK);
99 disk_ret(regs, DISK_RET_SUCCESS);
103 emu_access(struct bregs *regs, u8 device, u16 command)
105 u16 count = regs->al;
106 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
107 u16 sector = regs->cl & 0x3f;
110 if ((count > 128) || (count == 0) || (sector == 0)) {
111 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
113 disk_ret(regs, DISK_RET_EPARAM);
117 u16 nlc = GET_EBDA(cdemu.vdevice.cylinders);
118 u16 nlh = GET_EBDA(cdemu.vdevice.heads);
119 u16 nlspt = GET_EBDA(cdemu.vdevice.spt);
121 // sanity check on cyl heads, sec
122 if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
123 BX_INFO("int13_harddisk: function %02x, parameters out of"
124 " range %04x/%04x/%04x!\n"
125 , regs->ah, cylinder, head, sector);
126 disk_ret(regs, DISK_RET_EPARAM);
132 disk_ret(regs, DISK_RET_SUCCESS);
136 u32 ilba = GET_EBDA(cdemu.ilba);
137 // calculate the virtual lba inside the image
138 u32 vlba= (((((u32)cylinder*(u32)nlh)+(u32)head)*(u32)nlspt)
141 u32 slba = (u32)vlba/4;
142 u16 before= (u16)vlba%4;
143 u32 lba = ilba + slba;
145 u16 segment = regs->es;
146 u16 offset = regs->bx;
148 u8 status = cdrom_read(device, lba, count*512, segment, offset, before*512);
150 BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
152 disk_ret(regs, DISK_RET_EBADTRACK);
155 disk_ret(regs, DISK_RET_SUCCESS);
159 extended_access(struct bregs *regs, u8 device, u16 command)
161 u16 count = GET_INT13EXT(regs, count);
162 u16 segment = GET_INT13EXT(regs, segment);
163 u16 offset = GET_INT13EXT(regs, offset);
165 // Can't use 64 bits lba
166 u32 lba = GET_INT13EXT(regs, lba2);
168 BX_PANIC("int13_harddisk: function %02x. Can't use 64bits lba\n"
170 disk_ret(regs, DISK_RET_EPARAM);
174 u8 type = GET_EBDA(ata.devices[device].type);
176 // Get 32 bits lba and check
177 lba = GET_INT13EXT(regs, lba1);
178 if (type == ATA_TYPE_ATA
179 && lba >= GET_EBDA(ata.devices[device].sectors)) {
180 BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
181 disk_ret(regs, DISK_RET_EPARAM);
187 disk_ret(regs, DISK_RET_SUCCESS);
192 if (type == ATA_TYPE_ATA)
193 status = ata_cmd_data(device, command, lba, count, segment, offset);
195 status = cdrom_read(device, lba, count*2048, segment, offset, 0);
197 SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
200 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
202 disk_ret(regs, DISK_RET_EBADTRACK);
205 disk_ret(regs, DISK_RET_SUCCESS);
209 /****************************************************************
210 * Hard Drive functions
211 ****************************************************************/
213 // disk controller reset
215 disk_1300(struct bregs *regs, u8 device)
222 disk_1301(struct bregs *regs, u8 device)
224 regs->ah = GET_BDA(disk_last_status);
225 disk_ret(regs, DISK_RET_SUCCESS);
230 disk_1302(struct bregs *regs, u8 device)
232 basic_access(regs, device, ATA_CMD_READ_SECTORS);
235 // write disk sectors
237 disk_1303(struct bregs *regs, u8 device)
239 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
242 // verify disk sectors
244 disk_1304(struct bregs *regs, u8 device)
246 basic_access(regs, device, 0);
252 disk_1305(struct bregs *regs, u8 device)
257 // read disk drive parameters
259 disk_1308(struct bregs *regs, u8 device)
261 // Get logical geometry from table
262 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
263 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
264 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
265 u16 count = GET_EBDA(ata.hdcount);
267 nlc = nlc - 2; /* 0 based , last sector not used */
269 regs->ch = nlc & 0xff;
270 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
272 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
274 // FIXME should set ES & DI
275 disk_ret(regs, DISK_RET_SUCCESS);
278 // initialize drive parameters
280 disk_1309(struct bregs *regs, u8 device)
285 // seek to specified cylinder
287 disk_130c(struct bregs *regs, u8 device)
292 // alternate disk reset
294 disk_130d(struct bregs *regs, u8 device)
301 disk_1310(struct bregs *regs, u8 device)
303 // should look at 40:8E also???
305 // Read the status from controller
306 u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
307 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
308 disk_ret(regs, DISK_RET_SUCCESS);
310 disk_ret(regs, DISK_RET_ENOTREADY);
315 disk_1311(struct bregs *regs, u8 device)
320 // controller internal diagnostic
322 disk_1314(struct bregs *regs, u8 device)
327 // read disk drive size
329 disk_1315(struct bregs *regs, u8 device)
331 // Get logical geometry from table
332 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
333 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
334 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
336 // Compute sector count seen by int13
337 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
338 regs->cx = lba >> 16;
339 regs->dx = lba & 0xffff;
341 disk_ret(regs, DISK_RET_SUCCESS);
342 regs->ah = 3; // hard disk accessible
345 // IBM/MS installation check
347 disk_1341(struct bregs *regs, u8 device)
349 regs->bx = 0xaa55; // install check
350 regs->cx = 0x0007; // ext disk access and edd, removable supported
351 disk_ret(regs, DISK_RET_SUCCESS);
352 regs->ah = 0x30; // EDD 3.0
355 // IBM/MS extended read
357 disk_1342(struct bregs *regs, u8 device)
359 extended_access(regs, device, ATA_CMD_READ_SECTORS);
362 // IBM/MS extended write
364 disk_1343(struct bregs *regs, u8 device)
366 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
371 disk_1344(struct bregs *regs, u8 device)
373 extended_access(regs, device, 0);
376 // IBM/MS lock/unlock drive
378 disk_1345(struct bregs *regs, u8 device)
380 // Always success for HD
381 disk_ret(regs, DISK_RET_SUCCESS);
384 // IBM/MS eject media
386 disk_1346(struct bregs *regs, u8 device)
388 // Volume Not Removable
389 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
392 // IBM/MS extended seek
394 disk_1347(struct bregs *regs, u8 device)
396 extended_access(regs, device, 0);
399 // IBM/MS get drive parameters
401 disk_1348(struct bregs *regs, u8 device)
403 u16 size = GET_INT13DPT(regs, size);
405 // Buffer is too small
407 disk_ret(regs, DISK_RET_EPARAM);
413 u8 type = GET_EBDA(ata.devices[device].type);
414 u16 npc = GET_EBDA(ata.devices[device].pchs.cylinders);
415 u16 nph = GET_EBDA(ata.devices[device].pchs.heads);
416 u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
417 u32 lba = GET_EBDA(ata.devices[device].sectors);
418 u16 blksize = GET_EBDA(ata.devices[device].blksize);
420 SET_INT13DPT(regs, size, 0x1a);
421 if (type == ATA_TYPE_ATA) {
422 if ((lba/npspt)/nph > 0x3fff) {
423 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
424 SET_INT13DPT(regs, cylinders, 0x3fff);
426 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
427 SET_INT13DPT(regs, cylinders, (u32)npc);
429 SET_INT13DPT(regs, heads, (u32)nph);
430 SET_INT13DPT(regs, spt, (u32)npspt);
431 SET_INT13DPT(regs, sector_count1, lba); // FIXME should be Bit64
432 SET_INT13DPT(regs, sector_count2, 0L);
435 // 0x74 = removable, media change, lockable, max values
436 SET_INT13DPT(regs, infos, 0x74);
437 SET_INT13DPT(regs, cylinders, 0xffffffff);
438 SET_INT13DPT(regs, heads, 0xffffffff);
439 SET_INT13DPT(regs, spt, 0xffffffff);
440 SET_INT13DPT(regs, sector_count1, 0xffffffff); // FIXME should be Bit64
441 SET_INT13DPT(regs, sector_count2, 0xffffffff);
443 SET_INT13DPT(regs, blksize, blksize);
446 disk_ret(regs, DISK_RET_SUCCESS);
452 SET_INT13DPT(regs, size, 0x1e);
454 SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
455 SET_INT13DPT(regs, dpte_offset
456 , offsetof(struct extended_bios_data_area_s, ata.dpte));
459 u8 channel = device / 2;
460 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
461 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
462 u8 irq = GET_EBDA(ata.channels[channel].irq);
463 u8 mode = GET_EBDA(ata.devices[device].mode);
466 if (type == ATA_TYPE_ATA) {
467 u8 translation = GET_EBDA(ata.devices[device].translation);
468 options = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
469 options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
470 options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
473 options = (1<<5); // removable device
474 options |= (1<<6); // atapi device
476 options |= (1<<4); // lba translation
477 options |= (mode==ATA_MODE_PIO32?1:0)<<7;
479 SET_EBDA(ata.dpte.iobase1, iobase1);
480 SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
481 SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
482 SET_EBDA(ata.dpte.unused, 0xcb );
483 SET_EBDA(ata.dpte.irq, irq );
484 SET_EBDA(ata.dpte.blkcount, 1 );
485 SET_EBDA(ata.dpte.dma, 0 );
486 SET_EBDA(ata.dpte.pio, 0 );
487 SET_EBDA(ata.dpte.options, options);
488 SET_EBDA(ata.dpte.reserved, 0);
490 SET_EBDA(ata.dpte.revision, 0x11);
492 SET_EBDA(ata.dpte.revision, 0x10);
494 u8 sum = checksum_seg(EBDA_SEG
495 , offsetof(struct extended_bios_data_area_s, ata.dpte)
497 SET_EBDA(ata.dpte.checksum, ~sum);
500 disk_ret(regs, DISK_RET_SUCCESS);
505 channel = device / 2;
506 u8 iface = GET_EBDA(ata.channels[channel].iface);
507 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
509 SET_INT13DPT(regs, size, 0x42);
510 SET_INT13DPT(regs, key, 0xbedd);
511 SET_INT13DPT(regs, dpi_length, 0x24);
512 SET_INT13DPT(regs, reserved1, 0);
513 SET_INT13DPT(regs, reserved2, 0);
515 if (iface==ATA_IFACE_ISA) {
516 SET_INT13DPT(regs, host_bus[0], 'I');
517 SET_INT13DPT(regs, host_bus[1], 'S');
518 SET_INT13DPT(regs, host_bus[2], 'A');
519 SET_INT13DPT(regs, host_bus[3], 0);
523 SET_INT13DPT(regs, iface_type[0], 'A');
524 SET_INT13DPT(regs, iface_type[1], 'T');
525 SET_INT13DPT(regs, iface_type[2], 'A');
526 SET_INT13DPT(regs, iface_type[3], 0);
528 if (iface==ATA_IFACE_ISA) {
529 SET_INT13DPT(regs, iface_path[0], iobase1);
530 SET_INT13DPT(regs, iface_path[2], 0);
531 SET_INT13DPT(regs, iface_path[4], 0L);
535 SET_INT13DPT(regs, device_path[0], device%2);
536 SET_INT13DPT(regs, device_path[1], 0);
537 SET_INT13DPT(regs, device_path[2], 0);
538 SET_INT13DPT(regs, device_path[4], 0L);
540 sum = checksum_seg(regs->ds, 30, 34);
541 SET_INT13DPT(regs, checksum, ~sum);
544 // IBM/MS extended media change
546 disk_1349(struct bregs *regs, u8 device)
548 // Always success for HD
549 disk_ret(regs, DISK_RET_SUCCESS);
553 disk_134e01(struct bregs *regs, u8 device)
555 disk_ret(regs, DISK_RET_SUCCESS);
559 disk_134e03(struct bregs *regs, u8 device)
561 disk_ret(regs, DISK_RET_SUCCESS);
565 disk_134e04(struct bregs *regs, u8 device)
567 disk_ret(regs, DISK_RET_SUCCESS);
571 disk_134e06(struct bregs *regs, u8 device)
573 disk_ret(regs, DISK_RET_SUCCESS);
577 disk_134eXX(struct bregs *regs, u8 device)
580 disk_ret(regs, DISK_RET_EPARAM);
583 // IBM/MS set hardware configuration
585 disk_134e(struct bregs *regs, u8 device)
588 case 0x01: disk_134e01(regs, device); break;
589 case 0x03: disk_134e03(regs, device); break;
590 case 0x04: disk_134e04(regs, device); break;
591 case 0x06: disk_134e06(regs, device); break;
592 default: disk_134eXX(regs, device); break;
597 disk_13XX(struct bregs *regs, u8 device)
600 disk_ret(regs, DISK_RET_EPARAM);
604 disk_13(struct bregs *regs, u8 device)
608 // clear completion flag
609 SET_BDA(disk_interrupt_flag, 0);
612 case 0x00: disk_1300(regs, device); break;
613 case 0x01: disk_1301(regs, device); break;
614 case 0x02: disk_1302(regs, device); break;
615 case 0x03: disk_1303(regs, device); break;
616 case 0x04: disk_1304(regs, device); break;
617 case 0x05: disk_1305(regs, device); break;
618 case 0x08: disk_1308(regs, device); break;
619 case 0x09: disk_1309(regs, device); break;
620 case 0x0c: disk_130c(regs, device); break;
621 case 0x0d: disk_130d(regs, device); break;
622 case 0x10: disk_1310(regs, device); break;
623 case 0x11: disk_1311(regs, device); break;
624 case 0x14: disk_1314(regs, device); break;
625 case 0x15: disk_1315(regs, device); break;
626 case 0x41: disk_1341(regs, device); break;
627 case 0x42: disk_1342(regs, device); break;
628 case 0x43: disk_1343(regs, device); break;
629 case 0x44: disk_1344(regs, device); break;
630 case 0x45: disk_1345(regs, device); break;
631 case 0x46: disk_1346(regs, device); break;
632 case 0x47: disk_1347(regs, device); break;
633 case 0x48: disk_1348(regs, device); break;
634 case 0x49: disk_1349(regs, device); break;
635 case 0x4e: disk_134e(regs, device); break;
636 default: disk_13XX(regs, device); break;
641 /****************************************************************
643 ****************************************************************/
646 get_device(struct bregs *regs, u8 iscd, u8 drive)
648 // basic check : device has to be defined
649 if (drive >= CONFIG_MAX_ATA_DEVICES) {
650 disk_ret(regs, DISK_RET_EPARAM);
651 return CONFIG_MAX_ATA_DEVICES;
654 // Get the ata channel
655 u8 device = GET_EBDA(ata.idmap[iscd][drive]);
657 // basic check : device has to be valid
658 if (device >= CONFIG_MAX_ATA_DEVICES) {
659 disk_ret(regs, DISK_RET_EPARAM);
660 return CONFIG_MAX_ATA_DEVICES;
667 handle_legacy_disk(struct bregs *regs, u8 drive)
670 floppy_13(regs, drive);
675 // XXX - old code had other disk access method.
676 disk_ret(regs, DISK_RET_EPARAM);
681 u8 device = get_device(regs, 1, drive - 0xe0);
682 if (device >= CONFIG_MAX_ATA_DEVICES)
684 cdrom_13(regs, device);
688 u8 device = get_device(regs, 0, drive - 0x80);
689 if (device >= CONFIG_MAX_ATA_DEVICES)
691 disk_13(regs, device);
695 handle_40(struct bregs *regs)
698 handle_legacy_disk(regs, regs->dl);
701 // INT 13h Fixed Disk Services Entry Point
703 handle_13(struct bregs *regs)
708 if (CONFIG_CDROM_BOOT) {
709 if (regs->ah == 0x4b) {
713 if (GET_EBDA(cdemu.active)) {
714 if (drive == GET_EBDA(cdemu.emulated_drive)) {
722 handle_legacy_disk(regs, drive);
725 // record completion in BIOS task complete flag
730 SET_BDA(disk_interrupt_flag, 0xff);