10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#ifndef _FATFS_H
330d163575Sopenharmony_ci#define _FATFS_H
340d163575Sopenharmony_ci
350d163575Sopenharmony_ci#include "ff.h"
360d163575Sopenharmony_ci#include "fs/file.h"
370d163575Sopenharmony_ci#include "disk.h"
380d163575Sopenharmony_ci#include "unistd.h"
390d163575Sopenharmony_ci#include "string.h"
400d163575Sopenharmony_ci#include "stdio.h"
410d163575Sopenharmony_ci#include "stdlib.h"
420d163575Sopenharmony_ci#include "time.h"
430d163575Sopenharmony_ci#include "sys/stat.h"
440d163575Sopenharmony_ci#include "sys/statfs.h"
450d163575Sopenharmony_ci
460d163575Sopenharmony_ci#ifdef __cplusplus
470d163575Sopenharmony_ci#if __cplusplus
480d163575Sopenharmony_ciextern "C" {
490d163575Sopenharmony_ci#endif /* __cplusplus */
500d163575Sopenharmony_ci#endif /* __cplusplus */
510d163575Sopenharmony_ci
520d163575Sopenharmony_ci#define MAX_LFNAME_LENGTH       256
530d163575Sopenharmony_ci#define LABEL_LEN              12
540d163575Sopenharmony_ci#define FAT_RESERVED_NUM       2
550d163575Sopenharmony_ci#define FAT32_MAXSIZE          0x100000000
560d163575Sopenharmony_ci#define BAD_CLUSTER            0x7FFFFFFF
570d163575Sopenharmony_ci#define DISK_ERROR             0xFFFFFFFF
580d163575Sopenharmony_ci#define FAT12_END_OF_CLUSTER   0x00000FFF
590d163575Sopenharmony_ci#define FAT16_END_OF_CLUSTER   0x0000FFFF
600d163575Sopenharmony_ci#define FAT32_END_OF_CLUSTER   0x0FFFFFFF
610d163575Sopenharmony_ci#define FAT_ERROR              (-1)
620d163575Sopenharmony_ci
630d163575Sopenharmony_ci/* MBR */
640d163575Sopenharmony_ci#define MBR_PRIMARY_PART_NUM 4
650d163575Sopenharmony_ci#define JUMP_CODE "\xEB\xFE\x90"
660d163575Sopenharmony_ci
670d163575Sopenharmony_ci/* Partition type */
680d163575Sopenharmony_ci#define FAT12                  0x01 /* FAT12 as primary partition in first physical 32MB */
690d163575Sopenharmony_ci#define FAT16                  0x04 /* FAT16 with less than 65536 sectors(32MB) */
700d163575Sopenharmony_ci#define EXTENDED_PARTITION_CHS  0x05
710d163575Sopenharmony_ci#define FAT16B                 0x06 /* FAT16B with 65536 or more sectors */
720d163575Sopenharmony_ci#define FAT32_CHS              0x0B
730d163575Sopenharmony_ci#define FAT32_LBA              0x0C
740d163575Sopenharmony_ci#define EXTENDED_PARTITION_LBA 0x0F
750d163575Sopenharmony_ci#define GPT_PROTECTIVE_MBR     0xEE
760d163575Sopenharmony_ci
770d163575Sopenharmony_ci/* volume boot record type */
780d163575Sopenharmony_ci#define VBR_FAT                0
790d163575Sopenharmony_ci#define VBR_BS_NOT_FAT         2
800d163575Sopenharmony_ci#define VBR_NOT_BS             3
810d163575Sopenharmony_ci#define VBR_DISK_ERR           4
820d163575Sopenharmony_ci
830d163575Sopenharmony_ci/* Limit and boundary */
840d163575Sopenharmony_ci#define FAT_MAX_CLUSTER_SIZE     64  /* (sectors) */
850d163575Sopenharmony_ci#define FAT32_MAX_CLUSTER_SIZE   128 /* (sectors) */
860d163575Sopenharmony_ci#define FAT32_ENTRY_SIZE         4 /* (bytes) */
870d163575Sopenharmony_ci#define FAT16_ENTRY_SIZE         2 /* (bytes) */
880d163575Sopenharmony_ci#define VOL_MIN_SIZE             128 /* (sectors) */
890d163575Sopenharmony_ci#define SFD_START_SECTOR         63
900d163575Sopenharmony_ci#define MAX_BLOCK_SIZE         32768 /* (sectors) */
910d163575Sopenharmony_ci
920d163575Sopenharmony_ci/* Sector */
930d163575Sopenharmony_ci#define FAT32_RESERVED_SECTOR  32
940d163575Sopenharmony_ci#define FAT_RESERVED_SECTOR    1
950d163575Sopenharmony_ci
960d163575Sopenharmony_ci#define DIR_NAME_LEN           11
970d163575Sopenharmony_ci#define DIR_READ_COUNT         7
980d163575Sopenharmony_ci
990d163575Sopenharmony_ci#define VOLUME_CHAR_LENGTH     4
1000d163575Sopenharmony_ci
1010d163575Sopenharmony_ci#define FAT_DEBUG
1020d163575Sopenharmony_ci#ifdef FAT_DEBUG
1030d163575Sopenharmony_ci#define FDEBUG(format, ...) do { \
1040d163575Sopenharmony_ci    PRINTK("[%s:%d]"format"\n", __func__, __LINE__, ##__VA_ARGS__); \
1050d163575Sopenharmony_ci} while (0)
1060d163575Sopenharmony_ci#else
1070d163575Sopenharmony_ci#define FDEBUG(...)
1080d163575Sopenharmony_ci#endif
1090d163575Sopenharmony_ci
1100d163575Sopenharmony_ci/* Format options (3rd argument of format) */
1110d163575Sopenharmony_ci#define FMT_FAT      0x01
1120d163575Sopenharmony_ci#define FMT_FAT32    0x02
1130d163575Sopenharmony_ci#define FMT_ANY      0x07
1140d163575Sopenharmony_ci#define FMT_ERASE    0x08
1150d163575Sopenharmony_ci
1160d163575Sopenharmony_ciextern char FatLabel[LABEL_LEN];
1170d163575Sopenharmony_ci
1180d163575Sopenharmony_ciint fatfs_2_vfs(int result);
1190d163575Sopenharmony_ciint fatfs_lookup(struct Vnode *parent, const char *name, int len, struct Vnode **vpp);
1200d163575Sopenharmony_ciint fatfs_create(struct Vnode *parent, const char *name, int mode, struct Vnode **vpp);
1210d163575Sopenharmony_ciint fatfs_read(struct file *filep, char *buff, size_t count);
1220d163575Sopenharmony_cioff_t fatfs_lseek64(struct file *filep, off64_t offset, int whence);
1230d163575Sopenharmony_cioff64_t fatfs_lseek(struct file *filep, off_t offset, int whence);
1240d163575Sopenharmony_ciint fatfs_write(struct file *filep, const char *buff, size_t count);
1250d163575Sopenharmony_ciint fatfs_fsync(struct file *filep);
1260d163575Sopenharmony_ciint fatfs_fallocate64(struct file *filep, int mode, off64_t offset, off64_t len);
1270d163575Sopenharmony_ciint fatfs_fallocate(struct file *filep, int mode, off_t offset, off_t len);
1280d163575Sopenharmony_ciint fatfs_truncate64(struct Vnode *vnode, off64_t len);
1290d163575Sopenharmony_ciint fatfs_truncate(struct Vnode *vnode, off_t len);
1300d163575Sopenharmony_ciint fatfs_mount(struct Mount *mount, struct Vnode *device, const void *data);
1310d163575Sopenharmony_ciint fatfs_umount(struct Mount *mount, struct Vnode **device);
1320d163575Sopenharmony_ciint fatfs_statfs(struct Mount *mount, struct statfs *info);
1330d163575Sopenharmony_ciint fatfs_stat(struct Vnode *vnode, struct stat *buff);
1340d163575Sopenharmony_ciint fatfs_chattr(struct Vnode *vnode, struct IATTR *attr);
1350d163575Sopenharmony_ciint fatfs_opendir(struct Vnode *vnode, struct fs_dirent_s *idir);
1360d163575Sopenharmony_ciint fatfs_readdir(struct Vnode *vnode, struct fs_dirent_s *idir);
1370d163575Sopenharmony_ciint fatfs_rewinddir(struct Vnode *vnode, struct fs_dirent_s *dir);
1380d163575Sopenharmony_ciint fatfs_closedir(struct Vnode *vnode, struct fs_dirent_s *dir);
1390d163575Sopenharmony_ciint fatfs_rename(struct Vnode *oldvnode, struct Vnode *newparent, const char *oldname, const char *newname);
1400d163575Sopenharmony_ciint fatfs_mkfs(struct Vnode *device, int sectors, int option);
1410d163575Sopenharmony_ciint fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp);
1420d163575Sopenharmony_ciint fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name);
1430d163575Sopenharmony_ciint fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name);
1440d163575Sopenharmony_ciint fatfs_ioctl(struct file *filep, int req, unsigned long arg);
1450d163575Sopenharmony_ciint fatfs_fscheck(struct Vnode* vnode, struct fs_dirent_s *dir);
1460d163575Sopenharmony_ci
1470d163575Sopenharmony_ciFRESULT find_fat_partition(FATFS *fs, los_part *part, BYTE *format, QWORD *start_sector);
1480d163575Sopenharmony_ciFRESULT init_fatobj(FATFS *fs, BYTE fmt, QWORD start_sector);
1490d163575Sopenharmony_ciFRESULT _mkfs(los_part *partition, const MKFS_PARM *opt, BYTE *work, UINT len);
1500d163575Sopenharmony_ci
1510d163575Sopenharmony_ci#ifdef __cplusplus
1520d163575Sopenharmony_ci#if __cplusplus
1530d163575Sopenharmony_ci}
1540d163575Sopenharmony_ci#endif /* __cplusplus */
1550d163575Sopenharmony_ci#endif /* __cplusplus */
1560d163575Sopenharmony_ci
1570d163575Sopenharmony_ci#endif
158