Configuration CLI, performance tweaks, stability improvements, tested on GreenSCSI v1.0 Minimum Viable Product (MVP) PCB

This commit is contained in:
David Kuder 2021-11-29 22:43:26 -05:00
parent c2bac1a9d0
commit 494a803890
15 changed files with 3605 additions and 3288 deletions

File diff suppressed because it is too large Load Diff

37
src/cmd.h Executable file
View File

@ -0,0 +1,37 @@
#ifndef __CMD_H
#define __CMD_H
#define MAX_MSG_SIZE 256
#define MAXARG 32
#include <stdint.h>
// command line structure
typedef struct Commands_s {
const char *Name;
const char *ValidPath;
int MinParams;
const char *ShortHelp;
const char *FullHelp;
void (*Func)(int argc, char **argv);
struct Commands_s *Dispatch;
} Commands_t;
void cmdDisplay();
void cmdParse(char *cmd);
void cmdPoll();
void cmdCommandHelp(boolean singleCommand, Commands_t *table, int cmd);
void cmdDispatchHelp(Commands_t *table, int argc, char **argv);
void cmdDispatch(Commands_t *table, int argc, char **argv);
int execHandler(char *filename);
int execLoop();
void execcmd(int argc, char **argv);
void showcmd(int argc, char **argv);
void setcmd(int argc, char **argv);
extern Commands_t GlobalCommands[];
#endif /* __CMD_H */

1379
src/cmd.ino Executable file

File diff suppressed because it is too large Load Diff

35
src/config.h Executable file
View File

@ -0,0 +1,35 @@
#ifndef __CONFIG_H
#define __CONFIG_H
#define DEBUG 0 // 0:No debug information output (Faster by 2-3x+)
// 1: Debug information output to USB Serial
// 2: Debug information output to LOG.txt (slow)
#define SYNCHRO false // Support Synchronous mode.
#define READ_SPEED_OPTIMIZE true //
#define WRITE_SPEED_OPTIMIZE true //
#define USE_DB2ID_TABLE true // Use table to get ID from SEL-DB
// SCSI config
#define NUM_SCSIID 8 // Maximum number of supported SCSI-IDs (The minimum is 1)
#define NUM_SCSILUN 8 // Maximum number of LUNs supported (The minimum is 1)
#define NUM_VDEV 8 // Maximum number of VDEVs supported (The minimum is 1)
#define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
// HDD format
#define MAX_BLOCKSIZE (1 << 15) // Maximum BLOCK size (2048 to 8192 tested, 16384 had issues)
// Supported Device Types
#define SUPPORT_DISK true
#define SUPPORT_OPTICAL true
#define SUPPORT_TAPE false
// Compatibility Settings
#define SUPPORT_SASI false // Enable SASI compatiblity for Sharp X68000
#define SUPPORT_SASI_DEFAULT false // Turn it on by default
#endif /* __CONFIG_H */

75
src/defaults.ino Executable file
View File

@ -0,0 +1,75 @@
#include "config.h"
// SCSI Drive Vendor information
#if SUPPORT_DISK
static uint8_t SCSI_DISK_INQUIRY_RESPONSE[96] = {
0x00, //device type
0x00, //RMB = 0
0x05, //ISO, ECMA, ANSI version
0x02, //Response data format
35 - 4, //Additional data length
0, 0, //Reserve
0x00, //Support function
'Q', 'U', 'A', 'N', 'T', 'U', 'M', ' ', // vendor 8
'F', 'I', 'R', 'E', 'B', 'A', 'L', 'L', '1', ' ', ' ',' ', ' ', ' ', ' ', ' ', // product 16
'1', '.', '0', ' ', // version 4
// Release Number (1 Byte)
0x20,
// Revision Date (10 Bytes)
'2','0','2','1','/','1','1','/','2','2',
0
};
#endif /* SUPPORT_DISK */
#if SUPPORT_SASI
static uint8_t SCSI_NEC_INQUIRY_RESPONSE[96] = {
0x00, //Device type
0x00, //RMB = 0
0x01, //ISO,ECMA,ANSI version
0x01, //Response data format
35 - 4, //Additional data length
0, 0, //Reserve
0x00, //Support function
'N', 'E', 'C', 'I', 'T', 'S', 'U', ' ',
'A', 'r', 'd', 'S', 'C', 'S', 'i', 'n', 'o', ' ', ' ',' ', ' ', ' ', ' ', ' ',
'0', '0', '1', '0',
0
};
#endif /* SUPPORT_SASI */
#if SUPPORT_OPTICAL
static uint8_t SCSI_CDROM_INQUIRY_RESPONSE[96] = {
0x05, //device type
0x80, //RMB = 0
0x05, //ISO, ECMA, ANSI version
0x02, //Response data format
46 - 4, //Additional data length
0, 0, //Reserve
0x00, //Support function
'S', 'O', 'N', 'Y', ' ', ' ', ' ', ' ', // vendor 8
'C', 'D', '-', 'R', 'O', 'M', ' ', 'C', 'D', 'U', '-','8', '0', '0', '3', ' ', // product 16
'1', '.', '7', 'w', // version 4
// Release Number (1 Byte)
0x20,
// Revision Date (10 Bytes)
'2','0','2','1','/','1','1','/','2','2',
0
};
#endif /* SUPPORT_OPTICAL */
#if SUPPORT_TAPE
static uint8_t SCSI_TAPE_INQUIRY_RESPONSE[96] = {
0x01, //device type
0x80, //RMB = 0
0x03, //ISO, ECMA, ANSI version
0x02, //Response data format
37 - 4, //Additional data length
0, //Reserve
0x00, // Support function
0x00, // Support function
'I', 'B', 'M', ' ', ' ', ' ', ' ', ' ', // vendor 8
'U', 'L', 'T', '3', '5', '8', '0', '-', 'T', 'D', '1',' ', ' ', ' ', ' ', ' ', // product 16
'M', 'B', 'N', '0', // version 4
0
};
#endif /* SUPPORT_TAPE */

View File

