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