General refactor.

This commit is contained in:
2019-10-19 18:30:45 +01:00
parent 4e9a16f404
commit 78d23fce19
20 changed files with 905 additions and 912 deletions

View File

@@ -17,11 +17,9 @@
#include "linux.h"
#include <scsi/sg.h>
#include <stddef.h>
#include <string.h>
int32_t ata_protocol_to_scsi_direction(uint8_t protocol)
int32_t AtaProtocolToScsiDirection(uint8_t protocol)
{
switch(protocol)
{
@@ -39,17 +37,17 @@ int32_t ata_protocol_to_scsi_direction(uint8_t protocol)
}
}
int32_t linux_send_ata_chs_command(int device_fd,
AtaRegistersChs registers,
AtaErrorRegistersChs* errorRegisters,
uint8_t protocol,
uint8_t transferRegister,
char* buffer,
uint32_t timeout,
uint8_t transferBlocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len)
int32_t LinuxSendAtaChsCommand(int device_fd,
AtaRegistersChs registers,
AtaErrorRegistersChs* error_registers,
uint8_t protocol,
uint8_t transfer_register,
char* buffer,
uint32_t timeout,
uint8_t transfer_blocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len)
{
duration = 0;
sense = 0;
@@ -61,7 +59,7 @@ int32_t linux_send_ata_chs_command(int device_fd,
cdb[0] = 0x85;
cdb[1] = (protocol << 1) & 0x1E;
if(transferRegister != DICMOTE_ATA_TRANSFER_REGISTER_NONE && protocol != DICMOTE_ATA_PROTOCOL_NO_DATA)
if(transfer_register != DICMOTE_ATA_TRANSFER_REGISTER_NONE && protocol != DICMOTE_ATA_PROTOCOL_NO_DATA)
{
switch(protocol)
{
@@ -70,58 +68,58 @@ int32_t linux_send_ata_chs_command(int device_fd,
default: cdb[2] = 0x00; break;
}
if(transferBlocks) cdb[2] |= 0x04;
if(transfer_blocks) cdb[2] |= 0x04;
cdb[2] |= (transferRegister & 0x03);
cdb[2] |= (transfer_register & 0x03);
}
cdb[4] = registers.Feature;
cdb[6] = registers.SectorCount;
cdb[8] = registers.Sector;
cdb[10] = registers.CylinderLow;
cdb[12] = registers.CylinderHigh;
cdb[13] = registers.DeviceHead;
cdb[14] = registers.Command;
cdb[4] = registers.feature;
cdb[6] = registers.sector_count;
cdb[8] = registers.sector;
cdb[10] = registers.cylinder_low;
cdb[12] = registers.cylinder_high;
cdb[13] = registers.device_head;
cdb[14] = registers.command;
int error = linux_send_scsi_command(device_fd,
(char*)cdb,
buffer,
&sense_buf,
timeout,
ata_protocol_to_scsi_direction(protocol),
duration,
sense,
16,
buf_len,
&sense_len);
int error = LinuxSendScsiCommand(device_fd,
(char*)cdb,
buffer,
&sense_buf,
timeout,
AtaProtocolToScsiDirection(protocol),
duration,
sense,
16,
buf_len,
&sense_len);
if(sense_len < 22 || (sense_buf[8] != 0x09 && sense_buf[9] != 0x0C)) return error;
errorRegisters->Error = sense_buf[11];
error_registers->error = sense_buf[11];
errorRegisters->SectorCount = sense_buf[13];
errorRegisters->Sector = sense_buf[15];
errorRegisters->CylinderLow = sense_buf[17];
errorRegisters->CylinderHigh = sense_buf[19];
errorRegisters->DeviceHead = sense_buf[20];
errorRegisters->Status = sense_buf[21];
error_registers->sector_count = sense_buf[13];
error_registers->sector = sense_buf[15];
error_registers->cylinder_low = sense_buf[17];
error_registers->cylinder_high = sense_buf[19];
error_registers->device_head = sense_buf[20];
error_registers->status = sense_buf[21];
*sense = errorRegisters->Error != 0 || (errorRegisters->Status & 0xA5) != 0;
*sense = error_registers->error != 0 || (error_registers->status & 0xA5) != 0;
return error;
}
int32_t linux_send_ata_lba28_command(int device_fd,
AtaRegistersLba28 registers,
AtaErrorRegistersLba28* errorRegisters,
uint8_t protocol,
uint8_t transferRegister,
char* buffer,
uint32_t timeout,
uint8_t transferBlocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len)
int32_t LinuxSendAtaLba28Command(int device_fd,
AtaRegistersLba28 registers,
AtaErrorRegistersLba28* error_registers,
uint8_t protocol,
uint8_t transfer_register,
char* buffer,
uint32_t timeout,
uint8_t transfer_blocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len)
{
duration = 0;
sense = 0;
@@ -133,7 +131,7 @@ int32_t linux_send_ata_lba28_command(int device_fd,
cdb[0] = 0x85;
cdb[1] = (protocol << 1) & 0x1E;
if(transferRegister != DICMOTE_ATA_TRANSFER_REGISTER_NONE && protocol != DICMOTE_ATA_PROTOCOL_NO_DATA)
if(transfer_register != DICMOTE_ATA_TRANSFER_REGISTER_NONE && protocol != DICMOTE_ATA_PROTOCOL_NO_DATA)
{
switch(protocol)
{
@@ -142,59 +140,60 @@ int32_t linux_send_ata_lba28_command(int device_fd,
default: cdb[2] = 0x00; break;
}
if(transferBlocks) cdb[2] |= 0x04;
if(transfer_blocks) cdb[2] |= 0x04;
cdb[2] |= (transferRegister & 0x03);
cdb[2] |= (transfer_register & 0x03);
}
cdb[2] |= 0x20;
cdb[4] = registers.Feature;
cdb[6] = registers.SectorCount;
cdb[8] = registers.LbaLow;
cdb[10] = registers.LbaMid;
cdb[12] = registers.LbaHigh;
cdb[13] = registers.DeviceHead;
cdb[14] = registers.Command;
cdb[4] = registers.feature;
cdb[6] = registers.sector_count;
cdb[8] = registers.lba_low;
cdb[10] = registers.lba_mid;
cdb[12] = registers.lba_high;
cdb[13] = registers.device_head;
cdb[14] = registers.command;
int error = linux_send_scsi_command(device_fd,
(char*)cdb,
buffer,
&sense_buf,
timeout,
ata_protocol_to_scsi_direction(protocol),
duration,
sense,
16,
buf_len,
&sense_len);
int error = LinuxSendScsiCommand(device_fd,
(char*)cdb,
buffer,
&sense_buf,
timeout,
AtaProtocolToScsiDirection(protocol),
duration,
sense,
16,
buf_len,
&sense_len);
if(sense_len < 22 || (sense_buf[8] != 0x09 && sense_buf[9] != 0x0C)) return error;
errorRegisters->Error = sense_buf[11];
error_registers->error = sense_buf[11];
errorRegisters->SectorCount = sense_buf[13];
errorRegisters->LbaLow = sense_buf[15];
errorRegisters->LbaMid = sense_buf[17];
errorRegisters->LbaHigh = sense_buf[19];
errorRegisters->DeviceHead = sense_buf[20];
errorRegisters->Status = sense_buf[21];
error_registers->sector_count = sense_buf[13];
error_registers->lba_low = sense_buf[15];
error_registers->lba_mid = sense_buf[17];
error_registers->lba_high = sense_buf[19];
error_registers->device_head = sense_buf[20];
error_registers->status = sense_buf[21];
*sense = errorRegisters->Error != 0 || (errorRegisters->Status & 0xA5) != 0;
*sense = error_registers->error != 0 || (error_registers->status & 0xA5) != 0;
return error;
}
int32_t linux_send_ata_lba48_command(int device_fd,
AtaRegistersLba48 registers,
AtaErrorRegistersLba48* errorRegisters,
uint8_t protocol,
uint8_t transferRegister,
char* buffer,
uint32_t timeout,
uint8_t transferBlocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len)
int32_t LinuxSendAtaLba48Command(int device_fd,
AtaRegistersLba48 registers,
AtaErrorRegistersLba48* error_registers,
uint8_t protocol,
uint8_t transfer_register,
char* buffer,
uint32_t timeout,
uint8_t transfer_blocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len)
{
duration = 0;
sense = 0;
@@ -207,7 +206,7 @@ int32_t linux_send_ata_lba48_command(int device_fd,
cdb[0] = 0x85;
cdb[1] = (protocol << 1) & 0x1E;
cdb[1] |= 0x01;
if(transferRegister != DICMOTE_ATA_TRANSFER_REGISTER_NONE && protocol != DICMOTE_ATA_PROTOCOL_NO_DATA)
if(transfer_register != DICMOTE_ATA_TRANSFER_REGISTER_NONE && protocol != DICMOTE_ATA_PROTOCOL_NO_DATA)
{
switch(protocol)
{
@@ -216,50 +215,50 @@ int32_t linux_send_ata_lba48_command(int device_fd,
default: cdb[2] = 0x00; break;
}
if(transferBlocks) cdb[2] |= 0x04;
if(transfer_blocks) cdb[2] |= 0x04;
cdb[2] |= (transferRegister & 0x03);
cdb[2] |= (transfer_register & 0x03);
}
cdb[2] |= 0x20;
cdb[3] = ((registers.Feature & 0xFF00) >> 8);
cdb[4] = (registers.Feature & 0xFF);
cdb[5] = ((registers.SectorCount & 0xFF00) >> 8);
cdb[6] = (registers.SectorCount & 0xFF);
cdb[7] = ((registers.LbaLow & 0xFF00) >> 8);
cdb[8] = (registers.LbaLow & 0xFF);
cdb[9] = ((registers.LbaMid & 0xFF00) >> 8);
cdb[10] = (registers.LbaMid & 0xFF);
cdb[11] = ((registers.LbaHigh & 0xFF00) >> 8);
cdb[12] = (registers.LbaHigh & 0xFF);
cdb[13] = registers.DeviceHead;
cdb[14] = registers.Command;
cdb[3] = ((registers.feature & 0xFF00) >> 8);
cdb[4] = (registers.feature & 0xFF);
cdb[5] = ((registers.sector_count & 0xFF00) >> 8);
cdb[6] = (registers.sector_count & 0xFF);
cdb[7] = ((registers.lba_low & 0xFF00) >> 8);
cdb[8] = (registers.lba_low & 0xFF);
cdb[9] = ((registers.lba_mid & 0xFF00) >> 8);
cdb[10] = (registers.lba_mid & 0xFF);
cdb[11] = ((registers.lba_high & 0xFF00) >> 8);
cdb[12] = (registers.lba_high & 0xFF);
cdb[13] = registers.device_head;
cdb[14] = registers.command;
int error = linux_send_scsi_command(device_fd,
(char*)cdb,
buffer,
&sense_buf,
timeout,
ata_protocol_to_scsi_direction(protocol),
duration,
sense,
16,
buf_len,
&sense_len);
int error = LinuxSendScsiCommand(device_fd,
(char*)cdb,
buffer,
&sense_buf,
timeout,
AtaProtocolToScsiDirection(protocol),
duration,
sense,
16,
buf_len,
&sense_len);
if(sense_len < 22 || (sense_buf[8] != 0x09 && sense_buf[9] != 0x0C)) return error;
errorRegisters->Error = sense_buf[11];
error_registers->error = sense_buf[11];
errorRegisters->SectorCount = (uint16_t)((sense_buf[12] << 8) + sense_buf[13]);
errorRegisters->LbaLow = (uint16_t)((sense_buf[14] << 8) + sense_buf[15]);
errorRegisters->LbaMid = (uint16_t)((sense_buf[16] << 8) + sense_buf[17]);
errorRegisters->LbaHigh = (uint16_t)((sense_buf[18] << 8) + sense_buf[19]);
errorRegisters->DeviceHead = sense_buf[20];
errorRegisters->Status = sense_buf[21];
error_registers->sector_count = (uint16_t)((sense_buf[12] << 8) + sense_buf[13]);
error_registers->lba_low = (uint16_t)((sense_buf[14] << 8) + sense_buf[15]);
error_registers->lba_mid = (uint16_t)((sense_buf[16] << 8) + sense_buf[17]);
error_registers->lba_high = (uint16_t)((sense_buf[18] << 8) + sense_buf[19]);
error_registers->device_head = sense_buf[20];
error_registers->status = sense_buf[21];
*sense = errorRegisters->Error != 0 || (errorRegisters->Status & 0xA5) != 0;
*sense = error_registers->error != 0 || (error_registers->status & 0xA5) != 0;
return error;
}

View File

@@ -25,7 +25,7 @@
#include <string.h>
#include <unistd.h>
int linux_open_device(const char* device_path)
int LinuxOpenDevice(const char* device_path)
{
int fd;
@@ -36,19 +36,19 @@ int linux_open_device(const char* device_path)
return fd;
}
int32_t linux_get_device_type(const char* devicePath)
int32_t LinuxGetDeviceType(const char* device_path)
{
struct udev* udev;
struct udev_device* udev_device;
const char* tmpString;
const char* tmp_string;
char* chrptr;
int32_t deviceType = DICMOTE_DEVICE_TYPE_UNKNOWN;
int32_t device_type = DICMOTE_DEVICE_TYPE_UNKNOWN;
udev = udev_new();
if(!udev) return DICMOTE_DEVICE_TYPE_UNKNOWN;
chrptr = strrchr(devicePath, '/');
chrptr = strrchr(device_path, '/');
if(chrptr == 0) return DICMOTE_DEVICE_TYPE_UNKNOWN;
chrptr++;
@@ -57,83 +57,83 @@ int32_t linux_get_device_type(const char* devicePath)
udev_device = udev_device_new_from_subsystem_sysname(udev, "block", chrptr);
if(udev_device)
{
tmpString = udev_device_get_property_value(udev_device, "ID_BUS");
if(tmpString)
tmp_string = udev_device_get_property_value(udev_device, "ID_BUS");
if(tmp_string)
{
if(strncmp(tmpString, "ata", 3) == 0)
if(strncmp(tmp_string, "ata", 3) == 0)
{
deviceType = DICMOTE_DEVICE_TYPE_ATA;
device_type = DICMOTE_DEVICE_TYPE_ATA;
free((void*)tmpString);
tmpString = udev_device_get_property_value(udev_device, "ID_TYPE");
free((void*)tmp_string);
tmp_string = udev_device_get_property_value(udev_device, "ID_TYPE");
if(tmpString)
if(tmp_string)
{
// TODO: ATAPI removable non optical disks
if(strncmp(tmpString, "cd", 2) == 0) { deviceType = DICMOTE_DEVICE_TYPE_ATAPI; }
if(strncmp(tmp_string, "cd", 2) == 0) { device_type = DICMOTE_DEVICE_TYPE_ATAPI; }
free((void*)tmpString);
free((void*)tmp_string);
}
}
else if(strncmp(tmpString, "mmc", 3) == 0)
else if(strncmp(tmp_string, "mmc", 3) == 0)
{
free((void*)tmpString);
tmpString = malloc(1024);
free((void*)tmp_string);
tmp_string = malloc(1024);
deviceType = DICMOTE_DEVICE_TYPE_MMC;
device_type = DICMOTE_DEVICE_TYPE_MMC;
if(tmpString)
if(tmp_string)
{
memset((void*)tmpString, 0, 1024);
snprintf((char*)tmpString, 1024, "/sys/block/%s/device/scr", chrptr);
memset((void*)tmp_string, 0, 1024);
snprintf((char*)tmp_string, 1024, "/sys/block/%s/device/scr", chrptr);
if(access(tmpString, R_OK) == 0) deviceType = DICMOTE_DEVICE_TYPE_SECURE_DIGITAL;
if(access(tmp_string, R_OK) == 0) device_type = DICMOTE_DEVICE_TYPE_SECURE_DIGITAL;
free((void*)tmpString);
free((void*)tmp_string);
}
}
else if(strncmp(tmpString, "scsi", 4) == 0 || strncmp(tmpString, "ieee1394", 8) == 0 ||
strncmp(tmpString, "usb", 3) == 0)
else if(strncmp(tmp_string, "scsi", 4) == 0 || strncmp(tmp_string, "ieee1394", 8) == 0 ||
strncmp(tmp_string, "usb", 3) == 0)
{
free((void*)tmpString);
tmpString = udev_device_get_property_value(udev_device, "ID_TYPE");
free((void*)tmp_string);
tmp_string = udev_device_get_property_value(udev_device, "ID_TYPE");
if(tmpString)
if(tmp_string)
{
if(strncmp(tmpString, "cd", 2) == 0 || strncmp(tmpString, "disk", 4) == 0)
{ deviceType = DICMOTE_DEVICE_TYPE_SCSI; }
if(strncmp(tmp_string, "cd", 2) == 0 || strncmp(tmp_string, "disk", 4) == 0)
{ device_type = DICMOTE_DEVICE_TYPE_SCSI; }
free((void*)tmpString);
free((void*)tmp_string);
}
}
else if(strncmp(tmpString, "nvme", 4) == 0)
else if(strncmp(tmp_string, "nvme", 4) == 0)
{
free((void*)tmpString);
deviceType = DICMOTE_DEVICE_TYPE_NVME;
free((void*)tmp_string);
device_type = DICMOTE_DEVICE_TYPE_NVME;
}
}
}
udev_unref(udev);
return deviceType;
return device_type;
}
int32_t linux_get_sdhci_registers(const char* devicePath,
char** csd,
char** cid,
char** ocr,
char** scr,
uint32_t* csd_len,
uint32_t* cid_len,
uint32_t* ocr_len,
uint32_t* scr_len)
int32_t LinuxGetSdhciRegisters(const char* device_path,
char** csd,
char** cid,
char** ocr,
char** scr,
uint32_t* csd_len,
uint32_t* cid_len,
uint32_t* ocr_len,
uint32_t* scr_len)
{
char* tmpString;
char* sysfsPath_csd;
char* sysfsPath_cid;
char* sysfsPath_scr;
char* sysfsPath_ocr;
char* tmp_string;
char* sysfs_path_csd;
char* sysfs_path_cid;
char* sysfs_path_scr;
char* sysfs_path_ocr;
size_t len;
FILE* file;
*csd = NULL;
@@ -146,47 +146,47 @@ int32_t linux_get_sdhci_registers(const char* devicePath,
*scr_len = 0;
size_t n = 1026;
if(strncmp(devicePath, "/dev/mmcblk", 11) != 0) return 0;
if(strncmp(device_path, "/dev/mmcblk", 11) != 0) return 0;
len = strlen(devicePath) + 19;
sysfsPath_csd = malloc(len);
sysfsPath_cid = malloc(len);
sysfsPath_scr = malloc(len);
sysfsPath_ocr = malloc(len);
tmpString = malloc(1024);
len = strlen(device_path) + 19;
sysfs_path_csd = malloc(len);
sysfs_path_cid = malloc(len);
sysfs_path_scr = malloc(len);
sysfs_path_ocr = malloc(len);
tmp_string = malloc(1024);
if(!sysfsPath_csd || !sysfsPath_cid || !sysfsPath_scr || !sysfsPath_ocr || !tmpString)
if(!sysfs_path_csd || !sysfs_path_cid || !sysfs_path_scr || !sysfs_path_ocr || !tmp_string)
{
free(sysfsPath_csd);
free(sysfsPath_cid);
free(sysfsPath_scr);
free(sysfsPath_ocr);
free(tmpString);
free(sysfs_path_csd);
free(sysfs_path_cid);
free(sysfs_path_scr);
free(sysfs_path_ocr);
free(tmp_string);
return 0;
}
memset(sysfsPath_csd, 0, len);
memset(sysfsPath_cid, 0, len);
memset(sysfsPath_scr, 0, len);
memset(sysfsPath_ocr, 0, len);
memset(tmpString, 0, strlen(devicePath) - 5);
memset(sysfs_path_csd, 0, len);
memset(sysfs_path_cid, 0, len);
memset(sysfs_path_scr, 0, len);
memset(sysfs_path_ocr, 0, len);
memset(tmp_string, 0, strlen(device_path) - 5);
memcpy(tmpString, devicePath + 5, strlen(devicePath) - 5);
snprintf(sysfsPath_csd, len, "/sys/block/%s/device/csd", tmpString);
snprintf(sysfsPath_cid, len, "/sys/block/%s/device/cid", tmpString);
snprintf(sysfsPath_scr, len, "/sys/block/%s/device/scr", tmpString);
snprintf(sysfsPath_ocr, len, "/sys/block/%s/device/ocr", tmpString);
memcpy(tmp_string, device_path + 5, strlen(device_path) - 5);
snprintf(sysfs_path_csd, len, "/sys/block/%s/device/csd", tmp_string);
snprintf(sysfs_path_cid, len, "/sys/block/%s/device/cid", tmp_string);
snprintf(sysfs_path_scr, len, "/sys/block/%s/device/scr", tmp_string);
snprintf(sysfs_path_ocr, len, "/sys/block/%s/device/ocr", tmp_string);
if(access(sysfsPath_csd, R_OK) == 0)
if(access(sysfs_path_csd, R_OK) == 0)
{
file = fopen(sysfsPath_csd, "r");
file = fopen(sysfs_path_csd, "r");
if(file != NULL)
{
len = getline(&tmpString, &n, file);
len = getline(&tmp_string, &n, file);
if(len > 0)
{
*csd_len = hexs2bin(tmpString, (unsigned char**)csd);
*csd_len = Hexs2Bin(tmp_string, (unsigned char**)csd);
if(*csd_len <= 0)
{
@@ -199,16 +199,16 @@ int32_t linux_get_sdhci_registers(const char* devicePath,
}
}
if(access(sysfsPath_cid, R_OK) == 0)
if(access(sysfs_path_cid, R_OK) == 0)
{
file = fopen(sysfsPath_cid, "r");
file = fopen(sysfs_path_cid, "r");
if(file != NULL)
{
len = getline(&tmpString, &n, file);
len = getline(&tmp_string, &n, file);
if(len > 0)
{
*cid_len = hexs2bin(tmpString, (unsigned char**)cid);
*cid_len = Hexs2Bin(tmp_string, (unsigned char**)cid);
if(*cid_len <= 0)
{
@@ -221,16 +221,16 @@ int32_t linux_get_sdhci_registers(const char* devicePath,
}
}
if(access(sysfsPath_scr, R_OK) == 0)
if(access(sysfs_path_scr, R_OK) == 0)
{
file = fopen(sysfsPath_scr, "r");
file = fopen(sysfs_path_scr, "r");
if(file != NULL)
{
len = getline(&tmpString, &n, file);
len = getline(&tmp_string, &n, file);
if(len > 0)
{
*scr_len = hexs2bin(tmpString, (unsigned char**)scr);
*scr_len = Hexs2Bin(tmp_string, (unsigned char**)scr);
if(*scr_len <= 0)
{
@@ -243,16 +243,16 @@ int32_t linux_get_sdhci_registers(const char* devicePath,
}
}
if(access(sysfsPath_ocr, R_OK) == 0)
if(access(sysfs_path_ocr, R_OK) == 0)
{
file = fopen(sysfsPath_ocr, "r");
file = fopen(sysfs_path_ocr, "r");
if(file != NULL)
{
len = getline(&tmpString, &n, file);
len = getline(&tmp_string, &n, file);
if(len > 0)
{
*ocr_len = hexs2bin(tmpString, (unsigned char**)ocr);
*ocr_len = Hexs2Bin(tmp_string, (unsigned char**)ocr);
if(*ocr_len <= 0)
{
@@ -265,11 +265,11 @@ int32_t linux_get_sdhci_registers(const char* devicePath,
}
}
free(sysfsPath_csd);
free(sysfsPath_cid);
free(sysfsPath_scr);
free(sysfsPath_ocr);
free(tmpString);
free(sysfs_path_csd);
free(sysfs_path_cid);
free(sysfs_path_scr);
free(sysfs_path_ocr);
free(tmp_string);
return csd_len != 0 || cid_len != 0 || scr_len != 0 || ocr_len != 0;
}

View File

@@ -15,111 +15,109 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "linux.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
uint8_t linux_get_ieee1394_data(const char* devicePath,
uint32_t* idModel,
uint32_t* idVendor,
uint64_t* guid,
char* vendor,
char* model)
uint8_t LinuxGetIeee1394Data(const char* device_path,
uint32_t* id_model,
uint32_t* id_vendor,
uint64_t* guid,
char* vendor,
char* model)
{
char* devPath;
char tmpPath[4096];
char resolvedLink[4096];
char* dev_path;
char tmp_path[4096];
char resolved_link[4096];
struct stat sb;
ssize_t len;
char* rchr;
int found;
FILE* file;
*idModel = 0;
*idVendor = 0;
*guid = 0;
memset(tmpPath, 0, 4096);
memset(resolvedLink, 0, 4096);
*id_model = 0;
*id_vendor = 0;
*guid = 0;
memset(tmp_path, 0, 4096);
memset(resolved_link, 0, 4096);
if(strncmp(devicePath, "/dev/sd", 7) != 0 && strncmp(devicePath, "/dev/sr", 7) != 0 &&
strncmp(devicePath, "/dev/st", 7) != 0)
if(strncmp(device_path, "/dev/sd", 7) != 0 && strncmp(device_path, "/dev/sr", 7) != 0 &&
strncmp(device_path, "/dev/st", 7) != 0)
return 0;
devPath = (char*)devicePath + 5;
dev_path = (char*)device_path + 5;
snprintf(tmpPath, 4096, "/sys/block/%s", devPath);
snprintf(tmp_path, 4096, "/sys/block/%s", dev_path);
if(stat(tmpPath, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
if(stat(tmp_path, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
len = readlink(tmpPath, resolvedLink, 4096);
len = readlink(tmp_path, resolved_link, 4096);
if(len == 0) return 0;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "/sys%s", resolvedLink + 2);
memcpy(resolvedLink, tmpPath, 4096);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "/sys%s", resolved_link + 2);
memcpy(resolved_link, tmp_path, 4096);
while(strstr(resolvedLink, "firewire") != NULL)
while(strstr(resolved_link, "firewire") != NULL)
{
found = 1;
rchr = strrchr(resolvedLink, '/');
rchr = strrchr(resolved_link, '/');
if(rchr == NULL) break;
*rchr = '\0';
if(strlen(resolvedLink) == 0) break;
if(strlen(resolved_link) == 0) break;
snprintf(tmpPath, 4096, "%s/model", resolvedLink);
if(access(tmpPath, R_OK) != 0) found = 0;
memset(tmpPath, 0, 4096);
snprintf(tmp_path, 4096, "%s/model", resolved_link);
if(access(tmp_path, R_OK) != 0) found = 0;
memset(tmp_path, 0, 4096);
snprintf(tmpPath, 4096, "%s/vendor", resolvedLink);
if(access(tmpPath, R_OK) != 0) found = 0;
memset(tmpPath, 0, 4096);
snprintf(tmp_path, 4096, "%s/vendor", resolved_link);
if(access(tmp_path, R_OK) != 0) found = 0;
memset(tmp_path, 0, 4096);
snprintf(tmpPath, 4096, "%s/guid", resolvedLink);
if(access(tmpPath, R_OK) != 0) found = 0;
memset(tmpPath, 0, 4096);
snprintf(tmp_path, 4096, "%s/guid", resolved_link);
if(access(tmp_path, R_OK) != 0) found = 0;
memset(tmp_path, 0, 4096);
if(!found) continue;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/model", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/model", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fscanf(file, "%8x", idModel);
fscanf(file, "%8x", id_model);
fclose(file);
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/vendor", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/vendor", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fscanf(file, "%8x", idVendor);
fscanf(file, "%8x", id_vendor);
fclose(file);
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/guid", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/guid", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fscanf(file, "%16lx", guid);
@@ -127,12 +125,12 @@ uint8_t linux_get_ieee1394_data(const char* devicePath,
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/model_name", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/model_name", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fread(model, 256, 1, file);
@@ -140,12 +138,12 @@ uint8_t linux_get_ieee1394_data(const char* devicePath,
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/vendor_name", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/vendor_name", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fread(vendor, 256, 1, file);

View File

@@ -15,94 +15,107 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DICREMOTE_LINUX_H
#define DICREMOTE_LINUX_H
#ifndef DICREMOTE_LINUX_LINUX_H_
#define DICREMOTE_LINUX_LINUX_H_
#include "../dicmote.h"
#define PATH_SYS_DEVBLOCK "/sys/block"
DeviceInfoList* linux_list_devices();
int linux_open_device(const char* device_path);
int32_t linux_get_device_type(const char* devicePath);
int32_t linux_send_scsi_command(int device_fd,
char* cdb,
char* buffer,
char** senseBuffer,
uint32_t timeout,
int32_t direction,
uint32_t* duration,
uint32_t* sense,
uint32_t cdb_len,
uint32_t* buf_len,
uint32_t* sense_len);
int32_t linux_get_sdhci_registers(const char* devicePath,
char** csd,
char** cid,
char** ocr,
char** scr,
uint32_t* csd_len,
uint32_t* cid_len,
uint32_t* ocr_len,
uint32_t* scr_len);
uint8_t linux_get_usb_data(const char* devicePath,
uint16_t* descLen,
char* descriptors,
uint16_t* idVendor,
uint16_t* idProduct,
char* manufacturer,
char* product,
char* serial);
uint8_t linux_get_ieee1394_data(const char* devicePath,
uint32_t* idModel,
uint32_t* idVendor,
uint64_t* guid,
char* vendor,
char* model);
uint8_t linux_get_pcmcia_data(const char* devicePath, uint16_t* cisLen, char* cis);
int32_t linux_send_ata_chs_command(int device_fd,
AtaRegistersChs registers,
AtaErrorRegistersChs* errorRegisters,
uint8_t protocol,
uint8_t transferRegister,
char* buffer,
uint32_t timeout,
uint8_t transferBlocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len);
int32_t linux_send_ata_lba28_command(int device_fd,
AtaRegistersLba28 registers,
AtaErrorRegistersLba28* errorRegisters,
uint8_t protocol,
uint8_t transferRegister,
char* buffer,
uint32_t timeout,
uint8_t transferBlocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len);
int32_t linux_send_ata_lba48_command(int device_fd,
AtaRegistersLba48 registers,
AtaErrorRegistersLba48* errorRegisters,
uint8_t protocol,
uint8_t transferRegister,
char* buffer,
uint32_t timeout,
uint8_t transferBlocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len);
int32_t linux_send_sdhci_command(int device_fd,
uint8_t command,
uint8_t write,
uint8_t application,
uint32_t flags,
uint32_t argument,
uint32_t block_size,
uint32_t blocks,
char* buffer,
uint32_t timeout,
uint32_t* response,
uint32_t* duration,
uint32_t* sense);
#endif // DICREMOTE_LINUX_H
DeviceInfoList* LinuxListDevices();
int LinuxOpenDevice(const char* device_path);
int32_t LinuxGetDeviceType(const char* device_path);
int32_t LinuxSendScsiCommand(int device_fd,
char* cdb,
char* buffer,
char** sense_buffer,
uint32_t timeout,
int32_t direction,
uint32_t* duration,
uint32_t* sense,
uint32_t cdb_len,
uint32_t* buf_len,
uint32_t* sense_len);
int32_t LinuxGetSdhciRegisters(const char* device_path,
char** csd,
char** cid,
char** ocr,
char** scr,
uint32_t* csd_len,
uint32_t* cid_len,
uint32_t* ocr_len,
uint32_t* scr_len);
uint8_t LinuxGetUsbData(const char* device_path,
uint16_t* desc_len,
char* descriptors,
uint16_t* id_vendor,
uint16_t* id_product,
char* manufacturer,
char* product,
char* serial);
uint8_t LinuxGetIeee1394Data(const char* device_path,
uint32_t* id_model,
uint32_t* id_vendor,
uint64_t* guid,
char* vendor,
char* model);
uint8_t LinuxGetPcmciaData(const char* device_path, uint16_t* cis_len, char* cis);
int32_t LinuxSendAtaChsCommand(int device_fd,
AtaRegistersChs registers,
AtaErrorRegistersChs* error_registers,
uint8_t protocol,
uint8_t transfer_register,
char* buffer,
uint32_t timeout,
uint8_t transfer_blocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len);
int32_t LinuxSendAtaLba28Command(int device_fd,
AtaRegistersLba28 registers,
AtaErrorRegistersLba28* error_registers,
uint8_t protocol,
uint8_t transfer_register,
char* buffer,
uint32_t timeout,
uint8_t transfer_blocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len);
int32_t LinuxSendAtaLba48Command(int device_fd,
AtaRegistersLba48 registers,
AtaErrorRegistersLba48* error_registers,
uint8_t protocol,
uint8_t transfer_register,
char* buffer,
uint32_t timeout,
uint8_t transfer_blocks,
uint32_t* duration,
uint32_t* sense,
uint32_t* buf_len);
int32_t LinuxSendSdhciCommand(int device_fd,
uint8_t command,
uint8_t write,
uint8_t application,
uint32_t flags,
uint32_t argument,
uint32_t block_size,
uint32_t blocks,
char* buffer,
uint32_t timeout,
uint32_t* response,
uint32_t* duration,
uint32_t* sense);
#endif // DICREMOTE_LINUX_LINUX_H_

View File

@@ -28,15 +28,15 @@
#include <string.h>
#include <unistd.h>
DeviceInfoList* linux_list_devices()
DeviceInfoList* LinuxListDevices()
{
DIR* dir;
struct dirent* dirent;
struct udev* udev;
int hasUdev = false, i;
DeviceInfoList * listStart = NULL, *listCurrent = NULL, *listNext = NULL;
int has_udev = false, i;
DeviceInfoList * list_start = NULL, *list_current = NULL, *list_next = NULL;
struct udev_device* udev_device;
const char* tmpString;
const char* tmp_string;
FILE* file;
char* line_str;
size_t n, ret;
@@ -44,7 +44,7 @@ DeviceInfoList* linux_list_devices()
udev = udev_new();
hasUdev = udev != 0;
has_udev = udev != 0;
dir = opendir(PATH_SYS_DEVBLOCK);
if(!dir) return NULL;
@@ -59,82 +59,82 @@ DeviceInfoList* linux_list_devices()
continue;
}
listNext = malloc(sizeof(DeviceInfoList));
memset(listNext, 0, sizeof(DeviceInfoList));
list_next = malloc(sizeof(DeviceInfoList));
memset(list_next, 0, sizeof(DeviceInfoList));
if(!listNext)
if(!list_next)
{
closedir(dir);
if(hasUdev) udev_unref(udev);
if(has_udev) udev_unref(udev);
return listStart;
return list_start;
}
if(!listStart) listStart = listNext;
if(!list_start) list_start = list_next;
if(listCurrent) listCurrent->next = listNext;
if(list_current) list_current->next = list_next;
snprintf(listNext->this.path, 1024, "/dev/%s", dirent->d_name);
snprintf(list_next->this.path, 1024, "/dev/%s", dirent->d_name);
if(hasUdev)
if(has_udev)
{
udev_device = udev_device_new_from_subsystem_sysname(udev, "block", dirent->d_name);
if(udev_device)
{
tmpString = udev_device_get_property_value(udev_device, "ID_VENDOR");
if(tmpString)
tmp_string = udev_device_get_property_value(udev_device, "ID_VENDOR");
if(tmp_string)
{
strncpy(listNext->this.vendor, tmpString, 256);
free((void*)tmpString);
strncpy(list_next->this.vendor, tmp_string, 256);
free((void*)tmp_string);
}
tmpString = udev_device_get_property_value(udev_device, "ID_MODEL");
if(tmpString)
tmp_string = udev_device_get_property_value(udev_device, "ID_MODEL");
if(tmp_string)
{
strncpy(listNext->this.model, tmpString, 256);
free((void*)tmpString);
strncpy(list_next->this.model, tmp_string, 256);
free((void*)tmp_string);
for(i = 0; i < 256; i++)
{
if(listNext->this.model[i] == 0) break;
if(list_next->this.model[i] == 0) break;
if(listNext->this.model[i] == '_') listNext->this.model[i] = ' ';
if(list_next->this.model[i] == '_') list_next->this.model[i] = ' ';
}
}
tmpString = udev_device_get_property_value(udev_device, "ID_SCSI_SERIAL");
if(tmpString)
tmp_string = udev_device_get_property_value(udev_device, "ID_SCSI_SERIAL");
if(tmp_string)
{
strncpy(listNext->this.serial, tmpString, 256);
free((void*)tmpString);
strncpy(list_next->this.serial, tmp_string, 256);
free((void*)tmp_string);
}
else
{
tmpString = udev_device_get_property_value(udev_device, "ID_SERIAL_SHORT");
if(tmpString)
tmp_string = udev_device_get_property_value(udev_device, "ID_SERIAL_SHORT");
if(tmp_string)
{
strncpy(listNext->this.serial, tmpString, 256);
free((void*)tmpString);
strncpy(list_next->this.serial, tmp_string, 256);
free((void*)tmp_string);
}
}
tmpString = udev_device_get_property_value(udev_device, "ID_BUS");
if(tmpString)
tmp_string = udev_device_get_property_value(udev_device, "ID_BUS");
if(tmp_string)
{
strncpy(listNext->this.bus, tmpString, 256);
free((void*)tmpString);
strncpy(list_next->this.bus, tmp_string, 256);
free((void*)tmp_string);
}
}
}
tmpString = malloc(1024);
memset((void*)tmpString, 0, 1024);
snprintf((char*)tmpString, 1024, "%s/%s/device/vendor", PATH_SYS_DEVBLOCK, dirent->d_name);
tmp_string = malloc(1024);
memset((void*)tmp_string, 0, 1024);
snprintf((char*)tmp_string, 1024, "%s/%s/device/vendor", PATH_SYS_DEVBLOCK, dirent->d_name);
if(access(tmpString, R_OK) == 0 && strlen(listNext->this.vendor) == 0)
if(access(tmp_string, R_OK) == 0 && strlen(list_next->this.vendor) == 0)
{
file = fopen(tmpString, "rb");
file = fopen(tmp_string, "rb");
if(file != NULL)
{
@@ -145,15 +145,14 @@ DeviceInfoList* linux_list_devices()
if(ret > 0 && line_str != NULL)
{
strncpy(listNext->this.vendor, line_str, 256);
strncpy(list_next->this.vendor, line_str, 256);
for(i = 255; i >= 0; i--)
{
if(listNext->this.vendor[i] == 0)
continue;
if(list_next->this.vendor[i] == 0) continue;
else if(listNext->this.vendor[i] == 0x0A || listNext->this.vendor[i] == 0x0D ||
listNext->this.vendor[i] == ' ')
listNext->this.vendor[i] = 0;
else if(list_next->this.vendor[i] == 0x0A || list_next->this.vendor[i] == 0x0D ||
list_next->this.vendor[i] == ' ')
list_next->this.vendor[i] = 0;
else
break;
}
@@ -165,18 +164,18 @@ DeviceInfoList* linux_list_devices()
}
else if(strncmp(dirent->d_name, "loop", 4) == 0)
{
strncpy(listNext->this.vendor, "Linux", 256);
strncpy(list_next->this.vendor, "Linux", 256);
}
free((void*)tmpString);
free((void*)tmp_string);
tmpString = malloc(1024);
memset((void*)tmpString, 0, 1024);
snprintf((char*)tmpString, 1024, "%s/%s/device/model", PATH_SYS_DEVBLOCK, dirent->d_name);
tmp_string = malloc(1024);
memset((void*)tmp_string, 0, 1024);
snprintf((char*)tmp_string, 1024, "%s/%s/device/model", PATH_SYS_DEVBLOCK, dirent->d_name);
if(access(tmpString, R_OK) == 0 &&
(strlen(listNext->this.model) == 0 || strncmp(listNext->this.bus, "ata", 3) == 0))
if(access(tmp_string, R_OK) == 0 &&
(strlen(list_next->this.model) == 0 || strncmp(list_next->this.bus, "ata", 3) == 0))
{
file = fopen(tmpString, "rb");
file = fopen(tmp_string, "rb");
if(file != NULL)
{
@@ -187,15 +186,14 @@ DeviceInfoList* linux_list_devices()
if(ret > 0 && line_str != NULL)
{
strncpy(listNext->this.model, line_str, 256);
strncpy(list_next->this.model, line_str, 256);
for(i = 255; i >= 0; i--)
{
if(listNext->this.model[i] == 0)
continue;
if(list_next->this.model[i] == 0) continue;
else if(listNext->this.model[i] == 0x0A || listNext->this.model[i] == 0x0D ||
listNext->this.model[i] == ' ')
listNext->this.model[i] = 0;
else if(list_next->this.model[i] == 0x0A || list_next->this.model[i] == 0x0D ||
list_next->this.model[i] == ' ')
list_next->this.model[i] = 0;
else
break;
}
@@ -207,17 +205,17 @@ DeviceInfoList* linux_list_devices()
}
else if(strncmp(dirent->d_name, "loop", 4) == 0)
{
strncpy(listNext->this.model, "Linux", 256);
strncpy(list_next->this.model, "Linux", 256);
}
free((void*)tmpString);
free((void*)tmp_string);
tmpString = malloc(1024);
memset((void*)tmpString, 0, 1024);
snprintf((char*)tmpString, 1024, "%s/%s/device/serial", PATH_SYS_DEVBLOCK, dirent->d_name);
tmp_string = malloc(1024);
memset((void*)tmp_string, 0, 1024);
snprintf((char*)tmp_string, 1024, "%s/%s/device/serial", PATH_SYS_DEVBLOCK, dirent->d_name);
if(access(tmpString, R_OK) == 0 && (strlen(listNext->this.serial) == 0))
if(access(tmp_string, R_OK) == 0 && (strlen(list_next->this.serial) == 0))
{
file = fopen(tmpString, "rb");
file = fopen(tmp_string, "rb");
if(file != NULL)
{
@@ -228,15 +226,14 @@ DeviceInfoList* linux_list_devices()
if(ret > 0 && line_str != NULL)
{
strncpy(listNext->this.serial, line_str, 256);
strncpy(list_next->this.serial, line_str, 256);
for(i = 255; i >= 0; i--)
{
if(listNext->this.serial[i] == 0)
continue;
if(list_next->this.serial[i] == 0) continue;
else if(listNext->this.serial[i] == 0x0A || listNext->this.serial[i] == 0x0D ||
listNext->this.serial[i] == ' ')
listNext->this.serial[i] = 0;
else if(list_next->this.serial[i] == 0x0A || list_next->this.serial[i] == 0x0D ||
list_next->this.serial[i] == ' ')
list_next->this.serial[i] = 0;
else
break;
}
@@ -246,64 +243,63 @@ DeviceInfoList* linux_list_devices()
fclose(file);
}
}
free((void*)tmpString);
free((void*)tmp_string);
if(strlen(listNext->this.vendor) == 0 || strncmp(listNext->this.vendor, "ATA", 3) == 0)
if(strlen(list_next->this.vendor) == 0 || strncmp(list_next->this.vendor, "ATA", 3) == 0)
{
if(strlen(listNext->this.model) > 0)
if(strlen(list_next->this.model) > 0)
{
tmpString = malloc(256);
strncpy((void*)tmpString, listNext->this.model, 256);
tmp_string = malloc(256);
strncpy((void*)tmp_string, list_next->this.model, 256);
chrptr = strchr(tmpString, ' ');
chrptr = strchr(tmp_string, ' ');
if(chrptr)
{
memset(&listNext->this.vendor, 0, 256);
memset(&listNext->this.model, 0, 256);
strncpy(listNext->this.vendor, tmpString, chrptr - tmpString);
strncpy(listNext->this.model, chrptr + 1, 256 - (chrptr - tmpString) - 1);
memset(&list_next->this.vendor, 0, 256);
memset(&list_next->this.model, 0, 256);
strncpy(list_next->this.vendor, tmp_string, chrptr - tmp_string);
strncpy(list_next->this.model, chrptr + 1, 256 - (chrptr - tmp_string) - 1);
}
free((void*)tmpString);
free((void*)tmp_string);
}
}
// TODO: Get better device type from sysfs paths
if(strlen(listNext->this.bus) == 0)
if(strlen(list_next->this.bus) == 0)
{
if(strncmp(dirent->d_name, "loop", 4) == 0)
strncpy(listNext->this.bus, "loop", 4);
if(strncmp(dirent->d_name, "loop", 4) == 0) strncpy(list_next->this.bus, "loop", 4);
else if(strncmp(dirent->d_name, "nvme", 4) == 0)
strncpy(listNext->this.bus, "NVMe", 4);
strncpy(list_next->this.bus, "NVMe", 4);
else if(strncmp(dirent->d_name, "mmc", 3) == 0)
strncpy(listNext->this.bus, "MMC/SD", 6);
strncpy(list_next->this.bus, "MMC/SD", 6);
}
else
{
for(i = 0; i < 256; i++)
{
if(listNext->this.bus[i] == 0) break;
if(list_next->this.bus[i] == 0) break;
listNext->this.bus[i] = (char)toupper(listNext->this.bus[i]);
list_next->this.bus[i] = (char)toupper(list_next->this.bus[i]);
}
}
if(strncmp(listNext->this.bus, "ATA", 3) == 0 || strncmp(listNext->this.bus, "ATAPI", 5) == 0 ||
strncmp(listNext->this.bus, "SCSI", 4) == 0 || strncmp(listNext->this.bus, "USB", 3) == 0 ||
strncmp(listNext->this.bus, "PCMCIA", 6) == 0 || strncmp(listNext->this.bus, "FireWire", 8) == 0 ||
strncmp(listNext->this.bus, "MMC/SD", 6) == 0)
listNext->this.supported = true;
if(strncmp(list_next->this.bus, "ATA", 3) == 0 || strncmp(list_next->this.bus, "ATAPI", 5) == 0 ||
strncmp(list_next->this.bus, "SCSI", 4) == 0 || strncmp(list_next->this.bus, "USB", 3) == 0 ||
strncmp(list_next->this.bus, "PCMCIA", 6) == 0 || strncmp(list_next->this.bus, "FireWire", 8) == 0 ||
strncmp(list_next->this.bus, "MMC/SD", 6) == 0)
list_next->this.supported = true;
else
listNext->this.supported = false;
list_next->this.supported = false;
listCurrent = listNext;
dirent = readdir(dir);
list_current = list_next;
dirent = readdir(dir);
}
closedir(dir);
if(hasUdev) udev_unref(udev);
if(has_udev) udev_unref(udev);
return listStart;
return list_start;
}

View File

@@ -15,8 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "linux.h"
#include <dirent.h>
#include <stdint.h>
#include <stdio.h>
@@ -24,55 +22,55 @@
#include <sys/stat.h>
#include <unistd.h>
uint8_t linux_get_pcmcia_data(const char* devicePath, uint16_t* cisLen, char* cis)
uint8_t LinuxGetPcmciaData(const char* device_path, uint16_t* cis_len, char* cis)
{
char* devPath;
char tmpPath[4096];
char resolvedLink[4096];
char* dev_path;
char tmp_path[4096];
char resolved_link[4096];
struct stat sb;
ssize_t len;
char* rchr;
FILE* file;
DIR* dir;
struct dirent* dent;
*cisLen = 0;
*cis_len = 0;
memset(tmpPath, 0, 4096);
memset(resolvedLink, 0, 4096);
memset(tmp_path, 0, 4096);
memset(resolved_link, 0, 4096);
if(strncmp(devicePath, "/dev/sd", 7) != 0 && strncmp(devicePath, "/dev/sr", 7) != 0 &&
strncmp(devicePath, "/dev/st", 7) != 0)
if(strncmp(device_path, "/dev/sd", 7) != 0 && strncmp(device_path, "/dev/sr", 7) != 0 &&
strncmp(device_path, "/dev/st", 7) != 0)
return 0;
devPath = (char*)devicePath + 5;
dev_path = (char*)device_path + 5;
snprintf(tmpPath, 4096, "/sys/block/%s", devPath);
snprintf(tmp_path, 4096, "/sys/block/%s", dev_path);
if(stat(tmpPath, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
if(stat(tmp_path, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
len = readlink(tmpPath, resolvedLink, 4096);
len = readlink(tmp_path, resolved_link, 4096);
if(len == 0) return 0;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "/sys%s", resolvedLink + 2);
memcpy(resolvedLink, tmpPath, 4096);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "/sys%s", resolved_link + 2);
memcpy(resolved_link, tmp_path, 4096);
while(strstr(resolvedLink, "/sys/devices") != NULL)
while(strstr(resolved_link, "/sys/devices") != NULL)
{
rchr = strrchr(resolvedLink, '/');
rchr = strrchr(resolved_link, '/');
if(rchr == NULL) break;
*rchr = '\0';
if(strlen(resolvedLink) == 0) break;
if(strlen(resolved_link) == 0) break;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/pcmcia_socket", resolvedLink);
if(access(tmpPath, R_OK) != 0) continue;
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/pcmcia_socket", resolved_link);
if(access(tmp_path, R_OK) != 0) continue;
dir = opendir(tmpPath);
dir = opendir(tmp_path);
if(!dir) continue;
do
@@ -85,14 +83,14 @@ uint8_t linux_get_pcmcia_data(const char* devicePath, uint16_t* cisLen, char* ci
if(!dent) continue;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/pcmcia_socket/%s/cis", resolvedLink, dent->d_name);
if(access(tmpPath, R_OK) != 0) continue;
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/pcmcia_socket/%s/cis", resolved_link, dent->d_name);
if(access(tmp_path, R_OK) != 0) continue;
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(!file) return 0;
*cisLen = fread(cis, 65536, 1, file);
*cis_len = fread(cis, 65536, 1, file);
return 1;
}

View File

@@ -23,26 +23,26 @@
#include <string.h>
#include <sys/ioctl.h>
int32_t linux_send_scsi_command(int device_fd,
char* cdb,
char* buffer,
char** senseBuffer,
uint32_t timeout,
int32_t direction,
uint32_t* duration,
uint32_t* sense,
uint32_t cdb_len,
uint32_t* buf_len,
uint32_t* sense_len)
int32_t LinuxSendScsiCommand(int device_fd,
char* cdb,
char* buffer,
char** sense_buffer,
uint32_t timeout,
int32_t direction,
uint32_t* duration,
uint32_t* sense,
uint32_t cdb_len,
uint32_t* buf_len,
uint32_t* sense_len)
{
sg_io_hdr_t hdr;
int dir, ret;
*sense_len = 32;
memset(&hdr, 0, sizeof(sg_io_hdr_t));
*senseBuffer = malloc(*sense_len);
*sense_buffer = malloc(*sense_len);
if(!*senseBuffer) return -1;
if(!*sense_buffer) return -1;
switch(direction)
{
@@ -61,7 +61,7 @@ int32_t linux_send_scsi_command(int device_fd,
hdr.dxfer_len = *buf_len;
hdr.dxferp = buffer;
hdr.cmdp = (unsigned char*)cdb;
hdr.sbp = (unsigned char*)*senseBuffer;
hdr.sbp = (unsigned char*)*sense_buffer;
hdr.timeout = timeout;
hdr.flags = SG_FLAG_DIRECT_IO;

View File

@@ -22,19 +22,19 @@
#include <string.h>
#include <sys/ioctl.h>
int32_t linux_send_sdhci_command(int device_fd,
uint8_t command,
uint8_t write,
uint8_t application,
uint32_t flags,
uint32_t argument,
uint32_t block_size,
uint32_t blocks,
char* buffer,
uint32_t timeout,
uint32_t* response,
uint32_t* duration,
uint32_t* sense)
int32_t LinuxSendSdhciCommand(int device_fd,
uint8_t command,
uint8_t write,
uint8_t application,
uint32_t flags,
uint32_t argument,
uint32_t block_size,
uint32_t blocks,
char* buffer,
uint32_t timeout,
uint32_t* response,
uint32_t* duration,
uint32_t* sense)
{
struct mmc_ioc_cmd mmc_ioc_cmd;
int32_t error;

View File

@@ -23,114 +23,114 @@
#include <sys/stat.h>
#include <unistd.h>
uint8_t linux_get_usb_data(const char* devicePath,
uint16_t* descLen,
char* descriptors,
uint16_t* idVendor,
uint16_t* idProduct,
char* manufacturer,
char* product,
char* serial)
uint8_t LinuxGetUsbData(const char* device_path,
uint16_t* desc_len,
char* descriptors,
uint16_t* id_vendor,
uint16_t* id_product,
char* manufacturer,
char* product,
char* serial)
{
char* devPath;
char tmpPath[4096];
char resolvedLink[4096];
char* dev_path;
char tmp_path[4096];
char resolved_link[4096];
struct stat sb;
ssize_t len;
char* rchr;
int found = 1;
FILE* file;
*descLen = 0;
*idVendor = 0;
*idProduct = 0;
memset(tmpPath, 0, 4096);
memset(resolvedLink, 0, 4096);
*desc_len = 0;
*id_vendor = 0;
*id_product = 0;
memset(tmp_path, 0, 4096);
memset(resolved_link, 0, 4096);
if(strncmp(devicePath, "/dev/sd", 7) != 0 && strncmp(devicePath, "/dev/sr", 7) != 0 &&
strncmp(devicePath, "/dev/st", 7) != 0)
if(strncmp(device_path, "/dev/sd", 7) != 0 && strncmp(device_path, "/dev/sr", 7) != 0 &&
strncmp(device_path, "/dev/st", 7) != 0)
return 0;
devPath = (char*)devicePath + 5;
dev_path = (char*)device_path + 5;
snprintf(tmpPath, 4096, "/sys/block/%s", devPath);
snprintf(tmp_path, 4096, "/sys/block/%s", dev_path);
if(stat(tmpPath, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
if(stat(tmp_path, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
len = readlink(tmpPath, resolvedLink, 4096);
len = readlink(tmp_path, resolved_link, 4096);
if(len == 0) return 0;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "/sys%s", resolvedLink + 2);
memcpy(resolvedLink, tmpPath, 4096);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "/sys%s", resolved_link + 2);
memcpy(resolved_link, tmp_path, 4096);
while(strstr(resolvedLink, "usb") != NULL)
while(strstr(resolved_link, "usb") != NULL)
{
found = 1;
rchr = strrchr(resolvedLink, '/');
rchr = strrchr(resolved_link, '/');
if(rchr == NULL) break;
*rchr = '\0';
if(strlen(resolvedLink) == 0) break;
if(strlen(resolved_link) == 0) break;
snprintf(tmpPath, 4096, "%s/descriptors", resolvedLink);
if(access(tmpPath, R_OK) != 0) found = 0;
memset(tmpPath, 0, 4096);
snprintf(tmp_path, 4096, "%s/descriptors", resolved_link);
if(access(tmp_path, R_OK) != 0) found = 0;
memset(tmp_path, 0, 4096);
snprintf(tmpPath, 4096, "%s/idProduct", resolvedLink);
if(access(tmpPath, R_OK) != 0) found = 0;
memset(tmpPath, 0, 4096);
snprintf(tmp_path, 4096, "%s/idProduct", resolved_link);
if(access(tmp_path, R_OK) != 0) found = 0;
memset(tmp_path, 0, 4096);
snprintf(tmpPath, 4096, "%s/idVendor", resolvedLink);
if(access(tmpPath, R_OK) != 0) found = 0;
memset(tmpPath, 0, 4096);
snprintf(tmp_path, 4096, "%s/idVendor", resolved_link);
if(access(tmp_path, R_OK) != 0) found = 0;
memset(tmp_path, 0, 4096);
if(!found) continue;
snprintf(tmpPath, 4096, "%s/descriptors", resolvedLink);
file = fopen(tmpPath, "r");
snprintf(tmp_path, 4096, "%s/descriptors", resolved_link);
file = fopen(tmp_path, "r");
if(file == NULL) break;
*descLen = (uint16_t)fread(descriptors, 4096, 1, file);
*desc_len = (uint16_t)fread(descriptors, 4096, 1, file);
fclose(file);
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/idProduct", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/idProduct", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fscanf(file, "%4hx", idProduct);
fscanf(file, "%4hx", id_product);
fclose(file);
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/idVendor", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/idVendor", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fscanf(file, "%4hx", idVendor);
fscanf(file, "%4hx", id_vendor);
fclose(file);
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/manufacturer", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/manufacturer", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fread(manufacturer, 256, 1, file);
@@ -138,12 +138,12 @@ uint8_t linux_get_usb_data(const char* devicePath,
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/product", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/product", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fread(product, 256, 1, file);
@@ -151,12 +151,12 @@ uint8_t linux_get_usb_data(const char* devicePath,
}
}
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/serial", resolvedLink);
memset(tmp_path, 0, 4096);
snprintf(tmp_path, 4096, "%s/serial", resolved_link);
if(access(tmpPath, R_OK) == 0)
if(access(tmp_path, R_OK) == 0)
{
file = fopen(tmpPath, "r");
file = fopen(tmp_path, "r");
if(file != NULL)
{
fread(serial, 256, 1, file);
@@ -167,5 +167,5 @@ uint8_t linux_get_usb_data(const char* devicePath,
break;
}
return *descLen != 0;
return *desc_len != 0;
}