intel_fw 0.1.2

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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use core::fmt::{self, Display};
use core::ops::Range;
use core::str::from_utf8;

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

use crate::Removables;
use crate::dir::man::Manifest;

// These must never be removed. They are essential for platform initialization.
pub const ALWAYS_RETAIN: &[&str] = &[
    "BUP",  // bringup
    "ROMP", //
];

const MODULE_MAGIC: &str = "$MME";
const MODULE_MAGIC_BYTES: &[u8] = MODULE_MAGIC.as_bytes();
const SIG_LUT: &str = "LLUT";
const SIG_LUT_BYTES: &[u8] = SIG_LUT.as_bytes();
const SIG_LZMA_BYTES: &[u8] = &[0x36, 0x00, 0x40, 0x00];

// The highest byte of a LUT entry contains flags.
const CHUNK_INACTIVE: u8 = 0x80;
const CHUNK_OFFSET_MASK: u32 = 0x00ff_ffff;

// https://github.com/skochinsky/me-tools me_unpack.py MeModuleHeader2
#[derive(Immutable, IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct Entry {
    pub magic: [u8; 4],
    pub name: [u8; 0x10],
    pub hash: [u8; 0x20],
    pub mod_base: u32,     // e.g. 0x0200_9000
    pub offset: u32,       // e.g. 0x0001_5b4a
    pub code_size: u32,    // e.g. 0x0004_2000
    pub size: u32,         // e.g. 0x0001_d13b
    pub memory_size: u32,  // e.g. 0x0004_b425
    pub pre_uma_size: u32, // e.g. 0x0004_b425 (often same as memory_size)
    pub entry_point: u32,  // e.g. 0x2009_1000
    pub flags: Flags,      // e.g. 0x0010_d42a
    pub _54: u32,          // e.g. 0x0000_0008
    pub _58: u32,          // so far all 0
    pub _5c: u32,          // so far all 0
}

#[bitfield(u32)]
#[derive(Immutable, IntoBytes, FromBytes, Serialize, Deserialize)]
pub struct Flags {
    #[bits(4)]
    _r: u8,
    #[bits(3)]
    pub compression: Compression,
    _r: bool,

    _r: u8,

    _r: bool,
    #[bits(3)]
    rapi: usize,
    #[bits(2)]
    kapi: usize,
    #[bits(2)]
    _r: u8,

    _r: u8,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Compression {
    Uncompressed,
    Huffman,
    Lzma,
    Unknown,
}

impl Compression {
    const fn from_bits(val: u8) -> Self {
        match val {
            0 => Self::Uncompressed,
            1 => Self::Huffman,
            2 => Self::Lzma,
            _ => Self::Unknown,
        }
    }

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

#[derive(Clone, Copy, Debug)]
pub struct BinaryMap {
    pub rapi: usize, // 3 bits, really
    pub kapi: usize, // 2 bits, really
    pub code_start: usize,
    pub code_end: usize,
    pub data_end: usize,
}

impl Entry {
    pub fn bin_map(&self) -> BinaryMap {
        let b = self.mod_base;
        let f = self.flags;
        let rapi = f.rapi();
        let kapi = f.kapi();
        let code_start = b as usize + (rapi + kapi) * 0x1000;
        let code_end = (b + self.code_size) as usize;
        let data_end = (b + self.memory_size) as usize;
        BinaryMap {
            rapi,
            kapi,
            code_start,
            code_end,
            data_end,
        }
    }

    pub fn name(&self) -> String {
        match std::str::from_utf8(&self.name) {
            Ok(n) => n.trim_end_matches('\0').to_string(),
            Err(_) => format!("{:02x?}", &self.name),
        }
    }
}

impl Display for BinaryMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let r = self.rapi;
        let k = self.kapi;
        let s = self.code_start;
        let e = self.code_end;
        let de = self.data_end;
        write!(
            f,
            "RAPI {r:03b} KAPI {k:02b} code {s:08x}:{e:08x}, data end {de:08x}"
        )
    }
}

impl Display for Entry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let n = match from_utf8(&self.name) {
            Ok(n) => n.trim_end_matches('\0').to_string(),
            Err(_) => format!("{:02x?}", self.name),
        };
        let o = self.offset;
        let s = self.size;
        let e = self.entry_point;
        let fl = self.flags;
        let c = fl.compression();
        write!(f, "{n:16} {s:08x} @ {o:08x}, entry point {e:08x}, {c:10?}")
    }
}

