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