Standard library · System and tools

uefi

Firmware services for a UEFI application built with the native backend: console output, key input, memory, delays and shutdown.

import uefi

uefi is for programs that run as a UEFI application: no operating system, no C library, started by the firmware. Build one with salam build app.salam --backend=native --target=x86_64-unknown-uefi --output=BOOTX64.EFI and put the file at EFI/BOOT/BOOTX64.EFI on a FAT disk. The result is a PE32+ image with subsystem 10 (EFI application), no import table and a .reloc section, so the firmware can load it at any address. aarch64-unknown-uefi builds the same way, but the firmware looks for EFI/BOOT/BOOTAA64.EFI, so name the output BOOTAA64.EFI.

The program's main function is called with the image handle and the system table stored for the package, and println writes to the firmware console. A newline becomes a carriage return and a line feed, and UTF-8 text is converted to UTF-16 for the firmware. main returning 0 reports success to the firmware; any other value reports an aborted status. The standard memory functions allocate from the firmware pool.

A first application

Println and Print write through the same console as println and print. Shutdown powers the machine off, which is how a test run under QEMU ends. On a normal computer the same program runs as an ordinary program, so you can test it before you boot it.

uefi-guide-hello.salam
Try it
import uefi

func main:
    println "Hello from Salam"
    uefi.Println("through the firmware console")
    buf := uefi.Allocate(64 as u64)
    uefi.Free(buf)
    uefi.Shutdown()
end
Output
Hello from Salam
through the firmware console

Status codes

Firmware services report a status as a 64-bit number: 0 is success and a number with the top bit set is an error. IsError tells them apart and StatusName gives the text for the common ones.

uefi-guide-status.salam
Try it
import uefi

func main:
    println uefi.Available()
    println uefi.success, uefi.IsError(uefi.success)
    println uefi.not_ready, uefi.IsError(uefi.not_ready), uefi.StatusName(uefi.not_ready)
    println uefi.aborted, uefi.StatusName(uefi.aborted)
    println uefi.StatusName(9223372036854775809 as u64)
end
Output
false
0 false
9223372036854775814 true Not ready
9223372036854775829 Aborted
Load error

Constants and variables

success

#
const success: u64 = 0

The status 0, returned by a firmware service that worked.

uefi-Status.salam
Try it
import uefi

func main:
    println uefi.Available()
    println uefi.success, uefi.IsError(uefi.success)
    println uefi.not_ready, uefi.IsError(uefi.not_ready), uefi.StatusName(uefi.not_ready)
    println uefi.aborted, uefi.StatusName(uefi.aborted)
    println uefi.StatusName(9223372036854775809 as u64)
end
Output
false
0 false
9223372036854775814 true Not ready
9223372036854775829 Aborted
Load error

not_ready

#
const not_ready: u64 = -9223372036854775802

The status a firmware service returns when no input is waiting, such as a key that has not been pressed yet.

The example for this item is shown with the ones above: uefi-Status.salam

aborted

#
const aborted: u64 = -9223372036854775787

The status used for a failed exit: Exit with a non-zero code and a main that returns a non-zero code both report it.

The example for this item is shown with the ones above: uefi-Status.salam

Functions

Available

#
func Available(): bool

True when the program runs as a UEFI application, false on a normal computer where the functions use the host instead.

The example for this item is shown with the ones above: uefi-Status.salam

ImageHandle

#
func ImageHandle(): void*

The handle of the running image, as the firmware passed it to the entry point. Null on a normal computer.

uefi-System.salam
Try it
import uefi

func main:
    println (uefi.ImageHandle() as u64) == (0 as u64)
    println (uefi.SystemTable() as u64) == (0 as u64)
    buf := uefi.Allocate(128 as u64)
    println (buf as u64) != (0 as u64)
    uefi.Free(buf)
    uefi.Stall(1000 as u64)
end
Output
true
true
true

SystemTable

#
func SystemTable(): void*

The system table pointer the firmware passed to the entry point. Null on a normal computer.

The example for this item is shown with the ones above: uefi-System.salam

IsError

#
func IsError(status: u64): bool

True when a status has its top bit set, which is how firmware marks an error.

The example for this item is shown with the ones above: uefi-Status.salam

StatusName

#
func StatusName(status: u64): str

The text for a status: Success, a warning, or the name of a common error such as Not ready, Not found or Out of resources. Other errors give Error.

The example for this item is shown with the ones above: uefi-Status.salam

Print

#
func Print(text: str)

Writes text to the console without a newline. The same as print, kept here so a UEFI program reads the same on its own.

uefi-Console.salam
Try it
import uefi

func main:
    uefi.Print("Hello, ")
    uefi.Println("firmware")
    uefi.Clear()
    println uefi.ReadKey()
end
Output
Hello, firmware
-1

Println

#
func Println(text: str)

Writes text to the console followed by a new line.

The example for this item is shown with the ones above: uefi-Console.salam

Allocate

#
func Allocate(size: u64): void*

Allocates memory from the firmware pool, aligned to 16 bytes. Returns null when the firmware has no memory left.

The example for this item is shown with the ones above: uefi-System.salam

Free

#
func Free(ptr: void*)

Returns memory from Allocate to the firmware pool. Null is ignored.

The example for this item is shown with the ones above: uefi-System.salam

Stall

#
func Stall(micros: u64)

Waits the given number of microseconds with the firmware timer. On a normal computer it sleeps.

The example for this item is shown with the ones above: uefi-System.salam

Clear

#
func Clear()

Clears the console screen. Does nothing on a normal computer.

The example for this item is shown with the ones above: uefi-Console.salam

ReadKey

#
func ReadKey(): int

Waits for a key and returns its Unicode character, or 65536 plus the scan code for a key without a character such as an arrow, or -1 on a failure and on a normal computer.

The example for this item is shown with the ones above: uefi-Console.salam

Exit

#
func Exit(code: int)

Leaves the application and returns control to the firmware with success for 0 and an aborted status for any other code. On a normal computer it exits the process.

uefi-Exit.salam
Try it
import uefi

func main:
    println "leaving"
    uefi.Exit(0)
    println "not reached"
end
Output
leaving

Halt

#
func Halt()

Waits forever, one second at a time. On a normal computer it returns at once.

uefi-Halt.salam
Try it
import uefi

func main:
    println "before"
    uefi.Halt()
    println "after"
end
Output
before
after

Shutdown

#
func Shutdown()

Powers the machine off through the firmware. On a normal computer it exits the process with 0.

uefi-Shutdown.salam
Try it
import uefi

func main:
    println "shutting down"
    uefi.Shutdown()
    println "not reached"
end
Output
shutting down