3e833a054f4ed248186c2d17c8d014e43dee2e5d
[librfid] / utils / librfid-tool.c
1 /* librfid-tool - a small command-line tool for librfid testing
2  *
3  * (C) 2005-2008 by Harald Welte <laforge@gnumonks.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License version 2 
7  *  as published by the Free Software Foundation
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #ifndef __MINGW32__
26 #include <libgen.h>
27 #endif
28
29 #define _GNU_SOURCE
30 #include <getopt.h>
31
32 #include <librfid/rfid.h>
33 #include <librfid/rfid_scan.h>
34 #include <librfid/rfid_reader.h>
35 #include <librfid/rfid_layer2.h>
36 #include <librfid/rfid_protocol.h>
37
38 #include <librfid/rfid_layer2_iso14443a.h>
39 #include <librfid/rfid_layer2_iso15693.h>
40
41 #include <librfid/rfid_protocol_mifare_classic.h>
42 #include <librfid/rfid_protocol_mifare_ul.h>
43 #include <librfid/rfid_protocol_tagit.h>
44 #include <librfid/rfid_protocol_icode.h>
45
46 #include "librfid-tool.h"
47
48
49 static int select_mf(void)
50 {
51         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
52         unsigned char ret[256];
53         unsigned int rlen = sizeof(ret);
54
55         int rv;
56
57         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
58         if (rv < 0)
59                 return rv;
60
61         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
62
63         return 0;
64 }
65
66
67 static int iso7816_get_challenge(unsigned char len)
68 {
69         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
70         unsigned char ret[256];
71         unsigned int rlen = sizeof(ret);
72
73         cmd[4] = len;
74
75         int rv;
76
77         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
78         if (rv < 0)
79                 return rv;
80
81         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
82
83         return 0;
84 }
85
86 int
87 iso7816_select_application(void)
88 {
89         unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
90                        0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
91         unsigned char resp[7];
92         unsigned int rlen = sizeof(resp);
93
94         int rv;
95
96         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
97         if (rv < 0)
98                 return rv;
99
100         /* FIXME: parse response */
101         printf("%s\n", hexdump(resp, rlen));
102
103         return 0;
104 }
105
106 int
107 iso7816_select_ef(u_int16_t fid)
108 {
109         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
110         unsigned char resp[7];
111         unsigned int rlen = sizeof(resp);
112
113         int rv;
114
115         cmd[5] = (fid >> 8) & 0xff;
116         cmd[6] = fid & 0xff;
117
118         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
119         if (rv < 0)
120                 return rv;
121
122         /* FIXME: parse response */
123         printf("%s\n", hexdump(resp, rlen));
124
125         return 0;
126 }
127
128 int
129 iso7816_read_binary(unsigned char *buf, unsigned int *len)
130 {
131         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
132         unsigned char resp[256];
133         unsigned int rlen = sizeof(resp);
134         
135         int rv;
136
137         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
138         if (rv < 0)
139                 return rv;
140
141         printf("%s\n", hexdump(resp, rlen));
142
143         /* FIXME: parse response, determine whether we need additional reads */
144
145         /* FIXME: copy 'len' number of response bytes to 'buf' */
146         return 0;
147 }
148
149 /* wrapper function around SELECT EF and READ BINARY */
150 int
151 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
152 {
153         int rv;
154
155         rv = iso7816_select_ef(fid);
156         if (rv < 0)
157                 return rv;
158
159         return iso7816_read_binary(buf, len);
160 }
161
162 /* mifare ultralight helpers */
163 int
164 mifare_ulight_write(struct rfid_protocol_handle *ph)
165 {
166         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
167
168         return rfid_protocol_write(ph, 10, buf, 4);
169 }
170
171 int
172 mifare_ulight_blank(struct rfid_protocol_handle *ph)
173 {
174         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
175         int i, ret;
176
177         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
178                 ret = rfid_protocol_write(ph, i, buf, 4);
179                 if (ret < 0)
180                         return ret;
181         }
182         return 0;
183 }
184
185 static int
186 mifare_ulight_read(struct rfid_protocol_handle *ph)
187 {
188         unsigned char buf[20];
189         unsigned int len = sizeof(buf);
190         int ret;
191         int i;
192
193         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
194                 ret = rfid_protocol_read(ph, i, buf, &len);
195                 if (ret < 0)
196                         return ret;
197
198                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
199         }
200         return 0;
201 }
202
203 /* mifare classic helpers */
204 static int
205 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
206 {
207         unsigned char buf[20];
208         unsigned int len = sizeof(buf);
209         int ret;
210         int block;
211
212         /* FIXME: make this work for sectors > 31 */
213         printf("Reading sector %u\n", sector);
214
215         for (block = sector*4; block < sector*4+4; block++) {
216                 printf("Reading block %u: ", block);
217                 ret = rfid_protocol_read(ph, block, buf, &len);
218                 if(ret == -ETIMEDOUT)
219                         fprintf(stderr, "TIMEOUT\n");
220                 if (ret < 0) {
221                         printf("Error %d reading\n", ret);
222                         return ret;
223                 }
224
225                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
226         }
227         return 0;
228 }
229
230 static char *proto_names[] = {
231         [RFID_PROTOCOL_TCL] = "tcl",
232         [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
233         [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
234         [RFID_PROTOCOL_ICODE_SLI] = "icode",
235         [RFID_PROTOCOL_TAGIT] = "tagit",
236 };
237
238 static int proto_by_name(const char *name)
239 {
240         int i;
241
242         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
243                 if (proto_names[i] == NULL)
244                         continue;
245                 if (!strcasecmp(name, proto_names[i]))
246                         return i;
247         }
248         return -1;
249 }
250
251 static char *l2_names[] = {
252         [RFID_LAYER2_ISO14443A] = "iso14443a",
253         [RFID_LAYER2_ISO14443B] = "iso14443b",
254         [RFID_LAYER2_ISO15693] = "iso15693",
255         [RFID_LAYER2_ICODE1] = "icode1",
256 };
257
258 static int l2_by_name(const char *name)
259 {
260         int i;
261
262         for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
263                 if (l2_names[i] == NULL)
264                         continue;
265                 if (!strcasecmp(name, l2_names[i]))
266                         return i;
267         }
268         return -1;
269 }
270
271 static int do_scan(int first)
272 {
273         int rc;
274         unsigned int size;
275         unsigned int size_len = sizeof(size);
276
277         if (first) {
278                 rh->reader->rf_power(rh, 0);
279                 usleep(10*1000);
280                 rh->reader->rf_power(rh, 1);
281         }
282         printf("scanning for RFID token...\n");
283         rc = rfid_scan(rh, &l2h, &ph);
284         if (rc >= 2) {
285                 unsigned char uid_buf[16];
286                 unsigned int uid_len = sizeof(uid_buf);
287                 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
288                                    &uid_len);
289                 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
290                         hexdump(uid_buf, uid_len));
291         }
292         if (rc >= 3) {
293                 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
294
295                 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
296                                          &size, &size_len) == 0)
297                         printf("Size: %u bytes\n", size);
298         }
299
300         return rc;
301 }
302
303 static void do_endless_scan()
304 {
305         int rc;
306         int first = 1;
307
308         while (1) {
309                 if (first)
310                         putc('\n', stdout);
311                 printf("==> doing %s scan\n", first ? "first" : "successive");
312                 rc = do_scan(first);
313                 if (rc >= 3) {
314                         printf("closing proto\n");
315                         rfid_protocol_close(ph);
316                 }
317                 if (rc >= 2) {
318                         printf("closing layer2\n");
319                         rfid_layer2_close(l2h);
320                         first = 0;
321                 } else
322                         first = 1;
323         }
324 }
325
326 static void do_regdump(void)
327 {
328         u_int8_t buffer[0xff];
329         int i;
330
331         printf("dumping rc632 regs...\n");
332
333         rc632_register_dump(rh->ah, buffer);
334
335         printf("\n     ");
336         for (i=0; i<=0x0f; i++)
337                 printf(" 0x_%01X",i);
338         printf("\n-----------------------------------------------------------------------------------\n");
339
340         for (i=0; i <= 0x3f; i++) {
341                 if ((i % 0x10) == 0)
342                         printf("0x%01X_:",i/0x10);
343                 printf(" 0x%02X", buffer[i]);
344                 if ((i% 0x10) == 0x0f)
345                         printf("\n");
346         }
347
348         /* print regdump as c-style array*/
349         printf("u_int8_t rc632_regs[] = {");
350         for (i = 0; i <= 0x3f; i++) {
351                 if (((i+1) % 0x08) == 1) {
352                         if (i > 7)
353                                 printf("//%2d..%2d",i-8,i-1);
354                         printf("\n\t");
355                 }
356                 printf(" 0x%02X, ",buffer[i]);
357         }
358         printf("//%2d..%2d\n\t 0 };\n",i-8,i-1);
359
360 }
361
362 static void do_enum(int layer2)
363 {
364         int rc;
365         //unsigned int size;
366         //unsigned int size_len = sizeof(size);
367         unsigned char uid_buf[16];
368         unsigned int uid_len;
369
370         printf("scanning for RFID token on layer %s...\n", l2_names[layer2]);
371
372         if (rh->reader->l2_supported & (1 << layer2)) {
373                 l2h = rfid_layer2_init(rh, layer2);
374                 rc = rfid_layer2_open(l2h);
375         } else {
376                 printf("error during layer2_open\n");
377                 return ;
378         }
379
380         while (rc>=0) {
381                 if (l2h) {
382                         uid_len = sizeof(uid_buf);
383                         rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf, &uid_len);
384                         printf("Layer 2 success (%s)[%d]: %s\n", rfid_layer2_name(l2h), uid_len, hexdump(uid_buf, uid_len));
385                 }
386
387         /*
388                 ph = rfid_protocol_scan(l2h);
389                 if (ph) {
390                         printf("Protocol success (%s)\n", rfid_protocol_name(ph));
391
392                         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
393                                              &size, &size_len) == 0)
394                         printf("Size: %u bytes\n", size);
395                 } else
396                         printf("##############\n");
397                 */
398
399                 if (rc >= 0) {
400                         rfid_layer2_close(l2h);
401                 }
402                 rc = rfid_layer2_open(l2h);
403         }
404 }
405
406 #define OPTION_OFFSET 256
407
408 static struct option original_opts[] = {
409         { "help", 0, 0, 'h' },
410         { "layer2", 1, 0, 'l' },
411         { "protocol", 1, 0, 'p' },
412         { "scan", 0, 0, 's' },
413         { "scan-loop", 0, 0, 'S' },
414         { "dump", 0, 0, 'd' },
415         { "enum", 0, 0, 'e' },
416         {0, 0, 0, 0}
417 };
418
419 /* module / option merging code */
420 static struct option *opts = original_opts;
421 static unsigned int global_option_offset = 0;
422
423 static char *program_name;
424 static char *program_version = LIBRFID_TOOL_VERSION;
425
426 static void free_opts(int reset_offset)
427 {
428         if (opts != original_opts) {
429                 free(opts);
430                 opts = original_opts;
431                 if (reset_offset)
432                         global_option_offset = 0;
433         }
434 }
435
436 static struct option *
437 merge_options(struct option *oldopts, const struct option *newopts,
438               unsigned int *option_offset)
439 {
440         unsigned int num_old, num_new, i;
441         struct option *merge;
442
443         for (num_old = 0; oldopts[num_old].name; num_old++);
444         for (num_new = 0; oldopts[num_new].name; num_new++);
445
446         global_option_offset += OPTION_OFFSET;
447         *option_offset = global_option_offset;
448
449         merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
450         memcpy(merge, oldopts, num_old * sizeof(struct option));
451         free_opts(0); /* Release previous options merged if any */
452         for (i = 0; i < num_new; i++) {
453                 merge[num_old + i] = newopts[i];
454                 merge[num_old + i].val += *option_offset;
455         }
456         memset(merge + num_old + num_new, 0, sizeof(struct option));
457
458         return merge;
459 }
460
461 struct rfidtool_module *find_module(const char *name)
462 {
463         return NULL;
464 }
465
466 void register_module(struct rfidtool_module *me)
467 {
468         struct rfidtool_module *old;
469
470         if (strcmp(me->version, program_version) != 0) {
471                 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
472                         program_name, me->name, me->version, program_version);
473                 exit(1);
474         }
475
476         old = find_module(me->name);
477         if (old) {
478                 fprintf(stderr, "%s: target `%s' already registered.\n",
479                         program_name, me->name);
480                 exit(1);
481         }
482 }
483
484 static void help(void)
485 {
486         printf( " -s    --scan          scan until first RFID tag is found\n"
487                 " -S    --scan-loop     endless scanning loop\n" 
488                 " -p    --protocol      {tcl,mifare-ultralight,mifare-classic,tagit}\n"
489                 " -l    --layer2        {iso14443a,iso14443b,iso15693}\n"
490                 " -d    --dump          dump rc632 registers\n"
491                 " -e    --enum          enumerate all tag's in field (iso14443a)\n"
492                 " -h    --help\n");
493 }
494
495 int main(int argc, char **argv)
496 {
497         int rc;
498         char buf[0x100];
499         int i, len, protocol = -1, layer2 = -1;
500
501 #ifdef  __MINGW32__
502         program_name = argv[0];
503 #else /*__MINGW32__*/
504         program_name = basename(argv[0]);
505 #endif/*__MINGW32__*/
506         
507         printf("%s - (C) 2005-2008 by Harald Welte\n"
508                "This program is Free Software and has "
509                "ABSOLUTELY NO WARRANTY\n\n", program_name);
510
511         printf("initializing librfid\n");
512         rfid_init();
513
514         while (1) {
515                 int c, option_index = 0;
516                 c = getopt_long(argc, argv, "hp:l:sSde", opts, &option_index);
517                 if (c == -1)
518                         break;
519
520                 switch (c) {
521                 case 'e':
522                         if (reader_init() < 0)
523                                 exit(1);
524                         layer2 = RFID_LAYER2_ISO14443A;
525                         do_enum(layer2);
526                         exit(0);
527                         break;
528                 case 'd':
529                         if (reader_init() < 0)
530                                 exit(1);
531                         do_regdump();
532                         break;
533                 case 's':
534                         if (reader_init() < 0)
535                                 exit(1);
536                         do_scan(0);
537                         exit(0);
538                         break;
539                 case 'S':
540                         if (reader_init() < 0)
541                                 exit(1);
542                         do_endless_scan();
543                         exit(0);
544                         break;
545                 case 'p':
546                         protocol = proto_by_name(optarg);
547                         if (protocol < 0) {
548                                 fprintf(stderr, "unknown protocol `%s'\n", 
549                                         optarg);
550                                 exit(2);
551                         }
552                         break;
553                 case 'l':
554                         layer2 = l2_by_name(optarg);
555                         if (layer2 < 0) {
556                                 fprintf(stderr, "unknown layer2 `%s'\n",
557                                         optarg);
558                                 exit(2);
559                         }
560                         break;
561                 case 'h':
562                         help();
563                         exit(0);
564                         break;
565                 }
566         }
567
568         switch (protocol) {
569         case RFID_PROTOCOL_MIFARE_UL:
570         case RFID_PROTOCOL_MIFARE_CLASSIC:
571                 layer2 = RFID_LAYER2_ISO14443A;
572                 break;
573         case -1:
574                 fprintf(stderr, "you have to specify --protocol\n");
575                 exit(2);
576         }
577
578         if (layer2 < 0) {
579                 fprintf(stderr, "you have to specify --layer2\n");
580                 exit(2);
581         }
582         
583         if (reader_init() < 0)
584                 exit(1);
585
586
587         if (l2_init(layer2) < 0) {
588                 rfid_reader_close(rh);
589                 exit(1);
590         }
591
592         if (l3_init(protocol) < 0) {
593                 rfid_reader_close(rh);
594                 exit(1);
595         }
596
597         switch (protocol) {
598
599         case RFID_PROTOCOL_TCL:
600                 printf("Protocol T=CL\n");
601                 /* we've established T=CL at this point */
602                 printf("selecting Master File\n");
603                 rc = select_mf();
604                 if (rc < 0) {
605                         printf("error selecting MF\n");
606                         break;
607                 }
608
609                 printf("Getting random challenge, length 255\n");
610                 rc = iso7816_get_challenge(0xff);
611                 if (rc < 0) {
612                         printf("error getting random challenge\n");
613                         break;
614                 }
615
616                 printf("selecting Passport application\n");
617                 rc = iso7816_select_application();
618                 if (rc < 0) {
619                         printf("error selecting passport application\n");
620                         break;
621                 }
622
623                 printf("selecting EF 0x1e\n");
624                 rc = iso7816_select_ef(0x011e);
625                 if (rc < 0) {
626                         printf("error selecting EF 0x1e\n");
627                         break;
628                 }
629
630                 printf("selecting EF 0x01\n");
631                 rc = iso7816_select_ef(0x0101);
632                 if (rc < 0) {
633                         printf("error selecting EF 0x01\n");
634                         break;
635                 }
636
637                 while (1) {
638                         printf("reading EF1\n");
639                         len = sizeof(buf);
640                         printf("reading ef\n");
641                         rc = iso7816_read_binary(buf, &len);
642                         if (rc < 0) {
643                                 printf("error reading EF\n");
644                                 break;
645                         }
646                 }
647 #if 0
648                 for (i = 0; i < 4; i++)
649                         iso7816_get_challenge(0xff);
650 #endif
651                 break;
652         case RFID_PROTOCOL_MIFARE_UL:
653                 printf("Protocol Mifare Ultralight\n");
654                 mifare_ulight_read(ph);
655 #if 0
656                 mifare_ulight_blank(ph);
657                 mifare_ulight_write(ph);
658                 mifare_ulight_read(ph);
659 #endif
660                 break;
661         case RFID_PROTOCOL_MIFARE_CLASSIC:
662                 printf("Protocol Mifare Classic\n");
663                 {
664                         int sector;
665                         for (sector = 0; sector < 31; sector++) {
666                                 printf("Authenticating sector %u: ", sector);
667                                 fflush(stdout);
668                                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
669                                 if (rc < 0) {
670                                         printf("key format error\n");
671                                         exit(1);
672                                 }
673                                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
674                                 if (rc < 0) {
675                                         printf("mifare auth error\n");
676                                         exit(1);
677                                 } else 
678                                         printf("mifare auth succeeded!\n");
679
680                                 mifare_classic_read_sector(ph, sector);
681                         }
682                 }
683                 break;
684         default:
685                 printf("unknown protocol %u\n", protocol);
686                 exit(1);
687                 break;
688         }
689
690         rfid_reader_close(rh);
691         
692         exit(0);
693 }