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