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 "biosvar.h" // GET_GLOBAL
9 #include "block.h" // struct drive_s
10 #include "bregs.h" // struct bregs
11 #include "hw/ata.h" // ATA_CMD_REQUEST_SENSE
12 #include "hw/blockcmd.h" // CDB_CMD_REQUEST_SENSE
13 #include "malloc.h" // free
14 #include "output.h" // dprintf
15 #include "std/disk.h" // DISK_RET_SUCCESS
16 #include "string.h" // memset
17 #include "util.h" // cdrom_prepboot
19 // Locks for removable devices
20 u8 CDRom_locks[BUILD_MAX_EXTDRIVE] VARLOW;
23 /****************************************************************
25 ****************************************************************/
27 struct eltorito_s CDEmu VARLOW = { .size=sizeof(CDEmu) };
28 struct drive_s *emulated_drive_gf VARLOW;
29 struct drive_s *cdemu_drive_gf VARFSEG;
32 cdemu_read(struct disk_op_s *op)
34 struct drive_s *drive_gf = GET_LOW(emulated_drive_gf);
36 dop.drive_gf = drive_gf;
37 dop.command = op->command;
38 dop.lba = GET_LOW(CDEmu.ilba) + op->lba / 4;
40 int count = op->count;
42 u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
45 // Partial read of first block.
47 dop.buf_fl = cdbuf_fl;
48 int ret = process_op(&dop);
51 u8 thiscount = 4 - (op->lba & 3);
52 if (thiscount > count)
55 memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
56 op->buf_fl += thiscount * 512;
57 op->count += thiscount;
62 // Read n number of regular blocks.
63 dop.count = count / 4;
64 dop.buf_fl = op->buf_fl;
65 int ret = process_op(&dop);
66 op->count += dop.count * 4;
69 u8 thiscount = count & ~3;
71 op->buf_fl += thiscount * 512;
72 dop.lba += thiscount / 4;
76 // Partial read on last block.
78 dop.buf_fl = cdbuf_fl;
79 int ret = process_op(&dop);
83 memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
84 op->count += thiscount;
87 return DISK_RET_SUCCESS;
91 process_cdemu_op(struct disk_op_s *op)
93 if (!CONFIG_CDROM_EMU)
96 switch (op->command) {
98 return cdemu_read(op);
101 return DISK_RET_EWRITEPROTECT;
106 return DISK_RET_SUCCESS;
108 return DISK_RET_EPARAM;
115 if (!CONFIG_CDROM_EMU)
119 if (create_bounce_buf() < 0)
122 struct drive_s *drive = malloc_fseg(sizeof(*drive));
128 cdemu_drive_gf = drive;
129 memset(drive, 0, sizeof(*drive));
130 drive->type = DTYPE_CDEMU;
131 drive->blksize = DISK_SECTOR_SIZE;
132 drive->sectors = (u64)-1;
136 /****************************************************************
138 ****************************************************************/
141 cdrom_boot(struct drive_s *drive)
144 struct disk_op_s dop;
145 int cdid = getDriveId(EXTTYPE_CD, drive);
146 memset(&dop, 0, sizeof(dop));
147 dop.drive_gf = drive;
148 if (!dop.drive_gf || cdid < 0)
151 int ret = scsi_is_ready(&dop);
153 dprintf(1, "scsi_is_ready returned %d\n", ret);
155 // Read the Boot Record Volume Descriptor
156 u8 buffer[CDROM_SECTOR_SIZE];
157 dop.command = CMD_READ;
161 ret = scsi_process_op(&dop);
168 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
171 // ok, now we calculate the Boot catalog address
172 u32 lba = *(u32*)&buffer[0x47];
174 // And we read the Boot Catalog
177 ret = scsi_process_op(&dop);
182 if (buffer[0x00] != 0x01)
184 if (buffer[0x01] != 0x00)
185 return 9; // Platform
186 if (buffer[0x1E] != 0x55)
188 if (buffer[0x1F] != 0xAA)
191 // Initial/Default Entry
192 if (buffer[0x20] != 0x88)
193 return 11; // Bootable
195 // Fill in el-torito cdrom emulation fields.
196 emulated_drive_gf = drive;
197 u8 media = buffer[0x21];
199 u16 boot_segment = *(u16*)&buffer[0x22];
201 boot_segment = 0x07C0;
202 CDEmu.load_segment = boot_segment;
203 CDEmu.buffer_segment = 0x0000;
205 u16 nbsectors = *(u16*)&buffer[0x26];
206 CDEmu.sector_count = nbsectors;
208 lba = *(u32*)&buffer[0x28];
211 CDEmu.controller_index = drive->cntl_id / 2;
212 CDEmu.device_spec = drive->cntl_id % 2;
214 // And we read the image in memory
215 nbsectors = DIV_ROUND_UP(nbsectors, 4);
217 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
219 int count = nbsectors;
220 if (count > 64*1024/CDROM_SECTOR_SIZE)
221 count = 64*1024/CDROM_SECTOR_SIZE;
223 ret = scsi_process_op(&dop);
228 dop.buf_fl += count*CDROM_SECTOR_SIZE;
232 // No emulation requested - return success.
233 CDEmu.emulated_drive = EXTSTART_CD + cdid;
237 // Emulation of a floppy/harddisk requested
238 if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
241 // Set emulated drive id and increase bios installed hardware
245 CDEmu.emulated_drive = 0x00;
246 // XXX - get and set actual floppy count.
247 set_equipment_flags(0x41, 0x41);
250 case 0x01: // 1.2M floppy
251 CDEmu.chs.sptcyl = 15;
252 CDEmu.chs.cyllow = 79;
255 case 0x02: // 1.44M floppy
256 CDEmu.chs.sptcyl = 18;
257 CDEmu.chs.cyllow = 79;
260 case 0x03: // 2.88M floppy
261 CDEmu.chs.sptcyl = 36;
262 CDEmu.chs.cyllow = 79;
267 // Harddrive emulation
268 CDEmu.emulated_drive = 0x80;
269 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
271 // Peak at partition table to get chs.
272 struct mbr_s *mbr = MAKE_FLATPTR(boot_segment, 0);
273 CDEmu.chs = mbr->partitions[0].last;
276 // everything is ok, so from now on, the emulation is active
278 dprintf(6, "cdemu media=%d\n", media);