Skip to content
Open
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
82 changes: 39 additions & 43 deletions src/fdt_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ pub use property::FdtPropertyMut;
use zerocopy::{FromBytes, IntoBytes, big_endian};

use crate::error::{BufferError, FdtParseError};
use crate::fdt::raw::{RawCursor, RawFdtEvent};
use crate::fdt::{
FDT_BEGIN_NODE, FDT_END, FDT_END_NODE, FDT_LAST_COMP_VERSION, FDT_MAGIC, FDT_NOP, FDT_TAGSIZE,
FDT_VERSION, Fdt, FdtHeader, FdtOptions, FdtToken,
FDT_VERSION, Fdt, FdtHeader, FdtOptions,
};
use crate::memreserve::RawMemoryReservation;
use crate::{ToPropertyValue, Tree};
Expand Down Expand Up @@ -420,40 +421,44 @@ impl<B: FdtBuffer> FdtMut<B> {
/// ```
pub fn compact(&mut self) {
let header = self.as_read_only().header();
let off_dt_struct = header.off_dt_struct() as usize;
let off_dt_struct_u32 = header.off_dt_struct();
let off_dt_struct = off_dt_struct_u32 as usize;
let size_dt_struct = header.size_dt_struct() as usize;
let off_dt_strings = header.off_dt_strings() as usize;
let size_dt_strings = header.size_dt_strings() as usize;

let mut read_offset = off_dt_struct;
let mut write_offset = off_dt_struct;
let struct_end = off_dt_struct + size_dt_struct;

while read_offset < struct_end {
let token = self
.as_read_only()
.read_token(read_offset)
.expect("valid FDT token");
let chunk_len = token_chunk_len(self.as_read_only(), read_offset, token);

if token != FdtToken::Nop {
if write_offset != read_offset {
self.data
.data_mut()
.copy_within(read_offset..read_offset + chunk_len, write_offset);
let mut cursor = RawCursor::new(off_dt_struct_u32);
let mut write_offset = off_dt_struct;
let mut run_start = off_dt_struct;

while cursor.offset() < struct_end {
let prev_end = cursor.offset();
if cursor
.skip_nops_up_to(self.as_read_only(), struct_end - prev_end)
.expect("Fdt should be valid")
> 0
{
self.copy_compacted_run(run_start, prev_end, &mut write_offset);
run_start = cursor.offset();
if cursor.offset() >= struct_end {
break;
}
write_offset += chunk_len;
}

read_offset += chunk_len;

if token == FdtToken::End {
let event = cursor
.next(self.as_read_only())
.expect("Fdt should be valid");
if matches!(event, RawFdtEvent::End(_)) {
break;
}
}
self.copy_compacted_run(run_start, cursor.offset(), &mut write_offset);

let new_size_dt_struct = write_offset - off_dt_struct;
let bytes_saved = size_dt_struct - new_size_dt_struct;
let bytes_saved = size_dt_struct
.checked_sub(new_size_dt_struct)
.expect("compacted struct block should not exceed size_dt_struct");

if bytes_saved == 0 {
return;
Expand Down Expand Up @@ -486,6 +491,18 @@ impl<B: FdtBuffer> FdtMut<B> {
.expect("shrinking is infallible");
}

fn copy_compacted_run(&mut self, run_start: usize, run_end: usize, write_offset: &mut usize) {
let run_len = run_end - run_start;
if run_len > 0 {
if *write_offset != run_start {
self.data
.data_mut()
.copy_within(run_start..run_end, *write_offset);
}
*write_offset += run_len;
}
}

fn header_mut(&mut self) -> &mut FdtHeader {
let (header, _) =
FdtHeader::mut_from_prefix(self.data.data_mut()).expect("Fdt should be valid");
Expand Down Expand Up @@ -695,24 +712,3 @@ impl<'a, const N: usize> From<FdtMut<&'a mut [u8; N]>> for Fdt<'a> {
Self::new_unchecked(&fdt.data[..len])
}
}

fn token_chunk_len(fdt: Fdt<'_>, offset: usize, token: FdtToken) -> usize {
match token {
FdtToken::Nop | FdtToken::EndNode | FdtToken::End => FDT_TAGSIZE,
FdtToken::BeginNode => {
let name_start = offset + FDT_TAGSIZE;
let name_end = fdt.find_string_end(name_start).expect("valid node name");
Fdt::align_tag_offset(name_end) - offset
}
FdtToken::Prop => {
let (val_len_bytes, _) = big_endian::U32::ref_from_prefix(
fdt.data_at_offset(offset + FDT_TAGSIZE)
.expect("valid property length offset"),
)
.expect("valid property length");
let val_len = val_len_bytes.get() as usize;
let prop_end = offset + 3 * FDT_TAGSIZE + val_len;
Fdt::align_tag_offset(prop_end) - offset
}
}
}
Loading