http-boot-client/src/config.rs

71 lines
2.1 KiB
Rust

use alloc::string::String;
use log::{debug, info};
use uefi::prelude::*;
use uefi::proto::network::IpAddress;
use uefi::CStr16;
use uefi::{Error, Result};
use uefi_raw::table::runtime::VariableVendor;
use uefi_raw::{Guid, Status};
extern crate alloc;
const HTTP_BOOT_VENDOR: VariableVendor =
VariableVendor(Guid::parse_or_panic("638a24ce-a0ce-4908-8152-b2a4e8b2f29d"));
pub struct Config {
pub multicast_group: IpAddress,
pub multicast_port: u16,
pub client_uuid: String,
}
fn get_var<'a>(
runtime_services: &RuntimeServices,
var_name: &'a CStr16,
res: &'a mut [u8],
) -> Result<()> {
match runtime_services.get_variable(var_name, &HTTP_BOOT_VENDOR, res) {
Err(e) => Err(e),
Ok(_r) => Ok(()),
}
}
pub fn read_config(runtime_services: &RuntimeServices) -> Result<Config> {
info!("Reading EFI configuration from EFI vars");
let mut conf = Config {
multicast_group: IpAddress::new_v6([
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01,
]),
multicast_port: 42,
client_uuid: String::from("24609104-7ec6-423f-b57f-632b99b7566a"),
};
let mut port_buf: [u8; 2] = [0; 2]; // port is 16 bytes encoded
get_var(
runtime_services,
cstr16!("HTTP_BOOT_MCAST_PORT"),
&mut port_buf,
)?;
conf.multicast_port = u16::from_ne_bytes(port_buf);
debug!("Got port {}", conf.multicast_port);
let mut group_buf: [u8; 16] = [0; 16]; // at most ipv6 is 16 bytes
get_var(
runtime_services,
cstr16!("HTTP_BOOT_MCAST_GROUP"),
&mut group_buf,
)?;
conf.multicast_group = IpAddress::new_v6(group_buf);
debug!("Got group {:?}", conf.multicast_group);
let mut uid_buf: [u8; 36] = [0; 36]; // uuid is 36 bytes
get_var(runtime_services, cstr16!("HTTP_BOOT_UUID"), &mut uid_buf)?;
conf.client_uuid = match String::from_utf8(uid_buf.to_vec()) {
Err(_e) => return Err(Error::from(Status::INVALID_PARAMETER)),
Ok(s) => s,
};
debug!("Got uuid {:?}", conf.client_uuid);
Ok(conf)
}