Skip to content

Commit 18e7360

Browse files
committed
Merge branch 'patch-1' of github.com:woshiluo/serde-device-tree into woshiluo-patch-1
Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
2 parents 413e397 + 6b48fad commit 18e7360

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

examples/hifive-unmatched-a00.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use alloc::collections::BTreeMap;
44
use serde_derive::Deserialize;
55
use serde_device_tree::Compatible;
66

7-
static DEVICE_TREE: &'static [u8] = include_bytes!("hifive-unmatched-a00.dtb");
8-
97
#[derive(Debug, Deserialize)]
108
struct Tree<'a> {
119
#[serde(rename = "#address-cells")]
@@ -48,8 +46,20 @@ struct Cpu<'a> {
4846
compatible: Compatible<'a>,
4947
}
5048

49+
const RAW_DEVICE_TREE: &'static [u8] = include_bytes!("hifive-unmatched-a00.dtb");
50+
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
51+
52+
#[repr(align(4))]
53+
struct AlignedBuffer {
54+
pub data: [u8; RAW_DEVICE_TREE.len()],
55+
}
56+
5157
fn main() {
52-
let ptr = DEVICE_TREE.as_ptr();
58+
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
59+
data: [0; BUFFER_SIZE],
60+
});
61+
aligned_data.data[..BUFFER_SIZE].clone_from_slice(&RAW_DEVICE_TREE);
62+
let ptr = aligned_data.data.as_ptr();
5363
let t: Tree = unsafe { serde_device_tree::from_raw(ptr) }.unwrap();
5464
println!("#address_cells = {}", t.num_address_cells);
5565
println!("#size_cells = {}", t.num_size_cells);

examples/qemu-virt.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ use serde_device_tree::{
1616
from_raw_mut, Dtb, DtbPtr,
1717
};
1818

19+
const RAW_DEVICE_TREE: &'static [u8] = include_bytes!("qemu-virt.dtb");
20+
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
21+
22+
#[repr(align(4))]
23+
struct AlignedBuffer {
24+
pub data: [u8; RAW_DEVICE_TREE.len()],
25+
}
26+
1927
fn main() -> Result<(), Error> {
2028
// 整个设备树二进制文件需要装载到一块可写的内存区域
21-
static DEVICE_TREE: &[u8] = include_bytes!("qemu-virt.dtb");
22-
let mut slice = DEVICE_TREE.to_vec();
29+
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
30+
data: [0; BUFFER_SIZE],
31+
});
32+
aligned_data.data[..BUFFER_SIZE].clone_from_slice(&RAW_DEVICE_TREE);
33+
let mut slice = aligned_data.data.to_vec();
2334
// 这一步验证了设备树首部的正确性,`DtbPtr` 类型可以安全地传递到任何地方,
2435
// 甚至跨地址空间(如果你知道偏移的话)。
2536
let ptr = DtbPtr::from_raw(slice.as_mut_ptr())?;
@@ -127,10 +138,10 @@ fn main() -> Result<(), Error> {
127138

128139
// 解析过程中,设备树的内容被修改了。
129140
// 因此若要以其他方式再次访问设备树,先将这次解析的结果释放。
130-
assert_ne!(slice, DEVICE_TREE);
141+
assert_ne!(slice, RAW_DEVICE_TREE);
131142
}
132143
// 释放后,内存会恢复原状。
133-
assert_eq!(slice, DEVICE_TREE);
144+
assert_eq!(slice, RAW_DEVICE_TREE);
134145

135146
Ok(())
136147
}

src/de.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ where
6060
T: de::Deserialize<'de>,
6161
{
6262
// read header
63+
if (ptr as usize) & (ALIGN - 1) != 0 {
64+
return Err(Error::unaligned(ptr as usize));
65+
}
6366
let header = &*(ptr as *const Header);
6467
header.verify()?;
6568

src/de_mut/cursor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use core::marker::PhantomData;
33

44
#[derive(Clone, Copy, Debug)]
5+
#[repr(C)]
56
pub(super) struct AnyCursor<T: Type = Body>(usize, PhantomData<T>);
67

78
pub(super) type BodyCursor = AnyCursor<Body>;

src/de_mut/reg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct RegRegion(pub Range<usize>);
2222

2323
/// 节点地址空间格式。
2424
#[derive(Clone, Copy, Debug)]
25+
#[repr(C)]
2526
pub(super) struct RegConfig {
2627
pub address_cells: u32,
2728
pub size_cells: u32,

0 commit comments

Comments
 (0)