如何在 uefi-rs(UEFI Rust 包装器)中读取用户输入

问题描述 投票:0回答:1

我想知道是否有任何方法可以使用 uefi-rs Rust 包装器读取 UEFI 中的用户输入。 (或者我可以使用 Rust 收集 UEFI 中的用户输入的任何其他方式)

我已经尝试了许多不同的方法来使用 read_key(关键事件),其中一种甚至没有编译,另一种只打印零。 我还翻遍了数十个 GitHub 存储库,但似乎没有一个使用这些方法。

第一种方法(读取密钥):

    let bs = st.boot_services();
    let read_key =  st.stdin().read_key();
    let unwrap = read_key.unwrap();
    if unwrap.is_some() {
        st.stdout().write_str(&unwrap.expect("Error").fmt(&mut fmt::Formatter).to_string()).expect("Failed to write to the console.");
    }

第二种方法(等待事件):

    let mut binding = st.unsafe_clone();
    key_event = binding.stdin().wait_for_key_event();
    let result = bs.wait_for_event(&mut [key_event.unsafe_clone()]);
    let usize = result.unwrap();
    st.stdout().write_str(&usize.to_string()).expect("Failed to write to the console.");

那里可能有很多初学者的错误,因为我对 Rust 没有太多经验。我做了几个项目,现在我发现了这个,所以我想尝试一下。用户输入在 Assembly 中相对容易。 (根据我迄今为止使用 Rust 的经历)

如果有人知道我可以检查的存储库或知道答案,请提供帮助。现在已经为此工作了几个小时了。

板条箱文档链接:https://docs.rs/uefi/latest/uefi/ 源代码:https://github.com/rust-osdev/uefi-rs

user-interface rust user-input uefi
1个回答
1
投票

这是一个读取输入直到用户按“ESC”的简短示例。

#![no_main]
#![no_std]
#![feature(abi_efiapi)]

use core::fmt::{Write};
use uefi::{prelude::*, proto::console::text::ScanCode, Char16};

#[entry]
fn main(_handle: Handle, mut st: SystemTable<Boot>) -> Status {
    uefi_services::init(&mut st).unwrap();
    
    let mut exit_flag = false;

    while !exit_flag {
        let key = st.stdin().read_key().expect("Expected input");
        let _ = match key {
            Some(k) =>  {
                match k {
                    uefi::proto::console::text::Key::Printable(p) => {
                        write!(st.stdout(), "A printable key was entered: {:?}\r\n", p).expect("Write failed");
                        if p == Char16::try_from(27u16).expect("Unable to convert the ESC ascii code to Char16") {
                            exit_flag = true;
                        }
                    }
                    uefi::proto::console::text::Key::Special(s) => {
                        write!(st.stdout(), "A special key was entered: {:?}\r\n", s).expect("Write failed");
                        if s == ScanCode::ESCAPE {
                            exit_flag = true;
                        }
                    }
                };             
            },
            None => {}
        };
    }

    Status::SUCCESS
}

我以前从未使用过 uefi-rs,可能有更好的解决方案来处理这个板条箱的输入。

template为起点并替换main.rs。 构建:

cargo +nightly build --target x86_64-unknown-uefi

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.