1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2022 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 /** 33 * @defgroup los_fs fs 34 * @ingroup kernel 35 */ 36 37 #ifndef _LOS_FS_H_ 38 #define _LOS_FS_H_ 39 40 #include "los_config.h" 41 #include "los_memory.h" 42 #include "dirent.h" 43 #include "sys/mount.h" 44 #include "sys/statfs.h" 45 #include "sys/stat.h" 46 #include "sys/uio.h" 47 #include "unistd.h" 48 #include <stdarg.h> 49 #include "vfs_maps.h" 50 51 #ifdef __cplusplus 52 #if __cplusplus 53 extern "C" { 54 #endif /* __cplusplus */ 55 #endif /* __cplusplus */ 56 57 #ifndef LOSCFG_FS_MALLOC_HOOK 58 #define LOSCFG_FS_MALLOC_HOOK(size) LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, size) 59 #endif 60 61 #ifndef LOSCFG_FS_FREE_HOOK 62 #define LOSCFG_FS_FREE_HOOK(ptr) LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, ptr) 63 #endif 64 65 struct PartitionCfg { 66 /* partition low-level read func */ 67 int (*readFunc)(int partition, UINT32 *offset, void *buf, UINT32 size); 68 /* partition low-level write func */ 69 int (*writeFunc)(int partition, UINT32 *offset, const void *buf, UINT32 size); 70 /* partition low-level erase func */ 71 int (*eraseFunc)(int partition, UINT32 offset, UINT32 size); 72 73 int readSize; /* size of a block read */ 74 int writeSize; /* size of a block write */ 75 int blockSize; /* size of an erasable block */ 76 int blockCount; /* number of partition blocks */ 77 int cacheSize; /* size of block caches */ 78 79 int partNo; /* partition number */ 80 int lookaheadSize; /* lookahead size */ 81 int blockCycles; /* block cycles */ 82 }; 83 84 /* 85 * @brief Divide the device into partitions. 86 * 87 * @param dev Device name, which customized by caller, it is recommended to 88 * name it as: "emmc0", "emmc1", "flash0", etc. The name is combined with 89 * "device_type" + "device_id", and the last character is device_id. 90 * device_id >= 0 && device_id <= 9. 91 * @param fsType Filesystem type. 92 * @param lengthArray List of partition size. For example: 93 * [0x10000000, 0x2000000], 256M and 512M partitions will be created and 94 * left all will not allocated. 95 * @param addrArray List of partition start addr, partition num same as lengthArray 96 * @param partNum Length of 'lengthArray'. 97 * 98 * @return Return LOS_NOK if error. Return LOS_OK if success. 99 * Partition naming rules: 100 * In the current vfs, after successfully calling the 'fdisk' hook, 101 * "partNum" partitions will be created. 102 * The partition naming rules is: 103 * The name is a combination of: 'deviceName'+'p'+'partitionId', 104 * such as "emmc0p0", "emmc0p1", "emmc0p2"... 105 */ 106 int LOS_DiskPartition(const char *dev, const char *fsType, int *lengthArray, int *addrArray, 107 int partNum); 108 109 /* 110 * @brief Format a partition. 111 * 112 * @param partName PartitionName, following the rule of fdisk hook. 113 * @param data For FatFs, the data indicates a pointer to a byte which 114 * specifies combination of FAT type flags, FM_FAT, FM_FAT32, FM_EXFAT and 115 * bitwise-or of these three, FM_ANY. 116 * 117 * @return Return LOS_NOK if error. Return LOS_OK if success. 118 */ 119 int LOS_PartitionFormat(const char *partName, char *fsType, void *data); 120 121 /* 122 * @brief new file system callbacks register. 123 * These callback functions are the adaptation layer implemented by the developer, 124 * used to interconnect the vfs with the new file system. 125 * 126 * LOS_FsRegister must be called after kernel initialization is complete. 127 * 128 * @param fsType file system type, don't register the same type fs more than once. 129 * @param fsMops mount operation of the fs. 130 * @param fsFops file operation of the fs. 131 * @param fsMgt management operation of the fs. 132 * 133 * @return Return LOS_OK if success. 134 * Return LOS_NOK if error. 135 * errno EINVAL: input errors, such as null pointers. 136 * errno ENOMEM: memory may malloc failed. 137 * 138 */ 139 int LOS_FsRegister(const char *fsType, const struct MountOps *fsMops, 140 const struct FileOps *fsFops, const struct FsManagement *fsMgt); 141 142 /* 143 * @brief Lock the whole filesystem to forbid filesystem access. 144 * 145 * @return Return LOS_NOK if error. Return LOS_OK if seccess. 146 */ 147 int LOS_FsLock(void); 148 149 /* 150 * @brief Unlock the whole filesystem to allow filesystem access. 151 */ 152 void LOS_FsUnlock(void); 153 154 #ifdef __cplusplus 155 #if __cplusplus 156 extern "C" { 157 #endif /* __cplusplus */ 158 #endif /* __cplusplus */ 159 160 #endif /* _LOS_FS_H_ */ 161