2 -- Ability to read what card is there
3 local getopt = require('getopt')
4 local cmds = require('commands')
5 local taglib = require('taglib')
8 [[This script will automatically recognize and dump full content of a NFC NDEF Initialized tag; non-initialized tags will be ignored.
10 It also write the dump to an eml-file <uid>.eml.
12 (The difference between an .eml-file and a .bin-file is that the eml file contains
13 ASCII representation of the hex-data, with linebreaks between 'rows'. A .bin-file contains the
14 raw data, but when saving into that for, we lose the infromation about how the memory is structured.
15 For example: 24 bytes could be 6 blocks of 4 bytes, or vice versa.
16 Therefore, the .eml is better to use file when saving dumps.)
23 local example = "script run xxx"
24 local author = "Martin Holst Swende & Asper"
28 -- TODO; replace this with a call to the proper PrintAndLog
32 -- This is only meant to be used when errors occur
39 -- Perhaps this will be moved to a separate library at one point
41 --- Writes an eml-file.
42 -- @param uid - the uid of the tag. Used in filename
43 -- @param blockData. Assumed to be on the format {'\0\1\2\3,'\b\e\e\f' ...,
44 -- that is, blockData[row] contains a string with the actual data, not ascii hex representation
45 -- return filename if all went well,
46 -- @reurn nil, error message if unsuccessfull
47 writeDumpFile = function(uid, blockData)
48 local destination = string.format("%s.eml", uid)
49 local file = io.open(destination, "w")
51 return nil, string.format("Could not write to file %s", destination)
53 local rowlen = string.len(blockData[1])
55 for i,block in ipairs(blockData) do
56 if rowlen ~= string.len(block) then
57 prlog(string.format("WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1",i))
60 local formatString = string.format("H%d", string.len(block))
61 local _,hex = bin.unpack(formatString,block)
63 file:write(0x0A) -- Line feed
76 prlog("Example usage")
87 local function show(data)
89 local formatString = ("H%d"):format(string.len(data))
90 local _,hexdata = bin.unpack(formatString, data)
91 debug("Hexdata" , hexdata)
94 --- Fire up a connection with a tag, return uid
95 -- @return UID if successfull
96 -- @return nil, errormessage if unsuccessfull
99 debug("Opening connection")
100 core.clearCommandBuffer()
101 local x = string.format("hf 14a raw -r -p -s")
105 data, err = waitCmd(true)
106 if err then return oops(err) end
108 local formatString = ("H%d"):format(string.len(data))
109 local _,uid = bin.unpack(formatString, data)
112 --- Shut down tag communication
113 -- return no return values
114 local function close()
115 debug("Closing connection")
116 core.clearCommandBuffer()
117 local x = string.format("hf 14a raw -r")
121 --data, err = waitCmd(true)
122 --data, err = waitCmd(false)
127 ---_ Gets data from a block
128 -- @return {block, block+1, block+2, block+3} if successfull
129 -- @return nil, errormessage if unsuccessfull
130 local function getBlock(block)
133 core.clearCommandBuffer()
135 local x = string.format("hf 14a raw -r -c -p 30 %02x", block)
139 -- By now, there should be an ACK waiting from the device, since
140 -- we used the -r flag (don't read response).
142 data, err = waitCmd(false)
143 if err then return oops(err) end
146 if string.len(data) < 18 then
147 return nil, ("Expected at least 18 bytes, got %d - this tag is not NDEF-compliant"):format(string.len(data))
149 -- Now, parse out the block data
150 -- 0534 00B9 049C AD7F 4A00 0000 E110 1000 2155
151 -- b0b0 b0b0 b1b1 b1b1 b2b2 b2b2 b3b3 b3b3 CRCC
152 b0 = string.sub(data,1,4)
153 b1 = string.sub(data,5,8)
154 b2 = string.sub(data,9,12)
155 b3 = string.sub(data,13,16)
160 --- This function is a lua-implementation of
161 -- cmdhf14a.c:waitCmd(uint8_t iSelect)
162 function waitCmd(iSelect)
163 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,1000)
165 local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',response)
168 if iSelect then iLen = arg1 end
169 debug(("Received %i octets (arg0:%d, arg1:%d)"):format(iLen, arg0, arg1))
170 if iLen == 0 then return nil, "No response from tag" end
171 local recv = string.sub(response,count, iLen+count-1)
174 return nil, "No response from device"
179 local function main( args)
180 debug("script started")
181 local err, data, data2,k,v,i
182 -- Read the parameters
183 for o, a in getopt.getopt(args, 'hd') do
184 if o == "h" then help() return end
185 if o == "d" then DEBUG = true end
188 -- Info contained within the tag (block 0 example)
189 -- 0534 00B9 049C AD7F 4A00 0000 E110 1000 2155
190 -- b0b0 b0b0 b1b1 b1b1 b2b2 b2b2 b3b3 b3b3 CRCC
191 -- MM?? ???? ???? ???? ???? ???? NNVV SS?? ----
192 -- M = Manufacturer info
193 -- N = NDEF-Structure-Compliant (if value is E1)
194 -- V = NFC Forum Specification version (if 10 = v1.0)
196 -- First, 'connect' (fire up the field) and get the uid
197 local uidHexstr = open()
199 -- First, get blockt 3 byte 2
200 local blocks, err = getBlock(0)
201 if err then return oops(err) end
202 -- Block 3 contains number of blocks
203 local b3chars = {string.byte(blocks[4], 1,4)}
204 local numBlocks = b3chars[3] * 2 + 6
205 prlog("Number of blocks:", numBlocks)
208 if b3chars[1] ~= 0xE1 then
209 return oops("This tag is not NDEF-Complian")
212 local ndefVersion = b3chars[2]
214 -- Block 1, byte 1 contains manufacturer info
215 local bl1_b1 = string.byte(blocks[1], 1)
216 local manufacturer = taglib.lookupManufacturer(bl1_b1)
218 -- Reuse existing info
219 local blockData = {blocks[1],blocks[2],blocks[3],blocks[4]}
222 --[[ Due to the infineon my-d move bug
223 (if I send 30 0F i receive block0f+block00+block01+block02 insted of block0f+block10+block11+block12)
224 the only way to avoid this is to send the read command as many times as block numbers
225 removing bytes from 5 to 18 from each answer.
227 prlog("Dumping data...please wait")
228 for i=4,numBlocks-1,1 do
229 blocks, err = getBlock(i)
230 if err then return oops(err) end
231 table.insert(blockData,blocks[1])
236 prlog(string.format("Tag manufacturer: %s", manufacturer))
237 prlog(string.format("Tag UID: %s", uidHexstr))
238 prlog(string.format("Tag NDEF version: 0x%02x", ndefVersion))
240 for k,v in ipairs(blockData) do
241 prlog(string.format("Block %02x: %02x %02x %02x %02x",k-1, string.byte(v, 1,4)))
243 local filename, err = utils.writeDumpFile(uidHexstr, blockData)
244 if err then return oops(err) end
246 prlog(string.format("Dumped data into %s", filename))