You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nuttx.apache.org by Russell Haley <ru...@gmail.com> on 2023/02/02 02:00:01 UTC

Re: Including Lua scripts on filesystem

On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <f....@gmail.com>
wrote:

> Hello,
>
> Indeed the "proper" way of including a script would be to store it in a
> file system.
>
> However, when I needed to include a single and small script and I didn't
> want to introduce a complete FS just for this, I used xxd.
> xxd can convert any file to a C header file.
>
> You can then include the header, and access the whole file as a variable.
> Here is an example:
>
> I added this in my app Makefile:
>
> # Setup any special pre-build context
> context: header
>         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua > json_lua.h &&
> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>
>
>
> And then I used the file like this:
>
> #include "lua.h"#include "lauxlib.h"#include <string.h>
>  #include "json_lua.h"
>  static int luaopen_json(lua_State * L);
>
>  void ExtLibs_load(lua_State * L){
>         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>         luaL_requiref(L, "json", luaopen_json, 1);
>         lua_pop(L, 1);#endif}
>
>  int luaopen_json(lua_State * L){
>         const char * modname = lua_tostring(L, 1);
>
>         if (strcmp(modname, "json") != 0)
>                 return luaL_error(L, "cannot load json module");
>
>         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len, "json",
> "t") != LUA_OK)
>                 return lua_error(L);
>
>         lua_call(L, 0, 1);
>
>         return 1;}
>
>
> I hope this helps...
>
That is very helpful, and not just for nuttx development! Thanks for the
tip.

The romfs example actually uses xxd as well to convert the filesystem into
hex code that is also stored in a header file. If I am reading the code
correctly, the example app loads the entire filesystem into memory, which
isn't very efficient and not at all what I wanted. Can someone tell me if
that's true?

Thanks,
Russ

>
>
>
> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <xi...@gmail.com>
> wrote:
>
> > You can use the real file system on the device, there are many choices:
> > romfs, littlefs, fatfs, starmtfs and spiffs.
> >
> > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <ru...@gmail.com>
> > wrote:
> >
> > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <xi...@gmail.com>
> > > wrote:
> > >
> > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your scripts
> > into
> > > > some PC folder and run mount this folder from nsh:
> > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > >
> > > > While I appreciate the answer, I am using the sim as a testing
> platform
> > > and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
> > hoping
> > > for a solution that is applicable to an embedded project. If I can't
> just
> > > add files to the initial image then I will look at the romfs example
> and
> > > maybe the next best thing?
> > >
> > >
> > > >
> > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <ru...@gmail.com>
> > > > wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > configuration.
> > > I
> > > > > was unable to simply include the defconfig file that you linked to,
> > > but I
> > > > > was able to reconfigure for the sim:lua configuration.  I've now
> got
> > an
> > > > app
> > > > > in the examples folder that includes the Lua interpreter. Is there
> a
> > > > > tutorial on how to include folders and lua scripts or extra files
> in
> > > the
> > > > > initial file system?
> > > > >
> > > > > Much appreciated,
> > > > > Russ
> > > > >
> > > >
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by "Alan C. Assis" <ac...@gmail.com>.
Hi Russel,

If all you need is to copy some file.lua to your board, then an
alternative is using TMPFS and transfer the file using zmodem.

Few day ago I created a video tutorial about it:

https://www.youtube.com/watch?v=Ne3SZZrwL9c

BR,

Alan

On 2/2/23, Russell Haley <ru...@gmail.com> wrote:
> I am mistaken. I understood that the rom image was part of the application
> on flash, but had mis-read the documentation of boardctl(BOARDIOC_ROMDISK,
> (uintptr_t)&desc); and thought that the command loaded the image into *RAM*
> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>
> Incidentally, neither the romfs example nor my attempt to re-create it
> works in my sim:
>
> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>
> NuttShell (NSH) NuttX-12.0.0
> nsh> romfs
> ERROR: Failed to create RAM disk: Unknown error
> nsh> rapp
> Starting Russells App 1...
> ERROR: Failed to create RAM disk: Unknown error
> nsh>
>
> I don't know how to debug this "unknown error". I guess I will try to find
> where that error message comes from and hook in GDB? I'm terrible at this
> stuff, someone needs to revoke my compiler license (tee hee).
>
> Cheers,
> Russ
>
> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> wrote:
>
>> romfs is part of your image as the const string. There is no difference
>> from the below manual step.
>>
>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
>> wrote:
>>
>> > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>> f.j.panag@gmail.com
>> > >
>> > wrote:
>> >
>> > > Hello,
>> > >
>> > > Indeed the "proper" way of including a script would be to store it in
>> > > a
>> > > file system.
>> > >
>> > > However, when I needed to include a single and small script and I
>> didn't
>> > > want to introduce a complete FS just for this, I used xxd.
>> > > xxd can convert any file to a C header file.
>> > >
>> > > You can then include the header, and access the whole file as a
>> variable.
>> > > Here is an example:
>> > >
>> > > I added this in my app Makefile:
>> > >
>> > > # Setup any special pre-build context
>> > > context: header
>> > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>> > > json_lua.h
>> &&
>> > > echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>> > >
>> > >
>> > >
>> > > And then I used the file like this:
>> > >
>> > > #include "lua.h"#include "lauxlib.h"#include <string.h>
>> > >  #include "json_lua.h"
>> > >  static int luaopen_json(lua_State * L);
>> > >
>> > >  void ExtLibs_load(lua_State * L){
>> > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>> > >         luaL_requiref(L, "json", luaopen_json, 1);
>> > >         lua_pop(L, 1);#endif}
>> > >
>> > >  int luaopen_json(lua_State * L){
>> > >         const char * modname = lua_tostring(L, 1);
>> > >
>> > >         if (strcmp(modname, "json") != 0)
>> > >                 return luaL_error(L, "cannot load json module");
>> > >
>> > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>> > > "json",
>> > > "t") != LUA_OK)
>> > >                 return lua_error(L);
>> > >
>> > >         lua_call(L, 0, 1);
>> > >
>> > >         return 1;}
>> > >
>> > >
>> > > I hope this helps...
>> > >
>> > That is very helpful, and not just for nuttx development! Thanks for
>> > the
>> > tip.
>> >
>> > The romfs example actually uses xxd as well to convert the filesystem
>> into
>> > hex code that is also stored in a header file. If I am reading the code
>> > correctly, the example app loads the entire filesystem into memory,
>> > which
>> > isn't very efficient and not at all what I wanted. Can someone tell me
>> > if
>> > that's true?
>> >
>> > Thanks,
>> > Russ
>> >
>> > >
>> > >
>> > >
>> > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao
>> > > <xi...@gmail.com>
>> > > wrote:
>> > >
>> > > > You can use the real file system on the device, there are many
>> choices:
>> > > > romfs, littlefs, fatfs, starmtfs and spiffs.
>> > > >
>> > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley
>> > > > <russ.haley@gmail.com
>> >
>> > > > wrote:
>> > > >
>> > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>> > xiaoxiang781216@gmail.com>
>> > > > > wrote:
>> > > > >
>> > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>> scripts
>> > > > into
>> > > > > > some PC folder and run mount this folder from nsh:
>> > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>> > > > > >
>> > > > > > While I appreciate the answer, I am using the sim as a testing
>> > > platform
>> > > > > and hoping to move to either an STM32F4/7 or a Sony Spresense. I
>> > > > > am
>> > > > hoping
>> > > > > for a solution that is applicable to an embedded project. If I
>> can't
>> > > just
>> > > > > add files to the initial image then I will look at the romfs
>> example
>> > > and
>> > > > > maybe the next best thing?
>> > > > >
>> > > > >
>> > > > > >
>> > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>> > russ.haley@gmail.com>
>> > > > > > wrote:
>> > > > > >
>> > > > > > > Hi,
>> > > > > > >
>> > > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
>> > > > configuration.
>> > > > > I
>> > > > > > > was unable to simply include the defconfig file that you
>> > > > > > > linked
>> > to,
>> > > > > but I
>> > > > > > > was able to reconfigure for the sim:lua configuration.  I've
>> now
>> > > got
>> > > > an
>> > > > > > app
>> > > > > > > in the examples folder that includes the Lua interpreter. Is
>> > there
>> > > a
>> > > > > > > tutorial on how to include folders and lua scripts or extra
>> files
>> > > in
>> > > > > the
>> > > > > > > initial file system?
>> > > > > > >
>> > > > > > > Much appreciated,
>> > > > > > > Russ
>> > > > > > >
>> > > > > >
>> > > > >
>> > > >
>> > >
>> >
>>
>

Re: Including Lua scripts on filesystem

Posted by Russell Haley <ru...@gmail.com>.
I've put my source code here in case anyone wants to try it:

https://github.com/RussellHaley/nuttx-russ-app-1

Thanks for the help everyone!
Russ

On Fri, Feb 3, 2023 at 2:31 AM Russell Haley <ru...@gmail.com> wrote:

> Hmmm... So far I've tried the romfs example, adding the mkrd command as
> well as the suggestion by Sebastien but everything returns an error. Is
> everyone sure that romfs works with the sim:nsh example code?
>
> rapp_main_1.c
> static int open_romdisk_2(void)
> {
> int ret = 0;
> ret = romdisk_register(0, // /dev/ram0
>                           scripts_img, //var in the xxd header
>                           NSECTORS(scripts_img_len),
>                           CONFIG_EXAMPLES_ROMFS_SECTORSIZE);
> if (ret < 0)
> {
> printf("Failed to create a romdisk.\n");
> printf("Error Number is %d\n", errno);
> printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
> return 1;
> }
>
>    ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
>
>    if (ret < 0)
>     {
> printf("Failed to mount ramdisk.\n");
> printf("Error NUmber is %d\n", errno);
> printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
> return 1;
>     }
> }
>
> static int open_romdisk(void)
> {
> int ret = 0;
> struct boardioc_mkrd_s ramdisk;
> struct boardioc_romdisk_s desc;
>
> ramdisk.minor = CONFIG_EXAMPLES_ROMFS_RAMDEVNO;
> ramdisk.nsectors = NSECTORS(scripts_img_len);
> ramdisk.sectsize = CONFIG_EXAMPLES_ROMFS_SECTORSIZE;
>
> ret = boardctl(BOARDIOC_MKRD, (uintptr_t)&ramdisk);
>
> if (ret < 0)
>     {
> printf("Failed to create a ramdisk.\n");
> printf("Error NUmber is %d\n", errno);
> printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
> return 1;
>     }
>
> /* Create a RAM disk for the test */
> desc.minor    = CONFIG_EXAMPLES_ROMFS_RAMDEVNO;         /* Minor device
> number of the ROM disk. */
> desc.nsectors = NSECTORS(scripts_img_len);              /* The number of
> sectors in the ROM disk */
> desc.sectsize = CONFIG_EXAMPLES_ROMFS_SECTORSIZE;       /* The size of one
> sector in bytes */
> desc.image    = (FAR uint8_t *)scripts_img;             /* File system
> image */
>
> ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
>
>
> if (ret < 0)
>     {
> printf("Error NUmber is %d\n", errno);
> printf("ERROR: Failed to create ROM disk: %s\n", strerror(errno));
> return 1;
>     }
> /* Mount the test file system */
>
> printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
> CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);
>
> ret = mount(MOUNT_DEVNAME, CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, "romfs",
> MS_RDONLY, NULL);
> if (ret < 0)
> {
> printf("Error NUmber is %d\n", errno);
> printf("ERROR: Mount failed: %s\n", strerror(errno));
> return 1;
> }
>
> return 0;
> }
>
> /****************************************************************************
>  * main
>
>  ****************************************************************************/
>
> int main(int argc, FAR char *argv[])
> {
> printf("Starting Russells App 1...\n");
> printf("Rom Disk Images says: %s\n", scripts_img+139);
> // open_romdisk();
> open_romdisk_2();
> return 0;
> }
>
>
> OUTPUT:
>
> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>
> NuttShell (NSH) NuttX-12.0.0
> nsh> rapp
> Starting Russells App 1...
> Rom Disk Images says: require 'cjson'
>
>
> function readAll(file)
>     local f = assert(io.open(file, "rb"))
>     local content = f:read("*all")
>     f:close()
>     return content
> end
>
> local json = readAll('test.json')
> local colors = cj.decode(json)
>
> for i,v in pairs(colors) do
> for k,d in pairs(v) do
> print(k,d)
> end
> end
>
>
> Failed to create a romdisk.
> Error Number is 0
> ERROR: Failed to create RAM disk: Unknown error
>
> On Thu, Feb 2, 2023 at 3:05 AM Xiang Xiao <xi...@gmail.com>
> wrote:
>
>> You need to prepare /dev/ram1 with the mkrd command.
>>
>> On Thu, Feb 2, 2023 at 2:29 PM Russell Haley <ru...@gmail.com>
>> wrote:
>>
>> > On Wed, Feb 1, 2023 at 9:23 PM Xiang Xiao <xi...@gmail.com>
>> > wrote:
>> >
>> > > You can debug sim nuttx with your favorite PC debugger just like a
>> normal
>> > > host program.
>> > > Of course, please ensure the debug system is
>> > > generated(CONFIG_DEBUG_SYMBOLS=y).
>> > >
>> >
>> > I was able to set up KDevelop to run nuttx in the debugger, but the
>> console
>> > never gave me a nsh> prompt so I wasn't able to enter my app name and
>> run
>> > it. Nonetheless I was able to run in GDB on the command line. For some
>> > reason inode_search is returning 0, which is converted to -EEXIST or
>> -17.
>> >
>> > Breakpoint 3, inode_reserve (path=0x7fff00000000 <error: Cannot access
>> > memory at address 0x7fff00000000>, mode=48, inode=0x555555671220
>> > <g_inode_lock>) at inode/fs_inodereserve.c:174
>> > 174 {
>> > (gdb) n
>> > 184  *inode = NULL;
>> > (gdb)
>> > 186  if (path[0] == '\0')
>> > (gdb) p path
>> > $47 = 0x7ffff3f2f370 "/dev/ram1"
>> > (gdb) n
>> > 193  SETUP_SEARCH(&desc, path, false);
>> > (gdb)
>> > 195  ret = inode_search(&desc);
>> > (gdb)
>> > 196  if (ret >= 0)
>> > (gdb) p ret
>> > $49 = 0
>> > (gdb) n
>> > 202      ret = -EEXIST;
>> > (gdb)
>> >
>> > Not sure what to do from here? Any feedback would be great?
>> >
>> > Thanks,
>> > Russ
>> >
>> >
>> > >
>> > > On Thu, Feb 2, 2023 at 1:00 PM Russell Haley <ru...@gmail.com>
>> > wrote:
>> > >
>> > > > I am mistaken. I understood that the rom image was part of the
>> > > application
>> > > > on flash, but had mis-read the documentation of
>> > > boardctl(BOARDIOC_ROMDISK,
>> > > > (uintptr_t)&desc); and thought that the command loaded the image
>> into
>> > > *RAM*
>> > > > (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>> > > >
>> > > > Incidentally, neither the romfs example nor my attempt to re-create
>> it
>> > > > works in my sim:
>> > > >
>> > > > osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>> > > >
>> > > > NuttShell (NSH) NuttX-12.0.0
>> > > > nsh> romfs
>> > > > ERROR: Failed to create RAM disk: Unknown error
>> > > > nsh> rapp
>> > > > Starting Russells App 1...
>> > > > ERROR: Failed to create RAM disk: Unknown error
>> > > > nsh>
>> > > >
>> > > > I don't know how to debug this "unknown error". I guess I will try
>> to
>> > > find
>> > > > where that error message comes from and hook in GDB? I'm terrible at
>> > this
>> > > > stuff, someone needs to revoke my compiler license (tee hee).
>> > > >
>> > > > Cheers,
>> > > > Russ
>> > > >
>> > > > On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <
>> xiaoxiang781216@gmail.com>
>> > > > wrote:
>> > > >
>> > > > > romfs is part of your image as the const string. There is no
>> > difference
>> > > > > from the below manual step.
>> > > > >
>> > > > > On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <
>> russ.haley@gmail.com>
>> > > > > wrote:
>> > > > >
>> > > > > > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>> > > > > f.j.panag@gmail.com
>> > > > > > >
>> > > > > > wrote:
>> > > > > >
>> > > > > > > Hello,
>> > > > > > >
>> > > > > > > Indeed the "proper" way of including a script would be to
>> store
>> > it
>> > > > in a
>> > > > > > > file system.
>> > > > > > >
>> > > > > > > However, when I needed to include a single and small script
>> and I
>> > > > > didn't
>> > > > > > > want to introduce a complete FS just for this, I used xxd.
>> > > > > > > xxd can convert any file to a C header file.
>> > > > > > >
>> > > > > > > You can then include the header, and access the whole file as
>> a
>> > > > > variable.
>> > > > > > > Here is an example:
>> > > > > > >
>> > > > > > > I added this in my app Makefile:
>> > > > > > >
>> > > > > > > # Setup any special pre-build context
>> > > > > > > context: header
>> > > > > > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>> > > > json_lua.h
>> > > > > &&
>> > > > > > > echo -n "const " | cat - json_lua.h > temp && mv temp
>> json_lua.h
>> > > > > > >
>> > > > > > >
>> > > > > > >
>> > > > > > > And then I used the file like this:
>> > > > > > >
>> > > > > > > #include "lua.h"#include "lauxlib.h"#include <string.h>
>> > > > > > >  #include "json_lua.h"
>> > > > > > >  static int luaopen_json(lua_State * L);
>> > > > > > >
>> > > > > > >  void ExtLibs_load(lua_State * L){
>> > > > > > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>> > > > > > >         luaL_requiref(L, "json", luaopen_json, 1);
>> > > > > > >         lua_pop(L, 1);#endif}
>> > > > > > >
>> > > > > > >  int luaopen_json(lua_State * L){
>> > > > > > >         const char * modname = lua_tostring(L, 1);
>> > > > > > >
>> > > > > > >         if (strcmp(modname, "json") != 0)
>> > > > > > >                 return luaL_error(L, "cannot load json
>> module");
>> > > > > > >
>> > > > > > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>> > > > "json",
>> > > > > > > "t") != LUA_OK)
>> > > > > > >                 return lua_error(L);
>> > > > > > >
>> > > > > > >         lua_call(L, 0, 1);
>> > > > > > >
>> > > > > > >         return 1;}
>> > > > > > >
>> > > > > > >
>> > > > > > > I hope this helps...
>> > > > > > >
>> > > > > > That is very helpful, and not just for nuttx development! Thanks
>> > for
>> > > > the
>> > > > > > tip.
>> > > > > >
>> > > > > > The romfs example actually uses xxd as well to convert the
>> > filesystem
>> > > > > into
>> > > > > > hex code that is also stored in a header file. If I am reading
>> the
>> > > code
>> > > > > > correctly, the example app loads the entire filesystem into
>> memory,
>> > > > which
>> > > > > > isn't very efficient and not at all what I wanted. Can someone
>> tell
>> > > me
>> > > > if
>> > > > > > that's true?
>> > > > > >
>> > > > > > Thanks,
>> > > > > > Russ
>> > > > > >
>> > > > > > >
>> > > > > > >
>> > > > > > >
>> > > > > > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
>> > > > xiaoxiang781216@gmail.com>
>> > > > > > > wrote:
>> > > > > > >
>> > > > > > > > You can use the real file system on the device, there are
>> many
>> > > > > choices:
>> > > > > > > > romfs, littlefs, fatfs, starmtfs and spiffs.
>> > > > > > > >
>> > > > > > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
>> > > > russ.haley@gmail.com
>> > > > > >
>> > > > > > > > wrote:
>> > > > > > > >
>> > > > > > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>> > > > > > xiaoxiang781216@gmail.com>
>> > > > > > > > > wrote:
>> > > > > > > > >
>> > > > > > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put
>> your
>> > > > > scripts
>> > > > > > > > into
>> > > > > > > > > > some PC folder and run mount this folder from nsh:
>> > > > > > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>> > > > > > > > > >
>> > > > > > > > > > While I appreciate the answer, I am using the sim as a
>> > > testing
>> > > > > > > platform
>> > > > > > > > > and hoping to move to either an STM32F4/7 or a Sony
>> > Spresense.
>> > > I
>> > > > am
>> > > > > > > > hoping
>> > > > > > > > > for a solution that is applicable to an embedded project.
>> If
>> > I
>> > > > > can't
>> > > > > > > just
>> > > > > > > > > add files to the initial image then I will look at the
>> romfs
>> > > > > example
>> > > > > > > and
>> > > > > > > > > maybe the next best thing?
>> > > > > > > > >
>> > > > > > > > >
>> > > > > > > > > >
>> > > > > > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>> > > > > > russ.haley@gmail.com>
>> > > > > > > > > > wrote:
>> > > > > > > > > >
>> > > > > > > > > > > Hi,
>> > > > > > > > > > >
>> > > > > > > > > > > Big thanks to Xiang Xiao for pointing me to the
>> sim:lua
>> > > > > > > > configuration.
>> > > > > > > > > I
>> > > > > > > > > > > was unable to simply include the defconfig file that
>> you
>> > > > linked
>> > > > > > to,
>> > > > > > > > > but I
>> > > > > > > > > > > was able to reconfigure for the sim:lua configuration.
>> > > I've
>> > > > > now
>> > > > > > > got
>> > > > > > > > an
>> > > > > > > > > > app
>> > > > > > > > > > > in the examples folder that includes the Lua
>> interpreter.
>> > > Is
>> > > > > > there
>> > > > > > > a
>> > > > > > > > > > > tutorial on how to include folders and lua scripts or
>> > extra
>> > > > > files
>> > > > > > > in
>> > > > > > > > > the
>> > > > > > > > > > > initial file system?
>> > > > > > > > > > >
>> > > > > > > > > > > Much appreciated,
>> > > > > > > > > > > Russ
>> > > > > > > > > > >
>> > > > > > > > > >
>> > > > > > > > >
>> > > > > > > >
>> > > > > > >
>> > > > > >
>> > > > >
>> > > >
>> > >
>> >
>>
>

Re: Including Lua scripts on filesystem

Posted by Russell Haley <ru...@gmail.com>.
Hmmm... So far I've tried the romfs example, adding the mkrd command as
well as the suggestion by Sebastien but everything returns an error. Is
everyone sure that romfs works with the sim:nsh example code?

rapp_main_1.c
static int open_romdisk_2(void)
{
int ret = 0;
ret = romdisk_register(0, // /dev/ram0
                          scripts_img, //var in the xxd header
                          NSECTORS(scripts_img_len),
                          CONFIG_EXAMPLES_ROMFS_SECTORSIZE);
if (ret < 0)
{
printf("Failed to create a romdisk.\n");
printf("Error Number is %d\n", errno);
printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
return 1;
}

   ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);

   if (ret < 0)
    {
printf("Failed to mount ramdisk.\n");
printf("Error NUmber is %d\n", errno);
printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
return 1;
    }
}

static int open_romdisk(void)
{
int ret = 0;
struct boardioc_mkrd_s ramdisk;
struct boardioc_romdisk_s desc;

ramdisk.minor = CONFIG_EXAMPLES_ROMFS_RAMDEVNO;
ramdisk.nsectors = NSECTORS(scripts_img_len);
ramdisk.sectsize = CONFIG_EXAMPLES_ROMFS_SECTORSIZE;

ret = boardctl(BOARDIOC_MKRD, (uintptr_t)&ramdisk);

if (ret < 0)
    {
printf("Failed to create a ramdisk.\n");
printf("Error NUmber is %d\n", errno);
printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
return 1;
    }

/* Create a RAM disk for the test */
desc.minor    = CONFIG_EXAMPLES_ROMFS_RAMDEVNO;         /* Minor device
number of the ROM disk. */
desc.nsectors = NSECTORS(scripts_img_len);              /* The number of
sectors in the ROM disk */
desc.sectsize = CONFIG_EXAMPLES_ROMFS_SECTORSIZE;       /* The size of one
sector in bytes */
desc.image    = (FAR uint8_t *)scripts_img;             /* File system
image */

ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);


if (ret < 0)
    {
printf("Error NUmber is %d\n", errno);
printf("ERROR: Failed to create ROM disk: %s\n", strerror(errno));
return 1;
    }
/* Mount the test file system */

printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);

