• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of CHIPSEA TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <stdio.h>
30 #include "los_config.h"
31 #include "hal_vfs.h"
32 #include "flash_api.h"
33 
34 
35 static struct lfs_manager *VfsOps = NULL;
36 
lfs_block_read(const struct lfs_config * c,lfs_block_t block,lfs_off_t off,void * dst,lfs_size_t size)37 static int lfs_block_read(const struct lfs_config *c, lfs_block_t block,
38                           lfs_off_t off, void *dst, lfs_size_t size)
39 {
40     uint32_t addr = c->block_size * block + off + VfsOps->start_addr;
41     flash_read(addr, size, dst);
42     return 0;
43 }
44 
lfs_block_write(const struct lfs_config * c,lfs_block_t block,lfs_off_t off,const void * dst,lfs_size_t size)45 static int lfs_block_write(const struct lfs_config *c, lfs_block_t block,
46                            lfs_off_t off, const void *dst, lfs_size_t size)
47 {
48     uint32_t addr = c->block_size * block + off + VfsOps->start_addr;
49     flash_write(addr, size, dst);
50     return 0;
51 }
52 
lfs_block_erase(const struct lfs_config * c,lfs_block_t block)53 static int lfs_block_erase(const struct lfs_config *c, lfs_block_t block)
54 {
55     uint32_t addr = c->block_size * block + VfsOps->start_addr;
56 
57     return flash_erase(addr, c->block_size);
58 }
59 
lfs_block_sync(const struct lfs_config * c)60 static int lfs_block_sync(const struct lfs_config *c)
61 {
62     return 0;
63 }
64 
hal_vfs_init(void)65 int32_t hal_vfs_init(void)
66 {
67     printf("hal_vfs_init: +++++++++++++++++++++++++++++++++++++++++++++++ \n");
68     VfsOps = malloc(sizeof(struct lfs_manager));
69     if (VfsOps == NULL) {
70         printf("+++ hal_vfs_init: NO memory!!\n");
71         return -1;
72     } else {
73         memset(VfsOps, 0, sizeof(struct lfs_manager));
74     }
75 
76     VfsOps->LfsOps.read = lfs_block_read;
77     VfsOps->LfsOps.prog = lfs_block_write;
78     VfsOps->LfsOps.erase = lfs_block_erase;
79     VfsOps->LfsOps.sync = lfs_block_sync;
80     VfsOps->LfsOps.read_size = LFS_READ_SIZE;
81     VfsOps->LfsOps.prog_size = LFS_READ_SIZE;
82     VfsOps->LfsOps.cache_size = LFS_READ_SIZE;
83     VfsOps->LfsOps.lookahead_size = LFS_LOOK_AHEAD_SIZE;
84     VfsOps->LfsOps.block_cycles = LFS_BLOCK_CYCLES;
85     VfsOps->start_addr = LFS_DEFAULT_START_ADDR;
86     VfsOps->LfsOps.block_size = LFS_DEFAULT_BLOCK_SIZE;
87     VfsOps->LfsOps.block_count = LFS_DEFAULT_BLOCK_COUNT;
88 
89     // SetDefaultMountPath(0,"/data");
90     if (LOS_FsMount(NULL, "/data", "littlefs", 0, VfsOps) != 0) {
91         printf("+++ hal_vfs_init: Mount littlefs faild!\n");
92         free(VfsOps);
93         return -1;
94     }
95 
96     if (LOS_Mkdir("/data", 0777) != 0) {
97         printf("+++ hal_vfs_init: Make dir faild!\n");
98     }
99 #ifdef DSOFTBUS_ENABLED
100     if (LOS_Mkdir("/data/data", 0777) != 0) {
101         printf("+++ hal_vfs_init: 1 Make dir faild!\n");
102     }
103     if (LOS_Mkdir("/data/data/data", 0777) != 0) {
104         printf("+++ hal_vfs_init: 2 Make dir faild!\n");
105     }
106     if (LOS_Mkdir("/data/data/data/dsoftbus", 0777) != 0) {
107         printf("+++ hal_vfs_init: 3 Make dir faild!\n");
108     }
109 #endif
110 
111     flash_user_data_addr_length_set(LFS_DEFAULT_START_ADDR,
112             LFS_DEFAULT_BLOCK_SIZE * LFS_DEFAULT_BLOCK_COUNT);
113 
114     printf("+++ hal_vfs_init: Mount littlefs success!\n");
115     return 0;
116 }
117 
hal_vfs_deinit(void)118 void hal_vfs_deinit(void)
119 {
120     LOS_FsUmount(ROOTPATH);
121     free(VfsOps);
122 }
123 
124 
125