Skip to content

Commit

Permalink
Minor refactor, make WiFi default if no config file is present, fix p…
Browse files Browse the repository at this point in the history
…ointer reads (thanks, Anubis!).
  • Loading branch information
Koi-3088 committed Aug 14, 2022
1 parent ef4da85 commit decfb9d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 130 deletions.
64 changes: 44 additions & 20 deletions sys-botbase/source/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ HiddbgHdlsDeviceInfo controllerDevice = {0};
HiddbgHdlsState controllerState = {0};
time_t curTime = 0;
time_t origTime = 0;
USBResponse response;

//Keyboard:
HiddbgKeyboardAutoPilotState dummyKeyboardState = {0};
Expand Down Expand Up @@ -196,25 +197,36 @@ void writeMem(u64 offset, u64 size, u8* val)
printf("svcWriteDebugProcessMemory: %d\n", rc);
}

void peek(u8* out, u64 offset, u64 size)
void peek(u64 offset, u64 size)
{
u8 *out = malloc(sizeof(u8) * size);
attach();
readMem(out, offset, size);
detach();

if (!usb)
{
u64 i;
for (i = 0; i < size; i++)
{
printf("%02X", out[i]);
}
printf("\n");
}
if (usb)
{
response.size = size;
response.data = &out[0];
sendUsbResponse(response);
}
else
{
u64 i;
for (i = 0; i < size; i++)
printf("%02X", out[i]);
printf("\n");
}
free(out);
}

void peekMulti(u8* out, u64* offset, u64* size, u64 count, u64 totalSize)
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++)
Expand All @@ -224,15 +236,20 @@ void peekMulti(u8* out, u64* offset, u64* size, u64 count, u64 totalSize)
}
detach();

if (!usb)
{
u64 i;
for (i = 0; i < totalSize; i++)
{
printf("%02X", out[i]);
}
printf("\n");
}
if (usb)
{
response.size = totalSize;
response.data = &out[0];
sendUsbResponse(response);
}
else
{
u64 i;
for (i = 0; i < totalSize; i++)
printf("%02X", out[i]);
printf("\n");
}
free(out);
}

void readMem(u8* out, u64 offset, u64 size)
Expand Down Expand Up @@ -501,3 +518,10 @@ void resetTime()
if (R_FAILED(rt))
fatalThrow(rt);
}

void sendUsbResponse(USBResponse response)
{
usbCommsWrite((void*)&response, 4);
if (response.size > 0)
usbCommsWrite(response.data, response.size);
}
11 changes: 9 additions & 2 deletions sys-botbase/source/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ typedef struct {
u8 state;
} KeyData;

typedef struct
{
u64 size;
void* data;
}USBResponse;

#define JOYSTICK_LEFT 0
#define JOYSTICK_RIGHT 1

Expand All @@ -51,8 +57,8 @@ bool getIsProgramOpen(u64 id);

void poke(u64 offset, u64 size, u8* val);
void writeMem(u64 offset, u64 size, u8* val);
void peek(u8* out, u64 offset, u64 size);
void peekMulti(u8* out, u64* offset, u64* size, u64 count, u64 totalSize);
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 All @@ -65,3 +71,4 @@ void key(HiddbgKeyboardAutoPilotState* states, u64 sequentialCount);
void clickSequence(char* seq, u8* token);
void dateSkip();
void resetTime();
void sendUsbResponse(USBResponse response);
120 changes: 12 additions & 108 deletions sys-botbase/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
#define HEAP_SIZE 0x00C00000
#define THREAD_SIZE 0x1A000

typedef struct
{
u64 size;
void* data;
}USBResponse;

typedef enum {
Active = 0,
Exit = 1,
Expand Down Expand Up @@ -53,7 +47,7 @@ u8 clickThreadState = 0; // 1 = break thread
KeyData currentKeyEvent = {0};
TouchData currentTouchEvent = {0};
char* currentClick = NULL;
bool usb = true;
bool usb = false;

// for cancelling the touch/click thread
u8 touchToken = 0;
Expand All @@ -63,13 +57,6 @@ u8 clickToken = 0;
u32 __nx_applet_type = AppletType_None;
TimeServiceType __nx_time_service_type = TimeServiceType_System;

void sendUsbResponse(USBResponse response)
{
usbCommsWrite((void*)&response, 4);
if (response.size > 0)
usbCommsWrite(response.data, response.size);
}

// we override libnx internals to do a minimal init
void __libnx_initheap(void)
{
Expand Down Expand Up @@ -208,16 +195,7 @@ int argmain(int argc, char **argv)
MetaData meta = getMetaData();
u64 offset = parseStringToInt(argv[1]);
u64 size = parseStringToInt(argv[2]);

u8* out = malloc(sizeof(u8) * size);
peek(out, meta.heap_base + offset, size);
if (usb)
{
response.size = size;
response.data = &out[0];
sendUsbResponse(response);
}
free(out);
peek(meta.heap_base + offset, size);
}

if (!strcmp(argv[0], "peekMulti"))
Expand All @@ -236,20 +214,7 @@ int argmain(int argc, char **argv)
offsets[i] = meta.heap_base + parseStringToInt(argv[(i*2)+1]);
sizes[i] = parseStringToInt(argv[(i*2)+2]);
}

