summaryrefslogtreecommitdiffstats
path: root/src/phFriNfc_SmtCrdFmt.h
blob: 4800c41c21b5b154ceb1f51a9b5ddc7b08a9d359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Copyright (C) 2010 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*!
 * \file  phFriNfc_SmtCrdFmt.h
 * \brief NFC-FRI Smart Card Formatting.
 *
 * Project: NFC-FRI
 *
 * $Date: Mon Dec 13 14:14:11 2010 $
 * $Author: ing02260 $
 * $Revision: 1.5 $
 * $Aliases:  $
 *
 */

#ifndef PHFRINFC_SMTCRDFMT_H
#define PHFRINFC_SMTCRDFMT_H

/** 
 *  \name Smart Card Formatting
 *
 * File: \ref phFri_CardFormatFunctions.h
 *
 */
/*@{*/
#define PHFRINFC_SMTCRDFMT_FILEREVISION "$Revision: 1.5 $"
#define PHFRINFC_SMTCRDFMT_FILEALIASES  "$Aliases:  $"
/*@}*/

/*! \defgroup grp_fri_smart_card_formatting NFC FRI Smart Card Formatting
 *
 *  Smart Card Formatting functionality enables automatic formatting of any type of smart cards.
 *  This initializes the smart cards and makes them NDEF Compliant.
 *  Single API is provided to handle format/recovery management of different types cards.
 *  Following are different Types of cards supported by this module, currently.
 *      - Type1 ( Topaz)
 *      - Type2 ( Mifare UL)
 *      - Type4 ( Desfire)
 *      - Mifare Std.
 */
/*@{*/
/**
 *  \ingroup grp_fri_smart_card_formatting
 *  \brief Macro definitions.
 *  \note 
          On requirement basis, new constants will be defined
          during the implementation phase.
*/

#define DESFIRE_FMT_EV1


#define PH_FRI_NFC_SMTCRDFMT_NFCSTATUS_FORMAT_ERROR                 9
#define  PH_FRINFC_SMTCRDFMT_MSTD_DEFAULT_KEYA_OR_KEYB           {0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF}
#define  PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA                   {0xA0, 0xA1,0xA2,0xA3,0xA4,0xA5}
#define  PH_FRINFC_SMTCRDFMT_NFCFORUMSECT_KEYA                   {0xD3, 0xF7,0xD3,0xF7,0xD3,0xF7}
#define  PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_ACCESSBITS             {0x78,0x77,0x88}
#define  PH_FRINFC_SMTCRDFMT_MSTD_NFCFORUM_ACCESSBITS            {0x7F,0x07,0x88}
#define  PH_FRINFC_SMTCRDFMT_MAX_TLV_TYPE_SUPPORTED              1

#define  PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE             252

#define  PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT                   1

enum 
{
    PH_FRINFC_SMTCRDFMT_MIFARE_UL_CARD,
    PH_FRINFC_SMTCRDFMT_ISO14443_4A_CARD,           
    PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD,         
    PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD,         
    PH_FRINFC_SMTCRDFMT_TOPAZ_CARD                 
};

/**
 * \name Completion Routine Indices
 *
 * These are the indices of the completion routine pointers within the component context.
 * Completion routines belong to upper components.
 *
 */
/*@{*/
/** \ingroup grp_fri_nfc_ndef_map
*  Completion Routine Index for \ref phFriNfc_SmtCrd_Format */
#define PH_FRINFC_SMTCRDFMT_CR_FORMAT     0  /* */ 
/** \ingroup grp_fri_nfc_ndef_map Completion
 *  Routine Index for Unknown States/Operations */
#define PH_FRINFC_SMTCRDFMT_CR_INVALID_OPE    1  /* */  
/** \ingroup grp_fri_nfc_ndef_map
 *  Number of completion routines that have to be initialised */
#define  PH_FRINFC_SMTCRDFMT_CR               2
/*@}*/


/*@}*/

