1 // 16bit code to access cdrom drives.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
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
15 /****************************************************************
17 ****************************************************************/
19 // read disk drive size
21 cdrom_1315(struct bregs *regs, u8 device)
23 disk_ret(regs, DISK_RET_EADDRNOTFOUND);
28 cdrom_134500(struct bregs *regs, u8 device)
30 u8 locks = GET_EBDA(ata.devices[device].lock);
33 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
36 SET_EBDA(ata.devices[device].lock, locks + 1);
38 disk_ret(regs, DISK_RET_SUCCESS);
43 cdrom_134501(struct bregs *regs, u8 device)
45 u8 locks = GET_EBDA(ata.devices[device].lock);
48 disk_ret(regs, DISK_RET_ENOTLOCKED);
52 SET_EBDA(ata.devices[device].lock, locks);
53 regs->al = (locks ? 1 : 0);
54 disk_ret(regs, DISK_RET_SUCCESS);
59 cdrom_134502(struct bregs *regs, u8 device)
61 u8 locks = GET_EBDA(ata.devices[device].lock);
62 regs->al = (locks ? 1 : 0);
63 disk_ret(regs, DISK_RET_SUCCESS);
67 cdrom_1345XX(struct bregs *regs, u8 device)
69 disk_ret(regs, DISK_RET_EPARAM);
72 // IBM/MS lock/unlock drive
74 cdrom_1345(struct bregs *regs, u8 device)
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;
86 cdrom_1346(struct bregs *regs, u8 device)
88 u8 locks = GET_EBDA(ata.devices[device].lock);
90 disk_ret(regs, DISK_RET_ELOCKED);
94 // FIXME should handle 0x31 no media in device
95 // FIXME should handle 0xb5 valid request failed
97 // Call removable media eject
99 memset(&br, 0, sizeof(br));
101 call16_int(0x15, &br);
103 if (br.ah || br.flags & F_CF) {
104 disk_ret(regs, DISK_RET_ELOCKED);
107 disk_ret(regs, DISK_RET_SUCCESS);
110 // IBM/MS extended media change
112 cdrom_1349(struct bregs *regs, u8 device)
115 // always send changed ??
116 regs->ah = DISK_RET_ECHANGED;
120 cdrom_ok(struct bregs *regs, u8 device)
122 disk_ret(regs, DISK_RET_SUCCESS);
126 cdrom_wp(struct bregs *regs, u8 device)
128 disk_ret(regs, DISK_RET_EWRITEPROTECT);
132 cdrom_13(struct bregs *regs, u8 device)
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;
142 // These functions are the same as for hard disks
150 disk_13(regs, device);
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);
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);
172 default: disk_13XX(regs, device); break;
177 /****************************************************************
179 ****************************************************************/
181 // Read a series of 512 byte sectors from the cdrom starting at the
184 cdrom_read_emu(u16 biosid, u32 vlba, u32 count, void *far_buffer)
186 u32 ilba = GET_EBDA(cdemu.ilba);
187 return cdrom_read_512(biosid, ilba * 4 + vlba, count, far_buffer);
190 // read disk drive parameters
192 cdemu_1308(struct bregs *regs, u8 device)
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);
200 regs->ch = nlc & 0xff;
201 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
203 // FIXME ElTorito Various. should send the real count of drives 1 or 2
204 // FIXME ElTorito Harddisk. should send the HD count
206 u8 media = GET_EBDA(cdemu.media);
208 regs->bl = media * 2;
211 regs->di = (u32)&diskette_param_table2;
213 disk_ret(regs, DISK_RET_SUCCESS);
217 cdemu_13(struct bregs *regs)
221 u8 device = GET_EBDA(cdemu.controller_index) * 2;
222 device += GET_EBDA(cdemu.device_spec);
225 // These functions are the same as for hard disks
228 disk_13(regs, device);
231 // These functions are the same as standard CDROM.
244 cdrom_13(regs, device);
247 case 0x08: cdemu_1308(regs, device); break;
249 default: disk_13XX(regs, device); break;
268 #define SET_INT13ET(regs,var,val) \
269 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
271 // ElTorito - Terminate disk emu
273 cdemu_134b(struct bregs *regs)
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));
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
295 disk_ret(regs, DISK_RET_SUCCESS);
299 /****************************************************************
301 ****************************************************************/
305 atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
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));
324 atapi_is_ready(u16 device)
326 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
327 printf("not implemented for non-ATAPI device\n");
331 dprintf(6, "ata_detect_medium: begin\n");
333 memset(packet, 0, sizeof(packet));
334 packet[0] = 0x25; /* READ CAPACITY */
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. */
344 if (time >= timeout) {
345 dprintf(1, "read capacity failed\n");
348 int ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
349 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
354 ret = atapi_get_sense(device, &asc, &ascq);
358 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
359 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
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 */
372 u32 block_len = (u32) buf[4] << 24
377 if (block_len != 2048 && block_len != 512) {
378 printf("Unsupported sector size %u\n", block_len);
381 SET_EBDA(ata.devices[device].blksize, block_len);
383 u32 sectors = (u32) buf[0] << 24
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);
398 atapi_is_cdrom(u8 device)
400 if (device >= CONFIG_MAX_ATA_DEVICES)
403 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI)
406 if (GET_EBDA(ata.devices[device].device) != ATA_DEVICE_CDROM)
412 // Compare a string on the stack to one in the code segment.
414 streq_cs(u8 *s1, char *cs_s2)
418 if (*s1 != GET_VAR(CS, *s2))
430 // Find out the first cdrom
432 for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
433 if (atapi_is_cdrom(device))
436 int ret = atapi_is_ready(device);
438 dprintf(1, "atapi_is_ready returned %d\n", ret);
440 // Read the Boot Record Volume Descriptor
442 ret = cdrom_read(device, 0x11, 1
443 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
450 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
453 // ok, now we calculate the Boot catalog address
454 u32 lba = *(u32*)&buffer[0x47];
456 // And we read the Boot Catalog
457 ret = cdrom_read(device, lba, 1
458 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
463 if (buffer[0x00] != 0x01)
465 if (buffer[0x01] != 0x00)
466 return 9; // Platform
467 if (buffer[0x1E] != 0x55)
469 if (buffer[0x1F] != 0xAA)
472 // Initial/Default Entry
473 if (buffer[0x20] != 0x88)
474 return 11; // Bootable
476 u8 media = buffer[0x21];
477 SET_EBDA(cdemu.media, media);
479 SET_EBDA(cdemu.controller_index, device/2);
480 SET_EBDA(cdemu.device_spec, device%2);
482 u16 boot_segment = *(u16*)&buffer[0x22];
484 boot_segment = 0x07C0;
485 SET_EBDA(cdemu.load_segment, boot_segment);
486 SET_EBDA(cdemu.buffer_segment, 0x0000);
488 u16 nbsectors = *(u16*)&buffer[0x26];
489 SET_EBDA(cdemu.sector_count, nbsectors);
491 lba = *(u32*)&buffer[0x28];
492 SET_EBDA(cdemu.ilba, lba);
494 // And we read the image in memory
495 ret = cdrom_read_emu(device, 0, nbsectors, MAKE_FARPTR(boot_segment, 0));
500 // No emulation requested - return success.
502 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
503 // Win2000 cd boot needs to know it booted from cd
504 SET_EBDA(cdemu.emulated_drive, 0xE0);
509 // Emulation of a floppy/harddisk requested
510 if (! CONFIG_CDROM_EMU)
513 // Set emulated drive id and increase bios installed hardware
517 SET_EBDA(cdemu.emulated_drive, 0x00);
518 SETBITS_BDA(equipment_list_flags, 0x41);
520 // Harddrive emulation
521 SET_EBDA(cdemu.emulated_drive, 0x80);
522 SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
525 // Remember the media type
527 case 0x01: // 1.2M floppy
528 SET_EBDA(cdemu.vdevice.spt, 15);
529 SET_EBDA(cdemu.vdevice.cylinders, 80);
530 SET_EBDA(cdemu.vdevice.heads, 2);
532 case 0x02: // 1.44M floppy
533 SET_EBDA(cdemu.vdevice.spt, 18);
534 SET_EBDA(cdemu.vdevice.cylinders, 80);
535 SET_EBDA(cdemu.vdevice.heads, 2);
537 case 0x03: // 2.88M floppy
538 SET_EBDA(cdemu.vdevice.spt, 36);
539 SET_EBDA(cdemu.vdevice.cylinders, 80);
540 SET_EBDA(cdemu.vdevice.heads, 2);
542 case 0x04: { // Harddrive
543 u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
544 u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
545 u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
546 SET_EBDA(cdemu.vdevice.spt, spt & 0x3f);
547 SET_EBDA(cdemu.vdevice.cylinders, cyl);
548 SET_EBDA(cdemu.vdevice.heads, heads);
553 // everything is ok, so from now on, the emulation is active
554 SET_EBDA(cdemu.active, 0x01);
555 dprintf(6, "cdemu media=%d\n", media);