Skip to content

Commit 9bc3c29

Browse files
committed
docs: add docs for patch and string_block
Signed-off-by: Woshiluo Luo <[email protected]>
1 parent 6fff089 commit 9bc3c29

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

src/ser/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::ser::patch::Patch;
88

99
const RSVMAP_LEN: usize = 16;
1010

11+
/// Serialize the data to dtb, with a list fof Patch, write to the `writer`.
12+
///
1113
/// We do run-twice on convert, first time to generate string block, second time todo real
1214
/// structure.
1315
pub fn to_dtb<'se, T>(data: &T, list: &'se [Patch<'se>], writer: &'se mut [u8]) -> Result<(), Error>
@@ -45,6 +47,7 @@ where
4547
data.serialize(&mut ser)?;
4648
ser.dst.step_by_u32(FDT_END);
4749
}
50+
// Make header
4851
{
4952
let header = unsafe { &mut *(header.as_mut_ptr() as *mut Header) };
5053
header.magic = u32::from_be(DEVICE_TREE_MAGIC);

src/ser/patch.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use super::serializer::Serializer;
22
use core::cell::Cell;
33

4+
/// Since this crate is mostly work with `noalloc`, we use `Patch` and `PatchList` for change or
5+
/// add on a dtb.
46
pub struct Patch<'se> {
5-
name: &'se str,
67
pub data: &'se dyn erased_serde::Serialize,
8+
name: &'se str,
9+
10+
/// This patch match how many item between its path and serializer.
711
matched_depth: Cell<usize>,
12+
/// Show this patch have been parsed.
813
parsed: Cell<bool>,
914
}
1015

@@ -20,6 +25,7 @@ impl<'se> Patch<'se> {
2025
}
2126

2227
#[inline(always)]
28+
/// Reset the status of patch.
2329
pub fn init(&self) {
2430
self.matched_depth.set(0);
2531
self.parsed.set(false);
@@ -40,6 +46,7 @@ impl<'se> Patch<'se> {
4046

4147
// I hope to impl serde::ser::Serializer, but erase_serialize's return value is different from
4248
// normal serialize, so we do this.
49+
/// Serialize this patch with serializer.
4350
#[inline(always)]
4451
pub fn serialize(&self, serializer: &mut Serializer<'se>) {
4552
self.parsed.set(true);
@@ -49,6 +56,7 @@ impl<'se> Patch<'se> {
4956
}
5057
}
5158

59+
/// Here is a list of `Patch`, and have some methods for update `Patch` status.
5260
pub struct PatchList<'se> {
5361
list: &'se [Patch<'se>],
5462
}
@@ -86,6 +94,8 @@ impl<'se> PatchList<'se> {
8694
}
8795

