intel_fw 0.1.0

Parsing and editing library for Intel firmware images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Flash Partition Table (FPT)
//!
//! Knowledge herein is solely based on independent analysis efforts.
//! The expansion of the acronym FPT is confirmed by Intel in the appendix
//! on ME Firmware Status Registers in
//! <https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/intel-power-node-manager-v3-spec.pdf>.
//! For references regarding data structures and logic,
//! see <https://github.com/peterbjornx/meimagetool> `...intelme/model/fpt/` (Java)
//! and <https://github.com/linuxboot/fiano/blob/main/pkg/intel/me/structures.go>
//! and <https://github.com/platomav/MEAnalyzer>
//! and <https://github.com/corna/me_cleaner>
//! and <https://github.com/mostav02/Remove_IntelME_FPT>.

use core::{
    convert::Infallible,
    fmt::{self, Display},
    mem::size_of,
    num::Wrapping,
};

use bitfield_struct::bitfield;
use serde::{Deserialize, Serialize};
use zerocopy::{AlignmentError, ConvertError, FromBytes, IntoBytes, Ref, SizeError};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes};

use crate::{
    EMPTY,
    part::part::{ClearOptions, retain},
    ver::Version,
};

pub const FTPR: &str = "FTPR";
pub const FTUP: &str = "FTUP";
pub const DLMP: &str = "DLMP";
pub const MDMV: &str = "MDMV";
pub const PSVN: &str = "PSVN";
pub const IVBP: &str = "IVBP";
pub const MFS: &str = "MFS";
pub const NFTP: &str = "NFTP";
pub const ROMB: &str = "ROMB";
pub const WCOD: &str = "WCOD";
pub const LOCL: &str = "LOCL";
pub const FLOG: &str = "FLOG";
pub const UTOK: &str = "UTOK";
pub const ISHC: &str = "ISHC";
pub const AFSP: &str = "AFSP";
pub const FTPM: &str = "FTPM";
pub const GLUT: &str = "GLUT";
pub const EFFS: &str = "EFFS";
pub const FOVD: &str = "FOVD";

pub const DIR_PARTS: &[&str] = &[
    FTPR, //
    FTUP, //
    DLMP, //
    MDMV, //
    NFTP, //
];

pub const FS_PARTS: &[&str] = &[
    MFS,  //
    AFSP, //
    EFFS, //
];

// Those have been found to be safe to remove.
pub const REMOVABLE_PARTS: &[&str] = &[
    FLOG, //
    FTUP, //
    IVBP, // IVBP is used in hibernation, should probably not be removed?!
    MFS,  //
    NFTP, //
    PSVN, //
    UTOK, //
];

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum PartitionType {
    Code,
    Data,
    None,
}

// see https://troopers.de/downloads/troopers17/TR17_ME11_Static.pdf
pub fn get_part_info(n: &str) -> (PartitionType, &str) {
    match n {
        FTPR => (PartitionType::Code, "Main code partition"),
        FTUP => (PartitionType::Code, "[NFTP]+[WCOD]+[LOCL]"),
        DLMP => (PartitionType::Code, "IDLM partition"),
        MDMV => (PartitionType::Code, "Media protection (PAVP, JOM)"),
        PSVN => (PartitionType::Data, "Secure Version Number"),
        IVBP => (PartitionType::Data, "IV + Bring Up cache"),
        MFS => (PartitionType::Data, "ME Flash File System"),
        NFTP => (PartitionType::Code, "Additional code"),
        ROMB => (PartitionType::Code, "ROM Bypass"),
        WCOD => (PartitionType::Code, "WLAN uCode"),
        LOCL => (PartitionType::Code, "AMT Localization"),
        FLOG => (PartitionType::Data, "Flash Log"),
        UTOK => (PartitionType::Data, "Debug Unlock Token"),
        ISHC => (PartitionType::Code, "Integrated Sensors Hub"),
        AFSP => (PartitionType::None, "8778 55aa signature like MFS"),
        FTPM => (PartitionType::Code, "Firmware TPM (unconfirmed)"),
        GLUT => (PartitionType::Data, "Huffman Look-Up Table"),
        EFFS => (PartitionType::Data, "EFFS File System"),
        FOVD => (PartitionType::Data, "FOVD..."),
        _ => (PartitionType::None, "[> UNKNOWN <]"),
    }
}

#[derive(Immutable, IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct FPTHeader {
    pub signature: [u8; 4],
    pub entries: u32,
    pub header_ver: u8,
    pub entry_ver: u8,
    pub header_len: u8,
    pub checksum: u8,
    pub ticks_to_add: u16,
    pub tokens_to_add: u16,
    pub uma_size_or_reserved: u32,
    pub flash_layout_or_flags: u32,
    // Not Present in ME version 7
    /// Version of Flash Image Tool used to create the image
    /// It is abbreviated FIT(C), though not clear what the C is for.
    /// Note: This is NOT related to the Firmware Interface Table.
    pub fitc_ver: Version,
}

const FPT_HEADER_SIZE: usize = size_of::<FPTHeader>();

impl Display for FPTHeader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let hv = format!("  Header version: {}", self.header_ver);
        let ev = format!("  Entry version:  {}", self.entry_ver);
        let en = self.entries;
        let en = format!("  Entries:        {en}");
        let cs = format!("  Checksum:       {:02x}", self.checksum);
        let fv = self.fitc_ver;
        let fv = format!("  FITC version:   {fv}");
        write!(f, "{hv}\n{ev}\n{en}\n{cs}\n{fv}")
    }
}

type EntryConvertError<'a> =
    ConvertError<AlignmentError<&'a [u8], [FPTEntry]>, SizeError<&'a [u8], [FPTEntry]>, Infallible>;

#[derive(Debug)]
pub enum FptError<'a> {
    ParseHeaderError(SizeError<&'a [u8], FPTHeader>),
    ParseEntryError(EntryConvertError<'a>),
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum PartitionKind {
    Code,
    Data,
    NVRAM,
    XXData,
    EFFS,
    Unknown,
}

impl PartitionKind {
    const fn from_bits(val: u8) -> Self {
        match val {
            0 => Self::Code,
            1 => Self::Data,
            2 => Self::NVRAM,
            3 => Self::XXData,
            4 => Self::EFFS,
            _ => Self::Unknown,
        }
    }

    const fn into_bits(self) -> u8 {
        self as u8
    }
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum Validity {
    Valid,
    Invalid,
    Unknown,
}

impl Validity {
    const fn from_bits(val: u8) -> Self {
        match val {
            0x00 => Self::Valid,
            0xff => Self::Invalid,
            _ => Self::Unknown,
        }
    }

    const fn into_bits(self) -> u8 {
        self as u8
    }
}

#[bitfield(u32)]
#[derive(Immutable, IntoBytes, FromBytes, Serialize, Deserialize)]
pub struct EntryFlags {
    #[bits(7)]
    pub kind: PartitionKind,
    pub copy_to_dram_cache: bool,
    #[bits(7)]
    _res: u8,
    pub len1: bool,
    pub len2: bool,
    #[bits(7)]
    _res: u8,
    #[bits(8)]
    pub validity: Validity,
}

#[derive(Immutable, IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct FPTEntry {
    pub name: [u8; 4],
    pub owner: [u8; 4],
    pub offset: u32,
    pub size: u32,
    pub start_tokens: u32,
    pub max_tokens: u32,
    pub scratch_sectors: u32,
    pub flags: EntryFlags,
}

impl FPTEntry {
    pub fn name(&self) -> String {
        match std::str::from_utf8(&self.name) {
            // some names are shorter than 4 bytes and padded with 0x0
            Ok(n) => n.trim_end_matches('\0').to_string(),
            Err(_) => format!("{:02x?}", self.name),
        }
    }

    pub fn offset(&self) -> usize {
        self.offset as usize & 0x003f_ffff
    }

    pub fn set_offset(&mut self, offset: u32) {
        let u = self.offset & 0xffc0_0000;
        let o = offset & 0x003f_ffff;
        self.offset = u | o;
    }

    pub fn size(&self) -> usize {
        self.size as usize
    }
}

const FPT_ENTRY_SIZE: usize = size_of::<FPTEntry>();

impl Display for FPTEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let o = self.offset as usize;
        let s = self.size as usize;
        let end = o + s;
        let name = self.name();
        let (part_type, full_name) = get_part_info(&name);
        let part_info = format!("{part_type:?}: {full_name}");
        let name_offset_end_size = format!("{name:>4} @ 0x{o:08x}:0x{end:08x} (0x{s:08x})");

        let fl = self.flags;
        let fb = fl.into_bits();
        write!(f, "{name_offset_end_size}  {part_info}\n  {fb:08x}: {fl:?}")
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FPT {
    pub pre_header: Vec<u8>,
    pub header: FPTHeader,
    pub entries: Vec<FPTEntry>,
    pub original_size: usize,
}

pub const MIN_FPT_SIZE: usize = 0x400;

const FPT_MAGIC: &str = "$FPT";
const FPT_MAGIC_BYTES: &[u8] = FPT_MAGIC.as_bytes();

const POSSIBLE_OFFSET: usize = 16;

// The FPT magic is either at the start or at a 16 bytes offset.
fn determine_offset(data: &[u8]) -> Option<usize> {
    let m = &data[..FPT_MAGIC_BYTES.len()];
    if m.eq(FPT_MAGIC_BYTES) {
        Some(0)
    } else {
        let m = &data[POSSIBLE_OFFSET..POSSIBLE_OFFSET + FPT_MAGIC_BYTES.len()];
        if m.eq(FPT_MAGIC_BYTES) {
            Some(POSSIBLE_OFFSET)
        } else {
            None
        }
    }
}

impl<'a> FPT {
    pub fn parse(data: &'a [u8]) -> Option<Result<Self, FptError<'a>>> {
        let offset = determine_offset(data)?;
        // Save for checksum recalculation
        let pre_header = &data[..offset];
        let d = &data[offset..];
        let header = match FPTHeader::read_from_prefix(d) {
            Ok((h, _)) => h,
            Err(e) => return Some(Err(FptError::ParseHeaderError(e))),
        };
        // NOTE: Skip $FPT (header) itself
        let slice = &d[FPT_HEADER_SIZE..];
        let count = header.entries as usize;
        let entries = match Ref::<_, [FPTEntry]>::from_prefix_with_elems(slice, count) {
            Ok((r, _)) => r,
            Err(e) => return Some(Err(FptError::ParseEntryError(e))),
        };

        let original_size = pre_header.len() + FPT_HEADER_SIZE + FPT_ENTRY_SIZE * entries.len();

        Some(Ok(Self {
            pre_header: pre_header.to_vec(),
            header,
            entries: entries.to_vec(),
            original_size,
        }))
    }

    // Find an FPT in a given slice, and if the magic is detected, get the
    // parse result and the offset.
    pub fn scan(data: &'a [u8]) -> Option<(Result<Self, FptError<'a>>, usize)> {
        for o in (0..data.len() - MIN_FPT_SIZE - POSSIBLE_OFFSET).step_by(0x40) {
            if let Some(fpt) = Self::parse(&data[o..]) {
                return Some((fpt, o));
            }
        }
        None
    }

    pub fn get_sorted_entries(&self) -> Vec<FPTEntry> {
        let mut entries = self.entries.clone();
        entries.sort_by_key(|e| e.offset());
        entries
    }

    /// Two's complement of the sum of the bytes
    pub fn header_checksum(&self) -> u8 {
        let mut c = self.header;
        // Initial checksum field itself must be 0.
        c.checksum = 0;
        let d = [self.pre_header.as_bytes(), c.as_bytes()].concat();
        let sum = d.iter().map(|e| Wrapping(*e as i8)).sum::<Wrapping<i8>>();
        -sum.0 as u8
    }

    /// Remove all entries but FTPR, adjusting header and checksum
    ///
    /// Honor force retention/deletion lists, except for NVRAM entries and
    /// empty partitions, i.e. those of size 0; these are always removed.
    pub fn clear(&mut self, options: &ClearOptions) {
        self.entries = self
            .entries
            .iter()
            .filter(|e| {
                let f = e.flags;
                retain(e.name(), options) && e.size() > 0 && f.kind() != PartitionKind::NVRAM
            })
            .copied()
            .collect();
        self.header.entries = self.entries.len() as u32;
        // clear EFFS presence flag if applicable
        if (!options.parts_force_retention.contains(&EFFS.into())
            && options.parts_force_deletion.is_empty())
            || options.parts_force_deletion.contains(&EFFS.into())
        {
            // TODO: define bitfield, parameterize via API
            self.header.flash_layout_or_flags &= 0xffff_fffe;
        }
        self.header.checksum = self.header_checksum();
    }

    pub fn to_vec(self) -> Vec<u8> {
        let all = [
            self.pre_header.as_bytes(),
            self.header.as_bytes(),
            self.entries.as_bytes(),
        ]
        .concat();
        let mut res = all.to_vec();
        res.resize(self.original_size, EMPTY);
        res
    }
}

#[cfg(test)]
static DATA: &[u8] = include_bytes!("../../tests/me11.fpt");

#[cfg(test)]
static FPT_CLEANED: &[u8] = include_bytes!("../../tests/me11_cleaned.fpt");

#[test]
fn parse_size_error() {
    let parsed = FPT::parse(&DATA[..70]);
    assert!(parsed.is_some());
    let fpt_res = parsed.unwrap();
    assert!(matches!(fpt_res, Err(FptError::ParseEntryError(_))));
}

#[test]
fn parse_okay_fpt() {
    let parsed = FPT::parse(&DATA[16..]);
    assert!(parsed.is_some());
    let fpt_res = parsed.unwrap();
    assert!(fpt_res.is_ok());
    let fpt = fpt_res.unwrap();
    assert_eq!(fpt.header.entries as usize, fpt.entries.len());
}

#[test]
fn parse_okay_fpt_with_offset() {
    let parsed = FPT::parse(&DATA);
    assert!(parsed.is_some());
    let fpt_res = parsed.unwrap();
    assert!(fpt_res.is_ok());
    let fpt = fpt_res.unwrap();
    assert_eq!(fpt.header.entries as usize, fpt.entries.len());
}

#[test]
fn checksum() {
    let parsed = FPT::parse(&DATA);
    let fpt = parsed.unwrap().unwrap();
    assert_eq!(fpt.header_checksum(), fpt.header.checksum);
}

#[test]
fn clear() {
    let mut fpt = FPT::parse(&DATA).unwrap().unwrap();
    let opts = ClearOptions {
        keep_modules: false,
        parts_force_retention: vec![],
        parts_force_deletion: vec![],
    };
    fpt.clear(&opts);
    let s = fpt.original_size;
    let cleaned = &fpt.to_vec();
    assert_eq!(cleaned, &FPT_CLEANED[..s]);
}