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_GLOBAL
12 #include "ata.h" // ATA_CMD_REQUEST_SENSE
13 #include "blockcmd.h" // CDB_CMD_REQUEST_SENSE
15 // Locks for removable devices
16 u8 CDRom_locks[CONFIG_MAX_EXTDRIVE] VARLOW;
19 /****************************************************************
21 ****************************************************************/
23 struct cdemu_s CDEmu VARLOW;
24 struct drive_s *cdemu_drive_gf VAR16VISIBLE;
27 cdemu_read(struct disk_op_s *op)
29 struct drive_s *drive_g;
30 drive_g = GLOBALFLAT2GLOBAL(GET_LOW(CDEmu.emulated_drive_gf));
32 dop.drive_g = drive_g;
33 dop.command = op->command;
34 dop.lba = GET_LOW(CDEmu.ilba) + op->lba / 4;
36 int count = op->count;
38 u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
41 // Partial read of first block.
43 dop.buf_fl = cdbuf_fl;
44 int ret = process_op(&dop);
47 u8 thiscount = 4 - (op->lba & 3);
48 if (thiscount > count)
51 memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
52 op->buf_fl += thiscount * 512;
53 op->count += thiscount;
58 // Read n number of regular blocks.
59 dop.count = count / 4;
60 dop.buf_fl = op->buf_fl;
61 int ret = process_op(&dop);
62 op->count += dop.count * 4;
65 u8 thiscount = count & ~3;
67 op->buf_fl += thiscount * 512;
68 dop.lba += thiscount / 4;
72 // Partial read on last block.
74 dop.buf_fl = cdbuf_fl;
75 int ret = process_op(&dop);
79 memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
80 op->count += thiscount;
83 return DISK_RET_SUCCESS;
87 process_cdemu_op(struct disk_op_s *op)
89 if (!CONFIG_CDROM_EMU)
92 switch (op->command) {
94 return cdemu_read(op);
97 return DISK_RET_EWRITEPROTECT;
102 return DISK_RET_SUCCESS;
105 return DISK_RET_EPARAM;
112 if (!CONFIG_CDROM_EMU)
116 if (bounce_buf_init() < 0)
119 struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
125 cdemu_drive_gf = drive_g;
126 memset(drive_g, 0, sizeof(*drive_g));
127 drive_g->type = DTYPE_CDEMU;
128 drive_g->blksize = DISK_SECTOR_SIZE;
129 drive_g->sectors = (u64)-1;
147 #define SET_INT13ET(regs,var,val) \
148 SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
150 // ElTorito - Terminate disk emu
152 cdemu_134b(struct bregs *regs)
154 // FIXME ElTorito Hardcoded
155 SET_INT13ET(regs, size, 0x13);
156 SET_INT13ET(regs, media, GET_LOW(CDEmu.media));
157 SET_INT13ET(regs, emulated_drive, GET_LOW(CDEmu.emulated_extdrive));
158 struct drive_s *drive_gf = GET_LOW(CDEmu.emulated_drive_gf);
161 cntl_id = GET_GLOBALFLAT(drive_gf->cntl_id);
162 SET_INT13ET(regs, controller_index, cntl_id / 2);
163 SET_INT13ET(regs, device_spec, cntl_id % 2);
164 SET_INT13ET(regs, ilba, GET_LOW(CDEmu.ilba));
165 SET_INT13ET(regs, buffer_segment, GET_LOW(CDEmu.buffer_segment));
166 SET_INT13ET(regs, load_segment, GET_LOW(CDEmu.load_segment));
167 SET_INT13ET(regs, sector_count, GET_LOW(CDEmu.sector_count));
168 SET_INT13ET(regs, cylinders, GET_LOW(CDEmu.lchs.cylinders));
169 SET_INT13ET(regs, sectors, GET_LOW(CDEmu.lchs.spt));
170 SET_INT13ET(regs, heads, GET_LOW(CDEmu.lchs.heads));
172 // If we have to terminate emulation
173 if (regs->al == 0x00) {
174 // FIXME ElTorito Various. Should be handled accordingly to spec
175 SET_LOW(CDEmu.active, 0x00); // bye bye
177 // XXX - update floppy/hd count.
180 disk_ret(regs, DISK_RET_SUCCESS);
184 /****************************************************************
186 ****************************************************************/
189 cdrom_boot(struct drive_s *drive_g)
192 struct disk_op_s dop;
193 int cdid = getDriveId(EXTTYPE_CD, drive_g);
194 memset(&dop, 0, sizeof(dop));
195 dop.drive_g = drive_g;
196 if (!dop.drive_g || cdid < 0)
199 int ret = scsi_is_ready(&dop);
201 dprintf(1, "scsi_is_ready returned %d\n", ret);
203 // Read the Boot Record Volume Descriptor
204 u8 buffer[CDROM_SECTOR_SIZE];
208 ret = cdb_read(&dop);
215 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
218 // ok, now we calculate the Boot catalog address
219 u32 lba = *(u32*)&buffer[0x47];
221 // And we read the Boot Catalog
224 ret = cdb_read(&dop);
229 if (buffer[0x00] != 0x01)
231 if (buffer[0x01] != 0x00)
232 return 9; // Platform
233 if (buffer[0x1E] != 0x55)
235 if (buffer[0x1F] != 0xAA)
238 // Initial/Default Entry
239 if (buffer[0x20] != 0x88)
240 return 11; // Bootable
242 u8 media = buffer[0x21];
245 CDEmu.emulated_drive_gf = dop.drive_g;
247 u16 boot_segment = *(u16*)&buffer[0x22];
249 boot_segment = 0x07C0;
250 CDEmu.load_segment = boot_segment;
251 CDEmu.buffer_segment = 0x0000;
253 u16 nbsectors = *(u16*)&buffer[0x26];
254 CDEmu.sector_count = nbsectors;
256 lba = *(u32*)&buffer[0x28];
259 // And we read the image in memory
261 dop.count = DIV_ROUND_UP(nbsectors, 4);
262 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
263 ret = cdb_read(&dop);
268 // No emulation requested - return success.
269 CDEmu.emulated_extdrive = EXTSTART_CD + cdid;
273 // Emulation of a floppy/harddisk requested
274 if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
277 // Set emulated drive id and increase bios installed hardware
281 CDEmu.emulated_extdrive = 0x00;
282 // XXX - get and set actual floppy count.
283 set_equipment_flags(0x41, 0x41);
286 case 0x01: // 1.2M floppy
288 CDEmu.lchs.cylinders = 80;
289 CDEmu.lchs.heads = 2;
291 case 0x02: // 1.44M floppy
293 CDEmu.lchs.cylinders = 80;
294 CDEmu.lchs.heads = 2;
296 case 0x03: // 2.88M floppy
298 CDEmu.lchs.cylinders = 80;
299 CDEmu.lchs.heads = 2;
303 // Harddrive emulation
304 CDEmu.emulated_extdrive = 0x80;
305 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
307 // Peak at partition table to get chs.
308 struct mbr_s *mbr = (void*)0;
309 u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
310 u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
311 u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
313 CDEmu.lchs.spt = sptcyl & 0x3f;
314 CDEmu.lchs.cylinders = ((sptcyl<<2)&0x300) + cyllow + 1;
315 CDEmu.lchs.heads = heads + 1;
318 // everything is ok, so from now on, the emulation is active
320 dprintf(6, "cdemu media=%d\n", media);