Example #1
0
void aptAppStarted(void)
{
	u8 buf1[4], buf2[4];

	aptSetStatus(APP_RUNNING);
	svcClearEvent(aptStatusEvent);

	if(!aptIsCrippled())
	{
		memset(buf1, 0, 4);

		buf1[0] = 0x10;
		aptOpenSession();
		APT_AppletUtility(NULL, 0x7, 0x4, buf1, 0x1, buf2);
		aptCloseSession();

		buf1[0] = 0x00;
		aptOpenSession();
		APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();

		aptOpenSession();
		APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();
	}
}
Example #2
0
File: apt.c Project: Almamu/ctrulib
void aptAppStarted()
{
	u8 buf1[4], buf2[4];

	svcCreateMutex(&aptStatusMutex, true);
	aptStatus=0;
	svcReleaseMutex(aptStatusMutex);

	aptSetStatus(APP_RUNNING);

	if(!(__system_runflags&RUNFLAG_APTWORKAROUND))
	{
		memset(buf1, 0, 4);

		buf1[0] = 0x10;
		aptOpenSession();
		APT_AppletUtility(NULL, NULL, 0x7, 0x4, buf1, 0x1, buf2);
		aptCloseSession();

		buf1[0] = 0x00;
		aptOpenSession();
		APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();

		aptOpenSession();
		APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();
	}
}
Example #3
0
File: apt.c Project: Almamu/ctrulib
void aptExit()
{
	if (!aptInitialised) return;

	if(!(__system_runflags&RUNFLAG_APTWORKAROUND))aptAppletUtility_Exit_RetToApp(0);

	// This is only executed when application-termination was triggered via the home-menu power-off screen.
	if(aptGetStatusPower() == 1)
	{
		aptOpenSession();
		APT_ReplySleepQuery(NULL, currentAppId, 0x0);
		aptCloseSession();
	}

	if(!(__system_runflags&RUNFLAG_APTWORKAROUND))
	{
		aptOpenSession();
		APT_PrepareToCloseApplication(NULL, 0x1);
		aptCloseSession();
		
		aptOpenSession();
		APT_CloseApplication(NULL, 0x0, 0x0, 0x0);
		aptCloseSession();
	}
	
	svcCloseHandle(aptSleepSync);

	svcCloseHandle(aptStatusMutex);
	svcCloseHandle(aptLockHandle);
	svcCloseHandle(aptStatusEvent);
	
	aptInitialised = false;
}
Example #4
0
void aptAppStarted()
{
	u8 buf1[4], buf2[4];

	svcCreateMutex(&aptStatusMutex, true);
	aptStatus=0;
	svcReleaseMutex(aptStatusMutex);

	aptSetStatus(APP_RUNNING);

	if(!aptIsCrippled())
	{
		memset(buf1, 0, 4);

		buf1[0] = 0x10;
		aptOpenSession();
		APT_AppletUtility(NULL, NULL, 0x7, 0x4, buf1, 0x1, buf2);
		aptCloseSession();

		buf1[0] = 0x00;
		aptOpenSession();
		APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();

		aptOpenSession();
		APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();
	}
}
Example #5
0
Result aptInit(void)
{
	Result ret=0;

	if (aptInitialised) return ret;

	aptStatusMutex = 0;

	// Initialize APT stuff, escape load screen.
	ret = __apt_initservicehandle();
	if(ret!=0)return ret;
	if((ret=APT_GetLockHandle(&aptuHandle, 0x0, &aptLockHandle)))return ret;
	svcCloseHandle(aptuHandle);

	currentAppId = __apt_appid;

	svcCreateEvent(&aptStatusEvent, 0);
	svcCreateEvent(&aptSleepSync, 0);

	if(!aptIsCrippled())
	{
		aptOpenSession();
		if((ret=APT_Initialize(NULL, currentAppId, &aptEvents[0], &aptEvents[1])))return ret;
		aptCloseSession();
		
		aptOpenSession();
		if((ret=APT_Enable(NULL, 0x0)))return ret;
		aptCloseSession();
		
		// create APT close event
		svcCreateEvent(&aptEvents[2], 0);

		// After a cycle of APT_Finalize+APT_Initialize APT thinks the
		// application is suspended, so we need to tell it to unsuspend us.
		if (aptIsReinit())
		{
			aptOpenSession();
			APT_PrepareToJumpToApplication(NULL, 0x0);
			aptCloseSession();

			aptOpenSession();
			APT_JumpToApplication(NULL, 0x0, 0x0, 0x0);
			aptCloseSession();
		}
		
		aptOpenSession();
		if((ret=APT_NotifyToWait(NULL, currentAppId)))return ret;
		aptCloseSession();

		// create APT event handler thread
		svcCreateThread(&aptEventHandlerThread, aptEventHandler, 0x0,
			(u32*)(&aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]), 0x31, 0xfffffffe);
	} else
		aptAppStarted();

	aptInitialised = true;

	return 0;
}
Example #6
0
void aptExit()
{
	if (!aptInitialised) return;

	if(!aptIsCrippled())aptAppletUtility_Exit_RetToApp(0);

	// This is only executed when application-termination was triggered via the home-menu power-off screen.
	if(aptGetStatusPower() == 1)
	{
		aptOpenSession();
		APT_ReplySleepQuery(NULL, currentAppId, 0x0);
		aptCloseSession();
	}

	if(!aptIsCrippled())
	{
		bool isReinit = aptIsReinit();
		if (aptGetStatus() == APP_EXITING || !isReinit)
		{
			aptOpenSession();
			APT_PrepareToCloseApplication(NULL, 0x1);
			aptCloseSession();
		
			aptOpenSession();
			APT_CloseApplication(NULL, 0x0, 0x0, 0x0);
			aptCloseSession();

			if (isReinit)
			{
				extern void (*__system_retAddr)(void);
				__system_retAddr = NULL;
			}
		} else if (isReinit)
		{
			aptOpenSession();
			APT_Finalize(NULL, currentAppId);
			aptCloseSession();
		}
	}

	svcSignalEvent(aptEvents[2]);
	svcWaitSynchronization(aptEventHandlerThread, U64_MAX);
	svcCloseHandle(aptEventHandlerThread);
	svcCloseHandle(aptEvents[2]);
	
	svcCloseHandle(aptSleepSync);

	svcCloseHandle(aptStatusMutex);
	svcCloseHandle(aptLockHandle);
	svcCloseHandle(aptStatusEvent);
	
	aptInitialised = false;
}
Example #7
0
static bool __handle_incoming_parameter() {
	u8 type;

	aptOpenSession();
	APT_ReceiveParameter(NULL, currentAppId, 0x1000, aptParameters, NULL, &type);
	aptCloseSession();

	switch(type)
	{
	case 0x1: // Application just started.
		aptAppStarted();
		return true;

	case 0x3: // "Launched library applet finished loading"
		aptSetStatus(APP_APPLETSTARTED);
		return true;
	case 0xA: // "Launched library applet closed"
		if(__apt_launchapplet_parambuf && __apt_launchapplet_parambufsize)memcpy(__apt_launchapplet_parambuf, aptParameters, __apt_launchapplet_parambufsize);
		aptSetStatus(APP_APPLETCLOSED);
		return true;
	case 0xB: // Just returned from menu.
		GSPGPU_AcquireRight(NULL, 0x0);
		GSPGPU_RestoreVramSysArea(NULL);
		aptAppletUtility_Exit_RetToApp(0);
		aptSetStatus(APP_RUNNING);
		return true;

	case 0xC: // Exiting application.
		aptSetStatus(APP_EXITING);
		return false;
	}

	return true;
}
Example #8
0
void reboot() {
    aptInit();
    aptOpenSession();
    APT_HardwareResetAsync(NULL);
    aptCloseSession();
    aptExit();
}
Example #9
0
File: apt.c Project: Almamu/ctrulib
Result APT_CheckNew3DS(Handle* handle, u8 *out)
{
	Result ret=0;

	if(__apt_new3dsflag_initialized)
	{
		*out = __apt_new3dsflag;
		return 0;
	}

	aptOpenSession();
	if(currentAppId==APPID_APPLICATION)
	{
		ret = APT_CheckNew3DS_Application(NULL, out);
	}
	else
	{
		ret = APT_CheckNew3DS_System(NULL, out);
	}
	aptCloseSession();

	__apt_new3dsflag_initialized = 1;
	__apt_new3dsflag = *out;

	return ret;
}
Example #10
0
void reboot(void)
{
    aptOpenSession();
    APT_HardwareResetAsync();
    aptCloseSession();
    exit(EXIT_FAILURE);
}
Example #11
0
NS_APPID aptGetMenuAppID(void)
{
	NS_APPID menu_appid = 0;

	aptOpenSession();
	APT_GetAppletManInfo(0xff, NULL, NULL, &menu_appid, NULL);
	aptCloseSession();

	return menu_appid;
}
Example #12
0
extern void doReboot() {
    if (!menuret_enabled) {
        aptOpenSession();
        APT_HardwareResetAsync();
        aptCloseSession();
    }
    else {
        menuret = true;
        die = true;
    }
}
Example #13
0
File: apt.c Project: Almamu/ctrulib
Result aptInit(void)
{
	Result ret=0;

	if (aptInitialised) return ret;

	// Initialize APT stuff, escape load screen.
	ret = __apt_initservicehandle();
	if(ret!=0)return ret;
	if((ret=APT_GetLockHandle(&aptuHandle, 0x0, &aptLockHandle)))return ret;
	svcCloseHandle(aptuHandle);

	currentAppId = __apt_appid;

	svcCreateEvent(&aptStatusEvent, 0);
	svcCreateEvent(&aptSleepSync, 0);

	if(!(__system_runflags&RUNFLAG_APTWORKAROUND))
	{
		aptOpenSession();
		if((ret=APT_Initialize(NULL, currentAppId, &aptEvents[0], &aptEvents[1])))return ret;
		aptCloseSession();
		
		aptOpenSession();
		if((ret=APT_Enable(NULL, 0x0)))return ret;
		aptCloseSession();
		
		aptOpenSession();
		if((ret=APT_NotifyToWait(NULL, currentAppId)))return ret;
		aptCloseSession();
		
		// create APT event handler thread
		svcCreateThread(&aptEventHandlerThread, aptEventHandler, 0x0,
			(u32*)(&aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]), 0x31, 0xfffffffe);
	} else
		aptAppStarted();

	aptInitialised = true;

	return 0;
}
Example #14
0
File: main.c Project: masterhou/FBI
int main(int argc, const char* argv[]) {
    gfxInitDefault();
    gfxSet3D(false);
    
    if(argc > 0) {
        svchax_init(true);
        if(!__ctr_svchax || !__ctr_svchax_srv) {
            util_panic("Failed to acquire kernel access.");
            return 1;
        }
    }

    aptOpenSession();
    Result setCpuTimeRes = APT_SetAppCpuTimeLimit(30);
    aptCloseSession();

    if(R_FAILED(setCpuTimeRes)) {
        util_panic("Failed to set syscore CPU time limit: %08lX", setCpuTimeRes);
        return 1;
    }

    romfsInit();
    cfguInit();
    acInit();
    ptmuInit();
    httpcInit(0);

    amInit();
    AM_InitializeExternalTitleDatabase(false);

    soc_buffer = memalign(0x1000, 0x100000);
    if(soc_buffer != NULL) {
        socInit(soc_buffer, 0x100000);
    }

    screen_init();
    ui_init();
    task_init();

    mainmenu_open();

    while(aptMainLoop() && ui_update());

    cleanup();
    return 0;
}
Example #15
0
AppResult appLaunch(App app) {
    u8 buf0[0x300];
    u8 buf1[0x20];

    aptOpenSession();
    Result prepareResult = APT_PrepareToDoAppJump(NULL, 0, app.titleId, appMediatypeToByte(app.mediaType));
    if(prepareResult != 0) {
        aptCloseSession();
        platformSetError(serviceParseError((u32) prepareResult));
        return APP_LAUNCH_FAILED;
    }

    Result doResult = APT_DoAppJump(NULL, 0x300, 0x20, buf0, buf1);
    if(doResult != 0) {
        aptCloseSession();
        platformSetError(serviceParseError((u32) doResult));
        return APP_LAUNCH_FAILED;
    }

    aptCloseSession();
    return APP_SUCCESS;
}
Example #16
0
static void action_launch_title_update(ui_view* view, void* data, float* progress, char* text) {
    title_info* info = (title_info*) data;

    Result res = 0;

    aptOpenSession();

    if(R_SUCCEEDED(res = APT_PrepareToDoAppJump(0, info->titleId, info->mediaType))) {
        u8 buf0[0x300];
        u8 buf1[0x20];

        res = APT_DoAppJump(0x300, 0x20, buf0, buf1);
    }

    aptCloseSession();

    if(R_FAILED(res)) {
        ui_pop();
        info_destroy(view);

        error_display_res(NULL, info, ui_draw_title_info, res, "Failed to launch title.");
    }
}
Example #17
0
Result APT_CheckNew3DS(u8 *out)
{
	Result ret=0;

	if(out==NULL)return -1;

	*out = 0;

	if(__apt_new3dsflag_initialized)
	{
		*out = __apt_new3dsflag;
		return 0;
	}

	aptOpenSession();
	ret = APT_CheckNew3DS_System(out);
	aptCloseSession();

	__apt_new3dsflag_initialized = 1;
	__apt_new3dsflag = *out;

	return ret;
}
Example #18
0
int main() {

	gfxInitDefault();
	consoleInit(GFX_BOTTOM, NULL);

	printf("Update Suppressor %s\n", VERSION);

	setModes();

	if (debug)
		printf("Debug Mode\n");

	switch (mode) {
	case MODE_SINGLE:
		puts("Single Mode");
		break;
	case MODE_ALL:
		puts("All Mode");
		break;
	case MODE_FILE:
		puts("Not Implemented: File Mode");
		break;
	default:
		puts("wtf? Unknown Mode");
		break;
	}

	u64 PID = 0;
	if (mode == MODE_SINGLE) {
		aptOpenSession();
		APT_GetProgramID(&PID);
		aptCloseSession();

		printf("Title Id: %08X%08X\n", (unsigned int) (PID >> 32),
				(unsigned int) (PID & 0xFFFFFFFF));
	}
Example #19
0
void aptAppletUtility_Exit_RetToApp(u32 type)
{
	u8 buf1[4], buf2[4];

	memset(buf1, 0, 4);
	
	buf1[0]=0x10;
	aptOpenSession();
	APT_AppletUtility(NULL, 0x7, 0x4, buf1, 0x1, buf2);
	aptCloseSession();

	buf1[0]=0x00;
	aptOpenSession();
	APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
	aptCloseSession();

	buf1[0]=0x01;
	aptOpenSession();
	APT_AppletUtility(NULL, 0x7, 0x4, buf1, 0x1, buf2);
	aptCloseSession();

	buf1[0]=0x00;
	aptOpenSession();
	APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
	aptCloseSession();

	aptOpenSession();
	APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
	aptCloseSession();

	if(type)
	{
		aptOpenSession();
		APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
		aptCloseSession();
	}
}
Example #20
0
void aptAppletStarted(void)
{
	u8 buf1[4], buf2[4];

	memset(buf1, 0, 4);

	// Set status to SUSPENDED.
	svcClearEvent(aptStatusEvent);
	aptSetStatus(APP_SUSPENDED);

	aptOpenSession();
	APT_SendCaptureBufferInfo(0x20, __ns_capinfo);
	aptCloseSession();

	aptOpenSession();
	APT_ReplySleepQuery(currentAppId, 0x0);
	aptCloseSession();

	aptOpenSession();
	APT_StartLibraryApplet(__apt_launchapplet_appID, __apt_launchapplet_inhandle, __apt_launchapplet_parambuf, __apt_launchapplet_parambufsize);
	aptCloseSession();

	buf1[0]=0x00;
	aptOpenSession();
	APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
	aptCloseSession();

	aptOpenSession();
	APT_NotifyToWait(currentAppId);
	aptCloseSession();

	buf1[0]=0x00;
	aptOpenSession();
	APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
	aptCloseSession();
}
Example #21
0
int main() 
{
	int i, x, y;
	
	touchPosition lastTouch;
	u32 repeatkeys = 0;
	int repeatstate = 0;
	int repeatcount = 0;
	
	running = 0;
	pause = 0;
	exitemu = 0;
	
	
		
	PPU_Init();
	
	
	srvInit();
		
	aptInit();
	aptOpenSession();
	APT_SetAppCpuTimeLimit(NULL, 30); // enables syscore usage
	aptCloseSession();

	gfxInit();
	hidInit(NULL);
	fsInit();
	
	GPU_Init(NULL);
	gpuCmdSize = 0x40000;
	gpuCmd = (u32*)linearAlloc(gpuCmdSize*4);
	GPU_Reset(gxCmdBuf, gpuCmd, gpuCmdSize);
	
	shader = SHDR_ParseSHBIN((u32*)blarg_shbin, blarg_shbin_size);
	
	GX_SetMemoryFill(gxCmdBuf, (u32*)gpuOut, 0x404040FF, (u32*)&gpuOut[0x2EE00], 0x201, (u32*)gpuDOut, 0x00000000, (u32*)&gpuDOut[0x2EE00], 0x201);
	gfxSwapBuffersGpu();
	
	UI_SetFramebuffer(gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL));
	
	BorderTex = (u32*)linearAlloc(512*256*4);
	MainScreenTex = (u32*)linearAlloc(512*256*4);
	SubScreenTex = (u32*)linearAlloc(512*256*4);
	BrightnessTex = (u8*)linearAlloc(8*256);
	
	borderVertices = (float*)linearAlloc(5*3 * 2 * sizeof(float));
	screenVertices = (float*)linearAlloc(7*3 * 2 * sizeof(float));
	
	float* fptr = &vertexList[0];
	for (i = 0; i < 5*3*2; i++) borderVertices[i] = *fptr++;
	for (i = 0; i < 7*3*2; i++) screenVertices[i] = *fptr++;
	

	sdmcArchive = (FS_archive){0x9, (FS_path){PATH_EMPTY, 1, (u8*)""}};
	FSUSER_OpenArchive(NULL, &sdmcArchive);
	
	if (!LoadBorder("/blargSnesBorder.bmp"))
		CopyBitmapToTexture(defaultborder, BorderTex, 400, 240, 0xFF, 0, 64, true);
		
	CopyBitmapToTexture(screenfill, PPU_MainBuffer, 256, 224, 0, 16, 64, false);
	memset(PPU_SubBuffer, 0, 256*256*4);
	memset(PPU_Brightness, 0xFF, 224);
	
	UI_Switch(&UI_ROMMenu);
	
	svcCreateEvent(&SPCSync, 0);
	
	aptSetupEventHandler();

	
	APP_STATUS status;
	while((status = aptGetStatus()) != APP_EXITING)
	{
		if(status == APP_RUNNING)
		{
			svcSignalEvent(SPCSync);
			
			hidScanInput();
			u32 press = hidKeysDown();
			u32 held = hidKeysHeld();
			u32 release = hidKeysUp();
			
			GPUCMD_SetBuffer(gpuCmd, gpuCmdSize, 0);
			RenderTopScreen();
			GPUCMD_Finalize();
			GPUCMD_Run(gxCmdBuf);
			
			if (running)
			{
				// emulate
				
				CPU_Run(); // runs the SNES for one frame. Handles PPU rendering.
				
				// SRAM autosave check
				// TODO: also save SRAM under certain circumstances (pausing, returning to home menu, etc)
				framecount++;
				if (!(framecount & 7))
					SNES_SaveSRAM();
			}
			else
			{
				// update UI
				
				if (held & KEY_TOUCH)
				{
					hidTouchRead(&lastTouch);
					UI_Touch(true, lastTouch.px, lastTouch.py);
					held &= ~KEY_TOUCH;
				}
				else if (release & KEY_TOUCH)
				{
					UI_Touch(false, lastTouch.px, lastTouch.py);
					release &= ~KEY_TOUCH;
				}
				
				if (press)
				{
					UI_ButtonPress(press);
					
					// key repeat
					repeatkeys = press & (KEY_UP|KEY_DOWN|KEY_LEFT|KEY_RIGHT);
					repeatstate = 1;
					repeatcount = 15;
				}
				else if (held == repeatkeys)
				{
					repeatcount--;
					if (!repeatcount)
					{
						repeatcount = 7;
						if (repeatstate == 2)
							UI_ButtonPress(repeatkeys);
						else
							repeatstate = 2;
					}
				}
			}
			
			UI_SetFramebuffer(gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL));
			UI_Render();
			
			/*GPUCMD_SetBuffer(gpuCmd, gpuCmdSize, 0);
			RenderTopScreen();
			GPUCMD_Finalize();
			GPUCMD_Run(gxCmdBuf);*/
			
			// flush the bottomscreen cache while the PICA200 is busy rendering
			GSPGPU_FlushDataCache(NULL, gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL), 0x38400);
			
			// wait for the PICA200 to finish drawing
			gspWaitForP3D();
			
			// copy new screen textures
			// SetDisplayTransfer with flags=2 converts linear graphics to the tiled format used for textures
			GX_SetDisplayTransfer(gxCmdBuf, PPU_MainBuffer, 0x01000200, MainScreenTex, 0x01000200, 0x2);
			gspWaitForPPF();
			GX_SetDisplayTransfer(gxCmdBuf, PPU_SubBuffer, 0x01000200, SubScreenTex, 0x01000200, 0x2);
			gspWaitForPPF();
			
			// copy brightness.
			// TODO do better
			u8* bptr = BrightnessTex;
			for (i = 0; i < 224;)
			{
				u32 pixels = *(u32*)&PPU_Brightness[i];
				i += 4;
				
				*bptr = (u8)pixels;
				pixels >>= 8;
				bptr += 2;
				*bptr = (u8)pixels;
				pixels >>= 8;
				bptr += 6;
				
				*bptr = (u8)pixels;
				pixels >>= 8;
				bptr += 2;
				*bptr = (u8)pixels;
				pixels >>= 8;
				bptr += 22;
			}
			
			// transfer the final color buffer to the LCD and clear it
			// we can mostly overlap those two operations
			GX_SetDisplayTransfer(gxCmdBuf, gpuOut, 0x019001E0, (u32*)gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), 0x019001E0, 0x01001000);
			svcSleepThread(20000);
			GX_SetMemoryFill(gxCmdBuf, gpuOut, 0x404040FF, &gpuOut[0x2EE00], 0x201, gpuDOut, 0x00000000, &gpuDOut[0x2EE00], 0x201);
			gspWaitForPPF();
			gspWaitForPSC0();

			gspWaitForEvent(GSPEVENT_VBlank0, false);
			gfxSwapBuffersGpu();
		}
		else if(status == APP_SUSPENDING)
