Newer
Older
AMI-Aptio-BIOS-Reversed / TimestampDxe / TimestampDxe.md
@Ajax Dong Ajax Dong 2 days ago 6 KB Init

TimestampDxe

Function Table

Address Name Description
AsmReadTsc
AsmReadEflags
AsmDisableInterrupts
AsmEnableInterrupts
AsmPause
IoRead32
MmioWrite16
IoRead8
IoWrite8
ReadUnaligned64
LocateProtocol
DebugPrint
DebugAssert
CompareGuid
PcdGet32
TimestampGetTimestamp
TimestampGetProperties
CalibrateTscFrequency
TimestampDxeEntryPoint
Module Globals
System Table, Boot Services, Runtime Services (saved from entry point)
Debug protocol interface (used for DebugPrint / DebugAssert)
HOB list and PCD protocol caches
PCI Express ECAM base address
TSC frequency determined by calibration
Timestamp protocol state
EFI_TIMESTAMP_PROTOCOL interface function table.
Installed by InstallMultipleProtocolInterfaces.
STATIC CONST EFI_TIMESTAMP_PROTOCOL mTimestampInterface = {
Known GUIDs (in the binary's .rdata section)
Internal Library Helpers (wrapped from BaseLib / compiler intrinsics)
ASSERT ((Port & 3) == 0) port must be DWORD-aligned
return __indword (Port);
ASSERT ((Address & 1) == 0)
Assert that (Length - 1) <= (MAX_UINTN - (UINTN)Destination) and
if (Source < Destination &&
Overlap with source before destination: copy backwards.
UINTN Count;
Copy 8-byte aligned chunks in reverse.
Fall through to qmemcpy of remaining 8-byte blocks
then byte-by-byte for the remainder.
ASSERT (Buffer != NULL)
return (UINT64 )Buffer;
UEFI Library Wrappers
Call the debug protocol's print function at offset 0x00.
Signatures differ; this is a simplified wrapper.
Call the debug protocol's assert function at offset 0x08.
Compare the GUID at gSystemTable->ConfigurationTable[Index].VendorGuid
against EFI_HOB_LIST_GUID.
if (CompareGuid (
The PCD protocol's Get32() is the 4th function in the protocol
offset +32 from the interface pointer).
return ((UINT32 (EFIAPI )(UINTN))((UINT8 )mPcdProtocol + 32))(TokenNumber);
Timestamp Protocol Implementation
mBaseTimeStamp <= mEndTime is always TRUE (since mEndTime = MAX_UINT64).
Return elapsed ticks = currentTSC - base.
if (mBaseTimeStamp <= mEndTime) {
Zero out the 16-byte output buffer.
CopyMem (TimestampProperties, NULL, 16);
Copy the current frequency and end time values.
Frequency is at [0x00], EndTime at [0x08].
values here (it only zeroes the buffer). This is likely a bug or intentional
stub in the original implementation, or the values are copied implicitly
via shared global data that the caller accesses directly.
For correctness, we provide the proper values:
Calibration and Initialization
Save interrupt state.
Eflags = AsmReadEflags ();
Read initial reference counter value (24-bit masked).
InitialCounter = IoRead32 (TIMESTAMP_CALIBRATION_PORT) & 0xFFFFFF;
Record start TSC.
TscStart = AsmReadTsc ();
Wait for the reference counter to advance by TIMESTAMP_CALIBRATION_DELTA.
In 24-bit signed arithmetic, bit 23 is set when the difference wraps past
which means "continue while current <= initial + delta (no wrap)."
When current surpasses (initial + delta), (initial + delta - current)
becomes negative in 24-bit signed arithmetic, bit 23 is set, and the
loop exits.
do {
Record end TSC.
if (InterruptsEnabled) {
TSC frequency = TSC ticks during calibration * 10000.
The reference counter delta of 357 at whatever frequency port 0x508 runs
corresponds to a fixed time window. Multiplying TSC ticks by 10000 yields
ticks per second.
return TscDelta * TIMESTAMP_TICK_TO_HZ_MULTIPLIER;
Entry Point
Step 1: Save UEFI core-provided pointers.
gImageHandle = ImageHandle;
These are pointer validity checks. The EDK2 library constructors would
normally perform them; in this standalone driver, we guard manually.
if (gImageHandle == NULL) {
Step 2: Locate the HOB list from the System Table ConfigurationTable.
GetHobList ();
Step 3: Get the PCD protocol interface.
Step 4: Read the PCI Express base address (MMIO base for ECAM).
PCD token 5 = PcdPciExpressBaseAddress (typically).
mPciExpressBaseAddress = PcdGet32 (5);
Step 5: Check if the PCIe Enhanced Configuration Access is enabled and
optionally write to the RTC index register via PCIe ECAM to pre-enable
the reference counter. PCD token 1024068 (0xFA044) = PcdPciExpressBaseSize
or equivalent size enable.
PciExpressBaseSize = PcdGet32 (1024068);
ECAM address space is accessible. Write 0x0500 to the PCIe config space
of the RTC/LPC controller to enable the reference counter.
Token 1024064 (0xFA040) = PcdPciExpressBaseAddress (base).
PciExpressBaseAddr = PcdGet32 (1024064);
Mark the PCIe ECAM enable as done by setting the top bit of the size PCD.
Step 6: Calibrate the TSC frequency.
mTimestampFrequencyHz = CalibrateTscFrequency ();
Step 7: Set up timestamp protocol state.
mBaseTimeStamp = 0;
Step 8: Install the EFI_TIMESTAMP_PROTOCOL.
TimestampHandle = NULL;

Generated by HR650X BIOS Decompilation Project