Initial commit for scsiapi and Seagate FireCuda Gaming External HDD controller

This commit is contained in:
Adam Honse 2023-07-30 06:12:21 +00:00
parent f6e0996250
commit 9cf453008d
10 changed files with 1170 additions and 0 deletions

83
scsiapi/scsiapi.h Normal file
View file

@ -0,0 +1,83 @@
/*---------------------------------------------------------*\
| scsiapi.h |
| |
| Cross-platform SCSI access library |
| |
| Adam Honse <calcprogrammer1@gmail.com> 7/28/2023 |
\*---------------------------------------------------------*/
#pragma once
/*---------------------------------------------------------*\
| Includes |
\*---------------------------------------------------------*/
#include <string.h>
/*---------------------------------------------------------*\
| Platform-specific Includes |
\*---------------------------------------------------------*/
#ifdef WIN32
#include <windows.h>
#include <fileapi.h>
#include <ntddscsi.h>
#include "WinIoCtl.h"
#endif
#ifdef __linux__
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <scsi/sg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#ifdef __APPLE__
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------*\
| SCSI Device type |
\*---------------------------------------------------------*/
struct scsi_device_info
{
char * path; /* path string of SCSI device */
char * vendor; /* vendor string of SCSI device */
char * product; /* product string of SCSI device */
struct scsi_device_info * next; /* pointer to next scsi_device_info */
};
struct scsi_device
{
/*-----------------------------------------------------*\
| SCSI device handle/file descriptor |
\*-----------------------------------------------------*/
#ifdef WIN32
HANDLE fd;
#else
int fd;
#endif
};
/*---------------------------------------------------------*\
| Functions |
\*---------------------------------------------------------*/
void scsi_close(struct scsi_device * dev);
struct scsi_device_info * scsi_enumerate(const char * vendor, const char * product);
void scsi_free_enumeration(struct scsi_device_info * devs);
struct scsi_device * scsi_open_path(const char *path);
int scsi_write(struct scsi_device * dev, const unsigned char * data, size_t data_length, const unsigned char * cdb, size_t cdb_length, unsigned char * sense, size_t sense_length);
#ifdef __cplusplus
}
#endif

225
scsiapi/scsiapi_linux.c Normal file
View file

