From f26ce9f00e79a73631ec631ae89a5763284a3bb3 Mon Sep 17 00:00:00 2001 From: Rob Barnes Date: Tue, 21 Dec 2021 07:19:12 -0700 Subject: 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 Signed-off-by: Rob Barnes Reviewed-on: https://review.coreboot.org/c/coreboot/+/60281 Tested-by: build bot (Jenkins) Reviewed-by: Raul Rangel --- Documentation/util.md | 2 + util/apcb/apcb_v3_edit.py | 156 ++++++++++++++++++++++++++++++++++++++++++++++ util/apcb/description.md | 3 + 3 files changed, 161 insertions(+) create mode 100755 util/apcb/apcb_v3_edit.py diff --git a/Documentation/util.md b/Documentation/util.md index 8c6bcb7fca..b618dccc44 100644 --- a/Documentation/util.md +++ b/Documentation/util.md @@ -12,6 +12,8 @@ settings. `Perl` * __apcb__ - AMD PSP Control Block tools * _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 specific SPDs. `Python3` * __archive__ - Concatenate files and create an archive `C` * __autoport__ - Automated porting coreboot to Sandy Bridge/Ivy Bridge platforms `Go` diff --git a/util/apcb/apcb_v3_edit.py b/util/apcb/apcb_v3_edit.py new file mode 100755 index 0000000000..9a70bace3e --- /dev/null +++ b/util/apcb/apcb_v3_edit.py @@ -0,0 +1,156 @@ +#!/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 matches the expected SPD header: +# Byte 0 = 0x23 = 512 bytes total / 384 bytes used +# Byte 1 = 0x11 = Revision 1.1 +# Byte 2 = 0x11 = LPDDR4X SDRAM +# Byte 3 = 0x0E = Non-DIMM Solution +SPD_MAGIC = bytes.fromhex('2311110E') +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 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') + 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(f'Reading input APCB from {args.apcb_in}') + + with open(args.apcb_in, 'rb') as f: + apcb = f.read() + + orig_apcb_len = len(apcb) + + assert chksum(apcb) == apcb[16], f'ERROR: {args.apcb_in} checksum is invalid' + + print(f'Using SPD Sources = {args.spd_sources}') + + spds = [] + for spd_source in args.spd_sources: + with open(spd_source, 'rb') as f: + spd_data = bytes.fromhex(re.sub(r'\s+', '', f.read().decode())) + assert(len(spd_data) == 512), f'ERROR: {spd_source} not 512 bytes' + 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 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, \ + 'ERROR: Unexpected dimm number found in APCB' + assert spd_ssp.ChannelNumber >= 0 and spd_ssp.ChannelNumber <= 1, \ + 'ERROR: Unexpected channel number found in APCB' + + print(f'Found SPD instance {instance} with channel {spd_ssp.ChannelNumber} ' + f'and dimm {spd_ssp.DimmNumber} at offset {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(f'Enabling channel {spd_ssp.ChannelNumber}, ' + f'dimm {spd_ssp.DimmNumber} and injecting SPD') + spd_ssp = spd_ssp._replace(SpdValid=True, DimmPresent=True) + spd = spds[instance] + else: + print(f'Disabling channel {spd_ssp.ChannelNumber}, ' + f'dimm {spd_ssp.DimmNumber} and clearing SPD') + spd_ssp = spd_ssp._replace(SpdValid=False, DimmPresent=False) + spd = EMPTY_SPD + + assert len(spd) == 512, f'ERROR: Expected SPD to be 512 bytes, got {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 + + assert instance >= len(spds), \ + f'ERROR: Not enough SPD slots in APCB, found {instance}, need {len(spds)}' + + print(f'Fixing checksum and writing to {args.apcb_out}') + + apcb = inject(apcb, bytes([chksum(apcb)]), 16) + + assert chksum(apcb) == apcb[16], 'ERROR: Final checksum is invalid' + assert orig_apcb_len == len(apcb), \ + 'ERROR: The size of the APCB binary changed.' + + print(f'Writing {len(apcb)} bytes to {args.apcb_out}') + + with open(args.apcb_out, 'wb') as f: + f.write(apcb) + + +if __name__ == "__main__": + main() diff --git a/util/apcb/description.md b/util/apcb/description.md index 674243ac2d..46fd4281e4 100644 --- a/util/apcb/description.md +++ b/util/apcb/description.md @@ -2,3 +2,6 @@ AMD PSP Control Block tools * _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` -- cgit v1.2.3