From 3ffb3d93449a9ffac4718748b94fcc6d713003a3 Mon Sep 17 00:00:00 2001 From: sys64738 Date: Tue, 22 Jun 2021 04:13:35 +0200 Subject: [PATCH] stuff --- .gitignore | 4 + Makefile | 7 ++ dumpee.c | 152 +++++++++++++++++++++++++++++++++++ ee.bin | Bin 0 -> 2048 bytes libftfake.c | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ua.bin | Bin 0 -> 1884 bytes 6 files changed, 385 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 dumpee.c create mode 100644 ee.bin create mode 100644 libftfake.c create mode 100644 ua.bin diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50f6144 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +dumpee +libftfake.so +ftd2xx.h +WinTypes.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..93e05a3 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: libftfake.so dumpee + +libftfake.so: libftfake.c + gcc -shared -fPIC -o "$@" "$<" -I. -ldl -Wall + +dumpee: dumpee.c + gcc -o "$@" "$<" -L../build/ -I. -lftd2xx -Wl,-rpath=../build diff --git a/dumpee.c b/dumpee.c new file mode 100644 index 0000000..b858675 --- /dev/null +++ b/dumpee.c @@ -0,0 +1,152 @@ + +#include +#include +#include +#include +#include + +#include "ftd2xx.h" + +int main(int argc, char* argv[]) { + FT_STATUS stat; + FT_HANDLE ft; + int rv; + + FT_SetVIDPID(0x0403, 0x7fd0); + + stat = FT_Open(0, &ft); + if (stat) {printf("FT_Open failed\n");return 1;} + + FT_DEVICE dev; + static const char* devlut[] = { + "BM","AM","100ax","unk","2232c","232r","2232h","4232h","232h", + "X-series","4222h0", "4222h12","4222h3","4222prog"}; + stat = FT_GetDeviceInfo(ft, &dev, NULL, NULL, NULL, NULL); + if (stat) {printf("getdevinfo failed\n");rv=1;goto finish;} + printf("dev=%d %s\n", dev, devlut[dev]); + + static FT_PROGRAM_DATA pdata; + // "MUST set signature1 and signature2 before calling FF_EE_Read" + pdata.Signature1 = 0; + pdata.Signature2 = 0xffffffff; + pdata.Manufacturer = malloc(256); + pdata.ManufacturerId = malloc(256); + pdata.Description = malloc(256); + pdata.SerialNumber = malloc(256); + + stat = FT_EE_Read(ft, &pdata); + if (stat){printf("FT_EE_Read failed\n");rv=1;goto finish;} + + printf("Signature1 = 0x%08x\n", pdata.Signature1); + printf("Signature2 = 0x%08x\n", pdata.Signature2); + printf("Version = %04x\n", pdata.Version); + printf("VendorId = 0x%04x\n", pdata.VendorId); + printf("ProductId = 0x%04x\n", pdata.ProductId); + + printf("Manufacturer = %s\n", pdata.Manufacturer); + printf("ManufacturerId = %s\n", pdata.ManufacturerId); + printf("Description = %s\n", pdata.Description); + printf("SerialNumber = %s\n", pdata.SerialNumber); + printf("MaxPower = %d\n", pdata.MaxPower); + printf("PnP = %d\n", pdata.PnP); + printf("SelfPowered = %d\n", pdata.SelfPowered); + printf("RemoteWakeup = %d\n", pdata.RemoteWakeup); + + if (dev== FT_DEVICE_BM) + { + /* Rev4 (FT232B) extensions */ + printf("BM:\n"); + printf("---\n"); + printf("\tRev4 = 0x%X\n", pdata.Rev4); + printf("\tIsoIn = 0x%X\n", pdata.IsoIn); + printf("\tIsoOut = 0x%X\n", pdata.IsoOut); + printf("\tPullDownEnable = 0x%X\n", pdata.PullDownEnable); + printf("\tSerNumEnable = 0x%X\n", pdata.SerNumEnable); + printf("\tUSBVersionEnable = 0x%X\n", pdata.USBVersionEnable); + printf("\tUSBVersion = 0x%X\n", pdata.USBVersion); + } + + if (dev== FT_DEVICE_2232C) + { + /* Rev 5 (FT2232C) extensions */ + printf("2232RC:\n"); + printf("-------\n"); + printf("\tRev5 = 0x%X\n", pdata.Rev5); + printf("\tIsoInA = 0x%X\n", pdata.IsoInA); + printf("\tIsoInB = 0x%X\n", pdata.IsoInB); + printf("\tIsoOutA = 0x%X\n", pdata.IsoOutA); + printf("\tIsoOutB = 0x%X\n", pdata.IsoOutB); + printf("\tPullDownEnable5 = 0x%X\n", pdata.PullDownEnable5); + printf("\tSerNumEnable5 = 0x%X\n", pdata.SerNumEnable5); + printf("\tUSBVersionEnable5 = 0x%X\n", pdata.USBVersionEnable5); + printf("\tUSBVersion5 = 0x%X\n", pdata.USBVersion5); + printf("\tAIsHighCurrent = 0x%X\n", pdata.AIsHighCurrent); + printf("\tBIsHighCurrent = 0x%X\n", pdata.BIsHighCurrent); + printf("\tIFAIsFifo = 0x%X\n", pdata.IFAIsFifo); + printf("\tIFAIsFifoTar = 0x%X\n", pdata.IFAIsFifoTar); + printf("\tIFAIsFastSer = 0x%X\n", pdata.IFAIsFastSer); + printf("\tAIsVCP = 0x%X\n", pdata.AIsVCP); + printf("\tIFBIsFifo = 0x%X\n", pdata.IFBIsFifo); + printf("\tIFBIsFifoTar = 0x%X\n", pdata.IFBIsFifoTar); + printf("\tIFBIsFastSer = 0x%X\n", pdata.IFBIsFastSer); + printf("\tBIsVCP = 0x%X\n", pdata.BIsVCP); + } + + if (dev== FT_DEVICE_232R) + { + /* Rev 6 (FT232R) extensions */ + printf("232R:\n"); + printf("-----\n"); + printf("\tUseExtOsc = 0x%X\n", pdata.UseExtOsc); // Use External Oscillator + printf("\tHighDriveIOs = 0x%X\n", pdata.HighDriveIOs); // High Drive I/Os + printf("\tEndpointSize = 0x%X\n", pdata.EndpointSize); // Endpoint size + + printf("\tPullDownEnableR = 0x%X\n", pdata.PullDownEnableR); // non-zero if pull down enabled + printf("\tSerNumEnableR = 0x%X\n", pdata.SerNumEnableR); // non-zero if serial number to be used + + printf("\tInvertTXD = 0x%X\n", pdata.InvertTXD); // non-zero if invert TXD + printf("\tInvertRXD = 0x%X\n", pdata.InvertRXD); // non-zero if invert RXD + printf("\tInvertRTS = 0x%X\n", pdata.InvertRTS); // non-zero if invert RTS + printf("\tInvertCTS = 0x%X\n", pdata.InvertCTS); // non-zero if invert CTS + printf("\tInvertDTR = 0x%X\n", pdata.InvertDTR); // non-zero if invert DTR + printf("\tInvertDSR = 0x%X\n", pdata.InvertDSR); // non-zero if invert DSR + printf("\tInvertDCD = 0x%X\n", pdata.InvertDCD); // non-zero if invert DCD + printf("\tInvertRI = 0x%X\n", pdata.InvertRI); // non-zero if invert RI + + printf("\tCbus0 = 0x%X\n", pdata.Cbus0); // Cbus Mux control + printf("\tCbus1 = 0x%X\n", pdata.Cbus1); // Cbus Mux control + printf("\tCbus2 = 0x%X\n", pdata.Cbus2); // Cbus Mux control + printf("\tCbus3 = 0x%X\n", pdata.Cbus3); // Cbus Mux control + printf("\tCbus4 = 0x%X\n", pdata.Cbus4); // Cbus Mux control + + printf("\tRIsD2XX = 0x%X\n", pdata.RIsD2XX); // non-zero if using D2XX + } + + uint32_t uasz, uard; + stat = FT_EE_UASize(ft, &uasz); + if (stat) {printf("FT_EE_UASize failed\n");rv=1;goto finish;} + printf("UASize = 0x%x\n", uasz); + + uint8_t* data = (uint8_t*)malloc(uasz); + stat = FT_EE_UARead(ft, data, uasz, &uard); + if (stat) {printf("FT_EE_UARead failed\n");rv=1;goto finish;} + if (uasz != uard){printf("FT_EE_UARead bad len\n");rv=1;goto finish;} + + FILE* f = fopen("ua.bin", "wb+"); + fwrite(data, 1, uasz, f); + fclose(f); + + f = fopen("ee.bin", "wb+"); + for (size_t i = 0; ; ++i) { + uint16_t val; + stat = FT_ReadEE(ft, (uint32_t)i, &val); + if (stat) {printf("FT_ReadEE failed at i=%zu (%zu bytes)\n", i, i<<1); break;} + fwrite(&val, 2, 1, f); + } + fclose(f); + +finish: + FT_Close(ft); + return rv; +} + diff --git a/ee.bin b/ee.bin new file mode 100644 index 0000000000000000000000000000000000000000..7ac5612bc1ab5bc7fc9c9a8ca67dd70500151854 GIT binary patch literal 2048 zcmZQzU}m{c&mhonf`fs9VS&&lxeGEt1{|;eSxL+5nCE}}R`P`rE(~XMbikQpF~ZDN zo@8Xm%30fC0MpOt;veW};%ZDXV-99WW=Len1L8o2 zQXrYapui9eWScS=Fvu_)0!ae~V;~0cLC}=Jgu#Nr2xdMKon`lb6pV(zXb6mkz-S1J JhQJ660RUej6~6!g literal 0 HcmV?d00001 diff --git a/libftfake.c b/libftfake.c new file mode 100644 index 0000000..3a712f9 --- /dev/null +++ b/libftfake.c @@ -0,0 +1,222 @@ + +#include +#include +#include +#include + +#include + +#include "ftd2xx.h" + +static void* libftd2xx; +static void lib_open(void) { + static bool opened = false; + if (!opened) { + libftd2xx = dlopen("build/libftd2xx.so.1.4.8", RTLD_LAZY); + opened = libftd2xx != NULL; + if (!opened) { + printf("cant open lib!\n"); + } + } +} + +/* +FT_Close +FT_GetBitMode +FT_GetQueueStatus +FT_ListDevices +FT_Open +FT_OpenEx +FT_Read +FT_SetBitMode +FT_SetLatencyTimer +FT_SetTimeouts +FT_SetUSBParameters +FT_SetVIDPID +FT_Write +FT_ReadEE +FT_WriteEE + +FT_EE_Program +FT_EE_Read +FT_EE_UARead +FT_EE_UAWrite +*/ + +#define bind(rt, fntyp, ...) \ + rt rv = ({ \ + static rt (* fn) fntyp = NULL; \ + if (fn == NULL) { lib_open(); fn = (rt(*)fntyp)dlsym(libftd2xx, __func__); } \ + fn(__VA_ARGS__); \ + }) \ + +/*FT_STATUS FT_GetDeviceInfo(FT_HANDLE ftHandle, FT_DEVICE* ftDevice, + LPDWORD lpdwID, PCHAR serialNumber, PCHAR descr, LPVOID dummy) { + FT_STATUS rv = bind(FT_STATUS, (FT_HANDLE, FT_DEVICE*, LPDWORD, PCHAR, PCHAR, LPVOID), + ftHandle, ftDevice, lpdwID, serialNumber, descr, dummy); + + printf("FT_GetDeviceInfo(%p, &%d, &0x%x, %s, %s, %p) = %d\n", + ftHandle, *ftDevice, *lpdwID, serialNumber, descr, dummy, rv); +}*/ + +FT_STATUS FT_Close(FT_HANDLE ftHandle) { + bind(FT_STATUS, (FT_HANDLE), ftHandle); + printf("FT_Close(%p) = %d\n", ftHandle, rv); + return rv; +} + +FT_STATUS FT_GetBitMode(FT_HANDLE ftHandle, PUCHAR pucMode) { + bind(FT_STATUS, (FT_HANDLE, PUCHAR), ftHandle, pucMode); + printf("FT_GetBitMode(%p, &0x%02x) = %d\n", ftHandle, *pucMode, rv); + return rv; +} + +FT_STATUS FT_GetQueueStatus(FT_HANDLE ftHandle, DWORD* dwRxBytes) { + bind(FT_STATUS, (FT_HANDLE, DWORD*), ftHandle, dwRxBytes); + // responsible for lots of spam + //printf("FT_GetSqueueStatus(%p, &0x%x) = %d\n", ftHandle, *dwRxBytes, rv); + return rv; +} + +FT_STATUS FT_ListDevices(PVOID pArg1, PVOID pArg2, DWORD flags) { + bind(FT_STATUS, (PVOID, PVOID, DWORD), pArg1, pArg2, flags); + return rv; + // possible usages: + // * fn(nulterm array to strings, &numdevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER) + // * fn(null??, charbuf, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); + // * fn(numdev, NULL, FT_LIST_NUMBER_ONLY); + printf("FT_ListDevices(%p, %p, 0x%08x) = %d\n", pArg1, pArg2, flags, rv); + if (flags & FT_LIST_NUMBER_ONLY) { + if (pArg2) printf(" W! pArg2 != null!\n"); + printf("-> #devs = %d\n", *(int*)pArg1); + } else if (flags & FT_LIST_ALL) { + int numdevs = *(int*)pArg2; + const char* descr; + if (flags & FT_OPEN_BY_SERIAL_NUMBER) descr = "serial number"; + else if (flags & FT_OPEN_BY_DESCRIPTION) descr = "description"; + else if (flags & FT_OPEN_BY_LOCATION) descr = "location"; + else descr = "WTF"; + printf("-> #devs = %d, %ss:\n", numdevs, descr); + for (int i = 0; i < numdevs; ++i) { + printf(" [%d] = \"%s\"\n", i, ((char**)pArg1)[i]); + } + } else if (flags & FT_LIST_BY_INDEX) { + printf(" W! FT_LIST_BY_INDEX not impl\n"); + } else printf(" W! unk flags!\n"); + return rv; +} + +FT_STATUS FT_Open(int devno, FT_HANDLE* pHandle) { + bind(FT_STATUS, (int, FT_HANDLE*), devno, pHandle); + printf("FT_Open(%d, &%p) = %d\n", devno, *pHandle, rv); + return rv; +} + +FT_STATUS FT_OpenEx(PVOID pArg1, DWORD flags, FT_HANDLE* pHandle) { + bind(FT_STATUS, (PVOID, DWORD, FT_HANDLE*), pArg1, flags, pHandle); + printf("FT_OpenEx(%p, 0x%08x, &%p) = %d\n", pArg1, flags, *pHandle, rv); + return rv; +} + +FT_STATUS FT_Read(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesReturned) { + bind(FT_STATUS, (FT_HANDLE, LPVOID, DWORD, LPDWORD), + ftHandle, lpBuffer, dwBytesToRead, lpBytesReturned); + return rv; + printf("FT_Read(%p, %p, 0x%x, &0x%x) = %d\n", ftHandle, lpBuffer, dwBytesToRead, *lpBytesReturned, rv); + size_t nb = *lpBytesReturned; + if (nb < 0x100) { + printf(" "); + size_t i; + for (i = 0; i < nb; ++i) { + printf("%02x ", ((uint8_t*)lpBuffer)[i]); + + if ((i & 15) == 15) printf("\n "); + } + if ((i & 15)) printf("\n"); + } + return rv; +} + +FT_STATUS FT_SetBitMode(FT_HANDLE ftHandle, UCHAR ucMask, UCHAR ucEnable) { + bind(FT_STATUS, (FT_HANDLE, UCHAR, UCHAR), ftHandle, ucMask, ucEnable); + printf("FT_SetBitMode(%p, 0x%02x, 0x%02x) = %d\n", ftHandle, ucMask, ucEnable, rv); + return rv; +} + +FT_STATUS FT_SetLatencyTimer(FT_HANDLE ftHandle, UCHAR ucLatency) { + bind(FT_STATUS, (FT_HANDLE, UCHAR), ftHandle, ucLatency); + printf("FT_SetLatencyTimer(%p, 0x%02x) = %d\n", ftHandle, ucLatency, rv); + return rv; +} + +FT_STATUS FT_SetTimeouts(FT_HANDLE ftHandle, ULONG rto, ULONG wto) { + bind(FT_STATUS, (FT_HANDLE, ULONG, ULONG), ftHandle, rto, wto); + printf("FT_SetTimeouts(%p, 0x%08x, 0x%08x) = %d\n", ftHandle, rto, wto, rv); + return rv; +} + +FT_STATUS FT_SetUSBParameters(FT_HANDLE ftHandle, ULONG inxfersz, ULONG outxfersz) { + bind(FT_STATUS, (FT_HANDLE, ULONG, ULONG), ftHandle, inxfersz, outxfersz); + printf("FT_SetUSBParameters(%p, 0x%08x, 0x%08x) = %d\n", ftHandle, inxfersz, outxfersz, rv); + return rv; +} + +FT_STATUS FT_SetVIDPID(DWORD dwVid, DWORD dwPid) { + bind(FT_STATUS, (DWORD, DWORD), dwVid, dwPid); + printf("FT_SetVIDPID(0x%04x, 0x%04x) = %d\n", dwVid, dwPid, rv); + return rv; +} + +FT_STATUS FT_Write(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD len, LPDWORD written) { + bind(FT_STATUS, (FT_HANDLE, LPVOID, DWORD, LPDWORD), ftHandle, lpBuffer, len, written); + printf("FT_Write(%p, %p, 0x%x, &0x%0x) = %d\n", ftHandle, lpBuffer, len, *written, rv); + size_t nb = *written; + if (nb < 0x100) { + printf(" "); + size_t i; + for (i = 0; i < nb; ++i) { + printf("%02x ", ((uint8_t*)lpBuffer)[i]); + + if ((i & 15) == 15) printf("\n "); + } + if ((i & 15)) printf("\n"); + } + return rv; +} + +FT_STATUS FT_ReadEE(FT_HANDLE ftHandle, DWORD off, LPWORD val) { + bind(FT_STATUS, (FT_HANDLE, DWORD, LPWORD), ftHandle, off, val); + printf("FT_ReadEE(%p, 0x%04x, &0x%04x) = %d\n", ftHandle, off, *val, rv); + return rv; +} + +FT_STATUS FT_WriteEE(FT_HANDLE ftHandle, DWORD off, WORD val) { + bind(FT_STATUS, (FT_HANDLE, DWORD, WORD), ftHandle, off, val); + printf("FT_WriteEE(%p, 0x%04x, 0x%04x) = %d\n", ftHandle, off, val, rv); + return rv; +} + +FT_STATUS FT_EE_Program(FT_HANDLE ftHandle, PFT_PROGRAM_DATA pData) { + bind(FT_STATUS, (FT_HANDLE, PFT_PROGRAM_DATA), ftHandle, pData); + printf("FT_EE_Program(%p, %p) = %d\n", ftHandle, pData, rv); + return rv; +} + +FT_STATUS FT_EE_Read(FT_HANDLE ftHandle, PFT_PROGRAM_DATA pData) { + bind(FT_STATUS, (FT_HANDLE, PFT_PROGRAM_DATA), ftHandle, pData); + printf("FT_EE_Read(%p, %p) = %d\n", ftHandle, pData, rv); + return rv; +} + +FT_STATUS FT_EE_UARead(FT_HANDLE ftHandle, PUCHAR pucData, DWORD len, LPDWORD read) { + bind(FT_STATUS, (FT_HANDLE, PUCHAR, DWORD, LPDWORD), ftHandle, pucData, len, read); + printf("FT_EE_UARead(%p, %p, 0x%x, &0x%x) = %d\n", ftHandle, pucData, len, *read, rv); + return rv; +} + +FT_STATUS FT_EE_UAWrite(FT_HANDLE ftHandle, PUCHAR pucData, DWORD len) { + bind(FT_STATUS, (FT_HANDLE, PUCHAR, DWORD), ftHandle, pucData, len); + printf("FT_EE_UAWrite(%p, %p, 0x%x) = %d\n", ftHandle, pucData, len, rv); + return rv; +} + diff --git a/ua.bin b/ua.bin new file mode 100644 index 0000000000000000000000000000000000000000..f9a4da5bf754bcdbd7751e5aae1023364cc6cd90 GIT binary patch literal 1884 tcmYdVUdKHD>$j3Gj0_l{qXR>TB!T~