]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - vtoycli/fat_io_lib/include/fat_access.h
Only for VirtualBox.
[Ventoy.git] / vtoycli / fat_io_lib / include / fat_access.h
1 #ifndef __FAT_ACCESS_H__
2 #define __FAT_ACCESS_H__
3
4 #include "fat_defs.h"
5 #include "fat_opts.h"
6
7 //-----------------------------------------------------------------------------
8 // Defines
9 //-----------------------------------------------------------------------------
10 #define FAT_INIT_OK 0
11 #define FAT_INIT_MEDIA_ACCESS_ERROR (-1)
12 #define FAT_INIT_INVALID_SECTOR_SIZE (-2)
13 #define FAT_INIT_INVALID_SIGNATURE (-3)
14 #define FAT_INIT_ENDIAN_ERROR (-4)
15 #define FAT_INIT_WRONG_FILESYS_TYPE (-5)
16 #define FAT_INIT_WRONG_PARTITION_TYPE (-6)
17 #define FAT_INIT_STRUCT_PACKING (-7)
18
19 #define FAT_DIR_ENTRIES_PER_SECTOR (FAT_SECTOR_SIZE / FAT_DIR_ENTRY_SIZE)
20
21 //-----------------------------------------------------------------------------
22 // Function Pointers
23 //-----------------------------------------------------------------------------
24 typedef int (*fn_diskio_read) (uint32 sector, uint8 *buffer, uint32 sector_count);
25 typedef int (*fn_diskio_write)(uint32 sector, uint8 *buffer, uint32 sector_count);
26
27 //-----------------------------------------------------------------------------
28 // Structures
29 //-----------------------------------------------------------------------------
30 struct disk_if
31 {
32 // User supplied function pointers for disk IO
33 fn_diskio_read read_media;
34 fn_diskio_write write_media;
35 };
36
37 // Forward declaration
38 struct fat_buffer;
39
40 struct fat_buffer
41 {
42 uint8 sector[FAT_SECTOR_SIZE * FAT_BUFFER_SECTORS];
43 uint32 address;
44 int dirty;
45 uint8 * ptr;
46
47 // Next in chain of sector buffers
48 struct fat_buffer *next;
49 };
50
51 typedef enum eFatType
52 {
53 FAT_TYPE_16,
54 FAT_TYPE_32
55 } tFatType;
56
57 struct fatfs
58 {
59 // Filesystem globals
60 uint8 sectors_per_cluster;
61 uint32 cluster_begin_lba;
62 uint32 rootdir_first_cluster;
63 uint32 rootdir_first_sector;
64 uint32 rootdir_sectors;
65 uint32 fat_begin_lba;
66 uint16 fs_info_sector;
67 uint32 lba_begin;
68 uint32 fat_sectors;
69 uint32 next_free_cluster;
70 uint16 root_entry_count;
71 uint16 reserved_sectors;
72 uint8 num_of_fats;
73 tFatType fat_type;
74
75 // Disk/Media API
76 struct disk_if disk_io;
77
78 // [Optional] Thread Safety
79 void (*fl_lock)(void);
80 void (*fl_unlock)(void);
81
82 // Working buffer
83 struct fat_buffer currentsector;
84
85 // FAT Buffer
86 struct fat_buffer *fat_buffer_head;
87 struct fat_buffer fat_buffers[FAT_BUFFERS];
88 };
89
90 struct fs_dir_list_status
91 {
92 uint32 sector;
93 uint32 cluster;
94 uint8 offset;
95 };
96
97 struct fs_dir_ent
98 {
99 char filename[FATFS_MAX_LONG_FILENAME];
100 uint8 is_dir;
101 uint32 cluster;
102 uint32 size;
103
104 #if FATFS_INC_TIME_DATE_SUPPORT
105 uint16 access_date;
106 uint16 write_time;
107 uint16 write_date;
108 uint16 create_date;
109 uint16 create_time;
110 #endif
111 };
112
113 //-----------------------------------------------------------------------------
114 // Prototypes
115 //-----------------------------------------------------------------------------
116 int fatfs_init(struct fatfs *fs);
117 uint32 fatfs_lba_of_cluster(struct fatfs *fs, uint32 Cluster_Number);
118 int fatfs_sector_reader(struct fatfs *fs, uint32 Startcluster, uint32 offset, uint8 *target);
119 int fatfs_sector_read(struct fatfs *fs, uint32 lba, uint8 *target, uint32 count);
120 int fatfs_sector_write(struct fatfs *fs, uint32 lba, uint8 *target, uint32 count);
121 int fatfs_read_sector(struct fatfs *fs, uint32 cluster, uint32 sector, uint8 *target);
122 int fatfs_write_sector(struct fatfs *fs, uint32 cluster, uint32 sector, uint8 *target);
123 void fatfs_show_details(struct fatfs *fs);
124 uint32 fatfs_get_root_cluster(struct fatfs *fs);
125 uint32 fatfs_get_file_entry(struct fatfs *fs, uint32 Cluster, char *nametofind, struct fat_dir_entry *sfEntry);
126 int fatfs_sfn_exists(struct fatfs *fs, uint32 Cluster, char *shortname);
127 int fatfs_update_file_length(struct fatfs *fs, uint32 Cluster, char *shortname, uint32 fileLength);
128 int fatfs_mark_file_deleted(struct fatfs *fs, uint32 Cluster, char *shortname);
129 void fatfs_list_directory_start(struct fatfs *fs, struct fs_dir_list_status *dirls, uint32 StartCluster);
130 int fatfs_list_directory_next(struct fatfs *fs, struct fs_dir_list_status *dirls, struct fs_dir_ent *entry);
131 int fatfs_update_timestamps(struct fat_dir_entry *directoryEntry, int create, int modify, int access);
132
133 #endif