Rob Barnes has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/60281 )
Change subject: util/apcb: Add apcb_v3_edit tool ......................................................................
util/apcb: Add apcb_v3_edit tool
apcb_v3_edit.py tool edits APCB V3 binaries. Specifically it will inject up to 16 SPDs into an existing APCB. The APCB must have a magic number at the top of each SPD slot.
BUG=b:209486191 BRANCH=None TEST=Inject 4 SPDs into magic APCB, boot guybrush with modified APCB
Change-Id: I9148977c415df41210a3a13a1cd9b3bc1504a480 --- A util/apcb/apcb_v3_edit.py M util/apcb/description.md 2 files changed, 157 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/81/60281/1
diff --git a/util/apcb/apcb_v3_edit.py b/util/apcb/apcb_v3_edit.py new file mode 100755 index 0000000..c243a9b --- /dev/null +++ b/util/apcb/apcb_v3_edit.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python3 + +# Script for editing APCB_V3 binaries, such as injecting SPDs. + +import sys +import re +import argparse +from collections import namedtuple +from struct import * +import binascii +import os + +SPD_MAGIC = bytes.fromhex('f005ba110000') +EMPTY_SPD = b'\x00' * 512 + +spd_ssp_struct_fmt = '??B?IIBBBxIIBBBx' +spd_ssp_struct = namedtuple( + 'spd_ssp_struct', 'SpdValid, DimmPresent, \ + PageAddress, NvDimmPresent, \ + DramManufacturersIDCode, Address, \ + SpdMuxPresent, MuxI2CAddress, MuxChannel, \ + Technology, Package, SocketNumber, \ + ChannelNumber, DimmNumber') + +apcb_v3_header_fmt = 'HHHHBBBBBBH' +apcb_v3_header = namedtuple( + 'apcb_v3_header', 'GroupId, TypeId, SizeOfType, \ + InstanceId, ContextType, ContextFormat, UnitSize, \ + PriorityMask, KeySize, KeyPos, BoardMask') + +def parseargs(): + parser = argparse.ArgumentParser(description='Inject SPDs and SPD GPIO \ + selection pins into APCB binaries') + parser.add_argument( + 'apcb_in', + type=str, + help='APCB input file') + parser.add_argument( + 'apcb_out', + type=str, + help='APCB output file') + parser.add_argument( + '--spd_sources', + nargs='+', + help='List of SPD sources') + parser.add_argument( + '--hex', + action='store_true', + help='SPD input file is hex encoded, binary otherwise') + parser.add_argument( + '--strip_manufacturer_information', + action='store_true', + help='Strip all manufacturer information from SPD') + return parser.parse_args() + + +def chksum(data): + sum = 0 + for b in data[:16] + data[17:]: + sum = (sum + b) & 0xff + return (0x100 - sum) & 0xff + + +def inject(orig, insert, offset): + return b''.join([orig[:offset], insert, orig[offset + len(insert):]]) + + +def main(): + args = parseargs() + + print("Reading input APCB from %s" % (args.apcb_in)) + + apcb = open(args.apcb_in, 'rb').read() + + orig_apcb_len = len(apcb) + + assert chksum(apcb) == apcb[16], "Checksum is invalid" + + spds = [] + for spd_source in args.spd_sources: + spd_data = bytes.fromhex(re.sub(r'\s+', '', open(spd_source, 'rb').read().decode().strip())) + assert(len(spd_data) == 512) + spds.append(spd_data) + + spd_offset = 0 + instance = 0 + while True: + spd_offset = apcb.find(SPD_MAGIC, spd_offset) + if spd_offset < 0: + print('No more SPD magic numbers found in APCB') + break + + spd_ssp_offset = spd_offset - calcsize(spd_ssp_struct_fmt) + spd_ssp_bytes = apcb[spd_ssp_offset:spd_offset] + spd_ssp = spd_ssp_struct._make( + unpack(spd_ssp_struct_fmt, spd_ssp_bytes)) + + assert spd_ssp.DimmNumber >= 0 and spd_ssp.DimmNumber <= 1, \ + "Unexpected dimm number found in APCB" + assert spd_ssp.ChannelNumber >= 0 and spd_ssp.ChannelNumber <= 1, \ + "Unexpected channel number found in APCB" + + print("Found SPD instance %d with channel %d and dimm %d " + "at offset 0x%x" % (instance, spd_ssp.ChannelNumber, + spd_ssp.DimmNumber, spd_offset)) + + # APCB V3 header is above first channel 0 entry + if spd_ssp.ChannelNumber == 0: + apcb_v3_header_offset = spd_ssp_offset - \ + calcsize(apcb_v3_header_fmt) - 4 + apcb_v3_header_bytes = apcb[apcb_v3_header_offset: + apcb_v3_header_offset + calcsize(apcb_v3_header_fmt)] + apcb_v3 = apcb_v3_header._make( + unpack(apcb_v3_header_fmt, apcb_v3_header_bytes)) + apcb_v3 = apcb_v3._replace(BoardMask=(1 << instance)) + + if instance < len(spds): + print("SpdValid %d, DimmPresent %d" % + (spd_ssp.SpdValid, spd_ssp.DimmPresent)) + print("Enabling channel %d, dimm %d and injecting SPD" % + (spd_ssp.ChannelNumber, spd_ssp.DimmNumber)) + spd_ssp = spd_ssp._replace(SpdValid=True, DimmPresent=True) + spd = spds[instance] + else: + print("Disabling channel %d, dimm %d and clearing SPD" % + (spd_ssp.ChannelNumber, spd_ssp.DimmNumber)) + spd_ssp = spd_ssp._replace(SpdValid=False, DimmPresent=False) + spd = EMPTY_SPD + + assert len(spd) == 512, "Expected SPD to be 512 bytes, got %d" % len(spd) + + apcb = inject(apcb, pack(spd_ssp_struct_fmt, *spd_ssp), spd_ssp_offset) + apcb = inject(apcb, spd, spd_offset) + if spd_ssp.ChannelNumber == 0: + apcb = inject(apcb, pack(apcb_v3_header_fmt, *apcb_v3), apcb_v3_header_offset) + else: + instance += 1 + + spd_offset += 512 + + print("Fixing checksum and writing to %s" % (args.apcb_out)) + + apcb = inject(apcb, bytes([chksum(apcb)]), 16) + + assert chksum(apcb) == apcb[16], "Checksum is invalid" + assert orig_apcb_len == len(apcb), \ + "The size of the APCB binary changed, this should not happen." + + print(f'Writing {len(apcb)} bytes to {args.apcb_out}') + + open(args.apcb_out, 'wb').write(apcb) + +if __name__ == "__main__": + main() diff --git a/util/apcb/description.md b/util/apcb/description.md index 674243a..827d55b 100644 --- a/util/apcb/description.md +++ b/util/apcb/description.md @@ -2,3 +2,6 @@
* _apcb_edit.py_ - This tool allows patching an existing APCB binary with specific SPDs and GPIO selection pins. `Python3` + +* _apcb_v3_edit.py_ - This tool allows patching an existing APCB_v3 binary with + up to 16 specific SPDs. `Python3`