diff --git a/arch/sim/Kconfig b/arch/sim/Kconfig index 3499d77b61..4d9fa0bf6e 100644 --- a/arch/sim/Kconfig +++ b/arch/sim/Kconfig @@ -159,6 +159,17 @@ config SIM_HOSTFS ---help--- Access host filesystem through HostFS. +config SIM_IMAGEPATH_AS_CWD + bool "Simulator switch working directory" + default n + ---help--- + If this option is enabled, the working path of nuttx will be modified + to the folder where the nuttx file is located. + It affects the file access of hostfs, which will start looking for + files based on the nuttx image folder. + Otherwise the default $CWD will be used as the starting path for the search. + Absolute paths are never affected. + choice prompt "Simulated Network Interface" default SIM_NETDEV diff --git a/arch/sim/src/nuttx-names.in b/arch/sim/src/nuttx-names.in index 16d12bc05c..d2e079446f 100644 --- a/arch/sim/src/nuttx-names.in +++ b/arch/sim/src/nuttx-names.in @@ -39,6 +39,7 @@ NXSYMBOLS(backtrace) NXSYMBOLS(bind) NXSYMBOLS(calloc) NXSYMBOLS(chmod) +NXSYMBOLS(chdir) NXSYMBOLS(chown) NXSYMBOLS(clock_gettime) NXSYMBOLS(close) @@ -106,6 +107,7 @@ NXSYMBOLS(pthread_sigmask) NXSYMBOLS(puts) NXSYMBOLS(read) NXSYMBOLS(readdir) +NXSYMBOLS(readlink) NXSYMBOLS(readv) NXSYMBOLS(realloc) NXSYMBOLS(recvfrom) diff --git a/arch/sim/src/sim/posix/sim_hostmisc.c b/arch/sim/src/sim/posix/sim_hostmisc.c index 70c975d639..3ed0104810 100644 --- a/arch/sim/src/sim/posix/sim_hostmisc.c +++ b/arch/sim/src/sim/posix/sim_hostmisc.c @@ -22,10 +22,19 @@ * Included Files ****************************************************************************/ +#include +#include #include +#include +#include #include "sim_internal.h" +#ifdef CONFIG_HOST_MACOS +#include +#include +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -88,3 +97,38 @@ int host_backtrace(void** array, int size) return backtrace(array, size); #endif } + +/**************************************************************************** + * Name: host_init_cwd + ****************************************************************************/ + +#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD +void host_init_cwd(void) +{ + char *name; + char path[PATH_MAX]; + int len = PATH_MAX; + + /* Get the absolute path of the executable file */ + +# ifdef CONFIG_HOST_LINUX + len = readlink("/proc/self/exe", path, len); + if (len < 0) + { + perror("readlink failed"); + return; + } +# else + if (_NSGetExecutablePath(path, &len) < 0) + { + perror("_NSGetExecutablePath failed"); + return; + } +# endif + + path[len] = '\0'; + name = strrchr(path, '/'); + *++name = '\0'; + chdir(path); +} +#endif diff --git a/arch/sim/src/sim/sim_initialize.c b/arch/sim/src/sim/sim_initialize.c index 8e778a5c3f..cf66efc330 100644 --- a/arch/sim/src/sim/sim_initialize.c +++ b/arch/sim/src/sim/sim_initialize.c @@ -254,6 +254,10 @@ static int sim_loop_task(int argc, char **argv) void up_initialize(void) { +#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD + host_init_cwd(); +#endif + #ifdef CONFIG_PM /* Initialize the power management subsystem. This MCU-specific function * must be called *very* early in the initialization sequence *before* any diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index 346fc8fe7a..74bbd9e027 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -164,6 +164,10 @@ void *sim_doirq(int irq, void *regs); void host_abort(int status); int host_backtrace(void** array, int size); +#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD +void host_init_cwd(void); +#endif + /* sim_hostmemory.c *********************************************************/ void *host_allocheap(size_t sz); diff --git a/arch/sim/src/sim/win/sim_hostmisc.c b/arch/sim/src/sim/win/sim_hostmisc.c index a7c9237d90..8fe105026f 100644 --- a/arch/sim/src/sim/win/sim_hostmisc.c +++ b/arch/sim/src/sim/win/sim_hostmisc.c @@ -22,7 +22,15 @@ * Included Files ****************************************************************************/ +#include +#include +#include + +#include #include +#include + +#include "sim_internal.h" /**************************************************************************** * Public Functions @@ -47,3 +55,32 @@ int host_backtrace(void** array, int size) { return CaptureStackBackTrace(0, size, array, NULL); } + +/**************************************************************************** + * Name: host_init_cwd + ****************************************************************************/ + +#ifdef CONFIG_SIM_IMAGEPATH_AS_CWD +void host_init_cwd(void) +{ + char *name; + char path[MAX_PATH]; + + /* Get the absolute path of the executable file */ + + if (GetModuleFileNameA(GetModuleHandleA(NULL), path, MAX_PATH) == 0) + { + perror("GetModuleFileNameA failed"); + return; + } + + name = strrchr(path, '/'); + if (name == NULL) + { + name = strrchr(path, '\\'); + } + + *++name = '\0'; + _chdir(path); +} +#endif