diff --git a/src/cdrom/CMakeLists.txt b/src/cdrom/CMakeLists.txt index 201cee7f6..d3b38095e 100644 --- a/src/cdrom/CMakeLists.txt +++ b/src/cdrom/CMakeLists.txt @@ -13,4 +13,4 @@ # Copyright 2020-2021 David Hrdlička. # -add_library(cdrom OBJECT cdrom.c cdrom_image_backend.c cdrom_image_viso.c cdrom_image.c cdrom_mitsumi.c) +add_library(cdrom OBJECT cdrom.c cdrom_image_backend.c cdrom_image_viso.c cdrom_image.c cdrom_ioctl.c cdrom_mitsumi.c) diff --git a/src/cdrom/cdrom.c b/src/cdrom/cdrom.c index e4e76ea66..88811c016 100644 --- a/src/cdrom/cdrom.c +++ b/src/cdrom/cdrom.c @@ -1961,7 +1961,7 @@ cdrom_hard_reset(void) dev->cd_status = CD_STATUS_EMPTY; - if (dev->host_drive == 200) { + if (strlen(dev->image_path) > 0) { #ifdef _WIN32 if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) dev->image_path[strlen(dev->image_path) - 1] = '\\'; @@ -1971,7 +1971,10 @@ cdrom_hard_reset(void) dev->image_path[strlen(dev->image_path) - 1] = '/'; #endif - cdrom_image_open(dev, dev->image_path); + if ((strlen(dev->image_path) != 0) && (strstr(dev->image_path, "ioctl://") == dev->image_path)) + cdrom_ioctl_open(dev, dev->image_path); + else + cdrom_image_open(dev, dev->image_path); } } } @@ -2020,16 +2023,12 @@ cdrom_eject(uint8_t id) cdrom_t *dev = &cdrom[id]; /* This entire block should be in cdrom.c/cdrom_eject(dev*) ... */ - if (dev->host_drive == 0) { + if (strlen(dev->image_path) == 0) { /* Switch from empty to empty. Do nothing. */ return; } - if (dev->host_drive == 200) - strcpy(dev->prev_image_path, dev->image_path); - - dev->prev_host_drive = dev->host_drive; - dev->host_drive = 0; + strcpy(dev->prev_image_path, dev->image_path); dev->ops->exit(dev); dev->ops = NULL; @@ -2048,7 +2047,7 @@ cdrom_reload(uint8_t id) { cdrom_t *dev = &cdrom[id]; - if ((dev->host_drive == dev->prev_host_drive) || (dev->prev_host_drive == 0) || (dev->host_drive != 0)) { + if ((strcmp(dev->image_path, dev->prev_image_path) == 0) || (strlen(dev->prev_image_path) == 0) || (strlen(dev->image_path) > 0)) { /* Switch from empty to empty. Do nothing. */ return; } @@ -2058,26 +2057,29 @@ cdrom_reload(uint8_t id) dev->ops = NULL; memset(dev->image_path, 0, sizeof(dev->image_path)); - if (dev->prev_host_drive == 200) { + if (strlen(dev->image_path) > 0) { /* Reload a previous image. */ - strcpy(dev->image_path, dev->prev_image_path); + if (strlen(dev->prev_image_path) > 0) + strcpy(dev->image_path, dev->prev_image_path); #ifdef _WIN32 - if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) - dev->image_path[strlen(dev->image_path) - 1] = '\\'; + if (strlen(dev->prev_image_path) > 0) { + if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '/')) + dev->image_path[strlen(dev->image_path) - 1] = '\\'; + } #else - if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '\\')) - dev->image_path[strlen(dev->image_path) - 1] = '/'; + if (strlen(dev->prev_image_path) > 0) { + if ((strlen(dev->image_path) >= 1) && (dev->image_path[strlen(dev->image_path) - 1] == '\\')) + dev->image_path[strlen(dev->image_path) - 1] = '/'; + } #endif - cdrom_image_open(dev, dev->image_path); + if ((strlen(dev->image_path) != 0) && (strstr(dev->image_path, "ioctl://") == dev->image_path)) + cdrom_ioctl_open(dev, dev->image_path); + else + cdrom_image_open(dev, dev->image_path); cdrom_insert(id); - - if (strlen(dev->image_path) == 0) - dev->host_drive = 0; - else - dev->host_drive = 200; } plat_cdrom_ui_update(id, 1); diff --git a/src/cdrom/cdrom_image.c b/src/cdrom/cdrom_image.c index 0c7ee03a0..2203674cd 100644 --- a/src/cdrom/cdrom_image.c +++ b/src/cdrom/cdrom_image.c @@ -97,6 +97,9 @@ image_get_subchannel(cdrom_t *dev, uint32_t lba, subchannel_t *subc) 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 @@ -217,6 +220,12 @@ image_track_type(cdrom_t *dev, uint32_t lba) return 0; } +static int +image_ext_medium_changed(cdrom_t *dev) +{ + return 0; +} + static void image_exit(cdrom_t *dev) { @@ -241,6 +250,7 @@ static const cdrom_ops_t cdrom_image_ops = { image_sector_size, image_read_sector, image_track_type, + image_ext_medium_changed, image_exit }; @@ -249,7 +259,6 @@ image_open_abort(cdrom_t *dev) { cdrom_image_close(dev); dev->ops = NULL; - dev->host_drive = 0; dev->image_path[0] = 0; return 1; } diff --git a/src/cdrom/cdrom_ioctl.c b/src/cdrom/cdrom_ioctl.c new file mode 100644 index 000000000..a204fad0f --- /dev/null +++ b/src/cdrom/cdrom_ioctl.c @@ -0,0 +1,278 @@ +/* + * 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 passthrough support. + * + * + * + * Authors: TheCollector1995, , + * Miran Grca, + * + * Copyright 2023 TheCollector1995. + * Copyright 2023 Miran Grca. + */ +#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/plat_cdrom.h> +#include <86box/scsi_device.h> +#include <86box/cdrom.h> + +#ifdef ENABLE_CDROM_IOCTL_LOG +int cdrom_ioctl_do_log = ENABLE_CDROM_IOCTL_LOG; + +void +cdrom_ioctl_log(const char *fmt, ...) +{ + va_list ap; + + if (cdrom_ioctl_do_log) { + va_start(ap, fmt); + pclog_ex(fmt, ap); + va_end(ap); + } +} +#else +# define cdrom_ioctl_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 +ioctl_get_tracks(UNUSED(cdrom_t *dev), int *first, int *last) +{ + TMSF tmsf; + + plat_cdrom_get_audio_tracks(first, last, &tmsf); +} + +static void +ioctl_get_track_info(UNUSED(cdrom_t *dev), uint32_t track, int end, track_info_t *ti) +{ + TMSF tmsf; + + plat_cdrom_get_audio_track_info(end, track, &ti->number, &tmsf, &ti->attr); + + ti->m = tmsf.min; + ti->s = tmsf.sec; + ti->f = tmsf.fr; +} + +static void +ioctl_get_subchannel(UNUSED(cdrom_t *dev), uint32_t lba, subchannel_t *subc) +{ + TMSF rel_pos; + TMSF abs_pos; + + if ((dev->cd_status == CD_STATUS_PLAYING) || (dev->cd_status == CD_STATUS_PAUSED)) { + const uint32_t trk = plat_cdrom_get_track_start(lba, &subc->attr, &subc->track); + + FRAMES_TO_MSF(lba + 150, &abs_pos.min, &abs_pos.sec, &abs_pos.fr); + + /* Absolute position should be adjusted by 150, not the relative ones. */ + FRAMES_TO_MSF(lba - trk, &rel_pos.min, &rel_pos.sec, &rel_pos.fr); + + subc->index = 1; + } else + plat_cdrom_get_audio_sub(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_ioctl_log("ioctl_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 +ioctl_get_capacity(UNUSED(cdrom_t *dev)) +{ + int ret; + + ret = plat_cdrom_get_last_block(); + cdrom_ioctl_log("GetCapacity=%x.\n", ret); + return ret; +} + +static int +ioctl_is_track_audio(cdrom_t *dev, uint32_t pos, int ismsf) +{ + int m; + int s; + int f; + + if (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; + } + + /* GetTrack requires LBA. */ + return plat_cdrom_is_track_audio(pos); +} + +static int +ioctl_is_track_pre(UNUSED(cdrom_t *dev), uint32_t lba) +{ + return plat_cdrom_is_track_pre(lba); +} + +static int +ioctl_sector_size(UNUSED(cdrom_t *dev), uint32_t lba) +{ + cdrom_ioctl_log("LBA=%x.\n", lba); + return plat_cdrom_get_sector_size(lba); +} + +static int +ioctl_read_sector(UNUSED(cdrom_t *dev), int type, uint8_t *b, uint32_t lba) +{ + switch (type) { + case CD_READ_DATA: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Data.\n"); + return plat_cdrom_read_sector(b, 0, lba); + case CD_READ_AUDIO: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Audio.\n"); + return plat_cdrom_read_sector(b, 1, lba); + case CD_READ_RAW: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Raw.\n"); + return plat_cdrom_read_sector(b, 1, lba); + default: + cdrom_ioctl_log("cdrom_ioctl_read_sector(): Unknown CD read type.\n"); + break; + } + return 0; +} + +static int +ioctl_track_type(cdrom_t *dev, uint32_t lba) +{ + int ret = 0; + + if (ioctl_is_track_audio(dev, lba, 0)) + ret = CD_TRACK_AUDIO; + + cdrom_ioctl_log("cdrom_ioctl_track_type(): %i\n", ret); + + return ret; +} + +static int +ioctl_ext_medium_changed(cdrom_t *dev) +{ + int ret; + + if ((dev->cd_status == CD_STATUS_PLAYING) || (dev->cd_status == CD_STATUS_PAUSED)) + ret = 0; + else + ret = plat_cdrom_ext_medium_changed(); + + if (ret == 1) { + dev->cd_status = CD_STATUS_STOPPED; + dev->cdrom_capacity = ioctl_get_capacity(dev); + } else if (ret == -1) + dev->cd_status = CD_STATUS_EMPTY; + + return ret; +} + +static void +ioctl_exit(cdrom_t *dev) +{ + cdrom_ioctl_log("CDROM: ioctl_exit(%s)\n", dev->image_path); + dev->cd_status = CD_STATUS_EMPTY; + + plat_cdrom_close(); + + dev->ops = NULL; +} + +static const cdrom_ops_t cdrom_ioctl_ops = { + ioctl_get_tracks, + ioctl_get_track_info, + ioctl_get_subchannel, + ioctl_is_track_pre, + ioctl_sector_size, + ioctl_read_sector, + ioctl_track_type, + ioctl_ext_medium_changed, + ioctl_exit +}; + +static int +cdrom_ioctl_open_abort(cdrom_t *dev) +{ + cdrom_ioctl_close(dev); + dev->ops = NULL; + dev->image_path[0] = 0; + return 1; +} + +int +cdrom_ioctl_open(cdrom_t *dev, const char *drv) +{ + const char *actual_drv = &(drv[8]); + + /* Make sure to not STRCPY if the two are pointing + at the same place. */ + if (drv != dev->image_path) + strcpy(dev->image_path, drv); + + /* Open the image. */ + if (strstr(drv, "ioctl://") != drv) + return cdrom_ioctl_open_abort(dev); + cdrom_ioctl_log("actual_drv = %s\n", actual_drv); + int i = plat_cdrom_set_drive(actual_drv); + if (!i) + return cdrom_ioctl_open_abort(dev); + + /* All good, reset state. */ + dev->cd_status = CD_STATUS_STOPPED; + dev->is_dir = 0; + dev->seek_pos = 0; + dev->cd_buflen = 0; + dev->cdrom_capacity = ioctl_get_capacity(dev); + cdrom_ioctl_log("CD-ROM capacity: %i sectors (%" PRIi64 " bytes)\n", + dev->cdrom_capacity, ((uint64_t) dev->cdrom_capacity) << 11ULL); + + /* Attach this handler to the drive. */ + dev->ops = &cdrom_ioctl_ops; + + return 0; +} + +void +cdrom_ioctl_close(cdrom_t *dev) +{ + cdrom_ioctl_log("CDROM: ioctl_close(%s)\n", dev->image_path); + + if (dev && dev->ops && dev->ops->exit) + dev->ops->exit(dev); +} + diff --git a/src/cdrom/cdrom_mitsumi.c b/src/cdrom/cdrom_mitsumi.c index 7f4d2645b..01a9cf047 100644 --- a/src/cdrom/cdrom_mitsumi.c +++ b/src/cdrom/cdrom_mitsumi.c @@ -147,14 +147,18 @@ mitsumi_cdrom_log(const char *fmt, ...) # define mitsumi_cdrom_log(fmt, ...) #endif +static int +mitsumi_cdrom_is_ready(const cdrom_t *dev) +{ + return (dev->image_path[0] != 0x00); +} + static void mitsumi_cdrom_reset(mcd_t *dev) { cdrom_t cdrom; - cdrom.host_drive = 0; - - dev->stat = cdrom.host_drive ? (STAT_READY | STAT_CHANGE) : 0; + dev->stat = mitsumi_cdrom_is_ready(&cdrom) ? (STAT_READY | STAT_CHANGE) : 0; dev->cmdrd_count = 0; dev->cmdbuf_count = 0; dev->buf_count = 0; @@ -344,18 +348,18 @@ mitsumi_cdrom_out(uint16_t port, uint8_t val, void *priv) break; } if (!dev->cmdrd_count) - dev->stat = cdrom.host_drive ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; + dev->stat = mitsumi_cdrom_is_ready(&cdrom) ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; return; } dev->cmd = val; dev->cmdbuf_idx = 0; dev->cmdrd_count = 0; dev->cmdbuf_count = 1; - dev->cmdbuf[0] = cdrom.host_drive ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; + dev->cmdbuf[0] = mitsumi_cdrom_is_ready(&cdrom) ? (STAT_READY | (dev->change ? STAT_CHANGE : 0)) : 0; dev->data = 0; switch (val) { case CMD_GET_INFO: - if (cdrom.host_drive) { + if (mitsumi_cdrom_is_ready(&cdrom)) { cdrom_get_track_buffer(&cdrom, &(dev->cmdbuf[1])); dev->cmdbuf_count = 10; dev->readcount = 0; @@ -365,7 +369,7 @@ mitsumi_cdrom_out(uint16_t port, uint8_t val, void *priv) } break; case CMD_GET_Q: - if (cdrom.host_drive) { + if (mitsumi_cdrom_is_ready(&cdrom)) { cdrom_get_q(&cdrom, &(dev->cmdbuf[1]), &dev->cur_toc_track, dev->mode & MODE_GET_TOC); dev->cmdbuf_count = 11; dev->readcount = 0; @@ -391,7 +395,7 @@ mitsumi_cdrom_out(uint16_t port, uint8_t val, void *priv) break; case CMD_READ1X: case CMD_READ2X: - if (cdrom.host_drive) { + if (mitsumi_cdrom_is_ready(&cdrom)) { dev->readcount = 0; dev->drvmode = (val == CMD_READ1X) ? DRV_MODE_CDDA : DRV_MODE_READ; dev->cmdrd_count = 6; diff --git a/src/config.c b/src/config.c index 02c1d276a..77ec89125 100644 --- a/src/config.c +++ b/src/config.c @@ -1175,8 +1175,7 @@ load_floppy_and_cdrom_drives(void) memset(temp, 0x00, sizeof(temp)); for (c = 0; c < CDROM_NUM; c++) { sprintf(temp, "cdrom_%02i_host_drive", c + 1); - cdrom[c].host_drive = ini_section_get_int(cat, temp, 0); - cdrom[c].prev_host_drive = cdrom[c].host_drive; + ini_section_delete_var(cat, temp); sprintf(temp, "cdrom_%02i_parameters", c + 1); p = ini_section_get_string(cat, temp, NULL); @@ -1263,12 +1262,6 @@ load_floppy_and_cdrom_drives(void) path_normalize(cdrom[c].image_path); } - if (cdrom[c].host_drive && (cdrom[c].host_drive != 200)) - cdrom[c].host_drive = 0; - - if ((cdrom[c].host_drive == 0x200) && (strlen(cdrom[c].image_path) == 0)) - cdrom[c].host_drive = 0; - for (int i = 0; i < MAX_PREV_IMAGES; i++) { cdrom[c].image_history[i] = (char *) calloc((MAX_IMAGE_PATH_LEN + 1) << 1, sizeof(char)); sprintf(temp, "cdrom_%02i_image_history_%02i", c + 1, i + 1); @@ -1289,9 +1282,6 @@ load_floppy_and_cdrom_drives(void) /* If the CD-ROM is disabled, delete all its variables. */ if (cdrom[c].bus_type == CDROM_BUS_DISABLED) { - sprintf(temp, "cdrom_%02i_host_drive", c + 1); - ini_section_delete_var(cat, temp); - sprintf(temp, "cdrom_%02i_parameters", c + 1); ini_section_delete_var(cat, temp); @@ -1422,9 +1412,6 @@ load_other_removable_devices(void) /* If the ZIP drive is disabled, delete all its variables. */ if (zip_drives[c].bus_type == ZIP_BUS_DISABLED) { - sprintf(temp, "zip_%02i_host_drive", c + 1); - ini_section_delete_var(cat, temp); - sprintf(temp, "zip_%02i_parameters", c + 1); ini_section_delete_var(cat, temp); @@ -1538,9 +1525,6 @@ load_other_removable_devices(void) /* If the MO drive is disabled, delete all its variables. */ if (mo_drives[c].bus_type == MO_BUS_DISABLED) { - sprintf(temp, "mo_%02i_host_drive", c + 1); - ini_section_delete_var(cat, temp); - sprintf(temp, "mo_%02i_parameters", c + 1); ini_section_delete_var(cat, temp); @@ -2575,10 +2559,7 @@ save_floppy_and_cdrom_drives(void) for (c = 0; c < CDROM_NUM; c++) { sprintf(temp, "cdrom_%02i_host_drive", c + 1); - if ((cdrom[c].bus_type == 0) || (cdrom[c].host_drive != 200)) - ini_section_delete_var(cat, temp); - else - ini_section_set_int(cat, temp, cdrom[c].host_drive); + ini_section_delete_var(cat, temp); sprintf(temp, "cdrom_%02i_speed", c + 1); if ((cdrom[c].bus_type == 0) || (cdrom[c].speed == 8)) diff --git a/src/include/86box/cdrom.h b/src/include/86box/cdrom.h index 15127b06e..b3f5a5ea5 100644 --- a/src/include/86box/cdrom.h +++ b/src/include/86box/cdrom.h @@ -199,10 +199,11 @@ typedef struct cdrom_ops_t { void (*get_tracks)(struct cdrom *dev, int *first, int *last); void (*get_track_info)(struct cdrom *dev, uint32_t track, int end, track_info_t *ti); void (*get_subchannel)(struct cdrom *dev, uint32_t lba, subchannel_t *subc); - int (*is_track_pre)(struct cdrom *dev, uint32_t lba); - int (*sector_size)(struct cdrom *dev, uint32_t lba); - int (*read_sector)(struct cdrom *dev, int type, uint8_t *b, uint32_t lba); - int (*track_type)(struct cdrom *dev, uint32_t lba); + int (*is_track_pre)(struct cdrom *dev, uint32_t lba); + int (*sector_size)(struct cdrom *dev, uint32_t lba); + int (*read_sector)(struct cdrom *dev, int type, uint8_t *b, uint32_t lba); + int (*track_type)(struct cdrom *dev, uint32_t lba); + int (*ext_medium_changed)(struct cdrom *dev); void (*exit)(struct cdrom *dev); } cdrom_ops_t; @@ -240,8 +241,6 @@ typedef struct cdrom { uint32_t cd_end; uint32_t type; - int host_drive; - int prev_host_drive; int cd_buflen; int audio_op; int audio_muted_soft; @@ -305,11 +304,9 @@ extern void cdrom_reload(uint8_t id); extern int cdrom_image_open(cdrom_t *dev, const char *fn); extern void cdrom_image_close(cdrom_t *dev); -extern void cdrom_image_reset(cdrom_t *dev); -extern void cdrom_ioctl_eject(void); -extern void cdrom_ioctl_load(void); -extern int cdrom_ioctl_open(cdrom_t *dev, const char d); +extern int cdrom_ioctl_open(cdrom_t *dev, const char *drv); +extern void cdrom_ioctl_close(cdrom_t *dev); extern void cdrom_update_cdb(uint8_t *cdb, int lba_pos, int number_of_blocks); diff --git a/src/include/86box/cdrom_ioctl.h b/src/include/86box/cdrom_ioctl.h new file mode 100644 index 000000000..5fc157615 --- /dev/null +++ b/src/include/86box/cdrom_ioctl.h @@ -0,0 +1,32 @@ +/* + * 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 file handling module header, translated to C + * from cdrom_dosbox.h. + * + * Authors: RichardG, + * Miran Grca, + * + * Copyright 2016-2022 RichardG. + * Copyright 2016-2022 Miran Grca. + */ +#ifndef CDROM_IOCTL_H +#define CDROM_IOCTL_H + +/* this header file lists the functions provided by + various platform specific cdrom-ioctl files */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /*CDROM_IOCTL_H*/ diff --git a/src/include/86box/plat_cdrom.h b/src/include/86box/plat_cdrom.h new file mode 100644 index 000000000..8e81f9459 --- /dev/null +++ b/src/include/86box/plat_cdrom.h @@ -0,0 +1,70 @@ +/* + * 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. + * + * Definitions for platform specific serial to host passthrough. + * + * + * Authors: Andreas J. Reichel , + * Jasmine Iwanek + * + * Copyright 2021 Andreas J. Reichel. + * Copyright 2021-2022 Jasmine Iwanek. + */ + +#ifndef PLAT_CDROM_H +#define PLAT_CDROM_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define RAW_SECTOR_SIZE 2352 +#define COOKED_SECTOR_SIZE 2048 + +#define DATA_TRACK 0x14 +#define AUDIO_TRACK 0x10 + +#define CD_FPS 75 +#define FRAMES_TO_MSF(f, M, S, F) \ + { \ + uint64_t value = f; \ + *(F) = (value % CD_FPS) & 0xff; \ + value /= CD_FPS; \ + *(S) = (value % 60) & 0xff; \ + value /= 60; \ + *(M) = value & 0xff; \ + } +#define MSF_TO_FRAMES(M, S, F) ((M) *60 * CD_FPS + (S) *CD_FPS + (F)) + +typedef struct SMSF { + uint16_t min; + uint8_t sec; + uint8_t fr; +} TMSF; + +extern int plat_cdrom_is_track_audio(uint32_t sector); +extern int plat_cdrom_is_track_pre(uint32_t sector); +extern uint32_t plat_cdrom_get_last_block(void); +extern void plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out); +extern int plat_cdrom_get_audio_track_info(int end, int track, int *track_num, TMSF *start, uint8_t *attr); +extern int plat_cdrom_get_audio_sub(uint32_t sector, uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos); +extern int plat_cdrom_get_sector_size(uint32_t sector); +extern int plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector); +extern void plat_cdrom_eject(void); +extern void plat_cdrom_close(void); +extern int plat_cdrom_set_drive(const char *drv); +extern int plat_cdrom_ext_medium_changed(void); +extern uint32_t plat_cdrom_get_track_start(uint32_t sector, uint8_t *attr, uint8_t *track); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/machine_status.c b/src/machine_status.c index 65f4704b4..16976fa83 100644 --- a/src/machine_status.c +++ b/src/machine_status.c @@ -33,7 +33,7 @@ machine_status_init(void) machine_status.fdd[i].active = false; } for (size_t i = 0; i < CDROM_NUM; ++i) { - machine_status.cdrom[i].empty = cdrom[i].host_drive != 200 || (strlen(cdrom[i].image_path) == 0); + machine_status.cdrom[i].empty = (strlen(cdrom[i].image_path) == 0); machine_status.cdrom[i].active = false; } for (size_t i = 0; i < ZIP_NUM; i++) { @@ -55,4 +55,4 @@ machine_status_init(void) machine_status.net[i].active = false; machine_status.net[i].empty = !network_is_connected(i); } -} \ No newline at end of file +} diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index 422989a44..ebaa84be1 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -225,6 +225,10 @@ else() target_sources(plat PRIVATE ../unix/unix_serial_passthrough.c ../unix/unix_netsocket.c) endif() +if(WIN32) + target_sources(plat PRIVATE win_cdrom_ioctl.c) +endif() + if (APPLE) target_sources(ui PRIVATE macos_event_filter.mm) if(MOLTENVK) diff --git a/src/qt/qt_machinestatus.cpp b/src/qt/qt_machinestatus.cpp index 53e875e33..a074f556e 100644 --- a/src/qt/qt_machinestatus.cpp +++ b/src/qt/qt_machinestatus.cpp @@ -533,7 +533,7 @@ MachineStatus::refresh(QStatusBar *sbar) iterateCDROM([this, sbar](int i) { d->cdrom[i].label = std::make_unique(); - d->cdrom[i].setEmpty(cdrom[i].host_drive != 200 || QString(cdrom[i].image_path).isEmpty()); + d->cdrom[i].setEmpty(QString(cdrom[i].image_path).isEmpty()); d->cdrom[i].setActive(false); d->cdrom[i].refresh(); connect((ClickableLabel *) d->cdrom[i].label.get(), &ClickableLabel::clicked, [i](QPoint pos) { diff --git a/src/qt/qt_mediahistorymanager.cpp b/src/qt/qt_mediahistorymanager.cpp index 9d003f464..672ccfa8d 100644 --- a/src/qt/qt_mediahistorymanager.cpp +++ b/src/qt/qt_mediahistorymanager.cpp @@ -306,7 +306,7 @@ MediaHistoryManager::removeMissingImages(device_index_list_t &device_history) } // For this check, explicitly prepend `usr_path` to relative paths to account for $CWD platform variances QFileInfo absolute_path = file_info.isRelative() ? QFileInfo(getUsrPath().append(file_info.filePath())) : file_info; - if (!absolute_path.exists()) { + if ((file_info.filePath().left(8) != "ioctl://") && !absolute_path.exists()) { qWarning("Image file %s does not exist - removing from history", qPrintable(file_info.filePath())); checked_path = ""; } diff --git a/src/qt/qt_mediamenu.cpp b/src/qt/qt_mediamenu.cpp index c4fd50567..d9d6b6c31 100644 --- a/src/qt/qt_mediamenu.cpp +++ b/src/qt/qt_mediamenu.cpp @@ -29,6 +29,21 @@ #include extern "C" { +#ifdef Q_OS_WINDOWS +#define BITMAP WINDOWS_BITMAP +#undef UNICODE +#include +#include +#undef BITMAP +#endif +#include +#include +#include +#include +#include +#include +#include +#define HAVE_STDARG_H #include <86box/86box.h> #include <86box/config.h> #include <86box/device.h> @@ -131,14 +146,24 @@ MediaMenu::refresh(QMenu *parentMenu) cdromMutePos = menu->children().count(); menu->addAction(QApplication::style()->standardIcon(QStyle::SP_MediaVolumeMuted), tr("&Mute"), [this, i]() { cdromMute(i); })->setCheckable(true); menu->addSeparator(); - menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdromMount(i, 0); })->setCheckable(false); - menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdromMount(i, 1); })->setCheckable(false); + menu->addAction(ProgSettings::loadIcon("/cdrom.ico"), tr("&Image..."), [this, i]() { cdromMount(i, 0, nullptr); })->setCheckable(false); + menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DirIcon), tr("&Folder..."), [this, i]() { cdromMount(i, 1, nullptr); })->setCheckable(false); menu->addSeparator(); for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { cdromImageHistoryPos[slot] = menu->children().count(); menu->addAction(QString::asprintf(tr("Image %i").toUtf8().constData(), slot), [this, i, slot]() { cdromReload(i, slot); })->setCheckable(false); } menu->addSeparator(); +#ifdef Q_OS_WINDOWS + /* Loop through each Windows drive letter and test to see if + it's a CDROM */ + for (auto &letter : driveLetters) { + auto drive = QString::asprintf("%c:\\", letter).toUtf8().constData(); + if (GetDriveType(drive) == DRIVE_CDROM) + menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DriveCDIcon), tr("Host CD/DVD Drive (%1:)").arg(letter), [this, i, letter]() { cdromMount(i, 2, QString::asprintf(R"(\\.\%c:)", letter).toUtf8().constData()); })->setCheckable(false); + } + menu->addSeparator(); +#endif // Q_OS_WINDOWS cdromImagePos = menu->children().count(); cdromDirPos = menu->children().count(); menu->addAction(tr("E&ject"), [this, i]() { cdromEject(i); })->setCheckable(false); @@ -453,56 +478,56 @@ MediaMenu::cdromMount(int i, const QString &filename) { QByteArray fn = filename.toUtf8().data(); - cdrom[i].prev_host_drive = cdrom[i].host_drive; strcpy(cdrom[i].prev_image_path, cdrom[i].image_path); if (cdrom[i].ops && cdrom[i].ops->exit) cdrom[i].ops->exit(&(cdrom[i])); cdrom[i].ops = nullptr; memset(cdrom[i].image_path, 0, sizeof(cdrom[i].image_path)); -#ifdef _WIN32 - if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) +#ifdef Q_OS_WINDOWS + if ((fn.data() != nullptr) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '/')) fn.data()[strlen(fn.data()) - 1] = '\\'; #else if ((fn.data() != NULL) && (strlen(fn.data()) >= 1) && (fn.data()[strlen(fn.data()) - 1] == '\\')) fn.data()[strlen(fn.data()) - 1] = '/'; #endif - cdrom_image_open(&(cdrom[i]), fn.data()); + if ((fn.data() != nullptr) && fn.contains("ioctl://")) + cdrom_ioctl_open(&(cdrom[i]), fn.data()); + else + cdrom_image_open(&(cdrom[i]), fn.data()); /* Signal media change to the emulated machine. */ if (cdrom[i].insert) cdrom[i].insert(cdrom[i].priv); - cdrom[i].host_drive = (strlen(cdrom[i].image_path) == 0) ? 0 : 200; - if (cdrom[i].host_drive == 200) { + + if (strlen(cdrom[i].image_path) > 0) ui_sb_update_icon_state(SB_CDROM | i, 0); - } else { + else ui_sb_update_icon_state(SB_CDROM | i, 1); - } mhm.addImageToHistory(i, ui::MediaType::Optical, cdrom[i].prev_image_path, cdrom[i].image_path); + cdromUpdateMenu(i); ui_sb_update_tip(SB_CDROM | i); config_save(); } void -MediaMenu::cdromMount(int i, int dir) +MediaMenu::cdromMount(int i, int dir, const QString &arg) { QString filename; QFileInfo fi(cdrom[i].image_path); - if (dir) { - filename = QFileDialog::getExistingDirectory( - parentWidget); - } else { - filename = QFileDialog::getOpenFileName( - parentWidget, - QString(), - QString(), + if (dir > 1) + filename = QString::asprintf(R"(ioctl://%s)", arg.toStdString().c_str()); + else if (dir == 1) + filename = QFileDialog::getExistingDirectory(parentWidget); + else { + filename = QFileDialog::getOpenFileName(parentWidget, QString(), + QString(), tr("CD-ROM images") % util::DlgFilter({ "iso", "cue" }) % tr("All files") % util::DlgFilter({ "*" }, true)); } - if (filename.isEmpty()) { + if (filename.isEmpty()) return; - } cdromMount(i, filename); } @@ -530,7 +555,7 @@ MediaMenu::cdromUpdateUi(int i) { cdrom_t *drv = &cdrom[i]; - if (drv->host_drive == 0) { + if (strlen(cdrom[i].image_path) == 0) { mhm.addImageToHistory(i, ui::MediaType::Optical, drv->prev_image_path, QString()); ui_sb_update_icon_state(SB_CDROM | i, 1); } else { @@ -550,6 +575,9 @@ MediaMenu::updateImageHistory(int index, int slot, ui::MediaType type) QObjectList children; QFileInfo fi; QIcon menu_icon; + const auto fn = mhm.getImageForSlot(index, slot, type); + + QString menu_item_name; switch (type) { case ui::MediaType::Optical: @@ -558,8 +586,18 @@ MediaMenu::updateImageHistory(int index, int slot, ui::MediaType type) menu = cdromMenus[index]; children = menu->children(); imageHistoryUpdatePos = dynamic_cast(children[cdromImageHistoryPos[slot]]); - fi.setFile(mhm.getImageForSlot(index, slot, type)); - menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + if (fn.left(8) == "ioctl://") { + menu_icon = QApplication::style()->standardIcon(QStyle::SP_DriveCDIcon); +#ifdef Q_OS_WINDOWS + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(fn.right(2)).toUtf8().constData(); +#else + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(fn.right(name.length() - 8)); +#endif + } else { + fi.setFile(fn); + menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + menu_item_name = fn.isEmpty() ? tr("previous image").toUtf8().constData() : fn.toUtf8().constData(); + } imageHistoryUpdatePos->setIcon(menu_icon); break; case ui::MediaType::Floppy: @@ -568,17 +606,20 @@ MediaMenu::updateImageHistory(int index, int slot, ui::MediaType type) menu = floppyMenus[index]; children = menu->children(); imageHistoryUpdatePos = dynamic_cast(children[floppyImageHistoryPos[slot]]); - fi.setFile(mhm.getImageForSlot(index, slot, type)); + fi.setFile(fn); + menu_item_name = fi.fileName().isEmpty() ? tr("previous image").toUtf8().constData() : fi.fileName().toUtf8().constData(); break; default: - pclog("History not yet implemented for media type %s\n", qPrintable(mhm.mediaTypeToString(type))); + menu_item_name = fi.fileName().isEmpty() ? tr("previous image").toUtf8().constData() : fi.fileName().toUtf8().constData(); return; } - const QString menu_item_name = fi.fileName().isEmpty() ? tr("previous image").toUtf8().constData() : fi.fileName().toUtf8().constData(); imageHistoryUpdatePos->setText(QString::asprintf(tr("%s").toUtf8().constData(), menu_item_name.toUtf8().constData())); - imageHistoryUpdatePos->setVisible(!fi.fileName().isEmpty()); - imageHistoryUpdatePos->setVisible(fi.exists()); + + if (fn.left(8) == "ioctl://") + imageHistoryUpdatePos->setVisible(true); + else + imageHistoryUpdatePos->setVisible(!fn.isEmpty() && fi.exists()); } void @@ -591,8 +632,9 @@ MediaMenu::clearImageHistory() void MediaMenu::cdromUpdateMenu(int i) { - QString name = cdrom[i].image_path; - QFileInfo fi(cdrom[i].image_path); + QString name = cdrom[i].image_path; + QString name2; + QIcon menu_icon; if (!cdromMenus.contains(i)) return; @@ -605,14 +647,27 @@ MediaMenu::cdromUpdateMenu(int i) auto *imageMenu = dynamic_cast(childs[cdromImagePos]); imageMenu->setEnabled(!name.isEmpty()); - const QString menu_item_name = name.isEmpty() ? QString().toUtf8().constData() : fi.fileName().toUtf8().constData(); - const auto menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + QString menu_item_name; + if (name.left(8) == "ioctl://") { +#ifdef Q_OS_WINDOWS + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(name.right(2)).toUtf8().constData(); +#else + menu_item_name = tr("Host CD/DVD Drive (%1)").arg(name.right(name.length() - 8)); +#endif + name2 = menu_item_name; + menu_icon = QApplication::style()->standardIcon(QStyle::SP_DriveCDIcon); + } else { + QFileInfo fi(cdrom[i].image_path); + + menu_item_name = name.isEmpty() ? QString().toUtf8().constData() : name.toUtf8().constData(); + name2 = name; + menu_icon = fi.isDir() ? QApplication::style()->standardIcon(QStyle::SP_DirIcon) : ProgSettings::loadIcon("/cdrom.ico"); + } imageMenu->setIcon(menu_icon); imageMenu->setText(QString::asprintf(tr("Eject %s").toUtf8().constData(), menu_item_name.toUtf8().constData())); - for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) { + for (int slot = 0; slot < MAX_PREV_IMAGES; slot++) updateImageHistory(i, slot, ui::MediaType::Optical); - } QString busName = tr("Unknown Bus"); switch (cdrom[i].bus_type) { @@ -630,7 +685,7 @@ MediaMenu::cdromUpdateMenu(int i) } // menu->setTitle(tr("CD-ROM %1 (%2): %3").arg(QString::number(i+1), busName, name.isEmpty() ? tr("(empty)") : name)); - menu->setTitle(QString::asprintf(tr("CD-ROM %i (%s): %s").toUtf8().constData(), i + 1, busName.toUtf8().data(), name.isEmpty() ? tr("(empty)").toUtf8().data() : name.toUtf8().data())); + menu->setTitle(QString::asprintf(tr("CD-ROM %i (%s): %s").toUtf8().constData(), i + 1, busName.toUtf8().data(), name.isEmpty() ? tr("(empty)").toUtf8().data() : name2.toUtf8().data())); } void diff --git a/src/qt/qt_mediamenu.hpp b/src/qt/qt_mediamenu.hpp index 7835077b3..725ce92b3 100644 --- a/src/qt/qt_mediamenu.hpp +++ b/src/qt/qt_mediamenu.hpp @@ -41,7 +41,7 @@ public: void floppyUpdateMenu(int i); void cdromMute(int i); - void cdromMount(int i, int dir); + void cdromMount(int i, int dir, const QString &arg); void cdromMount(int i, const QString &filename); void cdromEject(int i); void cdromReload(int index, int slot); @@ -87,6 +87,8 @@ private: QString getMediaOpenDirectory(); ui::MediaHistoryManager mhm; + const QByteArray driveLetters = QByteArrayLiteral("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + int cassetteRecordPos; int cassettePlayPos; int cassetteRewindPos; diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index ef1942b3d..edfefa39f 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -278,7 +278,7 @@ int path_abs(char *path) { #ifdef Q_OS_WINDOWS - if ((path[1] == ':') || (path[0] == '\\') || (path[0] == '/')) + if ((path[1] == ':') || (path[0] == '\\') || (path[0] == '/') || (strstr(path, "ioctl://") == path)) return 1; return 0; @@ -291,10 +291,13 @@ void path_normalize(char *path) { #ifdef Q_OS_WINDOWS - while (*path++ != 0) { - if (*path == '\\') - *path = '/'; - } + if (strstr(path, "ioctl://") != path) { + while (*path++ != 0) { + if (*path == '\\') + *path = '/'; + } + } else + path[8] = path[9] = path[11] = '\\'; #endif } diff --git a/src/qt/win_cdrom_ioctl.c b/src/qt/win_cdrom_ioctl.c new file mode 100644 index 000000000..ff422bcd2 --- /dev/null +++ b/src/qt/win_cdrom_ioctl.c @@ -0,0 +1,411 @@ +/* + * 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. + * + * Win32 CD-ROM support via IOCTL. + * + * + * + * Authors: TheCollector1995, , + * Miran Grca, + * + * Copyright 2023 TheCollector1995. + * Copyright 2023 Miran Grca. + */ +#define UNICODE +#define BITMAP WINDOWS_BITMAP +#include +#include +#undef BITMAP +#include +#include +#include "ntddcdrm.h" +#include "ntddscsi.h" +#include +#include +#include +#include +#include +#include +#define HAVE_STDARG_H +#include <86box/86box.h> +#include <86box/scsi_device.h> +#include <86box/cdrom.h> +#include <86box/plat_unused.h> +#include <86box/plat_cdrom.h> + +/* 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 int toc_valid = 0; +static CDROM_TOC cur_toc = { 0 }; +static HANDLE handle = NULL; +static WCHAR ioctl_path[256] = { 0 }; +static WCHAR old_ioctl_path[256] = { 0 }; + +#ifdef ENABLE_WIN_CDROM_IOCTL_LOG +int win_cdrom_ioctl_do_log = ENABLE_WIN_CDROM_IOCTL_LOG; + +void +win_cdrom_ioctl_log(const char *fmt, ...) +{ + va_list ap; + + if (win_cdrom_ioctl_do_log) { + va_start(ap, fmt); + pclog_ex(fmt, ap); + va_end(ap); + } +} +#else +# define win_cdrom_ioctl_log(fmt, ...) +#endif + +static int +plat_cdrom_open(void) +{ + plat_cdrom_close(); + + handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + win_cdrom_ioctl_log("handle=%p, error=%x\n", handle, (unsigned int) GetLastError()); + + return (handle != INVALID_HANDLE_VALUE); +} + +static int +plat_cdrom_load(void) +{ + plat_cdrom_close(); + + handle = CreateFileW((LPCWSTR)ioctl_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + win_cdrom_ioctl_log("handle=%p, error=%x\n", handle, (unsigned int) GetLastError()); + if (handle != INVALID_HANDLE_VALUE) { + long size; + DeviceIoControl(handle, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, (LPDWORD)&size, NULL); + return 1; + } + return 0; +} + +static void +plat_cdrom_read_toc(void) +{ + long size = 0; + + if (!toc_valid) { + toc_valid = 1; + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, NULL, 0, &cur_toc, sizeof(cur_toc), (LPDWORD)&size, NULL); + plat_cdrom_close(); + } +} + +int +plat_cdrom_is_track_audio(uint32_t sector) +{ + int control = 0; + uint32_t track_addr = 0; + uint32_t next_track_addr = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, A: %08X, S: %08X\n", + cur_toc.FirstTrack, cur_toc.LastTrack, + cur_toc.TrackData[c].TrackNumber, c, + cur_toc.TrackData[c].Control, track_addr, sector); + if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && + (sector >= track_addr) && (sector < next_track_addr)) { + control = cur_toc.TrackData[c].Control; + break; + } + } + + const int ret = !(control & 0x04); + + win_cdrom_ioctl_log("plat_cdrom_is_track_audio(%08X): %i\n", sector, ret); + + return ret; +} + +int +plat_cdrom_is_track_pre(uint32_t sector) +{ + int control = 0; + uint32_t track_addr = 0; + uint32_t next_track_addr = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, A: %08X, S: %08X\n", + cur_toc.FirstTrack, cur_toc.LastTrack, + cur_toc.TrackData[c].TrackNumber, c, + cur_toc.TrackData[c].Control, track_addr, sector); + if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && + (sector >= track_addr) && (sector < next_track_addr)) { + control = cur_toc.TrackData[c].Control; + break; + } + } + + const int ret = (control & 0x01); + + win_cdrom_ioctl_log("plat_cdrom_is_track_audio(%08X): %i\n", sector, ret); + + return ret; +} + +uint32_t +plat_cdrom_get_track_start(uint32_t sector, uint8_t *attr, uint8_t *track) +{ + uint32_t track_addr = 0; + uint32_t next_track_addr = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; cur_toc.TrackData[c].TrackNumber != 0xaa; c++) { + track_addr = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + next_track_addr = MSFtoLBA(cur_toc.TrackData[c + 1].Address[1], cur_toc.TrackData[c + 1].Address[2], cur_toc.TrackData[c + 1].Address[3]) - 150; + win_cdrom_ioctl_log("F: %i, L: %i, C: %i (%i), c: %02X, a: %02X, A: %08X, S: %08X\n", + cur_toc.FirstTrack, cur_toc.LastTrack, + cur_toc.TrackData[c].TrackNumber, c, + cur_toc.TrackData[c].Control, cur_toc.TrackData[c].Adr, + track_addr, sector); + if ((cur_toc.TrackData[c].TrackNumber >= cur_toc.FirstTrack) && (cur_toc.TrackData[c].TrackNumber <= cur_toc.LastTrack) && + (sector >= track_addr) && (sector < next_track_addr)) { + *track = cur_toc.TrackData[c].TrackNumber; + *attr = cur_toc.TrackData[c].Control; + *attr |= ((cur_toc.TrackData[c].Adr << 4) & 0xf0); + break; + } + } + + win_cdrom_ioctl_log("plat_cdrom_get_track_start(%08X): %i\n", sector, track_addr); + + return track_addr; +} + +uint32_t +plat_cdrom_get_last_block(void) +{ + uint32_t lb = 0; + uint32_t address = 0; + + plat_cdrom_read_toc(); + + for (int c = 0; c <= cur_toc.LastTrack; c++) { + address = MSFtoLBA(cur_toc.TrackData[c].Address[1], cur_toc.TrackData[c].Address[2], cur_toc.TrackData[c].Address[3]) - 150; + if (address > lb) + lb = address; + } + win_cdrom_ioctl_log("LBCapacity=%d\n", lb); + return lb; +} + +int +plat_cdrom_ext_medium_changed(void) +{ + long size; + CDROM_TOC toc; + int ret = 0; + + plat_cdrom_open(); + int temp = DeviceIoControl(handle, IOCTL_CDROM_READ_TOC, + NULL, 0, &toc, sizeof(toc), + (LPDWORD)&size, NULL); + plat_cdrom_close(); + + if (!temp) + /* There has been some kind of error - not a medium change, but a not ready + condition. */ + ret = -1; + else if (!toc_valid || (memcmp(ioctl_path, old_ioctl_path, sizeof(ioctl_path)) != 0)) { + /* Changed to a different host drive - we already detect such medium changes. */ + toc_valid = 1; + cur_toc = toc; + if (memcmp(ioctl_path, old_ioctl_path, sizeof(ioctl_path)) != 0) + memcpy(old_ioctl_path, ioctl_path, sizeof(ioctl_path)); + } else if ((toc.TrackData[toc.LastTrack].Address[1] != + cur_toc.TrackData[cur_toc.LastTrack].Address[1]) || + (toc.TrackData[toc.LastTrack].Address[2] != + cur_toc.TrackData[cur_toc.LastTrack].Address[2]) || + (toc.TrackData[toc.LastTrack].Address[3] != + cur_toc.TrackData[cur_toc.LastTrack].Address[3])) + /* The TOC has changed. */ + ret = 1; + + win_cdrom_ioctl_log("plat_cdrom_ext_medium_changed(): %i\n", ret); + + return ret; +} + +void +plat_cdrom_get_audio_tracks(int *st_track, int *end, TMSF *lead_out) +{ + plat_cdrom_read_toc(); + + *st_track = 1; + *end = cur_toc.LastTrack; + lead_out->min = cur_toc.TrackData[cur_toc.LastTrack].Address[1]; + lead_out->sec = cur_toc.TrackData[cur_toc.LastTrack].Address[2]; + lead_out->fr = cur_toc.TrackData[cur_toc.LastTrack].Address[3]; + + win_cdrom_ioctl_log("plat_cdrom_get_audio_tracks(): %02i, %02i, %02i:%02i:%02i\n", + *st_track, *end, lead_out->min, lead_out->sec, lead_out->fr); +} + +/* This replaces both Info and EndInfo, they are specified by a variable. */ +int +plat_cdrom_get_audio_track_info(UNUSED(int end), int track, int *track_num, TMSF *start, uint8_t *attr) +{ + plat_cdrom_read_toc(); + + if ((track < 1) || (track == 0xaa) || (track > (cur_toc.LastTrack + 1))) { + win_cdrom_ioctl_log("plat_cdrom_get_audio_track_info(%02i)\n", track); + return 0; + } + + start->min = cur_toc.TrackData[track - 1].Address[1]; + start->sec = cur_toc.TrackData[track - 1].Address[2]; + start->fr = cur_toc.TrackData[track - 1].Address[3]; + + *track_num = cur_toc.TrackData[track - 1].TrackNumber; + *attr = cur_toc.TrackData[track - 1].Control; + *attr |= ((cur_toc.TrackData[track - 1].Adr << 4) & 0xf0); + + win_cdrom_ioctl_log("plat_cdrom_get_audio_track_info(%02i): %02i:%02i:%02i, %02i, %02X\n", + track, start->min, start->sec, start->fr, *track_num, *attr); + + return 1; +} + +/* TODO: See if track start is adjusted by 150 or not. */ +int +plat_cdrom_get_audio_sub(UNUSED(uint32_t sector), uint8_t *attr, uint8_t *track, uint8_t *index, TMSF *rel_pos, TMSF *abs_pos) +{ + CDROM_SUB_Q_DATA_FORMAT insub; + SUB_Q_CHANNEL_DATA sub; + long size = 0; + + insub.Format = IOCTL_CDROM_CURRENT_POSITION; + + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_CDROM_READ_Q_CHANNEL, &insub, sizeof(insub), &sub, sizeof(sub), (LPDWORD)&size, NULL); + plat_cdrom_close(); + + if (sub.CurrentPosition.TrackNumber < 1) + return 0; + + *track = sub.CurrentPosition.TrackNumber; + *attr = sub.CurrentPosition.Control; + *attr |= ((sub.CurrentPosition.ADR << 4) & 0xf0); + *index = sub.CurrentPosition.IndexNumber; + + rel_pos->min = sub.CurrentPosition.TrackRelativeAddress[1]; + rel_pos->sec = sub.CurrentPosition.TrackRelativeAddress[2]; + rel_pos->fr = sub.CurrentPosition.TrackRelativeAddress[3]; + abs_pos->min = sub.CurrentPosition.AbsoluteAddress[1]; + abs_pos->sec = sub.CurrentPosition.AbsoluteAddress[2]; + abs_pos->fr = sub.CurrentPosition.AbsoluteAddress[3]; + + win_cdrom_ioctl_log("plat_cdrom_get_audio_sub(): %02i, %02X, %02i, %02i:%02i:%02i, %02i:%02i:%02i\n", + *track, *attr, *index, rel_pos->min, rel_pos->sec, rel_pos->fr, abs_pos->min, abs_pos->sec, abs_pos->fr); + + return 1; +} + +int +plat_cdrom_get_sector_size(UNUSED(uint32_t sector)) +{ + long size; + DISK_GEOMETRY dgCDROM; + + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_CDROM_GET_DRIVE_GEOMETRY, NULL, 0, &dgCDROM, sizeof(dgCDROM), (LPDWORD)&size, NULL); + plat_cdrom_close(); + + win_cdrom_ioctl_log("BytesPerSector=%d\n", dgCDROM.BytesPerSector); + return dgCDROM.BytesPerSector; +} + +int +plat_cdrom_read_sector(uint8_t *buffer, int raw, uint32_t sector) +{ + BOOL status; + long size = 0; + int buflen = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE; + + plat_cdrom_open(); + + if (raw) { + win_cdrom_ioctl_log("Raw\n"); + /* Raw */ + RAW_READ_INFO in; + in.DiskOffset.LowPart = sector * COOKED_SECTOR_SIZE; + in.DiskOffset.HighPart = 0; + in.SectorCount = 1; + in.TrackMode = CDDA; + status = DeviceIoControl(handle, IOCTL_CDROM_RAW_READ, &in, sizeof(in), + buffer, buflen, (LPDWORD)&size, NULL); + } else { + win_cdrom_ioctl_log("Cooked\n"); + /* Cooked */ + int success = 0; + DWORD newPos = SetFilePointer(handle, sector * COOKED_SECTOR_SIZE, 0, FILE_BEGIN); + if (newPos != 0xFFFFFFFF) + success = ReadFile(handle, buffer, buflen, (LPDWORD)&size, NULL); + status = (success != 0); + } + plat_cdrom_close(); + win_cdrom_ioctl_log("ReadSector status=%d, sector=%d, size=%" PRId64 ".\n", status, sector, (long long) size); + + return (size == buflen) && (status > 0); +} + +void +plat_cdrom_eject(void) +{ + long size; + + plat_cdrom_open(); + DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, (LPDWORD)&size, NULL); + + plat_cdrom_close(); +} + +void +plat_cdrom_close(void) +{ + if (handle != NULL) { + CloseHandle(handle); + handle = NULL; + } +} + +int +plat_cdrom_set_drive(const char *drv) +{ + plat_cdrom_close(); + + memcpy(old_ioctl_path, ioctl_path, sizeof(ioctl_path)); + memset(ioctl_path, 0x00, sizeof(ioctl_path)); + + wsprintf(ioctl_path, L"%S", drv); + win_cdrom_ioctl_log("Path is %S\n", ioctl_path); + + toc_valid = 0; + + plat_cdrom_load(); + return 1; +} diff --git a/src/scsi/scsi_cdrom.c b/src/scsi/scsi_cdrom.c index 47ba0535e..262d9600c 100644 --- a/src/scsi/scsi_cdrom.c +++ b/src/scsi/scsi_cdrom.c @@ -1557,6 +1557,10 @@ static int scsi_cdrom_pre_execution_check(scsi_cdrom_t *dev, uint8_t *cdb) { int ready = 0; + int ext_medium_changed = 0; + + if (dev->drv && dev->drv->ops && dev->drv->ops->ext_medium_changed) + ext_medium_changed = dev->drv->ops->ext_medium_changed(dev->drv); if ((cdb[0] != GPCMD_REQUEST_SENSE) && (dev->cur_lun == SCSI_LUN_USE_CDB) && (cdb[1] & 0xe0)) { scsi_cdrom_log("CD-ROM %i: Attempting to execute a unknown command targeted at SCSI LUN %i\n", @@ -1590,10 +1594,10 @@ scsi_cdrom_pre_execution_check(scsi_cdrom_t *dev, uint8_t *cdb) goto skip_ready_check; } - if (dev->drv->cd_status & CD_STATUS_MEDIUM_CHANGED) + if ((dev->drv->cd_status & CD_STATUS_MEDIUM_CHANGED) || (ext_medium_changed == 1)) scsi_cdrom_insert((void *) dev); - ready = (dev->drv->cd_status != CD_STATUS_EMPTY); + ready = (dev->drv->cd_status != CD_STATUS_EMPTY) || (ext_medium_changed == -1); skip_ready_check: /* If the drive is not ready, there is no reason to keep the @@ -1883,7 +1887,7 @@ begin: case GPCMD_AUDIO_SCAN: scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2559,7 +2563,7 @@ begin: case CDROM_TYPE_TOSHIBA_XM5701TA_3136: case CDROM_TYPE_TOSHIBA_SDM1401_1008: /*GPCMD_AUDIO_TRACK_SEARCH_TOSHIBA*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2587,7 +2591,7 @@ begin: case CDROM_TYPE_NEC_211_100: case CDROM_TYPE_NEC_464_105: /*GPCMD_AUDIO_TRACK_SEARCH_NEC*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2666,7 +2670,7 @@ begin: case CDROM_TYPE_TOSHIBA_XM5701TA_3136: case CDROM_TYPE_TOSHIBA_SDM1401_1008: /*GPCMD_PLAY_AUDIO_TOSHIBA*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2693,7 +2697,7 @@ begin: case CDROM_TYPE_NEC_211_100: case CDROM_TYPE_NEC_464_105: /*GPCMD_PLAY_AUDIO_NEC*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2761,7 +2765,7 @@ begin: break; } - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -2878,7 +2882,7 @@ begin: } pos = cdb[4]; - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -3592,7 +3596,7 @@ atapi_out: break; case CDROM_TYPE_PIONEER_DRM604X_2403: /*GPCMD_AUDIO_TRACK_SEARCH_PIONEER*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } @@ -3642,7 +3646,7 @@ atapi_out: break; case CDROM_TYPE_PIONEER_DRM604X_2403: /*GPCMD_PLAY_AUDIO_PIONEER*/ scsi_cdrom_set_phase(dev, SCSI_PHASE_STATUS); - if ((dev->drv->host_drive < 1) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { + if ((dev->drv->image_path[0] == 0x00) || (dev->drv->cd_status <= CD_STATUS_DATA_ONLY)) { scsi_cdrom_illegal_mode(dev); break; } diff --git a/src/unix/unix_cdrom.c b/src/unix/unix_cdrom.c index 61813a754..186c7649b 100644 --- a/src/unix/unix_cdrom.c +++ b/src/unix/unix_cdrom.c @@ -124,7 +124,7 @@ plat_cdrom_ui_update(uint8_t id, uint8_t reload) { cdrom_t *drv = &cdrom[id]; - if (drv->host_drive == 0) { + if (drv->image_path[0] == 0x00) { ui_sb_update_icon_state(SB_CDROM | id, 1); } else { ui_sb_update_icon_state(SB_CDROM | id, 0); @@ -139,7 +139,6 @@ plat_cdrom_ui_update(uint8_t id, uint8_t reload) void cdrom_mount(uint8_t id, char *fn) { - cdrom[id].prev_host_drive = cdrom[id].host_drive; strcpy(cdrom[id].prev_image_path, cdrom[id].image_path); if (cdrom[id].ops && cdrom[id].ops->exit) cdrom[id].ops->exit(&(cdrom[id])); @@ -151,12 +150,10 @@ cdrom_mount(uint8_t id, char *fn) /* Signal media change to the emulated machine. */ if (cdrom[id].insert) cdrom[id].insert(cdrom[id].priv); - cdrom[id].host_drive = (strlen(cdrom[id].image_path) == 0) ? 0 : 200; - if (cdrom[id].host_drive == 200) { + if (cdrom[id].image_path[0] == 0x00) ui_sb_update_icon_state(SB_CDROM | id, 0); - } else { + else ui_sb_update_icon_state(SB_CDROM | id, 1); - } #if 0 media_menu_update_cdrom(id); #endif