os/kernelhwsrv/brdbootldr/ubootldr/main.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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
#include <e32rom.h>
sl@0
    35
sl@0
    36
#include <hal.h>
sl@0
    37
#include <d32ethernet.h>
sl@0
    38
sl@0
    39
#define FILE_ID	0x594D555D
sl@0
    40
#include "bootldr.h"
sl@0
    41
#include "ubootldrldd.h"
sl@0
    42
sl@0
    43
#include <d32comm.h>
sl@0
    44
sl@0
    45
#ifdef _DEBUG_CORELDR_
sl@0
    46
#define WAIT_TO_REBOOT  10000000
sl@0
    47
#else
sl@0
    48
#define WAIT_TO_REBOOT    100000
sl@0
    49
#endif
sl@0
    50
sl@0
    51
#define addr_to_page(a) (a&~(0x1000-1))
sl@0
    52
sl@0
    53
_LIT(KPanicText,"UBOOTLDR");
sl@0
    54
_LIT(LDD_NAME, "Enet");
sl@0
    55
_LIT(PDD_NAME, "Ethernet");
sl@0
    56
sl@0
    57
// Memory
sl@0
    58
RUBootldrLdd Ldd;
sl@0
    59
RChunk TheRamChunk;		// This is the download area
sl@0
    60
sl@0
    61
/// Global number of seconds to delay before reboot
sl@0
    62
sl@0
    63
void GetChunk()
sl@0
    64
	{
sl@0
    65
	TInt PageSize;
sl@0
    66
	UserHal::PageSizeInBytes(PageSize);
sl@0
    67
sl@0
    68
	TInt r = User::LoadLogicalDevice(KBootldrLddName);
sl@0
    69
sl@0
    70
	r = Ldd.Open();
sl@0
    71
	if (r!=KErrNone)
sl@0
    72
			{
sl@0
    73
			PrintToScreen(_L("FAULT due to LDD open\r\n"));
sl@0
    74
			BOOT_FAULT();
sl@0
    75
			}
sl@0
    76
sl@0
    77
	TUint8* kernelAddress;
sl@0
    78
	r=Ldd.CreateChunk(KRamTargetSize,(TAny**)&kernelAddress);
sl@0
    79
	if (r!=KErrNone)
sl@0
    80
			{
sl@0
    81
			PrintToScreen(_L("FAULT due to chunk create\r\n"));
sl@0
    82
			BOOT_FAULT();
sl@0
    83
			}
sl@0
    84
sl@0
    85
	r = Ldd.CommitMemory(KRamTargetSize,addr_to_page(KRamTargetAddr));
sl@0
    86
	if (r!=KErrNone)
sl@0
    87
			{
sl@0
    88
			PrintToScreen(_L("FAULT due to commit\r\n"));
sl@0
    89
			BOOT_FAULT();
sl@0
    90
			}
sl@0
    91
sl@0
    92
	r = Ldd.GetChunkHandle(TheRamChunk);
sl@0
    93
	if (r!=KErrNone)
sl@0
    94
			{
sl@0
    95
			PrintToScreen(_L("FAULT due to handle\r\n"));
sl@0
    96
			BOOT_FAULT();
sl@0
    97
			}
sl@0
    98
sl@0
    99
	TUint8* Base = TheRamChunk.Base();
sl@0
   100
	ActualDestinationAddress = (TUint32*)Base;
sl@0
   101
	}
sl@0
   102
sl@0
   103
// method of getting the date string at build time
sl@0
   104
// Taken from e32test\device\t_usbco22.cpp
sl@0
   105
#define WIDEN2(x) L ## x
sl@0
   106
#define WIDEN(x) WIDEN2(x)
sl@0
   107
#define __WDATE__ WIDEN(__DATE__)
sl@0
   108
sl@0
   109
sl@0
   110
//////////////////////////////////////////////////////////////////////////////
sl@0
   111
//
sl@0
   112
// Application entry point
sl@0
   113
//
sl@0
   114
//////////////////////////////////////////////////////////////////////////////
sl@0
   115
LOCAL_C void RunAppL()
sl@0
   116
	{
sl@0
   117
	TInt r;
sl@0
   118
sl@0
   119
	// Get the memory download area mapped into a chunk
sl@0
   120
	GetChunk();
sl@0
   121
sl@0
   122
	// Initialise our graphical screen
sl@0
   123
	InitDisplay();
sl@0
   124
sl@0
   125
#ifdef __USE_VARIANT_INIT__
sl@0
   126
	// The variant initialisation function is likely to perform bootloader
sl@0
   127
	// configuration or print debug information.
sl@0
   128
	VariantInit();
sl@0
   129
#endif
sl@0
   130
sl@0
   131
#ifdef __USE_USBMS__
sl@0
   132
	// If the bootloader is in the USB Mass Storage mode (indicated by the Variant
sl@0
   133
	// initialisation routine) then call that method first; it will either read/boot
sl@0
   134
	// an image, start the USB Mass Storage boot application or return.
sl@0
   135
	if ((LoadDevice==ELoadUSBMS) || (LoadDevice==EBootUSBMS))
sl@0
   136
		TryUSBMS();
sl@0
   137
#endif
sl@0
   138
sl@0
   139
	// Start the menu UI thread, this will cause a console to overwrite the framebuffer
sl@0
   140
	StartMenu();
sl@0
   141
	ClearScreen();
sl@0
   142
sl@0
   143
	PrintToScreen(_L("Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).\r\n"));
sl@0
   144
	PrintToScreen(_L("BootLoader %d.%d\r\n"), BOOTLDR_VERSION_MAJOR, BOOTLDR_VERSION_MINOR);
sl@0
   145
	PrintToScreen(_L("Platform build %d\n\rBuilt: %s\r\n"), PLATFORM_BUILD, __WDATE__);
sl@0
   146
sl@0
   147
	// Load Ethernet driver:  it might need to load to set the MAC address in HAL
sl@0
   148
    TInt pddLoaded = KErrUnknown;
sl@0
   149
	TInt lddLoaded = KErrUnknown;
sl@0
   150
	TInt opened    = KErrUnknown;
sl@0
   151
	RBusDevEthernet card;
sl@0
   152
sl@0
   153
    pddLoaded = User::LoadPhysicalDevice(PDD_NAME);
sl@0
   154
	if (pddLoaded == KErrNone)
sl@0
   155
		{
sl@0
   156
		lddLoaded = User::LoadLogicalDevice(LDD_NAME);
sl@0
   157
		if (lddLoaded == KErrNone)
sl@0
   158
			{
sl@0
   159
			opened = card.Open(0);
sl@0
   160
			User::After(2000);
sl@0
   161
			}
sl@0
   162
		}
sl@0
   163
sl@0
   164
	TInt serialNum;
sl@0
   165
	r = HAL::Get(HAL::ESerialNumber, serialNum);
sl@0
   166
sl@0
   167
	if (r == KErrNone && serialNum != 0)
sl@0
   168
		{
sl@0
   169
		// we only show the serial number if it was available from HAL and set to something
sl@0
   170
		PrintToScreen(_L("MAC Address: 00-0a-%02x-%02x-%02x-%02x\r\n"),
sl@0
   171
			(serialNum & 0xFF000000) >> 24,
sl@0
   172
			(serialNum & 0x00FF0000) >> 16,
sl@0
   173
			(serialNum & 0x0000FF00) >>  8,
sl@0
   174
			(serialNum & 0x000000FF)
sl@0
   175
			);
sl@0
   176
		}
sl@0
   177
sl@0
   178
sl@0
   179
	// Free the Ethernet driver now we're done getting the MAC address
sl@0
   180
	if (opened == KErrNone)
sl@0
   181
		{
sl@0
   182
		card.Close();
sl@0
   183
		}
sl@0
   184
	if (lddLoaded == KErrNone)
sl@0
   185
		{
sl@0
   186
	    User::FreeLogicalDevice(LDD_NAME);
sl@0
   187
		}
sl@0
   188
	if (pddLoaded == KErrNone)
sl@0
   189
		{
sl@0
   190
	    User::FreePhysicalDevice(PDD_NAME);
sl@0
   191
		}
sl@0
   192
sl@0
   193
	if (LoadDevice == EBootEMMC)
sl@0
   194
		{
sl@0
   195
		if ( SearchDrivesRaw() )
sl@0
   196
			{
sl@0
   197
			DoDownload();
sl@0
   198
			}
sl@0
   199
		}
sl@0
   200
		
sl@0
   201
#ifdef __USE_LOCAL_DRIVES__
sl@0
   202
	// The next method to test is reading an image file from removable media
sl@0
   203
	if (SearchDrives())
sl@0
   204
		DoDownload();
sl@0
   205
#endif
sl@0
   206
sl@0
   207
	// The last method is to attempt to download an image over the serial port
sl@0
   208
	switch (SerialBaud)
sl@0
   209
		{
sl@0
   210
		case EBps115200:
sl@0
   211
			PrintToScreen(_L("Initiating YModem-G on port %d @ 115200 baud\r\n"), SerialDownloadPort);
sl@0
   212
			break;
sl@0
   213
		case EBps230400:
sl@0
   214
			PrintToScreen(_L("Initiating YModem-G on port %d @ 230400 baud\r\n"), SerialDownloadPort);
sl@0
   215
			break;
sl@0
   216
		default:
sl@0
   217
			PrintToScreen(_L("Initiating YModem-G download on port %d\r\n"), SerialDownloadPort);
sl@0
   218
			SerialBaud = EBps115200;
sl@0
   219
			break;
sl@0
   220
		}
sl@0
   221
	r=InitSerialDownload(SerialDownloadPort);
sl@0
   222
	if (r!=KErrNone)
sl@0
   223
		{
sl@0
   224
		RDebug::Print(_L("FAULT: YModem-G download returned %d\r\n"),r);
sl@0
   225
		BOOT_FAULT();
sl@0
   226
		}
sl@0
   227
sl@0
   228
	PrintToScreen(_L("Receiving %lS\r\nSize %d\r\n"),&FileName,FileSize);
sl@0
   229
	DoDownload();
sl@0
   230
}
sl@0
   231
