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 ****************************************************************/
24 __disk_ret(const char *fname, struct bregs *regs, u8 code)
26 SET_BDA(disk_last_status, code);
28 __set_code_fail(fname, regs, code);
30 set_code_success(regs);
34 __disk_stub(const char *fname, struct bregs *regs)
36 __debug_stub(fname, regs);
37 __disk_ret(fname, regs, DISK_RET_SUCCESS);
40 #define DISK_STUB(regs) \
41 __disk_stub(__func__, (regs))
44 basic_access(struct bregs *regs, u8 device, u16 command)
46 u8 type = GET_EBDA(ata.devices[device].type);
48 if (type == ATA_TYPE_ATA) {
49 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
50 nlh = GET_EBDA(ata.devices[device].lchs.heads);
51 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
53 // Must be cd emulation.
54 nlc = GET_EBDA(cdemu.vdevice.cylinders);
55 nlh = GET_EBDA(cdemu.vdevice.heads);
56 nlspt = GET_EBDA(cdemu.vdevice.spt);
60 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
61 u16 sector = regs->cl & 0x3f;
64 if (count > 128 || count == 0 || sector == 0) {
65 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
67 disk_ret(regs, DISK_RET_EPARAM);
71 // sanity check on cyl heads, sec
72 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
73 BX_INFO("int13_harddisk: function %02x, parameters out of"
74 " range %04x/%04x/%04x!\n"
75 , regs->ah, cylinder, head, sector);
76 disk_ret(regs, DISK_RET_EPARAM);
82 disk_ret(regs, DISK_RET_SUCCESS);
86 // translate lchs to lba
87 u32 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
90 u16 segment = regs->es;
91 u16 offset = regs->bx;
92 void *far_buffer = MAKE_FARPTR(segment, offset);
97 if (type == ATA_TYPE_ATA)
98 status = ata_cmd_data(device, command, lba, count, far_buffer);
100 status = cdrom_read_emu(device, lba, count, far_buffer);
104 // Set nb of sector transferred
105 regs->al = GET_EBDA(ata.trsfsectors);
108 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
110 disk_ret(regs, DISK_RET_EBADTRACK);
112 disk_ret(regs, DISK_RET_SUCCESS);
116 extended_access(struct bregs *regs, u8 device, u16 command)
118 // Get lba and check.
119 u64 lba = GET_INT13EXT(regs, lba);
120 u8 type = GET_EBDA(ata.devices[device].type);
121 if (type == ATA_TYPE_ATA
122 && lba >= GET_EBDA(ata.devices[device].sectors)) {
123 BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
124 disk_ret(regs, DISK_RET_EPARAM);
130 disk_ret(regs, DISK_RET_SUCCESS);
134 u16 segment = GET_INT13EXT(regs, segment);
135 u16 offset = GET_INT13EXT(regs, offset);
136 void *far_buffer = MAKE_FARPTR(segment, offset);
137 u16 count = GET_INT13EXT(regs, count);
142 if (type == ATA_TYPE_ATA)
143 status = ata_cmd_data(device, command, lba, count, far_buffer);
145 status = cdrom_read(device, lba, count, far_buffer);
149 SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
152 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
154 disk_ret(regs, DISK_RET_EBADTRACK);
157 disk_ret(regs, DISK_RET_SUCCESS);
161 /****************************************************************
162 * Hard Drive functions
163 ****************************************************************/
165 // disk controller reset
167 disk_1300(struct bregs *regs, u8 device)
174 disk_1301(struct bregs *regs, u8 device)
176 u8 v = GET_BDA(disk_last_status);
179 // XXX - clear disk_last_status?
184 disk_1302(struct bregs *regs, u8 device)
186 basic_access(regs, device, ATA_CMD_READ_SECTORS);
189 // write disk sectors
191 disk_1303(struct bregs *regs, u8 device)
193 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
196 // verify disk sectors
198 disk_1304(struct bregs *regs, u8 device)
200 basic_access(regs, device, 0);
206 disk_1305(struct bregs *regs, u8 device)
211 // read disk drive parameters
213 disk_1308(struct bregs *regs, u8 device)
215 // Get logical geometry from table
216 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
217 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
218 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
219 u16 count = GET_EBDA(ata.hdcount);
221 nlc = nlc - 2; /* 0 based , last sector not used */
223 regs->ch = nlc & 0xff;
224 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
226 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
228 // FIXME should set ES & DI
229 disk_ret(regs, DISK_RET_SUCCESS);
232 // initialize drive parameters
234 disk_1309(struct bregs *regs, u8 device)
239 // seek to specified cylinder
241 disk_130c(struct bregs *regs, u8 device)
246 // alternate disk reset
248 disk_130d(struct bregs *regs, u8 device)
255 disk_1310(struct bregs *regs, u8 device)
257 // should look at 40:8E also???
259 // Read the status from controller
260 u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
261 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
262 disk_ret(regs, DISK_RET_SUCCESS);
264 disk_ret(regs, DISK_RET_ENOTREADY);
269 disk_1311(struct bregs *regs, u8 device)
274 // controller internal diagnostic
276 disk_1314(struct bregs *regs, u8 device)
281 // read disk drive size
283 disk_1315(struct bregs *regs, u8 device)
285 // Get logical geometry from table
286 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
287 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
288 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
290 // Compute sector count seen by int13
291 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
292 regs->cx = lba >> 16;
293 regs->dx = lba & 0xffff;
295 disk_ret(regs, DISK_RET_SUCCESS);
296 regs->ah = 3; // hard disk accessible
299 // IBM/MS installation check
301 disk_1341(struct bregs *regs, u8 device)
303 regs->bx = 0xaa55; // install check
304 regs->cx = 0x0007; // ext disk access and edd, removable supported
305 disk_ret(regs, DISK_RET_SUCCESS);
306 regs->ah = 0x30; // EDD 3.0
309 // IBM/MS extended read
311 disk_1342(struct bregs *regs, u8 device)
313 extended_access(regs, device, ATA_CMD_READ_SECTORS);
316 // IBM/MS extended write
318 disk_1343(struct bregs *regs, u8 device)
320 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
325 disk_1344(struct bregs *regs, u8 device)
327 extended_access(regs, device, 0);
330 // IBM/MS lock/unlock drive
332 disk_1345(struct bregs *regs, u8 device)
334 // Always success for HD
335 disk_ret(regs, DISK_RET_SUCCESS);
338 // IBM/MS eject media
340 disk_1346(struct bregs *regs, u8 device)
342 // Volume Not Removable
343 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
346 // IBM/MS extended seek
348 disk_1347(struct bregs *regs, u8 device)
350 extended_access(regs, device, 0);
353 // IBM/MS get drive parameters
355 disk_1348(struct bregs *regs, u8 device)
357 u16 size = GET_INT13DPT(regs, size);
359 // Buffer is too small
361 disk_ret(regs, DISK_RET_EPARAM);
367 u8 type = GET_EBDA(ata.devices[device].type);
368 u16 npc = GET_EBDA(ata.devices[device].pchs.cylinders);
369 u16 nph = GET_EBDA(ata.devices[device].pchs.heads);
370 u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
371 u64 lba = GET_EBDA(ata.devices[device].sectors);
372 u16 blksize = GET_EBDA(ata.devices[device].blksize);
374 SET_INT13DPT(regs, size, 0x1a);
375 if (type == ATA_TYPE_ATA) {
376 if (lba > (u64)npspt*nph*0x3fff) {
377 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
378 SET_INT13DPT(regs, cylinders, 0x3fff);
380 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
381 SET_INT13DPT(regs, cylinders, (u32)npc);
383 SET_INT13DPT(regs, heads, (u32)nph);
384 SET_INT13DPT(regs, spt, (u32)npspt);
385 SET_INT13DPT(regs, sector_count, lba);
388 // 0x74 = removable, media change, lockable, max values
389 SET_INT13DPT(regs, infos, 0x74);
390 SET_INT13DPT(regs, cylinders, 0xffffffff);
391 SET_INT13DPT(regs, heads, 0xffffffff);
392 SET_INT13DPT(regs, spt, 0xffffffff);
393 SET_INT13DPT(regs, sector_count, (u64)-1);
395 SET_INT13DPT(regs, blksize, blksize);
398 disk_ret(regs, DISK_RET_SUCCESS);
404 SET_INT13DPT(regs, size, 0x1e);
406 SET_INT13DPT(regs, dpte_segment, SEG_EBDA);
407 SET_INT13DPT(regs, dpte_offset
408 , offsetof(struct extended_bios_data_area_s, ata.dpte));
411 u8 channel = device / 2;
412 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
413 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
414 u8 irq = GET_EBDA(ata.channels[channel].irq);
415 u8 mode = GET_EBDA(ata.devices[device].mode);
418 if (type == ATA_TYPE_ATA) {
419 u8 translation = GET_EBDA(ata.devices[device].translation);
420 options = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
421 options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
422 options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
425 options = (1<<5); // removable device
426 options |= (1<<6); // atapi device
428 options |= (1<<4); // lba translation
429 options |= (mode==ATA_MODE_PIO32?1:0)<<7;
431 SET_EBDA(ata.dpte.iobase1, iobase1);
432 SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
433 SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
434 SET_EBDA(ata.dpte.unused, 0xcb );
435 SET_EBDA(ata.dpte.irq, irq );
436 SET_EBDA(ata.dpte.blkcount, 1 );
437 SET_EBDA(ata.dpte.dma, 0 );
438 SET_EBDA(ata.dpte.pio, 0 );
439 SET_EBDA(ata.dpte.options, options);
440 SET_EBDA(ata.dpte.reserved, 0);
442 SET_EBDA(ata.dpte.revision, 0x11);
444 SET_EBDA(ata.dpte.revision, 0x10);
446 u8 *p = MAKE_FARPTR(SEG_EBDA
447 , offsetof(struct extended_bios_data_area_s, ata.dpte));
448 u8 sum = checksum(p, 15);
449 SET_EBDA(ata.dpte.checksum, ~sum);
452 disk_ret(regs, DISK_RET_SUCCESS);
457 channel = device / 2;
458 u8 iface = GET_EBDA(ata.channels[channel].iface);
459 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
461 SET_INT13DPT(regs, size, 0x42);
462 SET_INT13DPT(regs, key, 0xbedd);
463 SET_INT13DPT(regs, dpi_length, 0x24);
464 SET_INT13DPT(regs, reserved1, 0);
465 SET_INT13DPT(regs, reserved2, 0);
467 if (iface==ATA_IFACE_ISA) {
468 SET_INT13DPT(regs, host_bus[0], 'I');
469 SET_INT13DPT(regs, host_bus[1], 'S');
470 SET_INT13DPT(regs, host_bus[2], 'A');
471 SET_INT13DPT(regs, host_bus[3], 0);
475 SET_INT13DPT(regs, iface_type[0], 'A');
476 SET_INT13DPT(regs, iface_type[1], 'T');
477 SET_INT13DPT(regs, iface_type[2], 'A');
478 SET_INT13DPT(regs, iface_type[3], 0);
480 if (iface==ATA_IFACE_ISA) {
481 SET_INT13DPT(regs, iface_path[0], iobase1);
482 SET_INT13DPT(regs, iface_path[2], 0);
483 SET_INT13DPT(regs, iface_path[4], 0L);
487 SET_INT13DPT(regs, device_path[0], device%2);
488 SET_INT13DPT(regs, device_path[1], 0);
489 SET_INT13DPT(regs, device_path[2], 0);
490 SET_INT13DPT(regs, device_path[4], 0L);
492 sum = checksum(MAKE_FARPTR(regs->ds, 30), 34);
493 SET_INT13DPT(regs, checksum, ~sum);
496 // IBM/MS extended media change
498 disk_1349(struct bregs *regs, u8 device)
500 // Always success for HD
501 disk_ret(regs, DISK_RET_SUCCESS);
505 disk_134e01(struct bregs *regs, u8 device)
507 disk_ret(regs, DISK_RET_SUCCESS);
511 disk_134e03(struct bregs *regs, u8 device)
513 disk_ret(regs, DISK_RET_SUCCESS);
517 disk_134e04(struct bregs *regs, u8 device)
519 disk_ret(regs, DISK_RET_SUCCESS);
523 disk_134e06(struct bregs *regs, u8 device)
525 disk_ret(regs, DISK_RET_SUCCESS);
529 disk_134eXX(struct bregs *regs, u8 device)
531 disk_ret(regs, DISK_RET_EPARAM);
534 // IBM/MS set hardware configuration
536 disk_134e(struct bregs *regs, u8 device)
539 case 0x01: disk_134e01(regs, device); break;
540 case 0x03: disk_134e03(regs, device); break;
541 case 0x04: disk_134e04(regs, device); break;
542 case 0x06: disk_134e06(regs, device); break;
543 default: disk_134eXX(regs, device); break;
548 disk_13XX(struct bregs *regs, u8 device)
550 disk_ret(regs, DISK_RET_EPARAM);
554 disk_13(struct bregs *regs, u8 device)
558 // clear completion flag
559 SET_BDA(disk_interrupt_flag, 0);
562 case 0x00: disk_1300(regs, device); break;
563 case 0x01: disk_1301(regs, device); break;
564 case 0x02: disk_1302(regs, device); break;
565 case 0x03: disk_1303(regs, device); break;
566 case 0x04: disk_1304(regs, device); break;
567 case 0x05: disk_1305(regs, device); break;
568 case 0x08: disk_1308(regs, device); break;
569 case 0x09: disk_1309(regs, device); break;
570 case 0x0c: disk_130c(regs, device); break;
571 case 0x0d: disk_130d(regs, device); break;
572 case 0x10: disk_1310(regs, device); break;
573 case 0x11: disk_1311(regs, device); break;
574 case 0x14: disk_1314(regs, device); break;
575 case 0x15: disk_1315(regs, device); break;
576 case 0x41: disk_1341(regs, device); break;
577 case 0x42: disk_1342(regs, device); break;
578 case 0x43: disk_1343(regs, device); break;
579 case 0x44: disk_1344(regs, device); break;
580 case 0x45: disk_1345(regs, device); break;
581 case 0x46: disk_1346(regs, device); break;
582 case 0x47: disk_1347(regs, device); break;
583 case 0x48: disk_1348(regs, device); break;
584 case 0x49: disk_1349(regs, device); break;
585 case 0x4e: disk_134e(regs, device); break;
586 default: disk_13XX(regs, device); break;
591 /****************************************************************
593 ****************************************************************/
596 get_device(struct bregs *regs, u8 iscd, u8 drive)
598 // basic check : device has to be defined
599 if (drive >= CONFIG_MAX_ATA_DEVICES) {
600 disk_ret(regs, DISK_RET_EPARAM);
601 return CONFIG_MAX_ATA_DEVICES;
604 // Get the ata channel
605 u8 device = GET_EBDA(ata.idmap[iscd][drive]);
607 // basic check : device has to be valid
608 if (device >= CONFIG_MAX_ATA_DEVICES) {
609 disk_ret(regs, DISK_RET_EPARAM);
610 return CONFIG_MAX_ATA_DEVICES;
617 handle_legacy_disk(struct bregs *regs, u8 drive)
620 floppy_13(regs, drive);
625 // XXX - old code had other disk access method.
626 disk_ret(regs, DISK_RET_EPARAM);
631 u8 device = get_device(regs, 1, drive - 0xe0);
632 if (device >= CONFIG_MAX_ATA_DEVICES)
634 cdrom_13(regs, device);
638 u8 device = get_device(regs, 0, drive - 0x80);
639 if (device >= CONFIG_MAX_ATA_DEVICES)
641 disk_13(regs, device);
645 handle_40(struct bregs *regs)
648 handle_legacy_disk(regs, regs->dl);
651 // INT 13h Fixed Disk Services Entry Point
653 handle_13(struct bregs *regs)
658 if (CONFIG_CDROM_EMU) {
659 if (regs->ah == 0x4b) {
663 if (GET_EBDA(cdemu.active)) {
664 if (drive == GET_EBDA(cdemu.emulated_drive)) {
672 handle_legacy_disk(regs, drive);
675 // record completion in BIOS task complete flag
680 SET_BDA(disk_interrupt_flag, 0xff);