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