The latest development version of this page may be more current than this released 4.0.0 version.

File system shell

Browse source code on GitHub

Overview

This example provides shell access to a LittleFS file system partition in flash.

Requirements

A board with LittleFS file system support and UART console

Building

native_sim

You can build this sample for native_sim with:

west build -b native_sim samples/subsys/shell/fs

Which by default will use the flash simulator. The flash simulator will use a file, flash.bin, in the current directory to keep the flash content. You have several options to control this. You can check them with:

zephyr/zephyr.exe -help

Check the native_sim UART documentation for information on how to connect to the UART.

With FUSE access in the host filesystem

If you enable the host FUSE filsystem access you will also have the flash filesystem mounted and accessible from your Linux host filesystem.

Before starting a build, make sure that the i386 pkgconfig directory is in your search path and that a 32-bit version of libfuse is installed. For more background information on this requirement check the native_sim documentation.

export PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig
west build -b native_sim samples/subsys/shell/fs -- -DCONFIG_FUSE_FS_ACCESS=y

Reel Board

west build -b reel_board samples/subsys/shell/fs

Particle Xenon

This target is customized to support the same SPI NOR partition table as the LittleFS filesystem sample.

west build -b particle_xenon samples/subsys/shell/fs

Flash load

If you want to use the ‘flash load’ command then build the sample with the ‘prj_flash_load.conf’ configuration file. It has defined a larger RX buffer. If the buffer is too small then some data may be lost during transfer of large files.

Running

Once the board has booted, you will be presented with a shell prompt. All file system related commands are available as sub-commands of fs.

Begin by mounting the LittleFS file system.

fs mount littlefs /lfs

Loading filesystem from host PC to flash memory

Use command:

flash load <address> <size>

It allows loading the data via UART, directly into flash memory at a given address. Data must be aligned to a value dependent on the target flash memory, otherwise it will cause an error and nothing will be loaded.

From the host side file system must be loaded with ‘dd’ tool with ‘bs=64’ (if the file is loaded in chunks greater than 64B the data is lost and isn’t received by the Zephyr shell).

Example in Zephyr console:

flash load 0x7a000 0x5000

Example in the host PC:

dd if=filesystem of=/dev/ttyACM0 bs=64

During the transfer there are printed messages indicating how many chunks are already written. After the successful transfer the ‘Read all’ message is printed.

Files System Shell Commands

Mount

Mount a file system partition to a given mount point

fs mount (littlefs|fat) <path>

Ls

List all files and directories in a given path

fs ls [path]

Cd

Change current working directory to given path

fs cd [path]

Pwd

List current working directory

fs pwd

Write

Write hexadecimal numbers to a given file. Optionally a offset in the file can be given.

fs write <path> [-o <offset>] <hex number> ...

Read

Read file and dump in hex and ASCII format

fs read <path>

Trunc

Truncate a given file

fs trunc <path>

Mkdir

Create a directory

fs mkdir <path>

Rm

Remove a file or directory

fs rm <path>

Flash Host Access

For the native sim board the flash partitions can be accessed from the host Linux system.

By default the flash partitions are accessible through the directory flash relative to the directory where the build is started.

See also

File System APIs