Dynamically allocate each drive_g with malloc_fseg().
[seabios.git] / 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_EBDA
12 #include "ata.h" // ATA_CMD_REQUEST_SENSE
13 #include "blockcmd.h" // CDB_CMD_REQUEST_SENSE
14
15
16 /****************************************************************
17  * CD emulation
18  ****************************************************************/
19
20 static int
21 cdemu_read(struct disk_op_s *op)
22 {
23     u16 ebda_seg = get_ebda_seg();
24     struct drive_s *drive_g = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
25     struct disk_op_s dop;
26     dop.drive_g = drive_g;
27     dop.command = op->command;
28     dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + op->lba / 4;
29
30     int count = op->count;
31     op->count = 0;
32     u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
33
34     if (op->lba & 3) {
35         // Partial read of first block.
36         dop.count = 1;
37         dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
38         int ret = process_op(&dop);
39         if (ret)
40             return ret;
41         u8 thiscount = 4 - (op->lba & 3);
42         if (thiscount > count)
43             thiscount = count;
44         count -= thiscount;
45         memcpy_far(FLATPTR_TO_SEG(op->buf_fl)
46                    , (void*)FLATPTR_TO_OFFSET(op->buf_fl)
47                    , ebda_seg, cdbuf_far + (op->lba & 3) * 512
48                    , thiscount * 512);
49         op->buf_fl += thiscount * 512;
50         op->count += thiscount;
51         dop.lba++;
52     }
53
54     if (count > 3) {
55         // Read n number of regular blocks.
56         dop.count = count / 4;
57         dop.buf_fl = op->buf_fl;
58         int ret = process_op(&dop);
59         op->count += dop.count * 4;
60         if (ret)
61             return ret;
62         u8 thiscount = count & ~3;
63         count &= 3;
64         op->buf_fl += thiscount * 512;
65         dop.lba += thiscount / 4;
66     }
67
68     if (count) {
69         // Partial read on last block.
70         dop.count = 1;
71         dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
72         int ret = process_op(&dop);
73         if (ret)
74             return ret;
75         u8 thiscount = count;
76         memcpy_far(FLATPTR_TO_SEG(op->buf_fl)
77                    , (void*)FLATPTR_TO_OFFSET(op->buf_fl)
78                    , ebda_seg, cdbuf_far, thiscount * 512);
79         op->count += thiscount;
80     }
81
82     return DISK_RET_SUCCESS;
83 }
84
85 int
86 process_cdemu_op(struct disk_op_s *op)
87 {
88     if (!CONFIG_CDROM_EMU)
89         return 0;
90
91     switch (op->command) {
92     case CMD_READ:
93         return cdemu_read(op);
94     case CMD_WRITE:
95     case CMD_FORMAT:
96         return DISK_RET_EWRITEPROTECT;
97     case CMD_VERIFY:
98     case CMD_RESET:
99     case CMD_SEEK:
100     case CMD_ISREADY:
101         return DISK_RET_SUCCESS;
102     default:
103         op->count = 0;
104         return DISK_RET_EPARAM;
105     }
106 }
107
108 struct drive_s *cdemu_drive VAR16VISIBLE;
109
110 void
111 cdemu_setup(void)
112 {
113     if (!CONFIG_CDROM_EMU)
114         return;
115
116     struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
117     if (! drive_g) {
118         warn_noalloc();
119         cdemu_drive = NULL;
120         return;
121     }
122     memset(drive_g, 0, sizeof(*drive_g));
123     cdemu_drive = STORE_GLOBAL_PTR(drive_g);
124     drive_g->type = DTYPE_CDEMU;
125     drive_g->blksize = DISK_SECTOR_SIZE;
126     drive_g->sectors = (u64)-1;
127 }
128
129 struct eltorito_s {
130     u8 size;
131     u8 media;
132     u8 emulated_drive;
133     u8 controller_index;
134     u32 ilba;
135     u16 device_spec;
136     u16 buffer_segment;
137     u16 load_segment;
138     u16 sector_count;
139     u8 cylinders;
140     u8 sectors;
141     u8 heads;
142 };
143
144 #define SET_INT13ET(regs,var,val)                                      \
145     SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
146
147 // ElTorito - Terminate disk emu
148 void
149 cdemu_134b(struct bregs *regs)
150 {
151     // FIXME ElTorito Hardcoded
152     u16 ebda_seg = get_ebda_seg();
153     SET_INT13ET(regs, size, 0x13);
154     SET_INT13ET(regs, media, GET_EBDA2(ebda_seg, cdemu.media));
155     SET_INT13ET(regs, emulated_drive
156                 , GET_EBDA2(ebda_seg, cdemu.emulated_extdrive));
157     struct drive_s *drive_g = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
158     u8 cntl_id = GET_GLOBAL(drive_g->cntl_id);
159     SET_INT13ET(regs, controller_index, cntl_id / 2);
160     SET_INT13ET(regs, device_spec, cntl_id % 2);
161     SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
162     SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
163     SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
164     SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
165     SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
166     SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
167     SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
168
169     // If we have to terminate emulation
170     if (regs->al == 0x00) {
171         // FIXME ElTorito Various. Should be handled accordingly to spec
172         SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
173
174         // XXX - update floppy/hd count.
175     }
176
177     disk_ret(regs, DISK_RET_SUCCESS);
178 }
179
180
181 /****************************************************************
182  * CD booting
183  ****************************************************************/
184
185 // Request SENSE
186 static int
187 atapi_get_sense(struct disk_op_s *op, u8 *asc, u8 *ascq)
188 {
189     struct cdb_request_sense cmd;
190     struct cdbres_request_sense data;
191     memset(&cmd, 0, sizeof(cmd));
192     cmd.command = CDB_CMD_REQUEST_SENSE;
193     cmd.length = sizeof(data);
194     op->count = 1;
195     op->buf_fl = &data;
196     int ret = atapi_cmd_data(op, &cmd, sizeof(data));
197     if (ret)
198         return ret;
199
200     *asc = data.asc;
201     *ascq = data.ascq;
202     return 0;
203 }
204
205 // Request capacity
206 static int
207 atapi_read_capacity(struct disk_op_s *op, u32 *blksize, u32 *sectors)
208 {
209     struct cdb_read_capacity cmd;
210     struct cdbres_read_capacity data;
211     memset(&cmd, 0, sizeof(cmd));
212     cmd.command = CDB_CMD_READ_CAPACITY;
213     op->count = 1;
214     op->buf_fl = &data;
215     int ret = atapi_cmd_data(op, &cmd, sizeof(data));
216     if (ret)
217         return ret;
218
219     *blksize = ntohl(data.blksize);
220     *sectors = ntohl(data.sectors);
221     return 0;
222 }
223
224 static int
225 atapi_is_ready(struct disk_op_s *op)
226 {
227     dprintf(6, "atapi_is_ready (drive=%p)\n", op->drive_g);
228
229     /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT is
230      * reported by the device.  If the device reports "IN PROGRESS",
231      * 30 seconds is added. */
232     u32 blksize, sectors;
233     int in_progress = 0;
234     u64 end = calc_future_tsc(5000);
235     for (;;) {
236         if (check_time(end)) {
237             dprintf(1, "read capacity failed\n");
238             return -1;
239         }
240
241         int ret = atapi_read_capacity(op, &blksize, &sectors);
242         if (!ret)
243             // Success
244             break;
245
246         u8 asc, ascq;
247         ret = atapi_get_sense(op, &asc, &ascq);
248         if (ret)
249             // Error - retry.
250             continue;
251
252         // Sense succeeded.
253         if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
254             dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
255             return -1;
256         }
257
258         if (asc == 0x04 && ascq == 0x01 && !in_progress) {
259             /* IN PROGRESS OF BECOMING READY */
260             printf("Waiting for device to detect medium... ");
261             /* Allow 30 seconds more */
262             end = calc_future_tsc(30000);
263             in_progress = 1;
264         }
265     }
266
267     if (blksize != GET_GLOBAL(op->drive_g->blksize)) {
268         printf("Unsupported sector size %u\n", blksize);
269         return -1;
270     }
271
272     dprintf(6, "sectors=%u\n", sectors);
273     printf("%dMB medium detected\n", sectors>>(20-11));
274     return 0;
275 }
276
277 int
278 cdrom_boot(int cdid)
279 {
280     struct disk_op_s dop;
281     memset(&dop, 0, sizeof(dop));
282     dop.drive_g = getDrive(EXTTYPE_CD, cdid);
283     if (!dop.drive_g)
284         return 1;
285
286     int ret = atapi_is_ready(&dop);
287     if (ret)
288         dprintf(1, "atapi_is_ready returned %d\n", ret);
289
290     // Read the Boot Record Volume Descriptor
291     u8 buffer[2048];
292     dop.lba = 0x11;
293     dop.count = 1;
294     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
295     ret = cdrom_read(&dop);
296     if (ret)
297         return 3;
298
299     // Validity checks
300     if (buffer[0])
301         return 4;
302     if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
303         return 5;
304
305     // ok, now we calculate the Boot catalog address
306     u32 lba = *(u32*)&buffer[0x47];
307
308     // And we read the Boot Catalog
309     dop.lba = lba;
310     ret = cdrom_read(&dop);
311     if (ret)
312         return 7;
313
314     // Validation entry
315     if (buffer[0x00] != 0x01)
316         return 8;   // Header
317     if (buffer[0x01] != 0x00)
318         return 9;   // Platform
319     if (buffer[0x1E] != 0x55)
320         return 10;  // key 1
321     if (buffer[0x1F] != 0xAA)
322         return 10;  // key 2
323
324     // Initial/Default Entry
325     if (buffer[0x20] != 0x88)
326         return 11; // Bootable
327
328     u16 ebda_seg = get_ebda_seg();
329     u8 media = buffer[0x21];
330     SET_EBDA2(ebda_seg, cdemu.media, media);
331
332     SET_EBDA2(ebda_seg, cdemu.emulated_drive, STORE_GLOBAL_PTR(dop.drive_g));
333
334     u16 boot_segment = *(u16*)&buffer[0x22];
335     if (!boot_segment)
336         boot_segment = 0x07C0;
337     SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
338     SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
339
340     u16 nbsectors = *(u16*)&buffer[0x26];
341     SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
342
343     lba = *(u32*)&buffer[0x28];
344     SET_EBDA2(ebda_seg, cdemu.ilba, lba);
345
346     // And we read the image in memory
347     dop.lba = lba;
348     dop.count = DIV_ROUND_UP(nbsectors, 4);
349     dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
350     ret = cdrom_read(&dop);
351     if (ret)
352         return 12;
353
354     if (media == 0) {
355         // No emulation requested - return success.
356         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, EXTSTART_CD + cdid);
357         return 0;
358     }
359
360     // Emulation of a floppy/harddisk requested
361     if (! CONFIG_CDROM_EMU || !cdemu_drive)
362         return 13;
363
364     // Set emulated drive id and increase bios installed hardware
365     // number of devices
366     if (media < 4) {
367         // Floppy emulation
368         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
369         // XXX - get and set actual floppy count.
370         SETBITS_BDA(equipment_list_flags, 0x41);
371
372         switch (media) {
373         case 0x01:  // 1.2M floppy
374             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
375             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
376             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
377             break;
378         case 0x02:  // 1.44M floppy
379             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
380             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
381             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
382             break;
383         case 0x03:  // 2.88M floppy
384             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
385             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
386             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
387             break;
388         }
389     } else {
390         // Harddrive emulation
391         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
392         SET_BDA(hdcount, GET_BDA(hdcount) + 1);
393
394         // Peak at partition table to get chs.
395         struct mbr_s *mbr = (void*)0;
396         u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
397         u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
398         u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
399
400         SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
401         SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
402                   , ((sptcyl<<2)&0x300) + cyllow + 1);
403         SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
404     }
405
406     // everything is ok, so from now on, the emulation is active
407     SET_EBDA2(ebda_seg, cdemu.active, 0x01);
408     dprintf(6, "cdemu media=%d\n", media);
409
410     return 0;
411 }