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