119ea8026Sopenharmony_ci/* 219ea8026Sopenharmony_ci * Block device emulated in a file 319ea8026Sopenharmony_ci * 419ea8026Sopenharmony_ci * Copyright (c) 2022, The littlefs authors. 519ea8026Sopenharmony_ci * Copyright (c) 2017, Arm Limited. All rights reserved. 619ea8026Sopenharmony_ci * SPDX-License-Identifier: BSD-3-Clause 719ea8026Sopenharmony_ci */ 819ea8026Sopenharmony_ci#ifndef LFS_FILEBD_H 919ea8026Sopenharmony_ci#define LFS_FILEBD_H 1019ea8026Sopenharmony_ci 1119ea8026Sopenharmony_ci#include "lfs.h" 1219ea8026Sopenharmony_ci#include "lfs_util.h" 1319ea8026Sopenharmony_ci 1419ea8026Sopenharmony_ci#ifdef __cplusplus 1519ea8026Sopenharmony_ciextern "C" 1619ea8026Sopenharmony_ci{ 1719ea8026Sopenharmony_ci#endif 1819ea8026Sopenharmony_ci 1919ea8026Sopenharmony_ci 2019ea8026Sopenharmony_ci// Block device specific tracing 2119ea8026Sopenharmony_ci#ifndef LFS_FILEBD_TRACE 2219ea8026Sopenharmony_ci#ifdef LFS_FILEBD_YES_TRACE 2319ea8026Sopenharmony_ci#define LFS_FILEBD_TRACE(...) LFS_TRACE(__VA_ARGS__) 2419ea8026Sopenharmony_ci#else 2519ea8026Sopenharmony_ci#define LFS_FILEBD_TRACE(...) 2619ea8026Sopenharmony_ci#endif 2719ea8026Sopenharmony_ci#endif 2819ea8026Sopenharmony_ci 2919ea8026Sopenharmony_ci// filebd config 3019ea8026Sopenharmony_cistruct lfs_filebd_config { 3119ea8026Sopenharmony_ci // Minimum size of a read operation in bytes. 3219ea8026Sopenharmony_ci lfs_size_t read_size; 3319ea8026Sopenharmony_ci 3419ea8026Sopenharmony_ci // Minimum size of a program operation in bytes. 3519ea8026Sopenharmony_ci lfs_size_t prog_size; 3619ea8026Sopenharmony_ci 3719ea8026Sopenharmony_ci // Size of an erase operation in bytes. 3819ea8026Sopenharmony_ci lfs_size_t erase_size; 3919ea8026Sopenharmony_ci 4019ea8026Sopenharmony_ci // Number of erase blocks on the device. 4119ea8026Sopenharmony_ci lfs_size_t erase_count; 4219ea8026Sopenharmony_ci}; 4319ea8026Sopenharmony_ci 4419ea8026Sopenharmony_ci// filebd state 4519ea8026Sopenharmony_citypedef struct lfs_filebd { 4619ea8026Sopenharmony_ci int fd; 4719ea8026Sopenharmony_ci const struct lfs_filebd_config *cfg; 4819ea8026Sopenharmony_ci} lfs_filebd_t; 4919ea8026Sopenharmony_ci 5019ea8026Sopenharmony_ci 5119ea8026Sopenharmony_ci// Create a file block device 5219ea8026Sopenharmony_ciint lfs_filebd_create(const struct lfs_config *cfg, const char *path, 5319ea8026Sopenharmony_ci const struct lfs_filebd_config *bdcfg); 5419ea8026Sopenharmony_ci 5519ea8026Sopenharmony_ci// Clean up memory associated with block device 5619ea8026Sopenharmony_ciint lfs_filebd_destroy(const struct lfs_config *cfg); 5719ea8026Sopenharmony_ci 5819ea8026Sopenharmony_ci// Read a block 5919ea8026Sopenharmony_ciint lfs_filebd_read(const struct lfs_config *cfg, lfs_block_t block, 6019ea8026Sopenharmony_ci lfs_off_t off, void *buffer, lfs_size_t size); 6119ea8026Sopenharmony_ci 6219ea8026Sopenharmony_ci// Program a block 6319ea8026Sopenharmony_ci// 6419ea8026Sopenharmony_ci// The block must have previously been erased. 6519ea8026Sopenharmony_ciint lfs_filebd_prog(const struct lfs_config *cfg, lfs_block_t block, 6619ea8026Sopenharmony_ci lfs_off_t off, const void *buffer, lfs_size_t size); 6719ea8026Sopenharmony_ci 6819ea8026Sopenharmony_ci// Erase a block 6919ea8026Sopenharmony_ci// 7019ea8026Sopenharmony_ci// A block must be erased before being programmed. The 7119ea8026Sopenharmony_ci// state of an erased block is undefined. 7219ea8026Sopenharmony_ciint lfs_filebd_erase(const struct lfs_config *cfg, lfs_block_t block); 7319ea8026Sopenharmony_ci 7419ea8026Sopenharmony_ci// Sync the block device 7519ea8026Sopenharmony_ciint lfs_filebd_sync(const struct lfs_config *cfg); 7619ea8026Sopenharmony_ci 7719ea8026Sopenharmony_ci 7819ea8026Sopenharmony_ci#ifdef __cplusplus 7919ea8026Sopenharmony_ci} /* extern "C" */ 8019ea8026Sopenharmony_ci#endif 8119ea8026Sopenharmony_ci 8219ea8026Sopenharmony_ci#endif 83