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
14 struct cdemu_s CDEMU VAR16_32;
17 /****************************************************************
19 ****************************************************************/
21 // read disk drive size
23 cdrom_1315(struct bregs *regs, u8 device)
25 disk_ret(regs, DISK_RET_EADDRNOTFOUND);
30 cdrom_134500(struct bregs *regs, u8 device)
32 u8 locks = GET_EBDA(cdrom_locks[device]);
35 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
38 SET_EBDA(cdrom_locks[device], locks + 1);
40 disk_ret(regs, DISK_RET_SUCCESS);
45 cdrom_134501(struct bregs *regs, u8 device)
47 u8 locks = GET_EBDA(cdrom_locks[device]);
50 disk_ret(regs, DISK_RET_ENOTLOCKED);
54 SET_EBDA(cdrom_locks[device], locks);
55 regs->al = (locks ? 1 : 0);
56 disk_ret(regs, DISK_RET_SUCCESS);
61 cdrom_134502(struct bregs *regs, u8 device)
63 u8 locks = GET_EBDA(cdrom_locks[device]);
64 regs->al = (locks ? 1 : 0);
65 disk_ret(regs, DISK_RET_SUCCESS);
69 cdrom_1345XX(struct bregs *regs, u8 device)
71 disk_ret(regs, DISK_RET_EPARAM);
74 // IBM/MS lock/unlock drive
76 cdrom_1345(struct bregs *regs, u8 device)
79 case 0x00: cdrom_134500(regs, device); break;
80 case 0x01: cdrom_134501(regs, device); break;
81 case 0x02: cdrom_134502(regs, device); break;
82 default: cdrom_1345XX(regs, device); break;
88 cdrom_1346(struct bregs *regs, u8 device)
90 u8 locks = GET_EBDA(cdrom_locks[device]);
92 disk_ret(regs, DISK_RET_ELOCKED);
96 // FIXME should handle 0x31 no media in device
97 // FIXME should handle 0xb5 valid request failed
99 // Call removable media eject
101 memset(&br, 0, sizeof(br));
103 call16_int(0x15, &br);
105 if (br.ah || br.flags & F_CF) {
106 disk_ret(regs, DISK_RET_ELOCKED);
109 disk_ret(regs, DISK_RET_SUCCESS);
112 // IBM/MS extended media change
114 cdrom_1349(struct bregs *regs, u8 device)
117 // always send changed ??
118 regs->ah = DISK_RET_ECHANGED;
122 cdrom_ok(struct bregs *regs, u8 device)
124 disk_ret(regs, DISK_RET_SUCCESS);
128 cdrom_wp(struct bregs *regs, u8 device)
130 disk_ret(regs, DISK_RET_EWRITEPROTECT);
134 cdrom_13(struct bregs *regs, u8 device)
139 case 0x15: cdrom_1315(regs, device); break;
140 case 0x45: cdrom_1345(regs, device); break;
141 case 0x46: cdrom_1346(regs, device); break;
142 case 0x49: cdrom_1349(regs, device); break;
144 // These functions are the same as for hard disks
152 disk_13(regs, device);
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, device);
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, device);
174 default: disk_13XX(regs, device); break;
179 /****************************************************************
181 ****************************************************************/
183 // Read a series of 512 byte sectors from the cdrom starting at the
186 cdrom_read_emu(u16 biosid, u32 vlba, u32 count, void *far_buffer)
188 u32 ilba = GET_GLOBAL(CDEMU.ilba);
189 return cdrom_read_512(biosid, ilba * 4 + vlba, count, far_buffer);
192 // read disk drive parameters
194 cdemu_1308(struct bregs *regs, u8 device)
196 u16 nlc = GET_GLOBAL(CDEMU.vdevice.cylinders) - 1;
197 u16 nlh = GET_GLOBAL(CDEMU.vdevice.heads) - 1;
198 u16 nlspt = GET_GLOBAL(CDEMU.vdevice.spt);
202 regs->ch = nlc & 0xff;
203 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
205 // FIXME ElTorito Various. should send the real count of drives 1 or 2
206 // FIXME ElTorito Harddisk. should send the HD count
208 u8 media = GET_GLOBAL(CDEMU.media);
210 regs->bl = media * 2;
213 regs->di = (u32)&diskette_param_table2;
215 disk_ret(regs, DISK_RET_SUCCESS);
219 cdemu_13(struct bregs *regs)
223 u8 device = GET_GLOBAL(CDEMU.controller_index) * 2;
224 device += GET_GLOBAL(CDEMU.device_spec);
227 // These functions are the same as for hard disks
230 disk_13(regs, device);
233 // These functions are the same as standard CDROM.
246 cdrom_13(regs, device);
249 case 0x08: cdemu_1308(regs, device); break;
251 default: disk_13XX(regs, device); break;
270 #define SET_INT13ET(regs,var,val) \
271 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
273 // ElTorito - Terminate disk emu
275 cdemu_134b(struct bregs *regs)
277 // FIXME ElTorito Hardcoded
278 SET_INT13ET(regs, size, 0x13);
279 SET_INT13ET(regs, media, GET_GLOBAL(CDEMU.media));
280 SET_INT13ET(regs, emulated_drive, GET_GLOBAL(CDEMU.emulated_drive));
281 SET_INT13ET(regs, controller_index, GET_GLOBAL(CDEMU.controller_index));
282 SET_INT13ET(regs, ilba, GET_GLOBAL(CDEMU.ilba));
283 SET_INT13ET(regs, device_spec, GET_GLOBAL(CDEMU.device_spec));
284 SET_INT13ET(regs, buffer_segment, GET_GLOBAL(CDEMU.buffer_segment));
285 SET_INT13ET(regs, load_segment, GET_GLOBAL(CDEMU.load_segment));
286 SET_INT13ET(regs, sector_count, GET_GLOBAL(CDEMU.sector_count));
287 SET_INT13ET(regs, cylinders, GET_GLOBAL(CDEMU.vdevice.cylinders));
288 SET_INT13ET(regs, sectors, GET_GLOBAL(CDEMU.vdevice.spt));
289 SET_INT13ET(regs, heads, GET_GLOBAL(CDEMU.vdevice.heads));
291 // If we have to terminate emulation
292 if (regs->al == 0x00) {
293 // FIXME ElTorito Various. Should be handled accordingly to spec
294 SET_EBDA(cdemu_active, 0x00); // bye bye
297 disk_ret(regs, DISK_RET_SUCCESS);
301 /****************************************************************
303 ****************************************************************/
307 atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
311 memset(atacmd, 0, sizeof(atacmd));
312 atacmd[0] = ATA_CMD_REQUEST_SENSE;
313 atacmd[4] = sizeof(buffer);
314 int ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
315 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
326 atapi_is_ready(u16 device)
328 if (GET_GLOBAL(ATA.devices[device].type) != ATA_TYPE_ATAPI) {
329 printf("not implemented for non-ATAPI device\n");
333 dprintf(6, "ata_detect_medium: begin\n");
335 memset(packet, 0, sizeof(packet));
336 packet[0] = 0x25; /* READ CAPACITY */
338 /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
339 * is reported by the device. If the device reports "IN PROGRESS",
340 * 30 seconds is added. */
346 if (time >= timeout) {
347 dprintf(1, "read capacity failed\n");
350 int ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
351 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
356 ret = atapi_get_sense(device, &asc, &ascq);
360 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
361 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
365 if (asc == 0x04 && ascq == 0x01 && !in_progress) {
366 /* IN PROGRESS OF BECOMING READY */
367 printf("Waiting for device to detect medium... ");
368 /* Allow 30 seconds more */
374 u32 block_len = (u32) buf[4] << 24
379 if (block_len != 2048 && block_len != 512) {
380 printf("Unsupported sector size %u\n", block_len);
383 SET_GLOBAL(ATA.devices[device].blksize, block_len);
385 u32 sectors = (u32) buf[0] << 24
390 dprintf(6, "sectors=%u\n", sectors);
391 if (block_len == 2048)
392 sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
393 if (sectors != GET_GLOBAL(ATA.devices[device].sectors))
394 printf("%dMB medium detected\n", sectors>>(20-9));
395 SET_GLOBAL(ATA.devices[device].sectors, sectors);
400 atapi_is_cdrom(u8 device)
402 if (device >= CONFIG_MAX_ATA_DEVICES)
405 if (GET_GLOBAL(ATA.devices[device].type) != ATA_TYPE_ATAPI)
408 if (GET_GLOBAL(ATA.devices[device].device) != ATA_DEVICE_CDROM)
414 // Compare a string on the stack to one in the code segment.
416 streq_cs(u8 *s1, char *cs_s2)
420 if (*s1 != GET_GLOBAL(*s2))
432 // Find out the first cdrom
434 for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
435 if (atapi_is_cdrom(device))
437 if (device >= CONFIG_MAX_ATA_DEVICES)
441 int ret = atapi_is_ready(device);
443 dprintf(1, "atapi_is_ready returned %d\n", ret);
445 // Read the Boot Record Volume Descriptor
447 ret = cdrom_read(device, 0x11, 1
448 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
455 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
458 // ok, now we calculate the Boot catalog address
459 u32 lba = *(u32*)&buffer[0x47];
461 // And we read the Boot Catalog
462 ret = cdrom_read(device, lba, 1
463 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
468 if (buffer[0x00] != 0x01)
470 if (buffer[0x01] != 0x00)
471 return 9; // Platform
472 if (buffer[0x1E] != 0x55)
474 if (buffer[0x1F] != 0xAA)
477 // Initial/Default Entry
478 if (buffer[0x20] != 0x88)
479 return 11; // Bootable
481 u8 media = buffer[0x21];
482 SET_GLOBAL(CDEMU.media, media);
484 SET_GLOBAL(CDEMU.controller_index, device/2);
485 SET_GLOBAL(CDEMU.device_spec, device%2);
487 u16 boot_segment = *(u16*)&buffer[0x22];
489 boot_segment = 0x07C0;
490 SET_GLOBAL(CDEMU.load_segment, boot_segment);
491 SET_GLOBAL(CDEMU.buffer_segment, 0x0000);
493 u16 nbsectors = *(u16*)&buffer[0x26];
494 SET_GLOBAL(CDEMU.sector_count, nbsectors);
496 lba = *(u32*)&buffer[0x28];
497 SET_GLOBAL(CDEMU.ilba, lba);
499 // And we read the image in memory
500 ret = cdrom_read_emu(device, 0, nbsectors, MAKE_FARPTR(boot_segment, 0));
505 // No emulation requested - return success.
507 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
508 // Win2000 cd boot needs to know it booted from cd
509 SET_GLOBAL(CDEMU.emulated_drive, 0xE0);
514 // Emulation of a floppy/harddisk requested
515 if (! CONFIG_CDROM_EMU)
518 // Set emulated drive id and increase bios installed hardware
522 SET_GLOBAL(CDEMU.emulated_drive, 0x00);
523 SETBITS_BDA(equipment_list_flags, 0x41);
525 // Harddrive emulation
526 SET_GLOBAL(CDEMU.emulated_drive, 0x80);
527 SET_GLOBAL(ATA.hdcount, GET_GLOBAL(ATA.hdcount) + 1);
530 // Remember the media type
532 case 0x01: // 1.2M floppy
533 SET_GLOBAL(CDEMU.vdevice.spt, 15);
534 SET_GLOBAL(CDEMU.vdevice.cylinders, 80);
535 SET_GLOBAL(CDEMU.vdevice.heads, 2);
537 case 0x02: // 1.44M floppy
538 SET_GLOBAL(CDEMU.vdevice.spt, 18);
539 SET_GLOBAL(CDEMU.vdevice.cylinders, 80);
540 SET_GLOBAL(CDEMU.vdevice.heads, 2);
542 case 0x03: // 2.88M floppy
543 SET_GLOBAL(CDEMU.vdevice.spt, 36);
544 SET_GLOBAL(CDEMU.vdevice.cylinders, 80);
545 SET_GLOBAL(CDEMU.vdevice.heads, 2);
547 case 0x04: { // Harddrive
548 u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
549 u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
550 u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
551 SET_GLOBAL(CDEMU.vdevice.spt, spt & 0x3f);
552 SET_GLOBAL(CDEMU.vdevice.cylinders, cyl);
553 SET_GLOBAL(CDEMU.vdevice.heads, heads);
558 // everything is ok, so from now on, the emulation is active
559 SET_EBDA(cdemu_active, 0x01);
560 dprintf(6, "cdemu media=%d\n", media);