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 LGPLv3 license.
8 #include "disk.h" // cdrom_13
9 #include "util.h" // memset
10 #include "bregs.h" // struct bregs
11 #include "biosvar.h" // GET_EBDA
12 #include "atabits.h" // ATA_TYPE_ATAPI
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 u16 ebda_seg = get_ebda_seg();
31 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[device]);
34 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
37 SET_EBDA2(ebda_seg, cdrom_locks[device], locks + 1);
39 disk_ret(regs, DISK_RET_SUCCESS);
44 cdrom_134501(struct bregs *regs, u8 device)
46 u16 ebda_seg = get_ebda_seg();
47 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[device]);
50 disk_ret(regs, DISK_RET_ENOTLOCKED);
54 SET_EBDA2(ebda_seg, 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 disk drive parameters
185 cdemu_1308(struct bregs *regs, u8 device)
187 u16 ebda_seg = get_ebda_seg();
188 u16 nlc = GET_EBDA2(ebda_seg, cdemu.cylinders) - 1;
189 u16 nlh = GET_EBDA2(ebda_seg, cdemu.heads) - 1;
190 u16 nlspt = GET_EBDA2(ebda_seg, cdemu.spt);
194 regs->ch = nlc & 0xff;
195 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
197 // FIXME ElTorito Various. should send the real count of drives 1 or 2
198 // FIXME ElTorito Harddisk. should send the HD count
200 u8 media = GET_EBDA2(ebda_seg, cdemu.media);
202 regs->bl = media * 2;
205 regs->di = (u32)&diskette_param_table2;
207 disk_ret(regs, DISK_RET_SUCCESS);
211 cdemu_13(struct bregs *regs)
215 u16 ebda_seg = get_ebda_seg();
216 u8 device = GET_EBDA2(ebda_seg, cdemu.controller_index) * 2;
217 device += GET_EBDA2(ebda_seg, cdemu.device_spec);
220 // These functions are the same as for hard disks
223 disk_13(regs, device);
226 // These functions are the same as standard CDROM.
239 cdrom_13(regs, device);
242 case 0x08: cdemu_1308(regs, device); break;
244 default: disk_13XX(regs, device); break;
263 #define SET_INT13ET(regs,var,val) \
264 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
266 // ElTorito - Terminate disk emu
268 cdemu_134b(struct bregs *regs)
270 // FIXME ElTorito Hardcoded
271 u16 ebda_seg = get_ebda_seg();
272 SET_INT13ET(regs, size, 0x13);
273 SET_INT13ET(regs, media, GET_EBDA2(ebda_seg, cdemu.media));
274 SET_INT13ET(regs, emulated_drive, GET_EBDA2(ebda_seg, cdemu.emulated_drive));
275 SET_INT13ET(regs, controller_index
276 , GET_EBDA2(ebda_seg, cdemu.controller_index));
277 SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
278 SET_INT13ET(regs, device_spec, GET_EBDA2(ebda_seg, cdemu.device_spec));
279 SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
280 SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
281 SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
282 SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.cylinders));
283 SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.spt));
284 SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.heads));
286 // If we have to terminate emulation
287 if (regs->al == 0x00) {
288 // FIXME ElTorito Various. Should be handled accordingly to spec
289 SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
292 disk_ret(regs, DISK_RET_SUCCESS);
296 /****************************************************************
298 ****************************************************************/
302 atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
306 memset(atacmd, 0, sizeof(atacmd));
307 atacmd[0] = ATA_CMD_REQUEST_SENSE;
308 atacmd[4] = sizeof(buffer);
309 int ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
310 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
321 atapi_is_ready(u16 device)
323 if (GET_GLOBAL(ATA.devices[device].type) != ATA_TYPE_ATAPI) {
324 printf("not implemented for non-ATAPI device\n");
328 dprintf(6, "ata_detect_medium: begin\n");
330 memset(packet, 0, sizeof(packet));
331 packet[0] = 0x25; /* READ CAPACITY */
333 /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
334 * is reported by the device. If the device reports "IN PROGRESS",
335 * 30 seconds is added. */
341 if (time >= timeout) {
342 dprintf(1, "read capacity failed\n");
345 int ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
346 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
351 ret = atapi_get_sense(device, &asc, &ascq);
355 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
356 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
360 if (asc == 0x04 && ascq == 0x01 && !in_progress) {
361 /* IN PROGRESS OF BECOMING READY */
362 printf("Waiting for device to detect medium... ");
363 /* Allow 30 seconds more */
369 u32 block_len = (u32) buf[4] << 24
374 if (block_len != GET_GLOBAL(ATA.devices[device].blksize)) {
375 printf("Unsupported sector size %u\n", block_len);
379 u32 sectors = (u32) buf[0] << 24
384 dprintf(6, "sectors=%u\n", sectors);
385 printf("%dMB medium detected\n", sectors>>(20-11));
390 atapi_is_cdrom(u8 device)
392 if (device >= CONFIG_MAX_ATA_DEVICES)
395 if (GET_GLOBAL(ATA.devices[device].type) != ATA_TYPE_ATAPI)
398 if (GET_GLOBAL(ATA.devices[device].device) != ATA_DEVICE_CDROM)
404 // Compare a string on the stack to one in the code segment.
406 streq_cs(u8 *s1, char *cs_s2)
410 if (*s1 != GET_GLOBAL(*s2))
422 // Find out the first cdrom
424 for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
425 if (atapi_is_cdrom(device))
427 if (device >= CONFIG_MAX_ATA_DEVICES)
431 int ret = atapi_is_ready(device);
433 dprintf(1, "atapi_is_ready returned %d\n", ret);
435 // Read the Boot Record Volume Descriptor
437 struct disk_op_s dop;
438 dop.driveid = device;
441 dop.far_buffer = MAKE_FARPTR(GET_SEG(SS), (u32)buffer);
442 ret = cdrom_read(&dop);
449 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
452 // ok, now we calculate the Boot catalog address
453 u32 lba = *(u32*)&buffer[0x47];
455 // And we read the Boot Catalog
457 ret = cdrom_read(&dop);
462 if (buffer[0x00] != 0x01)
464 if (buffer[0x01] != 0x00)
465 return 9; // Platform
466 if (buffer[0x1E] != 0x55)
468 if (buffer[0x1F] != 0xAA)
471 // Initial/Default Entry
472 if (buffer[0x20] != 0x88)
473 return 11; // Bootable
475 u16 ebda_seg = get_ebda_seg();
476 u8 media = buffer[0x21];
477 SET_EBDA2(ebda_seg, cdemu.media, media);
479 SET_EBDA2(ebda_seg, cdemu.controller_index, device/2);
480 SET_EBDA2(ebda_seg, cdemu.device_spec, device%2);
482 u16 boot_segment = *(u16*)&buffer[0x22];
484 boot_segment = 0x07C0;
485 SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
486 SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
488 u16 nbsectors = *(u16*)&buffer[0x26];
489 SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
491 lba = *(u32*)&buffer[0x28];
492 SET_EBDA2(ebda_seg, cdemu.ilba, lba);
494 // And we read the image in memory
496 dop.count = nbsectors;
497 dop.far_buffer = MAKE_FARPTR(boot_segment, 0);
498 ret = cdrom_read_512(&dop);
503 // No emulation requested - return success.
505 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
506 // Win2000 cd boot needs to know it booted from cd
507 SET_EBDA2(ebda_seg, cdemu.emulated_drive, 0xE0);
512 // Emulation of a floppy/harddisk requested
513 if (! CONFIG_CDROM_EMU)
516 // Set emulated drive id and increase bios installed hardware
520 SET_EBDA2(ebda_seg, cdemu.emulated_drive, 0x00);
521 SETBITS_BDA(equipment_list_flags, 0x41);
523 // Harddrive emulation
524 SET_EBDA2(ebda_seg, cdemu.emulated_drive, 0x80);
525 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
528 // Remember the media type
530 case 0x01: // 1.2M floppy
531 SET_EBDA2(ebda_seg, cdemu.spt, 15);
532 SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
533 SET_EBDA2(ebda_seg, cdemu.heads, 2);
535 case 0x02: // 1.44M floppy
536 SET_EBDA2(ebda_seg, cdemu.spt, 18);
537 SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
538 SET_EBDA2(ebda_seg, cdemu.heads, 2);
540 case 0x03: // 2.88M floppy
541 SET_EBDA2(ebda_seg, cdemu.spt, 36);
542 SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
543 SET_EBDA2(ebda_seg, cdemu.heads, 2);
545 case 0x04: { // Harddrive
546 u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
547 u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
548 u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
549 SET_EBDA2(ebda_seg, cdemu.spt, spt & 0x3f);
550 SET_EBDA2(ebda_seg, cdemu.cylinders, cyl);
551 SET_EBDA2(ebda_seg, cdemu.heads, heads);
556 // everything is ok, so from now on, the emulation is active
557 SET_EBDA2(ebda_seg, cdemu.active, 0x01);
558 dprintf(6, "cdemu media=%d\n", media);