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