|
1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // f32\sfat\sl_bpb.cpp |
|
15 // Boot sector code, specific for EFat.fsy |
|
16 // |
|
17 // |
|
18 |
|
19 /** |
|
20 @file |
|
21 @internalTechnology |
|
22 */ |
|
23 |
|
24 #include "sl_std.h" |
|
25 |
|
26 //------------------------------------------------------------------------------------------------------------------- |
|
27 |
|
28 TFatBootSector::TFatBootSector() |
|
29 { |
|
30 Initialise(); |
|
31 } |
|
32 |
|
33 /** initialises the boot sector data */ |
|
34 void TFatBootSector::Initialise() |
|
35 { |
|
36 Mem::FillZ(this, sizeof(TFatBootSector)); |
|
37 } |
|
38 |
|
39 //------------------------------------------------------------------------------------------------------------------- |
|
40 |
|
41 /** |
|
42 @return ETrue if the boot sector contents seems to be valid |
|
43 */ |
|
44 TBool TFatBootSector::IsValid() const |
|
45 { |
|
46 const TFatType fatType = FatType(); |
|
47 |
|
48 |
|
49 const TUint32 totSectors = Max(TotalSectors(), HugeSectors()); |
|
50 const TUint32 rootDirStartSec = ReservedSectors() + FatSectors()*NumberOfFats(); //-- root directory start sector |
|
51 |
|
52 if(fatType == EInvalid || ReservedSectors() < 1 || NumberOfFats() < 1 || FatSectors() < 1 || rootDirStartSec < 3 || |
|
53 RootDirEntries() < 1 || totSectors < 5) |
|
54 goto Invalid; |
|
55 |
|
56 if(TotalSectors() >0 && HugeSectors() >0 ) |
|
57 goto Invalid; //-- values clash |
|
58 |
|
59 return ETrue; |
|
60 |
|
61 Invalid: |
|
62 __PRINT(_L("TFatBootSector::IsValid() failed!")); |
|
63 |
|
64 return EFalse; |
|
65 } |
|
66 |
|
67 //------------------------------------------------------------------------------------------------------------------- |
|
68 |
|
69 /** |
|
70 Initialize boot sector object from the given bufer. Does not validate the data. |
|
71 @param aBuf buffer with data. |
|
72 */ |
|
73 void TFatBootSector::Internalize(const TDesC8& aBuf) |
|
74 { |
|
75 ASSERT(aBuf.Size() >= KSizeOfFatBootSector); |
|
76 |
|
77 Initialise(); |
|
78 |
|
79 TInt pos=0; |
|
80 |
|
81 Mem::Copy(&iJumpInstruction, &aBuf[pos],3); pos+=3; // 0 TUint8 iJumpInstruction[3] |
|
82 Mem::Copy(&iVendorId,&aBuf[pos],KVendorIdSize); pos+=KVendorIdSize; // 3 TUint8 iVendorId[KVendorIdSize] |
|
83 Mem::Copy(&iBytesPerSector,&aBuf[pos],2); pos+=2; // 11 TUint16 iBytesPerSector |
|
84 Mem::Copy(&iSectorsPerCluster,&aBuf[pos],1); pos+=1; // 13 TUint8 iSectorsPerCluster |
|
85 Mem::Copy(&iReservedSectors,&aBuf[pos],2); pos+=2; // 14 TUint16 iReservedSectors |
|
86 Mem::Copy(&iNumberOfFats,&aBuf[pos],1); pos+=1; // 16 TUint8 iNumberOfFats |
|
87 Mem::Copy(&iRootDirEntries,&aBuf[pos],2); pos+=2; // 17 TUint16 iRootDirEntries |
|
88 Mem::Copy(&iTotalSectors,&aBuf[pos],2); pos+=2; // 19 TUint16 iTotalSectors |
|
89 Mem::Copy(&iMediaDescriptor,&aBuf[pos],1); pos+=1; // 21 TUint8 iMediaDescriptor |
|
90 Mem::Copy(&iFatSectors,&aBuf[pos],2); pos+=2; // 22 TUint16 iFatSectors |
|
91 Mem::Copy(&iSectorsPerTrack,&aBuf[pos],2); pos+=2; // 24 TUint16 iSectorsPerTrack |
|
92 Mem::Copy(&iNumberOfHeads,&aBuf[pos],2); pos+=2; // 26 TUint16 iNumberOfHeads |
|
93 Mem::Copy(&iHiddenSectors,&aBuf[pos],4); pos+=4; // 28 TUint32 iHiddenSectors |
|
94 Mem::Copy(&iHugeSectors,&aBuf[pos],4); pos+=4; // 32 TUint32 iHugeSectors |
|
95 Mem::Copy(&iPhysicalDriveNumber,&aBuf[pos],1); pos+=1;// 36|64 TUint8 iPhysicalDriveNumber |
|
96 Mem::Copy(&iReserved,&aBuf[pos],1); pos+=1;// 37|65 TUint8 iReserved |
|
97 Mem::Copy(&iExtendedBootSignature,&aBuf[pos],1);pos+=1;// 38|66 TUint8 iExtendedBootSignature |
|
98 Mem::Copy(&iUniqueID,&aBuf[pos],4); pos+=4;// 39|67 TUint32 iUniqueID |
|
99 Mem::Copy(&iVolumeLabel,&aBuf[pos],KVolumeLabelSize); // 43|71 TUint8 iVolumeLabel[KVolumeLabelSize] |
|
100 pos+=KVolumeLabelSize; |
|
101 |
|
102 // 54|82 TUint8 iFileSysType[KFileSysTypeSize] |
|
103 ASSERT(aBuf.Size() >= pos+KFileSysTypeSize); |
|
104 Mem::Copy(&iFileSysType,&aBuf[pos],KFileSysTypeSize); |
|
105 } |
|
106 |
|
107 //------------------------------------------------------------------------------------------------------------------- |
|
108 |
|
109 /** |
|
110 Externalize boot sector object to the given data buffer. |
|
111 @param aBuf buffer to externalize. |
|
112 */ |
|
113 void TFatBootSector::Externalize(TDes8& aBuf) const |
|
114 { |
|
115 ASSERT(aBuf.MaxSize() >= KSizeOfFatBootSector); |
|
116 |
|
117 if(aBuf.Size() < KSizeOfFatBootSector) |
|
118 aBuf.SetLength(KSizeOfFatBootSector); |
|
119 |
|
120 TInt pos=0; |
|
121 |
|
122 Mem::Copy(&aBuf[pos],&iJumpInstruction,3); pos+=3; |
|
123 Mem::Copy(&aBuf[pos],&iVendorId,KVendorIdSize); pos+=8; |
|
124 Mem::Copy(&aBuf[pos],&iBytesPerSector,2); pos+=2; |
|
125 Mem::Copy(&aBuf[pos],&iSectorsPerCluster,1); pos+=1; |
|
126 Mem::Copy(&aBuf[pos],&iReservedSectors,2); pos+=2; |
|
127 Mem::Copy(&aBuf[pos],&iNumberOfFats,1); pos+=1; |
|
128 Mem::Copy(&aBuf[pos],&iRootDirEntries,2); pos+=2; |
|
129 Mem::Copy(&aBuf[pos],&iTotalSectors,2); pos+=2; |
|
130 Mem::Copy(&aBuf[pos],&iMediaDescriptor,1); pos+=1; |
|
131 Mem::Copy(&aBuf[pos],&iFatSectors,2); pos+=2; |
|
132 Mem::Copy(&aBuf[pos],&iSectorsPerTrack,2); pos+=2; |
|
133 Mem::Copy(&aBuf[pos],&iNumberOfHeads,2); pos+=2; |
|
134 Mem::Copy(&aBuf[pos],&iHiddenSectors,4); pos+=4; |
|
135 Mem::Copy(&aBuf[pos],&iHugeSectors,4); pos+=4; |
|
136 Mem::Copy(&aBuf[pos],&iPhysicalDriveNumber,1); pos+=1; |
|
137 Mem::FillZ(&aBuf[pos],1); pos+=1; |
|
138 Mem::Copy(&aBuf[pos],&iExtendedBootSignature,1);pos+=1; |
|
139 Mem::Copy(&aBuf[pos],&iUniqueID,4); pos+=4; |
|
140 |
|
141 Mem::Copy(&aBuf[pos],&iVolumeLabel,KVolumeLabelSize); |
|
142 pos+=KVolumeLabelSize; |
|
143 |
|
144 ASSERT(aBuf.MaxSize() >= pos+KFileSysTypeSize); |
|
145 Mem::Copy(&aBuf[pos],&iFileSysType,KFileSysTypeSize); |
|
146 } |
|
147 |
|
148 //------------------------------------------------------------------------------------------------------------------- |
|
149 |
|
150 #ifdef _DEBUG |
|
151 /** replaces all non-printable characters in a buffer with spaces */ |
|
152 static void FixDes(TDes& aDes) |
|
153 { |
|
154 for(TInt i=0; i< aDes.Length(); ++i) |
|
155 { |
|
156 TChar ch=aDes[i]; |
|
157 if(!ch.IsPrint()) |
|
158 aDes[i]=' '; |
|
159 } |
|
160 } |
|
161 #endif |
|
162 |
|
163 /** |
|
164 Print out the boot sector info. |
|
165 */ |
|
166 void TFatBootSector::PrintDebugInfo() const |
|
167 { |
|
168 #ifdef _DEBUG |
|
169 __PRINT(_L("\n======== BootSector info: =======\n")); |
|
170 |
|
171 TBuf<40> buf; |
|
172 buf.Copy(FileSysType()); FixDes(buf); |
|
173 __PRINT1(_L("FAT type:%S"), &buf); |
|
174 |
|
175 buf.Copy(VendorId()); FixDes(buf); |
|
176 __PRINT1(_L("Vendor ID:%S"), &buf); |
|
177 |
|
178 __PRINT1(_L("BytesPerSector:%d"),BytesPerSector()); |
|
179 __PRINT1(_L("SectorsPerCluster:%d"),SectorsPerCluster()); |
|
180 __PRINT1(_L("ReservedSectors:%d"),ReservedSectors()); |
|
181 __PRINT1(_L("NumberOfFats:%d"),NumberOfFats()); |
|
182 __PRINT1(_L("RootDirEntries:%d"),RootDirEntries()); |
|
183 __PRINT1(_L("Total Sectors:%d"),TotalSectors()); |
|
184 __PRINT1(_L("MediaDescriptor:0x%x"),MediaDescriptor()); |
|
185 __PRINT1(_L("FatSectors:%d"),FatSectors()); |
|
186 __PRINT1(_L("SectorsPerTrack:%d"),SectorsPerTrack()); |
|
187 __PRINT1(_L("NumberOfHeads:%d"),NumberOfHeads()); |
|
188 __PRINT1(_L("HugeSectors:%d"),HugeSectors()); |
|
189 __PRINT1(_L("Root Cluster Number:%d"),RootClusterNum()); |
|
190 __PRINT1(_L("FSInfo Sector Number:%d"),FSInfoSectorNum()); |
|
191 __PRINT1(_L("Backup Boot Rec Sector Number:%d"),BkBootRecSector()); |
|
192 __PRINT1(_L("PhysicalDriveNumber:%d"),PhysicalDriveNumber()); |
|
193 __PRINT1(_L("ExtendedBootSignature:%d"),ExtendedBootSignature()); |
|
194 __PRINT1(_L("UniqueID:0x%x"),UniqueID()); |
|
195 |
|
196 buf.Copy(VolumeLabel()); FixDes(buf); |
|
197 __PRINT1(_L("VolumeLabel:%S"), &buf); |
|
198 |
|
199 __PRINT(_L("=============================\n")); |
|
200 #endif |
|
201 |
|
202 } |
|
203 |
|
204 //------------------------------------------------------------------------------------------------------------------- |
|
205 |
|
206 /** |
|
207 Determine FAT type according to the information from boot sector, see FAT32 specs. |
|
208 @return FAT type. |
|
209 */ |
|
210 TFatType TFatBootSector::FatType(void) const |
|
211 { |
|
212 //-- check iBytesPerSector validity; it shall be one of: 512,1024,2048,4096 |
|
213 if(!IsPowerOf2(iBytesPerSector) || iBytesPerSector < 512 || iBytesPerSector > 4096) |
|
214 return EInvalid; //-- invalid iBytesPerSector value |
|
215 |
|
216 //-- check iSectorsPerCluster validity, it shall be one of: 1,2,4,8...128 |
|
217 if(!IsPowerOf2(iSectorsPerCluster) || iSectorsPerCluster > 128) |
|
218 return EInvalid; //-- invalid iSectorsPerCluster value |
|
219 |
|
220 |
|
221 const TUint32 rootDirSectors = (iRootDirEntries*KSizeOfFatDirEntry + (iBytesPerSector-1)) / iBytesPerSector; |
|
222 const TUint32 totSec = iTotalSectors ? iTotalSectors : iHugeSectors; |
|
223 const TUint32 dataSec = totSec - (iReservedSectors + (iNumberOfFats * iFatSectors) + rootDirSectors); |
|
224 const TUint32 clusterCnt = dataSec / iSectorsPerCluster; |
|
225 |
|
226 //-- magic. see FAT specs for details. |
|
227 if(clusterCnt < 4085) |
|
228 return EFat12; |
|
229 else if(clusterCnt < 65525) |
|
230 return EFat16; |
|
231 else |
|
232 return EInvalid; //-- FAT32 is not supported by this fsy |
|
233 } |
|
234 |
|
235 //------------------------------------------------------------------------------------------------------------------- |
|
236 |
|
237 /** @return The first Fat sector number */ |
|
238 TInt TFatBootSector::FirstFatSector() const |
|
239 { |
|
240 __ASSERT_DEBUG(IsValid(), Fault(EFatBadBootSectorParameter)); |
|
241 return ReservedSectors(); |
|
242 } |
|
243 |
|
244 /** |
|
245 @return Number of sectors in root directory. 0 for FAT32 |
|
246 */ |
|
247 TUint32 TFatBootSector::RootDirSectors() const |
|
248 { |
|
249 __ASSERT_DEBUG(IsValid(), Fault(EFatBadBootSectorParameter)); |
|
250 return ( (RootDirEntries()*KSizeOfFatDirEntry + (BytesPerSector()-1)) / BytesPerSector() ); |
|
251 } |
|
252 |
|
253 |
|
254 /** @return Start sector number of the root directory */ |
|
255 TInt TFatBootSector::RootDirStartSector() const |
|
256 { |
|
257 __ASSERT_DEBUG(IsValid(), Fault(EFatBadBootSectorParameter)); |
|
258 |
|
259 //-- FAT12/16 root dir starts just after the FATs |
|
260 return ReservedSectors() + TotalFatSectors()*NumberOfFats(); |
|
261 } |
|
262 |
|
263 |
|
264 /** @return first data sector number. for FAT32 it includes the root directory */ |
|
265 TInt TFatBootSector::FirstDataSector() const |
|
266 { |
|
267 return( ReservedSectors() + NumberOfFats()*TotalFatSectors() + RootDirSectors() ); |
|
268 } |
|
269 |
|
270 /** @return FAT-type independent sector count on the volume */ |
|
271 TUint32 TFatBootSector::VolumeTotalSectorNumber() const |
|
272 { |
|
273 __ASSERT_DEBUG(IsValid(), Fault(EFatBadBootSectorParameter)); |
|
274 return TotalSectors() >0 ? (TUint32)TotalSectors() : (TUint32)HugeSectors(); |
|
275 } |
|
276 |
|
277 /** @return FAT-type independent number of sectors in one FAT */ |
|
278 TUint32 TFatBootSector::TotalFatSectors() const |
|
279 { |
|
280 __ASSERT_DEBUG(IsValid(), Fault(EFatBadBootSectorParameter)); |
|
281 return (TUint32)FatSectors(); |
|
282 } |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |