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

📄 nand.c

📁 freescale i.mx31 BSP CE5.0全部源码
💻 C
📖 第 1 页 / 共 5 页
字号:
        return FALSE;
    }
    
    if(g_fNandExists != TRUE)
    {
        EdbgOutputDebugString("WARNING: NAND device doesn't exist - unable to retrieve image.\r\n");
        return FALSE;
    }

    // Try to read BOOTSECTION partition from NAND
    hPart = BP_OpenPartition(NEXT_FREE_LOC, USE_REMAINING_SPACE, 
                            PART_BOOTSECTION, FALSE, PART_OPEN_EXISTING);
    if(hPart == INVALID_HANDLE_VALUE)
    {
        EdbgOutputDebugString("ERROR: cannot find BOOTSECTION partition!\r\n");
        return FALSE;
    }

    // Read kernel region info from parameters
    if(BP_ReadData(hPart, (LPBYTE)&nandParams, sizeof(EBOOT_NANDCFG)))
    {
        if(nandParams.ConfigMagicNumber != EbootCFGMagicNumber)
        {
            EdbgOutputDebugString("ERROR: invalid BOOTSECTION data!\r\n");
            return FALSE;
        }
    }
    else
    {
        EdbgOutputDebugString("ERROR: failed to read BOOTSECTION data!\r\n");
        return FALSE;
    }

    EdbgOutputDebugString("Opening BINFS partition\r\n");

    // Open BINFS partition.
    hPart = BP_OpenPartition(NEXT_FREE_LOC, USE_REMAINING_SPACE,
                            PART_BINFS, TRUE, PART_OPEN_EXISTING);
    if(hPart == INVALID_HANDLE_VALUE)
    {
        EdbgOutputDebugString("ERROR: failed to open BINFS partition!\r\n");
        return FALSE;
    }

    EdbgOutputDebugString("Reading kernel region to RAM:\r\n");
    
    // Set the offset into the BINFS partition and read the image.
    if(!BP_SetDataPointer(hPart, nandParams.dwImageOffset))
    {
        EdbgOutputDebugString("ERROR: failed to set data pointer in BINFS partition (address=0x%x).\r\n", nandParams.dwImageOffset);
        return FALSE;
    }

    if(!BP_ReadData(hPart, (LPBYTE)nandParams.dwRegionStart, nandParams.dwRegionLength))
    {
        EdbgOutputDebugString("ERROR: failed to read BINFS data (start=0x%x, length=0x%x).\r\n", 
                                nandParams.dwRegionStart, nandParams.dwRegionLength);
        return FALSE;
    }
    
    // Read chain info to ram if required
    if(nandParams.dwChainLength != 0)
    {
        EdbgOutputDebugString("Reading Chain info to RAM:\r\n");
        if(!BP_SetDataPointer(hPart, nandParams.dwChainOffset))
        {
            EdbgOutputDebugString("ERROR: failed to set data pointer in BINFS partition (address=0x%x).\r\n", nandParams.dwChainOffset);
            return FALSE;
        }

        if(!BP_ReadData(hPart, (LPBYTE)nandParams.dwChainStart, nandParams.dwChainLength))
        {
            EdbgOutputDebugString("ERROR: failed to read BINFS data (start=0x%x, length=0x%x).\r\n", 
                                    nandParams.dwChainStart, nandParams.dwChainLength);
            return FALSE;
        }
    }
        
    // Update launch address
    pBootCfg->LaunchAddress = nandParams.dwLaunchAddr;
    OALMSG(OAL_INFO, (TEXT("NAND LaunchAddress:%X"), pBootCfg->LaunchAddress));
    return TRUE;
}

//-----------------------------------------------------------------------------
//
//  FUNCTION:        ShowNandFlashMenu
//
//  DESCRIPTION:    Nand Flash menu for bootloader.
//
//  PARAMETERS:        
//                    pBootCfg - Ptr to EBOOT configuration structure
//                    CFGChanged - flag TRUE if EBOOT configuration
//                                changed.
//                    
//  RETURNS:        
//                    TRUE if OS images needs to be downloaded.
//                    FALSE otherwise. ie. OS image already in RAM.
//
//-----------------------------------------------------------------------------
BOOL IsNandDevicePresent(void)
{
    return g_fNandExists;
}

//-----------------------------------------------------------------------------
//
//  FUNCTION:        ShowNandFlashMenu
//
//  DESCRIPTION:    Nand Flash menu for bootloader.
//
//  PARAMETERS:        
//                    pBootCfg - Ptr to EBOOT configuration structure
//                    CFGChanged - flag TRUE if EBOOT configuration
//                                changed.
//                    
//  RETURNS:        
//                    TRUE if OS image is read to ram.
//                    FALSE otherwise.
//
//-----------------------------------------------------------------------------
BOOL NandFlashMenu(BOOT_CFG *pBootCfg, BOOL *CFGChanged)
{
    BOOL bExitMenu;
    BOOL bImageExists;
    CHAR selection;

    bExitMenu = FALSE;
    bImageExists = FALSE;
    selection = OEM_DEBUG_READ_NODATA;

    do
    {
        // Nand Flash Menu
        EdbgOutputDebugString("\r\nNAND Flash Menu\r\n");
        EdbgOutputDebugString("NAND flash %s.\r\n", g_fNandExists? "present.":"not found!");
        EdbgOutputDebugString("1) Low-level format. (WARNING: This will destroy any existing OS data!)\r\n");
        EdbgOutputDebugString("2) Write ram image BIN to Flash.(%s)\r\n",
                                    (pBootCfg->ConfigFlags & CONFIG_FLAGS_SAVETOFLASH)? "Enabled":"Disabled");
        EdbgOutputDebugString("3) Launch existing OS image from NAND Flash.\r\n");
#if    DEBUG
        EdbgOutputDebugString("4) Erase EBOOT region from NAND Flash.\r\n");
        EdbgOutputDebugString("5) Erase NANDLoader region from NAND Flash.\r\n");
        EdbgOutputDebugString("6) Display EBOOT image from NAND Flash.\r\n");
        EdbgOutputDebugString("7) Display NANDLoader image from NAND Flash.\r\n");
        EdbgOutputDebugString("8) Set a block as bad.\r\n");
        EdbgOutputDebugString("9) Erase block range.\r\n");
#endif
        EdbgOutputDebugString("B) Return To Main Menu.\r\n");
        EdbgOutputDebugString("Enter your selection: ");

        while(1)
        {
            selection = OEMReadDebugByte();
#if    DEBUG
            if((selection >= '1' && selection <= '9') || 
#else
            if((selection >= '1' && selection <= '3') || 
#endif
                selection == 'B' || selection == 'b')
                break;
        }

        EdbgOutputDebugString ("%c\r\n\r\n", selection);

        switch(selection)
        {
            case '1':
                if(g_fNandExists == TRUE)
                {
                    // Note: We do not want to format the Eboot and Nandloader
                    // regions which are marked as oem reserved. 
                    OALMSG(OAL_INFO, (TEXT("INFO: Performing NAND low level format.\r\n")));
                    if(BP_LowLevelFormat(0, -1, FORMAT_SKIP_RESERVED) != TRUE)
                        OALMSG(OAL_ERROR, (TEXT("ERROR: Failed to low-level format NAND.\r\n")));
                    else
                        OALMSG(OAL_INFO, (TEXT("INFO: Successfully formatted NAND.\r\n")));
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

            case '2':
                if(g_fNandExists == TRUE)
                {
                    if((pBootCfg->ConfigFlags & CONFIG_FLAGS_SAVETOFLASH) == 0)
                        pBootCfg->ConfigFlags |= CONFIG_FLAGS_SAVETOFLASH;
                    else
                        pBootCfg->ConfigFlags &= ~CONFIG_FLAGS_SAVETOFLASH;
                    *CFGChanged = TRUE;
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

            case '3':
                if(g_fNandExists == TRUE)
                {
                    if(ReadKernelRegionFromNand(pBootCfg) == FALSE)
                        OALMSG(OAL_ERROR, (TEXT("ERROR: Failed to load kernel region.\r\n")));
                    else
                    {
                        bImageExists = TRUE;
                        bExitMenu = TRUE;
                    }
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

#if DEBUG
            case '4':
                if(g_fNandExists == TRUE)
                {
                    DWORD dwBlock = FindFirstGoodBlock(1);
                    DWORD dwNumBlock;

                    OALMSG(OAL_INFO, (TEXT("Erasing %d blocks starting at %d.\r\n"), 
                        NAND_IMAGE_NUM_BLOCK(NAND_EBOOT_IMAGE_SIZE, g_FlashInfo.dwBytesPerBlock), dwBlock));
                    for(dwNumBlock = 0; dwNumBlock < NAND_IMAGE_NUM_BLOCK(NAND_EBOOT_IMAGE_SIZE, g_FlashInfo.dwBytesPerBlock); dwBlock++)
                    {
                        if(FMD_EraseBlock(dwBlock) != TRUE)
                            OALMSG(OAL_ERROR, (TEXT("ERROR: Failed to erase EBOOT block %d.\r\n"), dwBlock));
                        else
                            dwNumBlock++;
                    }
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

            case '5':
                if(g_fNandExists == TRUE)
                {
                    OALMSG(OAL_INFO, (TEXT("Erasing block 0.\r\n"))); 
                    if(FMD_EraseBlock(0) != TRUE)
                        OALMSG(OAL_ERROR, (TEXT("ERROR: Failed to erase block 0.\r\n")));
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

            case '6':
                if(g_fNandExists == TRUE)
                {
                    DWORD dwBlockStatus;
                    DWORD dwBlock = FindFirstGoodBlock(1);
                    DWORD dwNumSectors = NAND_IMAGE_NUM_SECTOR(NAND_EBOOT_IMAGE_SIZE, g_FlashInfo.wDataBytesPerSector);
                    DWORD dwStartSectorAddr;
                    DWORD dwDestAddress;
                    DWORD dwCount;
                    BOOL fErr;

                    fErr = FALSE;
                    dwDestAddress = EBOOT_FCACHE_START;
                    while(TRUE)
                    { 
                        dwBlockStatus = FMD_GetBlockStatus(dwBlock);
                        if(!(dwBlockStatus & BLOCK_STATUS_UNKNOWN) && !(dwBlockStatus & BLOCK_STATUS_BAD))
                        {
                            dwStartSectorAddr = dwBlock * g_FlashInfo.wSectorsPerBlock;
                            dwCount = dwNumSectors > g_FlashInfo.wSectorsPerBlock? g_FlashInfo.wSectorsPerBlock : dwNumSectors;
                            if(FMD_ReadSector((SECTOR_ADDR)dwStartSectorAddr,
                                                (LPBYTE)dwDestAddress, NULL, 
                                                dwCount) != TRUE)
                            {
                                OALMSG(OAL_ERROR, (TEXT("ERROR: Failed to read EBOOT image.\r\n")));
                                fErr = TRUE;
                                break;
                            }
                            else
                            {
                                dwNumSectors -= dwCount;
                                dwDestAddress += dwCount * g_FlashInfo.wDataBytesPerSector;
                                if(dwNumSectors == 0)
                                    break;
                            }
                        }
                        else
                            OALMSG(OAL_INFO, (TEXT("block %d bad.\r\n"), dwBlock));

                        dwBlock++;
                    };
                    if(fErr == FALSE)
                    {
                        OALMSG(OAL_INFO, (TEXT("INFO: EBOOT image at %X.\r\n"), EBOOT_FCACHE_START));
                        DisplayMem(EBOOT_FCACHE_START, NAND_EBOOT_IMAGE_SIZE, 0x100);
                    }
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

            case '7':
                if(g_fNandExists == TRUE)
                {
                    if(FMD_ReadSector((SECTOR_ADDR)0,
                                        (LPBYTE)EBOOT_FCACHE_START, NULL, 
                                        NAND_IMAGE_NUM_SECTOR(NAND_LOADER_IMAGE_SIZE, g_FlashInfo.wDataBytesPerSector)) != TRUE)
                    {
                        OALMSG(OAL_ERROR, (TEXT("ERROR: Failed to read NAND Loader image.\r\n")));
                    }
                    else
                    {
                        OALMSG(OAL_INFO, (TEXT("INFO: NAND Loader image at %X.\r\n"), EBOOT_FCACHE_START));
                        DisplayMem(EBOOT_FCACHE_START, NAND_LOADER_IMAGE_SIZE, 0x100);
                    }
                }
                else
                    OALMSG(OAL_INFO, (TEXT("INFO: NAND device non existent.\r\n")));
                break;

            case '8':
                if(g_fNandExists == TRUE)
                {

⌨️ 快捷键说明

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