mb: Set coreboot as DSDT's manufacturer model ID
[coreboot.git] / util / ipqheader / ipqheader.py
blob761514649980e03b4739959e0f789cc23269a98c
1 #!/usr/bin/env python2
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.
32 import os
33 import struct
34 import sys
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
44 """
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.
53 header = [
54 0x5, # Type: APPSBL
55 0x3, # Version: 3
56 0x0, # Image source pointer
57 base, # Image destination pointer
58 size, # Code Size + Cert Size + Signature Size
59 size, # Code Size
60 base + size, # Destination + Code Size
61 0x0, # Signature Size
62 base + size, # Destination + Code Size + Signature Size
63 0x0, # Cert Size
66 header_packed = struct.pack('<10I', *header)
67 return header_packed
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
76 """
77 with open(infname, "rb") as infp:
78 image = infp.read()
79 insize = len(image)
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:
89 outfp.write(header)
90 outfp.write(image)
92 def usage(msg=None):
93 """Print command usage.
95 @arg msg: string, error message if any (default: None)
96 """
97 if msg != None:
98 sys.stderr.write("%s: %s\n" % (PROG_NAME, msg))
100 print "Usage: %s <base-addr> <input-file> <output-file>" % PROG_NAME
102 if msg != None:
103 exit(1)
105 def main():
106 """Main entry function"""
108 if len(sys.argv) != 4:
109 usage("incorrect number of arguments")
111 try:
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])
117 exit(1)
119 try:
120 mkheader(base_addr, infname, outfname)
121 except IOError as e:
122 sys.stderr.write("%s: %s\n" % (PROG_NAME, e))
123 exit(1)
124 except ValueError as e:
125 sys.stderr.write("%s: %s\n" % (PROG_NAME, e))
126 exit(1)
128 if __name__ == "__main__":
129 main()