/* * 86Box A hypervisor and IBM PC system emulator that specializes in * running old operating systems and software designed for IBM * PC systems and compatibles from 1981 through fairly recent * system designs based on the PCI bus. * * This file is part of the 86Box distribution. * * CD-ROM image support. * * * * Authors: RichardG867, * Miran Grca, * bit, * * Copyright 2015-2019 Richardg867. * Copyright 2015-2019 Miran Grca. * Copyright 2017-2019 bit. */ #include #include #include #include #include #include #include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/config.h> #include <86box/path.h> #include <86box/plat.h> #include <86box/scsi_device.h> #include <86box/cdrom_image_backend.h> #include <86box/cdrom.h> #include <86box/cdrom_image.h> #ifdef ENABLE_CDROM_IMAGE_LOG int cdrom_image_do_log = ENABLE_CDROM_IMAGE_LOG; void cdrom_image_log(const char *fmt, ...) { va_list ap; if (cdrom_image_do_log) { va_start(ap, fmt); pclog_ex(fmt, ap); va_end(ap); } } #else # define cdrom_image_log(fmt, ...) #endif /* The addresses sent from the guest are absolute, ie. a LBA of 0 corresponds to a MSF of 00:00:00. Otherwise, the counter displayed by the guest is wrong: there is a seeming 2 seconds in which audio plays but counter does not move, while a data track before audio jumps to 2 seconds before the actual start of the audio while audio still plays. With an absolute conversion, the counter is fine. */ #define MSFtoLBA(m, s, f) ((((m * 60) + s) * 75) + f) static void image_get_track_info(cdrom_t *dev, uint32_t track, int end, track_info_t *ti) { cd_img_t *img = (cd_img_t *) dev->local; track_t *ct = NULL; for (int i = 0; i < img->tracks_num; i++) { ct = &(img->tracks[i]); if (ct->point == track) break; } ti->number = ct->point; if (ct == NULL) { ti->attr = 0x14; ti->m = 0; ti->s = 2; ti->f = 0; } else { uint32_t pos = end ? ct->idx[1].start : (ct->idx[1].start + ct->idx[1].length); ti->attr = ct->attr; ti->m = (pos / 75) / 60; ti->s = (pos / 75) % 60; ti->f = pos % 75; } } static void image_get_raw_track_info(cdrom_t *dev, int *num, raw_track_info_t *rti) { cdi_get_raw_track_info((cd_img_t *) dev->local, num, (uint8_t *) rti); } static void image_get_subchannel(cdrom_t *dev, uint32_t lba, subchannel_t *subc) { cd_img_t *img = (cd_img_t *) dev->local; TMSF rel_pos; TMSF abs_pos; cdi_get_audio_sub(img, lba, &subc->attr, &subc->track, &subc->index, &rel_pos, &abs_pos); subc->abs_m = abs_pos.min; subc->abs_s = abs_pos.sec; subc->abs_f = abs_pos.fr; subc->rel_m = rel_pos.min; subc->rel_s = rel_pos.sec; subc->rel_f = rel_pos.fr; cdrom_image_log("image_get_subchannel(): %02X, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", subc->attr, subc->track, subc->index, subc->abs_m, subc->abs_s, subc->abs_f, subc->rel_m, subc->rel_s, subc->rel_f); } static int image_get_capacity(cdrom_t *dev) { cd_img_t *img = (cd_img_t *) dev->local; uint32_t lb = 0; track_t *lo = NULL; if (!img) return 0; for (int i = (img->tracks_num - 1); i >= 0; i--) { if (img->tracks[i].point == 0xa2) { lo = &(img->tracks[i]); break; } } if (lo != NULL) lb = lo->idx[1].start - 1; return lb; } static int image_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) { cd_img_t *img = (cd_img_t *) dev->local; int m; int s; int f; if (!img || (dev->cd_status == CD_STATUS_DATA_ONLY)) return 0; if (ismsf) { m = (pos >> 16) & 0xff; s = (pos >> 8) & 0xff; f = pos & 0xff; pos = MSFtoLBA(m, s, f) - 150; } return cdi_is_audio(img, pos); } static int image_is_track_pre(cdrom_t *dev, uint32_t lba) { cd_img_t *img = (cd_img_t *) dev->local; if (!img || (dev->cd_status == CD_STATUS_DATA_ONLY)) return 0; return cdi_is_pre(img, lba); } static int image_sector_size(struct cdrom *dev, uint32_t lba) { cd_img_t *img = (cd_img_t *) dev->local; return cdi_get_sector_size(img, lba); } static int image_read_sector(struct cdrom *dev, uint8_t *b, uint32_t lba) { cd_img_t *img = (cd_img_t *) dev->local; if (cdi_get_sector_size(img, lba) <= 2352) return cdi_read_sector(img, b, 1, lba); else return cdi_read_sector_sub(img, b, lba); } static int image_track_type(cdrom_t *dev, uint32_t lba) { cd_img_t *img = (cd_img_t *) dev->local; if (img) { if (image_is_track_audio(dev, lba, 0)) return CD_TRACK_AUDIO; else if (cdi_is_mode2(img, lba)) return CD_TRACK_MODE2 | cdi_get_mode2_form(img, lba); } return 0; } static int image_ext_medium_changed(cdrom_t *dev) { return 0; } static void image_exit(cdrom_t *dev) { cd_img_t *img = (cd_img_t *) dev->local; cdrom_image_log("CDROM: image_exit(%s)\n", dev->image_path); dev->cd_status = CD_STATUS_EMPTY; if (img) { cdi_close(img); dev->local = NULL; } dev->ops = NULL; } static const cdrom_ops_t cdrom_image_ops = { image_get_track_info, image_get_raw_track_info, image_get_subchannel, image_is_track_pre, image_sector_size, image_read_sector, image_track_type, image_ext_medium_changed, image_exit }; static int image_open_abort(cdrom_t *dev) { cdrom_image_close(dev); dev->ops = NULL; dev->image_path[0] = 0; return 1; } int cdrom_image_open(cdrom_t *dev, const char *fn) { cd_img_t *img; /* Make sure to not STRCPY if the two are pointing at the same place. */ if (fn != dev->image_path) strcpy(dev->image_path, fn); /* Create new instance of the CDROM_Image class. */ img = (cd_img_t *) calloc(1, sizeof(cd_img_t)); /* This guarantees that if ops is not NULL, then neither is the image pointer. */ if (img == NULL) return image_open_abort(dev); dev->local = img; /* Open the image. */ int i = cdi_set_device(img, fn); if (!i) return image_open_abort(dev); /* All good, reset state. */ if (i >= 2) dev->cd_status = CD_STATUS_DATA_ONLY; else dev->cd_status = CD_STATUS_STOPPED; dev->seek_pos = 0; dev->cd_buflen = 0; dev->cdrom_capacity = image_get_capacity(dev); cdrom_image_log("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); #ifdef ENABLE_CDROM_IMAGE_LOG int cm, cs, cf; cf = dev->cdrom_capacity % 75; cs = (dev->cdrom_capacity / 75) % 60; cm = (dev->cdrom_capacity / 75) / 60; cdrom_image_log("CD-ROM capacity: %i sectors (%" PRIi64 " bytes) (time: %02i:%02i:%02i)\n", dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity - 150ULL) * 2352ULL, cm, cs, cf); #endif /* Attach this handler to the drive. */ dev->ops = &cdrom_image_ops; return 0; } void cdrom_image_close(cdrom_t *dev) { cdrom_image_log("CDROM: image_close(%s)\n", dev->image_path); if (dev && dev->ops && dev->ops->exit) dev->ops->exit(dev); }