1 /******************************************************************************
2 *
3 * Copyright (C) 2010-2014 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains the common data types shared by Reader/Writer mode
22 * and Card Emulation.
23 *
24 ******************************************************************************/
25 #include "bt_types.h"
26 #include "nfc_target.h"
27 #include "rw_api.h"
28 #include "rw_int.h"
29
30 #define T1T_MAX_NUM_OPCODES 9
31 #define T1T_STATIC_OPCODES 5
32 #define T1T_MAX_TAG_MODELS 2
33
34 const tT1T_CMD_RSP_INFO t1t_cmd_rsp_infos[] = {
35 /* Note: the order of these commands can not be changed.
36 * If new events are added, add them after T1T_CMD_WRITE_NE8 */
37 /* opcode cmd_len, uid_offset, rsp_len */
38 {T1T_CMD_RID, 7, 3, 6}, {T1T_CMD_RALL, 7, 3, 122},
39 {T1T_CMD_READ, 7, 3, 2}, {T1T_CMD_WRITE_E, 7, 3, 2},
40 {T1T_CMD_WRITE_NE, 7, 3, 2}, {T1T_CMD_RSEG, 14, 10, 129},
41 {T1T_CMD_READ8, 14, 10, 9}, {T1T_CMD_WRITE_E8, 14, 10, 9},
42 {T1T_CMD_WRITE_NE8, 14, 10, 9}};
43
44 const tT1T_INIT_TAG t1t_init_content[] = {
45 /* Tag Name CC3, is dynamic, ltv[0] ltv[1] ltv[2]
46 mtv[0] mtv[1] mtv[2]*/
47 {RW_T1T_IS_TOPAZ96, 0x0E, FALSE, {0, 0, 0}, {0, 0, 0}},
48 {RW_T1T_IS_TOPAZ512, 0x3F, TRUE, {0xF2, 0x30, 0x33}, {0xF0, 0x02, 0x03}}};
49
50 #define T2T_MAX_NUM_OPCODES 3
51 #define T2T_MAX_TAG_MODELS 7
52
53 const tT2T_CMD_RSP_INFO t2t_cmd_rsp_infos[] = {
54 /* Note: the order of these commands can not be changed.
55 * If new events are added, add them after T2T_CMD_SEC_SEL */
56 /* opcode cmd_len, rsp_len, nack_rsp_len */
57 {T2T_CMD_READ, 2, 16, 1},
58 {T2T_CMD_WRITE, 6, 1, 1},
59 {T2T_CMD_SEC_SEL, 2, 1, 1}};
60
61 const tT2T_INIT_TAG t2t_init_content[] = {
62 /* Tag Name is_multi_v Ver Block Ver No
63 Vbitmask to_calc_cc CC3 OTP BLPB */
64 {TAG_MIFARE_MID, true, T2T_MIFARE_VERSION_BLOCK,
65 T2T_MIFARE_ULTRALIGHT_VER_NO, 0xFFFF, false, 0x06, false,
66 T2T_DEFAULT_LOCK_BLPB},
67 {TAG_MIFARE_MID, true, T2T_MIFARE_VERSION_BLOCK,
68 T2T_MIFARE_ULTRALIGHT_FAMILY_VER_NO, 0xFFFF, true, 0x00, false,
69 T2T_DEFAULT_LOCK_BLPB},
70 {TAG_KOVIO_MID, false, 0x00, 0x00, 0x0000, false, 0x1D, true, 0x04},
71 {TAG_INFINEON_MID, true, T2T_INFINEON_VERSION_BLOCK,
72 T2T_INFINEON_MYD_MOVE_LEAN, 0xFFF0, false, 0x06, false,
73 T2T_DEFAULT_LOCK_BLPB},
74 {TAG_INFINEON_MID, true, T2T_INFINEON_VERSION_BLOCK, T2T_INFINEON_MYD_MOVE,
75 0xFFF0, false, 0x10, false, T2T_DEFAULT_LOCK_BLPB},
76 {TAG_BRCM_MID, true, T2T_BRCM_VERSION_BLOCK, T2T_BRCM_STATIC_MEM, 0xFFFF,
77 false, 0x06, false, T2T_DEFAULT_LOCK_BLPB},
78 {TAG_BRCM_MID, true, T2T_BRCM_VERSION_BLOCK, T2T_BRCM_DYNAMIC_MEM, 0xFFFF,
79 false, 0x3C, false, T2T_DEFAULT_LOCK_BLPB}
80
81 };
82
83 const uint8_t t4t_v10_ndef_tag_aid[T4T_V10_NDEF_TAG_AID_LEN] = {
84 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00};
85 const uint8_t t4t_v20_ndef_tag_aid[T4T_V20_NDEF_TAG_AID_LEN] = {
86 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
87
88 const char* const t1t_cmd_str[] = {
89 "T1T_RID", "T1T_RALL", "T1T_READ", "T1T_WRITE_E", "T1T_WRITE_NE",
90 "T1T_RSEG", "T1T_READ8", "T1T_WRITE_E8", "T1T_WRITE_NE8"};
91
92 const char* const t2t_cmd_str[] = {"T2T_CMD_READ", "T2T_CMD_WRITE",
93 "T2T_CMD_SEC_SEL"};
94
95 static unsigned int tags_ones32(unsigned int x);
96
97 /*******************************************************************************
98 **
99 ** Function t1t_cmd_to_rsp_info
100 **
101 ** Description This function maps the given opcode to tT1T_CMD_RSP_INFO.
102 **
103 ** Returns tNFC_STATUS
104 **
105 *******************************************************************************/
t1t_cmd_to_rsp_info(uint8_t opcode)106 const tT1T_CMD_RSP_INFO* t1t_cmd_to_rsp_info(uint8_t opcode) {
107 const tT1T_CMD_RSP_INFO *p_ret = nullptr, *p;
108 int xx;
109
110 for (xx = 0, p = &t1t_cmd_rsp_infos[0]; xx < T1T_MAX_NUM_OPCODES; xx++, p++) {
111 if (opcode == p->opcode) {
112 if ((xx < T1T_STATIC_OPCODES) || (rw_cb.tcb.t1t.hr[0] != T1T_STATIC_HR0))
113 p_ret = p;
114 break;
115 }
116 }
117
118 return p_ret;
119 }
120
121 /*******************************************************************************
122 **
123 ** Function t1t_tag_init_data
124 **
125 ** Description This function maps the given opcode to tT1T_INIT_TAG.
126 **
127 ** Returns tNFC_STATUS
128 **
129 *******************************************************************************/
t1t_tag_init_data(uint8_t tag_model)130 const tT1T_INIT_TAG* t1t_tag_init_data(uint8_t tag_model) {
131 const tT1T_INIT_TAG *p_ret = nullptr, *p;
132 int xx;
133
134 for (xx = 0, p = &t1t_init_content[0]; xx < T1T_MAX_TAG_MODELS; xx++, p++) {
135 if (tag_model == p->tag_model) {
136 p_ret = p;
137 break;
138 }
139 }
140
141 return p_ret;
142 }
143
144 /*******************************************************************************
145 **
146 ** Function t2t_tag_init_data
147 **
148 ** Description This function maps the given manufacturer id and version to
149 ** tT2T_INIT_TAG.
150 **
151 ** Returns tNFC_STATUS
152 **
153 *******************************************************************************/
t2t_tag_init_data(uint8_t manufacturer_id,bool b_valid_ver,uint16_t version_no)154 const tT2T_INIT_TAG* t2t_tag_init_data(uint8_t manufacturer_id,
155 bool b_valid_ver, uint16_t version_no) {
156 const tT2T_INIT_TAG *p_ret = nullptr, *p;
157 int xx;
158
159 for (xx = 0, p = &t2t_init_content[0]; xx < T2T_MAX_TAG_MODELS; xx++, p++) {
160 if (manufacturer_id == p->manufacturer_id) {
161 if ((!p->b_multi_version) || (!b_valid_ver) ||
162 (p->version_no == (version_no & p->version_bmask))) {
163 p_ret = p;
164 break;
165 }
166 }
167 }
168
169 return p_ret;
170 }
171
172 /*******************************************************************************
173 **
174 ** Function t2t_cmd_to_rsp_info
175 **
176 ** Description This function maps the given opcode to tT2T_CMD_RSP_INFO.
177 **
178 ** Returns tNFC_STATUS
179 **
180 *******************************************************************************/
t2t_cmd_to_rsp_info(uint8_t opcode)181 const tT2T_CMD_RSP_INFO* t2t_cmd_to_rsp_info(uint8_t opcode) {
182 const tT2T_CMD_RSP_INFO *p_ret = nullptr, *p;
183 int xx;
184
185 for (xx = 0, p = &t2t_cmd_rsp_infos[0]; xx < T2T_MAX_NUM_OPCODES; xx++, p++) {
186 if (opcode == p->opcode) {
187 p_ret = p;
188 break;
189 }
190 }
191
192 return p_ret;
193 }
194
195 /*******************************************************************************
196 **
197 ** Function t1t_info_to_evt
198 **
199 ** Description This function maps the given tT1T_CMD_RSP_INFO to RW/CE
200 ** event code
201 **
202 ** Returns RW/CE event code
203 **
204 *******************************************************************************/
t1t_info_to_evt(const tT1T_CMD_RSP_INFO * p_info)205 uint8_t t1t_info_to_evt(const tT1T_CMD_RSP_INFO* p_info) {
206 return ((uint8_t)(p_info - t1t_cmd_rsp_infos) + RW_T1T_FIRST_EVT);
207 }
208
209 /*******************************************************************************
210 **
211 ** Function t2t_info_to_evt
212 **
213 ** Description This function maps the given tT2T_CMD_RSP_INFO to RW/CE
214 ** event code
215 **
216 ** Returns RW/CE event code
217 **
218 *******************************************************************************/
t2t_info_to_evt(const tT2T_CMD_RSP_INFO * p_info)219 uint8_t t2t_info_to_evt(const tT2T_CMD_RSP_INFO* p_info) {
220 return ((uint8_t)(p_info - t2t_cmd_rsp_infos) + RW_T2T_FIRST_EVT);
221 }
222
223 /*******************************************************************************
224 **
225 ** Function t1t_info_to_str
226 **
227 ** Description This function maps the given tT1T_CMD_RSP_INFO to T1T cmd
228 ** str
229 **
230 ** Returns T1T cmd str
231 **
232 *******************************************************************************/
t1t_info_to_str(const tT1T_CMD_RSP_INFO * p_info)233 const char* t1t_info_to_str(const tT1T_CMD_RSP_INFO* p_info) {
234 int ind = (int)(p_info - t1t_cmd_rsp_infos);
235 if (ind < T1T_MAX_NUM_OPCODES)
236 return (const char*)t1t_cmd_str[ind];
237 else
238 return "";
239 }
240
241 /*******************************************************************************
242 **
243 ** Function t2t_info_to_str
244 **
245 ** Description This function maps the given tT2T_CMD_RSP_INFO to T2T cmd
246 ** str
247 **
248 ** Returns T2T cmd str
249 **
250 *******************************************************************************/
t2t_info_to_str(const tT2T_CMD_RSP_INFO * p_info)251 const char* t2t_info_to_str(const tT2T_CMD_RSP_INFO* p_info) {
252 int ind = (int)(p_info - t2t_cmd_rsp_infos);
253 if (ind < T2T_MAX_NUM_OPCODES)
254 return (const char*)t2t_cmd_str[ind];
255 else
256 return "";
257 }
258
259 /*******************************************************************************
260 **
261 ** Function tags_pow
262 **
263 ** Description This function calculates x(base) power of y.
264 **
265 ** Returns int
266 **
267 *******************************************************************************/
tags_pow(int x,int y)268 int tags_pow(int x, int y) {
269 int i, ret = 1;
270 for (i = 0; i < y; i++) {
271 ret *= x;
272 }
273 return ret;
274 }
275
276 /*******************************************************************************
277 **
278 ** Function ones32
279 **
280 ** Description This function returns number of bits set in an unsigned
281 ** integer variable
282 **
283 ** Returns int
284 **
285 *******************************************************************************/
tags_ones32(unsigned int x)286 static unsigned int tags_ones32(unsigned int x) {
287 /* 32-bit recursive reduction using SWAR...
288 but first step is mapping 2-bit values
289 into sum of 2 1-bit values in sneaky way
290 */
291 x -= ((x >> 1) & 0x55555555);
292 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
293 x = (((x >> 4) + x) & 0x0f0f0f0f);
294 x += (x >> 8);
295 x += (x >> 16);
296 return (x & 0x0000003f);
297 }
298
299 /*******************************************************************************
300 **
301 ** Function tags_log2
302 **
303 ** Description This function calculates log to the base 2.
304 **
305 ** Returns int
306 **
307 *******************************************************************************/
tags_log2(unsigned int x)308 unsigned int tags_log2(unsigned int x) {
309 x |= (x >> 1);
310 x |= (x >> 2);
311 x |= (x >> 4);
312 x |= (x >> 8);
313 x |= (x >> 16);
314
315 return (tags_ones32(x) - 1);
316 }
317