550W Document
550W, a high-end OS
fat32.h
浏览该文件的文档.
1 #pragma once
2 
3 #include <common/types.h>
4 
5 #define max(x, y) (((x) > (y)) ? (x) : (y))
6 #define min(x, y) (((x) > (y)) ? (y) : (x))
7 
8 #define FAT32_BOOT_SEC 0
9 
10 #define BPB_BYTES_PER_SEC 0x0B
11 #define BPB_SEC_PER_CLUSTER 0x0D
12 #define BPB_RSVD_SEC_CNT 0x0E
13 #define BPB_FAT_NUM 0x10
14 #define BPB_HIDD_SEC 0x1C
15 #define BPB_TOTL_SEC 0x20
16 #define BPB_SEC_PER_FAT 0x24
17 #define BPB_ROOT_CLUSTER 0x2c
18 
19 typedef struct fat32 {
20  struct {
21  uint16_t bytes_per_sec;
22  uint8_t sec_per_cluster;
23  uint16_t rsvd_sec_cnt; /* count of reserved sectors */
24  uint8_t fat_num; /* count of FAT regions */
25  uint32_t hidd_sec; /* count of hidden sectors */
26  uint32_t totl_sec; /* total count of sectors including all regions */
27  uint32_t sec_per_fat; /* count of sectors for a FAT region */
28  uint32_t root_cluster; /* cluster num of root dir, typically is 2 */
29  } bpb;
30  uint32_t first_data_sec; /* root dir sec */
31  uint32_t data_sec_cnt;
32  uint32_t data_cluster_cnt; /* num of data clusters */
34  uint32_t entry_per_sec; /* entry num per fat sec */
35  uint32_t free_clusters; /* num of rest clusters */
36  uint32_t next_free_cluster; /* sequentially quick alloc, need a flag(TODO:reuse of hole in SDcard) */
37  uint32_t next_free_fat_sec; /* get fat sec quickly */
39 
40 extern fat32_t fat;
41 
42 #define ATTR_READ_WRITE 0x00
43 #define ATTR_READ_ONLY 0x01
44 #define ATTR_HIDDEN 0x02
45 #define ATTR_SYSTEM 0x04
46 #define ATTR_VOLUME_ID 0x08
47 #define ATTR_DIRECTORY 0x10
48 #define ATTR_ARCHIVE 0x20
49 #define ATTR_LONG_FILE_NAME 0x0F
50 // TODO : for future use?
51 #define ATTR_LINK 0x40
52 #define ATTR_CHARACTER_DEVICE 0x80
53 
54 #define SHORT_FIR_NAME 8
55 #define SHORT_EXT_NAME 3
56 #define MAX_SHORT_NAME 11
57 #define MAX_NAME_LEN 256
58 #define MAX_PATH_LEN 512 /* TODO : is there any limitation? */
59 #define DENTRY_LEN 32
60 // TODO: get_file_name, file name in char* and num increased of next entry
61 typedef struct short_name_entry {
64  uint8_t attr;
65  uint8_t nt_res;
66  uint8_t crt_time_tenth; // 39-32 of access
67  uint16_t crt_time; // 31-16 of access
68  uint16_t crt_date; // 15-0 of access
69  uint16_t lst_acce_date; // 47-32 of modify
70  uint16_t fst_clus_hi;
71  uint16_t lst_wrt_time; // 31-16 of modify
72  uint16_t lst_wrt_date; // 15-0 of modify
73  uint16_t fst_clus_lo;
74  uint32_t file_size;
75 } __attribute__((packed, aligned(4))) short_name_entry_t;
76 
77 #define LONG_NAME1_LEN 5
78 #define LONG_NAME2_LEN 6
79 #define LONG_NAME3_LEN 2
80 #define LONG_NAME_LEN (LONG_NAME1_LEN + LONG_NAME2_LEN + LONG_NAME3_LEN)
81 #define LAST_LONG_ENTRY 0x40
82 #define LONG_ENTRY_SEQ 0x1f
83 typedef struct long_name_entry {
84  uint8_t order; /* we used this as the order of next name part, 255 means end*/
85  uint16_t name1[LONG_NAME1_LEN];
86  uint8_t attr;
87  uint8_t type;
88  uint8_t checksum;
89  uint16_t name2[LONG_NAME2_LEN];
90  uint16_t rsvd;
91  uint16_t name3[LONG_NAME3_LEN];
92 } __attribute__((packed, aligned(4))) long_name_entry_t;
93 
94 typedef union dentry {
95  short_name_entry_t sn;
96  long_name_entry_t ln;
98 
99 typedef struct dir_info {
100  uint32_t first_cluster;
101  uint32_t size;
102  char name[MAX_PATH_LEN]; // real path
104 
106 
107 /* TODO : file descriptor?*/
108 
109 /*
110  * translate cluster to fisrt sec
111  */
112 static inline uint32_t fat_cluster2sec(uint32_t cluster) {
113  return ((cluster - 2) * fat.bpb.sec_per_cluster) + fat.first_data_sec;
114 }
115 
116 /*
117  * translate sec to cluster belonged to
118  */
119 static inline uint32_t fat_sec2cluster(uint32_t sec) {
120  return (sec - fat.first_data_sec) / fat.bpb.sec_per_cluster + 2;
121 }
122 
123 static inline uint32 fat32_dentry2fcluster(dentry_t *p) {
124  return ((uint32_t)p->sn.fst_clus_hi << 16) + p->sn.fst_clus_lo;
125 }
126 
127 static inline void fat32_fcluster2dentry(dentry_t *p, uint32 cluster) {
128  p->sn.fst_clus_hi = (cluster >> 16) & ((1lu << 16) - 1);
129  p->sn.fst_clus_lo = cluster & ((1lu << 16) - 1);
130 }
131 
132 #define FAT_MASK 0x0fffffffu
133 #define FAT_MAX 0x0ffffff8u /* end of the file */
134 #define FAT_BAD 0x0ffffff7u /* bad cluster of SDcard */
135 #define FAT_ENTRY_SIZE 4
136 
137 /*
138  * translate cluster to sec and offset of fat1(TODO: no use of fat2)
139  */
140 static inline uint32_t fat_cluster2fatsec(uint32_t cluster) {
141  return cluster / fat.entry_per_sec + fat.bpb.hidd_sec + fat.bpb.rsvd_sec_cnt;
142 }
143 
144 static inline uint32_t fat_cluster2fatoff(uint32_t cluster) {
145  return cluster % fat.entry_per_sec;
146 }
147 
148 // static inline uint32_t fat_fcluster2size(uint32_t first){
149 // uint32_t ftable[fat.bpb.bytes_per_sec];
150 // uint32_t size = 0;
151 // uint32_t cur_fat_sec = 0xffffffff;
152 // do{
153 // if(cur_fat_sec == 0xffffffff || fat_cluster2fatsec(first) != cur_fat_sec){
154 // cur_fat_sec = fat_cluster2fatsec(first);
155 // // TODO:
156 // //fseek(fp,cur_fat_sec*fat.bpb.bytes_per_sec,0);
157 // //fread(ftable,fat.bpb.bytes_per_sec,1,fp);
158 
159 // }
160 // first = ftable[fat_cluster2fatoff(first)] & FAT_MASK;
161 // size += fat.bpb.sec_per_cluster;
162 // }while(first < FAT_MAX);
163 // size = size * fat.bpb.bytes_per_sec;
164 // return size;
165 // }
166 
167 // utils
168 static inline char unicode2char(uint16_t unich) {
169  return (unich >= 65 && unich <= 90) ? unich - 65 + 'A' : (unich >= 48 && unich <= 57) ? unich - 48 + '0' : (unich >= 97 && unich <= 122) ? unich - 97 + 'a' : (unich == 95) ? '_' : (unich == 46) ? '.' : (unich == 0x20) ? ' ' : (unich == 45) ? '-' : 0;
170 }
171 static inline uint16_t char2unicode(char ch) {
172  return (ch >= 'A' && ch <= 'Z') ? 65 + ch - 'A' : (ch >= 'a' && ch <= 'z') ? 97 + ch - 'a' : (ch >= '0' && ch <= '9') ? 48 + ch - '0' : (ch == '_') ? 95 : (ch == '.') ? 46 : (ch == ' ') ? 0x20 : (ch == '-') ? 45 : 0;
173 }
#define LONG_NAME3_LEN
Definition: fat32.h:79
struct short_name_entry __attribute__((packed, aligned(4))) short_name_entry_t
#define LONG_NAME1_LEN
Definition: fat32.h:77
#define SHORT_EXT_NAME
Definition: fat32.h:55
#define SHORT_FIR_NAME
Definition: fat32.h:54
dir_info_t cur_dir
Definition: fat32.h:105
struct fat32 fat32_t
struct dir_info dir_info_t
dir_info_t root_dir
Definition: fs.c:20
#define MAX_PATH_LEN
Definition: fat32.h:58
union dentry dentry_t
fat32_t fat
Definition: fs.c:19
#define LONG_NAME2_LEN
Definition: fat32.h:78
Definition: fat32.h:99
uint32_t size
Definition: fat32.h:101
char name[MAX_PATH_LEN]
Definition: fat32.h:102
uint32_t first_cluster
Definition: fat32.h:100
Definition: fat32.h:19
uint32_t data_sec_cnt
Definition: fat32.h:31
uint32_t root_cluster
Definition: fat32.h:28
uint32_t next_free_cluster
Definition: fat32.h:36
uint32_t hidd_sec
Definition: fat32.h:25
struct fat32::@2 bpb
uint32_t sec_per_fat
Definition: fat32.h:27
uint32_t entry_per_sec
Definition: fat32.h:34
uint32_t first_data_sec
Definition: fat32.h:30
uint32_t bytes_per_cluster
Definition: fat32.h:33
uint8_t sec_per_cluster
Definition: fat32.h:22
uint8_t fat_num
Definition: fat32.h:24
uint16_t rsvd_sec_cnt
Definition: fat32.h:23
uint32_t next_free_fat_sec
Definition: fat32.h:37
uint32_t free_clusters
Definition: fat32.h:35
uint16_t bytes_per_sec
Definition: fat32.h:21
uint32_t data_cluster_cnt
Definition: fat32.h:32
uint32_t totl_sec
Definition: fat32.h:26
Definition: fat32.h:83
uint16_t rsvd
Definition: fat32.h:90
uint8_t type
Definition: fat32.h:87
uint16_t name2[LONG_NAME2_LEN]
Definition: fat32.h:89
uint16_t name1[LONG_NAME1_LEN]
Definition: fat32.h:85
uint16_t name3[LONG_NAME3_LEN]
Definition: fat32.h:91
uint8_t order
Definition: fat32.h:84
uint8_t checksum
Definition: fat32.h:88
uint8_t attr
Definition: fat32.h:86
Definition: fat32.h:61
uint16_t fst_clus_hi
Definition: fat32.h:70
char name2[SHORT_EXT_NAME]
Definition: fat32.h:63
uint16_t lst_wrt_time
Definition: fat32.h:71
uint8_t attr
Definition: fat32.h:64
uint16_t crt_time
Definition: fat32.h:67
uint8_t crt_time_tenth
Definition: fat32.h:66
uint16_t crt_date
Definition: fat32.h:68
uint32_t file_size
Definition: fat32.h:74
uint16_t fst_clus_lo
Definition: fat32.h:73
uint8_t nt_res
Definition: fat32.h:65
uint16_t lst_acce_date
Definition: fat32.h:69
char name1[SHORT_FIR_NAME]
Definition: fat32.h:62
uint16_t lst_wrt_date
Definition: fat32.h:72
unsigned int uint32
Definition: types.h:37
Definition: fat32.h:94
short_name_entry_t sn
Definition: fat32.h:95
long_name_entry_t ln
Definition: fat32.h:96