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