Skip to content

Commit

Permalink
feat: introduce PushVecU8 trait to replace WriteBytesExt trait (which…
Browse files Browse the repository at this point in the history
… makes us to .unwrap())
  • Loading branch information
iTrooz committed Dec 25, 2023
1 parent 53b59a5 commit 02c7ab2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
36 changes: 15 additions & 21 deletions efivar/src/boot/parse/device_path.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! This module contains parsing code for a device path, part of a device path list

use std::{convert::TryInto, fmt::Display, io::Write, path::PathBuf};
use std::{convert::TryInto, fmt::Display, path::PathBuf};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use byteorder::{LittleEndian, ReadBytesExt};
use uuid::Uuid;

use crate::{utils::read_nt_utf16_string, Error};
use crate::{push::PushVecU8, utils::read_nt_utf16_string, Error};

use super::consts;

Expand Down Expand Up @@ -138,8 +138,8 @@ impl DevicePath {
fn encap_as_device_path(r#type: u8, r#subtype: u8, mut raw_data: Vec<u8>) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![];

bytes.push(r#type);
bytes.push(r#subtype);
bytes.push_u8(r#type);
bytes.push_u8(r#subtype);

let raw_data_size: u16 = raw_data.len().try_into().unwrap();
bytes.append(&mut (raw_data_size + 1 + 1 + 2).to_le_bytes().to_vec());
Expand All @@ -153,23 +153,17 @@ impl EFIHardDrive {
/// get bytes representation for a EFIHardDrive, without encapsulating them in a DevicePath structure
fn to_bytes_raw(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![];
bytes
.write_u32::<LittleEndian>(self.partition_number)
.unwrap();
bytes
.write_u64::<LittleEndian>(self.partition_start)
.unwrap();
bytes
.write_u64::<LittleEndian>(self.partition_size)
.unwrap();
bytes.push_u32(self.partition_number);
bytes.push_u64(self.partition_start);
bytes.push_u64(self.partition_size);

let (f1, f2, f3, f4) = self.partition_sig.as_fields();
bytes.write_u32::<LittleEndian>(f1).unwrap();
bytes.write_u16::<LittleEndian>(f2).unwrap();
bytes.write_u16::<LittleEndian>(f3).unwrap();
bytes.write_all(f4).unwrap();
bytes.write_u8(self.format).unwrap();
bytes.write_u8(self.sig_type.as_u8()).unwrap();
bytes.push_u32(f1);
bytes.push_u16(f2);
bytes.push_u16(f3);
bytes.append(&mut f4.to_vec());
bytes.push_u8(self.format);
bytes.push_u8(self.sig_type.as_u8());

bytes
}
Expand Down Expand Up @@ -199,7 +193,7 @@ impl FilePath {
.collect();

// write null termination
utf8_bytes.write_u16::<LittleEndian>(0x0000).unwrap();
utf8_bytes.push_u16(0x0000);

utf8_bytes
}
Expand Down
1 change: 1 addition & 0 deletions efivar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod store;
pub mod boot;
mod enumerator;
mod error;
pub mod push;
mod reader;
mod sys;
pub mod test_utils;
Expand Down
23 changes: 23 additions & 0 deletions efivar/src/push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub trait PushVecU8 {
fn push_u8(&mut self, value: u8);
fn push_u16(&mut self, value: u16);
fn push_u32(&mut self, value: u32);
fn push_u64(&mut self, value: u64);
}
impl PushVecU8 for Vec<u8> {
fn push_u8(&mut self, value: u8) {
self.push(value)
}

fn push_u16(&mut self, value: u16) {
self.append(&mut value.to_le_bytes().to_vec())
}

fn push_u32(&mut self, value: u32) {
self.append(&mut value.to_le_bytes().to_vec())
}

fn push_u64(&mut self, value: u64) {
self.append(&mut value.to_le_bytes().to_vec())
}
}
9 changes: 4 additions & 5 deletions efivar/src/sys/linux/efivarfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use std::str::FromStr;

use super::LinuxSystemManager;
use crate::efi::{Variable, VariableFlags};
use crate::push::PushVecU8;
use crate::{Error, VarEnumerator, VarManager, VarReader, VarWriter};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use byteorder::{LittleEndian, ReadBytesExt};

pub const EFIVARFS_ROOT: &str = "/sys/firmware/efi/efivars";

Expand Down Expand Up @@ -86,12 +87,10 @@ impl VarWriter for SystemManager {
let mut buf = Vec::with_capacity(std::mem::size_of_val(&attribute_bits));

// Write attributes
buf.write_u32::<LittleEndian>(attribute_bits)
.map_err(|error| Error::for_variable(error, var))?;
buf.push_u32(attribute_bits);

// Write variable contents
buf.write(value)
.map_err(|error| Error::for_variable(error, var))?;
buf.append(&mut value.to_vec());

// Filename to the matching efivarfs file for this variable
let filename = format!("{}/{}", EFIVARFS_ROOT, var);
Expand Down

0 comments on commit 02c7ab2

Please sign in to comment.