diff --git a/crt0/switch_crt0.s b/crt0/switch_crt0.s index e5353896..a4e739a4 100644 --- a/crt0/switch_crt0.s +++ b/crt0/switch_crt0.s @@ -39,8 +39,10 @@ bss_loop: bl __libnx_init // call entrypoint - mov x0, #0 // argc - mov x1, #0 // argv + adrp x0, __system_argc // argc + ldr w0, [x0, #:lo12:__system_argc] + adrp x1, __system_argv // argv + ldr x1, [x1, #:lo12:__system_argv] adrp x30, __libnx_exit add x30, x30, #:lo12:__libnx_exit b main diff --git a/nx/source/devices/fs_dev.c b/nx/source/devices/fs_dev.c index 3de70048..f04b7043 100644 --- a/nx/source/devices/fs_dev.c +++ b/nx/source/devices/fs_dev.c @@ -352,9 +352,9 @@ int fsdevUnmountDevice(const char *name) /*! Initialize SDMC device */ Result fsdevInit(void) { - /*ssize_t units; + ssize_t units; uint32_t code; - char *p;*/ + char *p; Result rc = 0; FsFileSystem fs; fsdev_fsdevice *device = NULL; @@ -390,8 +390,7 @@ Result fsdevInit(void) fsdev_fsdevice_cwd = fsdev_fsdevice_default; } - //TODO: Re-enable this once __system_argc/__system_argv are actually defined. - /*if(__system_argc != 0 && __system_argv[0] != NULL) + if(__system_argc != 0 && __system_argv[0] != NULL) { if(FindDevice(__system_argv[0]) == dev) { @@ -426,7 +425,7 @@ Result fsdevInit(void) } } } - }*/ + } } } diff --git a/nx/source/system/init.c b/nx/source/system/init.c index 0764c0dd..f7674ba2 100644 --- a/nx/source/system/init.c +++ b/nx/source/system/init.c @@ -5,6 +5,8 @@ void __nx_exit(int rc); void virtmemSetup(); void newlibSetup(Handle main_thread); +void __system_initArgv(void); + #define INNER_HEAP_SIZE 0x200000 __attribute__((weak)) size_t __nx_inner_heap_size = INNER_HEAP_SIZE; __attribute__((weak)) char __nx_inner_heap[INNER_HEAP_SIZE]; @@ -59,6 +61,9 @@ void __attribute__((weak)) __libnx_init(Handle main_thread) virtmemSetup(); _SetupHeap(); + // Build argc/argv if present + __system_initArgv(); + // Initialize services. __appInit(); diff --git a/nx/source/system/initArgv.c b/nx/source/system/initArgv.c new file mode 100644 index 00000000..ab8d4768 --- /dev/null +++ b/nx/source/system/initArgv.c @@ -0,0 +1,57 @@ +#include + +#include + +// System globals we define here +int __system_argc; +char** __system_argv; + +extern char* fake_heap_start; +extern char* fake_heap_end; + +void __system_initArgv(void) +{ + //TODO: How to load args? + __system_argc = 0; + __system_argv = NULL; + + /* + int i; + const char* arglist = envGetSystemArgList(); + const char* temp = arglist; + + // Check if the argument list is present + if (!temp) + return; + + // Retrieve argc + __system_argc = *(u32*)temp; + temp += sizeof(u32); + + // Find the end of the argument data + for (i = 0; i < __system_argc; i ++) + { + for (; *temp; temp ++); + temp ++; + } + + // Reserve heap memory for argv data + u32 argSize = temp - arglist - sizeof(u32); + __system_argv = (char**)fake_heap_start; + fake_heap_start += sizeof(char**)*(__system_argc + 1); + char* argCopy = fake_heap_start; + fake_heap_start += argSize; + + // Fill argv array + memcpy(argCopy, &arglist[4], argSize); + temp = argCopy; + for (i = 0; i < __system_argc; i ++) + { + __system_argv[i] = (char*)temp; + for (; *temp; temp ++); + temp ++; + } + __system_argv[__system_argc] = NULL; + */ +} +