2019-10-20 21:50:25 +01:00
|
|
|
/*
|
|
|
|
|
* 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 "dicmote.h"
|
2019-10-26 14:27:59 +01:00
|
|
|
#include "endian.h"
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
void* WorkingLoop(void* arguments)
|
|
|
|
|
{
|
|
|
|
|
AtaErrorRegistersChs ata_chs_error_regs;
|
|
|
|
|
AtaErrorRegistersLba28 ata_lba28_error_regs;
|
|
|
|
|
AtaErrorRegistersLba48 ata_lba48_error_regs;
|
|
|
|
|
char* buffer;
|
|
|
|
|
char* cdb_buf;
|
|
|
|
|
char* cid;
|
|
|
|
|
char* csd;
|
|
|
|
|
char* in_buf;
|
|
|
|
|
char* ocr;
|
|
|
|
|
char* out_buf;
|
|
|
|
|
char* scr;
|
|
|
|
|
char* sense_buf;
|
|
|
|
|
DicPacketCmdAtaChs* pkt_cmd_ata_chs;
|
|
|
|
|
DicPacketCmdAtaLba28* pkt_cmd_ata_lba28;
|
|
|
|
|
DicPacketCmdAtaLba48* pkt_cmd_ata_lba48;
|
|
|
|
|
DicPacketCmdOpen* pkt_dev_open;
|
|
|
|
|
DicPacketCmdScsi* pkt_cmd_scsi;
|
|
|
|
|
DicPacketCmdSdhci* pkt_cmd_sdhci;
|
|
|
|
|
DicPacketHeader* pkt_hdr;
|
|
|
|
|
DicPacketHello* pkt_server_hello;
|
|
|
|
|
DicPacketHello* pkt_client_hello;
|
|
|
|
|
DicPacketNop* pkt_nop;
|
2019-10-26 17:32:35 +01:00
|
|
|
DicPacketResAmIRoot* pkt_res_am_i_root;
|
2019-10-20 21:50:25 +01:00
|
|
|
DicPacketResAtaChs* pkt_res_ata_chs;
|
|
|
|
|
DicPacketResAtaLba28* pkt_res_ata_lba28;
|
|
|
|
|
DicPacketResAtaLba48* pkt_res_ata_lba48;
|
|
|
|
|
DicPacketResGetDeviceType* pkt_dev_type;
|
|
|
|
|
DicPacketResGetFireWireData* pkt_res_firewire;
|
|
|
|
|
DicPacketResGetPcmciaData* pkt_res_pcmcia;
|
|
|
|
|
DicPacketResGetSdhciRegisters* pkt_res_sdhci_registers;
|
|
|
|
|
DicPacketResGetUsbData* pkt_res_usb;
|
|
|
|
|
DicPacketResListDevs* pkt_res_devinfo;
|
|
|
|
|
DicPacketResScsi* pkt_res_scsi;
|
|
|
|
|
DicPacketResSdhci* pkt_res_sdhci;
|
|
|
|
|
int skip_next_hdr;
|
|
|
|
|
int ret;
|
|
|
|
|
socklen_t cli_len;
|
|
|
|
|
ssize_t recv_size;
|
|
|
|
|
struct DeviceInfoList* device_info_list;
|
|
|
|
|
struct sockaddr_in cli_addr, serv_addr;
|
|
|
|
|
uint32_t duration;
|
|
|
|
|
uint32_t sdhci_response[4];
|
|
|
|
|
uint32_t sense;
|
|
|
|
|
uint32_t sense_len;
|
2019-10-26 22:25:31 +01:00
|
|
|
uint32_t n;
|
2019-10-26 16:11:19 +01:00
|
|
|
void* device_ctx = NULL;
|
2019-10-26 22:22:04 +01:00
|
|
|
void* net_ctx = NULL;
|
|
|
|
|
void* cli_ctx = NULL;
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!arguments)
|
|
|
|
|
{
|
|
|
|
|
printf("Hello packet not sent, returning");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_server_hello = (DicPacketHello*)arguments;
|
|
|
|
|
|
|
|
|
|
printf("Opening socket.\n");
|
2019-10-26 22:22:04 +01:00
|
|
|
net_ctx = NetSocket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
if(!net_ctx)
|
2019-10-20 21:50:25 +01:00
|
|
|
{
|
|
|
|
|
printf("Error %d opening socket.\n", errno);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
|
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
serv_addr.sin_port = htons(DICMOTE_PORT);
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
if(NetBind(net_ctx, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
|
2019-10-20 21:50:25 +01:00
|
|
|
{
|
|
|
|
|
printf("Error %d binding socket.\n", errno);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(net_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
ret = NetListen(net_ctx, 1);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(ret)
|
|
|
|
|
{
|
|
|
|
|
printf("Error %d listening.\n", errno);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(net_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_nop = malloc(sizeof(DicPacketNop));
|
|
|
|
|
|
|
|
|
|
if(!pkt_nop)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory.\n", errno);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(net_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_nop, 0, sizeof(DicPacketNop));
|
|
|
|
|
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_nop->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_nop->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_nop->hdr.len = htole32(sizeof(DicPacketNop));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_nop->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_nop->hdr.packet_type = DICMOTE_PACKET_TYPE_NOP;
|
|
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("Waiting for a client...\n");
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
cli_len = sizeof(cli_addr);
|
|
|
|
|
cli_ctx = NetAccept(net_ctx, (struct sockaddr*)&cli_addr, &cli_len);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
if(!cli_ctx)
|
2019-10-20 21:50:25 +01:00
|
|
|
{
|
|
|
|
|
printf("Error %d accepting incoming connection.\n", errno);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(net_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Client %s connected successfully.\n", PrintIpv4Address(cli_addr.sin_addr));
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_server_hello, sizeof(DicPacketHello));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_hdr = malloc(sizeof(DicPacketHeader));
|
|
|
|
|
|
|
|
|
|
if(!pkt_hdr)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory.\n", errno);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
|
|
|
|
NetClose(net_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_server_hello);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
recv_size = NetRecv(cli_ctx, pkt_hdr, sizeof(DicPacketHeader), MSG_PEEK);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(recv_size < 0)
|
|
|
|
|
{
|
|
|
|
|
printf("Error %d reading response from client.\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(recv_size == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("Client closed connection.\n");
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 15:34:28 +01:00
|
|
|
if(pkt_hdr->remote_id != htole32(DICMOTE_REMOTE_ID) || pkt_hdr->packet_id != htole32(DICMOTE_PACKET_ID))
|
2019-10-20 21:50:25 +01:00
|
|
|
{
|
|
|
|
|
printf("Received data is not a correct dicremote packet, closing connection...\n");
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(pkt_hdr->version != DICMOTE_PACKET_VERSION)
|
|
|
|
|
{
|
|
|
|
|
printf("Unrecognized packet version, closing connection...\n");
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(pkt_hdr->packet_type != DICMOTE_PACKET_TYPE_HELLO)
|
|
|
|
|
{
|
|
|
|
|
printf("Expecting hello packet type, received type %d, closing connection...\n", pkt_hdr->packet_type);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_client_hello = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!pkt_client_hello)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
recv_size = NetRecv(cli_ctx, pkt_client_hello, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(recv_size != le32toh(pkt_hdr->len))
|
2019-10-20 21:50:25 +01:00
|
|
|
{
|
2019-10-22 00:44:39 +01:00
|
|
|
printf("Expected %d bytes of packet, got %ld, closing connection...\n", le32toh(pkt_hdr->len), recv_size);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_hdr);
|
|
|
|
|
free(pkt_client_hello);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Client application: %s %s\n", pkt_client_hello->application, pkt_client_hello->version);
|
|
|
|
|
printf("Client operating system: %s %s (%s)\n",
|
|
|
|
|
pkt_client_hello->sysname,
|
|
|
|
|
pkt_client_hello->release,
|
|
|
|
|
pkt_client_hello->machine);
|
|
|
|
|
printf("Client maximum protocol: %d\n", pkt_client_hello->max_protocol);
|
|
|
|
|
|
|
|
|
|
free(pkt_client_hello);
|
|
|
|
|
|
|
|
|
|
skip_next_hdr = 0;
|
|
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
if(skip_next_hdr)
|
|
|
|
|
{
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
skip_next_hdr = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
recv_size = NetRecv(cli_ctx, pkt_hdr, sizeof(DicPacketHeader), MSG_PEEK);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(recv_size < 0)
|
|
|
|
|
{
|
|
|
|
|
printf("Error %d reading response from client, closing connection...\n", errno);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_hdr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(recv_size == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("Client closed connection, closing connection...\n");
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_hdr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 15:34:28 +01:00
|
|
|
if(pkt_hdr->remote_id != htole32(DICMOTE_REMOTE_ID) || pkt_hdr->packet_id != htole32(DICMOTE_PACKET_ID))
|
2019-10-20 21:50:25 +01:00
|
|
|
{
|
|
|
|
|
printf("Received data is not a correct dicremote packet, closing connection...\n");
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_hdr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(pkt_hdr->version != DICMOTE_PACKET_VERSION)
|
|
|
|
|
{
|
|
|
|
|
printf("Unrecognized packet version, skipping...\n");
|
|
|
|
|
skip_next_hdr = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(pkt_hdr->packet_type)
|
|
|
|
|
{
|
|
|
|
|
case DICMOTE_PACKET_TYPE_HELLO:
|
|
|
|
|
pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_OOO;
|
|
|
|
|
memset(&pkt_nop->reason, 0, 256);
|
|
|
|
|
strncpy(pkt_nop->reason, "Received hello packet out of order, skipping...", 256);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_nop, sizeof(DicPacketNop));
|
2019-10-20 21:50:25 +01:00
|
|
|
printf("%s...\n", pkt_nop->reason);
|
|
|
|
|
skip_next_hdr = 1;
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_LIST_DEVICES:
|
|
|
|
|
device_info_list = ListDevices();
|
|
|
|
|
|
|
|
|
|
// Packet only contains header so, dummy
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
|
|
|
|
|
if(!device_info_list)
|
|
|
|
|
{
|
|
|
|
|
pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_ERROR_LIST_DEVICES;
|
|
|
|
|
memset(&pkt_nop->reason, 0, 256);
|
|
|
|
|
strncpy(pkt_nop->reason, "Could not get device list, continuing...", 256);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_nop, sizeof(DicPacketNop));
|
2019-10-20 21:50:25 +01:00
|
|
|
printf("%s...\n", pkt_nop->reason);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_res_devinfo = malloc(sizeof(DicPacketResListDevs));
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_devinfo->devices = htole16(DeviceInfoListCount(device_info_list));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
n = sizeof(DicPacketResListDevs) + le16toh(pkt_res_devinfo->devices) * sizeof(DeviceInfo);
|
2019-10-20 21:50:25 +01:00
|
|
|
in_buf = malloc(n);
|
2019-10-22 00:44:39 +01:00
|
|
|
((DicPacketResListDevs*)in_buf)->hdr.len = htole32(n);
|
2019-10-20 21:50:25 +01:00
|
|
|
((DicPacketResListDevs*)in_buf)->devices = pkt_res_devinfo->devices;
|
|
|
|
|
free(pkt_res_devinfo);
|
|
|
|
|
pkt_res_devinfo = (DicPacketResListDevs*)in_buf;
|
|
|
|
|
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_devinfo->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_devinfo->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_devinfo->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_res_devinfo->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_LIST_DEVICES;
|
|
|
|
|
|
|
|
|
|
// Save list start
|
|
|
|
|
in_buf = (char*)device_info_list;
|
|
|
|
|
long off = sizeof(DicPacketResListDevs);
|
|
|
|
|
|
|
|
|
|
while(device_info_list)
|
|
|
|
|
{
|
|
|
|
|
memcpy(((char*)pkt_res_devinfo) + off, &device_info_list->this, sizeof(DeviceInfo));
|
|
|
|
|
device_info_list = device_info_list->next;
|
|
|
|
|
off += sizeof(DeviceInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_info_list = (struct DeviceInfoList*)in_buf;
|
|
|
|
|
FreeDeviceInfoList(device_info_list);
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_devinfo, le32toh(pkt_res_devinfo->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_res_devinfo);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_GET_SDHCI_REGISTERS:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_LIST_DEVICES:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_SCSI:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_ATA_CHS:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_ATA_LBA_28:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_ATA_LBA_48:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_SDHCI:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_GET_DEVTYPE:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_GET_USB_DATA:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_GET_FIREWIRE_DATA:
|
|
|
|
|
case DICMOTE_PACKET_TYPE_RESPONSE_GET_PCMCIA_DATA:
|
|
|
|
|
pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_OOO;
|
|
|
|
|
memset(&pkt_nop->reason, 0, 256);
|
|
|
|
|
strncpy(pkt_nop->reason, "Received response packet?! You should certainly not do that...", 256);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_nop, sizeof(DicPacketNop));
|
2019-10-20 21:50:25 +01:00
|
|
|
printf("%s...\n", pkt_nop->reason);
|
|
|
|
|
skip_next_hdr = 1;
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_OPEN_DEVICE:
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_dev_open = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!pkt_dev_open)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, pkt_dev_open, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 16:11:19 +01:00
|
|
|
device_ctx = DeviceOpen(pkt_dev_open->device_path);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_nop->reason_code =
|
2019-10-26 16:11:19 +01:00
|
|
|
device_ctx == NULL ? DICMOTE_PACKET_NOP_REASON_OPEN_ERROR : DICMOTE_PACKET_NOP_REASON_OPEN_OK;
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_nop->error_no = errno;
|
|
|
|
|
memset(&pkt_nop->reason, 0, 256);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_nop, sizeof(DicPacketNop));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
free(pkt_dev_open);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_GET_DEVTYPE:
|
|
|
|
|
// Packet only contains header so, dummy
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
|
|
|
|
|
pkt_dev_type = malloc(sizeof(DicPacketResGetDeviceType));
|
|
|
|
|
|
|
|
|
|
if(!pkt_dev_type)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_dev_type, 0, sizeof(DicPacketResGetDeviceType));
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_dev_type->hdr.len = htole32(sizeof(DicPacketResGetDeviceType));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_dev_type->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_GET_DEVTYPE;
|
|
|
|
|
pkt_dev_type->hdr.version = DICMOTE_PACKET_VERSION;
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_dev_type->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_dev_type->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-26 16:11:19 +01:00
|
|
|
pkt_dev_type->device_type = htole32(GetDeviceType(device_ctx));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_dev_type, sizeof(DicPacketResGetDeviceType));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_dev_type);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_SCSI:
|
|
|
|
|
// Packet contains data after
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_cmd_scsi = (DicPacketCmdScsi*)in_buf;
|
|
|
|
|
|
|
|
|
|
// TODO: Check size of buffers + size of packet is not bigger than size in header
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(le32toh(pkt_cmd_scsi->cdb_len) > 0) cdb_buf = in_buf + sizeof(DicPacketCmdScsi);
|
2019-10-20 21:50:25 +01:00
|
|
|
else
|
|
|
|
|
cdb_buf = NULL;
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(le32toh(pkt_cmd_scsi->buf_len) > 0)
|
|
|
|
|
buffer = in_buf + le32toh(pkt_cmd_scsi->cdb_len) + sizeof(DicPacketCmdScsi);
|
2019-10-20 21:50:25 +01:00
|
|
|
else
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
// Swap buf_len
|
|
|
|
|
pkt_cmd_scsi->buf_len = le32toh(pkt_cmd_scsi->buf_len);
|
|
|
|
|
|
2019-10-26 16:11:19 +01:00
|
|
|
ret = SendScsiCommand(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
cdb_buf,
|
|
|
|
|
buffer,
|
|
|
|
|
&sense_buf,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_scsi->timeout),
|
|
|
|
|
le32toh(pkt_cmd_scsi->direction),
|
2019-10-20 21:50:25 +01:00
|
|
|
&duration,
|
|
|
|
|
&sense,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_scsi->cdb_len),
|
2019-10-20 21:50:25 +01:00
|
|
|
&pkt_cmd_scsi->buf_len,
|
|
|
|
|
&sense_len);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
// Swap buf_len back
|
|
|
|
|
pkt_cmd_scsi->buf_len = htole32(pkt_cmd_scsi->buf_len);
|
|
|
|
|
|
|
|
|
|
out_buf = malloc(sizeof(DicPacketResScsi) + sense_len + le32toh(pkt_cmd_scsi->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!out_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, continuing...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
|
|
|
|
free(in_buf);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_res_scsi = (DicPacketResScsi*)out_buf;
|
|
|
|
|
if(sense_buf) memcpy(out_buf + sizeof(DicPacketResScsi), sense_buf, sense_len);
|
|
|
|
|
if(buffer) memcpy(out_buf + sizeof(DicPacketResScsi) + sense_len, buffer, pkt_cmd_scsi->buf_len);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_scsi->hdr.len = htole32(sizeof(DicPacketResScsi) + sense_len + pkt_cmd_scsi->buf_len);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_scsi->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_SCSI;
|
|
|
|
|
pkt_res_scsi->hdr.version = DICMOTE_PACKET_VERSION;
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_scsi->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_scsi->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_scsi->sense_len = htole32(sense_len);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_scsi->buf_len = pkt_cmd_scsi->buf_len;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_scsi->duration = htole32(duration);
|
|
|
|
|
pkt_res_scsi->sense = htole32(sense);
|
|
|
|
|
pkt_res_scsi->error_no = htole32(ret);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_scsi, le32toh(pkt_res_scsi->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_cmd_scsi);
|
|
|
|
|
free(pkt_res_scsi);
|
|
|
|
|
if(sense_buf) free(sense_buf);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_GET_SDHCI_REGISTERS:
|
|
|
|
|
// Packet only contains header so, dummy
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
|
|
|
|
|
pkt_res_sdhci_registers = malloc(sizeof(DicPacketResGetSdhciRegisters));
|
|
|
|
|
if(!pkt_res_sdhci_registers)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_res_sdhci_registers, 0, sizeof(DicPacketResGetSdhciRegisters));
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_sdhci_registers->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_sdhci_registers->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_sdhci_registers->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_res_sdhci_registers->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_GET_SDHCI_REGISTERS;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_sdhci_registers->hdr.len = htole32(sizeof(DicPacketResGetSdhciRegisters));
|
2019-10-26 16:11:19 +01:00
|
|
|
pkt_res_sdhci_registers->is_sdhci = GetSdhciRegisters(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
&csd,
|
|
|
|
|
&cid,
|
|
|
|
|
&ocr,
|
|
|
|
|
&scr,
|
|
|
|
|
&pkt_res_sdhci_registers->csd_len,
|
|
|
|
|
&pkt_res_sdhci_registers->cid_len,
|
|
|
|
|
&pkt_res_sdhci_registers->ocr_len,
|
|
|
|
|
&pkt_res_sdhci_registers->scr_len);
|
|
|
|
|
|
|
|
|
|
if(pkt_res_sdhci_registers->csd_len > 0 && csd != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(pkt_res_sdhci_registers->csd_len > 16) pkt_res_sdhci_registers->csd_len = 16;
|
|
|
|
|
|
|
|
|
|
memcpy(pkt_res_sdhci_registers->csd, csd, pkt_res_sdhci_registers->csd_len);
|
|
|
|
|
}
|
|
|
|
|
if(pkt_res_sdhci_registers->cid_len > 0 && cid != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(pkt_res_sdhci_registers->cid_len > 16) pkt_res_sdhci_registers->cid_len = 16;
|
|
|
|
|
|
|
|
|
|
memcpy(pkt_res_sdhci_registers->cid, cid, pkt_res_sdhci_registers->cid_len);
|
|
|
|
|
}
|
|
|
|
|
if(pkt_res_sdhci_registers->ocr_len > 0 && ocr != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(pkt_res_sdhci_registers->ocr_len > 4) pkt_res_sdhci_registers->ocr_len = 4;
|
|
|
|
|
|
|
|
|
|
memcpy(pkt_res_sdhci_registers->ocr, ocr, pkt_res_sdhci_registers->ocr_len);
|
|
|
|
|
}
|
|
|
|
|
if(pkt_res_sdhci_registers->scr_len > 0 && scr != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(pkt_res_sdhci_registers->scr_len > 8) pkt_res_sdhci_registers->scr_len = 8;
|
|
|
|
|
|
|
|
|
|
memcpy(pkt_res_sdhci_registers->scr, scr, pkt_res_sdhci_registers->scr_len);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
// Swap lengths
|
|
|
|
|
pkt_res_sdhci_registers->csd_len = htole32(pkt_res_sdhci_registers->csd_len);
|
|
|
|
|
pkt_res_sdhci_registers->cid_len = htole32(pkt_res_sdhci_registers->cid_len);
|
|
|
|
|
pkt_res_sdhci_registers->ocr_len = htole32(pkt_res_sdhci_registers->ocr_len);
|
|
|
|
|
pkt_res_sdhci_registers->scr_len = htole32(pkt_res_sdhci_registers->scr_len);
|
|
|
|
|
|
2019-10-20 21:50:25 +01:00
|
|
|
free(csd);
|
|
|
|
|
free(cid);
|
|
|
|
|
free(scr);
|
|
|
|
|
free(ocr);
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_sdhci_registers, le32toh(pkt_res_sdhci_registers->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_res_sdhci_registers);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_GET_USB_DATA:
|
|
|
|
|
// Packet only contains header so, dummy
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
|
|
|
|
|
pkt_res_usb = malloc(sizeof(DicPacketResGetUsbData));
|
|
|
|
|
if(!pkt_res_usb)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_res_usb, 0, sizeof(DicPacketResGetUsbData));
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_usb->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_usb->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_usb->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_res_usb->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_GET_USB_DATA;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_usb->hdr.len = htole32(sizeof(DicPacketResGetUsbData));
|
2019-10-26 16:11:19 +01:00
|
|
|
pkt_res_usb->is_usb = GetUsbData(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
&pkt_res_usb->desc_len,
|
|
|
|
|
pkt_res_usb->descriptors,
|
|
|
|
|
&pkt_res_usb->id_vendor,
|
|
|
|
|
&pkt_res_usb->id_product,
|
|
|
|
|
pkt_res_usb->manufacturer,
|
|
|
|
|
pkt_res_usb->product,
|
|
|
|
|
pkt_res_usb->serial);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
// Swap parameters
|
|
|
|
|
pkt_res_usb->desc_len = htole32(pkt_res_usb->desc_len);
|
|
|
|
|
// TODO: Need to swap vendor, product?
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_usb, le32toh(pkt_res_usb->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_res_usb);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_GET_FIREWIRE_DATA:
|
|
|
|
|
// Packet only contains header so, dummy
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
|
|
|
|
|
pkt_res_firewire = malloc(sizeof(DicPacketResGetFireWireData));
|
|
|
|
|
if(!pkt_res_firewire)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_res_firewire, 0, sizeof(DicPacketResGetFireWireData));
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_firewire->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_firewire->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_firewire->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_res_firewire->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_GET_FIREWIRE_DATA;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_firewire->hdr.len = htole32(sizeof(DicPacketResGetFireWireData));
|
2019-10-26 16:11:19 +01:00
|
|
|
pkt_res_firewire->is_firewire = GetFireWireData(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
&pkt_res_firewire->id_model,
|
|
|
|
|
&pkt_res_firewire->id_vendor,
|
|
|
|
|
&pkt_res_firewire->guid,
|
|
|
|
|
pkt_res_firewire->vendor,
|
|
|
|
|
pkt_res_firewire->model);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
// TODO: Need to swap IDs?
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_firewire, le32toh(pkt_res_firewire->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_res_firewire);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_GET_PCMCIA_DATA:
|
|
|
|
|
// Packet only contains header so, dummy
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
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);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_res_pcmcia, 0, sizeof(DicPacketResGetPcmciaData));
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_pcmcia->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_pcmcia->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_pcmcia->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_res_pcmcia->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_GET_PCMCIA_DATA;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_pcmcia->hdr.len = htole32(sizeof(DicPacketResGetPcmciaData));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_pcmcia->is_pcmcia =
|
2019-10-26 16:11:19 +01:00
|
|
|
GetPcmciaData(device_ctx, &pkt_res_pcmcia->cis_len, pkt_res_pcmcia->cis);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_pcmcia->cis_len = htole32(pkt_res_pcmcia->cis_len);
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_pcmcia, le32toh(pkt_res_pcmcia->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_res_pcmcia);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_ATA_CHS:
|
|
|
|
|
// Packet contains data after
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_cmd_ata_chs = (DicPacketCmdAtaChs*)in_buf;
|
|
|
|
|
|
|
|
|
|
// TODO: Check size of buffers + size of packet is not bigger than size in header
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(le32toh(pkt_cmd_ata_chs->buf_len) > 0) buffer = in_buf + sizeof(DicPacketCmdAtaChs);
|
2019-10-20 21:50:25 +01:00
|
|
|
else
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
|
|
memset(&ata_chs_error_regs, 0, sizeof(AtaErrorRegistersChs));
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_cmd_ata_chs->buf_len = le32toh(pkt_cmd_ata_chs->buf_len);
|
|
|
|
|
|
2019-10-20 21:50:25 +01:00
|
|
|
duration = 0;
|
|
|
|
|
sense = 1;
|
2019-10-26 16:11:19 +01:00
|
|
|
ret = SendAtaChsCommand(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_ata_chs->registers,
|
|
|
|
|
&ata_chs_error_regs,
|
|
|
|
|
pkt_cmd_ata_chs->protocol,
|
|
|
|
|
pkt_cmd_ata_chs->transfer_register,
|
|
|
|
|
buffer,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_ata_chs->timeout),
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_ata_chs->transfer_blocks,
|
|
|
|
|
&duration,
|
|
|
|
|
&sense,
|
|
|
|
|
&pkt_cmd_ata_chs->buf_len);
|
|
|
|
|
|
|
|
|
|
out_buf = malloc(sizeof(DicPacketResAtaChs) + pkt_cmd_ata_chs->buf_len);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_cmd_ata_chs->buf_len = htole32(pkt_cmd_ata_chs->buf_len);
|
|
|
|
|
|
2019-10-20 21:50:25 +01:00
|
|
|
if(!out_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, continuing...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
|
|
|
|
free(in_buf);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_res_ata_chs = (DicPacketResAtaChs*)out_buf;
|
2019-10-22 00:44:39 +01:00
|
|
|
if(buffer) memcpy(out_buf + sizeof(DicPacketResAtaChs), buffer, htole32(pkt_cmd_ata_chs->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_ata_chs->hdr.len = htole32(sizeof(DicPacketResAtaChs) + htole32(pkt_cmd_ata_chs->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_ata_chs->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_ATA_CHS;
|
|
|
|
|
pkt_res_ata_chs->hdr.version = DICMOTE_PACKET_VERSION;
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_ata_chs->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_ata_chs->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_res_ata_chs->registers = ata_chs_error_regs;
|
|
|
|
|
pkt_res_ata_chs->buf_len = pkt_cmd_ata_chs->buf_len;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_ata_chs->duration = htole32(duration);
|
|
|
|
|
pkt_res_ata_chs->sense = htole32(sense);
|
|
|
|
|
pkt_res_ata_chs->error_no = htole32(ret);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_ata_chs, le32toh(pkt_res_ata_chs->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_cmd_ata_chs);
|
|
|
|
|
free(pkt_res_ata_chs);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_ATA_LBA_28:
|
|
|
|
|
// Packet contains data after
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_cmd_ata_lba28 = (DicPacketCmdAtaLba28*)in_buf;
|
|
|
|
|
|
|
|
|
|
// TODO: Check size of buffers + size of packet is not bigger than size in header
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(le32toh(pkt_cmd_ata_lba28->buf_len) > 0) buffer = in_buf + sizeof(DicPacketCmdAtaLba28);
|
2019-10-20 21:50:25 +01:00
|
|
|
else
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
|
|
memset(&ata_lba28_error_regs, 0, sizeof(AtaErrorRegistersLba28));
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_cmd_ata_lba28->buf_len = le32toh(pkt_cmd_ata_lba28->buf_len);
|
|
|
|
|
|
2019-10-20 21:50:25 +01:00
|
|
|
duration = 0;
|
|
|
|
|
sense = 1;
|
2019-10-26 16:11:19 +01:00
|
|
|
ret = SendAtaLba28Command(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_ata_lba28->registers,
|
|
|
|
|
&ata_lba28_error_regs,
|
|
|
|
|
pkt_cmd_ata_lba28->protocol,
|
|
|
|
|
pkt_cmd_ata_lba28->transfer_register,
|
|
|
|
|
buffer,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_ata_lba28->timeout),
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_ata_lba28->transfer_blocks,
|
|
|
|
|
&duration,
|
|
|
|
|
&sense,
|
|
|
|
|
&pkt_cmd_ata_lba28->buf_len);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
out_buf = malloc(sizeof(DicPacketResAtaLba28) + pkt_cmd_ata_lba28->buf_len);
|
|
|
|
|
pkt_cmd_ata_lba28->buf_len = htole32(pkt_cmd_ata_lba28->buf_len);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!out_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, continuing...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
|
|
|
|
free(in_buf);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_res_ata_lba28 = (DicPacketResAtaLba28*)out_buf;
|
2019-10-22 00:44:39 +01:00
|
|
|
if(buffer)
|
|
|
|
|
memcpy(out_buf + sizeof(DicPacketResAtaLba28), buffer, le32toh(pkt_cmd_ata_lba28->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_ata_lba28->hdr.len =
|
|
|
|
|
htole32(sizeof(DicPacketResAtaLba28) + le32toh(pkt_cmd_ata_lba28->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_ata_lba28->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_ATA_LBA_28;
|
|
|
|
|
pkt_res_ata_lba28->hdr.version = DICMOTE_PACKET_VERSION;
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_ata_lba28->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_ata_lba28->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_res_ata_lba28->registers = ata_lba28_error_regs;
|
|
|
|
|
pkt_res_ata_lba28->buf_len = pkt_cmd_ata_lba28->buf_len;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_ata_lba28->duration = le32toh(duration);
|
|
|
|
|
pkt_res_ata_lba28->sense = le32toh(sense);
|
|
|
|
|
pkt_res_ata_lba28->error_no = le32toh(ret);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_ata_lba28, le32toh(pkt_res_ata_lba28->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_cmd_ata_lba28);
|
|
|
|
|
free(pkt_res_ata_lba28);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_ATA_LBA_48:
|
|
|
|
|
// Packet contains data after
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_cmd_ata_lba48 = (DicPacketCmdAtaLba48*)in_buf;
|
|
|
|
|
|
|
|
|
|
// TODO: Check size of buffers + size of packet is not bigger than size in header
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(le32toh(pkt_cmd_ata_lba48->buf_len) > 0) buffer = in_buf + sizeof(DicPacketCmdAtaLba48);
|
2019-10-20 21:50:25 +01:00
|
|
|
else
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
|
|
memset(&ata_lba48_error_regs, 0, sizeof(AtaErrorRegistersLba48));
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_cmd_ata_lba48->buf_len = le32toh(pkt_cmd_ata_lba48->buf_len);
|
|
|
|
|
|
|
|
|
|
// Swapping
|
|
|
|
|
pkt_cmd_ata_lba48->registers.lba_high = le16toh(pkt_cmd_ata_lba48->registers.lba_high);
|
|
|
|
|
pkt_cmd_ata_lba48->registers.lba_mid = le16toh(pkt_cmd_ata_lba48->registers.lba_mid);
|
|
|
|
|
pkt_cmd_ata_lba48->registers.lba_low = le16toh(pkt_cmd_ata_lba48->registers.lba_low);
|
|
|
|
|
pkt_cmd_ata_lba48->registers.sector_count = le16toh(pkt_cmd_ata_lba48->registers.sector_count);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = 1;
|
2019-10-26 16:11:19 +01:00
|
|
|
ret = SendAtaLba48Command(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_ata_lba48->registers,
|
|
|
|
|
&ata_lba48_error_regs,
|
|
|
|
|
pkt_cmd_ata_lba48->protocol,
|
|
|
|
|
pkt_cmd_ata_lba48->transfer_register,
|
|
|
|
|
buffer,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_ata_lba48->timeout),
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_ata_lba48->transfer_blocks,
|
|
|
|
|
&duration,
|
|
|
|
|
&sense,
|
|
|
|
|
&pkt_cmd_ata_lba48->buf_len);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
out_buf = malloc(sizeof(DicPacketResAtaLba48) + pkt_cmd_ata_lba48->buf_len);
|
|
|
|
|
pkt_cmd_ata_lba48->buf_len = htole32(pkt_cmd_ata_lba48->buf_len);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!out_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, continuing...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
|
|
|
|
free(in_buf);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_res_ata_lba48 = (DicPacketResAtaLba48*)out_buf;
|
2019-10-22 00:44:39 +01:00
|
|
|
if(buffer)
|
|
|
|
|
memcpy(out_buf + sizeof(DicPacketResAtaLba48), buffer, le32toh(pkt_cmd_ata_lba48->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_ata_lba48->hdr.len =
|
|
|
|
|
htole32(sizeof(DicPacketResAtaLba48) + le32toh(pkt_cmd_ata_lba48->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_ata_lba48->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_ATA_LBA_48;
|
|
|
|
|
pkt_res_ata_lba48->hdr.version = DICMOTE_PACKET_VERSION;
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_ata_lba48->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_ata_lba48->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-22 00:44:39 +01:00
|
|
|
|
|
|
|
|
// Swapping
|
|
|
|
|
ata_lba48_error_regs.lba_high = htole16(ata_lba48_error_regs.lba_high);
|
|
|
|
|
ata_lba48_error_regs.lba_mid = htole16(ata_lba48_error_regs.lba_mid);
|
|
|
|
|
ata_lba48_error_regs.lba_low = htole16(ata_lba48_error_regs.lba_low);
|
|
|
|
|
ata_lba48_error_regs.sector_count = htole16(ata_lba48_error_regs.sector_count);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_res_ata_lba48->registers = ata_lba48_error_regs;
|
|
|
|
|
pkt_res_ata_lba48->buf_len = pkt_cmd_ata_lba48->buf_len;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_ata_lba48->duration = le32toh(duration);
|
|
|
|
|
pkt_res_ata_lba48->sense = le32toh(sense);
|
|
|
|
|
pkt_res_ata_lba48->error_no = le32toh(ret);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_ata_lba48, le32toh(pkt_res_ata_lba48->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_cmd_ata_lba48);
|
|
|
|
|
free(pkt_res_ata_lba48);
|
|
|
|
|
continue;
|
|
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_SDHCI:
|
|
|
|
|
// Packet contains data after
|
2019-10-22 00:44:39 +01:00
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
pkt_cmd_sdhci = (DicPacketCmdSdhci*)in_buf;
|
|
|
|
|
|
|
|
|
|
// TODO: Check size of buffers + size of packet is not bigger than size in header
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
if(le32toh(pkt_cmd_sdhci->buf_len) > 0) buffer = in_buf + sizeof(DicPacketCmdSdhci);
|
2019-10-20 21:50:25 +01:00
|
|
|
else
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
|
|
memset((char*)&sdhci_response, 0, sizeof(uint32_t) * 4);
|
|
|
|
|
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = 1;
|
2019-10-26 16:11:19 +01:00
|
|
|
ret = SendSdhciCommand(device_ctx,
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_cmd_sdhci->command,
|
|
|
|
|
pkt_cmd_sdhci->write,
|
|
|
|
|
pkt_cmd_sdhci->application,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_sdhci->flags),
|
|
|
|
|
le32toh(pkt_cmd_sdhci->argument),
|
|
|
|
|
le32toh(pkt_cmd_sdhci->block_size),
|
|
|
|
|
le32toh(pkt_cmd_sdhci->blocks),
|
2019-10-20 21:50:25 +01:00
|
|
|
buffer,
|
2019-10-22 00:44:39 +01:00
|
|
|
le32toh(pkt_cmd_sdhci->buf_len),
|
|
|
|
|
le32toh(pkt_cmd_sdhci->timeout),
|
2019-10-20 21:50:25 +01:00
|
|
|
(uint32_t*)&sdhci_response,
|
|
|
|
|
&duration,
|
|
|
|
|
&sense);
|
|
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
out_buf = malloc(sizeof(DicPacketResSdhci) + le32toh(pkt_cmd_sdhci->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
if(!out_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, continuing...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
|
|
|
|
free(in_buf);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-20 21:50:25 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt_res_sdhci = (DicPacketResSdhci*)out_buf;
|
2019-10-22 00:44:39 +01:00
|
|
|
if(buffer) memcpy(out_buf + sizeof(DicPacketResSdhci), buffer, le32toh(pkt_cmd_sdhci->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_sdhci->hdr.len = htole32(sizeof(DicPacketResSdhci) + le32toh(pkt_cmd_sdhci->buf_len));
|
2019-10-20 21:50:25 +01:00
|
|
|
pkt_res_sdhci->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_SDHCI;
|
|
|
|
|
pkt_res_sdhci->hdr.version = DICMOTE_PACKET_VERSION;
|
2019-10-26 15:34:28 +01:00
|
|
|
pkt_res_sdhci->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_sdhci->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
2019-10-22 00:44:39 +01:00
|
|
|
|
|
|
|
|
sdhci_response[0] = htole32(sdhci_response[0]);
|
|
|
|
|
sdhci_response[1] = htole32(sdhci_response[1]);
|
|
|
|
|
sdhci_response[2] = htole32(sdhci_response[2]);
|
|
|
|
|
sdhci_response[3] = htole32(sdhci_response[3]);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
|
|
|
|
memcpy((char*)&pkt_res_sdhci->response, (char*)&sdhci_response, sizeof(uint32_t) * 4);
|
|
|
|
|
pkt_res_sdhci->buf_len = pkt_cmd_sdhci->buf_len;
|
2019-10-22 00:44:39 +01:00
|
|
|
pkt_res_sdhci->duration = htole32(duration);
|
|
|
|
|
pkt_res_sdhci->sense = htole32(sense);
|
|
|
|
|
pkt_res_sdhci->error_no = htole32(ret);
|
2019-10-20 21:50:25 +01:00
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_sdhci, le32toh(pkt_res_sdhci->hdr.len));
|
2019-10-20 21:50:25 +01:00
|
|
|
free(pkt_cmd_sdhci);
|
|
|
|
|
free(pkt_res_sdhci);
|
|
|
|
|
continue;
|
2019-10-26 15:47:49 +01:00
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_CLOSE_DEVICE:
|
2019-10-26 16:11:19 +01:00
|
|
|
DeviceClose(device_ctx);
|
|
|
|
|
device_ctx = NULL;
|
2019-10-26 15:47:49 +01:00
|
|
|
skip_next_hdr = 1;
|
|
|
|
|
continue;
|
2019-10-26 17:32:35 +01:00
|
|
|
case DICMOTE_PACKET_TYPE_COMMAND_AM_I_ROOT:
|
|
|
|
|
// Packet only contains header so, dummy
|
|
|
|
|
in_buf = malloc(le32toh(pkt_hdr->len));
|
|
|
|
|
|
|
|
|
|
if(!in_buf)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-26 17:32:35 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetRecv(cli_ctx, in_buf, le32toh(pkt_hdr->len), 0);
|
2019-10-26 17:32:35 +01:00
|
|
|
free(in_buf);
|
|
|
|
|
|
|
|
|
|
pkt_res_am_i_root = malloc(sizeof(DicPacketResAmIRoot));
|
|
|
|
|
if(!pkt_res_am_i_root)
|
|
|
|
|
{
|
|
|
|
|
printf("Fatal error %d allocating memory for packet, closing connection...\n", errno);
|
|
|
|
|
free(pkt_hdr);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetClose(cli_ctx);
|
2019-10-26 17:32:35 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pkt_res_am_i_root, 0, sizeof(DicPacketResAmIRoot));
|
|
|
|
|
pkt_res_am_i_root->hdr.remote_id = htole32(DICMOTE_REMOTE_ID);
|
|
|
|
|
pkt_res_am_i_root->hdr.packet_id = htole32(DICMOTE_PACKET_ID);
|
|
|
|
|
pkt_res_am_i_root->hdr.version = DICMOTE_PACKET_VERSION;
|
|
|
|
|
pkt_res_am_i_root->hdr.packet_type = DICMOTE_PACKET_TYPE_RESPONSE_AM_I_ROOT;
|
|
|
|
|
pkt_res_am_i_root->hdr.len = htole32(sizeof(DicPacketResAmIRoot));
|
|
|
|
|
pkt_res_am_i_root->am_i_root = AmIRoot();
|
|
|
|
|
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_res_am_i_root, le32toh(pkt_res_am_i_root->hdr.len));
|
2019-10-26 17:32:35 +01:00
|
|
|
free(pkt_res_am_i_root);
|
|
|
|
|
continue;
|
2019-10-20 21:50:25 +01:00
|
|
|
default:
|
|
|
|
|
pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_NOT_RECOGNIZED;
|
|
|
|
|
memset(&pkt_nop->reason, 0, 256);
|
|
|
|
|
snprintf(pkt_nop->reason,
|
|
|
|
|
256,
|
|
|
|
|
"Received unrecognized packet with type %d, skipping...",
|
|
|
|
|
pkt_hdr->packet_type);
|
2019-10-26 22:22:04 +01:00
|
|
|
NetWrite(cli_ctx, pkt_nop, sizeof(DicPacketNop));
|
2019-10-20 21:50:25 +01:00
|
|
|
printf("%s...\n", pkt_nop->reason);
|
|
|
|
|
skip_next_hdr = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|