Skip to content

Commit

Permalink
2.0
Browse files Browse the repository at this point in the history
This mostly adds multi-peek commands but also a few other QOL changes
  • Loading branch information
berichan authored Nov 21, 2021
2 parents cc763c5 + 29e2385 commit e4c86a2
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 27 deletions.
64 changes: 47 additions & 17 deletions sys-botbase/source/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

//Controller:
bool bControllerIsInitialised = false;
HidDeviceType controllerInitializedType = HidDeviceType_FullKey3;
HiddbgHdlsHandle controllerHandle = {0};
HiddbgHdlsDeviceInfo controllerDevice = {0};
HiddbgHdlsState controllerState = {0};
Expand Down Expand Up @@ -43,22 +44,6 @@ void detach(){
svcCloseHandle(debughandle);
}

void detachController()
{
initController();

Result rc = hiddbgDetachHdlsVirtualDevice(controllerHandle);
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgDetachHdlsVirtualDevice: %d\n", rc);
rc = hiddbgReleaseHdlsWorkBuffer(sessionId);
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgReleaseHdlsWorkBuffer: %d\n", rc);
hiddbgExit();
bControllerIsInitialised = false;

sessionId.id = 0;
}

u64 getMainNsoBase(u64 pid){
LoaderModuleInfo proc_modules[2];
s32 numModules = 0;
Expand Down Expand Up @@ -143,7 +128,7 @@ void initController()
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgInitialize: %d\n", rc);
// Set the controller type to Pro-Controller, and set the npadInterfaceType.
controllerDevice.deviceType = HidDeviceType_FullKey3;
controllerDevice.deviceType = controllerInitializedType;
controllerDevice.npadInterfaceType = HidNpadInterfaceType_Bluetooth;
// Set the controller colors. The grip colors are for Pro-Controller on [9.0.0+].
controllerDevice.singleColorBody = RGBA8_MAXALPHA(255,255,255);
Expand All @@ -168,6 +153,22 @@ void initController()
bControllerIsInitialised = true;
}

void detachController()
{
initController();

Result rc = hiddbgDetachHdlsVirtualDevice(controllerHandle);
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgDetachHdlsVirtualDevice: %d\n", rc);
rc = hiddbgReleaseHdlsWorkBuffer(sessionId);
if (R_FAILED(rc) && debugResultCodes)
printf("hiddbgReleaseHdlsWorkBuffer: %d\n", rc);
hiddbgExit();
bControllerIsInitialised = false;

sessionId.id = 0;
}

void poke(u64 offset, u64 size, u8* val)
{
attach();
Expand Down Expand Up @@ -198,6 +199,31 @@ void peek(u64 offset, u64 size)
free(out);
}

void peekMulti(u64* offset, u64* size, u64 count)
{
u64 totalSize = 0;
for (int i = 0; i < count; i++)
totalSize += size[i];

u8 *out = malloc(sizeof(u8) * totalSize);
u64 ofs = 0;
attach();
for (int i = 0; i < count; i++)
{
readMem(out + ofs, offset[i], size[i]);
ofs += size[i];
}
detach();

u64 i;
for (i = 0; i < totalSize; i++)
{
printf("%02X", out[i]);
}
printf("\n");
free(out);
}

void readMem(u8* out, u64 offset, u64 size)
{
Result rc = svcReadDebugProcessMemory(out, debughandle, offset, size);
Expand All @@ -212,6 +238,7 @@ void click(HidNpadButton btn)
svcSleepThread(buttonClickSleepTime * 1e+6L);
release(btn);
}

void press(HidNpadButton btn)
{
initController();
Expand Down Expand Up @@ -274,6 +301,9 @@ u64 followMainPointer(s64* jumps, size_t count)
{
readMem(out, offset + jumps[i], size);
offset = *(u64*)out;
// this traversal resulted in an error
if (offset == 0)
break;
}
detach();
free(out);
Expand Down
2 changes: 2 additions & 0 deletions sys-botbase/source/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

extern Handle debughandle;
extern bool bControllerIsInitialised;
extern HidDeviceType controllerInitializedType;
extern HiddbgHdlsHandle controllerHandle;
extern HiddbgHdlsDeviceInfo controllerDevice;
extern HiddbgHdlsState controllerState;
Expand Down Expand Up @@ -47,6 +48,7 @@ MetaData getMetaData(void);
void poke(u64 offset, u64 size, u8* val);
void writeMem(u64 offset, u64 size, u8* val);
void peek(u64 offset, u64 size);
void peekMulti(u64* offset, u64* size, u64 count);
void readMem(u8* out, u64 offset, u64 size);
void click(HidNpadButton btn);
void press(HidNpadButton btn);
Expand Down
142 changes: 132 additions & 10 deletions sys-botbase/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <poll.h>

#define TITLE_ID 0x430000000000000B
#define HEAP_SIZE 0x001000000
#define THREAD_SIZE 0x20000
#define HEAP_SIZE 0x00C00000
#define THREAD_SIZE 0x1A000

typedef enum {
Active = 0,
Expand All @@ -38,7 +38,7 @@ Mutex freezeMutex, touchMutex, keyMutex, clickMutex;

// events for releasing or idling threads
FreezeThreadState freeze_thr_state = Active;
u8 clickThreadState = 0;
u8 clickThreadState = 0; // 1 = break thread
// key and touch events currently being processed
KeyData currentKeyEvent = {0};
TouchData currentTouchEvent = {0};
Expand Down Expand Up @@ -107,7 +107,7 @@ void __appInit(void)
rc = viInitialize(ViServiceType_Default);
if (R_FAILED(rc))
fatalThrow(rc);
rc = psmInitialize();
rc = lblInitialize();
if (R_FAILED(rc))
fatalThrow(rc);
}
Expand All @@ -121,7 +121,7 @@ void __appExit(void)
timeExit();
socketExit();
viExit();
psmExit();
lblExit();
}

u64 mainLoopSleepTime = 50;
Expand Down Expand Up @@ -178,6 +178,25 @@ int argmain(int argc, char **argv)
peek(meta.heap_base + offset, size);
}

if (!strcmp(argv[0], "peekMulti"))
{
if(argc < 3 || argc % 2 == 0)
return 0;

MetaData meta = getMetaData();

u64 itemCount = (argc - 1)/2;
u64 offsets[itemCount];
u64 sizes[itemCount];

for (int i = 0; i < itemCount; ++i)
{
offsets[i] = meta.heap_base + parseStringToInt(argv[(i*2)+1]);
sizes[i] = parseStringToInt(argv[(i*2)+2]);
}
peekMulti(offsets, sizes, itemCount);
}

if (!strcmp(argv[0], "peekAbsolute"))
{
if(argc != 3)
Expand All @@ -188,6 +207,23 @@ int argmain(int argc, char **argv)
peek(offset, size);
}

if (!strcmp(argv[0], "peekAbsoluteMulti"))
{
if(argc < 3 || argc % 2 == 0)
return 0;

u64 itemCount = (argc - 1)/2;
u64 offsets[itemCount];
u64 sizes[itemCount];

for (int i = 0; i < itemCount; ++i)
{
offsets[i] = parseStringToInt(argv[(i*2)+1]);
sizes[i] = parseStringToInt(argv[(i*2)+2]);
}
peekMulti(offsets, sizes, itemCount);
}

if (!strcmp(argv[0], "peekMain"))
{
if(argc != 3)
Expand All @@ -200,6 +236,25 @@ int argmain(int argc, char **argv)
peek(meta.main_nso_base + offset, size);
}

if (!strcmp(argv[0], "peekMainMulti"))
{
if(argc < 3 || argc % 2 == 0)
return 0;

MetaData meta = getMetaData();

u64 itemCount = (argc - 1)/2;
u64 offsets[itemCount];
u64 sizes[itemCount];

for (int i = 0; i < itemCount; ++i)
{
offsets[i] = meta.main_nso_base + parseStringToInt(argv[(i*2)+1]);
sizes[i] = parseStringToInt(argv[(i*2)+2]);
}
peekMulti(offsets, sizes, itemCount);
}

//poke <address in hex or dec> <data in hex or dec>
if (!strcmp(argv[0], "poke"))
{
Expand Down Expand Up @@ -359,6 +414,12 @@ int argmain(int argc, char **argv)
u64 fFreezeRate = parseStringToInt(argv[2]);
freezeRate = fFreezeRate;
}

