diff --git a/src/lib/device_tree/Cargo.lock b/src/lib/device_tree/Cargo.lock index 18cc0ddac1..8e178efc8c 100644 --- a/src/lib/device_tree/Cargo.lock +++ b/src/lib/device_tree/Cargo.lock @@ -15,46 +15,16 @@ name = "device_tree" version = "0.1.0" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "driver 0.1.0", - "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "model 0.1.0", "num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "wrappers 0.1.0", ] [[package]] -name = "driver" +name = "model" version = "0.1.0" -[[package]] -name = "num" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-complex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "num-rational 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-bigint" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-complex" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "num-derive" version = "0.2.5" @@ -65,35 +35,6 @@ dependencies = [ "syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "num-integer" -version = "0.1.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-iter" -version = "0.1.39" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-rational" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "num-traits" version = "0.2.8" @@ -133,16 +74,17 @@ name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wrappers" +version = "0.1.0" +dependencies = [ + "model 0.1.0", +] + [metadata] "checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" -"checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db" -"checksum num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "57450397855d951f1a41305e54851b1a7b8f5d2e349543a02a2effe25459f718" -"checksum num-complex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "107b9be86cd2481930688277b675b0114578227f034674726605b8a482d8baf8" "checksum num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "eafd0b45c5537c3ba526f79d3e75120036502bebacbb3f3220914067ce39dbf2" -"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" -"checksum num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "76bd5272412d173d6bf9afdf98db8612bbabc9a7a830b7bfc9c188911716132e" -"checksum num-rational 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4e96f040177bb3da242b5b1ecf3f54b5d5af3efbbfb18608977a5d2767b22f10" "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db" diff --git a/src/lib/device_tree/src/lib.rs b/src/lib/device_tree/src/lib.rs index 10ff52db25..e93a35336d 100644 --- a/src/lib/device_tree/src/lib.rs +++ b/src/lib/device_tree/src/lib.rs @@ -113,14 +113,32 @@ impl<'a> FdtReader<'a> { } Ok(FdtReader::<'a> { - _mem_reservation_block: SectionReader::new(drv, header.off_mem_rsvmap as usize, (header.total_size - header.off_dt_struct) as usize), - struct_block: SectionReader::new(drv, header.off_dt_struct as usize, header.size_dt_struct as usize), - strings_block: SectionReader::new(drv, header.off_dt_strings as usize, header.size_dt_strings as usize), + _mem_reservation_block: SectionReader::new( + drv, + header.off_mem_rsvmap as usize, + (header.total_size - header.off_dt_struct) as usize, + ), + struct_block: SectionReader::new( + drv, + header.off_dt_struct as usize, + header.size_dt_struct as usize, + ), + strings_block: SectionReader::new( + drv, + header.off_dt_strings as usize, + header.size_dt_strings as usize, + ), }) } - pub fn walk(&'a self) -> FdtIterator<'a> { - FdtIterator { dt: self, cursor: 0, depth: 0, len_buf: [0; MAX_DEPTH], path_buf: [[0; MAX_NAME_SIZE]; MAX_DEPTH] } + pub fn iter(&'a self) -> FdtIterator<'a> { + FdtIterator { + dt: self, + cursor: 0, + depth: 0, + len_buf: [0; MAX_DEPTH], + path_buf: [[0; MAX_NAME_SIZE]; MAX_DEPTH], + } } } @@ -138,16 +156,25 @@ impl<'a> Iterator for FdtIterator<'a> { fn next(&mut self) -> Option> { loop { - let op = num_traits::cast::FromPrimitive::from_u32(cursor_u32(&self.dt.struct_block, &mut self.cursor).unwrap()); + let op = num_traits::cast::FromPrimitive::from_u32( + cursor_u32(&self.dt.struct_block, &mut self.cursor).unwrap(), + ); match op { Some(Token::BeginNode) => { if self.depth == MAX_DEPTH { panic!("max depth"); } - self.len_buf[self.depth] = cursor_string(&self.dt.struct_block, &mut self.cursor, &mut self.path_buf[self.depth]).unwrap() as usize; + self.len_buf[self.depth] = cursor_string( + &self.dt.struct_block, + &mut self.cursor, + &mut self.path_buf[self.depth], + ) + .unwrap() as usize; self.cursor = align4(self.cursor); self.depth += 1; - return Some(Entry::Node { path: Path { depth: self.depth, len: self.len_buf, buf: self.path_buf } }); + return Some(Entry::Node { + path: Path { depth: self.depth, len: self.len_buf, buf: self.path_buf }, + }); } Some(Token::EndNode) => { self.depth -= 1; @@ -157,11 +184,20 @@ impl<'a> Iterator for FdtIterator<'a> { panic!("max depth"); } let len = cursor_u32(&self.dt.struct_block, &mut self.cursor).unwrap() as usize; - let mut nameoff = cursor_u32(&self.dt.struct_block, &mut self.cursor).unwrap() as usize; - self.len_buf[self.depth] = cursor_string(&self.dt.strings_block, &mut nameoff, &mut self.path_buf[self.depth][..]).unwrap() as usize; + let mut nameoff = + cursor_u32(&self.dt.struct_block, &mut self.cursor).unwrap() as usize; + self.len_buf[self.depth] = cursor_string( + &self.dt.strings_block, + &mut nameoff, + &mut self.path_buf[self.depth][..], + ) + .unwrap() as usize; let value = SectionReader::new(&self.dt.struct_block, self.cursor, len); self.cursor = align4(self.cursor + len); - return Some(Entry::Property::<'a> { path: Path { depth: self.depth + 1, len: self.len_buf, buf: self.path_buf }, value: value }); + return Some(Entry::Property::<'a> { + path: Path { depth: self.depth + 1, len: self.len_buf, buf: self.path_buf }, + value: value, + }); } Some(Token::Nop) => continue, Some(Token::End) => return None, @@ -202,7 +238,9 @@ pub fn infer_type(data: &[u8]) -> Type { } if let Some(i) = data.iter().position(|&c| !is_print(c)) { if i == data.len() - 1 && data[i] == 0 { - return Type::String(unsafe { core::str::from_utf8_unchecked(&data[..data.len() - 1]) }); + return Type::String(unsafe { + core::str::from_utf8_unchecked(&data[..data.len() - 1]) + }); } } if data.len() == 4 { @@ -217,3 +255,20 @@ pub fn infer_type(data: &[u8]) -> Type { fn is_print(c: u8) -> bool { 0x20 <= c && c < 0xff } + +#[cfg(test)] +mod tests { + use super::*; + use wrappers::SliceReader; + + const TEST_DEVICE_TREE: &[u8] = include_bytes!("./test_device_tree"); + + #[test] + fn parsing_works() { + let reader = SliceReader::new(TEST_DEVICE_TREE); + + let parser = FdtReader::new(&reader).expect("create a device tree parser"); + + assert_eq!(parser.iter().count(), 75); + } +} diff --git a/src/lib/device_tree/src/test_device_tree b/src/lib/device_tree/src/test_device_tree new file mode 100644 index 0000000000..9bda96a70b Binary files /dev/null and b/src/lib/device_tree/src/test_device_tree differ diff --git a/src/mainboard/emulation/qemu-armv7/src/main.rs b/src/mainboard/emulation/qemu-armv7/src/main.rs index 12eefe5a8c..8580b91f84 100644 --- a/src/mainboard/emulation/qemu-armv7/src/main.rs +++ b/src/mainboard/emulation/qemu-armv7/src/main.rs @@ -10,9 +10,9 @@ use core::fmt; use device_tree::Entry::{Node, Property}; use model::{Driver, Result}; +use payloads::external::zimage::DTB; use uart; use wrappers::{DoD, SliceReader}; -use payloads::external::zimage::DTB; #[no_mangle] pub extern "C" fn _start() -> ! { @@ -47,16 +47,24 @@ pub fn print_fdt(console: &mut dyn Driver) -> Result<()> { let mut w = print::WriteTo::new(console); let spi = SliceReader::new(DTB); - for entry in device_tree::FdtReader::new(&spi)?.walk() { + for entry in device_tree::FdtReader::new(&spi)?.iter() { match entry { Node { path: p } => { - fmt::write(&mut w, format_args!("{:depth$}{}\r\n", "", p.name(), depth = p.depth() * 2)).unwrap(); + fmt::write( + &mut w, + format_args!("{:depth$}{}\r\n", "", p.name(), depth = p.depth() * 2), + ) + .unwrap(); } Property { path: p, value: v } => { let buf = &mut [0; 1024]; let len = v.pread(buf, 0)?; let val = device_tree::infer_type(&buf[..len]); - fmt::write(&mut w, format_args!("{:depth$}{} = {}\r\n", "", p.name(), val, depth = p.depth() * 2)).unwrap(); + fmt::write( + &mut w, + format_args!("{:depth$}{} = {}\r\n", "", p.name(), val, depth = p.depth() * 2), + ) + .unwrap(); } } } diff --git a/src/mainboard/sifive/hifive/src/main.rs b/src/mainboard/sifive/hifive/src/main.rs index f5f04e4ea7..77b2117c21 100644 --- a/src/mainboard/sifive/hifive/src/main.rs +++ b/src/mainboard/sifive/hifive/src/main.rs @@ -144,7 +144,7 @@ fn test_ddr(addr: *mut u32, size: usize, w: &mut print::WriteTo) -> Result<(), ( // TODO: move out of mainboard pub fn print_fdt(fdt: &mut dyn Driver, w: &mut print::WriteTo) -> Result<(), &'static str> { - for entry in FdtReader::new(fdt)?.walk() { + for entry in FdtReader::new(fdt)?.iter() { match entry { Entry::Node { path: p } => { write!(w, "{:depth$}{}\r\n", "", p.name(), depth = p.depth() * 2).unwrap(); diff --git a/tools/layoutflash/src/main.rs b/tools/layoutflash/src/main.rs index 967496fb10..eb09a663d8 100644 --- a/tools/layoutflash/src/main.rs +++ b/tools/layoutflash/src/main.rs @@ -1,11 +1,11 @@ #![feature(slice_patterns)] use device_tree::{infer_type, Entry, FdtReader, Type, MAX_NAME_SIZE}; use model::Driver; -use wrappers::SliceReader; use std::io; use std::io::{Seek, SeekFrom, Write}; -use std::{env, fs}; use std::process::exit; +use std::{env, fs}; +use wrappers::SliceReader; // TODO: Move this struct to lib so it can be used at runtime. #[derive(Default, Debug)] @@ -33,7 +33,7 @@ fn read_fixed_fdt(path: &str) -> Vec { let driver = SliceReader::new(data.as_slice()); let mut areas = Vec::new(); - for item in FdtReader::new(&driver).unwrap().walk() { + for item in FdtReader::new(&driver).unwrap().iter() { // TODO: We really need a better iterator for this. match item { Entry::Node { path } => { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy