source: rtems-tools/misc/tools/mkimage.py @ e8c180f

5
Last change on this file since e8c180f was e8c180f, checked in by Amar Takhar <verm@…>, on 03/04/20 at 01:08:10

Add to wscript and add python2 shebang.

This script does work but needs some user friendliness added which is
acknowledged by the author as it was meant as a quick replacement.

  • Property mode set to 100755
File size: 5.3 KB
Line 
1#!/usr/bin/env python2
2
3# A quickly bashed together replacement for u-boot's mkimage written in python
4#
5# Copyright 2010 Craig Barker
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# 1. Redistributions of source code must retain the above copyright notice,
11#    this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above copyright notice,
14#    this list of conditions and the following disclaimer in the documentation
15#    and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28
29
30from optparse import OptionParser
31from struct import *
32import sys
33import os.path
34import time
35import binascii
36
37MAGIC = 0x27051956
38IMG_NAME_LENGTH = 32
39
40archs = {'invalid':0, 'alpha':1, 'arm':2, 'x86':3, 'ia64':4, 'm68k':12,
41         'microblaze':14, 'mips':5, 'mips64':6, 'nios':13, 'nios2':15,
42         'powerpc':7, 'ppc':7, 's390':8, 'sh':9, 'sparc':10,
43         'sparc64':11, 'blackfin':16, 'arv32':17, 'st200':18 }
44
45oss   = {'invalid':0, 'openbsd':1, 'netbsd':2, 'freebsd':3, '4_4bsd':4,
46         'linux':5, 'svr4':6, 'esix':7, 'solaris':8, 'irix':9,
47         'sco':10, 'dell':11, 'ncr':12, 'lynos':13, 'vxworks':14,
48         'psos':15, 'qnx':16, 'u-boot':17, 'rtems':18, 'artos':19,
49         'unity':20, 'integrity':21 }
50
51types = {'invalid':0, 'standalone':1, 'kernel':2, 'ramdisk':3, 'multi':4,
52         'firmware':5,'script':6, 'filesystem':7, 'flat_dt':8 }
53
54comps = {'none':0, 'bzip2':2, 'gzip':1, 'lzma':3 }
55
56usage = "usage: %prog [options] image"
57parser = OptionParser(usage=usage)
58parser.add_option("-A","--arch", dest="arch", default="powerpc",
59                  help="set architecture to 'arch'", metavar="ARCH")
60parser.add_option("-O","--os", dest="os", default="linux",
61                  help="set operating system to 'os'", metavar="OS")
62parser.add_option("-T","--type", dest="type", default="kernel",
63                  help="set image type to 'type'", metavar="TYPE")
64parser.add_option("-C","--comp", dest="comp", default="gzip",
65                  help="set compression type 'comp'", metavar="COMP")
66parser.add_option("-a","--addr", dest="addr", default="0",
67                  help="set load address to 'addr' (hex)", metavar="ADDR")
68parser.add_option("-e","--ep", dest="ep", default="0",
69                  help="set entry point to 'ep' (hex)", metavar="EP")
70parser.add_option("-n","--name", dest="name", default="",
71                  help="set image name to 'name'", metavar="NAME")
72parser.add_option("-d","--datafile", dest="datafile",
73                  help="use image data from 'datafile'", metavar="DATAFILE")
74parser.add_option("-x","--xip", action="store_true", dest="xip", default=False,
75                  help="set XIP (execute in place)")
76
77(options, args) = parser.parse_args()
78
79if len(args) != 1: parser.print_help()
80
81if options.arch not in archs:
82        print "Invalid architecture specified, aborting"
83        sys.exit(2)
84
85if options.os not in oss:
86        print "Invalid operating system specified, aborting"
87        sys.exit(2)
88
89if options.comp not in comps:
90        print "Invalid compression specified, aborting"
91        sys.exit(2)
92
93if options.type not in types:
94        print "Invalid image type specified, aborting"
95        sys.exit(2)
96
97try:
98        inputsize = os.path.getsize(options.datafile)
99        inputfile = open(options.datafile, 'rb')
100
101except IOError:
102        print "Invalid datafile specified, aborting"
103        sys.exit(2)
104
105try:
106        outputfile = open(args[0],'wb')
107
108except IOError:
109        print "Error opening output file for writing, aborting"
110        sys.exit(1)
111
112struct = Struct("!IIIIIIIBBBB"+str(IMG_NAME_LENGTH)+"s")
113
114outputfile.seek(struct.size);
115
116inputcrc = 0;
117
118while True:
119        inputblock = inputfile.read(4096)
120        if not inputblock: break
121        inputcrc = binascii.crc32(inputblock, inputcrc)
122        outputfile.write(inputblock)
123
124inputcrc = inputcrc & 0xffffffff
125
126structdata = struct.pack(MAGIC, 0, int(time.time()), inputsize,
127                int(options.addr,16), int(options.ep,16), inputcrc,
128                oss[options.os], archs[options.arch], types[options.type],
129                comps[options.comp], options.name)
130
131headercrc = binascii.crc32(structdata) & 0xFFFFFFFF
132
133structdata =  struct.pack(MAGIC, headercrc, int(time.time()), inputsize,
134                int(options.addr,16), int(options.ep,16), inputcrc,
135                oss[options.os], archs[options.arch], types[options.type],
136                comps[options.comp], options.name)
137
138outputfile.seek(0)
139outputfile.write(structdata)
140outputfile.close()
141inputfile.close()
Note: See TracBrowser for help on using the repository browser.