diff --git a/nshlib/Kconfig b/nshlib/Kconfig index df11aa072..c208b41b5 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -700,8 +700,9 @@ config NSH_ROMFSETC default n depends on FS_ROMFS ---help--- - Mount a ROMFS filesystem at /etc and provide a startup script - at /etc/init.d/rcS. The default startup script will mount + Mount a ROMFS filesystem at /etc and provide a system init + script at /etc/init.d/rc.sysinit and a startup script + at /etc/init.d/rcS. The default system init script will mount a FAT FS RAMDISK at /tmp but the logic is easily extensible. if NSH_ROMFSETC @@ -711,8 +712,9 @@ config NSH_CROMFSETC default n depends on FS_CROMFS ---help--- - Mount a CROMFS filesystem at /etc and provide a compressed startup - script at /etc/init.d/rcS. + Mount a CROMFS filesystem at /etc and provide a compressed system + init script at /etc/init.d/rc.sysinit and a startup script + at /etc/init.d/rcS. config NSH_ROMFSRC bool "Support ROMFS login script" @@ -734,6 +736,14 @@ config NSH_ROMFSMOUNTPT can be changed with this setting. This must be a absolute path beginning with '/'. +config NSH_SYSINITSCRIPT + string "Relative path to sysinit script" + default "init.d/rc.sysinit" + ---help--- + This is the relative path to the sysinit script within the mountpoint. + The default is init.d/rc.sysinit. This is a relative path and must not + start with '/'. + config NSH_INITSCRIPT string "Relative path to startup script" default "init.d/rcS" diff --git a/nshlib/README.md b/nshlib/README.md index 471695e0f..8c52c7a99 100644 --- a/nshlib/README.md +++ b/nshlib/README.md @@ -176,21 +176,28 @@ the `echo $PWD` command. - `PWD` - The current working directory - `OLDPWD` - The previous working directory -## NSH Start-Up Script +## NSH System-init And Start-Up Script -NSH supports options to provide a start up script for NSH. In general this -capability is enabled with `CONFIG_NSH_ROMFSETC`, but has several other related -configuration options as described in the final section of this README. This -capability also depends on: +NSH supports options to provide a system init script and start up script for NSH. +In general this capability is enabled with `CONFIG_NSH_ROMFSETC`, but has +several other related configuration options as described in the final section +of this README. This capability also depends on: - `CONFIG_DISABLE_MOUNTPOINT` not set - `CONFIG_FS_ROMFS` -### Default Start-Up Behavior +### Default Script Behavior The implementation that is provided is intended to provide great flexibility for -the use of Start-Up files. This paragraph will discuss the general behavior when -all of the configuration options are set to the default values. +the use of script files, include system init file and start-up file. This +paragraph will discuss the general behavior when all of the configuration +options are set to the default values. + +System-init script is executed before Start-up script. The system-init script +is mainly used for file system mounting and core system service startup, and the +start-up script is used for application and other system service startup. So, +Between them, some initialize can use filesystem and core system service, +Examples: Peripheral driver initialize at `boardctl(BOARDIOC_FINALINIT, 0)`. In this default case, enabling `CONFIG_NSH_ROMFSETC` will cause NSH to behave as follows at NSH startup time: @@ -201,9 +208,11 @@ follows at NSH startup time: ``` | `--init.d/ `-- rcS + `-- rc.sysinit ```` Where `rcS` is the NSH start-up script + Where `rc.sysinit` is the NSH system-init script - NSH will then mount the ROMFS file system at `/etc`, resulting in: @@ -213,9 +222,10 @@ follows at NSH startup time: `--etc/ `--init.d/ `-- rcS + `-- rc.sysinit ``` -- By default, the contents of `rcS` script are: +- By default, the contents of `rc.sysinit` script are: ```shell # Create a RAMDISK and mount it at XXXRDMOUNTPOINTXXX @@ -225,8 +235,9 @@ follows at NSH startup time: mount -t vfat /dev/ram1 /tmp ``` -- NSH will execute the script at `/etc/init.d/rcS` at start-up (before the first - NSH prompt. After execution of the script, the root FS will look like: +- NSH will execute the script at `/etc/init.d/rc.sysinit` at system init + before the first NSH prompt. After execution of the script, the root + FS will look like: ``` |--dev/ @@ -235,6 +246,7 @@ follows at NSH startup time: |--etc/ | `--init.d/ | `-- rcS + | `-- rc.sysinit `--tmp/ ``` @@ -262,21 +274,27 @@ there are three things to study: - The file `apps/nshlib/rcS.template` (OR, if `CONFIG_NSH_ARCHROMFS` is defined, `include/arch/board/rcs.template`) -3. `rcS.template`. The file `apps/nshlib/rcS.template` contains the general form +3. `rc.sysinit.template`. The file `apps/nshlib/rc.sysinit.template` contains + the general form of the `rc.sysinit.template` file; configured values + are plugged into this template file to produce the final `rc.sysinit` file. + + `rcS.template`. The file `apps/nshlib/rcS.template` contains the general form of the `rcS` file; configured values are plugged into this template file to produce the final `rcS` file. -**Note**: `apps/nshlib/rcS.template` generates the standard, default -`nsh_romfsimg.h` file. If `CONFIG_NSH_ARCHROMFS` is defined in the NuttX -configuration file, then a custom, board-specific `nsh_romfsimg.h` file residing -in `boards////include` will be used. **Note** when the OS is -configured, `include/arch/board` will be linked to +**Note**: `apps/nshlib/rc.sysinit.template` and ` apps/nshlib/rcS.template` +generates the standard, default `nsh_romfsimg.h` file. If `CONFIG_NSH_ARCHROMFS` +is defined in the NuttX configuration file, then a custom, board-specific +`nsh_romfsimg.h` file residing in `boards////include` will be +used. **Note** when the OS is configured, `include/arch/board` will be linked to `boards////include`. -All of the startup-behavior is contained in `rcS.template`. The role of -`mkromfsimg.sh` is to (1) apply the specific configuration settings to -`rcS.template` to create the final `rcS`, and (2) to generate the header file -`nsh_romfsimg.h` containing the ROMFS file system image. +All of the startup-behavior is contained in `rc.sysinit.template` and +`rcS.template`. The role of `mkromfsimg.sh` is to (1) apply the specific +configuration settings to `rc.sysinit.template` to create the final +`rc.sysinit.template`, and `rcS.template` to create the final `rcS` and +(2) to generate the header file `nsh_romfsimg.h` containing the ROMFS file +system image. ## Simple Commands @@ -1803,8 +1821,9 @@ The behavior of NSH can be modified with the following settings in the `/dev/mmcsdN` where `N` is the minor number. Default is zero. - `CONFIG_NSH_ROMFSETC` – Mount a ROMFS file system at `/etc` and provide a - startup script at `/etc/init.d/rcS`. The default startup script will mount a - FAT FS RAMDISK at `/tmp` but the logic is easily extensible. + system init script at `/etc/init.d/rc.sysinit` and a startup script at + `/etc/init.d/rcS`. The default system init script will mount a FAT FS RAMDISK + at `/tmp` but the logic is easily extensible. - `CONFIG_NSH_CONSOLE` @@ -1944,6 +1963,10 @@ configuration setting apply: `/etc`, but that can be changed with this setting. This must be a absolute path beginning with `/`. +- `CONFIG_NSH_SYSINITSCRIPT` – This is the relative path to the system init + script within the mountpoint. The default is `init.d/rc.sysinit`. This + is a relative path and must not start with `/`. + - `CONFIG_NSH_INITSCRIPT` – This is the relative path to the startup script within the mountpoint. The default is `init.d/rcS`. This is a relative path and must not start with `/`. diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 539013407..60798cc9b 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -340,10 +340,17 @@ # define CONFIG_NSH_ROMFSMOUNTPT "/etc" # endif +# ifndef CONFIG_NSH_SYSINITSCRIPT +# define CONFIG_NSH_SYSINITSCRIPT "init.d/rc.sysinit" +# endif + # ifndef CONFIG_NSH_INITSCRIPT # define CONFIG_NSH_INITSCRIPT "init.d/rcS" # endif +# undef NSH_SYSINITPATH +# define NSH_SYSINITPATH CONFIG_NSH_ROMFSMOUNTPT "/" CONFIG_NSH_SYSINITSCRIPT + # undef NSH_INITPATH # define NSH_INITPATH CONFIG_NSH_ROMFSMOUNTPT "/" CONFIG_NSH_INITSCRIPT @@ -829,6 +836,7 @@ int nsh_usbconsole(void); int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, FAR const char *path); #ifdef CONFIG_NSH_ROMFSETC +int nsh_sysinitscript(FAR struct nsh_vtbl_s *vtbl); int nsh_initscript(FAR struct nsh_vtbl_s *vtbl); #ifdef CONFIG_NSH_ROMFSRC int nsh_loginscript(FAR struct nsh_vtbl_s *vtbl); diff --git a/nshlib/nsh_consolemain.c b/nshlib/nsh_consolemain.c index 135923cd6..13cb6eb94 100644 --- a/nshlib/nsh_consolemain.c +++ b/nshlib/nsh_consolemain.c @@ -78,9 +78,9 @@ int nsh_consolemain(int argc, FAR char *argv[]) #endif #if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT) - /* Execute the start-up script */ + /* Execute the system init script */ - nsh_initscript(&pstate->cn_vtbl); + nsh_sysinitscript(&pstate->cn_vtbl); #endif #ifdef CONFIG_NSH_NETINIT @@ -95,6 +95,12 @@ int nsh_consolemain(int argc, FAR char *argv[]) boardctl(BOARDIOC_FINALINIT, 0); #endif +#if defined(CONFIG_NSH_ROMFSETC) && !defined(CONFIG_NSH_DISABLESCRIPT) + /* Execute the start-up script */ + + nsh_initscript(&pstate->cn_vtbl); +#endif + /* Execute the session */ ret = nsh_session(pstate, true, argc, argv); diff --git a/nshlib/nsh_script.c b/nshlib/nsh_script.c index 9d9a81263..433f1529c 100644 --- a/nshlib/nsh_script.c +++ b/nshlib/nsh_script.c @@ -145,6 +145,22 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, return ret; } +/**************************************************************************** + * Name: nsh_sysinitscript + * + * Description: + * Attempt to execute the configured system initialization script. This + * script should be executed once when NSH starts. + * + ****************************************************************************/ + +#ifdef CONFIG_NSH_ROMFSETC +int nsh_sysinitscript(FAR struct nsh_vtbl_s *vtbl) +{ + return nsh_script(vtbl, "sysinit", NSH_SYSINITPATH); +} +#endif + /**************************************************************************** * Name: nsh_initscript * diff --git a/nshlib/rc.sysinit.template b/nshlib/rc.sysinit.template new file mode 100644 index 000000000..452760e22 --- /dev/null +++ b/nshlib/rc.sysinit.template @@ -0,0 +1,5 @@ +# Create a RAMDISK and mount it at XXXRDMOUNTPOINTXXX + +mkrd -m XXXMKRDMINORXXX -s XXMKRDSECTORSIZEXXX XXMKRDBLOCKSXXX +mkfatfs /dev/ramXXXMKRDMINORXXX +mount -t vfat /dev/ramXXXMKRDMINORXXX XXXRDMOUNTPOINTXXX diff --git a/nshlib/rcS.template b/nshlib/rcS.template index 452760e22..e69de29bb 100644 --- a/nshlib/rcS.template +++ b/nshlib/rcS.template @@ -1,5 +0,0 @@ -# Create a RAMDISK and mount it at XXXRDMOUNTPOINTXXX - -mkrd -m XXXMKRDMINORXXX -s XXMKRDSECTORSIZEXXX XXMKRDBLOCKSXXX -mkfatfs /dev/ramXXXMKRDMINORXXX -mount -t vfat /dev/ramXXXMKRDMINORXXX XXXRDMOUNTPOINTXXX