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;
23 cdemu_read(struct disk_op_s *op)
25 u16 ebda_seg = get_ebda_seg();
26 struct drive_s *drive_g;
27 drive_g = GLOBALFLAT2GLOBAL(GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf));
29 dop.drive_g = drive_g;
30 dop.command = op->command;
31 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + op->lba / 4;
33 int count = op->count;
35 u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
38 // Partial read of first block.
40 dop.buf_fl = cdbuf_fl;
41 int ret = process_op(&dop);
44 u8 thiscount = 4 - (op->lba & 3);
45 if (thiscount > count)
48 memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
49 op->buf_fl += thiscount * 512;
50 op->count += thiscount;
55 // Read n number of regular blocks.
56 dop.count = count / 4;
57 dop.buf_fl = op->buf_fl;
58 int ret = process_op(&dop);
59 op->count += dop.count * 4;
62 u8 thiscount = count & ~3;
64 op->buf_fl += thiscount * 512;
65 dop.lba += thiscount / 4;
69 // Partial read on last block.
71 dop.buf_fl = cdbuf_fl;
72 int ret = process_op(&dop);
76 memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
77 op->count += thiscount;
80 return DISK_RET_SUCCESS;
84 process_cdemu_op(struct disk_op_s *op)
86 if (!CONFIG_CDROM_EMU)
89 switch (op->command) {
91 return cdemu_read(op);
94 return DISK_RET_EWRITEPROTECT;
99 return DISK_RET_SUCCESS;
102 return DISK_RET_EPARAM;
109 if (!CONFIG_CDROM_EMU)
113 if (bounce_buf_init() < 0)
116 struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
122 cdemu_drive_gf = drive_g;
123 memset(drive_g, 0, sizeof(*drive_g));
124 drive_g->type = DTYPE_CDEMU;
125 drive_g->blksize = DISK_SECTOR_SIZE;
126 drive_g->sectors = (u64)-1;
144 #define SET_INT13ET(regs,var,val) \
145 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
147 // ElTorito - Terminate disk emu
149 cdemu_134b(struct bregs *regs)
151 // FIXME ElTorito Hardcoded
152 u16 ebda_seg = get_ebda_seg();
153 SET_INT13ET(regs, size, 0x13);
154 SET_INT13ET(regs, media, GET_EBDA2(ebda_seg, cdemu.media));
155 SET_INT13ET(regs, emulated_drive
156 , GET_EBDA2(ebda_seg, cdemu.emulated_extdrive));
157 struct drive_s *drive_gf = GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf);
160 cntl_id = GET_GLOBALFLAT(drive_gf->cntl_id);
161 SET_INT13ET(regs, controller_index, cntl_id / 2);
162 SET_INT13ET(regs, device_spec, cntl_id % 2);
163 SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
164 SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
165 SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
166 SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
167 SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
168 SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
169 SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
171 // If we have to terminate emulation
172 if (regs->al == 0x00) {
173 // FIXME ElTorito Various. Should be handled accordingly to spec
174 SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
176 // XXX - update floppy/hd count.
179 disk_ret(regs, DISK_RET_SUCCESS);
183 /****************************************************************
185 ****************************************************************/
188 atapi_is_ready(struct disk_op_s *op)
190 dprintf(6, "atapi_is_ready (drive=%p)\n", op->drive_g);
192 /* Retry TEST UNIT READY for 5 seconds unless MEDIUM NOT PRESENT is
193 * reported by the device. If the device reports "IN PROGRESS",
194 * 30 seconds is added. */
196 u64 end = calc_future_tsc(5000);
198 if (check_tsc(end)) {
199 dprintf(1, "test unit ready failed\n");
203 int ret = cdb_test_unit_ready(op);
208 struct cdbres_request_sense sense;
209 ret = cdb_get_sense(op, &sense);
215 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
216 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
220 if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
221 /* IN PROGRESS OF BECOMING READY */
222 printf("Waiting for device to detect medium... ");
223 /* Allow 30 seconds more */
224 end = calc_future_tsc(30000);
232 cdrom_boot(struct drive_s *drive_g)
234 struct disk_op_s dop;
235 int cdid = getDriveId(EXTTYPE_CD, drive_g);
236 memset(&dop, 0, sizeof(dop));
237 dop.drive_g = drive_g;
238 if (!dop.drive_g || cdid < 0)
241 int ret = atapi_is_ready(&dop);
243 dprintf(1, "atapi_is_ready returned %d\n", ret);
245 // Read the Boot Record Volume Descriptor
249 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
250 ret = cdb_read(&dop);
257 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
260 // ok, now we calculate the Boot catalog address
261 u32 lba = *(u32*)&buffer[0x47];
263 // And we read the Boot Catalog
266 ret = cdb_read(&dop);
271 if (buffer[0x00] != 0x01)
273 if (buffer[0x01] != 0x00)
274 return 9; // Platform
275 if (buffer[0x1E] != 0x55)
277 if (buffer[0x1F] != 0xAA)
280 // Initial/Default Entry
281 if (buffer[0x20] != 0x88)
282 return 11; // Bootable
284 u16 ebda_seg = get_ebda_seg();
285 u8 media = buffer[0x21];
286 SET_EBDA2(ebda_seg, cdemu.media, media);
288 SET_EBDA2(ebda_seg, cdemu.emulated_drive_gf, dop.drive_g);
290 u16 boot_segment = *(u16*)&buffer[0x22];
292 boot_segment = 0x07C0;
293 SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
294 SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
296 u16 nbsectors = *(u16*)&buffer[0x26];
297 SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
299 lba = *(u32*)&buffer[0x28];
300 SET_EBDA2(ebda_seg, cdemu.ilba, lba);
302 // And we read the image in memory
304 dop.count = DIV_ROUND_UP(nbsectors, 4);
305 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
306 ret = cdb_read(&dop);
311 // No emulation requested - return success.
312 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, EXTSTART_CD + cdid);
316 // Emulation of a floppy/harddisk requested
317 if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
320 // Set emulated drive id and increase bios installed hardware
324 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
325 // XXX - get and set actual floppy count.
326 SETBITS_BDA(equipment_list_flags, 0x41);
329 case 0x01: // 1.2M floppy
330 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
331 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
332 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
334 case 0x02: // 1.44M floppy
335 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
336 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
337 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
339 case 0x03: // 2.88M floppy
340 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
341 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
342 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
346 // Harddrive emulation
347 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
348 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
350 // Peak at partition table to get chs.
351 struct mbr_s *mbr = (void*)0;
352 u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
353 u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
354 u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
356 SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
357 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
358 , ((sptcyl<<2)&0x300) + cyllow + 1);
359 SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
362 // everything is ok, so from now on, the emulation is active
363 SET_EBDA2(ebda_seg, cdemu.active, 0x01);
364 dprintf(6, "cdemu media=%d\n", media);