source: mainline/tools/mkuimage.py@ 1dbd189

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1dbd189 was 81475250, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Build scripts work with Python 3 (thx Sean Bartell)

  • Property mode set to 100755
File size: 4.2 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2010 Jiri Svoboda
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
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.
17#
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.
28#
29
30"""
31Create legacy uImage (U-Boot image)
32"""
33
34from collections import deque
35import os
36import sys
37import xstruct
38import zlib
39
40UIMAGE_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"""
54
55def 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
63 while len(args) >= 2 and args[0][0] == '-':
64 opt = args.popleft()[1:]
65 optarg = args.popleft()
66
67 if opt == 'name':
68 image_name = optarg
69 elif opt == 'laddr':
70 load_addr = (int)(optarg, 0)
71 elif opt == 'saddr':
72 start_addr = (int)(optarg, 0)
73 else:
74 print(base_name + ": Unrecognized option.")
75 print_syntax(cmd_name)
76 return
77
78 if len(args) < 2:
79 print(base_name + ": Argument missing.")
80 print_syntax(cmd_name)
81 return
82
83 inf_name = args[0]
84 outf_name = args[1]
85
86 try:
87 mkuimage(inf_name, outf_name, image_name, load_addr, start_addr)
88 except:
89 os.remove(outf_name)
90 raise
91
92def mkuimage(inf_name, outf_name, image_name, load_addr, start_addr):
93 inf = open(inf_name, 'rb')
94 outf = open(outf_name, 'wb')
95
96 header = xstruct.create(UIMAGE_HEADER)
97 header_size = header.size()
98
99 #
100 # Write data
101 #
102 outf.seek(header_size, os.SEEK_SET)
103 data = inf.read()
104 data_size = inf.tell()
105 data_crc = calc_crc32(data)
106 data_tstamp = (int)(os.path.getmtime(inf_name))
107 outf.write(data)
108 data = ''
109
110 #
111 # Write header
112 #
113 outf.seek(0, os.SEEK_SET)
114
115 header.magic = 0x27051956 # uImage magic
116 header.header_crc = 0
117 header.c_tstamp = data_tstamp
118 header.data_size = data_size
119 header.load_addr = load_addr # Address where to load image
120 header.start_addr = start_addr # Address of entry point
121 header.data_crc = data_crc
122 header.os = 5 # Linux
123 header.arch = 2 # ARM
124 header.img_type = 2 # Kernel
125 header.compression = 0 # None
126 header.img_name = image_name.encode('ascii')
127
128 header_crc = calc_crc32(header.pack())
129 header.header_crc = header_crc
130
131 outf.write(header.pack())
132 outf.close()
133
134## Compute CRC32 of binary string.
135#
136# Works around bug in zlib.crc32() which returns signed int32 result
137# in Python < 3.0.
138#
139def calc_crc32(byteseq):
140 signed_crc = zlib.crc32(byteseq, 0)
141 if signed_crc < 0:
142 return signed_crc + (1 << 32)
143 else:
144 return signed_crc
145
146## Print command-line syntax.
147#
148def print_syntax(cmd):
149 print("syntax: " + cmd + " [<options>] <raw_image> <uImage>")
150 print()
151 print("\traw_image\tInput image name (raw binary data)")
152 print("\tuImage\t\tOutput uImage name (U-Boot image)")
153 print()
154 print("options:")
155 print("\t-name <name>\tImage name (default: 'Noname')")
156 print("\t-laddr <name>\tLoad address (default: 0x00000000)")
157 print("\t-saddr <name>\tStart address (default: 0x00000000)")
158
159if __name__ == '__main__':
160 main()
Note: See TracBrowser for help on using the repository browser.