Skip to content

Commit

Permalink
Swapping out previous __flash settings for commands lookup table into…
Browse files Browse the repository at this point in the history
… last half of LOG_FRAM (code and Makefile defines -- may still need to extract this new section using avr-objcopy -- pending testing)
  • Loading branch information
maxieds committed Feb 14, 2022
1 parent ac265b4 commit 138a174
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ This notice must be retained at the top of all source files where indicated.
#define DESFIRE_FIRMWARE_ARRAY_ALIGNAT __attribute__((aligned(1)))
#define DESFIRE_FIRMWARE_ENUM_PACKING __attribute__((aligned(1)))
#define DESFIRE_FIRMWARE_NOINIT __attribute__((section(".noinit")))
#define DESFIRE_DFFRAM_SECTION __attribute__((section(DESFIRE_DFFRAM_SECTION_NAME)))

/* Some standard boolean interpreted and other values for types and return values: */
typedef int BOOL;
Expand Down
60 changes: 7 additions & 53 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This notice must be retained at the top of all source files where indicated.
#include "../../Common.h"
#include "../../Random.h"

#include "DESFireFirmwareSettings.h"
#include "DESFireInstructions.h"
#include "DESFirePICCControl.h"
#include "DESFireCrypto.h"
Expand Down Expand Up @@ -118,245 +119,197 @@ static uint16_t ISO7816CmdAppendRecord(uint8_t *Buffer, uint16_t ByteCount);
* handlers to the end of the array, or insert them haphazardly in
* the middle !!!
*/
const __flash DESFireCommand DESFireCommandSet[] = {
const DESFIRE_DFFRAM_SECTION DESFIRE_FIRMWARE_ALIGNAT DESFireCommand DESFireCommandSet[] = {
{
.insCode = CMD_AUTHENTICATE,
.insDesc = (const __flash char[]) { "Authenicate_Legacy" },
.insFunc = &EV0CmdAuthenticateLegacy1
},
{
.insCode = CMD_CREDIT,
.insDesc = (const __flash char[]) { "Credit" },
.insFunc = &EV0CmdCredit
},
{
.insCode = CMD_AUTHENTICATE_ISO,
.insDesc = (const __flash char[]) { "Authenticate_ISO" },
.insFunc = &DesfireCmdAuthenticate3KTDEA1
},
{
.insCode = CMD_LIMITED_CREDIT,
.insDesc = (const __flash char[]) { "Limited_Credit" },
.insFunc = &EV0CmdLimitedCredit
},
{
.insCode = CMD_WRITE_RECORD,
.insDesc = (const __flash char[]) { "Write_Record" },
.insFunc = &EV0CmdWriteRecord
},
{
.insCode = CMD_WRITE_DATA,
.insDesc = (const __flash char[]) { "Write_Data" },
.insFunc = &EV0CmdWriteData
},
{
.insCode = CMD_GET_KEY_SETTINGS,
.insDesc = (const __flash char[]) { "Get_Key_Settings" },
.insFunc = &EV0CmdGetKeySettings
},
{
.insCode = CMD_GET_CARD_UID,
.insDesc = (const __flash char[]) { "Get_Card_UID" },
.insFunc = &DesfireCmdGetCardUID
},
{
.insCode = CMD_CHANGE_KEY_SETTINGS,
.insDesc = (const __flash char[]) { "Change_Key_Settings" },
.insFunc = &EV0CmdChangeKeySettings
},
{
.insCode = CMD_SELECT_APPLICATION,
.insDesc = (const __flash char[]) { "Select_Application" },
.insFunc = &EV0CmdSelectApplication
},
{
.insCode = CMD_SET_CONFIGURATION,
.insDesc = (const __flash char[]) { "Set_Configuration" },
.insFunc = NULL //&DesfireCmdSetConfiguration
.insFunc = NULL
},
{
.insCode = CMD_CHANGE_FILE_SETTINGS,
.insDesc = (const __flash char[]) { "Change_File_Settings" },
.insFunc = &EV0CmdChangeFileSettings
},
{
.insCode = CMD_GET_VERSION,
.insDesc = (const __flash char[]) { "Get_Version" },
.insFunc = &EV0CmdGetVersion1
},
{
.insCode = CMD_GET_ISO_FILE_IDS,
.insDesc = (const __flash char[]) { "Get_ISO_File_IDs" },
.insFunc = &EV0CmdGetFileIds
},
{
.insCode = CMD_GET_KEY_VERSION,
.insDesc = (const __flash char[]) { "Get_Key_Version" },
.insFunc = &DesfireCmdGetKeyVersion
},
{
.insCode = CMD_GET_APPLICATION_IDS,
.insDesc = (const __flash char[]) { "Get_Application_IDs" },
.insFunc = &EV0CmdGetApplicationIds1
},
{
.insCode = CMD_GET_VALUE,
.insDesc = (const __flash char[]) { "Get_Value" },
.insFunc = &EV0CmdGetValue
},
{
.insCode = CMD_GET_DF_NAMES,
.insDesc = (const __flash char[]) { "Get_DF_Names" },
.insFunc = &DesfireCmdGetDFNames
},
{
.insCode = CMD_FREE_MEMORY,
.insDesc = (const __flash char[]) { "Free_Memory" },
.insFunc = &DesfireCmdFreeMemory
},
{
.insCode = CMD_GET_FILE_IDS,
.insDesc = (const __flash char[]) { "Get_File_IDs" },
.insFunc = &EV0CmdGetFileIds
},
{
.insCode = CMD_AUTHENTICATE_EV2_FIRST,
.insDesc = (const __flash char[]) { "Authenticate_AES_EV2_First" },
.insFunc = NULL
},
{
.insCode = CMD_AUTHENTICATE_EV2_NONFIRST,
.insDesc = (const __flash char[]) { "Authenticate_AES_EV2_NonFirst" },
.insFunc = NULL
},
{
.insCode = CMD_ISO7816_EXTERNAL_AUTHENTICATE,
.insDesc = (const __flash char[]) { "ISO7816_External_Authenticate" },
.insFunc = &ISO7816CmdExternalAuthenticate
},
{
.insCode = CMD_ISO7816_GET_CHALLENGE,
.insDesc = (const __flash char[]) { "ISO7816_Get_Challenge" },
.insFunc = &ISO7816CmdGetChallenge
},
{
.insCode = CMD_ISO7816_SELECT,
.insDesc = (const __flash char[]) { "ISO7816_Select" },
.insFunc = &ISO7816CmdSelect
},
{
.insCode = CMD_ISO7816_INTERNAL_AUTHENTICATE,
.insDesc = (const __flash char[]) { "ISO7816_Internal_Authenticate" },
.insFunc = &ISO7816CmdInternalAuthenticate
},
{
.insCode = CMD_ABORT_TRANSACTION,
.insDesc = (const __flash char[]) { "Abort_Transaction" },
.insFunc = &EV0CmdAbortTransaction
},
{
.insCode = CMD_AUTHENTICATE_AES,
.insDesc = (const __flash char[]) { "Authenticate_AES" },
.insFunc = &DesfireCmdAuthenticateAES1
},
{
.insCode = CMD_ISO7816_READ_BINARY,
.insDesc = (const __flash char[]) { "ISO7816_Read_Binary" },
.insFunc = &ISO7816CmdReadBinary
},
{
.insCode = CMD_ISO7816_READ_RECORDS,
.insDesc = (const __flash char[]) { "ISO7816_Read_Records" },
.insFunc = &ISO7816CmdReadRecords
},
{
.insCode = CMD_READ_RECORDS,
.insDesc = (const __flash char[]) { "Read_Records" },
.insFunc = &EV0CmdReadRecords
},
{
.insCode = CMD_READ_DATA,
.insDesc = (const __flash char[]) { "Read_Data" },
.insFunc = &EV0CmdReadData
},
{
.insCode = CMD_CREATE_CYCLIC_RECORD_FILE,
.insDesc = (const __flash char[]) { "Create_Cyclic_Record_File" },
.insFunc = &EV0CmdCreateCyclicRecordFile
},
{
.insCode = CMD_CREATE_LINEAR_RECORD_FILE,
.insDesc = (const __flash char[]) { "Create_Linear_Record_File" },
.insFunc = &EV0CmdCreateLinearRecordFile
},
{
.insCode = CMD_CHANGE_KEY,
.insDesc = (const __flash char[]) { "Change_Key" },
.insFunc = &EV0CmdChangeKey
},
{
.insCode = CMD_CREATE_APPLICATION,
.insDesc = (const __flash char[]) { "Create_Application" },
.insFunc = &EV0CmdCreateApplication
},
{
.insCode = CMD_CREATE_BACKUPDATA_FILE,
.insDesc = (const __flash char[]) { "Create_Backup_File" },
.insFunc = &EV0CmdCreateBackupDataFile
},
{
.insCode = CMD_CREATE_VALUE_FILE,
.insDesc = (const __flash char[]) { "Create_Value_File" },
.insFunc = &EV0CmdCreateValueFile
},
{
.insCode = CMD_CREATE_STDDATA_FILE,
.insDesc = (const __flash char[]) { "Create_Data_File" },
.insFunc = &EV0CmdCreateStandardDataFile
},
{
.insCode = CMD_COMMIT_TRANSACTION,
.insDesc = (const __flash char[]) { "Commit_Transaction" },
.insFunc = &EV0CmdCommitTransaction
},
{
.insCode = CMD_ISO7816_UPDATE_BINARY,
.insDesc = (const __flash char[]) { "ISO7816_Update_Binary" },
.insFunc = &ISO7816CmdUpdateBinary
},
{
.insCode = CMD_DELETE_APPLICATION,
.insDesc = (const __flash char[]) { "Delete_Application" },
.insFunc = &EV0CmdDeleteApplication
},
{
.insCode = CMD_DEBIT,
.insDesc = (const __flash char[]) { "Debit" },
.insFunc = &EV0CmdDebit
},
{
.insCode = CMD_DELETE_FILE,
.insDesc = (const __flash char[]) { "Delete_File" },
.insFunc = &EV0CmdDeleteFile
},
{
.insCode = CMD_ISO7816_APPEND_RECORD,
.insDesc = (const __flash char[]) { "ISO7816_Append_Record" },
.insFunc = &ISO7816CmdAppendRecord
},
{
.insCode = CMD_CLEAR_RECORD_FILE,
.insDesc = (const __flash char[]) { "Clear_Record_File" },
.insFunc = &EV0CmdClearRecords
},
{
.insCode = CMD_FORMAT_PICC,
.insDesc = (const __flash char[]) { "Format_PICC" },
.insFunc = &EV0CmdFormatPicc
},
{
.insCode = CMD_GET_FILE_SETTINGS,
.insDesc = (const __flash char[]) { "Get_File_Settings" },
.insFunc = &EV0CmdGetFileSettings
},
};
Expand All @@ -367,14 +320,15 @@ uint16_t CallInstructionHandler(uint8_t *Buffer, uint16_t ByteCount) {
return DESFIRE_STATUS_RESPONSE_SIZE;
}
uint8_t insCode = Buffer[0];
uint32_t insLookupTableBuf = &DESFireCommandSet[0];
uint16_t curInsLower = 0, curInsUpper = sizeof(DESFireCommandSet) / sizeof(DESFireCommand) - 1;
uint16_t curInsIndex;
uint16_t nextDESFireFRAMBaseAddr = DESFIRE_DFFRAM_SECTION_START;
DESFireCommand dfCmd;
while (curInsUpper >= curInsLower) {
curInsIndex = curInsLower + (curInsUpper + 1 - curInsLower) / 2;
memcpy_P(&dfCmd, insLookupTableBuf + curInsIndex * sizeof(DESFireCommand), sizeof(DESFireCommand));
if (dfCmd.insCode == insCode) {
MemoryReadBlock(&dfCmd, nextDESFireFRAMBaseAddr, sizeof(DESFireCommand));
nextDESFireFRAMBaseAddr += sizeof(DESFireCommand);
if (dfCmd.insCode == insCode) {
if (dfCmd.insFunc == NULL) {
return CmdNotImplemented(Buffer, ByteCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ typedef uint16_t (*InsCodeHandlerFunc)(uint8_t *Buffer, uint16_t ByteCount);
typedef struct {
DESFireCommandType insCode;
InsCodeHandlerFunc insFunc;
const __flash char *insDesc;
} DESFireCommand;
} DESFireCommand DESFIRE_FIRMWARE_ALIGNAT;

extern const __flash DESFireCommand DESFireCommandSet[];
extern const DESFIRE_DFFRAM_SECTION DESFIRE_FIRMWARE_ALIGNAT DESFireCommand DESFireCommandSet[];

/* Helper and batch process functions */
uint16_t CallInstructionHandler(uint8_t *Buffer, uint16_t ByteCount);
Expand Down
13 changes: 12 additions & 1 deletion Firmware/Chameleon-Mini/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@
#include "Common.h"

#ifdef MEMORY_LIMITED_TESTING
#define LOG_SIZE 764 // 1536
#define LOG_SIZE 1024
#else
#define LOG_SIZE 2048
#endif
#ifdef DESFIRE_FRAM_LOG_START
/* This is separated so we can store the large DESFire (native) and ISO7816 command subset
* oragnized in a sorted lookup table by command (byte) ID in the FRAM memory space.
* It is too large to fit in .data, and storing it in the FLASH space causes collision problems
* with the memory settings data storage (and probably slower access times).
*/
#define FRAM_LOG_ADDR_ADDR DESFIRE_FRAM_LOG_START // start of the second half of FRAM
#define FRAM_LOG_START_ADDR (DESFIRE_FRAM_LOG_START + 0x002) // directly after the address
#define FRAM_LOG_SIZE (DESFIRE_FRAM_LOG_SIZE) // the whole second half (minus the 2 Bytes of Address)
#else
#define FRAM_LOG_ADDR_ADDR 0x4000 // start of the second half of FRAM
#define FRAM_LOG_START_ADDR 0x4002 // directly after the address
#define FRAM_LOG_SIZE 0x3FFE // the whole second half (minus the 2 Bytes of Address)
#endif

extern uint8_t LogMem[LOG_SIZE];
extern uint8_t *LogMemPtr;
Expand Down
16 changes: 12 additions & 4 deletions Firmware/Chameleon-Mini/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ SETTINGS += -DDESFIRE_MIN_OUTGOING_LOGSIZE=1

DESFIRE_MAINSRC = Application/DESFire

DESFIRE_CONFIG_SETTINGS_BASE = $(SETTINGS) -DCONFIG_MF_DESFIRE_SUPPORT -DMEMORY_LIMITED_TESTING -DDESFIRE_CRYPTO1_SAVE_SPACE -UDEFAULT_CONFIGURATION
DESFIRE_FRAM_LOG_START=0x4000
DESFIRE_FRAM_LOG_SIZE=0x2046
DESFIRE_DFFRAM_SECTION_START=0x6048
DESFIRE_DFFRAM_SECTION_NAME=\".dffram\"
DESFIRE_CONFIG_SETTINGS_BASE=$(SETTINGS) -DCONFIG_MF_DESFIRE_SUPPORT -DMEMORY_LIMITED_TESTING -DDESFIRE_CRYPTO1_SAVE_SPACE -UDEFAULT_CONFIGURATION \
-DDESFIRE_FRAM_LOG_START=$(DESFIRE_FRAM_LOG_START) -DDESFIRE_FRAM_LOG_SIZE=$(DESFIRE_FRAM_LOG_SIZE) \
-DDESFIRE_DFFRAM_SECTION_START=$(DESFIRE_DFFRAM_SECTION_START) \
-Wl,--section-start=$(DESFIRE_DFFRAM_SECTION_NAME)=$(DESFIRE_DFFRAM_SECTION_START) \
-DDESFIRE_DFFRAM_SECTION_NAME=$(DESFIRE_DFFRAM_SECTION_NAME)

#Memory definitions and objcopy flags to include sections in binaries
FLASH_DATA_ADDR = 0x10000 #Start of data section in flash
Expand Down Expand Up @@ -192,9 +200,9 @@ CC_FLAGS = -g0 -DUSE_LUFA_CONFIG_HEADER -DFLASH_DATA_ADDR=$(FLASH_DATA_ADDR)
-std=gnu99 -Werror=implicit-function-declaration \
-fno-inline-small-functions -fshort-enums -fpack-struct \
-ffunction-sections -fdata-sections -fvisibility=hidden \
-Wl,-strip-all -Wl,--gc-sections --data-sections \
-Wl,--gc-sections --data-sections \
-Wl,-relax -fno-split-wide-types -fno-tree-scev-cprop \
-fno-aggressive-loop-optimizations #-fno-jump-tables -fno-common
-fno-aggressive-loop-optimizations -fno-jump-tables -fno-common
LD_FLAGS = $(CC_FLAGS) -Wl,--section-start=.flashdata=$(FLASH_DATA_ADDR) -Wl,--section-start=.spmhelper=$(SPM_HELPER_ADDR)
OBJDIR = Bin
OBJECT_FILES =
Expand Down Expand Up @@ -312,5 +320,5 @@ desfire-build: local-clean $(TARGET).elf $(TARGET).hex $(TARGET).eep check_size
@avr-size -B -x $(TARGET).elf
@echo -e "\n"
@avr-size -C -x $(TARGET).elf
desfire: CONFIG_SETTINGS:=$(DESFIRE_CONFIG_SETTINGS_BASE) -DDEFAULT_CONFIGURATION=CONFIG_NONE
desfire: CONFIG_SETTINGS:=$(DESFIRE_CONFIG_SETTINGS_BASE) -DDEFAULT_CONFIGURATION=CONFIG_NONE -fno-inline-small-functions
desfire: desfire-build
2 changes: 0 additions & 2 deletions Firmware/Chameleon-Mini/Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,12 @@ void MemoryWriteBlockInSetting(const void *Buffer, uint16_t Address, uint16_t By

void MemoryClear(void) {
FlashErase((uint32_t) GlobalSettings.ActiveSettingIdx * MEMORY_SIZE_PER_SETTING, MEMORY_SIZE_PER_SETTING);

MemoryRecall();
}

void MemoryRecall(void) {
/* Recall memory from permanent flash */
FlashToFRAM((uint32_t) GlobalSettings.ActiveSettingIdx * MEMORY_SIZE_PER_SETTING, MEMORY_SIZE_PER_SETTING);

SystemTickClearFlag();
}

Expand Down

0 comments on commit 138a174

Please sign in to comment.