1 -- The getopt-functionality is loaded from pm3/getopt.lua
2 -- Have a look there for further details
3 getopt = require('getopt')
5 example = "script run dumptoemul-mfu -i dumpdata-foobar.bin"
6 author = "Martin Holst Swende \n @Marshmellow"
7 usage = "script run dumptoemul-mfu [-i <file>] [-o <file>]"
9 This script takes a dumpfile from 'hf mfu dump' and converts it to a format that can be used
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.
19 -- A debug printout-function
26 -- This is only meant to be used when errors occur
36 print("Example usage")
40 local 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)))
46 for i = 1, string.len(hexdata),8 do
47 js = js .."'" ..string.sub(hexdata,i,i+7).."',\n"
53 local function readdump(infile)
54 t = infile:read("*all")
56 local len,hex = bin.unpack(("H%d"):format(len),t)
60 local 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)))
65 for i = 1, string.len(hexdata),8 do
66 ascii = ascii ..string.sub(hexdata,i,i+7).."\n"
69 return string.sub(ascii,1,-1)
72 local function main(args)
74 local input = "dumpdata.bin"
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
82 -- Validate the parameters
84 local infile = io.open(input, "rb")
86 return oops("Could not read file ", input)
88 local dumpdata = readdump(infile)
89 -- The hex-data is now in ascii-format,
91 -- But first, check the uid
92 local uid = string.sub(dumpdata,1+48,8)
93 output = output or (uid .. ".eml")
95 -- Format some linebreaks
96 dumpdata = convert_to_emulform(dumpdata)
98 local outfile = io.open(output, "w")
99 if outfile == nil then
100 return oops("Could not write to file ", output)
103 outfile:write(dumpdata:lower())
105 print(("Wrote an emulator-dump to the file %s"):format(output))
110 In the future, we may implement so that scripts are invoked directly
111 into a 'main' function, instead of being executed blindly. For future
112 compatibility, I have done so, but I invoke my main from here.