Skip to content

Commit

Permalink
Merge upstream changes
Browse files Browse the repository at this point in the history
W.I.P changes include friend code, and device cert. Device cert doesn't
print anything as of now.

The NNID is displayed properly. This time it shows both your username
and ID. Fixed a problem that caused crashes when de-initializing actu.
  • Loading branch information
joel16 committed Nov 28, 2016
1 parent ccc5f21 commit 529e11a
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 22 deletions.
88 changes: 83 additions & 5 deletions source/actu.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,92 @@
#include <3ds.h>
#include <stdio.h>
#include "actu.h"

static Handle actHandle;
Result actInit(u32 sdkVer, u32 sharedMemSize)
{
Result ret=0;

ret = srvGetServiceHandle(&actHandle, "act:u");
if (R_FAILED(ret)) ret = srvGetServiceHandle(&actHandle, "act:a");
if (R_SUCCEEDED(ret))
{
act_shareMemSize = sharedMemSize;
act_shareMemHandle = 0;

if(act_shareMemSize)
{
act_shareMemAddr = (u32*)memalign(0x1000, act_shareMemSize);
if(act_shareMemAddr != NULL)
{
memset((void*)act_shareMemAddr, 0, act_shareMemSize);
ret = svcCreateMemoryBlock(&act_shareMemHandle, (u32)act_shareMemAddr, act_shareMemSize, 0, MEMPERM_READ | MEMPERM_WRITE);
if(R_FAILED(ret))
actExit();
}
}
ret = ACT_Initialize(sdkVer, &act_shareMemHandle, act_shareMemSize);

if (R_FAILED(ret))
actExit();
}
return ret;
}

void actExit(void)
{
if (AtomicDecrement(&actRefCount)) return;
svcCloseHandle(actHandle);

if(act_shareMemHandle)
{
svcUnmapMemoryBlock(act_shareMemHandle, (u32)act_shareMemAddr);
free(act_shareMemAddr);
act_shareMemAddr = NULL;
svcCloseHandle(act_shareMemHandle);
act_shareMemHandle = 0;
act_shareMemSize = 0;
}
}

Result ACT_Initialize(u32 sdkVer, void *addr, u32 memSize)
{
Result ret = 0;
u32 *cmdbuf = getThreadCommandBuffer();

cmdbuf[0] = IPC_MakeHeader(0x1,2,4); // 0x00010084
cmdbuf[1] = sdkVer;
cmdbuf[2] = memSize;
cmdbuf[3] = 0x20;
cmdbuf[4] = IPC_Desc_CurProcessHandle();
cmdbuf[5] = 0;
cmdbuf[6] = (u32)addr;

if(R_FAILED(ret = svcSendSyncRequest(actHandle))) return ret;

return (Result)cmdbuf[1];
}

Result ACT_GetAccountInfo(void *buffer, u32 size, u32 blkId)
{
Result ret = 0;
u32 *cmdbuf = getThreadCommandBuffer();

cmdbuf[0] = IPC_MakeHeader(6,3,2); // 0x00600C2
cmdbuf[1] = 0xFE;
cmdbuf[2] = size;
cmdbuf[3] = blkId;
cmdbuf[4] = 0x10 * size | 0xC;
cmdbuf[5] = (u32)buffer;

if(R_FAILED(ret = svcSendSyncRequest(actHandle))) return ret;

return (Result)cmdbuf[1];
}

Result actInit(void)
Result actuInit(void)
{
return srvGetServiceHandle(&actHandle, "act:u");
}

Result actExit(void)
Result actuExit(void)
{
return svcCloseHandle(actHandle);
}
Expand Down
18 changes: 16 additions & 2 deletions source/actu.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#include <3ds.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Result actInit(void);
Result actExit(void);
Handle actHandle;
int actRefCount;

u32 *act_shareMemAddr;
u32 act_shareMemSize;
Handle act_shareMemHandle;

Result actInit(u32 sdkVer, u32 sharedMemSize);
void actExit(void);
Result ACT_Initialize(u32 sdkVer, void *addr, u32 memSize);
Result ACT_GetAccountInfo(void *buffer, u32 size, u32 blkId);
Result actuInit(void);
Result actuExit(void);
Result ACTU_Initialize(u32 sdkVersion, u32 unknown, Handle handle);
Result ACTU_GetAccountDataBlock(u32 unknown, u32 size, u32 blockId, void* output);
57 changes: 56 additions & 1 deletion source/am.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
#include "am.h"

