@@ -59,7 +59,7 @@ SIZET DESFIRE_INITIAL_FIRST_FREE_BLOCK_ID = 0;
59
59
SIZET DESFIRE_FIRST_FREE_BLOCK_ID = 0 ;
60
60
SIZET CardCapacityBlocks = 0 ;
61
61
62
- uint16_t DesfireATQAValue = DESFIRE_DEFAULT_ATQA_VALUE ;
62
+ bool DesfireATQAReset = false ;
63
63
64
64
void InitBlockSizes (void ) {
65
65
DESFIRE_PICC_INFO_BLOCK_ID = 0 ;
@@ -174,6 +174,7 @@ void InitialisePiccBackendEV0(uint8_t StorageSize, bool formatPICC) {
174
174
} else {
175
175
MemoryRestoreDesfireHeaderBytes (false);
176
176
ReadBlockBytes (& AppDir , DESFIRE_APP_DIR_BLOCK_ID , sizeof (DESFireAppDirType ));
177
+ DesfireATQAReset = true;
177
178
SelectPiccApp ();
178
179
}
179
180
}
@@ -193,6 +194,7 @@ void InitialisePiccBackendEV1(uint8_t StorageSize, bool formatPICC) {
193
194
} else {
194
195
MemoryRestoreDesfireHeaderBytes (false);
195
196
ReadBlockBytes (& AppDir , DESFIRE_APP_DIR_BLOCK_ID , sizeof (DESFireAppDirType ));
197
+ DesfireATQAReset = true;
196
198
SelectPiccApp ();
197
199
}
198
200
}
@@ -212,6 +214,7 @@ void InitialisePiccBackendEV2(uint8_t StorageSize, bool formatPICC) {
212
214
} else {
213
215
MemoryRestoreDesfireHeaderBytes (false);
214
216
ReadBlockBytes (& AppDir , DESFIRE_APP_DIR_BLOCK_ID , sizeof (DESFireAppDirType ));
217
+ DesfireATQAReset = true;
215
218
SelectPiccApp ();
216
219
}
217
220
@@ -255,13 +258,26 @@ void FormatPicc(void) {
255
258
memset (& AppDir , 0x00 , sizeof (DESFireAppDirType ));
256
259
memset (& SelectedApp , 0x00 , sizeof (SelectedAppCacheType ));
257
260
/* Set a random new UID */
258
- BYTE uidData [DESFIRE_UID_SIZE - 1 ];
259
- RandomGetBuffer (uidData , DESFIRE_UID_SIZE - 1 );
260
- memcpy (& Picc .Uid [1 ], uidData , DESFIRE_UID_SIZE - 1 );
261
- /* Conform to NXP Application Note AN10927 about the first
262
- * byte of a randomly generated UID (refer to section 2.1.1).
261
+ BYTE uidData [DESFIRE_UID_SIZE ];
262
+ RandomGetBuffer (uidData , DESFIRE_UID_SIZE );
263
+ memcpy (& Picc .Uid [0 ], uidData , DESFIRE_UID_SIZE );
264
+ if (Picc .Uid [0 ] == ISO14443A_UID0_RANDOM ) {
265
+ Picc .Uid [0 ] != 0x30 ;
266
+ }
267
+ /* OLD: Conform to NXP Application Note AN10927 about the first
268
+ * byte of a randomly generated UID (refer to section 2.1.1).
269
+ */
270
+ //Picc.Uid[0] = ISO14443A_UID0_RANDOM;
271
+ //uint16_t ATQAValue = DESFIRE_ATQA_RANDOM_UID;
272
+ /* NEW: NXP AN10927 (section 2.1.1, page 5) states that a random
273
+ * UID (RID) is always limited to 4 bytes. This limitation
274
+ * is avoided by just setting the whole buffer to a random
275
+ * value whose first byte is not 0x08.
263
276
*/
264
- Picc .Uid [0 ] = ISO14443A_UID0_RANDOM ;
277
+ uint16_t ATQAValue = DESFIRE_ATQA_DEFAULT ;
278
+ Picc .ATQA [0 ] = (uint8_t )((ATQAValue >> 8 ) & 0x00FF );
279
+ Picc .ATQA [1 ] = (uint8_t )(ATQAValue & 0x00FF );
280
+ DesfireATQAReset = false;
265
281
/* Randomize the initial batch number data: */
266
282
BYTE batchNumberData [5 ];
267
283
RandomGetBuffer (batchNumberData , 5 );
@@ -354,11 +370,18 @@ void FactoryFormatPiccEV2(uint8_t StorageSize) {
354
370
}
355
371
356
372
void GetPiccUid (ConfigurationUidType Uid ) {
357
- memcpy (Uid , Picc .Uid , DESFIRE_UID_SIZE + 1 );
373
+ memcpy (Uid , & Picc .Uid [ 0 ] , DESFIRE_UID_SIZE );
358
374
}
359
375
360
376
void SetPiccUid (ConfigurationUidType Uid ) {
361
377
memcpy (& Picc .Uid [0 ], Uid , DESFIRE_UID_SIZE );
378
+ DesfireATQAReset = true;
379
+ //if (!DesfireATQAReset) {
380
+ // uint16_t ATQAValue = DESFIRE_ATQA_DEFAULT;
381
+ // Picc.ATQA[0] = (uint8_t)((ATQAValue >> 8) & 0x00FF);
382
+ // Picc.ATQA[1] = (uint8_t)(ATQAValue & 0x00FF);
383
+ // DesfireATQAReset = true;
384
+ //}
362
385
SynchronizePICCInfo ();
363
386
}
364
387
0 commit comments