• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _FATFS_H
33 #define _FATFS_H
34 
35 #include "ff.h"
36 #include "fs/file.h"
37 #include "disk.h"
38 #include "unistd.h"
39 #include "string.h"
40 #include "stdio.h"
41 #include "stdlib.h"
42 #include "time.h"
43 #include "sys/stat.h"
44 #include "sys/statfs.h"
45 
46 #ifdef __cplusplus
47 #if __cplusplus
48 extern "C" {
49 #endif /* __cplusplus */
50 #endif /* __cplusplus */
51 
52 #define MAX_LFNAME_LENGTH       256
53 #define LABEL_LEN              12
54 #define FAT_RESERVED_NUM       2
55 #define FAT32_MAXSIZE          0x100000000
56 #define BAD_CLUSTER            0x7FFFFFFF
57 #define DISK_ERROR             0xFFFFFFFF
58 #define FAT12_END_OF_CLUSTER   0x00000FFF
59 #define FAT16_END_OF_CLUSTER   0x0000FFFF
60 #define FAT32_END_OF_CLUSTER   0x0FFFFFFF
61 #define FAT_ERROR              (-1)
62 
63 /* MBR */
64 #define MBR_PRIMARY_PART_NUM 4
65 #define JUMP_CODE "\xEB\xFE\x90"
66 
67 /* Partition type */
68 #define FAT12                  0x01 /* FAT12 as primary partition in first physical 32MB */
69 #define FAT16                  0x04 /* FAT16 with less than 65536 sectors(32MB) */
70 #define EXTENDED_PARTITION_CHS  0x05
71 #define FAT16B                 0x06 /* FAT16B with 65536 or more sectors */
72 #define FAT32_CHS              0x0B
73 #define FAT32_LBA              0x0C
74 #define EXTENDED_PARTITION_LBA 0x0F
75 #define GPT_PROTECTIVE_MBR     0xEE
76 
77 /* volume boot record type */
78 #define VBR_FAT                0
79 #define VBR_BS_NOT_FAT         2
80 #define VBR_NOT_BS             3
81 #define VBR_DISK_ERR           4
82 
83 /* Limit and boundary */
84 #define FAT_MAX_CLUSTER_SIZE     64  /* (sectors) */
85 #define FAT32_MAX_CLUSTER_SIZE   128 /* (sectors) */
86 #define FAT32_ENTRY_SIZE         4 /* (bytes) */
87 #define FAT16_ENTRY_SIZE         2 /* (bytes) */
88 #define VOL_MIN_SIZE             128 /* (sectors) */
89 #define SFD_START_SECTOR         63
90 #define MAX_BLOCK_SIZE         32768 /* (sectors) */
91 
92 /* Sector */
93 #define FAT32_RESERVED_SECTOR  32
94 #define FAT_RESERVED_SECTOR    1
95 
96 #define DIR_NAME_LEN           11
97 #define DIR_READ_COUNT         7
98 
99 #define VOLUME_CHAR_LENGTH     4
100 
101 #define FAT_DEBUG
102 #ifdef FAT_DEBUG
103 #define FDEBUG(format, ...) do { \
104     PRINTK("[%s:%d]"format"\n", __func__, __LINE__, ##__VA_ARGS__); \
105 } while (0)
106 #else
107 #define FDEBUG(...)
108 #endif
109 
110 /* Format options (3rd argument of format) */
111 #define FMT_FAT      0x01
112 #define FMT_FAT32    0x02
113 #define FMT_ANY      0x07
114 #define FMT_ERASE    0x08
115 
116 extern char FatLabel[LABEL_LEN];
117 
118 int fatfs_2_vfs(int result);
119 int fatfs_lookup(struct Vnode *parent, const char *name, int len, struct Vnode **vpp);
120 int fatfs_create(struct Vnode *parent, const char *name, int mode, struct Vnode **vpp);
121 int fatfs_read(struct file *filep, char *buff, size_t count);
122 off_t fatfs_lseek64(struct file *filep, off64_t offset, int whence);
123 off64_t fatfs_lseek(struct file *filep, off_t offset, int whence);
124 int fatfs_write(struct file *filep, const char *buff, size_t count);
125 int fatfs_fsync(struct file *filep);
126 int fatfs_fallocate64(struct file *filep, int mode, off64_t offset, off64_t len);
127 int fatfs_fallocate(struct file *filep, int mode, off_t offset, off_t len);
128 int fatfs_truncate64(struct Vnode *vnode, off64_t len);
129 int fatfs_truncate(struct Vnode *vnode, off_t len);
130 int fatfs_mount(struct Mount *mount, struct Vnode *device, const void *data);
131 int fatfs_umount(struct Mount *mount, struct Vnode **device);
132 int fatfs_statfs(struct Mount *mount, struct statfs *info);
133 int fatfs_stat(struct Vnode *vnode, struct stat *buff);
134 int fatfs_chattr(struct Vnode *vnode, struct IATTR *attr);
135 int fatfs_opendir(struct Vnode *vnode, struct fs_dirent_s *idir);
136 int fatfs_readdir(struct Vnode *vnode, struct fs_dirent_s *idir);
137 int fatfs_rewinddir(struct Vnode *vnode, struct fs_dirent_s *dir);
138 int fatfs_closedir(struct Vnode *vnode, struct fs_dirent_s *dir);
139 int fatfs_rename(struct Vnode *oldvnode, struct Vnode *newparent, const char *oldname, const char *newname);
140 int fatfs_mkfs(struct Vnode *device, int sectors, int option);
141 int fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp);
142 int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name);
143 int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name);
144 int fatfs_ioctl(struct file *filep, int req, unsigned long arg);
145 int fatfs_fscheck(struct Vnode* vnode, struct fs_dirent_s *dir);
146 
147 FRESULT find_fat_partition(FATFS *fs, los_part *part, BYTE *format, QWORD *start_sector);
148 FRESULT init_fatobj(FATFS *fs, BYTE fmt, QWORD start_sector);
149 FRESULT _mkfs(los_part *partition, const MKFS_PARM *opt, BYTE *work, UINT len);
150 
151 #ifdef __cplusplus
152 #if __cplusplus
153 }
154 #endif /* __cplusplus */
155 #endif /* __cplusplus */
156 
157 #endif
158