fmd.cpp

来自「S3C24A0的完整BSP包,对开发此芯片的开发者很有用.」· C++ 代码 · 共 840 行 · 第 1/3 页

CPP
840
字号

        // Set the file pointer (byte indexing) to the correct offset for this particular region.
        //
        if ( !BP_SetDataPointer(hPart, dwStoreOffset) )
        {
            EdbgOutputDebugString("ERROR: StoreImageToBootMedia: Failed to set data pointer in BINFS partition (offset=0x%x).\r\n", dwStoreOffset);
            return(FALSE);
        }

        // Write the region to the BINFS partition.
        //
        if ( !BP_WriteData(hPart, (LPBYTE)dwRegionStart, dwRegionLength) )
        {
            EdbgOutputDebugString("ERROR: StoreImageToBootMedia: Failed to write region to BINFS partition (start=0x%x, length=0x%x).\r\n", dwRegionStart, dwRegionLength);
            return(FALSE);
        }

        // update our TOC?
        //
        if ((g_pTOC->id[g_dwTocEntry].dwLoadAddress == g_BINRegionInfo.Region[nCount].dwRegionStart) &&
             g_pTOC->id[g_dwTocEntry].dwTtlSectors == FILE_TO_SECTOR_SIZE(dwRegionLength) )
        {
            g_pTOC->id[g_dwTocEntry].dwStoreOffset = dwStoreOffset;
            g_pTOC->id[g_dwTocEntry].dwJumpAddress = 0; // Filled upon return to OEMLaunch

            g_pTOC->id[g_dwTocEntry].dwImageType = g_ImageType;

            g_pTOC->id[g_dwTocEntry].sgList[0].dwSector = FILE_TO_SECTOR_SIZE(g_dwLastWrittenLoc);
            g_pTOC->id[g_dwTocEntry].sgList[0].dwLength = g_pTOC->id[g_dwTocEntry].dwTtlSectors;

            // copy Kernel Region to SDRAM for jump
            memcpy((void*)g_pTOC->id[g_dwTocEntry].dwLoadAddress, (void*)dwRegionStart, dwRegionLength);

            EdbgOutputDebugString("Updateded TOC!\r\n");
        } 
        else if( (dwChainStart == g_BINRegionInfo.Region[nCount].dwRegionStart) &&
                 (dwChainLength == g_BINRegionInfo.Region[nCount].dwRegionLength)) 
        {
            //  Update our TOC for Chain region
            g_pTOC->chainInfo.dwLoadAddress = dwChainStart;
            g_pTOC->chainInfo.dwFlashAddress = FILE_TO_SECTOR_SIZE(g_dwLastWrittenLoc);
            g_pTOC->chainInfo.dwLength = FILE_TO_SECTOR_SIZE(dwMaxRegionLength[nCount]);

            EdbgOutputDebugString("Written Chain Region to the Flash\n");
            EdbgOutputDebugString("LoadAddress = 0x%X; FlashAddress = 0x%X; Length = 0x%X\n", 
                                  g_pTOC->chainInfo.dwLoadAddress, 
                                  g_pTOC->chainInfo.dwFlashAddress,
                                  g_pTOC->chainInfo.dwLength);
            // Now copy it to the SDRAM
            memcpy((void *)g_pTOC->chainInfo.dwLoadAddress, (void *)dwRegionStart, dwRegionLength);
//            memcpy((void *)0x8c050000, (void *)dwRegionStart, dwRegionLength);
        }
    }

    // create extended partition in whatever is left
    //
    hPartEx = BP_OpenPartition( NEXT_FREE_LOC,
                                USE_REMAINING_SPACE,
                                PART_DOS32,
                                TRUE,
                                PART_OPEN_ALWAYS);

    if (hPartEx == INVALID_HANDLE_VALUE )
    {
        EdbgOutputDebugString("*** WARN: StoreImageToBootMedia: Failed to open/create Extended partition ***\r\n");
    }

    EdbgOutputDebugString("-WriteRegionsToBootMedia\r\n");

    return(TRUE);
}


