Implement Get PCMCIA Data packet, and support for Linux.

This commit is contained in:
2019-10-19 01:09:50 +01:00
parent fa4fa839a6
commit eb4e3e3f32
6 changed files with 172 additions and 3 deletions

View File

@@ -3,10 +3,10 @@ project(dicremote C)
set(CMAKE_C_STANDARD 90)
set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c usb.c ieee1394.c)
set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c usb.c ieee1394.c pcmcia.c)
if("${CMAKE_SYSTEM}" MATCHES "Linux")
set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c linux/usb.c linux/ieee1394.c)
set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c linux/usb.c linux/ieee1394.c linux/pcmcia.c)
endif()
add_executable(dicremote ${MAIN_SOURCES} ${PLATFORM_SOURCES})

View File

@@ -432,5 +432,6 @@ uint8_t GetFireWireData(const char* devicePath,
uint64_t* guid,
char* vendor,
char* model);
uint8_t GetPcmciaData(const char* devicePath, uint16_t* cisLen, char* cis);
#endif

View File

@@ -58,5 +58,6 @@ uint8_t linux_get_ieee1394_data(const char* devicePath,
uint64_t* guid,
char* vendor,
char* model);
uint8_t linux_get_pcmcia_data(const char* devicePath, uint16_t* cisLen, char* cis);
#endif // DICREMOTE_LINUX_H

101
linux/pcmcia.c Normal file
View File

@@ -0,0 +1,101 @@
/*
* This file is part of the DiscImageChef Remote Server.
* Copyright (c) 2019 Natalia Portillo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "linux.h"
#include <dirent.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
uint8_t linux_get_pcmcia_data(const char* devicePath, uint16_t* cisLen, char* cis)
{
char* devPath;
char tmpPath[4096];
char resolvedLink[4096];
struct stat sb;
ssize_t len;
char* rchr;
FILE* file;
DIR* dir;
struct dirent* dent;
*cisLen = 0;
memset(tmpPath, 0, 4096);
memset(resolvedLink, 0, 4096);
if(strncmp(devicePath, "/dev/sd", 7) != 0 && strncmp(devicePath, "/dev/sr", 7) != 0 &&
strncmp(devicePath, "/dev/st", 7) != 0)
return 0;
devPath = (char*)devicePath + 5;
snprintf(tmpPath, 4096, "/sys/block/%s", devPath);
if(stat(tmpPath, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
len = readlink(tmpPath, resolvedLink, 4096);
if(len == 0) return 0;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "/sys%s", resolvedLink + 2);
memcpy(resolvedLink, tmpPath, 4096);
while(strstr(resolvedLink, "/sys/devices") != NULL)
{
rchr = strrchr(resolvedLink, '/');
if(rchr == NULL) break;
*rchr = '\0';
if(strlen(resolvedLink) == 0) break;
memset(tmpPath, 0, 4096);
snprintf(tmpPath, 4096, "%s/pcmcia_socket", resolvedLink);
if(access(tmpPath, R_OK) != 0) continue;
dir = opendir(tmpPath);
if(!dir) continue;
do
{
dent = readdir(dir);
if(!dent) break;
} while(dent && dent->d_type != DT_DIR);
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;
file = fopen(tmpPath, "r");
if(!file) return 0;
*cisLen = fread(cis, 65536, 1, file);
return 1;
}
return 0;
}

37
main.c
View File

@@ -67,6 +67,7 @@ int main()
char* scr;
DicPacketResGetUsbData* pkt_res_usb;
DicPacketResGetFireWireData* pkt_res_firewire;
DicPacketResGetPcmciaData* pkt_res_pcmcia;
printf("DiscImageChef Remote Server %s\n", DICMOTE_VERSION);
printf("Copyright (C) 2019 Natalia Portillo\n");
@@ -700,11 +701,45 @@ int main()
write(cli_sock, pkt_res_firewire, pkt_res_firewire->hdr.len);
free(pkt_res_firewire);
continue;
case DICMOTE_PACKET_TYPE_COMMAND_GET_PCMCIA_DATA:
// Packet only contains header so, dummy
in_buf = malloc(pkt_hdr->len);
if(!in_buf)
{
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
free(pkt_hdr);
close(cli_sock);
continue;
}
recv(cli_sock, in_buf, pkt_hdr->len, 0);
free(in_buf);
pkt_res_pcmcia = malloc(sizeof(DicPacketResGetPcmciaData));
if(!pkt_res_pcmcia)
{
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
free(pkt_hdr);
close(cli_sock);
continue;
}
memset(pkt_res_pcmcia, 0, sizeof(DicPacketResGetPcmciaData));
pkt_res_pcmcia->hdr.id = DICMOTE_PACKET_ID;
pkt_res_pcmcia->hdr.version = DICMOTE_PACKET_VERSION;
pkt_res_pcmcia->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_GET_PCMCIA_DATA;
pkt_res_pcmcia->hdr.len = sizeof(DicPacketResGetPcmciaData);
pkt_res_pcmcia->isPcmcia =
GetPcmciaData(device_path, &pkt_res_pcmcia->cis_len, pkt_res_pcmcia->cis);
write(cli_sock, pkt_res_pcmcia, pkt_res_pcmcia->hdr.len);
free(pkt_res_pcmcia);
continue;
case DICMOTE_PACKET_TYPE_COMMAND_ATA_CHS:
case DICMOTE_PACKET_TYPE_COMMAND_ATA_LBA28:
case DICMOTE_PACKET_TYPE_COMMAND_ATA_LBA48:
case DICMOTE_PACKET_TYPE_COMMAND_SDHCI:
case DICMOTE_PACKET_TYPE_COMMAND_GET_PCMCIA_DATA:
pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_NOT_IMPLEMENTED;
memset(&pkt_nop->reason, 0, 256);
strncpy(pkt_nop->reason, "Packet not yet implemented, skipping...", 256);

31
pcmcia.c Normal file
View File

@@ -0,0 +1,31 @@
/*
* This file is part of the DiscImageChef Remote Server.
* Copyright (c) 2019 Natalia Portillo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#if defined(__linux__) && !defined(__ANDROID__)
#include "linux/linux.h"
#endif
uint8_t GetPcmciaData(const char* devicePath, uint16_t* cisLen, char* cis)
{
#if defined(__linux__) && !defined(__ANDROID__)
return linux_get_pcmcia_data(devicePath, cisLen, cis);
#else
return 0;
#endif
}