ntag i2c 2k - fast write
[RRG-proxmark3.git] / client / lualibs / read14b.lua
blob1fb978caaf37c0e1efa3db27ef44c2a32c050788
1 --[[
2 This is a library to read 14443b tags. It can be used something like this
4 local reader = require('read14b')
5 result, err = reader.read14443b()
6 if not result then
7 print(err)
8 return
9 end
10 print(result.name)
12 --]]
13 -- Loads the commands-library
14 local cmds = require('commands')
15 local utils = require('utils')
17 -- Shouldn't take longer than 2.5 seconds
18 local TIMEOUT = 1000
20 local ISO14B_COMMAND = {
21 ISO14B_CONNECT = 0x1,
22 ISO14B_DISCONNECT = 0x2,
23 ISO14B_APDU = 0x4,
24 ISO14B_RAW = 0x8,
25 ISO14B_REQUEST_TRIGGER = 0x10,
26 ISO14B_APPEND_CRC = 0x20,
27 ISO14B_SELECT_STD = 0x40,
28 ISO14B_SELECT_SR = 0x80,
29 ISO14B_SET_TIMEOUT = 0x100,
30 ISO14B_SEND_CHAINING = 0x200,
31 ISO14B_SELECT_CTS = 0x400,
32 ISO14B_CLEARTRACE = 0x800,
35 local function parse14443b(data)
36 --[[
37 Based on this struct :
39 typedef struct {
40 uint8_t uid[10];
41 uint8_t uidlen;
42 uint8_t atqb[7];
43 uint8_t chipid;
44 uint8_t cid;
45 } PACKED iso14b_card_select_t;
46 --- local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
47 --]]
49 local uid = data:sub(1, 20)
50 local uidlen = data:sub(21, 22)
51 local atqb = data:sub(23, 36)
52 local chipid = data:sub(37, 38)
53 local cid = data:sub(39, 40)
55 uid = uid:sub(1, 2 * uidlen)
56 return {
57 uid = uid,
58 uidlen = uidlen,
59 atqb = atqb,
60 chipid = chipid,
61 cid = cid
63 end
65 -- This function does a connect and retrieves some info
66 -- @return if successful: an table containing card info
67 -- @return if unsuccessful : nil, error
68 local function read14443b(disconnect)
69 local flags = ISO14B_COMMAND.ISO14B_CONNECT +
70 ISO14B_COMMAND.ISO14B_SELECT_STD
72 if disconnect then
73 print('DISCONNECT')
74 flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT
75 end
77 local flags_str = ('%04x'):format(utils.SwapEndianness(('%04x'):format(flags), 16))
78 local time_str = ('%08x'):format(0)
79 local rawlen_str = ('%04x'):format(0)
80 local senddata = ('%s%s%s'):format(flags_str, time_str, rawlen_str)
81 local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = senddata}
83 local info = nil
84 local result, err = c:sendNG(false, TIMEOUT)
85 if result and result.Status == 0 then
86 if result.Oldarg0 == 0 then
87 info, err = parse14443b(result.Data)
88 else
89 err = 'iso14443b card select failed'
90 end
91 else
92 err = 'No response from card'
93 end
95 if err then
96 return nil, err
97 end
98 return info, nil
99 end
101 -- turns on the HF field.
102 local function connect14443b()
103 local data = ('%04x%08x%04x'):format(utils.SwapEndianness(('%04x'):format(ISO14B_COMMAND.ISO14B_CONNECT), 16), 0,0)
104 local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = data}
105 return c:sendNG(true)
108 -- Sends an instruction to do nothing, only disconnect
109 local function disconnect14443b()
110 local data = ('%04x%08x%04x'):format(utils.SwapEndianness(('%04x'):format(ISO14B_COMMAND.ISO14B_DISCONNECT), 16), 0,0)
111 local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = data}
112 -- We can ignore the response here, no ACK is returned for this command
113 -- Check /armsrc/iso14443b.c, ReaderIso14443b() for details
114 return c:sendNG(true)
117 -- Waits for a mifare card to be placed within the vicinity of the reader.
118 -- @return if successful: an table containing card info
119 -- @return if unsuccessful : nil, error
120 local function waitFor14443b()
121 print('Waiting for card... press <Enter> to quit')
122 while not core.kbd_enter_pressed() do
123 res, err = read14443b(false)
124 if res then return res, err end
125 -- err means that there was no response from card
127 disconnect14443b()
128 return nil, 'Aborted by user'
131 local library = {
132 read = read14443b,
133 waitFor14443b = waitFor14443b,
134 parse14443b = parse14443b,
135 connect = connect14443b,
136 disconnect = disconnect14443b,
137 ISO14B_COMMAND = ISO14B_COMMAND,
140 return library