Get CDROM emulation working.
[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     // always send changed ??
116     regs->ah = DISK_RET_ECHANGED;
117     set_cf(regs, 1);
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                              , 18L, 0, ATA_DATA_IN, GET_SEG(SS), (u32)buffer);
300     if (ret != 0)
301         return 0x0002;
302
303     *asc = buffer[12];
304     *ascq = buffer[13];
305
306     return 0;
307 }
308
309 static u16
310 atapi_is_ready(u16 device)
311 {
312     if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
313         printf("not implemented for non-ATAPI device\n");
314         return -1;
315     }
316
317     DEBUGF("ata_detect_medium: begin\n");
318     u8 packet[12];
319     memset(packet, 0, sizeof(packet));
320     packet[0] = 0x25; /* READ CAPACITY */
321
322     /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
323      * is reported by the device. If the device reports "IN PROGRESS",
324      * 30 seconds is added. */
325     u8 buf[8];
326     u32 timeout = 5000;
327     u32 time = 0;
328     u8 in_progress = 0;
329     for (;; time+=100) {
330         if (time >= timeout) {
331             DEBUGF("read capacity failed\n");
332             return -1;
333         }
334         u16 ret = ata_cmd_packet(device, packet, sizeof(packet)
335                                  , 0, 8L, ATA_DATA_IN, GET_SEG(SS), (u32)buf);
336         if (ret == 0)
337             break;
338
339         u8 asc=0, ascq=0;
340         ret = atapi_get_sense(device, &asc, &ascq);
341         if (!ret)
342             continue;
343
344         if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
345             DEBUGF("Device reports MEDIUM NOT PRESENT\n");
346             return -1;
347         }
348
349         if (asc == 0x04 && ascq == 0x01 && !in_progress) {
350             /* IN PROGRESS OF BECOMING READY */
351             printf("Waiting for device to detect medium... ");
352             /* Allow 30 seconds more */
353             timeout = 30000;
354             in_progress = 1;
355         }
356     }
357
358     u32 block_len = (u32) buf[4] << 24
359         | (u32) buf[5] << 16
360         | (u32) buf[6] << 8
361         | (u32) buf[7] << 0;
362
363     if (block_len != 2048 && block_len != 512) {
364         printf("Unsupported sector size %u\n", block_len);
365         return -1;
366     }
367     SET_EBDA(ata.devices[device].blksize, block_len);
368
369     u32 sectors = (u32) buf[0] << 24
370         | (u32) buf[1] << 16
371         | (u32) buf[2] << 8
372         | (u32) buf[3] << 0;
373
374     DEBUGF("sectors=%u\n", sectors);
375     if (block_len == 2048)
376         sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
377     if (sectors != GET_EBDA(ata.devices[device].sectors))
378         printf("%dMB medium detected\n", sectors>>(20-9));
379     SET_EBDA(ata.devices[device].sectors, sectors);
380     return 0;
381 }
382
383 static u16
384 atapi_is_cdrom(u8 device)
385 {
386     if (device >= CONFIG_MAX_ATA_DEVICES)
387         return 0;
388
389     if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI)
390         return 0;
391
392     if (GET_EBDA(ata.devices[device].device) != ATA_DEVICE_CDROM)
393         return 0;
394
395     return 1;
396 }
397
398 // Compare a string on the stack to one in the code segment.
399 static int
400 streq_cs(u8 *s1, char *cs_s2)
401 {
402     u8 *s2 = (u8*)cs_s2;
403     for (;;) {
404         if (*s1 != GET_VAR(CS, *s2))
405             return 0;
406         if (! *s1)
407             return 1;
408         s1++;
409         s2++;
410     }
411 }
412
413 u16
414 cdrom_boot()
415 {
416     // Find out the first cdrom
417     u8 device;
418     for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
419         if (atapi_is_cdrom(device))
420             break;
421
422     u16 ret = atapi_is_ready(device);
423     if (ret)
424         BX_INFO("ata_is_ready returned %d\n", ret);
425
426     // if not found
427     if (device >= CONFIG_MAX_ATA_DEVICES)
428         return 2;
429
430     // Read the Boot Record Volume Descriptor
431     u8 buffer[2048];
432     ret = cdrom_read(device, 0x11, 2048, GET_SEG(SS), (u32)buffer, 0);
433     if (ret)
434         return 3;
435
436     // Validity checks
437     if (buffer[0])
438         return 4;
439     if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
440         return 5;
441
442     // ok, now we calculate the Boot catalog address
443     u32 lba = *(u32*)&buffer[0x47];
444
445     // And we read the Boot Catalog
446     ret = cdrom_read(device, lba, 2048, GET_SEG(SS), (u32)buffer, 0);
447     if (ret)
448         return 7;
449
450     // Validation entry
451     if (buffer[0x00] != 0x01)
452         return 8;   // Header
453     if (buffer[0x01] != 0x00)
454         return 9;   // Platform
455     if (buffer[0x1E] != 0x55)
456         return 10;  // key 1
457     if (buffer[0x1F] != 0xAA)
458         return 10;  // key 2
459
460     // Initial/Default Entry
461     if (buffer[0x20] != 0x88)
462         return 11; // Bootable
463
464     SET_EBDA(cdemu.media,buffer[0x21]);
465     if (buffer[0x21] == 0)
466         // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
467         // Win2000 cd boot needs to know it booted from cd
468         SET_EBDA(cdemu.emulated_drive, 0xE0);
469     else if (buffer[0x21] < 4)
470         SET_EBDA(cdemu.emulated_drive, 0x00);
471     else
472         SET_EBDA(cdemu.emulated_drive, 0x80);
473
474     SET_EBDA(cdemu.controller_index, device/2);
475     SET_EBDA(cdemu.device_spec, device%2);
476
477     u16 boot_segment = *(u16*)&buffer[0x22];
478     if (!boot_segment)
479         boot_segment = 0x07C0;
480
481     SET_EBDA(cdemu.load_segment,boot_segment);
482     SET_EBDA(cdemu.buffer_segment,0x0000);
483
484     u16 nbsectors = *(u16*)&buffer[0x26];
485     SET_EBDA(cdemu.sector_count, nbsectors);
486
487     lba = *(u32*)&buffer[0x28];
488     SET_EBDA(cdemu.ilba, lba);
489
490     // And we read the image in memory
491     ret = cdrom_read(device, lba, nbsectors*512
492                      , boot_segment, 0, 0);
493     if (ret)
494         return 12;
495
496     // Remember the media type
497     switch (GET_EBDA(cdemu.media)) {
498     case 0x01:  // 1.2M floppy
499         SET_EBDA(cdemu.vdevice.spt, 15);
500         SET_EBDA(cdemu.vdevice.cylinders, 80);
501         SET_EBDA(cdemu.vdevice.heads, 2);
502         break;
503     case 0x02:  // 1.44M floppy
504         SET_EBDA(cdemu.vdevice.spt, 18);
505         SET_EBDA(cdemu.vdevice.cylinders, 80);
506         SET_EBDA(cdemu.vdevice.heads, 2);
507         break;
508     case 0x03:  // 2.88M floppy
509         SET_EBDA(cdemu.vdevice.spt, 36);
510         SET_EBDA(cdemu.vdevice.cylinders, 80);
511         SET_EBDA(cdemu.vdevice.heads, 2);
512         break;
513     case 0x04: { // Harddrive
514         u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
515         u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
516         u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
517         SET_EBDA(cdemu.vdevice.spt, spt & 0x3f);
518         SET_EBDA(cdemu.vdevice.cylinders, cyl);
519         SET_EBDA(cdemu.vdevice.heads, heads);
520         break;
521     }
522     }
523
524     if (GET_EBDA(cdemu.media) != 0) {
525         // Increase bios installed hardware number of devices
526         if (GET_EBDA(cdemu.emulated_drive) == 0x00)
527             SETBITS_BDA(equipment_list_flags, 0x41);
528         else
529             SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
530     }
531
532     // everything is ok, so from now on, the emulation is active
533     if (GET_EBDA(cdemu.media))
534         SET_EBDA(cdemu.active, 0x01);
535
536     return 0;
537 }