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

5
Last change on this file since e29d0a6 was e29d0a6, checked in by Amar Takhar <amar@…>, on 03/04/20 at 17:45:56

Convert optparse to argparse.

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