Add packaging directory
[platform/upstream/neard.git] / plugins / nfctype4.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdint.h>
28 #include <errno.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <sys/socket.h>
32
33 #include <linux/socket.h>
34
35 #include <near/nfc_copy.h>
36 #include <near/plugin.h>
37 #include <near/log.h>
38 #include <near/types.h>
39 #include <near/adapter.h>
40 #include <near/tag.h>
41 #include <near/ndef.h>
42 #include <near/tlv.h>
43
44 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
45
46 #define NFC_STATUS              0
47 #define NFC_STATUS_BYTE_LEN     1
48
49 #define STATUS_WORD_1           1
50 #define STATUS_WORD_2           2
51 #define APDU_HEADER_LEN         5
52 #define APDU_OK                 0x9000
53 #define APDU_NOT_FOUND          0x6A82
54
55 /* PICC Level Commands */
56 #define PICC_CLASS              0x90
57 #define GET_VERSION             0x60
58 #define CREATE_APPLICATION      0xCA
59 #define SELECT_APPLICATION      0x5A
60 #define CREATE_STD_DATA_FILE    0xCD
61 #define WRITE_DATA_TO_FILE      0x3D
62
63 #define DESFire_EV1_MAJOR_VERSION       0x01
64 #define PICC_LEVEL_APDU_OK              0x9100
65 #define GET_VERSION_FRAME_RESPONSE_BYTE 0xAF
66 #define DESFIRE_KEY_SETTINGS    0x0F
67 #define DESFIRE_NUM_OF_KEYS     0x21
68 #define DESFIRE_CC_FILE_NUM     0x01
69 #define DESFIRE_NDEF_FILE_NUM   0x02
70 #define DESFIRE_COMMSET         0x00
71 #define MAPPING_VERSION         0x20
72 #define FREE_READ_ACCESS        0x00
73 #define FREE_WRITE_ACCESS       0x00
74
75 #define T4_ALL_ACCESS           0x00
76 #define T4_READ_ONLY            0xFF
77
78 #define APDU_STATUS(a) near_get_be16(a)
79
80 /* Tag Type 4 version ID */
81 static uint8_t iso_appname_v1[] = { 0xd2, 0x76, 0x0, 0x0, 0x85, 0x01, 0x0 };
82 static uint8_t iso_appname_v2[] = { 0xd2, 0x76, 0x0, 0x0, 0x85, 0x01, 0x1 };
83
84 /* Tag T4 File ID */
85 static uint8_t iso_cc_fileid[] = { 0xe1, 0x03 };
86 #define LEN_ISO_CC_FILEID       2
87
88 #define LEN_ISO_CC_READ_SIZE    0x0F
89
90 #define CMD_HEADER_SIZE         5
91 struct type4_cmd {                      /* iso 7816 */
92         uint8_t class;
93         uint8_t instruction;
94         uint8_t param1;
95         uint8_t param2;
96         uint8_t data_length;
97         uint8_t data[];
98 } __attribute__((packed));
99
100 struct type4_NDEF_file_control_tlv {
101         uint8_t tag     ;               /* should be 4 */
102         uint8_t len     ;               /* should be 6 */
103         uint16_t file_id ;
104         uint16_t max_ndef_size ;
105         uint8_t read_access ;
106         uint8_t write_access ;
107 } __attribute__((packed));
108
109 struct type4_cc {                       /* Capability Container */
110         uint16_t CCLEN;
111         uint8_t mapping_version;
112         uint16_t max_R_apdu_data_size;
113         uint16_t max_C_apdu_data_size;
114         struct type4_NDEF_file_control_tlv tlv_fc ;
115         uint8_t tlv_blocks[];
116 } __attribute__((packed));
117
118 struct desfire_app {
119         uint8_t aid[3];
120         uint8_t key_settings;
121         uint8_t number_of_keys;
122         uint8_t file_id[2];
123         uint8_t iso_appname[7];
124 } __attribute__((packed));
125
126 struct desfire_std_file {
127         uint8_t file_num;
128         uint8_t file_id[2];
129         uint8_t comm_set;
130         uint8_t access_rights[2];
131         uint8_t size[3];
132 } __attribute__((packed));
133
134 struct desfire_cc_file {
135         uint8_t file_num;
136         uint8_t offset[3];
137         uint8_t max_len[3];
138         uint8_t cc_len[2];
139         uint8_t version;
140         uint8_t mle[2];
141         uint8_t mlc[2];
142         uint8_t ndef_tlv[4];
143         uint8_t ndef_size[2];
144         uint8_t read_access;
145         uint8_t write_access;
146 } __attribute__((packed));
147
148 struct t4_cookie {
149         uint32_t adapter_idx;
150         uint32_t target_idx;
151         near_tag_io_cb cb;
152         struct near_tag *tag;
153         uint16_t read_data;
154         uint16_t r_apdu_max_size;
155         uint16_t c_apdu_max_size;
156         uint16_t max_ndef_size;
157         uint8_t write_access;
158         struct near_ndef_message *ndef;
159         uint16_t memory_size;
160 };
161
162 static int t4_cookie_release(int err, void *data)
163 {
164         struct t4_cookie *cookie = data;
165
166         DBG("%p", cookie);
167
168         if (!cookie)
169                 return err;
170
171         if (err < 0 && cookie->cb)
172                 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
173
174         if (cookie->ndef)
175                 g_free(cookie->ndef->data);
176
177         g_free(cookie->ndef);
178         g_free(cookie);
179
180         return err;
181 }
182
183 /* ISO functions: This code prepares APDU */
184 static int ISO_send_cmd(uint8_t class,
185                         uint8_t instruction,
186                         uint8_t param1,
187                         uint8_t param2,
188                         uint8_t *cmd_data,
189                         uint8_t cmd_data_length,
190                         bool le,
191                         near_recv cb,
192                         void *in_data)
193 {
194         struct type4_cmd *cmd;
195         struct t4_cookie *in_rcv = in_data;
196         uint8_t total_cmd_length;
197         int err;
198
199         DBG("CLA-%02x INS-%02x P1-%02x P2-%02x",
200                         class, instruction, param1, param2);
201
202         if (!le) {
203                 if (cmd_data)
204                         total_cmd_length = APDU_HEADER_LEN + cmd_data_length;
205                 else
206                         total_cmd_length = APDU_HEADER_LEN;
207         } else {  /* Extra byte for Le */
208                 total_cmd_length = APDU_HEADER_LEN + cmd_data_length + 1;
209         }
210
211         cmd = g_try_malloc0(total_cmd_length);
212         if (!cmd) {
213                 DBG("Mem alloc failed");
214                 err = -ENOMEM;
215                 goto out_err;
216         }
217
218         cmd->class      =       class;
219         cmd->instruction =      instruction ;
220         cmd->param1     =       param1 ;
221         cmd->param2     =       param2 ;
222         cmd->data_length =      cmd_data_length;
223
224         if (cmd_data) {
225                 memcpy(cmd->data, cmd_data, cmd_data_length);
226                 /* The Le byte set to 0x00 defined that any length
227                  * of PICC response is allowed */
228                 if (le)
229                         cmd->data[cmd_data_length] = 0;
230         }
231
232         return near_adapter_send(in_rcv->adapter_idx, (uint8_t *) cmd,
233                                         total_cmd_length, cb, in_rcv,
234                                         t4_cookie_release);
235
236 out_err:
237         /* On exit, clean memory */
238         g_free(cmd);
239
240         return err;
241 }
242
243 /* ISO 7816 command: Select applications or files
244  * p1=0 select by "file id"
245  * P1=4 select by "DF name"
246  *  */
247 static int ISO_Select(uint8_t *filename, uint8_t fnamelen, uint8_t P1,
248                 near_recv cb, void *cookie)
249 {
250         DBG("");
251
252         return ISO_send_cmd(
253                         0x00,           /* CLA */
254                         0xA4,           /* INS: Select file */
255                         P1,             /* P1: select by name */
256                         0x00,           /* P2: First or only occurrence */
257                         filename,       /* cmd_data */
258                         fnamelen,       /* uint8_t cmd_data_length*/
259                         false,
260                         cb,
261                         cookie);
262 }
263
264 /* ISO 7816 command: Read binary data from files */
265 static int ISO_ReadBinary(uint16_t offset, uint8_t readsize,
266                         near_recv cb, void *cookie)
267 {
268         DBG("");
269         return ISO_send_cmd(
270                         0x00,           /* CLA */
271                         0xB0,           /* INS: Select file */
272                         (uint8_t) ((offset & 0xFF00) >> 8),
273                         (uint8_t) (offset & 0xFF),
274                         0,              /* no data send */
275                         readsize,       /* bytes to read */
276                         false,
277                         cb,
278                         cookie);
279 }
280
281 /* ISO 7816 command: Update data */
282 static int ISO_Update(uint16_t offset, uint8_t nlen,
283                         uint8_t *data, near_recv cb, void *cookie)
284 {
285         DBG("");
286         return ISO_send_cmd(
287                         0x00,                   /* CLA */
288                         0xD6,                   /* INS: Select file */
289                         (uint8_t) ((offset & 0xFF00) >> 8),
290                         (uint8_t) (offset & 0xFF),
291                         data,                   /* length of NDEF data */
292                         nlen,                   /* NLEN + NDEF data */
293                         false,
294                         cb,
295                         cookie);
296 }
297
298 static int data_read_cb(uint8_t *resp, int length, void *data)
299 {
300         struct t4_cookie *cookie = data ;
301         uint8_t *nfc_data;
302         size_t data_length, length_read, current_length;
303         uint16_t remain_bytes;
304
305         DBG("%d", length);
306
307         if (length < 0)
308                 return t4_cookie_release(length, cookie);
309
310         if (APDU_STATUS(resp + length - 2) != APDU_OK) {
311                 DBG("Fail read_cb SW:x%04x", APDU_STATUS(resp + length - 2));
312
313                 return t4_cookie_release(-EIO, cookie);
314         }
315
316         nfc_data = near_tag_get_data(cookie->tag, &data_length);
317
318         /* Remove SW1 / SW2  and NFC header */
319         length_read = length - NFC_HEADER_SIZE - 2 ;
320         length = length_read;
321
322         current_length = cookie->read_data;
323
324         if (current_length + (length_read) > data_length)
325                 length_read = data_length - current_length;
326
327         memcpy(nfc_data + current_length, resp + NFC_HEADER_SIZE, length_read);
328         if (current_length + length_read == data_length) {
329                 GList *records;
330
331                 DBG("Done reading");
332
333                 records = near_ndef_parse_msg(nfc_data, data_length, NULL);
334                 near_tag_add_records(cookie->tag, records, cookie->cb, 0);
335
336                 return t4_cookie_release(0, cookie);
337         }
338
339         cookie->read_data += length ;
340         remain_bytes = (data_length - cookie->read_data);
341
342         if (remain_bytes >= cookie->r_apdu_max_size)
343                 return ISO_ReadBinary(cookie->read_data + 2,
344                                 cookie->r_apdu_max_size, data_read_cb, cookie);
345         else
346                 return ISO_ReadBinary(cookie->read_data + 2,
347                                 (uint8_t) remain_bytes, data_read_cb, cookie);
348 }
349
350 static int t4_readbin_NDEF_ID(uint8_t *resp, int length, void *data)
351 {
352         struct t4_cookie *cookie = data;
353         struct near_tag *tag;
354         int err;
355
356         DBG("%d", length);
357
358         if (length < 0)
359                 return t4_cookie_release(length, cookie);
360
361         if (APDU_STATUS(resp + length - 2) != APDU_OK) {
362                 DBG("Fail SW:x%04x", APDU_STATUS(resp + length - 2));
363
364                 return t4_cookie_release(-EIO, cookie);
365         }
366
367         /* Add data to the tag */
368         err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx, NULL,
369                                 near_get_be16(resp + NFC_STATUS_BYTE_LEN));
370         if (err < 0)
371                 return t4_cookie_release(err, cookie);
372
373         tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
374         if (!tag)
375                 return t4_cookie_release(-ENOMEM, cookie);
376
377         near_tag_set_max_ndef_size(tag, cookie->max_ndef_size);
378         near_tag_set_c_apdu_max_size(tag, cookie->c_apdu_max_size);
379         near_tag_set_blank(tag, FALSE);
380
381         /* save the tag */
382         cookie->tag = tag;
383
384         /* Set write conditions */
385         if (cookie->write_access == T4_READ_ONLY)
386                 near_tag_set_ro(tag, TRUE);
387         else
388                 near_tag_set_ro(tag, FALSE);
389
390         /*
391          * TODO: see how we can get the UID value:
392          * near_tag_set_uid(tag, resp + NFC_HEADER_SIZE, 8);
393          */
394
395         /* Read 1st block */
396         return ISO_ReadBinary(2, cookie->r_apdu_max_size - 2,
397                         data_read_cb, cookie);
398 }
399
400 static int t4_select_NDEF_ID(uint8_t *resp, int length, void *data)
401 {
402         struct t4_cookie *cookie = data;
403
404         DBG("%d", length);
405
406         if (length < 0)
407                 return t4_cookie_release(length, cookie);
408
409         /* Check for APDU error */
410         if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
411                 DBG("Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
412
413                 return t4_cookie_release(-EIO, cookie);
414         }
415
416         /* Read 0x0f bytes, to grab the NDEF msg length */
417         return ISO_ReadBinary(0, LEN_ISO_CC_READ_SIZE,
418                                         t4_readbin_NDEF_ID, cookie);
419 }
420
421 static int t4_readbin_cc(uint8_t *resp, int length, void *data)
422 {
423         struct t4_cookie *cookie = data;
424         struct type4_cc *read_cc = (struct type4_cc *)&resp[1];
425
426         DBG("%d", length);
427
428         if (length < 0)
429                 return t4_cookie_release(length, cookie);
430
431         /* Check APDU error ( the two last bytes of the resp) */
432         if (APDU_STATUS(resp + length - 2) != APDU_OK) {
433                 DBG("Fail SW:x%04x", APDU_STATUS(resp + length - 2));
434
435                 return t4_cookie_release(-EIO, cookie);
436         }
437
438         cookie->r_apdu_max_size = g_ntohs(read_cc->max_R_apdu_data_size) -
439                         APDU_HEADER_LEN;
440         cookie->c_apdu_max_size = g_ntohs(read_cc->max_C_apdu_data_size);
441         cookie->max_ndef_size = g_ntohs(read_cc->tlv_fc.max_ndef_size);
442
443         /* TODO 5.1.1: TLV blocks can be zero, one or more... */
444         /* TODO 5.1.2: Must ignore proprietary blocks (x05)... */
445         if (read_cc->tlv_fc.tag != 0x4) {
446                 DBG("NDEF File Control tag not found");
447
448                 return t4_cookie_release(-EINVAL, cookie);
449         }
450
451         /* save rw conditions */
452         cookie->write_access = read_cc->tlv_fc.write_access;
453
454         return ISO_Select((uint8_t *) &read_cc->tlv_fc.file_id,
455                         LEN_ISO_CC_FILEID, 0, t4_select_NDEF_ID, cookie);
456 }
457
458 static int t4_select_cc(uint8_t *resp, int length, void *data)
459 {
460         struct t4_cookie *cookie = data;
461         int err;
462
463         DBG("%d", length);
464
465         if (length < 0)
466                 return t4_cookie_release(length, cookie);
467
468         /* Check for APDU error */
469         if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
470
471                 DBG(" Found empty tag");
472                 /* Add data to the tag */
473                 err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx,
474                                         NULL, 1 /* dummy length */);
475                 if (err < 0)
476                         return t4_cookie_release(err, cookie);
477
478                 cookie->tag = near_tag_get_tag(cookie->adapter_idx,
479                                                 cookie->target_idx);
480                 if (!cookie->tag)
481                         return t4_cookie_release(-ENOMEM, cookie);
482
483                 near_tag_set_blank(cookie->tag, TRUE);
484
485                 if (cookie->cb)
486                         cookie->cb(cookie->adapter_idx, cookie->target_idx, 0);
487
488                 return t4_cookie_release(0, cookie);
489         }
490
491         return ISO_ReadBinary(0, LEN_ISO_CC_READ_SIZE, t4_readbin_cc, cookie);
492 }
493
494 static int t4_select_file_by_name_v1(uint8_t *resp, int length, void *data)
495 {
496         struct t4_cookie *cookie = data;
497
498         DBG("%d", length);
499
500         if (length < 0)
501                 return t4_cookie_release(length, cookie);
502
503         /* Check for APDU error */
504         if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
505                 DBG("V1 Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
506
507                 return t4_cookie_release(-EIO, cookie);
508         }
509
510         if (resp[NFC_STATUS] != 0)
511                 return t4_cookie_release(-EIO, cookie);
512
513         /* Jump to select phase */
514         return ISO_Select(iso_cc_fileid, LEN_ISO_CC_FILEID, 0,
515                                 t4_select_cc, cookie);
516 }
517
518 static int t4_select_file_by_name_v2(uint8_t *resp, int length, void *data)
519 {
520         struct t4_cookie *cookie = data;
521
522         DBG("%d", length);
523
524         if (length < 0)
525                 return t4_cookie_release(length, cookie);
526
527         /* Check for APDU error - Not found */
528         if (APDU_STATUS(resp + STATUS_WORD_1) == APDU_NOT_FOUND) {
529                 DBG("Fallback to V1");
530
531                 return ISO_Select(iso_appname_v1, ARRAY_SIZE(iso_appname_v1),
532                                         0x4, t4_select_file_by_name_v1, cookie);
533         }
534
535         if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
536                 DBG("V2 Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
537
538                 return t4_cookie_release(-EIO, cookie);
539         }
540
541         if (resp[NFC_STATUS] != 0)
542                 return t4_cookie_release(-EIO, cookie);
543
544         /* Jump to select phase */
545         return ISO_Select(iso_cc_fileid, LEN_ISO_CC_FILEID, 0, t4_select_cc,
546                                                                         cookie);
547 }
548
549 static int nfctype4_read(uint32_t adapter_idx,
550                 uint32_t target_idx, near_tag_io_cb cb)
551 {
552         struct t4_cookie *cookie;
553
554         DBG("");
555
556         cookie = g_try_malloc0(sizeof(struct t4_cookie));
557         if (!cookie)
558                 return -ENOMEM;
559
560         cookie->adapter_idx = adapter_idx;
561         cookie->target_idx = target_idx;
562         cookie->cb = cb;
563         cookie->tag = NULL;
564         cookie->read_data = 0;
565
566         /* Check for V2 type 4 tag */
567         return ISO_Select(iso_appname_v2, ARRAY_SIZE(iso_appname_v2),
568                                 0x4, t4_select_file_by_name_v2, cookie);
569 }
570
571 static int data_write_cb(uint8_t *resp, int length, void *data)
572 {
573         struct t4_cookie *cookie = data;
574         int err = 0;
575
576         DBG("%d", length);
577
578         if (length < 0)
579                 return t4_cookie_release(length, cookie);
580
581         if (APDU_STATUS(resp + length - 2) != APDU_OK) {
582                 near_error("write failed SWx%04x",
583                                 APDU_STATUS(resp + length - 2));
584
585                 return t4_cookie_release(-EIO, cookie);
586         }
587
588         if (cookie->ndef->offset >= cookie->ndef->length) {
589                 DBG("Done writing");
590
591                 if (cookie->cb)
592                         cookie->cb(cookie->adapter_idx, cookie->target_idx, 0);
593
594                 return t4_cookie_release(0, cookie);
595         }
596
597         if ((cookie->ndef->length - cookie->ndef->offset) >
598                         cookie->c_apdu_max_size) {
599                 err = ISO_Update(cookie->ndef->offset,
600                                 cookie->c_apdu_max_size,
601                                 cookie->ndef->data + cookie->ndef->offset,
602                                 data_write_cb, cookie);
603                 cookie->ndef->offset += cookie->c_apdu_max_size;
604         } else {
605                 err = ISO_Update(cookie->ndef->offset,
606                                 cookie->ndef->length - cookie->ndef->offset,
607                                 cookie->ndef->data + cookie->ndef->offset,
608                                 data_write_cb, cookie);
609                 cookie->ndef->offset = cookie->ndef->length;
610         }
611
612         if (err < 0)
613                 return t4_cookie_release(err, cookie);
614
615         return err;
616 }
617
618 static int data_write(uint32_t adapter_idx, uint32_t target_idx,
619                                 struct near_ndef_message *ndef,
620                                 struct near_tag *tag, near_tag_io_cb cb)
621 {
622         struct t4_cookie *cookie;
623         int err;
624
625         cookie = g_try_malloc0(sizeof(struct t4_cookie));
626
627         if (!cookie) {
628                 err = -ENOMEM;
629
630                 if (cb)
631                         cb(adapter_idx, target_idx, err);
632
633                 return err;
634         }
635
636         cookie->adapter_idx = adapter_idx;
637         cookie->target_idx = target_idx;
638         cookie->cb = cb;
639         cookie->tag = NULL;
640         cookie->read_data = 0;
641         cookie->max_ndef_size = near_tag_get_max_ndef_size(tag);
642         cookie->c_apdu_max_size = near_tag_get_c_apdu_max_size(tag);
643         cookie->ndef = ndef;
644
645         if (cookie->max_ndef_size < cookie->ndef->length) {
646                 near_error("not enough space on tag to write data");
647
648                 return t4_cookie_release(-ENOMEM, cookie);
649         }
650
651         if ((cookie->ndef->length - cookie->ndef->offset) >
652                         cookie->c_apdu_max_size) {
653                 err = ISO_Update(cookie->ndef->offset,
654                                 cookie->c_apdu_max_size,
655                                 cookie->ndef->data,
656                                 data_write_cb, cookie);
657                 cookie->ndef->offset += cookie->c_apdu_max_size;
658         } else {
659                 err = ISO_Update(cookie->ndef->offset,
660                                 cookie->ndef->length,
661                                 cookie->ndef->data,
662                                 data_write_cb, cookie);
663                 cookie->ndef->offset = cookie->ndef->length;
664         }
665
666         if (err < 0)
667                 goto out_err;
668
669         return 0;
670
671 out_err:
672         return t4_cookie_release(err, cookie);
673 }
674
675 static int nfctype4_write(uint32_t adapter_idx, uint32_t target_idx,
676                         struct near_ndef_message *ndef, near_tag_io_cb cb)
677 {
678         struct near_tag *tag;
679         int err;
680
681         DBG("");
682
683         if (!ndef || !cb) {
684                 err = -EINVAL;
685                 goto out_err;
686         }
687
688         tag = near_tag_get_tag(adapter_idx, target_idx);
689         if (!tag) {
690                 err = -EINVAL;
691                 goto out_err;
692         }
693
694         err = data_write(adapter_idx, target_idx, ndef, tag, cb);
695
696 out_err:
697         if (cb)
698                 cb(adapter_idx, target_idx, err);
699
700         return err;
701 }
702
703 static int check_presence(uint8_t *resp, int length, void *data)
704 {
705         struct t4_cookie *cookie = data;
706         int err = 0;
707
708         DBG("%d", length);
709
710         if (length < 0)
711                 err = -EIO;
712
713         if (cookie->cb)
714                 cookie->cb(cookie->adapter_idx,
715                                 cookie->target_idx, err);
716
717         return t4_cookie_release(err, cookie);
718 }
719
720 static int nfctype4_check_presence(uint32_t adapter_idx,
721                 uint32_t target_idx, near_tag_io_cb cb)
722 {
723         struct t4_cookie *cookie;
724
725         DBG("");
726
727         cookie = g_try_malloc0(sizeof(struct t4_cookie));
728         if (!cookie)
729                 return -ENOMEM;
730
731         cookie->adapter_idx = adapter_idx;
732         cookie->target_idx = target_idx;
733         cookie->cb = cb;
734         cookie->tag = NULL;
735         cookie->read_data = 0;
736
737         /* Check for V2 type 4 tag */
738         return ISO_Select(iso_appname_v2, ARRAY_SIZE(iso_appname_v2),
739                                 0x4, check_presence, cookie);
740 }
741
742 static int select_ndef_file(uint8_t *resp, int length, void *data)
743 {
744         struct t4_cookie *cookie = data;
745
746         DBG("%d", length);
747
748         if (length < 0)
749                 return t4_cookie_release(length, cookie);
750
751         if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
752                 near_error("select ndef file resp failed %02X",
753                                         resp[length - 1]);
754
755                 return t4_cookie_release(-EIO, cookie);
756         }
757
758         DBG("ndef file selected");
759
760         if (cookie->cb)
761                 cookie->cb(cookie->adapter_idx, cookie->target_idx, 0);
762
763         return t4_cookie_release(0, cookie);
764 }
765
766 static int read_cc_file(uint8_t *resp, int length, void *data)
767 {
768         struct t4_cookie *cookie = data;
769         struct near_tag *tag;
770         struct type4_cc *read_cc = NULL;
771         int err = 0;
772
773         DBG("%d", length);
774
775         if (length < 0) {
776                 err = length;
777                 goto out_err;
778         }
779
780         /* Check APDU error ( the two last bytes of the resp) */
781         if (APDU_STATUS(resp + length - 2) != APDU_OK) {
782                 near_error("read cc failed SWx%04x",
783                                         APDU_STATUS(resp + length - 2));
784                 err = -EIO;
785                 goto out_err;
786         }
787
788         /* -2 for status word and -1 is for NFC first byte... */
789         read_cc = g_try_malloc0(length - 2 - NFC_STATUS_BYTE_LEN);
790         if (!read_cc) {
791                 err = -ENOMEM;
792                 goto out_err;
793         }
794
795         memcpy(read_cc, &resp[1], length - 2 - NFC_STATUS_BYTE_LEN) ;
796         cookie->c_apdu_max_size = g_ntohs(read_cc->max_C_apdu_data_size);
797         cookie->max_ndef_size = g_ntohs(read_cc->tlv_fc.max_ndef_size);
798
799         tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
800         if (!tag) {
801                 err = -EINVAL;
802                 goto out_err;
803         }
804
805         near_tag_set_max_ndef_size(tag, cookie->memory_size);
806         near_tag_set_c_apdu_max_size(tag, cookie->c_apdu_max_size);
807
808         if (read_cc->tlv_fc.tag  != 0x4) {
809                 near_error("NDEF File not found") ;
810                 err = -EINVAL ;
811                 goto out_err;
812         }
813
814         err = ISO_Select((uint8_t *)&read_cc->tlv_fc.file_id,
815                         LEN_ISO_CC_FILEID, 0, select_ndef_file, cookie);
816         if (err < 0) {
817                 near_error("select ndef file req failed %d", err);
818                 goto out_err;
819         }
820
821         g_free(read_cc);
822         return 0;
823
824 out_err:
825         g_free(read_cc);
826         return t4_cookie_release(err, cookie);
827 }
828
829 static int select_cc_file(uint8_t *resp, int length, void *data)
830 {
831         int err = 0;
832         struct t4_cookie *cookie = data;
833
834         if (length < 0) {
835                 near_error("CC file select resp failed %d", length);
836
837                 return t4_cookie_release(length, cookie);
838         }
839
840         if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
841                 near_error("CC file select response %02X",
842                                                 resp[length - 1]);
843
844                 return t4_cookie_release(-EIO, cookie);
845         }
846
847         err = ISO_ReadBinary(0, LEN_ISO_CC_READ_SIZE, read_cc_file, cookie);
848         if (err < 0) {
849                 near_error("read cc file req failed %d", err);
850                 goto out_err;
851         }
852
853         return 0;
854
855 out_err:
856         return t4_cookie_release(err, cookie);
857 }
858
859 static int select_iso_appname_v2(uint8_t *resp, int length, void *data)
860 {
861         int err = 0;
862         struct t4_cookie *cookie = data;
863
864         if (length < 0) {
865                 near_error("iso app select resp failed %d", length);
866
867                 return t4_cookie_release(length, cookie);
868         }
869
870         if (resp[NFC_STATUS] != 0x00) {
871                 near_error("iso app select response %02X",
872                                         resp[length - 1]);
873
874                 return t4_cookie_release(-EIO, cookie);
875         }
876
877         err = ISO_Select(iso_cc_fileid, LEN_ISO_CC_FILEID, 0,
878                         select_cc_file, cookie);
879         if (err < 0) {
880                 near_error("select cc req failed %d", err);
881                 goto out_err;
882         }
883
884         return 0;
885
886 out_err:
887         return t4_cookie_release(err, cookie);
888 }
889
890 static int format_resp(uint8_t *resp, int length, void *data)
891 {
892         int err = 0;
893         struct t4_cookie *cookie = data;
894         struct near_tag *tag;
895
896         DBG("");
897
898         if (length < 0) {
899                 near_error("write data to ndef file resp failed %d", length);
900
901                 return t4_cookie_release(length, cookie);
902         }
903
904         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
905                 near_error("wrtie data to ndef file response %02X",
906                                                 resp[length - 1]);
907
908                 return t4_cookie_release(-EIO, cookie);
909         }
910
911         tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
912         if (!tag)
913                 return t4_cookie_release(-EINVAL, cookie);
914
915         DBG("Formatting is done");
916         near_tag_set_blank(tag, FALSE);
917
918         /*
919          * 1) Till now all commands which are used for formatting are
920          *    at mifare desfire level. Now select iso appname_v2,
921          *    cc file and ndef file with ISO 7816-4 commands.
922          * 2) Selecting ndef file means making sure that read write
923          *    operations will perform on NDEF file.
924          */
925         err = ISO_Select(iso_appname_v2, ARRAY_SIZE(iso_appname_v2),
926                         0x4, select_iso_appname_v2, cookie);
927         if (err < 0) {
928                 near_error("iso_select appnamev2 req failed %d", err);
929                 goto out_err;
930         }
931
932         return err;
933
934 out_err:
935         return t4_cookie_release(err, cookie);
936 }
937
938 static int write_data_to_ndef_file(uint8_t *resp, int length, void *data)
939 {
940         int err = 0;
941         struct t4_cookie *cookie = data;
942         uint8_t *cmd_data = NULL;
943         uint8_t cmd_data_length;
944         uint8_t ndef_file_offset[] = {0x00, 0x00, 0x00};
945         uint8_t empty_ndef_file_len[] = {0x02, 0x00, 0x00}; /* 000002h */
946         uint8_t ndef_nlen[] = {0x00, 0x00}; /* 0000h */
947
948         DBG("");
949
950         if (length < 0) {
951                 near_error("create ndef file resp failed %d", length);
952                 err = length;
953                 goto out_err;
954         }
955
956         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
957                 near_error("create ndef file response %02X",
958                                                 resp[length - 1]);
959                 err = -EIO;
960                 goto out_err;
961         }
962
963         /* Step8 : Write data to NDEF file ( no NDEF message) */
964         cmd_data_length = 1 /* File num */
965                                 + ARRAY_SIZE(ndef_file_offset)
966                                 + ARRAY_SIZE(empty_ndef_file_len)
967                                 + ARRAY_SIZE(ndef_nlen);
968
969         cmd_data = g_try_malloc0(cmd_data_length);
970         if (!cmd_data) {
971                 err = -ENOMEM;
972                 goto out_err;
973         }
974
975         cmd_data[0] = DESFIRE_NDEF_FILE_NUM;
976         memcpy(cmd_data + 1, ndef_file_offset, ARRAY_SIZE(ndef_file_offset));
977         memcpy(cmd_data + 4, empty_ndef_file_len,
978                         ARRAY_SIZE(empty_ndef_file_len));
979         memcpy(cmd_data + 7, ndef_nlen, ARRAY_SIZE(ndef_nlen));
980
981         err = ISO_send_cmd(PICC_CLASS, WRITE_DATA_TO_FILE,
982                                 0x00, 0x00, cmd_data, cmd_data_length,
983                                 true, format_resp, cookie);
984         if (err < 0) {
985                 near_error("wrtie data to ndef file req failed %d", err);
986                 goto out_err;
987         }
988
989         g_free(cmd_data);
990         return 0;
991
992 out_err:
993         g_free(cmd_data);
994         return t4_cookie_release(err, cookie);
995 }
996
997 static int create_ndef_file(uint8_t *resp, int length, void *data)
998 {
999         int err = 0;
1000         struct t4_cookie *cookie = data;
1001         struct desfire_std_file *ndef = NULL;
1002         uint8_t iso_ndef_file_id[] = {0x04, 0xE1}; /* E104h */
1003         uint8_t ndef_file_access_rights[] = {0xE0, 0xEE}; /* EEE0h */
1004
1005         DBG("");
1006
1007         if (length < 0) {
1008                 near_error("write data to cc file resp failed %d", length);
1009                 err = length;
1010                 goto out_err;
1011         }
1012
1013         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
1014                 near_error("write data to cc file response %02X",
1015                                                 resp[length - 1]);
1016                 err = -EIO;
1017                 goto out_err;
1018         }
1019
1020         ndef = g_try_malloc0(sizeof(struct desfire_std_file));
1021         if (!ndef) {
1022                 err = -ENOMEM;
1023                 goto out_err;
1024         }
1025
1026         ndef->file_num = DESFIRE_NDEF_FILE_NUM;
1027         memcpy(ndef->file_id, iso_ndef_file_id, ARRAY_SIZE(iso_ndef_file_id));
1028         ndef->comm_set = DESFIRE_COMMSET;
1029         memcpy(ndef->access_rights, ndef_file_access_rights,
1030                                 ARRAY_SIZE(ndef_file_access_rights));
1031         ndef->size[0] = 0;
1032         ndef->size[1] = (uint8_t) (cookie->memory_size >> 8);
1033         ndef->size[2] = (uint8_t) cookie->memory_size;
1034
1035         err = ISO_send_cmd(PICC_CLASS, CREATE_STD_DATA_FILE,
1036                                 0x00, 0x00, (uint8_t *)ndef,
1037                                 sizeof(struct desfire_std_file),
1038                                 true, write_data_to_ndef_file, cookie);
1039         if (err < 0) {
1040                 near_error("create ndef file req failed %d", err);
1041                 goto out_err;
1042         }
1043
1044         g_free(ndef);
1045         return 0;
1046
1047 out_err:
1048         g_free(ndef);
1049         return t4_cookie_release(err, cookie);
1050 }
1051
1052 static int write_data_to_cc_file(uint8_t *resp, int length, void *data)
1053 {
1054         int err = 0;
1055         struct t4_cookie *cookie = data;
1056         struct desfire_cc_file *cc = NULL;
1057         uint8_t cc_file_offset[] = {0x00, 0x00, 0x00};
1058         uint8_t cc_file_max_len[] = {0x0F, 0x00, 0x00}; /* 00000Fh */
1059         uint8_t cc_len[] = {0x00, 0x0F}; /* 000Fh*/
1060         uint8_t mle_r_apdu[] = {0x00, 0x3B}; /* 003Bh */
1061         uint8_t mlc_c_apdu[] = {0x00, 0x34}; /* 0034h */
1062         /* T: 04, L: 06: V: E104h (NDEF ISO FID = E104h)*/
1063         uint8_t ndef_tlv[] = {0x04, 0x06, 0xE1, 0x04};
1064
1065
1066         DBG("");
1067
1068         if (length < 0) {
1069                 near_error("create cc file resp failed %d", length);
1070                 err = length;
1071                 goto out_err;
1072         }
1073
1074         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
1075                 near_error("create cc file response %02X",
1076                                                 resp[length - 1]);
1077                 err = -EIO;
1078                 goto out_err;
1079         }
1080
1081         cc = g_try_malloc0(sizeof(struct desfire_cc_file));
1082         if (!cc) {
1083                 err = -ENOMEM;
1084                 goto out_err;
1085         }
1086
1087         cc->file_num = DESFIRE_CC_FILE_NUM;
1088         memcpy(cc->offset, cc_file_offset, ARRAY_SIZE(cc_file_offset));
1089         memcpy(cc->max_len, cc_file_max_len, ARRAY_SIZE(cc_file_max_len));
1090         memcpy(cc->cc_len, cc_len, ARRAY_SIZE(cc_len));
1091         cc->version = MAPPING_VERSION;
1092         memcpy(cc->mle, mle_r_apdu, ARRAY_SIZE(mle_r_apdu));
1093         memcpy(cc->mlc, mlc_c_apdu, ARRAY_SIZE(mlc_c_apdu));
1094         memcpy(cc->ndef_tlv, ndef_tlv, ARRAY_SIZE(ndef_tlv));
1095         cc->ndef_size[0] = (uint8_t) (cookie->memory_size >> 8);
1096         cc->ndef_size[1] = (uint8_t) cookie->memory_size;
1097         cc->read_access = FREE_READ_ACCESS;
1098         cc->write_access = FREE_WRITE_ACCESS;
1099
1100         err = ISO_send_cmd(PICC_CLASS, WRITE_DATA_TO_FILE,
1101                                 0x00, 0x00, (uint8_t *)cc,
1102                                 sizeof(struct desfire_cc_file),
1103                                 true, create_ndef_file, cookie);
1104         if (err < 0) {
1105                 near_error("write data to cc file req failed %d", err);
1106                 goto out_err;
1107         }
1108
1109         g_free(cc);
1110         return 0;
1111
1112 out_err:
1113         g_free(cc);
1114         return t4_cookie_release(err, cookie);
1115 }
1116
1117 static int create_cc_file(uint8_t *resp, int length, void *data)
1118 {
1119         int err = 0;
1120         struct t4_cookie *cookie = data;
1121         struct desfire_std_file *cc = NULL;
1122         uint8_t iso_cc_file_id[] = {0x03, 0xe1}; /* E103h */
1123         uint8_t cc_file_access_rights[] = {0xE0, 0xEE}; /* EEE0h */
1124         uint8_t cc_file_max_len[] = {0x0F, 0x00, 0x00}; /* 00000Fh */
1125
1126         DBG("");
1127
1128         if (length < 0) {
1129                 near_error("select application1 resp failed %d", length);
1130                 err = length;
1131                 goto out_err;
1132         }
1133
1134         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
1135                 near_error("select application1 response %02X",
1136                                                 resp[length - 1]);
1137                 err = -EIO;
1138                 goto out_err;
1139         }
1140
1141         cc = g_try_malloc0(sizeof(struct desfire_std_file));
1142         if (!cc) {
1143                 err = -ENOMEM;
1144                 goto out_err;
1145         }
1146
1147         cc->file_num = DESFIRE_CC_FILE_NUM;
1148         memcpy(cc->file_id, iso_cc_file_id, ARRAY_SIZE(iso_cc_file_id));
1149         cc->comm_set = DESFIRE_COMMSET;
1150         memcpy(cc->access_rights, cc_file_access_rights,
1151                                 ARRAY_SIZE(cc_file_access_rights));
1152         memcpy(cc->size, cc_file_max_len, ARRAY_SIZE(cc_file_max_len));
1153
1154         err = ISO_send_cmd(PICC_CLASS,
1155                                 CREATE_STD_DATA_FILE,
1156                                 0x00, 0x00, (uint8_t *)cc,
1157                                 sizeof(struct desfire_std_file),
1158                                 true, write_data_to_cc_file, cookie);
1159         if (err < 0) {
1160                 near_error("create cc file req failed %d", err);
1161                 goto out_err;
1162         }
1163
1164         g_free(cc);
1165         return 0;
1166
1167 out_err:
1168         g_free(cc);
1169         return t4_cookie_release(err, cookie);
1170 }
1171
1172 static int select_application_1(uint8_t *resp, int length, void *data)
1173 {
1174         int err = 0;
1175         struct t4_cookie *cookie = data;
1176         uint8_t *cmd_data = NULL;
1177         uint8_t cmd_data_length;
1178         uint8_t desfire_aid_1[] = {0x01, 0x00, 0x00}; /* 000001h */
1179
1180         DBG("");
1181
1182         if (length < 0) {
1183                 near_error("create application resp failed %d", length);
1184                 err = length;
1185                 goto out_err;
1186         }
1187
1188         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
1189                 near_error("create application response %02X",
1190                                                 resp[length - 1]);
1191                 err = -EIO;
1192                 goto out_err;
1193         }
1194
1195         /* Step4 : Select application (which is created just now) */
1196         cmd_data_length = ARRAY_SIZE(desfire_aid_1);
1197         cmd_data = g_try_malloc0(cmd_data_length);
1198         if (!cmd_data) {
1199                 err = -ENOMEM;
1200                 goto out_err;
1201         }
1202
1203         memcpy(cmd_data, desfire_aid_1, cmd_data_length);
1204         err = ISO_send_cmd(PICC_CLASS, SELECT_APPLICATION,
1205                                 0x00, 0x00, cmd_data, cmd_data_length,
1206                                 true, create_cc_file, cookie);
1207         if (err < 0) {
1208                 near_error("select application1 req failed %d", err);
1209                 goto out_err;
1210         }
1211
1212         g_free(cmd_data);
1213         return 0;
1214
1215 out_err:
1216         g_free(cmd_data);
1217         return t4_cookie_release(err, cookie);
1218 }
1219
1220 static int create_application(uint8_t *resp, int length, void *data)
1221 {
1222         int err = 0;
1223         struct t4_cookie *cookie = data;
1224         uint8_t desfire_aid_1[] = {0x01, 0x00, 0x00}; /* 000001h */
1225         uint8_t desfire_file_id[] = {0x10, 0xE1}; /* E110h */
1226         struct desfire_app *app = NULL;
1227
1228         DBG("");
1229
1230         if (length < 0) {
1231                 near_error("select application resp failed %d", length);
1232                 err = length;
1233                 goto out_err;
1234         }
1235
1236         if (APDU_STATUS(resp + 1) != PICC_LEVEL_APDU_OK) {
1237                 near_error("select application response %02X",
1238                                                 resp[length - 1]);
1239                 err = -EIO;
1240                 goto out_err;
1241         }
1242
1243         app = g_try_malloc0(sizeof(struct desfire_app));
1244         if (!app) {
1245                 err = -ENOMEM;
1246                 goto out_err;
1247         }
1248
1249         memcpy(app->aid, desfire_aid_1, ARRAY_SIZE(desfire_aid_1));
1250         app->key_settings = DESFIRE_KEY_SETTINGS;
1251         app->number_of_keys = DESFIRE_NUM_OF_KEYS;
1252         memcpy(app->file_id, desfire_file_id, ARRAY_SIZE(desfire_file_id));
1253         memcpy(app->iso_appname, iso_appname_v2, ARRAY_SIZE(iso_appname_v2));
1254
1255         /* Step3 : Create Application */
1256         err = ISO_send_cmd(PICC_CLASS, CREATE_APPLICATION,
1257                                 0x00, 0x00, (uint8_t *)app,
1258                                 sizeof(struct desfire_app),
1259                                 true, select_application_1, cookie);
1260         if (err < 0) {
1261                 near_error("create application req failed %d", err);
1262                 goto out_err;
1263         }
1264
1265         g_free(app);
1266         return 0;
1267
1268 out_err:
1269         g_free(app);
1270         return t4_cookie_release(err, cookie);
1271 }
1272
1273 static int select_application(uint8_t *resp, int length, void *data)
1274 {
1275         int err;
1276         struct t4_cookie *cookie = data;
1277         uint8_t *cmd_data = NULL;
1278         uint8_t cmd_data_length;
1279         uint8_t desfire_aid[] = {0x00, 0x00, 0x00}; /* 000000h */
1280
1281         DBG("");
1282
1283         if (length < 0) {
1284                 near_error("get version3 resp failed %d", length);
1285                 err = length;
1286                 goto out_err;
1287         }
1288
1289         if (resp[length - 1] != 0x00) {
1290                 near_error("get version3 response %02X",
1291                                                 resp[length - 1]);
1292                 err = -EIO;
1293                 goto out_err;
1294         }
1295
1296         /* AID : 000000h */
1297         cmd_data_length = ARRAY_SIZE(desfire_aid);
1298         cmd_data = g_try_malloc0(cmd_data_length);
1299         if (!cmd_data) {
1300                 err = -ENOMEM;
1301                 goto out_err;
1302         }
1303
1304         memcpy(cmd_data, desfire_aid, cmd_data_length);
1305         /* Step2 : Select Application */
1306         err = ISO_send_cmd(PICC_CLASS,
1307                                 SELECT_APPLICATION,
1308                                 0x00, 0x00, cmd_data, cmd_data_length,
1309                                 true, create_application, cookie);
1310         if (err < 0) {
1311                 near_error("select application req failed %d", err);
1312                 goto out_err;
1313         }
1314
1315         g_free(cmd_data);
1316         return 0;
1317
1318 out_err:
1319         g_free(cmd_data);
1320         return t4_cookie_release(err, cookie);
1321 }
1322
1323 static int get_version_frame3(uint8_t *resp, int length, void *data)
1324 {
1325         int err;
1326         struct t4_cookie *cookie = data;
1327
1328         DBG("");
1329
1330         if (length < 0) {
1331                 near_error("get version2 resp failed %d", length);
1332
1333                 return t4_cookie_release(length, cookie);
1334         }
1335
1336         if (resp[4] == 0x01 /* Major Version */
1337                 && resp[length - 1] == GET_VERSION_FRAME_RESPONSE_BYTE) {
1338
1339                 err = ISO_send_cmd(PICC_CLASS,
1340                                         GET_VERSION_FRAME_RESPONSE_BYTE,
1341                                         0x00, 0x00, NULL, 0, false,
1342                                         select_application, cookie);
1343                 if (err < 0) {
1344                         near_error("get version3 req failed %d", err);
1345
1346                         return t4_cookie_release(err, cookie);
1347                 }
1348         } else {
1349                 near_error("get version2 response %02X", resp[length - 1]);
1350
1351                 return t4_cookie_release(-EIO, cookie);
1352         }
1353
1354         return 0;
1355 }
1356
1357 static int get_version_frame2(uint8_t *resp, int length, void *data)
1358 {
1359         int err;
1360         struct t4_cookie *cookie = data;
1361
1362         DBG("");
1363
1364         if (length < 0) {
1365                 near_error(" get version resp failed %d", length);
1366
1367                 return t4_cookie_release(length, cookie);
1368         }
1369
1370         if (resp[4] == 0x01 /* Major Version */
1371                 && resp[length - 1] == GET_VERSION_FRAME_RESPONSE_BYTE) {
1372
1373                 /*
1374                  * When N is the GET_VERSION response 6th byte,
1375                  * the DESFire tag memory size is 2 ^ (N /2).
1376                  */
1377                 cookie->memory_size = (1 << (resp[6] / 2));
1378                 err = ISO_send_cmd(PICC_CLASS,
1379                                         GET_VERSION_FRAME_RESPONSE_BYTE,
1380                                         0x00, 0x00, NULL, 0, false,
1381                                         get_version_frame3, cookie);
1382                 if (err < 0) {
1383                         near_error("get version2 req failed %d", err);
1384
1385                         return t4_cookie_release(err, cookie);
1386                 }
1387         } else {
1388                 near_error("get version response %02X", resp[length - 1]);
1389
1390                 return t4_cookie_release(-EIO, cookie);
1391         }
1392
1393         return 0;
1394 }
1395
1396 /* Steps to format Type 4 (MIFARE DESFire EV1) tag as per AN1104.pdf from nxp.
1397  * 1) Get version to determine memory size of tag
1398  * 2) Select applciation with AID equal to 000000h (PICC level)
1399  * 3) Create application with AID equal to 000001h
1400  * 4) Select application (Select previously created application in step3)
1401  * 5) Create std data file with File number equal to 01h (CC file), ISOFileID
1402  *    equal to E103h, ComSet equal to 00h, AccesRights to EEEEh, FileSize bigger
1403  *    equal to 00000Fh
1404  * 6) Write data to CC file with CCLEN equal to 000Fh, Mapping version equal to
1405  *    20h, MLe equal to 003Bh, MLc equal to 0034h, and NDEF File control TLV
1406  *    equal to: T=04h, L=06h, V=E1 04 (NDEF ISO FID = E104h), 08 00 (NDEF File
1407  *    size = 2048 Bytes) 00 (free read access) 00 (free write access)
1408  * 7) Create std data file with File number equal to 02h (NDEF File DESFireFId),
1409  *    ISO FileID equal to E104h, ComSet equal to 00h, ComSet equal to 00h,
1410  *    AccessRights equal to EEE0h, FileSize equal to 000800h (2048 bytes)
1411  * 8) Write data to write content of the NDEF File with NLEN equal to 0000h, and
1412  *    no NDEF messsage.
1413  * 9) Now Formatting is done, then select ISO appname2, select CC file and read.
1414  * 10) Select NDEF file (by doing last two steps means, making sure that read
1415  *    write operations perform on NDEF file).
1416  * */
1417
1418 static int nfctype4_format(uint32_t adapter_idx, uint32_t target_idx,
1419                                                 near_tag_io_cb cb)
1420 {
1421         int err;
1422         struct t4_cookie *cookie;
1423
1424         DBG("");
1425
1426         cookie = g_try_malloc0(sizeof(struct t4_cookie));
1427         if (!cookie)
1428                 return -ENOMEM;
1429
1430         cookie->adapter_idx = adapter_idx;
1431         cookie->target_idx = target_idx;
1432         cookie->cb = cb;
1433
1434         /* Step1 : Get Version */
1435         err = ISO_send_cmd(PICC_CLASS, GET_VERSION,
1436                                 0x00, 0x00, NULL, 0, false,
1437                                 get_version_frame2, cookie);
1438
1439         if (err < 0) {
1440                 near_error("get version req failed %d", err);
1441                 g_free(cookie);
1442         }
1443
1444         return err;
1445 }
1446
1447 static struct near_tag_driver type4a_driver = {
1448         .type           = NFC_PROTO_ISO14443,
1449         .priority       = NEAR_TAG_PRIORITY_DEFAULT,
1450         .read           = nfctype4_read,
1451         .write          = nfctype4_write,
1452         .check_presence = nfctype4_check_presence,
1453         .format         = nfctype4_format,
1454 };
1455
1456 static struct near_tag_driver type4b_driver = {
1457         .type           = NFC_PROTO_ISO14443_B,
1458         .priority       = NEAR_TAG_PRIORITY_DEFAULT,
1459         .read           = nfctype4_read,
1460         .write          = nfctype4_write,
1461         .check_presence = nfctype4_check_presence,
1462         .format         = nfctype4_format,
1463 };
1464
1465 static int nfctype4_init(void)
1466 {
1467         int ret;
1468
1469         DBG("");
1470
1471         ret = near_tag_driver_register(&type4b_driver);
1472         if (ret < 0)
1473                 return ret;
1474
1475         return near_tag_driver_register(&type4a_driver);
1476 }
1477
1478 static void nfctype4_exit(void)
1479 {
1480         DBG("");
1481
1482         near_tag_driver_unregister(&type4a_driver);
1483         near_tag_driver_unregister(&type4b_driver);
1484 }
1485
1486 NEAR_PLUGIN_DEFINE(nfctype4, "NFC Forum Type 4 tags support", VERSION,
1487                 NEAR_PLUGIN_PRIORITY_HIGH, nfctype4_init, nfctype4_exit)