1#!/usr/bin/env python3 2# Copyright (c) 2014, The Linux Foundation. All rights reserved. 3# 4# SPDX-License-Identifier: BSD-3-Clause 5 6import struct 7import sys 8import os 9 10"""A utility to generate ipq8064 uber SBL.. 11 12The very first blob (aka 'uber SBL') read out of NOR SPI flash by the IPQ8064 13maskrom is supposed to be a concatenation of up to three binaries: one to run 14on the RPM, another one to run on the AP, and the third one - the actual 15coreboot bootblock. 16 17The uber SBL starts with the combined header descriptor of 80 bytes, with the 18first two 4 byte words set to certain values, and the total size of the 19payload saved at offsets 28 and 32. 20 21To generate the uber SBL this utility expects two or three input file names in 22the command line, the first file including the described header, and the 23following one(s) - in QCA MBN format. This allows to create the uber SBL in 24one or two invocations. 25 26The input files are concatenated together aligned at 256 byte boundary offset 27from the combined header. See Usage() below for more details. 28 29The resulting uber SBL file is prepended by the same combined header adjusted 30to reflect the new total file size. 31""" 32 33DEFAULT_OUTPUT_FILE_NAME = 'sbl-ro.mbn' 34 35class 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: 55 print('Reading ' + sbl1) 56 57 try: 58 self.sbl1 = open(sbl1, 'rb').read() 59 except IOError as e: 60 print('I/O error({0}): {1}'.format(e.errno, e.strerror)) 61 raise 62 63 (codeword, magic, _) = struct.unpack_from( 64 self.NOR_SBL1_HEADER, self.sbl1) 65 66 if codeword != self.NOR_CODE_WORD: 67 print('\n\nError: Unexpected Codeword!') 68 print('Codeword : ' + ('0x%x' % self.NOR_CODE_WORD) + \ 69 ' != ' + ('0x%x' % codeword)) 70 sys.exit(-1) 71 72 if magic != self.MAGIC_NUM: 73 print('\n\nError: Unexpected Magic!') 74 print('Magic : ' + ('0x%x' % self.MAGIC_NUM) + \ 75 ' != ' + ('0x%x' % magic)) 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 99 pad = b'\377' * pad_size 100 outfile.write(pad) 101 if self.verbose: 102 print('Added %d byte padding' % pad_size) 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) 122 mbn_file_data = open(mbn_file_name, 'rb').read() 123 outfile.write(mbn_file_data) 124 if self.verbose: 125 print('Added %s (%d bytes)' % (mbn_file_name, 126 len(mbn_file_data))) 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 134def Usage(v): 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) 142 sys.exit(v) 143 144def 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 176if __name__ == '__main__': 177 main() 178