Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Crawford <[email protected]>
  • Loading branch information
crawfxrd committed Dec 14, 2024
1 parent 9221b51 commit 46a8376
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 298 deletions.
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
2 changes: 1 addition & 1 deletion src/rng.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ptr;
use std::proto::Protocol;
use std::prelude::*;
use std::proto::Protocol;

pub const RNG_PROTOCOL_GUID: Guid = guid!("3152bca5-eade-433d-862e-c01cdc291f44");

Expand Down
58 changes: 29 additions & 29 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,7 +295,7 @@ extern "efiapi" fn run() -> bool {
ResetType::Shutdown,
Status(0),
0,
ptr::null()
ptr::null(),
);
}
}
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

0 comments on commit 46a8376

Please sign in to comment.