summaryrefslogtreecommitdiffstats
path: root/debug/b43-beautifier
blob: b07d493eb04b0ab4f1bbaf4484fea808ac633b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
"""
#  b43 firmware assembly code beautifier
#
#  Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 3
#  as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import getopt
import sys
from libb43 import *


def usage():
	print "b43 firmware assembly code beautifier"
	print ""
	print "Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>"
	print "Licensed under the GNU/GPL version 3"
	print ""
	print "Usage: b43-beautifier [OPTIONS]"
	print ""
	print "-h|--help            Print this help text"
	print "-a|--asmfile [FILE]  Assembly code source file"
	print "-b|--binfile [FILE]  Binary code source file"
	print "-d|--defs [DIR]      Directory containing the defs files"
	print ""
	print "The options  -d AND (-a XOR -b)  are essential."
	print "The --defs directory is the \"common\" subdirectory in the"
	print "b43-ucode assembly source tree."

def parseArgs():
	global opt_asmfile
	global opt_binfile
	global opt_defsfiles

	opt_asmfile = None
	opt_binfile = None
	opt_defsfiles = None

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"ha:b:d:",
			[ "help", "asmfile=", "binfile=", "defs=" ])
	except getopt.GetoptError:
		usage()
		sys.exit(1)

	for (o, v) in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit(0)
		if o in ("-a", "--asmfile"):
			if opt_binfile:
				print "Must not set both of --asmfile and --binfile"
				sys.exit(1)
			opt_asmfile = v
		if o in ("-b", "--binfile"):
			if opt_asmfile:
				print "Must not set both of --asmfile and --binfile"
				sys.exit(1)
			opt_binfile = v
		if o in ("-d", "--defs"):
			opt_defsfiles = v

	if not opt_asmfile and not opt_binfile:
		print "Must set either --asmfile or --binfile"
		sys.exit(1)
	if not opt_defsfiles:
		print "Must set --defs"
		sys.exit(1)

def main():
	parseArgs()

	# Get the assembly
	if opt_asmfile:
		try:
			asm = file(opt_asmfile).read()
		except IOError, e:
			print "Could not read asmfile %s: %s" % (e.filename, e.strerror)
			sys.exit(1)
	else:
		try:
			bin = file(opt_binfile).read()
		except IOError, e:
			print "Could not read binfile %s: %s" % (e.filename, e.strerror)
			sys.exit(1)
		asm = Disassembler(bin, "").getAsm()

	b = B43Beautifier(asm, opt_defsfiles)
	sys.stdout.write(b.getAsm())

try:
	main()
except B43Exception:
	sys.exit(1)
bues.ch cgit interface