u64 totalSize = 0;
for (int i = 0; i < itemCount; i++)
totalSize += sizes[i];

u8* out = malloc(sizeof(u8) * totalSize);
peekMulti(out, offsets, sizes, itemCount, totalSize);
if (usb)
{
response.size = totalSize;
response.data = &out[0];
sendUsbResponse(response);
}
free(out);
peekMulti(offsets, sizes, itemCount);
}

if (!strcmp(argv[0], "peekAbsolute"))
Expand All @@ -259,15 +224,7 @@ int argmain(int argc, char **argv)

u64 offset = parseStringToInt(argv[1]);
u64 size = parseStringToInt(argv[2]);
u8 data[size];

peek(data, offset, size);
if (usb)
{
response.size = size;
response.data = &data[0];
sendUsbResponse(response);
}
peek(offset, size);
}

if (!strcmp(argv[0], "peekAbsoluteMulti"))
Expand All @@ -284,20 +241,7 @@ int argmain(int argc, char **argv)
offsets[i] = parseStringToInt(argv[(i*2)+1]);
sizes[i] = parseStringToInt(argv[(i*2)+2]);
}

u64 totalSize = 0;
for (int i = 0; i < itemCount; i++)
totalSize += sizes[i];

u8* out = malloc(sizeof(u8) * totalSize);
peekMulti(out, offsets, sizes, itemCount, totalSize);
if (usb)
{
response.size = totalSize;
response.data = &out[0];
sendUsbResponse(response);
}
free(out);
peekMulti(offsets, sizes, itemCount);
}

if (!strcmp(argv[0], "peekMain"))
Expand All @@ -308,15 +252,7 @@ int argmain(int argc, char **argv)
MetaData meta = getMetaData();
u64 offset = parseStringToInt(argv[1]);
u64 size = parseStringToInt(argv[2]);
u8 data[size];

peek(data, meta.main_nso_base + offset, size);
if (usb)
{
response.size = size;
response.data = &data[0];
sendUsbResponse(response);
}
peek(meta.main_nso_base + offset, size);
}

if (!strcmp(argv[0], "peekMainMulti"))
Expand All @@ -335,20 +271,7 @@ int argmain(int argc, char **argv)
offsets[i] = meta.main_nso_base + parseStringToInt(argv[(i*2)+1]);
sizes[i] = parseStringToInt(argv[(i*2)+2]);
}

u64 totalSize = 0;
for (int i = 0; i < itemCount; i++)
totalSize += sizes[i];

u8* out = malloc(sizeof(u8) * totalSize);
peekMulti(out, offsets, sizes, itemCount, totalSize);
if (usb)
{
response.size = totalSize;
response.data = &out[0];
sendUsbResponse(response);
}
free(out);
peekMulti(offsets, sizes, itemCount);
}

//poke <address in hex or dec> <data in hex or dec>
Expand Down Expand Up @@ -723,14 +646,7 @@ int argmain(int argc, char **argv)
u64 solved = followMainPointer(jumps, count);
solved += finalJump;

u8 data[size];
peek(data, solved, size);
if (usb)
{
response.size = size;
response.data = &data[0];
sendUsbResponse(response);
}
peek(solved, size);
}

// pointerPeekMulti <amount of bytes in hex or dec> <first (main) jump> <additional jumps> <final jump in pointerexpr> split by asterisks (*)
Expand Down Expand Up @@ -779,19 +695,7 @@ int argmain(int argc, char **argv)
lastIndex = currIndex;
}

u64 totalSize = 0;
for (int i = 0; i < itemCount; i++)
totalSize += sizes[i];

u8* out = malloc(sizeof(u8) * totalSize);
peekMulti(out, offsets, sizes, itemCount, totalSize);
if (usb)
{
response.size = totalSize;
response.data = &out[0];
sendUsbResponse(response);
}
free(out);
peekMulti(offsets, sizes, itemCount);
}

// pointerPoke <data to be sent> <first (main) jump> <additional jumps> <final jump in pointerexpr>
Expand Down Expand Up @@ -1404,8 +1308,8 @@ bool isUSB()
{
fscanf(config, "%[^\n]", str);
fclose(config);
if (strcmp(strlwr(str), "wifi") == 0)
return false;
if (strcmp(strlwr(str), "usb") == 0)
return true;
}
return true;
return false;
}

0 comments on commit decfb9d

Please sign in to comment.