8896
#[inline(always)]
97+
/// Return a list which is on this level, but haven't been parsed, which usually means this
98+
/// patch is for adding.
8999
pub fn add_list(&self, depth: usize) -> impl Iterator<Item = &'se Patch<'se>> + use<'se> {
90100
self.list.iter().filter(move |x| {
91101
x.matched_depth.get() == depth && x.get_depth() == depth + 1 && !x.parsed.get()

src/ser/serializer.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ use super::string_block::StringBlock;
55
use crate::common::*;
66

77
#[derive(Clone, Copy)]
8+
// The enum for current parsing type.
89
enum ValueType {
910
Node,
1011
Prop,
1112
}
1213

14+
/// Serializer
15+
/// - `dst`: Pointer of distance &[u8] and the ref of &[u8].
16+
/// - `current_value_type`, `current_name`, `current_dep`: For recursive.
1317
pub struct Serializer<'se> {
1418
pub dst: &'se mut Pointer<'se>,
1519
string_block: &'se mut StringBlock<'se>,
16-
value_type: ValueType,
20+
patch_list: &'se mut PatchList<'se>,
21+
22+
current_value_type: ValueType,
1723
current_name: &'se str,
1824
current_dep: usize,
19-
patch_list: &'se mut PatchList<'se>,
2025
}
2126

2227
impl<'se> Serializer<'se> {
@@ -31,7 +36,7 @@ impl<'se> Serializer<'se> {
3136
string_block: cache,
3237
current_dep: 0,
3338
current_name: "",
34-
value_type: ValueType::Node,
39+
current_value_type: ValueType::Node,
3540
patch_list,
3641
}
3742
}
@@ -49,7 +54,9 @@ impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
4954
T: serde::ser::Serialize + ?Sized,
5055
{
5156
let prop_header_offset = self.dst.step_by_prop();
52-
let prev_type = self.value_type;
57+
58+
// Save prev
59+
let prev_type = self.current_value_type;
5360
let prev_name = self.current_name;
5461
self.current_dep += 1;
5562
self.current_name = key;
@@ -66,7 +73,7 @@ impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
6673

6774
// We now know how long the prop value.
6875
// TODO: make we have some better way than put nop, like move this block ahead.
69-
if let ValueType::Node = self.value_type {
76+
if let ValueType::Node = self.current_value_type {
7077
self.dst
7178
.write_to_offset_u32(prop_header_offset - 4, FDT_NOP);
7279
} else {
@@ -80,10 +87,12 @@ impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
8087
);
8188
}
8289

83-
self.value_type = prev_type;
84-
self.current_name = prev_name;
8590
self.dst.step_align();
91+
92+
// Load prev
8693
self.patch_list.step_back(self.current_dep);
94+
self.current_value_type = prev_type;
95+
self.current_name = prev_name;
8796
self.current_dep -= 1;
8897

8998
Ok(())
@@ -258,7 +267,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
258267
}
259268

260269
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
261-
self.value_type = ValueType::Prop;
270+
self.current_value_type = ValueType::Prop;
262271
self.dst.step_by_u32(v);
263272
Ok(())
264273
}
@@ -280,7 +289,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
280289
}
281290

282291
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
283-
self.value_type = ValueType::Prop;
292+
self.current_value_type = ValueType::Prop;
284293
v.bytes().for_each(|x| {
285294
self.dst.step_by_u8(x);
286295
});
@@ -289,7 +298,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
289298
}
290299

291300
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
292-
self.value_type = ValueType::Prop;
301+
self.current_value_type = ValueType::Prop;
293302
v.iter().for_each(|x| self.dst.step_by_u8(*x));
294303
Ok(())
295304
}
@@ -347,7 +356,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
347356
}
348357

349358
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
350-
self.value_type = ValueType::Prop;
359+
self.current_value_type = ValueType::Prop;
351360
Ok(self)
352361
}
353362

@@ -389,7 +398,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
389398
} else {
390399
self.dst.step_by_name(self.current_name);
391400
}
392-
self.value_type = ValueType::Node;
401+
self.current_value_type = ValueType::Node;
393402
Ok(self)
394403
}
395404

src/ser/string_block.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
/// StringBlock
2+
/// As spec said, dtb have a block called string block for saving prop names.
13
pub struct StringBlock<'se> {
24
end: &'se mut usize,
35
data: &'se mut [u8],
46
}
57

68
impl<'se> StringBlock<'se> {
9+
/// Make a new string block.
10+
///
11+
/// For get how long is string block, we make `end` as a mut ref.
712
#[inline(always)]
813
pub fn new(dst: &'se mut [u8], end: &'se mut usize) -> StringBlock<'se> {
914
StringBlock { data: dst, end }
1015
}
1116

12-
/// Will panic when len > end
13-
/// TODO: show as error
14-
/// Return (Result String, End Offset)
17+
// TODO: show as error
18+
/// Assume the passing `offset` is the start of a string, and return this string.
19+
/// Return (Result String, End Offset).
20+
///
21+
/// Will panic when len > end.
1522
#[inline(always)]
1623
pub fn get_str_by_offset(&self, offset: usize) -> (&str, usize) {
1724
if offset > *self.end {
@@ -44,6 +51,7 @@ impl<'se> StringBlock<'se> {
4451
result
4552
}
4653

54+
/// Find a string. If not found, insert it.
4755
#[inline(always)]
4856
pub fn find_or_insert(&mut self, name: &str) -> usize {
4957
let mut current_pos = 0;

0 commit comments

Comments
 (0)