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

📄 flash.c

📁 Windows CE 6.0 BSP for VOIP sample phone. Intel PXA270 platform.
💻 C
📖 第 1 页 / 共 2 页
字号:
    unsigned char ucFlashSectorBuffer[BSP_SECTOR_SIZE];

    DWORD bytesRemaining = 0;
    DWORD readCount = 0;
    HANDLE hFMD = NULL;
    FlashInfo flashInfo;    
    SectorInfo sectorInfo;
    SECTOR_ADDR sector;
    SECTOR_ADDR sectorCount;

    KITLOutputDebugString("INFO: Reading OS Image from Flash...\r\n");
    
    // Initialize BSP_MDOC_NK_PARTITION for read
    //
    hFMD = FMD_Init((LPCTSTR)BSP_MDOC_NK_PARTITION_HANDLE, NULL, NULL);
    if (!hFMD)
    {
        KITLOutputDebugString("ERROR: ReadFlashNK: FMD_Init call failed\r\n");
        goto cleanUp;
    }

    // Get flash info
    if (!FMD_GetInfo(&flashInfo))
    {
        KITLOutputDebugString("ERROR: FMD_GetInfo call failed\r\n");
        goto cleanUp;
    }
    
    // Make sure that there is image in flash memory first
    // Read the first sector and check for signature
    if (!FMD_ReadSector(0, ucFlashSectorBuffer, &sectorInfo, 1) )
    {
        KITLOutputDebugString("\r\nERROR: Failed read sector %d from flash\r\n", 0);
        goto cleanUp;
    }

    if (*((UINT32 *)&ucFlashSectorBuffer[ROM_SIGNATURE_OFFSET]) != ROM_SIGNATURE) 
    {
        KITLOutputDebugString("ERROR: Image Signature in Flash Memory not found\r\n");
        goto cleanUp;
    }

    // Find out the start address of IMAGE in RAM
    pImage = (UINT8*)IMAGE_WINCE_OSIMAGE_RAM_UA_START;
    
    // copy the one sector that was read to RAM
    memcpy(pImage, ucFlashSectorBuffer, flashInfo.wDataBytesPerSector);
    bytesImageCopied += flashInfo.wDataBytesPerSector;
    
    // next read till we get the TOC, so that we know how much to read from Flash
    offsetTOC = *((UINT32 *)&ucFlashSectorBuffer[ROM_TOC_OFFSET_OFFSET]) ;
    endTOC = offsetTOC + sizeof(ROMHDR);
    
    sector = 1; // we already read one sector
    sectorCount = (endTOC + flashInfo.wDataBytesPerSector) / flashInfo.wDataBytesPerSector - 1;
    while (sectorCount-- > 0) 
    {      
        if (!FMD_ReadSector(sector, ucFlashSectorBuffer, &sectorInfo, 1) )
        {
            KITLOutputDebugString("\r\nERROR: Failed read sector %d from flash\r\n", sector);
            goto cleanUp;
        }
       
        memcpy(pImage + bytesImageCopied, ucFlashSectorBuffer, flashInfo.wDataBytesPerSector);
        bytesImageCopied += flashInfo.wDataBytesPerSector;
        sector++;
    }  

    // we should have the TOC now
    pTOC = (ROMHDR*)(pImage + offsetTOC);

#ifdef DEBUG
    DumpTOC(pTOC);
#endif
    
    // Image base must be same as we expect
    if (pTOC->physfirst != IMAGE_WINCE_OSIMAGE_RAM_CA_START) 
    {
        KITLOutputDebugString("ERROR: Image in flash memory has incorrect base\r\n");
        goto cleanUp;
    }     
        
    // Now copy the rest of the image reading sector by sector
    //
    bytesRemaining = pTOC->physlast - pTOC->physfirst - bytesImageCopied;
    while (bytesRemaining > 0) 
    {      
        //KITLOutputDebugString("INFO: Reading sector %d from flash...", sector);
        if (!FMD_ReadSector(sector, ucFlashSectorBuffer, &sectorInfo, 1) )
        {
            KITLOutputDebugString("\r\nERROR: Failed read sector %d from flash\r\n", sector);
            goto cleanUp;
        }
        readCount = (bytesRemaining < flashInfo.wDataBytesPerSector) ? bytesRemaining : flashInfo.wDataBytesPerSector;
        memcpy(pImage + bytesImageCopied, ucFlashSectorBuffer, readCount);
        bytesImageCopied += readCount;
        bytesRemaining -= readCount;
        sector++;
    }      
    
    // Ok, lets jump to image        
    rc = BL_JUMP;
 
