2 # Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 # SPDX-License-Identifier: BSD-3-Clause
10 """A utility to generate ipq8064 uber SBL..
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
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.
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.
26 The input files are concatenated together aligned at 256 byte boundary offset
27 from the combined header. See Usage() below for more details.
29 The resulting uber SBL file is prepended by the same combined header adjusted
30 to reflect the new total file size.
33 DEFAULT_OUTPUT_FILE_NAME
= 'sbl-ro.mbn'
36 """Object representing the uber SBL."""
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
44 def __init__(self
, sbl1
, verbose
):
45 """Initialize the object and verify the first file in the sequence.
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.
52 self
.verbose
= verbose
53 self
.mbn_file_names
= []
55 print('Reading ' + sbl1
)
58 self
.sbl1
= open(sbl1
, 'rb').read()
60 print('I/O error({0}): {1}'.format(e
.errno
, e
.strerror
))
63 (codeword
, magic
, _
) = struct
.unpack_from(
64 self
.NOR_SBL1_HEADER
, self
.sbl1
)
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
))
72 if magic
!= self
.MAGIC_NUM
:
73 print('\n\nError: Unexpected Magic!')
74 print('Magic : ' + ('0x%x' % self
.MAGIC_NUM
) + \
75 ' != ' + ('0x%x' % magic
))
78 def Append(self
, src
):
79 """Add a file to the list of files to be concatenated"""
80 self
.mbn_file_names
.append(src
)
82 def PadOutput(self
, outfile
, size
):
83 """Pad output file to the required alignment.
85 Adds 0xff to the passed in file to get its size to the ALIGNMENT
89 outfile: file handle of the file to be padded
90 size: int, current size of the file
92 Returns number of bytes in the added padding.
96 overflow
= size
% self
.ALIGNMENT
98 pad_size
= self
.ALIGNMENT
- overflow
99 pad
= b
'\377' * pad_size
102 print('Added %d byte padding' % pad_size
)
106 def Create(self
, out_file_name
):
107 """Create the uber SBL.
109 Concatenate input files with the appropriate padding and update the
110 combined header to reflect the new blob size.
113 out_file_name: string, name of the file to save the generated uber
116 outfile
= open(out_file_name
, 'wb')
117 total_size
= len(self
.sbl1
) - self
.NOR_SBL1_HEADER_SZ
118 outfile
.write(self
.sbl1
)
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
)
125 print('Added %s (%d bytes)' % (mbn_file_name
,
127 total_size
+= len(mbn_file_data
)
130 outfile
.write(struct
.pack('<I', total_size
))
131 outfile
.write(struct
.pack('<I', total_size
))
135 print('%s: [-v] [-h] [-o Output MBN] sbl1 sbl2 [bootblock]' % (
136 os
.path
.basename(sys
.argv
[0])))
138 print('Concatenates up to three mbn files: two SBLs and a coreboot bootblock')
139 print(' -h This message')
141 print(' -o Output file name, (default: %s)\n' % DEFAULT_OUTPUT_FILE_NAME
)
146 mbn_output
= DEFAULT_OUTPUT_FILE_NAME
149 while i
< (len(sys
.argv
) - 1):
151 if (sys
.argv
[i
] == '-h'):
152 Usage(0) # doesn't return
154 if (sys
.argv
[i
] == '-o'):
155 mbn_output
= sys
.argv
[i
+ 1]
159 if (sys
.argv
[i
] == '-v'):
166 if len(argv
) < 2 or len(argv
) > 3:
169 nsbl
= NorSbl(argv
[0], verbose
)
171 for mbnf
in argv
[1:]:
174 nsbl
.Create(mbn_output
)
176 if __name__
== '__main__':