Skip to content

Commit 1ca431a

Browse files
authored
Merge pull request #8 from woshiluo/search
Add utils for search fdt and find a node in fdt & Refactor to merge cursor type
2 parents 7c88139 + 720a12b commit 1ca431a

File tree

15 files changed

+1347
-880
lines changed

15 files changed

+1347
-880
lines changed

examples/hifive-unmatched-a00.dts

Lines changed: 655 additions & 0 deletions
Large diffs are not rendered by default.

src/de.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777

7878
let total_size = u32::from_be(header.total_size);
7979
let raw_data_len = (total_size - HEADER_LEN) as usize;
80-
let ans_ptr = core::ptr::from_raw_parts(ptr as *const u8, raw_data_len);
80+
let ans_ptr = core::ptr::from_raw_parts(ptr, raw_data_len);
8181
let device_tree: &DeviceTree = &*ans_ptr;
8282
let tags = device_tree.tags();
8383
let mut d = Deserializer {

src/de_mut/cursor.rs

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub(super) struct AnyCursor<T: Type = Body>(usize, PhantomData<T>);
77

88
pub(super) type BodyCursor = AnyCursor<Body>;
99
pub(super) type TitleCursor = AnyCursor<Title>;
10-
pub(super) type GroupCursor = AnyCursor<Group>;
1110
pub(super) type PropCursor = AnyCursor<Prop>;
1211

1312
pub(super) trait Type {}
@@ -17,13 +16,10 @@ pub(super) struct Body {}
1716
#[derive(Clone, Copy, Debug)]
1817
pub(super) struct Title {}
1918
#[derive(Clone, Copy, Debug)]
20-
pub(super) struct Group {}
21-
#[derive(Clone, Copy, Debug)]
2219
pub(super) struct Prop {}
2320

2421
impl Type for Body {}
2522
impl Type for Title {}
26-
impl Type for Group {}
2723
impl Type for Prop {}
2824

2925
pub enum MoveResult {
@@ -107,7 +103,7 @@ impl BodyCursor {
107103
self.0 += 1;
108104
MoveResult::Others
109105
}
110-
_ => todo!(),
106+
_ => todo!("unknown block {}", structure[self.0]),
111107
}
112108
}
113109

@@ -149,10 +145,10 @@ impl TitleCursor {
149145
}
150146

151147
/// 生成组光标。
152-
pub fn take_group_on(&self, dtb: RefDtb, name: &str) -> (GroupCursor, usize, BodyCursor) {
148+
pub fn take_group_on(&self, dtb: RefDtb, name: &str) -> (BodyCursor, usize, BodyCursor) {
153149
let name_bytes = name.as_bytes();
154150
let name_skip = align(name_bytes.len() + 1, BLOCK_LEN);
155-
let group = AnyCursor::<Group>(self.0, PhantomData);
151+
let group = AnyCursor::<Body>(self.0, PhantomData);
156152

157153
let mut body = AnyCursor::<Body>(self.0 + 1 + name_skip, PhantomData);
158154
let mut len = 1;
@@ -194,57 +190,6 @@ impl TitleCursor {
194190
}
195191
}
196192

197-
impl GroupCursor {
198-
/// 读取缓存的下一项偏移。
199-
pub fn offset_on(&self, dtb: RefDtb) -> usize {
200-
(dtb.borrow().structure[self.0].0 >> 8) as _
201-
}
202-
203-
/// 利用缓存的名字长度取出名字。
204-
pub fn name_on<'a>(&self, dtb: RefDtb<'a>) -> (&'a [u8], BodyCursor) {
205-
let structure = &dtb.borrow().structure;
206-
let len_name = (structure[self.0].0 & 0xff) as usize;
207-
let bytes = structure[self.0 + 1].lead_slice(len_name);
208-
(
209-
bytes,
210-
AnyCursor(self.0 + 1 + align(len_name + 1, BLOCK_LEN), PhantomData),
211-
)
212-
}
213-
214-
/// 初始化组反序列化。
215-
pub fn init_on(&self, dtb: RefDtb, len_item: usize, len_name: usize) {
216-
let mut body = AnyCursor::<Body>(self.0, PhantomData);
217-
for _ in 0..len_item {
218-
let current = body.0;
219-
let len_total = dtb.borrow().structure[current + 1]
220-
.lead_slice(u16::MAX as _)
221-
.iter()
222-
.enumerate()
223-
.skip(len_name + 1)
224-
.find(|(_, b)| **b == b'\0')
225-
.map(|(i, _)| i)
226-
.unwrap();
227-
body.step_n(align(len_total, BLOCK_LEN));
228-
body.skip_str_on(dtb);
229-
body.escape_from(dtb);
230-
let off_next = body.0 - current;
231-
dtb.borrow_mut().structure[current].0 = (off_next << 8 | len_total) as _;
232-
}
233-
}
234-
235-
/// 组结构恢复原状。
236-
pub fn drop_on(&self, dtb: RefDtb, len_item: usize) {
237-
use StructureBlock as B;
238-
let structure = &mut *dtb.borrow_mut().structure;
239-
let mut i = self.0;
240-
for _ in 0..len_item {
241-
let offset = (structure[i].0 >> 8) as usize;
242-
structure[i] = B::NODE_BEGIN;
243-
i += offset;
244-
}
245-
}
246-
}
247-
248193
impl PropCursor {
249194
pub fn name_on<'a>(&self, dtb: RefDtb<'a>) -> (&'a str, BodyCursor) {
250195
let dtb = dtb.borrow();

0 commit comments

Comments
 (0)