os/kernelhwsrv/brdbootldr/ubootldr/locdrv.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// improvised boot loader mechanism
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
/**
sl@0
    19
 @file
sl@0
    20
*/
sl@0
    21
sl@0
    22
#include <e32const.h>
sl@0
    23
#include <e32const_private.h>
sl@0
    24
#include <e32std.h>
sl@0
    25
#include <e32std_private.h>
sl@0
    26
#include <e32svr.h>
sl@0
    27
#include <e32cons.h>
sl@0
    28
#include <f32file.h>
sl@0
    29
#include <hal.h>
sl@0
    30
#include <u32hal.h>
sl@0
    31
#include "bootloader_variantconfig.h"
sl@0
    32
#include <nkern/nk_trace.h>
sl@0
    33
#include <e32twin.h>
sl@0
    34
sl@0
    35
#define FILE_ID	0x594D555D
sl@0
    36
#include "bootldr.h"
sl@0
    37
sl@0
    38
void CloseAndDeleteFile();
sl@0
    39
RFs TheFs;
sl@0
    40
sl@0
    41
// Extra stuff to determine inner compression from the ROM header
sl@0
    42
#include <e32rom.h>
sl@0
    43
extern TInt memcmp1(const TUint8* aTrg, const TUint8* aSrc, TInt aLength);
sl@0
    44
sl@0
    45
// XXX FIXED DRIVE PATH
sl@0
    46
const TPtrC filePath=_L("d:\\");
sl@0
    47
sl@0
    48
#if defined(__SUPPORT_UNZIP__)
sl@0
    49
const TBool ZipIsSupported = ETrue;
sl@0
    50
#else
sl@0
    51
const TBool ZipIsSupported = EFalse;
sl@0
    52
#endif
sl@0
    53
sl@0
    54
#if defined(__SUPPORT_FLASH_REPRO__)
sl@0
    55
const TBool FlashIsSupported = ETrue;
sl@0
    56
#else
sl@0
    57
const TBool FlashIsSupported = EFalse;
sl@0
    58
#endif
sl@0
    59
sl@0
    60
typedef struct 
sl@0
    61
	{
sl@0
    62
	TPtrC filename;
sl@0
    63
	TBool Zip;        // set to ETrue if this file is a ZIP file
sl@0
    64
	TBool Flash;      // set to ETrue if this file is to be written to flash
sl@0
    65
	TBool BootLoader; // set to ETrue if this file is to be flashed to the BootLoader address
sl@0
    66
	TBool Delete;     // set to ETrue if this file is to be deleted after loading into RAM
sl@0
    67
	}
sl@0
    68
	TFileTypes;
sl@0
    69
sl@0
    70
sl@0
    71
TFileTypes supportedFileTypes [] =
sl@0
    72
	{
sl@0
    73
		// Name              Zip     Flash   BootLoader  Delete
sl@0
    74
		{_L("FLASHLDR.ZIP"), ETrue,  ETrue,  ETrue,      ETrue   },
sl@0
    75
		{_L("FLASHLDR.BIN"), EFalse, ETrue,  ETrue,      ETrue   },
sl@0
    76
sl@0
    77
		{VARIANT_ZIP,        ETrue,  EFalse, EFalse,     EFalse  },
sl@0
    78
		{VARIANT_BIN,        EFalse, EFalse, EFalse,     EFalse  },
sl@0
    79
sl@0
    80
		{_L("SYS$ROM.ZIP"),  ETrue,  EFalse, EFalse,     EFalse  },
sl@0
    81
		{_L("SYS$ROM.BIN"),  EFalse, EFalse, EFalse,     EFalse  },
sl@0
    82
sl@0
    83
        {_L("FLASHIMG.ZIP"), ETrue,  ETrue,  EFalse,     EFalse  },
sl@0
    84
		{_L("FLASHIMG.BIN"), EFalse, ETrue,  EFalse,     EFalse  },
sl@0
    85
        {_L("BOOTLDR.ZIP"),  ETrue,  ETrue,  ETrue,      EFalse  },
sl@0
    86
		{_L("BOOTLDR.BIN"),  EFalse, ETrue,  ETrue,      EFalse  },
sl@0
    87
sl@0
    88
		{_L("COREIMG.BIN"),  EFalse, EFalse, EFalse,     ETrue   },
sl@0
    89
sl@0
    90
		{_L(""),0} // Last Entry - this empty row is used in code to detect table end
sl@0
    91
	};