sl@0
   232
TUint Check(const TUint32* aPtr, TInt aSize)
sl@0
   233
	{
sl@0
   234
	TUint sum=0;
sl@0
   235
	aSize/=4;
sl@0
   236
	while (aSize-->0)
sl@0
   237
		sum+=*aPtr++;
sl@0
   238
	return sum;
sl@0
   239
	}
sl@0
   240
sl@0
   241
TUint CheckSumRom()
sl@0
   242
	{
sl@0
   243
#if 0
sl@0
   244
	//
sl@0
   245
	// This code was obtained from t_romchk in the e32tests, it was useful for
sl@0
   246
	// checking that roms were not getting corrupted during the download
sl@0
   247
	// process.
sl@0
   248
	//
sl@0
   249
	// However, it is not a universal checksum and is not suitable for all
sl@0
   250
	// types of images it is therefore being disabled until we need it again.
sl@0
   251
	//
sl@0
   252
sl@0
   253
	PrintToScreen(_L("Checking ROM contents...\r\n"));
sl@0
   254
sl@0
   255
	const TRomHeader* romHdr = (const TRomHeader*)DestinationAddress();
sl@0
   256
	TInt size = romHdr->iUnpagedUncompressedSize;
sl@0
   257
	const TUint32* addr = (TUint32*)romHdr;
sl@0
   258
	PrintToScreen(_L("ROM at %x, size %x\r\n"),addr,size);
sl@0
   259
sl@0
   260
	TUint checkSum = Check(addr,size);
sl@0
   261
sl@0
   262
	// hack the checksum because ROMBUILD is broken
sl@0
   263
	checkSum -= (romHdr->iRomSize-size)/4; // adjust for missing 0xffffffff
sl@0
   264
	checkSum -= romHdr->iCompressionType;
sl@0
   265
	checkSum -= romHdr->iUnpagedCompressedSize;
sl@0
   266
	checkSum -= romHdr->iUnpagedUncompressedSize;
sl@0
   267
sl@0
   268
	TUint expectedChecksum = 0x12345678;
sl@0
   269
	if (checkSum != expectedChecksum)
sl@0
   270
		{
sl@0
   271
		PrintToScreen(_L("Fail: Checksum = %8x, expected %8x\r\n"),checkSum,expectedChecksum);
sl@0
   272
		RDebug::Print(_L("Fail: Checksum = %8x, expected %8x\r\n"),checkSum,expectedChecksum);
sl@0
   273
		BOOT_FAULT();
sl@0
   274
		}
sl@0
   275
	else
sl@0
   276
		{
sl@0
   277
		PrintToScreen(_L("Pass: Checksum = %8x, expected %8x\r\n"),checkSum,expectedChecksum);
sl@0
   278
		RDebug::Print(_L("Pass: Checksum = %8x, expected %8x\r\n"),checkSum,expectedChecksum);
sl@0
   279
		}
sl@0
   280
#endif
sl@0
   281
	return 0;
sl@0
   282
	}
