From 9bcc002f1e7958c7b4234ea804b5b2b4f675ea3a Mon Sep 17 00:00:00 2001 From: Nicola Corna Date: Mon, 23 Jan 2017 15:28:24 +0100 Subject: util: Add me_cleaner me_cleaner is a tool to strip down Intel ME/TXE images by removing all the non-fundamental code, while keeping the ME/TXE image valid and suitable for booting the system. The remaining code (ROMP and BUP modules) is the one responsible for the very basic initialization of the ME/TXE subsystem and can't be removed. This tool exploits the fact that: * Each ME/TXE partition is signed individually and it is possible to remove both the partition and the signature. * The ME/TXE modules are not signed directly, instead they are hashed and the list of their hashes is hashed again and signed: this means that modifying a module doesn't invalidate the signature, but only the hash of that single module. * The modules hashes are checked only when the corresponding module needs to be executed. * The system can boot after the execution of the first module (BUP, inside the FTPR partition), even if the subsequent stages fail. Currently me_cleaner works on every Intel platform with Intel ME or Intel TXE with the following limitations: * Doesn't work when Intel Boot Guard is set in Verified Boot mode. * Doesn't fully work on Nehalem yet. * On Skylake and later generations, since the partitions' internal structure has changed, me_cleaner leaves intact the FTPR partition, removing all the the other partitions. This tool has been tested on multiple platforms and architectures by different users, and seems to be stable. The reports are available here: https://github.com/corna/me_cleaner/issues/3 A more in-depth description of me_cleaner is available here: https://github.com/corna/me_cleaner/wiki/How-does-it-work%3F Change-Id: I9013799e9adea0dea0775b9afe718de5fc4ca748 Signed-off-by: Nicola Corna Reviewed-on: https://review.coreboot.org/18203 Tested-by: build bot (Jenkins) Reviewed-by: Philipp Deppenwiese Reviewed-by: Stefan Reinauer --- util/me_cleaner/me_cleaner.py | 313 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100755 util/me_cleaner/me_cleaner.py (limited to 'util/me_cleaner/me_cleaner.py') diff --git a/util/me_cleaner/me_cleaner.py b/util/me_cleaner/me_cleaner.py new file mode 100755 index 0000000000..2ad5898213 --- /dev/null +++ b/util/me_cleaner/me_cleaner.py @@ -0,0 +1,313 @@ +#!/usr/bin/python + +# me_cleaner - Tool for partial deblobbing of Intel ME/TXE firmware images +# Copyright (C) 2016, 2017 Nicola Corna +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +import sys +import itertools +import binascii +import hashlib +from struct import pack, unpack + + +unremovable_modules = ("BUP", "ROMP") + + +def get_chunks_offsets(llut, me_start): + chunk_count = unpack("> 4) & 7 + + sys.stdout.write(" {:<16} ({:<7}, ".format(name, comp_str[comp_type])) + + if comp_type == 0x00 or comp_type == 0x02: + sys.stdout.write("0x{:06x} - 0x{:06x}): " + .format(offset, offset + size)) + + if name in unremovable_modules: + print("NOT removed, essential") + else: + fill_range(f, offset, offset + size, b"\xff") + print("removed") + + elif comp_type == 0x01: + sys.stdout.write("fragmented data ): ") + if not chunks_offsets: + f.seek(offset) + llut = f.read(4) + if llut == b"LLUT": + llut += f.read(0x3c) + + chunk_count = unpack(" removable_chunk[0]: + fill_range(f, removable_chunk[0], removable_chunk[1], b"\xff") + + +def check_partition_signature(f, offset): + f.seek(offset) + header = f.read(0x80) + modulus = int(binascii.hexlify(f.read(0x100)[::-1]), 16) + public_exponent = int(binascii.hexlify(f.read(0x4)[::-1]), 16) + signature = int(binascii.hexlify(f.read(0x100)[::-1]), 16) + + header_len = unpack("> 24 & 0x7 + frba = flmap0 >> 12 & 0xff0 + if nr >= 2: + f.seek(frba + 0x8) + flreg2 = unpack("> 4 & 0x1fff000 | 0xfff + + if me_start >= me_end: + sys.exit("The ME/TXE region in this image has been " + "disabled") + + f.seek(me_start + 0x10) + if f.read(4) != b"$FPT": + sys.exit("The ME/TXE region is corrupted or missing") + + print("The ME/TXE region goes from {:#x} to {:#x}" + .format(me_start, me_end)) + else: + sys.exit("This image does not contains a ME/TXE firmware " + "(NR = {})".format(nr)) + else: + sys.exit("Unknown image") + + print("Found FPT header at {:#x}".format(me_start + 0x10)) + + f.seek(me_start + 0x14) + entries = unpack("