tizen 2.3.1 release
[external/qemu.git] / roms / seabios / src / cdrom.c
1 // Support for booting from cdroms (the "El Torito" spec).
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
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
14
15 // Locks for removable devices
16 u8 CDRom_locks[CONFIG_MAX_EXTDRIVE] VARLOW;
17
18
19 /****************************************************************
20  * CD emulation
21  ****************************************************************/
22
23 struct cdemu_s CDEmu VARLOW;
24 struct drive_s *cdemu_drive_gf VAR16VISIBLE;
25
26 static int
27 cdemu_read(struct disk_op_s *op)
28 {
29     struct drive_s *drive_g;
30     drive_g = GLOBALFLAT2GLOBAL(GET_LOW(CDEmu.emulated_drive_gf));
31     struct disk_op_s dop;
32     dop.drive_g = drive_g;
33     dop.command = op->command;
34     dop.lba = GET_LOW(CDEmu.ilba) + op->lba / 4;
35
36     int count = op->count;
37     op->count = 0;
38     u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
39
40     if (op->lba & 3) {
41         // Partial read of first block.
42         dop.count = 1;
43         dop.buf_fl = cdbuf_fl;
44         int ret = process_op(&dop);
45         if (ret)
46             return ret;
47         u8 thiscount = 4 - (op->lba & 3);
48         if (thiscount > count)
49             thiscount = count;
50         count -= thiscount;
51         memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
52         op->buf_fl += thiscount * 512;
53         op->count += thiscount;
54         dop.lba++;
55     }
56
57     if (count > 3) {
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;
63         if (ret)
64             return ret;
65         u8 thiscount = count & ~3;
66         count &= 3;
67         op->buf_fl += thiscount * 512;
68         dop.lba += thiscount / 4;
69     }
70
71     if (count) {
72         // Partial read on last block.
73         dop.count = 1;
74         dop.buf_fl = cdbuf_fl;
75         int ret = process_op(&dop);
76         if (ret)
77             return ret;
78         u8 thiscount = count;
79         memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
80         op->count += thiscount;
81     }
82
83     return DISK_RET_SUCCESS;
84 }
85
86 int
87 process_cdemu_op(struct disk_op_s *op)
88 {
89     if (!CONFIG_CDROM_EMU)
90         return 0;
91
92     switch (op->command) {
93     case CMD_READ:
94         return cdemu_read(op);
95     case CMD_WRITE:
96     case CMD_FORMAT:
97         return DISK_RET_EWRITEPROTECT;
98     case CMD_VERIFY:
99     case CMD_RESET:
100     case CMD_SEEK:
101     case CMD_ISREADY:
102         return DISK_RET_SUCCESS;
103     default:
104         op->count = 0;
105         return DISK_RET_EPARAM;
106     }
107 }
108
109 void
110 cdemu_setup(void)
111 {
112     if (!CONFIG_CDROM_EMU)
113         return;
114     if (!CDCount)
115         return;
116     if (bounce_buf_init() < 0)
117         return;
118
119     struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
120     if (!drive_g) {
121         warn_noalloc();
122         free(drive_g);
123         return;
124     }
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;
130 }
131
132 struct eltorito_s {
133     u8 size;
134     u8 media;
135     u8 emulated_drive;
136     u8 controller_index;
137     u32 ilba;
138     u16 device_spec;
139     u16 buffer_segment;
140     u16 load_segment;
141     u16 sector_count;
142     u8 cylinders;
143     u8 sectors;
144     u8 heads;
145 };
146
147 #define SET_INT13ET(regs,var,val)                                      \
148     SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
149
150 // ElTorito - Terminate disk emu
151 void
152 cdemu_134b(struct bregs *regs)
153 {
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);
159     u8 cntl_id = 0;
160     if (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));
171
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
176
177         // XXX - update floppy/hd count.
178     }
179
180     disk_ret(regs, DISK_RET_SUCCESS);
181 }
182
183
184 /****************************************************************
185  * CD booting
186  ****************************************************************/
187
188 int
189 cdrom_boot(struct drive_s *drive_g)
190 {
191     ASSERT32FLAT();
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)
197         return 1;
198
199     int ret = scsi_is_ready(&dop);
200     if (ret)
201         dprintf(1, "scsi_is_ready returned %d\n", ret);
202
203     // Read the Boot Record Volume Descriptor
204     u8 buffer[CDROM_SECTOR_SIZE];
205     dop.lba = 0x11;
206     dop.count = 1;
207     dop.buf_fl = buffer;
208     ret = cdb_read(&dop);
209     if (ret)
210         return 3;
211
212     // Validity checks
213     if (buffer[0])
214         return 4;
215     if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
216         return 5;
217
218     // ok, now we calculate the Boot catalog address
219     u32 lba = *(u32*)&buffer[0x47];
220
221     // And we read the Boot Catalog
222     dop.lba = lba;
223     dop.count = 1;
224     ret = cdb_read(&dop);
225     if (ret)
226         return 7;
227
228     // Validation entry
229     if (buffer[0x00] != 0x01)
230         return 8;   // Header
231     if (buffer[0x01] != 0x00)
232         return 9;   // Platform
233     if (buffer[0x1E] != 0x55)
234         return 10;  // key 1
235     if (buffer[0x1F] != 0xAA)
236         return 10;  // key 2
237
238     // Initial/Default Entry
239     if (buffer[0x20] != 0x88)
240         return 11; // Bootable
241
242     u8 media = buffer[0x21];
243     CDEmu.media = media;
244
245     CDEmu.emulated_drive_gf = dop.drive_g;
246
247     u16 boot_segment = *(u16*)&buffer[0x22];
248     if (!boot_segment)
249         boot_segment = 0x07C0;
250     CDEmu.load_segment = boot_segment;
251     CDEmu.buffer_segment = 0x0000;
252
253     u16 nbsectors = *(u16*)&buffer[0x26];
254     CDEmu.sector_count = nbsectors;
255
256     lba = *(u32*)&buffer[0x28];
257     CDEmu.ilba = lba;
258
259     // And we read the image in memory
260     dop.lba = lba;
261     dop.count = DIV_ROUND_UP(nbsectors, 4);
262     dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
263     ret = cdb_read(&dop);
264     if (ret)
265         return 12;
266
267     if (media == 0) {
268         // No emulation requested - return success.
269         CDEmu.emulated_extdrive = EXTSTART_CD + cdid;
270         return 0;
271     }
272
273     // Emulation of a floppy/harddisk requested
274     if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
275         return 13;
276
277     // Set emulated drive id and increase bios installed hardware
278     // number of devices
279     if (media < 4) {
280         // Floppy emulation
281         CDEmu.emulated_extdrive = 0x00;
282         // XXX - get and set actual floppy count.
283         set_equipment_flags(0x41, 0x41);
284
285         switch (media) {
286         case 0x01:  // 1.2M floppy
287             CDEmu.lchs.spt = 15;
288             CDEmu.lchs.cylinders = 80;
289             CDEmu.lchs.heads = 2;
290             break;
291         case 0x02:  // 1.44M floppy
292             CDEmu.lchs.spt = 18;
293             CDEmu.lchs.cylinders = 80;
294             CDEmu.lchs.heads = 2;
295             break;
296         case 0x03:  // 2.88M floppy
297             CDEmu.lchs.spt = 36;
298             CDEmu.lchs.cylinders = 80;
299             CDEmu.lchs.heads = 2;
300             break;
301         }
302     } else {
303         // Harddrive emulation
304         CDEmu.emulated_extdrive = 0x80;
305         SET_BDA(hdcount, GET_BDA(hdcount) + 1);
306
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);
312
313         CDEmu.lchs.spt = sptcyl & 0x3f;
314         CDEmu.lchs.cylinders = ((sptcyl<<2)&0x300) + cyllow + 1;
315         CDEmu.lchs.heads = heads + 1;
316     }
317
318     // everything is ok, so from now on, the emulation is active
319     CDEmu.active = 0x01;
320     dprintf(6, "cdemu media=%d\n", media);
321
322     return 0;
323 }