]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/scripts/dumptoemul-mfu.lua
FIX: the check for formatlen was wrong.
[proxmark3-svn] / client / scripts / dumptoemul-mfu.lua
CommitLineData
0de8e387 1-- The getopt-functionality is loaded from pm3/getopt.lua
2-- Have a look there for further details
3getopt = require('getopt')
4bin = require('bin')
f3cfe428 5example = "script run dumptoemul-mfu -i dumpdata-foobar.bin"
6author = "Martin Holst Swende \n @Marshmellow"
2b1f4228 7usage = "script run dumptoemul-mfu [-i <file>] [-o <file>]"
0de8e387 8desc =[[
2b1f4228 9This script takes a dumpfile from 'hf mfu dump' and converts it to a format that can be used
0de8e387 10by the emulator
11
12Arguments:
13 -h This help
14 -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used
15 -o <filename> Specifies the output file. If omitted, <uid>.eml is used.
16
17]]
0de8e387 18---
19-- A debug printout-function
20function dbg(args)
21 if DEBUG then
22 print("###", args)
23 end
24end
25---
26-- This is only meant to be used when errors occur
27function oops(err)
28 print("ERROR: ",err)
29end
30
0de8e387 31---
32-- Usage help
33function help()
34 print(desc)
f3cfe428 35 print(author)
0de8e387 36 print("Example usage")
37 print(example)
38end
39
40local function convert_to_ascii(hexdata)
41 if string.len(hexdata) % 8 ~= 0 then
42 return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata)))
43 end
44
45 local js,i = "[";
46 for i = 1, string.len(hexdata),8 do
47 js = js .."'" ..string.sub(hexdata,i,i+7).."',\n"
48 end
49 js = js .. "]"
50 return js
51end
52
53local function readdump(infile)
54 t = infile:read("*all")
0de8e387 55 len = string.len(t)
56 local len,hex = bin.unpack(("H%d"):format(len),t)
0de8e387 57 return hex
58end
59
60local function convert_to_emulform(hexdata)
61 if string.len(hexdata) % 8 ~= 0 then
62 return oops(("Bad data, length should be a multiple of 8 (was %d)"):format(string.len(hexdata)))
63 end
64 local ascii,i = "";
65 for i = 1, string.len(hexdata),8 do
66 ascii = ascii ..string.sub(hexdata,i,i+7).."\n"
67 end
68
69 return string.sub(ascii,1,-1)
70end
71
72local function main(args)
73
74 local input = "dumpdata.bin"
75 local output
76
77 for o, a in getopt.getopt(args, 'i:o:h') do
78 if o == "h" then return help() end
79 if o == "i" then input = a end
80 if o == "o" then output = a end
81 end
82 -- Validate the parameters
83
84 local infile = io.open(input, "rb")
85 if infile == nil then
86 return oops("Could not read file ", input)
87 end
88 local dumpdata = readdump(infile)
89 -- The hex-data is now in ascii-format,
90
91 -- But first, check the uid
2b1f4228 92 local uid = string.sub(dumpdata,1+48,8)
0de8e387 93 output = output or (uid .. ".eml")
94
95 -- Format some linebreaks
96 dumpdata = convert_to_emulform(dumpdata)
97
98 local outfile = io.open(output, "w")
99 if outfile == nil then
100 return oops("Could not write to file ", output)
101 end
102
103 outfile:write(dumpdata:lower())
104 io.close(outfile)
105 print(("Wrote an emulator-dump to the file %s"):format(output))
106end
107
108
109--[[
110In the future, we may implement so that scripts are invoked directly
111into a 'main' function, instead of being executed blindly. For future
112compatibility, I have done so, but I invoke my main from here.
113--]]
114main(args)
Impressum, Datenschutz