Return CDEMU parameters to ebda segment (instead of using globals).
[seabios.git] / src / disk.c
1 // 16bit code to access hard drives.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "disk.h" // floppy_13
9 #include "biosvar.h" // SET_BDA
10 #include "config.h" // CONFIG_*
11 #include "util.h" // debug_enter
12 #include "ata.h" // ATA_*
13 #include "pic.h" // eoi_pic2
14 #include "bregs.h" // struct bregs
15 #include "pci.h" // pci_bdf_to_bus
16
17
18 /****************************************************************
19  * Helper functions
20  ****************************************************************/
21
22 void
23 __disk_ret(const char *fname, int lineno, struct bregs *regs, u8 code)
24 {
25     SET_BDA(disk_last_status, code);
26     if (code)
27         __set_code_fail(fname, lineno, regs, code);
28     else
29         set_code_success(regs);
30 }
31
32 static void
33 __disk_stub(const char *fname, int lineno, struct bregs *regs)
34 {
35     __debug_stub(fname, lineno, regs);
36     __disk_ret(fname, lineno, regs, DISK_RET_SUCCESS);
37 }
38
39 #define DISK_STUB(regs) \
40     __disk_stub(__func__, __LINE__, (regs))
41
42 static void
43 basic_access(struct bregs *regs, u8 device, u16 command)
44 {
45     u8 type = GET_GLOBAL(ATA.devices[device].type);
46     u16 nlc, nlh, nlspt;
47     if (type == ATA_TYPE_ATA) {
48         nlc   = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
49         nlh   = GET_GLOBAL(ATA.devices[device].lchs.heads);
50         nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
51     } else {
52         // Must be cd emulation.
53         u16 ebda_seg = get_ebda_seg();
54         nlc   = GET_EBDA2(ebda_seg, cdemu.cylinders);
55         nlh   = GET_EBDA2(ebda_seg, cdemu.heads);
56         nlspt = GET_EBDA2(ebda_seg, cdemu.spt);
57     }
58
59     u16 count       = regs->al;
60     u16 cylinder    = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
61     u16 sector      = regs->cl & 0x3f;
62     u16 head        = regs->dh;
63
64     if (count > 128 || count == 0 || sector == 0) {
65         dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
66                 , regs->ah);
67         disk_ret(regs, DISK_RET_EPARAM);
68         return;
69     }
70
71     // sanity check on cyl heads, sec
72     if (cylinder >= nlc || head >= nlh || sector > nlspt) {
73         dprintf(1, "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);
77         return;
78     }
79
80     if (!command) {
81         // If verify or seek
82         disk_ret(regs, DISK_RET_SUCCESS);
83         return;
84     }
85
86     // translate lchs to lba
87     u32 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
88                + (u32)sector - 1);
89
90     u16 segment = regs->es;
91     u16 offset  = regs->bx;
92     void *far_buffer = MAKE_FARPTR(segment, offset);
93
94     irq_enable();
95
96     int status;
97     if (type == ATA_TYPE_ATA)
98         status = ata_cmd_data(device, command, lba, count, far_buffer);
99     else
100         status = cdrom_read_emu(device, lba, count, far_buffer);
101
102     irq_disable();
103
104     // Set nb of sector transferred
105     regs->al = GET_EBDA(sector_count);
106
107     if (status != 0) {
108         dprintf(1, "int13_harddisk: function %02x, error %d!\n"
109                 , regs->ah, status);
110         disk_ret(regs, DISK_RET_EBADTRACK);
111     }
112     disk_ret(regs, DISK_RET_SUCCESS);
113 }
114
115 static void
116 extended_access(struct bregs *regs, u8 device, u16 command)
117 {
118     // Get lba and check.
119     u64 lba = GET_INT13EXT(regs, lba);
120     u8 type = GET_GLOBAL(ATA.devices[device].type);
121     if (type == ATA_TYPE_ATA
122         && lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
123         dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
124                 , regs->ah);
125         disk_ret(regs, DISK_RET_EPARAM);
126         return;
127     }
128
129     if (!command) {
130         // If verify or seek
131         disk_ret(regs, DISK_RET_SUCCESS);
132         return;
133     }
134
135     u16 segment = GET_INT13EXT(regs, segment);
136     u16 offset = GET_INT13EXT(regs, offset);
137     void *far_buffer = MAKE_FARPTR(segment, offset);
138     u16 count = GET_INT13EXT(regs, count);
139
140     dprintf(DEBUG_HDL_13, "extacc lba=%d buf=%p count=%d\n"
141             , (u32)lba, far_buffer, count);
142
143     irq_enable();
144
145     int status;
146     if (type == ATA_TYPE_ATA)
147         status = ata_cmd_data(device, command, lba, count, far_buffer);
148     else
149         status = cdrom_read(device, lba, count, far_buffer);
150
151     irq_disable();
152
153     SET_INT13EXT(regs, count, GET_EBDA(sector_count));
154
155     if (status != 0) {
156         dprintf(1, "int13_harddisk: function %02x, error %d!\n"
157                 , regs->ah, status);
158         disk_ret(regs, DISK_RET_EBADTRACK);
159         return;
160     }
161     disk_ret(regs, DISK_RET_SUCCESS);
162 }
163
164
165 /****************************************************************
166  * Hard Drive functions
167  ****************************************************************/
168
169 // disk controller reset
170 static void
171 disk_1300(struct bregs *regs, u8 device)
172 {
173     ata_reset(device);
174 }
175
176 // read disk status
177 static void
178 disk_1301(struct bregs *regs, u8 device)
179 {
180     u8 v = GET_BDA(disk_last_status);
181     regs->ah = v;
182     set_cf(regs, v);
183     // XXX - clear disk_last_status?
184 }
185
186 // read disk sectors
187 static void
188 disk_1302(struct bregs *regs, u8 device)
189 {
190     basic_access(regs, device, ATA_CMD_READ_SECTORS);
191 }
192
193 // write disk sectors
194 static void
195 disk_1303(struct bregs *regs, u8 device)
196 {
197     basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
198 }
199
200 // verify disk sectors
201 static void
202 disk_1304(struct bregs *regs, u8 device)
203 {
204     basic_access(regs, device, 0);
205     // FIXME verify
206 }
207
208 // format disk track
209 static void
210 disk_1305(struct bregs *regs, u8 device)
211 {
212     DISK_STUB(regs);
213 }
214
215 // read disk drive parameters
216 static void
217 disk_1308(struct bregs *regs, u8 device)
218 {
219     // Get logical geometry from table
220     u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
221     u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
222     u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
223     u16 count = GET_BDA(hdcount);
224
225     nlc = nlc - 2; /* 0 based , last sector not used */
226     regs->al = 0;
227     regs->ch = nlc & 0xff;
228     regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
229     regs->dh = nlh - 1;
230     regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
231
232     // FIXME should set ES & DI
233     disk_ret(regs, DISK_RET_SUCCESS);
234 }
235
236 // initialize drive parameters
237 static void
238 disk_1309(struct bregs *regs, u8 device)
239 {
240     DISK_STUB(regs);
241 }
242
243 // seek to specified cylinder
244 static void
245 disk_130c(struct bregs *regs, u8 device)
246 {
247     DISK_STUB(regs);
248 }
249
250 // alternate disk reset
251 static void
252 disk_130d(struct bregs *regs, u8 device)
253 {
254     DISK_STUB(regs);
255 }
256
257 // check drive ready
258 static void
259 disk_1310(struct bregs *regs, u8 device)
260 {
261     // should look at 40:8E also???
262
263     // Read the status from controller
264     u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
265     if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
266         disk_ret(regs, DISK_RET_SUCCESS);
267     else
268         disk_ret(regs, DISK_RET_ENOTREADY);
269 }
270
271 // recalibrate
272 static void
273 disk_1311(struct bregs *regs, u8 device)
274 {
275     DISK_STUB(regs);
276 }
277
278 // controller internal diagnostic
279 static void
280 disk_1314(struct bregs *regs, u8 device)
281 {
282     DISK_STUB(regs);
283 }
284
285 // read disk drive size
286 static void
287 disk_1315(struct bregs *regs, u8 device)
288 {
289     // Get logical geometry from table
290     u16 nlc   = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
291     u16 nlh   = GET_GLOBAL(ATA.devices[device].lchs.heads);
292     u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
293
294     // Compute sector count seen by int13
295     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
296     regs->cx = lba >> 16;
297     regs->dx = lba & 0xffff;
298
299     disk_ret(regs, DISK_RET_SUCCESS);
300     regs->ah = 3; // hard disk accessible
301 }
302
303 // IBM/MS installation check
304 static void
305 disk_1341(struct bregs *regs, u8 device)
306 {
307     regs->bx = 0xaa55;  // install check
308     regs->cx = 0x0007;  // ext disk access and edd, removable supported
309     disk_ret(regs, DISK_RET_SUCCESS);
310     regs->ah = 0x30;    // EDD 3.0
311 }
312
313 // IBM/MS extended read
314 static void
315 disk_1342(struct bregs *regs, u8 device)
316 {
317     extended_access(regs, device, ATA_CMD_READ_SECTORS);
318 }
319
320 // IBM/MS extended write
321 static void
322 disk_1343(struct bregs *regs, u8 device)
323 {
324     extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
325 }
326
327 // IBM/MS verify
328 static void
329 disk_1344(struct bregs *regs, u8 device)
330 {
331     extended_access(regs, device, 0);
332 }
333
334 // IBM/MS lock/unlock drive
335 static void
336 disk_1345(struct bregs *regs, u8 device)
337 {
338     // Always success for HD
339     disk_ret(regs, DISK_RET_SUCCESS);
340 }
341
342 // IBM/MS eject media
343 static void
344 disk_1346(struct bregs *regs, u8 device)
345 {
346     // Volume Not Removable
347     disk_ret(regs, DISK_RET_ENOTREMOVABLE);
348 }
349
350 // IBM/MS extended seek
351 static void
352 disk_1347(struct bregs *regs, u8 device)
353 {
354     extended_access(regs, device, 0);
355 }
356
357 // IBM/MS get drive parameters
358 static void
359 disk_1348(struct bregs *regs, u8 device)
360 {
361     u16 size = GET_INT13DPT(regs, size);
362
363     // Buffer is too small
364     if (size < 26) {
365         disk_ret(regs, DISK_RET_EPARAM);
366         return;
367     }
368
369     // EDD 1.x
370
371     u8  type    = GET_GLOBAL(ATA.devices[device].type);
372     u16 npc     = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
373     u16 nph     = GET_GLOBAL(ATA.devices[device].pchs.heads);
374     u16 npspt   = GET_GLOBAL(ATA.devices[device].pchs.spt);
375     u64 lba     = GET_GLOBAL(ATA.devices[device].sectors);
376     u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
377
378     dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
379             , size, type, npc, nph, npspt, (u32)lba, blksize);
380
381     SET_INT13DPT(regs, size, 26);
382     if (type == ATA_TYPE_ATA) {
383         if (lba > (u64)npspt*nph*0x3fff) {
384             SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
385             SET_INT13DPT(regs, cylinders, 0x3fff);
386         } else {
387             SET_INT13DPT(regs, infos, 0x02); // geometry is valid
388             SET_INT13DPT(regs, cylinders, (u32)npc);
389         }
390         SET_INT13DPT(regs, heads, (u32)nph);
391         SET_INT13DPT(regs, spt, (u32)npspt);
392         SET_INT13DPT(regs, sector_count, lba);
393     } else {
394         // ATAPI
395         // 0x74 = removable, media change, lockable, max values
396         SET_INT13DPT(regs, infos, 0x74);
397         SET_INT13DPT(regs, cylinders, 0xffffffff);
398         SET_INT13DPT(regs, heads, 0xffffffff);
399         SET_INT13DPT(regs, spt, 0xffffffff);
400         SET_INT13DPT(regs, sector_count, (u64)-1);
401     }
402     SET_INT13DPT(regs, blksize, blksize);
403
404     if (size < 30) {
405         disk_ret(regs, DISK_RET_SUCCESS);
406         return;
407     }
408
409     // EDD 2.x
410
411     u16 ebda_seg = get_ebda_seg();
412     SET_INT13DPT(regs, size, 30);
413
414     SET_INT13DPT(regs, dpte_segment, ebda_seg);
415     SET_INT13DPT(regs, dpte_offset
416                  , offsetof(struct extended_bios_data_area_s, dpte));
417
418     // Fill in dpte
419     u8 channel = device / 2;
420     u8 slave = device % 2;
421     u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
422     u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
423     u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
424     u8 mode = GET_GLOBAL(ATA.devices[device].mode);
425
426     u16 options = 0;
427     if (type == ATA_TYPE_ATA) {
428         u8 translation = GET_GLOBAL(ATA.devices[device].translation);
429         if (translation != ATA_TRANSLATION_NONE) {
430             options |= 1<<3; // CHS translation
431             if (translation == ATA_TRANSLATION_LBA)
432                 options |= 1<<9;
433             if (translation == ATA_TRANSLATION_RECHS)
434                 options |= 3<<9;
435         }
436     } else {
437         // ATAPI
438         options |= 1<<5; // removable device
439         options |= 1<<6; // atapi device
440     }
441     options |= 1<<4; // lba translation
442     if (mode == ATA_MODE_PIO32)
443         options |= 1<<7;
444
445     SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
446     SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
447     SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
448                                       | ATA_CB_DH_LBA));
449     SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
450     SET_EBDA2(ebda_seg, dpte.irq, irq);
451     SET_EBDA2(ebda_seg, dpte.blkcount, 1);
452     SET_EBDA2(ebda_seg, dpte.dma, 0);
453     SET_EBDA2(ebda_seg, dpte.pio, 0);
454     SET_EBDA2(ebda_seg, dpte.options, options);
455     SET_EBDA2(ebda_seg, dpte.reserved, 0);
456     SET_EBDA2(ebda_seg, dpte.revision, 0x11);
457
458     u8 *p = MAKE_FARPTR(ebda_seg
459                         , offsetof(struct extended_bios_data_area_s, dpte));
460     SET_EBDA2(ebda_seg, dpte.checksum, -checksum(p, 15));
461
462     if (size < 66) {
463         disk_ret(regs, DISK_RET_SUCCESS);
464         return;
465     }
466
467     // EDD 3.x
468     SET_INT13DPT(regs, key, 0xbedd);
469     SET_INT13DPT(regs, dpi_length, 36);
470     SET_INT13DPT(regs, reserved1, 0);
471     SET_INT13DPT(regs, reserved2, 0);
472
473     SET_INT13DPT(regs, host_bus[0], 'P');
474     SET_INT13DPT(regs, host_bus[1], 'C');
475     SET_INT13DPT(regs, host_bus[2], 'I');
476     SET_INT13DPT(regs, host_bus[3], 0);
477
478     u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
479     u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
480                 | (pci_bdf_to_fn(bdf) << 16));
481     SET_INT13DPT(regs, iface_path, path);
482
483     SET_INT13DPT(regs, iface_type[0], 'A');
484     SET_INT13DPT(regs, iface_type[1], 'T');
485     SET_INT13DPT(regs, iface_type[2], 'A');
486     SET_INT13DPT(regs, iface_type[3], 0);
487     SET_INT13DPT(regs, iface_type[4], 0);
488     SET_INT13DPT(regs, iface_type[5], 0);
489     SET_INT13DPT(regs, iface_type[6], 0);
490     SET_INT13DPT(regs, iface_type[7], 0);
491
492     SET_INT13DPT(regs, device_path, slave);
493
494     SET_INT13DPT(regs, checksum, -checksum(MAKE_FARPTR(regs->ds, 30), 35));
495
496     disk_ret(regs, DISK_RET_SUCCESS);
497 }
498
499 // IBM/MS extended media change
500 static void
501 disk_1349(struct bregs *regs, u8 device)
502 {
503     // Always success for HD
504     disk_ret(regs, DISK_RET_SUCCESS);
505 }
506
507 static void
508 disk_134e01(struct bregs *regs, u8 device)
509 {
510     disk_ret(regs, DISK_RET_SUCCESS);
511 }
512
513 static void
514 disk_134e03(struct bregs *regs, u8 device)
515 {
516     disk_ret(regs, DISK_RET_SUCCESS);
517 }
518
519 static void
520 disk_134e04(struct bregs *regs, u8 device)
521 {
522     disk_ret(regs, DISK_RET_SUCCESS);
523 }
524
525 static void
526 disk_134e06(struct bregs *regs, u8 device)
527 {
528     disk_ret(regs, DISK_RET_SUCCESS);
529 }
530
531 static void
532 disk_134eXX(struct bregs *regs, u8 device)
533 {
534     disk_ret(regs, DISK_RET_EPARAM);
535 }
536
537 // IBM/MS set hardware configuration
538 static void
539 disk_134e(struct bregs *regs, u8 device)
540 {
541     switch (regs->al) {
542     case 0x01: disk_134e01(regs, device); break;
543     case 0x03: disk_134e03(regs, device); break;
544     case 0x04: disk_134e04(regs, device); break;
545     case 0x06: disk_134e06(regs, device); break;
546     default:   disk_134eXX(regs, device); break;
547     }
548 }
549
550 void
551 disk_13XX(struct bregs *regs, u8 device)
552 {
553     disk_ret(regs, DISK_RET_EPARAM);
554 }
555
556 void
557 disk_13(struct bregs *regs, u8 device)
558 {
559     //debug_stub(regs);
560
561     // clear completion flag
562     SET_BDA(disk_interrupt_flag, 0);
563
564     switch (regs->ah) {
565     case 0x00: disk_1300(regs, device); break;
566     case 0x01: disk_1301(regs, device); break;
567     case 0x02: disk_1302(regs, device); break;
568     case 0x03: disk_1303(regs, device); break;
569     case 0x04: disk_1304(regs, device); break;
570     case 0x05: disk_1305(regs, device); break;
571     case 0x08: disk_1308(regs, device); break;
572     case 0x09: disk_1309(regs, device); break;
573     case 0x0c: disk_130c(regs, device); break;
574     case 0x0d: disk_130d(regs, device); break;
575     case 0x10: disk_1310(regs, device); break;
576     case 0x11: disk_1311(regs, device); break;
577     case 0x14: disk_1314(regs, device); break;
578     case 0x15: disk_1315(regs, device); break;
579     case 0x41: disk_1341(regs, device); break;
580     case 0x42: disk_1342(regs, device); break;
581     case 0x43: disk_1343(regs, device); break;
582     case 0x44: disk_1344(regs, device); break;
583     case 0x45: disk_1345(regs, device); break;
584     case 0x46: disk_1346(regs, device); break;
585     case 0x47: disk_1347(regs, device); break;
586     case 0x48: disk_1348(regs, device); break;
587     case 0x49: disk_1349(regs, device); break;
588     case 0x4e: disk_134e(regs, device); break;
589     default:   disk_13XX(regs, device); break;
590     }
591 }
592
593
594 /****************************************************************
595  * Entry points
596  ****************************************************************/
597
598 static u8
599 get_device(struct bregs *regs, u8 iscd, u8 drive)
600 {
601     // basic check : device has to be defined
602     if (drive >= CONFIG_MAX_ATA_DEVICES) {
603         disk_ret(regs, DISK_RET_EPARAM);
604         return CONFIG_MAX_ATA_DEVICES;
605     }
606
607     // Get the ata channel
608     u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
609
610     // basic check : device has to be valid
611     if (device >= CONFIG_MAX_ATA_DEVICES) {
612         disk_ret(regs, DISK_RET_EPARAM);
613         return CONFIG_MAX_ATA_DEVICES;
614     }
615
616     return device;
617 }
618
619 static void
620 handle_legacy_disk(struct bregs *regs, u8 drive)
621 {
622     if (drive < 0x80) {
623         floppy_13(regs, drive);
624         return;
625     }
626
627     if (! CONFIG_ATA) {
628         // XXX - old code had other disk access method.
629         disk_ret(regs, DISK_RET_EPARAM);
630         return;
631     }
632
633     if (drive >= 0xe0) {
634         u8 device = get_device(regs, 1, drive - 0xe0);
635         if (device >= CONFIG_MAX_ATA_DEVICES)
636             return;
637         cdrom_13(regs, device);
638         return;
639     }
640
641     u8 device = get_device(regs, 0, drive - 0x80);
642     if (device >= CONFIG_MAX_ATA_DEVICES)
643         return;
644     disk_13(regs, device);
645 }
646
647 void VISIBLE16
648 handle_40(struct bregs *regs)
649 {
650     debug_enter(regs, DEBUG_HDL_40);
651     handle_legacy_disk(regs, regs->dl);
652 }
653
654 // INT 13h Fixed Disk Services Entry Point
655 void VISIBLE16
656 handle_13(struct bregs *regs)
657 {
658     debug_enter(regs, DEBUG_HDL_13);
659     u8 drive = regs->dl;
660
661     if (CONFIG_CDROM_EMU) {
662         if (regs->ah == 0x4b) {
663             cdemu_134b(regs);
664             return;
665         }
666         u16 ebda_seg = get_ebda_seg();
667         if (GET_EBDA2(ebda_seg, cdemu.active)) {
668             if (drive == GET_EBDA2(ebda_seg, cdemu.emulated_drive)) {
669                 cdemu_13(regs);
670                 return;
671             }
672             if (drive < 0xe0)
673                 drive--;
674         }
675     }
676     handle_legacy_disk(regs, drive);
677 }
678
679 // record completion in BIOS task complete flag
680 void VISIBLE16
681 handle_76()
682 {
683     debug_isr(DEBUG_ISR_76);
684     SET_BDA(disk_interrupt_flag, 0xff);
685     eoi_pic2();
686 }