Skip to content

Commit

Permalink
dmnt edition
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvita committed Aug 26, 2020
1 parent 3c19b0c commit 6d8b135
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 15 deletions.
Binary file modified server/source/noexs/exefs.nsp
Binary file not shown.
65 changes: 65 additions & 0 deletions server/source/noexs/include/service_guard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <switch/types.h>
#include <switch/result.h>
#include <switch/kernel/mutex.h>
#include <switch/sf/service.h>
#include <switch/services/sm.h>

typedef struct ServiceGuard {
Mutex mutex;
u32 refCount;
} ServiceGuard;

NX_INLINE bool serviceGuardBeginInit(ServiceGuard* g)
{
mutexLock(&g->mutex);
return (g->refCount++) == 0;
}

NX_INLINE Result serviceGuardEndInit(ServiceGuard* g, Result rc, void (*cleanupFunc)(void))
{
if (R_FAILED(rc)) {
cleanupFunc();
--g->refCount;
}
mutexUnlock(&g->mutex);
return rc;
}

NX_INLINE void serviceGuardExit(ServiceGuard* g, void (*cleanupFunc)(void))
{
mutexLock(&g->mutex);
if (g->refCount && (--g->refCount) == 0)
cleanupFunc();
mutexUnlock(&g->mutex);
}

