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