Difference between revisions of "FatFS"

From ERIKA WIKI
Jump to: navigation, search
(Sharing a FAT file system with Linux)
Line 67: Line 67:
 
<code>
 
<code>
 
       :~ mkdir fatfs-linux-build
 
       :~ mkdir fatfs-linux-build
       :~ aarch64-linux-gnu-gcc -DFATFS_LINUX -I '''/full/erika3/path/'''contrib/fatfs/diskio/memory/ -I '''/full/FatFs/path/'''source/  
+
       :~ aarch64-linux-gnu-gcc -DFATFS_LINUX -I ''/full/erika3/path/''contrib/fatfs/diskio/memory/ -I ''/full/FatFs/path/''source/  
                                                 -c '''/full/erika3/path/'''contrib/fatfs/diskio/memory/*.c  
+
                                                 -c ''/full/erika3/path/''contrib/fatfs/diskio/memory/*.c  
                                                 '''/full/FatFs/path/'''source/ff.c '''/full/FatFs/path/'''source/ffsystem.c  
+
                                                 ''/full/FatFs/path/''source/ff.c ''/full/FatFs/path/''source/ffsystem.c  
                                                 '''/full/FatFs/path/'''source/ffunicode.c  
+
                                                 ''/full/FatFs/path/''source/ffunicode.c  
 
         :~ aarch64-linux-gnu-ar rcs libfatfs.a *.o
 
         :~ aarch64-linux-gnu-ar rcs libfatfs.a *.o
 
</code>
 
</code>
Line 91: Line 91:
 
Link your main with the library:<br>
 
Link your main with the library:<br>
 
<code>
 
<code>
       :~ aarch64-linux-gnu-gcc  -I '''/full/erika3/path/'''contrib/fatfs/diskio/memory/ -I '''/full/FatFs/path/'''source/  
+
       :~ aarch64-linux-gnu-gcc  -I ''/full/erika3/path/''contrib/fatfs/diskio/memory/ -I ''/full/FatFs/path/''source/  
                                                 -c main.c -L. -lfatfs
+
                                                 -c main.c -L''/full/path/fatfs-linux-build'' -lfatfs
 
</code>
 
</code>
  
 
First execute the Linux application and then the Erika inmate. The latter will show all the directories/files you decided to create.
 
First execute the Linux application and then the Erika inmate. The latter will show all the directories/files you decided to create.

Revision as of 15:48, 8 March 2019

Introduction

FatFs is a tiny library which provides FAT/exFAT support for small embedded systems. File system functionalities are separated from the disk I/O layer and are easy to configure by enabling/disabling options.
The stack support will be provided in the next release of Erika with a memory driver which basically exports a portion of memory as a block device.
The following sections describe how to use use a subset of basic functionalities of FatFs.

Erika/FatFS demo on Jetson TX2 with Jailhouse Hypervisor (aarch64)

This demo shows how to handle directories/files on a FAT file system on Erika Enterpries v3 running as a Jailhouse inmate on Jetson TX2 board:

  1. Download the FatFs library as archive from the Download section on http://elm-chan.org/fsw/ff/00index_e.html or simply clone the Git reposity from https://github.com/RIOT-OS/FatFS.git
  2. In the FatFs downloaded sources enable the Long Filename Support and the Mkfs function (in /full/paty/FatFS/source/ffconf.h enable the defines #define FF_USE_LFN 1 and #define FF_USE_MKFS 1)
  3. Set the build environment as described in Nvidia Jetson TX1 and TX2 (cross-toolchain, Jetson platform setup, Jailhouse cross-compiling)
  4. Run the RT-Druid tool
  5. Create a new project by clicking on NewRT-Druid v3 Oil and C/C++ Project as shown in the next Figure:
    Figure 1: Create a new RT-Druid project.
  6. Name the new project (e.g., mytest) and select the Cross-GCC as shown in the next Figure:
    Figure 2: Naming the new RT-Druid project.
  7. Check the box for using an existing template and select aarch64JailhouseFatFsFatFs usage on memoryas shown in the next Figure:
    Figure 3: Selecting the FatFs template.
  8. Right click the project and select Properties as shown in the following Figure:
    Figure 4: Eclipse project properties.
  9. Click OilGenerator properties, enable project specific settings and specify the directory containing the FatFs library for ERIKA3:
    Figure 5: Setting the path of the FatFs library.
  10. Configure the RT-Druid as described in Nvidia Jetson TX1 and TX2#RT-Druid set-up

Creating a private FAT file system on Erika

When the #define EE_MEM_LOCAL is enabled in main.c, Erika will perform such actions:

  1. Before starting OS, will:
    • inizialize the fmem driver
    • create a FAT file system on an array located in .data section of the executable
    • create the directory test
    • create the file erika.txt in the directory test
  2. The Task1 will dump the directories and files content ( output should look like this):
    Figure 6: Application output on private file system.

Sharing a FAT file system with Linux

When the #define EE_MEM_LOCAL is disabled in main.c, Erika expects to find a valid FAT file system at the address location #define memory (const void*)(0x274800000).
This address represents the start of a shared memory region between Erika and Linux where the latter must provide a valid file system (see next subsections).
After proper fmem initialization with the shared memory address, Erika will perform the same actions of directory/file creation described in the previous example.
Please note that the memory region must be mapped into the inmate address space through the map_range function.

Furthermore Jailhouse should be aware of this shared region. This is achieved by putting in both the root cell and inmate cell the section:

                {     
                       .phys_start = 0x274800000,
                       .virt_start = 0x274800000,
                       .size = 0x80000,
                       .flags = JAILHOUSE_MEM_READ | JAILHOUSE_MEM_WRITE
                       | JAILHOUSE_MEM_ROOTSHARED,
                 },


NOTE: configurations sources are jetson-tx2.c and jetson-tx2-demo.c located in the folder configs/arm64 of the Evidence Jailhouse repository cloned from https://github.com/evidence/linux-jailhouse-jetson.git. Don't forget to increase the struct jailhouse_memory mem_regionsnumber at the beginning of the files. Furthermore the executable size (including the FatFs library) could not fit the default RAM size assigned to the inmate. Increase it to 0x100000 in the RAM memregion in configs/arm64/jetson-tx2-demo.c and in the function arch_mmu_enable in inmates/lib/arm-common/mem.c.
After configurations' update, they must be rebuilt (follow instruction in the README.md of the Hypervisor repository).

Linux can provide a file system to Erika with the help of the FatFs library which can be built for Linux as well. To build the library for Linux just use the following commands:

      :~ mkdir fatfs-linux-build
      :~ aarch64-linux-gnu-gcc -DFATFS_LINUX -I /full/erika3/path/contrib/fatfs/diskio/memory/ -I /full/FatFs/path/source/ 
                                                -c /full/erika3/path/contrib/fatfs/diskio/memory/*.c 
                                                /full/FatFs/path/source/ff.c /full/FatFs/path/source/ffsystem.c 
                                                /full/FatFs/path/source/ffunicode.c 
       :~ aarch64-linux-gnu-ar rcs libfatfs.a *.o

Now that library is built, you can use main.c which looks similar to the one provided with the FatFs Erika demo. FatFs functions are the same indeed (except for the handling of system time which is correctly taken from Linux time). The main difference with the Erika application is in the shared memory's mapping which is performed with mmap in order to be accessed by the application. A code snippet which performs such mapping could be the following:

       long int physical_address = 0x274800000;
       size_t memory_size = MINIMUM_FAT_SIZE;
       void *process_memory_address = NULL;
       int fd = open("/dev/mem", O_RDWR);
       if(fd >= 0 && physical_address && size){
               process_memory_address = mmap(NULL, memory_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 
                       physical_address);
        }

After calling this function with proper parameters, init the fmem drive with f_mem_init(process_memory_address, memory_size) and then create/modify the FAT file system (use f_mkfs, f_mount and friends...).

Link your main with the library:

      :~ aarch64-linux-gnu-gcc  -I /full/erika3/path/contrib/fatfs/diskio/memory/ -I /full/FatFs/path/source/ 
                                                -c main.c -L/full/path/fatfs-linux-build -lfatfs

First execute the Linux application and then the Erika inmate. The latter will show all the directories/files you decided to create.