How rickyzheng/uffs works
1. What this repo is
UFFS is a C-based NAND flash file system for embedded systems, with a strong split between a portable core and pluggable platform/flash backends. The same codebase supports real devices, a PC simulator, and cross-platform testing.
2. Core file-system engine
src/uffs contains the actual filesystem logic: mounting, directory/tree management, file descriptors, block bookkeeping, lookup, versioning, and public API entry points. This is the subsystem that translates filesystem operations into flash-page and block operations.
3. Flash and device abstraction boundary
The architecture is built around a hardware abstraction layer in src/inc/uffs/*.h and src/uffs/uffs_device.c, uffs_flash.c, uffs_fs.c. The core does not talk directly to a specific NAND controller; instead it uses abstract flash/device interfaces so the same filesystem can run on bare metal, POSIX, Win32, or the emulator.
4. Reliability and flash-management layers
Several subsystems exist specifically for NAND constraints:
- uffs_badblock.* for bad-block detection and management
- uffs_ecc.* for ECC handling
- uffs_crc.* for page/data integrity checks
- uffs_blockinfo.*, uffs_buf.*, uffs_pool.* for cached metadata and page buffering
These layers mediate between file operations and unreliable flash media.
5. Tree, metadata, and recovery model
uffs_tree.*, uffs_mtb.*, and related code handle the logical tree of files/directories plus the “mount tree”/metadata reconstruction from flash. The readme emphasizes quick mount by scanning only spare data, so boot-time recovery is a key architectural path, not an afterthought.
6. Memory model as a first-class design choice
UFFS is designed to work with static allocation as well as native/system allocation. uffs_mem.*, uffs_pool.*, and the example static-mem-allocate.c show that memory is intentionally bounded and configurable, which is central to the embedded use case.
7. OS and portability layer
src/platform/posix and src/platform/win32 provide OS wrappers (uffs_os.c, uffs_config.h) for threading, synchronization, timing, and platform-specific services. This is the main boundary that lets the filesystem run either with an RTOS/host OS or in a more bare-metal style environment.
8. PC simulator / flash emulator
src/emu is the simulator-side flash model and command shell. Files like uffs_fileem.c, ECC variants, and cmdline.c emulate NAND behavior, bad blocks, and ECC so the filesystem can be exercised on a desktop without real hardware.
9. Public API surface
src/inc/uffs/uffs_public.h, uffs_fs.h, uffs_fd.h, uffs_flash.h, and uffs.h define the API that application code uses. The repo includes example clients in src/example showing the intended integration path for embedded applications.
10. Integration and test harness
src/test/api_test provides a server/client test setup that bridges the simulator and external test processes. This is used to compare UFFS behavior against a reference filesystem and validate POSIX-like semantics.
11. SQLite regression suite reuse
A large vendored SQLite source tree lives under src/test/sqlite3/sqlite-src-3070900. UFFS adapts SQLite’s test infrastructure through the test-uffs harness and scripts, using it as a high-coverage filesystem regression workload. This is a major testing subsystem, not just a dependency copy.
12. Command-line utilities
src/utils/mkuffs.c is the main tool for building and interacting with UFFS disk images/simulator data. It is part of the operational workflow: format the simulated flash, mount it, and run tests against it.
13. Build and packaging
The project uses CMake at the top level and per-subdirectory CMake files to build the core, simulator, tests, and examples. Build output is intended to span multiple targets: library/core, simulator executable, test fixtures, and example programs.
14. Overall data flow
Application code calls the public UFFS API, which routes into the core filesystem engine. The core consults cached metadata, buffer pools, tree/block info, and integrity layers, then issues abstract flash operations through the device/OS boundary. In simulation, those flash operations are handled by the emulator; on real hardware, they are implemented by platform-specific flash drivers.
15. Key architectural boundaries to show in a diagram
The most important lines to draw are:
- application/API boundary
- filesystem core vs. flash/hardware abstraction
- core vs. platform OS services
- core vs. simulator/emulator
- core vs. test harness/regression tooling
Those boundaries explain how UFFS stays portable while still modeling NAND-specific behavior closely.
Main entry points: CMakeLists.txt Src build · uffs_fs.c FS core · uffs_device.c Device layer · uffs_fileem.c Emulator
Components 19
Build
Top-level and per-subdirectory CMake build orchestration.
API
Public headers and application-facing entry points.
- Public API · Application-facing filesystem and flash interfaces.
src/inc/uffs/uffs_public.h - FS API · Main filesystem entry points used by applications.
src/inc/uffs/uffs_fs.h - Flash API · Abstract flash device operations and geometry.
src/inc/uffs/uffs_flash.h - Examples · Reference application integrations and allocation patterns.
src/example/flash-interface-example.c
Core
Filesystem engine and flash-management internals.
- FS core · Mounting, lookup, file operations, and orchestration.
src/uffs/uffs_fs.c - Device layer · Bridges filesystem logic to abstract devices and backends.
src/uffs/uffs_device.c - Flash ops · Abstract flash page/block operations used by the core.
src/uffs/uffs_flash.c - Tree model · Logical file and directory structure in flash.
src/uffs/uffs_tree.c - Mount tree · Reconstructs metadata during mount and boot-time recovery.
src/uffs/uffs_mtb.c - Reliability · Bad blocks, ECC, CRC, buffering, and block metadata.
src/uffs/uffs_badblock.c - Memory pool · Static and system allocation support for embedded use.
src/uffs/uffs_pool.c
Ports
Operating-system and hardware abstraction backends.
- POSIX port · Host/RTOS-style services for POSIX environments.
src/platform/posix/uffs_os.c - Win32 port · Host services for Windows environments.
src/platform/win32/uffs_os.c
Tests
Simulator-backed integration and regression tooling.
- Emulator · Desktop flash model with ECC, bad blocks, and command shell.
src/emu/uffs_fileem.c - API test · Client/server test bridge between simulator and external processes.
src/test/api_test/api_test.c - SQLite suite · Vendored SQLite tests adapted to exercise UFFS behavior.
src/test/sqlite3/sqlite-src-3070900/test-uffs.sh
Tools
Image and utility commands used to operate UFFS.
- mkuffs · Builds and manipulates UFFS images and simulator data.
src/utils/mkuffs.c
Connections 27
- CMake → Src build: builds · Top-level build delegates into src subprojects.
- Src build → Public API: exports · Build exposes application-facing headers.
- Src build → FS core: compiles · Core filesystem engine is built from src/uffs.
- Src build → POSIX port: compiles · POSIX service layer is one build target.
- Src build → Win32 port: compiles · Windows service layer is one build target.
- Src build → Emulator: compiles · Simulator/emulator is built as a separate target.
- Src build → API test: compiles · Integration test harness is built as a separate target.
- Src build → Examples: compiles · Example clients are included in the build.
- Public API → FS API: defines · Public API includes filesystem entry points.
- Public API → Flash API: defines · Public API includes abstract flash interfaces.
- FS API → FS core: calls · Filesystem API routes into the core engine.
- Flash API → Device layer: contracts · Flash interface is implemented through device abstraction.
- FS core → Tree model: uses · Core relies on the logical tree for namespace operations.
- FS core → Mount tree: uses · Mount path reconstructs metadata from flash.
- FS core → Reliability: uses · Core consults integrity and NAND-management layers.
- FS core → Memory pool: uses · Core uses bounded allocation and pooled buffers.
- FS core → Device layer: issues IO · Core sends abstract operations through the device boundary.
- Device layer → Flash ops: adapts to · Device boundary maps into flash operations.
- Device layer → POSIX port: depends on · POSIX backend provides host services where needed.
- Device layer → Win32 port: depends on · Win32 backend provides host services where needed.
- Emulator → Flash ops: implements · Emulator provides NAND behavior for flash operations.
- Emulator → Reliability: simulates · Emulator models ECC and bad blocks for testing.
- API test → Emulator: drives · Integration harness exercises the emulator-backed filesystem.
- API test → FS core: validates · Harness validates filesystem behavior through the core API.
- SQLite suite → API test: runs via · SQLite regression workload is adapted through the test harness.
- mkuffs → Emulator: prepares · Utility prepares disk images and simulator data.
- Examples → Public API: uses · Examples show application integration through the public API.