📄 fat32.lst
字号:
C51 COMPILER V7.50 FAT32 11/27/2007 20:46:00 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE FAT32
OBJECT MODULE PLACED IN fat32.obj
COMPILER INVOKED BY: D:\Keil\C51\BIN\C51.EXE modules\file\fat32.c LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\fat32.lst) OBJ
-ECT(fat32.obj)
line level source
1 /*C**************************************************************************
2 * NAME: fat32.c
3 *----------------------------------------------------------------------------
4 * Copyright (c) 2003 Atmel.
5 *----------------------------------------------------------------------------
6 * RELEASE: snd1c-refd-nf-4_0_3
7 * REVISION: 1.4
8 *----------------------------------------------------------------------------
9 * PURPOSE:
10 * FAT32 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" /* fat32 file-system definition */
28 //#include "lib_mcu\serial\serial.h"
29
30 /*_____ M A C R O S ________________________________________________________*/
31
32
33 /*_____ D E F I N I T I O N ________________________________________________*/
34
35 extern pdata Byte gl_buffer[];
36
37 extern char pdata *lfn_name; /* long filename limited to MAX_FILENAME_LEN chars */
38 extern Uint16 song_name[11];
39
40 extern xdata Byte fat_buf_sector[]; /* 512 bytes buffer */
41
42 extern xdata Uint32 total_capacity;
43
44 /* disk management */
45 extern data Uint32 fat_ptr_fats; /* address of the first byte of FAT */
46 extern data Uint32 fat_ptr_data; /* address of the first byte of data */
47 extern data Byte fat_cluster_size; /* cluster size (sector count) */
48 extern idata Byte fat_cluster_mask; /* mask for end of cluster test */
49
50 extern bdata bit dir_is_root; /* TRUE: point the root directory */
51 extern bdata bit fat_is_fat16; /* TRUE: FAT16 - FALSE: FAT12 */
52 extern bdata bit fat_is_fat32; /* TRUE: FAT32 - FALSE: FAT12/FAT16 */
53 extern bdata bit fat_open_mode; /* READ or WRITE */
54 extern bdata bit fat_2_is_present; /* TRUE: 2 FATs - FALSE: 1 FAT */
C51 COMPILER V7.50 FAT32 11/27/2007 20:46:00 PAGE 2
55 extern bdata bit flag_end_disk_file;
56
57 extern xdata Uint32 fat_count_of_clusters;/* number of cluster - 2 */
58 extern xdata Union32 fat_file_size;
59 extern xdata Uint32 fat_fat_size; /* FAT size in sector count */
60
61
62 /* directory management */
63 extern xdata fat_st_clust_chain dclusters[MAX_DIR_FRAGMENT_NUMBER];
64 /* cluster chain for the current directory */
65 extern idata Uint16 fat_dclust_byte_count;/* byte counter in directory sector */
66
67 extern idata Uint16 fat_dchain_index; /* the number of the fragment of the dir, in fact
68 the index of the table in the cluster chain */
69 extern idata Byte fat_dchain_nb_clust; /* the offset of the cluster from the first cluster
70 of the dir fragment */
71 extern xdata Byte fat_last_dclust_index;/* index of the last cluster in directory chain */
72
73 extern xdata Uint16 fat_dir_list_index; /* index of current entry in dir list */
74 extern xdata Uint16 fat_dir_list_last; /* index of last entry in dir list */
75 extern xdata Uint32 fat_dir_start_sect; /* start sector of dir list */
76 extern idata Uint32 fat_dir_current_sect; /* sector of selected entry in dir list */
77 extern xdata Uint32 fat_dir_current_offs; /* entry offset from fat_dir_current_sect */
78
79 extern xdata fat_st_cache fat_cache; /* The cache structure, see the .h for more info */
80 extern xdata char ext[3]; /* file extension (limited to 3 characters) */
81
82
83 /* file management */
84 extern xdata fat_st_clust_chain fclusters[MAX_FILE_FRAGMENT_NUMBER];
85 /* cluster chain for the current file */
86 extern idata Byte fat_last_clust_index; /* index of the last cluster in file chain */
87
88 extern data Uint16 fat_fclust_byte_count;/* byte counter in file cluster */
89
90 extern idata Byte fat_fchain_index; /* the number of the fragment of the file, in fact
91 the index of the table in the cluster chain */
92
93 extern idata Uint16 fat_fchain_nb_clust; /* the offset of the cluster from the first cluster
94 of the file fragment */
95
96 extern xdata Uint32 fat_current_file_size;
97 extern xdata Uint32 fat_rootclus_fat32; /* root cluster address */
98 extern bdata bit fat_last_dir_cluster_full;
99 extern bdata bit fat_no_entries_free;
100 extern xdata Uint16 fat_total_clusters;
101 extern xdata Uint32 last_free_cluster;
102
103 /* Mode repeat A/B variables */
104 extern xdata Byte fat_fchain_index_save;
105 extern xdata Byte fat_fchain_nb_clust_save;
106 extern xdata Uint16 fat_fclust_byte_count_save;
107
108 extern xdata Byte current_ext;
109
110 extern idata Uint16 fat_current_end_entry_position;
111 extern idata Uint16 fat_current_start_entry_position;
112 extern xdata Uint16 fat_nb_deleted_entries;
113 extern xdata Uint16 fat_nb_total_entries;
114
115
116 /*_____ D E C L A R A T I O N ______________________________________________*/
C51 COMPILER V7.50 FAT32 11/27/2007 20:46:00 PAGE 3
117
118
119 void Hard_load_sector()
120 {
121 1 Uint16 i;
122 1
123 1 for (i = 0; i < (SECTOR_SIZE); i++)
124 1 {
125 2 fat_buf_sector[i++] = Hard_read_byte();
126 2 fat_buf_sector[i++] = Hard_read_byte();
127 2 fat_buf_sector[i++] = Hard_read_byte();
128 2 fat_buf_sector[i] = Hard_read_byte();
129 2 }
130 1 }
131 /*F**************************************************************************
132 * NAME: fat32_install
133 *----------------------------------------------------------------------------
134 * PARAMS:
135 *
136 * return:
137 * - OK: intallation succeeded
138 * - KO: - partition 1 not active
139 * - FAT type is not FAT16
140 * - sector size is not 512 bytes
141 * - MBR or PBR signatures are not correct
142 * - low level read open failure
143 *----------------------------------------------------------------------------
144 * PURPOSE:
145 * Install the fat system, read mbr, bootrecords...
146 *----------------------------------------------------------------------------
147 * EXAMPLE:
148 *----------------------------------------------------------------------------
149 * NOTE:
150 * if MBR not found, try to mount unpartitionned FAT
151 * sector size is fixed to 512 bytes to simplify low level drivers
152 * fat_ptr_fats = partition offset + nb_reserved_sector
153 *----------------------------------------------------------------------------
154 * REQUIREMENTS:
155 *****************************************************************************/
156 bit fat32_install (void)
157 {
158 1 Byte i;
159 1 Uint32 tot_sect;
160 1 Uint32 fat_nb_sector;
161 1 Uint16 bpb_rsvd_sec_cnt;
162 1 Byte bpb_num_fat;
163 1 Uint16 bpb_root_ent_count;
164 1
165 1 /* read and check usefull MBR info */
166 1 /* go to the first partition field */
167 1 Hard_read_open(MBR_ADDRESS);
168 1 Hard_load_sector();
169 1 Hard_read_close();
170 1
171 1 fat_ptr_fats = 0x01;
172 1 if ((fat_buf_sector[0] == 0xEB) && (fat_buf_sector[2] == 0x90)) /* Jump instruction to boot code */
173 1 {
174 2 if ((fat_buf_sector[21] & 0xF0) == 0xF0) /* Media byte */
175 2 {
176 3 if ((fat_buf_sector[510] == 0x55) && (fat_buf_sector[511] == 0xAA)) /* signature */
177 3 {
178 4 fat_ptr_fats = 0x00000000; /* disk may not be partitionned : first sector */
C51 COMPILER V7.50 FAT32 11/27/2007 20:46:00 PAGE 4
179 4 } /* is PBR */
180 3 else
181 3 {
182 4 return KO; /* no signature -> low level error */
183 4 }
184 3 }
185 2 }
186 1
187 1 if (fat_ptr_fats) /* if first sector is not a PBR */
188 1 {
189 2 if (Hard_read_open(MBR_ADDRESS) == OK)
190 2 {
191 3 for (i = 446/2; i != 0; i--)
192 3 {
193 4 Hard_read_byte(); /* go to first partition entry */
194 4 Hard_read_byte();
195 4 }
196 3 Hard_read_byte();
197 3 Hard_read_byte(); /* dummy reads */
198 3 Hard_read_byte();
199 3 Hard_read_byte();
200 3 Hard_read_byte();
201 3 Hard_read_byte();
202 3 Hard_read_byte();
203 3 Hard_read_byte();
204 3 /* read partition offset (in sectors) at offset 8 */
205 3 ((Byte*)&fat_ptr_fats)[3] = Hard_read_byte();
206 3 ((Byte*)&fat_ptr_fats)[2] = Hard_read_byte();
207 3 ((Byte*)&fat_ptr_fats)[1] = Hard_read_byte();
208 3 ((Byte*)&fat_ptr_fats)[0] = Hard_read_byte();
209 3 /* go to the MBR signature field */
210 3 for (i = 52; i != 0; i--)
211 3 {
212 4 Hard_read_byte(); /* dummy reads */
213 4 }
214 3 /* check MBR signature */
215 3 if ((Hard_read_byte() != LOW(BR_SIGNATURE)) &&
216 3 (Hard_read_byte() != HIGH(BR_SIGNATURE)))
217 3 {
218 4 fat_ptr_fats = 0x00000000; /* disk may not be partitionned */
219 4 }
220 3 Hard_read_close(); /* close physical read */
221 3 }
222 2 else
223 2 { /* low level error */
224 3 return KO;
225 3 }
226 2 }
227 1
228 1 /* read and check usefull PBR info */
229 1 if (Hard_read_open(fat_ptr_fats) == OK)
230 1 {
231 2 /* go to sector size field */
232 2 for (i = 11; i != 0; i--)
233 2 {
234 3 Hard_read_byte(); /* dummy reads */
235 3 }
236 2 /* read sector size (in bytes) */
237 2 if ((Hard_read_byte() != LOW(SECTOR_SIZE)) ||
238 2 (Hard_read_byte() != HIGH(SECTOR_SIZE)))
239 2 {
240 3 Hard_read_close(); /* close physical read */
C51 COMPILER V7.50 FAT32 11/27/2007 20:46:00 PAGE 5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -