blob: ef81bb376f24af9b8e87f577c05fb555c76c09e1 [file] [log] [blame]
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -05001// 16bit code to access cdrom drives.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
8#include "disk.h" // cdrom_13
9#include "util.h" // memset
10#include "ata.h" // ATA_CMD_READ_SECTORS
11
Kevin O'Connor180a9592008-03-04 22:50:53 -050012#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
13#define DEBUGF(fmt, args...)
14
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050015
16/****************************************************************
17 * CDROM functions
18 ****************************************************************/
19
20// read disk drive size
21static void
22cdrom_1315(struct bregs *regs, u8 device)
23{
24 disk_ret(regs, DISK_RET_EADDRNOTFOUND);
25}
26
27// lock
28static void
29cdrom_134500(struct bregs *regs, u8 device)
30{
31 u8 locks = GET_EBDA(ata.devices[device].lock);
32 if (locks == 0xff) {
33 regs->al = 1;
34 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
35 return;
36 }
37 SET_EBDA(ata.devices[device].lock, locks + 1);
38 regs->al = 1;
39 disk_ret(regs, DISK_RET_SUCCESS);
40}
41
42// unlock
43static void
44cdrom_134501(struct bregs *regs, u8 device)
45{
46 u8 locks = GET_EBDA(ata.devices[device].lock);
47 if (locks == 0x00) {
48 regs->al = 0;
49 disk_ret(regs, DISK_RET_ENOTLOCKED);
50 return;
51 }
52 locks--;
53 SET_EBDA(ata.devices[device].lock, locks);
54 regs->al = (locks ? 1 : 0);
55 disk_ret(regs, DISK_RET_SUCCESS);
56}
57
58// status
59static void
60cdrom_134502(struct bregs *regs, u8 device)
61{
62 u8 locks = GET_EBDA(ata.devices[device].lock);
63 regs->al = (locks ? 1 : 0);
64 disk_ret(regs, DISK_RET_SUCCESS);
65}
66
67static void
68cdrom_1345XX(struct bregs *regs, u8 device)
69{
70 disk_ret(regs, DISK_RET_EPARAM);
71}
72
73// IBM/MS lock/unlock drive
74static void
75cdrom_1345(struct bregs *regs, u8 device)
76{
77 switch (regs->al) {
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;
82 }
83}
84
85// IBM/MS eject media
86static void
87cdrom_1346(struct bregs *regs, u8 device)
88{
89 u8 locks = GET_EBDA(ata.devices[device].lock);
90 if (locks != 0) {
91 disk_ret(regs, DISK_RET_ELOCKED);
92 return;
93 }
94
95 // FIXME should handle 0x31 no media in device
96 // FIXME should handle 0xb5 valid request failed
97
98 // Call removable media eject
99 struct bregs br;
100 memset(&br, 0, sizeof(br));
101 br.ah = 0x52;
102 call16_int(0x15, &br);
103
104 if (br.ah || br.flags & F_CF) {
105 disk_ret(regs, DISK_RET_ELOCKED);
106 return;
107 }
108 disk_ret(regs, DISK_RET_SUCCESS);
109}
110
111// IBM/MS extended media change
112static void
113cdrom_1349(struct bregs *regs, u8 device)
114{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400115 set_fail(regs);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500116 // always send changed ??
117 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500118}
119
120static void
121cdrom_ok(struct bregs *regs, u8 device)
122{
123 disk_ret(regs, DISK_RET_SUCCESS);
124}
125
126static void
127cdrom_wp(struct bregs *regs, u8 device)
128{
129 disk_ret(regs, DISK_RET_EWRITEPROTECT);
130}
131
132void
133cdrom_13(struct bregs *regs, u8 device)
134{
135 //debug_stub(regs);
136
137 switch (regs->ah) {
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;
142
143 // These functions are the same as for hard disks
144 case 0x01:
145 case 0x41:
146 case 0x42:
147 case 0x44:
148 case 0x47:
149 case 0x48:
150 case 0x4e:
151 disk_13(regs, device);
152 break;
153
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);
164 break;
165
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);
171 break;
172
173 default: disk_13XX(regs, device); break;
174 }
175}
176
177
178/****************************************************************
179 * CD emulation
180 ****************************************************************/
181
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400182// Read a series of 512 byte sectors from the cdrom starting at the
183// image offset.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400184__always_inline int
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400185cdrom_read_emu(u16 biosid, u32 vlba, u32 count, void *far_buffer)
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500186{
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400187 u32 ilba = GET_EBDA(cdemu.ilba);
188 return cdrom_read_512(biosid, ilba * 4 + vlba, count, far_buffer);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500189}
190
191// read disk drive parameters
192static void
193cdemu_1308(struct bregs *regs, u8 device)
194{
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);
198
199 regs->al = 0x00;
200 regs->bl = 0x00;
201 regs->ch = nlc & 0xff;
202 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
203 regs->dh = nlh;
204 // FIXME ElTorito Various. should send the real count of drives 1 or 2
205 // FIXME ElTorito Harddisk. should send the HD count
206 regs->dl = 0x02;
207 u8 media = GET_EBDA(cdemu.media);
208 if (media <= 3)
209 regs->bl = media * 2;
210
211 regs->es = SEG_BIOS;
Kevin O'Connor117fc212008-04-13 18:17:02 -0400212 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500213
214 disk_ret(regs, DISK_RET_SUCCESS);
215}
216
217void
218cdemu_13(struct bregs *regs)
219{
220 //debug_stub(regs);
221
222 u8 device = GET_EBDA(cdemu.controller_index) * 2;
223 device += GET_EBDA(cdemu.device_spec);
224
225 switch (regs->ah) {
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400226 // These functions are the same as for hard disks
227 case 0x02:
228 case 0x04:
229 disk_13(regs, device);
230 break;
231
Kevin O'Connoraa7ddd72008-03-22 23:58:26 -0400232 // These functions are the same as standard CDROM.
233 case 0x00:
234 case 0x01:
235 case 0x03:
236 case 0x05:
237 case 0x09:
238 case 0x0c:
239 case 0x0d:
240 case 0x10:
241 case 0x11:
242 case 0x14:
243 case 0x15:
244 case 0x16:
245 cdrom_13(regs, device);
246 break;
247
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500248 case 0x08: cdemu_1308(regs, device); break;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400249
Kevin O'Connoraa7ddd72008-03-22 23:58:26 -0400250 default: disk_13XX(regs, device); break;
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500251 }
252}
253
254struct eltorito_s {
255 u8 size;
256 u8 media;
257 u8 emulated_drive;
258 u8 controller_index;
259 u32 ilba;
260 u16 device_spec;
261 u16 buffer_segment;
262 u16 load_segment;
263 u16 sector_count;
264 u8 cylinders;
265 u8 sectors;
266 u8 heads;
267};
268
269#define SET_INT13ET(regs,var,val) \
270 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
271
272// ElTorito - Terminate disk emu
273void
274cdemu_134b(struct bregs *regs)
275{
276 // FIXME ElTorito Hardcoded
277 SET_INT13ET(regs, size, 0x13);
278 SET_INT13ET(regs, media, GET_EBDA(cdemu.media));
279 SET_INT13ET(regs, emulated_drive, GET_EBDA(cdemu.emulated_drive));
280 SET_INT13ET(regs, controller_index, GET_EBDA(cdemu.controller_index));
281 SET_INT13ET(regs, ilba, GET_EBDA(cdemu.ilba));
282 SET_INT13ET(regs, device_spec, GET_EBDA(cdemu.device_spec));
283 SET_INT13ET(regs, buffer_segment, GET_EBDA(cdemu.buffer_segment));
284 SET_INT13ET(regs, load_segment, GET_EBDA(cdemu.load_segment));
285 SET_INT13ET(regs, sector_count, GET_EBDA(cdemu.sector_count));
286 SET_INT13ET(regs, cylinders, GET_EBDA(cdemu.vdevice.cylinders));
287 SET_INT13ET(regs, sectors, GET_EBDA(cdemu.vdevice.spt));
288 SET_INT13ET(regs, heads, GET_EBDA(cdemu.vdevice.heads));
289
290 // If we have to terminate emulation
291 if (regs->al == 0x00) {
292 // FIXME ElTorito Various. Should be handled accordingly to spec
293 SET_EBDA(cdemu.active, 0x00); // bye bye
294 }
295
296 disk_ret(regs, DISK_RET_SUCCESS);
297}
Kevin O'Connor180a9592008-03-04 22:50:53 -0500298
299
300/****************************************************************
301 * CD booting
302 ****************************************************************/
303
304// Request SENSE
305static u16
306atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
307{
308 u8 buffer[18];
309 u8 atacmd[12];
310 memset(atacmd, 0, sizeof(atacmd));
311 atacmd[0] = ATA_CMD_REQUEST_SENSE;
312 atacmd[4] = sizeof(buffer);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400313 u16 ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
Kevin O'Connor070231b2008-03-22 20:44:37 -0400314 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500315 if (ret != 0)
316 return 0x0002;
317
318 *asc = buffer[12];
319 *ascq = buffer[13];
320
321 return 0;
322}
323
324static u16
325atapi_is_ready(u16 device)
326{
327 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
328 printf("not implemented for non-ATAPI device\n");
329 return -1;
330 }
331
332 DEBUGF("ata_detect_medium: begin\n");
333 u8 packet[12];
334 memset(packet, 0, sizeof(packet));
335 packet[0] = 0x25; /* READ CAPACITY */
336
337 /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
338 * is reported by the device. If the device reports "IN PROGRESS",
339 * 30 seconds is added. */
340 u8 buf[8];
341 u32 timeout = 5000;
342 u32 time = 0;
343 u8 in_progress = 0;
344 for (;; time+=100) {
345 if (time >= timeout) {
346 DEBUGF("read capacity failed\n");
347 return -1;
348 }
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400349 u16 ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
Kevin O'Connor070231b2008-03-22 20:44:37 -0400350 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500351 if (ret == 0)
352 break;
353
354 u8 asc=0, ascq=0;
355 ret = atapi_get_sense(device, &asc, &ascq);
356 if (!ret)
357 continue;
358
359 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
360 DEBUGF("Device reports MEDIUM NOT PRESENT\n");
361 return -1;
362 }
363
364 if (asc == 0x04 && ascq == 0x01 && !in_progress) {
365 /* IN PROGRESS OF BECOMING READY */
366 printf("Waiting for device to detect medium... ");
367 /* Allow 30 seconds more */
368 timeout = 30000;
369 in_progress = 1;
370 }
371 }
372
373 u32 block_len = (u32) buf[4] << 24
374 | (u32) buf[5] << 16
375 | (u32) buf[6] << 8
376 | (u32) buf[7] << 0;
377
378 if (block_len != 2048 && block_len != 512) {
379 printf("Unsupported sector size %u\n", block_len);
380 return -1;
381 }
382 SET_EBDA(ata.devices[device].blksize, block_len);
383
384 u32 sectors = (u32) buf[0] << 24
385 | (u32) buf[1] << 16
386 | (u32) buf[2] << 8
387 | (u32) buf[3] << 0;
388
389 DEBUGF("sectors=%u\n", sectors);
390 if (block_len == 2048)
391 sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
392 if (sectors != GET_EBDA(ata.devices[device].sectors))
393 printf("%dMB medium detected\n", sectors>>(20-9));
394 SET_EBDA(ata.devices[device].sectors, sectors);
395 return 0;
396}
397
398static u16
399atapi_is_cdrom(u8 device)
400{
401 if (device >= CONFIG_MAX_ATA_DEVICES)
402 return 0;
403
404 if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI)
405 return 0;
406
407 if (GET_EBDA(ata.devices[device].device) != ATA_DEVICE_CDROM)
408 return 0;
409
410 return 1;
411}
412
413// Compare a string on the stack to one in the code segment.
414static int
415streq_cs(u8 *s1, char *cs_s2)
416{
417 u8 *s2 = (u8*)cs_s2;
418 for (;;) {
419 if (*s1 != GET_VAR(CS, *s2))
420 return 0;
421 if (! *s1)
422 return 1;
423 s1++;
424 s2++;
425 }
426}
427
428u16
429cdrom_boot()
430{
431 // Find out the first cdrom
432 u8 device;
433 for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
434 if (atapi_is_cdrom(device))
435 break;
436
437 u16 ret = atapi_is_ready(device);
438 if (ret)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400439 dprintf(1, "ata_is_ready returned %d\n", ret);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500440
441 // if not found
442 if (device >= CONFIG_MAX_ATA_DEVICES)
443 return 2;
444
445 // Read the Boot Record Volume Descriptor
446 u8 buffer[2048];
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400447 ret = cdrom_read(device, 0x11, 1
448 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500449 if (ret)
450 return 3;
451
452 // Validity checks
453 if (buffer[0])
454 return 4;
455 if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
456 return 5;
457
458 // ok, now we calculate the Boot catalog address
459 u32 lba = *(u32*)&buffer[0x47];
460
461 // And we read the Boot Catalog
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400462 ret = cdrom_read(device, lba, 1
463 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500464 if (ret)
465 return 7;
466
467 // Validation entry
468 if (buffer[0x00] != 0x01)
469 return 8; // Header
470 if (buffer[0x01] != 0x00)
471 return 9; // Platform
472 if (buffer[0x1E] != 0x55)
473 return 10; // key 1
474 if (buffer[0x1F] != 0xAA)
475 return 10; // key 2
476
477 // Initial/Default Entry
478 if (buffer[0x20] != 0x88)
479 return 11; // Bootable
480
Kevin O'Connordfa16502008-03-22 20:13:08 -0400481 u8 media = buffer[0x21];
482 SET_EBDA(cdemu.media, media);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500483
484 SET_EBDA(cdemu.controller_index, device/2);
485 SET_EBDA(cdemu.device_spec, device%2);
486
487 u16 boot_segment = *(u16*)&buffer[0x22];
488 if (!boot_segment)
489 boot_segment = 0x07C0;
Kevin O'Connordfa16502008-03-22 20:13:08 -0400490 SET_EBDA(cdemu.load_segment, boot_segment);
491 SET_EBDA(cdemu.buffer_segment, 0x0000);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500492
493 u16 nbsectors = *(u16*)&buffer[0x26];
494 SET_EBDA(cdemu.sector_count, nbsectors);
495
496 lba = *(u32*)&buffer[0x28];
497 SET_EBDA(cdemu.ilba, lba);
498
499 // And we read the image in memory
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400500 ret = cdrom_read_emu(device, 0, nbsectors, MAKE_FARPTR(boot_segment, 0));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500501 if (ret)
502 return 12;
503
Kevin O'Connordfa16502008-03-22 20:13:08 -0400504 if (media == 0) {
505 // No emulation requested - return success.
506
507 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
508 // Win2000 cd boot needs to know it booted from cd
509 SET_EBDA(cdemu.emulated_drive, 0xE0);
510
511 return 0;
512 }
513
514 // Emulation of a floppy/harddisk requested
515 if (! CONFIG_CDROM_EMU)
516 return 13;
517
518 // Set emulated drive id and increase bios installed hardware
519 // number of devices
520 if (media < 4) {
521 // Floppy emulation
522 SET_EBDA(cdemu.emulated_drive, 0x00);
523 SETBITS_BDA(equipment_list_flags, 0x41);
524 } else {
525 // Harddrive emulation
526 SET_EBDA(cdemu.emulated_drive, 0x80);
527 SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
528 }
529
Kevin O'Connor180a9592008-03-04 22:50:53 -0500530 // Remember the media type
Kevin O'Connordfa16502008-03-22 20:13:08 -0400531 switch (media) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500532 case 0x01: // 1.2M floppy
533 SET_EBDA(cdemu.vdevice.spt, 15);
534 SET_EBDA(cdemu.vdevice.cylinders, 80);
535 SET_EBDA(cdemu.vdevice.heads, 2);
536 break;
537 case 0x02: // 1.44M floppy
538 SET_EBDA(cdemu.vdevice.spt, 18);
539 SET_EBDA(cdemu.vdevice.cylinders, 80);
540 SET_EBDA(cdemu.vdevice.heads, 2);
541 break;
542 case 0x03: // 2.88M floppy
543 SET_EBDA(cdemu.vdevice.spt, 36);
544 SET_EBDA(cdemu.vdevice.cylinders, 80);
545 SET_EBDA(cdemu.vdevice.heads, 2);
546 break;
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_EBDA(cdemu.vdevice.spt, spt & 0x3f);
552 SET_EBDA(cdemu.vdevice.cylinders, cyl);
553 SET_EBDA(cdemu.vdevice.heads, heads);
554 break;
555 }
556 }
557
Kevin O'Connordfa16502008-03-22 20:13:08 -0400558 // everything is ok, so from now on, the emulation is active
559 SET_EBDA(cdemu.active, 0x01);
560 DEBUGF("cdemu media=%d\n", media);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500561
562 return 0;
563}