3 # Copyright (c) 2013 The Linux Foundation. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation
13 # and/or other materials provided with the distribution.
15 # 3. Neither the name of the copyright holder nor the names of its
16 # contributors may be used to endorse or promote products derived from this
17 # software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
36 PROG_NAME
= os
.path
.basename(sys
.argv
[0])
38 def create_header(base
, size
):
39 """Returns a packed MBN header image with the specified base and size.
41 @arg base: integer, specifies the image load address in RAM
42 @arg size: integer, specifies the size of the image
43 @returns: string, the MBN header
46 # SBLs require size to be 4 bytes aligned.
47 size
= (size
+ 3) & 0xfffffffc
49 # We currently do not support appending certificates. Signing GPL
50 # code might violate the GPL. So U-Boot will never be signed. So
51 # this is not required for U-Boot.
56 0x0, # Image source pointer
57 base
, # Image destination pointer
58 size
, # Code Size + Cert Size + Signature Size
60 base
+ size
, # Destination + Code Size
62 base
+ size
, # Destination + Code Size + Signature Size
66 header_packed
= struct
.pack('<10I', *header
)
69 def mkheader(base_addr
, infname
, outfname
):
70 """Prepends the image with the MBN header.
72 @arg base_addr: integer, specifies the image load address in RAM
73 @arg infname: string, image filename
74 @arg outfname: string, output image with header prepended
75 @raises IOError: if reading/writing input/output file fails
77 with
open(infname
, "rb") as infp
:
81 if base_addr
> 0xFFFFFFFF:
82 raise ValueError("invalid base address")
84 if base_addr
+ insize
> 0xFFFFFFFF:
85 raise ValueError("invalid destination range")
87 header
= create_header(base_addr
, insize
)
88 with
open(outfname
, "wb") as outfp
:
93 """Print command usage.
95 @arg msg: string, error message if any (default: None)
98 sys
.stderr
.write("%s: %s\n" % (PROG_NAME
, msg
))
100 print "Usage: %s <base-addr> <input-file> <output-file>" % PROG_NAME
106 """Main entry function"""
108 if len(sys
.argv
) != 4:
109 usage("incorrect number of arguments")
112 base_addr
= int(sys
.argv
[1], 0)
113 infname
= sys
.argv
[2]
114 outfname
= sys
.argv
[3]
115 except ValueError as e
:
116 sys
.stderr
.write("mkheader: invalid base address '%s'\n" % sys
.argv
[1])
120 mkheader(base_addr
, infname
, outfname
)
122 sys
.stderr
.write("%s: %s\n" % (PROG_NAME
, e
))
124 except ValueError as e
:
125 sys
.stderr
.write("%s: %s\n" % (PROG_NAME
, e
))
128 if __name__
== "__main__":