|
| 1 | +/* |
| 2 | +* Author: Brendan Heinonen |
| 3 | +*/ |
| 4 | +#include "leechcore.h" |
| 5 | +#include <errno.h> |
| 6 | +#include <fcntl.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <limits.h> |
| 9 | + |
| 10 | +#include <leechcore_device.h> |
| 11 | +#include <unistd.h> |
| 12 | + |
| 13 | + |
| 14 | +static VOID DeviceDevmem_ReadContigious(PLC_READ_CONTIGIOUS_CONTEXT ctxRC) { |
| 15 | + int bytes_read; |
| 16 | + int fd = (intptr_t)ctxRC->ctxLC->hDevice; |
| 17 | + |
| 18 | + lseek(fd, ctxRC->paBase, SEEK_SET); |
| 19 | + if ((bytes_read = read(fd, ctxRC->pb, ctxRC->cb)) < 0) { |
| 20 | + lcprintfvvv(ctxRC->ctxLC, "Failed to read physical memory at 0x%llx (error %d)\n", |
| 21 | + ctxRC->paBase, bytes_read); |
| 22 | + } |
| 23 | + ctxRC->cbRead = (DWORD)bytes_read; |
| 24 | +} |
| 25 | + |
| 26 | +static BOOL DeviceDevmem_WriteContigious(_In_ PLC_CONTEXT ctxLC, |
| 27 | + _In_ QWORD qwAddr, _In_ DWORD cb, |
| 28 | + _In_reads_(cb) PBYTE pb) { |
| 29 | + int bytes_written; |
| 30 | + int fd = (intptr_t)ctxLC->hDevice; |
| 31 | + lseek(fd, qwAddr, SEEK_SET); |
| 32 | + if ((bytes_written = write(fd, pb, cb)) < 0) { |
| 33 | + lcprintfvvv(ctxLC, "Failed to write physical memory at 0x%llx (error %d)\n", |
| 34 | + qwAddr, bytes_written); |
| 35 | + return false; |
| 36 | + } |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +VOID DeviceDevmem_Close(_Inout_ PLC_CONTEXT ctxLC) |
| 41 | +{ |
| 42 | + close((intptr_t)ctxLC->hDevice); |
| 43 | +} |
| 44 | + |
| 45 | +_Success_(return) EXPORTED_FUNCTION |
| 46 | +BOOL LcPluginCreate(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo) |
| 47 | +{ |
| 48 | + int ret = 0; |
| 49 | + PLC_DEVICE_PARAMETER_ENTRY pPathParameter = NULL; |
| 50 | + CHAR szPath[MAX_PATH]; |
| 51 | + int fd; |
| 52 | + |
| 53 | + lcprintf(ctxLC, "DEVICE: devmem: Initializing\n"); |
| 54 | + |
| 55 | + /* Sanity checks */ |
| 56 | + if(ppLcCreateErrorInfo) { *ppLcCreateErrorInfo = NULL; } |
| 57 | + if(ctxLC->version != LC_CONTEXT_VERSION) { return false; } |
| 58 | + |
| 59 | + /* Parse path parameter, or default to /dev/mem */ |
| 60 | + if((pPathParameter = LcDeviceParameterGet(ctxLC, "path"))) { |
| 61 | + strncpy(szPath, pPathParameter->szValue, sizeof(szPath)); |
| 62 | + } else { |
| 63 | + strncpy(szPath, "/dev/mem", sizeof(szPath)); |
| 64 | + } |
| 65 | + |
| 66 | + /* Open the device */ |
| 67 | + fd = open(szPath, O_RDWR | O_SYNC); |
| 68 | + if(fd < 0) { |
| 69 | + lcprintf(ctxLC, "DEVICE: devmem: Failed to open device %s (error %d)\n", szPath, errno); |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + /* Assign info and handles for LeechCore */ |
| 74 | + ctxLC->hDevice = (HANDLE)(intptr_t)fd; |
| 75 | + ctxLC->fMultiThread = false; |
| 76 | + ctxLC->Config.fVolatile = true; |
| 77 | + ctxLC->pfnClose = DeviceDevmem_Close; |
| 78 | + ctxLC->pfnReadContigious = DeviceDevmem_ReadContigious; |
| 79 | + ctxLC->pfnWriteContigious = DeviceDevmem_WriteContigious; |
| 80 | + return true; |
| 81 | +} |
0 commit comments