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