File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ // 在实际使用中,将这里的 `serde_derive::Deserialize` 改为 `serde::Deserialize`。
2+ use serde_derive:: Deserialize ;
3+
4+ use serde_device_tree:: {
5+ buildin:: { NodeSeq , Reg } ,
6+ error:: Error ,
7+ from_raw_mut, Dtb , DtbPtr ,
8+ } ;
9+
10+ const RAW_DEVICE_TREE : & [ u8 ] = include_bytes ! ( "../examples/qemu-virt.dtb" ) ;
11+ const BUFFER_SIZE : usize = RAW_DEVICE_TREE . len ( ) ;
12+
13+ #[ repr( align( 8 ) ) ]
14+ struct AlignedBuffer {
15+ pub data : [ u8 ; RAW_DEVICE_TREE . len ( ) ] ,
16+ }
17+
18+ #[ derive( Deserialize ) ]
19+ struct Tree < ' a > {
20+ soc : Soc < ' a > ,
21+ }
22+
23+ #[ allow( dead_code) ]
24+ #[ derive( Deserialize ) ]
25+ struct Soc < ' a > {
26+ virtio_mmio : NodeSeq < ' a > ,
27+ }
28+
29+ #[ derive( Deserialize ) ]
30+ #[ allow( unused) ]
31+ struct VirtIoMmio < ' a > {
32+ reg : Reg < ' a > ,
33+ }
34+
35+ #[ test]
36+ fn qemu_virt ( ) -> Result < ( ) , Error > {
37+ // 整个设备树二进制文件需要装载到一块可写的内存区域
38+ let mut aligned_data: Box < AlignedBuffer > = Box :: new ( AlignedBuffer {
39+ data : [ 0 ; BUFFER_SIZE ] ,
40+ } ) ;
41+ aligned_data. data [ ..BUFFER_SIZE ] . clone_from_slice ( RAW_DEVICE_TREE ) ;
42+ let mut slice = aligned_data. data . to_vec ( ) ;
43+ let ptr = DtbPtr :: from_raw ( slice. as_mut_ptr ( ) ) ?;
44+ let dtb = Dtb :: from ( ptr) . share ( ) ;
45+
46+ let t: Tree = from_raw_mut ( & dtb) . unwrap ( ) ;
47+
48+ assert_eq ! ( t. soc. virtio_mmio. len( ) , 8 ) ;
49+ assert_eq ! ( slice, RAW_DEVICE_TREE ) ;
50+
51+ Ok ( ( ) )
52+ }
You can’t perform that action at this time.
0 commit comments