更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
fatfs.h
浏览该文件的文档.
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
48extern "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/* Partiton 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
116extern char FatLabel[LABEL_LEN];
117
118int fatfs_2_vfs(int result);
119int fatfs_lookup(struct Vnode *parent, const char *name, int len, struct Vnode **vpp);
120int fatfs_create(struct Vnode *parent, const char *name, int mode, struct Vnode **vpp);
121int fatfs_read(struct file *filep, char *buff, size_t count);
122off_t fatfs_lseek64(struct file *filep, off64_t offset, int whence);
123off64_t fatfs_lseek(struct file *filep, off_t offset, int whence);
124int fatfs_write(struct file *filep, const char *buff, size_t count);
125int fatfs_fsync(struct file *filep);
126int fatfs_fallocate64(struct file *filep, int mode, off64_t offset, off64_t len);
127int fatfs_fallocate(struct file *filep, int mode, off_t offset, off_t len);
128int fatfs_truncate64(struct Vnode *vnode, off64_t len);
129int fatfs_truncate(struct Vnode *vnode, off_t len);
130int fatfs_mount(struct Mount *mount, struct Vnode *device, const void *data);
131int fatfs_umount(struct Mount *mount, struct Vnode **device);
132int fatfs_statfs(struct Mount *mount, struct statfs *info);
133int fatfs_stat(struct Vnode *vnode, struct stat *buff);
134int fatfs_chattr(struct Vnode *vnode, struct IATTR *attr);
135int fatfs_opendir(struct Vnode *vnode, struct fs_dirent_s *idir);
136int fatfs_readdir(struct Vnode *vnode, struct fs_dirent_s *idir);
137int fatfs_rewinddir(struct Vnode *vnode, struct fs_dirent_s *dir);
138int fatfs_closedir(struct Vnode *vnode, struct fs_dirent_s *dir);
139int fatfs_rename(struct Vnode *oldvnode, struct Vnode *newparent, const char *oldname, const char *newname);
140int fatfs_mkfs (struct Vnode *device, int sectors, int option);
141int fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp);
142int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name);
143int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name);
144int fatfs_ioctl(struct file *filep, int req, unsigned long arg);
145int fatfs_fscheck(struct Vnode* vnode, struct fs_dirent_s *dir);
146
147FRESULT find_fat_partition(FATFS *fs, los_part *part, BYTE *format, QWORD *start_sector);
148FRESULT init_fatobj(FATFS *fs, BYTE fmt, QWORD start_sector);
149FRESULT _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
int fatfs_stat(struct Vnode *vnode, struct stat *buff)
Definition: fatfs.c:1439
int fatfs_statfs(struct Mount *mount, struct statfs *info)
Definition: fatfs.c:1339
int fatfs_closedir(struct Vnode *vnode, struct fs_dirent_s *dir)
Definition: fatfs.c:1662
int fatfs_rewinddir(struct Vnode *vnode, struct fs_dirent_s *dir)
Definition: fatfs.c:1645
int fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp)
Definition: fatfs.c:1941
FRESULT find_fat_partition(FATFS *fs, los_part *part, BYTE *format, QWORD *start_sector)
char FatLabel[LABEL_LEN]
Definition: format.c:41
int fatfs_fsync(struct file *filep)
Definition: fatfs.c:896
int fatfs_2_vfs(int result)
Definition: fatfs.c:96
FRESULT _mkfs(los_part *partition, const MKFS_PARM *opt, BYTE *work, UINT len)
int fatfs_rename(struct Vnode *oldvnode, struct Vnode *newparent, const char *oldname, const char *newname)
Definition: fatfs.c:1698
int fatfs_ioctl(struct file *filep, int req, unsigned long arg)
Definition: fatfs.c:2049
int fatfs_fscheck(struct Vnode *vnode, struct fs_dirent_s *dir)
Definition: fatfs.c:2122
int fatfs_chattr(struct Vnode *vnode, struct IATTR *attr)
Definition: fatfs.c:1494
FRESULT init_fatobj(FATFS *fs, BYTE fmt, QWORD start_sector)
int fatfs_readdir(struct Vnode *vnode, struct fs_dirent_s *idir)
Definition: fatfs.c:1589
int fatfs_lookup(struct Vnode *parent, const char *name, int len, struct Vnode **vpp)
fat文件系统对 Lookup 接口的实现
Definition: fatfs.c:516
int fatfs_create(struct Vnode *parent, const char *name, int mode, struct Vnode **vpp)
创建 fat vnode 节点
Definition: fatfs.c:614
int fatfs_fallocate64(struct file *filep, int mode, off64_t offset, off64_t len)
Definition: fatfs.c:913
int fatfs_mount(struct Mount *mount, struct Vnode *device, const void *data)
fat将分区文件系统挂载 举例: mount /dev/mmcblk0p0 /bin1/vs/sd vfat
Definition: fatfs.c:1103
int fatfs_fallocate(struct file *filep, int mode, off_t offset, off_t len)
Definition: fatfs.c:1010
off64_t fatfs_lseek(struct file *filep, off_t offset, int whence)
Definition: fatfs.c:832
int fatfs_read(struct file *filep, char *buff, size_t count)
Definition: fatfs.c:697
off_t fatfs_lseek64(struct file *filep, off64_t offset, int whence)
Definition: fatfs.c:753
int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name)
Definition: fatfs.c:1946
int fatfs_mkfs(struct Vnode *device, int sectors, int option)
Definition: fatfs.c:1885
int fatfs_write(struct file *filep, const char *buff, size_t count)
Definition: fatfs.c:858
int fatfs_umount(struct Mount *mount, struct Vnode **device)
Definition: fatfs.c:1254
int fatfs_truncate64(struct Vnode *vnode, off64_t len)
Definition: fatfs.c:1015
int fatfs_truncate(struct Vnode *vnode, off_t len)
Definition: fatfs.c:1058
int fatfs_opendir(struct Vnode *vnode, struct fs_dirent_s *idir)
Definition: fatfs.c:1554
int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name)
Definition: fatfs.c:2008
int format(const char *dev, int sectors, int option)
formatting sd card
Definition: format.c:44
此结构用于记录 vnode 的属性
Definition: vnode.h:81
举例: mount /dev/mmcblk0p0 /bin1/vs/sd vfat 将/dev/mmcblk0p0 挂载到/bin1/vs/sd目录
Definition: mount.h:68
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 ARG_NUM_0 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_4 off64_t
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 ARG_NUM_0 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_6 ARG_NUM_3 ARG_NUM_5 ARG_NUM_7 ARG_NUM_1 ARG_NUM_4 ARG_NUM_5 ARG_NUM_4 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_7 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_7 ARG_NUM_3 ARG_NUM_2 char ARG_NUM_2 ARG_NUM_1 ARG_NUM_0 ARG_NUM_0 ARG_NUM_3 void ARG_NUM_1 ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_5 ARG_NUM_3 ARG_NUM_3 ARG_NUM_4 ARG_NUM_1 ARG_NUM_1 ARG_NUM_3 ARG_NUM_2 mode_t