* implement rfid_reader_{get,set}opt()
[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, blocks_per_sector, first_block;
211
212         printf("Reading sector %u\n", sector);
213
214         first_block = mfcl_sector2block(sector);
215         blocks_per_sector = mfcl_sector_blocks(sector);
216
217         if (first_block < 0 || blocks_per_sector < 0)
218                 return -EINVAL;
219
220         for (block = first_block; block < first_block + blocks_per_sector;
221              block++) {
222                 printf("Reading block %u: ", block);
223                 ret = rfid_protocol_read(ph, block, buf, &len);
224                 if (ret == -ETIMEDOUT)
225                         fprintf(stderr, "TIMEOUT\n");
226                 if (ret < 0) {
227                         printf("Error %d reading\n", ret);
228                         return ret;
229                 }
230
231                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
232         }
233         return 0;
234 }
235
236 static int
237 mifare_classic_dump(struct rfid_protocol_handle *ph)
238 {
239         unsigned int size;
240         unsigned int size_len = sizeof(size);
241         int sector, num_sectors;
242
243         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
244                                  &size, &size_len) == 0) {
245                 printf("Size: %u bytes\n", size);
246         } else {
247                 printf("Size: unknown ?!?\n");
248                 return -EINVAL;
249         }
250
251         switch (size) {
252         case 320:
253                 num_sectors = 5;
254                 break;
255         case 1024:
256                 num_sectors = 16;
257                 break;
258         case 4096:
259                 num_sectors = 40;
260                 break;
261         default:
262                 return -EINVAL;
263         }
264
265         for (sector = 0; sector < num_sectors; sector++) {
266                 int rc;
267
268                 printf("Authenticating sector %u: ", sector);
269                 fflush(stdout);
270
271                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
272                 if (rc < 0) {
273                         printf("key format error\n");
274                         exit(1);
275                 }
276
277                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A,
278                                mfcl_sector2block(sector));
279                 if (rc < 0) {
280                         printf("mifare auth error\n");
281                         exit(1);
282                 } else 
283                         printf("mifare auth succeeded!\n");
284
285                 mifare_classic_read_sector(ph, sector);
286         }
287 }
288
289 static char *proto_names[] = {
290         [RFID_PROTOCOL_TCL] = "tcl",
291         [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
292         [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
293         [RFID_PROTOCOL_ICODE_SLI] = "icode",
294         [RFID_PROTOCOL_TAGIT] = "tagit",
295 };
296
297 static int proto_by_name(const char *name)
298 {
299         int i;
300
301         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
302                 if (proto_names[i] == NULL)
303                         continue;
304                 if (!strcasecmp(name, proto_names[i]))
305                         return i;
306         }
307         return -1;
308 }
309
310 static char *l2_names[] = {
311         [RFID_LAYER2_ISO14443A] = "iso14443a",
312         [RFID_LAYER2_ISO14443B] = "iso14443b",
313         [RFID_LAYER2_ISO15693] = "iso15693",
314         [RFID_LAYER2_ICODE1] = "icode1",
315 };
316
317 static int l2_by_name(const char *name)
318 {
319         int i;
320
321         for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
322                 if (l2_names[i] == NULL)
323                         continue;
324                 if (!strcasecmp(name, l2_names[i]))
325                         return i;
326         }
327         return -1;
328 }
329
330 static int do_scan(int first)
331 {
332         int rc;
333         unsigned int size;
334         unsigned int size_len = sizeof(size);
335
336         if (first) {
337                 unsigned int opt;
338                 unsigned int optlen = sizeof(opt);
339
340                 /* turn off RF */
341                 opt = 1;
342                 rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
343
344                 usleep(10*1000);
345
346                 /* turn on RF */
347                 opt = 0;
348                 rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
349         }
350         printf("scanning for RFID token...\n");
351         rc = rfid_scan(rh, &l2h, &ph);
352         if (rc >= 2) {
353                 unsigned char uid_buf[16];
354                 unsigned int uid_len = sizeof(uid_buf);
355                 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
356                                    &uid_len);
357                 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
358                         hexdump(uid_buf, uid_len));
359         }
360         if (rc >= 3) {
361                 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
362
363                 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
364                                          &size, &size_len) == 0)
365                         printf("Size: %u bytes\n", size);
366         }
367
368         return rc;
369 }
370
371 static void do_endless_scan()
372 {
373         int rc;
374         int first = 1;
375
376         while (1) {
377                 if (first)
378                         putc('\n', stdout);
379                 printf("==> doing %s scan\n", first ? "first" : "successive");
380                 rc = do_scan(first);
381                 if (rc >= 3) {
382                         printf("closing proto\n");
383                         rfid_protocol_close(ph);
384                 }
385                 if (rc >= 2) {
386                         printf("closing layer2\n");
387                         rfid_layer2_close(l2h);
388                         first = 0;
389                 } else
390                         first = 1;
391         }
392 }
393
394 static void do_regdump(void)
395 {
396         u_int8_t buffer[0xff];
397         int i;
398
399         printf("dumping rc632 regs...\n");
400
401         rc632_register_dump(rh->ah, buffer);
402
403         printf("\n     ");
404         for (i=0; i<=0x0f; i++)
405                 printf(" 0x_%01X",i);
406         printf("\n-----------------------------------------------------------------------------------\n");
407
408         for (i=0; i <= 0x3f; i++) {
409                 if ((i % 0x10) == 0)
410                         printf("0x%01X_:",i/0x10);
411                 printf(" 0x%02X", buffer[i]);
412                 if ((i% 0x10) == 0x0f)
413                         printf("\n");
414         }
415
416         /* print regdump as c-style array*/
417         printf("u_int8_t rc632_regs[] = {");
418         for (i = 0; i <= 0x3f; i++) {
419                 if (((i+1) % 0x08) == 1) {
420                         if (i > 7)
421                                 printf("//%2d..%2d",i-8,i-1);
422                         printf("\n\t");
423                 }
424                 printf(" 0x%02X, ",buffer[i]);
425         }
426         printf("//%2d..%2d\n\t 0 };\n",i-8,i-1);
427
428 }
429
430 static void do_enum(int layer2)
431 {
432         int rc;
433         //unsigned int size;
434         //unsigned int size_len = sizeof(size);
435         unsigned char uid_buf[16];
436         unsigned int uid_len;
437
438         printf("scanning for RFID token on layer %s...\n", l2_names[layer2]);
439
440         if (rh->reader->l2_supported & (1 << layer2)) {
441                 l2h = rfid_layer2_init(rh, layer2);
442                 rc = rfid_layer2_open(l2h);
443         } else {
444                 printf("error during layer2_open\n");
445                 return ;
446         }
447
448         while (rc>=0) {
449                 if (l2h) {
450                         uid_len = sizeof(uid_buf);
451                         rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf, &uid_len);
452                         printf("Layer 2 success (%s)[%d]: %s\n", rfid_layer2_name(l2h), uid_len, hexdump(uid_buf, uid_len));
453                 }
454
455         /*
456                 ph = rfid_protocol_scan(l2h);
457                 if (ph) {
458                         printf("Protocol success (%s)\n", rfid_protocol_name(ph));
459
460                         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
461                                              &size, &size_len) == 0)
462                         printf("Size: %u bytes\n", size);
463                 } else
464                         printf("##############\n");
465                 */
466
467                 if (rc >= 0) {
468                         rfid_layer2_close(l2h);
469                 }
470                 rc = rfid_layer2_open(l2h);
471         }
472 }
473
474 #define OPTION_OFFSET 256
475
476 static struct option original_opts[] = {
477         { "help", 0, 0, 'h' },
478         { "layer2", 1, 0, 'l' },
479         { "protocol", 1, 0, 'p' },
480         { "scan", 0, 0, 's' },
481         { "scan-loop", 0, 0, 'S' },
482         { "dump", 0, 0, 'd' },
483         { "enum", 0, 0, 'e' },
484         {0, 0, 0, 0}
485 };
486
487 /* module / option merging code */
488 static struct option *opts = original_opts;
489 static unsigned int global_option_offset = 0;
490
491 static char *program_name;
492 static char *program_version = LIBRFID_TOOL_VERSION;
493
494 static void free_opts(int reset_offset)
495 {
496         if (opts != original_opts) {
497                 free(opts);
498                 opts = original_opts;
499                 if (reset_offset)
500                         global_option_offset = 0;
501         }
502 }
503
504 static struct option *
505 merge_options(struct option *oldopts, const struct option *newopts,
506               unsigned int *option_offset)
507 {
508         unsigned int num_old, num_new, i;
509         struct option *merge;
510
511         for (num_old = 0; oldopts[num_old].name; num_old++);
512         for (num_new = 0; oldopts[num_new].name; num_new++);
513
514         global_option_offset += OPTION_OFFSET;
515         *option_offset = global_option_offset;
516
517         merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
518         memcpy(merge, oldopts, num_old * sizeof(struct option));
519         free_opts(0); /* Release previous options merged if any */
520         for (i = 0; i < num_new; i++) {
521                 merge[num_old + i] = newopts[i];
522                 merge[num_old + i].val += *option_offset;
523         }
524         memset(merge + num_old + num_new, 0, sizeof(struct option));
525
526         return merge;
527 }
528
529 struct rfidtool_module *find_module(const char *name)
530 {
531         return NULL;
532 }
533
534 void register_module(struct rfidtool_module *me)
535 {
536         struct rfidtool_module *old;
537
538         if (strcmp(me->version, program_version) != 0) {
539                 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
540                         program_name, me->name, me->version, program_version);
541                 exit(1);
542         }
543
544         old = find_module(me->name);
545         if (old) {
546                 fprintf(stderr, "%s: target `%s' already registered.\n",
547                         program_name, me->name);
548                 exit(1);
549         }
550 }
551
552 static void help(void)
553 {
554         printf( " -s    --scan          scan until first RFID tag is found\n"
555                 " -S    --scan-loop     endless scanning loop\n" 
556                 " -p    --protocol      {tcl,mifare-ultralight,mifare-classic,tagit}\n"
557                 " -l    --layer2        {iso14443a,iso14443b,iso15693}\n"
558                 " -d    --dump          dump rc632 registers\n"
559                 " -e    --enum          enumerate all tag's in field (iso14443a)\n"
560                 " -h    --help\n");
561 }
562
563 int main(int argc, char **argv)
564 {
565         int rc;
566         char buf[0x100];
567         int i, len, protocol = -1, layer2 = -1;
568
569 #ifdef  __MINGW32__
570         program_name = argv[0];
571 #else /*__MINGW32__*/
572         program_name = basename(argv[0]);
573 #endif/*__MINGW32__*/
574         
575         printf("%s - (C) 2005-2008 by Harald Welte\n"
576                "This program is Free Software and has "
577                "ABSOLUTELY NO WARRANTY\n\n", program_name);
578
579         printf("initializing librfid\n");
580         rfid_init();
581
582         while (1) {
583                 int c, option_index = 0;
584                 c = getopt_long(argc, argv, "hp:l:sSde", opts, &option_index);
585                 if (c == -1)
586                         break;
587
588                 switch (c) {
589                 case 'e':
590                         if (reader_init() < 0)
591                                 exit(1);
592                         layer2 = RFID_LAYER2_ISO14443A;
593                         do_enum(layer2);
594                         exit(0);
595                         break;
596                 case 'd':
597                         if (reader_init() < 0)
598                                 exit(1);
599                         do_regdump();
600                         break;
601                 case 's':
602                         if (reader_init() < 0)
603                                 exit(1);
604                         do_scan(0);
605                         exit(0);
606                         break;
607                 case 'S':
608                         if (reader_init() < 0)
609                                 exit(1);
610                         do_endless_scan();
611                         exit(0);
612                         break;
613                 case 'p':
614                         protocol = proto_by_name(optarg);
615                         if (protocol < 0) {
616                                 fprintf(stderr, "unknown protocol `%s'\n", 
617                                         optarg);
618                                 exit(2);
619                         }
620                         break;
621                 case 'l':
622                         layer2 = l2_by_name(optarg);
623                         if (layer2 < 0) {
624                                 fprintf(stderr, "unknown layer2 `%s'\n",
625                                         optarg);
626                                 exit(2);
627                         }
628                         break;
629                 case 'h':
630                         help();
631                         exit(0);
632                         break;
633                 }
634         }
635
636         switch (protocol) {
637         case RFID_PROTOCOL_MIFARE_UL:
638         case RFID_PROTOCOL_MIFARE_CLASSIC:
639                 layer2 = RFID_LAYER2_ISO14443A;
640                 break;
641         case -1:
642                 fprintf(stderr, "you have to specify --protocol\n");
643                 exit(2);
644         }
645
646         if (layer2 < 0) {
647                 fprintf(stderr, "you have to specify --layer2\n");
648                 exit(2);
649         }
650         
651         if (reader_init() < 0)
652                 exit(1);
653
654
655         if (l2_init(layer2) < 0) {
656                 rfid_reader_close(rh);
657                 exit(1);
658         }
659
660         if (l3_init(protocol) < 0) {
661                 rfid_reader_close(rh);
662                 exit(1);
663         }
664
665         switch (protocol) {
666
667         case RFID_PROTOCOL_TCL:
668                 printf("Protocol T=CL\n");
669                 /* we've established T=CL at this point */
670                 printf("selecting Master File\n");
671                 rc = select_mf();
672                 if (rc < 0) {
673                         printf("error selecting MF\n");
674                         break;
675                 }
676
677                 printf("Getting random challenge, length 255\n");
678                 rc = iso7816_get_challenge(0xff);
679                 if (rc < 0) {
680                         printf("error getting random challenge\n");
681                         break;
682                 }
683
684                 printf("selecting Passport application\n");
685                 rc = iso7816_select_application();
686                 if (rc < 0) {
687                         printf("error selecting passport application\n");
688                         break;
689                 }
690
691                 printf("selecting EF 0x1e\n");
692                 rc = iso7816_select_ef(0x011e);
693                 if (rc < 0) {
694                         printf("error selecting EF 0x1e\n");
695                         break;
696                 }
697
698                 printf("selecting EF 0x01\n");
699                 rc = iso7816_select_ef(0x0101);
700                 if (rc < 0) {
701                         printf("error selecting EF 0x01\n");
702                         break;
703                 }
704
705                 while (1) {
706                         printf("reading EF1\n");
707                         len = sizeof(buf);
708                         printf("reading ef\n");
709                         rc = iso7816_read_binary(buf, &len);
710                         if (rc < 0) {
711                                 printf("error reading EF\n");
712                                 break;
713                         }
714                 }
715 #if 0
716                 for (i = 0; i < 4; i++)
717                         iso7816_get_challenge(0xff);
718 #endif
719                 break;
720         case RFID_PROTOCOL_MIFARE_UL:
721                 printf("Protocol Mifare Ultralight\n");
722                 mifare_ulight_read(ph);
723 #if 0
724                 mifare_ulight_blank(ph);
725                 mifare_ulight_write(ph);
726                 mifare_ulight_read(ph);
727 #endif
728                 break;
729         case RFID_PROTOCOL_MIFARE_CLASSIC:
730                 printf("Protocol Mifare Classic\n");
731                 mifare_classic_dump(ph);
732                 break;
733         default:
734                 printf("unknown protocol %u\n", protocol);
735                 exit(1);
736                 break;
737         }
738
739         rfid_reader_close(rh);
740         
741         exit(0);
742 }