1 // Support for booting from cdroms (the "El Torito" spec).
3 // Copyright (C) 2008,2009 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 LGPLv3 license.
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
16 /****************************************************************
18 ****************************************************************/
20 struct drive_s *cdemu_drive_gf VAR16VISIBLE;
21 u8 *cdemu_buf_fl VAR16VISIBLE;
24 cdemu_read(struct disk_op_s *op)
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));
30 dop.drive_g = drive_g;
31 dop.command = op->command;
32 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + op->lba / 4;
34 int count = op->count;
36 u8 *cdbuf_fl = GET_GLOBAL(cdemu_buf_fl);
39 // Partial read of first block.
41 dop.buf_fl = cdbuf_fl;
42 int ret = process_op(&dop);
45 u8 thiscount = 4 - (op->lba & 3);
46 if (thiscount > count)
49 memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
50 op->buf_fl += thiscount * 512;
51 op->count += thiscount;
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;
63 u8 thiscount = count & ~3;
65 op->buf_fl += thiscount * 512;
66 dop.lba += thiscount / 4;
70 // Partial read on last block.
72 dop.buf_fl = cdbuf_fl;
73 int ret = process_op(&dop);
77 memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
78 op->count += thiscount;
81 return DISK_RET_SUCCESS;
85 process_cdemu_op(struct disk_op_s *op)
87 if (!CONFIG_CDROM_EMU)
90 switch (op->command) {
92 return cdemu_read(op);
95 return DISK_RET_EWRITEPROTECT;
100 return DISK_RET_SUCCESS;
103 return DISK_RET_EPARAM;
110 if (!CONFIG_CDROM_EMU)
112 cdemu_drive_gf = NULL;
117 struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
118 u8 *buf = malloc_low(CDROM_SECTOR_SIZE);
119 if (!drive_g || !buf) {
125 cdemu_drive_gf = drive_g;
127 memset(drive_g, 0, sizeof(*drive_g));
128 drive_g->type = DTYPE_CDEMU;
129 drive_g->blksize = DISK_SECTOR_SIZE;
130 drive_g->sectors = (u64)-1;
148 #define SET_INT13ET(regs,var,val) \
149 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
151 // ElTorito - Terminate disk emu
153 cdemu_134b(struct bregs *regs)
155 // FIXME ElTorito Hardcoded
156 u16 ebda_seg = get_ebda_seg();
157 SET_INT13ET(regs, size, 0x13);
158 SET_INT13ET(regs, media, GET_EBDA2(ebda_seg, cdemu.media));
159 SET_INT13ET(regs, emulated_drive
160 , GET_EBDA2(ebda_seg, cdemu.emulated_extdrive));
161 struct drive_s *drive_gf = GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf);
164 cntl_id = GET_GLOBALFLAT(drive_gf->cntl_id);
165 SET_INT13ET(regs, controller_index, cntl_id / 2);
166 SET_INT13ET(regs, device_spec, cntl_id % 2);
167 SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
168 SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
169 SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
170 SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
171 SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
172 SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
173 SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
175 // If we have to terminate emulation
176 if (regs->al == 0x00) {
177 // FIXME ElTorito Various. Should be handled accordingly to spec
178 SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
180 // XXX - update floppy/hd count.
183 disk_ret(regs, DISK_RET_SUCCESS);
187 /****************************************************************
189 ****************************************************************/
192 atapi_is_ready(struct disk_op_s *op)
194 dprintf(6, "atapi_is_ready (drive=%p)\n", op->drive_g);
196 /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT is
197 * reported by the device. If the device reports "IN PROGRESS",
198 * 30 seconds is added. */
199 struct cdbres_read_capacity info;
201 u64 end = calc_future_tsc(5000);
203 if (check_tsc(end)) {
204 dprintf(1, "read capacity failed\n");
208 int ret = cdb_read_capacity(op, &info);
213 struct cdbres_request_sense sense;
214 ret = cdb_get_sense(op, &sense);
220 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
221 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
225 if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
226 /* IN PROGRESS OF BECOMING READY */
227 printf("Waiting for device to detect medium... ");
228 /* Allow 30 seconds more */
229 end = calc_future_tsc(30000);
234 u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
235 if (blksize != GET_GLOBAL(op->drive_g->blksize)) {
236 printf("Unsupported sector size %u\n", blksize);
240 dprintf(6, "sectors=%u\n", sectors);
241 printf("%dMB medium detected\n", sectors>>(20-11));
248 struct disk_op_s dop;
249 memset(&dop, 0, sizeof(dop));
250 dop.drive_g = getDrive(EXTTYPE_CD, cdid);
254 int ret = atapi_is_ready(&dop);
256 dprintf(1, "atapi_is_ready returned %d\n", ret);
258 // Read the Boot Record Volume Descriptor
262 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
263 ret = cdb_read(&dop);
270 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
273 // ok, now we calculate the Boot catalog address
274 u32 lba = *(u32*)&buffer[0x47];
276 // And we read the Boot Catalog
279 ret = cdb_read(&dop);
284 if (buffer[0x00] != 0x01)
286 if (buffer[0x01] != 0x00)
287 return 9; // Platform
288 if (buffer[0x1E] != 0x55)
290 if (buffer[0x1F] != 0xAA)
293 // Initial/Default Entry
294 if (buffer[0x20] != 0x88)
295 return 11; // Bootable
297 u16 ebda_seg = get_ebda_seg();
298 u8 media = buffer[0x21];
299 SET_EBDA2(ebda_seg, cdemu.media, media);
301 SET_EBDA2(ebda_seg, cdemu.emulated_drive_gf, dop.drive_g);
303 u16 boot_segment = *(u16*)&buffer[0x22];
305 boot_segment = 0x07C0;
306 SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
307 SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
309 u16 nbsectors = *(u16*)&buffer[0x26];
310 SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
312 lba = *(u32*)&buffer[0x28];
313 SET_EBDA2(ebda_seg, cdemu.ilba, lba);
315 // And we read the image in memory
317 dop.count = DIV_ROUND_UP(nbsectors, 4);
318 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
319 ret = cdb_read(&dop);
324 // No emulation requested - return success.
325 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, EXTSTART_CD + cdid);
329 // Emulation of a floppy/harddisk requested
330 if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
333 // Set emulated drive id and increase bios installed hardware
337 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
338 // XXX - get and set actual floppy count.
339 SETBITS_BDA(equipment_list_flags, 0x41);
342 case 0x01: // 1.2M floppy
343 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
344 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
345 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
347 case 0x02: // 1.44M floppy
348 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
349 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
350 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
352 case 0x03: // 2.88M floppy
353 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
354 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
355 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
359 // Harddrive emulation
360 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
361 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
363 // Peak at partition table to get chs.
364 struct mbr_s *mbr = (void*)0;
365 u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
366 u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
367 u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
369 SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
370 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
371 , ((sptcyl<<2)&0x300) + cyllow + 1);
372 SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
375 // everything is ok, so from now on, the emulation is active
376 SET_EBDA2(ebda_seg, cdemu.active, 0x01);
377 dprintf(6, "cdemu media=%d\n", media);