Add floppy controllers to "drives" list also.
[seabios.git] / src / cdrom.c
1 // 16bit code to access cdrom drives.
2 //
3 // Copyright (C) 2008,2009  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 LGPLv3 license.
7
8 #include "disk.h" // cdrom_13
9 #include "util.h" // memset
10 #include "bregs.h" // struct bregs
11 #include "biosvar.h" // GET_EBDA
12 #include "ata.h" // ATA_CMD_REQUEST_SENSE
13
14
15 /****************************************************************
16  * CDROM functions
17  ****************************************************************/
18
19 // read disk drive size
20 static void
21 cdrom_1315(struct bregs *regs, u8 driveid)
22 {
23     disk_ret(regs, DISK_RET_EADDRNOTFOUND);
24 }
25
26 // lock
27 static void
28 cdrom_134500(struct bregs *regs, u8 driveid)
29 {
30     u16 ebda_seg = get_ebda_seg();
31     u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
32     if (locks == 0xff) {
33         regs->al = 1;
34         disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
35         return;
36     }
37     SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks + 1);
38     regs->al = 1;
39     disk_ret(regs, DISK_RET_SUCCESS);
40 }
41
42 // unlock
43 static void
44 cdrom_134501(struct bregs *regs, u8 driveid)
45 {
46     u16 ebda_seg = get_ebda_seg();
47     u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
48     if (locks == 0x00) {
49         regs->al = 0;
50         disk_ret(regs, DISK_RET_ENOTLOCKED);
51         return;
52     }
53     locks--;
54     SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks);
55     regs->al = (locks ? 1 : 0);
56     disk_ret(regs, DISK_RET_SUCCESS);
57 }
58
59 // status
60 static void
61 cdrom_134502(struct bregs *regs, u8 driveid)
62 {
63     u8 locks = GET_EBDA(cdrom_locks[driveid]);
64     regs->al = (locks ? 1 : 0);
65     disk_ret(regs, DISK_RET_SUCCESS);
66 }
67
68 static void
69 cdrom_1345XX(struct bregs *regs, u8 driveid)
70 {
71     disk_ret(regs, DISK_RET_EPARAM);
72 }
73
74 // IBM/MS lock/unlock drive
75 static void
76 cdrom_1345(struct bregs *regs, u8 driveid)
77 {
78     switch (regs->al) {
79     case 0x00: cdrom_134500(regs, driveid); break;
80     case 0x01: cdrom_134501(regs, driveid); break;
81     case 0x02: cdrom_134502(regs, driveid); break;
82     default:   cdrom_1345XX(regs, driveid); break;
83     }
84 }
85
86 // IBM/MS eject media
87 static void
88 cdrom_1346(struct bregs *regs, u8 driveid)
89 {
90     u8 locks = GET_EBDA(cdrom_locks[driveid]);
91     if (locks != 0) {
92         disk_ret(regs, DISK_RET_ELOCKED);
93         return;
94     }
95
96     // FIXME should handle 0x31 no media in device
97     // FIXME should handle 0xb5 valid request failed
98
99     // Call removable media eject
100     struct bregs br;
101     memset(&br, 0, sizeof(br));
102     br.ah = 0x52;
103     call16_int(0x15, &br);
104
105     if (br.ah || br.flags & F_CF) {
106         disk_ret(regs, DISK_RET_ELOCKED);
107         return;
108     }
109     disk_ret(regs, DISK_RET_SUCCESS);
110 }
111
112 // IBM/MS extended media change
113 static void
114 cdrom_1349(struct bregs *regs, u8 driveid)
115 {
116     set_fail(regs);
117     // always send changed ??
118     regs->ah = DISK_RET_ECHANGED;
119 }
120
121 static void
122 cdrom_ok(struct bregs *regs, u8 driveid)
123 {
124     disk_ret(regs, DISK_RET_SUCCESS);
125 }
126
127 static void
128 cdrom_wp(struct bregs *regs, u8 driveid)
129 {
130     disk_ret(regs, DISK_RET_EWRITEPROTECT);
131 }
132
133 void
134 cdrom_13(struct bregs *regs, u8 driveid)
135 {
136     //debug_stub(regs);
137
138     switch (regs->ah) {
139     case 0x15: cdrom_1315(regs, driveid); break;
140     case 0x45: cdrom_1345(regs, driveid); break;
141     case 0x46: cdrom_1346(regs, driveid); break;
142     case 0x49: cdrom_1349(regs, driveid); break;
143
144     // These functions are the same as for hard disks
145     case 0x01:
146     case 0x41:
147     case 0x42:
148     case 0x44:
149     case 0x47:
150     case 0x48:
151     case 0x4e:
152         disk_13(regs, driveid);
153         break;
154
155     // all these functions return SUCCESS
156     case 0x00: // disk controller reset
157     case 0x09: // initialize drive parameters
158     case 0x0c: // seek to specified cylinder
159     case 0x0d: // alternate disk reset
160     case 0x10: // check drive ready
161     case 0x11: // recalibrate
162     case 0x14: // controller internal diagnostic
163     case 0x16: // detect disk change
164         cdrom_ok(regs, driveid);
165         break;
166
167     // all these functions return disk write-protected
168     case 0x03: // write disk sectors
169     case 0x05: // format disk track
170     case 0x43: // IBM/MS extended write
171         cdrom_wp(regs, driveid);
172         break;
173
174     default:   disk_13XX(regs, driveid); break;
175     }
176 }
177
178
179 /****************************************************************
180  * CD emulation
181  ****************************************************************/
182
183 static void
184 cdemu_1302(struct bregs *regs, u8 driveid)
185 {
186     cdemu_access(regs, driveid, CMD_READ);
187 }
188
189 static void
190 cdemu_1304(struct bregs *regs, u8 driveid)
191 {
192     cdemu_access(regs, driveid, CMD_VERIFY);
193 }
194
195 // read disk drive parameters
196 static void
197 cdemu_1308(struct bregs *regs, u8 driveid)
198 {
199     u16 ebda_seg = get_ebda_seg();
200     u16 nlc   = GET_EBDA2(ebda_seg, cdemu.lchs.cylinders) - 1;
201     u16 nlh   = GET_EBDA2(ebda_seg, cdemu.lchs.heads) - 1;
202     u16 nlspt = GET_EBDA2(ebda_seg, cdemu.lchs.spt);
203
204     regs->al = 0x00;
205     regs->bl = 0x00;
206     regs->ch = nlc & 0xff;
207     regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
208     regs->dh = nlh;
209     // FIXME ElTorito Various. should send the real count of drives 1 or 2
210     // FIXME ElTorito Harddisk. should send the HD count
211     regs->dl = 0x02;
212     u8 media = GET_EBDA2(ebda_seg, cdemu.media);
213     if (media <= 3)
214         regs->bl = media * 2;
215
216     regs->es = SEG_BIOS;
217     regs->di = (u32)&diskette_param_table2;
218
219     disk_ret(regs, DISK_RET_SUCCESS);
220 }
221
222 void
223 cdemu_13(struct bregs *regs)
224 {
225     //debug_stub(regs);
226
227     u16 ebda_seg = get_ebda_seg();
228     u8 driveid = GET_EBDA2(ebda_seg, cdemu.emulated_driveid);
229
230     switch (regs->ah) {
231     case 0x02: cdemu_1302(regs, driveid); break;
232     case 0x04: cdemu_1304(regs, driveid); break;
233     case 0x08: cdemu_1308(regs, driveid); break;
234
235     // These functions are the same as standard CDROM.
236     case 0x00:
237     case 0x01:
238     case 0x03:
239     case 0x05:
240     case 0x09:
241     case 0x0c:
242     case 0x0d:
243     case 0x10:
244     case 0x11:
245     case 0x14:
246     case 0x15:
247     case 0x16:
248         cdrom_13(regs, driveid);
249         break;
250
251     default:   disk_13XX(regs, driveid); break;
252     }
253 }
254
255 struct eltorito_s {
256     u8 size;
257     u8 media;
258     u8 emulated_drive;
259     u8 controller_index;
260     u32 ilba;
261     u16 device_spec;
262     u16 buffer_segment;
263     u16 load_segment;
264     u16 sector_count;
265     u8 cylinders;
266     u8 sectors;
267     u8 heads;
268 };
269
270 #define SET_INT13ET(regs,var,val)                                      \
271     SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
272
273 // ElTorito - Terminate disk emu
274 void
275 cdemu_134b(struct bregs *regs)
276 {
277     // FIXME ElTorito Hardcoded
278     u16 ebda_seg = get_ebda_seg();
279     SET_INT13ET(regs, size, 0x13);
280     SET_INT13ET(regs, media, GET_EBDA2(ebda_seg, cdemu.media));
281     SET_INT13ET(regs, emulated_drive
282                 , GET_EBDA2(ebda_seg, cdemu.emulated_extdrive));
283     u8 driveid = GET_EBDA2(ebda_seg, cdemu.emulated_driveid);
284     u8 cntl_id = GET_GLOBAL(Drives.drives[driveid].cntl_id);
285     SET_INT13ET(regs, controller_index, cntl_id / 2);
286     SET_INT13ET(regs, device_spec, cntl_id % 2);
287     SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
288     SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
289     SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
290     SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
291     SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
292     SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
293     SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
294
295     // If we have to terminate emulation
296     if (regs->al == 0x00) {
297         // FIXME ElTorito Various. Should be handled accordingly to spec
298         SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
299     }
300
301     disk_ret(regs, DISK_RET_SUCCESS);
302 }
303
304
305 /****************************************************************
306  * CD booting
307  ****************************************************************/
308
309 // Request SENSE
310 static int
311 atapi_get_sense(int driveid, u8 *asc, u8 *ascq)
312 {
313     u8 atacmd[12], buffer[18];
314     memset(atacmd, 0, sizeof(atacmd));
315     atacmd[0] = ATA_CMD_REQUEST_SENSE;
316     atacmd[4] = sizeof(buffer);
317     int ret = ata_cmd_packet(driveid, atacmd, sizeof(atacmd), sizeof(buffer)
318                              , MAKE_FLATPTR(GET_SEG(SS), buffer));
319     if (ret)
320         return ret;
321
322     *asc = buffer[12];
323     *ascq = buffer[13];
324
325     return 0;
326 }
327
328 // Request capacity
329 static int
330 atapi_read_capacity(int driveid, u32 *blksize, u32 *sectors)
331 {
332     u8 packet[12], buf[8];
333     memset(packet, 0, sizeof(packet));
334     packet[0] = 0x25; /* READ CAPACITY */
335     int ret = ata_cmd_packet(driveid, packet, sizeof(packet), sizeof(buf)
336                              , MAKE_FLATPTR(GET_SEG(SS), buf));
337     if (ret)
338         return ret;
339
340     *blksize = (((u32)buf[4] << 24) | ((u32)buf[5] << 16)
341                 | ((u32)buf[6] << 8) | ((u32)buf[7] << 0));
342     *sectors = (((u32)buf[0] << 24) | ((u32)buf[1] << 16)
343                 | ((u32)buf[2] << 8) | ((u32)buf[3] << 0));
344
345     return 0;
346 }
347
348 static int
349 atapi_is_ready(u16 driveid)
350 {
351     dprintf(6, "atapi_is_ready (driveid=%d)\n", driveid);
352
353     /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT is
354      * reported by the device.  If the device reports "IN PROGRESS",
355      * 30 seconds is added. */
356     u32 blksize, sectors;
357     int in_progress = 0;
358     u64 end = calc_future_tsc(5000);
359     for (;;) {
360         if (rdtscll() > end) {
361             dprintf(1, "read capacity failed\n");
362             return -1;
363         }
364
365         int ret = atapi_read_capacity(driveid, &blksize, &sectors);
366         if (!ret)
367             // Success
368             break;
369
370         u8 asc, ascq;
371         ret = atapi_get_sense(driveid, &asc, &ascq);
372         if (ret)
373             // Error - retry.
374             continue;
375
376         // Sense succeeded.
377         if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
378             dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
379             return -1;
380         }
381
382         if (asc == 0x04 && ascq == 0x01 && !in_progress) {
383             /* IN PROGRESS OF BECOMING READY */
384             printf("Waiting for device to detect medium... ");
385             /* Allow 30 seconds more */
386             end = calc_future_tsc(30000);
387             in_progress = 1;
388         }
389     }
390
391     if (blksize != GET_GLOBAL(Drives.drives[driveid].blksize)) {
392         printf("Unsupported sector size %u\n", blksize);
393         return -1;
394     }
395
396     dprintf(6, "sectors=%u\n", sectors);
397     printf("%dMB medium detected\n", sectors>>(20-11));
398     return 0;
399 }
400
401 int
402 cdrom_boot(int cdid)
403 {
404     // Verify device is a cdrom.
405     if (cdid >= Drives.cdcount)
406         return 1;
407     int driveid = GET_GLOBAL(Drives.idmap[EXTTYPE_CD][cdid]);
408
409     int ret = atapi_is_ready(driveid);
410     if (ret)
411         dprintf(1, "atapi_is_ready returned %d\n", ret);
412
413     // Read the Boot Record Volume Descriptor
414     u8 buffer[2048];
415     struct disk_op_s dop;
416     memset(&dop, 0, sizeof(dop));
417     dop.driveid = driveid;
418     dop.lba = 0x11;
419     dop.count = 1;
420     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
421     ret = cdrom_read(&dop);
422     if (ret)
423         return 3;
424
425     // Validity checks
426     if (buffer[0])
427         return 4;
428     if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
429         return 5;
430
431     // ok, now we calculate the Boot catalog address
432     u32 lba = *(u32*)&buffer[0x47];
433
434     // And we read the Boot Catalog
435     dop.lba = lba;
436     ret = cdrom_read(&dop);
437     if (ret)
438         return 7;
439
440     // Validation entry
441     if (buffer[0x00] != 0x01)
442         return 8;   // Header
443     if (buffer[0x01] != 0x00)
444         return 9;   // Platform
445     if (buffer[0x1E] != 0x55)
446         return 10;  // key 1
447     if (buffer[0x1F] != 0xAA)
448         return 10;  // key 2
449
450     // Initial/Default Entry
451     if (buffer[0x20] != 0x88)
452         return 11; // Bootable
453
454     u16 ebda_seg = get_ebda_seg();
455     u8 media = buffer[0x21];
456     SET_EBDA2(ebda_seg, cdemu.media, media);
457
458     SET_EBDA2(ebda_seg, cdemu.emulated_driveid, driveid);
459
460     u16 boot_segment = *(u16*)&buffer[0x22];
461     if (!boot_segment)
462         boot_segment = 0x07C0;
463     SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
464     SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
465
466     u16 nbsectors = *(u16*)&buffer[0x26];
467     SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
468
469     lba = *(u32*)&buffer[0x28];
470     SET_EBDA2(ebda_seg, cdemu.ilba, lba);
471
472     // And we read the image in memory
473     dop.lba = lba;
474     dop.count = DIV_ROUND_UP(nbsectors, 4);
475     dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
476     ret = cdrom_read(&dop);
477     if (ret)
478         return 12;
479
480     if (media == 0) {
481         // No emulation requested - return success.
482         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0xE0 + cdid);
483         return 0;
484     }
485
486     // Emulation of a floppy/harddisk requested
487     if (! CONFIG_CDROM_EMU)
488         return 13;
489
490     // Set emulated drive id and increase bios installed hardware
491     // number of devices
492     if (media < 4) {
493         // Floppy emulation
494         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
495         SETBITS_BDA(equipment_list_flags, 0x41);
496
497         switch (media) {
498         case 0x01:  // 1.2M floppy
499             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
500             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
501             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
502             break;
503         case 0x02:  // 1.44M floppy
504             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
505             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
506             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
507             break;
508         case 0x03:  // 2.88M floppy
509             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
510             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
511             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
512             break;
513         }
514     } else {
515         // Harddrive emulation
516         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
517         SET_BDA(hdcount, GET_BDA(hdcount) + 1);
518
519         // Peak at partition table to get chs.
520         struct mbr_s *mbr = (void*)0;
521         u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
522         u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
523         u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
524
525         SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
526         SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
527                   , ((sptcyl<<2)&0x300) + cyllow + 1);
528         SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
529     }
530
531     // everything is ok, so from now on, the emulation is active
532     SET_EBDA2(ebda_seg, cdemu.active, 0x01);
533     dprintf(6, "cdemu media=%d\n", media);
534
535     return 0;
536 }