Continuing the saga of UEFI SEC stage checking.
Turns out, when our motherboards turn on, the sanity check at very early stages verifies whether the stack is functioning properly.
A very specific value for this test is being chosen by Asus: 0x5A5AA5A5
I could not find an exact reason why it is exactly that, but I suspect it is because 0x5 and 0xA are exact bitwise opposites:
0x5A5AA5A5 = 01011010 01011010 10100101 10100101
NOT 0x5A5AA5A5 = 10100101 10100101 01011010 01011010
= 0xA5A55A5A
If the value received from the stack does not match this inverted value, execution of UEFI stops.
Reference to the last post in the series:
teapaw.org/webbyVs/status/2124604…
I was tracing my motherboard’s UEFI boot in Ghidra. It starts in 16-bit real mode, then pulls a little magic trick 😅
Enable protected mode. Push a code selector and an address. Execute 66 CB (RETF).
Poof. The next instruction is MOV ESP, 0, and we’re running 32-bit code.
Linking previous post: teapaw.org/webbyVs/status/2124559…
Webby retweeted
Jensen Huang’s wife Lori Huang precaution after he joining back as CEO
“Prevention is better the cure” 😅
Turns out, this logic is not very new. What appears to be the first mention of this stack check is in the IBM PC.
The OG ...
So we all have a piece of the original 1981 IBM PC sitting inside our UEFI😅
github.com/philspil66/IBM-PC…
20
Webby retweeted
AI is changing the cybersecurity landscape, and not just for the better.💔
Sadly, the same technology helping security researchers uncover vulnerabilities is also the same technology empowering attackers to exploit them faster.💔
💂🏿♂️🛡️
I was tracing my motherboard’s UEFI boot in Ghidra. It starts in 16-bit real mode, then pulls a little magic trick 😅
Enable protected mode. Push a code selector and an address. Execute 66 CB (RETF).
Poof. The next instruction is MOV ESP, 0, and we’re running 32-bit code.
Linking previous post: teapaw.org/webbyVs/status/2124559…
CPU right after reset on my board:
The first thing it does is jump, disable interrupts, clear the POST port, and set up a stack.
Basically the firmware equivalent of:
“Alarm goes off → immediately hit snooze → roll over → mumble ‘five more minutes’ → finally sit up.”
It hasn’t even properly woken up yet, and it’s already trying to go back to sleep.
Not now, Mom.
For guys who don't like assembly that much, I have asked an AI to generate C++-like code
void switch_to_32_bit(Cpu& cpu) {
// The preceding CALL left a 16-bit return offset on the stack.
// XOR EAX,EAX + POP AX zero-extends that offset to 32 bits.
const std::uint32_t return_ip = cpu.pop16(); // 0xDBE0
const std::uint32_t entry = return_ip + 0x09FF002B; // 0x09FFDC0B
// Enable protected mode (PE) and set the Monitor Coprocessor bit (MP).
cpu.write_cr0(cpu.read_cr0() | 0x3u);
// Selector 0x18 picks GDT entry 3, the flat data and stack segment.
cpu.load_segment(DS, 0x18);
cpu.load_segment(ES, 0x18);
cpu.load_segment(FS, 0x18);
cpu.load_segment(GS, 0x18);
cpu.load_segment(SS, 0x18);
// Build the far-return frame. The destination goes on top.
cpu.push32(0x10); // CS selector for GDT entry 2
cpu.push32(entry); // Destination EIP
cpu.retf32(); // Loads CS:EIP and transfers to the code below
}
// Execution lands here at 0x09FFDC0B (0000:DC0B in Ghidra).
void after_switch(Cpu& cpu) {
cpu.esp = 0; // MOV ESP, 0
cpu.eip = 0x09FFE0C8; // JMP to 0000:E0C8 in Ghidra
}
30
CPU right after reset on my board:
The first thing it does is jump, disable interrupts, clear the POST port, and set up a stack.
Basically the firmware equivalent of:
“Alarm goes off → immediately hit snooze → roll over → mumble ‘five more minutes’ → finally sit up.”
It hasn’t even properly woken up yet, and it’s already trying to go back to sleep.
Not now, Mom.
809
