sl@0
   283
sl@0
   284
sl@0
   285
GLDEF_C void DoDownload()
sl@0
   286
/*
sl@0
   287
 The DoDownload method is ultimatly called by all the download routines, it is
sl@0
   288
 responsible for unzipping images as well as stripping off EPOC headers and
sl@0
   289
 starting the flash writing methods.
sl@0
   290
 */
sl@0
   291
	{
sl@0
   292
	TInt r;
sl@0
   293
sl@0
   294
	// If the image is zipped start the unzip thread
sl@0
   295
	if (ImageZip)
sl@0
   296
		{
sl@0
   297
		// initialise unzip
sl@0
   298
		PrintToScreen(_L("Loading zip ..\r\n"));
sl@0
   299
		r=DoZipDownload(bootFile);
sl@0
   300
		if (r!=KErrNone)
sl@0
   301
			{
sl@0
   302
			PrintToScreen(_L("\r\nFAULT due to zip %d\r\n"), r);
sl@0
   303
			BOOT_FAULT();
sl@0
   304
			}
sl@0
   305
		PrintToScreen(_L("\r\nZip download complete.\r\n"));
sl@0
   306
		}
sl@0
   307
	else
sl@0
   308
		{
sl@0
   309
		LoadSize=FileSize;
sl@0
   310
				
sl@0
   311
		if( ImageDeflated )
sl@0
   312
    		{
sl@0
   313
    		PrintToScreen(_L("\r\n\r\nLoading deflated image...\r\n"));
sl@0
   314
    	    ImageHeaderPresent=EFalse;
sl@0
   315
    	    DoDeflateDownload();
sl@0
   316
    	    PrintToScreen(_L("Deflated image download complete.\r\n"));	  
sl@0
   317
    		}
sl@0
   318
		else
sl@0
   319
    		{
sl@0
   320
    		// If it isn't a zipped or deflated image then the filesize is the image size -
sl@0
   321
    		// check that it is valid for a Symbian image and determine whether to
sl@0
   322
    		// remove any header that may be present.
sl@0
   323
    		TInt size_mod_4k=FileSize & 0xfff;
sl@0
   324
    		if (size_mod_4k==0)
sl@0
   325
    			{
sl@0
   326
    			ImageHeaderPresent=EFalse;
sl@0
   327
    			PrintToScreen(_L("Image header not present.\r\n"));
sl@0
   328
    			}
sl@0
   329
    		else if (size_mod_4k==256)
sl@0
   330
    			{
sl@0
   331
    			ImageHeaderPresent=ETrue;
sl@0
   332
    			PrintToScreen(_L("Image header present.\r\n"));
sl@0
   333
    			}
sl@0
   334
    		else
sl@0
   335
    			{
sl@0
   336
    		    PrintToScreen(_L("\r\n\r\nInvalid size\r\n"));
sl@0
   337
    			BOOT_FAULT();	
sl@0
   338
    			}
sl@0
   339
    		ImageSize=ImageHeaderPresent ? LoadSize-256 : LoadSize;
sl@0
   340
    		TUint8* pD=(TUint8*)DestinationAddress();
sl@0
   341
    		TInt block_size;
sl@0
   342
    		if (FileSize==0)
sl@0
   343
    			block_size=0x1000;		// YModem download with unknown size
sl@0
   344
    		else
sl@0
   345
    			block_size=Max(0x1000,FileSize>>8);
sl@0
   346
    		block_size=(block_size+0xfff)&~0xfff;
sl@0
   347
    		if (FileSize>0)
sl@0
   348
    			InitProgressBar(0,(TUint)FileSize,_L("LOAD"));
sl@0
   349
    		r=KErrNone;
sl@0
   350
sl@0
   351
// If target location is flash start the flasher thread
sl@0
   352
#ifdef __SUPPORT_FLASH_REPRO__
sl@0
   353
    		if (LoadToFlash)
sl@0
   354
    			{
sl@0
   355
    			r=InitFlashWrite();
sl@0
   356
    			if (r!=KErrNone)
sl@0
   357
    				{
sl@0
   358
    				PrintToScreen(_L("FAULT due to InitFlashWrite return %d\r\n"), r);
sl@0
   359
    				BOOT_FAULT();
sl@0
   360
    				}
sl@0
   361
    			}
sl@0
   362
#endif
sl@0
   363
sl@0
   364
    		while (r==KErrNone)
sl@0
   365
    			{
sl@0
   366
    			TInt len=block_size;
sl@0
   367
    			r=ReadInputData(pD,len);
sl@0
   368
sl@0
   369
    			if (r!=KErrNone && r!=KErrEof)
sl@0
   370
    				break;
sl@0
   371
    			pD+=len;
sl@0
   372
    			ImageReadProgress+=len;
sl@0
   373
    			if (FileSize>0)
sl@0
   374
    				{
sl@0
   375
    				UpdateProgressBar(0,(TUint)ImageReadProgress);
sl@0
   376
				}
sl@0
   377
#ifdef __SUPPORT_FLASH_REPRO__
sl@0
   378
    			if (LoadToFlash)
sl@0
   379
    				NotifyDataAvailable(ImageReadProgress);
sl@0
   380
#endif
sl@0
   381
    			}
sl@0
   382
sl@0
   383
    		if (r!=KErrEof)	
sl@0
   384
    			{
sl@0
   385
    			PrintToScreen(_L("FAULT due to EOF. %d bytes read.\r\n"), ImageReadProgress);
sl@0
   386
    			BOOT_FAULT();
sl@0
   387
    			}
sl@0
   388
    		else
sl@0
   389
    			PrintToScreen(_L("Loaded %d bytes.\r\n"), ImageReadProgress);
sl@0
   390
sl@0
   391
    		if (ImageReadProgress < LoadSize)
sl@0
   392
    			ImageSize=ImageHeaderPresent ? ImageReadProgress-256 : ImageReadProgress;
sl@0
   393
#ifdef __SUPPORT_FLASH_REPRO__
sl@0
   394
    		if (LoadToFlash)
sl@0
   395
    			NotifyDownloadComplete();
sl@0
   396
#endif
sl@0
   397
    		}
sl@0
   398
sl@0
   399
		}
sl@0
   400
	if (CloseInputFunction)
sl@0
   401
		CloseInput();
sl@0
   402
sl@0
   403
	if (LoadToFlash)
sl@0
   404
		{
sl@0
   405
		// In the load to flash scenario it will boot the image.
sl@0
   406
		while(1)
sl@0
   407
			User::After(10000000);
sl@0
   408
		}
sl@0
   409
sl@0
   410
	// Booting from Ram - remove the header
sl@0
   411
	if (ImageHeaderPresent)
sl@0
   412
		{
sl@0
   413
		TUint8* pI=(TUint8*)DestinationAddress();
sl@0
   414
		Mem::Move(pI,pI+256,ImageSize);
sl@0
   415
		}
sl@0
   416
sl@0
   417
	CheckSumRom();
sl@0
   418
sl@0
   419
	PrintToScreen(_L("Booting Image...\r\n"));
sl@0
   420
	// Restart board with "boot image + located in ram"
sl@0
   421
sl@0
   422
	User::After(WAIT_TO_REBOOT);
sl@0
   423
		
sl@0
   424
sl@0
   425
	HAL::Set(HAL::EDisplayState, 0);
sl@0
   426
	Restart(KtRestartReasonBootRestart | KtRestartReasonRAMImage);
sl@0
   427
sl@0
   428
	//  *** NOTREACHED ***
sl@0
   429
	}
sl@0
   430
sl@0
   431
GLDEF_C TInt E32Main()
sl@0
   432
	{
sl@0
   433
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   434
sl@0
   435
	// Make bootloader panics fault the system. This means that this process
sl@0
   436
	// will NEVER END
sl@0
   437
	User::SetCritical(User::ESystemPermanent);
sl@0
   438
sl@0
   439
	TRAPD(error,RunAppL());
sl@0
   440
	__ASSERT_ALWAYS(!error, User::Panic(KPanicText, error));
sl@0
   441
	
sl@0
   442
	delete cleanup;
sl@0
   443
sl@0
   444
	PrintToScreen(_L("Boot loader ended abnormally")); 
sl@0
   445
sl@0
   446
	Ldd.Close();
sl@0
   447
	return 0;
sl@0
   448
	}
sl@0
   449