imgtools/romtools/rofsbuild/fatimagegenerator.cpp
changeset 626 ac03b93ca9c4
parent 605 122d2b873fd1
child 654 7c11c3d8d025
equal deleted inserted replaced
625:a1925fb7753a 626:ac03b93ca9c4
    33 TFatImgGenerator::TFatImgGenerator(TSupportedFatType aType ,ConfigurableFatAttributes& aAttr ) :
    33 TFatImgGenerator::TFatImgGenerator(TSupportedFatType aType ,ConfigurableFatAttributes& aAttr ) :
    34 iType(aType),
    34 iType(aType),
    35 iFatTable(0),
    35 iFatTable(0),
    36 iFatTableBytes(0), 
    36 iFatTableBytes(0), 
    37 iTotalClusters(0),	
    37 iTotalClusters(0),	
    38 iBytsPerClus(0)
    38 iBytsPerClus(aAttr.iDriveClusterSize)
    39 {
    39 {
    40 	memset(&iBootSector,0,sizeof(iBootSector));
    40 	memset(&iBootSector,0,sizeof(iBootSector));
    41 	memset(&iFat32Ext,0,sizeof(iFat32Ext));
    41 	memset(&iFat32Ext,0,sizeof(iFat32Ext));
    42 	memset(&iFatHeader,0,sizeof(iFatHeader));
    42 	memset(&iFatHeader,0,sizeof(iFatHeader));
       
    43 	
       
    44 	if(iBytsPerClus != 0){
       
    45 		if(iBytsPerClus > KMaxClusterBytes){
       
    46 			Print(EError,"Cluster size is too large!\n");
       
    47 			iType = EFatUnknown;
       
    48 			return ;
       
    49 		}else if(iBytsPerClus < aAttr.iDriveSectorSize){
       
    50 			Print(EError,"Cluster size cannot be smaller than sector size (%d)!\n", aAttr.iDriveSectorSize);
       
    51 			iType = EFatUnknown;
       
    52 			return ;
       
    53 		}else{
       
    54 			TUint32 tempSectorSize = aAttr.iDriveSectorSize;
       
    55 			while (tempSectorSize < iBytsPerClus){
       
    56 				tempSectorSize <<=1;
       
    57 			}
       
    58 			if (tempSectorSize > iBytsPerClus){
       
    59 				Print(EError,"Cluster size should be (power of 2)*(sector size) i.e. 512, 1024, 2048, 4096, etc!\n");
       
    60 				iType = EFatUnknown;
       
    61 				return;
       
    62 			}
       
    63 		}
       
    64 	}
    43 	if(aAttr.iDriveSectorSize != 512 && aAttr.iDriveSectorSize != 1024 && aAttr.iDriveSectorSize != 2048 && aAttr.iDriveSectorSize != 4096) {
    65 	if(aAttr.iDriveSectorSize != 512 && aAttr.iDriveSectorSize != 1024 && aAttr.iDriveSectorSize != 2048 && aAttr.iDriveSectorSize != 4096) {
       
    66 		Print(EError,"Sector size must be one of (512, 1024, 2048, 4096)!\n");
    44 		iType = EFatUnknown ;
    67 		iType = EFatUnknown ;
    45 		return ;
    68 		return ;
    46 	}
    69 	}
    47 	*((TUint32*)iBootSector.BS_jmpBoot) = 0x00905AEB ; 
    70 	*((TUint32*)iBootSector.BS_jmpBoot) = 0x00905AEB ; 
    48 	memcpy(iBootSector.BS_OEMName,"SYMBIAN ",8);
    71 	memcpy(iBootSector.BS_OEMName,"SYMBIAN ",8);
    64 			aAttr.iImageSize = 0x40000000LL ; // 1G 
    87 			aAttr.iImageSize = 0x40000000LL ; // 1G 
    65 	}
    88 	}
    66 
    89 
    67 	TUint32 totalSectors = (TUint32)((aAttr.iImageSize + aAttr.iDriveSectorSize - 1) / aAttr.iDriveSectorSize);
    90 	TUint32 totalSectors = (TUint32)((aAttr.iImageSize + aAttr.iDriveSectorSize - 1) / aAttr.iDriveSectorSize);
    68 	if(aType == EFat32) {
    91 	if(aType == EFat32) {
    69 		InitAsFat32(totalSectors,aAttr.iSectorPerCluster ,aAttr.iDriveSectorSize);
    92 		InitAsFat32(totalSectors,aAttr.iDriveSectorSize);
    70 	}
    93 	}
    71 	else if(aType == EFat16) {
    94 	else if(aType == EFat16) {
    72 		InitAsFat16(totalSectors,aAttr.iSectorPerCluster,aAttr.iDriveSectorSize); 
    95 		InitAsFat16(totalSectors,aAttr.iDriveSectorSize); 
    73 	}
    96 	}
    74 	if(iType == EFatUnknown) return ;
    97 	if(iType == EFatUnknown) return ;
    75 	iBytsPerClus = iBootSector.BPB_SecPerClus * aAttr.iDriveSectorSize;
    98 	iBytsPerClus = iBootSector.BPB_SecPerClus * aAttr.iDriveSectorSize;
    76 //	if(iBytsPerClus > KMaxClusterBytes){
       
    77 //		Print(EError,"Cluster too large!\n");
       
    78 //		iType = EFatUnknown;
       
    79 //		return ;
       
    80 //	}
       
    81 	
       
    82 
    99 
    83 }
   100 }
    84 TFatImgGenerator::~TFatImgGenerator() {
   101 TFatImgGenerator::~TFatImgGenerator() {
    85 	if(iFatTable)
   102 	if(iFatTable)
    86 		delete []iFatTable;  
   103 		delete []iFatTable;  
    90 		delete cluster;
   107 		delete cluster;
    91 		it++;
   108 		it++;
    92 	}
   109 	}
    93 }
   110 }
    94 
   111 
    95 void TFatImgGenerator::InitAsFat16(TUint32 aTotalSectors,TUint8 aSecPerClus,TUint16 aBytsPerSec){
   112 void TFatImgGenerator::InitAsFat16(TUint32 aTotalSectors,TUint16 aBytsPerSec){
    96 	
   113 	
    97 	TUint32 numOfClusters ;
   114 	TUint32 numOfClusters ;
       
   115 	TUint8 aSecPerClus = iBytsPerClus / aBytsPerSec;
    98 	if(aSecPerClus == 0) {
   116 	if(aSecPerClus == 0) {
    99 		//Auto-calc the SecPerClus
   117 		//Auto-calc the SecPerClus
   100 		// FAT32 ,Count of clusters must >= 4085 and < 65525 , however , to avoid the "off by xx" warning, 
   118 		// FAT32 ,Count of clusters must >= 4085 and < 65525 , however , to avoid the "off by xx" warning, 
   101 		// proprositional value >= (4085 + 16) && < (65525 - 16)
   119 		// proprositional value >= (4085 + 16) && < (65525 - 16)
   102 		if(aTotalSectors < (4085 + 16)) { //when SecPerClus is 1, numOfClusters eq to aTotalSectors
   120 		if(aTotalSectors < (4085 + 16)) { //when SecPerClus is 1, numOfClusters eq to aTotalSectors
   120 			}
   138 			}
   121 			aSecPerClus <<= 1 ; 
   139 			aSecPerClus <<= 1 ; 
   122 		}	
   140 		}	
   123 	}
   141 	}
   124 	else {
   142 	else {
   125 		if( (aSecPerClus * aBytsPerSec) > KMaxClusterBytes){
       
   126 			Print(EError,"Cluster too large!\n");
       
   127 			iType = EFatUnknown;
       
   128 			return ;
       
   129 		}
       
   130 		numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus;
   143 		numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus;
   131 		if(numOfClusters >= (65525 - 16)){
   144 		if(numOfClusters >= (65525 - 16)){
   132       Print(EError,"Cluster count is too big for FAT16, please use the FAT32 format or set a new bigger sector count of cluster!\n");
   145       Print(EError,"Cluster count is too big for FAT16, please use the FAT32 format or set a new bigger cluster size!\n");
   133 			iType = EFatUnknown ;
   146 			iType = EFatUnknown ;
   134 			return ;
   147 			return ;
   135 		}
   148 		}
   136 		else if(numOfClusters < (4085 + 16)){
   149 		else if(numOfClusters < (4085 + 16)){
   137       Print(EError,"Cluster count is too small for FAT16, please set a new small sector count of cluster or set the size bigger!\n");
   150       Print(EError,"Cluster count is too small for FAT16, please set a new smaller cluster size or set the image size bigger!\n");
   138 			iType = EFatUnknown ;
   151 			iType = EFatUnknown ;
   139 			return ;
   152 			return ;
   140 		}
   153 		}
   141 
   154 
   142 	}
   155 	}
   159 		*((TUint16*)iBootSector.BPB_TotSec16) = (TUint16)aTotalSectors; 
   172 		*((TUint16*)iBootSector.BPB_TotSec16) = (TUint16)aTotalSectors; 
   160 	TUint16 sectorsForFAT = (TUint16)((iFatTableBytes + aBytsPerSec - 1) / aBytsPerSec);
   173 	TUint16 sectorsForFAT = (TUint16)((iFatTableBytes + aBytsPerSec - 1) / aBytsPerSec);
   161 	*((TUint16*)iBootSector.BPB_FATSz16) =  sectorsForFAT ; 
   174 	*((TUint16*)iBootSector.BPB_FATSz16) =  sectorsForFAT ; 
   162 	memcpy(iFatHeader.BS_FilSysType,"FAT16   ",sizeof(iFatHeader.BS_FilSysType));
   175 	memcpy(iFatHeader.BS_FilSysType,"FAT16   ",sizeof(iFatHeader.BS_FilSysType));
   163 }
   176 }
   164 void TFatImgGenerator::InitAsFat32(TUint32 aTotalSectors,TUint8 aSecPerClus,TUint16 aBytsPerSec) { 
   177 void TFatImgGenerator::InitAsFat32(TUint32 aTotalSectors,TUint16 aBytsPerSec) { 
   165 	
   178 	
   166 	TUint32 numOfClusters;
   179 	TUint32 numOfClusters;
       
   180 	TUint8 aSecPerClus = iBytsPerClus / aBytsPerSec;
   167 	if(aSecPerClus == 0) {
   181 	if(aSecPerClus == 0) {
   168 		//Auto-calc the SecPerClus
   182 		//Auto-calc the SecPerClus
   169 		// FAT32 ,Count of clusters must >= 65525, however , to avoid the "off by xx" warning, 
   183 		// FAT32 ,Count of clusters must >= 65525, however , to avoid the "off by xx" warning, 
   170 		// proprositional value >= (65525 + 16)			
   184 		// proprositional value >= (65525 + 16)			
   171 		if(aTotalSectors < (65525 + 16)) { //when SecPerClus is 1, numOfClusters eq to aTotalSectors
   185 		if(aTotalSectors < (65525 + 16)) { //when SecPerClus is 1, numOfClusters eq to aTotalSectors
   183 			}
   197 			}
   184 			aSecPerClus >>= 1 ; 
   198 			aSecPerClus >>= 1 ; 
   185 		}	
   199 		}	
   186 	}
   200 	}
   187 	else {
   201 	else {
   188 		if( (aSecPerClus * aBytsPerSec) > KMaxClusterBytes){
       
   189 			Print(EError,"Cluster too large!\n");
       
   190 			iType = EFatUnknown;
       
   191 			return ;
       
   192 		}
       
   193 		numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus;
   202 		numOfClusters = (aTotalSectors + aSecPerClus - 1) / aSecPerClus;
   194 		if(numOfClusters < (65525 + 16)) {
   203 		if(numOfClusters < (65525 + 16)) {
   195             Print(EError,"Cluster count is too small for FAT32, please set a new small sector count of cluster or set the size bigger or use the FAT16 format!\n");
   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");
   196 			iType = EFatUnknown ;
   205 			iType = EFatUnknown ;
   197 			return ;
   206 			return ;
   198 		}
   207 		}
   199 
   208 
   200 	}
   209 	}