Raul E Rangel | 030d214 | 2020-05-26 15:11:47 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 2 | |
| 3 | # Script for editing APCB binaries, such as injecting SPDs and GPIO |
| 4 | # configurations. |
| 5 | |
| 6 | import sys |
| 7 | import re |
| 8 | import argparse |
| 9 | from collections import namedtuple |
| 10 | from struct import * |
| 11 | |
| 12 | GPIO_MAGIC = bytes.fromhex('fadeddad' * 3) |
| 13 | SPD_MAGIC = bytes.fromhex('f005ba110000') |
| 14 | EMPTY_SPD = b'\x00' * 512 |
| 15 | |
| 16 | spd_ssp_struct_fmt = '??B?IIBBBxIIBBBx' |
| 17 | spd_ssp_struct = namedtuple( |
| 18 | 'spd_ssp_struct', 'SpdValid, DimmPresent, \ |
| 19 | PageAddress, NvDimmPresent, \ |
| 20 | DramManufacturersIDCode, Address, \ |
| 21 | SpdMuxPresent, MuxI2CAddress, MuxChannel, \ |
| 22 | Technology, Package, SocketNumber, \ |
| 23 | ChannelNumber, DimmNumber') |
| 24 | |
| 25 | |
| 26 | def parseargs(): |
| 27 | parser = argparse.ArgumentParser(description='Inject SPDs and SPD GPIO \ |
| 28 | selection pins into APCB binaries') |
| 29 | parser.add_argument( |
| 30 | 'apcb_in', |
| 31 | nargs='?', |
| 32 | type=argparse.FileType('rb'), |
| 33 | default=sys.stdin, |
| 34 | help='APCB input file') |
| 35 | parser.add_argument( |
| 36 | 'apcb_out', |
| 37 | nargs='?', |
| 38 | type=argparse.FileType('wb'), |
| 39 | default=sys.stdout, |
| 40 | help='APCB output file') |
| 41 | parser.add_argument( |
| 42 | '--spd_0_0', |
Michael Niewöhner | e10efa3 | 2020-08-25 19:38:14 +0200 | [diff] [blame] | 43 | type=argparse.FileType('rb'), |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 44 | help='SPD input file for channel 0, dimm 0') |
| 45 | parser.add_argument( |
| 46 | '--spd_0_1', |
Michael Niewöhner | e10efa3 | 2020-08-25 19:38:14 +0200 | [diff] [blame] | 47 | type=argparse.FileType('rb'), |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 48 | help='SPD input file for channel 0, dimm 1') |
| 49 | parser.add_argument( |
| 50 | '--spd_1_0', |
Michael Niewöhner | e10efa3 | 2020-08-25 19:38:14 +0200 | [diff] [blame] | 51 | type=argparse.FileType('rb'), |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 52 | help='SPD input file for channel 1, dimm 0') |
| 53 | parser.add_argument( |
| 54 | '--spd_1_1', |
Michael Niewöhner | e10efa3 | 2020-08-25 19:38:14 +0200 | [diff] [blame] | 55 | type=argparse.FileType('rb'), |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 56 | help='SPD input file for channel 1, dimm 1') |
| 57 | parser.add_argument( |
| 58 | '--hex', |
| 59 | action='store_true', |
| 60 | help='SPD input file is hex encoded, binary otherwise') |
| 61 | parser.add_argument( |
Rob Barnes | ce036bd | 2020-07-27 09:27:31 -0600 | [diff] [blame] | 62 | '--strip_manufacturer_information', |
| 63 | action='store_true', |
| 64 | help='Strip all manufacturer information from SPD') |
| 65 | parser.add_argument( |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 66 | '--board_id_gpio0', |
| 67 | type=int, |
| 68 | required=True, |
| 69 | nargs=3, |
| 70 | help='Board ID GPIO 0: NUMBER IO_MUX BANK_CTRL') |
| 71 | parser.add_argument( |
| 72 | '--board_id_gpio1', |
| 73 | type=int, |
| 74 | required=True, |
| 75 | nargs=3, |
| 76 | help='Board ID GPIO 1: NUMBER IO_MUX BANK_CTRL') |
| 77 | parser.add_argument( |
| 78 | '--board_id_gpio2', |
| 79 | type=int, |
| 80 | required=True, |
| 81 | nargs=3, |
| 82 | help='Board ID GPIO 2: NUMBER IO_MUX BANK_CTRL') |
| 83 | parser.add_argument( |
| 84 | '--board_id_gpio3', |
| 85 | type=int, |
| 86 | required=True, |
| 87 | nargs=3, |
| 88 | help='Board ID GPIO 3: NUMBER IO_MUX BANK_CTRL') |
| 89 | return parser.parse_args() |
| 90 | |
| 91 | |
| 92 | def chksum(data): |
| 93 | sum = 0 |
| 94 | for b in data[:16] + data[17:]: |
| 95 | sum = (sum + b) & 0xff |
| 96 | return (0x100 - sum) & 0xff |
| 97 | |
| 98 | |
| 99 | def inject(orig, insert, offset): |
| 100 | return b''.join([orig[:offset], insert, orig[offset + len(insert):]]) |
| 101 | |
| 102 | |
| 103 | def main(): |
| 104 | args = parseargs() |
| 105 | |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 106 | print("Reading input APCB from %s" % (args.apcb_in.name)) |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 107 | |
| 108 | apcb = args.apcb_in.read() |
| 109 | |
| 110 | orig_apcb_len = len(apcb) |
| 111 | |
| 112 | gpio_offset = apcb.find(GPIO_MAGIC) |
| 113 | assert gpio_offset > 0, "GPIO magic number not found" |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 114 | print('GPIO magic number found at offset 0x%x' % gpio_offset) |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 115 | gpio_array = (args.board_id_gpio0 + args.board_id_gpio1 + |
| 116 | args.board_id_gpio2 + args.board_id_gpio3) |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 117 | print('Writing SPD GPIO array %s' % gpio_array) |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 118 | apcb = inject(apcb, pack('BBBBBBBBBBBB', *gpio_array), gpio_offset) |
| 119 | |
| 120 | spd_offset = 0 |
| 121 | while True: |
| 122 | spd_offset = apcb.find(SPD_MAGIC, spd_offset) |
| 123 | if spd_offset < 0: |
| 124 | break |
| 125 | |
| 126 | spd_ssp_offset = spd_offset - calcsize(spd_ssp_struct_fmt) |
| 127 | spd_ssp_bytes = apcb[spd_ssp_offset:spd_offset] |
| 128 | spd_ssp = spd_ssp_struct._make( |
| 129 | unpack(spd_ssp_struct_fmt, spd_ssp_bytes)) |
| 130 | |
| 131 | assert spd_ssp.DimmNumber >= 0 and spd_ssp.DimmNumber <= 1, \ |
| 132 | "Unexpected dimm number found in APCB" |
| 133 | assert spd_ssp.ChannelNumber >= 0 and spd_ssp.ChannelNumber <= 1, \ |
| 134 | "Unexpected channel number found in APCB" |
| 135 | |
| 136 | print("Found SPD magic number with channel %d and dimm %d " |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 137 | "at offset 0x%x" % (spd_ssp.ChannelNumber, spd_ssp.DimmNumber, |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 138 | spd_offset)) |
| 139 | |
| 140 | dimm_channel = (spd_ssp.ChannelNumber, spd_ssp.DimmNumber) |
| 141 | spd = None |
| 142 | if dimm_channel == (0, 0) and args.spd_0_0: |
| 143 | spd = args.spd_0_0.read() |
| 144 | elif dimm_channel == (0, 1) and args.spd_0_1: |
| 145 | spd = args.spd_0_1.read() |
| 146 | elif dimm_channel == (1, 0) and args.spd_1_0: |
| 147 | spd = args.spd_1_0.read() |
| 148 | elif dimm_channel == (1, 1) and args.spd_1_1: |
| 149 | spd = args.spd_1_0.read() |
| 150 | |
| 151 | if spd: |
| 152 | if args.hex: |
Michael Niewöhner | e10efa3 | 2020-08-25 19:38:14 +0200 | [diff] [blame] | 153 | spd = spd.decode() |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 154 | spd = re.sub(r'#.*', '', spd) |
| 155 | spd = re.sub(r'\s+', '', spd) |
| 156 | spd = bytes.fromhex(spd) |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 157 | |
| 158 | assert len(spd) == 512, \ |
| 159 | "Expected SPD to be 512 bytes, got %d" % len(spd) |
| 160 | |
Rob Barnes | ce036bd | 2020-07-27 09:27:31 -0600 | [diff] [blame] | 161 | if args.strip_manufacturer_information: |
| 162 | print("Stripping manufacturer information from SPD") |
| 163 | spd = spd[0:320] + b'\x00'*64 + spd[320+64:] |
| 164 | |
| 165 | assert len(spd) == 512, \ |
| 166 | "Error while stripping SPD manufacurer information" |
| 167 | |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 168 | print("Enabling channel %d, dimm %d and injecting SPD" % |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 169 | (spd_ssp.ChannelNumber, spd_ssp.DimmNumber)) |
| 170 | spd_ssp = spd_ssp._replace(SpdValid=True, DimmPresent=True) |
| 171 | |
| 172 | else: |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 173 | print("Disabling channel %d, dimm %d and clearing SPD" % |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 174 | (spd_ssp.ChannelNumber, spd_ssp.DimmNumber)) |
| 175 | spd_ssp = spd_ssp._replace(SpdValid=False, DimmPresent=False) |
| 176 | spd = EMPTY_SPD |
| 177 | |
| 178 | apcb = inject(apcb, pack(spd_ssp_struct_fmt, *spd_ssp), spd_ssp_offset) |
| 179 | apcb = inject(apcb, spd, spd_offset) |
| 180 | |
| 181 | spd_offset += 512 |
| 182 | |
Rob Barnes | 4d2db06 | 2020-07-23 17:25:52 -0600 | [diff] [blame] | 183 | print("Fixing checksum and writing to %s" % (args.apcb_out.name)) |
Rob Barnes | f836a23 | 2020-03-27 01:13:21 -0600 | [diff] [blame] | 184 | |
| 185 | apcb = inject(apcb, bytes([chksum(apcb)]), 16) |
| 186 | |
| 187 | assert chksum(apcb) == apcb[16], "Checksum is invalid" |
| 188 | assert orig_apcb_len == len(apcb), \ |
| 189 | "The size of the APCB binary changed, this should not happen." |
| 190 | |
| 191 | args.apcb_out.write(apcb) |
| 192 | |
| 193 | |
| 194 | if __name__ == "__main__": |
| 195 | main() |