author | lorewang |
Mon, 22 Nov 2010 10:41:57 +0800 | |
changeset 699 | 9ca650050cf0 |
parent 654 | 7c11c3d8d025 |
permissions | -rw-r--r-- |
605 | 1 |
/* |
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
#include "fatimagegenerator.h" |
|
18 |
#include "fatcluster.h" |
|
19 |
#include "fsnode.h" |
|
20 |
#include "h_utl.h" |
|
21 |
||
22 |
#include <memory.h> |
|
23 |
#include <time.h> |
|
24 |
#include <iostream> |
|
25 |
#include <fstream> |
|
26 |
#include <iomanip> |
|
27 |
using namespace std; |
|
28 |
||
29 |
const TInt KCharsOfCmdWndLine = 80 ; |
|
30 |
const TInt KRootEntryCount = 0x200; |
|
31 |
const TInt KRootClusterIndex = 0; |
|
32 |
||
33 |
TFatImgGenerator::TFatImgGenerator(TSupportedFatType aType ,ConfigurableFatAttributes& aAttr ) : |
|
34 |
iType(aType), |
|
35 |
iFatTable(0), |
|
36 |
iFatTableBytes(0), |
|
37 |
iTotalClusters(0), |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
38 |
iBytsPerClus(aAttr.iDriveClusterSize) |
605 | 39 |
{ |
40 |
memset(&iBootSector,0,sizeof(iBootSector)); |
|
41 |
memset(&iFat32Ext,0,sizeof(iFat32Ext)); |
|
42 |
memset(&iFatHeader,0,sizeof(iFatHeader)); |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
43 |
|
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
44 |
if(iBytsPerClus != 0){ |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
45 |
if(iBytsPerClus > KMaxClusterBytes){ |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
46 |
Print(EError,"Cluster size is too large!\n"); |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
47 |
iType = EFatUnknown; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
48 |
return ; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
49 |
}else if(iBytsPerClus < aAttr.iDriveSectorSize){ |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
50 |
Print(EError,"Cluster size cannot be smaller than sector size (%d)!\n", aAttr.iDriveSectorSize); |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
51 |
iType = EFatUnknown; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
52 |
return ; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
53 |
}else{ |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
54 |
TUint32 tempSectorSize = aAttr.iDriveSectorSize; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
55 |
while (tempSectorSize < iBytsPerClus){ |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
56 |
tempSectorSize <<=1; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
57 |
} |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
58 |
if (tempSectorSize > iBytsPerClus){ |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
59 |
Print(EError,"Cluster size should be (power of 2)*(sector size) i.e. 512, 1024, 2048, 4096, etc!\n"); |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
60 |
iType = EFatUnknown; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
61 |
return; |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
62 |
} |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
63 |
} |
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
64 |
} |
605 | 65 |
if(aAttr.iDriveSectorSize != 512 && aAttr.iDriveSectorSize != 1024 && aAttr.iDriveSectorSize != 2048 && aAttr.iDriveSectorSize != 4096) { |
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
66 |
Print(EError,"Sector size must be one of (512, 1024, 2048, 4096)!\n"); |
605 | 67 |
iType = EFatUnknown ; |
68 |
return ; |
|
69 |
} |
|
70 |
*((TUint32*)iBootSector.BS_jmpBoot) = 0x00905AEB ; |
|
71 |
memcpy(iBootSector.BS_OEMName,"SYMBIAN ",8); |
|
72 |
*((TUint16 *)iBootSector.BPB_BytsPerSec) = aAttr.iDriveSectorSize; |
|
73 |
||
74 |
iBootSector.BPB_NumFATs = aAttr.iDriveNoOfFATs; |
|
75 |
iBootSector.BPB_Media = 0xF8 ; |
|
76 |
iFatHeader.BS_DrvNum = 0x80 ; |
|
77 |
iFatHeader.BS_BootSig = 0x29 ; |
|
78 |
||
654 | 79 |
*((TUint32*)iFatHeader.BS_VolID) = aAttr.iVolumeId; |
605 | 80 |
memcpy(iFatHeader.BS_VolLab,aAttr.iDriveVolumeLabel,sizeof(iFatHeader.BS_VolLab)); |
81 |
if(aAttr.iImageSize == 0){ |
|
82 |
if(aType == EFat32) |
|
83 |
aAttr.iImageSize = 0x100000000LL ;// 4G |
|
84 |
else |
|
85 |
aAttr.iImageSize = 0x40000000LL ; // 1G |
|
86 |
} |
|
87 |
||
88 |
TUint32 totalSectors = (TUint32)((aAttr.iImageSize + aAttr.iDriveSectorSize - 1) / aAttr.iDriveSectorSize); |
|
89 |
if(aType == EFat32) { |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
90 |
InitAsFat32(totalSectors,aAttr.iDriveSectorSize); |
605 | 91 |
} |
92 |
else if(aType == EFat16) { |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
93 |
InitAsFat16(totalSectors,aAttr.iDriveSectorSize); |
605 | 94 |
} |
95 |
if(iType == EFatUnknown) return ; |
|
96 |
iBytsPerClus = iBootSector.BPB_SecPerClus * aAttr.iDriveSectorSize; |
|
97 |
||
98 |
} |
|
99 |
TFatImgGenerator::~TFatImgGenerator() { |
|
100 |
if(iFatTable) |
|
101 |
delete []iFatTable; |
|
102 |
Interator it = iDataClusters.begin(); |
|
103 |
while(it != iDataClusters.end()){ |
|
104 |
TFatCluster* cluster = *it ; |
|
105 |
delete cluster; |
|
106 |
it++; |
|
107 |
} |
|
108 |
} |
|
109 |
||
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
110 |
void TFatImgGenerator::InitAsFat16(TUint32 aTotalSectors,TUint16 aBytsPerSec){ |
605 | 111 |
|
112 |
TUint32 numOfClusters ; |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
113 |
TUint8 aSecPerClus = iBytsPerClus / aBytsPerSec; |
605 | 114 |
if(aSecPerClus == 0) { |
115 |
//Auto-calc the SecPerClus |
|
116 |
// FAT32 ,Count of clusters must >= 4085 and < 65525 , however , to avoid the "off by xx" warning, |
|
117 |
// proprositional value >= (4085 + 16) && < (65525 - 16) |
|
118 |
if(aTotalSectors < (4085 + 16)) { //when SecPerClus is 1, numOfClusters eq to aTotalSectors |
|
119 |
iType = EFatUnknown ; |
|
120 |
Print(EError,"Size is too small for FAT16, please set a bigger size !\n"); |
|
121 |
return ; |
|
122 |
} |
|
123 |
TUint8 secPerClusMax = KMaxClusterBytes / aBytsPerSec; |
|
124 |
numOfClusters = (aTotalSectors + secPerClusMax - 1) / secPerClusMax ; |
|
125 |
if(numOfClusters >= (65525 - 16)) { // too big |
|
126 |
iType = EFatUnknown ; |
|
127 |
Print(EError,"Size is too big for FAT16, please use the FAT32 format!\n"); |
|
128 |
return ; |
|
129 |
} |
|
130 |
||
131 |
aSecPerClus = 1; |
|
132 |
while(aSecPerClus < secPerClusMax){ |
|
133 |
numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus ; |
|
134 |
if (numOfClusters >= (4085 + 16) && numOfClusters < (65525 - 16)) { |
|
135 |
break; |
|
136 |
} |
|
137 |
aSecPerClus <<= 1 ; |
|
138 |
} |
|
139 |
} |
|
140 |
else { |
|
141 |
numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus; |
|
142 |
if(numOfClusters >= (65525 - 16)){ |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
143 |
Print(EError,"Cluster count is too big for FAT16, please use the FAT32 format or set a new bigger cluster size!\n"); |
605 | 144 |
iType = EFatUnknown ; |
145 |
return ; |
|
146 |
} |
|
147 |
else if(numOfClusters < (4085 + 16)){ |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
148 |
Print(EError,"Cluster count is too small for FAT16, please set a new smaller cluster size or set the image size bigger!\n"); |
605 | 149 |
iType = EFatUnknown ; |
150 |
return ; |
|
151 |
} |
|
152 |
||
153 |
} |
|
154 |
iTotalClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus ; |
|
155 |
iFatTableBytes = ((iTotalClusters << 1) + aBytsPerSec - 1) & (~(aBytsPerSec - 1)); |
|
156 |
iFatTable = new(std::nothrow) char[iFatTableBytes]; |
|
157 |
if(!iFatTable) { |
|
158 |
Print(EError,"Memory allocation failed for FAT16 Table!\n"); |
|
159 |
iType = EFatUnknown ; |
|
160 |
return ; |
|
161 |
} |
|
162 |
memset(iFatTable,0,iFatTableBytes); |
|
163 |
*((TUint32*)iFatTable) = 0xFFFFFFF8 ; |
|
164 |
iBootSector.BPB_SecPerClus = aSecPerClus; |
|
165 |
*((TUint16*)iBootSector.BPB_RsvdSecCnt) = 0x0001 ; |
|
166 |
*((TUint16*)iBootSector.BPB_RootEntCnt) = KRootEntryCount ; |
|
167 |
if(aTotalSectors > 0xFFFF) |
|
168 |
*((TUint32*)iBootSector.BPB_TotSec32) = aTotalSectors; |
|
169 |
else |
|
170 |
*((TUint16*)iBootSector.BPB_TotSec16) = (TUint16)aTotalSectors; |
|
171 |
TUint16 sectorsForFAT = (TUint16)((iFatTableBytes + aBytsPerSec - 1) / aBytsPerSec); |
|
172 |
*((TUint16*)iBootSector.BPB_FATSz16) = sectorsForFAT ; |
|
173 |
memcpy(iFatHeader.BS_FilSysType,"FAT16 ",sizeof(iFatHeader.BS_FilSysType)); |
|
174 |
} |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
175 |
void TFatImgGenerator::InitAsFat32(TUint32 aTotalSectors,TUint16 aBytsPerSec) { |
605 | 176 |
|
177 |
TUint32 numOfClusters; |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
178 |
TUint8 aSecPerClus = iBytsPerClus / aBytsPerSec; |
605 | 179 |
if(aSecPerClus == 0) { |
180 |
//Auto-calc the SecPerClus |
|
181 |
// FAT32 ,Count of clusters must >= 65525, however , to avoid the "off by xx" warning, |
|
182 |
// proprositional value >= (65525 + 16) |
|
183 |
if(aTotalSectors < (65525 + 16)) { //when SecPerClus is 1, numOfClusters eq to aTotalSectors |
|
184 |
iType = EFatUnknown ; |
|
185 |
Print(EError,"Size is too small for FAT32, please use the FAT16 format, or set the data size bigger!\n"); |
|
186 |
return ; |
|
187 |
} |
|
188 |
||
189 |
TUint8 secPerClusMax = KMaxClusterBytes / aBytsPerSec; |
|
190 |
aSecPerClus = secPerClusMax; |
|
191 |
while(aSecPerClus > 1){ |
|
192 |
numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus ; |
|
193 |
if (numOfClusters >= (65525 + 16)) { |
|
194 |
break; |
|
195 |
} |
|
196 |
aSecPerClus >>= 1 ; |
|
197 |
} |
|
198 |
} |
|
199 |
else { |
|
200 |
numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus; |
|
201 |
if(numOfClusters < (65525 + 16)) { |
|
626
ac03b93ca9c4
ROM Tools 12.3.4 + RCOMP 8.4.2
Zheng Shen <zheng.shen@nokia.com>
parents:
605
diff
changeset
|
202 |
Print(EError,"Cluster count is too small for FAT32, please set a new smaller cluster size or set the image size bigger or use the FAT16 format!\n"); |
605 | 203 |
iType = EFatUnknown ; |
204 |
return ; |
|
205 |
} |
|
206 |
||
207 |
} |
|
208 |
iTotalClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus ; |
|
209 |
iFatTableBytes = ((iTotalClusters << 2) + aBytsPerSec - 1) & (~(aBytsPerSec - 1)); |
|
210 |
iFatTable = new(std::nothrow) char[iFatTableBytes]; |
|
211 |
if(!iFatTable) { |
|
212 |
Print(EError,"Memory allocation failed for FAT32 Table!\n"); |
|
213 |
iType = EFatUnknown ; |
|
214 |
return ; |
|
215 |
} |
|
216 |
memset(iFatTable,0,iFatTableBytes); |
|
217 |
TUint32* fat32table = reinterpret_cast<TUint32*>(iFatTable); |
|
218 |
fat32table[0] = 0x0FFFFFF8 ; |
|
219 |
fat32table[1] = 0x0FFFFFFF ; |
|
220 |
iBootSector.BPB_SecPerClus = aSecPerClus; |
|
221 |
iBootSector.BPB_RsvdSecCnt[0] = 0x20 ; |
|
222 |
*((TUint32*)iBootSector.BPB_TotSec32) = aTotalSectors; |
|
223 |
*((TUint32*)iFat32Ext.BPB_FATSz32) = (iFatTableBytes + aBytsPerSec - 1) / aBytsPerSec; |
|
224 |
*((TUint32*)iFat32Ext.BPB_RootClus) = 2 ; |
|
225 |
*((TUint16*)iFat32Ext.BPB_FSInfo) = 1 ; |
|
226 |
*((TUint16*)iFat32Ext.BPB_BkBootSec) = 6 ; |
|
227 |
memcpy(iFatHeader.BS_FilSysType,"FAT32 ",sizeof(iFatHeader.BS_FilSysType)); |
|
228 |
} |
|
229 |
||
230 |
bool TFatImgGenerator::Execute(TFSNode* aRootDir , const char* aOutputFile){ |
|
231 |
if(EFatUnknown == iType) |
|
232 |
return false ; |
|
233 |
ofstream o(aOutputFile,ios_base::binary + ios_base::out + ios_base::trunc); |
|
234 |
TUint32 writtenBytes = 0 ; |
|
235 |
if(!o.is_open()) { |
|
236 |
Print(EError,"Can not open \"%s\" for writing !\n",aOutputFile) ; |
|
237 |
return false; |
|
238 |
} |
|
239 |
TUint16 bytsPerSector = *((TUint16*)iBootSector.BPB_BytsPerSec); |
|
240 |
Interator it = iDataClusters.begin(); |
|
241 |
while(it != iDataClusters.end()){ |
|
242 |
TFatCluster* cluster = *it ; |
|
243 |
delete cluster; |
|
244 |
it++; |
|
245 |
} |
|
246 |
iDataClusters.clear(); |
|
247 |
Print(EAlways,"Filesystem ready.\nWriting Header..."); |
|
248 |
||
249 |
if(EFat16 == iType){ |
|
250 |
char* header = new(std::nothrow) char[bytsPerSector]; |
|
251 |
if(!header){ |
|
252 |
Print(EError,"Can not allocate memory for FAT16 header!\n"); |
|
253 |
o.close(); |
|
254 |
return false ; |
|
255 |
} |
|
256 |
int offset = 0; |
|
257 |
memcpy(header,&iBootSector,sizeof(iBootSector)); |
|
258 |
offset = sizeof(iBootSector); |
|
259 |
memcpy(&header[offset],&iFatHeader,sizeof(iFatHeader)); |
|
260 |
offset += sizeof(iFatHeader); |
|
261 |
memset(&header[offset],0,bytsPerSector - offset); |
|
262 |
*((TUint16*)(&header[510])) = 0xAA55 ; |
|
263 |
||
264 |
o.write(header,bytsPerSector); |
|
265 |
writtenBytes += bytsPerSector; |
|
266 |
delete []header ; |
|
267 |
TUint16 rootDirSectors = (KRootEntryCount * 32) / bytsPerSector ; |
|
268 |
TUint16 rootDirClusters = (rootDirSectors + iBootSector.BPB_SecPerClus - 1) /iBootSector.BPB_SecPerClus; |
|
269 |
TUint32 rootDirBytes = KRootEntryCount * 32; |
|
270 |
TFatCluster* rootDir = new(std::nothrow) TFatCluster(0,rootDirClusters); |
|
271 |
rootDir->Init(rootDirBytes); |
|
272 |
iDataClusters.push_back(rootDir); |
|
273 |
aRootDir->WriteDirEntries(KRootClusterIndex,rootDir->GetData()); |
|
274 |
||
275 |
TUint index = 2 ; |
|
276 |
Print(EAlways," OK.\nPreparing cluster list..."); |
|
277 |
TFSNode* child = aRootDir->GetFirstChild() ; |
|
278 |
while(child){ |
|
279 |
if(!PrepareClusters(index,child)){ |
|
280 |
Print(EAlways," Failed.\nError:Image size is expected to be big enough for all the files.\n"); |
|
281 |
return false ; |
|
282 |
} |
|
283 |
child = child->GetSibling() ; |
|
284 |
} |
|
285 |
} |
|
286 |
else if(EFat32 == iType){ |
|
287 |
||
288 |
TUint headerSize = ( bytsPerSector << 5 ); // 32 reserved sectors for fat32 |
|
289 |
char* header = new(std::nothrow) char[headerSize]; |
|
290 |
if(!header){ |
|
291 |
Print(EError,"Can not allocate memory for FAT32 header!\n"); |
|
292 |
o.close(); |
|
293 |
return false ; |
|
294 |
} |
|
295 |
memset(header,0,headerSize); |
|
296 |
||
297 |
int offset = 0; |
|
298 |
memcpy(header,&iBootSector,sizeof(iBootSector)); |
|
299 |
offset = sizeof(iBootSector); |
|
300 |
memcpy(&header[offset],&iFat32Ext,sizeof(iFat32Ext)); |
|
301 |
offset += sizeof(iFat32Ext); |
|
302 |
memcpy(&header[offset],&iFatHeader,sizeof(iFatHeader)); |
|
303 |
offset += sizeof(iFatHeader); |
|
304 |
||
305 |
TFAT32FSInfoSector* fsinfo = reinterpret_cast<TFAT32FSInfoSector*>(&header[bytsPerSector]); |
|
306 |
*((TUint32*)fsinfo->FSI_LeadSig) = 0x41615252 ; |
|
307 |
*((TUint32*)fsinfo->FSI_StrucSig) = 0x61417272 ; |
|
308 |
memset(fsinfo->FSI_Free_Count,0xFF,8); |
|
309 |
char* tailed = header + 510 ; |
|
310 |
for(int i = 0 ; i < 32 ; i++ , tailed += bytsPerSector ) |
|
311 |
*((TUint16*)tailed) = 0xAA55 ; |
|
312 |
||
313 |
TUint index = 2 ; |
|
314 |
Print(EAlways," OK.\nPreparing cluster list..."); |
|
315 |
if(!PrepareClusters(index,aRootDir)) { |
|
316 |
Print(EAlways," Failed.\nERROR: Image size is expected to be big enough for all the files.\n"); |
|
317 |
delete []header ; |
|
318 |
return false; |
|
319 |
} |
|
320 |
||
321 |
||
322 |
*(TUint32*)(fsinfo->FSI_Free_Count) = iTotalClusters - index + 3; |
|
323 |
*(TUint32*)(fsinfo->FSI_Nxt_Free) = index ; |
|
324 |
||
325 |
// write bakup boot sectors |
|
326 |
memcpy(&header[bytsPerSector * 6],header,(bytsPerSector << 1)); |
|
327 |
o.write(header,headerSize); |
|
328 |
writtenBytes += headerSize; |
|
329 |
delete []header ; |
|
330 |
} |
|
331 |
//iDataClusters.sort(); |
|
332 |
it = iDataClusters.end() ; |
|
333 |
it -- ; |
|
334 |
int clusters = (*it)->GetIndex() + (*it)->ActualClusterCount() - 1; |
|
335 |
||
336 |
Print(EAlways," OK.\n%d clusters of data need to be written.\nWriting Fat table...",clusters); |
|
337 |
for(TUint8 w = 0 ; w < iBootSector.BPB_NumFATs ; w++){ |
|
338 |
o.write(iFatTable,iFatTableBytes); |
|
339 |
if(o.bad() || o.fail()){ |
|
340 |
Print(EAlways,"\nERROR:Writting failed. Please check the filesystem\n"); |
|
341 |
delete []iFatTable, |
|
342 |
o.close(); |
|
343 |
return false ; |
|
344 |
} |
|
345 |
writtenBytes += iFatTableBytes; |
|
346 |
} |
|
347 |
char* buffer = new(std::nothrow) char[KBufferedIOBytes]; |
|
348 |
if(!buffer){ |
|
349 |
Print(EError,"Can not allocate memory for I/O buffer !\n"); |
|
350 |
o.close(); |
|
351 |
return false ; |
|
352 |
} |
|
353 |
o.flush(); |
|
354 |
Print(EAlways," OK.\nWriting clusters data..."); |
|
355 |
||
356 |
int bytesInBuffer = 0; |
|
357 |
int writeTimes = 24; |
|
358 |
||
359 |
TFatCluster* lastClust = 0 ; |
|
360 |
for(it = iDataClusters.begin(); it != iDataClusters.end() ; it++ ){ |
|
361 |
TFatCluster* cluster = *it ; |
|
362 |
TUint fileSize = cluster->GetSize(); |
|
363 |
TUint toProcess = cluster->ActualClusterCount() * iBytsPerClus ; |
|
364 |
if(toProcess > KBufferedIOBytes){ // big file |
|
365 |
if(bytesInBuffer > 0){ |
|
366 |
o.write(buffer,bytesInBuffer); |
|
367 |
if(o.bad() || o.fail()){ |
|
368 |
Print(EError,"Writting failed.\n"); |
|
369 |
delete []buffer, |
|
370 |
o.close(); |
|
371 |
return false ; |
|
372 |
} |
|
373 |
writtenBytes += bytesInBuffer; |
|
374 |
bytesInBuffer = 0; |
|
375 |
Print(EAlways,"."); |
|
376 |
writeTimes ++ ; |
|
377 |
if((writeTimes % KCharsOfCmdWndLine) == 0){ |
|
378 |
o.flush(); |
|
379 |
cout << endl ; |
|
380 |
} |
|
381 |
} |
|
382 |
if(cluster->IsLazy()){ |
|
383 |
ifstream ifs(cluster->GetFileName(), ios_base::binary + ios_base::in); |
|
384 |
if(!ifs.is_open()){ |
|
385 |
Print(EError,"Can not open file \"%s\"\n",cluster->GetFileName()) ; |
|
386 |
o.close(); |
|
387 |
delete []buffer; |
|
388 |
return false ; |
|
389 |
} |
|
390 |
if(!ifs.good()) ifs.clear(); |
|
391 |
TUint processedBytes = 0 ; |
|
392 |
||
393 |
while(processedBytes < fileSize){ |
|
394 |
TUint ioBytes = fileSize - processedBytes ; |
|
395 |
if(ioBytes > KBufferedIOBytes) |
|
396 |
ioBytes = KBufferedIOBytes; |
|
397 |
ifs.read(buffer,ioBytes); |
|
398 |
processedBytes += ioBytes; |
|
399 |
o.write(buffer,ioBytes); |
|
400 |
if(o.bad() || o.fail()){ |
|
401 |
Print(EError,"Writting failed.\n"); |
|
402 |
delete []iFatTable, |
|
403 |
o.close(); |
|
404 |
return false ; |
|
405 |
} |
|
406 |
writtenBytes += ioBytes; |
|
407 |
Print(EAlways,"."); |
|
408 |
writeTimes ++ ; |
|
409 |
if((writeTimes % KCharsOfCmdWndLine) == 0){ |
|
410 |
o.flush(); |
|
411 |
Print(EAlways,"\n") ; |
|
412 |
} |
|
413 |
||
414 |
} |
|
415 |
TUint paddingBytes = toProcess - processedBytes; |
|
416 |
if( paddingBytes > 0 ){ |
|
417 |
memset(buffer,0,paddingBytes); |
|
418 |
o.write(buffer,paddingBytes); |
|
419 |
if(o.bad() || o.fail()){ |
|
420 |
Print(EError,"Writting failed.\n"); |
|
421 |
delete []buffer, |
|
422 |
o.close(); |
|
423 |
return false ; |
|
424 |
} |
|
425 |
writtenBytes += paddingBytes; |
|
426 |
} |
|
427 |
ifs.close(); |
|
428 |
||
429 |
} |
|
430 |
else { |
|
431 |
// impossible |
|
432 |
Print(EError,"Unexpected result!\n"); |
|
433 |
o.close(); |
|
434 |
delete []buffer; |
|
435 |
return false ; |
|
436 |
} |
|
437 |
} |
|
438 |
else { |
|
439 |
if(toProcess > (KBufferedIOBytes - bytesInBuffer)){ |
|
440 |
o.write(buffer,bytesInBuffer); |
|
441 |
if(o.bad() || o.fail()){ |
|
442 |
Print(EError,"Writting failed.\n"); |
|
443 |
delete []buffer, |
|
444 |
o.close(); |
|
445 |
return false ; |
|
446 |
} |
|
447 |
writtenBytes += bytesInBuffer; |
|
448 |
Print(EAlways,"."); |
|
449 |
writeTimes ++ ; |
|
450 |
if((writeTimes % KCharsOfCmdWndLine) == 0){ |
|
451 |
o.flush(); |
|
452 |
cout << endl ; |
|
453 |
} |
|
454 |
bytesInBuffer = 0; |
|
455 |
} |
|
456 |
if(cluster->IsLazy()){ |
|
457 |
ifstream ifs(cluster->GetFileName(), ios_base::binary + ios_base::in); |
|
458 |
if(!ifs.is_open()){ |
|
459 |
Print(EError,"Can not open file \"%s\"\n",cluster->GetFileName()) ; |
|
460 |
o.close(); |
|
461 |
delete []buffer; |
|
462 |
return false ; |
|
463 |
} |
|
464 |
if(!ifs.good()) ifs.clear(); |
|
465 |
ifs.read(&buffer[bytesInBuffer],fileSize); |
|
466 |
bytesInBuffer += fileSize; |
|
467 |
if(toProcess > fileSize) { // fill padding bytes |
|
468 |
memset(&buffer[bytesInBuffer],0,toProcess - fileSize); |
|
469 |
bytesInBuffer += (toProcess - fileSize); |
|
470 |
} |
|
471 |
ifs.close(); |
|
472 |
||
473 |
} |
|
474 |
else{ |
|
475 |
if(toProcess != cluster->GetSize() && cluster->GetIndex() != KRootClusterIndex){ |
|
476 |
Print(EError,"Unexpected size!\n"); |
|
477 |
o.close(); |
|
478 |
delete []buffer; |
|
479 |
return false ; |
|
480 |
} |
|
481 |
memcpy(&buffer[bytesInBuffer],cluster->GetData(),cluster->GetSize()); |
|
482 |
bytesInBuffer += cluster->GetSize(); |
|
483 |
} |
|
484 |
||
485 |
} |
|
486 |
lastClust = cluster ; |
|
487 |
||
488 |
} |
|
489 |
if(bytesInBuffer > 0){ |
|
490 |
o.write(buffer,bytesInBuffer); |
|
491 |
if(o.bad() || o.fail()){ |
|
492 |
Print(EError,"Writting failed.\n"); |
|
493 |
delete []buffer, |
|
494 |
o.close(); |
|
495 |
return false ; |
|
496 |
} |
|
497 |
writtenBytes += bytesInBuffer; |
|
498 |
o.flush(); |
|
499 |
} |
|
500 |
Print(EAlways,"\nDone.\n\n"); |
|
501 |
o.close(); |
|
502 |
||
503 |
return true ; |
|
504 |
} |
|
505 |
bool TFatImgGenerator::PrepareClusters(TUint& aNextClusIndex,TFSNode* aNode) { |
|
506 |
TUint sizeOfItem = aNode->GetSize(); |
|
507 |
TUint clusters = (sizeOfItem + iBytsPerClus - 1) / iBytsPerClus; |
|
508 |
||
509 |
if(iTotalClusters < aNextClusIndex + clusters) |
|
510 |
return false ; |
|
511 |
||
512 |
TUint16* fat16Table = reinterpret_cast<TUint16*>(iFatTable); |
|
513 |
TUint32* fat32Table = reinterpret_cast<TUint32*>(iFatTable); |
|
514 |
||
515 |
for(TUint i = aNextClusIndex + clusters - 1 ; i > aNextClusIndex ; i--){ |
|
516 |
if(iType == EFat16) |
|
517 |
fat16Table[i - 1] = i ; |
|
518 |
else |
|
519 |
fat32Table[i - 1] = i ; |
|
520 |
} |
|
521 |
if(iType == EFat16) |
|
522 |
fat16Table[aNextClusIndex + clusters - 1] = 0xffff ; |
|
523 |
else |
|
524 |
fat32Table[aNextClusIndex + clusters - 1] = 0x0fffffff ; |
|
525 |
||
526 |
TFatCluster* cluster = new TFatCluster(aNextClusIndex,clusters); |
|
527 |
if(aNode->IsDirectory()) { |
|
528 |
TUint bytes = clusters * iBytsPerClus ; |
|
529 |
cluster->Init(bytes); |
|
530 |
aNode->WriteDirEntries(aNextClusIndex,cluster->GetData()); |
|
531 |
} |
|
532 |
else { |
|
533 |
cluster->LazyInit(aNode->GetPCSideName(),sizeOfItem); |
|
534 |
aNode->WriteDirEntries(aNextClusIndex,NULL); |
|
535 |
} |
|
536 |
iDataClusters.push_back(cluster); |
|
537 |
||
538 |
aNextClusIndex += clusters; |
|
539 |
if(aNode->GetFirstChild()){ |
|
540 |
if(!PrepareClusters(aNextClusIndex,aNode->GetFirstChild())) |
|
541 |
return false ; |
|
542 |
} |
|
543 |
if(aNode->GetSibling()){ |
|
544 |
if(!PrepareClusters(aNextClusIndex,aNode->GetSibling())) |
|
545 |
return false; |
|
546 |
} |
|
547 |
return true ; |
|
548 |
} |