#[derive(Immutable, IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct LutHeader {
    pub magic: [u8; 4],
    pub chunk_count: u32,
    pub addr_base: u32,
    pub spi_base: u32,
    pub hs0: u32,
    pub hs1: u32,
    _unk1: u32,
    _unk2: u32,
    _r: [u8; 16], // all 0 in sample
    pub chunk_size: u32,
    _unk3: u32,
    pub name: [u8; 8],
}

pub const LUT_HEADER_SIZE: usize = size_of::<LutHeader>();

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct HuffmanModule {
    pub header: LutHeader,
    pub chunks: Vec<u32>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Module {
    Uncompressed(Entry),
    Huffman(Result<(Entry, HuffmanModule), String>),
    Lzma(Result<Entry, String>),
    Unknown(Entry),
}

#[derive(IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct Header {
    name: [u8; 4],
    _pad: [u8; 8],
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[repr(C)]
pub struct Directory {
    pub manifest: Manifest,
    pub header: Header,
    pub modules: Vec<Module>,
    pub offset: usize,
    pub size: usize,
    pub name: String,
}

impl Display for Directory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let n = &self.name;
        let o = self.offset;
        let s = self.size;
        let m = &self.manifest;
        write!(f, "{n} @ {o:08x}, {s} bytes, {m}")
    }
}

impl Directory {
    pub fn new(data: &[u8], offset: usize) -> Result<Self, String> {
        let manifest = match Manifest::new(data) {
            Ok(r) => r,
            Err(e) => return Err(format!("cannot parse Gen 2 directory manifest: {e:?}")),
        };
        let (header, rest) = match Header::read_from_prefix(&manifest.mdata) {
            Ok((h, rest)) => (h, rest),
            Err(e) => return Err(format!("cannot parse Gen 2 directory header: {e:?}")),
        };
        let name = match from_utf8(&header.name) {
            Ok(n) => n.trim_end_matches('\0').to_string(),
            Err(_) => format!("{:02x?}", header.name),
        };

        // Check magic bytes of first entry.
        let m = &rest[..4];
        if !m.eq(MODULE_MAGIC_BYTES) {
            return Err(format!(
                "entry magic not found, got {m:02x?}, wanted {MODULE_MAGIC_BYTES:02x?} ({MODULE_MAGIC})"
            ));
        }
        // Parse the entries themselves.
        let count = manifest.header.entries as usize;
        let r = match Ref::<_, [Entry]>::from_prefix_with_elems(rest, count) {
            Ok((r, _)) => r,
            Err(e) => return Err(format!("cannot parse Gen 2 directory entries: {e}")),
        };
        let entries = r.to_vec();

        // Check for consistency and wrap entries with additional information.
        let modules = entries
            .iter()
            .map(|e| {
                let f = e.flags;
                let c = f.compression();
                let o = e.offset as usize;
                if o + 4 > data.len() {
                    return Module::Unknown(*e);
                }
                let sig = &data[o..o + 4];
                match c {
                    Compression::Huffman => {
                        // A Huffman-encoded module starts with a header and a
                        // lookup-table indexing the chunks.
                        if sig != SIG_LUT_BYTES {
                            return Module::Huffman(Err(format!(
                                "Expected {SIG_LUT_BYTES:02x?} @ {o:08x}, got {sig:02x?}"
                            )));
                        }
                        let header = match LutHeader::read_from_prefix(&data[o..]) {
                            Ok((r, _)) => r,
                            Err(e) => return Module::Huffman(Err(format!("{e:?}"))),
                        };
                        let count = header.chunk_count as usize;
                        let lo = o + LUT_HEADER_SIZE;
                        let l = data.len();
                        if lo > l {
                            return Module::Huffman(Err(format!(
                                "offset {lo:08x} out of bounds ({l})"
                            )));
                        }
                        match Ref::<_, [u32]>::from_prefix_with_elems(&data[lo..], count) {
                            Ok((lut, _)) => {
                                let huff = HuffmanModule {
                                    header,
                                    chunks: lut.to_vec(),
                                };
                                Module::Huffman(Ok((*e, huff)))
                            }
                            Err(e) => Module::Huffman(Err(format!("{e}"))),
                        }
                    }
                    Compression::Lzma => {
                        if sig != SIG_LZMA_BYTES {
                            return Module::Lzma(Err(format!(
                                "Expected {SIG_LZMA_BYTES:02x?} @ {o:08x}, got {sig:02x?}"
                            )));
                        }
                        Module::Lzma(Ok(*e))
                    }
                    Compression::Uncompressed => Module::Uncompressed(*e),
                    Compression::Unknown => Module::Unknown(*e),
                }
            })
            .collect();

        let size = data.len();
        Ok(Self {
            manifest,
            header,
            modules,
            offset,
            size,
            name,
        })
    }