@ -0,0 +1,225 @@
/*---------------------------------------------------------*\
| scsiapi_linux.c |
| |
| Cross-platform SCSI access library |
| Linux implementation |
| |
| Adam Honse <calcprogrammer1@gmail.com> 7/28/2023 |
\*---------------------------------------------------------*/
#pragma once
/*---------------------------------------------------------*\
| Includes |
\*---------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "scsiapi.h"
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------*\
| Functions |
\*---------------------------------------------------------*/
void scsi_close(struct scsi_device * dev)
{
close(dev->fd);
}
struct scsi_device_info * scsi_enumerate(const char * vendor, const char * product)
{
struct scsi_device_info * ret_ptr = NULL;
struct scsi_device_info * last_ptr = NULL;
/*-----------------------------------------------------*\
| Search for /dev/sgX nodes matching vendor and product |
\*-----------------------------------------------------*/
unsigned int sg_idx = 0;
while(1)
{
/*-------------------------------------------------*\
| Create the scsi_generic class model path |
\*-------------------------------------------------*/
char sg_dev_buf[1024];
char sg_model_buf[512];
char sg_vendor_buf[512];
char sg_path_buf[512];
/*-------------------------------------------------*\
| Open the input event path to get the model and |
| vendor strings |
\*-------------------------------------------------*/
snprintf(sg_dev_buf, 1024, "/sys/class/scsi_generic/sg%d/device/model", sg_idx);
int sg_model_fd = open(sg_dev_buf, O_RDONLY | O_NONBLOCK);
snprintf(sg_dev_buf, 1024, "/sys/class/scsi_generic/sg%d/device/vendor", sg_idx);
int sg_vendor_fd = open(sg_dev_buf, O_RDONLY | O_NONBLOCK);
snprintf(sg_path_buf, 512, "/dev/sg%d", sg_idx);
/*-------------------------------------------------*\
| Fail if either path failed to open |
\*-------------------------------------------------*/
if(sg_model_fd < 0 || sg_vendor_fd < 0)
{
close(sg_model_fd);
close(sg_vendor_fd);
break;
}
memset(sg_model_buf, 0, 512);
memset(sg_vendor_buf, 0, 512);
/*-------------------------------------------------*\
| Read the model string and close the model file |
\*-------------------------------------------------*/
read(sg_model_fd, sg_model_buf, 512);
close(sg_model_fd);
for(int i = 0; i < strlen(sg_model_buf); i++)
{
if(sg_model_buf[i] == '\r' || sg_model_buf[i] == '\n')
{
sg_model_buf[i] = '\0';
break;
}
}
/*-------------------------------------------------*\
| Read the vendor string and close the vendor file |
\*-------------------------------------------------*/
read(sg_vendor_fd, sg_vendor_buf, 512);
close(sg_vendor_fd);
for(int i = 0; i < strlen(sg_vendor_buf); i++)
{
if(sg_vendor_buf[i] == '\r' || sg_vendor_buf[i] == '\n')
{
sg_vendor_buf[i] = '\0';
break;
}
}
/*-------------------------------------------------*\
| Check if this SCSI device should be added to the |
| list |
\*-------------------------------------------------*/
int add_to_list = 0;
if(vendor == NULL || product == NULL)
{
add_to_list = 1;
}
else if(strncmp(sg_model_buf, product, strlen(product)) == 0)
{
if(strncmp(sg_vendor_buf, vendor, strlen(vendor)) == 0)
{
add_to_list = 1;
}
}
/*-------------------------------------------------*\
| Create new scsi_device_info if adding to list |
\*-------------------------------------------------*/
if(add_to_list == 1)
{
struct scsi_device_info * info = malloc(sizeof(struct scsi_device_info));
info->path = malloc(strlen(sg_path_buf) + 1);
strcpy(info->path, sg_path_buf);
info->vendor = malloc(strlen(sg_vendor_buf) + 1);
strcpy(info->vendor, sg_vendor_buf);
info->product = malloc(strlen(sg_model_buf) + 1);
strcpy(info->product, sg_model_buf);
info->next = NULL;
if(ret_ptr == NULL)
{
ret_ptr = info;
}
else
{
last_ptr->next = info;
}
last_ptr = info;
}
sg_idx++;
}
return(ret_ptr);
}
void scsi_free_enumeration(struct scsi_device_info * devs)
{
struct scsi_device_info * dev = devs;
while(dev)
{
struct scsi_device_info * next = dev->next;
free(dev->path);
free(dev->vendor);
free(dev->product);
free(dev);
dev = next;
}
}
struct scsi_device * scsi_open_path(const char *path)
{
int device_fd = open(path, O_RDWR);
struct scsi_device * device = NULL;
if(device_fd > 0)
{
device = malloc(sizeof(struct scsi_device));
device->fd = device_fd;
}
return(device);
}
int scsi_write(struct scsi_device * dev, const unsigned char * data, size_t data_length, const unsigned char * cdb, size_t cdb_length, unsigned char * sense, size_t sense_length)
{
/*-----------------------------------------------------*\
| Create buffer to hold header |
\*-----------------------------------------------------*/
struct sg_io_hdr header;
/*-----------------------------------------------------*\
| Set up pass through command |
\*-----------------------------------------------------*/
header.interface_id = 'S';
header.dxfer_direction = SG_DXFER_TO_DEV;
header.cmd_len = cdb_length;
header.mx_sb_len = sense_length;
header.iovec_count = 0;
header.dxfer_len = data_length;
header.dxferp = data;
header.cmdp = cdb;
header.sbp = sense;
header.timeout = 20000;
header.flags = 0;
/*-----------------------------------------------------*\
| Send pass through command |
\*-----------------------------------------------------*/
ioctl(dev->fd, SG_IO, &header);
}
#ifdef __cplusplus
}
#endif