/*
 *  \ingroup grp_fri_smart_card_formatting
 *
 *  \brief NFC Smart Card Formatting Component Type1 Additional Information Structure
 *
 *  This structure is used to specify additional information required to format the Type1 card.
 *  \note 
 *           On requirement basis,structure will be filled/modified with other parameters
 *         during the implementation phase.
 *
 */
typedef struct phFriNfc_Type1_AddInfo
{
  /* Stores the CC byte values. For Ex: 0xE1, 0x10 , 0x0C, 0x00*/
  uint8_t CCBytes[5];
  uint8_t UID[4];
  uint8_t CCByteIndex;
            
} phFriNfc_Type1_AddInfo_t;

/*
 *
 *  \ingroup grp_fri_smart_card_formatting
 *  \brief NFC Smart Card Formatting Component Type2 Additional Information Structure
 *
 *  This structure is used to specify additional information required to format the Type2 card.
 *  \note 
 *           On requirement basis,structure will be filled/modified with other parametes
 *         during the implementation phase.
 *
 */
typedef struct phFriNfc_Type2_AddInfo
{
    /* Stores the CC byte values. For Ex: 0xE1, 0x10 , 0x10, 0x00*/
   uint8_t OTPBytes[4];
#ifdef FRINFC_READONLY_NDEF
   uint8_t  LockBytes[4];

#ifdef PH_NDEF_MIFARE_ULC
   uint8_t  ReadData[16];
   uint8_t  ReadDataIndex;
   uint8_t  DynLockBytes[4];
   uint8_t  BytesLockedPerLockBit;
   uint8_t  LockBytesPerPage;
   uint8_t  LockByteNumber;
   uint8_t  LockBlockNumber;
   uint8_t  NoOfLockBits;
   uint8_t  DefaultLockBytesFlag;
   uint8_t  LockBitsWritten;
#endif /* #ifdef PH_NDEF_MIFARE_ULC */

#endif /* #ifdef FRINFC_READONLY_NDEF */
   /* Current Block Address*/
   uint8_t CurrentBlock;
} phFriNfc_Type2_AddInfo_t;

/*
 *  \ingroup grp_fri_smart_card_formatting
 *  \brief NFC Smart Card Formatting Component Type4 Additional Information Structure
 *
 *  This structure is used to specify additional information required to format the type4 card.
 *  \note 
 *          On requirement basis,structure will be filled/modified with other parametes
 *         during the implementation phase.
 *
 */

typedef struct phFriNfc_Type4_AddInfo
{              
    /* Specifies Keys related to PICC/NFCForum Master Key settings*/
    /* Stores the PICC Master Key/NFC Forum MasterKey*/    
    uint8_t PICCMasterKey[16];
    uint8_t NFCForumMasterkey[16];

    /* To create the files follwoiing attributes are required*/
    uint8_t         PrevState;
    uint16_t        FileAccessRights;
    uint32_t        CardSize;
    uint16_t        MajorVersion;
    uint16_t        MinorVersion;

} phFriNfc_Type4_AddInfo_t;

