fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / cmdnfc.c
blob27c662e45c02594d49f8fae061d52a8a06a4cc69
1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
4 // the license.
5 //-----------------------------------------------------------------------------
6 // NFC commands
7 //-----------------------------------------------------------------------------
8 #include "cmdnfc.h"
9 #include "nfc/ndef.h"
10 #include "cliparser.h"
11 #include "ui.h"
12 #include "cmdparser.h"
13 #include "cmdhf14a.h"
14 #include "cmdhf14b.h"
15 #include "cmdhfmf.h"
16 #include "cmdhfmfp.h"
17 #include "cmdhfmfu.h"
18 #include "cmdhfst25ta.h"
19 #include "cmdhfthinfilm.h"
20 #include "cmdhftopaz.h"
21 #include "cmdnfc.h"
23 void print_type4_cc_info(uint8_t *d, uint8_t n) {
24 if (n < 0x0F) {
25 PrintAndLogEx(WARNING, "Not enough bytes read from CC file");
26 return;
29 PrintAndLogEx(NORMAL, "");
30 PrintAndLogEx(SUCCESS, "------------ " _CYAN_("Capability Container file") " ------------");
31 PrintAndLogEx(SUCCESS, " len %u bytes (" _GREEN_("0x%02X") ")", d[1], d[1]);
32 PrintAndLogEx(SUCCESS, " version %s (" _GREEN_("0x%02X") ")", (d[2] == 0x20) ? "v2.0" : "v1.0", d[2]);
34 uint16_t maxr = (d[3] << 8 | d[4]);
35 PrintAndLogEx(SUCCESS, " max bytes read %u bytes ( 0x%04X )", maxr, maxr);
36 uint16_t maxw = (d[5] << 8 | d[6]);
37 PrintAndLogEx(SUCCESS, " max bytes write %u bytes ( 0x%04X )", maxw, maxw);
38 PrintAndLogEx(NORMAL, "");
39 PrintAndLogEx(SUCCESS, " NDEF file control TLV {");
40 PrintAndLogEx(SUCCESS, " (t) type of file ( %02X )", d[7]);
41 PrintAndLogEx(SUCCESS, " (v) ( %02X )", d[8]);
42 PrintAndLogEx(SUCCESS, " file id ( %02X%02X )", d[9], d[10]);
44 uint16_t maxndef = (d[11] << 8 | d[12]);
45 PrintAndLogEx(SUCCESS, " max NDEF filesize %u bytes ( 0x%04X )", maxndef, maxndef);
46 PrintAndLogEx(SUCCESS, " ----- " _CYAN_("access rights") " -------");
47 PrintAndLogEx(SUCCESS, " read ( %02X ) protection: %s", d[13], ((d[13] & 0x80) == 0x80) ? _RED_("enabled") : _GREEN_("disabled"));
48 PrintAndLogEx(SUCCESS, " write ( %02X ) protection: %s", d[14], ((d[14] & 0x80) == 0x80) ? _RED_("enabled") : _GREEN_("disabled"));
49 PrintAndLogEx(SUCCESS, " }");
50 PrintAndLogEx(SUCCESS, "----------------- " _CYAN_("raw") " -----------------");
51 PrintAndLogEx(SUCCESS, "%s", sprint_hex_inrow(d, n));
52 PrintAndLogEx(NORMAL, "");
55 static int CmdNfcDecode(const char *Cmd) {
57 #ifndef MAX_NDEF_LEN
58 #define MAX_NDEF_LEN 2048
59 #endif
61 CLIParserContext *ctx;
62 CLIParserInit(&ctx, "nfc decode",
63 "Decode and print NFC Data Exchange Format (NDEF)",
64 "nfc decode -d 9101085402656e48656c6c6f5101085402656e576f726c64\n"
65 "nfc decode -d 0103d020240203e02c040300fe\n"
68 void *argtable[] = {
69 arg_param_begin,
70 arg_strx0("d", "data", "<hex>", "NDEF data to decode"),
71 arg_lit0("v", "verbose", "verbose mode"),
72 arg_param_end
74 CLIExecWithReturn(ctx, Cmd, argtable, false);
76 int datalen = 0;
77 uint8_t data[MAX_NDEF_LEN] = {0};
78 CLIGetHexWithReturn(ctx, 1, data, &datalen);
79 bool verbose = arg_get_lit(ctx, 2);
81 CLIParserFree(ctx);
82 if (datalen == 0)
83 return PM3_EINVARG;
85 int res = NDEFDecodeAndPrint(data, datalen, verbose);
86 if (res != PM3_SUCCESS) {
87 PrintAndLogEx(INFO, "Trying to parse NDEF records w/o NDEF header");
88 res = NDEFRecordsDecodeAndPrint(data, datalen);
90 return res;
93 static int CmdNFCType1Read(const char *Cmd) {
94 return CmdHFTopazInfo(Cmd);
97 static int CmdNFCType1Help(const char *Cmd);
99 static command_t CommandNFCType1Table[] = {
101 {"--------", CmdNFCType1Help, AlwaysAvailable, "-------------- " _CYAN_("NFC Forum Tag Type 1") " ---------------"},
102 {"read", CmdNFCType1Read, IfPm3Iso14443a, "read NFC Forum Tag Type 1"},
103 // {"write", CmdNFCType1Write, IfPm3Iso14443a, "write NFC Forum Tag Type 1"},
104 {"--------", CmdNFCType1Help, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
105 {"help", CmdNFCType1Help, AlwaysAvailable, "This help"},
106 {NULL, NULL, NULL, NULL}
109 static int CmdNFCType1(const char *Cmd) {
110 clearCommandBuffer();
111 return CmdsParse(CommandNFCType1Table, Cmd);
114 static int CmdNFCType1Help(const char *Cmd) {
115 (void)Cmd; // Cmd is not used so far
116 CmdsHelp(CommandNFCType1Table);
117 return PM3_SUCCESS;
120 static int CmdNFCType2Read(const char *Cmd) {
121 return CmdHF14MfuNDEFRead(Cmd);
124 static int CmdNFCType2Help(const char *Cmd);
126 static command_t CommandNFCType2Table[] = {
128 {"--------", CmdNFCType2Help, AlwaysAvailable, "-------------- " _CYAN_("NFC Forum Tag Type 2") " ---------------"},
129 {"read", CmdNFCType2Read, IfPm3Iso14443a, "read NFC Forum Tag Type 2"},
130 // {"write", CmdNFCType2Write, IfPm3Iso14443a, "write NFC Forum Tag Type 2"},
131 {"--------", CmdNFCType2Help, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
132 {"help", CmdNFCType2Help, AlwaysAvailable, "This help"},
133 {NULL, NULL, NULL, NULL}
136 static int CmdNFCType2(const char *Cmd) {
137 clearCommandBuffer();
138 return CmdsParse(CommandNFCType2Table, Cmd);
141 static int CmdNFCType2Help(const char *Cmd) {
142 (void)Cmd; // Cmd is not used so far
143 CmdsHelp(CommandNFCType2Table);
144 return PM3_SUCCESS;
148 static int CmdNFCType3Read(const char *Cmd) {
149 return CmdHFFelicaXXX(Cmd);
152 static int CmdNFCType3Help(const char *Cmd);
154 static command_t CommandNFCType3Table[] = {
156 {"--------", CmdNFCType3Help, AlwaysAvailable, "-------------- " _CYAN_("NFC Forum Tag Type 3") " ---------------"},
157 {"read", CmdNFCType3Read, IfPm3Felica, "read NFC Forum Tag Type 3"},
158 {"write", CmdNFCType3Write, IfPm3Felica, "write NFC Forum Tag Type 3"},
159 {"--------", CmdNFCType3Help, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
160 {"help", CmdNFCType3Help, AlwaysAvailable, "This help"},
161 {NULL, NULL, NULL, NULL}
164 static int CmdNFCType3(const char *Cmd) {
165 clearCommandBuffer();
166 return CmdsParse(CommandNFCType3Table, Cmd);
169 static int CmdNFCType3Help(const char *Cmd) {
170 (void)Cmd; // Cmd is not used so far
171 CmdsHelp(CommandNFCType3Table);
172 return PM3_SUCCESS;
176 static int CmdNFCType4ARead(const char *Cmd) {
177 return CmdHF14ANdefRead(Cmd);
180 static int CmdNFCST25TARead(const char *Cmd) {
181 return CmdHFST25TANdefRead(Cmd);
184 static int CmdNFCType4AHelp(const char *Cmd);
186 static command_t CommandNFCType4ATable[] = {
188 {"--------", CmdNFCType4AHelp, AlwaysAvailable, "--------- " _CYAN_("NFC Forum Tag Type 4 ISO14443A") " ----------"},
189 {"read", CmdNFCType4ARead, IfPm3Iso14443a, "read NFC Forum Tag Type 4 A"},
190 // {"write", CmdNFCType4AWrite, IfPm3Iso14443a, "write NFC Forum Tag Type 4 A"},
191 // {"mfdesread", CmdNFCMFDESRead, IfPm3Iso14443a, "read NDEF from MIFARE DESfire"}, // hf mfdes ndefread
192 // {"mfdesformat", CmdNFCMFDESFormat, IfPm3Iso14443a, "format MIFARE DESfire as NFC Forum Tag Type 4"},
193 {"st25taread", CmdNFCST25TARead, IfPm3Iso14443a, "read ST25TA as NFC Forum Tag Type 4"},
195 {"--------", CmdNFCType4AHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
196 {"help", CmdNFCType4AHelp, AlwaysAvailable, "This help"},
197 {NULL, NULL, NULL, NULL}
200 static int CmdNFCType4A(const char *Cmd) {
201 clearCommandBuffer();
202 return CmdsParse(CommandNFCType4ATable, Cmd);
205 static int CmdNFCType4AHelp(const char *Cmd) {
206 (void)Cmd; // Cmd is not used so far
207 CmdsHelp(CommandNFCType4ATable);
208 return PM3_SUCCESS;
211 static int CmdNFCType4BRead(const char *Cmd) {
212 return CmdHF14BNdefRead(Cmd);
215 static int CmdNFCType4BHelp(const char *Cmd);
217 static command_t CommandNFCType4BTable[] = {
219 {"--------", CmdNFCType4BHelp, AlwaysAvailable, "--------- " _CYAN_("NFC Forum Tag Type 4 ISO14443B") " -------------"},
220 {"read", CmdNFCType4BRead, IfPm3Iso14443b, "read NFC Forum Tag Type 4 B"},
221 // {"write", CmdNFCType4BWrite, IfPm3Iso14443b, "write NFC Forum Tag Type 4 B"},
222 {"--------", CmdNFCType4BHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
223 {"help", CmdNFCType4BHelp, AlwaysAvailable, "This help"},
224 {NULL, NULL, NULL, NULL}
227 static int CmdNFCType4B(const char *Cmd) {
228 clearCommandBuffer();
229 return CmdsParse(CommandNFCType4BTable, Cmd);
232 static int CmdNFCType4BHelp(const char *Cmd) {
233 (void)Cmd; // Cmd is not used so far
234 CmdsHelp(CommandNFCType4BTable);
235 return PM3_SUCCESS;
239 static int CmdNFCType5Read(const char *Cmd) {
240 return CmdHF15XXX(Cmd);
243 static int CmdNFCType5Help(const char *Cmd);
245 static command_t CommandNFCType5Table[] = {
247 {"--------", CmdNFCType5Help, AlwaysAvailable, "-------------- " _CYAN_("NFC Forum Tag Type 5") " ---------------"},
248 {"read", CmdNFCType5Read, IfPm3Iso15693, "read NFC Forum Tag Type 5"},
249 {"write", CmdNFCType5Write, IfPm3Iso15693, "write NFC Forum Tag Type 5"},
250 {"--------", CmdNFCType5Help, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
251 {"help", CmdNFCType5Help, AlwaysAvailable, "This help"},
252 {NULL, NULL, NULL, NULL}
255 static int CmdNFCType5(const char *Cmd) {
256 clearCommandBuffer();
257 return CmdsParse(CommandNFCType5Table, Cmd);
260 static int CmdNFCType5Help(const char *Cmd) {
261 (void)Cmd; // Cmd is not used so far
262 CmdsHelp(CommandNFCType5Table);
263 return PM3_SUCCESS;
267 static int CmdNFCMFCRead(const char *Cmd) {
268 return CmdHFMFNDEFRead(Cmd);
271 static int CmdNFCMFPRead(const char *Cmd) {
272 return CmdHFMFPNDEFRead(Cmd);
275 static int CmdNFCMFHelp(const char *Cmd);
277 static command_t CommandMFTable[] = {
279 {"--------", CmdNFCMFHelp, AlwaysAvailable, "--------- " _CYAN_("NFC Type MIFARE Classic/Plus Tag") " --------"},
280 {"cread", CmdNFCMFCRead, IfPm3Iso14443a, "read NFC Type MIFARE Classic Tag"},
281 // {"cwrite", CmdNFCMFCWrite, IfPm3Iso14443a, "write NFC Type MIFARE Classic Tag"},
282 // {"cformat", CmdNFCMFCFormat, IfPm3Iso14443a, "format MIFARE Classic Tag as NFC Tag"},
283 {"pread", CmdNFCMFPRead, IfPm3Iso14443a, "read NFC Type MIFARE Plus Tag"},
284 {"--------", CmdNFCMFHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
285 {"help", CmdNFCMFHelp, AlwaysAvailable, "This help"},
286 {NULL, NULL, NULL, NULL}
289 static int CmdNFCMF(const char *Cmd) {
290 clearCommandBuffer();
291 return CmdsParse(CommandMFTable, Cmd);
294 static int CmdNFCMFHelp(const char *Cmd) {
295 (void)Cmd; // Cmd is not used so far
296 CmdsHelp(CommandMFTable);
297 return PM3_SUCCESS;
300 static int CmdNFCBarcodeRead(const char *Cmd) {
301 return CmdHfThinFilmInfo(Cmd);
304 static int CmdNFCBarcodeSim(const char *Cmd) {
305 return CmdHfThinFilmSim(Cmd);
308 static int CmdNFCBarcodeHelp(const char *Cmd);
310 static command_t CommandBarcodeTable[] = {
312 {"--------", CmdNFCBarcodeHelp, AlwaysAvailable, "------------------ " _CYAN_("NFC Barcode") " --------------------"},
313 {"read", CmdNFCBarcodeRead, IfPm3Iso14443a, "read NFC Barcode"},
314 {"sim", CmdNFCBarcodeSim, IfPm3Iso14443a, "simulate NFC Barcode"},
315 {"--------", CmdNFCBarcodeHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
316 {"help", CmdNFCBarcodeHelp, AlwaysAvailable, "This help"},
317 {NULL, NULL, NULL, NULL}
320 static int CmdNFCBarcode(const char *Cmd) {
321 clearCommandBuffer();
322 return CmdsParse(CommandBarcodeTable, Cmd);
325 static int CmdNFCBarcodeHelp(const char *Cmd) {
326 (void)Cmd; // Cmd is not used so far
327 CmdsHelp(CommandBarcodeTable);
328 return PM3_SUCCESS;
331 static int CmdHelp(const char *Cmd);
333 static command_t CommandTable[] = {
335 {"--------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("NFC Tags") " --------------------"},
336 {"type1", CmdNFCType1, AlwaysAvailable, "{ NFC Forum Tag Type 1... }"},
337 {"type2", CmdNFCType2, AlwaysAvailable, "{ NFC Forum Tag Type 2... }"},
338 // {"type3", CmdNFCType3, AlwaysAvailable, "{ NFC Forum Tag Type 3... }"},
339 {"type4a", CmdNFCType4A, AlwaysAvailable, "{ NFC Forum Tag Type 4 ISO14443A... }"},
340 {"type4b", CmdNFCType4B, AlwaysAvailable, "{ NFC Forum Tag Type 4 ISO14443B... }"},
341 // {"type5", CmdNFCType5, AlwaysAvailable, "{ NFC Forum Tag Type 5... }"},
342 {"mf", CmdNFCMF, AlwaysAvailable, "{ NFC Type MIFARE Classic/Plus Tag... }"},
343 {"barcode", CmdNFCBarcode, AlwaysAvailable, "{ NFC Barcode Tag... }"},
344 // {"--------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("NFC peer-to-peer") " ------------"},
345 // {"isodep", CmdISODEP, AlwaysAvailable, "{ ISO-DEP protocol... }"},
346 // {"llcp", CmdNFCLLCP, AlwaysAvailable, "{ Logical Link Control Protocol... }"},
347 // {"snep", CmdNFCSNEP, AlwaysAvailable, "{ Simple NDEF Exchange Protocol... }"},
348 {"--------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
349 {"help", CmdHelp, AlwaysAvailable, "This help"},
350 {"decode", CmdNfcDecode, AlwaysAvailable, "Decode NDEF records"},
351 {NULL, NULL, NULL, NULL}
354 int CmdNFC(const char *Cmd) {
355 clearCommandBuffer();
356 return CmdsParse(CommandTable, Cmd);
359 int CmdHelp(const char *Cmd) {
360 (void)Cmd; // Cmd is not used so far
361 CmdsHelp(CommandTable);
362 return PM3_SUCCESS;