@ -1,3 +1,4 @@
#include "config.h"
#include "scsi_defs.h"
void Read6CommandHandler() {
@ -22,14 +23,14 @@ void Seek6CommandHandler() {
*/
void ReadCapacityCommandHandler() {
LOGN("[ReadCapacity]");
if(!m_img) {
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
uint32_t bl = m_img->m_blocksize;
uint32_t bc = m_img->m_fileSize / bl;
uint32_t bl = m_sel->m_blocksize;
uint32_t bc = m_sel->m_fileSize / bl;
uint8_t buf[8] = {
(uint8_t)(((uint32_t)(bc >> 24))&0xff), (uint8_t)(((uint32_t)(bc >> 16))&0xff), (uint8_t)(((uint32_t)(bc >> 8))&0xff), (uint8_t)(((uint32_t)(bc))&0xff),
(uint8_t)(((uint32_t)(bl >> 24))&0xff), (uint8_t)(((uint32_t)(bl >> 16))&0xff), (uint8_t)(((uint32_t)(bl >> 8))&0xff), (uint8_t)(((uint32_t)(bl))&0xff)
@ -65,7 +66,7 @@ uint8_t onReadCommand(uint32_t adds, uint32_t len)
LOG(" ");
LOGHEX4N(len);
if(!m_img) return 0x02; // Image file absent
if(!m_sel) return 0x02; // Image file absent
LED_ON();
writeDataPhaseSD(adds, len);
@ -83,7 +84,7 @@ uint8_t onWriteCommand(uint32_t adds, uint32_t len)
LOG(" ");
LOGHEX4N(len);
if(!m_img) return 0x02; // Image file absent
if(!m_sel) return 0x02; // Image file absent
LED_ON();
readDataPhaseSD(adds, len);
@ -91,30 +92,76 @@ uint8_t onWriteCommand(uint32_t adds, uint32_t len)
return 0; //sts
}
void ConfigureDiskHandlers(int id) {
void ConfigureDiskHandlers(VirtualDevice_t *vdev) {
for(int c = 0; c < 256; c++)
m_handler[id][c] = &UnknownCommandHandler;
vdev->m_handler[c] = &UnknownCommandHandler;
m_handler[id][CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
m_handler[id][CMD_REZERO_UNIT] = &RezeroUnitCommandHandler;
m_handler[id][CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
m_handler[id][CMD_FORMAT_UNIT] = &FormatUnitCommandHandler;
m_handler[id][0x06] = &FormatUnitCommandHandler;
m_handler[id][0x07] = &ReassignBlocksCommandHandler;
m_handler[id][CMD_READ6] = &Read6CommandHandler;
m_handler[id][CMD_WRITE6] = &Write6CommandHandler;
m_handler[id][CMD_SEEK6] = &Seek6CommandHandler;
m_handler[id][CMD_INQUIRY] = &InquiryCommandHandler;
m_handler[id][CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
m_handler[id][CMD_MODE_SENSE6] = &ModeSense6CommandHandler;
m_handler[id][CMD_START_STOP_UNIT] = &StartStopUnitCommandHandler;
m_handler[id][CMD_PREVENT_REMOVAL] = &PreAllowMediumRemovalCommandHandler;
m_handler[id][CMD_READ_CAPACITY10] = &ReadCapacityCommandHandler;
m_handler[id][CMD_READ10] = &Read10CommandHandler;
m_handler[id][CMD_WRITE10] = &Write10CommandHandler;
m_handler[id][CMD_SEEK10] = &Seek10CommandHandler;
m_handler[id][CMD_MODE_SENSE10] = &ModeSense10CommandHandler;
#if SCSI_SELECT == 1
m_handler[id][0xC2] = &DTCsetDriveParameterCommandHandler;
vdev->m_handler[CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
vdev->m_handler[CMD_REZERO_UNIT] = &RezeroUnitCommandHandler;
vdev->m_handler[CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
vdev->m_handler[CMD_FORMAT_UNIT] = &FormatUnitCommandHandler;
vdev->m_handler[CMD_FORMAT_UNIT_ALT] = &FormatUnitCommandHandler;
vdev->m_handler[CMD_REASSIGN_BLOCKS] = &ReassignBlocksCommandHandler;
vdev->m_handler[CMD_READ6] = &Read6CommandHandler;
vdev->m_handler[CMD_WRITE6] = &Write6CommandHandler;
vdev->m_handler[CMD_SEEK6] = &Seek6CommandHandler;
vdev->m_handler[CMD_INQUIRY] = &InquiryCommandHandler;
vdev->m_handler[CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
vdev->m_handler[CMD_MODE_SENSE6] = &ModeSenseCommandHandler;
vdev->m_handler[CMD_START_STOP_UNIT] = &StartStopUnitCommandHandler;
vdev->m_handler[CMD_PREVENT_REMOVAL] = &PreAllowMediumRemovalCommandHandler;
vdev->m_handler[CMD_READ_CAPACITY10] = &ReadCapacityCommandHandler;
vdev->m_handler[CMD_READ10] = &Read10CommandHandler;
vdev->m_handler[CMD_WRITE10] = &Write10CommandHandler;
vdev->m_handler[CMD_SEEK10] = &Seek10CommandHandler;
vdev->m_handler[CMD_MODE_SENSE10] = &ModeSenseCommandHandler;
#if SUPPORT_SASI
if(m_sasi_mode)
vdev->m_handler[CMD_SET_DRIVE_PARAMETER] = &DTCsetDriveParameterCommandHandler;
#endif
}
// If config file exists, read the first three lines and copy the contents.
// File must be well formed or you will get junk in the SCSI Vendor fields.
void ConfigureDisk(VirtualDevice_t *vdev, const char *image_name) {
memcpy(vdev->m_inquiryresponse, SCSI_DISK_INQUIRY_RESPONSE, sizeof(SCSI_DISK_INQUIRY_RESPONSE));
if(image_name) {
char configname[MAX_FILE_PATH+1];
memcpy(configname, image_name, MAX_FILE_PATH+1);
char *psuffix = strstr(configname, ".img");
if(psuffix) {
strcpy(psuffix, ".cfg");
} else {
sprintf(configname, "hd%d%d.cfg", vdev->m_id, vdev->m_lun);
}
FsFile config_file = sd.open(configname, O_RDONLY);
if (config_file.isOpen()) {
char vendor[9];
memset(vendor, 0, sizeof(vendor));
config_file.readBytes(vendor, sizeof(vendor));
LOGN("SCSI VENDOR: ");
LOGN(vendor);
memcpy(&(vdev->m_inquiryresponse[8]), vendor, 8);
char product[17];
memset(product, 0, sizeof(product));
config_file.readBytes(product, sizeof(product));
LOGN("SCSI PRODUCT: ");
LOGN(product);
memcpy(&(vdev->m_inquiryresponse[16]), product, 16);
char version[5];
memset(version, 0, sizeof(version));
config_file.readBytes(version, sizeof(version));
LOGN("SCSI VERSION: ");
LOGN(version);
memcpy(&(vdev->m_inquiryresponse[32]), version, 4);
config_file.close();
}
}
vdev->m_type = DEV_DISK;
ConfigureDiskHandlers(vdev);
}

View File

@ -1,3 +1,4 @@
#include "config.h"
#include "scsi_defs.h"
@ -6,13 +7,20 @@
*/
void InquiryCommandHandler() {
LOGN("[Inquiry]");
uint8_t len = m_cmd[4];
uint8_t buf[36];
memcpy(buf, m_inquiryresponse[m_id], 36);
uint8_t len;
if(!m_img) buf[0] = 0x7f;
if(!m_sel) {
memset(m_responsebuffer, 0, 96);
m_responsebuffer[0] = 0x7f;
m_responsebuffer[4] = 35 - 4;
len = 36;
} else {
memcpy(m_responsebuffer, m_sel->m_inquiryresponse, 96);
len = m_responsebuffer[4] + 5;
}
writeDataPhase(len < 36 ? len : 36, buf);
len = min(len, m_cmd[4]);
writeDataPhase(len, m_responsebuffer);
m_phase = PHASE_STATUSIN;
}
@ -31,13 +39,13 @@ void RequestSenseCommandHandler() {
0,
};
if(!m_img) {
if(!m_sel) {
// Image file absent
buf[2] = 0x02; // NOT_READY
buf[12] = 0x25; // Logical Unit Not Supported
} else {
buf[2] = m_sense[m_id][m_lun].m_key;
m_sense[m_id][m_lun].m_key = 0;
buf[2] = m_sel->m_sense.m_key;
m_sel->m_sense.m_key = 0;
}
writeDataPhase(len < 18 ? len : 18, buf);
m_phase = PHASE_STATUSIN;
@ -45,10 +53,10 @@ void RequestSenseCommandHandler() {
void TestUnitCommandHandler() {
LOGN("[TestUnit]");
if(!m_img) {
if(!m_sel) {
// Image file absent
m_sense[m_id][m_lun].m_key = NOT_READY; // NOT_READY
m_sense[m_id][m_lun].m_code = NO_MEDIA; // Logical Unit Not Supported
m_sel->m_sense.m_key = NOT_READY; // NOT_READY
m_sel->m_sense.m_code = NO_MEDIA; // Logical Unit Not Supported
}
m_phase = PHASE_STATUSIN;
}
@ -68,28 +76,295 @@ void ReassignBlocksCommandHandler() {
m_phase = PHASE_STATUSIN;
}
void ModeSense6CommandHandler() {
/*
* MODE SENSE command processing.
*/
void ModeSenseCommandHandler()
{
uint16_t len, maxlen;
int page, pagemax, pagemin;
switch(m_cmd[0]) {
case CMD_MODE_SENSE6:
LOGN("[ModeSense6]");
m_sts |= onModeSenseCommand(m_cmd[1]&0x80, m_cmd[2], m_cmd[4]);
maxlen = m_cmd[4];
len = 1;
break;
case CMD_MODE_SENSE10:
LOGN("[ModeSense10]");
maxlen = ((uint16_t)m_cmd[7] << 8) | m_cmd[8];
len = 2;
break;
default:
m_sts |= 0x02;
m_phase = PHASE_STATUSIN;
return;
}
/* Check whether medium is present */
if(!m_sel) {
m_sts |= 0x02;
m_phase = PHASE_STATUSIN;
return;
}
memset(m_responsebuffer, 0, sizeof(m_responsebuffer));
#if 0
if(m_sasi_mode) {
int pageCode = cmd2 & 0x3F;
// Assuming sector size 512, number of sectors 25, number of heads 8 as default settings
int size = m_sel->m_fileSize;
int cylinders = (int)(size >> 9);
cylinders >>= 3;
cylinders /= 25;
int sectorsize = 512;
int sectors = 25;
int heads = 8;
// Sector size
int disksize = 0;
for(disksize = 16; disksize > 0; --(disksize)) {
if ((1 << disksize) == sectorsize)
break;
}
// Number of blocks
uint32_t diskblocks = (uint32_t)(size >> disksize);
int a = 4;
if(dbd == 0) {
uint32_t bl = m_sel->m_blocksize;
uint32_t bc = m_sel->m_fileSize / bl;
uint8_t c[8] = {
0,// Density code
bc >> 16, bc >> 8, bc,
0, //Reserve
bl >> 16, bl >> 8, bl
};
memcpy(&m_responsebuffer[4], c, 8);
a += 8;
m_responsebuffer[3] = 0x08;
}
switch(pageCode) {
case 0x3F:
{
m_responsebuffer[len + 0] = 0x01;
m_responsebuffer[len + 1] = 0x06;
a += 8;
}
case 0x03: // drive parameters
{
m_responsebuffer[len + 0] = 0x80 | 0x03; // Page code
m_responsebuffer[len + 1] = 0x16; // Page length
m_responsebuffer[len + 2] = (uint8_t)(heads >> 8);// number of sectors / track
m_responsebuffer[len + 3] = (uint8_t)(heads);// number of sectors / track
m_responsebuffer[len + 10] = (uint8_t)(sectors >> 8);// number of sectors / track
m_responsebuffer[len + 11] = (uint8_t)(sectors);// number of sectors / track
int size = 1 << disksize;
m_responsebuffer[len + 12] = (uint8_t)(size >> 8);// number of sectors / track
m_responsebuffer[len + 13] = (uint8_t)(size);// number of sectors / track
a += 24;
if(pageCode != 0x3F) {
break;
}
}
case 0x04: // drive parameters
{
LOGN("AddDrive");
m_responsebuffer[len + 0] = 0x04; // Page code
m_responsebuffer[len + 1] = 0x12; // Page length
m_responsebuffer[len + 2] = (cylinders >> 16);// Cylinder length
m_responsebuffer[len + 3] = (cylinders >> 8);
m_responsebuffer[len + 4] = cylinders;
m_responsebuffer[len + 5] = heads; // Number of heads
a += 20;
if(pageCode != 0x3F) {
break;
}
}
default:
break;
}
m_responsebuffer[0] = a - 1;
writeDataPhase(len < a ? len : a, m_responsebuffer);
} else
#endif
{
/* Default medium type */
m_responsebuffer[len++] = (m_sel->m_type == DEV_OPTICAL) ? 0xf0 : 0x00;
/* Write protected */
m_responsebuffer[len++] = (m_sel->m_type == DEV_OPTICAL) ? 0x80 : 0x00;
// ModeSense10 has two extra bytes, and Block Descriptor Length has an extra MSB
if(m_cmd[0] == CMD_MODE_SENSE10) {
len += 2;
m_responsebuffer[len++] = 0;
}
/* Add block descriptor if DBD is not set */
if (m_cmd[1] & 0x08) {
m_responsebuffer[len++] = 0; /* No block descriptor */
} else {
if(m_sel->m_type == DEV_TAPE) {
m_responsebuffer[len++] = 8; /* Block descriptor length */
m_responsebuffer[len++] = 0x40; /* Medium Density Code */
m_responsebuffer[len++] = 0x00; /* Number of Blocks (0) */
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; /* Block Length 1024 */
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x04;
m_responsebuffer[len++] = 0x00;
} else {
uint32_t capacity = (m_sel->m_fileSize / m_sel->m_blocksize);
m_responsebuffer[len++] = 8; /* Block descriptor length */
m_responsebuffer[len++] = (capacity >> 24) & 0xff;
m_responsebuffer[len++] = (capacity >> 16) & 0xff;
m_responsebuffer[len++] = (capacity >> 8) & 0xff;
m_responsebuffer[len++] = capacity & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize >> 24) & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize >> 16) & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize >> 8) & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize) & 0xff;
}
}
/* Check for requested mode page */
page = m_cmd[2] & 0x3F;
pagemax = (page != 0x3f) ? page : 0x3e;
pagemin = (page != 0x3f) ? page : 0x00;
for(page = pagemax; page >= pagemin; page--) {
switch (page) {
case MODEPAGE_VENDOR_SPECIFIC:
/* Accept request only for current values */
if (m_cmd[2] & 0xC0) {
//DEBUGPRINT(DBG_TRACE, " [2]==%d", m_cmd[2]);
/* Prepare sense data */
m_sts |= STATUS_CHECK;
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
return;
}
/* Unit attention */
m_responsebuffer[len++] = 0x80; // PS, page id
m_responsebuffer[len++] = 0x02; // Page length
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00;
break;
case MODEPAGE_RW_ERROR_RECOVERY:
if(m_cmd[2] & 0x40) {
m_sts |= STATUS_CHECK;
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
return;
}
m_responsebuffer[len++] = MODEPAGE_RW_ERROR_RECOVERY; // PS, page id
m_responsebuffer[len++] = 0x0a; // Page length
m_responsebuffer[len++] = 0x05; //
m_responsebuffer[len++] = 0x00; // Read Retry Count
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Write Retry Count
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Recovery Time Limit
m_responsebuffer[len++] = 0x00;
break;
#if 0
case MODEPAGE_DCRC_PARAMETERS:
m_responsebuffer[len++] = 0x82; // PS, page id
m_responsebuffer[len++] = 0x0e; // Page length
m_responsebuffer[len++] = 0xe6; // Buffer full ratio, 90%
m_responsebuffer[len++] = 0x1a; // Buffer empty ratio, 10%
m_responsebuffer[len++] = 0x00; // Bus inactivity limit
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Disconnect time limit
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Connect time limit
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Maximum burst size
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // EMDP, Dimm, DTDC
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
break;
#endif
case MODEPAGE_FORMAT_PARAMETERS:
m_responsebuffer[len + 0] = MODEPAGE_FORMAT_PARAMETERS; //Page code
m_responsebuffer[len + 1] = 0x16; // Page length
m_responsebuffer[len + 11] = 0x3F;//Number of sectors / track
len += 24;
break;
case MODEPAGE_RIGID_GEOMETRY:
{
uint32_t bc = m_sel->m_fileSize / m_sel->m_file;
m_responsebuffer[len + 0] = MODEPAGE_RIGID_GEOMETRY; //Page code
m_responsebuffer[len + 1] = 0x16; // Page length
m_responsebuffer[len + 2] = bc >> 16;// Cylinder length
m_responsebuffer[len + 3] = bc >> 8;
m_responsebuffer[len + 4] = bc;
m_responsebuffer[len + 5] = 1; //Number of heads
len += 24;
}
break;
default:
if(pagemin == pagemax) {
m_sts |= STATUS_CHECK;
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
return;
}
}
}
/* Report size of requested data */
if(m_cmd[0] == CMD_MODE_SENSE6) {
m_responsebuffer[0] = len;
} else {
m_responsebuffer[0] = (len >> 8) & 0xff;
m_responsebuffer[1] = len & 0xff;
}
/* Truncate data if necessary */
if (maxlen < len) {
len = maxlen;
}
// Send it
writeDataPhase(len, m_responsebuffer);
}
m_phase = PHASE_STATUSIN;
}
uint8_t onModeSelectCommand() {
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x04;
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x04;
return 0x02;
}
void ModeSelect6CommandHandler() {
LOG("[ModeSelect6] ");
uint16_t len = m_cmd[4];
readDataPhase(len, m_buf);
readDataPhase(len, m_responsebuffer);
for(int i = 1; i < len; i++ ) {
LOG(":");
LOGHEX2(m_buf[i]);
LOGHEX2(m_responsebuffer[i]);
}
LOGN("");
// m_sts |= onModeSelectCommand();
@ -99,11 +374,11 @@ void ModeSelect6CommandHandler() {
void ModeSelect10CommandHandler() {
LOGN("[ModeSelect10]");
uint16_t len = ((uint16_t)m_cmd[7] << 8) | m_cmd[8];
readDataPhase(len, m_buf);
readDataPhase(len, m_responsebuffer);
for(int i = 1; i < len; i++ ) {
LOG(":");
LOGHEX2(m_buf[i]);
LOGHEX2(m_responsebuffer[i]);
}
LOGN("");
// m_sts |= onModeSelectCommand();
@ -120,16 +395,10 @@ void PreAllowMediumRemovalCommandHandler() {
m_phase = PHASE_STATUSIN;
}
void ModeSense10CommandHandler() {
LOGN("[ModeSense10]");
onModeSenseCommand(m_cmd[1] & 0x80, m_cmd[2], ((uint32_t)m_cmd[7] << 8) | m_cmd[8]);
m_phase = PHASE_STATUSIN;
}
void UnknownCommandHandler() {
LOGN("[*Unknown]");
m_sts |= 0x02;
m_sense[m_id][m_lun].m_key = 5;
m_sel->m_sense.m_key = 5;
m_phase = PHASE_STATUSIN;
}

File diff suppressed because it is too large Load Diff

893
src/greenscsi.ino Executable file
View File

@ -0,0 +1,893 @@
/*
* GreenSCSI
* Copyright (c) 2021 David Kuder
*
* Based on BlueSCSI
* Copyright (c) 2021 Eric Helgeson, Androda
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 of the License, or (at your
* option) any later version.
*
* This file 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://github.com/erichelgeson/bluescsi.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (c) 2019 komatsu
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <Arduino.h> // For Platform.IO
#include <SPI.h>
#include <SdFat.h>
#include "sdios.h"
#include "config.h"
#include "scsi_defs.h"
#include "cmd.h"
// SDFAT
SdFs sd;
#if DEBUG == 1
#define LOG(XX) Serial.print(XX)
#define LOGHEX2(XX) Serial.printf("%02x", XX)
#define LOGHEX4(XX) Serial.printf("%04x", XX)
#define LOGHEX6(XX) Serial.printf("%06x", XX)
#define LOGHEX8(XX) Serial.printf("%08x", XX)
#define LOGN(XX) Serial.println(XX)
#define LOGHEX2N(XX) Serial.printf("%02x\r\n", XX)
#define LOGHEX4N(XX) Serial.printf("%04x\r\n", XX)
#define LOGHEX6N(XX) Serial.printf("%06x\r\n", XX)
#define LOGHEX8N(XX) Serial.printf("%08x\r\n", XX)
#elif DEBUG == 2
#define LOG(XX) LOG_FILE.print(XX); LOG_FILE.sync();
#define LOGHEX2(XX) LOG_FILE.printf("%02x", XX); LOG_FILE.sync();
#define LOGHEX4(XX) LOG_FILE.printf("%04x", XX); LOG_FILE.sync();
#define LOGHEX6(XX) LOG_FILE.printf("%06x", XX); LOG_FILE.sync();
#define LOGHEX8(XX) LOG_FILE.printf("%08x", XX); LOG_FILE.sync();
#define LOGN(XX) LOG_FILE.println(XX); LOG_FILE.sync();
#define LOGHEX2N(XX) LOG_FILE.printf("%02x\r\n", XX); LOG_FILE.sync();
#define LOGHEX4N(XX) LOG_FILE.printf("%04x\r\n", XX); LOG_FILE.sync();
#define LOGHEX6N(XX) LOG_FILE.printf("%06x\r\n", XX); LOG_FILE.sync();
#define LOGHEX8N(XX) LOG_FILE.printf("%08x\r\n", XX); LOG_FILE.sync();
#else
#define LOG(XX) //Serial.print(XX)
#define LOGHEX2(XX) //Serial.printf("%02x", XX)
#define LOGHEX4(XX) //Serial.printf("%04x", XX)
#define LOGHEX6(XX) //Serial.printf("%06x", XX)
#define LOGHEX8(XX) //Serial.printf("%08x", XX)
#define LOGN(XX) //Serial.println(XX)
#define LOGHEX2N(XX) //Serial.printf("%02x\r\n", XX)
#define LOGHEX4N(XX) //Serial.printf("%04x\r\n", XX)
#define LOGHEX6N(XX) //Serial.printf("%06x\r\n", XX)
#define LOGHEX8N(XX) //Serial.printf("%08x\r\n", XX)
#endif
#define DB0 0 // SCSI:DB0 Port B Bit 16
#define DB1 1 // SCSI:DB1 Port B Bit 17
#define DB2 29 // SCSI:DB2 Port B Bit 18
#define DB3 30 // SCSI:DB3 Port B Bit 19
#define DB4 43 // SCSI:DB4 Port B Bit 20
#define DB5 46 // SCSI:DB5 Port B Bit 21
#define DB6 44 // SCSI:DB6 Port B Bit 22
#define DB7 45 // SCSI:DB7 Port B Bit 23
#define DB8 32 // SCSI:DBP Port B Bit 11
#define ATN 12 // SCSI:ATN Port C Bit 7
#define BSY 10 // SCSI:BSY Port C Bit 4
#define ACK 9 // SCSI:ACK Port C Bit 3
#define RST 8 // SCSI:RST Port D Bit 3
#define MSG 7 // SCSI:MSG Port D Bit 2
#define SEL 6 // SCSI:SEL Port D Bit 4
#define CD 5 // SCSI:C/D Port D Bit 7
#define REQ 4 // SCSI:REQ Port A Bit 13
#define IO 3 // SCSI:I/O Port A Bit 12
#define SD_CS BUILTIN_SDCARD // SDCARD:CS
#define LED 13 // LED Port C Bit 5
// LED control
#define LED_ON() { GPIOC_PSOR = (1 << 5); }
#define LED_OFF() { GPIOC_PCOR = (1 << 5); }
#define SET_REQ_ACTIVE() { GPIOA_PCOR = (1 << 13); }
#define SET_REQ_INACTIVE() { GPIOA_PSOR = (1 << 13); }
#define SET_MSG_ACTIVE() { GPIOD_PCOR = (1 << 2); }
#define SET_MSG_INACTIVE() { GPIOD_PSOR = (1 << 2); }
//#define SET_CD_ACTIVE() { GPIOD_PCOR = (1 << 7); }
//#define SET_CD_INACTIVE() { GPIOD_PSOR = (1 << 7); }
//#define SET_IO_ACTIVE() { GPIOA_PCOR = (1 << 12); }
//#define SET_IO_INACTIVE() { GPIOA_PSOR = (1 << 12); }
//#define SET_REQ_ACTIVE() { pinMode(REQ, OUTPUT_OPENDRAIN); digitalWrite(REQ, LOW); }
//#define SET_REQ_INACTIVE() { digitalWrite(REQ, HIGH); pinMode(REQ, INPUT); }
//#define SET_MSG_ACTIVE() { pinMode(MSG, OUTPUT_OPENDRAIN); digitalWrite(MSG, LOW); }
//#define SET_MSG_INACTIVE() { digitalWrite(MSG, HIGH); pinMode(MSG, INPUT); }
#define SET_CD_ACTIVE() { pinMode(CD, OUTPUT_OPENDRAIN); digitalWrite(CD, LOW); }
#define SET_CD_INACTIVE() { digitalWrite(CD, HIGH); pinMode(CD, INPUT); }
#define SET_IO_ACTIVE() { pinMode(IO, OUTPUT_OPENDRAIN); digitalWrite(IO, LOW); }
#define SET_IO_INACTIVE() { digitalWrite(IO, HIGH); pinMode(IO, INPUT); }
//#define SET_BSY_ACTIVE() { pinMode(BSY, OUTPUT_OPENDRAIN); digitalWrite(BSY, LOW); }
//#define SET_BSY_INACTIVE() { digitalWrite(BSY, HIGH); pinMode(BSY, INPUT); }
#define SET_BSY_ACTIVE() { GPIOC_PCOR = (1 << 4); }
#define SET_BSY_INACTIVE() { GPIOC_PSOR = (1 << 4); }
#define GET_ACK() (!(GPIOC_PDIR & (1 << 3)))
#define GET_ATN() (!(GPIOC_PDIR & (1 << 7)))
#define GET_BSY() (!(GPIOC_PDIR & (1 << 4)))
#define GET_RST() (!(GPIOD_PDIR & (1 << 3)))
#define GET_SEL() (!(GPIOD_PDIR & (1 << 4)))
// Turn on the output only for BSY
// BSY,REQ,MSG,CD,IO Turn on the output (no change required for OD)
#define SCSI_TARGET_ACTIVE() { }
// BSY,REQ,MSG,CD,IO Turn off output
#define SCSI_TARGET_INACTIVE() { SCSI_DB_INPUT(); SET_REQ_INACTIVE(); SET_MSG_INACTIVE(); SET_CD_INACTIVE(); SET_IO_INACTIVE(); SET_BSY_INACTIVE(); }
// HDImage file
#define HDIMG_ID_POS 2 // Position to embed ID number
#define HDIMG_LUN_POS 3 // Position to embed LUN numbers
#define HDIMG_BLK_POS 5 // Position to embed block size numbers
#define MAX_FILE_PATH 32 // Maximum file name length
typedef struct m_sense_s {
uint8_t m_key;
uint8_t m_code;
uint8_t m_key_specific[4];
} msense_t;
typedef void (*CommandHandler_t)();
CommandHandler_t m_badlunhandler[256];
// VirtualDevice
typedef struct VirtualDevice_s
{
boolean m_enabled;
uint8_t m_id;
uint8_t m_lun;
uint8_t m_type; // Device Type
char m_filename[MAX_FILE_PATH+1];
FsFile m_file; // File object
uint64_t m_fileSize; // File size
size_t m_blocksize; // SCSI BLOCK size
#if SUPPORT_TAPE
size_t m_filemarks; // Tape position counter (file marks since BOM)
#endif
uint8_t m_inquiryresponse[96];
CommandHandler_t m_handler[256];
msense_t m_sense;
} VirtualDevice_t;
VirtualDevice_t m_vdev[NUM_VDEV]; // Maximum number
uint8_t m_vdevcnt = 0; // Number of allocated vdevs
uint8_t m_vdevmap[NUM_SCSIID][NUM_SCSILUN]; // Map ID/LUN to a vdev
volatile bool m_isBusReset = false; // Bus reset
uint8_t scsi_id_mask; // Mask list of responding SCSI IDs
uint8_t m_id; // Currently responding SCSI-ID
uint8_t m_lun; // Logical unit number currently responding
uint8_t m_sts; // Status uint8_t
uint8_t m_msg; // Message uint8_ts
VirtualDevice_t *m_sel; // VirtualDevice for current SCSI-ID, LUN
uint8_t m_buf[MAX_BLOCKSIZE+1]; // General purpose buffer + overrun fetch
int m_msc;
uint8_t m_msb[256]; // Command storage uint8_ts
uint8_t m_cmdlen;
uint8_t m_cmd[12];
uint8_t m_responsebuffer[256];
#if SUPPORT_SASI
boolean m_sasi_mode = SUPPORT_SASI_DEFAULT;
#endif
uint16_t ledbits = 0;
uint8_t ledbit = 0;
typedef enum {
PHASE_BUSFREE = 0,
PHASE_SELECTION,
PHASE_MESSAGEOUT,
PHASE_COMMAND,
PHASE_STATUSIN,
PHASE_MESSAGEIN,
} phase_t;
phase_t m_phase = PHASE_BUSFREE;
// Log File
#define VERSION "1.2-20211126"
#if DEBUG == 2
#define LOG_FILENAME "LOG.txt"
FsFile LOG_FILE;
#endif
void onBusReset(void);
void initFileLog(void);
void finalizeFileLog(void);
/*
* IO read.
*/
inline uint8_t readIO(void)
{
// Port input data register
uint32_t ret = ~GPIOB_PDIR;
//uint8_t bret = ret >> 16;
#if READ_PARITY_CHECK
if((db_bsrr[bret] ^ ret) & 0x0800)
m_sts |= 0x01; // parity error
#endif
return ret >> 16; //bret;
}
boolean OpenImage(VirtualDevice_t *h, const char *image_name)
{
h->m_fileSize = 0;
h->m_file = sd.open(image_name, O_RDWR);
if(h->m_file.isOpen()) {
h->m_fileSize = h->m_file.size();
return true; // File opened
}
return false;
}
#if SUPPORT_DISK or SUPPORT_OPTICAL
/*
* Open HDD or CDROM image file
*/
boolean OpenDiskImage(VirtualDevice_t *h, const char *image_name, int blocksize)
{
if(!strncmp(image_name, "/sd/", 4))
image_name += 3;
h->m_fileSize = 0;
h->m_blocksize = blocksize;
h->m_file = sd.open(image_name, O_RDWR);
if(h->m_file.isOpen())
{
h->m_fileSize = h->m_file.size();
LOG(" Imagefile: ");
LOG(image_name);
if(h->m_fileSize>0)
{
// check blocksize dummy file
LOG(" / ");
LOG(h->m_fileSize / h->m_blocksize);
LOG(" sectors / ");
LOG(h->m_fileSize / 1024);
LOG(" KiB / ");
LOG(h->m_fileSize / 1024 / 1024);
LOGN(" MiB");
return true; // File opened
}
else
{
h->m_file.close();
h->m_fileSize = h->m_blocksize = 0; // no file
LOGN(" FileSizeError");
}
}
return false;
}
#endif /* SUPPORT_DISK or SUPPORT_OPTICAL */
#if SUPPORT_TAPE
/*
* Open Tape image file
*/
boolean OpenTapeImage(VirtualDevice_t *h, const char *image_name)
{
h->m_fileSize = 0;
h->m_blocksize = 0;
h->m_filemarks = 0;
h->m_file = sd.open(image_name, O_RDWR);
if(h->m_file.isOpen()) {
h->m_fileSize = h->m_file.size();
h->m_blocksize = 1024;
LOG(" Imagefile: ");
LOG(image_name);
if(h->m_fileSize>0) {
// check blocksize dummy file
LOG(" / ");
LOG(h->m_fileSize / 1024);
LOG(" KiB / ");
LOG(h->m_fileSize / 1024 / 1024);
LOGN(" MiB");
} else {
LOGN(" / Blank Tape");
}
return true; // File opened
}
return false;
}
#endif /* SUPPORT_TAPE */
void pinModeFastSlew(uint8_t pin, int dummy) {
pinMode(pin, OUTPUT_OPENDRAIN);
volatile uint32_t *config = portConfigRegister(pin);
*config = PORT_PCR_ODE | PORT_PCR_DSE | PORT_PCR_MUX(1);
}
/*
* Initialization.
* Initialize the bus and set the PIN orientation
*/
void setup()
{
#if DEBUG > 0
Serial.begin(115200);
delay(1500);
#endif
for(int id = 0; id < NUM_SCSIID; id++)
for(int lun = 0; lun < NUM_SCSILUN; lun++)
m_vdevmap[id][lun] = 0xff;
m_vdevcnt = 0;
// PIN initialization
pinMode(LED, OUTPUT);
LED_OFF();
//GPIO(SCSI BUS)Initialization
// Input port
pinMode(ATN, INPUT_PULLUP);
pinMode(ACK, INPUT_PULLUP);
pinMode(RST, INPUT_PULLUP);
pinMode(SEL, INPUT_PULLUP);
// Output port
pinModeFastSlew(BSY, OUTPUT_OPENDRAIN);
pinModeFastSlew(MSG, OUTPUT_OPENDRAIN);
pinModeFastSlew(CD, OUTPUT_OPENDRAIN);
pinModeFastSlew(IO, OUTPUT_OPENDRAIN);
pinModeFastSlew(REQ, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB0, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB1, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB2, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB3, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB4, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB5, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB6, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB7, OUTPUT_OPENDRAIN);
pinModeFastSlew(DB8, OUTPUT_OPENDRAIN);
// DB and DP are input modes
SCSI_DB_INPUT();
// Turn off the output port
SCSI_TARGET_INACTIVE();
//Occurs when the RST pin state changes from HIGH to LOW
attachInterrupt(RST, onBusReset, FALLING);
attachInterrupt(SEL, SelectionPhaseISR, FALLING);
LED_ON();
if(!sd.begin(SdioConfig(FIFO_SDIO))) {
#if DEBUG > 0
Serial.println("SD initialization failed!");
#endif
ledbits = 0b0000001010101010;
}
initFileLog();
ConfigureBadLunHandlers();
//Sector data overrun uint8_t setting
//m_buf[MAX_BLOCKSIZE] = 0xff; // DB0 all off,DBP off
//HD image file open
scsi_id_mask = 0x00;
// If greenscsi.cfg exists, run it
if(sd.exists("/greenscsi.cfg"))
execHandler((char*)"/sd/greenscsi.cfg");
// Scan for images if we haven't defined any targets yet.
if(m_vdevcnt == 0) findImages();
finalizeFileLog();
LED_OFF();
cmdDisplay();
}
void findImages() {
// Iterate over the root path in the SD card looking for candidate image files.
SdFile root;
root.open("/");
SdFile file;
bool imageReady;
int usedDefaultId = 0;
while (1) {
if (!file.openNext(&root, O_READ)) break;
char name[MAX_FILE_PATH+1];
if(!file.isDir()) {
file.getName(name, MAX_FILE_PATH+1);
file.close();
String file_name = String(name);
file_name.toLowerCase();
#if SUPPORT_DISK or SUPPORT_OPTICAL
if((file_name.startsWith("hd") || file_name.startsWith("cd")) && (file_name.endsWith(".img") || file_name.endsWith(".hda"))) {
// Defaults for Hard Disks and CD-ROMs
int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
int lun = 0;
int blk = 512;
// Positionally read in and coerase the chars to integers.
// We only require the minimum and read in the next if provided.
int file_name_length = file_name.length();
if(file_name_length > 2) { // HD[N]
int tmp_id = file_name[HDIMG_ID_POS] - '0';
if(tmp_id > -1 && tmp_id < NUM_SCSIID) {
id = tmp_id; // If valid id, set it, else use default
} else {
usedDefaultId++;
}
}
if(file_name_length > 3) { // HD0[N]
int tmp_lun = file_name[HDIMG_LUN_POS] - '0';
if(tmp_lun > -1 && tmp_lun < NUM_SCSILUN) {
lun = tmp_lun; // If valid id, set it, else use default
}
}
int blk1 = 0, blk2 = 0, blk3 = 0, blk4 = 0;
if(file_name_length > 8) { // HD00_[111]
blk1 = name[HDIMG_BLK_POS] - '0';
blk2 = name[HDIMG_BLK_POS+1] - '0';
blk3 = name[HDIMG_BLK_POS+2] - '0';
if(file_name_length > 9) // HD00_NNN[1]
blk4 = name[HDIMG_BLK_POS+3] - '0';
}
if(blk1 == 2 && blk2 == 5 && blk3 == 6) {
blk = 256;
} else if(blk1 == 1 && blk2 == 0 && blk3 == 2 && blk4 == 4) {
blk = 1024;
} else if(blk1 == 2 && blk2 == 0 && blk3 == 4 && blk4 == 8) {
blk = 2048;
}
if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
uint8_t v = m_vdevcnt;
VirtualDevice_t *h = &m_vdev[v];
m_vdevmap[id][lun] = v;
h->m_id = id;
h->m_lun = lun;
imageReady = false;
// Configure Handlers and Inquiry Response for all luns
#if SUPPORT_DISK
if(file_name.startsWith("hd")) {
LOG(" Disk at ID ");
LOG(id);
LOG(":LUN ");
LOG(lun);
ConfigureDisk(h, name);
imageReady = OpenDiskImage(h, name, blk);
}
#endif /* SUPPORT_DISK */
#if SUPPORT_OPTICAL
if(file_name.startsWith("cd")) {
LOG("CDROM at ID ");
LOG(id);
LOG(":LUN ");
LOG(lun);
ConfigureOptical(h, name);
imageReady = OpenDiskImage(h, name, blk);
}
#endif /* SUPPORT_OPTICAL */
if(imageReady) { // Marked as a responsive ID
m_vdevcnt++;
sprintf(h->m_filename, "/sd/%s", name);
h->m_enabled = true;
scsi_id_mask |= 1<<id;
}
} else {
LOG("Bad LUN or SCSI id for image: ");
LOGN(name);
}
} else
#endif /* SUPPORT_DISK or SUPPORT_OPTICAL */
#if SUPPORT_TAPE
if(file_name.startsWith("dt") && file_name.endsWith(".tap")) {
// Defaults for Tapes
int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
int lun = 0;
// Positionally read in and coerase the chars to integers.
// We only require the minimum and read in the next if provided.
int file_name_length = file_name.length();
if(file_name_length > 2) { // HD[N]
int tmp_id = file_name[HDIMG_ID_POS] - '0';
if(tmp_id > -1 && tmp_id < NUM_SCSIID) {
id = tmp_id; // If valid id, set it, else use default
} else {
usedDefaultId++;
}
}
if(file_name_length > 3) { // HD0[N]
int tmp_lun = file_name[HDIMG_LUN_POS] - '0';
if(tmp_lun > -1 && tmp_lun < NUM_SCSILUN) {
lun = tmp_lun; // If valid id, set it, else use default
}
}
if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
uint8_t v = m_vdevcnt;
VirtualDevice_t *h = &m_vdev[v];
m_vdevmap[id][lun] = v;
h->m_id = id;
h->m_lun = lun;
imageReady = false;
LOG(" Tape at ID ");
LOG(id);
LOG(":LUN ");
LOG(lun);
ConfigureTape(h, name);
imageReady = OpenTapeImage(h, name);
if(imageReady) { // Marked as a responsive ID
m_vdevcnt++;
h->m_enabled = true;
sprintf(h->m_filename, "/sd/%s", name);
scsi_id_mask |= 1<<id;
}
} else {
LOG("Bad LUN or SCSI id for image: ");
LOGN(name);
}
} else
#endif /* SUPPORT_TAPE */
{
// LOG("Not an image: ");
// LOGN(name);
}
} else {
file.close();
}
}
if(usedDefaultId > 0) {
LOGN("!! More than one image did not specify a SCSI ID. Last file will be used at ID 1. !!");
}
if(m_vdevcnt == 0) {
LOGN("!! No Virtual Devices defined. Please manually configure. !!");
}
root.close();
}
/*
* Setup initialization logfile
*/
void initFileLog() {
#if DEBUG == 2
LOG_FILE = sd.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
#endif
#if DEBUG
LOGN("GreenSCSI <-> SD - https://github.com/dkgrizzly/GreenSCSI");
LOG("VERSION: ");
LOGN(VERSION);
LOG("DEBUG:");
LOGN(DEBUG);
#endif
}
/*
* Finalize initialization logfile
*/
void finalizeFileLog() {
LOGN("Initialization complete.");
#if DEBUG == 2
LOG_FILE.close();
#endif
}
/*
* MsgIn2.
*/
void MsgIn2(int msg)
{
//LOGN("MsgIn2");
SET_MSG_ACTIVE();
SET_CD_ACTIVE();
SET_IO_ACTIVE();
writeHandshake(msg);
}
/*
* MsgOut2.
*/
void MsgOut2()
{
//LOGN("MsgOut2");
SET_MSG_ACTIVE();
SET_CD_ACTIVE();
SET_IO_INACTIVE();
m_msb[m_msc] = readHandshake();
m_msc++;
m_msc %= 256;
}
/*
* Main loop.
*/
elapsedMillis ticktock;
void loop() {
if(ledbits) {
if(ticktock > 250) {
if(ledbits & (1<<ledbit)) {
LED_ON();
} else {
LED_OFF();
}
ledbit++;
ledbit &= 0xf;
ticktock = 0;
}
// Cancel ID error indication if we have any IDs enabled
if((ledbit == 0) && (ledbits == 0b0000000000101010) && (scsi_id_mask != 0))
ledbits = 0;
}
if(!ledbits) {
if(scsi_id_mask == 0) ledbits = 0b0000000000101010;
}
switch(m_phase) {
default:
case PHASE_BUSFREE:
BusFreePhaseHandler();
break;
case PHASE_SELECTION:
//SelectionPhaseHandler();
// Do other work while we wait for SEL to be asserted
cmdPoll();
break;
case PHASE_MESSAGEOUT:
MessageOutPhaseHandler();
break;
case PHASE_COMMAND:
CommandPhaseHandler();
break;
case PHASE_STATUSIN:
StatusInPhaseHandler();
break;
case PHASE_MESSAGEIN:
MessageInPhaseHandler();
break;
}
}
void SelectionPhaseISR() {
m_msg = 0;
// Wait until RST = H, BSY = H, SEL = L
while(GET_BSY() || GET_RST()) {
if(!GET_SEL()) return;
}
// BSY+ SEL-
// If the ID to respond is not driven, wait for the next
uint8_t scsiid = readIO() & scsi_id_mask;
if(scsiid == 0) {
// We were not selected, treat this as an abort and cancel any transactions we can.
m_isBusReset = true;
SCSI_TARGET_INACTIVE();
m_phase = PHASE_BUSFREE;
return;
}
LOG("Selection ");
m_isBusReset = false;
// Set BSY to-when selected
SET_BSY_ACTIVE(); // Turn only BSY output ON, ACTIVE
// Ask for a TARGET-ID to respond
#if USE_DB2ID_TABLE
m_id = db2scsiid[scsiid];
#else
for(m_id=7;m_id>=0;m_id--)
if(scsiid & (1<<m_id)) break;
#endif
m_lun = 0xff;
//LOGN("Wait !SEL");
// Wait until SEL becomes inactive
while(GET_SEL() && !GET_BSY()) {
if(m_isBusReset) {
LOGN("!SEL");
m_phase = PHASE_BUSFREE;
return;
}
}
m_phase = PHASE_MESSAGEOUT;
}
void MessageOutPhaseHandler() {
//LOGN("ACTIVE");
SCSI_TARGET_ACTIVE(); // (BSY), REQ, MSG, CD, IO output turned on
//
if(GET_ATN()) {
//LOGN("ATN");
bool syncenable = false;
int syncperiod = 50;
int syncoffset = 0;
int loopWait = 0;
m_msc = 0;
memset(m_msb, 0x00, sizeof(m_msb));
while(GET_ATN() && loopWait < 255) {
MsgOut2();
loopWait++;
}
for(int i = 0; i < m_msc; i++) {
// ABORT
if (m_msb[i] == 0x06) {
m_phase = PHASE_BUSFREE;
return;
}
// BUS DEVICE RESET
if (m_msb[i] == 0x0C) {
syncoffset = 0;
m_phase = PHASE_BUSFREE;
return;
}
// IDENTIFY message overrides LUN IDs specified in commands.
if (m_msb[i] & 0x80) {
m_lun = m_msb[i] & 0x07;
}
// Extended message
if (m_msb[i] == 0x01) {
// Check only when synchronous transfer is possible
if (!syncenable || m_msb[i + 2] != 0x01) {
MsgIn2(0x07);
break;
}
// Transfer period factor(50 x 4 = Limited to 200ns)
syncperiod = m_msb[i + 3];
if (syncperiod > 50) {
syncperiod = 50;
}
// REQ/ACK offset(Limited to 16)
syncoffset = m_msb[i + 4];
if (syncoffset > 16) {
syncoffset = 16;
}
// STDR response message generation
MsgIn2(0x01);
MsgIn2(0x03);
MsgIn2(0x01);
MsgIn2(syncperiod);
MsgIn2(syncoffset);
break;
}
}
}
m_phase = PHASE_COMMAND;
}
void CommandPhaseHandler() {
LOG("Command:");
SET_MSG_INACTIVE();
SET_CD_ACTIVE();
SET_IO_INACTIVE();
m_cmd[0] = readHandshake();
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
return;
}
LOGHEX2(m_cmd[0]);
// Command length selection, reception
static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
m_cmdlen = cmd_class_len[m_cmd[0] >> 5];
// Receive the remaining command bytes
for(int i = 1; i < m_cmdlen; i++ ) {
m_cmd[i] = readHandshake();
LOG(":");
LOGHEX2(m_cmd[i]);
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
return;
}
}
// LUN confirmation
m_sts = m_cmd[1]&0xe0; // Preset LUN in status byte if not specified by Identify Message
if(m_lun == 0xff) m_lun = (m_sts>>5);
// HDD Image selection
m_sel = (VirtualDevice_t *)0; // None
if( (m_lun < NUM_SCSILUN) )
{
uint8_t v = m_vdevmap[m_id][m_lun];
if(v < NUM_VDEV) {
m_sel = &(m_vdev[v]); // There is an image
if(!m_sel->m_enabled)
m_sel = (VirtualDevice_t *)0; // Image absent
}
}
LOG(":ID ");
LOG(m_id);
LOG(":LUN ");
LOG(m_lun);
LOG(" ");
if(!m_sel) {
m_badlunhandler[m_cmd[0]]();
} else {
m_sel->m_handler[m_cmd[0]]();
}
}
void ConfigureBadLunHandlers() {
for(int c = 0; c < 256; c++)
m_badlunhandler[c] = &BadLunCommandHandler;
m_badlunhandler[0x03] = &RequestSenseCommandHandler;
m_badlunhandler[0x12] = &InquiryCommandHandler;
}
void StatusInPhaseHandler() {
//LOGN("Sts");
SET_MSG_INACTIVE();
SET_CD_ACTIVE();
SET_IO_ACTIVE();
writeHandshake(m_sts);
m_phase = PHASE_MESSAGEIN;
}
void MessageInPhaseHandler() {
//LOGN("MsgIn");
SET_MSG_ACTIVE();
SET_CD_ACTIVE();
SET_IO_ACTIVE();
writeHandshake(m_msg);
m_phase = PHASE_BUSFREE;
}
void BusFreePhaseHandler() {
//LOGN("BusFree");
// Release control of the bus.
SCSI_TARGET_INACTIVE();
// Clear the reset pending flag.
m_isBusReset = false;
// Reset back to waiting for selection phase.
m_phase = PHASE_SELECTION;
}

View File

@ -1,5 +1,8 @@
#include "config.h"
#include "scsi_defs.h"
#if SUPPORT_OPTICAL
static const uint8_t SessionTOC[] =
{
0x00, // toc length, MSB
@ -142,140 +145,13 @@ static void LBA2MSF(uint32_t LBA, uint8_t* MSF)
MSF[1] = (uint32_t)(rem / 60); // M
}
void OpticalModeSense6CommandHandler() {
uint8_t len;
int page, pagemax, pagemin;
LOGN("[ModeSense6]");
/* Check whether medium is present */
if(!m_img) {
m_sts |= 0x02;
m_phase = PHASE_STATUSIN;
return;
}
memset(m_responsebuffer, 0, sizeof(m_responsebuffer));
len = 1;
/* Default medium type */
m_responsebuffer[len++] = 0xf0;
/* Write protected */
m_responsebuffer[len++] = 0x80;
/* Add block descriptor if DBD is not set */
if (m_cmd[1] & 0x08) {
m_responsebuffer[len++] = 0; /* No block descriptor */
} else {
uint32_t capacity = (m_img->m_fileSize / m_img->m_blocksize) - 1;
m_responsebuffer[len++] = 8; /* Block descriptor length */
m_responsebuffer[len++] = (capacity >> 24) & 0xff;
m_responsebuffer[len++] = (capacity >> 16) & 0xff;
m_responsebuffer[len++] = (capacity >> 8) & 0xff;
m_responsebuffer[len++] = capacity & 0xff;
m_responsebuffer[len++] = (m_img->m_blocksize >> 24) & 0xff;
m_responsebuffer[len++] = (m_img->m_blocksize >> 16) & 0xff;
m_responsebuffer[len++] = (m_img->m_blocksize >> 8) & 0xff;
m_responsebuffer[len++] = (m_img->m_blocksize) & 0xff;
}
/* Check for requested mode page */
page = m_cmd[2] & 0x3F;
pagemax = (page != 0x3f) ? page : 0x3e;
pagemin = (page != 0x3f) ? page : 0x00;
for(page = pagemax; page >= pagemin; page--) {
switch (page) {
case MODEPAGE_VENDOR_SPECIFIC:
/* Accept request only for current values */
if (m_cmd[2] & 0xC0) {
//DEBUGPRINT(DBG_TRACE, " [2]==%d", m_cmd[2]);
/* Prepare sense data */
m_sts |= STATUS_CHECK;
m_sense[m_id][m_lun].m_key = ILLEGAL_REQUEST;
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
return;
}
/* Unit attention */
m_responsebuffer[len++] = 0x80; // PS, page id
m_responsebuffer[len++] = 0x02; // Page length
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00;
break;
#if 0
case MODEPAGE_DCRC_PARAMETERS:
m_responsebuffer[len++] = 0x82; // PS, page id
m_responsebuffer[len++] = 0x0e; // Page length
m_responsebuffer[len++] = 0xe6; // Buffer full ratio, 90%
m_responsebuffer[len++] = 0x1a; // Buffer empty ratio, 10%
m_responsebuffer[len++] = 0x00; // Bus inactivity limit
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Disconnect time limit
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Connect time limit
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Maximum burst size
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // EMDP, Dimm, DTDC
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
break;
#endif
case MODEPAGE_RW_ERROR_RECOVERY:
m_responsebuffer[len++] = 0x81; // PS, page id
m_responsebuffer[len++] = 0x0a; // Page length
m_responsebuffer[len++] = 0x07; //
m_responsebuffer[len++] = 0x00; // Read Retry Count
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Write Retry Count
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Recovery Time Limit
m_responsebuffer[len++] = 0x00;
break;
default:
if(pagemin == pagemax) {
/* Requested mode page is not supported */
/* Prepare sense data */
m_sts |= STATUS_CHECK;
m_sense[m_id][m_lun].m_key = ILLEGAL_REQUEST;
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
return;
}
}
}
/* Report size of requested data */
m_responsebuffer[0] = len;
/* Truncate data if necessary */
if (m_cmd[4] < len) {
len = m_cmd[4];
}
// Send it
writeDataPhase(len, m_responsebuffer);
m_phase = PHASE_STATUSIN;
}
void OpticalReadSimpleTOC()
{
int MSF = m_cmd[1] & 0x02 ? 1 : 0;
uint16_t allocationLength = (m_cmd[7] << 8) | m_cmd[8];
uint8_t len = 0;
if(!m_img) {
if(!m_sel) {
m_sts |= 0x02;
m_phase = PHASE_STATUSIN;
return;
@ -283,7 +159,7 @@ void OpticalReadSimpleTOC()
memset(m_responsebuffer, 0, sizeof(m_responsebuffer));
uint32_t capacity = (m_img->m_fileSize / m_img->m_blocksize) - 1;
uint32_t capacity = (m_sel->m_fileSize / m_sel->m_blocksize) - 1;
m_responsebuffer[len++] = 0x00; // toc length, MSB
m_responsebuffer[len++] = 0x00; // toc length, LSB
m_responsebuffer[len++] = 0x01; // First track number
@ -324,11 +200,11 @@ void OpticalReadSimpleTOC()
break;
default:
m_sts |= STATUS_CHECK;
m_sense[m_id][m_lun].m_key = ILLEGAL_REQUEST; // Illegal Request
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 6
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x06;
m_sel->m_sense.m_key = ILLEGAL_REQUEST; // Illegal Request
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 6
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x06;
m_phase = PHASE_STATUSIN;
}
@ -371,11 +247,11 @@ void OpticalReadFullTOC(int convertBCD)
// We only support session 1.
if (m_cmd[6] > 1) {
m_sts |= STATUS_CHECK;
m_sense[m_id][m_lun].m_key = ILLEGAL_REQUEST; // Illegal Request
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 6
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x06;
m_sel->m_sense.m_key = ILLEGAL_REQUEST; // Illegal Request
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 6
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x06;
m_phase = PHASE_STATUSIN;
return;
}
@ -431,11 +307,11 @@ void OpticalReadTOCCommandHandler()
}
m_sts |= STATUS_CHECK;
m_sense[m_id][m_lun].m_key = ILLEGAL_REQUEST; // Illegal Request
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 2
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x02;
m_sel->m_sense.m_key = ILLEGAL_REQUEST; // Illegal Request
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 2
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
}
@ -473,7 +349,7 @@ void OpticalReadDiscInfoCommandHandler()
{
uint16_t allocationLength = (m_cmd[7] << 8) | m_cmd[8];
uint32_t len = sizeof(DiscInfoBlock);
uint32_t capacity = (m_img->m_fileSize / m_img->m_blocksize) - 1;
uint32_t capacity = (m_sel->m_fileSize / m_sel->m_blocksize) - 1;
memcpy(m_responsebuffer, DiscInfoBlock, len);
LOGN("[DiscInfo]");
@ -505,6 +381,8 @@ void OpticalGetConfigurationCommandHandler()
uint16_t sfi = 0;
uint8_t len;
LOGN("[GetConfiguration]");
memset(m_responsebuffer, 0, sizeof(m_responsebuffer));
switch(m_cmd[1] & 0x3) {
@ -558,11 +436,11 @@ void OpticalEventStatusCommandHandler()
if((m_cmd[1] & 1) == 0) {
m_sts |= STATUS_CHECK;
m_sense[m_id][m_lun].m_key = ILLEGAL_REQUEST; // Illegal Request
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 1
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
m_sense[m_id][m_lun].m_key_specific[2] = 0x01;
m_sel->m_sense.m_key = ILLEGAL_REQUEST; // Illegal Request
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; // Invalid field in CDB
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; // Error in Byte 1
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x01;
m_phase = PHASE_STATUSIN;
return;
}
@ -587,7 +465,7 @@ void OpticalEventStatusCommandHandler()
m_responsebuffer[len++] = 0x04; // Media Change Event
m_responsebuffer[len++] = 0x12; // Supported Event Class
m_responsebuffer[len++] = 0x00; // No Change
m_responsebuffer[len++] = (!m_img) ? 0x00 : 0x02; // Media Present
m_responsebuffer[len++] = (!m_sel) ? 0x00 : 0x02; // Media Present
m_responsebuffer[len++] = 0x00; // Start Slot
m_responsebuffer[len++] = 0x00; // End Slot
break;
@ -617,14 +495,14 @@ void OpticalLockTrayCommandHandler() {
void OpticalReadCapacityCommandHandler() {
LOGN("[ReadCapacity]");
if(!m_img) {
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
uint32_t bl = m_img->m_blocksize;
uint32_t bc = m_img->m_fileSize / bl;
uint32_t bl = m_sel->m_blocksize;
uint32_t bc = m_sel->m_fileSize / bl;
uint8_t buf[8] = {
(uint8_t)(((uint32_t)(bc >> 24))&0xff), (uint8_t)(((uint32_t)(bc >> 16))&0xff), (uint8_t)(((uint32_t)(bc >> 8))&0xff), (uint8_t)(((uint32_t)(bc))&0xff),
(uint8_t)(((uint32_t)(bl >> 24))&0xff), (uint8_t)(((uint32_t)(bl >> 16))&0xff), (uint8_t)(((uint32_t)(bl >> 8))&0xff), (uint8_t)(((uint32_t)(bl))&0xff)
@ -633,28 +511,75 @@ void OpticalReadCapacityCommandHandler() {
m_phase = PHASE_STATUSIN;
}
void ConfigureOpticalHandlers(int id) {
void ConfigureOpticalHandlers(VirtualDevice_t *vdev) {
for(int c = 0; c < 256; c++)
m_handler[id][c] = &UnknownCommandHandler;
vdev->m_handler[c] = &UnknownCommandHandler;
m_handler[id][CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
m_handler[id][CMD_REZERO_UNIT] = &RezeroUnitCommandHandler;
m_handler[id][CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
m_handler[id][CMD_READ6] = &Read6CommandHandler;
m_handler[id][CMD_SEEK6] = &Seek6CommandHandler;
m_handler[id][CMD_INQUIRY] = &InquiryCommandHandler;
m_handler[id][CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
m_handler[id][CMD_MODE_SENSE6] = &OpticalModeSense6CommandHandler;
m_handler[id][CMD_START_STOP_UNIT] = &StartStopUnitCommandHandler;
m_handler[id][CMD_PREVENT_REMOVAL] = &OpticalLockTrayCommandHandler;
m_handler[id][CMD_READ_CAPACITY10] = &OpticalReadCapacityCommandHandler;
m_handler[id][CMD_READ10] = &Read10CommandHandler;
m_handler[id][CMD_SEEK10] = &Seek10CommandHandler;
m_handler[id][CMD_READ_TOC] = &OpticalReadTOCCommandHandler;
m_handler[id][CMD_READ_HEADER] = &OpticalHeaderCommandHandler;
m_handler[id][CMD_GET_CONFIGURATION] = &OpticalGetConfigurationCommandHandler;
m_handler[id][CMD_GET_EVENT_STATUS_NOTIFICATION] = &OpticalEventStatusCommandHandler;
m_handler[id][CMD_READ_DISC_INFORMATION] = &OpticalReadDiscInfoCommandHandler;
m_handler[id][CMD_MODE_SELECT10] = &ModeSelect10CommandHandler;
m_handler[id][CMD_MODE_SENSE10] = &ModeSense10CommandHandler;
vdev->m_handler[CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
vdev->m_handler[CMD_REZERO_UNIT] = &RezeroUnitCommandHandler;
vdev->m_handler[CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
vdev->m_handler[CMD_READ6] = &Read6CommandHandler;
vdev->m_handler[CMD_SEEK6] = &Seek6CommandHandler;
vdev->m_handler[CMD_INQUIRY] = &InquiryCommandHandler;
vdev->m_handler[CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
vdev->m_handler[CMD_MODE_SENSE6] = &ModeSenseCommandHandler;
vdev->m_handler[CMD_START_STOP_UNIT] = &StartStopUnitCommandHandler;
vdev->m_handler[CMD_PREVENT_REMOVAL] = &OpticalLockTrayCommandHandler;
vdev->m_handler[CMD_READ_CAPACITY10] = &OpticalReadCapacityCommandHandler;
vdev->m_handler[CMD_READ10] = &Read10CommandHandler;
vdev->m_handler[CMD_SEEK10] = &Seek10CommandHandler;
vdev->m_handler[CMD_READ_TOC] = &OpticalReadTOCCommandHandler;
vdev->m_handler[CMD_READ_HEADER] = &OpticalHeaderCommandHandler;
vdev->m_handler[CMD_GET_CONFIGURATION] = &OpticalGetConfigurationCommandHandler;
vdev->m_handler[CMD_GET_EVENT_STATUS_NOTIFICATION] = &OpticalEventStatusCommandHandler;
vdev->m_handler[CMD_READ_DISC_INFORMATION] = &OpticalReadDiscInfoCommandHandler;
vdev->m_handler[CMD_MODE_SELECT10] = &ModeSelect10CommandHandler;
vdev->m_handler[CMD_MODE_SENSE10] = &ModeSenseCommandHandler;
}
// If config file exists, read the first three lines and copy the contents.
// File must be well formed or you will get junk in the SCSI Vendor fields.
void ConfigureOptical(VirtualDevice_t *vdev, const char *image_name) {
memcpy(vdev->m_inquiryresponse, SCSI_CDROM_INQUIRY_RESPONSE, sizeof(SCSI_CDROM_INQUIRY_RESPONSE));
if(image_name) {
char configname[MAX_FILE_PATH+1];
memcpy(configname, image_name, MAX_FILE_PATH+1);
char *psuffix = strstr(configname, ".img");
if(psuffix) {
strcpy(psuffix, ".cfg");
} else {
sprintf(configname, "cd%d%d.cfg", vdev->m_id, vdev->m_lun);
}
FsFile config_file = sd.open(configname, O_RDONLY);
if (config_file.isOpen()) {
char vendor[9];
memset(vendor, 0, sizeof(vendor));
config_file.readBytes(vendor, sizeof(vendor));
LOGN("SCSI VENDOR: ");
LOGN(vendor);
memcpy(&(vdev->m_inquiryresponse[8]), vendor, 8);
char product[17];
memset(product, 0, sizeof(product));
config_file.readBytes(product, sizeof(product));
LOGN("SCSI PRODUCT: ");
LOGN(product);
memcpy(&(vdev->m_inquiryresponse[16]), product, 16);
char version[5];
memset(version, 0, sizeof(version));
config_file.readBytes(version, sizeof(version));
LOGN("SCSI VERSION: ");
LOGN(version);
memcpy(&(vdev->m_inquiryresponse[32]), version, 4);
config_file.close();
}
}
vdev->m_type = DEV_OPTICAL;
ConfigureOpticalHandlers(vdev);
}
#endif

25
src/partitions.ino Executable file
View File

@ -0,0 +1,25 @@
#if 0
#include "config.h"
#include "cmd.h"
typedef struct PartType_s {
unsigned char Type;
const char *Name;
} PartType_t;
PartType_t PartType[] = {
{ 0x00, "Empty" },
{ 0x01, "FAT12" },
{ 0x04, "FAT16 <32MB" },
{ 0x05, "Extended" },
{ 0x06, "FAT16 >32MB" },
{ 0x07, "NTFS" },
{ 0x0b, "FAT32" },
{ 0x0c, "FAT32 LBA" },
{ 0x0e, "FAT16 LBA" },
{ 0x0f, "Extended LBA" },
{ 0xf8, "TinySCSI Disk LUN" },
{ 0xf9, "TinySCSI Optical LUN" },
{ 0xff, NULL }
};
#endif

60
src/sasi.ino Executable file
View File

@ -0,0 +1,60 @@
#include "config.h"
#include "scsi_defs.h"
#if SUPPORT_SASI
/*
* dtc510b_setDriveparameter
*/
#define PACKED __attribute__((packed))
typedef struct PACKED dtc500_cmd_c2_param_struct
{
uint8_t StepPlusWidth; // Default is 13.6usec (11)
uint8_t StepPeriod; // Default is 3 msec.(60)
uint8_t StepMode; // Default is Bufferd (0)
uint8_t MaximumHeadAdress; // Default is 4 heads (3)
uint8_t HighCylinderAddressuint8_t; // Default set to 0 (0)
uint8_t LowCylinderAddressuint8_t; // Default is 153 cylinders (152)
uint8_t ReduceWrietCurrent; // Default is above Cylinder 128 (127)
uint8_t DriveType_SeekCompleteOption;// (0)
uint8_t Reserved8; // (0)
uint8_t Reserved9; // (0)
} DTC510_CMD_C2_PARAM;
static void logStrHex(const char *msg,uint32_t num)
{
LOG(msg);
LOGHEX6N(num);
}
void DTCsetDriveParameterCommandHandler() {
LOGN("[DTC510B setDriveParameter]");
DTC510_CMD_C2_PARAM DriveParameter;
uint16_t maxCylinder;
uint16_t numLAD;
//uint32_t stepPulseUsec;
int StepPeriodMsec;
// receive paramter
writeDataPhase(sizeof(DriveParameter),(uint8_t *)(&DriveParameter));
maxCylinder =
(((uint16_t)DriveParameter.HighCylinderAddressuint8_t)<<8) |
(DriveParameter.LowCylinderAddressuint8_t);
numLAD = maxCylinder * (DriveParameter.MaximumHeadAdress+1);
//stepPulseUsec = calcStepPulseUsec(DriveParameter.StepPlusWidth);
StepPeriodMsec = DriveParameter.StepPeriod*50;
logStrHex (" StepPlusWidth : ",DriveParameter.StepPlusWidth);
logStrHex (" StepPeriod : ",DriveParameter.StepPeriod );
logStrHex (" StepMode : ",DriveParameter.StepMode );
logStrHex (" MaximumHeadAdress : ",DriveParameter.MaximumHeadAdress);
logStrHex (" CylinderAddress : ",maxCylinder);
logStrHex (" ReduceWrietCurrent : ",DriveParameter.ReduceWrietCurrent);
logStrHex (" DriveType/SeekCompleteOption : ",DriveParameter.DriveType_SeekCompleteOption);
logStrHex (" Maximum LAD : ",numLAD-1);
m_sts = 0;
m_phase = PHASE_STATUSIN;
}
#endif

View File

@ -1,3 +1,5 @@
#ifndef __SCSI_DEFS_H
#define __SCSI_DEFS_H
/* Mode pages */
#define MODEPAGE_VENDOR_SPECIFIC 0x00
@ -14,6 +16,8 @@
#define CMD_REQUEST_SENSE 0x03
#define CMD_FORMAT_UNIT 0x04
#define CMD_READ_BLOCK_LIMITS 0x05
#define CMD_FORMAT_UNIT_ALT 0x06
#define CMD_REASSIGN_BLOCKS 0x07
#define CMD_READ6 0x08
#define CMD_WRITE6 0x0A /* Optional */
#define CMD_SEEK6 0x0B /* Optional */
@ -39,6 +43,7 @@
#define CMD_WRITEANDVERIFY10 0x2E /* Optional */
#define CMD_VERIFY10 0x2F
#define CMD_PREFETCH_CACHE10 0x34 /* Optional */
#define CMD_READPOSITION10 0x34 /* Optional */
#define CMD_SYNCHRONIZE_CACHE10 0x35 /* Optional */
#define CMD_LOCKUNLOCK_CACHE10 0x36 /* Optional */
#define CMD_READ_DEFECT_DATA 0x37 /* Optional */
@ -57,6 +62,7 @@
#define CMD_MODE_SENSE10 0x5A /* Optional */
/* 12 Byte command opcodes */
#define CMD_REPORT_LUNS 0xA0 /* Optional */
#define CMD_SET_DRIVE_PARAMETER 0xC2
#define CMD_MAC_UNKNOWN 0xEE /* Unknown */
/* Dayna SCSI/Link Ethernet */
@ -81,11 +87,16 @@
#define CMD_CABLETRON_LOAD_IMAGE 0x0d09
#define CMD_CABLETRON_SET_ADDR 0x0d0a
/*
* SCSI DEVICE CODES
*/
#define DEV_DISK 0x00
#define DEV_TAPE 0x01
#define DEV_OPTICAL 0x05
/*
* SCSI MESSAGE CODES
*/
#define COMMAND_COMPLETE 0x00
#define EXTENDED_MESSAGE 0x01
#define EXTENDED_MODIFY_DATA_POINTER 0x00
@ -149,3 +160,9 @@
#define UNIT_POWERON_RESET 0x29
#define NO_MEDIA 0x3A
#define ERROR_IN_OPCODE 0xC0
#if USE_DB2ID_TABLE
extern const uint8_t db2scsiid[256];
#endif
#endif

293
src/scsibus.ino Executable file
View File

@ -0,0 +1,293 @@
#include "config.h"
#include "scsi_defs.h"
#define SCSI_DB_MASK 0x00ff0800
/*
* Data uint8_t to GPIOB register setting value and parity table
*/
// Parity bit generation
#define PTY(V) ((1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7)))&1)
// Set DBP
#define DBP(D) ((((uint32_t)(D)<<16)|(PTY(D)<<11)) ^ SCSI_DB_MASK)
#define DBP8(D) DBP(D),DBP(D+1),DBP(D+2),DBP(D+3),DBP(D+4),DBP(D+5),DBP(D+6),DBP(D+7)
#define DBP32(D) DBP8(D),DBP8(D+8),DBP8(D+16),DBP8(D+24)
// BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
const uint32_t db_bsrr[256]={
DBP32(0x00),DBP32(0x20),DBP32(0x40),DBP32(0x60),
DBP32(0x80),DBP32(0xA0),DBP32(0xC0),DBP32(0xE0)
};
// Parity bit acquisition
#define PARITY(DB) ((db_bsrr[DB & 0xff] & 0x0800) >> 11)
// Macro cleaning
#undef DBP32
#undef DBP8
//#undef DBP
//#undef PTY
#if USE_DB2ID_TABLE
/* DB to SCSI-ID translation table */
const uint8_t db2scsiid[256]={
0xff,
0,
1,1,
2,2,2,2,
3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
};
#endif
// Put DB and DP in output mode
inline void SCSI_DB_OUTPUT(uint8_t d) {
// GPIOB_PDDR = db_bsrr[d];
// GPIOB_PDOR = db_bsrr[d] ^ SCSI_DB_MASK;
GPIOB_PDOR = db_bsrr[d];
}
// Put DB and DP in input mode
inline void SCSI_DB_INPUT() {
GPIOB_PDDR = 0x00000000;
GPIOB_PDOR = SCSI_DB_MASK;
}
/*
* Bus reset interrupt.
*/
void onBusReset(void)
{
#if SUPPORT_SASI
// SASI I / F for X1 turbo has RST pulse write cycle +2 clock ==
// I can't filter because it only activates about 1.25us
if(!m_sasi_mode) {
#endif
if(digitalRead(RST)) return;
delayMicroseconds(20);
if(digitalRead(RST)) return;
#if SUPPORT_SASI
}
#endif
SCSI_DB_INPUT();
LOGN("BusReset!");
m_isBusReset = true;
}
/*
* Read by handshake.
*/
inline uint8_t readHandshake(void)
{
SET_REQ_ACTIVE();
//SCSI_DB_INPUT();
while(!m_isBusReset && !GET_ACK());
uint8_t r = readIO();
SET_REQ_INACTIVE();
while(GET_ACK()) { if(m_isBusReset) return 0; }
return r;
}
inline void readHandshakeBlock(uint8_t *d, uint16_t len)
{
//SCSI_DB_INPUT();
while(len) {
SET_REQ_ACTIVE();
while(!m_isBusReset && !GET_ACK());
*d++ = readIO();
len--;
SET_REQ_INACTIVE();
while(GET_ACK()) {
yield();
if(m_isBusReset) return;
}
}
}
/*
* Write with a handshake.
*/
inline void writeHandshake(uint8_t d)
{
GPIOB_PDDR = SCSI_DB_MASK;
SCSI_DB_OUTPUT(d);
// ACK.Fall to DB output delay 100ns(MAX) (DTC-510B)
SET_REQ_ACTIVE(); // (30ns)
while(!m_isBusReset && !GET_ACK());
// ACK.Fall to REQ.Raise delay 500ns(typ.) (DTC-510B)
GPIOB_PDDR = DBP(0xff);
GPIOB_PDOR = DBP(0xff) ^ SCSI_DB_MASK; // DB=0xFF
SET_REQ_INACTIVE();
// REQ.Raise to DB hold time 0ns
SCSI_DB_INPUT(); // (150ns)
while( GET_ACK()) { if(m_isBusReset) return; }
}
inline void writeHandshakeBlock(const uint8_t *d, uint16_t len)
{
GPIOB_PDDR = SCSI_DB_MASK;
while(len) {
SCSI_DB_OUTPUT(*d++);
SET_REQ_ACTIVE(); // (30ns)
while(!GET_ACK());
len--;
SET_REQ_INACTIVE();
while( GET_ACK()) {
if(m_isBusReset) return;
}
}
SCSI_DB_INPUT(); // (150ns)
}
/*
* Data in phase.
* Send len uint8_ts of data array p.
*/
void writeDataPhase(int len, const uint8_t* p)
{
//LOGN("DATAIN PHASE");
SET_MSG_INACTIVE(); // gpio_write(MSG, low);
SET_CD_INACTIVE();; // gpio_write(CD, low);
SET_IO_ACTIVE(); // gpio_write(IO, high);
#if READ_SPEED_OPTIMIZE
writeHandshakeBlock(p, len);
#else
for (int i = 0; i < len; i++) {
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
return;
}
writeHandshake(p[i]);
}
#endif
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
}
}
/*
* Data in phase.
* Send len block while reading from SD card.
*/
void writeDataPhaseSD(uint32_t adds, uint32_t len)
{
#if READ_SPEED_OPTIMIZE
uint32_t bigread = (MAX_BLOCKSIZE / m_sel->m_blocksize);
#endif
uint32_t i = 0;
//LOGN("DATAIN PHASE(SD)");
uint32_t pos = adds * m_sel->m_blocksize;
m_sel->m_file.seek(pos);
SET_MSG_INACTIVE(); // gpio_write(MSG, low);
SET_CD_INACTIVE(); // gpio_write(CD, low);
SET_IO_ACTIVE(); // gpio_write(IO, high);
while(i < len) {
// Asynchronous reads will make it faster ...
#if READ_SPEED_OPTIMIZE
if((len-i) >= bigread) {
m_sel->m_file.read(m_buf, MAX_BLOCKSIZE);
writeHandshakeBlock(m_buf, MAX_BLOCKSIZE);
i += bigread;
} else {
m_sel->m_file.read(m_buf, m_sel->m_blocksize * (len-i));
writeHandshakeBlock(m_buf, m_sel->m_blocksize * (len-i));
i = len;
}
#else
m_sel->m_file.read(m_buf, m_sel->m_blocksize);
for(unsigned int j = 0; j < m_sel->m_blocksize; j++) {
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
return;
}
writeHandshake(m_buf[j]);
}
#endif
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
return;
}
}
}
/*
* Data out phase.
* len block read
*/
void readDataPhase(int len, uint8_t* p)
{
//LOGN("DATAOUT PHASE");
SET_MSG_INACTIVE();
SET_CD_INACTIVE();;
SET_IO_INACTIVE();;
#if WRITE_SPEED_OPTIMIZE
readHandshakeBlock(p, len);
#else
for(int i = 0; i < len; i++)
p[i] = readHandshake();
#endif
if(m_isBusReset) {
m_phase = PHASE_BUSFREE;
}
}
/*
* Data out phase.
* Write to SD card while reading len block.
*/
void readDataPhaseSD(uint32_t adds, uint32_t len)
{
#if WRITE_SPEED_OPTIMIZE
uint32_t bigread = (MAX_BLOCKSIZE / m_sel->m_blocksize);
#endif
uint32_t i = 0;
//LOGN("DATAOUT PHASE(SD)");
uint32_t pos = adds * m_sel->m_blocksize;
m_sel->m_file.seek(pos);
SET_MSG_INACTIVE();
SET_CD_INACTIVE();;
SET_IO_INACTIVE();;
while(i < len) {
#if WRITE_SPEED_OPTIMIZE
if((len-i) >= bigread) {
readHandshakeBlock(m_buf, MAX_BLOCKSIZE);
m_sel->m_file.write(m_buf, MAX_BLOCKSIZE);
i += bigread;
} else {
readHandshakeBlock(m_buf, m_sel->m_blocksize * (len-i));
m_sel->m_file.write(m_buf, m_sel->m_blocksize * (len-i));
i = len;
}
#else
for(unsigned int j = 0; j < m_sel->m_blocksize; j++) {
if(m_isBusReset) {
return;
}
m_buf[j] = readHandshake();
}
m_sel->m_file.write(m_buf, m_sel->m_blocksize);
#endif
}
m_sel->m_file.flush();
}

316
src/tape.ino Executable file
View File

@ -0,0 +1,316 @@
#include "config.h"
#include "scsi_defs.h"
#if SUPPORT_TAPE
/*
* READ6 / 10 Command processing.
*/
void TapeReadCommandHandler()
{
uint32_t len;
LOG("[Read6]");
len = (m_cmd[4] << 16) | (m_cmd[3] << 8) | m_cmd[2];
switch(m_cmd[1] & 0x3) {
case 0:
if(len == 0) return;
break;
case 1: // Fixed
break;
case 2: // SILI
break;
case 3: // Illegal Request
m_sts = 0x02;
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 1" */
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x01;
m_phase = PHASE_STATUSIN;
return;
}
LOG("-R ");
LOGHEX4N(len);
if(!m_sel) return 0x02; // Image file absent
LED_ON();
writeDataPhaseTape(len);
LED_OFF();
m_phase = PHASE_STATUSIN;
}
/*
* WRITE6 / 10 Command processing.
*/
void TapeWriteCommandHandler()
{
uint32_t len;
LOG("[Write6]-W ");
LOGHEX4N(len);
if(!m_sel) return 0x02; // Image file absent
LED_ON();
m_sel->m_file.write(&len, 4);
readDataPhaseTape(len);
m_sel->m_file.write(&len, 4);
LED_OFF();
m_phase = PHASE_STATUSIN;
}
void TapeModeSense6CommandHandler() {
uint8_t len;
int page, pagemax, pagemin;
LOGN("[ModeSense6]");
/* Check whether medium is present */
if(!m_sel) {
m_sts |= 0x02;
m_phase = PHASE_STATUSIN;
return;
}
memset(m_responsebuffer, 0, sizeof(m_responsebuffer));
len = 1;
/* Default medium type */
m_responsebuffer[len++] = 0xf0;
/* Write protected */
m_responsebuffer[len++] = 0x80;
/* Add block descriptor if DBD is not set */
if (m_cmd[1] & 0x08) {
m_responsebuffer[len++] = 0; /* No block descriptor */
} else {
uint32_t capacity = (m_sel->m_fileSize / m_sel->m_blocksize) - 1;
m_responsebuffer[len++] = 8; /* Block descriptor length */
m_responsebuffer[len++] = (capacity >> 24) & 0xff;
m_responsebuffer[len++] = (capacity >> 16) & 0xff;
m_responsebuffer[len++] = (capacity >> 8) & 0xff;
m_responsebuffer[len++] = capacity & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize >> 24) & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize >> 16) & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize >> 8) & 0xff;
m_responsebuffer[len++] = (m_sel->m_blocksize) & 0xff;
}
/* Check for requested mode page */
page = m_cmd[2] & 0x3F;
pagemax = (page != 0x3f) ? page : 0x3e;
pagemin = (page != 0x3f) ? page : 0x00;
for(page = pagemax; page >= pagemin; page--) {
switch (page) {
default:
if(pagemin == pagemax) {
/* Requested mode page is not supported */
/* Prepare sense data */
m_sts |= STATUS_CHECK;
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
m_sel->m_sense.m_key_specific[1] = 0x00;
m_sel->m_sense.m_key_specific[2] = 0x02;
m_phase = PHASE_STATUSIN;
return;
}
}
}
/* Report size of requested data */
m_responsebuffer[0] = len;
/* Truncate data if necessary */
if (m_cmd[4] < len) {
len = m_cmd[4];
}
// Send it
writeDataPhase(len, m_responsebuffer);
m_phase = PHASE_STATUSIN;
}
void TapeLoadUnloadCommandHandler() {
if(m_cmd[4] & 1) {
LOGN("[Load]");
} else {
LOGN("[Unload]");
}
m_phase = PHASE_STATUSIN;
}
void TapePreventRemovalCommandHandler() {
if(m_cmd[4] & 1) {
LOGN("[Prevent Removal]");
} else {
LOGN("[Allow Removal]");
}
m_phase = PHASE_STATUSIN;
}
void TapeReadCapacityCommandHandler() {
LOGN("[ReadCapacity]");
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
uint32_t bl = m_sel->m_blocksize;
uint32_t bc = m_sel->m_fileSize / bl;
uint8_t buf[8] = {
(uint8_t)(((uint32_t)(bc >> 24))&0xff), (uint8_t)(((uint32_t)(bc >> 16))&0xff), (uint8_t)(((uint32_t)(bc >> 8))&0xff), (uint8_t)(((uint32_t)(bc))&0xff),
(uint8_t)(((uint32_t)(bl >> 24))&0xff), (uint8_t)(((uint32_t)(bl >> 16))&0xff), (uint8_t)(((uint32_t)(bl >> 8))&0xff), (uint8_t)(((uint32_t)(bl))&0xff)
};
writeDataPhase(8, buf);
m_phase = PHASE_STATUSIN;
}
void TapeEraseCommandHandler() {
LOGN("[Erase]");
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
// Truncate the current .tap file (maybe archive it by renaming instead of truncating?)
}
void TapeReadBlockLimitsCommandHandler() {
uint16_t len = 0;
LOGN("[ReadBlockLimits]");
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0xFF; // Maximum Block Length
m_responsebuffer[len++] = 0xFF;
m_responsebuffer[len++] = 0xFF;
m_responsebuffer[len++] = 0x00; // Minimum Block Length
m_responsebuffer[len++] = 0x00;
writeDataPhase(len, buf);
m_phase = PHASE_STATUSIN;
}
void TapeRewindUnitCommandHandler() {
LOGN("[Rewind]");
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
m_sel->m_filemarks = 0;
m_sel->m_file.seek(0);
m_phase = PHASE_STATUSIN;
}
void TapeReadPositionCommandHandler() {
uint16_t len = 0;
LOGN("[ReadPosition]");
if(!m_sel) {
m_sts |= 0x02; // Image file absent
m_phase = PHASE_STATUSIN;
return;
}
m_responsebuffer[len++] = (m_sel->m_filemarks == 0) ? (1 << 7) : 0;
m_responsebuffer[len++] = 0x00; // Partition(0)
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff); // First Block Location
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff00) >> 8;
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff0000) >> 16;
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff000000) >> 24;
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff); // Last Block Location
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff00) >> 8;
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff0000) >> 16;
m_responsebuffer[len++] = (m_sel->m_filemarks & 0xff000000) >> 24;
m_responsebuffer[len++] = 0x00; // Reserved
m_responsebuffer[len++] = 0x00; // Blocks in buffer (0)
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00; // Bytes in buffer (0)
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00;
m_responsebuffer[len++] = 0x00;
writeDataPhase(len, buf);
m_phase = PHASE_STATUSIN;
}
void ConfigureTapeHandlers(VirtualDevice_t *vdev) {
for(int c = 0; c < 256; c++)
vdev->m_handler[c] = &UnknownCommandHandler;
vdev->m_handler[CMD_ERASE] = &TapeEraseCommandHandler;
vdev->m_handler[CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
vdev->m_handler[CMD_REZERO_UNIT] = &TapeRewindUnitCommandHandler;
vdev->m_handler[CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
vdev->m_handler[CMD_READ_BLOCK_LIMITS] = &TapeReadBlockLimitsCommandHandler;
vdev->m_handler[CMD_READ6] = &TapeReadCommandHandler;
vdev->m_handler[CMD_WRITE6] = &TapeWriteCommandHandler;
vdev->m_handler[CMD_SEEK6] = &TapeSeekCommandHandler;
vdev->m_handler[CMD_INQUIRY] = &InquiryCommandHandler;
vdev->m_handler[CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
vdev->m_handler[CMD_MODE_SENSE6] = &TapeModeSense6CommandHandler;
vdev->m_handler[CMD_START_STOP_UNIT] = &TapeLoadUnloadCommandHandler;
vdev->m_handler[CMD_PREVENT_REMOVAL] = &TapePreventRemovalCommandHandler;
vdev->m_handler[CMD_READ_CAPACITY10] = &TapeReadCapacityCommandHandler;
vdev->m_handler[CMD_READ10] = &TapeReadCommandHandler;
vdev->m_handler[CMD_WRITE10] = &TapeWriteCommandHandler;
vdev->m_handler[CMD_SEEK10] = &TapeSeekCommandHandler;
vdev->m_handler[CMD_READPOSITION10] = &TapeReadPositionCommandHandler;
vdev->m_handler[CMD_MODE_SELECT10] = &ModeSelect10CommandHandler;
vdev->m_handler[CMD_MODE_SENSE10] = &TapeModeSense10CommandHandler;
}
// If config file exists, read the first three lines and copy the contents.
// File must be well formed or you will get junk in the SCSI Vendor fields.
void ConfigureTape(VirtualDevice_t *vdev, const char *image_name) {
memcpy(vdev->m_inquiryresponse, SCSI_TAPE_INQUIRY_RESPONSE, sizeof(SCSI_CDROM_INQUIRY_RESPONSE));
if(image_name) {
char configname[MAX_FILE_PATH+1];
memcpy(configname, image_name, MAX_FILE_PATH+1);
char *psuffix = strstr(configname, ".img");
if(psuffix) {
strcpy(psuffix, ".cfg");
} else {
sprintf(configname, "mt%d%d.cfg", vdev->m_id, vdev->m_lun);
}
FsFile config_file = sd.open(configname, O_RDONLY);
if (config_file.isOpen()) {
char vendor[9];
memset(vendor, 0, sizeof(vendor));
config_file.readBytes(vendor, sizeof(vendor));
LOGN("SCSI VENDOR: ");
LOGN(vendor);
memcpy(&(vdev->m_inquiryresponse[8]), vendor, 8);
char product[17];
memset(product, 0, sizeof(product));
config_file.readBytes(product, sizeof(product));
LOGN("SCSI PRODUCT: ");
LOGN(product);
memcpy(&(vdev->m_inquiryresponse[16]), product, 16);
char version[5];
memset(version, 0, sizeof(version));
config_file.readBytes(version, sizeof(version));
LOGN("SCSI VERSION: ");
LOGN(version);
memcpy(&(vdev->m_inquiryresponse[32]), version, 4);
config_file.close();
}
}
vdev->m_type = DEV_TAPE;
ConfigureTapeHandlers(vdev);
}
#endif