1d6aed566Sopenharmony_ci/* 2d6aed566Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3d6aed566Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 4d6aed566Sopenharmony_ci * 5d6aed566Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 6d6aed566Sopenharmony_ci * are permitted provided that the following conditions are met: 7d6aed566Sopenharmony_ci * 8d6aed566Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 9d6aed566Sopenharmony_ci * conditions and the following disclaimer. 10d6aed566Sopenharmony_ci * 11d6aed566Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12d6aed566Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 13d6aed566Sopenharmony_ci * provided with the distribution. 14d6aed566Sopenharmony_ci * 15d6aed566Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16d6aed566Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 17d6aed566Sopenharmony_ci * permission. 18d6aed566Sopenharmony_ci * 19d6aed566Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20d6aed566Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21d6aed566Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22d6aed566Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23d6aed566Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24d6aed566Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25d6aed566Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26d6aed566Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27d6aed566Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28d6aed566Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29d6aed566Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30d6aed566Sopenharmony_ci */ 31d6aed566Sopenharmony_ci 32d6aed566Sopenharmony_ci#include <stdio.h> 33d6aed566Sopenharmony_ci#include <string.h> 34d6aed566Sopenharmony_ci#include "los_memory.h" 35d6aed566Sopenharmony_ci#include "lfs_rambd.h" 36d6aed566Sopenharmony_ci#include "lfs.h" 37d6aed566Sopenharmony_ci 38d6aed566Sopenharmony_ci#define LITTLEFS_PHYS_ADDR 0x00 39d6aed566Sopenharmony_ci#define LITTLEFS_PHYS_SIZE (64 * 1024) 40d6aed566Sopenharmony_ci 41d6aed566Sopenharmony_ci#define READ_SIZE 16 42d6aed566Sopenharmony_ci#define PROG_SIZE 16 43d6aed566Sopenharmony_ci#define BLOCK_SIZE 256 44d6aed566Sopenharmony_ci#define BLOCK_COUNT 128 45d6aed566Sopenharmony_ci#define CACHE_SIZE 16 46d6aed566Sopenharmony_ci#define LOOKAHEAD_SIZE 16 47d6aed566Sopenharmony_ci#define BLOCK_CYCLES 500 48d6aed566Sopenharmony_ci 49d6aed566Sopenharmony_cistatic int LittlefsRead(const struct lfs_config *cfg, lfs_block_t block, 50d6aed566Sopenharmony_ci lfs_off_t off, void *buffer, lfs_size_t size) 51d6aed566Sopenharmony_ci{ 52d6aed566Sopenharmony_ci (void)lfs_rambd_read(cfg, block, off, buffer, size); 53d6aed566Sopenharmony_ci 54d6aed566Sopenharmony_ci return LFS_ERR_OK; 55d6aed566Sopenharmony_ci} 56d6aed566Sopenharmony_ci 57d6aed566Sopenharmony_cistatic int LittlefsProg(const struct lfs_config *cfg, lfs_block_t block, 58d6aed566Sopenharmony_ci lfs_off_t off, const void *buffer, lfs_size_t size) 59d6aed566Sopenharmony_ci{ 60d6aed566Sopenharmony_ci (void)lfs_rambd_prog(cfg, block, off, buffer, size); 61d6aed566Sopenharmony_ci 62d6aed566Sopenharmony_ci return LFS_ERR_OK; 63d6aed566Sopenharmony_ci} 64d6aed566Sopenharmony_ci 65d6aed566Sopenharmony_cistatic int LittlefsErase(const struct lfs_config *cfg, lfs_block_t block) 66d6aed566Sopenharmony_ci{ 67d6aed566Sopenharmony_ci (void)lfs_rambd_erase(cfg, block); 68d6aed566Sopenharmony_ci 69d6aed566Sopenharmony_ci return LFS_ERR_OK; 70d6aed566Sopenharmony_ci} 71d6aed566Sopenharmony_ci 72d6aed566Sopenharmony_cistatic int LittlefsSync(const struct lfs_config *cfg) 73d6aed566Sopenharmony_ci{ 74d6aed566Sopenharmony_ci return LFS_ERR_OK; 75d6aed566Sopenharmony_ci} 76d6aed566Sopenharmony_ci 77d6aed566Sopenharmony_cistatic struct lfs_config g_lfsConfig = { 78d6aed566Sopenharmony_ci // block device operations 79d6aed566Sopenharmony_ci .context = NULL, 80d6aed566Sopenharmony_ci .read = LittlefsRead, 81d6aed566Sopenharmony_ci .prog = LittlefsProg, 82d6aed566Sopenharmony_ci .erase = LittlefsErase, 83d6aed566Sopenharmony_ci .sync = LittlefsSync, 84d6aed566Sopenharmony_ci 85d6aed566Sopenharmony_ci // block device configuration 86d6aed566Sopenharmony_ci .read_size = READ_SIZE, 87d6aed566Sopenharmony_ci .prog_size = PROG_SIZE, 88d6aed566Sopenharmony_ci .block_size = BLOCK_SIZE, 89d6aed566Sopenharmony_ci .block_count = BLOCK_COUNT, 90d6aed566Sopenharmony_ci .cache_size = CACHE_SIZE, 91d6aed566Sopenharmony_ci .lookahead_size = LOOKAHEAD_SIZE, 92d6aed566Sopenharmony_ci .block_cycles = BLOCK_CYCLES, 93d6aed566Sopenharmony_ci .read_buffer = NULL, 94d6aed566Sopenharmony_ci .prog_buffer = NULL, 95d6aed566Sopenharmony_ci .lookahead_buffer = NULL 96d6aed566Sopenharmony_ci}; 97d6aed566Sopenharmony_ci 98d6aed566Sopenharmony_civoid LittlefsDriverInit(int needErase) 99d6aed566Sopenharmony_ci{ 100d6aed566Sopenharmony_ci lfs_rambd_t *bd = (lfs_rambd_t *)LOS_MemAlloc(m_aucSysMem0, sizeof(lfs_rambd_t)); 101d6aed566Sopenharmony_ci (void)memset_s(bd, sizeof(lfs_rambd_t), 0, sizeof(lfs_rambd_t)); 102d6aed566Sopenharmony_ci g_lfsConfig.context = bd; 103d6aed566Sopenharmony_ci (void)lfs_rambd_create(&g_lfsConfig); 104d6aed566Sopenharmony_ci} 105d6aed566Sopenharmony_ci 106d6aed566Sopenharmony_cistruct lfs_config* LittlefsConfigGet(void) 107d6aed566Sopenharmony_ci{ 108d6aed566Sopenharmony_ci return &g_lfsConfig; 109d6aed566Sopenharmony_ci} 110