c8b59eae7b9698d1a633c77a8a13541ac6e066b5
[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 #include <librfid/rfid_protocol_tcl.h>
46
47 #include "librfid-tool.h"
48
49
50 static int select_mf(void)
51 {
52         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
53         unsigned char ret[256];
54         unsigned int rlen = sizeof(ret);
55
56         int rv;
57
58         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
59         if (rv < 0)
60                 return rv;
61
62         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
63
64         return 0;
65 }
66
67
68 static int iso7816_get_challenge(unsigned char len)
69 {
70         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
71         unsigned char ret[256];
72         unsigned int rlen = sizeof(ret);
73
74         cmd[4] = len;
75
76         int rv;
77
78         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
79         if (rv < 0)
80                 return rv;
81
82         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
83
84         return 0;
85 }
86
87 int
88 iso7816_select_application(void)
89 {
90         unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
91                        0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
92         unsigned char resp[7];
93         unsigned int rlen = sizeof(resp);
94
95         int rv;
96
97         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
98         if (rv < 0)
99                 return rv;
100
101         /* FIXME: parse response */
102         printf("%s\n", hexdump(resp, rlen));
103
104         return 0;
105 }
106
107 int
108 iso7816_select_ef(u_int16_t fid)
109 {
110         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
111         unsigned char resp[7];
112         unsigned int rlen = sizeof(resp);
113
114         int rv;
115
116         cmd[5] = (fid >> 8) & 0xff;
117         cmd[6] = fid & 0xff;
118
119         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
120         if (rv < 0)
121                 return rv;
122
123         /* FIXME: parse response */
124         printf("%s\n", hexdump(resp, rlen));
125
126         return 0;
127 }
128
129 int
130 iso7816_read_binary(unsigned char *buf, unsigned int *len)
131 {
132         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
133         unsigned char resp[256];
134         unsigned int rlen = sizeof(resp);
135         
136         int rv;
137
138         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
139         if (rv < 0)
140                 return rv;
141
142         printf("%s\n", hexdump(resp, rlen));
143
144         /* FIXME: parse response, determine whether we need additional reads */
145
146         /* FIXME: copy 'len' number of response bytes to 'buf' */
147         return 0;
148 }
149
150 /* wrapper function around SELECT EF and READ BINARY */
151 int
152 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
153 {
154         int rv;
155
156         rv = iso7816_select_ef(fid);
157         if (rv < 0)
158                 return rv;
159
160         return iso7816_read_binary(buf, len);
161 }
162
163 /* mifare ultralight helpers */
164 int
165 mifare_ulight_write(struct rfid_protocol_handle *ph)
166 {
167         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
168
169         return rfid_protocol_write(ph, 10, buf, 4);
170 }
171
172 int
173 mifare_ulight_blank(struct rfid_protocol_handle *ph)
174 {
175         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
176         int i, ret;
177
178         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
179                 ret = rfid_protocol_write(ph, i, buf, 4);
180                 if (ret < 0)
181                         return ret;
182         }
183         return 0;
184 }
185
186 static int
187 mifare_ulight_read(struct rfid_protocol_handle *ph)
188 {
189         unsigned char buf[20];
190         unsigned int len = sizeof(buf);
191         int ret;
192         int i;
193
194         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
195                 ret = rfid_protocol_read(ph, i, buf, &len);
196                 if (ret < 0)
197                         return ret;
198
199                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
200         }
201         return 0;
202 }
203
204 /* mifare classic helpers */
205 static int
206 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
207 {
208         unsigned char buf[20];
209         unsigned int len = sizeof(buf);
210         int ret;
211         int block, blocks_per_sector, first_block;
212
213         printf("Reading sector %u\n", sector);
214
215         first_block = mfcl_sector2block(sector);
216         blocks_per_sector = mfcl_sector_blocks(sector);
217
218         if (first_block < 0 || blocks_per_sector < 0)
219                 return -EINVAL;
220
221         for (block = first_block; block < first_block + blocks_per_sector;
222              block++) {
223                 printf("Reading block %u: ", block);
224                 ret = rfid_protocol_read(ph, block, buf, &len);
225                 if (ret == -ETIMEDOUT)
226                         fprintf(stderr, "TIMEOUT\n");
227                 if (ret < 0) {
228                         printf("Error %d reading\n", ret);
229                         return ret;
230                 }
231
232                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
233         }
234         return 0;
235 }
236
237 static int
238 mifare_classic_dump(struct rfid_protocol_handle *ph)
239 {
240         unsigned int size;
241         unsigned int size_len = sizeof(size);
242         int sector, num_sectors;
243
244         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
245                                  &size, &size_len) == 0) {
246                 printf("Size: %u bytes\n", size);
247         } else {
248                 printf("Size: unknown ?!?\n");
249                 return -EINVAL;
250         }
251
252         switch (size) {
253         case 320:
254                 num_sectors = 5;
255                 break;
256         case 1024:
257                 num_sectors = 16;
258                 break;
259         case 4096:
260                 num_sectors = 40;
261                 break;
262         default:
263                 return -EINVAL;
264         }
265
266         for (sector = 0; sector < num_sectors; sector++) {
267                 int rc;
268
269                 printf("Authenticating sector %u: ", sector);
270                 fflush(stdout);
271
272                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
273                 if (rc < 0) {
274                         printf("key format error\n");
275                         exit(1);
276                 }
277
278                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A,
279                                mfcl_sector2block(sector));
280                 if (rc < 0) {
281                         printf("mifare auth error\n");
282                         exit(1);
283                 } else 
284                         printf("mifare auth succeeded!\n");
285
286                 mifare_classic_read_sector(ph, sector);
287         }
288 }
289
290 static char *proto_names[] = {
291         [RFID_PROTOCOL_TCL] = "tcl",
292         [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
293         [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
294         [RFID_PROTOCOL_ICODE_SLI] = "icode",
295         [RFID_PROTOCOL_TAGIT] = "tagit",
296 };
297
298 static int proto_by_name(const char *name)
299 {
300         int i;
301
302         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
303                 if (proto_names[i] == NULL)
304                         continue;
305                 if (!strcasecmp(name, proto_names[i]))
306                         return i;
307         }
308         return -1;
309 }
310
311 static char *l2_names[] = {
312         [RFID_LAYER2_ISO14443A] = "iso14443a",
313         [RFID_LAYER2_ISO14443B] = "iso14443b",
314         [RFID_LAYER2_ISO15693] = "iso15693",
315         [RFID_LAYER2_ICODE1] = "icode1",
316 };
317
318 static int l2_by_name(const char *name)
319 {
320         int i;
321
322         for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
323                 if (l2_names[i] == NULL)
324                         continue;
325                 if (!strcasecmp(name, l2_names[i]))
326                         return i;
327         }
328         return -1;
329 }
330
331 static int do_scan(int first)
332 {
333         int rc;
334         unsigned int size;
335         unsigned int size_len = sizeof(size);
336         char *data;
337         unsigned int data_len;
338
339         if (first) {
340                 unsigned int opt;
341                 unsigned int optlen = sizeof(opt);
342
343                 /* turn off RF */
344                 opt = 1;
345                 rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
346
347                 usleep(10*1000);
348
349                 /* turn on RF */
350                 opt = 0;
351                 rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
352         }
353         printf("scanning for RFID token...\n");
354         rc = rfid_scan(rh, &l2h, &ph);
355         if (rc >= 2) {
356                 unsigned char uid_buf[16];
357                 unsigned int uid_len = sizeof(uid_buf);
358                 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
359                                    &uid_len);
360                 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
361                         hexdump(uid_buf, uid_len));
362         }
363         if (rc >= 3) {
364                 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
365
366                 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
367                                          &size, &size_len) == 0)
368                         printf("Size: %u bytes\n", size);
369                 size_len = sizeof(size);
370                 size = 0;
371                 if (rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS_LEN,
372                                          &size, &size_len) == 0) {
373                         data_len = size + 1;
374                         data = malloc(data_len);
375                         if (data) {
376                                 if (rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS,
377                                                          data, &data_len) == 0) {
378                                         printf("Got ATS of %u bytes: %s\n", size,
379                                                hexdump(data, data_len));
380                                 }
381                         }
382                 }
383         }
384
385         return rc;
386 }
387
388 static void do_endless_scan()
389 {
390         int rc;
391         int first = 1;
392
393         while (1) {
394                 if (first)
395                         putc('\n', stdout);
396                 printf("==> doing %s scan\n", first ? "first" : "successive");
397                 rc = do_scan(first);
398                 if (rc >= 3) {
399                         printf("closing proto\n");
400                         rfid_protocol_close(ph);
401                 }
402                 if (rc >= 2) {
403                         printf("closing layer2\n");
404                         rfid_layer2_close(l2h);
405                         first = 0;
406                 } else
407                         first = 1;
408         }
409 }
410
411 static void do_regdump(void)
412 {
413         u_int8_t buffer[0xff];
414         int i;
415
416         printf("dumping rc632 regs...\n");
417
418         rc632_register_dump(rh->ah, buffer);
419
420         printf("\n     ");
421         for (i=0; i<=0x0f; i++)
422                 printf(" 0x_%01X",i);
423         printf("\n-----------------------------------------------------------------------------------\n");
424
425         for (i=0; i <= 0x3f; i++) {
426                 if ((i % 0x10) == 0)
427                         printf("0x%01X_:",i/0x10);
428                 printf(" 0x%02X", buffer[i]);
429                 if ((i% 0x10) == 0x0f)
430                         printf("\n");
431         }
432
433         /* print regdump as c-style array*/
434         printf("u_int8_t rc632_regs[] = {");
435         for (i = 0; i <= 0x3f; i++) {
436                 if (((i+1) % 0x08) == 1) {
437                         if (i > 7)
438                                 printf("//%2d..%2d",i-8,i-1);
439                         printf("\n\t");
440                 }
441                 printf(" 0x%02X, ",buffer[i]);
442         }
443         printf("//%2d..%2d\n\t 0 };\n",i-8,i-1);
444
445 }
446
447 static void do_enum(int layer2)
448 {
449         int rc;
450         //unsigned int size;
451         //unsigned int size_len = sizeof(size);
452         unsigned char uid_buf[16];
453         unsigned int uid_len;
454
455         printf("scanning for RFID token on layer %s...\n", l2_names[layer2]);
456
457         if (rh->reader->l2_supported & (1 << layer2)) {
458                 l2h = rfid_layer2_init(rh, layer2);
459                 if (!l2h) {
460                         printf("error during layer2(%s)_init\n",
461                                l2_names[layer2]);
462                         return;
463                 }
464                 printf("Layer2 init ok\n");
465                 rc = rfid_layer2_open(l2h);
466         } else {
467                 printf("error during layer2_open\n");
468                 return ;
469         }
470
471         while (rc>=0) {
472                 if (l2h) {
473                         uid_len = sizeof(uid_buf);
474                         rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf, &uid_len);
475                         printf("Layer 2 success (%s)[%d]: '%s'\n", rfid_layer2_name(l2h), uid_len, hexdump(uid_buf, uid_len));
476                 }
477
478         /*
479                 ph = rfid_protocol_scan(l2h);
480                 if (ph) {
481                         printf("Protocol success (%s)\n", rfid_protocol_name(ph));
482
483                         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
484                                              &size, &size_len) == 0)
485                         printf("Size: %u bytes\n", size);
486                 } else
487                         printf("##############\n");
488                 */
489
490                 if (rc >= 0) {
491                         rfid_layer2_close(l2h);
492                 }
493                 rc = rfid_layer2_open(l2h);
494         }
495 }
496
497 static void do_enum_loop(int layer2, unsigned int delay)
498 {
499         while (1) {
500                 do_enum(layer2);
501                 {
502                         unsigned int opt;
503                         unsigned int optlen = sizeof(opt);
504
505                         /* turn off RF */
506                         opt = 1;
507                         rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
508
509                         usleep(10 * 1000);
510
511                         /* turn on RF */
512                         opt = 0;
513                         rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
514                 }
515                 usleep(delay * 1000);
516                 printf("--- next run ---\n");
517         }
518 }
519
520 #define OPTION_OFFSET 256
521
522 static struct option original_opts[] = {
523         { "help", 0, 0, 'h' },
524         { "layer2", 1, 0, 'l' },
525         { "protocol", 1, 0, 'p' },
526         { "scan", 0, 0, 's' },
527         { "scan-loop", 0, 0, 'S' },
528         { "dump", 0, 0, 'd' },
529         { "enum", 0, 0, 'e' },
530         { "enum-loop", 1, 0, 'E' },
531         {0, 0, 0, 0}
532 };
533
534 /* module / option merging code */
535 static struct option *opts = original_opts;
536 static unsigned int global_option_offset = 0;
537
538 static char *program_name;
539 static char *program_version = LIBRFID_TOOL_VERSION;
540
541 static void free_opts(int reset_offset)
542 {
543         if (opts != original_opts) {
544                 free(opts);
545                 opts = original_opts;
546                 if (reset_offset)
547                         global_option_offset = 0;
548         }
549 }
550
551 static struct option *
552 merge_options(struct option *oldopts, const struct option *newopts,
553               unsigned int *option_offset)
554 {
555         unsigned int num_old, num_new, i;
556         struct option *merge;
557
558         for (num_old = 0; oldopts[num_old].name; num_old++);
559         for (num_new = 0; oldopts[num_new].name; num_new++);
560
561         global_option_offset += OPTION_OFFSET;
562         *option_offset = global_option_offset;
563
564         merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
565         memcpy(merge, oldopts, num_old * sizeof(struct option));
566         free_opts(0); /* Release previous options merged if any */
567         for (i = 0; i < num_new; i++) {
568                 merge[num_old + i] = newopts[i];
569                 merge[num_old + i].val += *option_offset;
570         }
571         memset(merge + num_old + num_new, 0, sizeof(struct option));
572
573         return merge;
574 }
575
576 struct rfidtool_module *find_module(const char *name)
577 {
578         return NULL;
579 }
580
581 void register_module(struct rfidtool_module *me)
582 {
583         struct rfidtool_module *old;
584
585         if (strcmp(me->version, program_version) != 0) {
586                 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
587                         program_name, me->name, me->version, program_version);
588                 exit(1);
589         }
590
591         old = find_module(me->name);
592         if (old) {
593                 fprintf(stderr, "%s: target `%s' already registered.\n",
594                         program_name, me->name);
595                 exit(1);
596         }
597 }
598
599 static void help(void)
600 {
601         printf( " -s    --scan          scan until first RFID tag is found\n"
602                 " -S    --scan-loop     endless scanning loop\n" 
603                 " -p    --protocol      {tcl,mifare-ultralight,mifare-classic,tagit,icode}\n"
604                 " -l    --layer2        {iso14443a,iso14443b,iso15693,icode1}\n"
605                 " -d    --dump          dump rc632 registers\n"
606                 " -e    --enum          enumerate all tag's in field \n"
607                 " -E    --enum-loop     <delay> (ms) enumerate endless\n"
608                 " -h    --help\n");
609 }
610
611 int main(int argc, char **argv)
612 {
613         int rc;
614         char buf[0x100];
615         int i, len, protocol = -1, layer2 = -1;
616
617 #ifdef  __MINGW32__
618         program_name = argv[0];
619 #else /*__MINGW32__*/
620         program_name = basename(argv[0]);
621 #endif/*__MINGW32__*/
622         
623         printf("%s - (C) 2005-2008 by Harald Welte\n"
624                "This program is Free Software and has "
625                "ABSOLUTELY NO WARRANTY\n\n", program_name);
626
627         printf("initializing librfid\n");
628         rfid_init();
629
630         while (1) {
631                 int c, option_index = 0;
632                 c = getopt_long(argc, argv, "hp:l:sSdeE:", opts, &option_index);
633                 if (c == -1)
634                         break;
635
636                 switch (c) {
637                 case 'E':
638                         i = strtol(optarg, NULL, 10);
639
640                         if (reader_init() < 0)
641                                 exit(1);
642                         if (layer2<0)
643                                 layer2 = RFID_LAYER2_ISO14443A;
644
645                         do_enum_loop(layer2, i>1? i : 500);
646                         rfid_reader_close(rh);
647                         exit(0);
648                         break;
649                 case 'e':
650                         if (reader_init() < 0)
651                                 exit(1);
652                         if (layer2 < 0)
653                                 layer2 = RFID_LAYER2_ISO14443A;
654                         do_enum(layer2);
655                         rfid_reader_close(rh);
656                         exit(0);
657                         break;
658                 case 'd':
659                         if (reader_init() < 0)
660                                 exit(1);
661                         do_regdump();
662                         rfid_reader_close(rh);
663                         break;
664                 case 's':
665                         if (reader_init() < 0)
666                                 exit(1);
667                         do_scan(0);
668                         rfid_reader_close(rh);
669                         exit(0);
670                         break;
671                 case 'S':
672                         if (reader_init() < 0)
673                                 exit(1);
674                         do_endless_scan();
675                         exit(0);
676                         break;
677                 case 'p':
678                         protocol = proto_by_name(optarg);
679                         if (protocol < 0) {
680                                 fprintf(stderr, "unknown protocol `%s'\n", 
681                                         optarg);
682                                 exit(2);
683                         }
684                         break;
685                 case 'l':
686                         layer2 = l2_by_name(optarg);
687                         if (layer2 < 0) {
688                                 fprintf(stderr, "unknown layer2 `%s'\n",
689                                         optarg);
690                                 exit(2);
691                         }
692                         break;
693                 default:
694                         printf("unknown cmd: %c\n",c);
695                 case 'h':
696                         help();
697                         exit(0);
698                         break;
699                 case '?':
700                         exit(0);
701                 }
702         }
703
704         switch (protocol) {
705         case RFID_PROTOCOL_MIFARE_UL:
706         case RFID_PROTOCOL_MIFARE_CLASSIC:
707                 layer2 = RFID_LAYER2_ISO14443A;
708                 break;
709         case -1:
710                 fprintf(stderr, "you have to specify --protocol\n");
711                 exit(2);
712         }
713
714         if (layer2 < 0) {
715                 fprintf(stderr, "you have to specify --layer2\n");
716                 exit(2);
717         }
718         
719         if (reader_init() < 0)
720                 exit(1);
721
722
723         if (l2_init(layer2) < 0) {
724                 rfid_reader_close(rh);
725                 exit(1);
726         }
727
728         if (l3_init(protocol) < 0) {
729                 rfid_reader_close(rh);
730                 exit(1);
731         }
732
733         switch (protocol) {
734
735         case RFID_PROTOCOL_TCL:
736                 printf("Protocol T=CL\n");
737                 /* we've established T=CL at this point */
738                 printf("selecting Master File\n");
739                 rc = select_mf();
740                 if (rc < 0) {
741                         printf("error selecting MF\n");
742                         break;
743                 }
744
745                 printf("Getting random challenge, length 255\n");
746                 rc = iso7816_get_challenge(0xff);
747                 if (rc < 0) {
748                         printf("error getting random challenge\n");
749                         break;
750                 }
751
752                 printf("selecting Passport application\n");
753                 rc = iso7816_select_application();
754                 if (rc < 0) {
755                         printf("error selecting passport application\n");
756                         break;
757                 }
758
759                 printf("selecting EF 0x1e\n");
760                 rc = iso7816_select_ef(0x011e);
761                 if (rc < 0) {
762                         printf("error selecting EF 0x1e\n");
763                         break;
764                 }
765
766                 printf("selecting EF 0x01\n");
767                 rc = iso7816_select_ef(0x0101);
768                 if (rc < 0) {
769                         printf("error selecting EF 0x01\n");
770                         break;
771                 }
772
773                 while (1) {
774                         printf("reading EF1\n");
775                         len = sizeof(buf);
776                         printf("reading ef\n");
777                         rc = iso7816_read_binary(buf, &len);
778                         if (rc < 0) {
779                                 printf("error reading EF\n");
780                                 break;
781                         }
782                 }
783 #if 0
784                 for (i = 0; i < 4; i++)
785                         iso7816_get_challenge(0xff);
786 #endif
787                 break;
788         case RFID_PROTOCOL_MIFARE_UL:
789                 printf("Protocol Mifare Ultralight\n");
790                 mifare_ulight_read(ph);
791 #if 0
792                 mifare_ulight_blank(ph);
793                 mifare_ulight_write(ph);
794                 mifare_ulight_read(ph);
795 #endif
796                 break;
797         case RFID_PROTOCOL_MIFARE_CLASSIC:
798                 printf("Protocol Mifare Classic\n");
799                 mifare_classic_dump(ph);
800                 break;
801         default:
802                 printf("unknown protocol %u\n", protocol);
803                 exit(1);
804                 break;
805         }
806
807         rfid_reader_close(rh);
808         
809         exit(0);
810 }