blob: a77671a47cb2a11ea190f7a7cd0a1574d3305abb [file] [log] [blame]
Kevin O'Connor36c93a52009-09-12 19:35:04 -04001// Support for booting from cdroms (the "El Torito" spec).
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -05002//
Kevin O'Connorc892b132009-08-11 21:59:37 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -05007
Kevin O'Connord3140832012-05-13 22:46:12 -04008#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor135f3f62013-09-14 23:57:26 -04009#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "bregs.h" // struct bregs
Kevin O'Connor5d369d82013-09-02 20:48:46 -040011#include "hw/ata.h" // ATA_CMD_REQUEST_SENSE
12#include "hw/blockcmd.h" // CDB_CMD_REQUEST_SENSE
Kevin O'Connor9dea5902013-09-14 20:23:54 -040013#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "output.h" // dprintf
Kevin O'Connor135f3f62013-09-14 23:57:26 -040015#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040016#include "string.h" // memset
Kevin O'Connor135f3f62013-09-14 23:57:26 -040017#include "util.h" // cdrom_prepboot
Stefan Berger2aff1c12015-05-26 15:48:33 -040018#include "tcgbios.h" // tpm_*
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050019
20
21/****************************************************************
Kevin O'Connor51cfbe72009-08-18 22:38:49 -040022 * CD emulation
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050023 ****************************************************************/
24
Kevin O'Connor02f76762014-05-10 01:20:46 -040025struct eltorito_s CDEmu VARLOW = { .size=sizeof(CDEmu) };
26struct drive_s *emulated_drive_gf VARLOW;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050027struct drive_s *cdemu_drive_gf VARFSEG;
Kevin O'Connord5d02b62010-06-06 16:18:03 -040028
Kevin O'Connor36c93a52009-09-12 19:35:04 -040029static int
30cdemu_read(struct disk_op_s *op)
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050031{
Kevin O'Connor02f76762014-05-10 01:20:46 -040032 struct drive_s *drive_gf = GET_LOW(emulated_drive_gf);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040033 struct disk_op_s dop;
Kevin O'Connore5a0b612017-07-11 12:24:50 -040034 dop.drive_fl = drive_gf;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040035 dop.command = op->command;
Kevin O'Connord3140832012-05-13 22:46:12 -040036 dop.lba = GET_LOW(CDEmu.ilba) + op->lba / 4;
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050037
Kevin O'Connor36c93a52009-09-12 19:35:04 -040038 int count = op->count;
39 op->count = 0;
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +020040 u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040041
Kevin O'Connor36c93a52009-09-12 19:35:04 -040042 if (op->lba & 3) {
43 // Partial read of first block.
44 dop.count = 1;
Kevin O'Connord5d02b62010-06-06 16:18:03 -040045 dop.buf_fl = cdbuf_fl;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040046 int ret = process_op(&dop);
47 if (ret)
48 return ret;
49 u8 thiscount = 4 - (op->lba & 3);
50 if (thiscount > count)
51 thiscount = count;
52 count -= thiscount;
Kevin O'Connord5d02b62010-06-06 16:18:03 -040053 memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040054 op->buf_fl += thiscount * 512;
55 op->count += thiscount;
56 dop.lba++;
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050057 }
Kevin O'Connor36c93a52009-09-12 19:35:04 -040058
59 if (count > 3) {
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;
65 if (ret)
66 return ret;
67 u8 thiscount = count & ~3;
68 count &= 3;
69 op->buf_fl += thiscount * 512;
70 dop.lba += thiscount / 4;
71 }
72
73 if (count) {
74 // Partial read on last block.
75 dop.count = 1;
Kevin O'Connord5d02b62010-06-06 16:18:03 -040076 dop.buf_fl = cdbuf_fl;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040077 int ret = process_op(&dop);
78 if (ret)
79 return ret;
80 u8 thiscount = count;
Kevin O'Connord5d02b62010-06-06 16:18:03 -040081 memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040082 op->count += thiscount;
83 }
84
85 return DISK_RET_SUCCESS;
86}
87
88int
Kevin O'Connor17856452015-07-07 14:56:20 -040089cdemu_process_op(struct disk_op_s *op)
Kevin O'Connor36c93a52009-09-12 19:35:04 -040090{
91 if (!CONFIG_CDROM_EMU)
92 return 0;
93
94 switch (op->command) {
95 case CMD_READ:
96 return cdemu_read(op);
97 case CMD_WRITE:
98 case CMD_FORMAT:
99 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400100 default:
Kevin O'Connor85c72c62015-07-07 09:01:52 -0400101 return default_process_op(op);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400102 }
103}
104
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400105void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500106cdrom_prepboot(void)
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400107{
108 if (!CONFIG_CDROM_EMU)
109 return;
Kevin O'Connora0842f82010-12-29 11:05:46 -0500110 if (!CDCount)
Kevin O'Connord5d02b62010-06-06 16:18:03 -0400111 return;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500112 if (create_bounce_buf() < 0)
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200113 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400114
Kevin O'Connor1902c942013-10-26 11:48:06 -0400115 struct drive_s *drive = malloc_fseg(sizeof(*drive));
116 if (!drive) {
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500117 warn_noalloc();
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400118 return;
119 }
Kevin O'Connor1902c942013-10-26 11:48:06 -0400120 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;
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500125}
126
Kevin O'Connor180a9592008-03-04 22:50:53 -0500127
128/****************************************************************
129 * CD booting
130 ****************************************************************/
131
Kevin O'Connora05223c2008-06-28 12:15:57 -0400132int
Kevin O'Connor1902c942013-10-26 11:48:06 -0400133cdrom_boot(struct drive_s *drive)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500134{
Kevin O'Connord3140832012-05-13 22:46:12 -0400135 ASSERT32FLAT();
Kevin O'Connor7d700252010-02-15 11:56:07 -0500136 struct disk_op_s dop;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400137 int cdid = getDriveId(EXTTYPE_CD, drive);
Kevin O'Connor7d700252010-02-15 11:56:07 -0500138 memset(&dop, 0, sizeof(dop));
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400139 dop.drive_fl = drive;
140 if (!dop.drive_fl || cdid < 0)
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500141 return 1;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500142
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100143 int ret = scsi_is_ready(&dop);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500144 if (ret)
Paul Menzeld53aea02020-05-18 18:58:04 +0200145 dprintf(5, "scsi_is_ready returned %d\n", ret);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500146
147 // Read the Boot Record Volume Descriptor
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400148 u8 buffer[CDROM_SECTOR_SIZE];
Kevin O'Connor8ed94b42014-12-29 09:48:24 -0500149 dop.command = CMD_READ;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500150 dop.lba = 0x11;
151 dop.count = 1;
Kevin O'Connord3140832012-05-13 22:46:12 -0400152 dop.buf_fl = buffer;
Kevin O'Connor4dbe8292015-07-07 09:53:54 -0400153 ret = process_op(&dop);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500154 if (ret)
155 return 3;
156
157 // Validity checks
158 if (buffer[0])
159 return 4;
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400160 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500161 return 5;
162
163 // ok, now we calculate the Boot catalog address
164 u32 lba = *(u32*)&buffer[0x47];
165
166 // And we read the Boot Catalog
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500167 dop.lba = lba;
Kevin O'Connor76977b22010-02-17 01:01:32 -0500168 dop.count = 1;
Kevin O'Connor4dbe8292015-07-07 09:53:54 -0400169 ret = process_op(&dop);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500170 if (ret)
171 return 7;
172
173 // Validation entry
174 if (buffer[0x00] != 0x01)
175 return 8; // Header
176 if (buffer[0x01] != 0x00)
177 return 9; // Platform
178 if (buffer[0x1E] != 0x55)
179 return 10; // key 1
180 if (buffer[0x1F] != 0xAA)
181 return 10; // key 2
182
183 // Initial/Default Entry
184 if (buffer[0x20] != 0x88)
185 return 11; // Bootable
186
Stefan Berger2aff1c12015-05-26 15:48:33 -0400187 /* measure 2048 bytes (one sector) */
188 tpm_add_cdrom_catalog(MAKE_FLATPTR(GET_SEG(SS), buffer), sizeof(buffer));
189
Kevin O'Connor02f76762014-05-10 01:20:46 -0400190 // Fill in el-torito cdrom emulation fields.
191 emulated_drive_gf = drive;
Kevin O'Connordfa16502008-03-22 20:13:08 -0400192 u8 media = buffer[0x21];
Kevin O'Connor180a9592008-03-04 22:50:53 -0500193
194 u16 boot_segment = *(u16*)&buffer[0x22];
195 if (!boot_segment)
196 boot_segment = 0x07C0;
Kevin O'Connord3140832012-05-13 22:46:12 -0400197 CDEmu.load_segment = boot_segment;
198 CDEmu.buffer_segment = 0x0000;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500199
200 u16 nbsectors = *(u16*)&buffer[0x26];
Kevin O'Connord3140832012-05-13 22:46:12 -0400201 CDEmu.sector_count = nbsectors;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500202
203 lba = *(u32*)&buffer[0x28];
Kevin O'Connord3140832012-05-13 22:46:12 -0400204 CDEmu.ilba = lba;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500205
Kevin O'Connor02f76762014-05-10 01:20:46 -0400206 CDEmu.controller_index = drive->cntl_id / 2;
207 CDEmu.device_spec = drive->cntl_id % 2;
208
Kevin O'Connor180a9592008-03-04 22:50:53 -0500209 // And we read the image in memory
Kevin O'Connorc08ef9a2014-12-29 10:15:57 -0500210 nbsectors = DIV_ROUND_UP(nbsectors, 4);
Kevin O'Connora4d70aa2009-08-09 11:32:00 -0400211 dop.lba = lba;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500212 dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
Kevin O'Connorc08ef9a2014-12-29 10:15:57 -0500213 while (nbsectors) {
214 int count = nbsectors;
215 if (count > 64*1024/CDROM_SECTOR_SIZE)
216 count = 64*1024/CDROM_SECTOR_SIZE;
217 dop.count = count;
Kevin O'Connor4dbe8292015-07-07 09:53:54 -0400218 ret = process_op(&dop);
Kevin O'Connorc08ef9a2014-12-29 10:15:57 -0500219 if (ret)
220 return 12;
221 nbsectors -= count;
222 dop.lba += count;
223 dop.buf_fl += count*CDROM_SECTOR_SIZE;
224 }
Kevin O'Connor180a9592008-03-04 22:50:53 -0500225
Kevin O'Connordfa16502008-03-22 20:13:08 -0400226 if (media == 0) {
227 // No emulation requested - return success.
Kevin O'Connor02f76762014-05-10 01:20:46 -0400228 CDEmu.emulated_drive = EXTSTART_CD + cdid;
Kevin O'Connordfa16502008-03-22 20:13:08 -0400229 return 0;
230 }
231
232 // Emulation of a floppy/harddisk requested
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500233 if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
Kevin O'Connordfa16502008-03-22 20:13:08 -0400234 return 13;
235
236 // Set emulated drive id and increase bios installed hardware
237 // number of devices
238 if (media < 4) {
239 // Floppy emulation
Kevin O'Connor02f76762014-05-10 01:20:46 -0400240 CDEmu.emulated_drive = 0x00;
Kevin O'Connor7d700252010-02-15 11:56:07 -0500241 // XXX - get and set actual floppy count.
Kevin O'Connore51316d2012-06-10 09:09:22 -0400242 set_equipment_flags(0x41, 0x41);
Kevin O'Connor95827c42009-02-07 00:04:57 -0500243
244 switch (media) {
245 case 0x01: // 1.2M floppy
Kevin O'Connor02f76762014-05-10 01:20:46 -0400246 CDEmu.chs.sptcyl = 15;
247 CDEmu.chs.cyllow = 79;
248 CDEmu.chs.heads = 1;
Kevin O'Connor95827c42009-02-07 00:04:57 -0500249 break;
250 case 0x02: // 1.44M floppy
Kevin O'Connor02f76762014-05-10 01:20:46 -0400251 CDEmu.chs.sptcyl = 18;
252 CDEmu.chs.cyllow = 79;
253 CDEmu.chs.heads = 1;
Kevin O'Connor95827c42009-02-07 00:04:57 -0500254 break;
255 case 0x03: // 2.88M floppy
Kevin O'Connor02f76762014-05-10 01:20:46 -0400256 CDEmu.chs.sptcyl = 36;
257 CDEmu.chs.cyllow = 79;
258 CDEmu.chs.heads = 1;
Kevin O'Connor95827c42009-02-07 00:04:57 -0500259 break;
260 }
Kevin O'Connordfa16502008-03-22 20:13:08 -0400261 } else {
262 // Harddrive emulation
Kevin O'Connor02f76762014-05-10 01:20:46 -0400263 CDEmu.emulated_drive = 0x80;
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500264 SET_BDA(hdcount, GET_BDA(hdcount) + 1);
Kevin O'Connordfa16502008-03-22 20:13:08 -0400265
Kevin O'Connor95827c42009-02-07 00:04:57 -0500266 // Peak at partition table to get chs.
Kevin O'Connor02f76762014-05-10 01:20:46 -0400267 struct mbr_s *mbr = MAKE_FLATPTR(boot_segment, 0);
268 CDEmu.chs = mbr->partitions[0].last;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500269 }
270
Kevin O'Connordfa16502008-03-22 20:13:08 -0400271 // everything is ok, so from now on, the emulation is active
Kevin O'Connor02f76762014-05-10 01:20:46 -0400272 CDEmu.media = media;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400273 dprintf(6, "cdemu media=%d\n", media);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500274
275 return 0;
276}
Gerd Hoffmann14221cd2018-09-21 09:35:03 +0200277
278// check if media is present and the drive is bootable.
279// in case it is return the volume label.
280char*
281cdrom_media_info(struct drive_s *drive)
282{
283 ASSERT32FLAT();
284
285 struct disk_op_s dop;
286 memset(&dop, 0, sizeof(dop));
287 dop.drive_fl = drive;
288
289 int ret = scsi_is_ready(&dop);
290 if (ret)
291 return NULL;
292
293 // Read the Boot Record Volume Descriptor
294 u8 buffer[CDROM_SECTOR_SIZE];
295 dop.command = CMD_READ;
296 dop.lba = 0x11;
297 dop.count = 1;
298 dop.buf_fl = buffer;
299 ret = process_op(&dop);
300 if (ret)
301 return NULL;
302
303 // Is it bootable?
304 if (buffer[0])
305 return NULL;
306 if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
307 return NULL;
308
309 // Read the Primary Volume Descriptor
310 dop.command = CMD_READ;
311 dop.lba = 0x10;
312 dop.count = 1;
313 dop.buf_fl = buffer;
314 ret = process_op(&dop);
315 if (ret)
316 return NULL;
317
318 // Read volume id, trim trailing spaces
319 char *volume = znprintf(30, "%s", buffer + 40);
320 nullTrailingSpace(volume);
321 return volume;
322}