/*
 *  \ingroup grp_fri_smart_card_formatting
 *  \brief NFC Smart Card Formatting Component Mifare Std Additional Information Structure
 *
 *  This structure is used to specify additional information required to format the Mifare Std card.
 *  \note 
 *         On requirement basis,structure will be filled/modified with other parametes
 *         during the implementation phase.
 *
 */
 typedef struct phFriNfc_MfStd_AddInfo
{
    /** Device input parameter for poll and connect after failed authentication */
    phHal_sDevInputParam_t  *DevInputParam;

    /* Stores the Default KeyA and KeyB values*/
    uint8_t     Default_KeyA_OR_B[6];

    /* Key A of MAD sector*/
    uint8_t     MADSect_KeyA[6];

    /* Key A of NFC Forum Sector sector*/
    uint8_t     NFCForumSect_KeyA[6];

    /* Access Bits of MAD sector*/
    uint8_t     MADSect_AccessBits[3];

    /* Access Bits of NFC Forum sector*/
    uint8_t     NFCForumSect_AccessBits[3];

    /* Secret key B to given by the application */
    uint8_t     ScrtKeyB[6];

    /* Specifies the status of the different authentication handled in 
    formatting procedure*/
    uint8_t     AuthState;

    /* Stores the current block */
    uint16_t    CurrentBlock;

    /* Stores the current block */
    uint8_t     NoOfDevices;

    /* Store the compliant sectors */
    uint8_t     SectCompl[40];

    /* Flag to know that MAD sector */
    uint8_t     WrMADBlkFlag;

    /* Fill the MAD sector blocks */
    uint8_t     MADSectBlk[80];

    /* Fill the MAD sector blocks */
    uint8_t     UpdMADBlk;
} phFriNfc_MfStd_AddInfo_t;


 /*
 *  \ingroup grp_fri_smart_card_formatting
 *  \brief NFC Smart Card Formatting Component ISO-15693 Additional Information Structure
 *
 *  This structure is used to specify additional information required to format the ISO-15693 card.
 *  \note 
 *         On requirement basis,structure will be filled/modified with other parametes
 *         during the implementation phase.
 *
 */
 typedef struct phFriNfc_ISO15693_AddInfo
 {
    /* Stores the current block executed */
    uint16_t        current_block;
    /* Sequence executed */
    uint8_t         format_seq;
    /* Maximum data size in the card */
    uint16_t        max_data_size;
 }phFriNfc_ISO15693_AddInfo_t;

/**
 *  \ingroup grp_fri_smart_card_formatting
 *
 *  \brief NFC Smart Card Formatting Component Additional Information Structure
 *
 *  This structure is composed to have additional information of different type of tags 
 *   Ex: Type1/Type2/Type4/Mifare 1k/4k 
 *
 *  \note 
 *          On requirement basis, structure will be filled/modified with other parameters
 *         during the implementation phase.
 */
typedef struct phFriNfc_sNdefSmtCrdFmt_AddInfo
{
   phFriNfc_Type1_AddInfo_t         Type1Info;
   phFriNfc_Type2_AddInfo_t         Type2Info;
   phFriNfc_Type4_AddInfo_t         Type4Info;
   phFriNfc_MfStd_AddInfo_t         MfStdInfo;
   phFriNfc_ISO15693_AddInfo_t      s_iso15693_info;

}phFriNfc_sNdefSmtCrdFmt_AddInfo_t;

/**
 *  \ingroup grp_fri_smart_card_formatting
 *  \brief NFC Smart Card Formatting Component Context Structure
 *
 *  This structure is used to store the current context information of the instance.
 *
 *  \note  On requirement basis,structure will be filled/modified with other parameters
 *            during the implementation phase 
 *
 */
typedef struct phFriNfc_sNdefSmtCrdFmt
{
     /** Pointer to the lower (HAL) instance.*/
    void                                *LowerDevice;
    
    /** Holds the device additional informations*/
    phHal_sDepAdditionalInfo_t          psDepAdditionalInfo;

    /** Pointer to the Remote Device Information */
    phHal_sRemoteDevInformation_t       *psRemoteDevInfo;
    
    /** Stores the type of the smart card. */
    uint8_t                             CardType;
    
    /** Stores operating mode type of the MifareStd. */
    /* phHal_eOpModes_t                    OpModeType[2]; */

     /**< \internal The state of the operation. */
    uint8_t                             State;        

    /**< \internal Stores the card state Ex: Blank/Formatted etc. */
    uint8_t                             CardState;    

     /**< \internal Completion Routine Context. */
    phFriNfc_CplRt_t                    CompletionRoutine[PH_FRINFC_SMTCRDFMT_CR]; 

     /**<\internal Holds the completion routine informations of the Smart Card Formatting Layer*/
    phFriNfc_CplRt_t                    SmtCrdFmtCompletionInfo;

     /**<\internal Holds the Command Type(read/write)*/
    phHal_uCmdList_t                    Cmd;

     /**< \internal Holds the length of the received data. */
    uint16_t                            *SendRecvLength;

    /**<\internal Holds the ack of some intial commands*/
    uint8_t                             *SendRecvBuf;

      /**< \internal Holds the length of the data to be sent. */
    uint16_t                            SendLength; 

    /**< \internal Stores the output/result of the format procedure. Ex: Formatted Successfully,
             Format Error etc */
    NFCSTATUS                           FmtProcStatus;    

    /** Stores Additional Information needed to format the different types of tags*/
    phFriNfc_sNdefSmtCrdFmt_AddInfo_t   AddInfo;

    /*  Stores NDEF message TLV*/
    /* This stores the different TLV messages for the different card types*/
    uint8_t   TLVMsg[PH_FRINFC_SMTCRDFMT_MAX_TLV_TYPE_SUPPORTED][8];

           
} phFriNfc_sNdefSmtCrdFmt_t;

