Skip to content

Commit 1d22934

Browse files
committed
Apply some suggestions from Clippy
1 parent ab449fc commit 1d22934

File tree

9 files changed

+68
-94
lines changed

9 files changed

+68
-94
lines changed

towboot/src/boot/config_tables.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,17 @@ use uefi::table::cfg::{
1919
pub(super) fn parse_for_multiboot(info_builder: &mut InfoBuilder) {
2020
// first, copy all config table pointers
2121
// TODO: remove this when with_config_table takes a FnMut
22-
let config_tables: Vec<ConfigTableEntry> = with_config_table(|s|
23-
s.to_vec()
24-
);
22+
let config_tables: Vec<ConfigTableEntry> = with_config_table(<[ConfigTableEntry]>::to_vec);
2523
debug!("going through configuration tables...");
2624
for table in config_tables {
2725
match table.guid {
28-
ACPI_GUID => handle_acpi(&table, info_builder),
29-
ACPI2_GUID => handle_acpi(&table, info_builder),
26+
ACPI_GUID | ACPI2_GUID => handle_acpi(&table, info_builder),
3027
DEBUG_IMAGE_INFO_GUID => debug!("ignoring image debug info"),
3128
DXE_SERVICES_GUID => debug!("ignoring dxe services table"),
3229
HAND_OFF_BLOCK_LIST_GUID => debug!("ignoring hand-off block list"),
3330
LZMA_COMPRESS_GUID => debug!("ignoring lzma filesystem"),
34-
MEMORY_STATUS_CODE_RECORD_GUID => debug!("ignoring early memory info"),
35-
MEMORY_TYPE_INFORMATION_GUID => debug!("ignoring early memory info"),
36-
SMBIOS_GUID => handle_smbios(&table, info_builder),
37-
SMBIOS3_GUID => handle_smbios(&table, info_builder),
31+
MEMORY_STATUS_CODE_RECORD_GUID | MEMORY_TYPE_INFORMATION_GUID => debug!("ignoring early memory info"),
32+
SMBIOS_GUID | SMBIOS3_GUID => handle_smbios(&table, info_builder),
3833
guid => debug!("ignoring table {guid}"),
3934
}
4035
}
@@ -43,7 +38,7 @@ pub(super) fn parse_for_multiboot(info_builder: &mut InfoBuilder) {
4338
/// Parse the ACPI RSDP and create the Multiboot struct for it.
4439
fn handle_acpi(table: &ConfigTableEntry, info_builder: &mut InfoBuilder) {
4540
debug!("handling ACPI RSDP");
46-
let rsdp = unsafe { *(table.address as *const Rsdp) };
41+
let rsdp: Rsdp = unsafe { *(table.address.cast()) };
4742
if rsdp.validate().is_err() {
4843
warn!("the RSDP is invalid");
4944
return;
@@ -90,9 +85,7 @@ fn handle_acpi(table: &ConfigTableEntry, info_builder: &mut InfoBuilder) {
9085
/// Caveat: The Structure Table pointer in the Entry Point is not adjusted.
9186
fn handle_smbios(table: &ConfigTableEntry, info_builder: &mut InfoBuilder) {
9287
debug!("handling SMBIOS table");
93-
let bigger_slice = unsafe { slice::from_raw_parts(
94-
table.address as *const u8, 128,
95-
) };
88+
let bigger_slice = unsafe { slice::from_raw_parts(table.address.cast(), 128) };
9689
match EntryPoint::search(bigger_slice) {
9790
Ok(entry_point) => {
9891
let version = entry_point.to_version();

towboot/src/boot/elf.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl OurElfLoader {
2828
/// Create a new instance.
2929
///
3030
/// The parameter is the virtual address of the entry point.
31-
pub(super) fn new(entry_point: u64) -> Self {
32-
OurElfLoader {
31+
pub(super) const fn new(entry_point: u64) -> Self {
32+
Self {
3333
allocations: BTreeMap::new(),
3434
virtual_entry_point: entry_point,
3535
physical_entry_point: None,
@@ -46,7 +46,7 @@ impl OurElfLoader {
4646
for program_header in &binary.program_headers {
4747
if program_header.p_type == elf::program_header::PT_LOAD {
4848
self.allocate(program_header, quirks)?;
49-
self.load(program_header.p_vaddr, &data[program_header.file_range()])?;
49+
self.load(program_header.p_vaddr, &data[program_header.file_range()]);
5050
}
5151
}
5252
Ok(())
@@ -57,12 +57,10 @@ impl OurElfLoader {
5757
/// We should have found it in `allocate`,
5858
/// else fall back to the virtual one and hope for the best.
5959
pub(super) fn entry_point(&self) -> usize {
60-
if let Some(a) = self.physical_entry_point {
61-
a
62-
} else {
60+
self.physical_entry_point.unwrap_or_else(|| {
6361
warn!("didn't find the entry point while loading sections, assuming virtual = physical");
6462
self.virtual_entry_point.try_into().unwrap()
65-
}
63+
})
6664
}
6765

6866
/// Allocate memory for a segment.
@@ -103,7 +101,7 @@ impl OurElfLoader {
103101
}
104102

105103
/// Load a segment.
106-
fn load(&mut self, base: u64, region: &[u8]) -> Result<(), &'static str> {
104+
fn load(&mut self, base: u64, region: &[u8]) {
107105
// check whether we actually allocated this
108106
match self.allocations.get_mut(&base) {
109107
None => panic!("we didn't allocate {base:#x}, but tried to write to it o.O"),
@@ -117,15 +115,14 @@ impl OurElfLoader {
117115
"load {} bytes into {:#x} (at {:#x})", region.len(), base, ptr as usize
118116
);
119117
alloc.as_mut_slice()[0..region.len()].clone_from_slice(region);
120-
Ok(())
121118
},
122119
}
123120
}
124121
}
125122

126123
impl From<OurElfLoader> for Vec<Allocation> {
127124
/// Gets the allocated memory.
128-
fn from(loader: OurElfLoader) -> Vec<Allocation> {
125+
fn from(loader: OurElfLoader) -> Self {
129126
// using .values() would just borrow the values from the hash map
130127
loader.allocations.into_values().collect()
131128
}

towboot/src/boot/mod.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ impl LoadedKernel {
5959
/// Load a kernel from a vector.
6060
/// This requires that the Multiboot header has already been parsed.
6161
fn new(
62-
kernel_vec: Vec<u8>, header: &Header, quirks: &BTreeSet<Quirk>,
62+
kernel_bytes: &[u8], header: &Header, quirks: &BTreeSet<Quirk>,
6363
) -> Result<Self, Status> {
6464
if header.get_load_addresses().is_some() && !quirks.contains(&Quirk::ForceElf) {
65-
LoadedKernel::new_multiboot(kernel_vec, header, quirks)
65+
Self::new_multiboot(kernel_bytes, header, quirks)
6666
} else {
67-
LoadedKernel::new_elf(header, kernel_vec, quirks)
67+
Self::new_elf(header, kernel_bytes, quirks)
6868
}
6969
}
7070

7171
/// Load a kernel which has its addresses specified inside the Multiboot header.
7272
fn new_multiboot(
73-
kernel_vec: Vec<u8>, header: &Header, quirks: &BTreeSet<Quirk>,
73+
kernel_bytes: &[u8], header: &Header, quirks: &BTreeSet<Quirk>,
7474
) -> Result<Self, Status> {
7575
// TODO: Add support for AOut symbols? Do we really know this binary is AOut at this point?
7676
let addresses = header.get_load_addresses().unwrap();
@@ -82,7 +82,7 @@ impl LoadedKernel {
8282
let load_offset = addresses.compute_load_offset(header.header_start());
8383
// allocate
8484
let kernel_length: usize = addresses.compute_kernel_length(
85-
kernel_vec.len().try_into().unwrap()
85+
kernel_bytes.len().try_into().unwrap()
8686
).try_into().unwrap();
8787
let mut allocation = Allocation::new_at(
8888
addresses.load_addr().try_into().unwrap(),
@@ -92,14 +92,12 @@ impl LoadedKernel {
9292
let kernel_buf = allocation.as_mut_slice();
9393
// copy from beginning of text to end of data segment and fill the rest with zeroes
9494
kernel_buf.iter_mut().zip(
95-
kernel_vec.iter()
95+
kernel_bytes.iter()
9696
.skip(load_offset.try_into().unwrap())
9797
.take(kernel_length)
9898
.chain(core::iter::repeat(&0))
9999
)
100100
.for_each(|(dst,src)| *dst = *src);
101-
// drop the old vector
102-
core::mem::drop(kernel_vec);
103101

104102
let entry_point = get_kernel_uefi_entry(header, quirks)
105103
.or(header.get_entry_address().map(
@@ -119,20 +117,20 @@ impl LoadedKernel {
119117

120118
/// Load a kernel which uses ELF semantics.
121119
fn new_elf(
122-
header: &Header, kernel_vec: Vec<u8>, quirks: &BTreeSet<Quirk>,
120+
header: &Header, kernel_bytes: &[u8], quirks: &BTreeSet<Quirk>,
123121
) -> Result<Self, Status> {
124-
let mut binary = Elf::parse(kernel_vec.as_slice()).map_err(|msg| {
122+
let mut binary = Elf::parse(kernel_bytes).map_err(|msg| {
125123
error!("failed to parse ELF structure of kernel: {msg}");
126124
Status::LOAD_ERROR
127125
})?;
128126
let mut loader = OurElfLoader::new(binary.entry);
129127
loader
130-
.load_elf(&binary, kernel_vec.as_slice(), quirks)
128+
.load_elf(&binary, kernel_bytes, quirks)
131129
.map_err(|msg| {
132130
error!("failed to load kernel: {msg}");
133131
Status::LOAD_ERROR
134132
})?;
135-
let symbols = Some(elf::symbols(header, &mut binary, kernel_vec.as_slice()));
133+
let symbols = Some(elf::symbols(header, &mut binary, kernel_bytes));
136134
let entry_point = get_kernel_uefi_entry(header, quirks)
137135
.or(header.get_entry_address().map(
138136
|e| EntryPoint::Multiboot(e as usize)
@@ -206,7 +204,7 @@ fn get_kernel_uefi_entry(
206204

207205
/// Prepare information for the kernel.
208206
fn prepare_multiboot_information(
209-
entry: &Entry, header: Header, load_base_address: Option<u32>,
207+
entry: &Entry, header: &Header, load_base_address: Option<u32>,
210208
modules: &[Allocation], symbols: Option<Symbols>,
211209
graphics_output: Option<ScopedProtocol<GraphicsOutput>>,
212210
boot_services_exited: bool,
@@ -316,14 +314,15 @@ impl<'a> PreparedEntry<'a> {
316314
/// This is non-destructive and will always return.
317315
pub(crate) fn new(
318316
entry: &'a Entry, image_fs_handle: Handle,
319-
) -> Result<PreparedEntry<'a>, Status> {
317+
) -> Result<Self, Status> {
320318
let kernel_vec: Vec<u8> = File::open(&entry.image, image_fs_handle)?.try_into()?;
321319
let header = Header::from_slice(kernel_vec.as_slice()).ok_or_else(|| {
322320
error!("invalid Multiboot header");
323321
Status::LOAD_ERROR
324322
})?;
325323
debug!("loaded kernel {:?} to {:?}", header, kernel_vec.as_ptr());
326-
let mut loaded_kernel = LoadedKernel::new(kernel_vec, &header, &entry.quirks)?;
324+
let mut loaded_kernel = LoadedKernel::new(&kernel_vec, &header, &entry.quirks)?;
325+
core::mem::drop(kernel_vec);
327326
info!("kernel is loaded and bootable");
328327

329328
// Load all modules, fail completely if one fails to load.
@@ -340,7 +339,7 @@ impl<'a> PreparedEntry<'a> {
340339
let graphics_output = video::setup_video(&header, &entry.quirks);
341340

342341
let multiboot_information = prepare_multiboot_information(
343-
entry, header, loaded_kernel.load_base_address, &modules_vec,
342+
entry, &header, loaded_kernel.load_base_address, &modules_vec,
344343
loaded_kernel.symbols_struct(), graphics_output,
345344
!entry.quirks.contains(&Quirk::DontExitBootServices),
346345
);
@@ -414,7 +413,7 @@ impl<'a> PreparedEntry<'a> {
414413
// The kernel is going to need the section headers and symbols.
415414
core::mem::forget(self.loaded_kernel.symbols);
416415

417-
self.loaded_kernel.entry_point.jump(signature, info)
416+
self.loaded_kernel.entry_point.jump(signature, &info)
418417
}
419418
}
420419

@@ -435,19 +434,19 @@ enum EntryPoint {
435434
impl EntryPoint {
436435
/// Jump to the loaded kernel.
437436
/// This requires everything else to be ready and won't return.
438-
fn jump(self, signature: u32, info: Vec<u8>) -> ! {
437+
fn jump(self, signature: u32, info: &[u8]) -> ! {
439438
if let Self::Uefi(entry_address) = self {
440-
self.jump_uefi(entry_address, signature, info)
439+
Self::jump_uefi(entry_address, signature, info)
441440
} else if let Self::Multiboot(entry_address) = self {
442-
self.jump_multiboot(entry_address, signature, info)
441+
Self::jump_multiboot(entry_address, signature, info)
443442
} else {
444443
panic!("invalid entry point")
445444
}
446445
}
447446

448447
/// Jump to the loaded kernel, UEFI-style, eg. just passing the information.
449448
/// This requires everything else to be ready and won't return.
450-
fn jump_uefi(self, entry_address: usize, signature: u32, info: Vec<u8>) -> ! {
449+
fn jump_uefi(entry_address: usize, signature: u32, info: &[u8]) -> ! {
451450
debug!("jumping to 0x{entry_address:x}");
452451
unsafe {
453452
// TODO: The spec mentions 32 bit registers, even on 64 bit.
@@ -459,15 +458,15 @@ impl EntryPoint {
459458
"jmp {}",
460459
in(reg) entry_address,
461460
in("eax") signature,
462-
in("ecx") &info.as_slice()[0],
461+
in("ecx") &raw const info[0],
463462
options(noreturn),
464463
);
465464
}
466465
}
467466

468467
/// i686-specific part of the Multiboot machine state.
469468
#[cfg(target_arch = "x86")]
470-
fn jump_multiboot(self, entry_address: usize, signature: u32, info: Vec<u8>) -> ! {
469+
fn jump_multiboot(entry_address: usize, signature: u32, info: &[u8]) -> ! {
471470
debug!("preparing machine state and jumping to 0x{entry_address:x}");
472471

473472
// 3.2 Machine state says:
@@ -497,7 +496,7 @@ impl EntryPoint {
497496
sym Self::jump_multiboot_common,
498497
// LLVM needs some registers (https://github.com/rust-lang/rust/blob/1.67.1/compiler/rustc_target/src/asm/x86.rs#L206)
499498
in("eax") signature,
500-
in("ecx") &info.as_slice()[0],
499+
in("ecx") &raw const info[0],
501500
in("edi") entry_address,
502501
options(noreturn),
503502
);
@@ -506,7 +505,7 @@ impl EntryPoint {
506505

507506
/// x86_64-specific part of the Multiboot machine state.
508507
#[cfg(target_arch = "x86_64")]
509-
fn jump_multiboot(self, entry_address: usize, signature: u32, info: Vec<u8>) -> ! {
508+
fn jump_multiboot(entry_address: usize, signature: u32, info: &[u8]) -> ! {
510509
debug!("preparing machine state and jumping to 0x{entry_address:x}");
511510

512511
// 3.2 Machine state says:
@@ -576,7 +575,7 @@ impl EntryPoint {
576575
sym Self::jump_multiboot_common,
577576
// LLVM needs some registers (https://github.com/rust-lang/rust/blob/1.67.1/compiler/rustc_target/src/asm/x86.rs#L206)
578577
in("eax") signature,
579-
in("ecx") &info.as_slice()[0],
578+
in("ecx") &raw const info[0],
580579
in("edi") entry_address,
581580
options(noreturn),
582581
);

towboot/src/mem.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Allocation {
6464
/// You can move the allocated memory later to the correct address by calling
6565
/// [`move_to_where_it_should_be`], but please keep its safety implications in mind.
6666
/// This only works for our code and data by default, but this can be
67-
/// overridden with the ForceOverwrite quirk.
67+
/// overridden with the `ForceOverwrite` quirk.
6868
///
6969
/// [`move_to_where_it_should_be`]: struct.Allocation.html#method.move_to_where_it_should_be
7070
pub(crate) fn new_at(
@@ -83,7 +83,7 @@ impl Allocation {
8383
MemoryType::LOADER_DATA,
8484
count_pages,
8585
) {
86-
Ok(ptr) => Ok(Allocation {
86+
Ok(ptr) => Ok(Self {
8787
ptr,
8888
offset: page_offset,
8989
len: size,
@@ -155,13 +155,13 @@ impl Allocation {
155155
get_memory_map();
156156
Status::LOAD_ERROR
157157
})?;
158-
Ok(Allocation {
158+
Ok(Self {
159159
ptr, offset: 0, len: size, pages: count_pages, should_be_at: None,
160160
})
161161
}
162162

163163
/// Return a slice that references the associated memory.
164-
pub(crate) fn as_mut_slice(&mut self) -> &mut [u8] {
164+
pub(crate) const fn as_mut_slice(&mut self) -> &mut [u8] {
165165
unsafe { core::slice::from_raw_parts_mut(
166166
self.ptr.as_ptr().add(self.offset),
167167
self.len,
@@ -300,9 +300,8 @@ pub(super) fn prepare_information(
300300
.iter()
301301
// find the area starting at 1MB and get its length
302302
.find(|e| e.base_address() == 1024 * 1024)
303-
.map(|e| e.length())
304303
// if there is none, it's 0KB
305-
.unwrap_or(0) / 1024;
304+
.map_or(0, |e| e.length()) / 1024;
306305

307306
// When updating either uefi.rs or multiboot2, make sure that the types
308307
// still match.

towboot/src/menu.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloc::string::String;
55

66
use uefi::prelude::*;
77
use uefi::boot::{EventType, TimerTrigger, Tpl, create_event, set_timer, wait_for_event};
8-
use uefi::proto::console::text::{Key, ScanCode};
8+
use uefi::proto::console::text::{Input, Key, ScanCode};
99
use uefi::system::{with_stdin, with_stdout};
1010

1111
use log::{error, warn};
@@ -27,7 +27,7 @@ pub fn choose(config: &Config) -> &Entry {
2727
warn!("default entry is missing, trying the first one");
2828
config.entries.values().next().expect("no entries")
2929
});
30-
if let Some(0) = config.timeout {
30+
if config.timeout == Some(0) {
3131
return default_entry
3232
}
3333
match display_menu(config, default_entry) {
@@ -68,7 +68,7 @@ fn display_menu<'a>(
6868
]
6969
).discard_errdata()? {
7070
// key
71-
0 => match with_stdin(|stdin| stdin.read_key())? {
71+
0 => match with_stdin(Input::read_key)? {
7272
Some(Key::Special(ScanCode::ESCAPE)) => break,
7373
_ => (),
7474
},
@@ -108,9 +108,7 @@ fn select_entry(entries: &BTreeMap<String, Entry>) -> uefi::Result<&Entry> {
108108
// this is safe because we're never calling close_event
109109
&mut [unsafe { key_event.unsafe_clone() }]
110110
).discard_errdata()?;
111-
if let Some(Key::Printable(c)) = with_stdin(
112-
|stdin| stdin.read_key()
113-
)? {
111+
if let Some(Key::Printable(c)) = with_stdin(Input::read_key)? {
114112
match c.into() {
115113
'\r' => break, // enter
116114
'\u{8}' => {value.pop();}, // backspace

towboot_config/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pub struct Config {
1919

2020
impl Config {
2121
/// Determine which files are referenced in the configuration.
22-
pub fn needed_files(self: &mut Config) -> Vec<&mut String> {
22+
pub fn needed_files(&mut self) -> Vec<&mut String> {
2323
let mut files = Vec::new();
24-
for (_name, entry) in self.entries.iter_mut() {
24+
for entry in self.entries.values_mut() {
2525
files.push(&mut entry.image);
2626
for module in &mut entry.modules {
2727
files.push(&mut module.image);

0 commit comments

Comments
 (0)