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