sl@0
    92
sl@0
    93
sl@0
    94
GLDEF_C TBool SearchDrivesRaw()
sl@0
    95
	{
sl@0
    96
	// Scan local drives directly (i.e. via TLocalDrv rather than RFS)
sl@0
    97
	
sl@0
    98
	PrintToScreen(_L("Checking local drives directly.\r\n"));
sl@0
    99
	
sl@0
   100
	TDriveInfoV1Buf diBuf;
sl@0
   101
	UserHal::DriveInfo(diBuf);
sl@0
   102
	TDriveInfoV1 &di=diBuf();
sl@0
   103
	
sl@0
   104
	
sl@0
   105
	LocDrvChg = EFalse;
sl@0
   106
	LocDrvPos = 0;
sl@0
   107
	TInt LocalDriveNum = KErrNotFound;
sl@0
   108
	TInt r = KErrNone;
sl@0
   109
	TInt n=0;
sl@0
   110
	for ( ; n<KMaxLocalDrives && LocalDriveNum == KErrNotFound; n++)
sl@0
   111
		{
sl@0
   112
		r = LocDrv.Connect(n, LocDrvChg);
sl@0
   113
sl@0
   114
		if(r != KErrNone)
sl@0
   115
			{
sl@0
   116
			RDebug::Print(_L("\nDrive %d: TBusLocalDrive::Connect() failed %d"), n, r);
sl@0
   117
			continue;
sl@0
   118
			}	
sl@0
   119
sl@0
   120
	    TLocalDriveCapsV5Buf capsBuf;
sl@0
   121
	    TLocalDriveCapsV5& caps = capsBuf();
sl@0
   122
		r = LocDrv.Caps(capsBuf);
sl@0
   123
		if(r != KErrNone)
sl@0
   124
			{
sl@0
   125
			RDebug::Print(_L("\nDrive %d: TBusLocalDrive::Caps() failed %d"), n, r);
sl@0
   126
			continue;
sl@0
   127
			}
sl@0
   128
		else
sl@0
   129
			{
sl@0
   130
			PrintToScreen(_L("Found Drive %d OK\r\n"),n);
sl@0
   131
			RDebug::Print(_L("\nDrive %d: %S"), n, &di.iDriveName[n]);
sl@0
   132
			RDebug::Print(_L("PartitionType %X"), caps.iPartitionType);
sl@0
   133
			RDebug::Print(_L("PartitionSize %ld"), caps.iSize);
sl@0
   134
			
sl@0
   135
			// Check that drive is labelled as MMC or SDIO, 
sl@0
   136
			// note that this is a platform specific label...
sl@0
   137
			if ((di.iDriveName[n].MatchF(_L("MultiMediaCard0")) == KErrNone) ||
sl@0
   138
			    (di.iDriveName[n].MatchF(_L("SDIOCard0")) == KErrNone))
sl@0
   139
				{
sl@0
   140
				if (caps.iPartitionType == KPartitionTypeROM)
sl@0
   141
					{
sl@0
   142
					RDebug::Print(_L("- ROM Partition"));
sl@0
   143
					LocalDriveNum = n;
sl@0
   144
					FileSize = caps.iSize;
sl@0
   145
					break;
sl@0
   146
					}
sl@0
   147
				}
sl@0
   148
			
sl@0
   149
			CloseLocalDrive();
sl@0
   150
			}
sl@0
   151
		}
sl@0
   152
	
sl@0
   153
	if (LocalDriveNum == KErrNotFound)
sl@0
   154
		{
sl@0
   155
		RDebug::Print(_L("No ROM Partitions found"));
sl@0
   156
		return EFalse;
sl@0
   157
		}
sl@0
   158
	else
sl@0
   159
		{
sl@0
   160
		RDebug::Print(_L("Query ROM in drive %d"), LocalDriveNum);
sl@0
   161
		}
sl@0
   162
	
sl@0
   163
	InputFunction=ReadFromLocalDrive;
sl@0
   164
	CloseInputFunction=CloseLocalDrive;
sl@0
   165
	
sl@0
   166
	RDebug::Print(_L("Determine the compression"));
sl@0
   167
	
sl@0
   168
    r = GetInnerCompression(ImageDeflated, RomLoaderHeaderExists);
sl@0
   169
    
sl@0
   170
    if(KErrNone != r)
sl@0
   171
        {
sl@0
   172
        PrintToScreen(_L("Unable to determine the compression!\r\n"));
sl@0
   173
	    BOOT_FAULT();    
sl@0
   174
        }
sl@0
   175
    else
sl@0
   176
    	{
sl@0
   177
    	// Put position back to start
sl@0
   178
    	LocDrvPos = 0;
sl@0
   179
    	}
sl@0
   180
	
sl@0
   181
	return ETrue;
sl@0
   182
	}
sl@0
   183
sl@0
   184
sl@0
   185
TInt ReadFromLocalDrive(TUint8* aDest, TInt& aLength)
sl@0
   186
	{
sl@0
   187
	// construct as TPtr8(TUint8 *aBuf, TInt aMaxLength);
sl@0
   188
	// .. because TBusLocalDrive.Read only understands descriptors
sl@0
   189
	TPtr8 d(aDest, aLength);
sl@0
   190
	
sl@0
   191
	TInt r = LocDrv.Read(LocDrvPos,aLength,d);
sl@0
   192
	LocDrvPos+=aLength;
sl@0
   193
	
sl@0
   194
	if (LocDrvPos >= FileSize)
sl@0
   195
		{
sl@0
   196
		// ROM read completely
sl@0
   197
		r = KErrEof;
sl@0
   198
		}
sl@0
   199
	
sl@0
   200
	return r;
sl@0
   201
	}
sl@0
   202
sl@0
   203
sl@0
   204
void CloseLocalDrive()
sl@0
   205
	{
sl@0
   206
	LocDrv.Disconnect();
sl@0
   207
	}
sl@0
   208
sl@0
   209
sl@0
   210
GLDEF_C TBool SearchDrives()
sl@0
   211
	{
sl@0
   212
	// set up the list of files to look for -- this code will search through
sl@0
   213
	// all available drives until it finds one of these files in a root
sl@0
   214
	// directory
sl@0
   215
sl@0
   216
	PrintToScreen(_L("Checking local drives.\r\n"));
sl@0
   217
sl@0
   218
	// search for files
sl@0
   219
	TInt r = TheFs.Connect();
sl@0
   220
	if (r != KErrNone)
sl@0
   221
		{
sl@0
   222
		RDebug::Print(_L("FAULT: Connecting RFs returned %d\r\n"),r);
sl@0
   223
		BOOT_FAULT();
sl@0
   224
		}
sl@0
   225
sl@0
   226
	TFindFile finder(TheFs);
sl@0
   227
sl@0
   228
	// for each file in the list
sl@0
   229
	TInt NrChecked = 0;
sl@0
   230
	r = KErrNotFound;
sl@0
   231
sl@0
   232
	while ( (*(supportedFileTypes[NrChecked].filename.Ptr()) != 0) && (r == KErrNotFound))
sl@0
   233
		{
sl@0
   234
		if (  (!ZipIsSupported && (supportedFileTypes[NrChecked].Zip))
sl@0
   235
		   || (!FlashIsSupported && (supportedFileTypes[NrChecked].Flash))
sl@0
   236
		   )
sl@0
   237
			{
sl@0
   238
			//skip ZIP/FLASH file types if they aren't supported
sl@0
   239
			}
sl@0
   240
		else
sl@0
   241
			{
sl@0
   242
			TPtrC thisFile = supportedFileTypes[NrChecked].filename;
sl@0
   243
			r = finder.FindByDir(thisFile, filePath);
sl@0
   244
sl@0
   245
			if (r == KErrNone)
sl@0
   246
				PrintToScreen(_L("Found %s\r\n"), thisFile.Ptr());
sl@0
   247
			}
sl@0
   248
		NrChecked++;
sl@0
   249
		}
sl@0
   250
sl@0
   251
	// if found
sl@0
   252
	if (r == KErrNone)
sl@0
   253
		{
sl@0
   254
		// setup some flags 
sl@0
   255
		LoadFile=--NrChecked; // predecrement since this was incremented at the end of the last loop
sl@0
   256
		LoadDevice=ELoadDrive;
sl@0
   257
sl@0
   258
		const TPtrC bootFileName = finder.File();
sl@0
   259
sl@0
   260
		PrintToScreen(_L("Opening: %s\r\n"), supportedFileTypes[NrChecked].filename.Ptr());
sl@0
   261
sl@0
   262
		r = bootFile.Open(TheFs, bootFileName, EFileRead);
sl@0
   263
sl@0
   264
		if (r != KErrNone)
sl@0
   265
			{
sl@0
   266
			PrintToScreen(_L("Bootfile failed to open - err %d.\r\n"),r);
sl@0
   267
sl@0
   268
			BOOT_FAULT();
sl@0
   269
			}
sl@0
   270
sl@0
   271
		ImageZip         = supportedFileTypes[NrChecked].Zip;
sl@0
   272
		LoadToFlash      = supportedFileTypes[NrChecked].Flash;
sl@0
   273
		FlashBootLoader  = supportedFileTypes[NrChecked].BootLoader;
sl@0
   274
		
sl@0
   275
		InputFunction=ReadFromFile;
sl@0
   276
		CloseInputFunction = supportedFileTypes[NrChecked].Delete ? CloseAndDeleteFile : CloseFile;
sl@0
   277
sl@0
   278
        if( !ImageZip)
sl@0
   279
            {
sl@0
   280
            r = GetInnerCompression(ImageDeflated, RomLoaderHeaderExists);
sl@0
   281
            if(KErrNone != r)
sl@0
   282
                {
sl@0
   283
                PrintToScreen(_L("Unable to determine the compression!\r\n"));
sl@0
   284
			    BOOT_FAULT();    
sl@0
   285
                }
sl@0
   286
            else
sl@0
   287
            	{
sl@0
   288
            	// Move file pos back to the beginning
sl@0
   289
            	TInt pos = 0;
sl@0
   290
                r = bootFile.Seek(ESeekStart, pos);
sl@0
   291
            	}
sl@0
   292
            }
sl@0
   293
sl@0
   294
		r = bootFile.Size(FileSize);
sl@0
   295
sl@0
   296
		if (r == KErrNone)
sl@0
   297
			{
sl@0
   298
			PrintToScreen(_L("Opened, size: %d bytes.\r\n"), FileSize);
sl@0
   299
			if(ImageDeflated)
sl@0
   300
    			{
sl@0
   301
    			PrintToScreen(_L("ROM Image is deflated.\r\n"));    
sl@0
   302
    			}
sl@0
   303
			}
sl@0
   304
		else
sl@0
   305
			{
sl@0
   306
			PrintToScreen(_L("Unable to read file size\r\n"));
sl@0
   307
			BOOT_FAULT();
sl@0
   308
			}
sl@0
   309
sl@0
   310
		// Found image - return true
sl@0
   311
		return ETrue;
sl@0
   312
		}
sl@0
   313
sl@0
   314
	// else NOT FOUND
sl@0
   315
	return EFalse;
sl@0
   316
	}
