📄 rtc.c
字号:
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-2000 Microsoft Corporation
Module Name: rtc.c
Abstract: Boot loader real-time clock (RTC) functions.
Functions:
GetRealTimeFromCMOS
Notes:
--*/
#include <windows.h>
#include "bldr.h"
// Preprocessor definitions.
//
#define CMOS_ADDR_MASK 0x80 // Don't touch high bit in address register.
#define CMOS_ADDR_REG 0x70 // RTC address register port address.
#define CMOS_DATA_REG 0x71 // RTC data register port address.
// RTC register indices.
#define CMOS_REG_SEC 0 // Seconds.
#define CMOS_REG_MIN 2 // Minutes.
#define CMOS_REG_HOUR 4 // Hours.
#define CMOS_REG_DAY 7 // Day of month.
#define CMOS_REG_MONTH 8 // Month.
#define CMOS_REG_YEAR 9 // Year.
#define CMOS_STATUS_REGB 11 // MC146818 status register B.
#define CMOS_REG_RAMBASE 51 // Index to RTC battery-backed RAM area.
#define CMOS_RAM_SIZE 113 // Assume we have at least 113 bytes in CMOS.
// If this turns out to be a bad assumption,
// part of this code will need to be made
// platform-specific.
#define CMOS_REG_RAMEND (CMOS_REG_RAMBASE + CMOS_RAM_SIZE)
#define REGB_DM_BINARY 0x04 // If this bit is set, time values are in
// binary, otherwise they're in BCD.
#define DECODE_BCD(b) ((b>>4)*10 + (b & 0xF))
#define LOADERVAR_SIG 0xF1F2F3F4
LOADER_VARS gLoaderVars;
void CMOSWriteByte(BYTE nIndex, BYTE nData)
{
BYTE nAddr = 0;
if (nIndex < CMOS_REG_RAMBASE || nIndex > CMOS_REG_RAMEND)
DEBUGMSG(ZONE_WARN, (TEXT("WARNING: CMOS write outside the bounds of the available RAM area.\r\n")));
nAddr = _inp(CMOS_ADDR_REG);
_outp(CMOS_ADDR_REG, ((nAddr & CMOS_ADDR_MASK) | nIndex));
_outp(CMOS_DATA_REG, nData);
}
BYTE CMOSReadByte(BYTE nIndex)
{
BYTE nAddr = 0;
if (nIndex < CMOS_REG_RAMBASE || nIndex > CMOS_REG_RAMEND)
DEBUGMSG(ZONE_WARN, (TEXT("WARNING: CMOS read outside the bounds of the available RAM area.\r\n")));
nAddr = _inp(CMOS_ADDR_REG);
_outp(CMOS_ADDR_REG, ((nAddr & CMOS_ADDR_MASK) | nIndex));
return(_inp(CMOS_DATA_REG));
}
ULONG CMOSComputeCheckSum(PLOADER_VARS pVars)
{
ULONG nChkSum = 0;
PUCHAR pTmp = (PUCHAR)pVars;
UCHAR nCount=0;
// Don't include signature or checksum field in checksum computation.
pTmp += sizeof(ULONG) + sizeof(ULONG);
nCount = sizeof(LOADER_VARS) - sizeof(ULONG) - sizeof(ULONG);
while(nCount--)
{
nChkSum += *(pTmp);
pTmp++;
}
return(nChkSum);
}
BOOL StoreEnvVars(PLOADER_VARS pVars)
{
UCHAR nCount=0;
PUCHAR pTmp = (PUCHAR)pVars;
if (!pVars)
return(FALSE);
// Add signature, version, and checksum.
pVars->nSig = LOADERVAR_SIG;
pVars->nMajVer = BLDR_VERSION_MAJOR;
pVars->nMinVer = BLDR_VERSION_MINOR;
pVars->nChkSum = CMOSComputeCheckSum(pVars);
for (nCount=0 ; nCount < sizeof(LOADER_VARS) ; nCount++)
CMOSWriteByte((BYTE)(CMOS_REG_RAMBASE + nCount), *(pTmp + nCount));
return(TRUE);
}
BOOL VerifyEnvVars(PLOADER_VARS pVars)
{
// Verify signature, checksum, and version.
if (pVars->nSig != LOADERVAR_SIG)
{
RETAILMSG(1, (TEXT("\r\nERROR: CMOS signature not found - environment variables are bogus.\r\n")));
return(FALSE);
}
if (pVars->nChkSum != CMOSComputeCheckSum(pVars))
{
RETAILMSG(1, (TEXT("ERROR: CMOS checksum is bad - environment variables are bogus.\r\n")));
return(FALSE);
}
if (pVars->nMajVer != BLDR_VERSION_MAJOR || pVars->nMinVer != BLDR_VERSION_MINOR)
{
RETAILMSG(1, (TEXT("WARNING: Stored environment variables are from a different loader version - values may be bad.\r\n")));
return(TRUE);
}
return(TRUE);
}
BOOL LoadEnvVars(PLOADER_VARS pVars)
{
UCHAR nCount=0;
PUCHAR pTmp = (PUCHAR)pVars;
if (!pVars)
return(FALSE);
memset(pVars, 0, sizeof(LOADER_VARS));
for (nCount=0 ; nCount < sizeof(LOADER_VARS) ; nCount++)
*(pTmp + nCount) = CMOSReadByte((BYTE)(CMOS_REG_RAMBASE + nCount));
return(VerifyEnvVars(pVars));
}
void StoreDefaultEnvVars(void)
{
memset((PUCHAR)&gLoaderVars, 0, sizeof(LOADER_VARS));
gLoaderVars.nIPAddr = 0;
gLoaderVars.nSubnetMask = 0;
gLoaderVars.nImgLoc = BL_ENET;
gLoaderVars.nUseDHCP = TRUE;
gLoaderVars.nAutoCount = 5; // Wait 5 seconds.
DEBUGMSG(ZONE_INFO, (TEXT("INFO: Storing default environment variables.\r\n")));
StoreEnvVars(&gLoaderVars);
}
void GetRealTimeFromCMOS(SYSTEMTIME *pst)
{
BYTE bAddr;
BYTE bStatusRegB;
// Read current hour/minute/second from CMOS
bAddr = _inp(CMOS_ADDR_REG);
_outp(CMOS_ADDR_REG,(bAddr&CMOS_ADDR_MASK)|CMOS_STATUS_REGB);
bStatusRegB = _inp(CMOS_DATA_REG);
_outp(CMOS_ADDR_REG,(bAddr&CMOS_ADDR_MASK)|CMOS_REG_SEC);
pst->wSecond = _inp(CMOS_DATA_REG);
_outp(CMOS_ADDR_REG,(bAddr&CMOS_ADDR_MASK)|CMOS_REG_MIN);
pst->wMinute = _inp(CMOS_DATA_REG);
_outp(CMOS_ADDR_REG,(bAddr&CMOS_ADDR_MASK)|CMOS_REG_HOUR);
pst->wHour = _inp(CMOS_DATA_REG);
_outp(CMOS_ADDR_REG,(bAddr&CMOS_ADDR_MASK)|CMOS_REG_DAY);
pst->wDay = _inp(CMOS_DATA_REG);
_outp(CMOS_ADDR_REG,(bAddr&CMOS_ADDR_MASK)|CMOS_REG_MONTH);
pst->wMonth = _inp(CMOS_DATA_REG);
if (!(bStatusRegB & REGB_DM_BINARY)) {
// Values returned in BCD.
pst->wSecond = DECODE_BCD(pst->wSecond);
pst->wMinute = DECODE_BCD(pst->wMinute);
pst->wHour = DECODE_BCD(pst->wHour);
pst->wDay = DECODE_BCD(pst->wDay);
pst->wMonth = DECODE_BCD(pst->wMonth);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -