Skip to content

Commit 0f495ef

Browse files
committed
define flags as bitfield for ME gen 2 directory entry
Signed-off-by: Daniel Maslowski <[email protected]>
1 parent 1eaae1d commit 0f495ef

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

src/dir/gen2.rs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::fmt::{self, Display};
22
use core::str::from_utf8;
33

4+
use bitfield_struct::bitfield;
45
use serde::{Deserialize, Serialize};
56
use zerocopy::{FromBytes, Ref};
67
use zerocopy_derive::{FromBytes, Immutable, IntoBytes};
@@ -25,47 +26,73 @@ 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)]
3558
pub 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)]
4381
pub 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

5189
impl 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;
68-
let code_end = (b + self.code_size) 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;
95+
let code_end = (b + self.mod_size) as usize;
6996
let data_end = (b + self.memory_size) as usize;
7097
BinaryMap {
7198
rapi,
@@ -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

src/show.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,8 @@ fn print_gen2_dirs(dirs: &Vec<Gen2Dir>) {
1414
println!("{dir}");
1515
for e in &dir.entries {
1616
let pos = dir.offset + e.offset as usize;
17-
/*
18-
let sig =
19-
u32::read_from_prefix(&data[pos..pos + 4]).unwrap();
20-
let kind = match sig {
21-
SIG_LUT => "LLUT",
22-
SIG_LZMA => "LZMA",
23-
_ => {
24-
dump48(&data[pos..]);
25-
"unknown"
26-
}
27-
};
28-
*/
29-
let kind = "...";
30-
let t = e.compression_type();
3117
let b = e.bin_map();
32-
println!(" - {e} {pos:08x} {t:?} ({kind})\n {b}");
18+
println!(" - {e} @ {pos:08x}\n {b}");
3319
}
3420
println!();
3521
}

0 commit comments

Comments
 (0)