UEFI Support for Memtest86+

Patricio Chilano Mateo

1 MEMTEST86+ http://www.memtest.org/

• Memory diagnostic tool for and x86-64 platforms • Release History - Initial release on 2004 as a fork of Memtest86 v3.0 - Latest release on 2013 as v5.01 - Released under GPL • Written in and x86 • Features - Support for most modern CPUs and chipsets - 2TB of RAM on x86-64 - SMT support for up to 32 cores (experimental) - Coreboot support 2 UEFI • Interface between firmware and • Provides Boot and Runtime services • Advantages - Avoid CPU real mode - Slow - Only 1MB address space - Larger disks (2T) by means of GPT - Bootmanager - Flexible bootloader setup - Better interface - Mouse support - Graphics - UEFI Applications (shell)

Source: https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface 3 Goal

• Memtest86+ limitations (vs MemTest86) - Runs in BIOS environment - int 0x15 for getting memory size - VGA framebuffers for screen - int 0x10 for loading disk sectors (bootloader code, so probably not needed) • Most modern motherboards have UEFI firmware • Add support to run in UEFI - Compile as UEFI application (Maybe CDROM image) - 32 bit initially - Run in a UEFI platform (Galileo or Minnowboard)

4 in UEFI

• EFI System Partition (ESP) - Extra partition in GPT disk - Contains EFI drivers, applications, boot loaders - FAT32 file system (could be fat16) - Convention to store bootloaders - /EFI/, /EFI/redhat, /EFI/boot • NVRAM with list of boot options - How to save items in NVRAM? - If booted in UEFI mode use efibootmgr (linux) • Bootmanager - Reads NVRAM list, tries each item until one doesn’t return • MACS do it a bit different - EFI firmware read HFS+ and loads bootlader

5 Source: https://en.wikipedia.org/wiki/GUID_Partition_Table Development Tools

• GNU-EFI - Simpler to use - Requires wrapping of function calls • TianoCore EFI Development Kit (EDKII) - Support for advanced or new EFI features • qemu • OVMF (Open Virtual Machine Firmware) - Tianocore image to run on Qemu • gdb - attach gdb to qemu to debug applications

6 Code Base (Memtest86+) • C ~ 15000 lines (*) - 4000 in controller.c - interacts with the memory controller - reads and writes using pci (pci.c ~ 200 loc) - 2500 in main.c and init.c - initializes the screen and calls the tests (not sure if should change) - 1200 in custom libc - 1500 in test.c - 650 in error.c - 700 in smp.c (multicore support) - 350 in memsize.c - gets memory information from linuxbios.c - 127 in screen_buffer.c - uses the VGA buffer to write to screen • X86 Assembly ~ 1400 • 900 in head.S - Configures final gdt and idt, reloctes code and calls test_start() in main.c • bootsect.S and setup.S should not be used

(*) Lots of inline assembly in C code 7 Lines of Code to Write

• Getting memory information (~400) • Printing to screen (~900) • Managing relocations (~300) • Exisiting memory controller shouldn’t change • Bootloader code should not be used if UEFI Application - Check in case of CDROM image • main (not sure)

• Total > 1600 lines

8 Schedule (Optimistic) • 10 weeks remaining • Stage I (1 – 2 weeks) - Understand existing code - Develop sample UEFI applications • Stage II (~2 weeks) - Compile memtest86+ as UEFI application - Retrieve memory information • Stage III (~2-3 weeks) - Replace VGA framebuffers and print to screen • Stage IV (~2 weeks) - Debugging / making it work - Fix relocations problems if necessary - Stage V (~1 week) - Try in real hardware

9 Challenges

• Understanding memtest86+ code base • Reading UEFI spec (some!) • UEFI development environment - Build complex applications with mix of x86 Assembly - C • Calling UEFI services • UEFI memory mappings (memtest86+ relocates itself) • Debug uefi applications in qemu using gdb

10 Documentation

• UEFI specification • http://www.uefi.org/sites/default/files/resources/UEFI_Spec_2_7.pdf • UEFI Programming • https://www.rodsbooks.com/efi-programming/index.html • http://wiki.osdev.org/UEFI#Developing_with_GNU-EFI • Intel Software Developer Manual • https://software.intel.com/en-us/articles/intel-sdm#combined

11 Additional features (would be nice)

• Native 64 bit code • Add mouse support • Write reports/logs in external drive • Secure Boot • Bootable from USB/CD - Dual Boot (BIOS and UEFI support in single image)

12