imgtools/romtools/rofsbuild/r_smrimage.cpp
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 /*
       
     2 * Copyright (c) 1996-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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <stdlib.h>
       
    20 #include <string.h>
       
    21 #include <time.h>
       
    22 
       
    23 #ifdef __VC32__
       
    24  #ifdef __MSVCDOTNET__
       
    25   #include <strstream>
       
    26   #include <iomanip>
       
    27  #else //__MSVCDOTNET__
       
    28   #include <strstrea.h>
       
    29   #include <iomanip.h>
       
    30  #endif  //__MSVCDOTNET__
       
    31 #else // !__VC32__
       
    32 #ifdef __TOOLS2__
       
    33 	#include <sstream>
       
    34 	#include <iomanip>
       
    35 	#include <sys/stat.h>
       
    36 	#include <new>
       
    37 	using namespace std;
       
    38 #else
       
    39 	#include <strstrea.h>
       
    40 	#include <iomanip.h>
       
    41 #endif
       
    42  
       
    43 #endif //__VC32__
       
    44 #include <e32std.h>
       
    45 #include "h_utl.h"
       
    46 #include "r_smrimage.h"
       
    47 
       
    48 CSmrImage::CSmrImage(CObeyFile* aObeyFile)
       
    49 	:iObeyFile(aObeyFile)
       
    50 {	
       
    51 	HMem::Set(&iSmrRomHeader, 0, sizeof(SSmrRomHeader));
       
    52 }
       
    53 CSmrImage::~CSmrImage()
       
    54 {
       
    55 }
       
    56 TBool CSmrImage::SetImageName(const StringVector& aValues)
       
    57 {
       
    58 	if(aValues.size() == 0)
       
    59 	{
       
    60 		Print(EError, "Keyword Imageanme has not been set!");
       
    61 		return EFalse;
       
    62 
       
    63 	}
       
    64 	if(aValues.size() > 1)
       
    65 	{
       
    66 		Print(EError, "Keyword Imagename has been set more than one time!");
       
    67 		return EFalse;
       
    68 	}
       
    69 	iImageName = aValues.at(0);
       
    70 	if(iImageName.find(".img") == std::string::npos)
       
    71 	{
       
    72 		iImageName += ".img";
       
    73 	}
       
    74 	return ETrue;
       
    75 }
       
    76 TBool CSmrImage::SetFormatVersion(const StringVector& aValues)
       
    77 {
       
    78 	if(aValues.size() == 0)
       
    79 	{
       
    80 		Print(EError, "keyword formatversion has not been set!");
       
    81 		return EFalse;
       
    82 	}
       
    83 	if(aValues.size() > 1)
       
    84 	{
       
    85 		Print(EError, "Keyword Formatversion has been set more than one time!");
       
    86 		return EFalse;
       
    87 	}
       
    88 	iSmrRomHeader.iImageVersion = StrToInt(aValues.at(0).c_str());
       
    89 	return ETrue;
       
    90 }
       
    91 TBool CSmrImage::SetHcrData(const StringVector& aValues)
       
    92 {
       
    93 	
       
    94 	if(aValues.size() == 0)
       
    95 	{
       
    96 		Print(EError, "keyword hcrdata has not been set!");
       
    97 		return EFalse;
       
    98 	}
       
    99 	if(aValues.size() > 1)
       
   100 	{
       
   101 		Print(EError, "Keyword hcrdata has been set more than one time!");
       
   102 		return EFalse;
       
   103 	}
       
   104 	iHcrData = aValues.at(0);
       
   105 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
   106 	ifstream is(iHcrData.c_str(), ios_base::binary );
       
   107 #else //!__MSVCDOTNET__
       
   108 	ifstream is(iHcrData.c_str(), ios::nocreate | ios::binary ); 
       
   109 #endif //__MSVCDOTNET__
       
   110 	if(!is)
       
   111 	{
       
   112 		Print(EError, "HCR data file: %s dose not exist!", iHcrData.c_str());
       
   113 		return EFalse;
       
   114 	}
       
   115 	TUint32 magicWord = 0;
       
   116 	is.read(reinterpret_cast<char*>(&magicWord),sizeof(TUint32));
       
   117 	if(0x66524348 != magicWord){
       
   118 		Print(EError, "HCR data file: %s is an invalid HCR data file!", iHcrData.c_str());
       
   119 		return EFalse;
       
   120 	}
       
   121 	is.close();
       
   122 	return ETrue;
       
   123 }
       
   124 TBool CSmrImage::SetPayloadUID(const StringVector& aValues)
       
   125 {
       
   126 
       
   127 	if(aValues.size() == 0)
       
   128 	{
       
   129 		Print(EError, "keyword PayloadUID has not been set!");
       
   130 		return EFalse;
       
   131 	}
       
   132 	if(aValues.size() > 1)
       
   133 	{
       
   134 		Print(EError, "Keyword PayloadUID has been set more than one time!");
       
   135 		return EFalse;
       
   136 	}
       
   137 	iSmrRomHeader.iPayloadUID = StrToInt(aValues.at(0).c_str());
       
   138 	return ETrue;
       
   139 }
       
   140 TBool CSmrImage::SetPayloadFlags(const StringVector& aValues)
       
   141 {
       
   142 
       
   143 	if(aValues.size() == 0)
       
   144 	{
       
   145 		Print(EError, "keyword Payloadflags has not been set!");
       
   146 		return EFalse;
       
   147 	}
       
   148 	if(aValues.size() > 1)
       
   149 	{
       
   150 		Print(EError, "Keyword Payloadfalgs has been set more than one time!");
       
   151 		return EFalse;
       
   152 	}
       
   153 	iSmrRomHeader.iPayloadFlags = StrToInt(aValues.at(0).c_str());
       
   154 	return ETrue;
       
   155 }
       
   156 TUint32 CSmrImage::StrToInt(const char* aStr)
       
   157 {
       
   158 
       
   159 	TUint32 value;
       
   160 #ifdef __TOOLS2__
       
   161 	istringstream val(aStr);
       
   162 #else
       
   163 	istrstream val(aStr, strlen(aStr));
       
   164 #endif
       
   165 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
   166 	val >> setbase(0);
       
   167 #endif //__MSVCDOTNET
       
   168 	val >> value;
       
   169 	return value;
       
   170 }
       
   171 TInt CSmrImage::Initialise()
       
   172 {
       
   173 	TInt result = KErrGeneral;
       
   174 	if(! SetImageName(iObeyFile->getValues("imagename")))
       
   175 		return result;
       
   176 	if(! SetFormatVersion(iObeyFile->getValues("formatversion")))
       
   177 		return result;
       
   178 	if(! SetHcrData(iObeyFile->getValues("hcrdata")))
       
   179 		return result;
       
   180 	if(! SetPayloadUID(iObeyFile->getValues("payloaduid")))
       
   181 		return result;
       
   182 	if(! SetPayloadFlags(iObeyFile->getValues("payloadflags")))
       
   183 		return result;
       
   184 	result = KErrNone;
       
   185 	return result;
       
   186 }
       
   187 TInt CSmrImage::CreateImage()
       
   188 {
       
   189 	TInt imageSize = 0;
       
   190 	ifstream is;
       
   191 	is.open(iHcrData.c_str(), ios::binary);
       
   192 	if(!is)
       
   193 	{
       
   194 		Print(EError, "Open HCR data file: %s error!\n", iHcrData.c_str());
       
   195 		return KErrGeneral;
       
   196 	}
       
   197 	is.seekg(0, ios::end);
       
   198 	TInt fsize = is.tellg();
       
   199 	imageSize = sizeof(SSmrRomHeader) + fsize;
       
   200 	imageSize += (4 - imageSize) & 3;
       
   201 	char* vImage = new (std::nothrow) char[imageSize];
       
   202 	if(vImage == NULL)
       
   203 	{
       
   204 		Print(EError, "Not enough system memory generate SMR partition!\n");
       
   205 		return KErrNoMemory;
       
   206 	}
       
   207 	HMem::Set(vImage, 0, imageSize);
       
   208 	HMem::Copy(vImage, &iSmrRomHeader, sizeof(SSmrRomHeader));
       
   209 	SSmrRomHeader* pSmrHeader = (SSmrRomHeader *) vImage;
       
   210 	pSmrHeader->iFingerPrint[0] = 0x5F524D53;
       
   211 	pSmrHeader->iFingerPrint[1] = 0x54524150;
       
   212 	is.seekg(0, ios::beg);
       
   213 	is.read(vImage + sizeof(SSmrRomHeader), fsize);
       
   214 	pSmrHeader->iPayloadChecksum = HMem::CheckSum((TUint *)(vImage + sizeof(SSmrRomHeader)), imageSize - sizeof(SSmrRomHeader));
       
   215 	pSmrHeader->iImageSize = imageSize;
       
   216 	pSmrHeader->iImageTimestamp = time(0);
       
   217 	ofstream os(iImageName.c_str(), ios::binary);
       
   218 	os.write(vImage, imageSize);
       
   219 	os.close();
       
   220 	is.close();
       
   221 	delete[] vImage;
       
   222 	
       
   223 	
       
   224 	return KErrNone;
       
   225 
       
   226 
       
   227 }