#define NX_GENERATE_SERVICE_GUARD_PARAMS(name, _paramdecl, _parampass) \
\
static ServiceGuard g_##name##Guard; \
NX_INLINE Result _##name##Initialize _paramdecl; \
static void _##name##Cleanup(void); \
\
Result name##Initialize _paramdecl \
{ \
Result rc = 0; \
if (serviceGuardBeginInit(&g_##name##Guard)) \
rc = _##name##Initialize _parampass; \
return serviceGuardEndInit(&g_##name##Guard, rc, _##name##Cleanup); \
} \
\
void name##Exit(void) \
{ \
serviceGuardExit(&g_##name##Guard, _##name##Cleanup); \
}

#define NX_GENERATE_SERVICE_GUARD(name) NX_GENERATE_SERVICE_GUARD_PARAMS(name, (void), ())

#ifdef __cplusplus
}
#endif
Binary file modified server/source/noexs/noexs.nso
Binary file not shown.
54 changes: 39 additions & 15 deletions server/source/noexs/source/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if( i < 0) \
return MAKERESULT(Module_TCPGecko, TCPGeckoError_iofail); \
}

static bool dmnt = false;
Result writeCompressed(Gecko::Context& ctx, u32 len) {
static u8 tmp[GECKO_BUFFER_SIZE * 2];
u32 pos = 0;
Expand Down Expand Up @@ -68,7 +68,8 @@ static Result _poke8(Gecko::Context& ctx){
u8 val;
READ_CHECKED(ctx, ptr);
READ_CHECKED(ctx, val);
return ctx.dbg.writeMem(val, ptr);
if (dmnt) return dmntchtWriteCheatProcessMemory(ptr, &val, 1);
else return ctx.dbg.writeMem(val, ptr);
}

//0x03
Expand All @@ -77,7 +78,8 @@ static Result _poke16(Gecko::Context& ctx){
u16 val;
READ_CHECKED(ctx, ptr);
READ_CHECKED(ctx, val);
return ctx.dbg.writeMem(val, ptr);
if (dmnt) return dmntchtWriteCheatProcessMemory(ptr, &val, 2);
else return ctx.dbg.writeMem(val, ptr);
}

//0x04
Expand All @@ -86,7 +88,8 @@ static Result _poke32(Gecko::Context& ctx){
u32 val;
READ_CHECKED(ctx, ptr);
READ_CHECKED(ctx, val);
return ctx.dbg.writeMem(val, ptr);
if (dmnt) return dmntchtWriteCheatProcessMemory(ptr, &val, 4);
else return ctx.dbg.writeMem(val, ptr);
}

//0x05
Expand All @@ -95,7 +98,8 @@ static Result _poke64(Gecko::Context& ctx){
u64 val;
READ_CHECKED(ctx, ptr);
READ_CHECKED(ctx, val);
return ctx.dbg.writeMem(val, ptr);
if (dmnt) return dmntchtWriteCheatProcessMemory(ptr, &val, 8);
else return ctx.dbg.writeMem(val, ptr);
}

//0x06
Expand All @@ -104,15 +108,18 @@ static Result _readmem(Gecko::Context& ctx){
u64 addr;
u32 size;
u32 len;
bool* out = nullptr;

READ_CHECKED(ctx, addr);
READ_CHECKED(ctx, size);
rc = ctx.dbg.attached();
if (dmnt) rc = dmntchtHasCheatProcess(out);
else rc = ctx.dbg.attached();
WRITE_CHECKED(ctx, rc);
if(R_SUCCEEDED(rc)){
while(size > 0){
len = size < GECKO_BUFFER_SIZE ? size : GECKO_BUFFER_SIZE;
rc = ctx.dbg.readMem(ctx.buffer, addr, len);
if (dmnt) rc = dmntchtReadCheatProcessMemory(addr, ctx.buffer, len);
else rc = ctx.dbg.readMem(ctx.buffer, addr, len);
WRITE_CHECKED(ctx, rc);

if(R_FAILED(rc)){
Expand All @@ -135,15 +142,18 @@ static Result _writemem(Gecko::Context& ctx){
u32 size;
u32 len;
Result rc = 0;
bool * out = nullptr;
READ_CHECKED(ctx, addr);
READ_CHECKED(ctx, size);
rc = ctx.dbg.attached();
if (dmnt) rc = dmntchtHasCheatProcess(out);
else rc = ctx.dbg.attached();
WRITE_CHECKED(ctx, rc);
if(R_SUCCEEDED(rc)){
while(size > 0){
len = size < GECKO_BUFFER_SIZE ? size : GECKO_BUFFER_SIZE;
READ_BUFFER_CHECKED(ctx, ctx.buffer, len);
ctx.dbg.writeMem(ctx.buffer, addr, len);
if (dmnt) rc = dmntchtWriteCheatProcessMemory(addr, ctx.buffer, len);
else ctx.dbg.writeMem(ctx.buffer, addr, len);
addr += len;
size -= len;
}
Expand All @@ -153,7 +163,9 @@ static Result _writemem(Gecko::Context& ctx){

//0x08
static Result _resume(Gecko::Context& ctx){
Result rc = ctx.dbg.resume();
Result rc;
if (dmnt) rc = dmntchtResumeCheatProcess();
else rc = ctx.dbg.resume();
if(R_SUCCEEDED(rc)){
ctx.status = Gecko::Status::Running;
}
Expand All @@ -162,7 +174,9 @@ static Result _resume(Gecko::Context& ctx){

//0x09
static Result _pause(Gecko::Context& ctx){
Result rc = ctx.dbg.pause();
Result rc;
if (dmnt) rc = dmntchtPauseCheatProcess();
else rc = ctx.dbg.pause();
if(R_SUCCEEDED(rc)){
ctx.status = Gecko::Status::Paused;
}
Expand All @@ -176,6 +190,11 @@ static Result _attach(Gecko::Context& ctx){
Result rc = ctx.dbg.attach(pid);
if(R_SUCCEEDED(rc)){
ctx.status = Gecko::Status::Paused;
} else {
dmnt = true;
dmntchtInitialize();
rc = dmntchtPauseCheatProcess();
ctx.status = Gecko::Status::Paused;
}
return rc;
}
Expand All @@ -197,7 +216,8 @@ static Result _querymem_single(Gecko::Context& ctx){

READ_CHECKED(ctx, addr);

rc = ctx.dbg.query(&info, addr);
if (dmnt) rc = dmntchtQueryCheatProcessMemory(&info, addr);
else rc = ctx.dbg.query(&info, addr);

WRITE_CHECKED(ctx, info.addr);
WRITE_CHECKED(ctx, info.size);
Expand All @@ -217,7 +237,8 @@ static Result _querymem_multi(Gecko::Context& ctx) {
READ_CHECKED(ctx, requestCount);

for(count = 0; count < requestCount; count++){
rc =ctx.dbg.query(&info, addr);
if (dmnt) rc = dmntchtQueryCheatProcessMemory(&info, addr);
else rc =ctx.dbg.query(&info, addr);
WRITE_CHECKED(ctx, info.addr);
WRITE_CHECKED(ctx, info.size);
WRITE_CHECKED(ctx, info.type);
Expand Down Expand Up @@ -286,9 +307,11 @@ static Result _readmem_multi(Gecko::Context& ctx){
u32 data_size;
u32 count = 0;
u64 addr;
bool* out = nullptr;
READ_CHECKED(ctx, size);
READ_CHECKED(ctx, data_size);
rc = ctx.dbg.attached();
if (dmnt) rc = dmntchtHasCheatProcess(out);
else rc = ctx.dbg.attached();
if(R_SUCCEEDED(rc)){
if(data_size > GECKO_BUFFER_SIZE){
rc = MAKERESULT(Module_TCPGecko, TCPGeckoError_buffer_too_small);
Expand All @@ -298,7 +321,8 @@ static Result _readmem_multi(Gecko::Context& ctx){
if(R_SUCCEEDED(rc)){
for(count = 0; count < size; count++){
READ_CHECKED(ctx, addr);
rc = ctx.dbg.readMem(ctx.buffer, addr, data_size);
if (dmnt) rc = dmntchtReadCheatProcessMemory(addr, ctx.buffer, data_size);
else rc = ctx.dbg.readMem(ctx.buffer, addr, data_size);
WRITE_CHECKED(ctx, rc);
if(R_FAILED(rc)){
break;
Expand Down
Loading

0 comments on commit 6d8b135

Please sign in to comment.