]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf15.c
FIX: lf hitag : Mea culpa, simulation should not have reader_field on. thanks to...
[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
7fe9b0b7 25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdint.h>
3fe4ff4f 29
902cb3c0 30#include "proxmark3.h"
7fe9b0b7 31#include "data.h"
32#include "graph.h"
33#include "ui.h"
3fe4ff4f 34#include "util.h"
7fe9b0b7 35#include "cmdparser.h"
36#include "cmdhf15.h"
9455b51c 37#include "iso15693tools.h"
38#include "cmdmain.h"
39
52eeaef5 40#define FrameSOF Iso15693FrameSOF
41#define Logic0 Iso15693Logic0
42#define Logic1 Iso15693Logic1
43#define FrameEOF Iso15693FrameEOF
9455b51c 44
52eeaef5 45#define Crc(data,datalen) Iso15693Crc(data,datalen)
46#define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
9455b51c 47#define sprintUID(target,uid) Iso15693sprintUID(target,uid)
7fe9b0b7 48
9455b51c 49// structure and database for uid -> tagtype lookups
50typedef struct {
51 uint64_t uid;
52 int mask; // how many MSB bits used
53 char* desc;
54} productName;
55
56
57const productName uidmapping[] = {
52ab55ab 58
0546b4aa 59 // UID, #significant Bits, "Vendor(+Product)"
52ab55ab 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)" },
3fe4ff4f 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)" },
52ab55ab 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" },
9455b51c 112 { 0xE007000000000000LL, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
113 { 0xE007100000000000LL, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
0546b4aa 114 { 0xE007800000000000LL, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
9455b51c 115 { 0xE007C00000000000LL, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
116 { 0xE007C40000000000LL, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
52ab55ab 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
c71e7235 129 { 0xE012000000000000LL, 16, "HID Corporation" },
52ab55ab 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
5b3811fc 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
52ab55ab 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" },
595b623b 178 { 0xE031000000000000LL, 16, "RFIDsec Denmark" },
179 { 0xE032000000000000LL, 16, "Schweizer Electronic AG Germany" },
180 { 0xE033000000000000LL, 16, "AMIC Technology Corp Taiwan" },
52ab55ab 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" },
9455b51c 198 { 0,0,"no tag-info available" } // must be the last entry
199};
200
201
e4da8ed0 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
9455b51c 204// returns 1 if suceeded
205int getUID(uint8_t *buf)
7fe9b0b7 206{
902cb3c0 207 UsbCommand resp;
9455b51c 208 uint8_t *recv;
209 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
7d0efb37 210 uint8_t *req = c.d.asBytes;
211 int reqlen = 0;
9455b51c 212
7d0efb37 213 for (int retry = 0; retry <3; retry++) { // don't give up the at the first try
9455b51c 214
7d0efb37 215 req[0] = ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
216 req[1] = ISO15_CMD_INVENTORY;
217 req[2] = 0; // mask length
218 reqlen = AddCrc(req, 3);
219 c.arg[0] = reqlen;
9455b51c 220
221 SendCommand(&c);
222
7d0efb37 223 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
902cb3c0 224 recv = resp.d.asBytes;
7d0efb37 225 if (resp.arg[0] >= 12 && ISO15_CRC_CHECK == Crc(recv,12)) {
9455b51c 226 memcpy(buf,&recv[2],8);
227 return 1;
228 }
229 }
230 } // retry
231 return 0;
232}
233
234
7fe9b0b7 235
9455b51c 236// get a product description based on the UID
237// uid[8] tag uid
238// returns description of the best match
239static char* getTagInfo(uint8_t *uid) {
7d0efb37 240 uint64_t myuid, mask;
241 int i = 0, best = -1;
242 memcpy(&myuid, uid, sizeof(uint64_t));
243 while (uidmapping[i].mask > 0) {
244 mask = (~0LL) << (64-uidmapping[i].mask);
9455b51c 245 if ((myuid & mask) == uidmapping[i].uid) {
7d0efb37 246 if (best == -1) {
247 best = i;
9455b51c 248 } else {
7d0efb37 249 if (uidmapping[i].mask > uidmapping[best].mask) {
9455b51c 250 best=i;
251 }
252 }
253 }
254 i++;
255 }
7fe9b0b7 256
7d0efb37 257 if (best >= 0)
258 return uidmapping[best].desc;
9455b51c 259 return uidmapping[i].desc;
7fe9b0b7 260}
261
9455b51c 262
b4a9d841 263// return a clear-text message to an errorcode
9455b51c 264static char* TagErrorStr(uint8_t error) {
b4a9d841 265 switch (error) {
266 case 0x01: return "The command is not supported";
267 case 0x02: return "The command is not recognised";
268 case 0x03: return "The option is not supported.";
269 case 0x0f: return "Unknown error.";
270 case 0x10: return "The specified block is not available (doesn’t exist).";
271 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
272 case 0x12: return "The specified block is locked and its content cannot be changed.";
273 case 0x13: return "The specified block was not successfully programmed.";
274 case 0x14: return "The specified block was not successfully locked.";
275 default: return "Reserved for Future Use or Custom command error.";
276 }
9455b51c 277}
278
279
280// Mode 3
7fe9b0b7 281int CmdHF15Demod(const char *Cmd)
282{
9455b51c 283 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
284
285 int i, j;
286 int max = 0, maxPos = 0;
287
288 int skip = 4;
289
290 if (GraphTraceLen < 1000) return 0;
291
292 // First, correlate for SOF
293 for (i = 0; i < 100; i++) {
294 int corr = 0;
52eeaef5 295 for (j = 0; j < ARRAYLEN(FrameSOF); j += skip) {
9455b51c 296 corr += FrameSOF[j] * GraphBuffer[i + (j / skip)];
297 }
298 if (corr > max) {
299 max = corr;
300 maxPos = i;
301 }
302 }
52eeaef5 303 PrintAndLog("SOF at %d, correlation %d", maxPos, max / (ARRAYLEN(FrameSOF) / skip));
9455b51c 304
52eeaef5 305 i = maxPos + ARRAYLEN(FrameSOF) / skip;
9455b51c 306 int k = 0;
307 uint8_t outBuf[20];
308 memset(outBuf, 0, sizeof(outBuf));
309 uint8_t mask = 0x01;
310 for (;;) {
311 int corr0 = 0, corr1 = 0, corrEOF = 0;
52eeaef5 312 for (j = 0; j < ARRAYLEN(Logic0); j += skip) {
9455b51c 313 corr0 += Logic0[j] * GraphBuffer[i + (j / skip)];
314 }
52eeaef5 315 for (j = 0; j < ARRAYLEN(Logic1); j += skip) {
9455b51c 316 corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
317 }
52eeaef5 318 for (j = 0; j < ARRAYLEN(FrameEOF); j += skip) {
9455b51c 319 corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
320 }
321 // Even things out by the length of the target waveform.
322 corr0 *= 4;
323 corr1 *= 4;
324
325 if (corrEOF > corr1 && corrEOF > corr0) {
326 PrintAndLog("EOF at %d", i);
327 break;
328 } else if (corr1 > corr0) {
52eeaef5 329 i += ARRAYLEN(Logic1) / skip;
9455b51c 330 outBuf[k] |= mask;
331 } else {
52eeaef5 332 i += ARRAYLEN(Logic0) / skip;
9455b51c 333 }
334 mask <<= 1;
335 if (mask == 0) {
336 k++;
337 mask = 0x01;
338 }
52eeaef5 339 if ((i + (int)ARRAYLEN(FrameEOF)) >= GraphTraceLen) {
9455b51c 340 PrintAndLog("ran off end!");
341 break;
342 }
343 }
344 if (mask != 0x01) {
345 PrintAndLog("error, uneven octet! (discard extra bits!)");
346 PrintAndLog(" mask=%02x", mask);
347 }
348 PrintAndLog("%d octets", k);
349
350 for (i = 0; i < k; i++) {
351 PrintAndLog("# %2d: %02x ", i, outBuf[i]);
352 }
353 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
354 return 0;
7fe9b0b7 355}
356
9455b51c 357
358
359// * Acquire Samples as Reader (enables carrier, sends inquiry)
7fe9b0b7 360int CmdHF15Read(const char *Cmd)
361{
9455b51c 362 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
363 SendCommand(&c);
364 return 0;
365}
366
367// Record Activity without enabeling carrier
368int CmdHF15Record(const char *Cmd)
369{
370 UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693};
371 SendCommand(&c);
372 return 0;
7fe9b0b7 373}
374
8949bb5d 375int HF15Reader(const char *Cmd, bool verbose)
376{
377 uint8_t uid[8];
378
379 if (!getUID(uid)) {
380 if (verbose) PrintAndLog("No Tag found.");
381 return 0;
382 }
383
384 PrintAndLog("Tag UID : %s",sprintUID(NULL,uid));
385 PrintAndLog("Tag Info: %s",getTagInfo(uid));
386 return 1;
387}
388
7fe9b0b7 389int CmdHF15Reader(const char *Cmd)
390{
9455b51c 391 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
392 SendCommand(&c);
393 return 0;
7fe9b0b7 394}
395
9455b51c 396// Simulation is still not working very good
7fe9b0b7 397int CmdHF15Sim(const char *Cmd)
398{
3fe4ff4f 399 char cmdp = param_getchar(Cmd, 0);
400 uint8_t uid[8] = {0x00};
401
402 //E0 16 24 00 00 00 00 00
403 if (cmdp == 'h' || cmdp == 'H') {
404 PrintAndLog("Usage: hf 15 sim <UID>");
405 PrintAndLog("");
406 PrintAndLog(" sample: hf 15 sim E016240000000000");
407 return 0;
408 }
409
410 if (param_gethex(Cmd, 0, uid, 16)) {
411 PrintAndLog("UID must include 16 HEX symbols");
412 return 0;
413 }
414
415 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
416 uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]);
417
418 UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
419 memcpy(c.d.asBytes,uid,8);
420
9455b51c 421 SendCommand(&c);
422 return 0;
7fe9b0b7 423}
424
9455b51c 425// finds the AFI (Application Family Idendifier) of a card, by trying all values
426// (There is no standard way of reading the AFI, allthough some tags support this)
427int CmdHF15Afi(const char *Cmd)
7fe9b0b7 428{
9455b51c 429 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
430 SendCommand(&c);
431 return 0;
432}
433
434// Reads all memory pages
435int CmdHF15DumpMem(const char*Cmd) {
52eeaef5 436
437 uint8_t uid[8] = {0,0,0,0,0,0,0,0};
438 uint8_t *recv = NULL;
902cb3c0 439 UsbCommand resp;
9455b51c 440 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
52eeaef5 441 uint8_t *req = c.d.asBytes;
442 int reqlen = 0, blocknum = 0;
443 char output[80] = {0};
9455b51c 444
445 if (!getUID(uid)) {
446 PrintAndLog("No Tag found.");
447 return 0;
448 }
449
450 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid));
451 PrintAndLog("Tag Info: %s",getTagInfo(uid));
452
453 for (int retry=0; retry<5; retry++) {
454
455 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
456 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
457 req[1]=ISO15_CMD_READ;
458 memcpy(&req[2],uid,8);
459 req[10]=blocknum;
460 reqlen=AddCrc(req,11);
461 c.arg[0]=reqlen;
462
463 SendCommand(&c);
464
902cb3c0 465 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
466 recv = resp.d.asBytes;
467 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
9455b51c 468 if (!(recv[0] & ISO15_RES_ERROR)) {
52eeaef5 469 retry = 0;
470 *output = 0; // reset outputstring
3fe4ff4f 471 sprintf(output, "Block %02x ",blocknum);
902cb3c0 472 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
7bb9d33e 473 sprintf(output+strlen(output),"%02X ",recv[i]);
9455b51c 474 }
475 strcat(output," ");
52eeaef5 476 for ( int i = 1; i < resp.arg[0]-2; i++) { // data in cleaned ascii
477 sprintf(output+strlen(output),"%c",(recv[i] > 31 && recv[i] < 127) ? recv[i] : '.');
9455b51c 478 }
479 PrintAndLog("%s",output);
480 blocknum++;
481 // PrintAndLog("bn=%i",blocknum);
482 } else {
b4a9d841 483 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
8949bb5d 484 return 1;
9455b51c 485 }
486 } // else PrintAndLog("crc");
487 } // else PrintAndLog("r null");
9455b51c 488 } // retry
902cb3c0 489 // TODO: need fix
490// if (resp.arg[0]<3)
491// PrintAndLog("Lost Connection");
492// else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
493// PrintAndLog("CRC Failed");
494// else
495// PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
8949bb5d 496 return 1;
9455b51c 497}
498
499
500// "HF 15" interface
501
502static command_t CommandTable15[] =
503{
504 {"help", CmdHF15Help, 1, "This help"},
505 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
506 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
507 {"record", CmdHF15Record, 0, "Record Samples (ISO 15693)"}, // atrox
508 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
509 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
510 {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"},
511 {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"},
512 {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"},
52eeaef5 513 {NULL, NULL, 0, NULL}
7fe9b0b7 514};
515
516int CmdHF15(const char *Cmd)
517{
9455b51c 518 CmdsParse(CommandTable15, Cmd);
519 return 0;
7fe9b0b7 520}
521
9455b51c 522int CmdHF15Help(const char *Cmd)
7fe9b0b7 523{
9455b51c 524 CmdsHelp(CommandTable15);
525 return 0;
7fe9b0b7 526}
9455b51c 527
528
529// "HF 15 Cmd" Interface
530// Allows direct communication with the tag on command level
531
532int CmdHF15CmdInquiry(const char *Cmd)
533{
902cb3c0 534 UsbCommand resp;
9455b51c 535 uint8_t *recv;
536 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
537 uint8_t *req=c.d.asBytes;
538 int reqlen=0;
539
540 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
541 ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
542 req[1]=ISO15_CMD_INVENTORY;
543 req[2]=0; // mask length
544 reqlen=AddCrc(req,3);
545 c.arg[0]=reqlen;
546
547 SendCommand(&c);
548
902cb3c0 549 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
550 if (resp.arg[0]>=12) {
551 recv = resp.d.asBytes;
9455b51c 552 PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
553 PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));
554 } else {
902cb3c0 555 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
9455b51c 556 }
557 } else {
558 PrintAndLog("timeout.");
559 }
560 return 0;
561}
562
563
564// Turns debugging on(1)/off(0)
565int CmdHF15CmdDebug( const char *cmd) {
566 int debug=atoi(cmd);
567 if (strlen(cmd)<1) {
3fe4ff4f 568 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
569 PrintAndLog(" 0 no debugging");
570 PrintAndLog(" 1 turn debugging on");
9455b51c 571 return 0;
572 }
573
574 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
575 SendCommand(&c);
576 return 0;
577}
578
579
580int CmdHF15CmdRaw (const char *cmd) {
902cb3c0 581 UsbCommand resp;
9455b51c 582 uint8_t *recv;
583 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
584 int reply=1;
585 int fast=1;
586 int crc=0;
587 char buf[5]="";
588 int i=0;
589 uint8_t data[100];
590 unsigned int datalen=0, temp;
fdb67f1c 591 char *hexout;
9455b51c 592
593
594 if (strlen(cmd)<3) {
595 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
596 PrintAndLog(" -r do not read response");
597 PrintAndLog(" -2 use slower '1 out of 256' mode");
598 PrintAndLog(" -c calculate and append CRC");
599 PrintAndLog(" Tip: turn on debugging for verbose output");
600 return 0;
601 }
602
603 // strip
604 while (*cmd==' ' || *cmd=='\t') cmd++;
605
606 while (cmd[i]!='\0') {
607 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
608 if (cmd[i]=='-') {
609 switch (cmd[i+1]) {
610 case 'r':
611 case 'R':
612 reply=0;
613 break;
614 case '2':
615 fast=0;
616 break;
617 case 'c':
618 case 'C':
619 crc=1;
620 break;
621 default:
622 PrintAndLog("Invalid option");
623 return 0;
624 }
625 i+=2;
626 continue;
627 }
628 if ((cmd[i]>='0' && cmd[i]<='9') ||
629 (cmd[i]>='a' && cmd[i]<='f') ||
630 (cmd[i]>='A' && cmd[i]<='F') ) {
631 buf[strlen(buf)+1]=0;
632 buf[strlen(buf)]=cmd[i];
633 i++;
634
635 if (strlen(buf)>=2) {
636 sscanf(buf,"%x",&temp);
637 data[datalen]=(uint8_t)(temp & 0xff);
638 datalen++;
639 *buf=0;
640 }
641 continue;
642 }
643 PrintAndLog("Invalid char on input");
644 return 0;
645 }
646 if (crc) datalen=AddCrc(data,datalen);
647
648 c.arg[0]=datalen;
649 c.arg[1]=fast;
650 c.arg[2]=reply;
651 memcpy(c.d.asBytes,data,datalen);
652
653 SendCommand(&c);
654
655 if (reply) {
902cb3c0 656 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
657 recv = resp.d.asBytes;
658 PrintAndLog("received %i octets",resp.arg[0]);
659 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
fdb67f1c 660 if (hexout != NULL) {
902cb3c0 661 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
7bb9d33e 662 sprintf(&hexout[i * 3], "%02X ", recv[i]);
fdb67f1c 663 }
664 PrintAndLog("%s", hexout);
665 free(hexout);
666 }
9455b51c 667 } else {
668 PrintAndLog("timeout while waiting for reply.");
669 }
670
671 } // if reply
672 return 0;
673}
674
675
0546b4aa 676/**
677 * parses common HF 15 CMD parameters and prepares some data structures
678 * Parameters:
679 * **cmd command line
680 */
9455b51c 681int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
682 int temp;
3ac59c7f 683 uint8_t *req = c->d.asBytes;
3fe4ff4f 684 uint8_t uid[8] = {0x00};
3ac59c7f 685 uint32_t reqlen = 0;
9455b51c 686
687 // strip
688 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
689
690 if (strstr(*cmd,"-2")==*cmd) {
c43897de 691 c->arg[1]=0; // use 1of256
9455b51c 692 (*cmd)+=2;
693 }
694
695 // strip
696 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
697
698 if (strstr(*cmd,"-o")==*cmd) {
699 req[reqlen]=ISO15_REQ_OPTION;
700 (*cmd)+=2;
701 }
702
703 // strip
704 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
705
706 switch (**cmd) {
707 case 0:
708 PrintAndLog("missing addr");
709 return 0;
710 break;
711 case 's':
712 case 'S':
713 // you must have selected the tag earlier
714 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
715 ISO15_REQ_NONINVENTORY | ISO15_REQ_SELECT;
716 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
717 reqlen+=iso15cmdlen;
718 break;
719 case 'u':
720 case 'U':
721 // unaddressed mode may not be supported by all vendors
722 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
723 ISO15_REQ_NONINVENTORY;
724 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
725 reqlen+=iso15cmdlen;
726 break;
727 case '*':
65a23af2 728 // we scan for the UID ourself
9455b51c 729 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
730 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
731 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
732 reqlen+=iso15cmdlen;
733 if (!getUID(uid)) {
734 PrintAndLog("No Tag found");
735 return 0;
736 }
737 memcpy(req+reqlen,uid,8);
738 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
739 reqlen+=8;
740 break;
741 default:
742 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
743 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
744 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
745 reqlen+=iso15cmdlen;
746
747/* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
748 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
749 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
750 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
751 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
752 for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID
753 sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
754 uid[7-i]=temp&0xff;
755 }
756
757 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
758 memcpy(&req[reqlen],&uid[0],8);
759 reqlen+=8;
760 }
761 // skip to next space
762 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
763 // skip over the space
764 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
765
766 c->arg[0]=reqlen;
767 return 1;
768}
769
6d7234cd 770/**
771 * Commandline handling: HF15 CMD SYSINFO
772 * get system information from tag/VICC
773 */
774int CmdHF15CmdSysinfo(const char *Cmd) {
902cb3c0 775 UsbCommand resp;
6d7234cd 776 uint8_t *recv;
777 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
3ac59c7f 778 uint8_t *req = c.d.asBytes;
779 int reqlen = 0;
6d7234cd 780 char cmdbuf[100];
3ac59c7f 781 char *cmd = cmdbuf;
6d7234cd 782 char output[2048]="";
783 int i;
784
785 strncpy(cmd,Cmd,99);
786
787 // usage:
788 if (strlen(cmd)<1) {
0546b4aa 789 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
6d7234cd 790 PrintAndLog(" options:");
791 PrintAndLog(" -2 use slower '1 out of 256' mode");
792 PrintAndLog(" uid (either): ");
793 PrintAndLog(" <8B hex> full UID eg E011223344556677");
794 PrintAndLog(" s selected tag");
795 PrintAndLog(" u unaddressed mode");
796 PrintAndLog(" * scan for tag");
8949bb5d 797 PrintAndLog(" start#: page number to start 0-255");
798 PrintAndLog(" count#: number of pages");
6d7234cd 799 return 0;
800 }
801
802 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_SYSINFO},1);
3ac59c7f 803 reqlen = c.arg[0];
6d7234cd 804
805 reqlen=AddCrc(req,reqlen);
806 c.arg[0]=reqlen;
807
808 SendCommand(&c);
809
902cb3c0 810 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
811 recv = resp.d.asBytes;
812 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
6d7234cd 813 if (!(recv[0] & ISO15_RES_ERROR)) {
814 *output=0; // reset outputstring
902cb3c0 815 for ( i=1; i<resp.arg[0]-2; i++) {
7bb9d33e 816 sprintf(output+strlen(output),"%02X ",recv[i]);
6d7234cd 817 }
818 strcat(output,"\n\r");
819 strcat(output,"UID = ");
820 strcat(output,sprintUID(NULL,recv+2));
821 strcat(output,"\n\r");
822 strcat(output,getTagInfo(recv+2)); //ABC
823 strcat(output,"\n\r");
824 i=10;
825 if (recv[1] & 0x01)
7bb9d33e 826 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
6d7234cd 827 else
828 strcat(output,"DSFID not supported\n\r");
829 if (recv[1] & 0x02)
7bb9d33e 830 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
6d7234cd 831 else
832 strcat(output,"AFI not supported\n\r");
833 if (recv[1] & 0x04) {
834 strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r");
835 sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r",
836 (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1);
837 i+=2;
838 } else
839 strcat(output,"Tag does not provide information on memory layout\n\r");
7bb9d33e 840 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
6d7234cd 841 else strcat(output,"IC reference not given\n\r");
842
843
844 PrintAndLog("%s",output);
845 } else {
846 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
847 }
848 } else {
849 PrintAndLog("CRC failed");
850 }
851 } else {
eba61a56 852 PrintAndLog("timeout: no answer");
6d7234cd 853 }
854
855 return 0;
856}
857
7853775e 858/**
859 * Commandline handling: HF15 CMD READMULTI
860 * Read multiple blocks at once (not all tags support this)
861 */
862int CmdHF15CmdReadmulti(const char *Cmd) {
902cb3c0 863 UsbCommand resp;
7853775e 864 uint8_t *recv;
865 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
866 uint8_t *req=c.d.asBytes;
867 int reqlen=0, pagenum,pagecount;
868 char cmdbuf[100];
869 char *cmd=cmdbuf;
870 char output[2048]="";
871
872 strncpy(cmd,Cmd,99);
873
874 // usage:
875 if (strlen(cmd)<3) {
0546b4aa 876 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
7853775e 877 PrintAndLog(" options:");
878 PrintAndLog(" -2 use slower '1 out of 256' mode");
879 PrintAndLog(" uid (either): ");
880 PrintAndLog(" <8B hex> full UID eg E011223344556677");
881 PrintAndLog(" s selected tag");
882 PrintAndLog(" u unaddressed mode");
883 PrintAndLog(" * scan for tag");
884 PrintAndLog(" start#: page number to start 0-255");
885 PrintAndLog(" count#: number of pages");
886 return 0;
887 }
888
889 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READMULTI},1);
890 reqlen=c.arg[0];
891
892 pagenum=strtol(cmd,NULL,0);
893
894 // skip to next space
895 while (*cmd!=' ' && *cmd!='\t') cmd++;
896 // skip over the space
897 while (*cmd==' ' || *cmd=='\t') cmd++;
898
899 pagecount=strtol(cmd,NULL,0);
900 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
901
902 req[reqlen++]=(uint8_t)pagenum;
903 req[reqlen++]=(uint8_t)pagecount;
904
905 reqlen=AddCrc(req,reqlen);
906
907 c.arg[0]=reqlen;
908
909 SendCommand(&c);
0546b4aa 910
902cb3c0 911 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
912 recv = resp.d.asBytes;
913 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
7853775e 914 if (!(recv[0] & ISO15_RES_ERROR)) {
915 *output=0; // reset outputstring
902cb3c0 916 for ( int i=1; i<resp.arg[0]-2; i++) {
7bb9d33e 917 sprintf(output+strlen(output),"%02X ",recv[i]);
7853775e 918 }
919 strcat(output," ");
902cb3c0 920 for ( int i=1; i<resp.arg[0]-2; i++) {
7853775e 921 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
922 }
923 PrintAndLog("%s",output);
924 } else {
925 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
926 }
927 } else {
928 PrintAndLog("CRC failed");
929 }
930 } else {
931 PrintAndLog("no answer");
932 }
933
934 return 0;
935}
936
05151b6f 937/**
938 * Commandline handling: HF15 CMD READ
939 * Reads a single Block
940 */
9455b51c 941int CmdHF15CmdRead(const char *Cmd) {
902cb3c0 942 UsbCommand resp;
9455b51c 943 uint8_t *recv;
944 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
945 uint8_t *req=c.d.asBytes;
946 int reqlen=0, pagenum;
947 char cmdbuf[100];
948 char *cmd=cmdbuf;
949 char output[100]="";
950
951 strncpy(cmd,Cmd,99);
952
953 // usage:
954 if (strlen(cmd)<3) {
0546b4aa 955 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
9455b51c 956 PrintAndLog(" options:");
957 PrintAndLog(" -2 use slower '1 out of 256' mode");
958 PrintAndLog(" uid (either): ");
959 PrintAndLog(" <8B hex> full UID eg E011223344556677");
960 PrintAndLog(" s selected tag");
961 PrintAndLog(" u unaddressed mode");
962 PrintAndLog(" * scan for tag");
963 PrintAndLog(" page#: page number 0-255");
964 return 0;
965 }
966
967 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READ},1);
968 reqlen=c.arg[0];
969
970 pagenum=strtol(cmd,NULL,0);
971 /*if (pagenum<0) {
972 PrintAndLog("invalid pagenum");
973 return 0;
974 } */
975
976 req[reqlen++]=(uint8_t)pagenum;
977
978 reqlen=AddCrc(req,reqlen);
979
980 c.arg[0]=reqlen;
981
982 SendCommand(&c);
983
902cb3c0 984 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
985 recv = resp.d.asBytes;
986 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
9455b51c 987 if (!(recv[0] & ISO15_RES_ERROR)) {
988 *output=0; // reset outputstring
989 //sprintf(output, "Block %2i ",blocknum);
902cb3c0 990 for ( int i=1; i<resp.arg[0]-2; i++) {
7bb9d33e 991 sprintf(output+strlen(output),"%02X ",recv[i]);
9455b51c 992 }
993 strcat(output," ");
902cb3c0 994 for ( int i=1; i<resp.arg[0]-2; i++) {
e8da7740 995 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
9455b51c 996 }
997 PrintAndLog("%s",output);
998 } else {
b4a9d841 999 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 1000 }
1001 } else {
1002 PrintAndLog("CRC failed");
1003 }
1004 } else {
1005 PrintAndLog("no answer");
1006 }
1007
1008 return 0;
1009}
1010
1011
05151b6f 1012/**
1013 * Commandline handling: HF15 CMD WRITE
1014 * Writes a single Block - might run into timeout, even when successful
1015 */
9455b51c 1016int CmdHF15CmdWrite(const char *Cmd) {
902cb3c0 1017 UsbCommand resp;
9455b51c 1018 uint8_t *recv;
1019 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
1020 uint8_t *req=c.d.asBytes;
1021 int reqlen=0, pagenum, temp;
1022 char cmdbuf[100];
1023 char *cmd=cmdbuf;
1024 char *cmd2;
1025
1026 strncpy(cmd,Cmd,99);
1027
1028 // usage:
1029 if (strlen(cmd)<3) {
0546b4aa 1030 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
9455b51c 1031 PrintAndLog(" options:");
1032 PrintAndLog(" -2 use slower '1 out of 256' mode");
1033 PrintAndLog(" -o set OPTION Flag (needed for TI)");
1034 PrintAndLog(" uid (either): ");
1035 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1036 PrintAndLog(" s selected tag");
1037 PrintAndLog(" u unaddressed mode");
1038 PrintAndLog(" * scan for tag");
1039 PrintAndLog(" page#: page number 0-255");
1040 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
1041 return 0;
1042 }
1043
1044 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_WRITE},1);
1045 reqlen=c.arg[0];
1046
1047 // *cmd -> page num ; *cmd2 -> data
1048 cmd2=cmd;
1049 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
1050 *cmd2=0;
1051 cmd2++;
1052
1053 pagenum=strtol(cmd,NULL,0);
1054 /*if (pagenum<0) {
1055 PrintAndLog("invalid pagenum");
1056 return 0;
1057 } */
1058 req[reqlen++]=(uint8_t)pagenum;
1059
1060
1061 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
1062 if (*cmd2==' ') {
1063 cmd2++;
1064 continue;
1065 }
1066 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
1067 req[reqlen++]=temp & 0xff;
1068 cmd2+=2;
1069 }
1070
1071 reqlen=AddCrc(req,reqlen);
1072
1073 c.arg[0]=reqlen;
1074
1075 SendCommand(&c);
1076
902cb3c0 1077 if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
1078 recv = resp.d.asBytes;
1079 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
9455b51c 1080 if (!(recv[0] & ISO15_RES_ERROR)) {
1081 PrintAndLog("OK");
1082 } else {
b4a9d841 1083 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 1084 }
1085 } else {
1086 PrintAndLog("CRC failed");
1087 }
1088 } else {
65a23af2 1089 PrintAndLog("timeout: no answer - data may be written anyway");
9455b51c 1090 }
1091
1092 return 0;
1093}
1094
1095
1096
1097static command_t CommandTable15Cmd[] =
1098{
1099 {"help", CmdHF15CmdHelp, 1, "This Help"},
1100 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
1101 /*
1102 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1103 */
1104 {"read", CmdHF15CmdRead, 0, "Read a block"},
1105 {"write", CmdHF15CmdWrite, 0, "Write a block"},
9455b51c 1106 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
0546b4aa 1107 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
9455b51c 1108 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
1109 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
1110 {NULL, NULL, 0, NULL}
1111};
1112
1113int CmdHF15Cmd(const char *Cmd)
1114{
1115 CmdsParse(CommandTable15Cmd, Cmd);
1116 return 0;
1117}
1118
1119int CmdHF15CmdHelp(const char *Cmd)
1120{
1121 CmdsHelp(CommandTable15Cmd);
1122 return 0;
1123}
1124
Impressum, Datenschutz