/*
    @func   BOOL | ReadKernelRegionFromBootMedia |
            BinFS support. Reads the kernel region from Boot Media into RAM.  The kernel region is fixed up
            to run from RAM and this is done just before jumping to the kernel entry point.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm
    @xref
*/
BOOL ReadKernelRegionFromBootMedia( )
{
    HANDLE hPart;
    SectorInfo si;
	DWORD 	chainaddr, flashaddr;
	int i;

	EdbgOutputDebugString("ReadKernelRegionFromBootMedia: Enter \r\n");

    if (!g_bBootMediaExist) {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: device doesn't exist.\r\n");
        return(FALSE);
    }

    if ( !VALID_TOC(g_pTOC) ) {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: INVALID_TOC\r\n");
        return(FALSE);
    }

    if ( !(IMAGE_TYPE_BINFS & g_pTOC->id[g_dwTocEntry].dwImageType) ) {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: INVALID_IMAGE_TYPE: 0x%x\r\n",
            g_pTOC->id[g_dwTocEntry].dwImageType);
        return(FALSE);
    }

    if ( !VALID_IMAGE_DESCRIPTOR(&g_pTOC->id[g_dwTocEntry]) ) {
        EdbgOutputDebugString("OEMPlatformInit: ERROR_INVALID_IMAGE_DESCRIPTOR: 0x%x\r\n",
            g_pTOC->id[g_dwTocEntry].dwSignature);
        return FALSE;
    }

    if ( !OEMVerifyMemory(g_pTOC->id[g_dwTocEntry].dwLoadAddress, sizeof(DWORD)) ||
         !OEMVerifyMemory(g_pTOC->id[g_dwTocEntry].dwJumpAddress, sizeof(DWORD)) ||
         !g_pTOC->id[g_dwTocEntry].dwTtlSectors )
    {
        EdbgOutputDebugString("OEMPlatformInit: ERROR_INVALID_ADDRESS: (address=0x%x, sectors=0x%x, launch address=0x%x)...\r\n",
            g_pTOC->id[g_dwTocEntry].dwLoadAddress, g_pTOC->id[g_dwTocEntry].dwTtlSectors, g_pTOC->id[g_dwTocEntry].dwJumpAddress);
        return FALSE;
    }

    EdbgOutputDebugString("INFO: Loading image from Boot Media to RAM (address=0x%x, sectors=0x%x, launch address=0x%x)...\r\n",
        g_pTOC->id[g_dwTocEntry].dwLoadAddress, g_pTOC->id[g_dwTocEntry].dwTtlSectors, g_pTOC->id[g_dwTocEntry].dwJumpAddress);

    // Open the BINFS partition (it must exist).
    //
    hPart = BP_OpenPartition( NEXT_FREE_LOC,
                              USE_REMAINING_SPACE,
                              PART_BINFS,
                              TRUE,
                              PART_OPEN_EXISTING);

    if (hPart == INVALID_HANDLE_VALUE )
    {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: Failed to open existing BINFS partition.\r\n");
        return(FALSE);
    }

    // Set the partition file pointer to the correct offset for the kernel region.
    //
    if ( !BP_SetDataPointer(hPart, g_pTOC->id[g_dwTocEntry].dwStoreOffset) )
    {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: Failed to set data pointer in BINFS partition (offset=0x%x).\r\n",
            g_pTOC->id[g_dwTocEntry].dwStoreOffset);
        return(FALSE);
    }

    // Read the kernel region from the Boot Media into RAM.
    //
    if ( !BP_ReadData( hPart,
                       (LPBYTE)(g_pTOC->id[g_dwTocEntry].dwLoadAddress),
                       SECTOR_TO_FILE_SIZE(g_pTOC->id[g_dwTocEntry].dwTtlSectors)) )
    {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: Failed to read kernel region from BINFS partition.\r\n");
        return(FALSE);
    }



    EdbgOutputDebugString("g_pTOC->chainInfo.dwLength=0x%x\r\n", g_pTOC->chainInfo.dwLength);

	chainaddr = g_pTOC->chainInfo.dwLoadAddress;
	flashaddr = g_pTOC->chainInfo.dwFlashAddress;
	for ( i = 0; i < (g_pTOC->chainInfo.dwLength); i++ )
	{
	    EdbgOutputDebugString("chainaddr=0x%x, flashaddr=0x%x\r\n", chainaddr, flashaddr+i);

		if ( !FMD_ReadSector(flashaddr+i, (PUCHAR)(chainaddr), &si, 1) ) {
			EdbgOutputDebugString("TOC_Write ERROR: Unable to read/verify TOC\r\n");
			return FALSE;
		}
		chainaddr += 512;
	}


