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

📄 bootpart.cpp

📁 6410BSP3
💻 CPP
📖 第 1 页 / 共 3 页
字号:
 *          none specified.  
 *      dwNumSectors - Number of logical sectors of the partition.  USE_REMAINING_SPACE 
 *          to indicate to take up the rest of the space on the flash for that partition.
 *      dwPartType - Type of partition to create.
 *      fActive - TRUE indicates to create the active partition.  FALSE for
 *          inactive.
 *      dwPartIndex - Index of the partition entry on the MBR
 *
 *  EXIT
 *      Handle to the partition on success.  INVALID_HANDLE_VALUE on error.
 */

static HANDLE CreatePartition (DWORD dwStartSector, DWORD dwNumSectors, DWORD dwPartType, BOOL fActive, DWORD dwPartIndex)
{
    DWORD dwBootInd = 0;

    RETAILMSG(1, (TEXT("CreatePartition: Enter CreatePartition for 0x%x.\r\n"), dwPartType));
#if 0
    
    if (fActive)
        dwBootInd |= PART_IND_ACTIVE;
    if (dwPartType == PART_BOOTSECTION || dwPartType == PART_BINFS || dwPartType == PART_XIP)
        dwBootInd |= PART_IND_READ_ONLY;    

     // If start sector is invalid, it means find next free sector
    if (dwStartSector == NEXT_FREE_LOC) {        
        
        dwStartSector = FindFreeSector();
        if (dwStartSector == INVALID_ADDR) {
            RETAILMSG(1, (TEXT("CreatePartition: can't find free sector.\r\n")));
            return INVALID_HANDLE_VALUE;
        }

        // Start partitions on the next block if they are currently on the wrong block type.
        if (dwStartSector % g_FlashInfo.wSectorsPerBlock) {            
            DWORD dwBlock = dwStartSector / g_FlashInfo.wSectorsPerBlock;
            //if (IS_PART_READONLY(dwBootInd) != IS_BLOCK_READONLY(dwBlock)) {
                dwStartSector = (dwBlock+1) * g_FlashInfo.wSectorsPerBlock;
            //}
        }
    }
    if (IS_PART_READONLY(dwBootInd)) {

        // Allow read-only partitions to go to the end of disk, if requested.
        if (dwNumSectors == USE_REMAINING_SPACE) {

            DWORD dwLastLogSector = LastLogSector();
            if (dwLastLogSector == INVALID_ADDR)
                return INVALID_HANDLE_VALUE;

            dwNumSectors = dwLastLogSector - dwStartSector + 1;
        }
    }
    else {
        DWORD dwLastLogSector = LastLogSector();
        if (dwLastLogSector == INVALID_ADDR)
            return INVALID_HANDLE_VALUE;

        // Determine the number of blocks to reserve for the FAL compaction when creating an extended partition.
        DWORD dwReservedBlocks = g_FlashInfo.dwNumBlocks / PERCENTAGE_OF_MEDIA_TO_RESERVE;
        if((dwReservedBlocks = g_FlashInfo.dwNumBlocks / PERCENTAGE_OF_MEDIA_TO_RESERVE) < MINIMUM_FLASH_BLOCKS_TO_RESERVE) {
            dwReservedBlocks = MINIMUM_FLASH_BLOCKS_TO_RESERVE;
        }
        
        DWORD dwNumMaxSectors = dwLastLogSector - dwStartSector + 1 - dwReservedBlocks * g_FlashInfo.wSectorsPerBlock;

        // If dwNumSectors was provided, validate it isn't past the max.
        // If dwNumSectors is USE_REMAINING_SPACE, fill disk with max sectors.
        if ((dwNumSectors == USE_REMAINING_SPACE)  || (dwNumMaxSectors <  dwNumSectors)) {
            RETAILMSG(1, (TEXT("CreatePartition: Num sectors set to 0x%x to allow for compaction blocks.\r\n"), dwNumMaxSectors));
            dwNumSectors = dwNumMaxSectors ;    
        }
    //}

    
    if (!AreSectorsFree (dwStartSector, dwNumSectors)){
        RETAILMSG (1, (TEXT("CreatePartition: sectors [0x%x, 0x%x] requested are out of range or taken by another partition\r\n"), dwStartSector, dwNumSectors));
        return INVALID_HANDLE_VALUE;
    }
#endif

//	dwStartSector = (SPECIAL_AREA_START + 1)*SECTORS_PER_SUBLK;
	dwStartSector = (1)*SECTORS_PER_SUBLK;	// by hmseo-061209.. because bibdrv calculate the sector address by add with SPECIAL_AREA_START.
	
	RETAILMSG(1, (TEXT("CreatePartition: Start = 0x%x, Num = 0x%x.\r\n"), dwStartSector, dwNumSectors));

	AddPartitionTableEntry (dwPartIndex, dwStartSector, dwNumSectors, (BYTE)dwPartType, (BYTE)dwBootInd);

#if 0	// by hmseo - it is not fmd driver, therefore this WriteLogicalNumbers routine is not needed. 061124
    //if (IS_PART_READONLY(dwBootInd)) {
        if (!WriteLogicalNumbers (dwStartSector, dwNumSectors, TRUE)) {
            RETAILMSG(1, (TEXT("CreatePartition: can't mark sector info.\r\n")));
            return INVALID_HANDLE_VALUE;
        }
    //}
#endif

    if (!WriteMBR())
        return INVALID_HANDLE_VALUE;

    g_partStateTable[dwPartIndex].pPartEntry = (PPARTENTRY)(g_pbMBRSector + PARTTABLE_OFFSET + sizeof(PARTENTRY)*dwPartIndex);
    g_partStateTable[dwPartIndex].dwDataPointer = 0;

    return (HANDLE)&g_partStateTable[dwPartIndex];            
}


/*  BP_ReadData
 *
 *  Reads data from the partition starting at the data pointer.  Call fails
 *  if length of the buffer is too long (i.e. trying to read past the end
 *  of the partition)
 *
 *  ENTRY
 *      hPartition - handle to the partition
 *      pbBuffer - pointer to buffer of data to read
 *      dwLength - number of bytes to read
 *
 *  EXIT
 *      TRUE on success
 */
BOOL BP_ReadData(HANDLE hPartition, LPBYTE pbBuffer, DWORD dwLength)
{
    if (hPartition == INVALID_HANDLE_VALUE)
        return FALSE;
    
    DWORD dwNumSects;
    static LPBYTE pbSector = g_pbBlock;
    PPARTSTATE pPartState = (PPARTSTATE) hPartition;
    DWORD dwNextPtrValue = pPartState->dwDataPointer + dwLength;

    if (!pbBuffer || !pbSector || dwLength == 0)
        return(FALSE);

    // RETAILMSG(1,(TEXT("ReadData: Start = 0x%x, Length = 0x%x.\r\n"), pPartState->dwDataPointer, dwLength));

    // Check to make sure buffer size is within limits of partition
    if (((dwNextPtrValue - 1) / g_FlashInfo.wDataBytesPerSector) >= pPartState->pPartEntry->Part_TotalSectors) {
        RETAILMSG (1, (TEXT("ReadData: trying to read past end of partition.\r\n")));
        return FALSE;
    }

    // Get the starting physical sector
    DWORD dwSectorAddr = Log2Phys (pPartState->dwDataPointer / g_FlashInfo.wDataBytesPerSector + pPartState->pPartEntry->Part_StartSector);
    DWORD dwSector0 = dwSectorAddr;
    DWORD dwLen0 = dwLength;
    LPBYTE pbBuf0 = pbBuffer;

    // If current pointer is not on a sector boundary, copy bytes up to the first sector boundary
    DWORD dwOffsetSector = pPartState->dwDataPointer % g_FlashInfo.wDataBytesPerSector;
    if (dwOffsetSector)
    {
        if (!FMD_ReadSector(dwSectorAddr, pbSector, NULL, 1))
        {
            RETAILMSG (1, (TEXT("ReadData: failed to read sector (0x%x).\r\n"), dwSectorAddr));
            return(FALSE);
        }
        
        DWORD dwNumBytesRead = g_FlashInfo.wDataBytesPerSector - dwOffsetSector;
        if (dwNumBytesRead > dwLength)
            dwNumBytesRead = dwLength;
        
        memcpy(pbBuffer, pbSector + dwOffsetSector, dwNumBytesRead);   
        dwLength -= dwNumBytesRead;
        pbBuffer += dwNumBytesRead;
        dwSectorAddr++;
    }

    // Compute sector length.
    dwNumSects = (dwLength / g_FlashInfo.wDataBytesPerSector);

	if (dwNumSects)
		FMD_ReadSector(dwSectorAddr, pbBuffer, NULL, dwNumSects);

    DWORD dwNumExtraBytes = (dwLength % g_FlashInfo.wDataBytesPerSector);
    if (dwNumExtraBytes)
    {
    	dwSectorAddr += dwNumSects;
    	pbBuffer += dwNumSects*g_FlashInfo.wDataBytesPerSector;
        if (!FMD_ReadSector(dwSectorAddr, pbSector, NULL, 1))
        {
            RETAILMSG (1, (TEXT("ReadData: failed to read sector (0x%x).\r\n"), dwSectorAddr));
            return(FALSE);
        }
        memcpy(pbBuffer, pbSector, dwNumExtraBytes);
    }

#if 0    
    //dump if inside 10 sectors
    if (pPartState->dwDataPointer < 10*512) {
    	RETAILMSG(1, (L"BP_ReadData: StartSector=0x%x, SectorOffset=0x%x, Leghth=0x%x",
    		dwSector0, dwOffsetSector, dwLen0));

    	DWORD dwAddr = pPartState->dwDataPointer;
    	DWORD dwOffset16 = dwOffsetSector % 16;
    	DWORD i;

    	if (dwOffset16 != 0) {
    		dwAddr -= dwOffset16;
    		RETAILMSG(1, (L"\r\nSector=0x%x(0x%x)",
    			dwSector0 + (dwAddr / 512),
    			(dwSector0 + (dwAddr / 512))*512));
    		RETAILMSG(1, (L"\r\n%x%x%x%x: ", (dwAddr >> 12) & 0x0f, (dwAddr >> 8) & 0x0f,
    				(dwAddr >> 4) & 0x0f, dwAddr & 0x0f));
    		for (i = 0; i < dwOffset16; i++, dwAddr++)
    			RETAILMSG(1, (L"__ "));
    	}
    		    		
    	for (i = 0; i < dwLen0; i++, dwAddr++) {
    		if ((dwAddr % 16) == 0) {
    			if ((dwAddr % 512) == 0)
    				RETAILMSG(1, (L"\r\nSector=0x%x(0x%x)",
    					dwSector0 + (dwAddr / 512),
    					(dwSector0 + (dwAddr / 512))*512));
    			RETAILMSG(1, (L"\r\n%x%x%x%x: ", (dwAddr >> 12) & 0x0f, (dwAddr >> 8) & 0x0f,
    				(dwAddr >> 4) & 0x0f, dwAddr & 0x0f));
    		}
    		RETAILMSG(1, (L"%x%x ", (pbBuf0[i] >> 4) & 0x0f, pbBuf0[i] & 0x0f));
    	}
    	
    	RETAILMSG(1, (L"\r\n"));
    }
#endif

    pPartState->dwDataPointer = dwNextPtrValue;
    return(TRUE);
}

/*  BP_WriteData
 *
 *  Writes data to the partition starting at the data pointer.  Call fails
 *  if length of the buffer is too long (i.e. trying to write past the end
 *  of the partition)
 *
 *  ENTRY
 *      hPartition - handle to the partition
 *      pbBuffer - pointer to buffer of data to write
 *      dwLength - length in bytes of the buffer
 *
 *  EXIT
 *      TRUE on success
 */

BOOL BP_WriteData(HANDLE hPartition, LPBYTE pbBuffer, DWORD dwLength)
{
	if (hPartition == INVALID_HANDLE_VALUE)
		return FALSE;

	DWORD dwNumSects;
	static LPBYTE pbSector = g_pbBlock;
	PPARTSTATE pPartState = (PPARTSTATE) hPartition;
	DWORD dwNextPtrValue = pPartState->dwDataPointer + dwLength;

	if (!pbBuffer || !pbSector || dwLength == 0)
		return(FALSE);

	RETAILMSG (1, (TEXT("BP_WriteData: Start = 0x%x, Length = 0x%x.\r\n"), pPartState->dwDataPointer, dwLength));

	// Check to make sure buffer size is within limits of partition
	if (((dwNextPtrValue - 1) / g_FlashInfo.wDataBytesPerSector) >= pPartState->pPartEntry->Part_TotalSectors) {
		RETAILMSG (1, (TEXT("ReadData: trying to read past end of partition.(Part_TotalSectors = 0x%x)\r\n"), pPartState->pPartEntry->Part_TotalSectors));
		return FALSE;
	}

	// Get the starting physical sector
//	DWORD dwSectorAddr = Log2Phys (pPartState->dwDataPointer / g_FlashInfo.wDataBytesPerSector + pPartState->pPartEntry->Part_StartSector);
//    DWORD dwSectorAddr = 0;	// hmseo-061124
//	RETAILMSG (1, (TEXT("BP_WriteData: pPartState->pPartEntry->Part_StartSector = 0x%x.\r\n"), pPartState->pPartEntry->Part_StartSector));
//	DWORD dwSectorAddr = pPartState->dwDataPointer / BYTES_PER_SECTOR + pPartState->pPartEntry->Part_StartSector;	// hmseo-061124
	DWORD dwSectorAddr = (pPartState->dwDataPointer / BYTES_PER_SECTOR);	// hmseo-061124

	// If current pointer is not on a sector boundary, copy bytes up to the first sector boundary
	DWORD dwOffsetSector = pPartState->dwDataPointer % g_FlashInfo.wDataBytesPerSector;

	RETAILMSG (1, (TEXT("BP_WriteData: dwSectorAddr = 0x%x, dwOffsetSector = 0x%x.\r\n"), dwSectorAddr, dwOffsetSector));

	if (dwOffsetSector)
	{
		if (!FMD_ReadSector(dwSectorAddr, pbSector, NULL, SECTORS_PER_SUPAGE))
		{
			RETAILMSG (1, (TEXT("WriteData: failed to read sector (0x%x).\r\n"), dwSectorAddr));
			return(FALSE);
		}

		DWORD dwNumBytesWrite = g_FlashInfo.wDataBytesPerSector - dwOffsetSector;
		if (dwNumBytesWrite > dwLength)
			dwNumBytesWrite = dwLength;

		memcpy(pbSector + dwOffsetSector, pbBuffer, dwNumBytesWrite);   
		dwLength -= dwNumBytesWrite;
		pbBuffer += dwNumBytesWrite;

		if (!FMD_WriteSector(dwSectorAddr, pbSector, NULL, SECTORS_PER_SUPAGE))
		{
			RETAILMSG(1, (TEXT("WriteData: faild to write sector (0x%x).\r\n"), dwSectorAddr));
			return(FALSE);
		}

		dwSectorAddr+=SECTORS_PER_SUPAGE;
	}

	// Compute sector length.
//	dwNumSects = (dwLength / g_FlashInfo.wDataBytesPerSector);	// by hmseo
	dwNumSects = (dwLength / (BYTES_PER_SECTOR*SECTORS_PER_SUPAGE) * SECTORS_PER_SUPAGE);

	RETAILMSG (1, (TEXT("BP_WriteData: dwNumSects = 0x%x\r\n"), dwNumSects));

	if (dwNumSects)
	{
		INT32   nErr;
		UINT32  nVol = 0;
		UINT32	nPercent = 0;
		UINT32	nCnt;

//		for(dwSectorAddr = 0; dwSectorAddr < dwNumSects; dwSectorAddr+=4)
		for(nCnt = 0; nCnt < dwNumSects; nCnt+=SECTORS_PER_SUPAGE)
		{
			nErr = FMD_WriteSector(dwSectorAddr+nCnt, pbBuffer, NULL, SECTORS_PER_SUPAGE);
			if(nErr != TRUE)
			{
				RETAILMSG(1, (L"[ONW: ERR]  FMD_WriteSector Error at %d sector\r\n", dwSectorAddr));
				while(1);
			}		
			pbBuffer += (BYTES_PER_SECTOR*SECTORS_PER_SUPAGE);
		}
	}

//	DWORD dwNumExtraBytes = (dwLength % g_FlashInfo.wDataBytesPerSector);
	DWORD dwNumExtraBytes = (dwLength % (BYTES_PER_SECTOR*SECTORS_PER_SUPAGE));
	
	RETAILMSG (1, (TEXT("BP_WriteData: dwNumExtraBytes = 0x%x\r\n"), dwNumExtraBytes));

	if (dwNumExtraBytes)
	{
		RETAILMSG (1, (TEXT("BP_WriteData: dwSectorAddr = 0x%x\r\n"), dwSectorAddr));
		RETAILMSG (1, (TEXT("BP_WriteData: dwNumSects = 0x%x\r\n"), dwNumSects));
		
		dwSectorAddr += dwNumSects;
//		pbBuffer += dwNumSects*g_FlashInfo.wDataBytesPerSector;

		RETAILMSG (1, (TEXT("BP_WriteData: dwSectorAddr = 0x%x\r\n"), dwSectorAddr));
		RETAILMSG (1, (TEXT("BP_WriteData: pbBuffer = 0x%x\r\n"), pbBuffer));

		if (!FMD_ReadSector(dwSectorAddr, pbSector, NULL, SECTORS_PER_SUPAGE))
		{
			RETAILMSG (1, (TEXT("WriteData: failed to read sector (0x%x).\r\n"), dwSectorAddr));
			return(FALSE);
		}
		memcpy(pbSector, pbBuffer, dwNumExtraBytes);

		if (!FMD_WriteSector(dwSectorAddr, pbSector, NULL, SECTORS_PER_SUPAGE))
		{
			RETAILMSG(1, (TEXT("WriteData: failed to read sector (0x%x).\r\n"), dwSectorAddr));
			return(FALSE);
		}

⌨️ 快捷键说明

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