Skip to content

Commit

Permalink
Implement lazy loading, ground work for sysmodule support
Browse files Browse the repository at this point in the history
  • Loading branch information
ckurtz22 authored and m4xw committed Sep 15, 2019
1 parent 661de37 commit 8df5812
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 9 deletions.
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SOURCES := source
DATA := data
INCLUDES := include
#ROMFS := romfs
APP_TITLEID := 00FF00006D7470FF

# Meta
APP_TITLE := "mtp-server-nx"
Expand All @@ -57,7 +58,7 @@ ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)

CFLAGS += $(INCLUDE) -D__SWITCH__
CFLAGS += $(INCLUDE) -D__SWITCH__ -DWANT_APPLET

CXXFLAGS := $(CFLAGS) -fno-rtti -std=gnu++17

Expand Down Expand Up @@ -187,22 +188,18 @@ DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
ifeq ($(strip $(APP_JSON)),)

all : $(OUTPUT).nro
all : applet
applet : $(OUTPUT).nro
sysmodule : $(OUTPUT).nsp

ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif

else

all : $(OUTPUT).nsp
$(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm
$(OUTPUT).nso : $(OUTPUT).elf
endif

$(OUTPUT).elf : $(OFILES)
$(OFILES_SRC) : $(HFILES_BIN)
Expand Down
11 changes: 10 additions & 1 deletion include/SwitchMtpDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SwitchMtpDatabase : public android::MtpDatabase {
std::string display_name;
std::string path;
std::time_t last_modified;
bool scanned;
};

MtpServer* local_server;
Expand Down Expand Up @@ -106,6 +107,8 @@ class SwitchMtpDatabase : public android::MtpDatabase {
entry.path = p.string();
entry.object_format = MTP_FORMAT_ASSOCIATION;
entry.object_size = 0;
entry.scanned = false;

struct stat result;
stat(p.string().c_str(), &result);
entry.last_modified = result.st_mtime;
Expand All @@ -114,7 +117,6 @@ class SwitchMtpDatabase : public android::MtpDatabase {
if (local_server)
local_server->sendObjectAdded(handle);

parse_directory (p, handle, storage);
} else {
try {
entry.storage_id = storage;
Expand Down Expand Up @@ -154,6 +156,9 @@ class SwitchMtpDatabase : public android::MtpDatabase {
{
add_file_entry(*it, parent, storage);
}

if (db.find(parent) != db.end())
db.at(parent).scanned = true;
}

void readFiles(const std::string& sourcedir, const std::string& display, MtpStorageID storage, bool hidden)
Expand Down Expand Up @@ -305,6 +310,10 @@ class SwitchMtpDatabase : public android::MtpDatabase {

if (parent == MTP_PARENT_ROOT)
parent = 0;

// Scan unscanned directories
else if (!db.at(parent).scanned)
parse_directory (db.at(parent).path, parent, storageID);

try
{
Expand Down
172 changes: 172 additions & 0 deletions mtp-server-nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"name": "mtp-server-nx",
"title_id": "0x00FF00006D7470FF",
"title_id_range_min": "0x00FF00006D7470FF",
"title_id_range_max": "0x00FF00006D7470FF",
"main_thread_stack_size": "0x004000",
"main_thread_priority": 44,
"default_cpu_id": 3,
"process_category": 0,
"is_retail": true,
"pool_partition": 2,
"is_64_bit": true,
"address_space_type": 1,
"filesystem_access": {
"permissions": "0xffffffffffffffff"
},
"service_access": ["*"],
"service_host": ["*"],
"kernel_capabilities": [{
"type": "kernel_flags",
"value": {
"highest_thread_priority": 63,
"lowest_thread_priority": 24,
"lowest_cpu_id": 3,
"highest_cpu_id": 3
}
}, {
"type": "syscalls",
"value": {
"svcUnknown": "0x00",
"svcSetHeapSize": "0x01",
"svcSetMemoryPermission": "0x02",
"svcSetMemoryAttribute": "0x03",
"svcMapMemory": "0x04",
"svcUnmapMemory": "0x05",
"svcQueryMemory": "0x06",
"svcExitProcess": "0x07",
"svcCreateThread": "0x08",
"svcStartThread": "0x09",
"svcExitThread": "0x0a",
"svcSleepThread": "0x0b",
"svcGetThreadPriority": "0x0c",
"svcSetThreadPriority": "0x0d",
"svcGetThreadCoreMask": "0x0e",
"svcSetThreadCoreMask": "0x0f",
"svcGetCurrentProcessorNumber": "0x10",
"svcSignalEvent": "0x11",
"svcClearEvent": "0x12",
"svcMapSharedMemory": "0x13",
"svcUnmapSharedMemory": "0x14",
"svcCreateTransferMemory": "0x15",
"svcCloseHandle": "0x16",
"svcResetSignal": "0x17",
"svcWaitSynchronization": "0x18",
"svcCancelSynchronization": "0x19",
"svcArbitrateLock": "0x1a",
"svcArbitrateUnlock": "0x1b",
"svcWaitProcessWideKeyAtomic": "0x1c",
"svcSignalProcessWideKey": "0x1d",
"svcGetSystemTick": "0x1e",
"svcConnectToNamedPort": "0x1f",
"svcSendSyncRequestLight": "0x20",
"svcSendSyncRequest": "0x21",
"svcSendSyncRequestWithUserBuffer": "0x22",
"svcSendAsyncRequestWithUserBuffer": "0x23",
"svcGetProcessId": "0x24",
"svcGetThreadId": "0x25",
"svcBreak": "0x26",
"svcOutputDebugString": "0x27",
"svcReturnFromException": "0x28",
"svcGetInfo": "0x29",
"svcFlushEntireDataCache": "0x2a",
"svcFlushDataCache": "0x2b",
"svcMapPhysicalMemory": "0x2c",
"svcUnmapPhysicalMemory": "0x2d",
"svcGetFutureThreadInfo": "0x2e",
"svcGetLastThreadInfo": "0x2f",
"svcGetResourceLimitLimitValue": "0x30",
"svcGetResourceLimitCurrentValue": "0x31",
"svcSetThreadActivity": "0x32",
"svcGetThreadContext3": "0x33",
"svcWaitForAddress": "0x34",
"svcSignalToAddress": "0x35",
"svcUnknown": "0x36",
"svcUnknown": "0x37",
"svcUnknown": "0x38",
"svcUnknown": "0x39",
"svcUnknown": "0x3a",
"svcUnknown": "0x3b",
"svcDumpInfo": "0x3c",
"svcDumpInfoNew": "0x3d",
"svcUnknown": "0x3e",
"svcUnknown": "0x3f",
"svcCreateSession": "0x40",
"svcAcceptSession": "0x41",
"svcReplyAndReceiveLight": "0x42",
"svcReplyAndReceive": "0x43",
"svcReplyAndReceiveWithUserBuffer": "0x44",
"svcCreateEvent": "0x45",
"svcUnknown": "0x46",
"svcUnknown": "0x47",
"svcMapPhysicalMemoryUnsafe": "0x48",
"svcUnmapPhysicalMemoryUnsafe": "0x49",
"svcSetUnsafeLimit": "0x4a",
"svcCreateCodeMemory": "0x4b",
"svcControlCodeMemory": "0x4c",
"svcSleepSystem": "0x4d",
"svcReadWriteRegister": "0x4e",
"svcSetProcessActivity": "0x4f",
"svcCreateSharedMemory": "0x50",
"svcMapTransferMemory": "0x51",
"svcUnmapTransferMemory": "0x52",
"svcCreateInterruptEvent": "0x53",
"svcQueryPhysicalAddress": "0x54",
"svcQueryIoMapping": "0x55",
"svcCreateDeviceAddressSpace": "0x56",
"svcAttachDeviceAddressSpace": "0x57",
"svcDetachDeviceAddressSpace": "0x58",
"svcMapDeviceAddressSpaceByForce": "0x59",
"svcMapDeviceAddressSpaceAligned": "0x5a",
"svcMapDeviceAddressSpace": "0x5b",
"svcUnmapDeviceAddressSpace": "0x5c",
"svcInvalidateProcessDataCache": "0x5d",
"svcStoreProcessDataCache": "0x5e",
"svcFlushProcessDataCache": "0x5f",
"svcDebugActiveProcess": "0x60",
"svcBreakDebugProcess": "0x61",
"svcTerminateDebugProcess": "0x62",
"svcGetDebugEvent": "0x63",
"svcContinueDebugEvent": "0x64",
"svcGetProcessList": "0x65",
"svcGetThreadList": "0x66",
"svcGetDebugThreadContext": "0x67",
"svcSetDebugThreadContext": "0x68",
"svcQueryDebugProcessMemory": "0x69",
"svcReadDebugProcessMemory": "0x6a",
"svcWriteDebugProcessMemory": "0x6b",
"svcSetHardwareBreakPoint": "0x6c",
"svcGetDebugThreadParam": "0x6d",
"svcUnknown": "0x6e",
"svcGetSystemInfo": "0x6f",
"svcCreatePort": "0x70",
"svcManageNamedPort": "0x71",
"svcConnectToPort": "0x72",
"svcSetProcessMemoryPermission": "0x73",
"svcMapProcessMemory": "0x74",
"svcUnmapProcessMemory": "0x75",
"svcQueryProcessMemory": "0x76",
"svcMapProcessCodeMemory": "0x77",
"svcUnmapProcessCodeMemory": "0x78",
"svcCreateProcess": "0x79",
"svcStartProcess": "0x7a",
"svcTerminateProcess": "0x7b",
"svcGetProcessInfo": "0x7c",
"svcCreateResourceLimit": "0x7d",
"svcSetResourceLimitLimitValue": "0x7e",
"svcCallSecureMonitor": "0x7f"
}
}, {
"type": "min_kernel_version",
"value": "0x0030"
}, {
"type": "handle_table_size",
"value": 1023
}, {
"type": "debug_flags",
"value": {
"allow_debug": false,
"force_debug": true
}
}]
}
64 changes: 64 additions & 0 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,65 @@

using namespace android;

#ifdef WANT_SYSMODULE
extern "C"
{
#define INNER_HEAP_SIZE 0x80000
extern u32 __start__;

size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];

void __libnx_initheap(void);
void __appInit(void);
void __appExit(void);
}

u32 __nx_applet_type = AppletType_None;

void __libnx_initheap(void)
{
void *addr = nx_inner_heap;
size_t size = nx_inner_heap_size;

extern char *fake_heap_start;
extern char *fake_heap_end;

fake_heap_start = (char *)addr;
fake_heap_end = (char *)addr + size;
}

void __appInit(void)
{
smInitialize();
Result rc = setsysInitialize();
if (R_SUCCEEDED(rc)) {
SetSysFirmwareVersion fw;
rc = setsysGetFirmwareVersion(&fw);
if (R_SUCCEEDED(rc))
hosversionSet(MAKEHOSVERSION(fw.major, fw.minor, fw.micro));
setsysExit();
}
fsInitialize();
hidInitialize();
fsdevMountSdmc();
}

MtpServer* serverExit = NULL;

void __appExit(void)
{
serverExit->stop();
usbExit();
hidExit();
fsExit();
smExit();
}
#endif // WANT_SYSMODULE

static void stop_thread(MtpServer* server)
{
#ifdef WANT_APPLET
while (appletMainLoop())
{
hidScanInput();
Expand All @@ -42,6 +99,11 @@ static void stop_thread(MtpServer* server)
break;
}
}
#endif // WANT_APPLET

#ifdef WANT_SYSMODULE
serverExit = server;
#endif // WANT_SYSMODULE
}

int main(int argc, char* argv[])
Expand Down Expand Up @@ -71,8 +133,10 @@ int main(int argc, char* argv[])
}
}

#ifdef WANT_APPLET
consoleInit(NULL);
std::cout << "Press + to exit";
#endif // WANT_APPLET

struct usb_device_descriptor device_descriptor = {
.bLength = USB_DT_DEVICE_SIZE,
Expand Down

0 comments on commit 8df5812

Please sign in to comment.