]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // Modified 2010-2012 by <adrian -at- atrox.at> | |
4 | // Modified 2012 by <vsza at vsza.hu> | |
5 | // | |
6 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
7 | // at your option, any later version. See the LICENSE.txt file for the text of | |
8 | // the license. | |
9 | //----------------------------------------------------------------------------- | |
10 | // High frequency ISO15693 commands | |
11 | //----------------------------------------------------------------------------- | |
12 | // There are three basic operation modes, depending on which device (proxmark/pc) | |
13 | // the signal processing, (de)modulation, transmission protocol and logic is done. | |
14 | // Mode 1: | |
15 | // All steps are done on the proxmark, the output of the commands is returned via | |
16 | // USB-debug-print commands. | |
17 | // Mode 2: | |
18 | // The protocol is done on the PC, passing only Iso15693 data frames via USB. This | |
19 | // allows direct communication with a tag on command level | |
20 | // Mode 3: | |
21 | // The proxmark just samples the antenna and passes this "analog" data via USB to | |
22 | // the client. Signal Processing & decoding is done on the pc. This is the slowest | |
23 | // variant, but offers the possibility to analyze the waveforms directly. | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <stdint.h> | |
29 | ||
30 | #include "proxmark3.h" | |
31 | #include "data.h" | |
32 | #include "graph.h" | |
33 | #include "ui.h" | |
34 | #include "util.h" | |
35 | #include "cmdparser.h" | |
36 | #include "cmdhf15.h" | |
37 | #include "iso15693tools.h" | |
38 | #include "cmdmain.h" | |
39 | ||
40 | #define FrameSOF Iso15693FrameSOF | |
41 | #define Logic0 Iso15693Logic0 | |
42 | #define Logic1 Iso15693Logic1 | |
43 | #define FrameEOF Iso15693FrameEOF | |
44 | ||
45 | #define Crc(data,datalen) Iso15693Crc(data,datalen) | |
46 | #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen) | |
47 | #define sprintUID(target,uid) Iso15693sprintUID(target,uid) | |
48 | ||
49 | // structure and database for uid -> tagtype lookups | |
50 | typedef struct { | |
51 | uint64_t uid; | |
52 | int mask; // how many MSB bits used | |
53 | char* desc; | |
54 | } productName; | |
55 | ||
56 | ||
57 | const productName uidmapping[] = { | |
58 | ||
59 | // UID, #significant Bits, "Vendor(+Product)" | |
60 | { 0xE001000000000000LL, 16, "Motorola UK" }, | |
61 | ||
62 | // E0 02 xx | |
63 | // 02 = ST Microelectronics | |
64 | // XX = IC id (Chip ID Family) | |
65 | { 0xE002000000000000LL, 16, "ST Microelectronics SA France" }, | |
66 | { 0xE002050000000000LL, 24, "ST Microelectronics; LRI64 [IC id = 05]"}, | |
67 | { 0xE002080000000000LL, 24, "ST Microelectronics; LRI2K [IC id = 08]"}, | |
68 | { 0xE0020A0000000000LL, 24, "ST Microelectronics; LRIS2K [IC id = 10]"}, | |
69 | { 0xE002440000000000LL, 24, "ST Microelectronics; LRIS64K [IC id = 68]"}, | |
70 | ||
71 | { 0xE003000000000000LL, 16, "Hitachi, Ltd Japan" }, | |
72 | ||
73 | // E0 04 xx | |
74 | // 04 = Manufacturer code (Philips/NXP) | |
75 | // XX = IC id (Chip ID Family) | |
76 | //I-Code SLI SL2 ICS20 [IC id = 01] | |
77 | //I-Code SLI-S [IC id = 02] | |
78 | //I-Code SLI-L [IC id = 03] | |
79 | //I-Code SLIX [IC id = 01 + bit36 set to 1 (starting from bit0 - different from normal SLI)] | |
80 | //I-Code SLIX-S [IC id = 02 + bit36 set to 1] | |
81 | //I-Code SLIX-L [IC id = 03 + bit36 set to 1] | |
82 | { 0xE004000000000000LL, 16, "NXP Semiconductors Germany (Philips)" }, | |
83 | { 0xE004010000000000LL, 24, "NXP(Philips); IC SL2 ICS20/ICS21(SLI) ICS2002/ICS2102(SLIX)" }, | |
84 | { 0xE004020000000000LL, 24, "NXP(Philips); IC SL2 ICS53/ICS54(SLI-S) ICS5302/ICS5402(SLIX-S)" }, | |
85 | { 0xE004030000000000LL, 24, "NXP(Philips); IC SL2 ICS50/ICS51(SLI-L) ICS5002/ICS5102(SLIX-L)" }, | |
86 | ||
87 | // E0 05 XX .. .. .. | |
88 | // 05 = Manufacturer code (Infineon) | |
89 | // XX = IC id (Chip ID Family) | |
90 | { 0xE005000000000000LL, 16, "Infineon Technologies AG Germany" }, | |
91 | { 0xE005A10000000000LL, 24, "Infineon; SRF55V01P [IC id = 161] plain mode 1kBit"}, | |
92 | { 0xE005A80000000000LL, 24, "Infineon; SRF55V01P [IC id = 168] pilot series 1kBit"}, | |
93 | { 0xE005400000000000LL, 24, "Infineon; SRF55V02P [IC id = 64] plain mode 2kBit"}, | |
94 | { 0xE005000000000000LL, 24, "Infineon; SRF55V10P [IC id = 00] plain mode 10KBit"}, | |
95 | { 0xE005500000000000LL, 24, "Infineon; SRF55V02S [IC id = 80] secure mode 2kBit"}, | |
96 | { 0xE005100000000000LL, 24, "Infineon; SRF55V10S [IC id = 16] secure mode 10KBit"}, | |
97 | { 0xE0051E0000000000LL, 23, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"}, | |
98 | { 0xE005200000000000LL, 21, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"}, | |
99 | ||
100 | { 0xE006000000000000LL, 16, "Cylink USA" }, | |
101 | ||
102 | ||
103 | // E0 07 xx | |
104 | // 07 = Texas Instruments | |
105 | // XX = from bit 41 to bit 43 = product configuration - from bit 44 to bit 47 IC id (Chip ID Family) | |
106 | //Tag IT RFIDType-I Plus, 2kBit, TI Inlay | |
107 | //Tag-it HF-I Plus Inlay [IC id = 00] -> b'0000 000 2kBit | |
108 | //Tag-it HF-I Plus Chip [IC id = 64] -> b'1000 000 2kBit | |
109 | //Tag-it HF-I Standard Chip / Inlays [IC id = 96] -> b'1100 000 256Bit | |
110 | //Tag-it HF-I Pro Chip / Inlays [IC id = 98] -> b'1100 010 256Bit, Password protection | |
111 | { 0xE007000000000000LL, 16, "Texas Instrument France" }, | |
112 | { 0xE007000000000000LL, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" }, | |
113 | { 0xE007100000000000LL, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" }, | |
114 | { 0xE007800000000000LL, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" }, | |
115 | { 0xE007C00000000000LL, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" }, | |
116 | { 0xE007C40000000000LL, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" }, | |
117 | ||
118 | { 0xE008000000000000LL, 16, "Fujitsu Limited Japan" }, | |
119 | { 0xE009000000000000LL, 16, "Matsushita Electronics Corporation, Semiconductor Company Japan" }, | |
120 | { 0xE00A000000000000LL, 16, "NEC Japan" }, | |
121 | { 0xE00B000000000000LL, 16, "Oki Electric Industry Co. Ltd Japan" }, | |
122 | { 0xE00C000000000000LL, 16, "Toshiba Corp. Japan" }, | |
123 | { 0xE00D000000000000LL, 16, "Mitsubishi Electric Corp. Japan" }, | |
124 | { 0xE00E000000000000LL, 16, "Samsung Electronics Co. Ltd Korea" }, | |
125 | { 0xE00F000000000000LL, 16, "Hynix / Hyundai, Korea" }, | |
126 | { 0xE010000000000000LL, 16, "LG-Semiconductors Co. Ltd Korea" }, | |
127 | { 0xE011000000000000LL, 16, "Emosyn-EM Microelectronics USA" }, | |
128 | ||
129 | { 0xE012000000000000LL, 16, "HID Corporation" }, | |
130 | { 0xE012000000000000LL, 16, "INSIDE Technology France" }, | |
131 | { 0xE013000000000000LL, 16, "ORGA Kartensysteme GmbH Germany" }, | |
132 | { 0xE014000000000000LL, 16, "SHARP Corporation Japan" }, | |
133 | { 0xE015000000000000LL, 16, "ATMEL France" }, | |
134 | ||
135 | { 0xE016000000000000LL, 16, "EM Microelectronic-Marin SA Switzerland (Skidata)"}, | |
136 | { 0xE016040000000000LL, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034 [IC id = 01] (Read/Write - no AFI)"}, | |
137 | { 0xE0160C0000000000LL, 24, "EM-Marin SA (Skidata); EM4035 [IC id = 03] (Read/Write - replaced by 4233)"}, | |
138 | { 0xE016100000000000LL, 24, "EM-Marin SA (Skidata); EM4135 [IC id = 04] (Read/Write - replaced by 4233) 36x64bit start page 13"}, | |
139 | { 0xE016140000000000LL, 24, "EM-Marin SA (Skidata); EM4036 [IC id = 05] 28pF"}, | |
140 | { 0xE016180000000000LL, 24, "EM-Marin SA (Skidata); EM4006 [IC id = 06] (Read Only)"}, | |
141 | { 0xE0161C0000000000LL, 24, "EM-Marin SA (Skidata); EM4133 [IC id = 07] 23,5pF (Read/Write)"}, | |
142 | { 0xE016200000000000LL, 24, "EM-Marin SA (Skidata); EM4033 [IC id = 08] 23,5pF (Read Only - no AFI / no DSFID / no security blocks)"}, | |
143 | { 0xE016240000000000LL, 24, "EM-Marin SA (Skidata); EM4233 [IC id = 09] 23,5pF CustomerID-102"}, | |
144 | { 0xE016280000000000LL, 24, "EM-Marin SA (Skidata); EM4233 SLIC [IC id = 10] 23,5pF (1Kb flash memory - not provide High Security mode and QuietStorage feature)" }, | |
145 | { 0xE0163C0000000000LL, 24, "EM-Marin SA (Skidata); EM4237 [IC id = 15] 23,5pF"}, | |
146 | { 0xE0167C0000000000LL, 24, "EM-Marin SA (Skidata); EM4233 [IC id = 31] 95pF"}, | |
147 | { 0xE016940000000000LL, 24, "EM-Marin SA (Skidata); EM4036 [IC id = 37] 95pF 51x64bit "}, | |
148 | { 0xE0169c0000000000LL, 24, "EM-Marin SA (Skidata); EM4133 [IC id = 39] 95pF (Read/Write)" }, | |
149 | { 0xE016A80000000000LL, 24, "EM-Marin SA (Skidata); EM4233 SLIC [IC id = 42] 97pF" }, | |
150 | { 0xE016BC0000000000LL, 24, "EM-Marin SA (Skidata); EM4237 [IC id = 47] 97pF" }, | |
151 | ||
152 | { 0xE017000000000000LL, 16, "KSW Microtec GmbH Germany" }, | |
153 | { 0xE018000000000000LL, 16, "ZMD AG Germany" }, | |
154 | { 0xE019000000000000LL, 16, "XICOR, Inc. USA" }, | |
155 | { 0xE01A000000000000LL, 16, "Sony Corporation Japan Identifier Company Country" }, | |
156 | { 0xE01B000000000000LL, 16, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" }, | |
157 | { 0xE01C000000000000LL, 16, "Emosyn USA" }, | |
158 | { 0xE01D000000000000LL, 16, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" }, | |
159 | { 0xE01E000000000000LL, 16, "Magellan Technology Pty Limited Australia" }, | |
160 | { 0xE01F000000000000LL, 16, "Melexis NV BO Switzerland" }, | |
161 | { 0xE020000000000000LL, 16, "Renesas Technology Corp. Japan" }, | |
162 | { 0xE021000000000000LL, 16, "TAGSYS France" }, | |
163 | { 0xE022000000000000LL, 16, "Transcore USA" }, | |
164 | { 0xE023000000000000LL, 16, "Shanghai belling corp., ltd. China" }, | |
165 | { 0xE024000000000000LL, 16, "Masktech Germany Gmbh Germany" }, | |
166 | { 0xE025000000000000LL, 16, "Innovision Research and Technology Plc UK" }, | |
167 | { 0xE026000000000000LL, 16, "Hitachi ULSI Systems Co., Ltd. Japan" }, | |
168 | { 0xE027000000000000LL, 16, "Cypak AB Sweden" }, | |
169 | { 0xE028000000000000LL, 16, "Ricoh Japan" }, | |
170 | { 0xE029000000000000LL, 16, "ASK France" }, | |
171 | { 0xE02A000000000000LL, 16, "Unicore Microsystems, LLC Russian Federation" }, | |
172 | { 0xE02B000000000000LL, 16, "Dallas Semiconductor/Maxim USA" }, | |
173 | { 0xE02C000000000000LL, 16, "Impinj, Inc. USA" }, | |
174 | { 0xE02D000000000000LL, 16, "RightPlug Alliance USA" }, | |
175 | { 0xE02E000000000000LL, 16, "Broadcom Corporation USA" }, | |
176 | { 0xE02F000000000000LL, 16, "MStar Semiconductor, Inc Taiwan, ROC" }, | |
177 | { 0xE030000000000000LL, 16, "BeeDar Technology Inc. USA" }, | |
178 | { 0xE031000000000000LL, 16, "RFIDsec Denmark" }, | |
179 | { 0xE032000000000000LL, 16, "Schweizer Electronic AG Germany" }, | |
180 | { 0xE033000000000000LL, 16, "AMIC Technology Corp Taiwan" }, | |
181 | { 0xE034000000000000LL, 16, "Mikron JSC Russia" }, | |
182 | { 0xE035000000000000LL, 16, "Fraunhofer Institute for Photonic Microsystems Germany" }, | |
183 | { 0xE036000000000000LL, 16, "IDS Microchip AG Switzerland" }, | |
184 | { 0xE037000000000000LL, 16, "Kovio USA" }, | |
185 | { 0xE038000000000000LL, 16, "HMT Microelectronic Ltd Switzerland Identifier Company Country" }, | |
186 | { 0xE039000000000000LL, 16, "Silicon Craft Technology Thailand" }, | |
187 | { 0xE03A000000000000LL, 16, "Advanced Film Device Inc. Japan" }, | |
188 | { 0xE03B000000000000LL, 16, "Nitecrest Ltd UK" }, | |
189 | { 0xE03C000000000000LL, 16, "Verayo Inc. USA" }, | |
190 | { 0xE03D000000000000LL, 16, "HID Global USA" }, | |
191 | { 0xE03E000000000000LL, 16, "Productivity Engineering Gmbh Germany" }, | |
192 | { 0xE03F000000000000LL, 16, "Austriamicrosystems AG (reserved) Austria" }, | |
193 | { 0xE040000000000000LL, 16, "Gemalto SA France" }, | |
194 | { 0xE041000000000000LL, 16, "Renesas Electronics Corporation Japan" }, | |
195 | { 0xE042000000000000LL, 16, "3Alogics Inc Korea" }, | |
196 | { 0xE043000000000000LL, 16, "Top TroniQ Asia Limited Hong Kong" }, | |
197 | { 0xE044000000000000LL, 16, "Gentag Inc (USA) USA" }, | |
198 | { 0,0,"no tag-info available" } // must be the last entry | |
199 | }; | |
200 | ||
201 | ||
202 | // fast method to just read the UID of a tag (collission detection not supported) | |
203 | // *buf should be large enough to fit the 64bit uid | |
204 | // returns 1 if suceeded | |
205 | int getUID(uint8_t *buf) | |
206 | { | |
207 | UsbCommand resp; | |
208 | uint8_t *recv; | |
209 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
210 | uint8_t *req=c.d.asBytes; | |
211 | int reqlen=0; | |
212 | ||
213 | for (int retry=0;retry<3; retry++) { // don't give up the at the first try | |
214 | ||
215 | req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
216 | ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1; | |
217 | req[1]=ISO15_CMD_INVENTORY; | |
218 | req[2]=0; // mask length | |
219 | reqlen=AddCrc(req,3); | |
220 | c.arg[0]=reqlen; | |
221 | ||
222 | SendCommand(&c); | |
223 | ||
224 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { | |
225 | recv = resp.d.asBytes; | |
226 | if (resp.arg[0]>=12 && ISO15_CRC_CHECK==Crc(recv,12)) { | |
227 | memcpy(buf,&recv[2],8); | |
228 | return 1; | |
229 | } | |
230 | } | |
231 | } // retry | |
232 | return 0; | |
233 | } | |
234 | ||
235 | ||
236 | ||
237 | // get a product description based on the UID | |
238 | // uid[8] tag uid | |
239 | // returns description of the best match | |
240 | static char* getTagInfo(uint8_t *uid) { | |
241 | uint64_t myuid,mask; | |
242 | int i=0, best=-1; | |
243 | memcpy(&myuid,uid,sizeof(uint64_t)); | |
244 | while (uidmapping[i].mask>0) { | |
245 | mask=(~0LL) <<(64-uidmapping[i].mask); | |
246 | if ((myuid & mask) == uidmapping[i].uid) { | |
247 | if (best==-1) { | |
248 | best=i; | |
249 | } else { | |
250 | if (uidmapping[i].mask>uidmapping[best].mask) { | |
251 | best=i; | |
252 | } | |
253 | } | |
254 | } | |
255 | i++; | |
256 | } | |
257 | ||
258 | if (best>=0) return uidmapping[best].desc; | |
259 | ||
260 | return uidmapping[i].desc; | |
261 | } | |
262 | ||
263 | ||
264 | // return a clear-text message to an errorcode | |
265 | static char* TagErrorStr(uint8_t error) { | |
266 | switch (error) { | |
267 | case 0x01: return "The command is not supported"; | |
268 | case 0x02: return "The command is not recognised"; | |
269 | case 0x03: return "The option is not supported."; | |
270 | case 0x0f: return "Unknown error."; | |
271 | case 0x10: return "The specified block is not available (doesn’t exist)."; | |
272 | case 0x11: return "The specified block is already -locked and thus cannot be locked again"; | |
273 | case 0x12: return "The specified block is locked and its content cannot be changed."; | |
274 | case 0x13: return "The specified block was not successfully programmed."; | |
275 | case 0x14: return "The specified block was not successfully locked."; | |
276 | default: return "Reserved for Future Use or Custom command error."; | |
277 | } | |
278 | } | |
279 | ||
280 | ||
281 | // Mode 3 | |
282 | int CmdHF15Demod(const char *Cmd) | |
283 | { | |
284 | // The sampling rate is 106.353 ksps/s, for T = 18.8 us | |
285 | ||
286 | int i, j; | |
287 | int max = 0, maxPos = 0; | |
288 | ||
289 | int skip = 4; | |
290 | ||
291 | if (GraphTraceLen < 1000) return 0; | |
292 | ||
293 | // First, correlate for SOF | |
294 | for (i = 0; i < 100; i++) { | |
295 | int corr = 0; | |
296 | for (j = 0; j < ARRAYLEN(FrameSOF); j += skip) { | |
297 | corr += FrameSOF[j] * GraphBuffer[i + (j / skip)]; | |
298 | } | |
299 | if (corr > max) { | |
300 | max = corr; | |
301 | maxPos = i; | |
302 | } | |
303 | } | |
304 | PrintAndLog("SOF at %d, correlation %d", maxPos, max / (ARRAYLEN(FrameSOF) / skip)); | |
305 | ||
306 | i = maxPos + ARRAYLEN(FrameSOF) / skip; | |
307 | int k = 0; | |
308 | uint8_t outBuf[20]; | |
309 | memset(outBuf, 0, sizeof(outBuf)); | |
310 | uint8_t mask = 0x01; | |
311 | for (;;) { | |
312 | int corr0 = 0, corr1 = 0, corrEOF = 0; | |
313 | for (j = 0; j < ARRAYLEN(Logic0); j += skip) { | |
314 | corr0 += Logic0[j] * GraphBuffer[i + (j / skip)]; | |
315 | } | |
316 | for (j = 0; j < ARRAYLEN(Logic1); j += skip) { | |
317 | corr1 += Logic1[j] * GraphBuffer[i + (j / skip)]; | |
318 | } | |
319 | for (j = 0; j < ARRAYLEN(FrameEOF); j += skip) { | |
320 | corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)]; | |
321 | } | |
322 | // Even things out by the length of the target waveform. | |
323 | corr0 *= 4; | |
324 | corr1 *= 4; | |
325 | ||
326 | if (corrEOF > corr1 && corrEOF > corr0) { | |
327 | PrintAndLog("EOF at %d", i); | |
328 | break; | |
329 | } else if (corr1 > corr0) { | |
330 | i += ARRAYLEN(Logic1) / skip; | |
331 | outBuf[k] |= mask; | |
332 | } else { | |
333 | i += ARRAYLEN(Logic0) / skip; | |
334 | } | |
335 | mask <<= 1; | |
336 | if (mask == 0) { | |
337 | k++; | |
338 | mask = 0x01; | |
339 | } | |
340 | if ((i + (int)ARRAYLEN(FrameEOF)) >= GraphTraceLen) { | |
341 | PrintAndLog("ran off end!"); | |
342 | break; | |
343 | } | |
344 | } | |
345 | if (mask != 0x01) { | |
346 | PrintAndLog("error, uneven octet! (discard extra bits!)"); | |
347 | PrintAndLog(" mask=%02x", mask); | |
348 | } | |
349 | PrintAndLog("%d octets", k); | |
350 | ||
351 | for (i = 0; i < k; i++) { | |
352 | PrintAndLog("# %2d: %02x ", i, outBuf[i]); | |
353 | } | |
354 | PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2)); | |
355 | return 0; | |
356 | } | |
357 | ||
358 | ||
359 | ||
360 | // * Acquire Samples as Reader (enables carrier, sends inquiry) | |
361 | int CmdHF15Read(const char *Cmd) | |
362 | { | |
363 | UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693}; | |
364 | SendCommand(&c); | |
365 | return 0; | |
366 | } | |
367 | ||
368 | // Record Activity without enabeling carrier | |
369 | int CmdHF15Record(const char *Cmd) | |
370 | { | |
371 | UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693}; | |
372 | SendCommand(&c); | |
373 | return 0; | |
374 | } | |
375 | ||
376 | int HF15Reader(const char *Cmd, bool verbose) | |
377 | { | |
378 | uint8_t uid[8]; | |
379 | ||
380 | if (!getUID(uid)) { | |
381 | if (verbose) PrintAndLog("No Tag found."); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | PrintAndLog("Tag UID : %s",sprintUID(NULL,uid)); | |
386 | PrintAndLog("Tag Info: %s",getTagInfo(uid)); | |
387 | return 1; | |
388 | } | |
389 | ||
390 | int CmdHF15Reader(const char *Cmd) | |
391 | { | |
392 | UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}}; | |
393 | SendCommand(&c); | |
394 | return 0; | |
395 | } | |
396 | ||
397 | // Simulation is still not working very good | |
398 | int CmdHF15Sim(const char *Cmd) | |
399 | { | |
400 | char cmdp = param_getchar(Cmd, 0); | |
401 | uint8_t uid[8] = {0x00}; | |
402 | ||
403 | //E0 16 24 00 00 00 00 00 | |
404 | if (cmdp == 'h' || cmdp == 'H') { | |
405 | PrintAndLog("Usage: hf 15 sim <UID>"); | |
406 | PrintAndLog(""); | |
407 | PrintAndLog(" sample: hf 15 sim E016240000000000"); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | if (param_gethex(Cmd, 0, uid, 16)) { | |
412 | PrintAndLog("UID must include 16 HEX symbols"); | |
413 | return 0; | |
414 | } | |
415 | ||
416 | PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X", | |
417 | uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]); | |
418 | ||
419 | UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}}; | |
420 | memcpy(c.d.asBytes,uid,8); | |
421 | ||
422 | SendCommand(&c); | |
423 | return 0; | |
424 | } | |
425 | ||
426 | // finds the AFI (Application Family Idendifier) of a card, by trying all values | |
427 | // (There is no standard way of reading the AFI, allthough some tags support this) | |
428 | int CmdHF15Afi(const char *Cmd) | |
429 | { | |
430 | UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}}; | |
431 | SendCommand(&c); | |
432 | return 0; | |
433 | } | |
434 | ||
435 | // Reads all memory pages | |
436 | int CmdHF15DumpMem(const char*Cmd) { | |
437 | ||
438 | uint8_t uid[8] = {0,0,0,0,0,0,0,0}; | |
439 | uint8_t *recv = NULL; | |
440 | UsbCommand resp; | |
441 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
442 | uint8_t *req = c.d.asBytes; | |
443 | int reqlen = 0, blocknum = 0; | |
444 | char output[80] = {0}; | |
445 | ||
446 | if (!getUID(uid)) { | |
447 | PrintAndLog("No Tag found."); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid)); | |
452 | PrintAndLog("Tag Info: %s",getTagInfo(uid)); | |
453 | ||
454 | for (int retry=0; retry<5; retry++) { | |
455 | ||
456 | req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
457 | ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS; | |
458 | req[1]=ISO15_CMD_READ; | |
459 | memcpy(&req[2],uid,8); | |
460 | req[10]=blocknum; | |
461 | reqlen=AddCrc(req,11); | |
462 | c.arg[0]=reqlen; | |
463 | ||
464 | SendCommand(&c); | |
465 | ||
466 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { | |
467 | recv = resp.d.asBytes; | |
468 | if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) { | |
469 | if (!(recv[0] & ISO15_RES_ERROR)) { | |
470 | retry = 0; | |
471 | *output = 0; // reset outputstring | |
472 | sprintf(output, "Block %02x ",blocknum); | |
473 | for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex | |
474 | sprintf(output+strlen(output),"%02X ",recv[i]); | |
475 | } | |
476 | strcat(output," "); | |
477 | for ( int i = 1; i < resp.arg[0]-2; i++) { // data in cleaned ascii | |
478 | sprintf(output+strlen(output),"%c",(recv[i] > 31 && recv[i] < 127) ? recv[i] : '.'); | |
479 | } | |
480 | PrintAndLog("%s",output); | |
481 | blocknum++; | |
482 | // PrintAndLog("bn=%i",blocknum); | |
483 | } else { | |
484 | PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); | |
485 | return 1; | |
486 | } | |
487 | } // else PrintAndLog("crc"); | |
488 | } // else PrintAndLog("r null"); | |
489 | } // retry | |
490 | // TODO: need fix | |
491 | // if (resp.arg[0]<3) | |
492 | // PrintAndLog("Lost Connection"); | |
493 | // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0])) | |
494 | // PrintAndLog("CRC Failed"); | |
495 | // else | |
496 | // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); | |
497 | return 1; | |
498 | } | |
499 | ||
500 | ||
501 | // "HF 15" interface | |
502 | ||
503 | static command_t CommandTable15[] = | |
504 | { | |
505 | {"help", CmdHF15Help, 1, "This help"}, | |
506 | {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"}, | |
507 | {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"}, | |
508 | {"record", CmdHF15Record, 0, "Record Samples (ISO 15693)"}, // atrox | |
509 | {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"}, | |
510 | {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"}, | |
511 | {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"}, | |
512 | {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"}, | |
513 | {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"}, | |
514 | {NULL, NULL, 0, NULL} | |
515 | }; | |
516 | ||
517 | int CmdHF15(const char *Cmd) | |
518 | { | |
519 | CmdsParse(CommandTable15, Cmd); | |
520 | return 0; | |
521 | } | |
522 | ||
523 | int CmdHF15Help(const char *Cmd) | |
524 | { | |
525 | CmdsHelp(CommandTable15); | |
526 | return 0; | |
527 | } | |
528 | ||
529 | ||
530 | // "HF 15 Cmd" Interface | |
531 | // Allows direct communication with the tag on command level | |
532 | ||
533 | int CmdHF15CmdInquiry(const char *Cmd) | |
534 | { | |
535 | UsbCommand resp; | |
536 | uint8_t *recv; | |
537 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
538 | uint8_t *req=c.d.asBytes; | |
539 | int reqlen=0; | |
540 | ||
541 | req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
542 | ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1; | |
543 | req[1]=ISO15_CMD_INVENTORY; | |
544 | req[2]=0; // mask length | |
545 | reqlen=AddCrc(req,3); | |
546 | c.arg[0]=reqlen; | |
547 | ||
548 | SendCommand(&c); | |
549 | ||
550 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { | |
551 | if (resp.arg[0]>=12) { | |
552 | recv = resp.d.asBytes; | |
553 | PrintAndLog("UID=%s",sprintUID(NULL,&recv[2])); | |
554 | PrintAndLog("Tag Info: %s",getTagInfo(&recv[2])); | |
555 | } else { | |
556 | PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]); | |
557 | } | |
558 | } else { | |
559 | PrintAndLog("timeout."); | |
560 | } | |
561 | return 0; | |
562 | } | |
563 | ||
564 | ||
565 | // Turns debugging on(1)/off(0) | |
566 | int CmdHF15CmdDebug( const char *cmd) { | |
567 | int debug=atoi(cmd); | |
568 | if (strlen(cmd)<1) { | |
569 | PrintAndLog("Usage: hf 15 cmd debug <0|1>"); | |
570 | PrintAndLog(" 0 no debugging"); | |
571 | PrintAndLog(" 1 turn debugging on"); | |
572 | return 0; | |
573 | } | |
574 | ||
575 | UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}}; | |
576 | SendCommand(&c); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | ||
581 | int CmdHF15CmdRaw (const char *cmd) { | |
582 | UsbCommand resp; | |
583 | uint8_t *recv; | |
584 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
585 | int reply=1; | |
586 | int fast=1; | |
587 | int crc=0; | |
588 | char buf[5]=""; | |
589 | int i=0; | |
590 | uint8_t data[100]; | |
591 | unsigned int datalen=0, temp; | |
592 | char *hexout; | |
593 | ||
594 | ||
595 | if (strlen(cmd)<3) { | |
596 | PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>"); | |
597 | PrintAndLog(" -r do not read response"); | |
598 | PrintAndLog(" -2 use slower '1 out of 256' mode"); | |
599 | PrintAndLog(" -c calculate and append CRC"); | |
600 | PrintAndLog(" Tip: turn on debugging for verbose output"); | |
601 | return 0; | |
602 | } | |
603 | ||
604 | // strip | |
605 | while (*cmd==' ' || *cmd=='\t') cmd++; | |
606 | ||
607 | while (cmd[i]!='\0') { | |
608 | if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; } | |
609 | if (cmd[i]=='-') { | |
610 | switch (cmd[i+1]) { | |
611 | case 'r': | |
612 | case 'R': | |
613 | reply=0; | |
614 | break; | |
615 | case '2': | |
616 | fast=0; | |
617 | break; | |
618 | case 'c': | |
619 | case 'C': | |
620 | crc=1; | |
621 | break; | |
622 | default: | |
623 | PrintAndLog("Invalid option"); | |
624 | return 0; | |
625 | } | |
626 | i+=2; | |
627 | continue; | |
628 | } | |
629 | if ((cmd[i]>='0' && cmd[i]<='9') || | |
630 | (cmd[i]>='a' && cmd[i]<='f') || | |
631 | (cmd[i]>='A' && cmd[i]<='F') ) { | |
632 | buf[strlen(buf)+1]=0; | |
633 | buf[strlen(buf)]=cmd[i]; | |
634 | i++; | |
635 | ||
636 | if (strlen(buf)>=2) { | |
637 | sscanf(buf,"%x",&temp); | |
638 | data[datalen]=(uint8_t)(temp & 0xff); | |
639 | datalen++; | |
640 | *buf=0; | |
641 | } | |
642 | continue; | |
643 | } | |
644 | PrintAndLog("Invalid char on input"); | |
645 | return 0; | |
646 | } | |
647 | if (crc) datalen=AddCrc(data,datalen); | |
648 | ||
649 | c.arg[0]=datalen; | |
650 | c.arg[1]=fast; | |
651 | c.arg[2]=reply; | |
652 | memcpy(c.d.asBytes,data,datalen); | |
653 | ||
654 | SendCommand(&c); | |
655 | ||
656 | if (reply) { | |
657 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { | |
658 | recv = resp.d.asBytes; | |
659 | PrintAndLog("received %i octets",resp.arg[0]); | |
660 | hexout = (char *)malloc(resp.arg[0] * 3 + 1); | |
661 | if (hexout != NULL) { | |
662 | for (int i = 0; i < resp.arg[0]; i++) { // data in hex | |
663 | sprintf(&hexout[i * 3], "%02X ", recv[i]); | |
664 | } | |
665 | PrintAndLog("%s", hexout); | |
666 | free(hexout); | |
667 | } | |
668 | } else { | |
669 | PrintAndLog("timeout while waiting for reply."); | |
670 | } | |
671 | ||
672 | } // if reply | |
673 | return 0; | |
674 | } | |
675 | ||
676 | ||
677 | /** | |
678 | * parses common HF 15 CMD parameters and prepares some data structures | |
679 | * Parameters: | |
680 | * **cmd command line | |
681 | */ | |
682 | int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) { | |
683 | int temp; | |
684 | uint8_t *req = c->d.asBytes; | |
685 | uint8_t uid[8] = {0x00}; | |
686 | uint32_t reqlen = 0; | |
687 | ||
688 | // strip | |
689 | while (**cmd==' ' || **cmd=='\t') (*cmd)++; | |
690 | ||
691 | if (strstr(*cmd,"-2")==*cmd) { | |
692 | c->arg[1]=0; // use 1of256 | |
693 | (*cmd)+=2; | |
694 | } | |
695 | ||
696 | // strip | |
697 | while (**cmd==' ' || **cmd=='\t') (*cmd)++; | |
698 | ||
699 | if (strstr(*cmd,"-o")==*cmd) { | |
700 | req[reqlen]=ISO15_REQ_OPTION; | |
701 | (*cmd)+=2; | |
702 | } | |
703 | ||
704 | // strip | |
705 | while (**cmd==' ' || **cmd=='\t') (*cmd)++; | |
706 | ||
707 | switch (**cmd) { | |
708 | case 0: | |
709 | PrintAndLog("missing addr"); | |
710 | return 0; | |
711 | break; | |
712 | case 's': | |
713 | case 'S': | |
714 | // you must have selected the tag earlier | |
715 | req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
716 | ISO15_REQ_NONINVENTORY | ISO15_REQ_SELECT; | |
717 | memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); | |
718 | reqlen+=iso15cmdlen; | |
719 | break; | |
720 | case 'u': | |
721 | case 'U': | |
722 | // unaddressed mode may not be supported by all vendors | |
723 | req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
724 | ISO15_REQ_NONINVENTORY; | |
725 | memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); | |
726 | reqlen+=iso15cmdlen; | |
727 | break; | |
728 | case '*': | |
729 | // we scan for the UID ourself | |
730 | req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
731 | ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS; | |
732 | memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); | |
733 | reqlen+=iso15cmdlen; | |
734 | if (!getUID(uid)) { | |
735 | PrintAndLog("No Tag found"); | |
736 | return 0; | |
737 | } | |
738 | memcpy(req+reqlen,uid,8); | |
739 | PrintAndLog("Detected UID %s",sprintUID(NULL,uid)); | |
740 | reqlen+=8; | |
741 | break; | |
742 | default: | |
743 | req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
744 | ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS; | |
745 | memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); | |
746 | reqlen+=iso15cmdlen; | |
747 | ||
748 | /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX", | |
749 | (short unsigned int *)&uid[7],(short unsigned int *)&uid[6], | |
750 | (short unsigned int *)&uid[5],(short unsigned int *)&uid[4], | |
751 | (short unsigned int *)&uid[3],(short unsigned int *)&uid[2], | |
752 | (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */ | |
753 | for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID | |
754 | sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp); | |
755 | uid[7-i]=temp&0xff; | |
756 | } | |
757 | ||
758 | PrintAndLog("Using UID %s",sprintUID(NULL,uid)); | |
759 | memcpy(&req[reqlen],&uid[0],8); | |
760 | reqlen+=8; | |
761 | } | |
762 | // skip to next space | |
763 | while (**cmd!=' ' && **cmd!='\t') (*cmd)++; | |
764 | // skip over the space | |
765 | while (**cmd==' ' || **cmd=='\t') (*cmd)++; | |
766 | ||
767 | c->arg[0]=reqlen; | |
768 | return 1; | |
769 | } | |
770 | ||
771 | /** | |
772 | * Commandline handling: HF15 CMD SYSINFO | |
773 | * get system information from tag/VICC | |
774 | */ | |
775 | int CmdHF15CmdSysinfo(const char *Cmd) { | |
776 | UsbCommand resp; | |
777 | uint8_t *recv; | |
778 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
779 | uint8_t *req = c.d.asBytes; | |
780 | int reqlen = 0; | |
781 | char cmdbuf[100]; | |
782 | char *cmd = cmdbuf; | |
783 | char output[2048]=""; | |
784 | int i; | |
785 | ||
786 | strncpy(cmd,Cmd,99); | |
787 | ||
788 | // usage: | |
789 | if (strlen(cmd)<1) { | |
790 | PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>"); | |
791 | PrintAndLog(" options:"); | |
792 | PrintAndLog(" -2 use slower '1 out of 256' mode"); | |
793 | PrintAndLog(" uid (either): "); | |
794 | PrintAndLog(" <8B hex> full UID eg E011223344556677"); | |
795 | PrintAndLog(" s selected tag"); | |
796 | PrintAndLog(" u unaddressed mode"); | |
797 | PrintAndLog(" * scan for tag"); | |
798 | PrintAndLog(" start#: page number to start 0-255"); | |
799 | PrintAndLog(" count#: number of pages"); | |
800 | return 0; | |
801 | } | |
802 | ||
803 | prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_SYSINFO},1); | |
804 | reqlen = c.arg[0]; | |
805 | ||
806 | reqlen=AddCrc(req,reqlen); | |
807 | c.arg[0]=reqlen; | |
808 | ||
809 | SendCommand(&c); | |
810 | ||
811 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) { | |
812 | recv = resp.d.asBytes; | |
813 | if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) { | |
814 | if (!(recv[0] & ISO15_RES_ERROR)) { | |
815 | *output=0; // reset outputstring | |
816 | for ( i=1; i<resp.arg[0]-2; i++) { | |
817 | sprintf(output+strlen(output),"%02X ",recv[i]); | |
818 | } | |
819 | strcat(output,"\n\r"); | |
820 | strcat(output,"UID = "); | |
821 | strcat(output,sprintUID(NULL,recv+2)); | |
822 | strcat(output,"\n\r"); | |
823 | strcat(output,getTagInfo(recv+2)); //ABC | |
824 | strcat(output,"\n\r"); | |
825 | i=10; | |
826 | if (recv[1] & 0x01) | |
827 | sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]); | |
828 | else | |
829 | strcat(output,"DSFID not supported\n\r"); | |
830 | if (recv[1] & 0x02) | |
831 | sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]); | |
832 | else | |
833 | strcat(output,"AFI not supported\n\r"); | |
834 | if (recv[1] & 0x04) { | |
835 | strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r"); | |
836 | sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r", | |
837 | (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1); | |
838 | i+=2; | |
839 | } else | |
840 | strcat(output,"Tag does not provide information on memory layout\n\r"); | |
841 | if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]); | |
842 | else strcat(output,"IC reference not given\n\r"); | |
843 | ||
844 | ||
845 | PrintAndLog("%s",output); | |
846 | } else { | |
847 | PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0])); | |
848 | } | |
849 | } else { | |
850 | PrintAndLog("CRC failed"); | |
851 | } | |
852 | } else { | |
853 | PrintAndLog("timeout: no answer"); | |
854 | } | |
855 | ||
856 | return 0; | |
857 | } | |
858 | ||
859 | /** | |
860 | * Commandline handling: HF15 CMD READMULTI | |
861 | * Read multiple blocks at once (not all tags support this) | |
862 | */ | |
863 | int CmdHF15CmdReadmulti(const char *Cmd) { | |
864 | UsbCommand resp; | |
865 | uint8_t *recv; | |
866 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
867 | uint8_t *req=c.d.asBytes; | |
868 | int reqlen=0, pagenum,pagecount; | |
869 | char cmdbuf[100]; | |
870 | char *cmd=cmdbuf; | |
871 | char output[2048]=""; | |
872 | ||
873 | strncpy(cmd,Cmd,99); | |
874 | ||
875 | // usage: | |
876 | if (strlen(cmd)<3) { | |
877 | PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>"); | |
878 | PrintAndLog(" options:"); | |
879 | PrintAndLog(" -2 use slower '1 out of 256' mode"); | |
880 | PrintAndLog(" uid (either): "); | |
881 | PrintAndLog(" <8B hex> full UID eg E011223344556677"); | |
882 | PrintAndLog(" s selected tag"); | |
883 | PrintAndLog(" u unaddressed mode"); | |
884 | PrintAndLog(" * scan for tag"); | |
885 | PrintAndLog(" start#: page number to start 0-255"); | |
886 | PrintAndLog(" count#: number of pages"); | |
887 | return 0; | |
888 | } | |
889 | ||
890 | prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READMULTI},1); | |
891 | reqlen=c.arg[0]; | |
892 | ||
893 | pagenum=strtol(cmd,NULL,0); | |
894 | ||
895 | // skip to next space | |
896 | while (*cmd!=' ' && *cmd!='\t') cmd++; | |
897 | // skip over the space | |
898 | while (*cmd==' ' || *cmd=='\t') cmd++; | |
899 | ||
900 | pagecount=strtol(cmd,NULL,0); | |
901 | if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ... | |
902 | ||
903 | req[reqlen++]=(uint8_t)pagenum; | |
904 | req[reqlen++]=(uint8_t)pagecount; | |
905 | ||
906 | reqlen=AddCrc(req,reqlen); | |
907 | ||
908 | c.arg[0]=reqlen; | |
909 | ||
910 | SendCommand(&c); | |
911 | ||
912 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) { | |
913 | recv = resp.d.asBytes; | |
914 | if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) { | |
915 | if (!(recv[0] & ISO15_RES_ERROR)) { | |
916 | *output=0; // reset outputstring | |
917 | for ( int i=1; i<resp.arg[0]-2; i++) { | |
918 | sprintf(output+strlen(output),"%02X ",recv[i]); | |
919 | } | |
920 | strcat(output," "); | |
921 | for ( int i=1; i<resp.arg[0]-2; i++) { | |
922 | sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.'); | |
923 | } | |
924 | PrintAndLog("%s",output); | |
925 | } else { | |
926 | PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0])); | |
927 | } | |
928 | } else { | |
929 | PrintAndLog("CRC failed"); | |
930 | } | |
931 | } else { | |
932 | PrintAndLog("no answer"); | |
933 | } | |
934 | ||
935 | return 0; | |
936 | } | |
937 | ||
938 | /** | |
939 | * Commandline handling: HF15 CMD READ | |
940 | * Reads a single Block | |
941 | */ | |
942 | int CmdHF15CmdRead(const char *Cmd) { | |
943 | UsbCommand resp; | |
944 | uint8_t *recv; | |
945 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
946 | uint8_t *req=c.d.asBytes; | |
947 | int reqlen=0, pagenum; | |
948 | char cmdbuf[100]; | |
949 | char *cmd=cmdbuf; | |
950 | char output[100]=""; | |
951 | ||
952 | strncpy(cmd,Cmd,99); | |
953 | ||
954 | // usage: | |
955 | if (strlen(cmd)<3) { | |
956 | PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>"); | |
957 | PrintAndLog(" options:"); | |
958 | PrintAndLog(" -2 use slower '1 out of 256' mode"); | |
959 | PrintAndLog(" uid (either): "); | |
960 | PrintAndLog(" <8B hex> full UID eg E011223344556677"); | |
961 | PrintAndLog(" s selected tag"); | |
962 | PrintAndLog(" u unaddressed mode"); | |
963 | PrintAndLog(" * scan for tag"); | |
964 | PrintAndLog(" page#: page number 0-255"); | |
965 | return 0; | |
966 | } | |
967 | ||
968 | prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READ},1); | |
969 | reqlen=c.arg[0]; | |
970 | ||
971 | pagenum=strtol(cmd,NULL,0); | |
972 | /*if (pagenum<0) { | |
973 | PrintAndLog("invalid pagenum"); | |
974 | return 0; | |
975 | } */ | |
976 | ||
977 | req[reqlen++]=(uint8_t)pagenum; | |
978 | ||
979 | reqlen=AddCrc(req,reqlen); | |
980 | ||
981 | c.arg[0]=reqlen; | |
982 | ||
983 | SendCommand(&c); | |
984 | ||
985 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) { | |
986 | recv = resp.d.asBytes; | |
987 | if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) { | |
988 | if (!(recv[0] & ISO15_RES_ERROR)) { | |
989 | *output=0; // reset outputstring | |
990 | //sprintf(output, "Block %2i ",blocknum); | |
991 | for ( int i=1; i<resp.arg[0]-2; i++) { | |
992 | sprintf(output+strlen(output),"%02X ",recv[i]); | |
993 | } | |
994 | strcat(output," "); | |
995 | for ( int i=1; i<resp.arg[0]-2; i++) { | |
996 | sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.'); | |
997 | } | |
998 | PrintAndLog("%s",output); | |
999 | } else { | |
1000 | PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); | |
1001 | } | |
1002 | } else { | |
1003 | PrintAndLog("CRC failed"); | |
1004 | } | |
1005 | } else { | |
1006 | PrintAndLog("no answer"); | |
1007 | } | |
1008 | ||
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | ||
1013 | /** | |
1014 | * Commandline handling: HF15 CMD WRITE | |
1015 | * Writes a single Block - might run into timeout, even when successful | |
1016 | */ | |
1017 | int CmdHF15CmdWrite(const char *Cmd) { | |
1018 | UsbCommand resp; | |
1019 | uint8_t *recv; | |
1020 | UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? | |
1021 | uint8_t *req=c.d.asBytes; | |
1022 | int reqlen=0, pagenum, temp; | |
1023 | char cmdbuf[100]; | |
1024 | char *cmd=cmdbuf; | |
1025 | char *cmd2; | |
1026 | ||
1027 | strncpy(cmd,Cmd,99); | |
1028 | ||
1029 | // usage: | |
1030 | if (strlen(cmd)<3) { | |
1031 | PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>"); | |
1032 | PrintAndLog(" options:"); | |
1033 | PrintAndLog(" -2 use slower '1 out of 256' mode"); | |
1034 | PrintAndLog(" -o set OPTION Flag (needed for TI)"); | |
1035 | PrintAndLog(" uid (either): "); | |
1036 | PrintAndLog(" <8B hex> full UID eg E011223344556677"); | |
1037 | PrintAndLog(" s selected tag"); | |
1038 | PrintAndLog(" u unaddressed mode"); | |
1039 | PrintAndLog(" * scan for tag"); | |
1040 | PrintAndLog(" page#: page number 0-255"); | |
1041 | PrintAndLog(" hexdata: data to be written eg AA BB CC DD"); | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_WRITE},1); | |
1046 | reqlen=c.arg[0]; | |
1047 | ||
1048 | // *cmd -> page num ; *cmd2 -> data | |
1049 | cmd2=cmd; | |
1050 | while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++; | |
1051 | *cmd2=0; | |
1052 | cmd2++; | |
1053 | ||
1054 | pagenum=strtol(cmd,NULL,0); | |
1055 | /*if (pagenum<0) { | |
1056 | PrintAndLog("invalid pagenum"); | |
1057 | return 0; | |
1058 | } */ | |
1059 | req[reqlen++]=(uint8_t)pagenum; | |
1060 | ||
1061 | ||
1062 | while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars | |
1063 | if (*cmd2==' ') { | |
1064 | cmd2++; | |
1065 | continue; | |
1066 | } | |
1067 | sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp); | |
1068 | req[reqlen++]=temp & 0xff; | |
1069 | cmd2+=2; | |
1070 | } | |
1071 | ||
1072 | reqlen=AddCrc(req,reqlen); | |
1073 | ||
1074 | c.arg[0]=reqlen; | |
1075 | ||
1076 | SendCommand(&c); | |
1077 | ||
1078 | if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) { | |
1079 | recv = resp.d.asBytes; | |
1080 | if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) { | |
1081 | if (!(recv[0] & ISO15_RES_ERROR)) { | |
1082 | PrintAndLog("OK"); | |
1083 | } else { | |
1084 | PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); | |
1085 | } | |
1086 | } else { | |
1087 | PrintAndLog("CRC failed"); | |
1088 | } | |
1089 | } else { | |
1090 | PrintAndLog("timeout: no answer - data may be written anyway"); | |
1091 | } | |
1092 | ||
1093 | return 0; | |
1094 | } | |
1095 | ||
1096 | ||
1097 | ||
1098 | static command_t CommandTable15Cmd[] = | |
1099 | { | |
1100 | {"help", CmdHF15CmdHelp, 1, "This Help"}, | |
1101 | {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"}, | |
1102 | /* | |
1103 | {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"}, | |
1104 | */ | |
1105 | {"read", CmdHF15CmdRead, 0, "Read a block"}, | |
1106 | {"write", CmdHF15CmdWrite, 0, "Write a block"}, | |
1107 | {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"}, | |
1108 | {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"}, | |
1109 | {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"}, | |
1110 | {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"}, | |
1111 | {NULL, NULL, 0, NULL} | |
1112 | }; | |
1113 | ||
1114 | int CmdHF15Cmd(const char *Cmd) | |
1115 | { | |
1116 | CmdsParse(CommandTable15Cmd, Cmd); | |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | int CmdHF15CmdHelp(const char *Cmd) | |
1121 | { | |
1122 | CmdsHelp(CommandTable15Cmd); | |
1123 | return 0; | |
1124 | } | |
1125 |