void __attribute__((weak)) __appInit(void) { Result rc; // Initialize default services. rc = smInitialize(); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM)); rc = appletInitialize(); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_AM)); if (__nx_applet_type != AppletType_None) { rc = hidInitialize(); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_HID)); } rc = timeInitialize(); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_Time)); rc = fsInitialize(); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_FS)); fsdevMountSdmc(); }
void __attribute__((weak)) __libnx_initheap(void) { void* addr; size_t size = 0; size_t mem_available = 0, mem_used = 0; if (envHasHeapOverride()) { addr = envGetHeapOverrideAddr(); size = envGetHeapOverrideSize(); } else { if (__nx_heap_size==0) { svcGetInfo(&mem_available, 6, CUR_PROCESS_HANDLE, 0); svcGetInfo(&mem_used, 7, CUR_PROCESS_HANDLE, 0); if (mem_available > mem_used+0x200000) size = (mem_available - mem_used - 0x200000) & ~0x1FFFFF; if (size==0) size = 0x2000000*16; } else { size = __nx_heap_size; } Result rc = svcSetHeapSize(&addr, size); if (R_FAILED(rc)) fatalSimple(MAKERESULT(Module_Libnx, LibnxError_HeapAllocFailed)); } // Newlib extern char* fake_heap_start; extern char* fake_heap_end; fake_heap_start = (char*)addr; fake_heap_end = (char*)addr + size; }
void postprocess(IpcParsedCommand &r, u64 cmd_id) override { if (this->active_object == this->service_object && (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext)) { IpcCommand c; ipcInitialize(&c); this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size); } else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) { if (cmd_id == IpcCtrl_Cmd_ConvertCurrentObjectToDomain) { this->is_domain = true; this->domain = std::make_shared<DomainOwner>(); struct { u64 magic; u64 result; u32 domain_id; } *resp = (decltype(resp))cur_out_r.Raw; Result rc; if (R_FAILED((rc = this->domain->set_object(this->service_object, resp->domain_id)))) { fatalSimple(rc); } this->mitm_domain_id = resp->domain_id; this->forward_service.type = ServiceType_Domain; this->forward_service.object_id = resp->domain_id; } } }
Result handle_message(IpcParsedCommand &r) override { IpcCommand c; ipcInitialize(&c); u64 cmd_id = ((u32 *)r.Raw)[2]; Result retval = 0xF601; cur_out_r.NumHandles = 0; Log(armGetTls(), 0x100); u32 *cmdbuf = (u32 *)armGetTls(); if (r.CommandType == IpcCommandType_Close) { Reboot(); } if (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext) { std::shared_ptr<IServiceObject> obj; if (r.IsDomainMessage) { obj = this->domain->get_domain_object(r.ThisObjectId); if (obj != nullptr && r.MessageType == DomainMessageType_Close) { if (r.ThisObjectId == this->mitm_domain_id) { Reboot(); } this->domain->delete_object(r.ThisObjectId); struct { u64 magic; u64 result; } *o_resp; o_resp = (decltype(o_resp)) ipcPrepareHeaderForDomain(&c, sizeof(*o_resp), 0); *(DomainMessageHeader *)((uintptr_t)o_resp - sizeof(DomainMessageHeader)) = {0}; o_resp->magic = SFCO_MAGIC; o_resp->result = 0x0; Log(armGetTls(), 0x100); return o_resp->result; } } else { obj = this->service_object; } if (obj != nullptr) { retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size); if (R_SUCCEEDED(retval)) { if (r.IsDomainMessage) { ipcParseForDomain(&cur_out_r); } else { ipcParse(&cur_out_r); } return retval; } } } else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) { /* Ipc Clone Current Object. */ retval = serviceIpcDispatch(&forward_service); Log(armGetTls(), 0x100); if (R_SUCCEEDED(retval)) { ipcParse(&cur_out_r); struct { u64 magic; u64 result; } *resp = (decltype(resp))cur_out_r.Raw; retval = resp->result; if ((cmd_id == IpcCtrl_Cmd_CloneCurrentObject || cmd_id == IpcCtrl_Cmd_CloneCurrentObjectEx)) { if (R_SUCCEEDED(retval)) { Handle s_h; Handle c_h; Result rc; if (R_FAILED((rc = svcCreateSession(&s_h, &c_h, 0, 0)))) { fatalSimple(rc); } if (cur_out_r.NumHandles != 1) { Reboot(); } MitMSession<T> *new_sess = new MitMSession<T>((MitMServer<T> *)this->server, s_h, c_h, cur_out_r.Handles[0]); new_sess->service_object = this->service_object; if (this->is_domain) { new_sess->is_domain = true; new_sess->domain = this->domain; new_sess->mitm_domain_id = this->mitm_domain_id; new_sess->forward_service.type = this->forward_service.type; new_sess->forward_service.object_id = this->forward_service.object_id; } this->get_manager()->add_waitable(new_sess); ipcSendHandleMove(&c, c_h); struct { u64 magic; u64 result; } *o_resp; o_resp = (decltype(o_resp)) ipcPrepareHeader(&c, sizeof(*o_resp)); o_resp->magic = SFCO_MAGIC; o_resp->result = 0x0; } } } Log(armGetTls(), 0x100); return retval; } /* 0xF601 --> Dispatch onwards. */ if (retval == 0xF601) { /* Patch PID Descriptor, if relevant. */ if (r.HasPid) { /* [ctrl 0] [ctrl 1] [handle desc 0] [pid low] [pid high] */ cmdbuf[4] = 0xFFFE0000UL | (cmdbuf[4] & 0xFFFFUL); } Log(armGetTls(), 0x100); retval = serviceIpcDispatch(&forward_service); if (R_SUCCEEDED(retval)) { if (r.IsDomainMessage) { ipcParseForDomain(&cur_out_r); } else { ipcParse(&cur_out_r); } struct { u64 magic; u64 result; } *resp = (decltype(resp))cur_out_r.Raw; retval = resp->result; } } Log(armGetTls(), 0x100); Log(&cmd_id, sizeof(u64)); u64 retval_for_log = retval; Log(&retval_for_log, sizeof(u64)); if (R_FAILED(retval)) { // Reboot(); } if (retval == 0xA08) { Reboot(); } return retval; }