diff --git a/CMakeLists.txt b/CMakeLists.txt index 619810a..a7788e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/dicmote.h b/dicmote.h index 75b0809..2e6ca07 100644 --- a/dicmote.h +++ b/dicmote.h @@ -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 diff --git a/linux/linux.h b/linux/linux.h index 575c87b..d8fa039 100644 --- a/linux/linux.h +++ b/linux/linux.h @@ -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 diff --git a/linux/pcmcia.c b/linux/pcmcia.c new file mode 100644 index 0000000..b9e9728 --- /dev/null +++ b/linux/pcmcia.c @@ -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 . + */ + +#include "linux.h" + +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/main.c b/main.c index a7eb7cc..c1f0614 100644 --- a/main.c +++ b/main.c @@ -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); diff --git a/pcmcia.c b/pcmcia.c new file mode 100644 index 0000000..eec98b8 --- /dev/null +++ b/pcmcia.c @@ -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 . + */ + +#include + +#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 +} \ No newline at end of file