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 ****************************************************************/
21 cdemu_read(struct disk_op_s *op)
23 u16 ebda_seg = get_ebda_seg();
24 struct drive_s *drive_g = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
26 dop.drive_g = drive_g;
27 dop.command = op->command;
28 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + op->lba / 4;
30 int count = op->count;
32 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
35 // Partial read of first block.
37 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
38 int ret = process_op(&dop);
41 u8 thiscount = 4 - (op->lba & 3);
42 if (thiscount > count)
45 memcpy_far(FLATPTR_TO_SEG(op->buf_fl)
46 , (void*)FLATPTR_TO_OFFSET(op->buf_fl)
47 , ebda_seg, cdbuf_far + (op->lba & 3) * 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 = MAKE_FLATPTR(ebda_seg, cdbuf_far);
72 int ret = process_op(&dop);
76 memcpy_far(FLATPTR_TO_SEG(op->buf_fl)
77 , (void*)FLATPTR_TO_OFFSET(op->buf_fl)
78 , ebda_seg, cdbuf_far, thiscount * 512);
79 op->count += thiscount;
82 return DISK_RET_SUCCESS;
86 process_cdemu_op(struct disk_op_s *op)
88 if (!CONFIG_CDROM_EMU)
91 switch (op->command) {
93 return cdemu_read(op);
96 return DISK_RET_EWRITEPROTECT;
101 return DISK_RET_SUCCESS;
104 return DISK_RET_EPARAM;
108 struct drive_s *cdemu_drive VAR16VISIBLE;
113 if (!CONFIG_CDROM_EMU)
116 struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
122 memset(drive_g, 0, sizeof(*drive_g));
123 cdemu_drive = STORE_GLOBAL_PTR(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_g = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
158 u8 cntl_id = GET_GLOBAL(drive_g->cntl_id);
159 SET_INT13ET(regs, controller_index, cntl_id / 2);
160 SET_INT13ET(regs, device_spec, cntl_id % 2);
161 SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
162 SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
163 SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
164 SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
165 SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
166 SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
167 SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
169 // If we have to terminate emulation
170 if (regs->al == 0x00) {
171 // FIXME ElTorito Various. Should be handled accordingly to spec
172 SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
174 // XXX - update floppy/hd count.
177 disk_ret(regs, DISK_RET_SUCCESS);
181 /****************************************************************
183 ****************************************************************/
186 atapi_is_ready(struct disk_op_s *op)
188 dprintf(6, "atapi_is_ready (drive=%p)\n", op->drive_g);
190 /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT is
191 * reported by the device. If the device reports "IN PROGRESS",
192 * 30 seconds is added. */
193 struct cdbres_read_capacity info;
195 u64 end = calc_future_tsc(5000);
197 if (check_time(end)) {
198 dprintf(1, "read capacity failed\n");
202 int ret = cdb_read_capacity(op, &info);
207 struct cdbres_request_sense sense;
208 ret = cdb_get_sense(op, &sense);
214 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
215 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
219 if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
220 /* IN PROGRESS OF BECOMING READY */
221 printf("Waiting for device to detect medium... ");
222 /* Allow 30 seconds more */
223 end = calc_future_tsc(30000);
228 u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
229 if (blksize != GET_GLOBAL(op->drive_g->blksize)) {
230 printf("Unsupported sector size %u\n", blksize);
234 dprintf(6, "sectors=%u\n", sectors);
235 printf("%dMB medium detected\n", sectors>>(20-11));
242 struct disk_op_s dop;
243 memset(&dop, 0, sizeof(dop));
244 dop.drive_g = getDrive(EXTTYPE_CD, cdid);
248 int ret = atapi_is_ready(&dop);
250 dprintf(1, "atapi_is_ready returned %d\n", ret);
252 // Read the Boot Record Volume Descriptor
256 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
257 ret = cdb_read(&dop);
264 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
267 // ok, now we calculate the Boot catalog address
268 u32 lba = *(u32*)&buffer[0x47];
270 // And we read the Boot Catalog
273 ret = cdb_read(&dop);
278 if (buffer[0x00] != 0x01)
280 if (buffer[0x01] != 0x00)
281 return 9; // Platform
282 if (buffer[0x1E] != 0x55)
284 if (buffer[0x1F] != 0xAA)
287 // Initial/Default Entry
288 if (buffer[0x20] != 0x88)
289 return 11; // Bootable
291 u16 ebda_seg = get_ebda_seg();
292 u8 media = buffer[0x21];
293 SET_EBDA2(ebda_seg, cdemu.media, media);
295 SET_EBDA2(ebda_seg, cdemu.emulated_drive, STORE_GLOBAL_PTR(dop.drive_g));
297 u16 boot_segment = *(u16*)&buffer[0x22];
299 boot_segment = 0x07C0;
300 SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
301 SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
303 u16 nbsectors = *(u16*)&buffer[0x26];
304 SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
306 lba = *(u32*)&buffer[0x28];
307 SET_EBDA2(ebda_seg, cdemu.ilba, lba);
309 // And we read the image in memory
311 dop.count = DIV_ROUND_UP(nbsectors, 4);
312 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
313 ret = cdb_read(&dop);
318 // No emulation requested - return success.
319 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, EXTSTART_CD + cdid);
323 // Emulation of a floppy/harddisk requested
324 if (! CONFIG_CDROM_EMU || !cdemu_drive)
327 // Set emulated drive id and increase bios installed hardware
331 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
332 // XXX - get and set actual floppy count.
333 SETBITS_BDA(equipment_list_flags, 0x41);
336 case 0x01: // 1.2M floppy
337 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
338 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
339 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
341 case 0x02: // 1.44M floppy
342 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
343 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
344 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
346 case 0x03: // 2.88M floppy
347 SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
348 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
349 SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
353 // Harddrive emulation
354 SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
355 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
357 // Peak at partition table to get chs.
358 struct mbr_s *mbr = (void*)0;
359 u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
360 u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
361 u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
363 SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
364 SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
365 , ((sptcyl<<2)&0x300) + cyllow + 1);
366 SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
369 // everything is ok, so from now on, the emulation is active
370 SET_EBDA2(ebda_seg, cdemu.active, 0x01);
371 dprintf(6, "cdemu media=%d\n", media);