Zephyr is a real-time operating system (RTOS) designed to run on embedded, resource-constrained devices. A more recent application involves using this RTOS on more performant machines with CPU architectures like RISC-V, ARM64, and x86, and also running existing software written for other operating systems. A crucial component that helps implementing non-native APIs is an abstraction layer called the Zephyr Virtual Filesystem (ZVFS).
We have enhanced the implementation of ZVFS by expanding its compatibility with the Portable Operating System Interface (POSIX). The improvement makes applications originally developed for other OSes more compatible with Zephyr, effectively streamlining their porting to the latter, and introduces a number of POSIX functions you can use. At the same time, ZVFS is now an optional layer that can be disabled.
This article describes the way ZVFS works with POSIX and provides details on the new additions.
Improving ZVFS for inter-OS operability
As one of its many features, Zephyr offers various abstraction layers, one of which, called ZVFS, implements some functionalities of POSIX. These functionalities ensure compatibility among different OSes by providing a file descriptor table that holds information about opened files and handles them through descriptor indices. This way, POSIX unifies different OSes by facilitating uniformly written code, thanks to which the same application can be run on a number of systems.
While Zephyr on its own doesn’t provide such functionalities, ZVFS does integrate POSIX this way. The file descriptor table ZVFS provides is used through functions whose names indicate a relationship with ZVFS, for example, zvfs_*fd*.
To simplify the process of porting existing software to Zephyr, we have improved ZVFS so that it contains every capability needed to implement a file access API similar to that of POSIX. As a result, Zephyr now uses mechanisms similar to those found in other OSes. For the improvement, we’ve introduced the following changes:
- The need to implement a data structure that keeps track of open files for each external file access standard supported by Zephyr has been removed.
- The POSIX file descriptor table has become optional. This is useful when the table isn’t needed for an application to work correctly.
- All single-file operations have been moved to ZVFS, and a POSIX subsystem calls ZVFS to perform these operations. A POSIX subsystem is a component enabling POSIX software to be run on non-POSIX OSes.
Additionally, we’ve defined the zvfs_stat structure reference to avoid a dependency cycle, and we’ve made the ZVFS functions accessible from other places in the source code since they don’t require any changes to be shared among various subsystems. What’s more, to reduce resource consumption, ZVFS can now be disabled by setting CONFIG_ZVFS to no in menuconfig.
Adding C functions to ZVFS
Apart from improving the already existing ZVFS elements, Antmicro has also implemented C functions from ISO C, which is another available standard different than POSIX. The functions store information about files in a similar structure to the file descriptor tables used in OSes.
Our implementation introduces methods called fdopen and fileno() in ZVFS so that you can perform operations using C functions on files previously opened through POSIX. The methods act as a bridge between the POSIX and C functions to use the latter to open and write files, thanks to which you don’t have to define custom methods for C functions from scratch.
As a result, porting applications that use ISO C interfaces for file operations to Zephyr is easier, and you can use both C functions and POSIX functions in the same program to handle the same file through the fdopen method.
Create and customize embedded software with Zephyr
Antmicro’s changes to ZVFS provide a wider array of applications from different OSes that you can launch on and develop for embedded devices running the Zephyr RTOS. The strengthened compatibility with POSIX improve the previously existing features, and the addition of C functions adds new ways of performing file operations.
Our other work around Zephyr includes, for example, the possibility of monitoring and optimizing AI applications, OTA and fleet device management, and developing GUIs with a POSIX-compliant library called grvl. Write to us at contact@antmicro.com, and we’ll happily provide commercial services tailored to your use cases.