sl@0
   317
sl@0
   318
TInt ReadFromFile(TUint8* aDest, TInt& aLength)
sl@0
   319
	{
sl@0
   320
	// construct as TPtr8(TUint8 *aBuf, TInt aMaxLength);
sl@0
   321
	// .. because RFile.Read only understands descriptors
sl@0
   322
	TPtr8 d(aDest, aLength);
sl@0
   323
sl@0
   324
	TInt r = bootFile.Read(d);
sl@0
   325
sl@0
   326
	if (d.Length() < aLength) // may happen at the end of a file
sl@0
   327
		{
sl@0
   328
		aLength = d.Length();
sl@0
   329
		}
sl@0
   330
sl@0
   331
	if (d.Length() == 0)	// indicates end of file
sl@0
   332
		{
sl@0
   333
		if (FileSize-aLength == ImageReadProgress)
sl@0
   334
			return KErrEof;
sl@0
   335
		else
sl@0
   336
			// this will drop through to the error code below and will fault
sl@0
   337
			return KErrGeneral;
sl@0
   338
		}
sl@0
   339
sl@0
   340
	return r;
sl@0
   341
	}
sl@0
   342
sl@0
   343
void CloseFile()
sl@0
   344
	{
sl@0
   345
	bootFile.Close();
sl@0
   346
	}
sl@0
   347
sl@0
   348
void CloseAndDeleteFile()
sl@0
   349
	{
sl@0
   350
	TFileName fileName;
sl@0
   351
	TInt r = bootFile.FullName(fileName);
sl@0
   352
	if (r != KErrNone)
sl@0
   353
		PrintToScreen(_L("CloseAndDeleteFile() RFile::FullName returned %d"), r);
sl@0
   354
sl@0
   355
	bootFile.Close();
sl@0
   356
sl@0
   357
	r = TheFs.Delete(fileName);
sl@0
   358
	PrintToScreen(_L("Deleted file fileName %S"), &fileName);
sl@0
   359
	}
