blob: 97f76fcdb0da6fdc01988746299b3cd00eb93549 [file] [log] [blame]
Gokul Sriram Palanisamyc5d203b2016-10-06 22:55:20 +05301#
2# Copyright (c) 2014-2016 The Linux Foundation. All rights reserved.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License version 2 and
6# only version 2 as published by the Free Software Foundation.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13
14import sys
15import struct
16
17def create_header(base, size, img_type):
18 """Returns a packed MBN header image with the specified base and size.
19
20 @arg base: integer, specifies the image load address in RAM
21 @arg size: integer, specifies the size of the image
22 @arg img_type: integer, specifies the image type
23 @returns: string, the MBN header
24 """
25
26 header = [
27 img_type, # Type: APPSBL
28 0x3, # Version: 3
29 0x0, # Image source pointer
30 base, # Image destination pointer
31 size, # Code Size + Cert Size + Signature Size
32 size, # Code Size
33 base + size, # Destination + Code Size
34 0x0, # Signature Size
35 base + size, # Destination + Code Size + Signature Size
36 0x0, # Cert Size
37 ]
38
39 header_packed = struct.pack('<10I', *header)
40 return header_packed
41
42def mkheader(base_addr, img_type, infname, outfname):
43 """Prepends the image with the MBN header.
44
45 @arg base_addr: integer, specifies the image load address in RAM
46 @arg img_type: integer, specifies the image type
47 @arg infname: string, image filename
48 @arg outfname: string, output image with header prepended
49 @raises IOError: if reading/writing input/output file fails
50 """
51 with open(infname, "rb") as infp:
52 image = infp.read()
53 insize = len(image)
54
55 if base_addr > 0xFFFFFFFF:
56 raise ValueError("invalid base address")
57
58 if base_addr + insize > 0xFFFFFFFF:
59 raise ValueError("invalid destination range")
60
61 header = create_header(base_addr, insize, img_type)
62 with open(outfname, "wb") as outfp:
63 outfp.write(header)
64 outfp.write(image)
65
66def usage(msg=None):
67 """Print command usage.
68
69 @arg msg: string, error message if any (default: None)
70 """
71 if msg != None:
72 sys.stderr.write("mkheader: %s\n" % msg)
73
74 print "Usage: mkheader.py <base-addr> <img_type> <input-file> <output-file>"
75
76 if msg != None:
77 exit(1)
78
79def main():
80 """Main entry function"""
81
82 if len(sys.argv) != 5:
83 usage("incorrect no. of arguments")
84
85 try:
86 base_addr = int(sys.argv[1], 0)
87 img_type = int(sys.argv[2], 0)
88 infname = sys.argv[3]
89 outfname = sys.argv[4]
90 except ValueError as e:
91 sys.stderr.write("mkheader: invalid base address '%s'\n" % sys.argv[1])
92 exit(1)
93
94 try:
95 mkheader(base_addr, img_type, infname, outfname)
96 except IOError as e:
97 sys.stderr.write("mkheader: %s\n" % e)
98 exit(1)
99 except ValueError as e:
100 sys.stderr.write("mkheader: %s\n" % e)
101 exit(1)
102
103if __name__ == "__main__":
104 main()
105