From 74c102ed3ff07c1fe9597680d484d99eeaf22d78 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Tue, 25 Jun 2019 22:40:49 -0400 Subject: [PATCH] Added hiddbgReadSerialFlash. --- nx/include/switch/services/hiddbg.h | 4 ++ nx/source/services/hiddbg.c | 57 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/nx/include/switch/services/hiddbg.h b/nx/include/switch/services/hiddbg.h index 809711e8..c9bc0eda 100644 --- a/nx/include/switch/services/hiddbg.h +++ b/nx/include/switch/services/hiddbg.h @@ -51,6 +51,10 @@ typedef struct { Result hiddbgInitialize(void); void hiddbgExit(void); +/// Reads spi-flash for the specified controller. See hidsys.h for UniquePadId. +/// This doesn't seem to be usable? +Result hiddbgReadSerialFlash(u32 offset, void* buffer, size_t size, u64 UniquePadId); + /// Initialize Hdls. Hdls is for virtual HID controllers. Only available with [7.0.0+]. Result hiddbgAttachHdlsWorkBuffer(void); diff --git a/nx/source/services/hiddbg.c b/nx/source/services/hiddbg.c index 2aa8b797..0814e55f 100644 --- a/nx/source/services/hiddbg.c +++ b/nx/source/services/hiddbg.c @@ -140,6 +140,63 @@ static Result _hiddbgCmdInU64NoOut(u64 cmd_id, u64 val) { return rc; } +static Result _hiddbgReadSerialFlash(TransferMemory *tmem, u32 offset, u64 size, u64 UniquePadId) { + IpcCommand c; + ipcInitialize(&c); + + ipcSendHandleCopy(&c, tmem->handle); + + struct { + u64 magic; + u64 cmd_id; + u32 offset; + u64 size; + u64 UniquePadId; + } *raw; + + raw = serviceIpcPrepareHeader(&g_hiddbgSrv, &c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 229; + raw->offset = offset; + raw->size = size; + raw->UniquePadId = UniquePadId; + + Result rc = serviceIpcDispatch(&g_hiddbgSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + } *resp; + + serviceIpcParse(&g_hiddbgSrv, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + } + + return rc; +} + +Result hiddbgReadSerialFlash(u32 offset, void* buffer, size_t size, u64 UniquePadId) { + Result rc=0; + TransferMemory tmem; + size_t sizealign = (size+0x1000) & ~0xfff; + + if (hosversionBefore(6,0,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + rc = tmemCreate(&tmem, sizealign, Perm_Rw); + if (R_FAILED(rc)) return rc; + + rc = _hiddbgReadSerialFlash(&tmem, offset, size, UniquePadId); + if (R_SUCCEEDED(rc)) memcpy(buffer, tmem.src_addr, size); + tmemClose(&g_hiddbgHdlsTmem); + return rc; +} + static Result _hiddbgAttachHdlsWorkBuffer(TransferMemory *tmem) { IpcCommand c; ipcInitialize(&c);