Merge pull request #75 from pwpiwi/qtmakefile
[legacy-proxmark3.git] / client / cmdhf15.c
blobc3ff7dd6d3976574b668ab5ec13fa5c0b398f8b3
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.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
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"
40 #define FrameSOF Iso15693FrameSOF
41 #define Logic0 Iso15693Logic0
42 #define Logic1 Iso15693Logic1
43 #define FrameEOF Iso15693FrameEOF
45 #define Crc(data,datalen) Iso15693Crc(data,datalen)
46 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
47 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
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;
57 const productName uidmapping[] = {
59 // UID, #significant Bits, "Vendor(+Product)"
60 { 0xE001000000000000LL, 16, "Motorola UK" },
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]"},
71 { 0xE003000000000000LL, 16, "Hitachi, Ltd Japan" },
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)" },
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]"},
100 { 0xE006000000000000LL, 16, "Cylink USA" },
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" },
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" },
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" },
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" },
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
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)
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;
213 for (int retry=0;retry<3; retry++) { // don't give up the at the first try
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;
222 SendCommand(&c);
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;
231 } // retry
232 return 0;
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;
255 i++;
258 if (best>=0) return uidmapping[best].desc;
260 return uidmapping[i].desc;
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.";
281 // Mode 3
282 int CmdHF15Demod(const char *Cmd)
284 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
286 int i, j;
287 int max = 0, maxPos = 0;
289 int skip = 4;
291 if (GraphTraceLen < 1000) return 0;
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)];
299 if (corr > max) {
300 max = corr;
301 maxPos = i;
304 PrintAndLog("SOF at %d, correlation %d", maxPos,
305 max / (arraylen(FrameSOF) / skip));
307 i = maxPos + arraylen(FrameSOF) / skip;
308 int k = 0;
309 uint8_t outBuf[20];
310 memset(outBuf, 0, sizeof(outBuf));
311 uint8_t mask = 0x01;
312 for (;;) {
313 int corr0 = 0, corr1 = 0, corrEOF = 0;
314 for (j = 0; j < arraylen(Logic0); j += skip) {
315 corr0 += Logic0[j] * GraphBuffer[i + (j / skip)];
317 for (j = 0; j < arraylen(Logic1); j += skip) {
318 corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
320 for (j = 0; j < arraylen(FrameEOF); j += skip) {
321 corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
323 // Even things out by the length of the target waveform.
324 corr0 *= 4;
325 corr1 *= 4;
327 if (corrEOF > corr1 && corrEOF > corr0) {
328 PrintAndLog("EOF at %d", i);
329 break;
330 } else if (corr1 > corr0) {
331 i += arraylen(Logic1) / skip;
332 outBuf[k] |= mask;
333 } else {
334 i += arraylen(Logic0) / skip;
336 mask <<= 1;
337 if (mask == 0) {
338 k++;
339 mask = 0x01;
341 if ((i + (int)arraylen(FrameEOF)) >= GraphTraceLen) {
342 PrintAndLog("ran off end!");
343 break;
346 if (mask != 0x01) {
347 PrintAndLog("error, uneven octet! (discard extra bits!)");
348 PrintAndLog(" mask=%02x", mask);
350 PrintAndLog("%d octets", k);
352 for (i = 0; i < k; i++) {
353 PrintAndLog("# %2d: %02x ", i, outBuf[i]);
355 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
356 return 0;
361 // * Acquire Samples as Reader (enables carrier, sends inquiry)
362 int CmdHF15Read(const char *Cmd)
364 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
365 SendCommand(&c);
366 return 0;
369 // Record Activity without enabeling carrier
370 int CmdHF15Record(const char *Cmd)
372 UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693};
373 SendCommand(&c);
374 return 0;
377 int CmdHF15Reader(const char *Cmd)
379 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
380 SendCommand(&c);
381 return 0;
384 // Simulation is still not working very good
385 int CmdHF15Sim(const char *Cmd)
387 char cmdp = param_getchar(Cmd, 0);
388 uint8_t uid[8] = {0x00};
390 //E0 16 24 00 00 00 00 00
391 if (cmdp == 'h' || cmdp == 'H') {
392 PrintAndLog("Usage: hf 15 sim <UID>");
393 PrintAndLog("");
394 PrintAndLog(" sample: hf 15 sim E016240000000000");
395 return 0;
398 if (param_gethex(Cmd, 0, uid, 16)) {
399 PrintAndLog("UID must include 16 HEX symbols");
400 return 0;
403 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
404 uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]);
406 UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
407 memcpy(c.d.asBytes,uid,8);
409 SendCommand(&c);
410 return 0;
413 // finds the AFI (Application Family Idendifier) of a card, by trying all values
414 // (There is no standard way of reading the AFI, allthough some tags support this)
415 int CmdHF15Afi(const char *Cmd)
417 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
418 SendCommand(&c);
419 return 0;
422 // Reads all memory pages
423 int CmdHF15DumpMem(const char*Cmd) {
424 UsbCommand resp;
425 uint8_t uid[8];
426 uint8_t *recv=NULL;
427 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
428 uint8_t *req=c.d.asBytes;
429 int reqlen=0;
430 int blocknum=0;
431 char output[80];
433 if (!getUID(uid)) {
434 PrintAndLog("No Tag found.");
435 return 0;
438 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid));
439 PrintAndLog("Tag Info: %s",getTagInfo(uid));
441 for (int retry=0; retry<5; retry++) {
443 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
444 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
445 req[1]=ISO15_CMD_READ;
446 memcpy(&req[2],uid,8);
447 req[10]=blocknum;
448 reqlen=AddCrc(req,11);
449 c.arg[0]=reqlen;
451 SendCommand(&c);
453 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
454 recv = resp.d.asBytes;
455 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
456 if (!(recv[0] & ISO15_RES_ERROR)) {
457 retry=0;
458 *output=0; // reset outputstring
459 sprintf(output, "Block %02x ",blocknum);
460 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
461 sprintf(output+strlen(output),"%02X ",recv[i]);
463 strcat(output," ");
464 for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
465 sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');
467 PrintAndLog("%s",output);
468 blocknum++;
469 // PrintAndLog("bn=%i",blocknum);
470 } else {
471 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
472 return 0;
474 } // else PrintAndLog("crc");
475 } // else PrintAndLog("r null");
476 } // retry
477 // TODO: need fix
478 // if (resp.arg[0]<3)
479 // PrintAndLog("Lost Connection");
480 // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
481 // PrintAndLog("CRC Failed");
482 // else
483 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
484 return 0;
488 // "HF 15" interface
490 static command_t CommandTable15[] =
492 {"help", CmdHF15Help, 1, "This help"},
493 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
494 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
495 {"record", CmdHF15Record, 0, "Record Samples (ISO 15693)"}, // atrox
496 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
497 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
498 {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"},
499 {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"},
500 {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"},
501 {NULL, NULL, 0, NULL}
504 int CmdHF15(const char *Cmd)
506 CmdsParse(CommandTable15, Cmd);
507 return 0;
510 int CmdHF15Help(const char *Cmd)
512 CmdsHelp(CommandTable15);
513 return 0;
517 // "HF 15 Cmd" Interface
518 // Allows direct communication with the tag on command level
520 int CmdHF15CmdInquiry(const char *Cmd)
522 UsbCommand resp;
523 uint8_t *recv;
524 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
525 uint8_t *req=c.d.asBytes;
526 int reqlen=0;
528 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
529 ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
530 req[1]=ISO15_CMD_INVENTORY;
531 req[2]=0; // mask length
532 reqlen=AddCrc(req,3);
533 c.arg[0]=reqlen;
535 SendCommand(&c);
537 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
538 if (resp.arg[0]>=12) {
539 recv = resp.d.asBytes;
540 PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
541 PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));
542 } else {
543 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
545 } else {
546 PrintAndLog("timeout.");
548 return 0;
552 // Turns debugging on(1)/off(0)
553 int CmdHF15CmdDebug( const char *cmd) {
554 int debug=atoi(cmd);
555 if (strlen(cmd)<1) {
556 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
557 PrintAndLog(" 0 no debugging");
558 PrintAndLog(" 1 turn debugging on");
559 return 0;
562 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
563 SendCommand(&c);
564 return 0;
568 int CmdHF15CmdRaw (const char *cmd) {
569 UsbCommand resp;
570 uint8_t *recv;
571 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
572 int reply=1;
573 int fast=1;
574 int crc=0;
575 char buf[5]="";
576 int i=0;
577 uint8_t data[100];
578 unsigned int datalen=0, temp;
579 char *hexout;
582 if (strlen(cmd)<3) {
583 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
584 PrintAndLog(" -r do not read response");
585 PrintAndLog(" -2 use slower '1 out of 256' mode");
586 PrintAndLog(" -c calculate and append CRC");
587 PrintAndLog(" Tip: turn on debugging for verbose output");
588 return 0;
591 // strip
592 while (*cmd==' ' || *cmd=='\t') cmd++;
594 while (cmd[i]!='\0') {
595 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
596 if (cmd[i]=='-') {
597 switch (cmd[i+1]) {
598 case 'r':
599 case 'R':
600 reply=0;
601 break;
602 case '2':
603 fast=0;
604 break;
605 case 'c':
606 case 'C':
607 crc=1;
608 break;
609 default:
610 PrintAndLog("Invalid option");
611 return 0;
613 i+=2;
614 continue;
616 if ((cmd[i]>='0' && cmd[i]<='9') ||
617 (cmd[i]>='a' && cmd[i]<='f') ||
618 (cmd[i]>='A' && cmd[i]<='F') ) {
619 buf[strlen(buf)+1]=0;
620 buf[strlen(buf)]=cmd[i];
621 i++;
623 if (strlen(buf)>=2) {
624 sscanf(buf,"%x",&temp);
625 data[datalen]=(uint8_t)(temp & 0xff);
626 datalen++;
627 *buf=0;
629 continue;
631 PrintAndLog("Invalid char on input");
632 return 0;
634 if (crc) datalen=AddCrc(data,datalen);
636 c.arg[0]=datalen;
637 c.arg[1]=fast;
638 c.arg[2]=reply;
639 memcpy(c.d.asBytes,data,datalen);
641 SendCommand(&c);
643 if (reply) {
644 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
645 recv = resp.d.asBytes;
646 PrintAndLog("received %i octets",resp.arg[0]);
647 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
648 if (hexout != NULL) {
649 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
650 sprintf(&hexout[i * 3], "%02X ", recv[i]);
652 PrintAndLog("%s", hexout);
653 free(hexout);
655 } else {
656 PrintAndLog("timeout while waiting for reply.");
659 } // if reply
660 return 0;
665 * parses common HF 15 CMD parameters and prepares some data structures
666 * Parameters:
667 * **cmd command line
669 int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
670 int temp;
671 uint8_t *req=c->d.asBytes;
672 uint8_t uid[8] = {0x00};
673 uint32_t reqlen=0;
675 // strip
676 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
678 if (strstr(*cmd,"-2")==*cmd) {
679 c->arg[1]=0; // use 1of256
680 (*cmd)+=2;
683 // strip
684 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
686 if (strstr(*cmd,"-o")==*cmd) {
687 req[reqlen]=ISO15_REQ_OPTION;
688 (*cmd)+=2;
691 // strip
692 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
694 switch (**cmd) {
695 case 0:
696 PrintAndLog("missing addr");
697 return 0;
698 break;
699 case 's':
700 case 'S':
701 // you must have selected the tag earlier
702 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
703 ISO15_REQ_NONINVENTORY | ISO15_REQ_SELECT;
704 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
705 reqlen+=iso15cmdlen;
706 break;
707 case 'u':
708 case 'U':
709 // unaddressed mode may not be supported by all vendors
710 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
711 ISO15_REQ_NONINVENTORY;
712 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
713 reqlen+=iso15cmdlen;
714 break;
715 case '*':
716 // we scan for the UID ourself
717 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
718 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
719 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
720 reqlen+=iso15cmdlen;
721 if (!getUID(uid)) {
722 PrintAndLog("No Tag found");
723 return 0;
725 memcpy(req+reqlen,uid,8);
726 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
727 reqlen+=8;
728 break;
729 default:
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;
735 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
736 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
737 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
738 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
739 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
740 for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID
741 sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
742 uid[7-i]=temp&0xff;
745 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
746 memcpy(&req[reqlen],&uid[0],8);
747 reqlen+=8;
749 // skip to next space
750 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
751 // skip over the space
752 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
754 c->arg[0]=reqlen;
755 return 1;
759 * Commandline handling: HF15 CMD SYSINFO
760 * get system information from tag/VICC
762 int CmdHF15CmdSysinfo(const char *Cmd) {
763 UsbCommand resp;
764 uint8_t *recv;
765 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
766 uint8_t *req=c.d.asBytes;
767 int reqlen=0;
768 char cmdbuf[100];
769 char *cmd=cmdbuf;
770 char output[2048]="";
771 int i;
773 strncpy(cmd,Cmd,99);
775 // usage:
776 if (strlen(cmd)<1) {
777 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
778 PrintAndLog(" options:");
779 PrintAndLog(" -2 use slower '1 out of 256' mode");
780 PrintAndLog(" uid (either): ");
781 PrintAndLog(" <8B hex> full UID eg E011223344556677");
782 PrintAndLog(" s selected tag");
783 PrintAndLog(" u unaddressed mode");
784 PrintAndLog(" * scan for tag");
785 PrintAndLog(" start#: page number to start 0-255");
786 PrintAndLog(" count#: number of pages");
787 return 0;
790 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_SYSINFO},1);
791 reqlen=c.arg[0];
793 reqlen=AddCrc(req,reqlen);
794 c.arg[0]=reqlen;
796 SendCommand(&c);
798 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
799 recv = resp.d.asBytes;
800 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
801 if (!(recv[0] & ISO15_RES_ERROR)) {
802 *output=0; // reset outputstring
803 for ( i=1; i<resp.arg[0]-2; i++) {
804 sprintf(output+strlen(output),"%02X ",recv[i]);
806 strcat(output,"\n\r");
807 strcat(output,"UID = ");
808 strcat(output,sprintUID(NULL,recv+2));
809 strcat(output,"\n\r");
810 strcat(output,getTagInfo(recv+2)); //ABC
811 strcat(output,"\n\r");
812 i=10;
813 if (recv[1] & 0x01)
814 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
815 else
816 strcat(output,"DSFID not supported\n\r");
817 if (recv[1] & 0x02)
818 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
819 else
820 strcat(output,"AFI not supported\n\r");
821 if (recv[1] & 0x04) {
822 strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r");
823 sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r",
824 (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1);
825 i+=2;
826 } else
827 strcat(output,"Tag does not provide information on memory layout\n\r");
828 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
829 else strcat(output,"IC reference not given\n\r");
832 PrintAndLog("%s",output);
833 } else {
834 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
836 } else {
837 PrintAndLog("CRC failed");
839 } else {
840 PrintAndLog("timeout: no answer");
843 return 0;
847 * Commandline handling: HF15 CMD READMULTI
848 * Read multiple blocks at once (not all tags support this)
850 int CmdHF15CmdReadmulti(const char *Cmd) {
851 UsbCommand resp;
852 uint8_t *recv;
853 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
854 uint8_t *req=c.d.asBytes;
855 int reqlen=0, pagenum,pagecount;
856 char cmdbuf[100];
857 char *cmd=cmdbuf;
858 char output[2048]="";
860 strncpy(cmd,Cmd,99);
862 // usage:
863 if (strlen(cmd)<3) {
864 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
865 PrintAndLog(" options:");
866 PrintAndLog(" -2 use slower '1 out of 256' mode");
867 PrintAndLog(" uid (either): ");
868 PrintAndLog(" <8B hex> full UID eg E011223344556677");
869 PrintAndLog(" s selected tag");
870 PrintAndLog(" u unaddressed mode");
871 PrintAndLog(" * scan for tag");
872 PrintAndLog(" start#: page number to start 0-255");
873 PrintAndLog(" count#: number of pages");
874 return 0;
877 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READMULTI},1);
878 reqlen=c.arg[0];
880 pagenum=strtol(cmd,NULL,0);
882 // skip to next space
883 while (*cmd!=' ' && *cmd!='\t') cmd++;
884 // skip over the space
885 while (*cmd==' ' || *cmd=='\t') cmd++;
887 pagecount=strtol(cmd,NULL,0);
888 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
890 req[reqlen++]=(uint8_t)pagenum;
891 req[reqlen++]=(uint8_t)pagecount;
893 reqlen=AddCrc(req,reqlen);
895 c.arg[0]=reqlen;
897 SendCommand(&c);
899 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
900 recv = resp.d.asBytes;
901 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
902 if (!(recv[0] & ISO15_RES_ERROR)) {
903 *output=0; // reset outputstring
904 for ( int i=1; i<resp.arg[0]-2; i++) {
905 sprintf(output+strlen(output),"%02X ",recv[i]);
907 strcat(output," ");
908 for ( int i=1; i<resp.arg[0]-2; i++) {
909 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
911 PrintAndLog("%s",output);
912 } else {
913 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
915 } else {
916 PrintAndLog("CRC failed");
918 } else {
919 PrintAndLog("no answer");
922 return 0;
926 * Commandline handling: HF15 CMD READ
927 * Reads a single Block
929 int CmdHF15CmdRead(const char *Cmd) {
930 UsbCommand resp;
931 uint8_t *recv;
932 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
933 uint8_t *req=c.d.asBytes;
934 int reqlen=0, pagenum;
935 char cmdbuf[100];
936 char *cmd=cmdbuf;
937 char output[100]="";
939 strncpy(cmd,Cmd,99);
941 // usage:
942 if (strlen(cmd)<3) {
943 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
944 PrintAndLog(" options:");
945 PrintAndLog(" -2 use slower '1 out of 256' mode");
946 PrintAndLog(" uid (either): ");
947 PrintAndLog(" <8B hex> full UID eg E011223344556677");
948 PrintAndLog(" s selected tag");
949 PrintAndLog(" u unaddressed mode");
950 PrintAndLog(" * scan for tag");
951 PrintAndLog(" page#: page number 0-255");
952 return 0;
955 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READ},1);
956 reqlen=c.arg[0];
958 pagenum=strtol(cmd,NULL,0);
959 /*if (pagenum<0) {
960 PrintAndLog("invalid pagenum");
961 return 0;
962 } */
964 req[reqlen++]=(uint8_t)pagenum;
966 reqlen=AddCrc(req,reqlen);
968 c.arg[0]=reqlen;
970 SendCommand(&c);
972 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
973 recv = resp.d.asBytes;
974 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
975 if (!(recv[0] & ISO15_RES_ERROR)) {
976 *output=0; // reset outputstring
977 //sprintf(output, "Block %2i ",blocknum);
978 for ( int i=1; i<resp.arg[0]-2; i++) {
979 sprintf(output+strlen(output),"%02X ",recv[i]);
981 strcat(output," ");
982 for ( int i=1; i<resp.arg[0]-2; i++) {
983 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
985 PrintAndLog("%s",output);
986 } else {
987 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
989 } else {
990 PrintAndLog("CRC failed");
992 } else {
993 PrintAndLog("no answer");
996 return 0;
1001 * Commandline handling: HF15 CMD WRITE
1002 * Writes a single Block - might run into timeout, even when successful
1004 int CmdHF15CmdWrite(const char *Cmd) {
1005 UsbCommand resp;
1006 uint8_t *recv;
1007 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
1008 uint8_t *req=c.d.asBytes;
1009 int reqlen=0, pagenum, temp;
1010 char cmdbuf[100];
1011 char *cmd=cmdbuf;
1012 char *cmd2;
1014 strncpy(cmd,Cmd,99);
1016 // usage:
1017 if (strlen(cmd)<3) {
1018 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
1019 PrintAndLog(" options:");
1020 PrintAndLog(" -2 use slower '1 out of 256' mode");
1021 PrintAndLog(" -o set OPTION Flag (needed for TI)");
1022 PrintAndLog(" uid (either): ");
1023 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1024 PrintAndLog(" s selected tag");
1025 PrintAndLog(" u unaddressed mode");
1026 PrintAndLog(" * scan for tag");
1027 PrintAndLog(" page#: page number 0-255");
1028 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
1029 return 0;
1032 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_WRITE},1);
1033 reqlen=c.arg[0];
1035 // *cmd -> page num ; *cmd2 -> data
1036 cmd2=cmd;
1037 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
1038 *cmd2=0;
1039 cmd2++;
1041 pagenum=strtol(cmd,NULL,0);
1042 /*if (pagenum<0) {
1043 PrintAndLog("invalid pagenum");
1044 return 0;
1045 } */
1046 req[reqlen++]=(uint8_t)pagenum;
1049 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
1050 if (*cmd2==' ') {
1051 cmd2++;
1052 continue;
1054 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
1055 req[reqlen++]=temp & 0xff;
1056 cmd2+=2;
1059 reqlen=AddCrc(req,reqlen);
1061 c.arg[0]=reqlen;
1063 SendCommand(&c);
1065 if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
1066 recv = resp.d.asBytes;
1067 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
1068 if (!(recv[0] & ISO15_RES_ERROR)) {
1069 PrintAndLog("OK");
1070 } else {
1071 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
1073 } else {
1074 PrintAndLog("CRC failed");
1076 } else {
1077 PrintAndLog("timeout: no answer - data may be written anyway");
1080 return 0;
1085 static command_t CommandTable15Cmd[] =
1087 {"help", CmdHF15CmdHelp, 1, "This Help"},
1088 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
1090 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1092 {"read", CmdHF15CmdRead, 0, "Read a block"},
1093 {"write", CmdHF15CmdWrite, 0, "Write a block"},
1094 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
1095 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
1096 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
1097 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
1098 {NULL, NULL, 0, NULL}
1101 int CmdHF15Cmd(const char *Cmd)
1103 CmdsParse(CommandTable15Cmd, Cmd);
1104 return 0;
1107 int CmdHF15CmdHelp(const char *Cmd)
1109 CmdsHelp(CommandTable15Cmd);
1110 return 0;