T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 2 | # Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 3 | # |
Patrick Georgi | 7051707 | 2020-05-10 18:47:05 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: BSD-3-Clause |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 5 | |
| 6 | import struct |
| 7 | import sys |
| 8 | import os |
| 9 | |
| 10 | """A utility to generate ipq8064 uber SBL.. |
| 11 | |
| 12 | The very first blob (aka 'uber SBL') read out of NOR SPI flash by the IPQ8064 |
| 13 | maskrom is supposed to be a concatenation of up to three binaries: one to run |
| 14 | on the RPM, another one to run on the AP, and the third one - the actual |
| 15 | coreboot bootblock. |
| 16 | |
| 17 | The uber SBL starts with the combined header descriptor of 80 bytes, with the |
| 18 | first two 4 byte words set to certain values, and the total size of the |
| 19 | payload saved at offsets 28 and 32. |
| 20 | |
| 21 | To generate the uber SBL this utility expects two or three input file names in |
| 22 | the command line, the first file including the described header, and the |
| 23 | following one(s) - in QCA MBN format. This allows to create the uber SBL in |
| 24 | one or two invocations. |
| 25 | |
| 26 | The input files are concatenated together aligned at 256 byte boundary offset |
| 27 | from the combined header. See Usage() below for more details. |
| 28 | |
| 29 | The resulting uber SBL file is prepended by the same combined header adjusted |
| 30 | to reflect the new total file size. |
| 31 | """ |
| 32 | |
| 33 | DEFAULT_OUTPUT_FILE_NAME = 'sbl-ro.mbn' |
| 34 | |
| 35 | class NorSbl: |
| 36 | """Object representing the uber SBL.""" |
| 37 | |
| 38 | NOR_SBL1_HEADER = '<II72s' |
| 39 | NOR_SBL1_HEADER_SZ = struct.calcsize(NOR_SBL1_HEADER) |
| 40 | ALIGNMENT = 256 # Make sure this == UBER_SBL_PAD_SIZE |
| 41 | NOR_CODE_WORD = 0x844bdcd1 |
| 42 | MAGIC_NUM = 0x73d71034 |
| 43 | |
| 44 | def __init__(self, sbl1, verbose): |
| 45 | """Initialize the object and verify the first file in the sequence. |
| 46 | |
| 47 | Args: |
| 48 | sbl1: string, the name of the first out of the three input blobs, |
| 49 | must be prepended by the combined header. |
| 50 | verbose: boolean, if True - print debug information on the console. |
| 51 | """ |
| 52 | self.verbose = verbose |
| 53 | self.mbn_file_names = [] |
| 54 | if self.verbose: |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 55 | print('Reading ' + sbl1) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 56 | |
| 57 | try: |
| 58 | self.sbl1 = open(sbl1, 'rb').read() |
| 59 | except IOError as e: |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 60 | print('I/O error({0}): {1}'.format(e.errno, e.strerror)) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 61 | raise |
| 62 | |
| 63 | (codeword, magic, _) = struct.unpack_from( |
| 64 | self.NOR_SBL1_HEADER, self.sbl1) |
| 65 | |
| 66 | if codeword != self.NOR_CODE_WORD: |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 67 | print('\n\nError: Unexpected Codeword!') |
| 68 | print('Codeword : ' + ('0x%x' % self.NOR_CODE_WORD) + \ |
| 69 | ' != ' + ('0x%x' % codeword)) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 70 | sys.exit(-1) |
| 71 | |
| 72 | if magic != self.MAGIC_NUM: |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 73 | print('\n\nError: Unexpected Magic!') |
| 74 | print('Magic : ' + ('0x%x' % self.MAGIC_NUM) + \ |
| 75 | ' != ' + ('0x%x' % magic)) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 76 | sys.exit(-1) |
| 77 | |
| 78 | def Append(self, src): |
| 79 | """Add a file to the list of files to be concatenated""" |
| 80 | self.mbn_file_names.append(src) |
| 81 | |
| 82 | def PadOutput(self, outfile, size): |
| 83 | """Pad output file to the required alignment. |
| 84 | |
| 85 | Adds 0xff to the passed in file to get its size to the ALIGNMENT |
| 86 | boundary. |
| 87 | |
| 88 | Args: |
| 89 | outfile: file handle of the file to be padded |
| 90 | size: int, current size of the file |
| 91 | |
| 92 | Returns number of bytes in the added padding. |
| 93 | """ |
| 94 | |
| 95 | # Is padding needed? |
| 96 | overflow = size % self.ALIGNMENT |
| 97 | if overflow: |
| 98 | pad_size = self.ALIGNMENT - overflow |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 99 | pad = b'\377' * pad_size |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 100 | outfile.write(pad) |
| 101 | if self.verbose: |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 102 | print('Added %d byte padding' % pad_size) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 103 | return pad_size |
| 104 | return 0 |
| 105 | |
| 106 | def Create(self, out_file_name): |
| 107 | """Create the uber SBL. |
| 108 | |
| 109 | Concatenate input files with the appropriate padding and update the |
| 110 | combined header to reflect the new blob size. |
| 111 | |
| 112 | Args: |
| 113 | out_file_name: string, name of the file to save the generated uber |
| 114 | SBL in. |
| 115 | """ |
| 116 | outfile = open(out_file_name, 'wb') |
| 117 | total_size = len(self.sbl1) - self.NOR_SBL1_HEADER_SZ |
| 118 | outfile.write(self.sbl1) |
| 119 | |
| 120 | for mbn_file_name in self.mbn_file_names: |
| 121 | total_size += self.PadOutput(outfile, total_size) |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 122 | mbn_file_data = open(mbn_file_name, 'rb').read() |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 123 | outfile.write(mbn_file_data) |
| 124 | if self.verbose: |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 125 | print('Added %s (%d bytes)' % (mbn_file_name, |
| 126 | len(mbn_file_data))) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 127 | total_size += len(mbn_file_data) |
| 128 | |
| 129 | outfile.seek(28) |
| 130 | outfile.write(struct.pack('<I', total_size)) |
| 131 | outfile.write(struct.pack('<I', total_size)) |
| 132 | |
| 133 | |
| 134 | def Usage(v): |
T Michael Turney | 540b8ec | 2020-01-24 08:42:47 -0800 | [diff] [blame] | 135 | print('%s: [-v] [-h] [-o Output MBN] sbl1 sbl2 [bootblock]' % ( |
| 136 | os.path.basename(sys.argv[0]))) |
| 137 | print() |
| 138 | print('Concatenates up to three mbn files: two SBLs and a coreboot bootblock') |
| 139 | print(' -h This message') |
| 140 | print(' -v verbose') |
| 141 | print(' -o Output file name, (default: %s)\n' % DEFAULT_OUTPUT_FILE_NAME) |
Vadim Bendebury | 5aaa828 | 2014-11-30 13:05:38 -0800 | [diff] [blame] | 142 | sys.exit(v) |
| 143 | |
| 144 | def main(): |
| 145 | verbose = 0 |
| 146 | mbn_output = DEFAULT_OUTPUT_FILE_NAME |
| 147 | i = 0 |
| 148 | |
| 149 | while i < (len(sys.argv) - 1): |
| 150 | i += 1 |
| 151 | if (sys.argv[i] == '-h'): |
| 152 | Usage(0) # doesn't return |
| 153 | |
| 154 | if (sys.argv[i] == '-o'): |
| 155 | mbn_output = sys.argv[i + 1] |
| 156 | i += 1 |
| 157 | continue |
| 158 | |
| 159 | if (sys.argv[i] == '-v'): |
| 160 | verbose = 1 |
| 161 | continue |
| 162 | |
| 163 | break |
| 164 | |
| 165 | argv = sys.argv[i:] |
| 166 | if len(argv) < 2 or len(argv) > 3: |
| 167 | Usage(-1) |
| 168 | |
| 169 | nsbl = NorSbl(argv[0], verbose) |
| 170 | |
| 171 | for mbnf in argv[1:]: |
| 172 | nsbl.Append(mbnf) |
| 173 | |
| 174 | nsbl.Create(mbn_output) |
| 175 | |
| 176 | if __name__ == '__main__': |
| 177 | main() |