cleanUp:
    // Close FMD driver
    if (hFMD)
    {
        FMD_Deinit(hFMD);
    }
    return rc;
}


BOOL WriteFlashMDOCLDR(DWORD dwImageStart, DWORD dwImageLength)
{
    BOOL rc = FALSE;
    UINT8 *pData;
    UINT32 *pInfo;

    // Get data location
    pData = OEMMapMemAddr(dwImageStart, dwImageStart);
    
    // Check if image fits size 
    if (dwImageLength > IMAGE_BOOT_MDOCLDRIMAGE_FLASH_SIZE)
    {
        // This could mean that we are downloading a BIN file
        // OEMVerifyMemory already verified that the size is less than 0x2000 (6KB)
        // (This is the max ram image size as specified in image_cfg.h)

        // First check if this is a CE Image
        pInfo = (UINT32*)(pData + ROM_SIGNATURE_OFFSET);
        if (*pInfo != ROM_SIGNATURE)
        {
            KITLOutputDebugString("ERROR: WriteFlashMDOCLDR: Image Signature not found\r\n");
            goto cleanUp;
        }
        
        // ROMIMAGE adds 4K page at the beginning of the image.
        // We will flash the first 2K bytes that appear after this 4K page.
        // When MDOCLDR is compiled, look at the generated mdocldr.map 
        // to make sure that the code does not overflow beyond 2k boundary
        //
        pData += 0x1000;
        KITLOutputDebugString("INFO: LDR image in BIN file (size 0x%x), flashing 0x%x bytes)\r\n", dwImageLength, IMAGE_BOOT_MDOCLDRIMAGE_FLASH_SIZE);
    }        

    // Fill unused space with 0xFF
    if (dwImageLength < IMAGE_BOOT_MDOCLDRIMAGE_FLASH_SIZE) 
    {
        memset(pData + dwImageLength, 0xFF, IMAGE_BOOT_MDOCLDRIMAGE_FLASH_SIZE - dwImageLength);
    }

    // Write the MDOCLDR to flash
    if (!WriteMDOCLDR(pData))
    {
        KITLOutputDebugString("ERROR: WriteFlashMDOCLDR: failed\r\n");
    }

    rc = TRUE;
    
cleanUp:    
    return rc;
}


BOOL WriteFlashEBOOT(DWORD dwImageStart, DWORD dwImageLength)
{
    BOOL rc = FALSE;
    UINT32 *pInfo;
    UINT8 *pData;
    unsigned char ucFlashBuffer[1024];
    FlashInfo flashInfo;
    HANDLE hFMD = NULL;
    UINT32 sectorSize;
    UINT32 imageIndex;

    // Check if image fits 
    if (dwImageLength > IMAGE_BOOT_BLDRIMAGE_RAM_SIZE)
    {
        KITLOutputDebugString("ERROR: WriteFlashEBOOT: EBOOT image too big (size 0x%x, maximal size 0x%x)\r\n", dwImageLength, IMAGE_BOOT_BLDRIMAGE_RAM_SIZE);
        goto cleanUp;
    }        

    // Get data location
    pData = OEMMapMemAddr(dwImageStart, dwImageStart);
    
    // Verify that we get CE image.
    pInfo = (UINT32*)(pData + ROM_SIGNATURE_OFFSET);
    if (*pInfo != ROM_SIGNATURE)
    {
        KITLOutputDebugString("ERROR: WriteFlashEBOOT: Image Signature not found\r\n");
        goto cleanUp;
    }
    
    // Fill unused space with 0xFF
    if (dwImageLength < IMAGE_BOOT_BLDRIMAGE_RAM_SIZE) 
    {
        memset(pData + dwImageLength, 0xFF, IMAGE_BOOT_BLDRIMAGE_RAM_SIZE - dwImageLength);
    }

    // Before Eboot is written to flash, check if the user wanted a re-format
    if (g_FormatFlash)
    {
        KITLOutputDebugString("WriteFlashEBOOT: User wanted a re-format. Re-formatting flash...\r\n");
        if (!FMD_OEMIoControl(IOCTL_DOC_FORMAT_COMP, NULL, 0, NULL, 0, NULL))
        {
            KITLOutputDebugString("WriteFlashEBOOT: Re-formatting flash failed.\r\n");
            goto cleanUp;
        }
        KITLOutputDebugString("WriteFlashEBOOT: Format complete.\r\n");
    }

    KITLOutputDebugString("WriteFlashEBOOT: Writing EBOOT image to flash\r\n");

    // Prepare write
    hFMD = FMD_bdkInit(BSP_MDOC_EBOOT_PARTITION_HANDLE, "BIPO", 2*IMAGE_BOOT_BLDRIMAGE_RAM_SIZE, FALSE);
    if (!hFMD)
    {
        KITLOutputDebugString("ERROR: WriteFlashEBOOT: FMD_Init call failed\r\n");
        goto cleanUp;
    }

    if (!FMD_bdkGetInfo(&flashInfo))
    {
        goto cleanUp;
    }

    sectorSize = flashInfo.wDataBytesPerSector;

    // Write the image to the MDOC.
    for (imageIndex = 0; imageIndex < IMAGE_BOOT_BLDRIMAGE_RAM_SIZE; imageIndex+=sectorSize)
    {
        // We are going to write each 512 byte (sectorSize) block twice for boot security.
        // The LDR code takes this into account as well.
        memcpy(ucFlashBuffer, pData+imageIndex, sectorSize);
        memcpy(ucFlashBuffer+sectorSize, ucFlashBuffer, sectorSize);

        if (!FMD_bdkWriteSector((imageIndex/sectorSize)*2, ucFlashBuffer, NULL, 2))
        {
            KITLOutputDebugString("WriteFlashEBOOT: Error in FMD_bdkWriteSector\r\n");
            goto cleanUp;
        }
    }

    // Done
    rc = TRUE;

cleanUp:
    // Close FMD driver
    if (hFMD)
    {
        FMD_bdkDeinit(hFMD);
    }
    
    return rc;
}


BOOL WriteFlashNK(DWORD dwImageStart, DWORD dwImageLength)
{
    BOOL rc = FALSE;
    HANDLE hFMD = NULL;
    UINT8 *pData;
    UINT32 *pInfo;
    FlashInfo flashInfo;    
    SectorInfo sectorInfo;
    SECTOR_ADDR sector;
    UINT32 bufferIndex = 0;
    UINT32 bytesRemaining = 0;
    UINT32 writeCount = 0;
    unsigned char ucFlashSectorBuffer[BSP_SECTOR_SIZE];

    // Initialize BSP_MDOC_NK_PARTITION for write
    //
    hFMD = FMD_Init((LPCTSTR)BSP_MDOC_NK_PARTITION_HANDLE, NULL, NULL);
    if (!hFMD)
    {
        KITLOutputDebugString("ERROR: WriteFlashNK: FMD_Init call failed\r\n");
        goto cleanUp;
    }

    // Get flash info
    if (!FMD_GetInfo(&flashInfo))
    {
        KITLOutputDebugString("ERROR: FMD_GetInfo call failed\r\n");
        goto cleanUp;
    }

    // Get data location
    pData = OEMMapMemAddr(dwImageStart, dwImageStart);

    // Verify that we get CE image.
    pInfo = (UINT32*)(pData + ROM_SIGNATURE_OFFSET);
    if (*pInfo != ROM_SIGNATURE)
    {
        KITLOutputDebugString("ERROR: WriteFlashEBOOT: Image Signature not found\r\n");
        goto cleanUp;
    }
    
    // Write OS Image to sector by sector
    //
    KITLOutputDebugString("INFO: WriteFlashNK: Writing 0x%x bytes of OS image to flash\r\n", dwImageLength);

    bufferIndex = 0;
    sector = 0;
    bytesRemaining = dwImageLength;
    while (bytesRemaining > 0) 
    {      
        writeCount = (bytesRemaining < flashInfo.wDataBytesPerSector) ? bytesRemaining : flashInfo.wDataBytesPerSector;
        memset(ucFlashSectorBuffer, 0, sizeof(ucFlashSectorBuffer));
        memcpy(ucFlashSectorBuffer, pData + bufferIndex, writeCount);

        // Write config data one sector at a time
        if (!FMD_WriteSector(sector, ucFlashSectorBuffer, &sectorInfo, 1) )
        {
            KITLOutputDebugString("\r\nERROR: Failed write sector %d to flash\r\n", sector);
            goto cleanUp;
        }
        bufferIndex += writeCount;
        bytesRemaining -= writeCount;
        sector++;
    } 

    // Done
    rc = TRUE;

cleanUp:
    // Close FMD driver
    if (hFMD)
    {
        FMD_Deinit(hFMD);
    }
    return rc;
}


