@@ -31,8 +31,17 @@ use serde::de;
3131/// # Example
3232///
3333/// ```
34- /// # static DEVICE_TREE: &'static [u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
35- /// # let dtb_pa = DEVICE_TREE.as_ptr() as usize;
34+ /// # static RAW_DEVICE_TREE: &'static [u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
35+ /// # const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
36+ /// # #[repr(align(4))]
37+ /// # struct AlignedBuffer {
38+ /// # pub data: [u8; RAW_DEVICE_TREE.len()],
39+ /// # }
40+ /// # let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
41+ /// # data: [0; BUFFER_SIZE],
42+ /// # });
43+ /// # aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
44+ /// # let fdt_ptr = aligned_data.data.as_ptr();
3645/// use serde_derive::Deserialize;
3746///
3847/// #[derive(Debug, Deserialize)]
@@ -47,7 +56,7 @@ use serde::de;
4756/// stdout_path: Option<&'a str>,
4857/// }
4958///
50- /// let tree: Tree = unsafe { serde_device_tree::from_raw(dtb_pa as *const u8) }
59+ /// let tree: Tree = unsafe { serde_device_tree::from_raw(fdt_ptr as *const u8) }
5160/// .expect("parse device tree");
5261/// if let Some(chosen) = tree.chosen {
5362/// if let Some(stdout_path) = chosen.stdout_path {
6877
6978 let total_size = u32:: from_be ( header. total_size ) ;
7079 let raw_data_len = ( total_size - HEADER_LEN ) as usize ;
71- let ans_ptr = core:: ptr:: from_raw_parts ( ptr as * const ( ) , raw_data_len) ;
80+ let ans_ptr = core:: ptr:: from_raw_parts ( ptr as * const u8 , raw_data_len) ;
7281 let device_tree: & DeviceTree = & * ans_ptr;
7382 let tags = device_tree. tags ( ) ;
7483 let mut d = Deserializer {
@@ -513,7 +522,16 @@ mod tests {
513522 #[ test]
514523 fn error_invalid_magic ( ) {
515524 static DEVICE_TREE : & [ u8 ] = & [ 0x11 , 0x22 , 0x33 , 0x44 ] ; // not device tree blob format
516- let ptr = DEVICE_TREE . as_ptr ( ) ;
525+ const DEVICE_TREE_LEN : usize = DEVICE_TREE . len ( ) ;
526+ #[ repr( align( 8 ) ) ]
527+ struct AlignedBuffer {
528+ pub data : [ u8 ; DEVICE_TREE_LEN ] ,
529+ }
530+ let mut aligned_data: Box < AlignedBuffer > = Box :: new ( AlignedBuffer {
531+ data : [ 0 ; DEVICE_TREE_LEN ] ,
532+ } ) ;
533+ aligned_data. data [ ..DEVICE_TREE_LEN ] . clone_from_slice ( DEVICE_TREE ) ;
534+ let ptr = aligned_data. data . as_ptr ( ) ;
517535
518536 #[ derive( Debug , Deserialize ) ]
519537 struct Tree { }
0 commit comments