Skip to content

Commit

Permalink
feat: add test from qemu-virt
Browse files Browse the repository at this point in the history
Signed-off-by: Woshiluo Luo <[email protected]>
  • Loading branch information
woshiluo committed Oct 31, 2024
1 parent 6b98e91 commit b7b58c2
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/qemu-virt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 在实际使用中,将这里的 `serde_derive::Deserialize` 改为 `serde::Deserialize`。
use serde_derive::Deserialize;

use serde_device_tree::{
buildin::{NodeSeq, Reg},
error::Error,
from_raw_mut, Dtb, DtbPtr,
};

const RAW_DEVICE_TREE: &[u8] = include_bytes!("../examples/qemu-virt.dtb");
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();

#[repr(align(8))]
struct AlignedBuffer {
pub data: [u8; RAW_DEVICE_TREE.len()],
}

#[derive(Deserialize)]
struct Tree<'a> {
soc: Soc<'a>,
}

#[allow(dead_code)]
#[derive(Deserialize)]
struct Soc<'a> {
virtio_mmio: NodeSeq<'a>,
}

#[derive(Deserialize)]
#[allow(unused)]
struct VirtIoMmio<'a> {
reg: Reg<'a>,
}

#[test]
fn qemu_virt() -> Result<(), Error> {
// 整个设备树二进制文件需要装载到一块可写的内存区域
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
data: [0; BUFFER_SIZE],
});
aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
let mut slice = aligned_data.data.to_vec();
let ptr = DtbPtr::from_raw(slice.as_mut_ptr())?;
let dtb = Dtb::from(ptr).share();

let t: Tree = from_raw_mut(&dtb).unwrap();

assert_eq!(t.soc.virtio_mmio.len(), 8);
assert_eq!(slice, RAW_DEVICE_TREE);

Ok(())
}

0 comments on commit b7b58c2

Please sign in to comment.