imgtools/imglib/filesystem/source/fatbasebootsector.cpp
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * This file contains the definition of class TFATBaseBootSector. 
       
    16 * TFATBaseBootSector is the base class for the boot sector class 
       
    17 * of different FAT file system type.This contains the data member 
       
    18 * representing the common fields in each FAT image type
       
    19 * @internalComponent
       
    20 * @released
       
    21 *
       
    22 */
       
    23 
       
    24 #include "fatbasebootsector.h"
       
    25 #include <cstring>
       
    26 
       
    27 /**
       
    28 Constructor of the base boot sector class
       
    29 
       
    30 @internalComponent
       
    31 @released
       
    32 */
       
    33 TFATBaseBootSector::TFATBaseBootSector()
       
    34 {
       
    35 }
       
    36 
       
    37 /**
       
    38 Destructor of the base boot sector class
       
    39 
       
    40 @internalComponent
       
    41 @released
       
    42 */
       
    43 TFATBaseBootSector::~TFATBaseBootSector()
       
    44 {
       
    45 }
       
    46 
       
    47 /**
       
    48 Function Sets the OEM name generally, Indication of what system 
       
    49 formats the volume
       
    50 
       
    51 @internalComponent
       
    52 @released
       
    53 */
       
    54 void TFATBaseBootSector::SetOEMName()
       
    55 	{
       
    56 		strcpy( reinterpret_cast<char*>(iOEMName),KDefaultOEMName);
       
    57 	}
       
    58 
       
    59 /**
       
    60 Function to get the OEM name
       
    61 
       
    62 @internalComponent
       
    63 @released
       
    64 
       
    65 @return OEM Name
       
    66 */
       
    67 unsigned char* TFATBaseBootSector::OEMName() 
       
    68 	{
       
    69 		return iOEMName;
       
    70 	}
       
    71 
       
    72 /**
       
    73 Function to set the jump instructions
       
    74 
       
    75 @internalComponent
       
    76 @released
       
    77 */
       
    78 void TFATBaseBootSector::SetJumpInstruction()
       
    79 {
       
    80 	iJmpBoot[0]= 0xEB;
       
    81 	iJmpBoot[1]= 0x5A;
       
    82 	iJmpBoot[2]= 0x90;
       
    83 }
       
    84 
       
    85 /**
       
    86 Function to get the jump instructions
       
    87 
       
    88 @internalComponent
       
    89 @released
       
    90 
       
    91 @return jump boot instruction
       
    92 */
       
    93 unsigned char* TFATBaseBootSector::JumpInstruction()  
       
    94 {
       
    95 	return iJmpBoot;
       
    96 }
       
    97 
       
    98 
       
    99 /**
       
   100 Function to set the bytes per sector.  
       
   101 
       
   102 @internalComponent
       
   103 @released
       
   104 
       
   105 @param aDriveSectorSize Sector size in bytes 
       
   106 */
       
   107 void TFATBaseBootSector::SetBytesPerSector(unsigned int aDriveSectorSize)
       
   108 {
       
   109 	// Take the default value if SectorSize is not provided by the user. 
       
   110 	if (aDriveSectorSize != 0)
       
   111 	{
       
   112 		unsigned short int acceptableValues[] = {512,1024,2048,4096};
       
   113 		unsigned short int acceptableValuesCount = 4;
       
   114 		bool validSectorSize = false;
       
   115 		for (unsigned int count=0; count<acceptableValuesCount; count++)
       
   116 		{
       
   117 			if(aDriveSectorSize == acceptableValues[count])
       
   118 			{
       
   119 				validSectorSize = true;
       
   120 				break;
       
   121 			}
       
   122 		}
       
   123 		// If invalid value for Sector Size is provided, consider the default value.
       
   124 		if (validSectorSize)
       
   125 		{
       
   126 			iBytesPerSector=aDriveSectorSize;
       
   127 			return;
       
   128 		}
       
   129 		else
       
   130 		{
       
   131 			std::cout<<"Warning: Invalid Sector Size value. Default value is considered.\n";
       
   132 		}
       
   133 	}
       
   134 	iBytesPerSector=KDefaultBytesPerSector;	
       
   135 }
       
   136 
       
   137 /**
       
   138 Return the bytes per sector
       
   139 
       
   140 @internalComponent
       
   141 @released
       
   142 
       
   143 @return bytes per sector, hard coded as 512 here.
       
   144 */
       
   145 unsigned int TFATBaseBootSector::BytesPerSector() const
       
   146 {
       
   147 	return iBytesPerSector;
       
   148 }
       
   149 
       
   150 /**
       
   151 Sets the number of Fats on the volume
       
   152 
       
   153 @internalComponent
       
   154 @released
       
   155 
       
   156 @param aDriveNoOfFATs Number of fats
       
   157 */
       
   158 void TFATBaseBootSector::SetNumberOfFats(unsigned int aDriveNoOfFATs) 
       
   159 {
       
   160 	// Take the default value if No of FATs is not provided by the user. 
       
   161 	if (aDriveNoOfFATs != 0)
       
   162 	{
       
   163 		// If invalid value for No of FATs is provided, generate a warning and take the default value. 
       
   164 		if ((aDriveNoOfFATs>255) || (aDriveNoOfFATs<1))
       
   165 		{
       
   166 			std::cout<<"Warning: No of FATs should be between 0 and 256. Default value is considered.\n";
       
   167 			iNumberOfFats= KDefaultNumFats;
       
   168 			return;
       
   169 		}
       
   170 		iNumberOfFats= aDriveNoOfFATs;
       
   171 	}
       
   172 	else
       
   173 	{
       
   174 		iNumberOfFats= KDefaultNumFats;
       
   175 	}
       
   176 }
       
   177 
       
   178 
       
   179 /**
       
   180 Total sectors on the volume,This count includes the total count of all sectors 
       
   181 in all four regions of the volume.iTatalSectors is a 16 bit field and iTotalSectors32
       
   182 is a 32 bit field.Hence if the total sectors are more than 2^16(0x10000 in hex) 
       
   183 then iTotalSectors32 is set otherwise it is zero.
       
   184 
       
   185 @internalComponent
       
   186 @released
       
   187 
       
   188 @param aPartitionSize Partition size in bytes
       
   189 */
       
   190 void TFATBaseBootSector::ComputeTotalSectors(Long64 aPartitionSize)
       
   191 {
       
   192 	int Log2OfBytesPerSector = Log2(iBytesPerSector);
       
   193 	unsigned long TotalSectors64 = (unsigned long)(aPartitionSize >> Log2OfBytesPerSector);
       
   194 	if(TotalSectors64 >= 0x10000)
       
   195 	{
       
   196 			iTotalSectors = 0;
       
   197 			iTotalSectors32 = (unsigned int) TotalSectors64;
       
   198 	}
       
   199 	else
       
   200 	{
       
   201 			iTotalSectors = (unsigned short)TotalSectors64;
       
   202 			iTotalSectors32=0;
       
   203 	}
       
   204 }
       
   205 
       
   206 /**
       
   207 Set the media descriptor,0xF8 is the standard for fixed (non removable) media.
       
   208 
       
   209 @internalComponent
       
   210 @released
       
   211 */
       
   212 void TFATBaseBootSector::SetMedia()
       
   213 {
       
   214 	iMedia=KBPBMedia;
       
   215 }
       
   216 
       
   217 /**
       
   218 This methods gets the media descriptor
       
   219 
       
   220 @internalComponent
       
   221 @released
       
   222 
       
   223 @return media descriptor
       
   224 */
       
   225 unsigned char TFATBaseBootSector::Media() const
       
   226 {
       
   227 	return iMedia;
       
   228 }
       
   229 
       
   230 /**
       
   231 Set the number of hidden sectors in the volume,Count of hidden sector
       
   232 preceding the partition.
       
   233 
       
   234 @internalComponent
       
   235 @released
       
   236 */
       
   237 void TFATBaseBootSector::SetHiddenSectors()
       
   238 {
       
   239 	iHiddenSectors=KDefaultHiddenSectors;
       
   240 }
       
   241 
       
   242 /**
       
   243 Gets the number of hidden sectors in the volume
       
   244 
       
   245 @internalComponent
       
   246 @released
       
   247 
       
   248 @return the number of hidden sectors in a given FAT Volume
       
   249 */
       
   250 unsigned int TFATBaseBootSector::HiddenSectors() const
       
   251 {
       
   252 	return iHiddenSectors;
       
   253 }
       
   254 
       
   255 /**
       
   256 Set the sectors per track preceding the partition.
       
   257 
       
   258 @internalComponent
       
   259 @released
       
   260 */
       
   261 void TFATBaseBootSector::SetSectorsPerTrack()
       
   262 {
       
   263 	iSectorsPerTrack=KDefaultSectorsPerTrack;// default value for flash memory
       
   264 }
       
   265 
       
   266 /**
       
   267 Gets the number sectors per track in the volume
       
   268 
       
   269 @internalComponent
       
   270 @released
       
   271 
       
   272 @return the number of sectors per track in a given FAT Volume
       
   273 */
       
   274 unsigned short TFATBaseBootSector::SectorsPerTrack() const
       
   275 {
       
   276 	 return iSectorsPerTrack;
       
   277 }
       
   278 
       
   279 /**
       
   280 Set the number of heads
       
   281 
       
   282 @internalComponent
       
   283 @released
       
   284 */
       
   285 void TFATBaseBootSector::SetNumberOfHeads()
       
   286 {
       
   287 	iNumHeads=KDefaultNumHeads;// default value for flash memory
       
   288 }
       
   289 
       
   290 /**
       
   291 Gets the the number of heads
       
   292 
       
   293 @internalComponent
       
   294 @released
       
   295 
       
   296 @return number of heads in a given FAT Volume
       
   297 */
       
   298 unsigned short TFATBaseBootSector::NumberOfHeads() const
       
   299 {
       
   300 	return iNumHeads;// default value for flash memory
       
   301 }
       
   302 
       
   303 /**
       
   304 Set the Physical drive number,not used in Symbian OS
       
   305 
       
   306 @internalComponent
       
   307 @released
       
   308 */
       
   309 void TFATBaseBootSector::SetBootSectorDriveNumber()
       
   310 {
       
   311 	iPhysicalDriveNumber=KDefaultDriveNumber;
       
   312 }
       
   313 
       
   314 /**
       
   315 Function to return drive number
       
   316 
       
   317 @internalComponent
       
   318 @released
       
   319 
       
   320 @return Physical drive number, not used in Symbian OS
       
   321 */
       
   322 unsigned char TFATBaseBootSector::BootSectorDriveNumber() const
       
   323 {
       
   324 	return iPhysicalDriveNumber;
       
   325 }
       
   326 
       
   327 /**
       
   328 Set the reserved byte value
       
   329 
       
   330 @internalComponent
       
   331 @released
       
   332 */
       
   333 void TFATBaseBootSector::SetReservedByte()
       
   334 {
       
   335 	iReservedByte=KDefaultReservedByte;
       
   336 }
       
   337 
       
   338 /**
       
   339 Get the value of reserved byte in boot sector
       
   340 
       
   341 @internalComponent
       
   342 @released
       
   343 
       
   344 @return Returns the reserved byte value
       
   345 */
       
   346 unsigned char TFATBaseBootSector::ReservedByte() const
       
   347 {
       
   348 	return iReservedByte;
       
   349 }
       
   350 
       
   351 /**
       
   352 Set the extended boot signature
       
   353 
       
   354 @internalComponent
       
   355 @released
       
   356 
       
   357 */
       
   358 void TFATBaseBootSector::SetBootSignature() 
       
   359 {
       
   360 	iBootSign=KDefaultBootSignature;
       
   361 }
       
   362 
       
   363 /**
       
   364 Gets the extended boot signature
       
   365 
       
   366 @internalComponent
       
   367 @released
       
   368 
       
   369 @return boot signature
       
   370 */
       
   371 unsigned char TFATBaseBootSector::BootSignature() const
       
   372 {
       
   373 	return iBootSign;
       
   374 }
       
   375 
       
   376 /**
       
   377 Set the unique volume serial number,This ID is usually generated by 
       
   378 simply combining the current date and time in to 32 bit value.
       
   379 
       
   380 @internalComponent
       
   381 @released
       
   382 */
       
   383 void TFATBaseBootSector::SetVolumeId()
       
   384 {	
       
   385 	time_t rawtime;
       
   386 	time(&rawtime);
       
   387 	iVolumeId=rawtime;
       
   388 }
       
   389 
       
   390 /**
       
   391 Returns the volume id 
       
   392 
       
   393 @internalComponent
       
   394 @released
       
   395 
       
   396 @return volume id field of the boot sector 
       
   397 */
       
   398 unsigned int TFATBaseBootSector::VolumeId() const
       
   399 {
       
   400 return iVolumeId ;
       
   401 }
       
   402 
       
   403 /**
       
   404 Set the volume's label
       
   405 
       
   406 @internalComponent
       
   407 @released
       
   408 
       
   409 @param aVolumeLable Data Drive Volume Label
       
   410 */
       
   411 void TFATBaseBootSector::SetVolumeLab(String aVolumeLable)
       
   412 {
       
   413 	// Set the default value of VolumeLable(i.e. "NO NAME    ") if not provided
       
   414 	// by the user.
       
   415 	if (aVolumeLable.empty())	
       
   416 	{
       
   417 		strcpy(reinterpret_cast<char*>(iVolumeLabel),KDefaultVolumeLabel);
       
   418 	}
       
   419 	else 
       
   420 	{
       
   421 		// If the volume label provided is greater than 11 characters then consider only 
       
   422 		// the first 11 characters and generate a warning.
       
   423 		int volumeMaxLangth= 11;
       
   424 		int volumeLabelSize= aVolumeLable.size();
       
   425 		if (volumeLabelSize > volumeMaxLangth)
       
   426 		{
       
   427 			std::cout<<"Warning: Size overflow for Data Drive Volume Label. Truncating to 11-bytes.\n";	
       
   428 			aVolumeLable.resize(volumeMaxLangth);
       
   429 			strcpy(reinterpret_cast<char*>(iVolumeLabel),aVolumeLable.c_str());
       
   430 			return;
       
   431 		}
       
   432 		
       
   433 		// If the VolumeLable provided is less than 11-characters then pad the 
       
   434 		// remaining bytes with white-spaces.		
       
   435 		if (volumeLabelSize < KMaxVolumeLabel)
       
   436 		{
       
   437 			while(volumeLabelSize < 11)
       
   438 			{
       
   439 				aVolumeLable.append(" ");
       
   440 				volumeLabelSize = aVolumeLable.size();
       
   441 			}	
       
   442 		}
       
   443 		strcpy(reinterpret_cast<char*>(iVolumeLabel),aVolumeLable.c_str());
       
   444 	}
       
   445 }
       
   446 
       
   447 /**
       
   448 returns  the volume's label
       
   449 
       
   450 @internalComponent
       
   451 @released
       
   452 
       
   453 */
       
   454 unsigned char* TFATBaseBootSector::VolumeLab() 
       
   455 {
       
   456 	return iVolumeLabel;
       
   457 }
       
   458 
       
   459 /**
       
   460 Returns the number of reserved sectors on the volume
       
   461 
       
   462 @internalComponent
       
   463 @released
       
   464 
       
   465 @return iReservedSectors
       
   466 */
       
   467 unsigned short TFATBaseBootSector::ReservedSectors() const
       
   468 {
       
   469 	return iReservedSectors;
       
   470 }
       
   471 
       
   472 /**
       
   473 Returns the number of Fats on the volume
       
   474 
       
   475 @internalComponent
       
   476 @released
       
   477 
       
   478 @return iNumberOfFats
       
   479 */
       
   480 unsigned char TFATBaseBootSector::NumberOfFats() const
       
   481 {
       
   482 	return iNumberOfFats;
       
   483 }
       
   484 
       
   485 /**
       
   486 Returns the number of entries allowed in the root directory, specific to Fat12/16, zero for FAT32
       
   487 
       
   488 @internalComponent
       
   489 @released
       
   490 
       
   491 @return iRootDirEntries
       
   492 */
       
   493 unsigned short TFATBaseBootSector::RootDirEntries() const
       
   494 {
       
   495 	return iRootDirEntries;
       
   496 }
       
   497 
       
   498 /**
       
   499 Returns the total sectors on the volume, 
       
   500 
       
   501 @internalComponent
       
   502 @released
       
   503 
       
   504 @return iTotalSectors 
       
   505 */
       
   506 unsigned int TFATBaseBootSector::TotalSectors(Long64 aPartitionSize) const
       
   507 {
       
   508 	if((aPartitionSize/iBytesPerSector)>= 0x10000)	
       
   509 	return iTotalSectors32;
       
   510 	else 
       
   511 	return iTotalSectors;
       
   512 }
       
   513 
       
   514 /**
       
   515 Returns base 2 Logarithm of a number 
       
   516 
       
   517 @internalComponent
       
   518 @released
       
   519 
       
   520 @param aNum number whose logarithm is to be taken
       
   521 @return log base 2 of the number passed
       
   522 */
       
   523 int TFATBaseBootSector::Log2(int aNum) 
       
   524 {
       
   525 	int res=-1;
       
   526 	while(aNum)
       
   527 		{
       
   528 		res++;
       
   529 		aNum>>=1;
       
   530 		}
       
   531 	return(res);
       
   532 }
       
   533 
       
   534 /**
       
   535 Returns the sectors per cluster ratio
       
   536 
       
   537 @internalComponent
       
   538 @released
       
   539 
       
   540 @return iSectorsPerCluster
       
   541 */
       
   542 unsigned char TFATBaseBootSector::SectorsPerCluster() const
       
   543 {
       
   544 	return iSectorsPerCluster;
       
   545 }
       
   546 
       
   547 /**
       
   548 Returns the 16 bit count of total sectors on the volume 
       
   549 
       
   550 @internalComponent
       
   551 @released
       
   552 
       
   553 @return iTotalSectors 
       
   554 */
       
   555 unsigned short TFATBaseBootSector::LowSectorsCount() const
       
   556 {
       
   557 	return iTotalSectors;
       
   558 }
       
   559 
       
   560 /**
       
   561 Returns the 32 bit count of total sectors on the volume 
       
   562 
       
   563 @internalComponent
       
   564 @released
       
   565 
       
   566 @return iTotalSectors 
       
   567 */
       
   568 unsigned int TFATBaseBootSector::HighSectorsCount() const
       
   569 {
       
   570 	return iTotalSectors32;
       
   571 }
       
   572 
       
   573 /**
       
   574 Returns sectors used for the Fat table, zero for FAT32
       
   575 
       
   576 @internalComponent
       
   577 @released
       
   578 
       
   579 @return iFatSectors
       
   580 */
       
   581 unsigned short TFATBaseBootSector::FatSectors() const
       
   582 {
       
   583 	return (unsigned short)iFatSectors;
       
   584 }
       
   585 
       
   586 /**
       
   587 Returns sectors used for the Fat table in FAT32
       
   588 
       
   589 @internalComponent
       
   590 @released
       
   591 
       
   592 @return iFatSectors32
       
   593 */
       
   594 unsigned int TFATBaseBootSector::FatSectors32() const
       
   595 {
       
   596 	return iFatSectors32;
       
   597 }