From f6da74a9ed8a6a90826f3d256f11b98f2511c167 Mon Sep 17 00:00:00 2001 From: akuker Date: Sun, 16 Aug 2020 12:08:37 -0500 Subject: [PATCH] Additional changes --- src/raspberrypi/command_thread.cpp | 281 ++++++----------------------- src/raspberrypi/rascsi.cpp | 3 +- src/raspberrypi/rascsi_mgr.cpp | 1 + src/raspberrypi/rasctl_command.cpp | 133 +++++++++++++- src/raspberrypi/rasctl_command.h | 2 + 5 files changed, 194 insertions(+), 226 deletions(-) diff --git a/src/raspberrypi/command_thread.cpp b/src/raspberrypi/command_thread.cpp index bfe2ec4..9d0a4e7 100644 --- a/src/raspberrypi/command_thread.cpp +++ b/src/raspberrypi/command_thread.cpp @@ -343,154 +343,6 @@ void *Command_Thread::MonThread(void *param) return NULL; } -BOOL ValidateCommand(rasctl_command *cmd){ - - - - - - - - - - - - - -// BOOL Command_Thread::ParseArgument(int argc, char* argv[]) -// { -// int id = -1; -// bool is_sasi = false; -// int max_id = 7; - -// int opt; -// while ((opt = getopt(argc, argv, "-IiHhD:d:")) != -1) { -// switch (opt) { -// case 'I': -// case 'i': -// is_sasi = false; -// max_id = 7; -// id = -1; -// continue; - -// case 'H': -// case 'h': -// is_sasi = true; -// max_id = 15; -// id = -1; -// continue; - -// case 'D': -// case 'd': { -// char* end; -// id = strtol(optarg, &end, 10); -// if ((id < 0) || (max_id < id)) { -// fprintf(stderr, "%s: invalid %s (0-%d)\n", -// optarg, is_sasi ? "HD" : "ID", max_id); -// return false; -// } -// break; -// } - -// default: -// return false; - -// case 1: -// break; -// } - -// if (id < 0) { -// fprintf(stderr, "%s: ID not specified\n", optarg); -// return false; -// } else if (Rascsi_Manager::GetInstance()->m_disk[id] && !Rascsi_Manager::GetInstance()->m_disk[id]->IsNULL()) { -// fprintf(stderr, "%d: duplicate ID\n", id); -// return false; -// } - -// char* path = optarg; -// int type = -1; -// if (HasSuffix(path, ".hdf") -// || HasSuffix(path, ".hds") -// || HasSuffix(path, ".hdn") -// || HasSuffix(path, ".hdi") -// || HasSuffix(path, ".hda") -// || HasSuffix(path, ".nhd")) { -// type = 0; -// } else if (HasSuffix(path, ".mos")) { -// type = 2; -// } else if (HasSuffix(path, ".iso")) { -// type = 3; -// } else if (xstrcasecmp(path, "bridge") == 0) { -// type = 4; -// } else { -// // Cannot determine the file type -// fprintf(stderr, -// "%s: unknown file extension\n", path); -// return false; -// } - -// int un = 0; -// if (is_sasi) { -// un = id % Rascsi_Manager::UnitNum; -// id /= Rascsi_Manager::UnitNum; -// } - -// // Execute the command -// if (!ExecuteCommand(stderr, id, un, 0, type, path)) { -// return false; -// } -// id = -1; -// } - -// // Display the device list -// Rascsi_Manager::GetInstance()->ListDevice(stdout); -// return true; -// } - - - - - - - // if (type == 0) { - // // Passed the check - // if (!file) { - // return FALSE; - // } - - // // Check that command is at least 5 characters long - // len = strlen(file); - // if (len < 5) { - // return FALSE; - // } - - // // Check the extension - // if (file[len - 4] != '.') { - // return FALSE; - // } - - // // If the extension is not SASI type, replace with SCSI - // ext = &file[len - 3]; - // if (xstrcasecmp(ext, "hdf") != 0) { - // type = 1; - // } - - // // Check the Controller Number - // if (cmd->id < 0 || id >= Rascsi_Manager::GetInstance()->CtrlMax) { - // FPRT(fp, "Error : Invalid ID\n"); - // return FALSE; - // } - - // // Check the Unit Number - // if (cmd->un < 0 || un >= Rascsi_Manager::GetInstance()->UnitNum) { - // FPRT(fp, "Error : Invalid unit number\n"); - // return FALSE; - // } - -return FALSE; - -} - //--------------------------------------------------------------------------- // @@ -534,28 +386,20 @@ BOOL Command_Thread::ExecuteCommand(FILE *fp, Rasctl_Command *incoming_command) return result; } - - BOOL Command_Thread::DoShutdown(FILE *fp, Rasctl_Command *incoming_command){ m_running = FALSE; + return TRUE; } BOOL Command_Thread::DoList(FILE *fp, Rasctl_Command *incoming_command){ Rascsi_Manager::GetInstance()->ListDevice(fp); return TRUE; } + BOOL Command_Thread::DoAttach(FILE *fp, Rasctl_Command *incoming_command){ Filepath filepath; Disk *pUnit = nullptr; - // Connect Command -// if (cmd == rasctl_cmd_attach) { // ATTACH -// // Distinguish between SASI and SCSI -// ext = NULL; -// pUnit = NULL; - -// } - // Create a new drive, based upon type switch (incoming_command->type) { case rasctl_dev_sasi_hd: // HDF @@ -622,78 +466,71 @@ BOOL Command_Thread::DoDetach(FILE *fp, Rasctl_Command *incoming_command){ BOOL Command_Thread::DoInsert(FILE *fp, Rasctl_Command *incoming_command){ - - // // Does the controller exist? - // if (Rascsi_Manager::GetInstance()->m_ctrl[id] == NULL) { - // FPRT(fp, "Error : No such device\n"); - // return FALSE; - // } - - // // Does the unit exist? - // pUnit = Rascsi_Manager::GetInstance()->m_disk[id * Rascsi_Manager::GetInstance()->UnitNum + un]; - // if (pUnit == NULL) { - // FPRT(fp, "Error : No such device\n"); - // return FALSE; - // } - - // // Valid only for MO or CD - // if (pUnit->GetID() != MAKEID('S', 'C', 'M', 'O') && - // pUnit->GetID() != MAKEID('S', 'C', 'C', 'D')) { - // FPRT(fp, "Error : Operation denied(Deveice isn't removable)\n"); - // return FALSE; - // } - - // case 2: // INSERT - // // Set the file path - // filepath.SetPath(file); - - // // Open the file - // if (!pUnit->Open(filepath)) { - // FPRT(fp, "Error : File open error [%s]\n", file); - // return FALSE; - // } - // break; + Filepath filepath; + Disk *pUnit = Rascsi_Manager::GetInstance()->GetDevice(fp, incoming_command->id, incoming_command->un); + // Does the device exist? + if (pUnit == nullptr) { + FPRT(fp, "Error : No such device\n"); + return FALSE; + } + + // Valid only for MO or CD + if (pUnit->GetID() != MAKEID('S', 'C', 'M', 'O') && + pUnit->GetID() != MAKEID('S', 'C', 'C', 'D')) { + FPRT(fp, "Error : Insert operation denied(Device isn't removable)\n"); + return FALSE; + } + + // Set the file path + filepath.SetPath(incoming_command->file); + + // Open the file + if (!pUnit->Open(filepath)) { + FPRT(fp, "Error : File open error [%s]\n", incoming_command->file); + return FALSE; + } - return FALSE; + return TRUE; } BOOL Command_Thread::DoEject(FILE *fp, Rasctl_Command *incoming_command){ - -// // Does the controller exist? -// if (Rascsi_Manager::GetInstance()->m_ctrl[id] == NULL) { -// FPRT(fp, "Error : No such device\n"); -// return FALSE; -// } + Disk *pUnit = Rascsi_Manager::GetInstance()->GetDevice(fp, incoming_command->id, incoming_command->un); -// // Does the unit exist? -// pUnit = Rascsi_Manager::GetInstance()->m_disk[id * Rascsi_Manager::GetInstance()->UnitNum + un]; -// if (pUnit == NULL) { -// FPRT(fp, "Error : No such device\n"); -// return FALSE; -// } + // Does the device exist? + if (pUnit == nullptr) { + FPRT(fp, "Error : No such device\n"); + return FALSE; + } + + // Valid only for MO or CD + if (pUnit->GetID() != MAKEID('S', 'C', 'M', 'O') && + pUnit->GetID() != MAKEID('S', 'C', 'C', 'D')) { + FPRT(fp, "Error : Operation denied(Deveice isn't removable)\n"); + return FALSE; + } + pUnit->Eject(TRUE); + return TRUE; +} - // // Valid only for MO or CD -// if (pUnit->GetID() != MAKEID('S', 'C', 'M', 'O') && -// pUnit->GetID() != MAKEID('S', 'C', 'C', 'D')) { -// FPRT(fp, "Error : Operation denied(Deveice isn't removable)\n"); -// return FALSE; -// } -// case 3: // EJECT -// pUnit->Eject(TRUE); -// break; - return FALSE;} BOOL Command_Thread::DoProtect(FILE *fp, Rasctl_Command *incoming_command){ - - // case 4: // PROTECT -// if (pUnit->GetID() != MAKEID('S', 'C', 'M', 'O')) { -// FPRT(fp, "Error : Operation denied(Deveice isn't MO)\n"); -// return FALSE; -// } -// pUnit->WriteP(!pUnit->IsWriteP()); -// break; - - return FALSE;} + + Disk *pUnit = Rascsi_Manager::GetInstance()->GetDevice(fp, incoming_command->id, incoming_command->un); + + // Does the device exist? + if (pUnit == nullptr) { + FPRT(fp, "Error : No such device\n"); + return FALSE; + } + + // Valid only for MO + if (pUnit->GetID() != MAKEID('S', 'C', 'M', 'O')) { + FPRT(fp, "Error : Write Protect denied(Device isn't a MO drive)\n"); + return FALSE; + } + pUnit->WriteP(!pUnit->IsWriteP()); + return TRUE; +} diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index 48438f0..876e0b3 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -220,8 +220,9 @@ int main(int argc, char* argv[]) while (Command_Thread::IsRunning()) { mgr->Step(); } - +#ifdef BAREMETAL err_exit: +#endif // Cleanup Cleanup(); diff --git a/src/raspberrypi/rascsi_mgr.cpp b/src/raspberrypi/rascsi_mgr.cpp index 30e844c..8d011ec 100644 --- a/src/raspberrypi/rascsi_mgr.cpp +++ b/src/raspberrypi/rascsi_mgr.cpp @@ -73,6 +73,7 @@ BOOL Rascsi_Manager::AttachDevicePrivate(FILE *fp, Disk *disk, int id, int unit_ // Add the disk to the controller m_ctrl[id]->SetUnit(unit_num, disk); + return TRUE; } BOOL Rascsi_Manager::DetachDevicePrivate(FILE *fp, int id, int unit_num) diff --git a/src/raspberrypi/rasctl_command.cpp b/src/raspberrypi/rasctl_command.cpp index 24e4532..bae5d4d 100644 --- a/src/raspberrypi/rasctl_command.cpp +++ b/src/raspberrypi/rasctl_command.cpp @@ -1,4 +1,5 @@ #include "rasctl_command.h" +#include "rascsi_mgr.h" const char* Rasctl_Command::m_delimiter = "\x1E"; // Record separator charater //const char* Rasctl_Command::m_delimiter = " "; // Record separator charater @@ -39,7 +40,7 @@ Rasctl_Command* Rasctl_Command::DeSerialize(BYTE* buff, int size){ return_command->un = atoi(cur_token); break; default: - for(int i=0; i= Rascsi_Manager::CtrlMax)){ + FPRT(fp, "Invalid ID. Must be between 0 and %d",Rascsi_Manager::CtrlMax-1); + return FALSE; + } + + // Check that unit number is in range + if((un < 0) || (un >= Rascsi_Manager::UnitNum)){ + FPRT(fp, "Invalid unit number. Must be between 0 and %d",Rascsi_Manager::UnitNum-1); + return FALSE; + } + + // If the command is attach, make sure we're adding a valid type + if(cmd == rasctl_cmd_attach){ + if(type == rasctl_dev_invalid){ + FPRT(fp, "Unknown device type being attached"); + return FALSE; + } + } + + // If we're trying to insert a disk, we need to make sure a file is specified. + // Also, if we're trying to attach a hard drive, the image must be specified + if((cmd == rasctl_cmd_insert) || ((cmd == rasctl_cmd_attach) && (rasctl_dev_is_hd(type)))){ + if(file == nullptr){ + FPRT(fp, "File name must be specified for this operation"); + return FALSE; + } + // Check that the file exists if specified + if(stat(file, &stat_buffer) == 0){ + FPRT(fp, "File %s is not accessible", file); + return FALSE; + } + // Check that the file is 1 byte or larger + if(stat_buffer.st_size < 1){ + FPRT(fp, "File must not be empty. %s is %ld bytes in size",file,stat_buffer.st_size); + return FALSE; + } + } + +// char* path = optarg; +// int type = -1; +// if (HasSuffix(path, ".hdf") +// || HasSuffix(path, ".hds") +// || HasSuffix(path, ".hdn") +// || HasSuffix(path, ".hdi") +// || HasSuffix(path, ".hda") +// || HasSuffix(path, ".nhd")) { +// type = 0; +// } else if (HasSuffix(path, ".mos")) { +// type = 2; +// } else if (HasSuffix(path, ".iso")) { +// type = 3; +// } else if (xstrcasecmp(path, "bridge") == 0) { +// type = 4; +// } else { +// // Cannot determine the file type +// fprintf(stderr, +// "%s: unknown file extension\n", path); +// return false; +// } + +// int un = 0; +// if (is_sasi) { +// un = id % Rascsi_Manager::UnitNum; +// id /= Rascsi_Manager::UnitNum; +// } + +// // Execute the command +// if (!ExecuteCommand(stderr, id, un, 0, type, path)) { +// return false; +// } +// id = -1; +// } + +// // Display the device list +// Rascsi_Manager::GetInstance()->ListDevice(stdout); +// return true; +// } + + + + + + + // if (type == 0) { + // // Passed the check + // if (!file) { + // return FALSE; + // } + + // // Check that command is at least 5 characters long + // len = strlen(file); + // if (len < 5) { + // return FALSE; + // } + + // // Check the extension + // if (file[len - 4] != '.') { + // return FALSE; + // } + + // // If the extension is not SASI type, replace with SCSI + // ext = &file[len - 3]; + // if (xstrcasecmp(ext, "hdf") != 0) { + // type = 1; + // } + +return FALSE; + +} diff --git a/src/raspberrypi/rasctl_command.h b/src/raspberrypi/rasctl_command.h index 9ece6d7..12f75db 100644 --- a/src/raspberrypi/rasctl_command.h +++ b/src/raspberrypi/rasctl_command.h @@ -42,6 +42,8 @@ class Rasctl_Command{ public: void Serialize(BYTE *buff, int max_buff_size); static Rasctl_Command* DeSerialize(BYTE* buff, int size); + BOOL IsValid(FILE *fp); + rasctl_command cmd = rasctl_cmd_invalid; rasctl_dev_type type = rasctl_dev_invalid;