- add size detection for mifare classic / ultralight
[librfid] / utils / mifare-tool.c
1 /* mifare-tool - a small command-line tool for librfid mifare testing
2  *
3  * (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <libgen.h>
25
26 #define _GNU_SOURCE
27 #include <getopt.h>
28
29 #include <librfid/rfid.h>
30 #include <librfid/rfid_scan.h>
31 #include <librfid/rfid_reader.h>
32 #include <librfid/rfid_layer2.h>
33 #include <librfid/rfid_protocol.h>
34
35 #include <librfid/rfid_protocol_mifare_classic.h>
36 #include <librfid/rfid_protocol_mifare_ul.h>
37
38 #include <librfid/rfid_access_mifare_classic.h>
39
40 #include "librfid-tool.h"
41
42 static char *program_name;
43
44 static void help(void)
45 {
46 }
47
48 static struct option mifare_opts[] = {
49         { "key", 1, 0, 'k' },
50         { "read", 1, 0, 'r' },
51         { "loop-read", 1, 0, 'l' },
52         { "write", 1 ,0, 'w' },
53         { "help", 0, 0, 'h' },
54         { 0, 0, 0, 0 }
55 };
56
57 static int mifare_cl_auth(unsigned char *key, int page)
58 {
59         int rc;
60
61         rc = mfcl_set_key(ph, key);
62         if (rc < 0) {
63                 fprintf(stderr, "key format error\n");
64                 return rc;
65         }
66         rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, page);
67         if (rc < 0) {
68                 fprintf(stderr, "mifare auth error\n");
69                 return rc;
70         } else 
71                 printf("mifare auth succeeded!\n");
72         
73         return 0;
74 }
75
76 static void mifare_l3(void)
77 {
78         while (l2_init(RFID_LAYER2_ISO14443A) < 0) ;
79
80         printf("ISO14443-3A anticollision succeeded\n");
81
82         while (l3_init(RFID_PROTOCOL_MIFARE_CLASSIC) < 0) ;
83
84         printf("Mifare card available\n");
85 }
86
87 int main(int argc, char **argv)
88 {
89         int len, rc, c, option_index = 0;
90         unsigned int page;
91         char key[MIFARE_CL_KEY_LEN];
92         char buf[MIFARE_CL_PAGE_SIZE];
93         program_name = basename(argv[0]);
94
95         memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON,
96                sizeof(MIFARE_CL_KEYA_DEFAULT_INFINEON));
97
98         printf("%s - (C) 2006 by Harald Welte\n"
99                "This program is Free Software and has "
100                "ABSOLUTELY NO WARRANTY\n\n", program_name);
101
102         printf("initializing librfid\n");
103         rfid_init();
104
105         if (reader_init() < 0) {
106                 fprintf(stderr, "error opening reader\n");
107                 exit(1);
108         }
109
110         while (1) {
111                 c = getopt_long(argc, argv, "k:r:l:w:", mifare_opts,
112                                 &option_index);
113                 if (c == -1)
114                         break;
115
116                 switch (c) {
117                 case 'k':
118                         hexread(key, optarg, strlen(optarg));
119                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
120                         break;
121                 case 'r':
122                         page = atoi(optarg);
123                         printf("read(key='%s',page=%u):",
124                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
125                         len = MIFARE_CL_PAGE_SIZE;
126                         mifare_l3();
127                         if (mifare_cl_auth(key, page) < 0)
128                                 exit(1);
129                         rc = rfid_protocol_read(ph, page, buf, &len);
130                         if (rc < 0) {
131                                 printf("\n");
132                                 fprintf(stderr, "error during read\n");
133                                 break;
134                         }
135                         printf("%s\n", hexdump(buf, len));
136
137                         if (page & 0x3 == 0x3) {
138                                 struct mfcl_access_sect s;
139                                 struct mfcl_access_exp_sect es;
140                                 int b;
141                                 u_int8_t recreated[4];
142                                 mfcl_parse_access(&s, buf+6);
143                                 printf("access b0:%u b1:%u b2:%u b3:%u\n",
144                                         s.block[0], s.block[1],
145                                         s.block[2], s.block[3]);
146                                 mfcl_access_to_exp(&es, &s);
147                                 for (b = 0; b < 3; b++)
148                                         printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
149                                 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
150 #if 0
151                                 mfcl_compile_access(recreated, &s);
152                                 printf("recreated; %s\n", hexdump(recreated,4));
153 #endif
154                         }
155                         break;
156                 case 'l':
157                         page = atoi(optarg);
158                         printf("read_loop(key='%s',page=%u):\n",
159                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
160                         while (1) {
161                                 mifare_l3();
162                                 if (mifare_cl_auth(key, page) < 0)
163                                         continue;
164                                 rc = rfid_protocol_read(ph, page, buf, &len);
165                                 if (rc < 0) {
166                                         printf("\n");
167                                         fprintf(stderr, "error during read\n");
168                                         continue;
169                                 }
170                                 printf("%s\n", hexdump(buf, len));
171                         }
172                         break;
173                 case 'w':
174                         page = atoi(optarg);
175                         len = strlen(argv[optind]);
176                         len = hexread(buf, argv[optind], len);
177                         printf("write(key='%s',page=%u):",
178                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
179                         printf(" '%s'(%u):", hexdump(buf, len), len);
180                         mifare_l3();
181                         if (mifare_cl_auth(key, page) < 0)
182                                 exit(1);
183                         rc = rfid_protocol_write(ph, page, buf, len); 
184                         if (rc < 0) {
185                                 printf("\n");
186                                 fprintf(stderr, "error during write\n");
187                                 break;
188                         }
189                         printf("success\n");
190                         break;
191                 case 'h':
192                 default:
193                         help();
194                 }
195         }
196
197 #if 0
198         rfid_protocol_close(ph);
199         rfid_protocol_fini(ph);
200
201         rfid_layer2_close(l2h);
202         rfid_layer2_fini(l2h);
203 #endif
204         rfid_reader_close(rh);
205         exit(0);
206 }
207