    /// Calculate a new minimum possible offset for relocation to save space.
    ///
    /// As taken from `me_cleaner`:
    /// The two bytes in the middle of `addr_base` are added to `spi_base` to
    /// compute the final start of the LUT. However, `addr_base` is not
    /// modifiable, so act only on `spi_base`.
    pub fn calc_new_offset(&self, min_offset: u32) -> Result<u32, String> {
        if let Some((offset, m)) = self.get_huffman_mod() {
            let b = (m.header.addr_base & 0x00ff_ffff) >> 8;
            let lut_offset = (offset + LUT_HEADER_SIZE) as u32;
            let mx = min_offset.max(b - lut_offset);
            // NOTE: Must be aligned to 0x20.
            return Ok(mx.next_multiple_of(0x20));
        }

        Err("new offset cannot be calculated".into())
    }

    fn dump_ranges(ranges: &[Range<usize>]) {
        let group_size = 4;
        for (i, r) in ranges.iter().enumerate() {
            if i % group_size == group_size - 1 {
                println!("{r:08x?}");
            } else {
                print!("{r:08x?} ");
            }
        }
        println!();
    }

    /// Get the offset ranges of the chunks.
    fn chunks_as_ranges(&self, chunks: &[u32], stream_end: usize) -> Vec<Range<usize>> {
        // NOTE: This is the end of the directory.
        // me_cleaner uses the end of the ME region.
        let dir_end = self.offset + self.size;
        let mut nonzero_offsets = vec![stream_end];
        let offsets = chunks
            .iter()
            .map(|c| {
                let o = *c as usize;
                if (o >> 24) as u8 == CHUNK_INACTIVE {
                    0
                } else {
                    let xo = o & CHUNK_OFFSET_MASK as usize;
                    if xo != 0 {
                        nonzero_offsets.push(xo);
                    }
                    xo
                }
            })
            .collect::<Vec<usize>>();
        // This should move dir_end to the end.
        nonzero_offsets.sort();
        // Turn offsets into ranges by finding the offset of the next chunk.
        offsets
            .iter()
            .map(|offset| {
                let o = *offset;
                let e = if o != 0 {
                    // NOTE: nonzero_offsets are a subset of offsets, so this should never fail.
                    #[allow(clippy::unwrap_used)]
                    let p = nonzero_offsets.iter().position(|e| *e == o).unwrap();
                    let next = p + 1;
                    // The last entry potentially has no successor. It would be
                    // dir_end, which is the initial entry in nonzero_offsets.
                    if next < nonzero_offsets.len() {
                        nonzero_offsets[next]
                    } else {
                        stream_end.min(dir_end)
                    }
                } else {
                    0
                };
                o..e
            })
            .collect::<Vec<Range<usize>>>()
    }

    /// Rebase all Huffman chunks based on the offset difference.
    ///
    /// Use [`calc_new_offset`] to find a suitable offset.
    ///
    /// [`calc_new_offset`]: Directory::calc_new_offset
    pub fn rebase_huffman_chunks(&mut self, offset_diff: u32) -> Result<(), String> {
        if let Some(Module::Huffman(Ok((_, h)))) = self
            .modules
            .iter_mut()
            .find(|m| matches!(m, Module::Huffman(Ok(_))))
        {
            h.header.spi_base -= offset_diff;
            h.header.hs1 -= offset_diff;
            for c in &mut h.chunks {
                // The 3 low bytes are the actual offset.
                // An inactive chunk must not be changed and has offset 0.
                if (*c >> 24) as u8 == CHUNK_INACTIVE {
                    //
                } else {
                    *c -= offset_diff;
                }
            }
            return Ok(());
        }
        Err("no Huffman chunks found".into())
    }

    /// Find a Huffman module and its offset within the Directory.
    ///
    /// Note that all Huffman modules share the same lookup-table to define
    /// their chunks, so finding any such module suffices to obtain the offset
    /// and the LUT itself through the module header.
    pub fn get_huffman_mod(&self) -> Option<(usize, &HuffmanModule)> {
        if let Some(Module::Huffman(Ok((e, h)))) = self
            .modules
            .iter()
            .find(|m| matches!(m, Module::Huffman(Ok(_))))
        {
            let lut_header_offset = e.offset as usize;
            return Some((lut_header_offset, h));
        }
        None
    }
}

