From e83167df1ac79230ae87b16b906a65f4220ceee7 Mon Sep 17 00:00:00 2001 From: OBattler Date: Sat, 27 Oct 2018 22:19:39 +0200 Subject: [PATCH] Fixed mode sense loading mess (that was messing up the heap and destabilizing the emulator) in the ZIP, CD-ROM, and SCSI disk code. --- src/disk/zip.c | 53 +++++++++++++++++++------------------------ src/scsi/scsi_cdrom.c | 20 +++++----------- src/scsi/scsi_disk.c | 12 ++++------ 3 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/disk/zip.c b/src/disk/zip.c index a4d6261d4..ab4fd270c 100644 --- a/src/disk/zip.c +++ b/src/disk/zip.c @@ -9,7 +9,7 @@ * Implementation of the Iomega ZIP drive with SCSI(-like) * commands, for both ATAPI and SCSI usage. * - * Version: @(#)zip.c 1.0.33 2018/10/26 + * Version: @(#)zip.c 1.0.34 2018/10/27 * * Author: Miran Grca, * @@ -561,11 +561,20 @@ zip_disk_reload(zip_t *dev) void -zip_disk_close(zip_t *dev) +zip_disk_unload(zip_t *dev) { if (dev->drv->f) { fclose(dev->drv->f); dev->drv->f = NULL; + } +} + + +void +zip_disk_close(zip_t *dev) +{ + if (dev->drv->f) { + zip_disk_unload(dev); memcpy(dev->drv->prev_image_path, dev->drv->image_path, sizeof(dev->drv->prev_image_path)); memset(dev->drv->image_path, 0, sizeof(dev->drv->image_path)); @@ -698,36 +707,20 @@ zip_mode_sense_load(zip_t *dev) { FILE *f; wchar_t file_name[512]; - int i; memset(&dev->ms_pages_saved, 0, sizeof(mode_sense_pages_t)); - for (i = 0; i < 0x3f; i++) { - if (dev->drv->is_250) { - if (zip_250_mode_sense_pages_default.pages[i][1] != 0) { - if (zip_drives[dev->id].bus_type == ZIP_BUS_SCSI) { - memcpy(dev->ms_pages_saved.pages[i], - zip_250_mode_sense_pages_default_scsi.pages[i], - zip_250_mode_sense_pages_default_scsi.pages[i][1] + 2); - } else { - memcpy(dev->ms_pages_saved.pages[i], - zip_250_mode_sense_pages_default.pages[i], - zip_250_mode_sense_pages_default.pages[i][1] + 2); - } - } - } else { - if (zip_mode_sense_pages_default.pages[i][1] != 0) { - if (dev->drv->bus_type == ZIP_BUS_SCSI) { - memcpy(dev->ms_pages_saved.pages[i], - zip_mode_sense_pages_default_scsi.pages[i], - zip_mode_sense_pages_default_scsi.pages[i][1] + 2); - } else { - memcpy(dev->ms_pages_saved.pages[i], - zip_mode_sense_pages_default.pages[i], - zip_mode_sense_pages_default.pages[i][1] + 2); - } - } - } + if (dev->drv->is_250) { + if (zip_drives[dev->id].bus_type == ZIP_BUS_SCSI) + memcpy(&dev->ms_pages_saved, &zip_250_mode_sense_pages_default_scsi, sizeof(mode_sense_pages_t)); + else + memcpy(&dev->ms_pages_saved, &zip_250_mode_sense_pages_default, sizeof(mode_sense_pages_t)); + } else { + if (zip_drives[dev->id].bus_type == ZIP_BUS_SCSI) + memcpy(&dev->ms_pages_saved, &zip_mode_sense_pages_default_scsi, sizeof(mode_sense_pages_t)); + else + memcpy(&dev->ms_pages_saved, &zip_mode_sense_pages_default, sizeof(mode_sense_pages_t)); } + memset(file_name, 0, 512 * sizeof(wchar_t)); if (dev->drv->bus_type == ZIP_BUS_SCSI) swprintf(file_name, 512, L"scsi_zip_%02i_mode_sense_bin", dev->id); @@ -2815,7 +2808,7 @@ zip_close(void) dev = (zip_t *) zip_drives[c].priv; if (dev) { - zip_disk_close(dev); + zip_disk_unload(dev); free(dev); zip_drives[c].priv = NULL; diff --git a/src/scsi/scsi_cdrom.c b/src/scsi/scsi_cdrom.c index 58adbb694..739be4af4 100644 --- a/src/scsi/scsi_cdrom.c +++ b/src/scsi/scsi_cdrom.c @@ -9,7 +9,7 @@ * Implementation of the CD-ROM drive with SCSI(-like) * commands, for both ATAPI and SCSI usage. * - * Version: @(#)scsi_cdrom.c 1.0.62 2018/10/26 + * Version: @(#)scsi_cdrom.c 1.0.63 2018/10/27 * * Author: Miran Grca, * @@ -477,21 +477,13 @@ scsi_cdrom_mode_sense_load(scsi_cdrom_t *dev) { FILE *f; wchar_t file_name[512]; - int i; memset(&dev->ms_pages_saved, 0, sizeof(mode_sense_pages_t)); - for (i = 0; i < 0x3f; i++) { - if (scsi_cdrom_mode_sense_pages_default.pages[i][1] != 0) { - if (dev->drv->bus_type == CDROM_BUS_SCSI) - memcpy(dev->ms_pages_saved.pages[i], - scsi_cdrom_mode_sense_pages_default_scsi.pages[i], - scsi_cdrom_mode_sense_pages_default_scsi.pages[i][1] + 2); - else - memcpy(dev->ms_pages_saved.pages[i], - scsi_cdrom_mode_sense_pages_default.pages[i], - scsi_cdrom_mode_sense_pages_default.pages[i][1] + 2); - } - } + if (dev->drv->bus_type == CDROM_BUS_SCSI) + memcpy(&dev->ms_pages_saved, &scsi_cdrom_mode_sense_pages_default_scsi, sizeof(mode_sense_pages_t)); + else + memcpy(&dev->ms_pages_saved, &scsi_cdrom_mode_sense_pages_default, sizeof(mode_sense_pages_t)); + memset(file_name, 0, 512 * sizeof(wchar_t)); if (dev->drv->bus_type == CDROM_BUS_SCSI) swprintf(file_name, 512, L"scsi_cdrom_%02i_mode_sense_bin", dev->id); diff --git a/src/scsi/scsi_disk.c b/src/scsi/scsi_disk.c index 5b2c84d81..968df53b6 100644 --- a/src/scsi/scsi_disk.c +++ b/src/scsi/scsi_disk.c @@ -6,7 +6,7 @@ * * Emulation of SCSI fixed disks. * - * Version: @(#)scsi_disk.c 1.0.26 2018/10/26 + * Version: @(#)scsi_disk.c 1.0.27 2018/10/27 * * Author: Miran Grca, * @@ -177,14 +177,10 @@ scsi_disk_mode_sense_load(scsi_disk_t *dev) { FILE *f; wchar_t file_name[512]; - int i; + memset(&dev->ms_pages_saved, 0, sizeof(mode_sense_pages_t)); - for (i = 0; i < 0x3f; i++) { - if (scsi_disk_mode_sense_pages_default.pages[i][1] != 0) { - memcpy(dev->ms_pages_saved.pages[i], scsi_disk_mode_sense_pages_default.pages[i], - scsi_disk_mode_sense_pages_default.pages[i][1] + 2); - } - } + memcpy(&dev->ms_pages_saved, &scsi_disk_mode_sense_pages_default, sizeof(mode_sense_pages_t)); + swprintf(file_name, 512, L"scsi_disk_%02i_mode_sense.bin", dev->id); memset(file_name, 0, 512 * sizeof(wchar_t)); f = plat_fopen(nvr_path(file_name), L"rb");