source: mainline/tools/mkuimage.py@ 140e21a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 140e21a was 5711f6ec, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

Add configuration option to select uImage os type

NetBSD stage 2 for beagleboardxm, Linux for everything else.

  • Property mode set to 100755
File size: 4.3 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 os_type = 5 #Linux is the default
63
64 while len(args) >= 2 and args[0][0] == '-':
65 opt = args.popleft()[1:]
66 optarg = args.popleft()
67
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
80
81 if len(args) < 2:
82 print(base_name + ": Argument missing.")
83 print_syntax(cmd_name)
84 return
85
86 inf_name = args[0]
87 outf_name = args[1]
88
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
94
95def 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')
98
99 header = xstruct.create(UIMAGE_HEADER)
100 header_size = header.size()
101
102 #
103 # Write data
104 #
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 = ''
112
113 #
114 # Write header
115 #
116 outf.seek(0, os.SEEK_SET)
117
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')
130
131 header_crc = calc_crc32(header.pack())
132 header.header_crc = header_crc
133
134 outf.write(header.pack())
135 outf.close()
136
137## Compute CRC32 of binary string.
138#
139# Works around bug in zlib.crc32() which returns signed int32 result
140# in Python < 3.0.
141#
142def 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
148
149## Print command-line syntax.
150#
151def 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)")
161
162if __name__ == '__main__':
163 main()
Note: See TracBrowser for help on using the repository browser.