📄 fs.lst
字号:
ARM COMPILER V2.42, fs 27/03/06 10:45:50 PAGE 1
ARM COMPILER V2.42, COMPILATION OF MODULE fs
OBJECT MODULE PLACED IN .\obj\fs.obj
COMPILER INVOKED BY: C:\Keil\ARM\BIN\CA.exe src\fs.c THUMB DEBUG PRINT(.\LST\FS.LST) TABS(4) OBJECT(.\obj\fs.obj)
stmt level source
1 /*****************************************************************************\
2 * efs - General purpose Embedded Filesystem library *
3 * --------------------- ----------------------------------- *
4 * *
5 * Filename : fs.c *
6 * Description : These are general filesystem functions, supported by the *
7 * functions of dir.c and fat.c file.c uses these functions *
8 * heavily, but is not used by fs.c (not true anymore) *
9 * *
10 * This library is free software; you can redistribute it and/or *
11 * modify it under the terms of the GNU Lesser General Public *
12 * License as published by the Free Software Foundation; either *
13 * version 2.1 of the License, or (at your option) any later version. *
14 * *
15 * This library is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * Lesser General Public License for more details. *
19 * *
20 * (c)2004 Lennart Yseboodt *
21 * (c)2004 Michael De Nil *
22 \*****************************************************************************/
23
24 /*****************************************************************************/
25 #include "fs.h"
26 #include "fat.h"
27 #include "dir.h"
28 /*****************************************************************************/
29
30 /* ****************************************************************************
31 * eint16 fs_initFs(FileSystem *fs,Partition *part)
32 * Description: This functions glues the initialisation of the filesystem together.
33 * It loads the volumeID, computes the FS type and searches for the rootsector.
34 * Return value: Returns 0 on succes and -1 on error (if magic code is wrong)
35 */
36 eint16 fs_initFs(FileSystem *fs,Partition *part)
37 {
38 1 if(!fs_isValidFat(part)){
39 2 return(-1);
40 2 }
41 1 fs->part=part;
42 1 fs_loadVolumeId(fs,part);
43 1 if(!fs_verifySanity(fs))return(-2);
44 1 fs_countDataSectors(fs);
45 1 fs_determineFatType(fs);
46 1 fs_findFirstSectorRootDir(fs);
47 1 fs_initCurrentDir(fs);
48 1 return(0);
49 1 }
50 /*****************************************************************************/
51
52 /* ****************************************************************************
53 * eint16 fs_isValidFat(Partition *part)
54 * Description: This functions loads the volumeID and checks if the magic
55 * value is present.
56 * Return value: returns 0 when magic code is missing, 1 if it is there.
57 */
58 eint16 fs_isValidFat(Partition *part)
59 {
ARM COMPILER V2.42, fs 27/03/06 10:45:50 PAGE 2
60 1 euint8 *buf;
61 1
62 1 buf=part_getSect(part,0,IOM_MODE_READONLY|IOM_MODE_EXP_REQ); /* Load Volume label */
63 1 if( ex_getb16(buf,0x1FE) != 0xAA55 ){
64 2 return (0);
65 2 }
66 1 part_relSect(part,buf);
67 1 return(1);
68 1 }
69 /*****************************************************************************/
70
71 /* ****************************************************************************
72 * void fs_loadVolumeId(FileSystem *fs, Partition *part)
73 * Description: This function loads all relevant fields from the volumeid.
74 */
75 void fs_loadVolumeId(FileSystem *fs, Partition *part)
76 {
77 1 euint8 *buf;
78 1
79 1 buf=part_getSect(part,0,IOM_MODE_READONLY|IOM_MODE_EXP_REQ);
80 1
81 1 fs->volumeId.BytesPerSector=ex_getb16(buf,0x0B);
82 1 fs->volumeId.SectorsPerCluster=*((eint8*)(buf+0x0D));
83 1 fs->volumeId.ReservedSectorCount=ex_getb16(buf,0x0E);
84 1 fs->volumeId.NumberOfFats=*((eint8*)(buf+0x10));
85 1 fs->volumeId.RootEntryCount=ex_getb16(buf,0x11);
86 1 fs->volumeId.SectorCount16=ex_getb16(buf,0x13);
87 1 fs->volumeId.FatSectorCount16=ex_getb16(buf,0x16);
88 1 fs->volumeId.SectorCount32=ex_getb32(buf,0x20);
89 1 fs->volumeId.FatSectorCount32=ex_getb32(buf,0x24);
90 1 fs->volumeId.RootCluster=ex_getb32(buf,0x2C);
91 1
92 1 part_relSect(part,buf);
93 1
94 1 }
95 /*****************************************************************************/
96
97 /* ****************************************************************************
98 * esint16 fs_verifySanity(FileSystem *fs)
99 * Description: Does some sanity calculations.
100 * Return value: 1 on success, 0 when discrepancies were found.
101 */
102 esint16 fs_verifySanity(FileSystem *fs)
103 {
104 1 esint16 sane=1; /* Sane until proven otherwise */
105 1 /* First check, BPS, we only support 512 */
106 1 if(fs->volumeId.BytesPerSector!=512)sane=0;
107 1 /* Check is SPC is valid (multiple of 2, and clustersize >=32KB */
108 1 if(!((fs->volumeId.SectorsPerCluster == 1 ) |
109 1 (fs->volumeId.SectorsPerCluster == 2 ) |
110 1 (fs->volumeId.SectorsPerCluster == 4 ) |
111 1 (fs->volumeId.SectorsPerCluster == 8 ) |
112 1 (fs->volumeId.SectorsPerCluster == 16) |
113 1 (fs->volumeId.SectorsPerCluster == 32) |
114 1 (fs->volumeId.SectorsPerCluster == 64) ))sane=0;
115 1 /* Any number of FAT's should be supported... (untested) */
116 1 /* There should be at least 1 reserved sector */
117 1 if(fs->volumeId.ReservedSectorCount==0)sane=0;
118 1 if(fs->volumeId.FatSectorCount16 != 0){
119 2 if(fs->volumeId.FatSectorCount16 > fs->part->disc->partitions[fs->part->activePartition].numSectors)san
-e=0;
120 2 }else{
121 2 if(fs->volumeId.FatSectorCount32 > fs->part->disc->partitions[fs->part->activePartition].numSectors)san
-e=0;
122 2 }
123 1 return(sane);
ARM COMPILER V2.42, fs 27/03/06 10:45:50 PAGE 3
124 1 }
125 /*****************************************************************************/
126
127 /* ****************************************************************************
128 * void fs_countDataSectors(FileSystem *fs)
129 * Description: This functions calculates the sectorcounts, fatsectorcounts and
130 * dataclustercounts. It fills in the general fields.
131 */
132 void fs_countDataSectors(FileSystem *fs)
133 {
134 1 euint32 rootDirSectors,dataSectorCount;
135 1
136 1 rootDirSectors=((fs->volumeId.RootEntryCount*32) +
137 1 (fs->volumeId.BytesPerSector - 1)) /
138 1 fs->volumeId.BytesPerSector;
139 1
140 1 if(fs->volumeId.FatSectorCount16 != 0)
141 1 {
142 2 fs->FatSectorCount=fs->volumeId.FatSectorCount16;
143 2 fs->volumeId.FatSectorCount32=0;
144 2 }
145 1 else
146 1 {
147 2 fs->FatSectorCount=fs->volumeId.FatSectorCount32;
148 2 fs->volumeId.FatSectorCount16=0;
149 2 }
150 1
151 1 if(fs->volumeId.SectorCount16!=0)
152 1 {
153 2 fs->SectorCount=fs->volumeId.SectorCount16;
154 2 fs->volumeId.SectorCount32=0;
155 2 }
156 1 else
157 1 {
158 2 fs->SectorCount=fs->volumeId.SectorCount32;
159 2 fs->volumeId.SectorCount16=0;
160 2 }
161 1
162 1 dataSectorCount=fs->SectorCount - (
163 1 fs->volumeId.ReservedSectorCount +
164 1 (fs->volumeId.NumberOfFats * fs->FatSectorCount) +
165 1 rootDirSectors);
166 1
167 1 fs->DataClusterCount=dataSectorCount/fs->volumeId.SectorsPerCluster;
168 1 }
169 /*****************************************************************************/
170
171 /* ****************************************************************************
172 * void fs_determineFatType(FileSystem *fs)
173 * Description: This function looks af the Dataclustercount and determines the
174 * FAT type. It fills in fs->type.
175 */
176 void fs_determineFatType(FileSystem *fs)
177 {
178 1 if(fs->DataClusterCount < 4085)
179 1 {
180 2 fs->type=FAT12;
181 2 fs->volumeId.RootCluster=0;
182 2 }
183 1 else if(fs->DataClusterCount < 65525)
184 1 {
185 2 fs->type=FAT16;
186 2 fs->volumeId.RootCluster=0;
187 2 }
188 1 else
189 1 {
ARM COMPILER V2.42, fs 27/03/06 10:45:50 PAGE 4
190 2 fs->type=FAT32;
191 2 }
192 1 }
193 /*****************************************************************************/
194
195 /* ****************************************************************************
196 * void fs_findFirstSectorRootDir(FileSystem *fs)
197 * Description: This functions fills in the fs->FirstSectorRootDir field, even
198 * for FAT32, although that is not necessary (because you have FirstClusterRootDir).
199 */
200 void fs_findFirstSectorRootDir(FileSystem *fs)
201 {
202 1 if(fs->type==FAT32)
203 1 fs->FirstSectorRootDir = fs->volumeId.ReservedSectorCount +
204 1 (fs->volumeId.NumberOfFats*fs->volumeId.FatSectorCount32) +
205 1 (fs->volumeId.RootCluster-2)*fs->volumeId.SectorsPerCluster;
206 1 else
207 1 fs->FirstSectorRootDir = fs->volumeId.ReservedSectorCount +
208 1 (fs->volumeId.NumberOfFats*fs->volumeId.FatSectorCount16);
209 1 }
210 /*****************************************************************************/
211
212 void fs_initCurrentDir(FileSystem *fs)
213 {
214 1 fs->FirstClusterCurrentDir = fs_getFirstClusterRootDir(fs);
215 1 }
216 /*****************************************************************************/
217
218 /* ****************************************************************************
219 * long fs_clusterToSector(FileSystem *fs,euint32 cluster)
220 * Description: This function converts a clusternumber in the effective sector
221 * number where this cluster starts. Boundary check is not implemented
222 * Return value: A long is returned representing the sectornumber.
223 */
224 euint32 fs_clusterToSector(FileSystem *fs,euint32 cluster)
225 {
226 1 eint32 base;
227 1
228 1 if(fs->type==FAT32)
229 1 {
230 2 base=
231 2 fs->volumeId.ReservedSectorCount+
232 2 fs->FatSectorCount*fs->volumeId.NumberOfFats;
233 2 }
234 1 else
235 1 {
236 2 base=
237 2 fs->volumeId.ReservedSectorCount+
238 2 fs->FatSectorCount*fs->volumeId.NumberOfFats+
239 2 fs->volumeId.RootEntryCount/16;
240 2 }
241 1 return( base + (cluster-2)*fs->volumeId.SectorsPerCluster );
242 1 }
243 /*****************************************************************************/
244
245 /* Function is unused, but may be usefull */
246 euint32 fs_sectorToCluster(FileSystem *fs,euint32 sector)
247 {
248 1 eint32 base;
249 1
250 1 if(fs->type==FAT32)
251 1 {
252 2 base=
253 2 fs->volumeId.ReservedSectorCount+
254 2 fs->FatSectorCount*fs->volumeId.NumberOfFats;
255 2 }
ARM COMPILER V2.42, fs 27/03/06 10:45:50 PAGE 5
256 1 else
257 1 {
258 2 base=
259 2 fs->volumeId.ReservedSectorCount+
260 2 fs->FatSectorCount*fs->volumeId.NumberOfFats+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -