Annotation of qemu/roms/seabios/src/cdrom.c, revision 1.1.1.6

1.1       root        1: // Support for booting from cdroms (the "El Torito" spec).
                      2: //
                      3: // Copyright (C) 2008,2009  Kevin O'Connor <[email protected]>
                      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
1.1.1.3   root       13: #include "blockcmd.h" // CDB_CMD_REQUEST_SENSE
1.1       root       14: 
                     15: 
                     16: /****************************************************************
                     17:  * CD emulation
                     18:  ****************************************************************/
                     19: 
1.1.1.3   root       20: struct drive_s *cdemu_drive_gf VAR16VISIBLE;
                     21: 
1.1       root       22: static int
                     23: cdemu_read(struct disk_op_s *op)
                     24: {
                     25:     u16 ebda_seg = get_ebda_seg();
1.1.1.3   root       26:     struct drive_s *drive_g;
                     27:     drive_g = GLOBALFLAT2GLOBAL(GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf));
1.1       root       28:     struct disk_op_s dop;
                     29:     dop.drive_g = drive_g;
                     30:     dop.command = op->command;
                     31:     dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + op->lba / 4;
                     32: 
                     33:     int count = op->count;
                     34:     op->count = 0;
1.1.1.5   root       35:     u8 *cdbuf_fl = GET_GLOBAL(bounce_buf_fl);
1.1       root       36: 
                     37:     if (op->lba & 3) {
                     38:         // Partial read of first block.
                     39:         dop.count = 1;
1.1.1.3   root       40:         dop.buf_fl = cdbuf_fl;
1.1       root       41:         int ret = process_op(&dop);
                     42:         if (ret)
                     43:             return ret;
                     44:         u8 thiscount = 4 - (op->lba & 3);
                     45:         if (thiscount > count)
                     46:             thiscount = count;
                     47:         count -= thiscount;
1.1.1.3   root       48:         memcpy_fl(op->buf_fl, cdbuf_fl + (op->lba & 3) * 512, thiscount * 512);
1.1       root       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;
1.1.1.3   root       71:         dop.buf_fl = cdbuf_fl;
1.1       root       72:         int ret = process_op(&dop);
                     73:         if (ret)
                     74:             return ret;
                     75:         u8 thiscount = count;
1.1.1.3   root       76:         memcpy_fl(op->buf_fl, cdbuf_fl, thiscount * 512);
1.1       root       77:         op->count += thiscount;
                     78:     }
                     79: 
                     80:     return DISK_RET_SUCCESS;
                     81: }
                     82: 
                     83: int
                     84: process_cdemu_op(struct disk_op_s *op)
                     85: {
                     86:     if (!CONFIG_CDROM_EMU)
                     87:         return 0;
                     88: 
                     89:     switch (op->command) {
                     90:     case CMD_READ:
                     91:         return cdemu_read(op);
                     92:     case CMD_WRITE:
                     93:     case CMD_FORMAT:
                     94:         return DISK_RET_EWRITEPROTECT;
                     95:     case CMD_VERIFY:
                     96:     case CMD_RESET:
                     97:     case CMD_SEEK:
                     98:     case CMD_ISREADY:
                     99:         return DISK_RET_SUCCESS;
                    100:     default:
                    101:         op->count = 0;
                    102:         return DISK_RET_EPARAM;
                    103:     }
                    104: }
                    105: 
                    106: void
1.1.1.2   root      107: cdemu_setup(void)
1.1       root      108: {
                    109:     if (!CONFIG_CDROM_EMU)
                    110:         return;
1.1.1.4   root      111:     if (!CDCount)
1.1.1.3   root      112:         return;
1.1.1.5   root      113:     if (bounce_buf_init() < 0)
                    114:         return;
1.1       root      115: 
1.1.1.3   root      116:     struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
1.1.1.5   root      117:     if (!drive_g) {
1.1.1.3   root      118:         warn_noalloc();
                    119:         free(drive_g);
1.1       root      120:         return;
                    121:     }
1.1.1.3   root      122:     cdemu_drive_gf = drive_g;
                    123:     memset(drive_g, 0, sizeof(*drive_g));
1.1       root      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));
1.1.1.3   root      157:     struct drive_s *drive_gf = GET_EBDA2(ebda_seg, cdemu.emulated_drive_gf);
                    158:     u8 cntl_id = 0;
                    159:     if (drive_gf)
                    160:         cntl_id = GET_GLOBALFLAT(drive_gf->cntl_id);
1.1       root      161:     SET_INT13ET(regs, controller_index, cntl_id / 2);
                    162:     SET_INT13ET(regs, device_spec, cntl_id % 2);
                    163:     SET_INT13ET(regs, ilba, GET_EBDA2(ebda_seg, cdemu.ilba));
                    164:     SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
                    165:     SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
                    166:     SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
                    167:     SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
                    168:     SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
                    169:     SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
                    170: 
                    171:     // If we have to terminate emulation
                    172:     if (regs->al == 0x00) {
                    173:         // FIXME ElTorito Various. Should be handled accordingly to spec
                    174:         SET_EBDA2(ebda_seg, cdemu.active, 0x00); // bye bye
1.1.1.3   root      175: 
                    176:         // XXX - update floppy/hd count.
1.1       root      177:     }
                    178: 
                    179:     disk_ret(regs, DISK_RET_SUCCESS);
                    180: }
                    181: 
                    182: 
                    183: /****************************************************************
                    184:  * CD booting
                    185:  ****************************************************************/
                    186: 
                    187: int