sl@0
   360
sl@0
   361
//#define TROM_LOADER_HEADER_SIZE 0x100
sl@0
   362
#define BUFFER_SIZE         (TROM_LOADER_HEADER_SIZE + sizeof(TRomHeader))
sl@0
   363
sl@0
   364
TInt GetInnerCompression(TBool &aImageDeflated, TBool &aRomLoaderHeaderExists )
sl@0
   365
    {
sl@0
   366
    TInt r = KErrNone;
sl@0
   367
    
sl@0
   368
    TUint8 buffer[BUFFER_SIZE];
sl@0
   369
    TInt   bufferSize = BUFFER_SIZE;
sl@0
   370
    
sl@0
   371
    const TUint8 * romLoaderSignature1 = (const TUint8*)"EPOC";
sl@0
   372
    const TUint8 * romLoaderSignature2 = (const TUint8*)"ROM";
sl@0
   373
    
sl@0
   374
    r = ReadInputData((TUint8*)&buffer, bufferSize);
sl@0
   375
	if( KErrNone!=r)
sl@0
   376
		{
sl@0
   377
		PrintToScreen(_L("Unable to read loader headers... (size:%d)\r\n"), bufferSize);
sl@0
   378
		BOOT_FAULT();    
sl@0
   379
		}
sl@0
   380
	else
sl@0
   381
    	{
sl@0
   382
    	// Check headers
sl@0
   383
    	TRomHeader* romHeader= (TRomHeader *) &buffer;
sl@0
   384
    	aRomLoaderHeaderExists = EFalse;
sl@0
   385
    	
sl@0
   386
    	if( !memcmp1(buffer, romLoaderSignature1, 4) && !memcmp1(&buffer[8], romLoaderSignature2, 3) )
sl@0
   387
    	    {
sl@0
   388
            // We have TRomLoaderHeader skip it
sl@0
   389
            romHeader = (TRomHeader *) (&buffer[TROM_LOADER_HEADER_SIZE]);
sl@0
   390
            aRomLoaderHeaderExists = ETrue;
sl@0
   391
    	    }
sl@0
   392
    	
sl@0
   393
        if(romHeader->iCompressionType == 0 )
sl@0
   394
            {
sl@0
   395
             RDebug::Print(_L("Image is NOT Compressed"));
sl@0
   396
             aImageDeflated = EFalse;
sl@0
   397
             FileSize = romHeader->iUncompressedSize;
sl@0
   398
             PrintToScreen(_L("ROMSIZE:%d\r\n"), romHeader->iUncompressedSize);
sl@0
   399
             RDebug::Print(_L("ROMSIZE:%d"), romHeader->iUncompressedSize);
sl@0
   400
             
sl@0
   401
         	if (romHeader->iPageableRomStart > 0)
sl@0
   402
         		{
sl@0
   403
         		PrintToScreen(_L("Paged ROM FOUND\r\n"));
sl@0
   404
         		RDebug::Print(_L("Paged ROM FOUND"));
sl@0
   405
         		FileSize = romHeader->iPageableRomStart;
sl@0
   406
         		RDebug::Print(_L("Unpaged ROMSIZE:%d"), romHeader->iPageableRomStart);
sl@0
   407
         		}          
sl@0
   408
            }
sl@0
   409
        else if (romHeader->iCompressionType == KUidCompressionDeflate )
sl@0
   410
            {
sl@0
   411
            RDebug::Print(_L("Image is Compressed\r\n"));
sl@0
   412
            aImageDeflated = ETrue;
sl@0
   413
            FileSize = romHeader->iUnpagedUncompressedSize;
sl@0
   414
            RDebug::Print(_L("Compressed ROMSIZE:%d\r\n"), romHeader->iUnpagedUncompressedSize);       
sl@0
   415
            }
sl@0
   416
        else
sl@0
   417
            {
sl@0
   418
            RDebug::Print(_L("Not supported compression method:0x%08x\r\n"), romHeader->iCompressionType);
sl@0
   419
            PrintToScreen(_L("Not supported compression method:0x%08x\r\n"), romHeader->iCompressionType);
sl@0
   420
            r = KErrNotSupported;
sl@0
   421
            }
sl@0
   422
     	}
sl@0
   423
	return r;        
sl@0
   424
    }
sl@0
   425