📄 fat32.lst
字号:
C51 COMPILER V7.00 FAT32 01/06/2004 00:14:22 PAGE 1
C51 COMPILER V7.00, COMPILATION OF MODULE FAT32
OBJECT MODULE PLACED IN fat32.OBJ
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE fat32.c LARGE BROWSE INTVECTOR(0X8000) DEBUG OBJECTEXTEND TABS(2)
stmt level source
1 /*
2 Copyright (C) 2003 Bart Bilos <boombox666@yahoo.com>.
3
4 code adapted and modified from the yampp project
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 */
21
22 #include <stdio.h> /* prototype declarations for I/O functions */
23 #include <string.h> /* memory operations */
24 #include "IDE_LIB.h" /* IDE working variables */
25 #include "fat32.h"
26 #include "util.h"
27
28 // current BPB
29 Bpb CurrBPB;
30 // sector buffer
31 char sector_data[512];
32 // housekeeping vars
33 // first sector of FAT
34 unsigned long FirstFATSector;
35 // first data sector
36 unsigned long FirstDataSectorD;
37 // first directory cluster
38 unsigned long FirstDirCluster;
39 // how many sectors per cluster
40 unsigned int SecPerClust;
41
42 // fixes the BPB from big endian to little endian format
43 void fixBPB(Bpb *a_Bpb) {
44 1 a_Bpb->BPB_BytsPerSec = int_big_endian(a_Bpb->BPB_BytsPerSec);
45 1 a_Bpb->BPB_ResvdSecCnt = int_big_endian(a_Bpb->BPB_ResvdSecCnt);
46 1 a_Bpb->BPB_RootEntCnt = int_big_endian(a_Bpb->BPB_RootEntCnt);
47 1 a_Bpb->BPB_TotSec16 = int_big_endian(a_Bpb->BPB_TotSec16);
48 1 a_Bpb->BPB_FATSz16 = int_big_endian(a_Bpb->BPB_FATSz16);
49 1 a_Bpb->BPB_SecPerTrk = int_big_endian(a_Bpb->BPB_SecPerTrk);
50 1 a_Bpb->BPB_NumHeads = int_big_endian(a_Bpb->BPB_NumHeads);
51 1 a_Bpb->BPB_HiddSec = long_big_endian(a_Bpb->BPB_HiddSec);
52 1 a_Bpb->BPB_TotSec32 = long_big_endian(a_Bpb->BPB_TotSec32);
53 1 a_Bpb->BPB_FATSz32 = long_big_endian(a_Bpb->BPB_FATSz32);
54 1 a_Bpb->BPB_ExtFlags = int_big_endian(a_Bpb->BPB_ExtFlags);
55 1 a_Bpb->BPB_FSVer = int_big_endian(a_Bpb->BPB_FSVer);
C51 COMPILER V7.00 FAT32 01/06/2004 00:14:22 PAGE 2
56 1 a_Bpb->BPB_RootClus = long_big_endian(a_Bpb->BPB_RootClus);
57 1 a_Bpb->BPB_FSInfo = int_big_endian(a_Bpb->BPB_FSInfo);
58 1 a_Bpb->BPB_BkBootSec = int_big_endian(a_Bpb->BPB_BkBootSec);
59 1 a_Bpb->BS_VolID = long_big_endian(a_Bpb->BS_VolID);
60 1 }
61
62 // calculates the sector number from the cluster number
63 unsigned long clust2sect(unsigned long clust) {
64 1 return clust * SecPerClust + FirstDataSectorD;
65 1 }
66
67 // gives the cluster of the root dir
68 unsigned long rootdir() {
69 1 return FirstDirCluster;
70 1 }
71
72 // reads the harddrive MBR.
73 // locates the first partition.
74 // checks if its fat32
75 char init_fat32(void) {
76 1 Bootsector *startsect;
77 1 unsigned long int first_sector=0;
78 1 startsect = (Bootsector * ) sector_data;
79 1 // read sector 0
80 1 if(read_sector(first_sector,sector_data) != 0) {
81 2 return 1;
82 2 }
83 1 // locate partition and load BPB
84 1 first_sector = long_big_endian(startsect->BS_partition_start);
85 1 if(read_sector(first_sector,sector_data) != 0) {
86 2 return 1;
87 2 }
88 1 // copy relevant part of BPB from sector to the struct
89 1 memcpy(&CurrBPB,sector_data,90);
90 1 fixBPB(&CurrBPB);
91 1 FirstFATSector = CurrBPB.BPB_ResvdSecCnt + first_sector;
92 1 FirstDataSectorD = CurrBPB.BPB_ResvdSecCnt + (CurrBPB.BPB_NumFATs * CurrBPB.BPB_FATSz32) + first_sector;
- // This is FirstDataSector
93 1 FirstDirCluster = CurrBPB.BPB_RootClus;
94 1 SecPerClust = CurrBPB.BPB_SecPerClus;
95 1 FirstDataSectorD -= (2 * SecPerClust);
96 1 return 0;
97 1 }
98
99 // check if sector has suitable directory entries
100 char checksect(unsigned long a_sect_tocheck) {
101 1 read_sector(a_sect_tocheck,sector_data);
102 1 // just check if the first byte is non null
103 1 if(sector_data[0]!=0) return 1;
104 1 else return 0;
105 1 }
106
107 /*
108 // print out statistics about the specific entry
109 void FAT32entrydata(direntry *a_entry){
110 int i;
111 char fileentry[12];
112 for(i=0;i<12;i++)
113 fileentry[i] = a_entry->DE_Name[i];
114 fileentry[11]=0;
115 printf("%s ",fileentry);
116 if(ATTR_NORMAL & a_entry->DE_Attributes) {
C51 COMPILER V7.00 FAT32 01/06/2004 00:14:22 PAGE 3
117 printf("N");
118 } else {
119 printf("-");
120 }
121 if(ATTR_READONLY & a_entry->DE_Attributes) {
122 printf("R");
123 } else {
124 printf("-");
125 }
126 if(ATTR_HIDDEN & a_entry->DE_Attributes) {
127 printf("H");
128 } else {
129 printf("-");
130 }
131 if(ATTR_SYSTEM & a_entry->DE_Attributes) {
132 printf("S");
133 } else {
134 printf("-");
135 }
136 if(ATTR_VOLUME & a_entry->DE_Attributes) {
137 printf("V");
138 } else {
139 printf("-");
140 }
141 if(ATTR_LONG_FILENAME & a_entry->DE_Attributes) {
142 printf("L");
143 } else {
144 printf("-");
145 }
146 if(ATTR_ARCHIVE & a_entry->DE_Attributes) {
147 printf("A");
148 } else {
149 printf("-");
150 }
151 if(ATTR_DIRECTORY & a_entry->DE_Attributes) {
152 printf("D");
153 } else {
154 printf("-");
155 }
156 printf(" starting at %6x",int_big_endian(a_entry->DE_StartCluster));
157 printf(" %10lu bytes big \n",long_big_endian(a_entry->DE_FileSize));
158 }
159
160 // goes through an table at a_clust and returns the first directory it finds
161 // or 0 if the table has no directories
162 unsigned int traverse_dir(unsigned long int a_clust) {
163 direntry *de;
164 unsigned long nextdir=0;
165 int i;
166 unsigned long dwStartSector = clust2sect(a_clust);
167 while(checksect(dwStartSector)){
168 // sector read in and seems fine
169 de = (direntry * ) sector_data;
170 // reset sector pointer
171 for(i=0;i<16;i++){
172 if(de->DE_Name[0] != SLOT_EMPTY) {
173 FAT32entrydata(de);
174 if(ATTR_DIRECTORY&de->DE_Attributes) {
175 // check here for the . and .. directories
176 nextdir = int_big_endian(de->DE_StartCluster);
177 }
178 } else {
C51 COMPILER V7.00 FAT32 01/06/2004 00:14:22 PAGE 4
179 return nextdir;
180 }
181 de++;
182 }
183 dwStartSector++;
184 }
185 return nextdir;
186 }
187
188 */
189
190 // seek a file in the cluster, if found return the direntry
191 direntry *getfileentry(unsigned long a_cluster, char * filename) {
192 1 direntry *de;
193 1 unsigned long startsect = clust2sect(a_cluster);
194 1 int i;
195 1
196 1 while(checksect(startsect)){
197 2 de = (direntry * ) sector_data;
198 2 for(i=0;i<16;i++){
199 3 if(de->DE_Name[0] != SLOT_EMPTY) {
200 4 if(memcmp (de->DE_Name,filename,11) == 0) {
201 5 return de;
202 5 }
203 4 }
204 3 de++;
205 3 }
206 2 }
207 1 return NULL;
208 1 }
209
210 // amount of open files
211 filehandle filehandles[FILES+1] = 0;
212
213 // initialise the file system
214 void initfiles(void) {
215 1 unsigned char i;
216 1 // clear all start sectors so they will all be free
217 1 for (i = 0; i < FILES+1;i++)
218 1 filehandles[i].fh_startsect = 0;
219 1 }
220
221 // open file in current dir with FAT 11 char filenotation
222 unsigned char fopen (char *a_filename) {
223 1 direntry *file;
224 1 int i;
225 1 file = getfileentry(rootdir(),a_filename);
226 1 // has a file been found?
227 1 if(file == NULL)
228 1 // no exit and report
229 1 return 0;
230 1 else {
231 2 // yes, seek first empty entry
232 2 for(i = 1;i < FILES; i++) {
233 3 if(filehandles[i].fh_startsect == 0) {
234 4 // fill empty handle
235 4 filehandles[i].fh_startsect = clust2sect(int_big_endian(file->DE_StartCluster) | int_big_endian(fi
-le->DE_HighClust<<16));
236 4 filehandles[i].fh_fileptr = 0;
237 4 filehandles[i].fh_size = long_big_endian(file->DE_FileSize);
238 4 // return with handle number
239 4 return i;
C51 COMPILER V7.00 FAT32 01/06/2004 00:14:22 PAGE 5
240 4 }
241 3 }
242 2 return 0;
243 2 }
244 1 }
245
246 // position the file pointer
247 unsigned long fseek(unsigned char a_handle, unsigned long pos, char mode) {
248 1 if(a_handle < FILES+1) {
249 2 // set file pointer to pos
250 2 if(mode == 0) {
251 3 if(pos < filehandles[a_handle].fh_size) {
252 4 filehandles[a_handle].fh_fileptr = pos;
253 4 } else
254 3 return pos;
255 3 }
256 2 // set file pointer to end of file
257 2 if(mode == 1){
258 3 filehandles[a_handle].fh_fileptr = filehandles[a_handle].fh_size;
259 3 return filehandles[a_handle].fh_size;
260 3 }
261 2 }
262 1 else
263 1 return 0;
264 1 }
265
266 // close the handle
267 unsigned char fclose (unsigned char a_handle) {
268 1 if(a_handle < FILES+1) {
269 2 filehandles[a_handle].fh_startsect = 0;
270 2 }
271 1 else
272 1 return 0;
273 1 }
274
275 // read from file
276 unsigned char fread (unsigned char a_handle, char *a_buffer,unsigned int a_bytes) {
277 1 filehandle * currfile = & filehandles[a_handle];
278 1 int sects = a_bytes >> 9;
279 1 int rest = (a_bytes & 0x01FF);
280 1 long int startsect = currfile->fh_startsect;
281 1 long int size = currfile->fh_size;
282 1 long int bytesread=0;
283 1 // is the amount of bytes read greater than the total size
284 1 if((a_bytes + currfile->fh_fileptr) > currfile->fh_size)
285 1 // yes return
286 1 return 0;
287 1 // no proceed
288 1 for(sects;sects != 0;sects--){
289 2 // read whole multiples of 512
290 2 read_sector(startsect,sector_data);
291 2 memcpy(a_buffer,sector_data,512);
292 2 a_buffer = a_buffer + 512;
293 2 bytesread = bytesread + 512;
294 2 startsect++;
295 2 }
296 1 if(rest != 0){
297 2 // read the remainder
298 2 read_sector(startsect,sector_data);
299 2 memcpy(a_buffer,sector_data,rest);
300 2 bytesread = bytesread + rest;
301 2 startsect++;
C51 COMPILER V7.00 FAT32 01/06/2004 00:14:22 PAGE 6
302 2 }
303 1 // update filepointer
304 1 currfile->fh_fileptr = currfile->fh_fileptr + bytesread;
305 1 // return amount of data read
306 1 return bytesread;
307 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 2227 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 748 62
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -