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)
115 // always send changed ??
116 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)
299 , 18L, 0, ATA_DATA_IN, 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)
335 , 0, 8L, ATA_DATA_IN, 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, 2048, GET_SEG(SS), (u32)buffer, 0);
439 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
442 // ok, now we calculate the Boot catalog address
443 u32 lba = *(u32*)&buffer[0x47];
445 // And we read the Boot Catalog
446 ret = cdrom_read(device, lba, 2048, GET_SEG(SS), (u32)buffer, 0);
451 if (buffer[0x00] != 0x01)
453 if (buffer[0x01] != 0x00)
454 return 9; // Platform
455 if (buffer[0x1E] != 0x55)
457 if (buffer[0x1F] != 0xAA)
460 // Initial/Default Entry
461 if (buffer[0x20] != 0x88)
462 return 11; // Bootable
464 SET_EBDA(cdemu.media,buffer[0x21]);
465 if (buffer[0x21] == 0)
466 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
467 // Win2000 cd boot needs to know it booted from cd
468 SET_EBDA(cdemu.emulated_drive, 0xE0);
469 else if (buffer[0x21] < 4)
470 SET_EBDA(cdemu.emulated_drive, 0x00);
472 SET_EBDA(cdemu.emulated_drive, 0x80);
474 SET_EBDA(cdemu.controller_index, device/2);
475 SET_EBDA(cdemu.device_spec, device%2);
477 u16 boot_segment = *(u16*)&buffer[0x22];
479 boot_segment = 0x07C0;
481 SET_EBDA(cdemu.load_segment,boot_segment);
482 SET_EBDA(cdemu.buffer_segment,0x0000);
484 u16 nbsectors = *(u16*)&buffer[0x26];
485 SET_EBDA(cdemu.sector_count, nbsectors);
487 lba = *(u32*)&buffer[0x28];
488 SET_EBDA(cdemu.ilba, lba);
490 // And we read the image in memory
491 ret = cdrom_read(device, lba, nbsectors*512
492 , boot_segment, 0, 0);
496 // Remember the media type
497 switch (GET_EBDA(cdemu.media)) {
498 case 0x01: // 1.2M floppy
499 SET_EBDA(cdemu.vdevice.spt, 15);
500 SET_EBDA(cdemu.vdevice.cylinders, 80);
501 SET_EBDA(cdemu.vdevice.heads, 2);
503 case 0x02: // 1.44M floppy
504 SET_EBDA(cdemu.vdevice.spt, 18);
505 SET_EBDA(cdemu.vdevice.cylinders, 80);
506 SET_EBDA(cdemu.vdevice.heads, 2);
508 case 0x03: // 2.88M floppy
509 SET_EBDA(cdemu.vdevice.spt, 36);
510 SET_EBDA(cdemu.vdevice.cylinders, 80);
511 SET_EBDA(cdemu.vdevice.heads, 2);
513 case 0x04: { // Harddrive
514 u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
515 u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
516 u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
517 SET_EBDA(cdemu.vdevice.spt, spt & 0x3f);
518 SET_EBDA(cdemu.vdevice.cylinders, cyl);
519 SET_EBDA(cdemu.vdevice.heads, heads);
524 if (GET_EBDA(cdemu.media) != 0) {
525 // Increase bios installed hardware number of devices
526 if (GET_EBDA(cdemu.emulated_drive) == 0x00)
527 SETBITS_BDA(equipment_list_flags, 0x41);
529 SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
532 // everything is ok, so from now on, the emulation is active
533 if (GET_EBDA(cdemu.media))
534 SET_EBDA(cdemu.active, 0x01);