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