3 # SPDX-License-Identifier: BSD-3-Clause
9 PROG_NAME
= os
.path
.basename(sys
.argv
[0])
11 def create_header(base
, size
):
12 """Returns a packed MBN header image with the specified base and size.
14 @arg base: integer, specifies the image load address in RAM
15 @arg size: integer, specifies the size of the image
16 @returns: string, the MBN header
19 # SBLs require size to be 4 bytes aligned.
20 size
= (size
+ 3) & 0xfffffffc
22 # We currently do not support appending certificates. Signing GPL
23 # code might violate the GPL. So U-Boot will never be signed. So
24 # this is not required for U-Boot.
29 0x0, # Image source pointer
30 base
, # Image destination pointer
31 size
, # Code Size + Cert Size + Signature Size
33 base
+ size
, # Destination + Code Size
35 base
+ size
, # Destination + Code Size + Signature Size
39 header_packed
= struct
.pack('<10I', *header
)
42 def mkheader(base_addr
, infname
, outfname
):
43 """Prepends the image with the MBN header.
45 @arg base_addr: integer, specifies the image load address in RAM
46 @arg infname: string, image filename
47 @arg outfname: string, output image with header prepended
48 @raises IOError: if reading/writing input/output file fails
50 with
open(infname
, "rb") as infp
:
54 if base_addr
> 0xFFFFFFFF:
55 raise ValueError("invalid base address")
57 if base_addr
+ insize
> 0xFFFFFFFF:
58 raise ValueError("invalid destination range")
60 header
= create_header(base_addr
, insize
)
61 with
open(outfname
, "wb") as outfp
:
66 """Print command usage.
68 @arg msg: string, error message if any (default: None)
71 sys
.stderr
.write("%s: %s\n" % (PROG_NAME
, msg
))
73 print("Usage: %s <base-addr> <input-file> <output-file>" % PROG_NAME
)
79 """Main entry function"""
81 if len(sys
.argv
) != 4:
82 usage("incorrect number of arguments")
85 base_addr
= int(sys
.argv
[1], 0)
87 outfname
= sys
.argv
[3]
88 except ValueError as e
:
89 sys
.stderr
.write("mkheader: invalid base address '%s'\n" % sys
.argv
[1])
93 mkheader(base_addr
, infname
, outfname
)
95 sys
.stderr
.write("%s: %s\n" % (PROG_NAME
, e
))
97 except ValueError as e
:
98 sys
.stderr
.write("%s: %s\n" % (PROG_NAME
, e
))
101 if __name__
== "__main__":