# Unified Extensible Firmware Interface (UEFI) Support

_Peios Kernel / Hardware and drivers / Driver development / firmware: efi_

> Reference for Unified Extensible Firmware Interface (UEFI) Support.

> **Peios:** File-based firmware loading has an additional Peios signature check. The development profile defaults to logging (`kacs_fwsig=log`); `kacs_fwsig=enforce` requires PeiosTcb trust for that path. This does not cover the separate `FW_UPLOAD` interface. See [Peios kernel behaviour](</kernel/introduction/peios-behaviour.md>).


<a id="rst-unified-extensible-firmware-interface-uefi-support"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-uefi-stub-library-functions"></a>

## UEFI stub library functions


#### <a id="c-efi_get_memory_map"></a>`efi_get_memory_map()`

```c
efi_status_t efi_get_memory_map(struct efi_boot_memmap **map, bool install_cfg_tbl)
```

get memory map

**Parameters**

- `struct efi_boot_memmap **map`: pointer to memory map pointer to which to assign the
  newly allocated memory map
- `bool install_cfg_tbl`: whether or not to install the boot memory map as a
  configuration table

**Description**

Retrieve the UEFI memory map. The allocated memory leaves room for
up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.

**Return**

status code

#### <a id="c-efi_allocate_pages"></a>`efi_allocate_pages()`

```c
efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr, unsigned long max)
```

Allocate memory pages

**Parameters**

- `unsigned long size`: minimum number of bytes to allocate
- `unsigned long *addr`: On return the address of the first allocated page. The first
  allocated page has alignment EFI_ALLOC_ALIGN which is an
  architecture dependent multiple of the page size.
- `unsigned long max`: the address that the last allocated memory page shall not
  exceed

**Description**

Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
given by **max**.

**Return**

status code

#### <a id="c-efi_free"></a>`efi_free()`

```c
void efi_free(unsigned long size, unsigned long addr)
```

free memory pages

**Parameters**

- `unsigned long size`: size of the memory area to free in bytes
- `unsigned long addr`: start of the memory area to free (must be EFI_PAGE_SIZE
  aligned)

**Description**

**size** is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
architecture specific multiple of EFI_PAGE_SIZE. So this function should
only be used to return pages allocated with [`efi_allocate_pages()`](#c-efi_allocate_pages) or
`efi_low_alloc_above()`.


<a id="rst-uefi-common-platform-error-record-cper-functions"></a>

## UEFI Common Platform Error Record (CPER) functions


#### <a id="c-cper_print_bits"></a>`cper_print_bits()`

```c
void cper_print_bits(const char *pfx, unsigned int bits, const char * const strs[], unsigned int strs_size)
```

print strings for set bits

**Parameters**

- `const char *pfx`: prefix for each line, including log level and prefix string
- `unsigned int bits`: bit mask
- `const char * const strs[]`: string array, indexed by bit position
- `unsigned int strs_size`: size of the string array: **strs**

**Description**

For each set bit in **bits**, print the corresponding string in **strs**.
If the output length is longer than 80, multiple line will be
printed, with **pfx** is printed at the beginning of each line.

#### <a id="c-cper_bits_to_str"></a>`cper_bits_to_str()`

```c
int cper_bits_to_str(char *buf, int buf_size, unsigned long bits, const char * const strs[], unsigned int strs_size)
```

return a string for set bits

**Parameters**

- `char *buf`: buffer to store the output string
- `int buf_size`: size of the output string buffer
- `unsigned long bits`: bit mask
- `const char * const strs[]`: string array, indexed by bit position
- `unsigned int strs_size`: size of the string array: **strs**

**Description**

Add to **buf** the bitmask in hexadecimal. Then, for each set bit in **bits**,
add the corresponding string describing the bit in **strs** to **buf**.

A typical example is:

```text
const char * const bits[] = {
        "bit 3 name",
        "bit 4 name",
        "bit 5 name",
};
char str[120];
unsigned int bitmask = BIT(3) | BIT(5);
#define MASK GENMASK(5,3)

cper_bits_to_str(str, sizeof(str), FIELD_GET(MASK, bitmask),
                 bits, ARRAY_SIZE(bits));
```

The above code fills the string `str` with `bit 3 name|bit 5 name`.

**Return**

number of bytes stored or an error code if lower than zero.
