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

add extract-dtb command #5

Merged
merged 1 commit into from
Jan 27, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
Cargo.lock
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ documentation = "https://docs.rs/psi_device_tree"
hashbrown = "0.13"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
clap = { version = "4.4.18", features = ["derive"] }

[features]
string-dedup = [] # Requires std

[[bin]]
name = "extract-dtb"
path = "extract-dtb/main.rs"
67 changes: 67 additions & 0 deletions extract-dtb/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use clap::Parser;
use psi_device_tree::DeviceTree as DT;
use std::fs::{self, File};
use std::io::{Read, Seek, SeekFrom, Write};
use std::os::unix::fs::MetadataExt;

// doodfeed is not a burger
const DTB_MAGIC: u32 = 0xd00d_feed;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Filename
#[arg(short, long)]
filename: String,
#[arg(short, long)]
dest: String,
}

fn main() {
let args = Args::parse();
let filename = args.filename;
let dest = args.dest;

let mut f = File::open(filename).unwrap();
let step = 8;
let size = f.metadata().unwrap().size();

for o in (0..size).step_by(step as usize) {
// read first bytes
f.seek(SeekFrom::Start(o)).unwrap();
let buf = &mut [0u8; 4];
f.read(buf).unwrap();

// is le magic?
if u32::from_be_bytes(*buf) == DTB_MAGIC {
// next 4 bytes plz to get size of device tree
f.read(buf).unwrap();
// size is not little endian
let size = u32::from_be_bytes(*buf) as usize;

f.seek(SeekFrom::Start(o)).unwrap();
// create vec of size filled with zerozero
let mut buf = vec![0; size];
f.read_exact(&mut buf).unwrap();

let dt = DT::load(&buf).unwrap();

// does it exist? if not, rule 34
fs::create_dir_all(&dest).unwrap();

let mut filename = String::from(format!("{o:08x}"));
filename.push_str(".dtb");

let path = std::path::Path::new(&dest).join(filename);

println!("Write dtb to {}", path.display());

let mut output = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(path)
.unwrap();
output.write_all(&buf).unwrap();
}
}
}
16 changes: 4 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ use serde::{Deserialize, Serialize};
pub use error::*;
use util::{align, SliceRead, VecWrite};

#[cfg(not(feature = "string-dedup"))]
mod string_table;
#[cfg(feature = "string-dedup")]
mod advanced_string_table;
#[cfg(not(feature = "string-dedup"))]
mod string_table;

#[cfg(not(feature = "string-dedup"))]
use string_table::StringTable;
Expand Down Expand Up @@ -247,11 +247,7 @@ impl DeviceTree {
}

impl Node {
fn load(
buffer: &[u8],
start: usize,
off_dt_strings: usize,
) -> Result<(usize, Node)> {
fn load(buffer: &[u8], start: usize, off_dt_strings: usize) -> Result<(usize, Node)> {
// check for DT_BEGIN_NODE
if buffer.read_be_u32(start)? != OF_DT_BEGIN_NODE {
return Err(Error::ParseError(start));
Expand Down Expand Up @@ -370,11 +366,7 @@ impl Node {
Ok(raw.as_slice().read_be_u32(0)?)
}

pub fn store(
&self,
structure: &mut Vec<u8>,
strings: &mut StringTable,
) -> Result<()> {
pub fn store(&self, structure: &mut Vec<u8>, strings: &mut StringTable) -> Result<()> {
structure.pad(4)?;
let len = structure.len();
structure.write_be_u32(len, OF_DT_BEGIN_NODE)?;
Expand Down
Loading