1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![no_std]
#![feature(slice_internals)]
#![deny(warnings)]
mod header;
mod indent;
mod path;
mod property;
mod structure_block;
mod walker;
pub use path::Path;
pub use property::{PHandle, Property, Reg, Str, StrList};
pub mod utils {
pub use crate::indent::indent;
}
pub use header::HeaderError;
use core::{fmt, mem, slice};
use header::FdtHeader;
use property::RegCfg;
use structure_block::StructureBlock;
use walker::Walker;
pub struct Dtb<'a>(&'a [u8]);
impl Dtb<'static> {
#[inline]
pub unsafe fn from_raw_parts(ptr: *const u8) -> Result<Self, HeaderError> {
(*ptr.cast::<FdtHeader>()).verify(|_| true)?;
Ok(Self::from_raw_parts_unchecked(ptr))
}
#[inline]
pub unsafe fn from_raw_parts_filtered(
ptr: *const u8,
f: impl Fn(&HeaderError) -> bool,
) -> Result<Self, HeaderError> {
(*ptr.cast::<FdtHeader>()).verify(f)?;
Ok(Self::from_raw_parts_unchecked(ptr))
}
#[inline]
pub unsafe fn from_raw_parts_unchecked(ptr: *const u8) -> Self {
Self(slice::from_raw_parts(
ptr,
(*ptr.cast::<FdtHeader>()).totalsize.into_u32() as _,
))
}
}
pub enum ConvertError {
Truncated,
Header(HeaderError),
}
impl<'a> Dtb<'a> {
pub fn from_slice(slice: &'a [u8]) -> Result<Self, ConvertError> {
if slice.len() < mem::size_of::<FdtHeader>() {
return Err(ConvertError::Truncated);
}
let header = unsafe { &*slice.as_ptr().cast::<FdtHeader>() };
match header.verify(|_| true) {
Ok(()) => {
let len = header.totalsize.into_u32() as usize;
if len <= slice.len() {
Ok(Self(&slice[..len]))
} else {
Err(ConvertError::Truncated)
}
}
Err(e) => Err(ConvertError::Header(e)),
}
}
}
impl Dtb<'_> {
#[inline]
pub const fn total_size(&self) -> usize {
self.0.len()
}
pub fn walk(&self, mut f: impl FnMut(&Path<'_>, DtbObj) -> WalkOperation) {
let header = self.header();
let off_struct = header.off_dt_struct.into_u32() as usize;
let len_struct = header.size_dt_struct.into_u32() as usize;
let off_strings = header.off_dt_strings.into_u32() as usize;
let len_strings = header.size_dt_strings.into_u32() as usize;
Walker {
tail: unsafe {
slice::from_raw_parts(
self.0[off_struct..]
.as_ptr()
.cast::<StructureBlock>()
.offset(2),
len_struct / StructureBlock::LEN - 3,
)
},
strings: &self.0[off_strings..][..len_strings],
}
.walk_inner(&mut f, &Path::ROOT, RegCfg::DEFAULT, false);
}
#[inline]
fn header(&self) -> &FdtHeader {
unsafe { &*self.0.as_ptr().cast() }
}
}
pub enum DtbObj<'a> {
SubNode { name: &'a [u8] },
Property(Property<'a>),
}
pub enum WalkOperation {
StepInto,
StepOver,
StepOut,
Terminate,
}
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
struct U32BigEndian(u32);
impl U32BigEndian {
#[inline]
pub const fn from_u32(val: u32) -> Self {
Self(u32::to_be(val))
}
#[inline]
pub const fn into_u32(self) -> u32 {
u32::from_be(self.0)
}
}
impl fmt::Debug for U32BigEndian {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
u32::from_be(self.0).fmt(f)
}
}
#[inline]
fn is_aligned(val: usize, bits: usize) -> bool {
val & (bits - 1) == 0
}