📄 bootpart.c
字号:
//
// 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.
//
#include "bootpart.h"
#include "fmd.h"
#include "2440lib.h"
LPBYTE g_pbMBRSector = NULL;
LPBYTE g_pbBlock = NULL;
DWORD g_dwMBRSectorNum = INVALID_ADDR;
FlashInfo g_FlashInfo;
PARTSTATE g_partStateTable[NUM_PARTS];
PSectorInfo g_pSectorInfoBuf;
DWORD g_dwLastLogSector; // Stores the last valid logical sector
DWORD g_dwDataBytesPerBlock;
DWORD g_dwLastWrittenLoc;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
static DWORD GetMBRSectorNum ()
{
DWORD dwBlockNum = 0;
while (dwBlockNum < g_FlashInfo.dwNumBlocks) {
if (!IS_BLOCK_UNUSABLE (dwBlockNum)) {
return (dwBlockNum * g_FlashInfo.wSectorsPerBlock+8);
}
dwBlockNum++;
}
return INVALID_ADDR;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
static BOOL IsValidMBR()
{
// Check to see if the MBR is valid
// MBR block is always located at logical sector 0
g_dwMBRSectorNum = GetMBRSectorNum();
Uart_Printf("IsValidMBR: MBR sector = 0x%x\r\n", g_dwMBRSectorNum);
if ((g_dwMBRSectorNum == INVALID_ADDR) || !FMD_ReadSector (g_dwMBRSectorNum, g_pbMBRSector, NULL, 1)) {
return FALSE;
}
return ((g_pbMBRSector[0] == 0xE9) &&
(g_pbMBRSector[1] == 0xfd) &&
(g_pbMBRSector[2] == 0xff) &&
(g_pbMBRSector[SECTOR_SIZE-2] == 0x55) &&
(g_pbMBRSector[SECTOR_SIZE-1] == 0xAA));
}
static BOOL ReadBlock (DWORD dwBlock, LPBYTE pbBlock, PSectorInfo pSectorInfoTable)
{
int iSector = 0;
for (iSector = 0; iSector < g_FlashInfo.wSectorsPerBlock; iSector++) {
if (!FMD_ReadSector(dwBlock * g_FlashInfo.wSectorsPerBlock + iSector, pbBlock, pSectorInfoTable, 1))
return FALSE;
if (pbBlock)
pbBlock += g_FlashInfo.wDataBytesPerSector;
if (pSectorInfoTable)
pSectorInfoTable++;
}
return TRUE;
}
/* WriteBlock
*
* Writes one block of data
*
* ENTRY
* dwBlock - Physical block number to write to
* pbBlock - Pointer to data to write
* pSectorInfoTable - Pointer to table of sector infos to write
*
* EXIT
* TRUE on success
*/
static BOOL WriteBlock (DWORD dwBlock, LPBYTE pbBlock, PSectorInfo pSectorInfoTable)
{
int iSector = 0;
for (iSector = 0; iSector < g_FlashInfo.wSectorsPerBlock; iSector++) {
if (!FMD_WriteSector(dwBlock * g_FlashInfo.wSectorsPerBlock + iSector, pbBlock, pSectorInfoTable, 1))
return FALSE;
if (pbBlock)
pbBlock += g_FlashInfo.wDataBytesPerSector;
if (pSectorInfoTable)
pSectorInfoTable++;
}
return TRUE;
}
BOOL WriteSectors (DWORD dwBlock, LPBYTE pbBlock, PSectorInfo pSectorInfoTable, DWORD dwNumSectors)
{
BOOL bResult = TRUE;
int iSector = 0;
for (iSector = 0; iSector < dwNumSectors; iSector++) {
if (!FMD_WriteSector(dwBlock * g_FlashInfo.wSectorsPerBlock + iSector, pbBlock, pSectorInfoTable, 1))
return FALSE;
if (pbBlock)
pbBlock += g_FlashInfo.wDataBytesPerSector;
if (pSectorInfoTable)
{
pSectorInfoTable += sizeof(SectorInfo);
pSectorInfoTable->dwReserved1= (iSector+1)%8;
pSectorInfoTable->bOEMReserved = 0xff;
pSectorInfoTable->bBadBlock= 0xff;
pSectorInfoTable->wReserved2= 0xFFF9;
}
}
return TRUE;
}
static BOOL WriteMBR()
{
int iSector= 0;
DWORD dwMBRBlockNum = g_dwMBRSectorNum / g_FlashInfo.wSectorsPerBlock;
Uart_Printf("WriteMBR: MBR block = 0x%x.\r\n", dwMBRBlockNum);
memset (g_pbBlock, 0xFF, g_dwDataBytesPerBlock); //2048*64个FF
memset (g_pSectorInfoBuf, 0xFF, sizeof(SectorInfo) * g_FlashInfo.wSectorsPerBlock); //64*16个FF
// No need to check return, since a failed read means data hasn't been written yet.
ReadBlock (dwMBRBlockNum, g_pbBlock, g_pSectorInfoBuf);
if (!FMD_EraseBlock (dwMBRBlockNum)) {
Uart_Printf("CreatePartition: error erasing block 0x%x\r\n", dwMBRBlockNum);
return FALSE;
}
memset(g_pbBlock,0x00,9*g_FlashInfo.wDataBytesPerSector);
memcpy (g_pbBlock + (g_dwMBRSectorNum % g_FlashInfo.wSectorsPerBlock) * g_FlashInfo.wDataBytesPerSector, g_pbMBRSector, g_FlashInfo.wDataBytesPerSector);
g_pSectorInfoBuf->bOEMReserved =0xFF;//&= ~OEM_BLOCK_READONLY;
g_pSectorInfoBuf->wReserved2 =0xFFF8;//&= ~SECTOR_WRITE_COMPLETED;
g_pSectorInfoBuf->dwReserved1 = 0;
g_pSectorInfoBuf->bBadBlock = 0xff;
if(!WriteSectors(dwMBRBlockNum, g_pbBlock, g_pSectorInfoBuf,9))
{
Uart_Printf("Write MBR in Block 0x%X Sector 0x%X error!\r\n", dwMBRBlockNum, g_dwMBRSectorNum);
return FALSE;
}
/*
if (!WriteBlock (dwMBRBlockNum, g_pbBlock, g_pSectorInfoBuf)) {
Uart_Printf("CreatePartition: could not write to block 0x%x\r\n", dwMBRBlockNum);
return FALSE;
}
*/
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
static BOOL CreateMBR()
{
// This, plus a valid partition table, is all the CE partition manager needs to recognize
// the MBR as valid. It does not contain boot code.
PPARTENTRY tmpbuffer, tmpbuffer1;
WORD partNum = 0;
DWORD totalSectors = g_FlashInfo.wSectorsPerBlock*(g_FlashInfo.dwNumBlocks-NUM_OF_BLOCK_TO_RESERVE) -4096;
memset (g_pbMBRSector, 0x00, g_FlashInfo.wDataBytesPerSector);
g_pbMBRSector[0] = 0xE9;
g_pbMBRSector[1] = 0xfd;
g_pbMBRSector[2] = 0xff;
g_pbMBRSector[DEFAULT_SECTOR_SIZE-2] = 0x55;
g_pbMBRSector[DEFAULT_SECTOR_SIZE-1] = 0xAA;
// Zero out partition table so that mspart treats entries as empty.
memset (g_pbMBRSector+PARTTABLE_OFFSET, 0, sizeof(PARTENTRY) * NUM_PARTS);
tmpbuffer = (PPARTENTRY)(g_pbMBRSector+ PARTTABLE_OFFSET + partNum * (int)sizeof(PARTENTRY));
tmpbuffer->Part_BootInd = 0x00;
tmpbuffer->Part_FirstHead = 0x00;
tmpbuffer->Part_FirstSector = 0x02;
tmpbuffer->Part_FirstTrack = 0x00;
tmpbuffer->Part_FileSystem = 0x05;
tmpbuffer->Part_LastHead = 0x00;
tmpbuffer->Part_LastSector = 0x40;
tmpbuffer->Part_LastTrack = 0x00;
tmpbuffer->Part_StartSector1 = 0x01;
tmpbuffer->Part_StartSector2 = 0x00;
tmpbuffer->Part_StartSector3 = 0x00;
tmpbuffer->Part_StartSector4 = 0x00;
tmpbuffer->Part_TotalSectors1 = (BYTE)(totalSectors&0xFF); //0x00;//
tmpbuffer->Part_TotalSectors2 =(BYTE)((totalSectors>>8)&0xFF); //0x10;//
tmpbuffer->Part_TotalSectors3 =(BYTE)((totalSectors>>16)&0xFF); //0x00;//
tmpbuffer->Part_TotalSectors4 =(BYTE)((totalSectors>>24)&0xFF); // 0x00;//
/* tmpbuffer = (PPARTENTRY)(g_pbMBRSector+ PARTTABLE_OFFSET + (partNum+1 )* (int)sizeof(PARTENTRY));
tmpbuffer->Part_BootInd = 0x00;
tmpbuffer->Part_FirstHead = 0x00;
tmpbuffer->Part_FirstSector = 0x03;
tmpbuffer->Part_FirstTrack = 0x00;
tmpbuffer->Part_FileSystem = 0x05;
tmpbuffer->Part_LastHead = 0x00;
tmpbuffer->Part_LastSector = 0x40;
tmpbuffer->Part_LastTrack = 0x00;
tmpbuffer->Part_StartSector1 = 0x01;
tmpbuffer->Part_StartSector2 = 0x10;
tmpbuffer->Part_StartSector3 = 0x00;
tmpbuffer->Part_StartSector4 = 0x00;
tmpbuffer->Part_TotalSectors1 = (BYTE)(totalSectors&0xFF);
tmpbuffer->Part_TotalSectors2 =(BYTE)((totalSectors>>8)&0xFF);
tmpbuffer->Part_TotalSectors3 =(BYTE)((totalSectors>>16)&0xFF);
tmpbuffer->Part_TotalSectors4 = (BYTE)((totalSectors>>24)&0xFF);
*/ return WriteMBR();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -