| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 | /* Advanced CD-ROM drive emulation. * Adds a few capabilities on top of the SCSI2SD CD-ROM emulation: * * - bin/cue support for support of multiple tracks * - on the fly image switching * * SCSI2SD V6 - Copyright (C) 2014 Michael McMaster <michael@codesrc.com> * ZuluSCSI™ - Copyright (c) 2023 Rabbit Hole Computing™ * * This file is licensed under the GPL version 3 or any later version.  * It is derived from cdrom.c in SCSI2SD V6 * * https://www.gnu.org/licenses/gpl-3.0.html * ---- * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version.  * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details.  * * You should have received a copy of the GNU General Public License * along with this program.  If not, see <https://www.gnu.org/licenses/>. */#include <string.h>#include "BlueSCSI_cdrom.h"#include "BlueSCSI_log.h"#include "BlueSCSI_config.h"extern "C" {#include <scsi.h>}/******************************************//* Basic TOC generation without cue sheet *//******************************************/static const uint8_t SimpleTOC[] ={	0x00, // toc length, MSB	0x12, // toc length, LSB	0x01, // First track number	0x01, // Last track number,	// TRACK 1 Descriptor	0x00, // reserved	0x14, // Q sub-channel encodes current position, Digital track	0x01, // Track 1,	0x00, // Reserved	0x00,0x00,0x00,0x00, // Track start sector (LBA)	0x00, // reserved	0x14, // Q sub-channel encodes current position, Digital track	0xAA, // Leadout Track	0x00, // Reserved	0x00,0x00,0x00,0x00, // Track start sector (LBA)};static const uint8_t LeadoutTOC[] ={	0x00, // toc length, MSB	0x0A, // toc length, LSB	0x01, // First track number	0x01, // Last track number,	0x00, // reserved	0x14, // Q sub-channel encodes current position, Digital track	0xAA, // Leadout Track	0x00, // Reserved	0x00,0x00,0x00,0x00, // Track start sector (LBA)};static const uint8_t SessionTOC[] ={	0x00, // toc length, MSB	0x0A, // toc length, LSB	0x01, // First session number	0x01, // Last session number,	// TRACK 1 Descriptor	0x00, // reserved	0x14, // Q sub-channel encodes current position, Digital track	0x01, // First track number in last complete session	0x00, // Reserved	0x00,0x00,0x00,0x00 // LBA of first track in last session};static const uint8_t FullTOC[] ={	0x00, // toc length, MSB	0x44, // toc length, LSB	0x01, // First session number	0x01, // Last session number,	// A0 Descriptor	0x01, // session number	0x14, // ADR/Control	0x00, // TNO	0xA0, // POINT	0x00, // Min	0x00, // Sec	0x00, // Frame	0x00, // Zero	0x01, // First Track number.	0x00, // Disc type 00 = Mode 1	0x00,  // PFRAME	// A1	0x01, // session number	0x14, // ADR/Control	0x00, // TNO	0xA1, // POINT	0x00, // Min	0x00, // Sec	0x00, // Frame	0x00, // Zero	0x01, // Last Track number	0x00, // PSEC	0x00,  // PFRAME	// A2	0x01, // session number	0x14, // ADR/Control	0x00, // TNO	0xA2, // POINT	0x00, // Min	0x00, // Sec	0x00, // Frame	0x00, // Zero	0x79, // LEADOUT position BCD	0x59, // leadout PSEC BCD	0x74, // leadout PFRAME BCD	// TRACK 1 Descriptor	0x01, // session number	0x14, // ADR/Control	0x00, // TNO	0x01, // Point	0x00, // Min	0x00, // Sec	0x00, // Frame	0x00, // Zero	0x00, // PMIN	0x00, // PSEC	0x00,  // PFRAME	// b0	0x01, // session number	0x54, // ADR/Control	0x00, // TNO	0xB1, // POINT	0x79, // Min BCD	0x59, // Sec BCD	0x74, // Frame BCD	0x00, // Zero	0x79, // PMIN BCD	0x59, // PSEC BCD	0x74,  // PFRAME BCD	// c0	0x01, // session number	0x54, // ADR/Control	0x00, // TNO	0xC0, // POINT	0x00, // Min	0x00, // Sec	0x00, // Frame	0x00, // Zero	0x00, // PMIN	0x00, // PSEC	0x00  // PFRAME};static void LBA2MSF(uint32_t LBA, uint8_t* MSF){	MSF[0] = 0; // reserved.	MSF[3] = LBA % 75; // M	uint32_t rem = LBA / 75;	MSF[2] = rem % 60; // S	MSF[1] = rem / 60;}static void doReadTOC(int MSF, uint8_t track, uint16_t allocationLength){	if (track == 0xAA)	{		// 0xAA requests only lead-out track information (reports capacity)		uint32_t len = sizeof(LeadoutTOC);		memcpy(scsiDev.data, LeadoutTOC, len);		uint32_t capacity = getScsiCapacity(			scsiDev.target->cfg->sdSectorStart,			scsiDev.target->liveCfg.bytesPerSector,			scsiDev.target->cfg->scsiSectors);		// Replace start of leadout track		if (MSF)		{			LBA2MSF(capacity, scsiDev.data + 8);		}		else		{			scsiDev.data[8] = capacity >> 24;			scsiDev.data[9] = capacity >> 16;			scsiDev.data[10] = capacity >> 8;			scsiDev.data[11] = capacity;		}		if (len > allocationLength)		{			len = allocationLength;		}		scsiDev.dataLen = len;		scsiDev.phase = DATA_IN;	}	else if (track <= 1)	{		// We only support track 1.		// track 0 means "return all tracks"		uint32_t len = sizeof(SimpleTOC);		memcpy(scsiDev.data, SimpleTOC, len);		uint32_t capacity = getScsiCapacity(			scsiDev.target->cfg->sdSectorStart,			scsiDev.target->liveCfg.bytesPerSector,			scsiDev.target->cfg->scsiSectors);		// Replace start of leadout track		if (MSF)		{			LBA2MSF(capacity, scsiDev.data + 0x10);		}		else		{			scsiDev.data[0x10] = capacity >> 24;			scsiDev.data[0x11] = capacity >> 16;			scsiDev.data[0x12] = capacity >> 8;			scsiDev.data[0x13] = capacity;		}		if (len > allocationLength)		{			len = allocationLength;		}		scsiDev.dataLen = len;		scsiDev.phase = DATA_IN;	}	else	{		scsiDev.status = CHECK_CONDITION;		scsiDev.target->sense.code = ILLEGAL_REQUEST;		scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;		scsiDev.phase = STATUS;	}}static void doReadSessionInfo(uint8_t session, uint16_t allocationLength){	uint32_t len = sizeof(SessionTOC);	memcpy(scsiDev.data, SessionTOC, len);	if (len > allocationLength)	{		len = allocationLength;	}	scsiDev.dataLen = len;	scsiDev.phase = DATA_IN;}static inline uint8_tfromBCD(uint8_t val){	return ((val >> 4) * 10) + (val & 0xF);}static void doReadFullTOC(int convertBCD, uint8_t session, uint16_t allocationLength){	// We only support session 1.	if (session > 1)	{		scsiDev.status = CHECK_CONDITION;		scsiDev.target->sense.code = ILLEGAL_REQUEST;		scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;		scsiDev.phase = STATUS;	}	else	{		uint32_t len = sizeof(FullTOC);		memcpy(scsiDev.data, FullTOC, len);		if (convertBCD)		{			int descriptor = 4;			while (descriptor < len)			{				int i;				for (i = 0; i < 7; ++i)				{					scsiDev.data[descriptor + i] =						fromBCD(scsiDev.data[descriptor + 4 + i]);				}				descriptor += 11;			}		}		if (len > allocationLength)		{			len = allocationLength;		}		scsiDev.dataLen = len;		scsiDev.phase = DATA_IN;	}}static uint8_t SimpleHeader[] ={	0x01, // 2048byte user data, L-EC in 288 byte aux field.	0x00, // reserved	0x00, // reserved	0x00, // reserved	0x00,0x00,0x00,0x00 // Track start sector (LBA or MSF)};void doReadHeader(int MSF, uint32_t lba, uint16_t allocationLength){	uint32_t len = sizeof(SimpleHeader);	memcpy(scsiDev.data, SimpleHeader, len);	if (len > allocationLength)	{		len = allocationLength;	}	scsiDev.dataLen = len;	scsiDev.phase = DATA_IN;}/**************************************//* Ejection and image switching logic *//**************************************/// Reinsert any ejected CDROMs on rebootvoid cdromReinsertFirstImage(image_config_t &img){    if (img.image_index > 0)    {        // Multiple images for this drive, force restart from first one        debuglog("---- Restarting from first CD-ROM image");        img.image_index = 9;        cdromSwitchNextImage(img);    }    else if (img.ejected)    {        // Reinsert the single image        debuglog("---- Closing CD-ROM tray");        img.ejected = false;        img.cdrom_events = 2; // New media    }}// Check if we have multiple CD-ROM images to cycle when drive is ejected.bool cdromSwitchNextImage(image_config_t &img){    // Check if we have a next image to load, so that drive is closed next time the host asks.    img.image_index++;    char filename[MAX_FILE_PATH];    int target_idx = img.scsiId & 7;    if (!scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename)))    {        img.image_index = 0;        scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename));    }    if (filename[0] != '\0')    {        log("Switching to next CD-ROM image for ", target_idx, ": ", filename);        img.file.close();        bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, 2048);        if (status)        {            img.ejected = false;            img.cdrom_events = 2; // New media            return true;        }    }    return false;}static void doGetEventStatusNotification(bool immed){    image_config_t &img = *(image_config_t*)scsiDev.target->cfg;    if (!immed)    {        // Asynchronous notification not supported        scsiDev.status = CHECK_CONDITION;        scsiDev.target->sense.code = ILLEGAL_REQUEST;        scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;        scsiDev.phase = STATUS;    }    else if (img.cdrom_events)    {        scsiDev.data[0] = 0;        scsiDev.data[1] = 6; // EventDataLength        scsiDev.data[2] = 0x04; // Media status events        scsiDev.data[3] = 0x04; // Supported events        scsiDev.data[4] = img.cdrom_events;        scsiDev.data[5] = 0x01; // Power status        scsiDev.data[6] = 0; // Start slot        scsiDev.data[7] = 0; // End slot        scsiDev.dataLen = 8;        scsiDev.phase = DATA_IN;        img.cdrom_events = 0;        if (img.ejected)        {            // We are now reporting to host that the drive is open.            // Simulate a "close" for next time the host polls.            cdromSwitchNextImage(img);        }    }    else    {        scsiDev.data[0] = 0;        scsiDev.data[1] = 2; // EventDataLength        scsiDev.data[2] = 0x00; // Media status events        scsiDev.data[3] = 0x04; // Supported events        scsiDev.dataLen = 4;        scsiDev.phase = DATA_IN;    }}/**************************************//* CD-ROM command dispatching         *//**************************************/// Handle direct-access scsi device commandsextern "C" int scsiCDRomCommand(){    image_config_t &img = *(image_config_t*)scsiDev.target->cfg;	int commandHandled = 1;	uint8_t command = scsiDev.cdb[0];    if (command == 0x1B && (scsiDev.cdb[4] & 2))    {        // CD-ROM load & eject        int start = scsiDev.cdb[4] & 1;        if (start)        {            debuglog("------ CDROM close tray");            img.ejected = false;            img.cdrom_events = 2; // New media        }        else        {            debuglog("------ CDROM open tray");            img.ejected = true;            img.cdrom_events = 3; // Media removal        }    }	else if (command == 0x43)	{		// CD-ROM Read TOC		int MSF = scsiDev.cdb[1] & 0x02 ? 1 : 0;		uint8_t track = scsiDev.cdb[6];		uint16_t allocationLength =			(((uint32_t) scsiDev.cdb[7]) << 8) +			scsiDev.cdb[8];		// Reject MMC commands for now, otherwise the TOC data format		// won't be understood.		// The "format" field is reserved for SCSI-2		uint8_t format = scsiDev.cdb[2] & 0x0F;		switch (format)		{			case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2			case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2			case 2: doReadFullTOC(0, track, allocationLength); break; // MMC2			case 3: doReadFullTOC(1, track, allocationLength); break; // MMC2			default:			{				scsiDev.status = CHECK_CONDITION;				scsiDev.target->sense.code = ILLEGAL_REQUEST;				scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;				scsiDev.phase = STATUS;			}		}	}	else if (command == 0x44)	{		// CD-ROM Read Header		int MSF = scsiDev.cdb[1] & 0x02 ? 1 : 0;		uint32_t lba = 0; // IGNORED for now		uint16_t allocationLength =			(((uint32_t) scsiDev.cdb[7]) << 8) +			scsiDev.cdb[8];		doReadHeader(MSF, lba, allocationLength);	}    else if (command == 0x4A)    {        // Get event status notifications (media change notifications)        bool immed = scsiDev.cdb[1] & 1;        doGetEventStatusNotification(immed);    }	else	{		commandHandled = 0;	}	return commandHandled;}
 |