Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code #49

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::cell::Cell;
use orbclient::{Color, Mode, Renderer};
use std::prelude::*;
use std::proto::Protocol;
use std::uefi::graphics::{GraphicsOutput, GraphicsBltOp, GraphicsBltPixel};
use std::uefi::graphics::{GraphicsBltOp, GraphicsBltPixel, GraphicsOutput};
use std::uefi::guid::GRAPHICS_OUTPUT_PROTOCOL_GUID;

pub struct Output(pub &'static mut GraphicsOutput);
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Display {
y as usize,
w as usize,
h as usize,
0
0,
);
status.is_success()
}
Expand Down
457 changes: 256 additions & 201 deletions src/fde.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/hii.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: GPL-3.0-only

use std::proto::Protocol;
use std::prelude::*;
use std::uefi::hii::database::HiiDatabase;
use std::proto::Protocol;
use std::uefi::guid::HII_DATABASE_GUID;
use std::uefi::hii::database::HiiDatabase;

#[allow(dead_code)]
pub struct Database(pub &'static mut HiiDatabase);
Expand Down
12 changes: 8 additions & 4 deletions src/image/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ pub fn parse(file_data: &[u8]) -> core::result::Result<Image, String> {
let getw = |i: usize| -> u16 { (get(i) as u16) + ((get(i + 1) as u16) << 8) };

let getd = |i: usize| -> u32 {
(get(i) as u32) + ((get(i + 1) as u32) << 8) + ((get(i + 2) as u32) << 16) +
((get(i + 3) as u32) << 24)
(get(i) as u32)
+ ((get(i + 1) as u32) << 8)
+ ((get(i + 2) as u32) << 16)
+ ((get(i + 3) as u32) << 24)
};

let gets = |start: usize, len: usize| -> String {
(start..start + len).map(|i| get(i) as char).collect::<String>()
(start..start + len)
.map(|i| get(i) as char)
.collect::<String>()
};

if gets(0, 2) == "BM" {
Expand Down Expand Up @@ -90,7 +94,7 @@ pub fn parse(file_data: &[u8]) -> core::result::Result<Image, String> {
// It shouldn't ever return an Err in this case, unless there's an error somewhere
// above
Image::from_data(width, height, data.into_boxed_slice())
}else{
} else {
Err("BMP: invalid signature".to_string())
}
}
26 changes: 20 additions & 6 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ pub struct ImageRoi<'a> {
y: u32,
w: u32,
h: u32,
image: &'a Image
image: &'a Image,
}

impl<'a> ImageRoi<'a> {
/// Draw the ROI on a window
pub fn draw<R: Renderer>(&self, renderer: &mut R, x: i32, mut y: i32) {
let stride = self.image.w;
let mut offset = (self.y * stride + self.x) as usize;
let last_offset = cmp::min(((self.y + self.h) * stride + self.x) as usize, self.image.data.len());
let last_offset = cmp::min(
((self.y + self.h) * stride + self.x) as usize,
self.image.data.len(),
);
while offset < last_offset {
let next_offset = offset + stride as usize;
renderer.image_legacy(x, y, self.w, 1, &self.image.data[offset..]);
Expand All @@ -49,13 +52,24 @@ impl Image {

/// Create a new image filled whole with color
pub fn from_color(width: u32, height: u32, color: Color) -> Self {
Self::from_data(width, height, vec![color; width as usize * height as usize].into_boxed_slice()).unwrap()
Self::from_data(
width,
height,
vec![color; width as usize * height as usize].into_boxed_slice(),
)
.unwrap()
}

/// Create a new image from a boxed slice of colors
pub fn from_data(width: u32, height: u32, data: Box<[Color]>) -> core::result::Result<Self, String> {
pub fn from_data(
width: u32,
height: u32,
data: Box<[Color]>,
) -> core::result::Result<Self, String> {
if (width * height) as usize != data.len() {
return Err("not enough or too much data given compared to width and height".to_string())
return Err(
"not enough or too much data given compared to width and height".to_string(),
);
}

Ok(Image {
Expand All @@ -78,7 +92,7 @@ impl Image {
y: y1,
w: x2 - x1,
h: y2 - y1,
image: self
image: self,
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ pub fn raw_key(wait: bool) -> Result<TextInputKey> {

if wait {
let mut index = 0;
Result::from((uefi.BootServices.WaitForEvent)(1, &uefi.ConsoleIn.WaitForKey, &mut index))?;
Result::from((uefi.BootServices.WaitForEvent)(
1,
&uefi.ConsoleIn.WaitForKey,
&mut index,
))?;
}

let mut key = TextInputKey {
ScanCode: 0,
UnicodeChar: 0
UnicodeChar: 0,
};

Result::from((uefi.ConsoleIn.ReadKeyStroke)(uefi.ConsoleIn, &mut key))?;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use std::prelude::*;
use core::ptr;

mod display;
mod fde;
mod hii;
pub mod image;
mod key;
mod rng;
mod fde;
mod security;
mod ui;

Expand Down
4 changes: 2 additions & 2 deletions src/rng.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::ptr;
use std::proto::Protocol;
use std::prelude::*;
use std::proto::Protocol;

pub const RNG_PROTOCOL_GUID: Guid = Guid(0x3152bca5, 0xeade, 0x433d, [0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44]);
pub const RNG_PROTOCOL_GUID: Guid = guid!("3152bca5-eade-433d-862e-c01cdc291f44");

pub struct Rng(pub &'static mut RngProtocol);

Expand Down
60 changes: 30 additions & 30 deletions src/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use ectool::{AccessLpcDirect, Ec, SecurityState, Timeout};
use orbclient::{Color, Renderer};
use std::prelude::*;
use std::proto::Protocol;
use std::uefi::{
boot::InterfaceType,
reset::ResetType,
};
use std::uefi::{boot::InterfaceType, reset::ResetType};

use crate::display::{Display, Output};
use crate::key::{key, Key};
Expand Down Expand Up @@ -59,9 +56,9 @@ fn confirm(display: &mut Display) -> Result<()> {
let margin_tb = 8 * scale;

let form_width = cmp::min(640 * scale as u32, display_w - margin_lr as u32 * 2);
let form_x = (display_w as i32 - form_width as i32)/ 2;
let form_x = (display_w as i32 - form_width as i32) / 2;

let title_font_size = (20 * scale) as f32;
let title_font_size = (20 * scale) as f32;
let font_size = (16 * scale) as f32;
// } Style

Expand Down Expand Up @@ -138,7 +135,7 @@ fn confirm(display: &mut Display) -> Result<()> {
y,
form_width + margin_lr as u32,
1,
Color::rgb(0xac, 0xac, 0xac)
Color::rgb(0xac, 0xac, 0xac),
);
y += margin_tb * 2;
}
Expand All @@ -152,15 +149,22 @@ fn confirm(display: &mut Display) -> Result<()> {

// Draw input box
let input_text = ui.font.render(&input, font_size);
ui.draw_pretty_box(display, x, y, max_input_text.width(), font_size as u32, false);
ui.draw_pretty_box(
display,
x,
y,
max_input_text.width(),
font_size as u32,
false,
);
input_text.draw(display, x, y, ui.text_color);
if input.len() < code.len() {
display.rect(
x + input_text.width() as i32,
y,
font_size as u32 / 2,
font_size as u32,
ui.text_color
ui.text_color,
);
}
y += font_size as i32 + margin_tb;
Expand Down Expand Up @@ -189,7 +193,7 @@ fn confirm(display: &mut Display) -> Result<()> {
bottom_y,
form_width + margin_lr as u32,
1,
Color::rgb(0xac, 0xac, 0xac)
Color::rgb(0xac, 0xac, 0xac),
);
}

Expand All @@ -199,16 +203,14 @@ fn confirm(display: &mut Display) -> Result<()> {
match k {
Key::Backspace => {
input.pop();
},
Key::Character(c) => {
match c {
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
if input.len() < code.len() {
input.push(c);
}
}
Key::Character(c) => match c {
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
if input.len() < code.len() {
input.push(c);
}
_ => (),
}
_ => (),
},
Key::Enter => {
if button_i == 0 {
Expand All @@ -223,19 +225,19 @@ fn confirm(display: &mut Display) -> Result<()> {
// Return error if cancel selected
return Err(Status::ABORTED);
}
},
}
Key::Escape => {
input.clear();
},
}
Key::Down => {
if button_i + 1 < buttons.len() {
button_i += 1;
}
},
}
Key::Up => {
button_i = button_i.saturating_sub(1);
},
_ => {},
}
_ => {}
}
}
}
Expand Down Expand Up @@ -278,7 +280,7 @@ extern "efiapi" fn run() -> bool {
display.sync();

res
},
}
Err(err) => Err(err),
};

Expand All @@ -293,15 +295,15 @@ extern "efiapi" fn run() -> bool {
ResetType::Shutdown,
Status(0),
0,
ptr::null()
ptr::null(),
);
}
}

true
}

pub const SYSTEM76_SECURITY_PROTOCOL_GUID: Guid = Guid(0x764247c4, 0xa859, 0x4a6b, [0xb5, 0x00, 0xed, 0x5d, 0x7a, 0x70, 0x7d, 0xd4]);
pub const SYSTEM76_SECURITY_PROTOCOL_GUID: Guid = guid!("764247c4-a859-4a6b-b500-ed5d7a707dd4");
pub struct System76SecurityProtocol {
#[allow(dead_code)]
pub Run: extern "efiapi" fn() -> bool,
Expand All @@ -312,16 +314,14 @@ pub fn install() -> Result<()> {

//let uefi = unsafe { std::system_table_mut() };

let protocol = Box::new(System76SecurityProtocol {
Run: run,
});
let protocol = Box::new(System76SecurityProtocol { Run: run });
let protocol_ptr = Box::into_raw(protocol);
let mut handle = Handle(0);
Result::from((uefi.BootServices.InstallProtocolInterface)(
&mut handle,
&SYSTEM76_SECURITY_PROTOCOL_GUID,
InterfaceType::Native,
protocol_ptr as usize
protocol_ptr as usize,
))?;

Ok(())
Expand Down
Loading