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
12 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
13 #define DEBUGF(fmt, args...)
16 /****************************************************************
18 ****************************************************************/
20 // read disk drive size
22 cdrom_1315(struct bregs *regs, u8 device)
24 disk_ret(regs, DISK_RET_EADDRNOTFOUND);
29 cdrom_134500(struct bregs *regs, u8 device)
31 u8 locks = GET_EBDA(ata.devices[device].lock);
34 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
37 SET_EBDA(ata.devices[device].lock, locks + 1);
39 disk_ret(regs, DISK_RET_SUCCESS);
44 cdrom_134501(struct bregs *regs, u8 device)
46 u8 locks = GET_EBDA(ata.devices[device].lock);
49 disk_ret(regs, DISK_RET_ENOTLOCKED);
53 SET_EBDA(ata.devices[device].lock, locks);
54 regs->al = (locks ? 1 : 0);
55 disk_ret(regs, DISK_RET_SUCCESS);
60 cdrom_134502(struct bregs *regs, u8 device)
62 u8 locks = GET_EBDA(ata.devices[device].lock);
63 regs->al = (locks ? 1 : 0);
64 disk_ret(regs, DISK_RET_SUCCESS);
68 cdrom_1345XX(struct bregs *regs, u8 device)
70 disk_ret(regs, DISK_RET_EPARAM);
73 // IBM/MS lock/unlock drive
75 cdrom_1345(struct bregs *regs, u8 device)
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;
87 cdrom_1346(struct bregs *regs, u8 device)
89 u8 locks = GET_EBDA(ata.devices[device].lock);
91 disk_ret(regs, DISK_RET_ELOCKED);
95 // FIXME should handle 0x31 no media in device
96 // FIXME should handle 0xb5 valid request failed
98 // Call removable media eject
100 memset(&br, 0, sizeof(br));
102 call16_int(0x15, &br);
104 if (br.ah || br.flags & F_CF) {
105 disk_ret(regs, DISK_RET_ELOCKED);
108 disk_ret(regs, DISK_RET_SUCCESS);
111 // IBM/MS extended media change
113 cdrom_1349(struct bregs *regs, u8 device)
116 // always send changed ??
117 regs->ah = DISK_RET_ECHANGED;
121 cdrom_ok(struct bregs *regs, u8 device)
123 disk_ret(regs, DISK_RET_SUCCESS);
127 cdrom_wp(struct bregs *regs, u8 device)
129 disk_ret(regs, DISK_RET_EWRITEPROTECT);
133 cdrom_13(struct bregs *regs, u8 device)
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;
143 // These functions are the same as for hard disks
151 disk_13(regs, device);
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);
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);
173 default: disk_13XX(regs, device); break;
178 /****************************************************************
180 ****************************************************************/
182 // Read a series of 512 byte sectors from the cdrom starting at the
185 cdrom_read_emu(u16 biosid, u32 vlba, u32 count, void *far_buffer)
187 u32 ilba = GET_EBDA(cdemu.ilba);
188 return cdrom_read_512(biosid, ilba * 4 + vlba, count, far_buffer);
191 // read disk drive parameters
193 cdemu_1308(struct bregs *regs, u8 device)
195 u16 nlc = GET_EBDA(cdemu.vdevice.cylinders) - 1;
196 u16 nlh = GET_EBDA(cdemu.vdevice.heads) - 1;
197 u16 nlspt = GET_EBDA(cdemu.vdevice.spt);
201 regs->ch = nlc & 0xff;
202 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
204 // FIXME ElTorito Various. should send the real count of drives 1 or 2
205 // FIXME ElTorito Harddisk. should send the HD count
207 u8 media = GET_EBDA(cdemu.media);
209 regs->bl = media * 2;
212 regs->di = (u16)&diskette_param_table2;
214 disk_ret(regs, DISK_RET_SUCCESS);
218 cdemu_13(struct bregs *regs)
222 u8 device = GET_EBDA(cdemu.controller_index) * 2;
223 device += GET_EBDA(cdemu.device_spec);
226 // These functions are the same as for hard disks
229 disk_13(regs, device);
232 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;
254 #define SET_INT13ET(regs,var,val) \
255 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
257 // ElTorito - Terminate disk emu
259 cdemu_134b(struct bregs *regs)
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));
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
281 disk_ret(regs, DISK_RET_SUCCESS);
285 /****************************************************************
287 ****************************************************************/
291 atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
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), sizeof(buffer)
299 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
310 atapi_is_ready(u16 device)
312 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
313 printf("not implemented for non-ATAPI device\n");
317 DEBUGF("ata_detect_medium: begin\n");
319 memset(packet, 0, sizeof(packet));
320 packet[0] = 0x25; /* READ CAPACITY */
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. */
330 if (time >= timeout) {
331 DEBUGF("read capacity failed\n");
334 u16 ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
335 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
340 ret = atapi_get_sense(device, &asc, &ascq);
344 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
345 DEBUGF("Device reports MEDIUM NOT PRESENT\n");
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 */
358 u32 block_len = (u32) buf[4] << 24
363 if (block_len != 2048 && block_len != 512) {
364 printf("Unsupported sector size %u\n", block_len);
367 SET_EBDA(ata.devices[device].blksize, block_len);
369 u32 sectors = (u32) buf[0] << 24
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);
384 atapi_is_cdrom(u8 device)
386 if (device >= CONFIG_MAX_ATA_DEVICES)
389 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI)
392 if (GET_EBDA(ata.devices[device].device) != ATA_DEVICE_CDROM)
398 // Compare a string on the stack to one in the code segment.
400 streq_cs(u8 *s1, char *cs_s2)
404 if (*s1 != GET_VAR(CS, *s2))
416 // Find out the first cdrom
418 for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
419 if (atapi_is_cdrom(device))
422 u16 ret = atapi_is_ready(device);
424 BX_INFO("ata_is_ready returned %d\n", ret);
427 if (device >= CONFIG_MAX_ATA_DEVICES)
430 // Read the Boot Record Volume Descriptor
432 ret = cdrom_read(device, 0x11, 1
433 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
440 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
443 // ok, now we calculate the Boot catalog address
444 u32 lba = *(u32*)&buffer[0x47];
446 // And we read the Boot Catalog
447 ret = cdrom_read(device, lba, 1
448 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
453 if (buffer[0x00] != 0x01)
455 if (buffer[0x01] != 0x00)
456 return 9; // Platform
457 if (buffer[0x1E] != 0x55)
459 if (buffer[0x1F] != 0xAA)
462 // Initial/Default Entry
463 if (buffer[0x20] != 0x88)
464 return 11; // Bootable
466 u8 media = buffer[0x21];
467 SET_EBDA(cdemu.media, media);
469 SET_EBDA(cdemu.controller_index, device/2);
470 SET_EBDA(cdemu.device_spec, device%2);
472 u16 boot_segment = *(u16*)&buffer[0x22];
474 boot_segment = 0x07C0;
475 SET_EBDA(cdemu.load_segment, boot_segment);
476 SET_EBDA(cdemu.buffer_segment, 0x0000);
478 u16 nbsectors = *(u16*)&buffer[0x26];
479 SET_EBDA(cdemu.sector_count, nbsectors);
481 lba = *(u32*)&buffer[0x28];
482 SET_EBDA(cdemu.ilba, lba);
484 // And we read the image in memory
485 ret = cdrom_read_emu(device, 0, nbsectors, MAKE_FARPTR(boot_segment, 0));
490 // No emulation requested - return success.
492 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
493 // Win2000 cd boot needs to know it booted from cd
494 SET_EBDA(cdemu.emulated_drive, 0xE0);
499 // Emulation of a floppy/harddisk requested
500 if (! CONFIG_CDROM_EMU)
503 // Set emulated drive id and increase bios installed hardware
507 SET_EBDA(cdemu.emulated_drive, 0x00);
508 SETBITS_BDA(equipment_list_flags, 0x41);
510 // Harddrive emulation
511 SET_EBDA(cdemu.emulated_drive, 0x80);
512 SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
515 // Remember the media type
517 case 0x01: // 1.2M floppy
518 SET_EBDA(cdemu.vdevice.spt, 15);
519 SET_EBDA(cdemu.vdevice.cylinders, 80);
520 SET_EBDA(cdemu.vdevice.heads, 2);
522 case 0x02: // 1.44M floppy
523 SET_EBDA(cdemu.vdevice.spt, 18);
524 SET_EBDA(cdemu.vdevice.cylinders, 80);
525 SET_EBDA(cdemu.vdevice.heads, 2);
527 case 0x03: // 2.88M floppy
528 SET_EBDA(cdemu.vdevice.spt, 36);
529 SET_EBDA(cdemu.vdevice.cylinders, 80);
530 SET_EBDA(cdemu.vdevice.heads, 2);
532 case 0x04: { // Harddrive
533 u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
534 u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
535 u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
536 SET_EBDA(cdemu.vdevice.spt, spt & 0x3f);
537 SET_EBDA(cdemu.vdevice.cylinders, cyl);
538 SET_EBDA(cdemu.vdevice.heads, heads);
543 // everything is ok, so from now on, the emulation is active
544 SET_EBDA(cdemu.active, 0x01);
545 DEBUGF("cdemu media=%d\n", media);