349e401bbd65346bcc3a3291906df47477f7fcdb
[seabios.git] / src / cdrom.c
1 // Support for booting from cdroms (the "El Torito" spec).
2 //
3 // Copyright (C) 2008,2009  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 LGPLv3 license.
7
8 #include "disk.h" // cdrom_13
9 #include "util.h" // memset
10 #include "bregs.h" // struct bregs
11 #include "biosvar.h" // GET_EBDA
12 #include "ata.h" // ATA_CMD_REQUEST_SENSE
13 #include "blockcmd.h" // CDB_CMD_REQUEST_SENSE
14
15
16 /****************************************************************
17  * CD emulation
18  ****************************************************************/
19
20 struct drive_s *cdemu_drive_gf VAR16VISIBLE;
21 u8 *cdemu_buf_fl VAR16VISIBLE;
22
23 static int
24 cdemu_read(struct disk_op_s *op)
25 {
26     u16 ebda_seg = get_ebda_seg();
27     struct drive_s *drive_g;
28     drive_g = GLOBALFLAT2GLOBAL(GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf));
29     struct disk_op_s dop;
30     dop.drive_g = drive_g;
31     dop.command = op->command;
32     dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + op->lba / 4;
33
34     int count = op->count;
35     op->count = 0;
36     u8 *cdbuf_fl = GET_GLOBAL(cdemu_buf_fl);
37
38     if (op->lba & 3) {
39         // Partial read of first block.
40         dop.count = 1;
41         dop.buf_fl = cdbuf_fl;
42         int ret = process_op(&dop);
43         if (ret)
44             return ret;
45         u8 thiscount = 4 - (op->lba & 3);
46         if (thiscount > count)
47             thiscount = count;
48         count -= thiscount;
49         memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
50         op->buf_fl += thiscount * 512;
51         op->count += thiscount;
52         dop.lba++;
53     }
54
55     if (count > 3) {
56         // Read n number of regular blocks.
57         dop.count = count / 4;
58         dop.buf_fl = op->buf_fl;
59         int ret = process_op(&dop);
60         op->count += dop.count * 4;
61         if (ret)
62             return ret;
63         u8 thiscount = count & ~3;
64         count &= 3;
65         op->buf_fl += thiscount * 512;
66         dop.lba += thiscount / 4;
67     }
68
69     if (count) {
70         // Partial read on last block.
71         dop.count = 1;
72         dop.buf_fl = cdbuf_fl;
73         int ret = process_op(&dop);
74         if (ret)
75             return ret;
76         u8 thiscount = count;
77         memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
78         op->count += thiscount;
79     }
80
81     return DISK_RET_SUCCESS;
82 }
83
84 int
85 process_cdemu_op(struct disk_op_s *op)
86 {
87     if (!CONFIG_CDROM_EMU)
88         return 0;
89
90     switch (op->command) {
91     case CMD_READ:
92         return cdemu_read(op);
93     case CMD_WRITE:
94     case CMD_FORMAT:
95         return DISK_RET_EWRITEPROTECT;
96     case CMD_VERIFY:
97     case CMD_RESET:
98     case CMD_SEEK:
99     case CMD_ISREADY:
100         return DISK_RET_SUCCESS;
101     default:
102         op->count = 0;
103         return DISK_RET_EPARAM;
104     }
105 }
106
107 void
108 cdemu_setup(void)
109 {
110     if (!CONFIG_CDROM_EMU)
111         return;
112     if (!Drives.cdcount)
113         return;
114
115     struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
116     u8 *buf = malloc_low(CDROM_SECTOR_SIZE);
117     if (!drive_g || !buf) {
118         warn_noalloc();
119         free(drive_g);
120         free(buf);
121         return;
122     }
123     cdemu_drive_gf = drive_g;
124     cdemu_buf_fl = buf;
125     memset(drive_g, 0, sizeof(*drive_g));
126     drive_g->type = DTYPE_CDEMU;
127     drive_g->blksize = DISK_SECTOR_SIZE;
128     drive_g->sectors = (u64)-1;
129 }
130
131 struct eltorito_s {
132     u8 size;
133     u8 media;
134     u8 emulated_drive;
135     u8 controller_index;
136     u32 ilba;
137     u16 device_spec;
138     u16 buffer_segment;
139     u16 load_segment;
140     u16 sector_count;
141     u8 cylinders;
142     u8 sectors;
143     u8 heads;
144 };
145
146 #define SET_INT13ET(regs,var,val)                                      \
147     SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
148
149 // ElTorito - Terminate disk emu
150 void
151 cdemu_134b(struct bregs *regs)
152 {
153     // FIXME ElTorito Hardcoded
154     u16 ebda_seg = get_ebda_seg();
155     SET_INT13ET(regs, size, 0x13);
156     SET_INT13ET(regs, media, GET_EBDA2(ebda_seg, cdemu.media));
157     SET_INT13ET(regs, emulated_drive
158                 , GET_EBDA2(ebda_seg, cdemu.emulated_extdrive));
159     struct drive_s *drive_gf = GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf);
160     u8 cntl_id = 0;
161     if (drive_gf)
162         cntl_id = GET_GLOBALFLAT(drive_gf->cntl_id);
163     SET_INT13ET(regs, controller_index, cntl_id / 2);
164     SET_INT13ET(regs, device_spec, cntl_id % 2);
165     SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
166     SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
167     SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
168     SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
169     SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
170     SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
171     SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
172
173     // If we have to terminate emulation
174     if (regs->al == 0x00) {
175         // FIXME ElTorito Various. Should be handled accordingly to spec
176         SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
177
178         // XXX - update floppy/hd count.
179     }
180
181     disk_ret(regs, DISK_RET_SUCCESS);
182 }
183
184
185 /****************************************************************
186  * CD booting
187  ****************************************************************/
188
189 static int
190 atapi_is_ready(struct disk_op_s *op)
191 {
192     dprintf(6, "atapi_is_ready (drive=%p)\n", op->drive_g);
193
194     /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT is
195      * reported by the device.  If the device reports "IN PROGRESS",
196      * 30 seconds is added. */
197     struct cdbres_read_capacity info;
198     int in_progress = 0;
199     u64 end = calc_future_tsc(5000);
200     for (;;) {
201         if (check_tsc(end)) {
202             dprintf(1, "read capacity failed\n");
203             return -1;
204         }
205
206         int ret = cdb_read_capacity(op, &info);
207         if (!ret)
208             // Success
209             break;
210
211         struct cdbres_request_sense sense;
212         ret = cdb_get_sense(op, &sense);
213         if (ret)
214             // Error - retry.
215             continue;
216
217         // Sense succeeded.
218         if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
219             dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
220             return -1;
221         }
222
223         if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
224             /* IN PROGRESS OF BECOMING READY */
225             printf("Waiting for device to detect medium... ");
226             /* Allow 30 seconds more */
227             end = calc_future_tsc(30000);
228             in_progress = 1;
229         }
230     }
231
232     u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
233     if (blksize != GET_GLOBAL(op->drive_g->blksize)) {
234         printf("Unsupported sector size %u\n", blksize);
235         return -1;
236     }
237
238     dprintf(6, "sectors=%u\n", sectors);
239     printf("%dMB medium detected\n", sectors>>(20-11));
240     return 0;
241 }
242
243 int
244 cdrom_boot(int cdid)
245 {
246     struct disk_op_s dop;
247     memset(&dop, 0, sizeof(dop));
248     dop.drive_g = getDrive(EXTTYPE_CD, cdid);
249     if (!dop.drive_g)
250         return 1;
251
252     int ret = atapi_is_ready(&dop);
253     if (ret)
254         dprintf(1, "atapi_is_ready returned %d\n", ret);
255
256     // Read the Boot Record Volume Descriptor
257     u8 buffer[2048];
258     dop.lba = 0x11;
259     dop.count = 1;
260     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
261     ret = cdb_read(&dop);
262     if (ret)
263         return 3;
264
265     // Validity checks
266     if (buffer[0])
267         return 4;
268     if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
269         return 5;
270
271     // ok, now we calculate the Boot catalog address
272     u32 lba = *(u32*)&buffer[0x47];
273
274     // And we read the Boot Catalog
275     dop.lba = lba;
276     dop.count = 1;
277     ret = cdb_read(&dop);
278     if (ret)
279         return 7;
280
281     // Validation entry
282     if (buffer[0x00] != 0x01)
283         return 8;   // Header
284     if (buffer[0x01] != 0x00)
285         return 9;   // Platform
286     if (buffer[0x1E] != 0x55)
287         return 10;  // key 1
288     if (buffer[0x1F] != 0xAA)
289         return 10;  // key 2
290
291     // Initial/Default Entry
292     if (buffer[0x20] != 0x88)
293         return 11; // Bootable
294
295     u16 ebda_seg = get_ebda_seg();
296     u8 media = buffer[0x21];
297     SET_EBDA2(ebda_seg, cdemu.media, media);
298
299     SET_EBDA2(ebda_seg, cdemu.emulated_drive_gf, dop.drive_g);
300
301     u16 boot_segment = *(u16*)&buffer[0x22];
302     if (!boot_segment)
303         boot_segment = 0x07C0;
304     SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
305     SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
306
307     u16 nbsectors = *(u16*)&buffer[0x26];
308     SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
309
310     lba = *(u32*)&buffer[0x28];
311     SET_EBDA2(ebda_seg, cdemu.ilba, lba);
312
313     // And we read the image in memory
314     dop.lba = lba;
315     dop.count = DIV_ROUND_UP(nbsectors, 4);
316     dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
317     ret = cdb_read(&dop);
318     if (ret)
319         return 12;
320
321     if (media == 0) {
322         // No emulation requested - return success.
323         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, EXTSTART_CD + cdid);
324         return 0;
325     }
326
327     // Emulation of a floppy/harddisk requested
328     if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
329         return 13;
330
331     // Set emulated drive id and increase bios installed hardware
332     // number of devices
333     if (media < 4) {
334         // Floppy emulation
335         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
336         // XXX - get and set actual floppy count.
337         SETBITS_BDA(equipment_list_flags, 0x41);
338
339         switch (media) {
340         case 0x01:  // 1.2M floppy
341             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
342             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
343             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
344             break;
345         case 0x02:  // 1.44M floppy
346             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
347             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
348             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
349             break;
350         case 0x03:  // 2.88M floppy
351             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
352             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
353             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
354             break;
355         }
356     } else {
357         // Harddrive emulation
358         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
359         SET_BDA(hdcount, GET_BDA(hdcount) + 1);
360
361         // Peak at partition table to get chs.
362         struct mbr_s *mbr = (void*)0;
363         u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
364         u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
365         u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
366
367         SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
368         SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
369                   , ((sptcyl<<2)&0x300) + cyllow + 1);
370         SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
371     }
372
373     // everything is ok, so from now on, the emulation is active
374     SET_EBDA2(ebda_seg, cdemu.active, 0x01);
375     dprintf(6, "cdemu media=%d\n", media);
376
377     return 0;
378 }