ret = mount(MOUNT_DEVNAME, CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, "romfs",
MS_RDONLY, NULL);
if (ret < 0)
{
printf("Error NUmber is %d\n", errno);
printf("ERROR: Mount failed: %s\n", strerror(errno));
return 1;
}

return 0;
}
/****************************************************************************
 * main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
printf("Starting Russells App 1...\n");
printf("Rom Disk Images says: %s\n", scripts_img+139);
// open_romdisk();
open_romdisk_2();
return 0;
}


OUTPUT:

osboxes@osboxes ~/n/nuttx (master)> ./nuttx

NuttShell (NSH) NuttX-12.0.0
nsh> rapp
Starting Russells App 1...
Rom Disk Images says: require 'cjson'


function readAll(file)
    local f = assert(io.open(file, "rb"))
    local content = f:read("*all")
    f:close()
    return content
end

local json = readAll('test.json')
local colors = cj.decode(json)

for i,v in pairs(colors) do
for k,d in pairs(v) do
print(k,d)
end
end


Failed to create a romdisk.
Error Number is 0
ERROR: Failed to create RAM disk: Unknown error

On Thu, Feb 2, 2023 at 3:05 AM Xiang Xiao <xi...@gmail.com> wrote:

> You need to prepare /dev/ram1 with the mkrd command.
>
> On Thu, Feb 2, 2023 at 2:29 PM Russell Haley <ru...@gmail.com> wrote:
>
> > On Wed, Feb 1, 2023 at 9:23 PM Xiang Xiao <xi...@gmail.com>
> > wrote:
> >
> > > You can debug sim nuttx with your favorite PC debugger just like a
> normal
> > > host program.
> > > Of course, please ensure the debug system is
> > > generated(CONFIG_DEBUG_SYMBOLS=y).
> > >
> >
> > I was able to set up KDevelop to run nuttx in the debugger, but the
> console
> > never gave me a nsh> prompt so I wasn't able to enter my app name and run
> > it. Nonetheless I was able to run in GDB on the command line. For some
> > reason inode_search is returning 0, which is converted to -EEXIST or -17.
> >
> > Breakpoint 3, inode_reserve (path=0x7fff00000000 <error: Cannot access
> > memory at address 0x7fff00000000>, mode=48, inode=0x555555671220
> > <g_inode_lock>) at inode/fs_inodereserve.c:174
> > 174 {
> > (gdb) n
> > 184  *inode = NULL;
> > (gdb)
> > 186  if (path[0] == '\0')
> > (gdb) p path
> > $47 = 0x7ffff3f2f370 "/dev/ram1"
> > (gdb) n
> > 193  SETUP_SEARCH(&desc, path, false);
> > (gdb)
> > 195  ret = inode_search(&desc);
> > (gdb)
> > 196  if (ret >= 0)
> > (gdb) p ret
> > $49 = 0
> > (gdb) n
> > 202      ret = -EEXIST;
> > (gdb)
> >
> > Not sure what to do from here? Any feedback would be great?
> >
> > Thanks,
> > Russ
> >
> >
> > >
> > > On Thu, Feb 2, 2023 at 1:00 PM Russell Haley <ru...@gmail.com>
> > wrote:
> > >
> > > > I am mistaken. I understood that the rom image was part of the
> > > application
> > > > on flash, but had mis-read the documentation of
> > > boardctl(BOARDIOC_ROMDISK,
> > > > (uintptr_t)&desc); and thought that the command loaded the image into
> > > *RAM*
> > > > (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> > > >
> > > > Incidentally, neither the romfs example nor my attempt to re-create
> it
> > > > works in my sim:
> > > >
> > > > osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> > > >
> > > > NuttShell (NSH) NuttX-12.0.0
> > > > nsh> romfs
> > > > ERROR: Failed to create RAM disk: Unknown error
> > > > nsh> rapp
> > > > Starting Russells App 1...
> > > > ERROR: Failed to create RAM disk: Unknown error
> > > > nsh>
> > > >
> > > > I don't know how to debug this "unknown error". I guess I will try to
> > > find
> > > > where that error message comes from and hook in GDB? I'm terrible at
> > this
> > > > stuff, someone needs to revoke my compiler license (tee hee).
> > > >
> > > > Cheers,
> > > > Russ
> > > >
> > > > On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xiaoxiang781216@gmail.com
> >
> > > > wrote:
> > > >
> > > > > romfs is part of your image as the const string. There is no
> > difference
> > > > > from the below manual step.
> > > > >
> > > > > On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <
> russ.haley@gmail.com>
> > > > > wrote:
> > > > >
> > > > > > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> > > > > f.j.panag@gmail.com
> > > > > > >
> > > > > > wrote:
> > > > > >
> > > > > > > Hello,
> > > > > > >
> > > > > > > Indeed the "proper" way of including a script would be to store
> > it
> > > > in a
> > > > > > > file system.
> > > > > > >
> > > > > > > However, when I needed to include a single and small script
> and I
> > > > > didn't
> > > > > > > want to introduce a complete FS just for this, I used xxd.
> > > > > > > xxd can convert any file to a C header file.
> > > > > > >
> > > > > > > You can then include the header, and access the whole file as a
> > > > > variable.
> > > > > > > Here is an example:
> > > > > > >
> > > > > > > I added this in my app Makefile:
> > > > > > >
> > > > > > > # Setup any special pre-build context
> > > > > > > context: header
> > > > > > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> > > > json_lua.h
> > > > > &&
> > > > > > > echo -n "const " | cat - json_lua.h > temp && mv temp
> json_lua.h
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > And then I used the file like this:
> > > > > > >
> > > > > > > #include "lua.h"#include "lauxlib.h"#include <string.h>
> > > > > > >  #include "json_lua.h"
> > > > > > >  static int luaopen_json(lua_State * L);
> > > > > > >
> > > > > > >  void ExtLibs_load(lua_State * L){
> > > > > > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> > > > > > >         luaL_requiref(L, "json", luaopen_json, 1);
> > > > > > >         lua_pop(L, 1);#endif}
> > > > > > >
> > > > > > >  int luaopen_json(lua_State * L){
> > > > > > >         const char * modname = lua_tostring(L, 1);
> > > > > > >
> > > > > > >         if (strcmp(modname, "json") != 0)
> > > > > > >                 return luaL_error(L, "cannot load json
> module");
> > > > > > >
> > > > > > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> > > > "json",
> > > > > > > "t") != LUA_OK)
> > > > > > >                 return lua_error(L);
> > > > > > >
> > > > > > >         lua_call(L, 0, 1);
> > > > > > >
> > > > > > >         return 1;}
> > > > > > >
> > > > > > >
> > > > > > > I hope this helps...
> > > > > > >
> > > > > > That is very helpful, and not just for nuttx development! Thanks
> > for
> > > > the
> > > > > > tip.
> > > > > >
> > > > > > The romfs example actually uses xxd as well to convert the
> > filesystem
> > > > > into
> > > > > > hex code that is also stored in a header file. If I am reading
> the
> > > code
> > > > > > correctly, the example app loads the entire filesystem into
> memory,
> > > > which
> > > > > > isn't very efficient and not at all what I wanted. Can someone
> tell
> > > me
> > > > if
> > > > > > that's true?
> > > > > >
> > > > > > Thanks,
> > > > > > Russ
> > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> > > > xiaoxiang781216@gmail.com>
> > > > > > > wrote:
> > > > > > >
> > > > > > > > You can use the real file system on the device, there are
> many
> > > > > choices:
> > > > > > > > romfs, littlefs, fatfs, starmtfs and spiffs.
> > > > > > > >
> > > > > > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> > > > russ.haley@gmail.com
> > > > > >
> > > > > > > > wrote:
> > > > > > > >
> > > > > > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> > > > > > xiaoxiang781216@gmail.com>
> > > > > > > > > wrote:
> > > > > > > > >
> > > > > > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put
> your
> > > > > scripts
> > > > > > > > into
> > > > > > > > > > some PC folder and run mount this folder from nsh:
> > > > > > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > > > > > > > >
> > > > > > > > > > While I appreciate the answer, I am using the sim as a
> > > testing
> > > > > > > platform
> > > > > > > > > and hoping to move to either an STM32F4/7 or a Sony
> > Spresense.
> > > I
> > > > am
> > > > > > > > hoping
> > > > > > > > > for a solution that is applicable to an embedded project.
> If
> > I
> > > > > can't
> > > > > > > just
> > > > > > > > > add files to the initial image then I will look at the
> romfs
> > > > > example
> > > > > > > and
> > > > > > > > > maybe the next best thing?
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> > > > > > russ.haley@gmail.com>
> > > > > > > > > > wrote:
> > > > > > > > > >
> > > > > > > > > > > Hi,
> > > > > > > > > > >
> > > > > > > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > > > > > > > configuration.
> > > > > > > > > I
> > > > > > > > > > > was unable to simply include the defconfig file that
> you
> > > > linked
> > > > > > to,
> > > > > > > > > but I
> > > > > > > > > > > was able to reconfigure for the sim:lua configuration.
> > > I've
> > > > > now
> > > > > > > got
> > > > > > > > an
> > > > > > > > > > app
> > > > > > > > > > > in the examples folder that includes the Lua
> interpreter.
> > > Is
> > > > > > there
> > > > > > > a
> > > > > > > > > > > tutorial on how to include folders and lua scripts or
> > extra
> > > > > files
> > > > > > > in
> > > > > > > > > the
> > > > > > > > > > > initial file system?
> > > > > > > > > > >
> > > > > > > > > > > Much appreciated,
> > > > > > > > > > > Russ
> > > > > > > > > > >
> > > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
You need to prepare /dev/ram1 with the mkrd command.

On Thu, Feb 2, 2023 at 2:29 PM Russell Haley <ru...@gmail.com> wrote:

> On Wed, Feb 1, 2023 at 9:23 PM Xiang Xiao <xi...@gmail.com>
> wrote:
>
> > You can debug sim nuttx with your favorite PC debugger just like a normal
> > host program.
> > Of course, please ensure the debug system is
> > generated(CONFIG_DEBUG_SYMBOLS=y).
> >
>
> I was able to set up KDevelop to run nuttx in the debugger, but the console
> never gave me a nsh> prompt so I wasn't able to enter my app name and run
> it. Nonetheless I was able to run in GDB on the command line. For some
> reason inode_search is returning 0, which is converted to -EEXIST or -17.
>
> Breakpoint 3, inode_reserve (path=0x7fff00000000 <error: Cannot access
> memory at address 0x7fff00000000>, mode=48, inode=0x555555671220
> <g_inode_lock>) at inode/fs_inodereserve.c:174
> 174 {
> (gdb) n
> 184  *inode = NULL;
> (gdb)
> 186  if (path[0] == '\0')
> (gdb) p path
> $47 = 0x7ffff3f2f370 "/dev/ram1"
> (gdb) n
> 193  SETUP_SEARCH(&desc, path, false);
> (gdb)
> 195  ret = inode_search(&desc);
> (gdb)
> 196  if (ret >= 0)
> (gdb) p ret
> $49 = 0
> (gdb) n
> 202      ret = -EEXIST;
> (gdb)
>
> Not sure what to do from here? Any feedback would be great?
>
> Thanks,
> Russ
>
>
> >
> > On Thu, Feb 2, 2023 at 1:00 PM Russell Haley <ru...@gmail.com>
> wrote:
> >
> > > I am mistaken. I understood that the rom image was part of the
> > application
> > > on flash, but had mis-read the documentation of
> > boardctl(BOARDIOC_ROMDISK,
> > > (uintptr_t)&desc); and thought that the command loaded the image into
> > *RAM*
> > > (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> > >
> > > Incidentally, neither the romfs example nor my attempt to re-create it
> > > works in my sim:
> > >
> > > osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> > >
> > > NuttShell (NSH) NuttX-12.0.0
> > > nsh> romfs
> > > ERROR: Failed to create RAM disk: Unknown error
> > > nsh> rapp
> > > Starting Russells App 1...
> > > ERROR: Failed to create RAM disk: Unknown error
> > > nsh>
> > >
> > > I don't know how to debug this "unknown error". I guess I will try to
> > find
> > > where that error message comes from and hook in GDB? I'm terrible at
> this
> > > stuff, someone needs to revoke my compiler license (tee hee).
> > >
> > > Cheers,
> > > Russ
> > >
> > > On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> > > wrote:
> > >
> > > > romfs is part of your image as the const string. There is no
> difference
> > > > from the below manual step.
> > > >
> > > > On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> > > > wrote:
> > > >
> > > > > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> > > > f.j.panag@gmail.com
> > > > > >
> > > > > wrote:
> > > > >
> > > > > > Hello,
> > > > > >
> > > > > > Indeed the "proper" way of including a script would be to store
> it
> > > in a
> > > > > > file system.
> > > > > >
> > > > > > However, when I needed to include a single and small script and I
> > > > didn't
> > > > > > want to introduce a complete FS just for this, I used xxd.
> > > > > > xxd can convert any file to a C header file.
> > > > > >
> > > > > > You can then include the header, and access the whole file as a
> > > > variable.
> > > > > > Here is an example:
> > > > > >
> > > > > > I added this in my app Makefile:
> > > > > >
> > > > > > # Setup any special pre-build context
> > > > > > context: header
> > > > > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> > > json_lua.h
> > > > &&
> > > > > > echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> > > > > >
> > > > > >
> > > > > >
> > > > > > And then I used the file like this:
> > > > > >
> > > > > > #include "lua.h"#include "lauxlib.h"#include <string.h>
> > > > > >  #include "json_lua.h"
> > > > > >  static int luaopen_json(lua_State * L);
> > > > > >
> > > > > >  void ExtLibs_load(lua_State * L){
> > > > > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> > > > > >         luaL_requiref(L, "json", luaopen_json, 1);
> > > > > >         lua_pop(L, 1);#endif}
> > > > > >
> > > > > >  int luaopen_json(lua_State * L){
> > > > > >         const char * modname = lua_tostring(L, 1);
> > > > > >
> > > > > >         if (strcmp(modname, "json") != 0)
> > > > > >                 return luaL_error(L, "cannot load json module");
> > > > > >
> > > > > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> > > "json",
> > > > > > "t") != LUA_OK)
> > > > > >                 return lua_error(L);
> > > > > >
> > > > > >         lua_call(L, 0, 1);
> > > > > >
> > > > > >         return 1;}
> > > > > >
> > > > > >
> > > > > > I hope this helps...
> > > > > >
> > > > > That is very helpful, and not just for nuttx development! Thanks
> for
> > > the
> > > > > tip.
> > > > >
> > > > > The romfs example actually uses xxd as well to convert the
> filesystem
> > > > into
> > > > > hex code that is also stored in a header file. If I am reading the
> > code
> > > > > correctly, the example app loads the entire filesystem into memory,
> > > which
> > > > > isn't very efficient and not at all what I wanted. Can someone tell
> > me
> > > if
> > > > > that's true?
> > > > >
> > > > > Thanks,
> > > > > Russ
> > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> > > xiaoxiang781216@gmail.com>
> > > > > > wrote:
> > > > > >
> > > > > > > You can use the real file system on the device, there are many
> > > > choices:
> > > > > > > romfs, littlefs, fatfs, starmtfs and spiffs.
> > > > > > >
> > > > > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> > > russ.haley@gmail.com
> > > > >
> > > > > > > wrote:
> > > > > > >
> > > > > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> > > > > xiaoxiang781216@gmail.com>
> > > > > > > > wrote:
> > > > > > > >
> > > > > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> > > > scripts
> > > > > > > into
> > > > > > > > > some PC folder and run mount this folder from nsh:
> > > > > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > > > > > > >
> > > > > > > > > While I appreciate the answer, I am using the sim as a
> > testing
> > > > > > platform
> > > > > > > > and hoping to move to either an STM32F4/7 or a Sony
> Spresense.
> > I
> > > am
> > > > > > > hoping
> > > > > > > > for a solution that is applicable to an embedded project. If
> I
> > > > can't
> > > > > > just
> > > > > > > > add files to the initial image then I will look at the romfs
> > > > example
> > > > > > and
> > > > > > > > maybe the next best thing?
> > > > > > > >
> > > > > > > >
> > > > > > > > >
> > > > > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> > > > > russ.haley@gmail.com>
> > > > > > > > > wrote:
> > > > > > > > >
> > > > > > > > > > Hi,
> > > > > > > > > >
> > > > > > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > > > > > > configuration.
> > > > > > > > I
> > > > > > > > > > was unable to simply include the defconfig file that you
> > > linked
> > > > > to,
> > > > > > > > but I
> > > > > > > > > > was able to reconfigure for the sim:lua configuration.
> > I've
> > > > now
> > > > > > got
> > > > > > > an
> > > > > > > > > app
> > > > > > > > > > in the examples folder that includes the Lua interpreter.
> > Is
> > > > > there
> > > > > > a
> > > > > > > > > > tutorial on how to include folders and lua scripts or
> extra
> > > > files
> > > > > > in
> > > > > > > > the
> > > > > > > > > > initial file system?
> > > > > > > > > >
> > > > > > > > > > Much appreciated,
> > > > > > > > > > Russ
> > > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by Russell Haley <ru...@gmail.com>.
On Wed, Feb 1, 2023 at 9:23 PM Xiang Xiao <xi...@gmail.com> wrote:

> You can debug sim nuttx with your favorite PC debugger just like a normal
> host program.
> Of course, please ensure the debug system is
> generated(CONFIG_DEBUG_SYMBOLS=y).
>

I was able to set up KDevelop to run nuttx in the debugger, but the console
never gave me a nsh> prompt so I wasn't able to enter my app name and run
it. Nonetheless I was able to run in GDB on the command line. For some
reason inode_search is returning 0, which is converted to -EEXIST or -17.

Breakpoint 3, inode_reserve (path=0x7fff00000000 <error: Cannot access
memory at address 0x7fff00000000>, mode=48, inode=0x555555671220
<g_inode_lock>) at inode/fs_inodereserve.c:174
174 {
(gdb) n
184  *inode = NULL;
(gdb)
186  if (path[0] == '\0')
(gdb) p path
$47 = 0x7ffff3f2f370 "/dev/ram1"
(gdb) n
193  SETUP_SEARCH(&desc, path, false);
(gdb)
195  ret = inode_search(&desc);
(gdb)
196  if (ret >= 0)
(gdb) p ret
$49 = 0
(gdb) n
202      ret = -EEXIST;
(gdb)

Not sure what to do from here? Any feedback would be great?

Thanks,
Russ


>
> On Thu, Feb 2, 2023 at 1:00 PM Russell Haley <ru...@gmail.com> wrote:
>
> > I am mistaken. I understood that the rom image was part of the
> application
> > on flash, but had mis-read the documentation of
> boardctl(BOARDIOC_ROMDISK,
> > (uintptr_t)&desc); and thought that the command loaded the image into
> *RAM*
> > (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> >
> > Incidentally, neither the romfs example nor my attempt to re-create it
> > works in my sim:
> >
> > osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> >
> > NuttShell (NSH) NuttX-12.0.0
> > nsh> romfs
> > ERROR: Failed to create RAM disk: Unknown error
> > nsh> rapp
> > Starting Russells App 1...
> > ERROR: Failed to create RAM disk: Unknown error
> > nsh>
> >
> > I don't know how to debug this "unknown error". I guess I will try to
> find
> > where that error message comes from and hook in GDB? I'm terrible at this
> > stuff, someone needs to revoke my compiler license (tee hee).
> >
> > Cheers,
> > Russ
> >
> > On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> > wrote:
> >
> > > romfs is part of your image as the const string. There is no difference
> > > from the below manual step.
> > >
> > > On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> > > wrote:
> > >
> > > > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> > > f.j.panag@gmail.com
> > > > >
> > > > wrote:
> > > >
> > > > > Hello,
> > > > >
> > > > > Indeed the "proper" way of including a script would be to store it
> > in a
> > > > > file system.
> > > > >
> > > > > However, when I needed to include a single and small script and I
> > > didn't
> > > > > want to introduce a complete FS just for this, I used xxd.
> > > > > xxd can convert any file to a C header file.
> > > > >
> > > > > You can then include the header, and access the whole file as a
> > > variable.
> > > > > Here is an example:
> > > > >
> > > > > I added this in my app Makefile:
> > > > >
> > > > > # Setup any special pre-build context
> > > > > context: header
> > > > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> > json_lua.h
> > > &&
> > > > > echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> > > > >
> > > > >
> > > > >
> > > > > And then I used the file like this:
> > > > >
> > > > > #include "lua.h"#include "lauxlib.h"#include <string.h>
> > > > >  #include "json_lua.h"
> > > > >  static int luaopen_json(lua_State * L);
> > > > >
> > > > >  void ExtLibs_load(lua_State * L){
> > > > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> > > > >         luaL_requiref(L, "json", luaopen_json, 1);
> > > > >         lua_pop(L, 1);#endif}
> > > > >
> > > > >  int luaopen_json(lua_State * L){
> > > > >         const char * modname = lua_tostring(L, 1);
> > > > >
> > > > >         if (strcmp(modname, "json") != 0)
> > > > >                 return luaL_error(L, "cannot load json module");
> > > > >
> > > > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> > "json",
> > > > > "t") != LUA_OK)
> > > > >                 return lua_error(L);
> > > > >
> > > > >         lua_call(L, 0, 1);
> > > > >
> > > > >         return 1;}
> > > > >
> > > > >
> > > > > I hope this helps...
> > > > >
> > > > That is very helpful, and not just for nuttx development! Thanks for
> > the
> > > > tip.
> > > >
> > > > The romfs example actually uses xxd as well to convert the filesystem
> > > into
> > > > hex code that is also stored in a header file. If I am reading the
> code
> > > > correctly, the example app loads the entire filesystem into memory,
> > which
> > > > isn't very efficient and not at all what I wanted. Can someone tell
> me
> > if
> > > > that's true?
> > > >
> > > > Thanks,
> > > > Russ
> > > >
> > > > >
> > > > >
> > > > >
> > > > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> > xiaoxiang781216@gmail.com>
> > > > > wrote:
> > > > >
> > > > > > You can use the real file system on the device, there are many
> > > choices:
> > > > > > romfs, littlefs, fatfs, starmtfs and spiffs.
> > > > > >
> > > > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> > russ.haley@gmail.com
> > > >
> > > > > > wrote:
> > > > > >
> > > > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> > > > xiaoxiang781216@gmail.com>
> > > > > > > wrote:
> > > > > > >
> > > > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> > > scripts
> > > > > > into
> > > > > > > > some PC folder and run mount this folder from nsh:
> > > > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > > > > > >
> > > > > > > > While I appreciate the answer, I am using the sim as a
> testing
> > > > > platform
> > > > > > > and hoping to move to either an STM32F4/7 or a Sony Spresense.
> I
> > am
> > > > > > hoping
> > > > > > > for a solution that is applicable to an embedded project. If I
> > > can't
> > > > > just
> > > > > > > add files to the initial image then I will look at the romfs
> > > example
> > > > > and
> > > > > > > maybe the next best thing?
> > > > > > >
> > > > > > >
> > > > > > > >
> > > > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> > > > russ.haley@gmail.com>
> > > > > > > > wrote:
> > > > > > > >
> > > > > > > > > Hi,
> > > > > > > > >
> > > > > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > > > > > configuration.
> > > > > > > I
> > > > > > > > > was unable to simply include the defconfig file that you
> > linked
> > > > to,
> > > > > > > but I
> > > > > > > > > was able to reconfigure for the sim:lua configuration.
> I've
> > > now
> > > > > got
> > > > > > an
> > > > > > > > app
> > > > > > > > > in the examples folder that includes the Lua interpreter.
> Is
> > > > there
> > > > > a
> > > > > > > > > tutorial on how to include folders and lua scripts or extra
> > > files
> > > > > in
> > > > > > > the
> > > > > > > > > initial file system?
> > > > > > > > >
> > > > > > > > > Much appreciated,
> > > > > > > > > Russ
> > > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
You can debug sim nuttx with your favorite PC debugger just like a normal
host program.
Of course, please ensure the debug system is
generated(CONFIG_DEBUG_SYMBOLS=y).

On Thu, Feb 2, 2023 at 1:00 PM Russell Haley <ru...@gmail.com> wrote:

> I am mistaken. I understood that the rom image was part of the application
> on flash, but had mis-read the documentation of boardctl(BOARDIOC_ROMDISK,
> (uintptr_t)&desc); and thought that the command loaded the image into *RAM*
> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>
> Incidentally, neither the romfs example nor my attempt to re-create it
> works in my sim:
>
> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>
> NuttShell (NSH) NuttX-12.0.0
> nsh> romfs
> ERROR: Failed to create RAM disk: Unknown error
> nsh> rapp
> Starting Russells App 1...
> ERROR: Failed to create RAM disk: Unknown error
> nsh>
>
> I don't know how to debug this "unknown error". I guess I will try to find
> where that error message comes from and hook in GDB? I'm terrible at this
> stuff, someone needs to revoke my compiler license (tee hee).
>
> Cheers,
> Russ
>
> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> wrote:
>
> > romfs is part of your image as the const string. There is no difference
> > from the below manual step.
> >
> > On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> > wrote:
> >
> > > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> > f.j.panag@gmail.com
> > > >
> > > wrote:
> > >
> > > > Hello,
> > > >
> > > > Indeed the "proper" way of including a script would be to store it
> in a
> > > > file system.
> > > >
> > > > However, when I needed to include a single and small script and I
> > didn't
> > > > want to introduce a complete FS just for this, I used xxd.
> > > > xxd can convert any file to a C header file.
> > > >
> > > > You can then include the header, and access the whole file as a
> > variable.
> > > > Here is an example:
> > > >
> > > > I added this in my app Makefile:
> > > >
> > > > # Setup any special pre-build context
> > > > context: header
> > > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> json_lua.h
> > &&
> > > > echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> > > >
> > > >
> > > >
> > > > And then I used the file like this:
> > > >
> > > > #include "lua.h"#include "lauxlib.h"#include <string.h>
> > > >  #include "json_lua.h"
> > > >  static int luaopen_json(lua_State * L);
> > > >
> > > >  void ExtLibs_load(lua_State * L){
> > > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> > > >         luaL_requiref(L, "json", luaopen_json, 1);
> > > >         lua_pop(L, 1);#endif}
> > > >
> > > >  int luaopen_json(lua_State * L){
> > > >         const char * modname = lua_tostring(L, 1);
> > > >
> > > >         if (strcmp(modname, "json") != 0)
> > > >                 return luaL_error(L, "cannot load json module");
> > > >
> > > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> "json",
> > > > "t") != LUA_OK)
> > > >                 return lua_error(L);
> > > >
> > > >         lua_call(L, 0, 1);
> > > >
> > > >         return 1;}
> > > >
> > > >
> > > > I hope this helps...
> > > >
> > > That is very helpful, and not just for nuttx development! Thanks for
> the
> > > tip.
> > >
> > > The romfs example actually uses xxd as well to convert the filesystem
> > into
> > > hex code that is also stored in a header file. If I am reading the code
> > > correctly, the example app loads the entire filesystem into memory,
> which
> > > isn't very efficient and not at all what I wanted. Can someone tell me
> if
> > > that's true?
> > >
> > > Thanks,
> > > Russ
> > >
> > > >
> > > >
> > > >
> > > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> xiaoxiang781216@gmail.com>
> > > > wrote:
> > > >
> > > > > You can use the real file system on the device, there are many
> > choices:
> > > > > romfs, littlefs, fatfs, starmtfs and spiffs.
> > > > >
> > > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> russ.haley@gmail.com
> > >
> > > > > wrote:
> > > > >
> > > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> > > xiaoxiang781216@gmail.com>
> > > > > > wrote:
> > > > > >
> > > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> > scripts
> > > > > into
> > > > > > > some PC folder and run mount this folder from nsh:
> > > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > > > > >
> > > > > > > While I appreciate the answer, I am using the sim as a testing
> > > > platform
> > > > > > and hoping to move to either an STM32F4/7 or a Sony Spresense. I
> am
> > > > > hoping
> > > > > > for a solution that is applicable to an embedded project. If I
> > can't
> > > > just
> > > > > > add files to the initial image then I will look at the romfs
> > example
> > > > and
> > > > > > maybe the next best thing?
> > > > > >
> > > > > >
> > > > > > >
> > > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> > > russ.haley@gmail.com>
> > > > > > > wrote:
> > > > > > >
> > > > > > > > Hi,
> > > > > > > >
> > > > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > > > > configuration.
> > > > > > I
> > > > > > > > was unable to simply include the defconfig file that you
> linked
> > > to,
> > > > > > but I
> > > > > > > > was able to reconfigure for the sim:lua configuration.  I've
> > now
> > > > got
> > > > > an
> > > > > > > app
> > > > > > > > in the examples folder that includes the Lua interpreter. Is
> > > there
> > > > a
> > > > > > > > tutorial on how to include folders and lua scripts or extra
> > files
> > > > in
> > > > > > the
> > > > > > > > initial file system?
> > > > > > > >
> > > > > > > > Much appreciated,
> > > > > > > > Russ
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by Nathan Hartman <ha...@gmail.com>.
On Thu, Feb 2, 2023 at 1:49 PM Xiang Xiao <xi...@gmail.com> wrote:

> On Fri, Feb 3, 2023 at 2:06 AM Sebastien Lorquet <se...@lorquet.fr>
> wrote:
>
> > You cant expect customers to follow a nsh-based design. The learning
> > curve for nuttx is hard enough, and if you add more constraints in apps
> > building it is even harder.
> >
> >
> It depends on the developer's background. If the developer has
> experience with other POSIX OS(Android, Ubuntu and macOS), all is natural.



Yes. Developers do come from different backgrounds and NuttX is used in
many different ways. When the developer is from the baremetal or other
embedded background, often they don't want "heavyweight" feel of scripts
etc and they prefer to use NuttX like a library RTOS. That's what I do on
custom boards. In my case, I use NSH during prototyping but later I remove
NSH and boot directly into my app. I use NSH and others as examples of how
to implement the particular initializations I need.

Cheers
Nathan

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
As I said before, most features I talk about is disabled by default. Nobody
forbids you to use an alternative approach.

On Fri, Feb 3, 2023 at 6:40 AM Alan C. Assis <ac...@gmail.com> wrote:

> Hi Sebastien,
>
> Please once again, don't start a frame war, we need people to
> contribute to NuttX, to make our loved project great, not to destroy
> it.
>
> That said, I want to say a famous phrase from Linus Torvalds:
>
> "Doers decide!"
>
> If you want to decide how ROMFS should work, please do it.
>
>
I agree with Alan. Let's talk about technology and code.


> It is an open-source project after all, so you are invited to
> contribute, instead of starting a frame war against does who really do
> and really contribute to make NuttX great.
>
> About the System-V init you can use it from some projects, I heard
> someone using it on NuttX:
>
> https://github.com/arachsys/init/tree/master
>
> BR,
>
> Alan
>
> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> > Hi,
> >
> > Once again please remember this project is Apache NuttX and not Xiaomi
> OS.
> >
> > The fact that your company is a major contributor and that you have
> > established your own local usage patterns does not mean these are the
> > only valid ones.
> >
>

But, in principle NuttX prefer POSIX way, this is the rule set by Greg and
agree by many community member:
https://github.com/apache/nuttx/blob/master/INVIOLABLES.md#strict-posix-compliance
.
Almost features added by Xiaomi strictly follow these rule:

   1. Implement this feature If POSIX define it
   2. If not, Implement it like other POSIX OS(*BSD or Linux)


> Each user has its own vision of how such a flexible system should work,
> > and all users count.
> >
> > So please dont suggest "bandaid" solutions to me like /etc is the only
> > place to mount a romfs because the init script can "do things".
> >
> > It is perfectly valid to mount three romfs each at /foo /bar and /baz at
> > boot and NuttX should allow it without telling me what's better.
> >
>

You can mount to any place already:
https://github.com/apache/nuttx-apps/blob/master/nshlib/Kconfig#L782-L787
I just mentioned the default value.


> And I'm sorry but nsh is a demo shell and not an init system, on my own
> > product there is NO WAY I want to give full shell access to my customer.
> >
> > This wont happen and nsh is not even part of my production build.
> >
> > FSes can be initialized in the board's _bringup routine and I dont want
> > a systemd/init/shell to read scripts to mount /proc and bind a littlefs
> > to a directory of my choosing. And _bringup is already used to init spi
> > and mtd.


Nobody forbids you to do this. Actually we also use this approach for the
very small system(e.g. BLE only module) too.
Let's come back to Russell's question, he wants to try Lua script in the
sim environment.



> Because NuttX is a RTOS and is meant to be more lightweight and
> > flexible than a generic freebsd/macos/linux OS.
> >
>

But, we also need the generic solution to handle the complex SoC(e.g.
4XA55+HiFi DSP+2xM33) and application(e.g. Smartwatch and IPCamera).


> >
> > Patch will come, I think this is a good change for nsh, but I'm a bit
> > busy at work at this moment, stuck in a kind of dual-PIC18 cursed hell
> > with hard deadlines lol.
> >
> > I also have a kind of "raid0" to aggregate two MTD with compatible
> > geometries into one, because reasons. It needs more tests, but it could
> > be useful for other users.
> >
> > Sebastien
> >
> >
> > On 2/2/23 19:48, Xiang Xiao wrote:
> >> On Fri, Feb 3, 2023 at 2:06 AM Sebastien Lorquet <se...@lorquet.fr>
> >> wrote:
> >>
> >>> No, and this was exactly my problem.
> >>>
> >>> The romfs should NOT be mounted by nsh.
> >>>
> >>>
> >> It's an option and disabled by default.
> >> BTW, which place do you think is better than the current one?
> >>
> >>
> >>> Mounting to /etc/ is also NOT flexible enough.
> >>>
> >>> Because in a normal board the boot app is not nsh but the customer app.
> >>>
> >>>
> >> You can boot other applications/services from scripts.
> >>
> >>
> >>> At the moment this romfs mount is also closely coupled to the boot
> >>> script mechanism, which is very annoying if your boot script is on a
> >>> flash device (eg littlefs) and not on a romfs.
> >>>
> >>> I have decoupled this, but still not provided a pull request, that was
> >>> in fact easy and the result is cleaner than the current way it is done.
> >>>
> >>>
> >>   Yes, the Kconfig option makes the romfs mount couple with boot script,
> >> but
> >> it is easy to decouple them. Patch is welcome.
> >>
> >>> You cant expect customers to follow a nsh-based design. The learning
> >>> curve for nuttx is hard enough, and if you add more constraints in apps
> >>> building it is even harder.
> >>>
> >>>
> >> It depends on the developer's background. If the developer has
> >> experience with other POSIX OS(Android, Ubuntu and macOS), all is
> >> natural.
> >>
> >>
> >>> Remember that NSH and apps are just "examples" and nuttx should work
> >>> with completely separated apps.
> >>>
> >>>
> >> Of course, all we discuss so far is the user space feature, not related
> >> to
> >> the kernel.
> >>
> >> Thats why I think nsh should not be responsible for too much.
> >>>
> >> nsh equals init pus sh. One improvement is split init from nsh like
> other
> >> OS.
> >>
> >>
> >>> Sebastien
> >>>
> >>>
> >>> Le 02/02/2023 à 17:47, Xiang Xiao a écrit :
> >>>> The infrastructure is there, you can:
> >>>>
> >>>>      1. Enable CONFIG_NSH_ROMFSETC
> >>>>      2. Add your files to RCSRCS/RCRAWS in board's Makefile
> >>>>
> >>>> Then nsh will auto mount it to /etc. sim already has a demo for this:
> >>>>
> https://github.com/apache/nuttx/tree/master/boards/sim/sim/sim/src/etc
> >>>>
> >>>>
> >>>> On Fri, Feb 3, 2023 at 12:33 AM Russell Haley <ru...@gmail.com>
> >>> wrote:
> >>>>> On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com>
> >>>>> wrote:
> >>>>>
> >>>>>> It should be nice to have a simple logic to let users to use ROMFS.
> >>>>>>
> >>>>>> Currently everyone needs to duplicate it in their own
> >>>>>> code/application.
> >>>>>>
> >>>>>> I'm thinking something like apps/romfiles/ where people just put the
> >>>>>> files that they want to be in the ROM and it will be included
> >>>>>> automatically, instead reinventing the wheel.
> >>>>>>
> >>>>> This is what I was hoping to find.  I thought that since nuttx was
> >>> already
> >>>>> building a root filesystem there would be a simple way to include
> >>>>> files
> >>>>> with the image.
> >>>>>
> >>>>> Cheers,
> >>>>> Russ
> >>>>>
> >>>>>> BR,
> >>>>>>
> >>>>>> Alan
> >>>>>>
> >>>>>> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> >>>>>>> Hi,
> >>>>>>>
> >>>>>>> Dont use boardctl for the romfs, this is too intertwined in the
> >>>>>>> mechanism used to initialize nsh, which is not suitable for
> >>>>>> customization.
> >>>>>>> This need to be decoupled, I've done this in my own apps. I'll send
> >>>>>>> it
> >>>>>>> later, maybe.
> >>>>>>>
> >>>>>>>
> >>>>>>> Here is the proper way to mount a romfs from your board bringup()
> >>>>>> routines:
> >>>>>>> I am using stm32 as example, the ROM fs directory holding the file
> >>>>>>> is
> >>> a
> >>>>>>> brother to board/
> >>>>>>>
> >>>>>>> boarddir:
> >>>>>>> +-- romfs
> >>>>>>>         +-- your files...
> >>>>>>> +-- board
> >>>>>>>         +-- include
> >>>>>>>         +-- src
> >>>>>>>
> >>>>>>> You can do something else if you change the makefile below
> >>>>>>>
> >>>>>>> First change your board/src makefile to add a dependency so that
> >>>>>>> stm32_bringup.c depends on romfs.h
> >>>>>>>
> >>>>>>> .PHONY: myromfs.h #might not be required
> >>>>>>> stm32_bringup.c: myromfs.h
> >>>>>>> myromfs.h:
> >>>>>>>            @echo "ROMFS"
> >>>>>>>            @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
> >>>>>>>            @xxd -i my.romfs > myromfs.h
> >>>>>>>
> >>>>>>> Then do this in stm32_bringup.c:
> >>>>>>>
> >>>>>>> #include "myromfs.h"
> >>>>>>> #define SECTORSIZE  64
> >>>>>>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
> >>>>>>> #include <nuttx/fs/fs.h>
> >>>>>>> #include <nuttx/drivers/ramdisk.h>
> >>>>>>> #include <sys/mount.h>
> >>>>>>>
> >>>>>>> and then in stm32_bringup(void):
> >>>>>>>
> >>>>>>>     ret = romdisk_register(0, // /dev/ram0
> >>>>>>>                             myromfs, //var in the xxd header
> >>>>>>>                             NSECTORS(myromfs_len),
> >>>>>>>                             SECTORSIZE);
> >>>>>>>
> >>>>>>>      ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY,
> >>>>>>> NULL);
> >>>>>>>
> >>>>>>> Then you will have your files mounted in /romfs at boot.
> >>>>>>>
> >>>>>>> You can do symlinks:
> >>>>>>>
> >>>>>>>     ret = symlink("/romfs/etc" , "/etc");
> >>>>>>>      _info("Linking /etc: %d\n", ret);
> >>>>>>>
> >>>>>>> Sebastien
> >>>>>>>
> >>>>>>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
> >>>>>>>> I am mistaken. I understood that the rom image was part of the
> >>>>>>>> application
> >>>>>>>> on flash, but had mis-read the documentation of
> >>>>>>>> boardctl(BOARDIOC_ROMDISK,
> >>>>>>>> (uintptr_t)&desc); and thought that the command loaded the image
> >>>>>>>> into
> >>>>>>>> *RAM*
> >>>>>>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> >>>>>>>>
> >>>>>>>> Incidentally, neither the romfs example nor my attempt to
> re-create
> >>> it
> >>>>>>>> works in my sim:
> >>>>>>>>
> >>>>>>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> >>>>>>>>
> >>>>>>>> NuttShell (NSH) NuttX-12.0.0
> >>>>>>>> nsh> romfs
> >>>>>>>> ERROR: Failed to create RAM disk: Unknown error
> >>>>>>>> nsh> rapp
> >>>>>>>> Starting Russells App 1...
> >>>>>>>> ERROR: Failed to create RAM disk: Unknown error
> >>>>>>>> nsh>
> >>>>>>>>
> >>>>>>>> I don't know how to debug this "unknown error". I guess I will try
> >>>>>>>> to
> >>>>>>>> find
> >>>>>>>> where that error message comes from and hook in GDB? I'm terrible
> >>>>>>>> at
> >>>>>> this
> >>>>>>>> stuff, someone needs to revoke my compiler license (tee hee).
> >>>>>>>>
> >>>>>>>> Cheers,
> >>>>>>>> Russ
> >>>>>>>>
> >>>>>>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao
> >>>>>>>> <xiaoxiang781216@gmail.com
> >>>>>>>> wrote:
> >>>>>>>>
> >>>>>>>>> romfs is part of your image as the const string. There is no
> >>>>> difference
> >>>>>>>>> from the below manual step.
> >>>>>>>>>
> >>>>>>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley
> >>>>>>>>> <russ.haley@gmail.com
> >>>>>>>>> wrote:
> >>>>>>>>>
> >>>>>>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> >>>>>>>>> f.j.panag@gmail.com
> >>>>>>>>>> wrote:
> >>>>>>>>>>
> >>>>>>>>>>> Hello,
> >>>>>>>>>>>
> >>>>>>>>>>> Indeed the "proper" way of including a script would be to store
> >>>>>>>>>>> it
> >>>>> in
> >>>>>>>>>>> a
> >>>>>>>>>>> file system.
> >>>>>>>>>>>
> >>>>>>>>>>> However, when I needed to include a single and small script and
> >>>>>>>>>>> I
> >>>>>>>>> didn't
> >>>>>>>>>>> want to introduce a complete FS just for this, I used xxd.
> >>>>>>>>>>> xxd can convert any file to a C header file.
> >>>>>>>>>>>
> >>>>>>>>>>> You can then include the header, and access the whole file as a
> >>>>>>>>> variable.
> >>>>>>>>>>> Here is an example:
> >>>>>>>>>>>
> >>>>>>>>>>> I added this in my app Makefile:
> >>>>>>>>>>>
> >>>>>>>>>>> # Setup any special pre-build context
> >>>>>>>>>>> context: header
> >>>>>>>>>>>            $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> >>>>>>>>>>> json_lua.h
> >>>>>>>>> &&
> >>>>>>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp
> json_lua.h
> >>>>>>>>>>>
> >>>>>>>>>>>
> >>>>>>>>>>>
> >>>>>>>>>>> And then I used the file like this:
> >>>>>>>>>>>
> >>>>>>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
> >>>>>>>>>>>     #include "json_lua.h"
> >>>>>>>>>>>     static int luaopen_json(lua_State * L);
> >>>>>>>>>>>
> >>>>>>>>>>>     void ExtLibs_load(lua_State * L){
> >>>>>>>>>>>            // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> >>>>>>>>>>>            luaL_requiref(L, "json", luaopen_json, 1);
> >>>>>>>>>>>            lua_pop(L, 1);#endif}
> >>>>>>>>>>>
> >>>>>>>>>>>     int luaopen_json(lua_State * L){
> >>>>>>>>>>>            const char * modname = lua_tostring(L, 1);
> >>>>>>>>>>>
> >>>>>>>>>>>            if (strcmp(modname, "json") != 0)
> >>>>>>>>>>>                    return luaL_error(L, "cannot load json
> >>>>>>>>>>> module");
> >>>>>>>>>>>
> >>>>>>>>>>>            if (luaL_loadbufferx(L, (char*)json_lua,
> >>>>>>>>>>> json_lua_len,
> >>>>>>>>>>> "json",
> >>>>>>>>>>> "t") != LUA_OK)
> >>>>>>>>>>>                    return lua_error(L);
> >>>>>>>>>>>
> >>>>>>>>>>>            lua_call(L, 0, 1);
> >>>>>>>>>>>
> >>>>>>>>>>>            return 1;}
> >>>>>>>>>>>
> >>>>>>>>>>>
> >>>>>>>>>>> I hope this helps...
> >>>>>>>>>>>
> >>>>>>>>>> That is very helpful, and not just for nuttx development! Thanks
> >>> for
> >>>>>>>>>> the
> >>>>>>>>>> tip.
> >>>>>>>>>>
> >>>>>>>>>> The romfs example actually uses xxd as well to convert the
> >>>>> filesystem
> >>>>>>>>> into
> >>>>>>>>>> hex code that is also stored in a header file. If I am reading
> >>>>>>>>>> the
> >>>>>> code
> >>>>>>>>>> correctly, the example app loads the entire filesystem into
> >>>>>>>>>> memory,
> >>>>>>>>>> which
> >>>>>>>>>> isn't very efficient and not at all what I wanted. Can someone
> >>>>>>>>>> tell
> >>>>> me
> >>>>>>>>>> if
> >>>>>>>>>> that's true?
> >>>>>>>>>>
> >>>>>>>>>> Thanks,
> >>>>>>>>>> Russ
> >>>>>>>>>>
> >>>>>>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> >>>>>> xiaoxiang781216@gmail.com>
> >>>>>>>>>>> wrote:
> >>>>>>>>>>>
> >>>>>>>>>>>> You can use the real file system on the device, there are many
> >>>>>>>>> choices:
> >>>>>>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
> >>>>>>>>>>>>
> >>>>>>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> >>>>>> russ.haley@gmail.com
> >>>>>>>>>>>> wrote:
> >>>>>>>>>>>>
> >>>>>>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> >>>>>>>>>> xiaoxiang781216@gmail.com>
> >>>>>>>>>>>>> wrote:
> >>>>>>>>>>>>>
> >>>>>>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> >>>>>>>>> scripts
> >>>>>>>>>>>> into
> >>>>>>>>>>>>>> some PC folder and run mount this folder from nsh:
> >>>>>>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>> While I appreciate the answer, I am using the sim as a
> >>>>>>>>>>>>>> testing
> >>>>>>>>>>> platform
> >>>>>>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony
> Spresense.
> >>>>>>>>>>>>> I
> >>>>> am
> >>>>>>>>>>>> hoping
> >>>>>>>>>>>>> for a solution that is applicable to an embedded project. If
> I
> >>>>>>>>> can't
> >>>>>>>>>>> just
> >>>>>>>>>>>>> add files to the initial image then I will look at the romfs
> >>>>>>>>> example
> >>>>>>>>>>> and
> >>>>>>>>>>>>> maybe the next best thing?
> >>>>>>>>>>>>>
> >>>>>>>>>>>>>
> >>>>>>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> >>>>>>>>>> russ.haley@gmail.com>
> >>>>>>>>>>>>>> wrote:
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>> Hi,
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
> >>>>>>>>>>>> configuration.
> >>>>>>>>>>>>> I
> >>>>>>>>>>>>>>> was unable to simply include the defconfig file that you
> >>> linked
> >>>>>>>>>> to,
> >>>>>>>>>>>>> but I
> >>>>>>>>>>>>>>> was able to reconfigure for the sim:lua configuration.
> I've
> >>>>>>>>> now
> >>>>>>>>>>> got
> >>>>>>>>>>>> an
> >>>>>>>>>>>>>> app
> >>>>>>>>>>>>>>> in the examples folder that includes the Lua interpreter.
> Is
> >>>>>>>>>> there
> >>>>>>>>>>> a
> >>>>>>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
> >>>>>>>>> files
> >>>>>>>>>>> in
> >>>>>>>>>>>>> the
> >>>>>>>>>>>>>>> initial file system?
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>> Much appreciated,
> >>>>>>>>>>>>>>> Russ
> >>>>>>>>>>>>>>>
> >
>

Re: Including Lua scripts on filesystem

Posted by "Alan C. Assis" <ac...@gmail.com>.
Hi Sebastien,

Please once again, don't start a frame war, we need people to
contribute to NuttX, to make our loved project great, not to destroy
it.

That said, I want to say a famous phrase from Linus Torvalds:

"Doers decide!"

If you want to decide how ROMFS should work, please do it.

It is an open-source project after all, so you are invited to
contribute, instead of starting a frame war against does who really do
and really contribute to make NuttX great.

About the System-V init you can use it from some projects, I heard
someone using it on NuttX:

https://github.com/arachsys/init/tree/master

BR,

Alan

On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> Hi,
>
> Once again please remember this project is Apache NuttX and not Xiaomi OS.
>
> The fact that your company is a major contributor and that you have
> established your own local usage patterns does not mean these are the
> only valid ones.
>
> Each user has its own vision of how such a flexible system should work,
> and all users count.
>
> So please dont suggest "bandaid" solutions to me like /etc is the only
> place to mount a romfs because the init script can "do things".
>
> It is perfectly valid to mount three romfs each at /foo /bar and /baz at
> boot and NuttX should allow it without telling me what's better.
>
> And I'm sorry but nsh is a demo shell and not an init system, on my own
> product there is NO WAY I want to give full shell access to my customer.
>
> This wont happen and nsh is not even part of my production build.
>
> FSes can be initialized in the board's _bringup routine and I dont want
> a systemd/init/shell to read scripts to mount /proc and bind a littlefs
> to a directory of my choosing. And _bringup is already used to init spi
> and mtd. Because NuttX is a RTOS and is meant to be more lightweight and
> flexible than a generic freebsd/macos/linux OS.
>
>
> Patch will come, I think this is a good change for nsh, but I'm a bit
> busy at work at this moment, stuck in a kind of dual-PIC18 cursed hell
> with hard deadlines lol.
>
> I also have a kind of "raid0" to aggregate two MTD with compatible
> geometries into one, because reasons. It needs more tests, but it could
> be useful for other users.
>
> Sebastien
>
>
> On 2/2/23 19:48, Xiang Xiao wrote:
>> On Fri, Feb 3, 2023 at 2:06 AM Sebastien Lorquet <se...@lorquet.fr>
>> wrote:
>>
>>> No, and this was exactly my problem.
>>>
>>> The romfs should NOT be mounted by nsh.
>>>
>>>
>> It's an option and disabled by default.
>> BTW, which place do you think is better than the current one?
>>
>>
>>> Mounting to /etc/ is also NOT flexible enough.
>>>
>>> Because in a normal board the boot app is not nsh but the customer app.
>>>
>>>
>> You can boot other applications/services from scripts.
>>
>>
>>> At the moment this romfs mount is also closely coupled to the boot
>>> script mechanism, which is very annoying if your boot script is on a
>>> flash device (eg littlefs) and not on a romfs.
>>>
>>> I have decoupled this, but still not provided a pull request, that was
>>> in fact easy and the result is cleaner than the current way it is done.
>>>
>>>
>>   Yes, the Kconfig option makes the romfs mount couple with boot script,
>> but
>> it is easy to decouple them. Patch is welcome.
>>
>>> You cant expect customers to follow a nsh-based design. The learning
>>> curve for nuttx is hard enough, and if you add more constraints in apps
>>> building it is even harder.
>>>
>>>
>> It depends on the developer's background. If the developer has
>> experience with other POSIX OS(Android, Ubuntu and macOS), all is
>> natural.
>>
>>
>>> Remember that NSH and apps are just "examples" and nuttx should work
>>> with completely separated apps.
>>>
>>>
>> Of course, all we discuss so far is the user space feature, not related
>> to
>> the kernel.
>>
>> Thats why I think nsh should not be responsible for too much.
>>>
>> nsh equals init pus sh. One improvement is split init from nsh like other
>> OS.
>>
>>
>>> Sebastien
>>>
>>>
>>> Le 02/02/2023 à 17:47, Xiang Xiao a écrit :
>>>> The infrastructure is there, you can:
>>>>
>>>>      1. Enable CONFIG_NSH_ROMFSETC
>>>>      2. Add your files to RCSRCS/RCRAWS in board's Makefile
>>>>
>>>> Then nsh will auto mount it to /etc. sim already has a demo for this:
>>>> https://github.com/apache/nuttx/tree/master/boards/sim/sim/sim/src/etc
>>>>
>>>>
>>>> On Fri, Feb 3, 2023 at 12:33 AM Russell Haley <ru...@gmail.com>
>>> wrote:
>>>>> On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> It should be nice to have a simple logic to let users to use ROMFS.
>>>>>>
>>>>>> Currently everyone needs to duplicate it in their own
>>>>>> code/application.
>>>>>>
>>>>>> I'm thinking something like apps/romfiles/ where people just put the
>>>>>> files that they want to be in the ROM and it will be included
>>>>>> automatically, instead reinventing the wheel.
>>>>>>
>>>>> This is what I was hoping to find.  I thought that since nuttx was
>>> already
>>>>> building a root filesystem there would be a simple way to include
>>>>> files
>>>>> with the image.
>>>>>
>>>>> Cheers,
>>>>> Russ
>>>>>
>>>>>> BR,
>>>>>>
>>>>>> Alan
>>>>>>
>>>>>> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> Dont use boardctl for the romfs, this is too intertwined in the
>>>>>>> mechanism used to initialize nsh, which is not suitable for
>>>>>> customization.
>>>>>>> This need to be decoupled, I've done this in my own apps. I'll send
>>>>>>> it
>>>>>>> later, maybe.
>>>>>>>
>>>>>>>
>>>>>>> Here is the proper way to mount a romfs from your board bringup()
>>>>>> routines:
>>>>>>> I am using stm32 as example, the ROM fs directory holding the file
>>>>>>> is
>>> a
>>>>>>> brother to board/
>>>>>>>
>>>>>>> boarddir:
>>>>>>> +-- romfs
>>>>>>>         +-- your files...
>>>>>>> +-- board
>>>>>>>         +-- include
>>>>>>>         +-- src
>>>>>>>
>>>>>>> You can do something else if you change the makefile below
>>>>>>>
>>>>>>> First change your board/src makefile to add a dependency so that
>>>>>>> stm32_bringup.c depends on romfs.h
>>>>>>>
>>>>>>> .PHONY: myromfs.h #might not be required
>>>>>>> stm32_bringup.c: myromfs.h
>>>>>>> myromfs.h:
>>>>>>>            @echo "ROMFS"
>>>>>>>            @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
>>>>>>>            @xxd -i my.romfs > myromfs.h
>>>>>>>
>>>>>>> Then do this in stm32_bringup.c:
>>>>>>>
>>>>>>> #include "myromfs.h"
>>>>>>> #define SECTORSIZE  64
>>>>>>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
>>>>>>> #include <nuttx/fs/fs.h>
>>>>>>> #include <nuttx/drivers/ramdisk.h>
>>>>>>> #include <sys/mount.h>
>>>>>>>
>>>>>>> and then in stm32_bringup(void):
>>>>>>>
>>>>>>>     ret = romdisk_register(0, // /dev/ram0
>>>>>>>                             myromfs, //var in the xxd header
>>>>>>>                             NSECTORS(myromfs_len),
>>>>>>>                             SECTORSIZE);
>>>>>>>
>>>>>>>      ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY,
>>>>>>> NULL);
>>>>>>>
>>>>>>> Then you will have your files mounted in /romfs at boot.
>>>>>>>
>>>>>>> You can do symlinks:
>>>>>>>
>>>>>>>     ret = symlink("/romfs/etc" , "/etc");
>>>>>>>      _info("Linking /etc: %d\n", ret);
>>>>>>>
>>>>>>> Sebastien
>>>>>>>
>>>>>>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
>>>>>>>> I am mistaken. I understood that the rom image was part of the
>>>>>>>> application
>>>>>>>> on flash, but had mis-read the documentation of
>>>>>>>> boardctl(BOARDIOC_ROMDISK,
>>>>>>>> (uintptr_t)&desc); and thought that the command loaded the image
>>>>>>>> into
>>>>>>>> *RAM*
>>>>>>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>>>>>>>>
>>>>>>>> Incidentally, neither the romfs example nor my attempt to re-create
>>> it
>>>>>>>> works in my sim:
>>>>>>>>
>>>>>>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>>>>>>>>
>>>>>>>> NuttShell (NSH) NuttX-12.0.0
>>>>>>>> nsh> romfs
>>>>>>>> ERROR: Failed to create RAM disk: Unknown error
>>>>>>>> nsh> rapp
>>>>>>>> Starting Russells App 1...
>>>>>>>> ERROR: Failed to create RAM disk: Unknown error
>>>>>>>> nsh>
>>>>>>>>
>>>>>>>> I don't know how to debug this "unknown error". I guess I will try
>>>>>>>> to
>>>>>>>> find
>>>>>>>> where that error message comes from and hook in GDB? I'm terrible
>>>>>>>> at
>>>>>> this
>>>>>>>> stuff, someone needs to revoke my compiler license (tee hee).
>>>>>>>>
>>>>>>>> Cheers,
>>>>>>>> Russ
>>>>>>>>
>>>>>>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao
>>>>>>>> <xiaoxiang781216@gmail.com
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> romfs is part of your image as the const string. There is no
>>>>> difference
>>>>>>>>> from the below manual step.
>>>>>>>>>
>>>>>>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley
>>>>>>>>> <russ.haley@gmail.com
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>>>>>>>>> f.j.panag@gmail.com
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>> Hello,
>>>>>>>>>>>
>>>>>>>>>>> Indeed the "proper" way of including a script would be to store
>>>>>>>>>>> it
>>>>> in
>>>>>>>>>>> a
>>>>>>>>>>> file system.
>>>>>>>>>>>
>>>>>>>>>>> However, when I needed to include a single and small script and
>>>>>>>>>>> I
>>>>>>>>> didn't
>>>>>>>>>>> want to introduce a complete FS just for this, I used xxd.
>>>>>>>>>>> xxd can convert any file to a C header file.
>>>>>>>>>>>
>>>>>>>>>>> You can then include the header, and access the whole file as a
>>>>>>>>> variable.
>>>>>>>>>>> Here is an example:
>>>>>>>>>>>
>>>>>>>>>>> I added this in my app Makefile:
>>>>>>>>>>>
>>>>>>>>>>> # Setup any special pre-build context
>>>>>>>>>>> context: header
>>>>>>>>>>>            $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>>>>>>>>>>> json_lua.h
>>>>>>>>> &&
>>>>>>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> And then I used the file like this:
>>>>>>>>>>>
>>>>>>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>>>>>>>>     #include "json_lua.h"
>>>>>>>>>>>     static int luaopen_json(lua_State * L);
>>>>>>>>>>>
>>>>>>>>>>>     void ExtLibs_load(lua_State * L){
>>>>>>>>>>>            // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>>>>>>>>            luaL_requiref(L, "json", luaopen_json, 1);
>>>>>>>>>>>            lua_pop(L, 1);#endif}
>>>>>>>>>>>
>>>>>>>>>>>     int luaopen_json(lua_State * L){
>>>>>>>>>>>            const char * modname = lua_tostring(L, 1);
>>>>>>>>>>>
>>>>>>>>>>>            if (strcmp(modname, "json") != 0)
>>>>>>>>>>>                    return luaL_error(L, "cannot load json
>>>>>>>>>>> module");
>>>>>>>>>>>
>>>>>>>>>>>            if (luaL_loadbufferx(L, (char*)json_lua,
>>>>>>>>>>> json_lua_len,
>>>>>>>>>>> "json",
>>>>>>>>>>> "t") != LUA_OK)
>>>>>>>>>>>                    return lua_error(L);
>>>>>>>>>>>
>>>>>>>>>>>            lua_call(L, 0, 1);
>>>>>>>>>>>
>>>>>>>>>>>            return 1;}
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> I hope this helps...
>>>>>>>>>>>
>>>>>>>>>> That is very helpful, and not just for nuttx development! Thanks
>>> for
>>>>>>>>>> the
>>>>>>>>>> tip.
>>>>>>>>>>
>>>>>>>>>> The romfs example actually uses xxd as well to convert the
>>>>> filesystem
>>>>>>>>> into
>>>>>>>>>> hex code that is also stored in a header file. If I am reading
>>>>>>>>>> the
>>>>>> code
>>>>>>>>>> correctly, the example app loads the entire filesystem into
>>>>>>>>>> memory,
>>>>>>>>>> which
>>>>>>>>>> isn't very efficient and not at all what I wanted. Can someone
>>>>>>>>>> tell
>>>>> me
>>>>>>>>>> if
>>>>>>>>>> that's true?
>>>>>>>>>>
>>>>>>>>>> Thanks,
>>>>>>>>>> Russ
>>>>>>>>>>
>>>>>>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
>>>>>> xiaoxiang781216@gmail.com>
>>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> You can use the real file system on the device, there are many
>>>>>>>>> choices:
>>>>>>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>>>>>>>>
>>>>>>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
>>>>>> russ.haley@gmail.com
>>>>>>>>>>>> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>>>>>>>>> xiaoxiang781216@gmail.com>
>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>>>>>>>>> scripts
>>>>>>>>>>>> into
>>>>>>>>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> While I appreciate the answer, I am using the sim as a
>>>>>>>>>>>>>> testing
>>>>>>>>>>> platform
>>>>>>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense.
>>>>>>>>>>>>> I
>>>>> am
>>>>>>>>>>>> hoping
>>>>>>>>>>>>> for a solution that is applicable to an embedded project. If I
>>>>>>>>> can't
>>>>>>>>>>> just
>>>>>>>>>>>>> add files to the initial image then I will look at the romfs
>>>>>>>>> example
>>>>>>>>>>> and
>>>>>>>>>>>>> maybe the next best thing?
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>>>>>>>>> russ.haley@gmail.com>
>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Hi,
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>>>>>>>>> configuration.
>>>>>>>>>>>>> I
>>>>>>>>>>>>>>> was unable to simply include the defconfig file that you
>>> linked
>>>>>>>>>> to,
>>>>>>>>>>>>> but I
>>>>>>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>>>>>>>>> now
>>>>>>>>>>> got
>>>>>>>>>>>> an
>>>>>>>>>>>>>> app
>>>>>>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>>>>>>>>> there
>>>>>>>>>>> a
>>>>>>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
>>>>>>>>> files
>>>>>>>>>>> in
>>>>>>>>>>>>> the
>>>>>>>>>>>>>>> initial file system?
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Much appreciated,
>>>>>>>>>>>>>>> Russ
>>>>>>>>>>>>>>>
>

Re: Including Lua scripts on filesystem

Posted by Sebastien Lorquet <se...@lorquet.fr>.
Hi,

Once again please remember this project is Apache NuttX and not Xiaomi OS.

The fact that your company is a major contributor and that you have 
established your own local usage patterns does not mean these are the 
only valid ones.

Each user has its own vision of how such a flexible system should work, 
and all users count.

So please dont suggest "bandaid" solutions to me like /etc is the only 
place to mount a romfs because the init script can "do things".

It is perfectly valid to mount three romfs each at /foo /bar and /baz at 
boot and NuttX should allow it without telling me what's better.

And I'm sorry but nsh is a demo shell and not an init system, on my own 
product there is NO WAY I want to give full shell access to my customer.

This wont happen and nsh is not even part of my production build.

FSes can be initialized in the board's _bringup routine and I dont want 
a systemd/init/shell to read scripts to mount /proc and bind a littlefs 
to a directory of my choosing. And _bringup is already used to init spi 
and mtd. Because NuttX is a RTOS and is meant to be more lightweight and 
flexible than a generic freebsd/macos/linux OS.


Patch will come, I think this is a good change for nsh, but I'm a bit 
busy at work at this moment, stuck in a kind of dual-PIC18 cursed hell 
with hard deadlines lol.

I also have a kind of "raid0" to aggregate two MTD with compatible 
geometries into one, because reasons. It needs more tests, but it could 
be useful for other users.

Sebastien


On 2/2/23 19:48, Xiang Xiao wrote:
> On Fri, Feb 3, 2023 at 2:06 AM Sebastien Lorquet <se...@lorquet.fr>
> wrote:
>
>> No, and this was exactly my problem.
>>
>> The romfs should NOT be mounted by nsh.
>>
>>
> It's an option and disabled by default.
> BTW, which place do you think is better than the current one?
>
>
>> Mounting to /etc/ is also NOT flexible enough.
>>
>> Because in a normal board the boot app is not nsh but the customer app.
>>
>>
> You can boot other applications/services from scripts.
>
>
>> At the moment this romfs mount is also closely coupled to the boot
>> script mechanism, which is very annoying if your boot script is on a
>> flash device (eg littlefs) and not on a romfs.
>>
>> I have decoupled this, but still not provided a pull request, that was
>> in fact easy and the result is cleaner than the current way it is done.
>>
>>
>   Yes, the Kconfig option makes the romfs mount couple with boot script, but
> it is easy to decouple them. Patch is welcome.
>
>> You cant expect customers to follow a nsh-based design. The learning
>> curve for nuttx is hard enough, and if you add more constraints in apps
>> building it is even harder.
>>
>>
> It depends on the developer's background. If the developer has
> experience with other POSIX OS(Android, Ubuntu and macOS), all is natural.
>
>
>> Remember that NSH and apps are just "examples" and nuttx should work
>> with completely separated apps.
>>
>>
> Of course, all we discuss so far is the user space feature, not related to
> the kernel.
>
> Thats why I think nsh should not be responsible for too much.
>>
> nsh equals init pus sh. One improvement is split init from nsh like other
> OS.
>
>
>> Sebastien
>>
>>
>> Le 02/02/2023 à 17:47, Xiang Xiao a écrit :
>>> The infrastructure is there, you can:
>>>
>>>      1. Enable CONFIG_NSH_ROMFSETC
>>>      2. Add your files to RCSRCS/RCRAWS in board's Makefile
>>>
>>> Then nsh will auto mount it to /etc. sim already has a demo for this:
>>> https://github.com/apache/nuttx/tree/master/boards/sim/sim/sim/src/etc
>>>
>>>
>>> On Fri, Feb 3, 2023 at 12:33 AM Russell Haley <ru...@gmail.com>
>> wrote:
>>>> On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com> wrote:
>>>>
>>>>> It should be nice to have a simple logic to let users to use ROMFS.
>>>>>
>>>>> Currently everyone needs to duplicate it in their own code/application.
>>>>>
>>>>> I'm thinking something like apps/romfiles/ where people just put the
>>>>> files that they want to be in the ROM and it will be included
>>>>> automatically, instead reinventing the wheel.
>>>>>
>>>> This is what I was hoping to find.  I thought that since nuttx was
>> already
>>>> building a root filesystem there would be a simple way to include files
>>>> with the image.
>>>>
>>>> Cheers,
>>>> Russ
>>>>
>>>>> BR,
>>>>>
>>>>> Alan
>>>>>
>>>>> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> Dont use boardctl for the romfs, this is too intertwined in the
>>>>>> mechanism used to initialize nsh, which is not suitable for
>>>>> customization.
>>>>>> This need to be decoupled, I've done this in my own apps. I'll send it
>>>>>> later, maybe.
>>>>>>
>>>>>>
>>>>>> Here is the proper way to mount a romfs from your board bringup()
>>>>> routines:
>>>>>> I am using stm32 as example, the ROM fs directory holding the file is
>> a
>>>>>> brother to board/
>>>>>>
>>>>>> boarddir:
>>>>>> +-- romfs
>>>>>>         +-- your files...
>>>>>> +-- board
>>>>>>         +-- include
>>>>>>         +-- src
>>>>>>
>>>>>> You can do something else if you change the makefile below
>>>>>>
>>>>>> First change your board/src makefile to add a dependency so that
>>>>>> stm32_bringup.c depends on romfs.h
>>>>>>
>>>>>> .PHONY: myromfs.h #might not be required
>>>>>> stm32_bringup.c: myromfs.h
>>>>>> myromfs.h:
>>>>>>            @echo "ROMFS"
>>>>>>            @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
>>>>>>            @xxd -i my.romfs > myromfs.h
>>>>>>
>>>>>> Then do this in stm32_bringup.c:
>>>>>>
>>>>>> #include "myromfs.h"
>>>>>> #define SECTORSIZE  64
>>>>>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
>>>>>> #include <nuttx/fs/fs.h>
>>>>>> #include <nuttx/drivers/ramdisk.h>
>>>>>> #include <sys/mount.h>
>>>>>>
>>>>>> and then in stm32_bringup(void):
>>>>>>
>>>>>>     ret = romdisk_register(0, // /dev/ram0
>>>>>>                             myromfs, //var in the xxd header
>>>>>>                             NSECTORS(myromfs_len),
>>>>>>                             SECTORSIZE);
>>>>>>
>>>>>>      ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
>>>>>>
>>>>>> Then you will have your files mounted in /romfs at boot.
>>>>>>
>>>>>> You can do symlinks:
>>>>>>
>>>>>>     ret = symlink("/romfs/etc" , "/etc");
>>>>>>      _info("Linking /etc: %d\n", ret);
>>>>>>
>>>>>> Sebastien
>>>>>>
>>>>>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
>>>>>>> I am mistaken. I understood that the rom image was part of the
>>>>>>> application
>>>>>>> on flash, but had mis-read the documentation of
>>>>>>> boardctl(BOARDIOC_ROMDISK,
>>>>>>> (uintptr_t)&desc); and thought that the command loaded the image into
>>>>>>> *RAM*
>>>>>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>>>>>>>
>>>>>>> Incidentally, neither the romfs example nor my attempt to re-create
>> it
>>>>>>> works in my sim:
>>>>>>>
>>>>>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>>>>>>>
>>>>>>> NuttShell (NSH) NuttX-12.0.0
>>>>>>> nsh> romfs
>>>>>>> ERROR: Failed to create RAM disk: Unknown error
>>>>>>> nsh> rapp
>>>>>>> Starting Russells App 1...
>>>>>>> ERROR: Failed to create RAM disk: Unknown error
>>>>>>> nsh>
>>>>>>>
>>>>>>> I don't know how to debug this "unknown error". I guess I will try to
>>>>>>> find
>>>>>>> where that error message comes from and hook in GDB? I'm terrible at
>>>>> this
>>>>>>> stuff, someone needs to revoke my compiler license (tee hee).
>>>>>>>
>>>>>>> Cheers,
>>>>>>> Russ
>>>>>>>
>>>>>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xiaoxiang781216@gmail.com
>>>>>>> wrote:
>>>>>>>
>>>>>>>> romfs is part of your image as the const string. There is no
>>>> difference
>>>>>>>> from the below manual step.
>>>>>>>>
>>>>>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <russ.haley@gmail.com
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>>>>>>>> f.j.panag@gmail.com
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> Hello,
>>>>>>>>>>
>>>>>>>>>> Indeed the "proper" way of including a script would be to store it
>>>> in
>>>>>>>>>> a
>>>>>>>>>> file system.
>>>>>>>>>>
>>>>>>>>>> However, when I needed to include a single and small script and I
>>>>>>>> didn't
>>>>>>>>>> want to introduce a complete FS just for this, I used xxd.
>>>>>>>>>> xxd can convert any file to a C header file.
>>>>>>>>>>
>>>>>>>>>> You can then include the header, and access the whole file as a
>>>>>>>> variable.
>>>>>>>>>> Here is an example:
>>>>>>>>>>
>>>>>>>>>> I added this in my app Makefile:
>>>>>>>>>>
>>>>>>>>>> # Setup any special pre-build context
>>>>>>>>>> context: header
>>>>>>>>>>            $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>>>>>>>>>> json_lua.h
>>>>>>>> &&
>>>>>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> And then I used the file like this:
>>>>>>>>>>
>>>>>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>>>>>>>     #include "json_lua.h"
>>>>>>>>>>     static int luaopen_json(lua_State * L);
>>>>>>>>>>
>>>>>>>>>>     void ExtLibs_load(lua_State * L){
>>>>>>>>>>            // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>>>>>>>            luaL_requiref(L, "json", luaopen_json, 1);
>>>>>>>>>>            lua_pop(L, 1);#endif}
>>>>>>>>>>
>>>>>>>>>>     int luaopen_json(lua_State * L){
>>>>>>>>>>            const char * modname = lua_tostring(L, 1);
>>>>>>>>>>
>>>>>>>>>>            if (strcmp(modname, "json") != 0)
>>>>>>>>>>                    return luaL_error(L, "cannot load json module");
>>>>>>>>>>
>>>>>>>>>>            if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>>>>>>>>>> "json",
>>>>>>>>>> "t") != LUA_OK)
>>>>>>>>>>                    return lua_error(L);
>>>>>>>>>>
>>>>>>>>>>            lua_call(L, 0, 1);
>>>>>>>>>>
>>>>>>>>>>            return 1;}
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> I hope this helps...
>>>>>>>>>>
>>>>>>>>> That is very helpful, and not just for nuttx development! Thanks
>> for
>>>>>>>>> the
>>>>>>>>> tip.
>>>>>>>>>
>>>>>>>>> The romfs example actually uses xxd as well to convert the
>>>> filesystem
>>>>>>>> into
>>>>>>>>> hex code that is also stored in a header file. If I am reading the
>>>>> code
>>>>>>>>> correctly, the example app loads the entire filesystem into memory,
>>>>>>>>> which
>>>>>>>>> isn't very efficient and not at all what I wanted. Can someone tell
>>>> me
>>>>>>>>> if
>>>>>>>>> that's true?
>>>>>>>>>
>>>>>>>>> Thanks,
>>>>>>>>> Russ
>>>>>>>>>
>>>>>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
>>>>> xiaoxiang781216@gmail.com>
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>> You can use the real file system on the device, there are many
>>>>>>>> choices:
>>>>>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>>>>>>>
>>>>>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
>>>>> russ.haley@gmail.com
>>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>>>>>>>> xiaoxiang781216@gmail.com>
>>>>>>>>>>>> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>>>>>>>> scripts
>>>>>>>>>>> into
>>>>>>>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>>>>>>>
>>>>>>>>>>>>> While I appreciate the answer, I am using the sim as a testing
>>>>>>>>>> platform
>>>>>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I
>>>> am
>>>>>>>>>>> hoping
>>>>>>>>>>>> for a solution that is applicable to an embedded project. If I
>>>>>>>> can't
>>>>>>>>>> just
>>>>>>>>>>>> add files to the initial image then I will look at the romfs
>>>>>>>> example
>>>>>>>>>> and
>>>>>>>>>>>> maybe the next best thing?
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>>>>>>>> russ.haley@gmail.com>
>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> Hi,
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>>>>>>>> configuration.
>>>>>>>>>>>> I
>>>>>>>>>>>>>> was unable to simply include the defconfig file that you
>> linked
>>>>>>>>> to,
>>>>>>>>>>>> but I
>>>>>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>>>>>>>> now
>>>>>>>>>> got
>>>>>>>>>>> an
>>>>>>>>>>>>> app
>>>>>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>>>>>>>> there
>>>>>>>>>> a
>>>>>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
>>>>>>>> files
>>>>>>>>>> in
>>>>>>>>>>>> the
>>>>>>>>>>>>>> initial file system?
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Much appreciated,
>>>>>>>>>>>>>> Russ
>>>>>>>>>>>>>>

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
On Fri, Feb 3, 2023 at 2:06 AM Sebastien Lorquet <se...@lorquet.fr>
wrote:

> No, and this was exactly my problem.
>
> The romfs should NOT be mounted by nsh.
>
>
It's an option and disabled by default.
BTW, which place do you think is better than the current one?


> Mounting to /etc/ is also NOT flexible enough.
>
> Because in a normal board the boot app is not nsh but the customer app.
>
>
You can boot other applications/services from scripts.


>
> At the moment this romfs mount is also closely coupled to the boot
> script mechanism, which is very annoying if your boot script is on a
> flash device (eg littlefs) and not on a romfs.
>
> I have decoupled this, but still not provided a pull request, that was
> in fact easy and the result is cleaner than the current way it is done.
>
>
 Yes, the Kconfig option makes the romfs mount couple with boot script, but
it is easy to decouple them. Patch is welcome.

>
> You cant expect customers to follow a nsh-based design. The learning
> curve for nuttx is hard enough, and if you add more constraints in apps
> building it is even harder.
>
>
It depends on the developer's background. If the developer has
experience with other POSIX OS(Android, Ubuntu and macOS), all is natural.


> Remember that NSH and apps are just "examples" and nuttx should work
> with completely separated apps.
>
>
Of course, all we discuss so far is the user space feature, not related to
the kernel.

Thats why I think nsh should not be responsible for too much.
>
>
nsh equals init pus sh. One improvement is split init from nsh like other
OS.


>
> Sebastien
>
>
> Le 02/02/2023 à 17:47, Xiang Xiao a écrit :
> > The infrastructure is there, you can:
> >
> >     1. Enable CONFIG_NSH_ROMFSETC
> >     2. Add your files to RCSRCS/RCRAWS in board's Makefile
> >
> > Then nsh will auto mount it to /etc. sim already has a demo for this:
> > https://github.com/apache/nuttx/tree/master/boards/sim/sim/sim/src/etc
> >
> >
> > On Fri, Feb 3, 2023 at 12:33 AM Russell Haley <ru...@gmail.com>
> wrote:
> >
> >> On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com> wrote:
> >>
> >>> It should be nice to have a simple logic to let users to use ROMFS.
> >>>
> >>> Currently everyone needs to duplicate it in their own code/application.
> >>>
> >>> I'm thinking something like apps/romfiles/ where people just put the
> >>> files that they want to be in the ROM and it will be included
> >>> automatically, instead reinventing the wheel.
> >>>
> >> This is what I was hoping to find.  I thought that since nuttx was
> already
> >> building a root filesystem there would be a simple way to include files
> >> with the image.
> >>
> >> Cheers,
> >> Russ
> >>
> >>> BR,
> >>>
> >>> Alan
> >>>
> >>> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> >>>> Hi,
> >>>>
> >>>> Dont use boardctl for the romfs, this is too intertwined in the
> >>>> mechanism used to initialize nsh, which is not suitable for
> >>> customization.
> >>>> This need to be decoupled, I've done this in my own apps. I'll send it
> >>>> later, maybe.
> >>>>
> >>>>
> >>>> Here is the proper way to mount a romfs from your board bringup()
> >>> routines:
> >>>> I am using stm32 as example, the ROM fs directory holding the file is
> a
> >>>> brother to board/
> >>>>
> >>>> boarddir:
> >>>> +-- romfs
> >>>>        +-- your files...
> >>>> +-- board
> >>>>        +-- include
> >>>>        +-- src
> >>>>
> >>>> You can do something else if you change the makefile below
> >>>>
> >>>> First change your board/src makefile to add a dependency so that
> >>>> stm32_bringup.c depends on romfs.h
> >>>>
> >>>> .PHONY: myromfs.h #might not be required
> >>>> stm32_bringup.c: myromfs.h
> >>>> myromfs.h:
> >>>>           @echo "ROMFS"
> >>>>           @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
> >>>>           @xxd -i my.romfs > myromfs.h
> >>>>
> >>>> Then do this in stm32_bringup.c:
> >>>>
> >>>> #include "myromfs.h"
> >>>> #define SECTORSIZE  64
> >>>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
> >>>> #include <nuttx/fs/fs.h>
> >>>> #include <nuttx/drivers/ramdisk.h>
> >>>> #include <sys/mount.h>
> >>>>
> >>>> and then in stm32_bringup(void):
> >>>>
> >>>>    ret = romdisk_register(0, // /dev/ram0
> >>>>                            myromfs, //var in the xxd header
> >>>>                            NSECTORS(myromfs_len),
> >>>>                            SECTORSIZE);
> >>>>
> >>>>     ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
> >>>>
> >>>> Then you will have your files mounted in /romfs at boot.
> >>>>
> >>>> You can do symlinks:
> >>>>
> >>>>    ret = symlink("/romfs/etc" , "/etc");
> >>>>     _info("Linking /etc: %d\n", ret);
> >>>>
> >>>> Sebastien
> >>>>
> >>>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
> >>>>> I am mistaken. I understood that the rom image was part of the
> >>>>> application
> >>>>> on flash, but had mis-read the documentation of
> >>>>> boardctl(BOARDIOC_ROMDISK,
> >>>>> (uintptr_t)&desc); and thought that the command loaded the image into
> >>>>> *RAM*
> >>>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> >>>>>
> >>>>> Incidentally, neither the romfs example nor my attempt to re-create
> it
> >>>>> works in my sim:
> >>>>>
> >>>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> >>>>>
> >>>>> NuttShell (NSH) NuttX-12.0.0
> >>>>> nsh> romfs
> >>>>> ERROR: Failed to create RAM disk: Unknown error
> >>>>> nsh> rapp
> >>>>> Starting Russells App 1...
> >>>>> ERROR: Failed to create RAM disk: Unknown error
> >>>>> nsh>
> >>>>>
> >>>>> I don't know how to debug this "unknown error". I guess I will try to
> >>>>> find
> >>>>> where that error message comes from and hook in GDB? I'm terrible at
> >>> this
> >>>>> stuff, someone needs to revoke my compiler license (tee hee).
> >>>>>
> >>>>> Cheers,
> >>>>> Russ
> >>>>>
> >>>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xiaoxiang781216@gmail.com
> >
> >>>>> wrote:
> >>>>>
> >>>>>> romfs is part of your image as the const string. There is no
> >> difference
> >>>>>> from the below manual step.
> >>>>>>
> >>>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <russ.haley@gmail.com
> >
> >>>>>> wrote:
> >>>>>>
> >>>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> >>>>>> f.j.panag@gmail.com
> >>>>>>> wrote:
> >>>>>>>
> >>>>>>>> Hello,
> >>>>>>>>
> >>>>>>>> Indeed the "proper" way of including a script would be to store it
> >> in
> >>>>>>>> a
> >>>>>>>> file system.
> >>>>>>>>
> >>>>>>>> However, when I needed to include a single and small script and I
> >>>>>> didn't
> >>>>>>>> want to introduce a complete FS just for this, I used xxd.
> >>>>>>>> xxd can convert any file to a C header file.
> >>>>>>>>
> >>>>>>>> You can then include the header, and access the whole file as a
> >>>>>> variable.
> >>>>>>>> Here is an example:
> >>>>>>>>
> >>>>>>>> I added this in my app Makefile:
> >>>>>>>>
> >>>>>>>> # Setup any special pre-build context
> >>>>>>>> context: header
> >>>>>>>>           $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> >>>>>>>> json_lua.h
> >>>>>> &&
> >>>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> >>>>>>>>
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> And then I used the file like this:
> >>>>>>>>
> >>>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
> >>>>>>>>    #include "json_lua.h"
> >>>>>>>>    static int luaopen_json(lua_State * L);
> >>>>>>>>
> >>>>>>>>    void ExtLibs_load(lua_State * L){
> >>>>>>>>           // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> >>>>>>>>           luaL_requiref(L, "json", luaopen_json, 1);
> >>>>>>>>           lua_pop(L, 1);#endif}
> >>>>>>>>
> >>>>>>>>    int luaopen_json(lua_State * L){
> >>>>>>>>           const char * modname = lua_tostring(L, 1);
> >>>>>>>>
> >>>>>>>>           if (strcmp(modname, "json") != 0)
> >>>>>>>>                   return luaL_error(L, "cannot load json module");
> >>>>>>>>
> >>>>>>>>           if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> >>>>>>>> "json",
> >>>>>>>> "t") != LUA_OK)
> >>>>>>>>                   return lua_error(L);
> >>>>>>>>
> >>>>>>>>           lua_call(L, 0, 1);
> >>>>>>>>
> >>>>>>>>           return 1;}
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> I hope this helps...
> >>>>>>>>
> >>>>>>> That is very helpful, and not just for nuttx development! Thanks
> for
> >>>>>>> the
> >>>>>>> tip.
> >>>>>>>
> >>>>>>> The romfs example actually uses xxd as well to convert the
> >> filesystem
> >>>>>> into
> >>>>>>> hex code that is also stored in a header file. If I am reading the
> >>> code
> >>>>>>> correctly, the example app loads the entire filesystem into memory,
> >>>>>>> which
> >>>>>>> isn't very efficient and not at all what I wanted. Can someone tell
> >> me
> >>>>>>> if
> >>>>>>> that's true?
> >>>>>>>
> >>>>>>> Thanks,
> >>>>>>> Russ
> >>>>>>>
> >>>>>>>>
> >>>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> >>> xiaoxiang781216@gmail.com>
> >>>>>>>> wrote:
> >>>>>>>>
> >>>>>>>>> You can use the real file system on the device, there are many
> >>>>>> choices:
> >>>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
> >>>>>>>>>
> >>>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> >>> russ.haley@gmail.com
> >>>>>>>>> wrote:
> >>>>>>>>>
> >>>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> >>>>>>> xiaoxiang781216@gmail.com>
> >>>>>>>>>> wrote:
> >>>>>>>>>>
> >>>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> >>>>>> scripts
> >>>>>>>>> into
> >>>>>>>>>>> some PC folder and run mount this folder from nsh:
> >>>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> >>>>>>>>>>>
> >>>>>>>>>>> While I appreciate the answer, I am using the sim as a testing
> >>>>>>>> platform
> >>>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I
> >> am
> >>>>>>>>> hoping
> >>>>>>>>>> for a solution that is applicable to an embedded project. If I
> >>>>>> can't
> >>>>>>>> just
> >>>>>>>>>> add files to the initial image then I will look at the romfs
> >>>>>> example
> >>>>>>>> and
> >>>>>>>>>> maybe the next best thing?
> >>>>>>>>>>
> >>>>>>>>>>
> >>>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> >>>>>>> russ.haley@gmail.com>
> >>>>>>>>>>> wrote:
> >>>>>>>>>>>
> >>>>>>>>>>>> Hi,
> >>>>>>>>>>>>
> >>>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
> >>>>>>>>> configuration.
> >>>>>>>>>> I
> >>>>>>>>>>>> was unable to simply include the defconfig file that you
> linked
> >>>>>>> to,
> >>>>>>>>>> but I
> >>>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
> >>>>>> now
> >>>>>>>> got
> >>>>>>>>> an
> >>>>>>>>>>> app
> >>>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
> >>>>>>> there
> >>>>>>>> a
> >>>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
> >>>>>> files
> >>>>>>>> in
> >>>>>>>>>> the
> >>>>>>>>>>>> initial file system?
> >>>>>>>>>>>>
> >>>>>>>>>>>> Much appreciated,
> >>>>>>>>>>>> Russ
> >>>>>>>>>>>>
>

Re: Including Lua scripts on filesystem

Posted by Sebastien Lorquet <se...@lorquet.fr>.
No, and this was exactly my problem.

The romfs should NOT be mounted by nsh.

Mounting to /etc/ is also NOT flexible enough.

Because in a normal board the boot app is not nsh but the customer app.


At the moment this romfs mount is also closely coupled to the boot 
script mechanism, which is very annoying if your boot script is on a 
flash device (eg littlefs) and not on a romfs.

I have decoupled this, but still not provided a pull request, that was 
in fact easy and the result is cleaner than the current way it is done.


You cant expect customers to follow a nsh-based design. The learning 
curve for nuttx is hard enough, and if you add more constraints in apps 
building it is even harder.

Remember that NSH and apps are just "examples" and nuttx should work 
with completely separated apps.

Thats why I think nsh should not be responsible for too much.


Sebastien


Le 02/02/2023 à 17:47, Xiang Xiao a écrit :
> The infrastructure is there, you can:
>
>     1. Enable CONFIG_NSH_ROMFSETC
>     2. Add your files to RCSRCS/RCRAWS in board's Makefile
>
> Then nsh will auto mount it to /etc. sim already has a demo for this:
> https://github.com/apache/nuttx/tree/master/boards/sim/sim/sim/src/etc
>
>
> On Fri, Feb 3, 2023 at 12:33 AM Russell Haley <ru...@gmail.com> wrote:
>
>> On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com> wrote:
>>
>>> It should be nice to have a simple logic to let users to use ROMFS.
>>>
>>> Currently everyone needs to duplicate it in their own code/application.
>>>
>>> I'm thinking something like apps/romfiles/ where people just put the
>>> files that they want to be in the ROM and it will be included
>>> automatically, instead reinventing the wheel.
>>>
>> This is what I was hoping to find.  I thought that since nuttx was already
>> building a root filesystem there would be a simple way to include files
>> with the image.
>>
>> Cheers,
>> Russ
>>
>>> BR,
>>>
>>> Alan
>>>
>>> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
>>>> Hi,
>>>>
>>>> Dont use boardctl for the romfs, this is too intertwined in the
>>>> mechanism used to initialize nsh, which is not suitable for
>>> customization.
>>>> This need to be decoupled, I've done this in my own apps. I'll send it
>>>> later, maybe.
>>>>
>>>>
>>>> Here is the proper way to mount a romfs from your board bringup()
>>> routines:
>>>> I am using stm32 as example, the ROM fs directory holding the file is a
>>>> brother to board/
>>>>
>>>> boarddir:
>>>> +-- romfs
>>>>        +-- your files...
>>>> +-- board
>>>>        +-- include
>>>>        +-- src
>>>>
>>>> You can do something else if you change the makefile below
>>>>
>>>> First change your board/src makefile to add a dependency so that
>>>> stm32_bringup.c depends on romfs.h
>>>>
>>>> .PHONY: myromfs.h #might not be required
>>>> stm32_bringup.c: myromfs.h
>>>> myromfs.h:
>>>>           @echo "ROMFS"
>>>>           @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
>>>>           @xxd -i my.romfs > myromfs.h
>>>>
>>>> Then do this in stm32_bringup.c:
>>>>
>>>> #include "myromfs.h"
>>>> #define SECTORSIZE  64
>>>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
>>>> #include <nuttx/fs/fs.h>
>>>> #include <nuttx/drivers/ramdisk.h>
>>>> #include <sys/mount.h>
>>>>
>>>> and then in stm32_bringup(void):
>>>>
>>>>    ret = romdisk_register(0, // /dev/ram0
>>>>                            myromfs, //var in the xxd header
>>>>                            NSECTORS(myromfs_len),
>>>>                            SECTORSIZE);
>>>>
>>>>     ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
>>>>
>>>> Then you will have your files mounted in /romfs at boot.
>>>>
>>>> You can do symlinks:
>>>>
>>>>    ret = symlink("/romfs/etc" , "/etc");
>>>>     _info("Linking /etc: %d\n", ret);
>>>>
>>>> Sebastien
>>>>
>>>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
>>>>> I am mistaken. I understood that the rom image was part of the
>>>>> application
>>>>> on flash, but had mis-read the documentation of
>>>>> boardctl(BOARDIOC_ROMDISK,
>>>>> (uintptr_t)&desc); and thought that the command loaded the image into
>>>>> *RAM*
>>>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>>>>>
>>>>> Incidentally, neither the romfs example nor my attempt to re-create it
>>>>> works in my sim:
>>>>>
>>>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>>>>>
>>>>> NuttShell (NSH) NuttX-12.0.0
>>>>> nsh> romfs
>>>>> ERROR: Failed to create RAM disk: Unknown error
>>>>> nsh> rapp
>>>>> Starting Russells App 1...
>>>>> ERROR: Failed to create RAM disk: Unknown error
>>>>> nsh>
>>>>>
>>>>> I don't know how to debug this "unknown error". I guess I will try to
>>>>> find
>>>>> where that error message comes from and hook in GDB? I'm terrible at
>>> this
>>>>> stuff, someone needs to revoke my compiler license (tee hee).
>>>>>
>>>>> Cheers,
>>>>> Russ
>>>>>
>>>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> romfs is part of your image as the const string. There is no
>> difference
>>>>>> from the below manual step.
>>>>>>
>>>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>>>>>> f.j.panag@gmail.com
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Hello,
>>>>>>>>
>>>>>>>> Indeed the "proper" way of including a script would be to store it
>> in
>>>>>>>> a
>>>>>>>> file system.
>>>>>>>>
>>>>>>>> However, when I needed to include a single and small script and I
>>>>>> didn't
>>>>>>>> want to introduce a complete FS just for this, I used xxd.
>>>>>>>> xxd can convert any file to a C header file.
>>>>>>>>
>>>>>>>> You can then include the header, and access the whole file as a
>>>>>> variable.
>>>>>>>> Here is an example:
>>>>>>>>
>>>>>>>> I added this in my app Makefile:
>>>>>>>>
>>>>>>>> # Setup any special pre-build context
>>>>>>>> context: header
>>>>>>>>           $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>>>>>>>> json_lua.h
>>>>>> &&
>>>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> And then I used the file like this:
>>>>>>>>
>>>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>>>>>    #include "json_lua.h"
>>>>>>>>    static int luaopen_json(lua_State * L);
>>>>>>>>
>>>>>>>>    void ExtLibs_load(lua_State * L){
>>>>>>>>           // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>>>>>           luaL_requiref(L, "json", luaopen_json, 1);
>>>>>>>>           lua_pop(L, 1);#endif}
>>>>>>>>
>>>>>>>>    int luaopen_json(lua_State * L){
>>>>>>>>           const char * modname = lua_tostring(L, 1);
>>>>>>>>
>>>>>>>>           if (strcmp(modname, "json") != 0)
>>>>>>>>                   return luaL_error(L, "cannot load json module");
>>>>>>>>
>>>>>>>>           if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>>>>>>>> "json",
>>>>>>>> "t") != LUA_OK)
>>>>>>>>                   return lua_error(L);
>>>>>>>>
>>>>>>>>           lua_call(L, 0, 1);
>>>>>>>>
>>>>>>>>           return 1;}
>>>>>>>>
>>>>>>>>
>>>>>>>> I hope this helps...
>>>>>>>>
>>>>>>> That is very helpful, and not just for nuttx development! Thanks for
>>>>>>> the
>>>>>>> tip.
>>>>>>>
>>>>>>> The romfs example actually uses xxd as well to convert the
>> filesystem
>>>>>> into
>>>>>>> hex code that is also stored in a header file. If I am reading the
>>> code
>>>>>>> correctly, the example app loads the entire filesystem into memory,
>>>>>>> which
>>>>>>> isn't very efficient and not at all what I wanted. Can someone tell
>> me
>>>>>>> if
>>>>>>> that's true?
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Russ
>>>>>>>
>>>>>>>>
>>>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
>>> xiaoxiang781216@gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> You can use the real file system on the device, there are many
>>>>>> choices:
>>>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>>>>>
>>>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
>>> russ.haley@gmail.com
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>>>>>> xiaoxiang781216@gmail.com>
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>>>>>> scripts
>>>>>>>>> into
>>>>>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>>>>>
>>>>>>>>>>> While I appreciate the answer, I am using the sim as a testing
>>>>>>>> platform
>>>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I
>> am
>>>>>>>>> hoping
>>>>>>>>>> for a solution that is applicable to an embedded project. If I
>>>>>> can't
>>>>>>>> just
>>>>>>>>>> add files to the initial image then I will look at the romfs
>>>>>> example
>>>>>>>> and
>>>>>>>>>> maybe the next best thing?
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>>>>>> russ.haley@gmail.com>
>>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Hi,
>>>>>>>>>>>>
>>>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>>>>>> configuration.
>>>>>>>>>> I
>>>>>>>>>>>> was unable to simply include the defconfig file that you linked
>>>>>>> to,
>>>>>>>>>> but I
>>>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>>>>>> now
>>>>>>>> got
>>>>>>>>> an
>>>>>>>>>>> app
>>>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>>>>>> there
>>>>>>>> a
>>>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
>>>>>> files
>>>>>>>> in
>>>>>>>>>> the
>>>>>>>>>>>> initial file system?
>>>>>>>>>>>>
>>>>>>>>>>>> Much appreciated,
>>>>>>>>>>>> Russ
>>>>>>>>>>>>

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
The infrastructure is there, you can:

   1. Enable CONFIG_NSH_ROMFSETC
   2. Add your files to RCSRCS/RCRAWS in board's Makefile

Then nsh will auto mount it to /etc. sim already has a demo for this:
https://github.com/apache/nuttx/tree/master/boards/sim/sim/sim/src/etc


On Fri, Feb 3, 2023 at 12:33 AM Russell Haley <ru...@gmail.com> wrote:

> On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com> wrote:
>
> > It should be nice to have a simple logic to let users to use ROMFS.
> >
> > Currently everyone needs to duplicate it in their own code/application.
> >
> > I'm thinking something like apps/romfiles/ where people just put the
> > files that they want to be in the ROM and it will be included
> > automatically, instead reinventing the wheel.
> >
>
> This is what I was hoping to find.  I thought that since nuttx was already
> building a root filesystem there would be a simple way to include files
> with the image.
>
> Cheers,
> Russ
>
> >
> > BR,
> >
> > Alan
> >
> > On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> > > Hi,
> > >
> > > Dont use boardctl for the romfs, this is too intertwined in the
> > > mechanism used to initialize nsh, which is not suitable for
> > customization.
> > >
> > > This need to be decoupled, I've done this in my own apps. I'll send it
> > > later, maybe.
> > >
> > >
> > > Here is the proper way to mount a romfs from your board bringup()
> > routines:
> > >
> > > I am using stm32 as example, the ROM fs directory holding the file is a
> > > brother to board/
> > >
> > > boarddir:
> > > +-- romfs
> > >       +-- your files...
> > > +-- board
> > >       +-- include
> > >       +-- src
> > >
> > > You can do something else if you change the makefile below
> > >
> > > First change your board/src makefile to add a dependency so that
> > > stm32_bringup.c depends on romfs.h
> > >
> > > .PHONY: myromfs.h #might not be required
> > > stm32_bringup.c: myromfs.h
> > > myromfs.h:
> > >          @echo "ROMFS"
> > >          @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
> > >          @xxd -i my.romfs > myromfs.h
> > >
> > > Then do this in stm32_bringup.c:
> > >
> > > #include "myromfs.h"
> > > #define SECTORSIZE  64
> > > #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
> > > #include <nuttx/fs/fs.h>
> > > #include <nuttx/drivers/ramdisk.h>
> > > #include <sys/mount.h>
> > >
> > > and then in stm32_bringup(void):
> > >
> > >   ret = romdisk_register(0, // /dev/ram0
> > >                           myromfs, //var in the xxd header
> > >                           NSECTORS(myromfs_len),
> > >                           SECTORSIZE);
> > >
> > >    ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
> > >
> > > Then you will have your files mounted in /romfs at boot.
> > >
> > > You can do symlinks:
> > >
> > >   ret = symlink("/romfs/etc" , "/etc");
> > >    _info("Linking /etc: %d\n", ret);
> > >
> > > Sebastien
> > >
> > > Le 02/02/2023 à 05:59, Russell Haley a écrit :
> > >> I am mistaken. I understood that the rom image was part of the
> > >> application
> > >> on flash, but had mis-read the documentation of
> > >> boardctl(BOARDIOC_ROMDISK,
> > >> (uintptr_t)&desc); and thought that the command loaded the image into
> > >> *RAM*
> > >> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> > >>
> > >> Incidentally, neither the romfs example nor my attempt to re-create it
> > >> works in my sim:
> > >>
> > >> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> > >>
> > >> NuttShell (NSH) NuttX-12.0.0
> > >> nsh> romfs
> > >> ERROR: Failed to create RAM disk: Unknown error
> > >> nsh> rapp
> > >> Starting Russells App 1...
> > >> ERROR: Failed to create RAM disk: Unknown error
> > >> nsh>
> > >>
> > >> I don't know how to debug this "unknown error". I guess I will try to
> > >> find
> > >> where that error message comes from and hook in GDB? I'm terrible at
> > this
> > >> stuff, someone needs to revoke my compiler license (tee hee).
> > >>
> > >> Cheers,
> > >> Russ
> > >>
> > >> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> > >> wrote:
> > >>
> > >>> romfs is part of your image as the const string. There is no
> difference
> > >>> from the below manual step.
> > >>>
> > >>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> > >>> wrote:
> > >>>
> > >>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> > >>> f.j.panag@gmail.com
> > >>>> wrote:
> > >>>>
> > >>>>> Hello,
> > >>>>>
> > >>>>> Indeed the "proper" way of including a script would be to store it
> in
> > >>>>> a
> > >>>>> file system.
> > >>>>>
> > >>>>> However, when I needed to include a single and small script and I
> > >>> didn't
> > >>>>> want to introduce a complete FS just for this, I used xxd.
> > >>>>> xxd can convert any file to a C header file.
> > >>>>>
> > >>>>> You can then include the header, and access the whole file as a
> > >>> variable.
> > >>>>> Here is an example:
> > >>>>>
> > >>>>> I added this in my app Makefile:
> > >>>>>
> > >>>>> # Setup any special pre-build context
> > >>>>> context: header
> > >>>>>          $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> > >>>>> json_lua.h
> > >>> &&
> > >>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>> And then I used the file like this:
> > >>>>>
> > >>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
> > >>>>>   #include "json_lua.h"
> > >>>>>   static int luaopen_json(lua_State * L);
> > >>>>>
> > >>>>>   void ExtLibs_load(lua_State * L){
> > >>>>>          // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> > >>>>>          luaL_requiref(L, "json", luaopen_json, 1);
> > >>>>>          lua_pop(L, 1);#endif}
> > >>>>>
> > >>>>>   int luaopen_json(lua_State * L){
> > >>>>>          const char * modname = lua_tostring(L, 1);
> > >>>>>
> > >>>>>          if (strcmp(modname, "json") != 0)
> > >>>>>                  return luaL_error(L, "cannot load json module");
> > >>>>>
> > >>>>>          if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> > >>>>> "json",
> > >>>>> "t") != LUA_OK)
> > >>>>>                  return lua_error(L);
> > >>>>>
> > >>>>>          lua_call(L, 0, 1);
> > >>>>>
> > >>>>>          return 1;}
> > >>>>>
> > >>>>>
> > >>>>> I hope this helps...
> > >>>>>
> > >>>> That is very helpful, and not just for nuttx development! Thanks for
> > >>>> the
> > >>>> tip.
> > >>>>
> > >>>> The romfs example actually uses xxd as well to convert the
> filesystem
> > >>> into
> > >>>> hex code that is also stored in a header file. If I am reading the
> > code
> > >>>> correctly, the example app loads the entire filesystem into memory,
> > >>>> which
> > >>>> isn't very efficient and not at all what I wanted. Can someone tell
> me
> > >>>> if
> > >>>> that's true?
> > >>>>
> > >>>> Thanks,
> > >>>> Russ
> > >>>>
> > >>>>>
> > >>>>>
> > >>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> > xiaoxiang781216@gmail.com>
> > >>>>> wrote:
> > >>>>>
> > >>>>>> You can use the real file system on the device, there are many
> > >>> choices:
> > >>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
> > >>>>>>
> > >>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> > russ.haley@gmail.com
> > >>>>>> wrote:
> > >>>>>>
> > >>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> > >>>> xiaoxiang781216@gmail.com>
> > >>>>>>> wrote:
> > >>>>>>>
> > >>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> > >>> scripts
> > >>>>>> into
> > >>>>>>>> some PC folder and run mount this folder from nsh:
> > >>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > >>>>>>>>
> > >>>>>>>> While I appreciate the answer, I am using the sim as a testing
> > >>>>> platform
> > >>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I
> am
> > >>>>>> hoping
> > >>>>>>> for a solution that is applicable to an embedded project. If I
> > >>> can't
> > >>>>> just
> > >>>>>>> add files to the initial image then I will look at the romfs
> > >>> example
> > >>>>> and
> > >>>>>>> maybe the next best thing?
> > >>>>>>>
> > >>>>>>>
> > >>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> > >>>> russ.haley@gmail.com>
> > >>>>>>>> wrote:
> > >>>>>>>>
> > >>>>>>>>> Hi,
> > >>>>>>>>>
> > >>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
> > >>>>>> configuration.
> > >>>>>>> I
> > >>>>>>>>> was unable to simply include the defconfig file that you linked
> > >>>> to,
> > >>>>>>> but I
> > >>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
> > >>> now
> > >>>>> got
> > >>>>>> an
> > >>>>>>>> app
> > >>>>>>>>> in the examples folder that includes the Lua interpreter. Is
> > >>>> there
> > >>>>> a
> > >>>>>>>>> tutorial on how to include folders and lua scripts or extra
> > >>> files
> > >>>>> in
> > >>>>>>> the
> > >>>>>>>>> initial file system?
> > >>>>>>>>>
> > >>>>>>>>> Much appreciated,
> > >>>>>>>>> Russ
> > >>>>>>>>>
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by Russell Haley <ru...@gmail.com>.
On Thu, Feb 2, 2023 at 6:35 AM Alan C. Assis <ac...@gmail.com> wrote:

> It should be nice to have a simple logic to let users to use ROMFS.
>
> Currently everyone needs to duplicate it in their own code/application.
>
> I'm thinking something like apps/romfiles/ where people just put the
> files that they want to be in the ROM and it will be included
> automatically, instead reinventing the wheel.
>

This is what I was hoping to find.  I thought that since nuttx was already
building a root filesystem there would be a simple way to include files
with the image.

Cheers,
Russ

>
> BR,
>
> Alan
>
> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> > Hi,
> >
> > Dont use boardctl for the romfs, this is too intertwined in the
> > mechanism used to initialize nsh, which is not suitable for
> customization.
> >
> > This need to be decoupled, I've done this in my own apps. I'll send it
> > later, maybe.
> >
> >
> > Here is the proper way to mount a romfs from your board bringup()
> routines:
> >
> > I am using stm32 as example, the ROM fs directory holding the file is a
> > brother to board/
> >
> > boarddir:
> > +-- romfs
> >       +-- your files...
> > +-- board
> >       +-- include
> >       +-- src
> >
> > You can do something else if you change the makefile below
> >
> > First change your board/src makefile to add a dependency so that
> > stm32_bringup.c depends on romfs.h
> >
> > .PHONY: myromfs.h #might not be required
> > stm32_bringup.c: myromfs.h
> > myromfs.h:
> >          @echo "ROMFS"
> >          @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
> >          @xxd -i my.romfs > myromfs.h
> >
> > Then do this in stm32_bringup.c:
> >
> > #include "myromfs.h"
> > #define SECTORSIZE  64
> > #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
> > #include <nuttx/fs/fs.h>
> > #include <nuttx/drivers/ramdisk.h>
> > #include <sys/mount.h>
> >
> > and then in stm32_bringup(void):
> >
> >   ret = romdisk_register(0, // /dev/ram0
> >                           myromfs, //var in the xxd header
> >                           NSECTORS(myromfs_len),
> >                           SECTORSIZE);
> >
> >    ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
> >
> > Then you will have your files mounted in /romfs at boot.
> >
> > You can do symlinks:
> >
> >   ret = symlink("/romfs/etc" , "/etc");
> >    _info("Linking /etc: %d\n", ret);
> >
> > Sebastien
> >
> > Le 02/02/2023 à 05:59, Russell Haley a écrit :
> >> I am mistaken. I understood that the rom image was part of the
> >> application
> >> on flash, but had mis-read the documentation of
> >> boardctl(BOARDIOC_ROMDISK,
> >> (uintptr_t)&desc); and thought that the command loaded the image into
> >> *RAM*
> >> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> >>
> >> Incidentally, neither the romfs example nor my attempt to re-create it
> >> works in my sim:
> >>
> >> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> >>
> >> NuttShell (NSH) NuttX-12.0.0
> >> nsh> romfs
> >> ERROR: Failed to create RAM disk: Unknown error
> >> nsh> rapp
> >> Starting Russells App 1...
> >> ERROR: Failed to create RAM disk: Unknown error
> >> nsh>
> >>
> >> I don't know how to debug this "unknown error". I guess I will try to
> >> find
> >> where that error message comes from and hook in GDB? I'm terrible at
> this
> >> stuff, someone needs to revoke my compiler license (tee hee).
> >>
> >> Cheers,
> >> Russ
> >>
> >> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> >> wrote:
> >>
> >>> romfs is part of your image as the const string. There is no difference
> >>> from the below manual step.
> >>>
> >>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> >>> wrote:
> >>>
> >>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> >>> f.j.panag@gmail.com
> >>>> wrote:
> >>>>
> >>>>> Hello,
> >>>>>
> >>>>> Indeed the "proper" way of including a script would be to store it in
> >>>>> a
> >>>>> file system.
> >>>>>
> >>>>> However, when I needed to include a single and small script and I
> >>> didn't
> >>>>> want to introduce a complete FS just for this, I used xxd.
> >>>>> xxd can convert any file to a C header file.
> >>>>>
> >>>>> You can then include the header, and access the whole file as a
> >>> variable.
> >>>>> Here is an example:
> >>>>>
> >>>>> I added this in my app Makefile:
> >>>>>
> >>>>> # Setup any special pre-build context
> >>>>> context: header
> >>>>>          $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> >>>>> json_lua.h
> >>> &&
> >>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> >>>>>
> >>>>>
> >>>>>
> >>>>> And then I used the file like this:
> >>>>>
> >>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
> >>>>>   #include "json_lua.h"
> >>>>>   static int luaopen_json(lua_State * L);
> >>>>>
> >>>>>   void ExtLibs_load(lua_State * L){
> >>>>>          // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> >>>>>          luaL_requiref(L, "json", luaopen_json, 1);
> >>>>>          lua_pop(L, 1);#endif}
> >>>>>
> >>>>>   int luaopen_json(lua_State * L){
> >>>>>          const char * modname = lua_tostring(L, 1);
> >>>>>
> >>>>>          if (strcmp(modname, "json") != 0)
> >>>>>                  return luaL_error(L, "cannot load json module");
> >>>>>
> >>>>>          if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> >>>>> "json",
> >>>>> "t") != LUA_OK)
> >>>>>                  return lua_error(L);
> >>>>>
> >>>>>          lua_call(L, 0, 1);
> >>>>>
> >>>>>          return 1;}
> >>>>>
> >>>>>
> >>>>> I hope this helps...
> >>>>>
> >>>> That is very helpful, and not just for nuttx development! Thanks for
> >>>> the
> >>>> tip.
> >>>>
> >>>> The romfs example actually uses xxd as well to convert the filesystem
> >>> into
> >>>> hex code that is also stored in a header file. If I am reading the
> code
> >>>> correctly, the example app loads the entire filesystem into memory,
> >>>> which
> >>>> isn't very efficient and not at all what I wanted. Can someone tell me
> >>>> if
> >>>> that's true?
> >>>>
> >>>> Thanks,
> >>>> Russ
> >>>>
> >>>>>
> >>>>>
> >>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <
> xiaoxiang781216@gmail.com>
> >>>>> wrote:
> >>>>>
> >>>>>> You can use the real file system on the device, there are many
> >>> choices:
> >>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
> >>>>>>
> >>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <
> russ.haley@gmail.com
> >>>>>> wrote:
> >>>>>>
> >>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> >>>> xiaoxiang781216@gmail.com>
> >>>>>>> wrote:
> >>>>>>>
> >>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> >>> scripts
> >>>>>> into
> >>>>>>>> some PC folder and run mount this folder from nsh:
> >>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> >>>>>>>>
> >>>>>>>> While I appreciate the answer, I am using the sim as a testing
> >>>>> platform
> >>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
> >>>>>> hoping
> >>>>>>> for a solution that is applicable to an embedded project. If I
> >>> can't
> >>>>> just
> >>>>>>> add files to the initial image then I will look at the romfs
> >>> example
> >>>>> and
> >>>>>>> maybe the next best thing?
> >>>>>>>
> >>>>>>>
> >>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> >>>> russ.haley@gmail.com>
> >>>>>>>> wrote:
> >>>>>>>>
> >>>>>>>>> Hi,
> >>>>>>>>>
> >>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
> >>>>>> configuration.
> >>>>>>> I
> >>>>>>>>> was unable to simply include the defconfig file that you linked
> >>>> to,
> >>>>>>> but I
> >>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
> >>> now
> >>>>> got
> >>>>>> an
> >>>>>>>> app
> >>>>>>>>> in the examples folder that includes the Lua interpreter. Is
> >>>> there
> >>>>> a
> >>>>>>>>> tutorial on how to include folders and lua scripts or extra
> >>> files
> >>>>> in
> >>>>>>> the
> >>>>>>>>> initial file system?
> >>>>>>>>>
> >>>>>>>>> Much appreciated,
> >>>>>>>>> Russ
> >>>>>>>>>
> >
>

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
Then, they should run the mount or whatever they want from rcS like how
Unix did. Actually, don't invert the wheel again, just follow how Unix is
done.

On Fri, Feb 3, 2023 at 1:27 AM Alan C. Assis <ac...@gmail.com> wrote:

> I think if it was generic but with some CONFIG_ROMFS_MNTPOINT it could
> be usable for all customer environment and preferences.
>
> Well, there still some cases that could be covered: ie. a user app
> that needs files mounted at two or more places inside the root file
> system.
>
> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> > IMHO this is too dependent on customer environment and preferences.
> >
> > But it would be nice to be an official out-of-tree board skeleton to
> > show people how to organize it. libopencm3 has a demo project that is
> > very helpful.
> >
> > Because it's always the first step when you use nuttx on real hardware
> > that is not an unmodified commercial devboard.
> >
> > Sebastien
> >
> > Le 02/02/2023 à 15:35, Alan C. Assis a écrit :
> >> It should be nice to have a simple logic to let users to use ROMFS.
> >>
> >> Currently everyone needs to duplicate it in their own code/application.
> >>
> >> I'm thinking something like apps/romfiles/ where people just put the
> >> files that they want to be in the ROM and it will be included
> >> automatically, instead reinventing the wheel.
> >>
> >> BR,
> >>
> >> Alan
> >>
> >> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> >>> Hi,
> >>>
> >>> Dont use boardctl for the romfs, this is too intertwined in the
> >>> mechanism used to initialize nsh, which is not suitable for
> >>> customization.
> >>>
> >>> This need to be decoupled, I've done this in my own apps. I'll send it
> >>> later, maybe.
> >>>
> >>>
> >>> Here is the proper way to mount a romfs from your board bringup()
> >>> routines:
> >>>
> >>> I am using stm32 as example, the ROM fs directory holding the file is a
> >>> brother to board/
> >>>
> >>> boarddir:
> >>> +-- romfs
> >>>        +-- your files...
> >>> +-- board
> >>>        +-- include
> >>>        +-- src
> >>>
> >>> You can do something else if you change the makefile below
> >>>
> >>> First change your board/src makefile to add a dependency so that
> >>> stm32_bringup.c depends on romfs.h
> >>>
> >>> .PHONY: myromfs.h #might not be required
> >>> stm32_bringup.c: myromfs.h
> >>> myromfs.h:
> >>>           @echo "ROMFS"
> >>>           @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
> >>>           @xxd -i my.romfs > myromfs.h
> >>>
> >>> Then do this in stm32_bringup.c:
> >>>
> >>> #include "myromfs.h"
> >>> #define SECTORSIZE  64
> >>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
> >>> #include <nuttx/fs/fs.h>
> >>> #include <nuttx/drivers/ramdisk.h>
> >>> #include <sys/mount.h>
> >>>
> >>> and then in stm32_bringup(void):
> >>>
> >>>    ret = romdisk_register(0, // /dev/ram0
> >>>                            myromfs, //var in the xxd header
> >>>                            NSECTORS(myromfs_len),
> >>>                            SECTORSIZE);
> >>>
> >>>     ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
> >>>
> >>> Then you will have your files mounted in /romfs at boot.
> >>>
> >>> You can do symlinks:
> >>>
> >>>    ret = symlink("/romfs/etc" , "/etc");
> >>>     _info("Linking /etc: %d\n", ret);
> >>>
> >>> Sebastien
> >>>
> >>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
> >>>> I am mistaken. I understood that the rom image was part of the
> >>>> application
> >>>> on flash, but had mis-read the documentation of
> >>>> boardctl(BOARDIOC_ROMDISK,
> >>>> (uintptr_t)&desc); and thought that the command loaded the image into
> >>>> *RAM*
> >>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
> >>>>
> >>>> Incidentally, neither the romfs example nor my attempt to re-create it
> >>>> works in my sim:
> >>>>
> >>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
> >>>>
> >>>> NuttShell (NSH) NuttX-12.0.0
> >>>> nsh> romfs
> >>>> ERROR: Failed to create RAM disk: Unknown error
> >>>> nsh> rapp
> >>>> Starting Russells App 1...
> >>>> ERROR: Failed to create RAM disk: Unknown error
> >>>> nsh>
> >>>>
> >>>> I don't know how to debug this "unknown error". I guess I will try to
> >>>> find
> >>>> where that error message comes from and hook in GDB? I'm terrible at
> >>>> this
> >>>> stuff, someone needs to revoke my compiler license (tee hee).
> >>>>
> >>>> Cheers,
> >>>> Russ
> >>>>
> >>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
> >>>> wrote:
> >>>>
> >>>>> romfs is part of your image as the const string. There is no
> >>>>> difference
> >>>>> from the below manual step.
> >>>>>
> >>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> >>>>> wrote:
> >>>>>
> >>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> >>>>> f.j.panag@gmail.com
> >>>>>> wrote:
> >>>>>>
> >>>>>>> Hello,
> >>>>>>>
> >>>>>>> Indeed the "proper" way of including a script would be to store it
> >>>>>>> in
> >>>>>>> a
> >>>>>>> file system.
> >>>>>>>
> >>>>>>> However, when I needed to include a single and small script and I
> >>>>> didn't
> >>>>>>> want to introduce a complete FS just for this, I used xxd.
> >>>>>>> xxd can convert any file to a C header file.
> >>>>>>>
> >>>>>>> You can then include the header, and access the whole file as a
> >>>>> variable.
> >>>>>>> Here is an example:
> >>>>>>>
> >>>>>>> I added this in my app Makefile:
> >>>>>>>
> >>>>>>> # Setup any special pre-build context
> >>>>>>> context: header
> >>>>>>>           $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
> >>>>>>> json_lua.h
> >>>>> &&
> >>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>> And then I used the file like this:
> >>>>>>>
> >>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
> >>>>>>>    #include "json_lua.h"
> >>>>>>>    static int luaopen_json(lua_State * L);
> >>>>>>>
> >>>>>>>    void ExtLibs_load(lua_State * L){
> >>>>>>>           // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> >>>>>>>           luaL_requiref(L, "json", luaopen_json, 1);
> >>>>>>>           lua_pop(L, 1);#endif}
> >>>>>>>
> >>>>>>>    int luaopen_json(lua_State * L){
> >>>>>>>           const char * modname = lua_tostring(L, 1);
> >>>>>>>
> >>>>>>>           if (strcmp(modname, "json") != 0)
> >>>>>>>                   return luaL_error(L, "cannot load json module");
> >>>>>>>
> >>>>>>>           if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
> >>>>>>> "json",
> >>>>>>> "t") != LUA_OK)
> >>>>>>>                   return lua_error(L);
> >>>>>>>
> >>>>>>>           lua_call(L, 0, 1);
> >>>>>>>
> >>>>>>>           return 1;}
> >>>>>>>
> >>>>>>>
> >>>>>>> I hope this helps...
> >>>>>>>
> >>>>>> That is very helpful, and not just for nuttx development! Thanks for
> >>>>>> the
> >>>>>> tip.
> >>>>>>
> >>>>>> The romfs example actually uses xxd as well to convert the
> filesystem
> >>>>> into
> >>>>>> hex code that is also stored in a header file. If I am reading the
> >>>>>> code
> >>>>>> correctly, the example app loads the entire filesystem into memory,
> >>>>>> which
> >>>>>> isn't very efficient and not at all what I wanted. Can someone tell
> >>>>>> me
> >>>>>> if
> >>>>>> that's true?
> >>>>>>
> >>>>>> Thanks,
> >>>>>> Russ
> >>>>>>
> >>>>>>>
> >>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao
> >>>>>>> <xi...@gmail.com>
> >>>>>>> wrote:
> >>>>>>>
> >>>>>>>> You can use the real file system on the device, there are many
> >>>>> choices:
> >>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
> >>>>>>>>
> >>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley
> >>>>>>>> <russ.haley@gmail.com
> >>>>>>>> wrote:
> >>>>>>>>
> >>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> >>>>>> xiaoxiang781216@gmail.com>
> >>>>>>>>> wrote:
> >>>>>>>>>
> >>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> >>>>> scripts
> >>>>>>>> into
> >>>>>>>>>> some PC folder and run mount this folder from nsh:
> >>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> >>>>>>>>>>
> >>>>>>>>>> While I appreciate the answer, I am using the sim as a testing
> >>>>>>> platform
> >>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I
> >>>>>>>>> am
> >>>>>>>> hoping
> >>>>>>>>> for a solution that is applicable to an embedded project. If I
> >>>>> can't
> >>>>>>> just
> >>>>>>>>> add files to the initial image then I will look at the romfs
> >>>>> example
> >>>>>>> and
> >>>>>>>>> maybe the next best thing?
> >>>>>>>>>
> >>>>>>>>>
> >>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> >>>>>> russ.haley@gmail.com>
> >>>>>>>>>> wrote:
> >>>>>>>>>>
> >>>>>>>>>>> Hi,
> >>>>>>>>>>>
> >>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
> >>>>>>>> configuration.
> >>>>>>>>> I
> >>>>>>>>>>> was unable to simply include the defconfig file that you linked
> >>>>>> to,
> >>>>>>>>> but I
> >>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
> >>>>> now
> >>>>>>> got
> >>>>>>>> an
> >>>>>>>>>> app
> >>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
> >>>>>> there
> >>>>>>> a
> >>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
> >>>>> files
> >>>>>>> in
> >>>>>>>>> the
> >>>>>>>>>>> initial file system?
> >>>>>>>>>>>
> >>>>>>>>>>> Much appreciated,
> >>>>>>>>>>> Russ
> >>>>>>>>>>>
> >
>

Re: Including Lua scripts on filesystem

Posted by "Alan C. Assis" <ac...@gmail.com>.
I think if it was generic but with some CONFIG_ROMFS_MNTPOINT it could
be usable for all customer environment and preferences.

Well, there still some cases that could be covered: ie. a user app
that needs files mounted at two or more places inside the root file
system.

On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> IMHO this is too dependent on customer environment and preferences.
>
> But it would be nice to be an official out-of-tree board skeleton to
> show people how to organize it. libopencm3 has a demo project that is
> very helpful.
>
> Because it's always the first step when you use nuttx on real hardware
> that is not an unmodified commercial devboard.
>
> Sebastien
>
> Le 02/02/2023 à 15:35, Alan C. Assis a écrit :
>> It should be nice to have a simple logic to let users to use ROMFS.
>>
>> Currently everyone needs to duplicate it in their own code/application.
>>
>> I'm thinking something like apps/romfiles/ where people just put the
>> files that they want to be in the ROM and it will be included
>> automatically, instead reinventing the wheel.
>>
>> BR,
>>
>> Alan
>>
>> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
>>> Hi,
>>>
>>> Dont use boardctl for the romfs, this is too intertwined in the
>>> mechanism used to initialize nsh, which is not suitable for
>>> customization.
>>>
>>> This need to be decoupled, I've done this in my own apps. I'll send it
>>> later, maybe.
>>>
>>>
>>> Here is the proper way to mount a romfs from your board bringup()
>>> routines:
>>>
>>> I am using stm32 as example, the ROM fs directory holding the file is a
>>> brother to board/
>>>
>>> boarddir:
>>> +-- romfs
>>>        +-- your files...
>>> +-- board
>>>        +-- include
>>>        +-- src
>>>
>>> You can do something else if you change the makefile below
>>>
>>> First change your board/src makefile to add a dependency so that
>>> stm32_bringup.c depends on romfs.h
>>>
>>> .PHONY: myromfs.h #might not be required
>>> stm32_bringup.c: myromfs.h
>>> myromfs.h:
>>>           @echo "ROMFS"
>>>           @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
>>>           @xxd -i my.romfs > myromfs.h
>>>
>>> Then do this in stm32_bringup.c:
>>>
>>> #include "myromfs.h"
>>> #define SECTORSIZE  64
>>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
>>> #include <nuttx/fs/fs.h>
>>> #include <nuttx/drivers/ramdisk.h>
>>> #include <sys/mount.h>
>>>
>>> and then in stm32_bringup(void):
>>>
>>>    ret = romdisk_register(0, // /dev/ram0
>>>                            myromfs, //var in the xxd header
>>>                            NSECTORS(myromfs_len),
>>>                            SECTORSIZE);
>>>
>>>     ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
>>>
>>> Then you will have your files mounted in /romfs at boot.
>>>
>>> You can do symlinks:
>>>
>>>    ret = symlink("/romfs/etc" , "/etc");
>>>     _info("Linking /etc: %d\n", ret);
>>>
>>> Sebastien
>>>
>>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
>>>> I am mistaken. I understood that the rom image was part of the
>>>> application
>>>> on flash, but had mis-read the documentation of
>>>> boardctl(BOARDIOC_ROMDISK,
>>>> (uintptr_t)&desc); and thought that the command loaded the image into
>>>> *RAM*
>>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>>>>
>>>> Incidentally, neither the romfs example nor my attempt to re-create it
>>>> works in my sim:
>>>>
>>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>>>>
>>>> NuttShell (NSH) NuttX-12.0.0
>>>> nsh> romfs
>>>> ERROR: Failed to create RAM disk: Unknown error
>>>> nsh> rapp
>>>> Starting Russells App 1...
>>>> ERROR: Failed to create RAM disk: Unknown error
>>>> nsh>
>>>>
>>>> I don't know how to debug this "unknown error". I guess I will try to
>>>> find
>>>> where that error message comes from and hook in GDB? I'm terrible at
>>>> this
>>>> stuff, someone needs to revoke my compiler license (tee hee).
>>>>
>>>> Cheers,
>>>> Russ
>>>>
>>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
>>>> wrote:
>>>>
>>>>> romfs is part of your image as the const string. There is no
>>>>> difference
>>>>> from the below manual step.
>>>>>
>>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>>>>> f.j.panag@gmail.com
>>>>>> wrote:
>>>>>>
>>>>>>> Hello,
>>>>>>>
>>>>>>> Indeed the "proper" way of including a script would be to store it
>>>>>>> in
>>>>>>> a
>>>>>>> file system.
>>>>>>>
>>>>>>> However, when I needed to include a single and small script and I
>>>>> didn't
>>>>>>> want to introduce a complete FS just for this, I used xxd.
>>>>>>> xxd can convert any file to a C header file.
>>>>>>>
>>>>>>> You can then include the header, and access the whole file as a
>>>>> variable.
>>>>>>> Here is an example:
>>>>>>>
>>>>>>> I added this in my app Makefile:
>>>>>>>
>>>>>>> # Setup any special pre-build context
>>>>>>> context: header
>>>>>>>           $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>>>>>>> json_lua.h
>>>>> &&
>>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> And then I used the file like this:
>>>>>>>
>>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>>>>    #include "json_lua.h"
>>>>>>>    static int luaopen_json(lua_State * L);
>>>>>>>
>>>>>>>    void ExtLibs_load(lua_State * L){
>>>>>>>           // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>>>>           luaL_requiref(L, "json", luaopen_json, 1);
>>>>>>>           lua_pop(L, 1);#endif}
>>>>>>>
>>>>>>>    int luaopen_json(lua_State * L){
>>>>>>>           const char * modname = lua_tostring(L, 1);
>>>>>>>
>>>>>>>           if (strcmp(modname, "json") != 0)
>>>>>>>                   return luaL_error(L, "cannot load json module");
>>>>>>>
>>>>>>>           if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>>>>>>> "json",
>>>>>>> "t") != LUA_OK)
>>>>>>>                   return lua_error(L);
>>>>>>>
>>>>>>>           lua_call(L, 0, 1);
>>>>>>>
>>>>>>>           return 1;}
>>>>>>>
>>>>>>>
>>>>>>> I hope this helps...
>>>>>>>
>>>>>> That is very helpful, and not just for nuttx development! Thanks for
>>>>>> the
>>>>>> tip.
>>>>>>
>>>>>> The romfs example actually uses xxd as well to convert the filesystem
>>>>> into
>>>>>> hex code that is also stored in a header file. If I am reading the
>>>>>> code
>>>>>> correctly, the example app loads the entire filesystem into memory,
>>>>>> which
>>>>>> isn't very efficient and not at all what I wanted. Can someone tell
>>>>>> me
>>>>>> if
>>>>>> that's true?
>>>>>>
>>>>>> Thanks,
>>>>>> Russ
>>>>>>
>>>>>>>
>>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao
>>>>>>> <xi...@gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> You can use the real file system on the device, there are many
>>>>> choices:
>>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>>>>
>>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley
>>>>>>>> <russ.haley@gmail.com
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>>>>> xiaoxiang781216@gmail.com>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>>>>> scripts
>>>>>>>> into
>>>>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>>>>
>>>>>>>>>> While I appreciate the answer, I am using the sim as a testing
>>>>>>> platform
>>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I
>>>>>>>>> am
>>>>>>>> hoping
>>>>>>>>> for a solution that is applicable to an embedded project. If I
>>>>> can't
>>>>>>> just
>>>>>>>>> add files to the initial image then I will look at the romfs
>>>>> example
>>>>>>> and
>>>>>>>>> maybe the next best thing?
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>>>>> russ.haley@gmail.com>
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>> Hi,
>>>>>>>>>>>
>>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>>>>> configuration.
>>>>>>>>> I
>>>>>>>>>>> was unable to simply include the defconfig file that you linked
>>>>>> to,
>>>>>>>>> but I
>>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>>>>> now
>>>>>>> got
>>>>>>>> an
>>>>>>>>>> app
>>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>>>>> there
>>>>>>> a
>>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
>>>>> files
>>>>>>> in
>>>>>>>>> the
>>>>>>>>>>> initial file system?
>>>>>>>>>>>
>>>>>>>>>>> Much appreciated,
>>>>>>>>>>> Russ
>>>>>>>>>>>
>

Re: Including Lua scripts on filesystem

Posted by Sebastien Lorquet <se...@lorquet.fr>.
IMHO this is too dependent on customer environment and preferences.

But it would be nice to be an official out-of-tree board skeleton to 
show people how to organize it. libopencm3 has a demo project that is 
very helpful.

Because it's always the first step when you use nuttx on real hardware 
that is not an unmodified commercial devboard.

Sebastien

Le 02/02/2023 à 15:35, Alan C. Assis a écrit :
> It should be nice to have a simple logic to let users to use ROMFS.
>
> Currently everyone needs to duplicate it in their own code/application.
>
> I'm thinking something like apps/romfiles/ where people just put the
> files that they want to be in the ROM and it will be included
> automatically, instead reinventing the wheel.
>
> BR,
>
> Alan
>
> On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
>> Hi,
>>
>> Dont use boardctl for the romfs, this is too intertwined in the
>> mechanism used to initialize nsh, which is not suitable for customization.
>>
>> This need to be decoupled, I've done this in my own apps. I'll send it
>> later, maybe.
>>
>>
>> Here is the proper way to mount a romfs from your board bringup() routines:
>>
>> I am using stm32 as example, the ROM fs directory holding the file is a
>> brother to board/
>>
>> boarddir:
>> +-- romfs
>>        +-- your files...
>> +-- board
>>        +-- include
>>        +-- src
>>
>> You can do something else if you change the makefile below
>>
>> First change your board/src makefile to add a dependency so that
>> stm32_bringup.c depends on romfs.h
>>
>> .PHONY: myromfs.h #might not be required
>> stm32_bringup.c: myromfs.h
>> myromfs.h:
>>           @echo "ROMFS"
>>           @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
>>           @xxd -i my.romfs > myromfs.h
>>
>> Then do this in stm32_bringup.c:
>>
>> #include "myromfs.h"
>> #define SECTORSIZE  64
>> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
>> #include <nuttx/fs/fs.h>
>> #include <nuttx/drivers/ramdisk.h>
>> #include <sys/mount.h>
>>
>> and then in stm32_bringup(void):
>>
>>    ret = romdisk_register(0, // /dev/ram0
>>                            myromfs, //var in the xxd header
>>                            NSECTORS(myromfs_len),
>>                            SECTORSIZE);
>>
>>     ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
>>
>> Then you will have your files mounted in /romfs at boot.
>>
>> You can do symlinks:
>>
>>    ret = symlink("/romfs/etc" , "/etc");
>>     _info("Linking /etc: %d\n", ret);
>>
>> Sebastien
>>
>> Le 02/02/2023 à 05:59, Russell Haley a écrit :
>>> I am mistaken. I understood that the rom image was part of the
>>> application
>>> on flash, but had mis-read the documentation of
>>> boardctl(BOARDIOC_ROMDISK,
>>> (uintptr_t)&desc); and thought that the command loaded the image into
>>> *RAM*
>>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>>>
>>> Incidentally, neither the romfs example nor my attempt to re-create it
>>> works in my sim:
>>>
>>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>>>
>>> NuttShell (NSH) NuttX-12.0.0
>>> nsh> romfs
>>> ERROR: Failed to create RAM disk: Unknown error
>>> nsh> rapp
>>> Starting Russells App 1...
>>> ERROR: Failed to create RAM disk: Unknown error
>>> nsh>
>>>
>>> I don't know how to debug this "unknown error". I guess I will try to
>>> find
>>> where that error message comes from and hook in GDB? I'm terrible at this
>>> stuff, someone needs to revoke my compiler license (tee hee).
>>>
>>> Cheers,
>>> Russ
>>>
>>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
>>> wrote:
>>>
>>>> romfs is part of your image as the const string. There is no difference
>>>> from the below manual step.
>>>>
>>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
>>>> wrote:
>>>>
>>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>>>> f.j.panag@gmail.com
>>>>> wrote:
>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> Indeed the "proper" way of including a script would be to store it in
>>>>>> a
>>>>>> file system.
>>>>>>
>>>>>> However, when I needed to include a single and small script and I
>>>> didn't
>>>>>> want to introduce a complete FS just for this, I used xxd.
>>>>>> xxd can convert any file to a C header file.
>>>>>>
>>>>>> You can then include the header, and access the whole file as a
>>>> variable.
>>>>>> Here is an example:
>>>>>>
>>>>>> I added this in my app Makefile:
>>>>>>
>>>>>> # Setup any special pre-build context
>>>>>> context: header
>>>>>>           $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>>>>>> json_lua.h
>>>> &&
>>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>>>
>>>>>>
>>>>>>
>>>>>> And then I used the file like this:
>>>>>>
>>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>>>    #include "json_lua.h"
>>>>>>    static int luaopen_json(lua_State * L);
>>>>>>
>>>>>>    void ExtLibs_load(lua_State * L){
>>>>>>           // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>>>           luaL_requiref(L, "json", luaopen_json, 1);
>>>>>>           lua_pop(L, 1);#endif}
>>>>>>
>>>>>>    int luaopen_json(lua_State * L){
>>>>>>           const char * modname = lua_tostring(L, 1);
>>>>>>
>>>>>>           if (strcmp(modname, "json") != 0)
>>>>>>                   return luaL_error(L, "cannot load json module");
>>>>>>
>>>>>>           if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>>>>>> "json",
>>>>>> "t") != LUA_OK)
>>>>>>                   return lua_error(L);
>>>>>>
>>>>>>           lua_call(L, 0, 1);
>>>>>>
>>>>>>           return 1;}
>>>>>>
>>>>>>
>>>>>> I hope this helps...
>>>>>>
>>>>> That is very helpful, and not just for nuttx development! Thanks for
>>>>> the
>>>>> tip.
>>>>>
>>>>> The romfs example actually uses xxd as well to convert the filesystem
>>>> into
>>>>> hex code that is also stored in a header file. If I am reading the code
>>>>> correctly, the example app loads the entire filesystem into memory,
>>>>> which
>>>>> isn't very efficient and not at all what I wanted. Can someone tell me
>>>>> if
>>>>> that's true?
>>>>>
>>>>> Thanks,
>>>>> Russ
>>>>>
>>>>>>
>>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <xi...@gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> You can use the real file system on the device, there are many
>>>> choices:
>>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>>>
>>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <russ.haley@gmail.com
>>>>>>> wrote:
>>>>>>>
>>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>>>> xiaoxiang781216@gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>>>> scripts
>>>>>>> into
>>>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>>>
>>>>>>>>> While I appreciate the answer, I am using the sim as a testing
>>>>>> platform
>>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
>>>>>>> hoping
>>>>>>>> for a solution that is applicable to an embedded project. If I
>>>> can't
>>>>>> just
>>>>>>>> add files to the initial image then I will look at the romfs
>>>> example
>>>>>> and
>>>>>>>> maybe the next best thing?
>>>>>>>>
>>>>>>>>
>>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>>>> russ.haley@gmail.com>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>>>> configuration.
>>>>>>>> I
>>>>>>>>>> was unable to simply include the defconfig file that you linked
>>>>> to,
>>>>>>>> but I
>>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>>>> now
>>>>>> got
>>>>>>> an
>>>>>>>>> app
>>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>>>> there
>>>>>> a
>>>>>>>>>> tutorial on how to include folders and lua scripts or extra
>>>> files
>>>>>> in
>>>>>>>> the
>>>>>>>>>> initial file system?
>>>>>>>>>>
>>>>>>>>>> Much appreciated,
>>>>>>>>>> Russ
>>>>>>>>>>

Re: Including Lua scripts on filesystem

Posted by "Alan C. Assis" <ac...@gmail.com>.
It should be nice to have a simple logic to let users to use ROMFS.

Currently everyone needs to duplicate it in their own code/application.

I'm thinking something like apps/romfiles/ where people just put the
files that they want to be in the ROM and it will be included
automatically, instead reinventing the wheel.

BR,

Alan

On 2/2/23, Sebastien Lorquet <se...@lorquet.fr> wrote:
> Hi,
>
> Dont use boardctl for the romfs, this is too intertwined in the
> mechanism used to initialize nsh, which is not suitable for customization.
>
> This need to be decoupled, I've done this in my own apps. I'll send it
> later, maybe.
>
>
> Here is the proper way to mount a romfs from your board bringup() routines:
>
> I am using stm32 as example, the ROM fs directory holding the file is a
> brother to board/
>
> boarddir:
> +-- romfs
>       +-- your files...
> +-- board
>       +-- include
>       +-- src
>
> You can do something else if you change the makefile below
>
> First change your board/src makefile to add a dependency so that
> stm32_bringup.c depends on romfs.h
>
> .PHONY: myromfs.h #might not be required
> stm32_bringup.c: myromfs.h
> myromfs.h:
>          @echo "ROMFS"
>          @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
>          @xxd -i my.romfs > myromfs.h
>
> Then do this in stm32_bringup.c:
>
> #include "myromfs.h"
> #define SECTORSIZE  64
> #define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
> #include <nuttx/fs/fs.h>
> #include <nuttx/drivers/ramdisk.h>
> #include <sys/mount.h>
>
> and then in stm32_bringup(void):
>
>   ret = romdisk_register(0, // /dev/ram0
>                           myromfs, //var in the xxd header
>                           NSECTORS(myromfs_len),
>                           SECTORSIZE);
>
>    ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);
>
> Then you will have your files mounted in /romfs at boot.
>
> You can do symlinks:
>
>   ret = symlink("/romfs/etc" , "/etc");
>    _info("Linking /etc: %d\n", ret);
>
> Sebastien
>
> Le 02/02/2023 à 05:59, Russell Haley a écrit :
>> I am mistaken. I understood that the rom image was part of the
>> application
>> on flash, but had mis-read the documentation of
>> boardctl(BOARDIOC_ROMDISK,
>> (uintptr_t)&desc); and thought that the command loaded the image into
>> *RAM*
>> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>>
>> Incidentally, neither the romfs example nor my attempt to re-create it
>> works in my sim:
>>
>> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>>
>> NuttShell (NSH) NuttX-12.0.0
>> nsh> romfs
>> ERROR: Failed to create RAM disk: Unknown error
>> nsh> rapp
>> Starting Russells App 1...
>> ERROR: Failed to create RAM disk: Unknown error
>> nsh>
>>
>> I don't know how to debug this "unknown error". I guess I will try to
>> find
>> where that error message comes from and hook in GDB? I'm terrible at this
>> stuff, someone needs to revoke my compiler license (tee hee).
>>
>> Cheers,
>> Russ
>>
>> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com>
>> wrote:
>>
>>> romfs is part of your image as the const string. There is no difference
>>> from the below manual step.
>>>
>>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
>>> wrote:
>>>
>>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>>> f.j.panag@gmail.com
>>>> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> Indeed the "proper" way of including a script would be to store it in
>>>>> a
>>>>> file system.
>>>>>
>>>>> However, when I needed to include a single and small script and I
>>> didn't
>>>>> want to introduce a complete FS just for this, I used xxd.
>>>>> xxd can convert any file to a C header file.
>>>>>
>>>>> You can then include the header, and access the whole file as a
>>> variable.
>>>>> Here is an example:
>>>>>
>>>>> I added this in my app Makefile:
>>>>>
>>>>> # Setup any special pre-build context
>>>>> context: header
>>>>>          $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua >
>>>>> json_lua.h
>>> &&
>>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>>
>>>>>
>>>>>
>>>>> And then I used the file like this:
>>>>>
>>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>>   #include "json_lua.h"
>>>>>   static int luaopen_json(lua_State * L);
>>>>>
>>>>>   void ExtLibs_load(lua_State * L){
>>>>>          // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>>          luaL_requiref(L, "json", luaopen_json, 1);
>>>>>          lua_pop(L, 1);#endif}
>>>>>
>>>>>   int luaopen_json(lua_State * L){
>>>>>          const char * modname = lua_tostring(L, 1);
>>>>>
>>>>>          if (strcmp(modname, "json") != 0)
>>>>>                  return luaL_error(L, "cannot load json module");
>>>>>
>>>>>          if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len,
>>>>> "json",
>>>>> "t") != LUA_OK)
>>>>>                  return lua_error(L);
>>>>>
>>>>>          lua_call(L, 0, 1);
>>>>>
>>>>>          return 1;}
>>>>>
>>>>>
>>>>> I hope this helps...
>>>>>
>>>> That is very helpful, and not just for nuttx development! Thanks for
>>>> the
>>>> tip.
>>>>
>>>> The romfs example actually uses xxd as well to convert the filesystem
>>> into
>>>> hex code that is also stored in a header file. If I am reading the code
>>>> correctly, the example app loads the entire filesystem into memory,
>>>> which
>>>> isn't very efficient and not at all what I wanted. Can someone tell me
>>>> if
>>>> that's true?
>>>>
>>>> Thanks,
>>>> Russ
>>>>
>>>>>
>>>>>
>>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <xi...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> You can use the real file system on the device, there are many
>>> choices:
>>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>>
>>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <russ.haley@gmail.com
>>>>>> wrote:
>>>>>>
>>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>>> xiaoxiang781216@gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>>> scripts
>>>>>> into
>>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>>
>>>>>>>> While I appreciate the answer, I am using the sim as a testing
>>>>> platform
>>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
>>>>>> hoping
>>>>>>> for a solution that is applicable to an embedded project. If I
>>> can't
>>>>> just
>>>>>>> add files to the initial image then I will look at the romfs
>>> example
>>>>> and
>>>>>>> maybe the next best thing?
>>>>>>>
>>>>>>>
>>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>>> russ.haley@gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>>> configuration.
>>>>>>> I
>>>>>>>>> was unable to simply include the defconfig file that you linked
>>>> to,
>>>>>>> but I
>>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>>> now
>>>>> got
>>>>>> an
>>>>>>>> app
>>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>>> there
>>>>> a
>>>>>>>>> tutorial on how to include folders and lua scripts or extra
>>> files
>>>>> in
>>>>>>> the
>>>>>>>>> initial file system?
>>>>>>>>>
>>>>>>>>> Much appreciated,
>>>>>>>>> Russ
>>>>>>>>>
>

Re: Including Lua scripts on filesystem

Posted by Sebastien Lorquet <se...@lorquet.fr>.
Hi,

Dont use boardctl for the romfs, this is too intertwined in the 
mechanism used to initialize nsh, which is not suitable for customization.

This need to be decoupled, I've done this in my own apps. I'll send it 
later, maybe.


Here is the proper way to mount a romfs from your board bringup() routines:

I am using stm32 as example, the ROM fs directory holding the file is a 
brother to board/

boarddir:
+-- romfs
      +-- your files...
+-- board
      +-- include
      +-- src

You can do something else if you change the makefile below

First change your board/src makefile to add a dependency so that 
stm32_bringup.c depends on romfs.h

.PHONY: myromfs.h #might not be required
stm32_bringup.c: myromfs.h
myromfs.h:
         @echo "ROMFS"
         @genromfs -v -f my.romfs -d $(BOARD_DIR)/../romfs
         @xxd -i my.romfs > myromfs.h

Then do this in stm32_bringup.c:

#include "myromfs.h"
#define SECTORSIZE  64
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
#include <nuttx/fs/fs.h>
#include <nuttx/drivers/ramdisk.h>
#include <sys/mount.h>

and then in stm32_bringup(void):

  ret = romdisk_register(0, // /dev/ram0
                          myromfs, //var in the xxd header
                          NSECTORS(myromfs_len),
                          SECTORSIZE);

   ret = nx_mount("/dev/ram0", "/romfs", "romfs", MS_RDONLY, NULL);

Then you will have your files mounted in /romfs at boot.

You can do symlinks:

  ret = symlink("/romfs/etc" , "/etc");
   _info("Linking /etc: %d\n", ret);

Sebastien

Le 02/02/2023 à 05:59, Russell Haley a écrit :
> I am mistaken. I understood that the rom image was part of the application
> on flash, but had mis-read the documentation of boardctl(BOARDIOC_ROMDISK,
> (uintptr_t)&desc); and thought that the command loaded the image into *RAM*
> (the  BOARDIOC_MKRD command makes the RAM disk. oops!).
>
> Incidentally, neither the romfs example nor my attempt to re-create it
> works in my sim:
>
> osboxes@osboxes ~/n/nuttx (master)> ./nuttx
>
> NuttShell (NSH) NuttX-12.0.0
> nsh> romfs
> ERROR: Failed to create RAM disk: Unknown error
> nsh> rapp
> Starting Russells App 1...
> ERROR: Failed to create RAM disk: Unknown error
> nsh>
>
> I don't know how to debug this "unknown error". I guess I will try to find
> where that error message comes from and hook in GDB? I'm terrible at this
> stuff, someone needs to revoke my compiler license (tee hee).
>
> Cheers,
> Russ
>
> On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com> wrote:
>
>> romfs is part of your image as the const string. There is no difference
>> from the below manual step.
>>
>> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
>> wrote:
>>
>>> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
>> f.j.panag@gmail.com
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> Indeed the "proper" way of including a script would be to store it in a
>>>> file system.
>>>>
>>>> However, when I needed to include a single and small script and I
>> didn't
>>>> want to introduce a complete FS just for this, I used xxd.
>>>> xxd can convert any file to a C header file.
>>>>
>>>> You can then include the header, and access the whole file as a
>> variable.
>>>> Here is an example:
>>>>
>>>> I added this in my app Makefile:
>>>>
>>>> # Setup any special pre-build context
>>>> context: header
>>>>          $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua > json_lua.h
>> &&
>>>> echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
>>>>
>>>>
>>>>
>>>> And then I used the file like this:
>>>>
>>>> #include "lua.h"#include "lauxlib.h"#include <string.h>
>>>>   #include "json_lua.h"
>>>>   static int luaopen_json(lua_State * L);
>>>>
>>>>   void ExtLibs_load(lua_State * L){
>>>>          // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
>>>>          luaL_requiref(L, "json", luaopen_json, 1);
>>>>          lua_pop(L, 1);#endif}
>>>>
>>>>   int luaopen_json(lua_State * L){
>>>>          const char * modname = lua_tostring(L, 1);
>>>>
>>>>          if (strcmp(modname, "json") != 0)
>>>>                  return luaL_error(L, "cannot load json module");
>>>>
>>>>          if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len, "json",
>>>> "t") != LUA_OK)
>>>>                  return lua_error(L);
>>>>
>>>>          lua_call(L, 0, 1);
>>>>
>>>>          return 1;}
>>>>
>>>>
>>>> I hope this helps...
>>>>
>>> That is very helpful, and not just for nuttx development! Thanks for the
>>> tip.
>>>
>>> The romfs example actually uses xxd as well to convert the filesystem
>> into
>>> hex code that is also stored in a header file. If I am reading the code
>>> correctly, the example app loads the entire filesystem into memory, which
>>> isn't very efficient and not at all what I wanted. Can someone tell me if
>>> that's true?
>>>
>>> Thanks,
>>> Russ
>>>
>>>>
>>>>
>>>> On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <xi...@gmail.com>
>>>> wrote:
>>>>
>>>>> You can use the real file system on the device, there are many
>> choices:
>>>>> romfs, littlefs, fatfs, starmtfs and spiffs.
>>>>>
>>>>> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <russ.haley@gmail.com
>>>>> wrote:
>>>>>
>>>>>> On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
>>> xiaoxiang781216@gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
>> scripts
>>>>> into
>>>>>>> some PC folder and run mount this folder from nsh:
>>>>>>> mount -t hostfs -o fs=/path/to/your/pc/folder. /data
>>>>>>>
>>>>>>> While I appreciate the answer, I am using the sim as a testing
>>>> platform
>>>>>> and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
>>>>> hoping
>>>>>> for a solution that is applicable to an embedded project. If I
>> can't
>>>> just
>>>>>> add files to the initial image then I will look at the romfs
>> example
>>>> and
>>>>>> maybe the next best thing?
>>>>>>
>>>>>>
>>>>>>> On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
>>> russ.haley@gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> Big thanks to Xiang Xiao for pointing me to the sim:lua
>>>>> configuration.
>>>>>> I
>>>>>>>> was unable to simply include the defconfig file that you linked
>>> to,
>>>>>> but I
>>>>>>>> was able to reconfigure for the sim:lua configuration.  I've
>> now
>>>> got
>>>>> an
>>>>>>> app
>>>>>>>> in the examples folder that includes the Lua interpreter. Is
>>> there
>>>> a
>>>>>>>> tutorial on how to include folders and lua scripts or extra
>> files
>>>> in
>>>>>> the
>>>>>>>> initial file system?
>>>>>>>>
>>>>>>>> Much appreciated,
>>>>>>>> Russ
>>>>>>>>

Re: Including Lua scripts on filesystem

Posted by Russell Haley <ru...@gmail.com>.
I am mistaken. I understood that the rom image was part of the application
on flash, but had mis-read the documentation of boardctl(BOARDIOC_ROMDISK,
(uintptr_t)&desc); and thought that the command loaded the image into *RAM*
(the  BOARDIOC_MKRD command makes the RAM disk. oops!).

Incidentally, neither the romfs example nor my attempt to re-create it
works in my sim:

osboxes@osboxes ~/n/nuttx (master)> ./nuttx

NuttShell (NSH) NuttX-12.0.0
nsh> romfs
ERROR: Failed to create RAM disk: Unknown error
nsh> rapp
Starting Russells App 1...
ERROR: Failed to create RAM disk: Unknown error
nsh>

I don't know how to debug this "unknown error". I guess I will try to find
where that error message comes from and hook in GDB? I'm terrible at this
stuff, someone needs to revoke my compiler license (tee hee).

Cheers,
Russ

On Wed, Feb 1, 2023 at 7:29 PM Xiang Xiao <xi...@gmail.com> wrote:

> romfs is part of your image as the const string. There is no difference
> from the below manual step.
>
> On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com>
> wrote:
>
> > On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <
> f.j.panag@gmail.com
> > >
> > wrote:
> >
> > > Hello,
> > >
> > > Indeed the "proper" way of including a script would be to store it in a
> > > file system.
> > >
> > > However, when I needed to include a single and small script and I
> didn't
> > > want to introduce a complete FS just for this, I used xxd.
> > > xxd can convert any file to a C header file.
> > >
> > > You can then include the header, and access the whole file as a
> variable.
> > > Here is an example:
> > >
> > > I added this in my app Makefile:
> > >
> > > # Setup any special pre-build context
> > > context: header
> > >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua > json_lua.h
> &&
> > > echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> > >
> > >
> > >
> > > And then I used the file like this:
> > >
> > > #include "lua.h"#include "lauxlib.h"#include <string.h>
> > >  #include "json_lua.h"
> > >  static int luaopen_json(lua_State * L);
> > >
> > >  void ExtLibs_load(lua_State * L){
> > >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> > >         luaL_requiref(L, "json", luaopen_json, 1);
> > >         lua_pop(L, 1);#endif}
> > >
> > >  int luaopen_json(lua_State * L){
> > >         const char * modname = lua_tostring(L, 1);
> > >
> > >         if (strcmp(modname, "json") != 0)
> > >                 return luaL_error(L, "cannot load json module");
> > >
> > >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len, "json",
> > > "t") != LUA_OK)
> > >                 return lua_error(L);
> > >
> > >         lua_call(L, 0, 1);
> > >
> > >         return 1;}
> > >
> > >
> > > I hope this helps...
> > >
> > That is very helpful, and not just for nuttx development! Thanks for the
> > tip.
> >
> > The romfs example actually uses xxd as well to convert the filesystem
> into
> > hex code that is also stored in a header file. If I am reading the code
> > correctly, the example app loads the entire filesystem into memory, which
> > isn't very efficient and not at all what I wanted. Can someone tell me if
> > that's true?
> >
> > Thanks,
> > Russ
> >
> > >
> > >
> > >
> > > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <xi...@gmail.com>
> > > wrote:
> > >
> > > > You can use the real file system on the device, there are many
> choices:
> > > > romfs, littlefs, fatfs, starmtfs and spiffs.
> > > >
> > > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <russ.haley@gmail.com
> >
> > > > wrote:
> > > >
> > > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> > xiaoxiang781216@gmail.com>
> > > > > wrote:
> > > > >
> > > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your
> scripts
> > > > into
> > > > > > some PC folder and run mount this folder from nsh:
> > > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > > > >
> > > > > > While I appreciate the answer, I am using the sim as a testing
> > > platform
> > > > > and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
> > > > hoping
> > > > > for a solution that is applicable to an embedded project. If I
> can't
> > > just
> > > > > add files to the initial image then I will look at the romfs
> example
> > > and
> > > > > maybe the next best thing?
> > > > >
> > > > >
> > > > > >
> > > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> > russ.haley@gmail.com>
> > > > > > wrote:
> > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > > > configuration.
> > > > > I
> > > > > > > was unable to simply include the defconfig file that you linked
> > to,
> > > > > but I
> > > > > > > was able to reconfigure for the sim:lua configuration.  I've
> now
> > > got
> > > > an
> > > > > > app
> > > > > > > in the examples folder that includes the Lua interpreter. Is
> > there
> > > a
> > > > > > > tutorial on how to include folders and lua scripts or extra
> files
> > > in
> > > > > the
> > > > > > > initial file system?
> > > > > > >
> > > > > > > Much appreciated,
> > > > > > > Russ
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: Including Lua scripts on filesystem

Posted by Xiang Xiao <xi...@gmail.com>.
romfs is part of your image as the const string. There is no difference
from the below manual step.

On Thu, Feb 2, 2023 at 10:00 AM Russell Haley <ru...@gmail.com> wrote:

> On Tue, Jan 31, 2023 at 6:16 AM Fotis Panagiotopoulos <f.j.panag@gmail.com
> >
> wrote:
>
> > Hello,
> >
> > Indeed the "proper" way of including a script would be to store it in a
> > file system.
> >
> > However, when I needed to include a single and small script and I didn't
> > want to introduce a complete FS just for this, I used xxd.
> > xxd can convert any file to a C header file.
> >
> > You can then include the header, and access the whole file as a variable.
> > Here is an example:
> >
> > I added this in my app Makefile:
> >
> > # Setup any special pre-build context
> > context: header
> >         $(Q) cd path/to/libs/json.lua/ && xxd -i json.lua > json_lua.h &&
> > echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
> >
> >
> >
> > And then I used the file like this:
> >
> > #include "lua.h"#include "lauxlib.h"#include <string.h>
> >  #include "json_lua.h"
> >  static int luaopen_json(lua_State * L);
> >
> >  void ExtLibs_load(lua_State * L){
> >         // json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
> >         luaL_requiref(L, "json", luaopen_json, 1);
> >         lua_pop(L, 1);#endif}
> >
> >  int luaopen_json(lua_State * L){
> >         const char * modname = lua_tostring(L, 1);
> >
> >         if (strcmp(modname, "json") != 0)
> >                 return luaL_error(L, "cannot load json module");
> >
> >         if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len, "json",
> > "t") != LUA_OK)
> >                 return lua_error(L);
> >
> >         lua_call(L, 0, 1);
> >
> >         return 1;}
> >
> >
> > I hope this helps...
> >
> That is very helpful, and not just for nuttx development! Thanks for the
> tip.
>
> The romfs example actually uses xxd as well to convert the filesystem into
> hex code that is also stored in a header file. If I am reading the code
> correctly, the example app loads the entire filesystem into memory, which
> isn't very efficient and not at all what I wanted. Can someone tell me if
> that's true?
>
> Thanks,
> Russ
>
> >
> >
> >
> > On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <xi...@gmail.com>
> > wrote:
> >
> > > You can use the real file system on the device, there are many choices:
> > > romfs, littlefs, fatfs, starmtfs and spiffs.
> > >
> > > On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <ru...@gmail.com>
> > > wrote:
> > >
> > > > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <
> xiaoxiang781216@gmail.com>
> > > > wrote:
> > > >
> > > > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your scripts
> > > into
> > > > > some PC folder and run mount this folder from nsh:
> > > > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > > > >
> > > > > While I appreciate the answer, I am using the sim as a testing
> > platform
> > > > and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
> > > hoping
> > > > for a solution that is applicable to an embedded project. If I can't
> > just
> > > > add files to the initial image then I will look at the romfs example
> > and
> > > > maybe the next best thing?
> > > >
> > > >
> > > > >
> > > > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <
> russ.haley@gmail.com>
> > > > > wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> > > configuration.
> > > > I
> > > > > > was unable to simply include the defconfig file that you linked
> to,
> > > > but I
> > > > > > was able to reconfigure for the sim:lua configuration.  I've now
> > got
> > > an
> > > > > app
> > > > > > in the examples folder that includes the Lua interpreter. Is
> there
> > a
> > > > > > tutorial on how to include folders and lua scripts or extra files
> > in
> > > > the
> > > > > > initial file system?
> > > > > >
> > > > > > Much appreciated,
> > > > > > Russ
> > > > > >
> > > > >
> > > >
> > >
> >
>