1 /* librfid-tool - a small command-line tool for librfid testing
3 * (C) 2005-2008 by Harald Welte <laforge@gnumonks.org>
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
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.
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
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>
38 #include <librfid/rfid_layer2_iso14443a.h>
39 #include <librfid/rfid_layer2_iso15693.h>
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>
46 #include "librfid-tool.h"
49 static int select_mf(void)
51 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
52 unsigned char ret[256];
53 unsigned int rlen = sizeof(ret);
57 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
61 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
67 static int iso7816_get_challenge(unsigned char len)
69 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
70 unsigned char ret[256];
71 unsigned int rlen = sizeof(ret);
77 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
81 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
87 iso7816_select_application(void)
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);
96 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
100 /* FIXME: parse response */
101 printf("%s\n", hexdump(resp, rlen));
107 iso7816_select_ef(u_int16_t fid)
109 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
110 unsigned char resp[7];
111 unsigned int rlen = sizeof(resp);
115 cmd[5] = (fid >> 8) & 0xff;
118 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
122 /* FIXME: parse response */
123 printf("%s\n", hexdump(resp, rlen));
129 iso7816_read_binary(unsigned char *buf, unsigned int *len)
131 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
132 unsigned char resp[256];
133 unsigned int rlen = sizeof(resp);
137 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
141 printf("%s\n", hexdump(resp, rlen));
143 /* FIXME: parse response, determine whether we need additional reads */
145 /* FIXME: copy 'len' number of response bytes to 'buf' */
149 /* wrapper function around SELECT EF and READ BINARY */
151 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
155 rv = iso7816_select_ef(fid);
159 return iso7816_read_binary(buf, len);
162 /* mifare ultralight helpers */
164 mifare_ulight_write(struct rfid_protocol_handle *ph)
166 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
168 return rfid_protocol_write(ph, 10, buf, 4);
172 mifare_ulight_blank(struct rfid_protocol_handle *ph)
174 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
177 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
178 ret = rfid_protocol_write(ph, i, buf, 4);
186 mifare_ulight_read(struct rfid_protocol_handle *ph)
188 unsigned char buf[20];
189 unsigned int len = sizeof(buf);
193 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
194 ret = rfid_protocol_read(ph, i, buf, &len);
198 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
203 /* mifare classic helpers */
205 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
207 unsigned char buf[20];
208 unsigned int len = sizeof(buf);
210 int block, blocks_per_sector, first_block;
212 printf("Reading sector %u\n", sector);
214 first_block = mfcl_sector2block(sector);
215 blocks_per_sector = mfcl_sector_blocks(sector);
217 if (first_block < 0 || blocks_per_sector < 0)
220 for (block = first_block; block < first_block + blocks_per_sector;
222 printf("Reading block %u: ", block);
223 ret = rfid_protocol_read(ph, block, buf, &len);
224 if (ret == -ETIMEDOUT)
225 fprintf(stderr, "TIMEOUT\n");
227 printf("Error %d reading\n", ret);
231 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
237 mifare_classic_dump(struct rfid_protocol_handle *ph)
240 unsigned int size_len = sizeof(size);
241 int sector, num_sectors;
243 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
244 &size, &size_len) == 0) {
245 printf("Size: %u bytes\n", size);
247 printf("Size: unknown ?!?\n");
265 for (sector = 0; sector < num_sectors; sector++) {
268 printf("Authenticating sector %u: ", sector);
271 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
273 printf("key format error\n");
277 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A,
278 mfcl_sector2block(sector));
280 printf("mifare auth error\n");
283 printf("mifare auth succeeded!\n");
285 mifare_classic_read_sector(ph, sector);
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",
297 static int proto_by_name(const char *name)
301 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
302 if (proto_names[i] == NULL)
304 if (!strcasecmp(name, proto_names[i]))
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",
317 static int l2_by_name(const char *name)
321 for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
322 if (l2_names[i] == NULL)
324 if (!strcasecmp(name, l2_names[i]))
330 static int do_scan(int first)
334 unsigned int size_len = sizeof(size);
337 rh->reader->rf_power(rh, 0);
339 rh->reader->rf_power(rh, 1);
341 printf("scanning for RFID token...\n");
342 rc = rfid_scan(rh, &l2h, &ph);
344 unsigned char uid_buf[16];
345 unsigned int uid_len = sizeof(uid_buf);
346 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
348 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
349 hexdump(uid_buf, uid_len));
352 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
354 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
355 &size, &size_len) == 0)
356 printf("Size: %u bytes\n", size);
362 static void do_endless_scan()
370 printf("==> doing %s scan\n", first ? "first" : "successive");
373 printf("closing proto\n");
374 rfid_protocol_close(ph);
377 printf("closing layer2\n");
378 rfid_layer2_close(l2h);
385 static void do_regdump(void)
387 u_int8_t buffer[0xff];
390 printf("dumping rc632 regs...\n");
392 rc632_register_dump(rh->ah, buffer);
395 for (i=0; i<=0x0f; i++)
396 printf(" 0x_%01X",i);
397 printf("\n-----------------------------------------------------------------------------------\n");
399 for (i=0; i <= 0x3f; i++) {
401 printf("0x%01X_:",i/0x10);
402 printf(" 0x%02X", buffer[i]);
403 if ((i% 0x10) == 0x0f)
407 /* print regdump as c-style array*/
408 printf("u_int8_t rc632_regs[] = {");
409 for (i = 0; i <= 0x3f; i++) {
410 if (((i+1) % 0x08) == 1) {
412 printf("//%2d..%2d",i-8,i-1);
415 printf(" 0x%02X, ",buffer[i]);
417 printf("//%2d..%2d\n\t 0 };\n",i-8,i-1);
421 static void do_enum(int layer2)
425 //unsigned int size_len = sizeof(size);
426 unsigned char uid_buf[16];
427 unsigned int uid_len;
429 printf("scanning for RFID token on layer %s...\n", l2_names[layer2]);
431 if (rh->reader->l2_supported & (1 << layer2)) {
432 l2h = rfid_layer2_init(rh, layer2);
433 rc = rfid_layer2_open(l2h);
435 printf("error during layer2_open\n");
441 uid_len = sizeof(uid_buf);
442 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf, &uid_len);
443 printf("Layer 2 success (%s)[%d]: %s\n", rfid_layer2_name(l2h), uid_len, hexdump(uid_buf, uid_len));
447 ph = rfid_protocol_scan(l2h);
449 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
451 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
452 &size, &size_len) == 0)
453 printf("Size: %u bytes\n", size);
455 printf("##############\n");
459 rfid_layer2_close(l2h);
461 rc = rfid_layer2_open(l2h);
465 #define OPTION_OFFSET 256
467 static struct option original_opts[] = {
468 { "help", 0, 0, 'h' },
469 { "layer2", 1, 0, 'l' },
470 { "protocol", 1, 0, 'p' },
471 { "scan", 0, 0, 's' },
472 { "scan-loop", 0, 0, 'S' },
473 { "dump", 0, 0, 'd' },
474 { "enum", 0, 0, 'e' },
478 /* module / option merging code */
479 static struct option *opts = original_opts;
480 static unsigned int global_option_offset = 0;
482 static char *program_name;
483 static char *program_version = LIBRFID_TOOL_VERSION;
485 static void free_opts(int reset_offset)
487 if (opts != original_opts) {
489 opts = original_opts;
491 global_option_offset = 0;
495 static struct option *
496 merge_options(struct option *oldopts, const struct option *newopts,
497 unsigned int *option_offset)
499 unsigned int num_old, num_new, i;
500 struct option *merge;
502 for (num_old = 0; oldopts[num_old].name; num_old++);
503 for (num_new = 0; oldopts[num_new].name; num_new++);
505 global_option_offset += OPTION_OFFSET;
506 *option_offset = global_option_offset;
508 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
509 memcpy(merge, oldopts, num_old * sizeof(struct option));
510 free_opts(0); /* Release previous options merged if any */
511 for (i = 0; i < num_new; i++) {
512 merge[num_old + i] = newopts[i];
513 merge[num_old + i].val += *option_offset;
515 memset(merge + num_old + num_new, 0, sizeof(struct option));
520 struct rfidtool_module *find_module(const char *name)
525 void register_module(struct rfidtool_module *me)
527 struct rfidtool_module *old;
529 if (strcmp(me->version, program_version) != 0) {
530 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
531 program_name, me->name, me->version, program_version);
535 old = find_module(me->name);
537 fprintf(stderr, "%s: target `%s' already registered.\n",
538 program_name, me->name);
543 static void help(void)
545 printf( " -s --scan scan until first RFID tag is found\n"
546 " -S --scan-loop endless scanning loop\n"
547 " -p --protocol {tcl,mifare-ultralight,mifare-classic,tagit}\n"
548 " -l --layer2 {iso14443a,iso14443b,iso15693}\n"
549 " -d --dump dump rc632 registers\n"
550 " -e --enum enumerate all tag's in field (iso14443a)\n"
554 int main(int argc, char **argv)
558 int i, len, protocol = -1, layer2 = -1;
561 program_name = argv[0];
562 #else /*__MINGW32__*/
563 program_name = basename(argv[0]);
564 #endif/*__MINGW32__*/
566 printf("%s - (C) 2005-2008 by Harald Welte\n"
567 "This program is Free Software and has "
568 "ABSOLUTELY NO WARRANTY\n\n", program_name);
570 printf("initializing librfid\n");
574 int c, option_index = 0;
575 c = getopt_long(argc, argv, "hp:l:sSde", opts, &option_index);
581 if (reader_init() < 0)
583 layer2 = RFID_LAYER2_ISO14443A;
588 if (reader_init() < 0)
593 if (reader_init() < 0)
599 if (reader_init() < 0)
605 protocol = proto_by_name(optarg);
607 fprintf(stderr, "unknown protocol `%s'\n",
613 layer2 = l2_by_name(optarg);
615 fprintf(stderr, "unknown layer2 `%s'\n",
628 case RFID_PROTOCOL_MIFARE_UL:
629 case RFID_PROTOCOL_MIFARE_CLASSIC:
630 layer2 = RFID_LAYER2_ISO14443A;
633 fprintf(stderr, "you have to specify --protocol\n");
638 fprintf(stderr, "you have to specify --layer2\n");
642 if (reader_init() < 0)
646 if (l2_init(layer2) < 0) {
647 rfid_reader_close(rh);
651 if (l3_init(protocol) < 0) {
652 rfid_reader_close(rh);
658 case RFID_PROTOCOL_TCL:
659 printf("Protocol T=CL\n");
660 /* we've established T=CL at this point */
661 printf("selecting Master File\n");
664 printf("error selecting MF\n");
668 printf("Getting random challenge, length 255\n");
669 rc = iso7816_get_challenge(0xff);
671 printf("error getting random challenge\n");
675 printf("selecting Passport application\n");
676 rc = iso7816_select_application();
678 printf("error selecting passport application\n");
682 printf("selecting EF 0x1e\n");
683 rc = iso7816_select_ef(0x011e);
685 printf("error selecting EF 0x1e\n");
689 printf("selecting EF 0x01\n");
690 rc = iso7816_select_ef(0x0101);
692 printf("error selecting EF 0x01\n");
697 printf("reading EF1\n");
699 printf("reading ef\n");
700 rc = iso7816_read_binary(buf, &len);
702 printf("error reading EF\n");
707 for (i = 0; i < 4; i++)
708 iso7816_get_challenge(0xff);
711 case RFID_PROTOCOL_MIFARE_UL:
712 printf("Protocol Mifare Ultralight\n");
713 mifare_ulight_read(ph);
715 mifare_ulight_blank(ph);
716 mifare_ulight_write(ph);
717 mifare_ulight_read(ph);
720 case RFID_PROTOCOL_MIFARE_CLASSIC:
721 printf("Protocol Mifare Classic\n");
722 mifare_classic_dump(ph);
725 printf("unknown protocol %u\n", protocol);
730 rfid_reader_close(rh);