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