68
scsiapi/scsiapi_macos.c Normal file
View file

@ -0,0 +1,68 @@
/*---------------------------------------------------------*\
| scsiapi_macos.c |
| |
| Cross-platform SCSI access library |
| MacOS implementation (NON-FUNCTIONAL) |
| |
| Adam Honse <calcprogrammer1@gmail.com> 7/28/2023 |
\*---------------------------------------------------------*/
#pragma once
/*---------------------------------------------------------*\
| Includes |
\*---------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "scsiapi.h"
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------*\
| Functions |
\*---------------------------------------------------------*/
void scsi_close(struct scsi_device * dev)
{
}
struct scsi_device_info * scsi_enumerate(const char * vendor, const char * product)
{
return(NULL);
}
void scsi_free_enumeration(struct scsi_device_info * devs)
{
struct scsi_device_info * dev = devs;
while(dev)
{
struct scsi_device_info * next = dev->next;
free(dev->path);
free(dev->vendor);
free(dev->product);
free(dev);
dev = next;
}
}
struct scsi_device * scsi_open_path(const char *path)
{
return(NULL);
}
int scsi_write(struct scsi_device * dev, const unsigned char * data, size_t data_length, const unsigned char * cdb, size_t cdb_length, unsigned char * sense, size_t sense_length)
{
}
#ifdef __cplusplus
}
#endif

241
scsiapi/scsiapi_windows.c Normal file
View file

@ -0,0 +1,241 @@
/*---------------------------------------------------------*\
| scsiapi.c |
| |
| Cross-platform SCSI access library |
| Windows implementation |
| |
| Adam Honse <calcprogrammer1@gmail.com> 7/28/2023 |
\*---------------------------------------------------------*/
#pragma once
/*---------------------------------------------------------*\
| Includes |
\*---------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "scsiapi.h"
#define DEVBUFSIZE (128 * 1024)
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------*\
| Functions |
\*---------------------------------------------------------*/
void scsi_close(struct scsi_device * dev)
{
}
struct scsi_device_info * scsi_enumerate(const char * vendor, const char * product)
{
struct scsi_device_info * ret_ptr = NULL;
struct scsi_device_info * last_ptr = NULL;
char buff[DEVBUFSIZE] = "";
int char_count;
/*-----------------------------------------------------*\
| Query all devices and look for SCSI devices |
\*-----------------------------------------------------*/
char_count = QueryDosDevice(NULL, buff, DEVBUFSIZE);
if(char_count == 0)
{
return 0;
}
for(int i = 0; i < char_count; i++)
{
char * buf_ptr = buff + i;
if(strstr(buf_ptr, "SCSI"))
{
/*---------------------------------------------*\
| Extract vendor and product from SCSI path |
| Format: SCSI#Disk&Ven_VENDOR&Prod_PRODUCT#... |
\*---------------------------------------------*/
char c_vendor[512];
char c_product[512];
char c_path[MAX_PATH];
sscanf(buf_ptr, "SCSI#Disk&Ven_%[^&]&Prod_%[^#]#", c_vendor, c_product);
strcpy(c_path, "\\\\?\\");
strncat(c_path, buf_ptr, MAX_PATH - 4);
/*---------------------------------------------*\
| Windows converts spaces to underscores so |
| undo that |
| There may be a better way to do this... |
\*---------------------------------------------*/
for(int pos = 0; pos < strlen(c_vendor); pos++)
{
if(c_vendor[pos] == '_')
{
c_vendor[pos] = ' ';
}
}
for(int pos = 0; pos < strlen(c_product); pos++)
{
if(c_product[pos] == '_')
{
c_product[pos] = ' ';
}
}
/*---------------------------------------------*\
| Check if this SCSI device should be added to |
| the list |
\*---------------------------------------------*/
int add_to_list = 0;
if(vendor == NULL || product == NULL)
{
add_to_list = 1;
}
else if(strncmp(c_product, product, strlen(product)) == 0)
{
if(strncmp(c_vendor, vendor, strlen(vendor)) == 0)
{
add_to_list = 1;
}
}
/*---------------------------------------------*\
| Create new scsi_device_info if adding to list |
\*---------------------------------------------*/
if(add_to_list == 1)
{
struct scsi_device_info * info = malloc(sizeof(struct scsi_device_info));
info->path = malloc(strlen(c_path) + 1);
strcpy(info->path, c_path);
info->vendor = malloc(strlen(c_vendor) + 1);
strcpy(info->vendor, c_vendor);
info->product = malloc(strlen(c_product) + 1);
strcpy(info->product, c_product);
info->next = NULL;
if(ret_ptr == NULL)
{
ret_ptr = info;
}
else
{
last_ptr->next = info;
}
last_ptr = info;
}
}
i += strlen(buff + i);
}
return(ret_ptr);
}
void scsi_free_enumeration(struct scsi_device_info * devs)
{
struct scsi_device_info * dev = devs;
while(dev)
{
struct scsi_device_info * next = dev->next;
free(dev->path);
free(dev->vendor);
free(dev->product);
free(dev);
dev = next;
}
}
struct scsi_device * scsi_open_path(const char *path)
{
HANDLE device_fd = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)0x0, OPEN_EXISTING, 0x0, (HANDLE)0x0);
struct scsi_device * device = NULL;
if(device_fd != INVALID_HANDLE_VALUE)
{
device = malloc(sizeof(struct scsi_device));
device->fd = device_fd;
}
return(device);
}
int scsi_write(struct scsi_device * dev, const unsigned char * data, size_t data_length, const unsigned char * cdb, size_t cdb_length, unsigned char * sense, size_t sense_length)
{
/*-----------------------------------------------------*\
| Create buffer to hold SCSI_PASS_THROUGH_DIRECT |
| Size must be enough for the SCSI_PASS_THROUGH_DIRECT |
| struct plus the sense data. |
\*-----------------------------------------------------*/
int buffer_length = (sizeof(SCSI_PASS_THROUGH_DIRECT) + sense_length);
unsigned char * buffer = malloc(buffer_length);
/*-----------------------------------------------------*\
| Zero out the buffer |
\*-----------------------------------------------------*/
memset(buffer, 0, buffer_length);
/*-----------------------------------------------------*\
| Create PSCSI_PASS_THROUGH_DIRECT pointer and point it |
| to the buffer |
\*-----------------------------------------------------*/
PSCSI_PASS_THROUGH_DIRECT command = (PSCSI_PASS_THROUGH_DIRECT)buffer;
/*-----------------------------------------------------*\
| Set up pass through command |
\*-----------------------------------------------------*/
command->Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
command->ScsiStatus = 0x00;
command->PathId = 0x00;
command->TargetId = 0x00;
command->Lun = 0x00;
command->CdbLength = cdb_length;
command->SenseInfoLength = sense_length;
command->DataIn = SCSI_IOCTL_DATA_OUT;
command->DataTransferLength = data_length;
command->TimeOutValue = 0x00000014;
command->DataBuffer = data;
command->SenseInfoOffset = sizeof(SCSI_PASS_THROUGH_DIRECT);
/*-----------------------------------------------------*\
| Copy CDB and sense data into buffer |
\*-----------------------------------------------------*/
memcpy(command->Cdb, cdb, cdb_length);
memcpy(&buffer[sizeof(SCSI_PASS_THROUGH_DIRECT)], sense, sense_length);
/*-----------------------------------------------------*\
| Send pass through command |
\*-----------------------------------------------------*/
DeviceIoControl(dev->fd, IOCTL_SCSI_PASS_THROUGH_DIRECT, command, buffer_length, command, buffer_length, NULL, NULL);
/*-----------------------------------------------------*\
| Copy sense data out of buffer |
\*-----------------------------------------------------*/
memcpy(sense, &buffer[sizeof(SCSI_PASS_THROUGH_DIRECT)], sense_length);
/*-----------------------------------------------------*\
| Free the buffer |
\*-----------------------------------------------------*/
free(buffer);
}
#ifdef __cplusplus
}
#endif