/**
 * \ingroup grp_fri_smart_card_formatting
 * \brief Smart Card Formatting \b Reset function
 *
 * \copydoc page_reg Resets the component instance to the initial state and initializes the 
 *          internal variables.
 *
 * \param[in] NdefSmtCrdFmt is a Pointer to a valid and initialized or uninitialised instance
 *            of \ref phFriNfc_sNdefSmtCrdFmt_t .
 * \param[in] LowerDevice Overlapped HAL reference, pointing at a valid instance of this
 *            underlying component.
 * \param[in] psRemoteDevInfo Points to the Remote Device Information structure encapsulating
 *                            the information about the device (Smart card, NFC device) to access.
 * \param[in] psDevInputParam The Device input parameter, as used for the HAL POLL function.
 *                            This parameter is needed by the component in special cases, when an internal call
 *                            to POLL is required again, such as for FeliCa. The storage of the structure behind 
 *                            the pointer must be retained by the calling software. The component itself only
 *                            keeps the reference. No change is applied to the structure's content.
 * \param[in] ReceiveBuffer Pointer to a buffer that the component uses internally use to
 *            store the data received from the lower component.
 *            The size shall be at least \ref PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE .
 * \param[in] ReceiveLength The size of ReceiveBuffer. This specifies the actual length 
 *            of the data received from the lower component.
 *            The size shall be at least \ref PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE .
 * 
 * \retval NFCSTATUS_SUCCESS                Operation successful.
 * \retval NFCSTATUS_INVALID_PARAMETER      At least one parameter of the function is invalid.
 *
 * \note  This function has to be called at the beginning, after creating an instance of
 *        \ref phFriNfc_sNdefSmtCrdFmt_t .  Use this function to reset the instance and/or to switch
 *        to a different underlying card types.
 */
NFCSTATUS phFriNfc_NdefSmtCrd_Reset(phFriNfc_sNdefSmtCrdFmt_t       *NdefSmtCrdFmt,
                                    void                            *LowerDevice,
                                    phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
                                    phHal_sDevInputParam_t          *psDevInputParam,
                                    uint8_t                         *SendRecvBuffer,
                                    uint16_t                        *SendRecvBuffLen);
                                    


/*!
 * \ingroup grp_fri_smart_card_formatting
 *
 * \brief Setting of the Completion Routine.
 *
 * \copydoc page_ovr This function allows the caller to set a Completion Routine (notifier).
 *
 * \param[in] NdefSmtCrdFmt Pointer to a valid instance of the \ref phFriNfc_sNdefSmtCrdFmt_t structure describing
 *                    the component context.
 *
 * \param CompletionRoutine Pointer to a valid completion routine being called when the non-blocking
 *        operation has finished.
 *
 * \param CompletionRoutineParam Pointer to a location with user-defined information that is submitted
 *                               to the Completion Routine once it is called.

 * \retval NFCSTATUS_SUCCESS                Operation successful.
 * \retval NFCSTATUS_INVALID_PARAMETER      At least one parameter of the function is invalid.
 *
 */
