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