11use core:: fmt:: { self , Display } ;
22use core:: str:: from_utf8;
33
4+ use bitfield_struct:: bitfield;
45use serde:: { Deserialize , Serialize } ;
56use zerocopy:: { FromBytes , Ref } ;
67use zerocopy_derive:: { FromBytes , Immutable , IntoBytes } ;
@@ -25,46 +26,72 @@ pub struct Entry {
2526 pub memory_size : u32 , // e.g. 0x0004_b425
2627 pub pre_uma_size : u32 , // e.g. 0x0004_b425 (often same as memory_size)
2728 pub entry_point : u32 , // e.g. 0x2009_1000
28- pub flags : u32 , // e.g. 0x0010_d42a
29+ pub flags : Flags , // e.g. 0x0010_d42a
2930 pub _54 : u32 , // e.g. 0x0000_0008
3031 pub _58 : u32 , // so far all 0
3132 pub _5c : u32 , // so far all 0
3233}
3334
34- #[ derive( Clone , Copy , Debug ) ]
35+ #[ bitfield( u32 ) ]
36+ #[ derive( Immutable , FromBytes , IntoBytes , Serialize , Deserialize ) ]
37+ pub struct Flags {
38+ #[ bits( 4 ) ]
39+ _r : u8 ,
40+ #[ bits( 3 ) ]
41+ pub compression : Compression ,
42+ _r : bool ,
43+
44+ _r : u8 ,
45+
46+ _r : bool ,
47+ #[ bits( 3 ) ]
48+ rapi : usize ,
49+ #[ bits( 2 ) ]
50+ kapi : usize ,
51+ #[ bits( 2 ) ]
52+ _r : u8 ,
53+
54+ _r : u8 ,
55+ }
56+
57+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
3558pub enum Compression {
3659 Uncompressed ,
3760 Huffman ,
3861 Lzma ,
3962 Unknown ,
4063}
4164
65+ impl Compression {
66+ const fn from_bits ( val : u8 ) -> Self {
67+ match val {
68+ 0 => Self :: Uncompressed ,
69+ 1 => Self :: Huffman ,
70+ 2 => Self :: Lzma ,
71+ _ => Self :: Unknown ,
72+ }
73+ }
74+
75+ const fn into_bits ( self ) -> u8 {
76+ self as u8
77+ }
78+ }
79+
4280#[ derive( Clone , Copy , Debug ) ]
4381pub struct BinaryMap {
44- pub rapi : u32 , // 3 bits, really
45- pub kapi : u32 , // 2 bits, really
82+ pub rapi : usize , // 3 bits, really
83+ pub kapi : usize , // 2 bits, really
4684 pub code_start : usize ,
4785 pub code_end : usize ,
4886 pub data_end : usize ,
4987}
5088
5189impl Entry {
52- pub fn compression_type ( & self ) -> Compression {
53- let comp_flag = ( self . flags >> 4 ) & 0b111 ;
54- match comp_flag {
55- 0 => Compression :: Uncompressed ,
56- 1 => Compression :: Huffman ,
57- 2 => Compression :: Lzma ,
58- _ => Compression :: Unknown ,
59- }
60- }
61-
6290 pub fn bin_map ( & self ) -> BinaryMap {
6391 let b = self . mod_base ;
64- let f = self . flags ;
65- let rapi = ( f >> 17 ) & 0b111 ;
66- let kapi = ( f >> 20 ) & 0b11 ;
67- let code_start = ( b + ( rapi + kapi) * 0x1000 ) as usize ;
92+ let rapi = self . flags . rapi ( ) ;
93+ let kapi = self . flags . kapi ( ) ;
94+ let code_start = ( b as usize + ( rapi + kapi) * 0x1000 ) as usize ;
6895 let code_end = ( b + self . code_size ) as usize ;
6996 let data_end = ( b + self . memory_size ) as usize ;
7097 BinaryMap {
@@ -100,7 +127,8 @@ impl Display for Entry {
100127 let o = self . offset ;
101128 let s = self . size ;
102129 let e = self . entry_point ;
103- write ! ( f, "{n:16} {s:08x} @ {o:08x}, entry point {e:08x}" )
130+ let c = self . flags . compression ( ) ;
131+ write ! ( f, "{n:16} {s:08x} @ {o:08x}, entry point {e:08x}, {c:10?}" )
104132 }
105133}
106134
0 commit comments