🐈

RustでUefiApplicationを作成する方法

に公開

RustでUefiApplicationを作成する方法

RustでUefi Applicationを作ろうと思った際に、情報が少なくて苦労したというのと
UEFI Applicationを作る際に便利なuefi crateが結構大きく変更されているため苦労した

そのため、2025/10/6時点で最新のuefi-0.35.0を使ったuefi applicationの作り方をまとめます

crateの作成

基本的には下記を参照
https://rust-osdev.github.io/uefi-rs/tutorial/app.html

一点付け加えるなら
String, Vec<T>などを使うため、global_allocatorを足すと良い

uefi = { version = "0.35", default-features = false, features = ["global_allocator", "logger", "panic_handler"] }

BootServie/Runtime

BootServiceに所属する関数を使用する場合

uefi::boot::

Runtimeに所属する関数を使用する場合

uefi::runtime::

Variable

name: &CStr16
guid: VariableVendor
attr: VariableAttributes
size: usize
data: Vec<u8>

loop {
    match uefi::runtime::get_variable(name, guid, data) {
        Ok((slice, attrs)) => {
            attr = attrs;
            size = data.len();
            break;
        }
        Err(err) if err.status() == Status::BUFFER_TOO_SMALL=> {
            size = err.data().unwrap();
            data.resize(size, 0);
        }
        Err(err) => {
            break;
        }
    }
}

Protocol

let output_handle = match get_handle_for_protocol:: <Output>() {
    Ok(h) => h,
    Err(err) => return err.status(),
};
let output_protocol: boot::ScopedProtocol<Output> = match boot::open_protocol_exclusive:: <Output>(output_handle) {
    Ok(p) => p,
    Err(err) => return err.status()
};

補足

UEFI仕様書 (https://uefi.org/specs/UEFI/2.11/)
C言語の形式で書かれているが、SystemTable, Variable, ProtocolなどUEFIの基本的な概念はこちらを参照

uefi Crateのドキュメント (https://docs.rs/uefi/0.35.0/uefi/index.html)
実際に実装されている関数などはこちらを参照
UEFIの仕様には存在しない便利な関数なども実装されている

最後に

RustでUefi環境のvariable editorを作っています。
よかったら見ていってください
https://github.com/furiro/UefiEditor

Discussion