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
18 #include "tcgbios.h" // tpm_*
21 /****************************************************************
23 ****************************************************************/
25 struct eltorito_s CDEmu VARLOW = { .size=sizeof(CDEmu) };
26 struct drive_s *emulated_drive_gf VARLOW;
27 struct drive_s *cdemu_drive_gf VARFSEG;
30 cdemu_read(struct disk_op_s *op)
32 struct drive_s *drive_gf = GET_LOW(emulated_drive_gf);
34 dop.drive_gf = drive_gf;
35 dop.command = op->command;
36 dop.lba = GET_LOW(CDEmu.ilba) + op->lba / 4;
38 int count = op->count;
40 u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
43 // Partial read of first block.
45 dop.buf_fl = cdbuf_fl;
46 int ret = process_op(&dop);
49 u8 thiscount = 4 - (op->lba & 3);
50 if (thiscount > count)
53 memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
54 op->buf_fl += thiscount * 512;
55 op->count += thiscount;
60 // Read n number of regular blocks.
61 dop.count = count / 4;
62 dop.buf_fl = op->buf_fl;
63 int ret = process_op(&dop);
64 op->count += dop.count * 4;
67 u8 thiscount = count & ~3;
69 op->buf_fl += thiscount * 512;
70 dop.lba += thiscount / 4;
74 // Partial read on last block.
76 dop.buf_fl = cdbuf_fl;
77 int ret = process_op(&dop);
81 memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
82 op->count += thiscount;
85 return DISK_RET_SUCCESS;
89 cdemu_process_op(struct disk_op_s *op)
91 if (!CONFIG_CDROM_EMU)
94 switch (op->command) {
96 return cdemu_read(op);
99 return DISK_RET_EWRITEPROTECT;
101 return default_process_op(op);
108 if (!CONFIG_CDROM_EMU)
112 if (create_bounce_buf() < 0)
115 struct drive_s *drive = malloc_fseg(sizeof(*drive));
120 cdemu_drive_gf = drive;
121 memset(drive, 0, sizeof(*drive));
122 drive->type = DTYPE_CDEMU;
123 drive->blksize = DISK_SECTOR_SIZE;
124 drive->sectors = (u64)-1;
128 /****************************************************************
130 ****************************************************************/
133 cdrom_boot(struct drive_s *drive)
136 struct disk_op_s dop;
137 int cdid = getDriveId(EXTTYPE_CD, drive);
138 memset(&dop, 0, sizeof(dop));
139 dop.drive_gf = drive;
140 if (!dop.drive_gf || cdid < 0)
143 int ret = scsi_is_ready(&dop);
145 dprintf(1, "scsi_is_ready returned %d\n", ret);
147 // Read the Boot Record Volume Descriptor
148 u8 buffer[CDROM_SECTOR_SIZE];
149 dop.command = CMD_READ;
153 ret = process_op(&dop);
160 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
163 // ok, now we calculate the Boot catalog address
164 u32 lba = *(u32*)&buffer[0x47];
166 // And we read the Boot Catalog
169 ret = process_op(&dop);
174 if (buffer[0x00] != 0x01)
176 if (buffer[0x01] != 0x00)
177 return 9; // Platform
178 if (buffer[0x1E] != 0x55)
180 if (buffer[0x1F] != 0xAA)
183 // Initial/Default Entry
184 if (buffer[0x20] != 0x88)
185 return 11; // Bootable
187 /* measure 2048 bytes (one sector) */
188 tpm_add_cdrom_catalog(MAKE_FLATPTR(GET_SEG(SS), buffer), sizeof(buffer));
190 // Fill in el-torito cdrom emulation fields.
191 emulated_drive_gf = drive;
192 u8 media = buffer[0x21];
194 u16 boot_segment = *(u16*)&buffer[0x22];
196 boot_segment = 0x07C0;
197 CDEmu.load_segment = boot_segment;
198 CDEmu.buffer_segment = 0x0000;
200 u16 nbsectors = *(u16*)&buffer[0x26];
201 CDEmu.sector_count = nbsectors;
203 lba = *(u32*)&buffer[0x28];
206 CDEmu.controller_index = drive->cntl_id / 2;
207 CDEmu.device_spec = drive->cntl_id % 2;
209 // And we read the image in memory
210 nbsectors = DIV_ROUND_UP(nbsectors, 4);
212 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
214 int count = nbsectors;
215 if (count > 64*1024/CDROM_SECTOR_SIZE)
216 count = 64*1024/CDROM_SECTOR_SIZE;
218 ret = process_op(&dop);
223 dop.buf_fl += count*CDROM_SECTOR_SIZE;
227 // No emulation requested - return success.
228 CDEmu.emulated_drive = EXTSTART_CD + cdid;
232 // Emulation of a floppy/harddisk requested
233 if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
236 // Set emulated drive id and increase bios installed hardware
240 CDEmu.emulated_drive = 0x00;
241 // XXX - get and set actual floppy count.
242 set_equipment_flags(0x41, 0x41);
245 case 0x01: // 1.2M floppy
246 CDEmu.chs.sptcyl = 15;
247 CDEmu.chs.cyllow = 79;
250 case 0x02: // 1.44M floppy
251 CDEmu.chs.sptcyl = 18;
252 CDEmu.chs.cyllow = 79;
255 case 0x03: // 2.88M floppy
256 CDEmu.chs.sptcyl = 36;
257 CDEmu.chs.cyllow = 79;
262 // Harddrive emulation
263 CDEmu.emulated_drive = 0x80;
264 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
266 // Peak at partition table to get chs.
267 struct mbr_s *mbr = MAKE_FLATPTR(boot_segment, 0);
268 CDEmu.chs = mbr->partitions[0].last;
271 // everything is ok, so from now on, the emulation is active
273 dprintf(6, "cdemu media=%d\n", media);