From 592d11b29d2673a3db597976f1f964f9273137a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ma=C4=87kowski?= Date: Thu, 26 Nov 2026 21:26:50 +0000 Subject: [PATCH] refactor(fdt_mut): migrate FdtMut::compact to RawCursor --- src/fdt_mut.rs | 82 ++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/src/fdt_mut.rs b/src/fdt_mut.rs index 1e13cfc..b0e1d6a 100644 --- a/src/fdt_mut.rs +++ b/src/fdt_mut.rs @@ -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}; @@ -420,40 +421,44 @@ impl FdtMut { /// ``` 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; @@ -486,6 +491,18 @@ impl FdtMut { .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"); @@ -695,24 +712,3 @@ impl<'a, const N: usize> From> 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 - } - } -}