⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bootconfig.c

📁 Freescale ARM9系列CPU MX27的WINCE 5.0下的BSP
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//-----------------------------------------------------------------------------
//
//  Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Copyright (C) 2004-2006,2007 Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//-----------------------------------------------------------------------------
//
// File: bootconfig.c
//
// BOOT configuration access routines.
//
//-----------------------------------------------------------------------------
#include <windows.h>

#include "bsp.h"

#if (BOOTCFG_STORAGE == BOOTCFG_STORAGE_NAND)
#include <fmd.h>
#include "nand.h"
#endif

//------------------------------------------------------------------------------
// External Variables

//------------------------------------------------------------------------------
// Defines

//------------------------------------------------------------------------------
// Types

//------------------------------------------------------------------------------
// Global Variables

//------------------------------------------------------------------------------
// Local Variables
#if (BOOTCFG_STORAGE == BOOTCFG_STORAGE_NAND)
static DWORD g_dwBootConfigBlock = -1;
#endif

//------------------------------------------------------------------------------
// External  Functions
BOOL NORLoadEBootCFG(BYTE * pBootCfg, DWORD cbBootCfgSize);
BOOL NORStoreEBootCFG(BYTE * pBootCfg, DWORD cbBootCfgSize);
//------------------------------------------------------------------------------
// Local Functions
static DWORD inet_addr(char *pszDottedD);

//------------------------------------------------------------------------------
//
// Function: OALReadBootCFG
//
// This function is used to load the stored bootloader configuration from
// permanent storage. It also checks if the configuration values are valid,
// if not, resets to default values.
//
// Parameters:
//      pBootCFG
//          [out] ptr to boot configuration data store
//
// Returns:
//      TRUE if boot configuration read successfully. FALSE otherwise.
//
//------------------------------------------------------------------------------
#if (BOOTCFG_STORAGE == BOOTCFG_STORAGE_NAND)
BOOL OALReadBootCFG(BOOT_CFG *pBootCFG)
{
    FlashInfo flashInfo;
    BYTE sector[512];   // hardcode cos EBOOT no malloc service
    DWORD dwCount, dwStartSectorAddr, i, j, k;
    PVOID handle = NULL;
    BOOL success = TRUE;
    DWORD dwStatus;

    OALMSG(OAL_FUNC, (TEXT("INFO: Reading boot configuration from NAND.\r\n")));

    handle = FMD_Init(NULL, NULL, NULL);
    if (handle == NULL) {
        OALMSG(OAL_ERROR, (TEXT("ERROR: OALReadBootCFG: FMD_Init failed.\r\n")));
        success = FALSE;
        goto _done;
    }
    
    if (!FMD_GetInfo(&flashInfo)) {
        OALMSG(OAL_ERROR, (TEXT("ERROR: Unable to get NAND flash info.\r\n")));
        success = FALSE;
        goto _done;
    }
    
    dwCount = NAND_IMAGE_NUM_SECTOR(sizeof(BOOT_CFG), flashInfo.wDataBytesPerSector);
    if (dwCount > flashInfo.dwNumBlocks) {
        OALMSG(OAL_ERROR, (TEXT("ERROR: Boot config size exceeded max.\r\n")));
        success = FALSE;
        goto _done;
    }
    
    // Read NAND block to get boot configuration.
    // Boot configuration is stored in sector 0 of last good block.
    if (g_dwBootConfigBlock == -1) {
        for (i = flashInfo.dwNumBlocks - 1; i >= 0; i--) {
            DWORD dwStatus;
            dwStatus = FMD_GetBlockStatus(i);
            if ((dwStatus & BLOCK_STATUS_BAD) == 0) {
                g_dwBootConfigBlock = i;
                OALMSG(OAL_FUNC, (TEXT("INFO: last good block found: %d status 0x%x\r\n"), 
                    g_dwBootConfigBlock, dwStatus));
                break;
            }
        }
        
        if (g_dwBootConfigBlock == -1 || g_dwBootConfigBlock == 0) {
            OALMSG(OAL_ERROR, (TEXT("ERROR: Invalid boot configuration storage block %d!\r\n"),
                g_dwBootConfigBlock));
            success = FALSE;
            goto _done;
        }
    }
    
    dwStatus = FMD_GetBlockStatus(g_dwBootConfigBlock);
    
    OALMSG(OAL_FUNC, (TEXT("INFO: block %d status 0x%08x\r\n"), g_dwBootConfigBlock, dwStatus));
    
    if ((dwStatus ^ (BLOCK_STATUS_RESERVED | BLOCK_STATUS_READONLY)) != 0) {
        if (!FMD_SetBlockStatus(g_dwBootConfigBlock, (BLOCK_STATUS_RESERVED | BLOCK_STATUS_READONLY))) {
            OALMSG(OAL_ERROR, (TEXT("ERROR: cannot reserve block %d for boot config!\r\n"),
                g_dwBootConfigBlock));
            success = FALSE;
            goto _done;
        }
        else
            OALMSG(OAL_INFO, (TEXT("INFO: block %d is reserved for boot configuration\r\n"),
                g_dwBootConfigBlock));
    }
    
    OALMSG(OAL_FUNC, (TEXT("INFO: Boot configuration block: %d\r\n"), g_dwBootConfigBlock));
    OALMSG(OAL_FUNC, (TEXT("INFO: Boot configuration sectors: %d\r\n"), dwCount));
    
    dwStartSectorAddr = g_dwBootConfigBlock * flashInfo.wSectorsPerBlock;
    
    for (i = 0, j = 0; i < dwCount; i++, j += flashInfo.wDataBytesPerSector) {
        if (FMD_ReadSector((SECTOR_ADDR)dwStartSectorAddr, sector, NULL, 1)) {
            k = ((sizeof(BOOT_CFG) - j) < flashInfo.wDataBytesPerSector)? (sizeof(BOOT_CFG) - j) : flashInfo.wDataBytesPerSector;
            memcpy(&pBootCFG[j], sector, k);
            dwStartSectorAddr++;
        } else {
            OALMSG(OAL_ERROR, (TEXT("ERROR: Unable to read boot configuration from sector %d.\r\n"), i));
            success = FALSE;
            goto _done;
        }
    }

_done:
    // Make sure that signature is valid. Otherwise reset to default factory settings
    if (pBootCFG->Signature != CONFIG_SIGNATURE ||
        pBootCFG->VerMajor != CONFIG_VERSION_MAJOR ||
        pBootCFG->VerMinor != CONFIG_VERSION_MINOR)
    {
        OALResetBootCFG(pBootCFG);
        // Store default BOOT configuration
        OALWriteBootCFG(pBootCFG);
    }

    if(handle)
        FMD_Deinit(handle);
        
    return success;
}