char * base64encode(const char * input)
{
int len = strlen(input);
int leftover = len % 3;
char *ret = malloc(((len/3) * 4) + ((leftover)?4:0) + 1);
int n = 0;
int outlen = 0;
uint8_t i = 0;
uint8_t *inp = (uint8_t *) input;
const char *index = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

if (ret == NULL)
return NULL;

// Convert each 3 bytes of input to 4 bytes of output.
len -= leftover;
for (n = 0; n < len; n+=3) {
i = inp[n] >> 2;
ret[outlen++] = index[i];

i = (inp[n] & 0x03) << 4;
i |= (inp[n+1] & 0xf0) >> 4;
ret[outlen++] = index[i];

i = ((inp[n+1] & 0x0f) << 2);
i |= ((inp[n+2] & 0xc0) >> 6);
ret[outlen++] = index[i];

i = (inp[n+2] & 0x3f);
ret[outlen++] = index[i];
}

// Handle leftover 1 or 2 bytes.
if (leftover) {
i = (inp[n] >> 2);
ret[outlen++] = index[i];

i = (inp[n] & 0x03) << 4;
if (leftover == 2) {
i |= (inp[n+1] & 0xf0) >> 4;
ret[outlen++] = index[i];

i = ((inp[n+1] & 0x0f) << 2);
}
ret[outlen++] = index[i];
ret[outlen++] = '=';
if (leftover == 1)
ret[outlen++] = '=';
}
ret[outlen] = '\0';
return ret;
}

Result AMNet_GetDeviceCert(u8 *buffer)
Result amNetGetDeviceCert(u8 const * buffer)
{
Result ret = 0;
u32 *cmdbuf = getThreadCommandBuffer();
Expand Down
5 changes: 4 additions & 1 deletion source/am.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <3ds.h>
#include <stdlib.h>
#include <string.h>

Handle amHandle;

Result AMNet_GetDeviceCert(u8 *buffer);
char * base64encode(const char * input);
Result amNetGetDeviceCert(u8 const * buffer);
13 changes: 9 additions & 4 deletions source/frd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

Result frdInit(u32 sdkVer)
{
srvGetServiceHandle(&frdHandle, "frd:u");
srvGetServiceHandle(&frdHandle, "frd:n");
srvGetServiceHandle(&frdHandle, "frd:a");
return frdSetClientSdkVersion(sdkVer);
Result ret;

ret = srvGetServiceHandle(&frdHandle, "frd:u");
if(R_FAILED(ret)) ret = srvGetServiceHandle(&frdHandle, "frd:n");
if(R_FAILED(ret)) ret = srvGetServiceHandle(&frdHandle, "frd:a");

frdSetClientSdkVersion(sdkVer);

return ret;
}

Result frdExit()
Expand Down
46 changes: 37 additions & 9 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include <unistd.h>

#include "actu.h"
#include "am.h"
#include "cfgs.h"
#include "frd.h"
#include "mcu.h"
#include "screenshot.h"
#include "utils.h"

#define SDK(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|d)

Expand Down Expand Up @@ -219,6 +221,21 @@ u64 getSoapId(void)
return (tmp | (((u64) 4) << 32));
}

char * getDeviceCert(void)
{
u8 const cert[0x180];
amNetGetDeviceCert(cert);
return base64Encode(cert);
}

char * getNNID(void)
{
static char tmp[0x11];
ACT_GetAccountInfo(tmp, 0x11, 0x8);

return tmp;
}

int main(int argc, char *argv[])
{
gfxInitDefault();
Expand All @@ -229,11 +246,20 @@ int main(int argc, char *argv[])
ptmuInit();
mcuInit();
amInit();
amAppInit();
psInit();
aptInit();
hidInit();
acInit();
actuInit();
actInit(SDK(11,2,0,200), 0x20000);
gspLcdInit();
httpcInit(0x9000);
frdInit(SDK(11,4,0,200));

consoleInit(GFX_BOTTOM, NULL);

printf("\x1b[31;1m*\x1b[0m Device cert: \x1b[31;1m%s\x1b[0m \n\n", getDeviceCert());

consoleInit(GFX_TOP, NULL);

//=====================================================================//
Expand All @@ -251,7 +277,7 @@ int main(int argc, char *argv[])
while (aptMainLoop())
{
printf("\x1b[0;0H"); //Move the cursor to the top left corner of the screen
printf("\x1b[32;1m3DSident 0.7\x1b[0m\n\n");
printf("\x1b[32;1m3DSident 0.7.1\x1b[0m\n\n");

//=====================================================================//
//------------------------------Firm Info------------------------------//
Expand Down Expand Up @@ -292,20 +318,19 @@ int main(int argc, char *argv[])
printf("\x1b[31;1m*\x1b[0m Model: \x1b[31;1m%s %s\n\x1b[0m", getModel(), getRegion());
getScreenType();
printf("\x1b[31;1m*\x1b[0m Language: \x1b[31;1m%s\x1b[0m \n", getLang());
printf("\x1b[31;1m*\x1b[0m NNID: \x1b[31;1m%s\x1b[0m ", (char*)getNNID());

nnidNum = 0xFFFFFFFF;
ret = actInit();
ret = ACTU_Initialize(0xB0002C8, 0, 0);
ret = ACTU_GetAccountDataBlock(0xFE, 4, 12, &nnidNum);
ret = actExit();

if (nnidNum != 0xFFFFFFFF)
vaPrint("\x1b[31;1m*\x1b[0m NNID number: \x1b[31;1m%08X\x1b[0m \n", (int) nnidNum);
vaPrint("(\x1b[31;1m%08X\x1b[0m) \n", (int) nnidNum);
else
vaPrint("\x1b[31;1m*\x1b[0m NNID number: \x1b[31;1mError could not retrieve NNID\x1b[0m \n");
printf("\x1b[31;1mError could not retrieve NNID\x1b[0m\n");

printf("\x1b[31;1m*\x1b[0m Device ID: \x1b[31;1m%lu \n", getDeviceId());
printf("\x1b[31;1m*\x1b[0m ECS Device ID: \x1b[31;1m%llu \n", getSoapId());
printf("\x1b[31;1m*\x1b[0m Friend Code: \x1b[31;1m%llu \n", principalIdToFriendCode(getMyFriendKey().principalId));
printf("\x1b[31;1m*\x1b[0m MAC Address: \x1b[31;1m%s\x1b[0m \n", getMacAddress());
printf("\x1b[31;1m*\x1b[0m Serial number: \x1b[31;1m%s\x1b[0m \n", getSerialNum());

Expand All @@ -316,7 +341,7 @@ int main(int argc, char *argv[])
buf[12], buf[13], buf[14], buf[15]);

FSUSER_GetNandCid(buf, 0x10);
printf("\x1b[31;1m*\x1b[0m NAND CID: \x1b[31;1m%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\x1b[0m\n \n",
printf("\x1b[31;1m*\x1b[0m NAND CID: \x1b[31;1m%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\x1b[0m \n\n",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
buf[6], buf[7], buf[8], buf[9], buf[10], buf[11],
buf[12], buf[13], buf[14], buf[15]);
Expand Down Expand Up @@ -357,7 +382,7 @@ int main(int argc, char *argv[])
_3dSliderPercent = (osGet3DSliderState() * 100.0);
printf("\x1b[32;1m*\x1b[0m 3D slider state: \x1b[32;1m%.1lf\x1b[0m (\x1b[32;1m%.0lf%%\x1b[0m) \n", osGet3DSliderState(), _3dSliderPercent);

printf("\n\x1b[32;1m> Press any key to exit =)\x1b[0m\n");
printf("\n\x1b[32;1m> Press any key to exit =)\x1b[0m");

gspWaitForVBlank();
hidScanInput();
Expand All @@ -377,7 +402,10 @@ int main(int argc, char *argv[])
free(str_ver);
free(str_sysver);

frdExit();
httpcExit();
gspLcdExit();
actuExit();
acExit();
hidExit();
aptExit();
Expand Down
56 changes: 56 additions & 0 deletions source/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "utils.h"

char * base64Encode(u8 const * input)
{
int len = strlen((const char *)input);
int leftover = len % 3;
char *ret = malloc(((len/3) * 4) + ((leftover)?4:0) + 1);
int n = 0;
int outlen = 0;
uint8_t i = 0;
uint8_t *inp = (uint8_t *) input;
const char *index = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

if (ret == NULL)
return NULL;

// Convert each 3 bytes of input to 4 bytes of output.
len -= leftover;
for (n = 0; n < len; n+=3) {
i = inp[n] >> 2;
ret[outlen++] = index[i];

i = (inp[n] & 0x03) << 4;
i |= (inp[n+1] & 0xf0) >> 4;
ret[outlen++] = index[i];

i = ((inp[n+1] & 0x0f) << 2);
i |= ((inp[n+2] & 0xc0) >> 6);
ret[outlen++] = index[i];

i = (inp[n+2] & 0x3f);
ret[outlen++] = index[i];
}

// Handle leftover 1 or 2 bytes.
if (leftover) {
i = (inp[n] >> 2);
ret[outlen++] = index[i];

i = (inp[n] & 0x03) << 4;
if (leftover == 2) {
i |= (inp[n+1] & 0xf0) >> 4;
ret[outlen++] = index[i];

i = ((inp[n+1] & 0x0f) << 2);
}
ret[outlen++] = index[i];
ret[outlen++] = '=';
if (leftover == 1)
ret[outlen++] = '=';
}
ret[outlen] = '\0';
return ret;
}
Loading

0 comments on commit 529e11a

Please sign in to comment.