impl Removables for Directory {
    /// Removable ranges relative to the start of the Directory
    fn removables(&self, retention_list: &[String]) -> Vec<Range<usize>> {
        use log::{debug, info, warn};
        let debug = false;
        let mut removables = vec![];

        let mut unremovable_chunks = vec![];
        let mut all_chunks = vec![];
        let dir_offset = self.offset;

        for m in &self.modules {
            // Get the full directory entry.
            let e = match m {
                Module::Uncompressed(m) => m,
                Module::Lzma(Ok(m)) => m,
                Module::Huffman(Ok((m, h))) => {
                    let n = m.name();
                    let o = m.offset as usize;
                    let s = m.size as usize;

                    // NOTE: The header is always the same, since multiple
                    // Huffman-encoded modules point to the same offset.
                    let cs = h.header.chunk_size;
                    if all_chunks.is_empty() {
                        info!("Huffman chunk size: {cs}");
                        let stream_end = (h.header.hs0 + h.header.hs1) as usize;
                        all_chunks = self.chunks_as_ranges(&h.chunks, stream_end);
                    }

                    const CHUNK_OFFSET: u32 = 0x10000000;
                    // Each module occupies its own range of chunks.
                    let b = m.mod_base - (h.header.addr_base + CHUNK_OFFSET);
                    let c = (m.code_size / cs) as usize;
                    let first_chunk = (b / cs) as usize;
                    let last_chunk = first_chunk + c;
                    info!("Huffman compressed {n} @ {o:08x} ({s} bytes)");
                    let a = if retention_list.contains(&n) {
                        for o in &all_chunks[first_chunk..=last_chunk] {
                            if o.start != 0 {
                                unremovable_chunks.push(o.clone());
                            }
                        }
                        "retained"
                    } else {
                        "removed"
                    };
                    info!("  Chunks {first_chunk}..{last_chunk} will be {a}");
                    continue;
                }
                Module::Lzma(Err(e)) | Module::Huffman(Err(e)) => {
                    warn!("Compressed module could not be parsed: {e}, skipping");
                    continue;
                }
                Module::Unknown(m) => {
                    let n = m.name();
                    let o = m.offset;
                    let s = m.size;
                    info!("Unknown module {n} @ {o:08x} ({s} bytes)");
                    continue;
                }
            };
            let n = e.name();
            let o = e.offset as usize;
            let s = e.size as usize;

            match &n {
                n if retention_list.contains(n) => {
                    info!("Retain {n} @ {o:08x} ({s} bytes)");
                }
                n => {
                    info!("Remove {n} @ {o:08x} ({s} bytes)");
                    removables.push(o..o + s);
                }
            }
        }

        let mut r = 0;
        for c in &all_chunks {
            let mut remove = true;
            // Filter out chunks that must be kept: those in range of some unremovable chunk
            // TODO: Simplify when Range.is_overlapping is stabiliized (currently nightly),
            // https://doc.rust-lang.org/core/slice/trait.GetDisjointMutIndex.html#tymethod.is_overlapping
            for u in &unremovable_chunks {
                if (u.contains(&c.start)) || (u.contains(&(c.end - 1))) {
                    debug!("OVERLAP: {u:06x?} (partially) contains {c:06x?}");
                    remove = false;
                    break;
                }
            }
            if remove && c.start < c.end {
                // NOTE: Chunks are relative to the start of the ME region (or FPT?).
                // We provide the offset relative to the directory.
                let o = c.start - dir_offset;
                let e = c.end - dir_offset;
                removables.push(o..e);
                r += 1;
            } else if debug && c.start != 0 {
                debug!("KEEP {c:06x?}");
            }
        }

        let a = &all_chunks.len();
        let u = unremovable_chunks.len();
        info!("Total chunks:   {a}");
        info!("   unremovable: {u}");
        info!("   will remove: {r}");

        if debug {
            debug!("== All chunks");
            Self::dump_ranges(&all_chunks);
            debug!("== Unremovable chunks");
            Self::dump_ranges(&unremovable_chunks);
            debug!("== All removables");
            Self::dump_ranges(&removables);
        }

        removables
    }
}