From e2fb4a494836a2fd6c46e5dbf64d8d29b7cb8c99 Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Tue, 10 Dec 2024 19:42:11 -0700 Subject: [PATCH] Format code Signed-off-by: Tim Crawford --- src/display.rs | 4 +- src/fde.rs | 457 ++++++++++++++++++++++++++--------------------- src/hii.rs | 4 +- src/image/bmp.rs | 12 +- src/image/mod.rs | 26 ++- src/key.rs | 8 +- src/main.rs | 2 +- src/rng.rs | 9 +- src/security.rs | 65 +++---- src/ui.rs | 122 +++++++------ 10 files changed, 409 insertions(+), 300 deletions(-) diff --git a/src/display.rs b/src/display.rs index 49ef000..3bd1933 100644 --- a/src/display.rs +++ b/src/display.rs @@ -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); @@ -51,7 +51,7 @@ impl Display { y as usize, w as usize, h as usize, - 0 + 0, ); status.is_success() } diff --git a/src/fde.rs b/src/fde.rs index b9c5c9b..1b07380 100644 --- a/src/fde.rs +++ b/src/fde.rs @@ -1,18 +1,17 @@ // SPDX-License-Identifier: GPL-3.0-only +use core::{char, cmp, mem, ptr, slice}; use orbclient::{Color, Renderer}; use orbfont::Text; -use core::{char, cmp, mem, ptr, slice}; +use std::ffi; use std::prelude::*; use std::proto::Protocol; -use std::ffi; -use std::uefi::hii::{AnimationId, ImageId, StringId}; use std::uefi::hii::database::HiiHandle; use std::uefi::hii::ifr::{ - HiiValue, - IfrOpCode, IfrOpHeader, IfrStatementHeader, IfrTypeValueEnum, - IfrAction, IfrCheckbox, IfrNumeric, IfrOneOf, IfrOneOfOption, IfrOrderedList, IfrRef, IfrSubtitle + HiiValue, IfrAction, IfrCheckbox, IfrNumeric, IfrOneOf, IfrOneOfOption, IfrOpCode, IfrOpHeader, + IfrOrderedList, IfrRef, IfrStatementHeader, IfrSubtitle, IfrTypeValueEnum, }; +use std::uefi::hii::{AnimationId, ImageId, StringId}; use std::uefi::text::TextInputKey; use crate::display::{Display, Output}; @@ -34,8 +33,8 @@ pub struct HiiStringProtocol { StringSize: &mut usize, StringFontInfo: usize, // TODO ) -> Status, - pub SetString: extern "efiapi" fn(), //TODO - pub GetLanguages: extern "efiapi" fn(), //TODO + pub SetString: extern "efiapi" fn(), //TODO + pub GetLanguages: extern "efiapi" fn(), //TODO pub GetSecondaryLanguages: extern "efiapi" fn(), //TODO } @@ -50,7 +49,7 @@ impl HiiStringProtocol { StringId, data.as_mut_ptr(), &mut len, - 0 + 0, ))?; data.truncate(len); @@ -138,7 +137,7 @@ pub trait ListEntryObject { } macro_rules! list_entry { - ($t:ident, $l:tt) => ( + ($t:ident, $l:tt) => { impl ListEntryObject<$t> for ListEntry<$t> { unsafe fn object(&self) -> &$t { self.object_at(offset_of!($t, $l)) @@ -148,7 +147,7 @@ macro_rules! list_entry { self.object_at_mut(offset_of!($t, $l)) } } - ); + }; } pub struct ListEntryIter<'a, T> { @@ -156,7 +155,10 @@ pub struct ListEntryIter<'a, T> { current: Option<&'a ListEntry>, } -impl<'a, T> Iterator for ListEntryIter<'a, T> where ListEntry: ListEntryObject { +impl<'a, T> Iterator for ListEntryIter<'a, T> +where + ListEntry: ListEntryObject, +{ type Item = &'a T; fn next(&mut self) -> Option { let current = self.current.take()?; @@ -211,18 +213,15 @@ pub struct StatementErrorInfo { pub TimeOut: u8, } -pub type ValidateQuestion = extern "efiapi" fn ( +pub type ValidateQuestion = extern "efiapi" fn( Form: &Form, Statement: &Statement, Value: &HiiValue, ErrorInfo: &mut StatementErrorInfo, ) -> u32; -pub type PasswordCheck = extern "efiapi" fn( - Form: &Form, - Statement: &Statement, - PasswordString: *const u16 -) -> Status; +pub type PasswordCheck = + extern "efiapi" fn(Form: &Form, Statement: &Statement, PasswordString: *const u16) -> Status; #[repr(C)] pub struct Statement { @@ -342,7 +341,7 @@ enum EventType { Keyboard, } -fn wait_for_events(form: &Form) -> Result { +fn wait_for_events(form: &Form) -> Result { let uefi = std::system_table(); let mut index = 0; let mut events = vec![uefi.ConsoleIn.WaitForKey]; @@ -351,7 +350,11 @@ fn wait_for_events(form: &Form) -> Result { events.push(form.FormRefreshEvent); } - Result::from((uefi.BootServices.WaitForEvent)(events.len(), events.as_mut_ptr(), &mut index))?; + Result::from((uefi.BootServices.WaitForEvent)( + events.len(), + events.as_mut_ptr(), + &mut index, + ))?; if index == 0 { Ok(EventType::Keyboard) @@ -364,9 +367,8 @@ fn wait_for_events(form: &Form) -> Result { fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { let hii_string = <&'static mut HiiStringProtocol>::one()?; - let string = |string_id: StringId| -> Result { - hii_string.string(form.HiiHandle, string_id) - }; + let string = + |string_id: StringId| -> Result { hii_string.string(form.HiiHandle, string_id) }; let display: &mut Display = unsafe { if DISPLAY.is_null() { @@ -390,7 +392,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { let margin_lr = 8 * scale; let margin_tb = 4 * scale; - let title_font_size = (20 * scale) as f32; + let title_font_size = (20 * scale) as f32; let font_size = (16 * scale) as f32; // (display_h as f32) / 26.0 let help_font_size = (12 * scale) as f32; // } Style @@ -414,10 +416,10 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { for option in statement.OptionListHead.iter() { let option_ptr = option as *const _; if let Some(op) = option.OptionOpCode() { - let value = unsafe { - op.Value.to_enum(op.Kind) - }; - let prompt = ui.font.render(&string(op.Option).unwrap_or_default(), font_size); + let value = unsafe { op.Value.to_enum(op.Kind) }; + let prompt = ui + .font + .render(&string(op.Option).unwrap_or_default(), font_size); options.push(ElementOption { option_ptr, prompt, @@ -426,111 +428,130 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { } } - let add_element = |header: IfrStatementHeader, selectable: bool, editable: bool, list: bool| { - let value = unsafe { - statement.CurrentValue.Value.to_enum(statement.CurrentValue.Kind) - }; - let buffer_opt = if statement.CurrentValue.Buffer.is_null() { - None - } else { - let buffer = unsafe { - slice::from_raw_parts_mut( - statement.CurrentValue.Buffer, - statement.CurrentValue.BufferLen as usize - ) + let add_element = + |header: IfrStatementHeader, selectable: bool, editable: bool, list: bool| { + let value = unsafe { + statement + .CurrentValue + .Value + .to_enum(statement.CurrentValue.Kind) }; - // Order list according to buffer - if list { - let mut offset = 0; - for i in 0..options.len() { - for j in i..options.len() { - macro_rules! check_option { - ($x:ident) => ({ - let next_offset = offset + mem::size_of_val(&$x); - if next_offset <= buffer.len() { - let mut x_copy = $x; - unsafe { - ptr::copy( - buffer.as_ptr().add(offset) as *const _, - &mut x_copy, - 1 - ); - }; - if $x == x_copy { - offset = next_offset; - true + let buffer_opt = if statement.CurrentValue.Buffer.is_null() { + None + } else { + let buffer = unsafe { + slice::from_raw_parts_mut( + statement.CurrentValue.Buffer, + statement.CurrentValue.BufferLen as usize, + ) + }; + // Order list according to buffer + if list { + let mut offset = 0; + for i in 0..options.len() { + for j in i..options.len() { + macro_rules! check_option { + ($x:ident) => {{ + let next_offset = offset + mem::size_of_val(&$x); + if next_offset <= buffer.len() { + let mut x_copy = $x; + unsafe { + ptr::copy( + buffer.as_ptr().add(offset) as *const _, + &mut x_copy, + 1, + ); + }; + if $x == x_copy { + offset = next_offset; + true + } else { + false + } } else { false } - } else { - false + }}; + } + let matches = match options[j].value { + IfrTypeValueEnum::U8(u8) => check_option!(u8), + IfrTypeValueEnum::U16(u16) => check_option!(u16), + IfrTypeValueEnum::U32(u32) => check_option!(u32), + IfrTypeValueEnum::U64(u64) => check_option!(u64), + _ => false, + }; + if matches { + if i != j { + options.swap(i, j); } - }); - } - let matches = match options[j].value { - IfrTypeValueEnum::U8(u8) => check_option!(u8), - IfrTypeValueEnum::U16(u16) => check_option!(u16), - IfrTypeValueEnum::U32(u32) => check_option!(u32), - IfrTypeValueEnum::U64(u64) => check_option!(u64), - _ => false, - }; - if matches { - if i != j { - options.swap(i, j); + break; } - break; } } } + Some(buffer) + }; + if statement_ptr == form.HighLightedStatement || (selected == !0 && selectable) + { + selected = elements.len(); } - Some(buffer) + elements.push(Element { + statement_ptr, + prompt: string(header.Prompt).unwrap_or_default(), + help: string(header.Help).unwrap_or_default(), + value, + options, + selectable, + editable, + list, + list_i: 0, + buffer_opt, + }); }; - if statement_ptr == form.HighLightedStatement || (selected == !0 && selectable) { - selected = elements.len(); - } - elements.push(Element { - statement_ptr, - prompt: string(header.Prompt).unwrap_or_default(), - help: string(header.Help).unwrap_or_default(), - value, - options, - selectable, - editable, - list, - list_i: 0, - buffer_opt, - }); - }; if let Some(op) = statement.OpCode() { macro_rules! cast { - ($type:ty) => ({ + ($type:ty) => {{ op.cast::<$type>() - }); + }}; } match op.OpCode { - IfrOpCode::Action => if let Some(action) = unsafe { cast!(IfrAction) } { - add_element(action.QuestionHeader.Header, true, false, false); - }, - IfrOpCode::Checkbox => if let Some(checkbox) = unsafe { cast!(IfrCheckbox) } { - add_element(checkbox.Question.Header, true, true, false); - }, - IfrOpCode::Numeric => if let Some(numeric) = unsafe { cast!(IfrNumeric) } { - add_element(numeric.Question.Header, true, true, false); - }, - IfrOpCode::OneOf => if let Some(one_of) = unsafe { cast!(IfrOneOf) } { - add_element(one_of.Question.Header, true, true, false); - }, - IfrOpCode::OrderedList => if let Some(ordered_list) = unsafe { cast!(IfrOrderedList) } { - add_element(ordered_list.Question.Header, true, true, true); - }, - IfrOpCode::Ref => if let Some(ref_) = unsafe { cast!(IfrRef) } { - add_element(ref_.Question.Header, true, false, false); - }, - IfrOpCode::Subtitle => if let Some(subtitle) = unsafe { cast!(IfrSubtitle) } { - add_element(subtitle.Statement, false, false, false); - }, - _ => () + IfrOpCode::Action => { + if let Some(action) = unsafe { cast!(IfrAction) } { + add_element(action.QuestionHeader.Header, true, false, false); + } + } + IfrOpCode::Checkbox => { + if let Some(checkbox) = unsafe { cast!(IfrCheckbox) } { + add_element(checkbox.Question.Header, true, true, false); + } + } + IfrOpCode::Numeric => { + if let Some(numeric) = unsafe { cast!(IfrNumeric) } { + add_element(numeric.Question.Header, true, true, false); + } + } + IfrOpCode::OneOf => { + if let Some(one_of) = unsafe { cast!(IfrOneOf) } { + add_element(one_of.Question.Header, true, true, false); + } + } + IfrOpCode::OrderedList => { + if let Some(ordered_list) = unsafe { cast!(IfrOrderedList) } { + add_element(ordered_list.Question.Header, true, true, true); + } + } + IfrOpCode::Ref => { + if let Some(ref_) = unsafe { cast!(IfrRef) } { + add_element(ref_.Question.Header, true, false, false); + } + } + IfrOpCode::Subtitle => { + if let Some(subtitle) = unsafe { cast!(IfrSubtitle) } { + add_element(subtitle.Statement, false, false, false); + } + } + _ => (), } } } @@ -540,14 +561,21 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { 'display: loop { display.set(ui.background_color); - let draw_value_box = |display: &mut Display, x: i32, y: i32, value: &IfrTypeValueEnum, highlighted: bool| -> i32 { + let draw_value_box = |display: &mut Display, + x: i32, + y: i32, + value: &IfrTypeValueEnum, + highlighted: bool| + -> i32 { //TODO: Do not format in drawing loop let value_string = match value { IfrTypeValueEnum::U8(value) => format!("{value}"), IfrTypeValueEnum::U16(value) => format!("{value}"), IfrTypeValueEnum::U32(value) => format!("{value}"), IfrTypeValueEnum::U64(value) => format!("{value}"), - IfrTypeValueEnum::Bool(value) => return ui.draw_check_box(display, x, y, *value), + IfrTypeValueEnum::Bool(value) => { + return ui.draw_check_box(display, x, y, *value) + } other => format!("{other:?}"), }; @@ -557,37 +585,46 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { rendered.height() as i32 }; - let draw_options_box = |display: &mut Display, x: i32, mut y: i32, element: &Element| { - let mut w = 0; - for option in element.options.iter() { - w = cmp::max(w, option.prompt.width()); - } + let draw_options_box = + |display: &mut Display, x: i32, mut y: i32, element: &Element| { + let mut w = 0; + for option in element.options.iter() { + w = cmp::max(w, option.prompt.width()); + } - let start_y = y; - for (i, option) in element.options.iter().enumerate() { - let highlighted = i == element.list_i; - if highlighted && editing { - ui.draw_pretty_box(display, x, y, w, option.prompt.height(), true); + let start_y = y; + for (i, option) in element.options.iter().enumerate() { + let highlighted = i == element.list_i; + if highlighted && editing { + ui.draw_pretty_box(display, x, y, w, option.prompt.height(), true); + } + let text_color = if highlighted && editing { + ui.highlight_text_color + } else { + ui.text_color + }; + option.prompt.draw(display, x, y, text_color); + y += option.prompt.height() as i32 + margin_tb; + } + if y > start_y { + ui.draw_pretty_box( + display, + x, + start_y, + w, + (y - start_y - margin_tb) as u32, + false, + ); } - let text_color = if highlighted && editing { - ui.highlight_text_color - } else { - ui.text_color - }; - option.prompt.draw(display, x, y, text_color); - y += option.prompt.height() as i32 + margin_tb; - } - if y > start_y { - ui.draw_pretty_box(display, x, start_y, w, (y - start_y - margin_tb) as u32, false); - } - y - }; + y + }; let mut y = margin_tb; let mut bottom_y = display_h as i32; - let (editing_list, editing_value) = elements.get(selected) + let (editing_list, editing_value) = elements + .get(selected) .map(|e| (e.list, e.options.is_empty())) .unwrap_or((false, false)); @@ -600,13 +637,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { y += rendered.height() as i32 + margin_tb; } - display.rect( - 0, - y, - display_w, - 1, - Color::rgb(0xac, 0xac, 0xac) - ); + display.rect(0, y, display_w, 1, Color::rgb(0xac, 0xac, 0xac)); y += margin_tb * 2; // Draw footer @@ -618,13 +649,9 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { 0 => { bottom_y -= rendered.height() as i32 + margin_tb; (display_w as i32) * 2 / 3 + margin_lr - }, - 1 => { - (display_w as i32) / 3 + margin_lr - }, - _ => { - margin_lr } + 1 => (display_w as i32) / 3 + margin_lr, + _ => margin_lr, }; ui.draw_text_box(display, x, bottom_y, &rendered, false, false); i += 1; @@ -646,7 +673,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { } if selected == !0 { render_hotkey_help(""); - } else if ! editing || ! editing_value { + } else if !editing || !editing_value { render_hotkey_help("↑↓=Move Highlight"); } @@ -663,13 +690,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { } bottom_y -= margin_tb * 3 / 2; - display.rect( - 0, - bottom_y, - display_w, - 1, - Color::rgb(0xac, 0xac, 0xac) - ); + display.rect(0, bottom_y, display_w, 1, Color::rgb(0xac, 0xac, 0xac)); if let Some(element) = elements.get(selected) { if !element.help.trim().is_empty() { @@ -679,13 +700,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { ui.draw_text_box(display, x, bottom_y, &rendered, false, false); bottom_y -= margin_tb * 3 / 2; - display.rect( - 0, - bottom_y, - display_w, - 1, - Color::rgb(0xac, 0xac, 0xac) - ); + display.rect(0, bottom_y, display_w, 1, Color::rgb(0xac, 0xac, 0xac)); } } } @@ -696,7 +711,14 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { if element_start > 0 { // Draw up arrow to indicate more items above let arrow = ui.font.render("↑", help_font_size); - ui.draw_text_box(display, (display_w - arrow.width()) as i32 - margin_lr, y, &arrow, false, false); + ui.draw_text_box( + display, + (display_w - arrow.width()) as i32 - margin_lr, + y, + &arrow, + false, + false, + ); } for i in element_start..(element_start + max_form_elements) { @@ -706,7 +728,14 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { let mut h = 0; for line in element.prompt.lines() { let rendered = ui.font.render(line, font_size); - ui.draw_text_box(display, margin_lr, y + h, &rendered, highlighted && ! editing, highlighted && ! editing); + ui.draw_text_box( + display, + margin_lr, + y + h, + &rendered, + highlighted && !editing, + highlighted && !editing, + ); h += rendered.height() as i32; } if h == 0 { @@ -717,8 +746,17 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { if element.list { y = draw_options_box(display, x, y, element); y -= h + margin_tb; - } else if let Some(option) = element.options.iter().find(|o| o.value == element.value) { - ui.draw_text_box(display, x, y, &option.prompt, true, highlighted && editing); + } else if let Some(option) = + element.options.iter().find(|o| o.value == element.value) + { + ui.draw_text_box( + display, + x, + y, + &option.prompt, + true, + highlighted && editing, + ); } else if element.editable { draw_value_box(display, x, y, &element.value, highlighted && editing); } @@ -727,10 +765,19 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { } } - if elements.len() > max_form_elements && element_start < elements.len() - max_form_elements { + if elements.len() > max_form_elements + && element_start < elements.len() - max_form_elements + { // Draw down arrow to indicate more items below let arrow = ui.font.render("↓", help_font_size); - ui.draw_text_box(display, (display_w - arrow.width()) as i32 - margin_lr, bottom_y - arrow.height() as i32 - margin_tb * 2, &arrow, false, false); + ui.draw_text_box( + display, + (display_w - arrow.width()) as i32 - margin_lr, + bottom_y - arrow.height() as i32 - margin_tb * 2, + &arrow, + false, + false, + ); } display.sync(); @@ -748,13 +795,15 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { Err(err) => match err { Status::NOT_READY => break 'input, _ => return Err(err), - } + }, }; - if ! editing { + if !editing { for hotkey in form.HotKeyListHead.iter() { let key_data = unsafe { &*hotkey.KeyData }; - if key_data.ScanCode == raw_key.ScanCode && key_data.UnicodeChar == raw_key.UnicodeChar { + if key_data.ScanCode == raw_key.ScanCode + && key_data.UnicodeChar == raw_key.UnicodeChar + { user_input.Action = hotkey.Action; user_input.DefaultId = hotkey.DefaultId; break 'render; @@ -785,19 +834,18 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { ptr::copy( &(*element.statement_ptr).CurrentValue, &mut user_input.InputValue, - 1 + 1, ); } - let (kind, value) = unsafe { - IfrTypeValueEnum::Bool(!b).to_union() - }; + let (kind, value) = + unsafe { IfrTypeValueEnum::Bool(!b).to_union() }; user_input.InputValue.Kind = kind; user_input.InputValue.Value = value; break 'render; } - } else if element.editable && ! editing { + } else if element.editable && !editing { editing = true; } else { user_input.SelectedStatement = element.statement_ptr; @@ -805,7 +853,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { ptr::copy( &(*element.statement_ptr).CurrentValue, &mut user_input.InputValue, - 1 + 1, ); } if editing { @@ -814,19 +862,21 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { if let Some(ref mut buffer) = element.buffer_opt { for option in element.options.iter() { macro_rules! copy_option { - ($x:ident) => ({ - let next_offset = offset + mem::size_of_val(&$x); + ($x:ident) => {{ + let next_offset = + offset + mem::size_of_val(&$x); if next_offset <= buffer.len() { unsafe { ptr::copy( &$x, - buffer.as_mut_ptr().add(offset) as *mut _, - 1 + buffer.as_mut_ptr().add(offset) + as *mut _, + 1, ) } } offset = next_offset; - }); + }}; } match option.value { IfrTypeValueEnum::U8(u8) => copy_option!(u8), @@ -852,7 +902,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { break 'render; } } - }, + } Key::Escape => { if editing { editing = false; @@ -861,7 +911,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { user_input.Action = BROWSER_ACTION_FORM_EXIT; break 'render; } - }, + } Key::Down => { if editing { if let Some(element) = elements.get_mut(selected) { @@ -872,7 +922,10 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { element.list_i = 0; } } else { - let i_opt = element.options.iter().position(|o| o.value == element.value); + let i_opt = element + .options + .iter() + .position(|o| o.value == element.value); if let Some(mut i) = i_opt { if i + 1 < element.options.len() { i += 1; @@ -907,18 +960,21 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { element_start = selected - max_form_elements + 1; } } - }, + } Key::Up => { if editing { if let Some(element) = elements.get_mut(selected) { if element.list { if element.list_i > 0 { element.list_i -= 1; - } else if ! element.options.is_empty() { + } else if !element.options.is_empty() { element.list_i = element.options.len() - 1; } } else { - let i_opt = element.options.iter().position(|o| o.value == element.value); + let i_opt = element + .options + .iter() + .position(|o| o.value == element.value); if let Some(mut i) = i_opt { if i > 0 { i -= 1; @@ -957,7 +1013,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { }; } } - }, + } Key::PageDown => { if editing { if let Some(element) = elements.get_mut(selected) { @@ -967,7 +1023,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { } } } - }, + } Key::PageUp => { if editing { if let Some(element) = elements.get_mut(selected) { @@ -977,7 +1033,7 @@ fn form_display_inner(form: &Form, user_input: &mut UserInput) -> Result<()> { } } } - }, + } _ => (), } } @@ -991,8 +1047,7 @@ extern "efiapi" fn form_display(form: &Form, user_input: &mut UserInput) -> Stat form_display_inner(form, user_input).into() } -extern "efiapi" fn exit_display() { -} +extern "efiapi" fn exit_display() {} extern "efiapi" fn confirm_data_change() -> usize { 0 diff --git a/src/hii.rs b/src/hii.rs index 0848622..cee2fad 100644 --- a/src/hii.rs +++ b/src/hii.rs @@ -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); diff --git a/src/image/bmp.rs b/src/image/bmp.rs index 844dedc..026ca32 100644 --- a/src/image/bmp.rs +++ b/src/image/bmp.rs @@ -17,12 +17,16 @@ pub fn parse(file_data: &[u8]) -> core::result::Result { 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::() + (start..start + len) + .map(|i| get(i) as char) + .collect::() }; if gets(0, 2) == "BM" { @@ -90,7 +94,7 @@ pub fn parse(file_data: &[u8]) -> core::result::Result { // 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()) } } diff --git a/src/image/mod.rs b/src/image/mod.rs index 40d8257..b9b8ac6 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -15,7 +15,7 @@ pub struct ImageRoi<'a> { y: u32, w: u32, h: u32, - image: &'a Image + image: &'a Image, } impl<'a> ImageRoi<'a> { @@ -23,7 +23,10 @@ impl<'a> ImageRoi<'a> { pub fn draw(&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..]); @@ -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 { + pub fn from_data( + width: u32, + height: u32, + data: Box<[Color]>, + ) -> core::result::Result { 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 { @@ -78,7 +92,7 @@ impl Image { y: y1, w: x2 - x1, h: y2 - y1, - image: self + image: self, } } diff --git a/src/key.rs b/src/key.rs index 4f281cd..46436a0 100644 --- a/src/key.rs +++ b/src/key.rs @@ -79,12 +79,16 @@ pub fn raw_key(wait: bool) -> Result { 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))?; diff --git a/src/main.rs b/src/main.rs index ae0edf2..8e3abf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/rng.rs b/src/rng.rs index 15942b4..95bb72a 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -1,8 +1,13 @@ 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( + 0x3152bca5, + 0xeade, + 0x433d, + [0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44], +); pub struct Rng(pub &'static mut RngProtocol); diff --git a/src/security.rs b/src/security.rs index 272c1db..4cc62ab 100644 --- a/src/security.rs +++ b/src/security.rs @@ -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}; @@ -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 @@ -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; } @@ -152,7 +149,14 @@ 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( @@ -160,7 +164,7 @@ fn confirm(display: &mut Display) -> Result<()> { y, font_size as u32 / 2, font_size as u32, - ui.text_color + ui.text_color, ); } y += font_size as i32 + margin_tb; @@ -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), ); } @@ -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 { @@ -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); - }, - _ => {}, + } + _ => {} } } } @@ -278,7 +280,7 @@ extern "efiapi" fn run() -> bool { display.sync(); res - }, + } Err(err) => Err(err), }; @@ -293,7 +295,7 @@ extern "efiapi" fn run() -> bool { ResetType::Shutdown, Status(0), 0, - ptr::null() + ptr::null(), ); } } @@ -301,7 +303,12 @@ extern "efiapi" fn run() -> bool { 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( + 0x764247c4, + 0xa859, + 0x4a6b, + [0xb5, 0x00, 0xed, 0x5d, 0x7a, 0x70, 0x7d, 0xd4], +); pub struct System76SecurityProtocol { #[allow(dead_code)] pub Run: extern "efiapi" fn() -> bool, @@ -312,16 +319,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(()) diff --git a/src/ui.rs b/src/ui.rs index c983c81..3820783 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -100,7 +100,7 @@ impl Ui { let mut last_text_opt = None; let mut i = 0; while i < words.len() { - if ! line.is_empty() { + if !line.is_empty() { line.push(' '); } line.push_str(words[i]); @@ -129,7 +129,15 @@ impl Ui { texts } - pub fn draw_pretty_box(&self, display: &mut Display, x: i32, y: i32, w: u32, h: u32, highlighted: bool) { + pub fn draw_pretty_box( + &self, + display: &mut Display, + x: i32, + y: i32, + w: u32, + h: u32, + highlighted: bool, + ) { let (_display_w, display_h) = (display.width(), display.height()); let scale = if display_h > 1440 { @@ -145,7 +153,7 @@ impl Ui { let padding_tb = 2 * scale; let rect_radius = 4; //TODO: does not scale due to hardcoded checkbox image! - // } Style + // } Style let checkbox = if highlighted { // Center @@ -154,7 +162,7 @@ impl Ui { y - padding_tb + rect_radius, w + padding_lr as u32 * 2, h + (padding_tb - rect_radius) as u32 * 2, - self.highlight_color + self.highlight_color, ); // Top middle @@ -183,7 +191,7 @@ impl Ui { y - padding_tb, w + (padding_lr - rect_radius) as u32 * 2, 2, - self.outline_color + self.outline_color, ); // Bottom middle @@ -192,7 +200,7 @@ impl Ui { y + h as i32 + padding_tb - 2, w + (padding_lr - rect_radius) as u32 * 2, 2, - self.outline_color + self.outline_color, ); // Left middle @@ -201,7 +209,7 @@ impl Ui { y - padding_tb + rect_radius, 2, h + (padding_tb - rect_radius) as u32 * 2, - self.outline_color + self.outline_color, ); // Right middle @@ -210,64 +218,78 @@ impl Ui { y - padding_tb + rect_radius, 2, h + (padding_tb - rect_radius) as u32 * 2, - self.outline_color + self.outline_color, ); self.checkbox_unchecked }; // Top left - checkbox.roi( - 0, - 0, - rect_radius as u32, - rect_radius as u32 - ).draw( - display, - x - padding_lr, - y - padding_tb - ); + checkbox + .roi(0, 0, rect_radius as u32, rect_radius as u32) + .draw(display, x - padding_lr, y - padding_tb); // Top right - checkbox.roi( - checkbox.width() - rect_radius as u32, - 0, - rect_radius as u32, - rect_radius as u32 - ).draw( - display, - x + w as i32 + padding_lr - rect_radius, - y - padding_tb - ); + checkbox + .roi( + checkbox.width() - rect_radius as u32, + 0, + rect_radius as u32, + rect_radius as u32, + ) + .draw( + display, + x + w as i32 + padding_lr - rect_radius, + y - padding_tb, + ); // Bottom left - checkbox.roi( - 0, - checkbox.height() - rect_radius as u32, - rect_radius as u32, - rect_radius as u32 - ).draw( - display, - x - padding_lr, - y + h as i32 + padding_tb - rect_radius - ); + checkbox + .roi( + 0, + checkbox.height() - rect_radius as u32, + rect_radius as u32, + rect_radius as u32, + ) + .draw( + display, + x - padding_lr, + y + h as i32 + padding_tb - rect_radius, + ); // Bottom right - checkbox.roi( - checkbox.width() - rect_radius as u32, - checkbox.height() - rect_radius as u32, - rect_radius as u32, - rect_radius as u32 - ).draw( - display, - x + w as i32 + padding_lr - rect_radius, - y + h as i32 + padding_tb - rect_radius - ); + checkbox + .roi( + checkbox.width() - rect_radius as u32, + checkbox.height() - rect_radius as u32, + rect_radius as u32, + rect_radius as u32, + ) + .draw( + display, + x + w as i32 + padding_lr - rect_radius, + y + h as i32 + padding_tb - rect_radius, + ); } - pub fn draw_text_box(&self, display: &mut Display, x: i32, y: i32, rendered: &Text, pretty_box: bool, highlighted: bool) { + pub fn draw_text_box( + &self, + display: &mut Display, + x: i32, + y: i32, + rendered: &Text, + pretty_box: bool, + highlighted: bool, + ) { if pretty_box { - self.draw_pretty_box(display, x, y, rendered.width(), rendered.height(), highlighted); + self.draw_pretty_box( + display, + x, + y, + rendered.width(), + rendered.height(), + highlighted, + ); } let text_color = if highlighted { self.highlight_text_color