Add more linker protections around variables accessed from 16bit mode.
[seabios.git] / src / cdrom.c
1 // 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 #include "bregs.h" // struct bregs
12 #include "biosvar.h" // GET_EBDA
13
14 struct cdemu_s CDEMU VAR16_32;
15
16
17 /****************************************************************
18  * CDROM functions
19  ****************************************************************/
20
21 // read disk drive size
22 static void
23 cdrom_1315(struct bregs *regs, u8 device)
24 {
25     disk_ret(regs, DISK_RET_EADDRNOTFOUND);
26 }
27
28 // lock
29 static void
30 cdrom_134500(struct bregs *regs, u8 device)
31 {
32     u8 locks = GET_EBDA(cdrom_locks[device]);
33     if (locks == 0xff) {
34         regs->al = 1;
35         disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
36         return;
37     }
38     SET_EBDA(cdrom_locks[device], locks + 1);
39     regs->al = 1;
40     disk_ret(regs, DISK_RET_SUCCESS);
41 }
42
43 // unlock
44 static void
45 cdrom_134501(struct bregs *regs, u8 device)
46 {
47     u8 locks = GET_EBDA(cdrom_locks[device]);
48     if (locks == 0x00) {
49         regs->al = 0;
50         disk_ret(regs, DISK_RET_ENOTLOCKED);
51         return;
52     }
53     locks--;
54     SET_EBDA(cdrom_locks[device], locks);
55     regs->al = (locks ? 1 : 0);
56     disk_ret(regs, DISK_RET_SUCCESS);
57 }
58
59 // status
60 static void
61 cdrom_134502(struct bregs *regs, u8 device)
62 {
63     u8 locks = GET_EBDA(cdrom_locks[device]);
64     regs->al = (locks ? 1 : 0);
65     disk_ret(regs, DISK_RET_SUCCESS);
66 }
67
68 static void
69 cdrom_1345XX(struct bregs *regs, u8 device)
70 {
71     disk_ret(regs, DISK_RET_EPARAM);
72 }
73
74 // IBM/MS lock/unlock drive
75 static void
76 cdrom_1345(struct bregs *regs, u8 device)
77 {
78     switch (regs->al) {
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;
83     }
84 }
85
86 // IBM/MS eject media
87 static void
88 cdrom_1346(struct bregs *regs, u8 device)
89 {
90     u8 locks = GET_EBDA(cdrom_locks[device]);
91     if (locks != 0) {
92         disk_ret(regs, DISK_RET_ELOCKED);
93         return;
94     }
95
96     // FIXME should handle 0x31 no media in device
97     // FIXME should handle 0xb5 valid request failed
98
99     // Call removable media eject
100     struct bregs br;
101     memset(&br, 0, sizeof(br));
102     br.ah = 0x52;
103     call16_int(0x15, &br);
104
105     if (br.ah || br.flags & F_CF) {
106         disk_ret(regs, DISK_RET_ELOCKED);
107         return;
108     }
109     disk_ret(regs, DISK_RET_SUCCESS);
110 }
111
112 // IBM/MS extended media change
113 static void
114 cdrom_1349(struct bregs *regs, u8 device)
115 {
116     set_fail(regs);
117     // always send changed ??
118     regs->ah = DISK_RET_ECHANGED;
119 }
120
121 static void
122 cdrom_ok(struct bregs *regs, u8 device)
123 {
124     disk_ret(regs, DISK_RET_SUCCESS);
125 }
126
127 static void
128 cdrom_wp(struct bregs *regs, u8 device)
129 {
130     disk_ret(regs, DISK_RET_EWRITEPROTECT);
131 }
132
133 void
134 cdrom_13(struct bregs *regs, u8 device)
135 {
136     //debug_stub(regs);
137
138     switch (regs->ah) {
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;
143
144     // These functions are the same as for hard disks
145     case 0x01:
146     case 0x41:
147     case 0x42:
148     case 0x44:
149     case 0x47:
150     case 0x48:
151     case 0x4e:
152         disk_13(regs, device);
153         break;
154
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);
165         break;
166
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);
172         break;
173
174     default:   disk_13XX(regs, device); break;
175     }
176 }
177
178
179 /****************************************************************
180  * CD emulation
181  ****************************************************************/
182
183 // Read a series of 512 byte sectors from the cdrom starting at the
184 // image offset.
185 __always_inline int
186 cdrom_read_emu(u16 biosid, u32 vlba, u32 count, void *far_buffer)
187 {
188     u32 ilba = GET_GLOBAL(CDEMU.ilba);
189     return cdrom_read_512(biosid, ilba * 4 + vlba, count, far_buffer);
190 }
191
192 // read disk drive parameters
193 static void
194 cdemu_1308(struct bregs *regs, u8 device)
195 {
196     u16 nlc   = GET_GLOBAL(CDEMU.vdevice.cylinders) - 1;
197     u16 nlh   = GET_GLOBAL(CDEMU.vdevice.heads) - 1;
198     u16 nlspt = GET_GLOBAL(CDEMU.vdevice.spt);
199
200     regs->al = 0x00;
201     regs->bl = 0x00;
202     regs->ch = nlc & 0xff;
203     regs->cl = ((nlc >> 2) & 0xc0) | (nlspt  & 0x3f);
204     regs->dh = nlh;
205     // FIXME ElTorito Various. should send the real count of drives 1 or 2
206     // FIXME ElTorito Harddisk. should send the HD count
207     regs->dl = 0x02;
208     u8 media = GET_GLOBAL(CDEMU.media);
209     if (media <= 3)
210         regs->bl = media * 2;
211
212     regs->es = SEG_BIOS;
213     regs->di = (u32)&diskette_param_table2;
214
215     disk_ret(regs, DISK_RET_SUCCESS);
216 }
217
218 void
219 cdemu_13(struct bregs *regs)
220 {
221     //debug_stub(regs);
222
223     u8 device  = GET_GLOBAL(CDEMU.controller_index) * 2;
224     device += GET_GLOBAL(CDEMU.device_spec);
225
226     switch (regs->ah) {
227     // These functions are the same as for hard disks
228     case 0x02:
229     case 0x04:
230         disk_13(regs, device);
231         break;
232
233     // These functions are the same as standard CDROM.
234     case 0x00:
235     case 0x01:
236     case 0x03:
237     case 0x05:
238     case 0x09:
239     case 0x0c:
240     case 0x0d:
241     case 0x10:
242     case 0x11:
243     case 0x14:
244     case 0x15:
245     case 0x16:
246         cdrom_13(regs, device);
247         break;
248
249     case 0x08: cdemu_1308(regs, device); break;
250
251     default:   disk_13XX(regs, device); break;
252     }
253 }
254
255 struct eltorito_s {
256     u8 size;
257     u8 media;
258     u8 emulated_drive;
259     u8 controller_index;
260     u32 ilba;
261     u16 device_spec;
262     u16 buffer_segment;
263     u16 load_segment;
264     u16 sector_count;
265     u8 cylinders;
266     u8 sectors;
267     u8 heads;
268 };
269
270 #define SET_INT13ET(regs,var,val)                                      \
271     SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
272
273 // ElTorito - Terminate disk emu
274 void
275 cdemu_134b(struct bregs *regs)
276 {
277     // FIXME ElTorito Hardcoded
278     SET_INT13ET(regs, size, 0x13);
279     SET_INT13ET(regs, media, GET_GLOBAL(CDEMU.media));
280     SET_INT13ET(regs, emulated_drive, GET_GLOBAL(CDEMU.emulated_drive));
281     SET_INT13ET(regs, controller_index, GET_GLOBAL(CDEMU.controller_index));
282     SET_INT13ET(regs, ilba, GET_GLOBAL(CDEMU.ilba));
283     SET_INT13ET(regs, device_spec, GET_GLOBAL(CDEMU.device_spec));
284     SET_INT13ET(regs, buffer_segment, GET_GLOBAL(CDEMU.buffer_segment));
285     SET_INT13ET(regs, load_segment, GET_GLOBAL(CDEMU.load_segment));
286     SET_INT13ET(regs, sector_count, GET_GLOBAL(CDEMU.sector_count));
287     SET_INT13ET(regs, cylinders, GET_GLOBAL(CDEMU.vdevice.cylinders));
288     SET_INT13ET(regs, sectors, GET_GLOBAL(CDEMU.vdevice.spt));
289     SET_INT13ET(regs, heads, GET_GLOBAL(CDEMU.vdevice.heads));
290
291     // If we have to terminate emulation
292     if (regs->al == 0x00) {
293         // FIXME ElTorito Various. Should be handled accordingly to spec
294         SET_EBDA(cdemu_active, 0x00); // bye bye
295     }
296
297     disk_ret(regs, DISK_RET_SUCCESS);
298 }
299
300
301 /****************************************************************
302  * CD booting
303  ****************************************************************/
304
305 // Request SENSE
306 static int
307 atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
308 {
309     u8 buffer[18];
310     u8 atacmd[12];
311     memset(atacmd, 0, sizeof(atacmd));
312     atacmd[0] = ATA_CMD_REQUEST_SENSE;
313     atacmd[4] = sizeof(buffer);
314     int ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
315                              , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
316     if (ret != 0)
317         return ret;
318
319     *asc = buffer[12];
320     *ascq = buffer[13];
321
322     return 0;
323 }
324
325 static int
326 atapi_is_ready(u16 device)
327 {
328     if (GET_GLOBAL(ATA.devices[device].type) != ATA_TYPE_ATAPI) {
329         printf("not implemented for non-ATAPI device\n");
330         return -1;
331     }
332
333     dprintf(6, "ata_detect_medium: begin\n");
334     u8 packet[12];
335     memset(packet, 0, sizeof(packet));
336     packet[0] = 0x25; /* READ CAPACITY */
337
338     /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
339      * is reported by the device. If the device reports "IN PROGRESS",
340      * 30 seconds is added. */
341     u8 buf[8];
342     u32 timeout = 5000;
343     u32 time = 0;
344     u8 in_progress = 0;
345     for (;; time+=100) {
346         if (time >= timeout) {
347             dprintf(1, "read capacity failed\n");
348             return -1;
349         }
350         int ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
351                                  , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
352         if (ret == 0)
353             break;
354
355         u8 asc=0, ascq=0;
356         ret = atapi_get_sense(device, &asc, &ascq);
357         if (!ret)
358             continue;
359
360         if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
361             dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
362             return -1;
363         }
364
365         if (asc == 0x04 && ascq == 0x01 && !in_progress) {
366             /* IN PROGRESS OF BECOMING READY */
367             printf("Waiting for device to detect medium... ");
368             /* Allow 30 seconds more */
369             timeout = 30000;
370             in_progress = 1;
371         }
372     }
373
374     u32 block_len = (u32) buf[4] << 24
375         | (u32) buf[5] << 16
376         | (u32) buf[6] << 8
377         | (u32) buf[7] << 0;
378
379     if (block_len != 2048 && block_len != 512) {
380         printf("Unsupported sector size %u\n", block_len);
381         return -1;
382     }
383     SET_GLOBAL(ATA.devices[device].blksize, block_len);
384
385     u32 sectors = (u32) buf[0] << 24
386         | (u32) buf[1] << 16
387         | (u32) buf[2] << 8
388         | (u32) buf[3] << 0;
389
390     dprintf(6, "sectors=%u\n", sectors);
391     if (block_len == 2048)
392         sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
393     if (sectors != GET_GLOBAL(ATA.devices[device].sectors))
394         printf("%dMB medium detected\n", sectors>>(20-9));
395     SET_GLOBAL(ATA.devices[device].sectors, sectors);
396     return 0;
397 }
398
399 static int
400 atapi_is_cdrom(u8 device)
401 {
402     if (device >= CONFIG_MAX_ATA_DEVICES)
403         return 0;
404
405     if (GET_GLOBAL(ATA.devices[device].type) != ATA_TYPE_ATAPI)
406         return 0;
407
408     if (GET_GLOBAL(ATA.devices[device].device) != ATA_DEVICE_CDROM)
409         return 0;
410
411     return 1;
412 }
413
414 // Compare a string on the stack to one in the code segment.
415 static int
416 streq_cs(u8 *s1, char *cs_s2)
417 {
418     u8 *s2 = (u8*)cs_s2;
419     for (;;) {
420         if (*s1 != GET_GLOBAL(*s2))
421             return 0;
422         if (! *s1)
423             return 1;
424         s1++;
425         s2++;
426     }
427 }
428
429 int
430 cdrom_boot()
431 {
432     // Find out the first cdrom
433     u8 device;
434     for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
435         if (atapi_is_cdrom(device))
436             break;
437     if (device >= CONFIG_MAX_ATA_DEVICES)
438         // cdrom not found
439         return 2;
440
441     int ret = atapi_is_ready(device);
442     if (ret)
443         dprintf(1, "atapi_is_ready returned %d\n", ret);
444
445     // Read the Boot Record Volume Descriptor
446     u8 buffer[2048];
447     ret = cdrom_read(device, 0x11, 1
448                      , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
449     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
462     ret = cdrom_read(device, lba, 1
463                      , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
464     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
481     u8 media = buffer[0x21];
482     SET_GLOBAL(CDEMU.media, media);
483
484     SET_GLOBAL(CDEMU.controller_index, device/2);
485     SET_GLOBAL(CDEMU.device_spec, device%2);
486
487     u16 boot_segment = *(u16*)&buffer[0x22];
488     if (!boot_segment)
489         boot_segment = 0x07C0;
490     SET_GLOBAL(CDEMU.load_segment, boot_segment);
491     SET_GLOBAL(CDEMU.buffer_segment, 0x0000);
492
493     u16 nbsectors = *(u16*)&buffer[0x26];
494     SET_GLOBAL(CDEMU.sector_count, nbsectors);
495
496     lba = *(u32*)&buffer[0x28];
497     SET_GLOBAL(CDEMU.ilba, lba);
498
499     // And we read the image in memory
500     ret = cdrom_read_emu(device, 0, nbsectors, MAKE_FARPTR(boot_segment, 0));
501     if (ret)
502         return 12;
503
504     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_GLOBAL(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_GLOBAL(CDEMU.emulated_drive, 0x00);
523         SETBITS_BDA(equipment_list_flags, 0x41);
524     } else {
525         // Harddrive emulation
526         SET_GLOBAL(CDEMU.emulated_drive, 0x80);
527         SET_GLOBAL(ATA.hdcount, GET_GLOBAL(ATA.hdcount) + 1);
528     }
529
530     // Remember the media type
531     switch (media) {
532     case 0x01:  // 1.2M floppy
533         SET_GLOBAL(CDEMU.vdevice.spt, 15);
534         SET_GLOBAL(CDEMU.vdevice.cylinders, 80);
535         SET_GLOBAL(CDEMU.vdevice.heads, 2);
536         break;
537     case 0x02:  // 1.44M floppy
538         SET_GLOBAL(CDEMU.vdevice.spt, 18);
539         SET_GLOBAL(CDEMU.vdevice.cylinders, 80);
540         SET_GLOBAL(CDEMU.vdevice.heads, 2);
541         break;
542     case 0x03:  // 2.88M floppy
543         SET_GLOBAL(CDEMU.vdevice.spt, 36);
544         SET_GLOBAL(CDEMU.vdevice.cylinders, 80);
545         SET_GLOBAL(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_GLOBAL(CDEMU.vdevice.spt, spt & 0x3f);
552         SET_GLOBAL(CDEMU.vdevice.cylinders, cyl);
553         SET_GLOBAL(CDEMU.vdevice.heads, heads);
554         break;
555     }
556     }
557
558     // everything is ok, so from now on, the emulation is active
559     SET_EBDA(cdemu_active, 0x01);
560     dprintf(6, "cdemu media=%d\n", media);
561
562     return 0;
563 }