NFCSTATUS phFriNfc_NdefSmtCrd_SetCR(phFriNfc_sNdefSmtCrdFmt_t     *NdefSmtCrdFmt,
                                    uint8_t                       FunctionID,
                                    pphFriNfc_Cr_t                CompletionRoutine,
                                    void                          *CompletionRoutineContext);


/*!
 * \ingroup grp_fri_smart_card_formatting
 *
 * \brief Initiates the card formatting procedure for Remote Smart Card Type.
 *
 * \copydoc page_ovr  The function initiates and formats the Smart Card.After this formation, remote
 * card would be properly initialized and Ndef Compliant.
 * Depending upon the different card type, this function handles formatting procedure.
 * This function also handles the different recovery procedures for different types of the cards. For both
 * Format and Recovery Management same API is used.
 * 
 * \param[in] phFriNfc_sNdefSmtCrdFmt_t Pointer to a valid instance of the \ref phFriNfc_sNdefSmartCardFmt_t
 *                             structure describing the component context.
 * \retval  NFCSTATUS_PENDING   The action has been successfully triggered.
 * \retval  Other values        An error has occurred.
 *
 */
NFCSTATUS phFriNfc_NdefSmtCrd_Format(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt, const uint8_t *ScrtKeyB);


#ifdef FRINFC_READONLY_NDEF
/*!
 * \ingroup grp_fri_smart_card_formatting
 *
 * \brief Initiates the conversion of the already NDEF formatted tag to READ ONLY.
 *
 * \copydoc page_ovr  The function initiates the conversion of the already NDEF formatted
 * tag to READ ONLY.After this formation, remote card would be properly Ndef Compliant and READ ONLY.
 * Depending upon the different card type, this function handles formatting procedure.
 * This function supports only for the DESFIRE, MIFARE UL and TOPAZ tags.
 *
 * \param[in] phFriNfc_sNdefSmtCrdFmt_t Pointer to a valid instance of the \ref phFriNfc_sNdefSmartCardFmt_t
 *                             structure describing the component context.
 * \retval  NFCSTATUS_PENDING   The action has been successfully triggered.
 * \retval  Other values        An error has occurred.
 *
 */
NFCSTATUS
phFriNfc_NdefSmtCrd_ConvertToReadOnly (
    phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);

#endif /* #ifdef FRINFC_READONLY_NDEF */


/**
 *\ingroup grp_fri_smart_card_formatting
 *
 * \brief Smart card Formatting \b Completion \b Routine or \b Process function
 *
 * \copydoc page_ovr Completion Routine: This function is called by the lower layer (OVR HAL)
 *                  when an I/O operation has finished. The internal state machine decides
 *                  whether to call into the lower device again or to complete the process
 *                  by calling into the upper layer's completion routine, stored within this
 *                  component's context (\ref phFriNfc_sNdefSmtCrdFmt_t).
 *
 * The function call scheme is according to \ref grp_interact. No State reset is performed during
 * operation.
 *
 * \param[in] Context The context of the current (not the lower/upper) instance, as set by the lower,
 *            calling layer, upon its completion.
 * \param[in] Status  The completion status of the lower layer (to be handled by the implementation of
 *                    the state machine of this function like a regular return value of an internally
 *                    called function).
 *
 * \note For general information about the completion routine interface please see \ref pphFriNfc_Cr_t . * The Different Status Values are as follows
 *
 */
void phFriNfc_NdefSmtCrd_Process(void        *Context,
                                 NFCSTATUS    Status);

void phFriNfc_SmtCrdFmt_HCrHandler(phFriNfc_sNdefSmtCrdFmt_t  *NdefSmtCrdFmt,
                                   NFCSTATUS            Status);

/*@}*/

#endif /* PHFRINFC_SMTCRDFMT_H */