Rob Barnes has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/76188?usp=email )
Change subject: util/apcb: Add apcb edit tool for phoenix ......................................................................
util/apcb: Add apcb edit tool for phoenix
Add a new apcb edit tool, apcb_v3a_edit.py, that injects SPDs into an APCB for phoenix platform.
The tool makes several assumptions: * Each SPD only uses blocks 0, 1, 3 and 5. All other blocks are zero. * Each block is 64 bytes. * Dimm and socket are always 0 * Unused SPD entries are zero'd
BUG=b:281983434 BRANCH=None TEST=build, flash, boot myst
Change-Id: Ifb50287de77138170714a702ab87d56427aacfef Signed-off-by: Rob Barnes robbarnes@google.com --- M Makefile.inc M src/mainboard/google/myst/Makefile.inc A util/apcb/apcb_v3a_edit.py 3 files changed, 159 insertions(+), 17 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/88/76188/1
diff --git a/Makefile.inc b/Makefile.inc index b35ec86..a065094 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -613,6 +613,8 @@
APCB_V3_EDIT_TOOL:=$(top)/util/apcb/apcb_v3_edit.py
+APCB_V3A_EDIT_TOOL:=$(top)/util/apcb/apcb_v3a_edit.py + CBOOTIMAGE:=$(objutil)/cbootimage/cbootimage
FUTILITY?=$(objutil)/futility/futility diff --git a/src/mainboard/google/myst/Makefile.inc b/src/mainboard/google/myst/Makefile.inc index 54d4f30..d5a9c34 100644 --- a/src/mainboard/google/myst/Makefile.inc +++ b/src/mainboard/google/myst/Makefile.inc @@ -26,23 +26,23 @@ # Add the below section back in after the apcbtool is updated to handle the # Phoenix APCB SPD configuration.
-#ifneq ($(wildcard $(src)/mainboard/$(MAINBOARDDIR)/variants/$(VARIANT_DIR)/memory/Makefile.inc),) -# -#LIB_SPD_DEPS = $(SPD_SOURCES) -# -#APCB_SOURCES = $(obj)/$(APCB_NAME).gen -# -#$(obj)/$(APCB_NAME).gen: $(SPD_SOURCES) \ -# $(APCB_V3_EDIT_TOOL) \ -# $(MAINBOARD_BLOBS_DIR)/$(APCB_NAME).bin -# $(APCB_V3_EDIT_TOOL) $(MAINBOARD_BLOBS_DIR)/$(APCB_NAME).bin \ -# $(obj)/$(APCB_NAME).gen \ -# --spd_sources $(SPD_SOURCES) \ -# --mem_type 'lp5' -#else -#$(info SPD sources not found. Skipping APCB.) -#files_added:: die_no_apcb -#endif +ifneq ($(wildcard $(src)/mainboard/$(MAINBOARDDIR)/variants/$(VARIANT_DIR)/memory/Makefile.inc),) + +LIB_SPD_DEPS = $(SPD_SOURCES) + +APCB_SOURCES = $(obj)/$(APCB_NAME).gen + +$(obj)/$(APCB_NAME).gen: $(SPD_SOURCES) \ + $(APCB_V3A_EDIT_TOOL) \ + $(MAINBOARD_BLOBS_DIR)/$(APCB_NAME).bin + $(APCB_V3A_EDIT_TOOL) $(MAINBOARD_BLOBS_DIR)/$(APCB_NAME).bin \ + $(obj)/$(APCB_NAME).gen \ + --spd_sources $(SPD_SOURCES) \ + --mem_type 'lp5' +else +$(info SPD sources not found. Skipping APCB.) +files_added:: die_no_apcb +endif
else $(info APCB sources not found. Skipping APCB.) diff --git a/util/apcb/apcb_v3a_edit.py b/util/apcb/apcb_v3a_edit.py new file mode 100755 index 0000000..e437b95 --- /dev/null +++ b/util/apcb/apcb_v3a_edit.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 + +# Script for editing APCB_v3a binaries, such as injecting SPDs. + +import re +import argparse +from collections import namedtuple +from struct import * + +APCB_CHECKSUM_OFFSET = 16 +SPD_ENTRY_MAGIC = bytes.fromhex('0200480000000000') +SPD_SIZE = 512 +EMPTY_SPD = b'\x00' * SPD_SIZE +ZERO_BLOCKS = (2, 4, 6, 7) +BLOCK_SIZE = 64 + +spd_block_header_fmt = '<HHHH' +spd_block_header = namedtuple( + 'spd_block_header', 'Type, Length, Key, Reserved') + +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') + parser.add_argument( + '--mem_type', + type=str, + default='lp5', + help='Memory type [lp5|lp5x]. Default = lp5') + 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() + + # Load input APCB + print(f'Reading input APCB from {args.apcb_in}') + with open(args.apcb_in, 'rb') as f: + apcb = f.read() + assert chksum(apcb) == apcb[APCB_CHECKSUM_OFFSET], 'Initial checksum is invalid' + orig_apcb_len = len(apcb) + + # Load SPDs + print(f'Using SPD Sources = {", ".join(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) == SPD_SIZE, f'{spd_source} is not {SPD_SIZE} bytes' + # Verify ZERO_BLOCKS are zero + for b in ZERO_BLOCKS: + assert all(v==0 for v in spd_data[b*BLOCK_SIZE:(b+1)*BLOCK_SIZE]), f'SPD block #{b} is not zero' + spds.append(spd_data) + assert len(spds) > 0, "No SPDs provided" + + # Inject SPDs into APCB + offset = 0 + spd_idx = 0 + while True: + offset = apcb.find(SPD_ENTRY_MAGIC, offset) + if offset < 0: + print(f'No more SPD entries found') + assert spd_idx >= len(spds), f'Not enough SPD entries in APCB. Need {len(spds)}, found {spd_idx}' + break + + if spd_idx < len(spds): + print(f'Injecting SPD instance {spd_idx}') + spd = spds[spd_idx] + else: + print(f'Injecting empty SPD for instance {spd_idx}') + spd = EMPTY_SPD + + # Inject SPD blocks + for b in range(int(SPD_SIZE/BLOCK_SIZE)): + if b in ZERO_BLOCKS: continue + header_size = calcsize(spd_block_header_fmt) + header_data = apcb[offset:offset + header_size] + header = spd_block_header._make(unpack(spd_block_header_fmt, header_data)) + socket = (header.Key >> 12) & 0xF + channel = (header.Key >> 8) & 0xF + dimm = (header.Key >> 4) & 0xF + block = (header.Key >> 0) & 0xF + + assert header.Type == 2 + assert header.Length == header_size + BLOCK_SIZE + assert socket == 0 + assert channel == 0 + assert block == b + assert dimm == 0 + assert header.Reserved == 0 + offset += header_size + + spd_block = spd[b*BLOCK_SIZE:(b+1)*BLOCK_SIZE] + assert len(spd_block) == BLOCK_SIZE + + # Verify that APCB size didn't change + apcb_len = len(apcb) + apcb = inject(apcb, spd_block, offset) + assert len(apcb) == apcb_len + + offset += BLOCK_SIZE + + spd_idx += 1 + + # Fix APCB checksum + print(f'Fixing APCB checksum') + apcb = inject(apcb, bytes([chksum(apcb)]), APCB_CHECKSUM_OFFSET) + assert chksum(apcb) == apcb[APCB_CHECKSUM_OFFSET], 'Final checksum is invalid' + assert orig_apcb_len == len(apcb), \ + 'The size of the APCB changed.' + + # Write APCB to file + print(f'Writing {len(apcb)} byte APCB to {args.apcb_out}') + with open(args.apcb_out, 'wb') as f: + f.write(apcb) + + +if __name__ == "__main__": + main()