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 <string.h>
33#include "los_memory.h"
34#include "lfs_rambd.h"
35#include "lfs.h"
36
37#define READ_SIZE      16
38#define PROG_SIZE      16
39#define BLOCK_SIZE     256
40#define BLOCK_COUNT    128
41#define CACHE_SIZE     16
42#define LOOKAHEAD_SIZE 16
43#define BLOCK_CYCLES   500
44
45static int LittlefsRead(const struct lfs_config *cfg, lfs_block_t block,
46                        lfs_off_t off, void *buffer, lfs_size_t size)
47{
48    (void)lfs_rambd_read(cfg, block, off, buffer, size);
49
50    return LFS_ERR_OK;
51}
52
53static int LittlefsProg(const struct lfs_config *cfg, lfs_block_t block,
54                        lfs_off_t off, const void *buffer, lfs_size_t size)
55{
56    (void)lfs_rambd_prog(cfg, block, off, buffer, size);
57
58    return LFS_ERR_OK;
59}
60
61static int LittlefsErase(const struct lfs_config *cfg, lfs_block_t block)
62{
63    (void)lfs_rambd_erase(cfg, block);
64
65    return LFS_ERR_OK;
66}
67
68static int LittlefsSync(const struct lfs_config *cfg)
69{
70    return LFS_ERR_OK;
71}
72
73static struct lfs_config g_lfsConfig = {
74    // block device operations
75    .context = NULL,
76    .read  = LittlefsRead,
77    .prog  = LittlefsProg,
78    .erase = LittlefsErase,
79    .sync  = LittlefsSync,
80
81    // block device configuration
82    .read_size = READ_SIZE,
83    .prog_size = PROG_SIZE,
84    .block_size = BLOCK_SIZE,
85    .block_count = BLOCK_COUNT,
86    .cache_size = CACHE_SIZE,
87    .lookahead_size = LOOKAHEAD_SIZE,
88    .block_cycles = BLOCK_CYCLES,
89    .read_buffer = NULL,
90    .prog_buffer = NULL,
91    .lookahead_buffer = NULL
92};
93
94void LittlefsDriverInit(int needErase)
95{
96    lfs_rambd_t *bd = (lfs_rambd_t *)LOS_MemAlloc(m_aucSysMem0, sizeof(lfs_rambd_t));
97    (void)memset_s(bd, sizeof(lfs_rambd_t), 0, sizeof(lfs_rambd_t));
98    g_lfsConfig.context = bd;
99    (void)lfs_rambd_create(&g_lfsConfig);
100}
101
102struct lfs_config* LittlefsConfigGet(void)
103{
104    return &g_lfsConfig;
105}
106