summaryrefslogtreecommitdiff
path: root/src/rfid_proto_mifare_ul.c
blob: 0ea55d2671881a562ec0211077a8dc8e51f04d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

/* Mifare Ultralight implementation, PCD side.
 *
 * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
 *
 */

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <librfid/rfid.h>
#include <librfid/rfid_protocol.h>
#include <librfid/rfid_layer2.h>
#include <librfid/rfid_protocol_mifare_ul.h>

#include "rfid_iso14443_common.h"


/* FIXME */
#define MIFARE_UL_READ_FWT	100
#define MIFARE_UL_WRITE_FWT	100

static int
mful_read(struct rfid_protocol_handle *ph, unsigned int page,
	  unsigned char *rx_data, unsigned int *rx_len)
{
	unsigned char rx_buf[16];
	unsigned int real_rx_len = sizeof(rx_buf);
	unsigned char tx[2];
	int ret;

	if (page > MIFARE_UL_PAGE_MAX)
		return -EINVAL;

	tx[0] = MIFARE_UL_CMD_READ;
	tx[1] = page & 0xff;

	ret = rfid_layer2_transceive(ph->l2h, RFID_14443A_FRAME_REGULAR,
				     tx, sizeof(tx), rx_buf, 
				     &real_rx_len, MIFARE_UL_READ_FWT, 0);

	if (ret < 0)
		return ret;

	if (real_rx_len < *rx_len)
		*rx_len = real_rx_len;

	memcpy(rx_data, rx_buf, *rx_len);

	return ret;
}

static int
mful_write(struct rfid_protocol_handle *ph, unsigned int page,
	   unsigned char *tx_data, unsigned int tx_len)
{
	unsigned int i;
	unsigned char tx[6];
	unsigned char rx[10];
	unsigned int rx_len = sizeof(rx);
	int ret;

	if (tx_len != 4 || page > MIFARE_UL_PAGE_MAX)
		return -EINVAL;

	tx[0] = MIFARE_UL_CMD_WRITE;
	tx[1] = page & 0xff;

	for (i = 0; i < 4; i++)
		tx[2+i] = tx_data[i];

	ret = rfid_layer2_transceive(ph->l2h, RFID_14443A_FRAME_REGULAR,
				     tx, sizeof(tx), rx, &rx_len, 
				     MIFARE_UL_WRITE_FWT, 0);
					
	if (ret < 0)
		return ret;

	if (rx[0] != MIFARE_UL_RESP_ACK)
		return -EIO;

	return ret;
}

static int
mful_transceive(struct rfid_protocol_handle *ph,
		const unsigned char *tx_data, unsigned int tx_len,
		unsigned char *rx_data, unsigned int *rx_len,
		unsigned int timeout, unsigned int flags)
{
	return -EINVAL;
}

static struct rfid_protocol_handle *
mful_init(struct rfid_layer2_handle *l2h)
{
	struct rfid_protocol_handle *ph;
	ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
	return ph;
}

static int mful_fini(struct rfid_protocol_handle *ph)
{
	free_protocol_handle(ph);
	return 0;
}

const struct rfid_protocol rfid_protocol_mful = {
	.id	= RFID_PROTOCOL_MIFARE_UL,
	.name	= "Mifare Ultralight",
	.fn	= {
		.init 		= &mful_init,
		.read		= &mful_read,
		.write 		= &mful_write,
		.fini		= &mful_fini,
	},
};

/* Functions below are not (yet? covered in the generic librfid api */


/* lock a certain page */
int rfid_mful_lock_page(struct rfid_protocol_handle *ph, unsigned int page)
{
	unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };

	if (ph->proto != &rfid_protocol_mful)
		return -EINVAL;

	if (page < 3 || page > 15)
		return -EINVAL;

	if (page > 8)
		buf[2] = (1 << page);
	else
		buf[3] = (1 << (page - 8));

	return mful_write(ph, MIFARE_UL_PAGE_LOCK, buf, sizeof(buf));
}

/* convenience wrapper to lock the otp page */
int rfid_mful_lock_otp(struct rfid_protocol_handle *ph)
{
	return rfid_mful_lock_page(ph, MIFARE_UL_PAGE_OTP);
}
personal git repositories of Harald Welte. Your mileage may vary