navienginebootldr/bootloader_variant.cpp
changeset 0 5de814552237
equal deleted inserted replaced
-1:000000000000 0:5de814552237
       
     1 /*
       
     2 * Copyright (c) 2008-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 "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 * base\bsp\hwip_nec_naviengine\ne1_tb_bootloader\bootloader_variant.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #define FILE_ID	0x464C5348
       
    22 #include "bootldr.h"
       
    23 #include "ubootldrldd.h"
       
    24 #include <e32std.h>
       
    25 #include <e32std_private.h>
       
    26 #include <e32svr.h>
       
    27 #include <e32cons.h>
       
    28 #include <f32file.h>
       
    29 #include <hal.h>
       
    30 #include <u32hal.h>
       
    31 #include "flash_nor.h"
       
    32 #include <d32comm.h>
       
    33 
       
    34 TLinAddr ConfigBlockAddress;							// Virtual address
       
    35 
       
    36 // RAM copy of config block
       
    37 SBootloaderConfig aConfigBlock;
       
    38 
       
    39 // Invoke the flash_nor routines to read a block containing the bootloader
       
    40 // configuration.
       
    41 //
       
    42 // If the block is found to be invalid, create a new block from defaults.
       
    43 //
       
    44 GLDEF_C void ReadConfig()
       
    45 	{
       
    46 	TLinAddr FlashAddr;
       
    47 	FlashAddr = (TLinAddr) GetFlashChunk();	// Map flash
       
    48 
       
    49 	// Config block is last block of the bootloader space before a flashed image
       
    50 	ConfigBlockAddress = FlashAddr + KNORFlashMaxBootloaderSize - KFlashEraseBlockSize;
       
    51 
       
    52 	// Make sure the region is not blank and that the first word has the
       
    53 	// bootloader cfg uid on it.
       
    54 	if (!BlankCheck(ConfigBlockAddress, KConfigBlockSize) &&
       
    55 		(*(volatile TUint32*)ConfigBlockAddress == KUidUBootldrCfgMagic))
       
    56 		{
       
    57 		// Copy the config block out of flash and into ram
       
    58 		volatile TUint32* j=(volatile TUint32*)ConfigBlockAddress;
       
    59 		TUint32* dest = (TUint32 *)&aConfigBlock;
       
    60 
       
    61 		for (TUint i=0 ; i<KConfigBlockSize ; ++i, ++dest, ++j)
       
    62 			*dest=*j;
       
    63 		}
       
    64 	else
       
    65 		{
       
    66 		PrintToScreen(_L("Config area was blank, setting defaults\r\n"));
       
    67 
       
    68 		aConfigBlock.iMagic = KUidUBootldrCfgMagic;
       
    69 		aConfigBlock.iPortNumber = KUartPortNumber;
       
    70 		aConfigBlock.iBaudRate   = EBps115200;
       
    71 		aConfigBlock.iLoadDevice = ELoadDrive;
       
    72 
       
    73 		// Write the new block to flash
       
    74 		WriteConfig();
       
    75 		}
       
    76 
       
    77 	// Configure bootloader from config block
       
    78 	SerialDownloadPort = aConfigBlock.iPortNumber;
       
    79 	SerialBaud         = aConfigBlock.iBaudRate;
       
    80 	LoadDevice = (TLoadDevice)aConfigBlock.iLoadDevice;
       
    81 
       
    82 	// The H4 supports downloading images via the USB Mass Storage method. As
       
    83 	// the method of detecting a USB-MS boot is rather awkward the task is
       
    84 	// performed at a variant level.
       
    85 	if (LoadDevice==ELoadUSBMS)
       
    86 		{
       
    87 		// Read the custom restart reason from the hal
       
    88 		TInt aRestartReason;
       
    89 		TInt r = HAL::Get(HAL::ECustomRestartReason, aRestartReason);
       
    90 		if (r!=KErrNone)
       
    91 			{
       
    92 			PrintToScreen(_L("Failed to read restart reason from HAL (0x%x)"), r);
       
    93 			RDebug::Printf("Failed to read restart reason from HAL (0x%x)", r);
       
    94 			BOOT_FAULT();
       
    95 			}
       
    96 
       
    97 		// Decode the On H4 the restart reasons get shifted internally
       
    98 		if (aRestartReason & (KtRestartCustomRestartUSBLoader >> KsRestartCustomRestartReasons))
       
    99 			{
       
   100 			// In the boot scenario the indicate that the bootloader should
       
   101 			// check for an appropriate file before restarting the USB Mass
       
   102 			// Storage application.
       
   103 			LoadDevice = EBootUSBMS;
       
   104 			}
       
   105 		}
       
   106 	}
       
   107 
       
   108 
       
   109 // Write the config block back into flash
       
   110 GLDEF_C void WriteConfig()
       
   111 	{
       
   112 	aConfigBlock.iPortNumber=SerialDownloadPort;
       
   113 	aConfigBlock.iBaudRate  =SerialBaud;
       
   114 	aConfigBlock.iLoadDevice=LoadDevice;
       
   115 	// XXX Generate a checksum for the config block (excluding the checksum)
       
   116 
       
   117 	TInt r = Erase(ConfigBlockAddress, KFlashEraseBlockSize);
       
   118 	if (r!=KErrNone)
       
   119 		{
       
   120 		PrintToScreen(_L("Config block failed erase (0x%x)"), r);
       
   121 		RDebug::Printf("Config block failed erase (0x%x)", r);
       
   122 		BOOT_FAULT();
       
   123 		}
       
   124 
       
   125 	r=Write(ConfigBlockAddress, KFlashEraseBlockSize, (TUint32 *)&aConfigBlock);
       
   126 	if (r!=KErrNone)
       
   127 		{
       
   128 		PrintToScreen(_L("Config block failed write (0x%x)"), r);
       
   129 		RDebug::Printf("Config block failed write (0x%x)", r);
       
   130 		BOOT_FAULT();
       
   131 		}
       
   132 	}
       
   133 
       
   134 
       
   135 GLDEF_C void VariantInit()
       
   136 /**
       
   137  * Can place variant code here.
       
   138  */
       
   139 	{
       
   140 	// Set the bootloader configuration based on values saved in Flash
       
   141 	ReadConfig();
       
   142 
       
   143 	// Other variant actions e.g. print out the board information
       
   144 
       
   145 	return;
       
   146 	}