📄 msapi_bootloader.c
字号:
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2006-2007 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// (¨MStar Confidential Information〃) by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// This file is commonly used by bootloader code and system(main) code.
// Must be backward compatible with previously released version of bootloader code.
////////////////////////////////////////////////////////////////////////////////////////////
#define MSAPI_BOOTLOADER_C
/******************************************************************************/
/* Header Files */
/******************************************************************************/
// C Library
#include <stdio.h>
// Global Layer
#include "sysinfo.h"
// Driver Layer
#include "DrvSys.h"
#include "DrvTimer.h"
#include "DrvMIU.h"
#include "DrvFlash.h"
#include "DrvPower.h"
#include "DrvScaler.h"
// API Layer
#include "msAPI_Ram.h"
#include "msAPI_Bootloader.h"
// Image
#include "imginfo.h"
#include "Bin_ID.h"
// Device
#include "msGPIO.h"
/******************************************************************************/
/* Macro */
/******************************************************************************/
#define DL_DBG(x) //x
#define MEMBER_OFFSET(struct_name, member_name) ( (U16) &(((##struct_name *)NULL)->##member_name) ) // 16-bit Offset
#define EEPROM_OFFSET(member_name) (RM_ADDR_DOWNLOAD+MEMBER_OFFSET(ST_DOWNLOAD_INFO, member_name)) // ST_DOWNLOAD_INFO member offset on EEPROM
/******************************************************************************/
/* Local Variables */
/******************************************************************************/
/******************************************************************************/
/* Local Functions */
/******************************************************************************/
/******************************************************************************/
/* Global Functions */
/******************************************************************************/
#if (BOOTLOADER_SYSTEM)
#if (!ENABLE_EEPROM)
#error "Must enable EEPROM for bootloader system"
#endif
/******************************************************************************/
/// API to do bootup check and, if nessary, pass execution to main code
/// Check integrity of main image and the download type.
/// Reboot to main code if main image is OK and no need to do download.
/******************************************************************************/
void msAPI_BLoader_BootupSwitch(void)
{
g_bMainImageOK = msAPI_BLoader_CheckIntegrity();
g_u8BootupDownloadType = msAPI_BLoader_GetDownloadType();
if (g_bMainImageOK == TRUE && g_u8BootupDownloadType == DOWNLOAD_TYPE_NONE)
{
// Reboot to main code
MDrv_Sys_RunCodeInSPI();
MDrv_MIU_SPI_SetOffset(BOOTLOADER_CODE_SIZE);
MDrv_Sys_Reboot(); // Software reset
}
//msAPI_BLoader_ClearDownloadInfo(); // Enabled for debug only
}
#endif // #if (BOOTLOADER_SYSTEM)
/******************************************************************************/
/// API to do whole-chip reset to run bootloader
/// Use watchdog reset to do whole-chip reset
/******************************************************************************/
void msAPI_BLoader_Reboot(void)
{
MDrv_MIU_SPI_SetOffset(0LU);
MDrv_Sys_SetWatchDogTimer(0);
MDrv_Sys_EnableWatchDog();
while(1); // Necessary because of using watch dog reset
}
/******************************************************************************/
/// API to clear download information to zeros
/******************************************************************************/
void msAPI_BLoader_ClearDownloadInfo(void)
{
U16 u16Index;
for (u16Index=0; u16Index<RM_SIZE_DOWNLOAD; u16Index++)
{
msAPI_rmWriteByte(RM_ADDR_DOWNLOAD+u16Index, 0x00);
}
}
#if (BOOTLOADER_SYSTEM)
/******************************************************************************/
/// API to get download type
/// @return EN_DOWNLOAD_TYPE
/******************************************************************************/
EN_DOWNLOAD_TYPE msAPI_BLoader_GetDownloadType(void)
{
if (msAPI_rmReadByte(EEPROM_OFFSET(u8IntegrityID)) == DOWNLOAD_INTEGRITY_ID)
{
switch (msAPI_rmReadByte(EEPROM_OFFSET(u8Type)))
{
case DOWNLOAD_TYPE_OAD:
return DOWNLOAD_TYPE_OAD;
case DOWNLOAD_TYPE_1KXMODEM:
DL_DBG(printf("BLoader: DOWNLOAD_TYPE_1KXMODEM\n"));
return DOWNLOAD_TYPE_1KXMODEM;
case DOWNLOAD_TYPE_USB_USER:
return DOWNLOAD_TYPE_USB_USER;
case DOWNLOAD_TYPE_USB_EXPERT:
return DOWNLOAD_TYPE_USB_EXPERT;
}
}
DL_DBG(printf("BLoader: DOWNLOAD_TYPE_NONE\n"));
return DOWNLOAD_TYPE_NONE;
}
/******************************************************************************/
/// API to do integrity check of system code on flash
/// @return BOOLEAN
/// - # TRUE Integrity check passed.
/// - # FALSE Integrity check failed.
/******************************************************************************/
BOOLEAN msAPI_BLoader_CheckIntegrity(void)
{
U32 data u32Value;
U32 data u32FlashOffset;
U16 data u16PackCount;
BINFORMAT Fmt;
// Read system code magic number
u32FlashOffset = IMG_INFO_OFFSET + BOOTLOADER_CODE_SIZE + MEMBER_OFFSET(MS_IMG_INFO, u32Magic);
MDrv_Flash_Read(u32FlashOffset, 4, (U8 *)&u32Value);
// Check system code magic number
if (u32Value != MAGIC_APP)
{
DL_DBG(printf("System magic number (0x%LX) not match\n", u32Value));
return FALSE;
}
u32FlashOffset = IMG_INFO_OFFSET + BOOTLOADER_CODE_SIZE + MEMBER_OFFSET(MS_IMG_INFO, u32PackInfoFlashOffset);
MDrv_Flash_Read(u32FlashOffset, 4, (U8 *)&u32FlashOffset);
u32FlashOffset += BOOTLOADER_CODE_SIZE;
DL_DBG(printf("System image pack info addr: 0x%LX\n", u32FlashOffset));
// Get the total count in the pack header
MDrv_Flash_Read(u32FlashOffset, 2, (U8 *)&u16PackCount);
if (u16PackCount > 0)
{
MDrv_Flash_Read(u32FlashOffset+2 + ((u16PackCount-1) * sizeof(BINFORMAT)), sizeof(BINFORMAT), (U8 *)&Fmt);
if ( Fmt.B_ID == BIN_ID_MAGIC_55AAABCD )
{
u32FlashOffset = Fmt.B_FAddr + BOOTLOADER_CODE_SIZE;
// Read image end magic number
MDrv_Flash_Read(u32FlashOffset, 4, (U8 *)&u32Value);
// Check image end magic number
if (u32Value == MAGIC_IMAGE_END)
{
DL_DBG(printf("Integrity check: PASS\n"));
return TRUE;
}
DL_DBG(printf("System image end magic number (0x%LX) not match\n", u32Value));
}
else
{
DL_DBG(printf("Invalid start magic number\n"));
}
}
else
{
DL_DBG(printf("Pack count=0\n"));
}
DL_DBG(printf("Integrity check: FAILED\n"));
return FALSE;
}
/******************************************************************************/
/// API to get download file names
/// @param pau8ShortFilePath \b IN The returning path with short file name
/// @param pau8LongFileName \b IN The returning long file name
/******************************************************************************/
void msAPI_BLoader_GetDownloadPath(U8 *pau8ShortFilePath, U8 *pau8LongFileName)
{
msAPI_rmBurstReadBytes(EEPROM_OFFSET(au8ShortFileName), pau8ShortFilePath, DOWNLOAD_MAX_SHORTNAME_LEN + 1);
msAPI_rmBurstReadBytes(EEPROM_OFFSET(au8LongFileName), pau8LongFileName, DOWNLOAD_MAX_LONGNAME_LEN + 1);
}
#else
/******************************************************************************/
/// API to set download type
/// @param u8DownloadType \b IN Download tpye defined in EN_DOWNLOAD_TYPE
/******************************************************************************/
void msAPI_BLoader_SetDownloadType(EN_DOWNLOAD_TYPE u8DownloadType)
{
msAPI_rmWriteByte(EEPROM_OFFSET(u8IntegrityID), DOWNLOAD_INTEGRITY_ID);
msAPI_rmWriteByte(EEPROM_OFFSET(u8Type), u8DownloadType);
}
#endif
/******************************************************************************/
/// API to set download file names
/// @param pau8ShortFilePath \b IN Path with short file name
/// @param pau8LongFileName \b IN Long file name
/******************************************************************************/
void msAPI_BLoader_SetDownloadPath(U8 *pau8ShortFilePath, U8 *pau8LongFileName)
{
msAPI_rmBurstWriteBytes(EEPROM_OFFSET(au8ShortFileName), pau8ShortFilePath, DOWNLOAD_MAX_SHORTNAME_LEN + 1);
msAPI_rmBurstWriteBytes(EEPROM_OFFSET(au8LongFileName), pau8LongFileName, DOWNLOAD_MAX_LONGNAME_LEN + 1);
}
#undef MSAPI_BOOTLOADER_C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -