3 # Copyright (c) 2010 Jiri Svoboda
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
31 Create legacy uImage (U-Boot image)
34 from collections
import deque
40 UIMAGE_HEADER
= """big:
56 args
= deque(sys
.argv
)
57 cmd_name
= args
.popleft()
58 base_name
= os
.path
.basename(cmd_name
)
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()
71 load_addr
= (int)(optarg
, 0)
73 start_addr
= (int)(optarg
, 0)
75 os_type
= (int)(optarg
, 0)
77 print(base_name
+ ": Unrecognized option.")
78 print_syntax(cmd_name
)
82 print(base_name
+ ": Argument missing.")
83 print_syntax(cmd_name
)
90 mkuimage(inf_name
, outf_name
, image_name
, load_addr
, start_addr
, os_type
)
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()
105 outf
.seek(header_size
, os
.SEEK_SET
)
107 data_size
= inf
.tell()
108 data_crc
= calc_crc32(data
)
109 data_tstamp
= (int)(os
.path
.getmtime(inf_name
))
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
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())
137 ## Compute CRC32 of binary string.
139 # Works around bug in zlib.crc32() which returns signed int32 result
142 def calc_crc32(byteseq
):
143 signed_crc
= zlib
.crc32(byteseq
, 0)
145 return signed_crc
+ (1 << 32)
149 ## Print command-line syntax.
151 def print_syntax(cmd
):
152 print("syntax: " + cmd
+ " [<options>] <raw_image> <uImage>")
154 print("\traw_image\tInput image name (raw binary data)")
155 print("\tuImage\t\tOutput uImage name (U-Boot image)")
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__':