@@ -121,7 +121,7 @@ BYTE AMKAllKeysFrozen(void) {
121
121
}
122
122
123
123
SIZET GetAppProperty (DesfireCardLayout propId , BYTE AppSlot ) {
124
- if (AppSlot >= DESFIRE_MAX_SLOTS ) {
124
+ if (AppSlot >= AppDir . FirstFreeSlot || AppSlot >= DESFIRE_MAX_SLOTS ) {
125
125
return 0x00 ;
126
126
}
127
127
SelectedAppCacheType appCache ;
@@ -157,7 +157,7 @@ SIZET GetAppProperty(DesfireCardLayout propId, BYTE AppSlot) {
157
157
}
158
158
159
159
void SetAppProperty (DesfireCardLayout propId , BYTE AppSlot , SIZET Value ) {
160
- if (AppSlot >= DESFIRE_MAX_SLOTS ) {
160
+ if (AppSlot >= AppDir . FirstFreeSlot || AppSlot >= DESFIRE_MAX_SLOTS ) {
161
161
return ;
162
162
}
163
163
SelectedAppCacheType appCache ;
@@ -308,16 +308,6 @@ void ReadAppKey(uint8_t AppSlot, uint8_t KeyId, uint8_t *Key, SIZET KeySize) {
308
308
SIZET keyStorageArray [DESFIRE_MAX_KEYS ];
309
309
ReadBlockBytes (keyStorageArray , keyStorageArrayBlockId , 2 * DESFIRE_MAX_KEYS );
310
310
ReadBlockBytes (Key , keyStorageArray [KeyId ], KeySize );
311
- /*if (KeySize > DESFIRE_EEPROM_BLOCK_SIZE) {
312
- ReadBlockBytes(Key, keyStorageArray[KeyId], DESFIRE_EEPROM_BLOCK_SIZE);
313
- uint8_t fullBlock[DESFIRE_EEPROM_BLOCK_SIZE];
314
- ReadBlockBytes(fullBlock, keyStorageArray[KeyId] + 1, DESFIRE_EEPROM_BLOCK_SIZE);
315
- memcpy(Key + DESFIRE_EEPROM_BLOCK_SIZE, fullBlock, KeySize - DESFIRE_EEPROM_BLOCK_SIZE);
316
- } else {
317
- uint8_t fullBlock[DESFIRE_EEPROM_BLOCK_SIZE];
318
- ReadBlockBytes(fullBlock, keyStorageArray[KeyId], DESFIRE_EEPROM_BLOCK_SIZE);
319
- memcpy(Key, fullBlock, KeySize);
320
- }*/
321
311
}
322
312
323
313
void WriteAppKey (uint8_t AppSlot , uint8_t KeyId , const uint8_t * Key , SIZET KeySize ) {
@@ -329,7 +319,6 @@ void WriteAppKey(uint8_t AppSlot, uint8_t KeyId, const uint8_t *Key, SIZET KeySi
329
319
SIZET keyStorageArrayBlockId = ReadKeyStorageAddress (AppSlot );
330
320
SIZET keyStorageArray [DESFIRE_MAX_KEYS ];
331
321
ReadBlockBytes (keyStorageArray , keyStorageArrayBlockId , 2 * DESFIRE_MAX_KEYS );
332
- // TODO:
333
322
WriteBlockBytes (Key , keyStorageArray [KeyId ], KeySize );
334
323
}
335
324
@@ -519,8 +508,10 @@ void SelectAppBySlot(uint8_t AppSlot) {
519
508
if (appCacheSelectedBlockId == 0 ) {
520
509
return ;
521
510
}
522
- SIZET prevAppCacheSelectedBlockId = AppDir .AppCacheStructBlockOffset [SelectedApp .Slot ];
523
- WriteBlockBytes (& SelectedApp , prevAppCacheSelectedBlockId , sizeof (SelectedAppCacheType ));
511
+ if (SelectedApp .Slot != (uint8_t ) -1 ) {
512
+ SIZET prevAppCacheSelectedBlockId = AppDir .AppCacheStructBlockOffset [SelectedApp .Slot ];
513
+ WriteBlockBytes (& SelectedApp , prevAppCacheSelectedBlockId , sizeof (SelectedAppCacheType ));
514
+ }
524
515
ReadBlockBytes (& SelectedApp , appCacheSelectedBlockId , sizeof (SelectedAppCacheType ));
525
516
SelectedApp .Slot = AppSlot ;
526
517
SynchronizeAppDir ();
@@ -605,7 +596,7 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
605
596
appCacheData .KeyCount = 1 ; // Master Key
606
597
appCacheData .MaxKeyCount = KeyCount ;
607
598
appCacheData .FileCount = 0 ;
608
- appCacheData .CryptoCommStandard = DESFIRE_DEFAULT_COMMS_STANDARD ;
599
+ appCacheData .CryptoCommStandard = DesfireCommMode ;
609
600
appCacheData .KeySettings = AllocateBlocks (APP_CACHE_KEY_SETTINGS_ARRAY_BLOCK_SIZE );
610
601
if (appCacheData .KeySettings == 0 ) {
611
602
return STATUS_OUT_OF_EEPROM_ERROR ;
@@ -636,7 +627,9 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
636
627
return STATUS_OUT_OF_EEPROM_ERROR ;
637
628
} else {
638
629
SIZET fileAccessRightsData [DESFIRE_MAX_FILES ];
639
- memset (fileAccessRightsData , 0x0f , sizeof (SIZET ) * DESFIRE_MAX_FILES );
630
+ for (int fidx = 0 ; fidx < DESFIRE_MAX_FILES ; fidx ++ ) {
631
+ fileAccessRightsData [fidx ] = 0x000f ;
632
+ }
640
633
WriteBlockBytes (fileAccessRightsData , appCacheData .FileAccessRights , sizeof (SIZET ) * DESFIRE_MAX_FILES );
641
634
}
642
635
appCacheData .KeyVersionsArray = AllocateBlocks (APP_CACHE_KEY_VERSIONS_ARRAY_BLOCK_SIZE );
@@ -651,9 +644,9 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
651
644
if (appCacheData .KeyTypesArray == 0 ) {
652
645
return STATUS_OUT_OF_EEPROM_ERROR ;
653
646
} else {
654
- BYTE keyTypesData [APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE ];
655
- memset (keyTypesData , 0x00 , APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE );
656
- WriteBlockBytes (keyTypesData , appCacheData .KeyTypesArray , APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_EEPROM_BLOCK_SIZE );
647
+ BYTE keyTypesData [APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE ];
648
+ memset (keyTypesData , 0x00 , APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE );
649
+ WriteBlockBytes (keyTypesData , appCacheData .KeyTypesArray , APP_CACHE_KEY_TYPES_ARRAY_BLOCK_SIZE * DESFIRE_BLOCK_SIZE );
657
650
}
658
651
appCacheData .FilesAddress = AllocateBlocks (APP_CACHE_FILE_BLOCKIDS_ARRAY_BLOCK_SIZE );
659
652
if (appCacheData .FilesAddress == 0 ) {
@@ -676,8 +669,6 @@ uint16_t CreateApp(const DESFireAidType Aid, uint8_t KeyCount, uint8_t KeySettin
676
669
}
677
670
BYTE cryptoBlankKeyData [CRYPTO_MAX_KEY_SIZE ];
678
671
memset (cryptoBlankKeyData , 0x00 , CRYPTO_MAX_KEY_SIZE );
679
- //WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0], DESFIRE_EEPROM_BLOCK_SIZE);
680
- //WriteBlockBytes(cryptoBlankKeyData, keyAddresses[0] + 1, CRYPTO_MAX_KEY_SIZE - DESFIRE_EEPROM_BLOCK_SIZE);
681
672
WriteBlockBytes (cryptoBlankKeyData , keyAddresses [0 ], CRYPTO_MAX_KEY_SIZE );
682
673
WriteBlockBytes (keyAddresses , appCacheData .KeyAddress , sizeof (SIZET ) * DESFIRE_MAX_KEYS );
683
674
}
0 commit comments