1.1.1.4   root      188: cdrom_boot(struct drive_s *drive_g)
1.1       root      189: {
1.1.1.3   root      190:     struct disk_op_s dop;
1.1.1.4   root      191:     int cdid = getDriveId(EXTTYPE_CD, drive_g);
1.1.1.3   root      192:     memset(&dop, 0, sizeof(dop));
1.1.1.4   root      193:     dop.drive_g = drive_g;
                    194:     if (!dop.drive_g || cdid < 0)
1.1       root      195:         return 1;
                    196: 
1.1.1.6 ! root      197:     int ret = scsi_is_ready(&dop);
1.1       root      198:     if (ret)
1.1.1.6 ! root      199:         dprintf(1, "scsi_is_ready returned %d\n", ret);
1.1       root      200: 
                    201:     // Read the Boot Record Volume Descriptor
                    202:     u8 buffer[2048];
                    203:     dop.lba = 0x11;
                    204:     dop.count = 1;
                    205:     dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
1.1.1.3   root      206:     ret = cdb_read(&dop);
1.1       root      207:     if (ret)
                    208:         return 3;
                    209: 
                    210:     // Validity checks
                    211:     if (buffer[0])
                    212:         return 4;
                    213:     if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
                    214:         return 5;
                    215: 
                    216:     // ok, now we calculate the Boot catalog address
                    217:     u32 lba = *(u32*)&buffer[0x47];
                    218: 
                    219:     // And we read the Boot Catalog
                    220:     dop.lba = lba;
1.1.1.3   root      221:     dop.count = 1;
                    222:     ret = cdb_read(&dop);
1.1       root      223:     if (ret)
                    224:         return 7;
                    225: 
                    226:     // Validation entry
                    227:     if (buffer[0x00] != 0x01)
                    228:         return 8;   // Header
                    229:     if (buffer[0x01] != 0x00)
                    230:         return 9;   // Platform
                    231:     if (buffer[0x1E] != 0x55)
                    232:         return 10;  // key 1
                    233:     if (buffer[0x1F] != 0xAA)
                    234:         return 10;  // key 2
                    235: 
                    236:     // Initial/Default Entry
                    237:     if (buffer[0x20] != 0x88)
                    238:         return 11; // Bootable
                    239: 
                    240:     u16 ebda_seg = get_ebda_seg();
                    241:     u8 media = buffer[0x21];
                    242:     SET_EBDA2(ebda_seg, cdemu.media, media);
                    243: 
1.1.1.3   root      244:     SET_EBDA2(ebda_seg, cdemu.emulated_drive_gf, dop.drive_g);
1.1       root      245: 
                    246:     u16 boot_segment = *(u16*)&buffer[0x22];
                    247:     if (!boot_segment)
                    248:         boot_segment = 0x07C0;
                    249:     SET_EBDA2(ebda_seg, cdemu.load_segment, boot_segment);
                    250:     SET_EBDA2(ebda_seg, cdemu.buffer_segment, 0x0000);
                    251: 
                    252:     u16 nbsectors = *(u16*)&buffer[0x26];
                    253:     SET_EBDA2(ebda_seg, cdemu.sector_count, nbsectors);
                    254: 
                    255:     lba = *(u32*)&buffer[0x28];
                    256:     SET_EBDA2(ebda_seg, cdemu.ilba, lba);
                    257: 
                    258:     // And we read the image in memory
                    259:     dop.lba = lba;
                    260:     dop.count = DIV_ROUND_UP(nbsectors, 4);
                    261:     dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
1.1.1.3   root      262:     ret = cdb_read(&dop);
1.1       root      263:     if (ret)
                    264:         return 12;
                    265: 
                    266:     if (media == 0) {
                    267:         // No emulation requested - return success.
                    268:         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, EXTSTART_CD + cdid);
                    269:         return 0;
                    270:     }
                    271: 
                    272:     // Emulation of a floppy/harddisk requested
1.1.1.3   root      273:     if (! CONFIG_CDROM_EMU || !cdemu_drive_gf)
1.1       root      274:         return 13;
                    275: 
                    276:     // Set emulated drive id and increase bios installed hardware
                    277:     // number of devices
                    278:     if (media < 4) {
                    279:         // Floppy emulation
                    280:         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x00);
1.1.1.3   root      281:         // XXX - get and set actual floppy count.
1.1       root      282:         SETBITS_BDA(equipment_list_flags, 0x41);
                    283: 
                    284:         switch (media) {
                    285:         case 0x01:  // 1.2M floppy
                    286:             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
                    287:             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
                    288:             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
                    289:             break;
                    290:         case 0x02:  // 1.44M floppy
                    291:             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
                    292:             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
                    293:             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
                    294:             break;
                    295:         case 0x03:  // 2.88M floppy
                    296:             SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
                    297:             SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
                    298:             SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
                    299:             break;
                    300:         }
                    301:     } else {
                    302:         // Harddrive emulation
                    303:         SET_EBDA2(ebda_seg, cdemu.emulated_extdrive, 0x80);
                    304:         SET_BDA(hdcount, GET_BDA(hdcount) + 1);
                    305: 
                    306:         // Peak at partition table to get chs.
                    307:         struct mbr_s *mbr = (void*)0;
                    308:         u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
                    309:         u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
                    310:         u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
                    311: 
                    312:         SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
                    313:         SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
                    314:                   , ((sptcyl<<2)&0x300) + cyllow + 1);
                    315:         SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
                    316:     }
                    317: 
                    318:     // everything is ok, so from now on, the emulation is active
                    319:     SET_EBDA2(ebda_seg, cdemu.active, 0x01);
                    320:     dprintf(6, "cdemu media=%d\n", media);
                    321: 
                    322:     return 0;
                    323: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.