if(!strcmp(argv[1], "controllerType")){
detachController();
u8 fControllerType = (u8)parseStringToInt(argv[2]);
controllerInitializedType = fControllerType;
}
}

if(!strcmp(argv[0], "getTitleID")){
Expand Down Expand Up @@ -414,7 +475,7 @@ int argmain(int argc, char **argv)
}

if(!strcmp(argv[0], "getVersion")){
printf("1.9\n");
printf("2.0\n");
}

// follow pointers and print absolute offset (little endian, flip it yourself if required)
Expand Down Expand Up @@ -442,7 +503,8 @@ int argmain(int argc, char **argv)
for (int i = 1; i < argc-1; i++)
jumps[i-1] = parseStringToSignedLong(argv[i]);
u64 solved = followMainPointer(jumps, count);
solved += finalJump;
if (solved != 0)
solved += finalJump;
printf("%016lX\n", solved);
}

Expand All @@ -458,13 +520,17 @@ int argmain(int argc, char **argv)
for (int i = 1; i < argc-1; i++)
jumps[i-1] = parseStringToSignedLong(argv[i]);
u64 solved = followMainPointer(jumps, count);
solved += finalJump;
MetaData meta = getMetaData();
solved -= meta.heap_base;
if (solved != 0)
{
solved += finalJump;
MetaData meta = getMetaData();
solved -= meta.heap_base;
}
printf("%016lX\n", solved);
}

// pointerPeek <amount of bytes in hex or dec> <first (main) jump> <additional jumps> <final jump in pointerexpr>
// warning: no validation
if (!strcmp(argv[0], "pointerPeek"))
{
if(argc < 4)
Expand All @@ -481,7 +547,57 @@ int argmain(int argc, char **argv)
peek(solved, size);
}

// pointerPeekMulti <amount of bytes in hex or dec> <first (main) jump> <additional jumps> <final jump in pointerexpr> split by asterisks (*)
// warning: no validation
if (!strcmp(argv[0], "pointerPeekMulti"))
{
if(argc < 4)
return 0;

// we guess a max of 40 for now
u64 offsets[40];
u64 sizes[40];
u64 itemCount = 0;

u64 currIndex = 1;
u64 lastIndex = 1;

while (currIndex < argc)
{
// count first
char* thisArg = argv[currIndex];
while (strcmp(thisArg, "*"))
{
currIndex++;
if (currIndex < argc)
thisArg = argv[currIndex];
else
break;
}

u64 thisCount = currIndex - lastIndex;

s64 finalJump = parseStringToSignedLong(argv[currIndex-1]);
u64 size = parseStringToSignedLong(argv[lastIndex]);
u64 count = thisCount - 2;
s64 jumps[count];
for (int i = 1; i < count+1; i++)
jumps[i-1] = parseStringToSignedLong(argv[i+lastIndex]);
u64 solved = followMainPointer(jumps, count);
solved += finalJump;

offsets[itemCount] = solved;
sizes[itemCount] = size;
itemCount++;
currIndex++;
lastIndex = currIndex;
}

peekMulti(offsets, sizes, itemCount);
}

// pointerPoke <data to be sent> <first (main) jump> <additional jumps> <final jump in pointerexpr>
// warning: no validation
if (!strcmp(argv[0], "pointerPoke"))
{
if(argc < 4)
Expand Down Expand Up @@ -673,6 +789,7 @@ int argmain(int argc, char **argv)
rc = viSetDisplayPowerState(&temp_display, ViPowerState_NotScanning); // not scanning keeps the screen on but does not push new pixels to the display. Battery save is non-negligible and should be used where possible
svcSleepThread(1e+6l);
viCloseDisplay(&temp_display);
lblSwitchBacklightOff(1ul);
}
}

Expand All @@ -688,14 +805,19 @@ int argmain(int argc, char **argv)
rc = viSetDisplayPowerState(&temp_display, ViPowerState_On);
svcSleepThread(1e+6l);
viCloseDisplay(&temp_display);
lblSwitchBacklightOn(1ul);
}
}

if (!strcmp(argv[0], "charge"))
{
u32 charge;
Result rc = psmInitialize();
if (R_FAILED(rc))
fatalThrow(rc);
psmGetBatteryChargePercentage(&charge);
printf("%d\n", charge);
psmExit();
}

return 0;
Expand Down

0 comments on commit e4c86a2

Please sign in to comment.