Search code examples
x86x86-64bootloaderuefigrub

Who enables the A20 line when booting in pure UEFI?


Is this handled by the UEFI firmware or by for the GRUB grubx64.efi bootloader?

I looked at https://wiki.osdev.org/UEFI which claims:

UEFI firmware ... also prepares a protected mode environment with flat segmentation and for x86-64 CPUs, a long mode environment with identity-mapped paging. The A20 gate is enabled as well.

But could not find any official sources to back up this information. The UEFI specification does not mention this.

The linux kernel provides a efi-stub that can act as a bootloader, but after checking its source I can not see if it enables A20 or not. So I still do not know if its the job of UEFI firmware or the job of the bootloader.

(I want to write my own bootloader for UEFI and want to understand what setup does UEFI firmware provide "out of the box" and what part must be implemented by me)


Solution

  • UEFI firmware enables A20 if it isn't already enabled, sets up the GDT with flat descriptors, enters protected mode etc. On 64-bit CPUS it also enters long mode which involved enabling identity mapped paging. In order for UEFI to operate as expected it needs A20 enabled to have proper access to all the physical memory.

    On some more modern processors A20 is enabled at power on and may not even have the ability to be turned off. Intel has begun moving away from the legacy requirements including the dropping of support pre-286 environments. The existence of an A20 gate that could be toggled came about so that the 286 (and later processors) would retain compatibility with older 8086/80186 (or equivalent) processors. The inability to change the A20 state, and the dropping of the legacy BIOS are a move in that direction.

    Once your UEFI bootloader code starts running, you are guaranteed that A20 will be enabled at that point. A20 is just something you don't concern yourself with from the perspective of enabling it.

    Although A20 isn't specifically mentioned in the UEFI specification, it seems to be implied:

    2.3.2.1 Handoff State When a 32-bit UEFI OS is loaded, the system firmware hands off control to the OS in flat 32-bit mode. All descriptors are set to their 4GiB limits so that all of memory is accessible from all segments.

    With A20 disabled not all memory is accessible from all segments, thus I infer that A20 must be enabled by default in the processor or the UEFI firmware.