Example #22
0
Result aptInit(void)
{
	Result ret=0;

	if (AtomicPostIncrement(&aptRefCount)) return 0;

	// Initialize APT stuff, escape load screen.
	ret = __apt_initservicehandle();
	if(R_FAILED(ret)) goto _fail;
	if(R_FAILED(ret=APT_GetLockHandle(0x0, &aptLockHandle))) goto _fail;
	svcCloseHandle(aptuHandle);

	currentAppId = envGetAptAppId();

	svcCreateEvent(&aptStatusEvent, 0);
	svcCreateEvent(&aptSleepSync, 0);
	LightLock_Init(&aptStatusMutex);
	aptStatus=0;

	if(!aptIsCrippled())
	{
		aptOpenSession();
		if(R_FAILED(ret=APT_Initialize(currentAppId, &aptEvents[0], &aptEvents[1])))return ret;
		aptCloseSession();
		
		aptOpenSession();
		if(R_FAILED(ret=APT_Enable(0x0))) goto _fail;
		aptCloseSession();
		
		// create APT close event
		svcCreateEvent(&aptEvents[2], 0);

		// After a cycle of APT_Finalize+APT_Initialize APT thinks the
		// application is suspended, so we need to tell it to unsuspend us.
		if (aptIsReinit())
		{
			aptOpenSession();
			APT_PrepareToJumpToApplication(0x0);
			aptCloseSession();

			aptOpenSession();
			APT_JumpToApplication(0x0, 0x0, 0x0);
			aptCloseSession();
		}
		
		aptOpenSession();
		if(R_FAILED(ret=APT_NotifyToWait(currentAppId)))goto _fail;
		aptCloseSession();

		// create APT event handler thread
		aptEventHandlerThread = threadCreate(aptEventHandler, 0x0, APT_HANDLER_STACKSIZE, 0x31, -2, true);

		// Wait for the state to become APT_RUNNING
		aptWaitStatusEvent();
	} else
		aptAppStarted();

	return 0;

_fail:
	AtomicDecrement(&aptRefCount);
	return ret;
}
Example #23
0
static void __handle_notification(void) {
	APT_Signal type;
	Result ret=0;

	// Get notification type.
	aptOpenSession();
	ret = APT_InquireNotification(currentAppId, &type);
	aptCloseSession();
	if(R_FAILED(ret)) return;

	_aptDebug(1, type);

	switch(type)
	{
	case APTSIGNAL_HOMEBUTTON:
	case APTSIGNAL_POWERBUTTON:
		// The main thread should call aptReturnToMenu() when the status gets set to this.
		if(aptGetStatus() == APP_RUNNING)
		{
			aptOpenSession();
			APT_ReplySleepQuery(currentAppId, 0x0);
			aptCloseSession();
		
			if(type == APTSIGNAL_HOMEBUTTON)  aptSetStatusPower(0);
			if(type == APTSIGNAL_POWERBUTTON) aptSetStatusPower(1);
			aptSetStatus(APP_SUSPENDING);
		}
		break;

	case APTSIGNAL_PREPARESLEEP:
		// Reply to sleep-request.
		aptStatusBeforeSleep = aptGetStatus();
		aptSetStatus(APP_PREPARE_SLEEPMODE);
		svcWaitSynchronization(aptSleepSync, U64_MAX);
		svcClearEvent(aptSleepSync);
		
		aptOpenSession();
		APT_ReplySleepQuery(currentAppId, 0x1);
		aptCloseSession();
		break;

	case APTSIGNAL_ENTERSLEEP:
		if(aptGetStatus() == APP_PREPARE_SLEEPMODE)
		{
			// Report into sleep-mode.
			aptSetStatus(APP_SLEEPMODE);
			
			aptOpenSession();
			APT_ReplySleepNotificationComplete(currentAppId);
			aptCloseSession();
		}
		break;

	// Leaving sleep-mode.
	case APTSIGNAL_WAKEUP:
		if(aptGetStatus() == APP_SLEEPMODE)
		{
			if(aptStatusBeforeSleep == APP_RUNNING)GSPGPU_SetLcdForceBlack(0);

			// Restore old aptStatus.
			aptSetStatus(aptStatusBeforeSleep);
		}
		break;

	default:
		break;
	}
}
Example #24
0
void aptReturnToMenu(void)
{
	NS_APPID menu_appid;
	u32 tmp0 = 1, tmp1 = 0;

	if(aptIsCrippled())
	{
		svcClearEvent(aptStatusEvent);
		aptSetStatus(APP_EXITING);
		return;
	}

	// This is only executed when ret-to-menu was triggered via the home-button, not the power-button.
	if(aptGetStatusPower() == 0)
	{
		aptOpenSession();
		APT_AppletUtility(NULL, 0x6, 0x4, (u8*)&tmp0, 0x1, (u8*)&tmp1);
		aptCloseSession();
	}

	// Set status to SUSPENDED.
	__apt_launchapplet_appID = 0;
	svcClearEvent(aptStatusEvent);
	aptSetStatus(APP_SUSPENDED);

	// Prepare for return to menu
	aptOpenSession();
	APT_PrepareToJumpToHomeMenu();
	aptCloseSession();

	// Save Vram
	GSPGPU_SaveVramSysArea();

	// Capture screen.
	memset(__ns_capinfo, 0, 0x20);

	aptInitCaptureInfo(__ns_capinfo);

	menu_appid = aptGetMenuAppID();

	// Send capture-screen info to menu.
	aptOpenSession();
	APT_SendParameter(currentAppId, menu_appid, 0x20, __ns_capinfo, 0x0, 0x10);
	aptCloseSession();

	aptOpenSession();
	APT_SendCaptureBufferInfo(0x20, __ns_capinfo);
	aptCloseSession();

	// Release GSP module.
	GSPGPU_ReleaseRight();

	// Jump to menu!
	aptOpenSession();
	APT_JumpToHomeMenu(0x0, 0x0, 0x0);
	aptCloseSession();

	// Wait for return to application.
	aptOpenSession();
	APT_NotifyToWait(currentAppId);
	aptCloseSession();

	// This is only executed when ret-to-menu was triggered via the home-button, not the power-button.
	if(aptGetStatusPower() == 0)
	{
		tmp0 = 0;
		aptOpenSession();
		APT_AppletUtility(NULL, 0x4, 0x1, (u8*)&tmp0, 0x1, (u8*)&tmp1);
		aptCloseSession();
	}

	aptWaitStatusEvent();
}
Example #25
0
int main(int argc, char **argv)
{
	srvInit();	
	aptInit();
	gfxInitDefault();
	acInit();
	cfguInit();
	httpcInit();
	ptmuInit();
	hidInit();
	irrstInit();
	aptOpenSession();
	Result ret=APT_SetAppCpuTimeLimit(30);
	aptCloseSession();
	fsInit();
	ftp_state = false;
	isTopLCDOn = true;
	isBottomLCDOn = true;
	Handle fileHandle;
	u64 size;
	u32 bytesRead;
	int restore;
	
	// Check user build and enables kernel access
	if (nsInit()==0){
		CIA_MODE = true;
		nsExit();
	}else CIA_MODE = false;
	isNinjhax2 = false;
	if (!hbInit()) khaxInit();
	else isNinjhax2 = true;
	
	// Select Audio System (csnd:SND preferred)
	if (csndInit() == 0){
		csndAccess = true;
		csndExit();
	}else csndAccess = false;
	
	// Init Audio-Device
	int i = 0;
	for (i=0;i < 32; i++){
		audioChannels[i] = false;
		if (!isNinjhax2 && (i < 0x08))  audioChannels[i] = true;
		else if (csndAccess && (i < 0x08)) audioChannels[i] = true;
	}
	
	// Set main script
	char path[256];
	if (argc > 0){
		int latest_slash = 0;
		int i=5;
		while (argv[0][i]  != '\0'){
			if (argv[0][i] == '/') latest_slash = i;
			i++;
		}
		strcpy(path,&argv[0][5]);
		path[latest_slash-5] = 0;
		strcpy(start_dir,path);
		strcpy(cur_dir,path); // Set current dir
		strcat(path,"/index.lua");
	}else{
		strcpy(start_dir,"/");
		strcpy(cur_dir,"/"); // Set current dir for GW Mode
		strcpy(path,"/index.lua");
	}
	
	while(aptMainLoop())
	{
		restore=0;		
		char error[2048];		
		
		// Load main script
		FS_Path filePath=fsMakePath(PATH_ASCII, path);
		FS_Archive script=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}};
		Result ret = FSUSER_OpenFileDirectly(&fileHandle, script, filePath, FS_OPEN_READ, 0x00000000);
		if (!ret){
			FSFILE_GetSize(fileHandle, &size);
			buffer = (unsigned char*)(malloc((size+1) * sizeof (char)));
			FSFILE_Read(fileHandle, &bytesRead, 0x0, buffer, size);
			buffer[size]=0;
			FSFILE_Close(fileHandle);
			svcCloseHandle(fileHandle);
			errMsg = runScript((const char*)buffer, true);
			free(buffer);
		}else errMsg = "index.lua file not found.";
		
		// Force LCDs power on
		if ((!isTopLCDOn) || (!isBottomLCDOn)){
			gspLcdInit();
			if (!isTopLCDOn) GSPLCD_PowerOnBacklight(GSPLCD_SCREEN_TOP);
			if (!isBottomLCDOn) GSPLCD_PowerOnBacklight(GSPLCD_SCREEN_BOTTOM);
			gspLcdExit();
			isTopLCDOn = true;
			isBottomLCDOn = true;
		}
		
		// Fake error to force interpreter shutdown
		if (strstr(errMsg, "lpp_exit_04")) break;
			
		if (ftp_state) ftp_exit();
		ftp_state = false;
		int connfd;
		while (restore==0){
			gspWaitForVBlank();
			RefreshScreen();
			ClearScreen(0);
			ClearScreen(1);
			strcpy(error,"Error: ");
			strcat(error,errMsg);
			if (ftp_state){ 
				u32 ip=(u32)gethostid();
				char ip_address[64];
				strcat(error,"\n\nPress A to restart\nPress B to exit\nPress Y to enable FTP server\n\nFTP state: ON\nIP: ");
				sprintf(ip_address,"%lu.%lu.%lu.%lu", ip & 0xFF, (ip>>8)&0xFF, (ip>>16)&0xFF, (ip>>24)&0xFF);
				strcat(error,ip_address);
				strcat(error,"\nPort: 5000");
				if(connfd<0)connfd=ftp_getConnection();
				else{
					int ret=ftp_frame(connfd);
					if(ret==1) connfd=-1;
				}
			}else strcat(error,"\n\nPress A to restart\nPress B to exit\nPress Y to enable FTP server\n\nFTP state: OFF");
			DebugOutput(error);
			hidScanInput();
			if(hidKeysDown() & KEY_A){
				strcpy(cur_dir,start_dir);
				restore=1;
			}else if(hidKeysDown() & KEY_B){
				restore=2;
			}else if(hidKeysDown() & KEY_Y){
				if (!ftp_state){
					u32 wifiStatus;
					if ((u32)ACU_GetWifiStatus(&wifiStatus) !=  0xE0A09D2E){
						if (wifiStatus != 0){
							ftp_init();
							connfd = -1;
							ftp_state = true;
						}
					}
				}
			}
			
			gfxFlushBuffers();
			gfxSwapBuffers();
		}
		if (ftp_state) ftp_exit();
		if (isCSND){
			if (csndAccess) csndExit();
			else ndspExit();
			isCSND = false;
		}
		if (restore==2){
			break;
		}
	}
Example #26
0
int main(int argc, char **argv) {
    //mode
    //1 = only execute memchunkhax2
    //2 = only execute service call
    //3 = execute memchunkhax2 and service call afterwards
    //4 = execute all
    u8 mode = 4;
    u8 success;

    const char* service = "am:u";

    gfxInitDefault();
    consoleInit(GFX_TOP, NULL);

    if (mode == 1) {
      // Run the exploit
      success = execute_memchunkhax2();
      printf("Exploit returned: %s\n", success ? "Success!" : "Failure.");
    }

    if (mode == 2) {
      testService(service);
    }

    if (mode == 3) {
      // Run the exploit
      success = execute_memchunkhax2();
      printf("Exploit returned: %s\n", success ? "Success!" : "Failure.");

      testService(service);
    }

    if (mode == 4) {
      // This one should fail
      testService(service);

      // Run the exploit
      success = execute_memchunkhax2();
      printf("Exploit returned: %s\n", success ? "Success!" : "Failure.");

      // This one hopefully won't
      testService(service);
    }

    printf("Press START to reboot.\n");
    printf("Press SELECT to (try to) exit.\n");


    u8 debugFirst = 1;
    while(aptMainLoop()) {
      hidScanInput();

      if (debugFirst == 1) {
        printf("entered loop");
        debugFirst = 0;
      }

      if(hidKeysDown() & KEY_SELECT) {
        break;
      }

      if(hidKeysDown() & KEY_START) {
        //Reboot console
        aptOpenSession();
        APT_HardwareResetAsync();
        aptCloseSession();
      }

      gfxFlushBuffers();
      gfxSwapBuffers();
      gspWaitForVBlank();
    }

    gfxExit();
    return 0;
}
Example #27
0
Result APT_LaunchLibraryApplet(NS_APPID appID, Handle inhandle, u32 *parambuf, u32 parambufsize)
{
	Result ret=0;
	u8 tmp=0;

	u8 buf1[4];
	u8 buf2[4];

	aptOpenSession();
	APT_ReplySleepQuery(currentAppId, 0);
	aptCloseSession();

	aptOpenSession();
	ret=APT_PrepareToStartLibraryApplet(appID);
	aptCloseSession();
	if(R_FAILED(ret))return ret;

	memset(buf1, 0, 4);
	aptOpenSession();
	APT_AppletUtility(NULL, 0x4, 0x1, buf1, 0x1, buf2);
	aptCloseSession();

	while(1)
	{
		aptOpenSession();
		ret=APT_IsRegistered(appID, &tmp);
		aptCloseSession();
		if(R_FAILED(ret))return ret;

		if(tmp!=0)break;
	}

	aptCallHook(APTHOOK_ONSUSPEND);

	__apt_launchapplet_appID = appID;
	__apt_launchapplet_inhandle = inhandle;
	__apt_launchapplet_parambuf = parambuf;
	__apt_launchapplet_parambufsize = parambufsize;

	// Set status to SUSPENDED.
	svcClearEvent(aptStatusEvent);
	aptSetStatus(APP_SUSPENDED);

	// Save Vram
	GSPGPU_SaveVramSysArea();

	// Capture screen.
	memset(__ns_capinfo, 0, 0x20);

	aptInitCaptureInfo(__ns_capinfo);

	// Send capture-screen info to the library applet.
	aptOpenSession();
	APT_SendParameter(currentAppId, appID, 0x20, __ns_capinfo, 0x0, 0x2);
	aptCloseSession();

	// Release GSP module.
	GSPGPU_ReleaseRight();

	return 0;
}