kernel arm 32 linker script: add KERNEL_LOAD_ADDRESS for beaglebone
[helenos.git] / tools / mkuimage.py
blob372b00c0489783d7fb4b9fe5da4bdb1ab04122fb
1 #!/usr/bin/env python
3 # Copyright (c) 2010 Jiri Svoboda
4 # All rights reserved.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
10 # - Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # - Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # - The name of the author may not be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 """
31 Create legacy uImage (U-Boot image)
32 """
34 from collections import deque
35 import os
36 import sys
37 import xstruct
38 import zlib
40 UIMAGE_HEADER = """big:
41 uint32_t magic
42 uint32_t header_crc
43 uint32_t c_tstamp
44 uint32_t data_size
45 uint32_t load_addr
46 uint32_t start_addr
47 uint32_t data_crc
48 uint8_t os
49 uint8_t arch
50 uint8_t img_type
51 uint8_t compression
52 char img_name[32]
53 """
55 def main():
56 args = deque(sys.argv)
57 cmd_name = args.popleft()
58 base_name = os.path.basename(cmd_name)
59 image_name = 'Noname'
60 load_addr = 0
61 start_addr = 0
62 os_type = 5 #Linux is the default
64 while len(args) >= 2 and args[0][0] == '-':
65 opt = args.popleft()[1:]
66 optarg = args.popleft()
68 if opt == 'name':
69 image_name = optarg
70 elif opt == 'laddr':
71 load_addr = (int)(optarg, 0)
72 elif opt == 'saddr':
73 start_addr = (int)(optarg, 0)
74 elif opt == 'ostype':
75 os_type = (int)(optarg, 0)
76 else:
77 print(base_name + ": Unrecognized option.")
78 print_syntax(cmd_name)
79 return
81 if len(args) < 2:
82 print(base_name + ": Argument missing.")
83 print_syntax(cmd_name)
84 return
86 inf_name = args[0]
87 outf_name = args[1]
89 try:
90 mkuimage(inf_name, outf_name, image_name, load_addr, start_addr, os_type)
91 except:
92 os.remove(outf_name)
93 raise
95 def mkuimage(inf_name, outf_name, image_name, load_addr, start_addr, os_type):
96 inf = open(inf_name, 'rb')
97 outf = open(outf_name, 'wb')
99 header = xstruct.create(UIMAGE_HEADER)
100 header_size = header.size()
103 # Write data
105 outf.seek(header_size, os.SEEK_SET)
106 data = inf.read()
107 data_size = inf.tell()
108 data_crc = calc_crc32(data)
109 data_tstamp = (int)(os.path.getmtime(inf_name))
110 outf.write(data)
111 data = ''
114 # Write header
116 outf.seek(0, os.SEEK_SET)
118 header.magic = 0x27051956 # uImage magic
119 header.header_crc = 0
120 header.c_tstamp = data_tstamp
121 header.data_size = data_size
122 header.load_addr = load_addr # Address where to load image
123 header.start_addr = start_addr # Address of entry point
124 header.data_crc = data_crc
125 header.os = os_type
126 header.arch = 2 # ARM
127 header.img_type = 2 # Kernel
128 header.compression = 0 # None
129 header.img_name = image_name.encode('ascii')
131 header_crc = calc_crc32(header.pack())
132 header.header_crc = header_crc
134 outf.write(header.pack())
135 outf.close()
137 ## Compute CRC32 of binary string.
139 # Works around bug in zlib.crc32() which returns signed int32 result
140 # in Python < 3.0.
142 def calc_crc32(byteseq):
143 signed_crc = zlib.crc32(byteseq, 0)
144 if signed_crc < 0:
145 return signed_crc + (1 << 32)
146 else:
147 return signed_crc
149 ## Print command-line syntax.
151 def print_syntax(cmd):
152 print("syntax: " + cmd + " [<options>] <raw_image> <uImage>")
153 print()
154 print("\traw_image\tInput image name (raw binary data)")
155 print("\tuImage\t\tOutput uImage name (U-Boot image)")
156 print()
157 print("options:")
158 print("\t-name <name>\tImage name (default: 'Noname')")
159 print("\t-laddr <name>\tLoad address (default: 0x00000000)")
160 print("\t-saddr <name>\tStart address (default: 0x00000000)")
162 if __name__ == '__main__':
163 main()