#elif (BOOTCFG_STORAGE == BOOTCFG_STORAGE_EEPROM)
BOOL OALReadBootCFG(BOOT_CFG *pBootCFG)
{
    OALMSG(OAL_FUNC, (TEXT("OALReadBootCFG+\r\n")));

    // Init EEPROM to get boot configuration
    if(BSPEEPROMInit((PBYTE)BSP_BASE_REG_PA_CS8900A_IOBASE) == TRUE)
        BSPEEPROMReadUserData(0, (UINT16 *)pBootCFG, sizeof(BOOT_CFG) / 2);
    else
        OALMSG(OAL_ERROR, (TEXT("INFO: OALReadBootCFG: BSPEEPROMInit failed.\r\n")));

    OALMSG(OAL_INFO, (TEXT("OALReadBootCFG:  pBootCFG->Signature = 0x%x\r\n"), pBootCFG->Signature));
    OALMSG(OAL_INFO, (TEXT("OALReadBootCFG:  pBootCFG->VerMajor = 0x%x\r\n"), pBootCFG->VerMajor));
    OALMSG(OAL_INFO, (TEXT("OALReadBootCFG:  pBootCFG->VerMinor = 0x%x\r\n"), pBootCFG->VerMinor));
    
    // Make sure that signature is valid. Otherwise reset to default factory settings
    if (pBootCFG->Signature != CONFIG_SIGNATURE ||
        pBootCFG->VerMajor != CONFIG_VERSION_MAJOR ||
        pBootCFG->VerMinor != CONFIG_VERSION_MINOR)
    {
        OALResetBootCFG(pBootCFG);
        // Store default BOOT configuration
        OALWriteBootCFG(pBootCFG);
        return FALSE;
    }

    OALMSG(OAL_FUNC, (TEXT("OALReadBootCFG-\r\n")));

    return TRUE;
}

#elif (BOOTCFG_STORAGE == BOOTCFG_STORAGE_NOR)
BOOL OALReadBootCFG(BOOT_CFG *pBootCFG)
{
    BOOL success = TRUE;

    if (!NORLoadEBootCFG((BYTE*) pBootCFG, sizeof(BOOT_CFG))) {
        KITLOutputDebugString("ERROR: LoadEBootCFG: failed to load configuration.\r\n");
        success = FALSE;
    }

    // Make sure that signature is valid. Otherwise reset to default factory settings
    if (pBootCFG->Signature != CONFIG_SIGNATURE ||
        pBootCFG->VerMajor != CONFIG_VERSION_MAJOR ||
        pBootCFG->VerMinor != CONFIG_VERSION_MINOR)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -