remove extra reads of primary status register (Bjoern Riemer)
[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                 printf("Layer2 init ok\n");
460                 rc = rfid_layer2_open(l2h);
461         } else {
462                 printf("error during layer2_open\n");
463                 return ;
464         }
465
466         while (rc>=0) {
467                 if (l2h) {
468                         uid_len = sizeof(uid_buf);
469                         rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf, &uid_len);
470                         printf("Layer 2 success (%s)[%d]: '%s'\n", rfid_layer2_name(l2h), uid_len, hexdump(uid_buf, uid_len));
471                 }
472
473         /*
474                 ph = rfid_protocol_scan(l2h);
475                 if (ph) {
476                         printf("Protocol success (%s)\n", rfid_protocol_name(ph));
477
478                         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
479                                              &size, &size_len) == 0)
480                         printf("Size: %u bytes\n", size);
481                 } else
482                         printf("##############\n");
483                 */
484
485                 if (rc >= 0) {
486                         rfid_layer2_close(l2h);
487                 }
488                 rc = rfid_layer2_open(l2h);
489         }
490 }
491
492 static void do_enum_loop(int layer2, unsigned int delay)
493 {
494         while (1) {
495                 do_enum(layer2);
496                 {
497                         unsigned int opt;
498                         unsigned int optlen = sizeof(opt);
499
500                         /* turn off RF */
501                         opt = 1;
502                         rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
503
504                         usleep(10 * 1000);
505
506                         /* turn on RF */
507                         opt = 0;
508                         rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
509                 }
510                 usleep(delay * 1000);
511                 printf("--- next run ---\n");
512         }
513 }
514
515 #define OPTION_OFFSET 256
516
517 static struct option original_opts[] = {
518         { "help", 0, 0, 'h' },
519         { "layer2", 1, 0, 'l' },
520         { "protocol", 1, 0, 'p' },
521         { "scan", 0, 0, 's' },
522         { "scan-loop", 0, 0, 'S' },
523         { "dump", 0, 0, 'd' },
524         { "enum", 0, 0, 'e' },
525         { "enum-loop", 1, 0, 'E' },
526         {0, 0, 0, 0}
527 };
528
529 /* module / option merging code */
530 static struct option *opts = original_opts;
531 static unsigned int global_option_offset = 0;
532
533 static char *program_name;
534 static char *program_version = LIBRFID_TOOL_VERSION;
535
536 static void free_opts(int reset_offset)
537 {
538         if (opts != original_opts) {
539                 free(opts);
540                 opts = original_opts;
541                 if (reset_offset)
542                         global_option_offset = 0;
543         }
544 }
545
546 static struct option *
547 merge_options(struct option *oldopts, const struct option *newopts,
548               unsigned int *option_offset)
549 {
550         unsigned int num_old, num_new, i;
551         struct option *merge;
552
553         for (num_old = 0; oldopts[num_old].name; num_old++);
554         for (num_new = 0; oldopts[num_new].name; num_new++);
555
556         global_option_offset += OPTION_OFFSET;
557         *option_offset = global_option_offset;
558
559         merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
560         memcpy(merge, oldopts, num_old * sizeof(struct option));
561         free_opts(0); /* Release previous options merged if any */
562         for (i = 0; i < num_new; i++) {
563                 merge[num_old + i] = newopts[i];
564                 merge[num_old + i].val += *option_offset;
565         }
566         memset(merge + num_old + num_new, 0, sizeof(struct option));
567
568         return merge;
569 }
570
571 struct rfidtool_module *find_module(const char *name)
572 {
573         return NULL;
574 }
575
576 void register_module(struct rfidtool_module *me)
577 {
578         struct rfidtool_module *old;
579
580         if (strcmp(me->version, program_version) != 0) {
581                 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
582                         program_name, me->name, me->version, program_version);
583                 exit(1);
584         }
585
586         old = find_module(me->name);
587         if (old) {
588                 fprintf(stderr, "%s: target `%s' already registered.\n",
589                         program_name, me->name);
590                 exit(1);
591         }
592 }
593
594 static void help(void)
595 {
596         printf( " -s    --scan          scan until first RFID tag is found\n"
597                 " -S    --scan-loop     endless scanning loop\n" 
598                 " -p    --protocol      {tcl,mifare-ultralight,mifare-classic,tagit,icode}\n"
599                 " -l    --layer2        {iso14443a,iso14443b,iso15693,icode1}\n"
600                 " -d    --dump          dump rc632 registers\n"
601                 " -e    --enum          enumerate all tag's in field \n"
602                 " -E    --enum-loop     <delay> (ms) enumerate endless\n"
603                 " -h    --help\n");
604 }
605
606 int main(int argc, char **argv)
607 {
608         int rc;
609         char buf[0x100];
610         int i, len, protocol = -1, layer2 = -1;
611
612 #ifdef  __MINGW32__
613         program_name = argv[0];
614 #else /*__MINGW32__*/
615         program_name = basename(argv[0]);
616 #endif/*__MINGW32__*/
617         
618         printf("%s - (C) 2005-2008 by Harald Welte\n"
619                "This program is Free Software and has "
620                "ABSOLUTELY NO WARRANTY\n\n", program_name);
621
622         printf("initializing librfid\n");
623         rfid_init();
624
625         while (1) {
626                 int c, option_index = 0;
627                 c = getopt_long(argc, argv, "hp:l:sSdeE:", opts, &option_index);
628                 if (c == -1)
629                         break;
630
631                 switch (c) {
632                 case 'E':
633                         i = strtol(optarg, NULL, 10);
634
635                         if (reader_init() < 0)
636                                 exit(1);
637                         if (layer2<0)
638                                 layer2 = RFID_LAYER2_ISO14443A;
639
640                         do_enum_loop(layer2, i>1? i : 500);
641                         rfid_reader_close(rh);
642                         exit(0);
643                         break;
644                 case 'e':
645                         if (reader_init() < 0)
646                                 exit(1);
647                         if (layer2 < 0)
648                                 layer2 = RFID_LAYER2_ISO14443A;
649                         do_enum(layer2);
650                         rfid_reader_close(rh);
651                         exit(0);
652                         break;
653                 case 'd':
654                         if (reader_init() < 0)
655                                 exit(1);
656                         do_regdump();
657                         rfid_reader_close(rh);
658                         break;
659                 case 's':
660                         if (reader_init() < 0)
661                                 exit(1);
662                         do_scan(0);
663                         rfid_reader_close(rh);
664                         exit(0);
665                         break;
666                 case 'S':
667                         if (reader_init() < 0)
668                                 exit(1);
669                         do_endless_scan();
670                         exit(0);
671                         break;
672                 case 'p':
673                         protocol = proto_by_name(optarg);
674                         if (protocol < 0) {
675                                 fprintf(stderr, "unknown protocol `%s'\n", 
676                                         optarg);
677                                 exit(2);
678                         }
679                         break;
680                 case 'l':
681                         layer2 = l2_by_name(optarg);
682                         if (layer2 < 0) {
683                                 fprintf(stderr, "unknown layer2 `%s'\n",
684                                         optarg);
685                                 exit(2);
686                         }
687                         break;
688                 default:
689                         printf("unknown cmd: %c\n",c);
690                 case 'h':
691                         help();
692                         exit(0);
693                         break;
694                 case '?':
695                         exit(0);
696                 }
697         }
698
699         switch (protocol) {
700         case RFID_PROTOCOL_MIFARE_UL:
701         case RFID_PROTOCOL_MIFARE_CLASSIC:
702                 layer2 = RFID_LAYER2_ISO14443A;
703                 break;
704         case -1:
705                 fprintf(stderr, "you have to specify --protocol\n");
706                 exit(2);
707         }
708
709         if (layer2 < 0) {
710                 fprintf(stderr, "you have to specify --layer2\n");
711                 exit(2);
712         }
713         
714         if (reader_init() < 0)
715                 exit(1);
716
717
718         if (l2_init(layer2) < 0) {
719                 rfid_reader_close(rh);
720                 exit(1);
721         }
722
723         if (l3_init(protocol) < 0) {
724                 rfid_reader_close(rh);
725                 exit(1);
726         }
727
728         switch (protocol) {
729
730         case RFID_PROTOCOL_TCL:
731                 printf("Protocol T=CL\n");
732                 /* we've established T=CL at this point */
733                 printf("selecting Master File\n");
734                 rc = select_mf();
735                 if (rc < 0) {
736                         printf("error selecting MF\n");
737                         break;
738                 }
739
740                 printf("Getting random challenge, length 255\n");
741                 rc = iso7816_get_challenge(0xff);
742                 if (rc < 0) {
743                         printf("error getting random challenge\n");
744                         break;
745                 }
746
747                 printf("selecting Passport application\n");
748                 rc = iso7816_select_application();
749                 if (rc < 0) {
750                         printf("error selecting passport application\n");
751                         break;
752                 }
753
754                 printf("selecting EF 0x1e\n");
755                 rc = iso7816_select_ef(0x011e);
756                 if (rc < 0) {
757                         printf("error selecting EF 0x1e\n");
758                         break;
759                 }
760
761                 printf("selecting EF 0x01\n");
762                 rc = iso7816_select_ef(0x0101);
763                 if (rc < 0) {
764                         printf("error selecting EF 0x01\n");
765                         break;
766                 }
767
768                 while (1) {
769                         printf("reading EF1\n");
770                         len = sizeof(buf);
771                         printf("reading ef\n");
772                         rc = iso7816_read_binary(buf, &len);
773                         if (rc < 0) {
774                                 printf("error reading EF\n");
775                                 break;
776                         }
777                 }
778 #if 0
779                 for (i = 0; i < 4; i++)
780                         iso7816_get_challenge(0xff);
781 #endif
782                 break;
783         case RFID_PROTOCOL_MIFARE_UL:
784                 printf("Protocol Mifare Ultralight\n");
785                 mifare_ulight_read(ph);
786 #if 0
787                 mifare_ulight_blank(ph);
788                 mifare_ulight_write(ph);
789                 mifare_ulight_read(ph);
790 #endif
791                 break;
792         case RFID_PROTOCOL_MIFARE_CLASSIC:
793                 printf("Protocol Mifare Classic\n");
794                 mifare_classic_dump(ph);
795                 break;
796         default:
797                 printf("unknown protocol %u\n", protocol);
798                 exit(1);
799                 break;
800         }
801
802         rfid_reader_close(rh);
803         
804         exit(0);
805 }