📄 fat.lst
字号:
C51 COMPILER V7.06 FAT 05/05/2005 12:12:07 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE FAT
OBJECT MODULE PLACED IN fat.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE modules\file\fat.c OPTIMIZE(7,SPEED) BROWSE INCDIR(.\modules\system;.\;.\;.
-\lib_refd) DEFINE(KEIL) DEBUG OBJECTEXTEND PRINT(.\fat.lst) OBJECT(fat.obj)
stmt level source
1 /*C**************************************************************************
2 * NAME: fat.c
3 *----------------------------------------------------------------------------
4 * Copyright (c) 2003 Atmel.
5 *----------------------------------------------------------------------------
6 * RELEASE: snd1c-refd-nf-4_0_3
7 * REVISION: 1.41
8 *----------------------------------------------------------------------------
9 * PURPOSE:
10 * FAT16/FAT12 file-system basics functions
11 *
12 * NOTES:
13 * Supports only the first partition
14 * Supports only 512 bytes sector size
15 * Supports only file fragmentation < MAX_FILE_FRAGMENT_NUMBER
16 * Supports only one file openned at a time
17 *
18 * Global Variables:
19 * - gl_buffer: array of bytes in pdata space
20 *****************************************************************************/
21
22 /*_____ I N C L U D E S ____________________________________________________*/
23
24 #include "config.h" /* system configuration */
25 #include "..\mem\hard.h" /* low level function definition */
26 #include "file.h" /* file function definition */
27 #include "fat.h" /* fat file-system definition */
28
29
30 /*_____ M A C R O S ________________________________________________________*/
31
32
33 /*_____ D E F I N I T I O N ________________________________________________*/
34
35 extern bit reserved_disk_space;
36 extern pdata Byte gl_buffer[];
37 extern xdata Byte fat_buf_sector[]; /* 512 bytes buffer */
38
39 extern char pdata *lfn_name; /* long filename limited to MAX_FILENAME_LEN chars */
40
41 /* disk management */
42 extern data Uint32 fat_ptr_fats; /* address of the first byte of FAT */
43 extern data Uint32 fat_ptr_rdir; /* address of the first byte of root dir */
44 extern data Uint32 fat_ptr_data; /* address of the first byte of data */
45 extern data Byte fat_cluster_size; /* cluster size (sector count) */
46 extern idata Byte fat_cluster_mask; /* mask for end of cluster test */
47
48 extern bdata bit dir_is_root; /* TRUE: point the root directory */
49 extern bdata bit fat_is_fat16; /* TRUE: FAT16 - FALSE: FAT12 */
50 extern bdata bit fat_open_mode; /* READ or WRITE */
51 extern bdata bit fat_2_is_present; /* TRUE: 2 FATs - FALSE: 1 FAT */
52 extern bdata bit flag_end_disk_file;
53
54
C51 COMPILER V7.06 FAT 05/05/2005 12:12:07 PAGE 2
55 extern xdata Uint32 fat_count_of_clusters;/* number of cluster - 2 */
56 extern xdata Uint16 fat_root_entry; /* position in root dir */
57 extern xdata Union32 fat_file_size;
58 extern xdata Uint16 fat_fat_size; /* FAT size in sector count */
59
60
61 /* directory management */
62 extern idata Uint32 fat_dir_current_sect; /* sector of selected entry in dir list */
63 extern xdata Uint16 fat_dir_list_index; /* index of current entry in dir list */
64 extern xdata Uint16 fat_dir_list_last; /* index of last entry in dir list */
65 extern idata Uint16 fat_dclust_byte_count;/* byte counter in directory sector */
66 extern idata Uint16 fat_dchain_index; /* the number of the fragment of the dir, in fact
67 the index of the table in the cluster chain */
68 extern idata Byte fat_dchain_nb_clust; /* the offset of the cluster from the first cluster
69 of the dir fragment */
70 extern xdata Uint32 fat_dir_start_sect; /* start sector of dir list */
71 extern xdata Uint16 fat_dir_current_offs; /* entry offset from fat_dir_current_sect */
72 extern xdata Byte fat_last_dclust_index;/* index of the last cluster in directory chain */
73 extern xdata fat_st_cache fat_cache; /* The cache structure, see the .h for more info */
74 extern xdata fat_st_clust_chain dclusters[MAX_DIR_FRAGMENT_NUMBER];
75 /* cluster chain for the current directory */
76 extern xdata char ext[3]; /* file extension (limited to 3 characters) */
77 #define fat_dir_entry_list fat_buf_sector /* manual variable overlay */
78
79
80 /* file management */
81 extern data Uint16 fat_fclust_byte_count;/* byte counter in file cluster */
82 extern idata Byte fat_last_clust_index; /* index of the last cluster in file chain */
83 extern idata Byte fat_fchain_index; /* the number of the fragment of the file, in fact
84 the index of the table in the cluster chain */
85 extern idata Uint16 fat_fchain_nb_clust; /* the offset of the cluster from the first cluster
86 of the file fragment */
87
88 extern xdata fat_st_clust_chain fclusters[MAX_FILE_FRAGMENT_NUMBER];
89 /* cluster chain for the current file */
90
91
92 /* Mode repeat A/B variables */
93 extern xdata Byte fat_fchain_index_save;
94 extern xdata Byte fat_fchain_nb_clust_save;
95 extern xdata Uint16 fat_fclust_byte_count_save;
96
97 static xdata fat_st_free_space free_space;
98
99 /*_____ D E C L A R A T I O N ______________________________________________*/
100
101 static void fat_get_dir_entry (fat_st_dir_entry xdata *);
102 static void fat_get_dir_file_list (Byte);
103 static bit fat_get_clusters (fat_st_clust_chain xdata *, Byte);
104 static bit fat_set_clusters (void);
105 static bit fat_dopen (void);
106 static bit fat_dseek (Int16);
107 static Byte fat_dgetc (void);
108
109 code Byte PBR_record_part1[] =
110 {
111 0xEB, 0x3C, 0x90, /* JMP instruction to boot code */
112 'O', 'E', 'M', ' ', 'N', 'A', 'M', 'E', /* OEM name */
113 SECTOR_SIZE, SECTOR_SIZE >> 8, /* number of bytes per sector */
114 0x00, /* number of sector per cluster */
115 NB_RESERVED, NB_RESERVED >> 8, /* number of reserved sector */
116 NB_FATS, /* number of FAT */
C51 COMPILER V7.06 FAT 05/05/2005 12:12:07 PAGE 3
117 NB_ROOT_ENTRY, NB_ROOT_ENTRY >> 8, /* number of root directory entries */
118 0x00, 0x00, /* total sectors if less than 65535 */
119 HARD_DISK, /* media byte */
120 };
121
122 code Byte PBR_record_part2[] =
123 {
124 FAT_DRIVE_NUMBER, /* Drive number */
125 0x00, /* not used */
126 FAT_EXT_SIGN, /* extended boot signature */
127 0x00, 0x00, 0x00, 0x00, /* volume ID */
128 'N', 'O', ' ', 'N', 'A', 'M', 'E', ' ', ' ', ' ', ' ', /* volume label */
129 'F', 'A', 'T', '1', 0x00, ' ', ' ', ' ', /* File system type in ASCII */
130
131 };
132
133 /*F**************************************************************************
134 * NAME: fat_load_sector
135 *----------------------------------------------------------------------------
136 * PARAMS:
137 *
138 * return:
139 *----------------------------------------------------------------------------
140 * PURPOSE:
141 * This function load a sector in fat_buf_sector
142 *----------------------------------------------------------------------------
143 * EXAMPLE:
144 *----------------------------------------------------------------------------
145 * NOTE:
146 *----------------------------------------------------------------------------
147 * REQUIREMENTS:
148 *
149 *****************************************************************************/
150 bit fat_load_sector(Uint32 sector)
151 {
152 1 Uint16 i;
153 1 if (Hard_read_open(sector) == OK)
154 1 {
155 2 for (i = 0; i < (SECTOR_SIZE); i++)
156 2 {
157 3 fat_buf_sector[i++] = Hard_read_byte();
158 3 fat_buf_sector[i++] = Hard_read_byte();
159 3 fat_buf_sector[i++] = Hard_read_byte();
160 3 fat_buf_sector[i] = Hard_read_byte();
161 3 }
162 2 Hard_read_close();
163 2 return OK;
164 2 }
165 1 else
166 1 {
167 2 return KO;
168 2 }
169 1 }
170
171 /*F**************************************************************************
172 * NAME: fat_install
173 *----------------------------------------------------------------------------
174 * PARAMS:
175 *
176 * return:
177 * - OK: intallation succeeded
178 * - KO: - partition 1 signature not recognized
C51 COMPILER V7.06 FAT 05/05/2005 12:12:07 PAGE 4
179 * - FAT type is not FAT12/FAT16
180 * - sector size is not 512 bytes
181 * - MBR or PBR signatures are not correct
182 * - low level read open failure
183 *----------------------------------------------------------------------------
184 * PURPOSE:
185 * Install the fat system, read mbr, bootrecords...
186 *----------------------------------------------------------------------------
187 * EXAMPLE:
188 *----------------------------------------------------------------------------
189 * NOTE:
190 * if MBR not found, try to mount unpartitionned FAT
191 * sector size is fixed to 512 bytes to simplify low level drivers
192 * fat_ptr_fats = partition offset + nb_reserved_sector
193 * fat_ptr_rdir = fat_ptr_fat + fat_size * nb_fat
194 * fat_ptr_data = fat_ptr_rdir + nb_root_entries * 32 / 512
195 *----------------------------------------------------------------------------
196 * REQUIREMENTS:
197 *****************************************************************************/
198 bit fat_install (void)
199 {
200 1 Uint32 fat_nb_sector;
201 1
202 1 /* MBR/PBR determination */
203 1 fat_ptr_fats = 1;
204 1 if (fat_load_sector(MBR_ADDRESS) == OK)
205 1 {
206 2
207 2 if ((fat_buf_sector[0] == 0xEB) && /* PBR Byte 0 */
208 2 (fat_buf_sector[2] == 0x90) && /* PBR Byte 2 */
209 2 ((fat_buf_sector[21] & 0xF0) == 0xF0)) /* PBR Byte 21 : Media byte */
210 2 {
211 3 if ((fat_buf_sector[510] != LOW(BR_SIGNATURE)) && /* check PBR signature */
212 3 (fat_buf_sector[511] != HIGH(BR_SIGNATURE)))
213 3 {
214 4 return KO;
215 4 }
216 3 else
217 3 {
218 4 fat_ptr_fats = 0x00000000; /* first sector is PBR */
219 4 }
220 3 }
221 2 else
222 2 { /* first sector is MBR */
223 3 if ((fat_buf_sector[446] != PARTITION_ACTIVE) &&
224 3 (fat_buf_sector[446] != 0x00))
225 3 {
226 4 return KO; /* not a MBR */
227 4 }
228 3 else
229 3 {
230 4 /* read partition offset (in sectors) at offset 8 */
231 4 ((Byte*)&fat_ptr_fats)[3] = fat_buf_sector[454];
232 4 ((Byte*)&fat_ptr_fats)[2] = fat_buf_sector[455];
233 4 ((Byte*)&fat_ptr_fats)[1] = fat_buf_sector[456];
234 4 ((Byte*)&fat_ptr_fats)[0] = fat_buf_sector[457];
235 4 if ((fat_buf_sector[510] != LOW(BR_SIGNATURE)) && /* check PBR signature */
236 4 (fat_buf_sector[511] != HIGH(BR_SIGNATURE)))
237 4 {
238 5 return KO;
239 5 }
240 4
C51 COMPILER V7.06 FAT 05/05/2005 12:12:07 PAGE 5
241 4 }
242 3
243 3 }
244 2 }
245 1 else
246 1 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -