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