/*
    EdbgOutputDebugString("INFO: Loading chain image from Boot Media to RAM (address=0x%x, sectors=0x%x, sector address=0x%x)...\r\n",
        g_pTOC->chainInfo.dwLoadAddress, g_pTOC->chainInfo.dwLength, g_pTOC->chainInfo.dwFlashAddress);

	// Read Chain Area
	//
    if ( !OEMVerifyMemory(g_pTOC->chainInfo.dwLoadAddress, sizeof(DWORD)) )
    {
        EdbgOutputDebugString("OEMPlatformInit: ERROR_INVALID_ADDRESS: (address=0x%x, flash address=0x%x, length=0x%x)...\r\n",
            g_pTOC->chainInfo.dwLoadAddress, g_pTOC->chainInfo.dwFlashAddress, g_pTOC->chainInfo.dwLength);
        return FALSE;
    }

    // Set the partition file pointer to the correct offset for the chain region.
    //
    if ( !BP_SetDataPointer(hPart, g_pTOC->chainInfo.dwFlashAddress) )
    {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: Failed to set data pointer in BINFS partition (offset=0x%x).\r\n",
            g_pTOC->chainInfo.dwFlashAddress);
        return(FALSE);
    }

    // Read the chain region from the Boot Media into RAM.
    //
    if ( !BP_ReadData( hPart,
                       (LPBYTE)(g_pTOC->chainInfo.dwLoadAddress),
                       SECTOR_TO_FILE_SIZE(g_pTOC->chainInfo.dwLength)) )
    {
        EdbgOutputDebugString("ERROR: ReadKernelRegionFromBootMedia: Failed to read kernel region from BINFS partition.\r\n");
        return(FALSE);
    }
*/
	{
	int i;
	for ( i = 0; i < 60; i++ )
			EdbgOutputDebugString("[0x%x]0x%x ", g_pTOC->chainInfo.dwLoadAddress + i, *(LPBYTE)(g_pTOC->chainInfo.dwLoadAddress + i));
	}

    return(TRUE);
}


BOOL ReadRamImageFromBootMedia( )
{
    DWORD dwSectorsNeeded;
    DWORD dwSector, dwLength;   // Start Sector & Length
    DWORD dwLoadAddress, dwJumpAddr, i;

    if ( !TOC_Read( ) ) { // should already be read
        return FALSE;
    }

    // is this a RAM image?
    // N.B: we can't read BinFS regions because we don' know where they are in g_pTOC->id[dwEntry].sgList[i].dwSector
    if ( g_pTOC->id[g_dwTocEntry].dwImageType != IMAGE_TYPE_RAMIMAGE ) {
        EdbgOutputDebugString("ReadRamImageFromBootMedia ERROR: TOC[%u] not a RAM image: 0x%x\r\n",
            g_dwTocEntry, g_pTOC->id[g_dwTocEntry].dwImageType);
        return FALSE;
    }

    dwSectorsNeeded = g_pTOC->id[g_dwTocEntry].dwTtlSectors;
    dwLoadAddress   = g_pTOC->id[g_dwTocEntry].dwLoadAddress;
    dwJumpAddr      = g_pTOC->id[g_dwTocEntry].dwJumpAddress ? g_pTOC->id[g_dwTocEntry].dwJumpAddress :
                                                          g_pTOC->id[g_dwTocEntry].dwLoadAddress;

    EdbgOutputDebugString("dwSectorsNeeded: 0x%x,  dwLoadAddress: 0x%x, dwJumpAddr: 0x%x\r\n",
        dwSectorsNeeded, dwLoadAddress, dwJumpAddr);

    //
    // Load the disk image directly into RAM
    // BUGBUG: recover from read failures
    //
    i = 0;
	while (dwSectorsNeeded && i < MAX_SG_SECTORS)
	{
        dwSector = g_pTOC->id[g_dwTocEntry].sgList[i].dwSector;
        dwLength = g_pTOC->id[g_dwTocEntry].sgList[i].dwLength;

        // read each sg segment
        while (dwLength) {

            if ( !FMD_ReadSector(dwSector, (LPBYTE)dwLoadAddress, NULL, 1) ) {
                EdbgOutputDebugString("ReadRamImageFromBootMedia ERROR reading sector: 0x%x\r\n", dwSector);
                return FALSE;
            }

    		dwSector++;
    		dwLength--;
            dwLoadAddress += SECTOR_SIZE;
        }

        dwSectorsNeeded -= dwLength;
        i++;
    }

    return TRUE;
}

BOOL WriteRamImageToBootMedia(DWORD dwEntry)
{
    EdbgOutputDebugString("*** ERROR: WriteRamImageToBootMedia: NOT IMPLEMENTED *** \r\n");
    return FALSE;
}

⌨️ 快捷键说明

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