#ifdef DEBUG
void DumpTOC(ROMHDR *pTOC)
{
    // Print out ROMHDR information
    KITLOutputDebugString("\r\n");
    KITLOutputDebugString("ROMHDR (pTOC = 0x%x) ---------------------\r\n", pTOC);
    KITLOutputDebugString("    DLL First           : 0x%x\r\n", pTOC->dllfirst);
    KITLOutputDebugString("    DLL Last            : 0x%x\r\n", pTOC->dlllast);
    KITLOutputDebugString("    Physical First      : 0x%x\r\n", pTOC->physfirst);
    KITLOutputDebugString("    Physical Last       : 0x%x\r\n", pTOC->physlast); 
    KITLOutputDebugString("    Num Modules         : %d\r\n",   pTOC->nummods);
    KITLOutputDebugString("    RAM Start           : 0x%x\r\n", pTOC->ulRAMStart); 
    KITLOutputDebugString("    RAM Free            : 0x%x\r\n", pTOC->ulRAMFree); 
    KITLOutputDebugString("    RAM End             : 0x%x\r\n", pTOC->ulRAMEnd); 
    KITLOutputDebugString("    Num Copy Entries    : %d\r\n",   pTOC->ulCopyEntries);
    KITLOutputDebugString("    Copy Entries Offset : 0x%x\r\n", pTOC->ulCopyOffset);  
    KITLOutputDebugString("    Prof Symbol Length  : 0x%x\r\n", pTOC->ulProfileLen);
    KITLOutputDebugString("    Prof Symbol Offset  : 0x%x\r\n", pTOC->ulProfileOffset);
    KITLOutputDebugString("    Num Files           : %d\r\n",   pTOC->numfiles);
    KITLOutputDebugString("    Kernel Flags        : 0x%x\r\n", pTOC->ulKernelFlags);
    KITLOutputDebugString("    FileSys RAM Percent : 0x%x\r\n", pTOC->ulFSRamPercent);
    KITLOutputDebugString("    Driver Glob Start   : 0x%x\r\n", pTOC->ulDrivglobStart);
    KITLOutputDebugString("    Driver Glob Length  : 0x%x\r\n", pTOC->ulDrivglobLen);
    KITLOutputDebugString("    CPU                 :     0x%x\r\n", pTOC->usCPUType);
    KITLOutputDebugString("    MiscFlags           :     0x%x\r\n", pTOC->usMiscFlags);
    KITLOutputDebugString("    Extensions          : 0x%x\r\n", pTOC->pExtensions); 
    KITLOutputDebugString("    Tracking Mem Start  : 0x%x\r\n", pTOC->ulTrackingStart);
    KITLOutputDebugString("    Tracking Mem Length : 0x%x\r\n", pTOC->ulTrackingLen);
    KITLOutputDebugString("\r\n");
}
#endif

⌨️ 快捷键说明

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