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 ****************************************************************/
184 cdemu_1302(struct bregs *regs, u8 device)
186 emu_access(regs, device, ATA_CMD_READ_SECTORS);
189 // verify disk sectors
191 cdemu_1304(struct bregs *regs, u8 device)
193 emu_access(regs, device, 0);
196 // read disk drive parameters
198 cdemu_1308(struct bregs *regs, u8 device)
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);
206 regs->ch = nlc & 0xff;
207 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
209 // FIXME ElTorito Various. should send the real count of drives 1 or 2
210 // FIXME ElTorito Harddisk. should send the HD count
212 u8 media = GET_EBDA(cdemu.media);
214 regs->bl = media * 2;
217 regs->di = (u16)&diskette_param_table2;
219 disk_ret(regs, DISK_RET_SUCCESS);
223 cdemu_13(struct bregs *regs)
227 u8 device = GET_EBDA(cdemu.controller_index) * 2;
228 device += GET_EBDA(cdemu.device_spec);
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;
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)
300 , MAKE_32_PTR(GET_SEG(SS), (u32)buffer));
311 atapi_is_ready(u16 device)
313 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
314 printf("not implemented for non-ATAPI device\n");
318 DEBUGF("ata_detect_medium: begin\n");
320 memset(packet, 0, sizeof(packet));
321 packet[0] = 0x25; /* READ CAPACITY */
323 /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
324 * is reported by the device. If the device reports "IN PROGRESS",
325 * 30 seconds is added. */
331 if (time >= timeout) {
332 DEBUGF("read capacity failed\n");
335 u16 ret = ata_cmd_packet(device, packet, sizeof(packet)
337 , MAKE_32_PTR(GET_SEG(SS), (u32)buf));
342 ret = atapi_get_sense(device, &asc, &ascq);
346 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
347 DEBUGF("Device reports MEDIUM NOT PRESENT\n");
351 if (asc == 0x04 && ascq == 0x01 && !in_progress) {
352 /* IN PROGRESS OF BECOMING READY */
353 printf("Waiting for device to detect medium... ");
354 /* Allow 30 seconds more */
360 u32 block_len = (u32) buf[4] << 24
365 if (block_len != 2048 && block_len != 512) {
366 printf("Unsupported sector size %u\n", block_len);
369 SET_EBDA(ata.devices[device].blksize, block_len);
371 u32 sectors = (u32) buf[0] << 24
376 DEBUGF("sectors=%u\n", sectors);
377 if (block_len == 2048)
378 sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
379 if (sectors != GET_EBDA(ata.devices[device].sectors))
380 printf("%dMB medium detected\n", sectors>>(20-9));
381 SET_EBDA(ata.devices[device].sectors, sectors);
386 atapi_is_cdrom(u8 device)
388 if (device >= CONFIG_MAX_ATA_DEVICES)
391 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI)
394 if (GET_EBDA(ata.devices[device].device) != ATA_DEVICE_CDROM)
400 // Compare a string on the stack to one in the code segment.
402 streq_cs(u8 *s1, char *cs_s2)
406 if (*s1 != GET_VAR(CS, *s2))
418 // Find out the first cdrom
420 for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
421 if (atapi_is_cdrom(device))
424 u16 ret = atapi_is_ready(device);
426 BX_INFO("ata_is_ready returned %d\n", ret);
429 if (device >= CONFIG_MAX_ATA_DEVICES)
432 // Read the Boot Record Volume Descriptor
434 ret = cdrom_read(device, 0x11, 2048
435 , MAKE_32_PTR(GET_SEG(SS), (u32)buffer), 0);
442 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
445 // ok, now we calculate the Boot catalog address
446 u32 lba = *(u32*)&buffer[0x47];
448 // And we read the Boot Catalog
449 ret = cdrom_read(device, lba, 2048
450 , MAKE_32_PTR(GET_SEG(SS), (u32)buffer), 0);
455 if (buffer[0x00] != 0x01)
457 if (buffer[0x01] != 0x00)
458 return 9; // Platform
459 if (buffer[0x1E] != 0x55)
461 if (buffer[0x1F] != 0xAA)
464 // Initial/Default Entry
465 if (buffer[0x20] != 0x88)
466 return 11; // Bootable
468 u8 media = buffer[0x21];
469 SET_EBDA(cdemu.media, media);
471 SET_EBDA(cdemu.controller_index, device/2);
472 SET_EBDA(cdemu.device_spec, device%2);
474 u16 boot_segment = *(u16*)&buffer[0x22];
476 boot_segment = 0x07C0;
477 SET_EBDA(cdemu.load_segment, boot_segment);
478 SET_EBDA(cdemu.buffer_segment, 0x0000);
480 u16 nbsectors = *(u16*)&buffer[0x26];
481 SET_EBDA(cdemu.sector_count, nbsectors);
483 lba = *(u32*)&buffer[0x28];
484 SET_EBDA(cdemu.ilba, lba);
486 // And we read the image in memory
487 ret = cdrom_read(device, lba, nbsectors*512
488 , MAKE_32_PTR(boot_segment, 0), 0);
493 // No emulation requested - return success.
495 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
496 // Win2000 cd boot needs to know it booted from cd
497 SET_EBDA(cdemu.emulated_drive, 0xE0);
502 // Emulation of a floppy/harddisk requested
503 if (! CONFIG_CDROM_EMU)
506 // Set emulated drive id and increase bios installed hardware
510 SET_EBDA(cdemu.emulated_drive, 0x00);
511 SETBITS_BDA(equipment_list_flags, 0x41);
513 // Harddrive emulation
514 SET_EBDA(cdemu.emulated_drive, 0x80);
515 SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
518 // Remember the media type
520 case 0x01: // 1.2M floppy
521 SET_EBDA(cdemu.vdevice.spt, 15);
522 SET_EBDA(cdemu.vdevice.cylinders, 80);
523 SET_EBDA(cdemu.vdevice.heads, 2);
525 case 0x02: // 1.44M floppy
526 SET_EBDA(cdemu.vdevice.spt, 18);
527 SET_EBDA(cdemu.vdevice.cylinders, 80);
528 SET_EBDA(cdemu.vdevice.heads, 2);
530 case 0x03: // 2.88M floppy
531 SET_EBDA(cdemu.vdevice.spt, 36);
532 SET_EBDA(cdemu.vdevice.cylinders, 80);
533 SET_EBDA(cdemu.vdevice.heads, 2);
535 case 0x04: { // Harddrive
536 u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
537 u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
538 u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
539 SET_EBDA(cdemu.vdevice.spt, spt & 0x3f);
540 SET_EBDA(cdemu.vdevice.cylinders, cyl);
541 SET_EBDA(cdemu.vdevice.heads, heads);
546 // everything is ok, so from now on, the emulation is active
547 SET_EBDA(cdemu.active, 0x01);
548 DEBUGF("cdemu media=%d\n", media);