From bc3ae6b7688aca881a4b1e61e01f86c9e316d600 Mon Sep 17 00:00:00 2001 From: Federico Cerutti Date: Wed, 14 Oct 2020 19:47:38 +0200 Subject: [PATCH 1/4] Elf sections check before flash Due to the addition of new codecs and applications, code size grew to overwrite the part of FRAM currently used to store card slots (settings). This simple (although hacky) makefile command verifies if such an issue will occur before flashing the firmware. --- Firmware/Chameleon-Mini/Makefile | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Firmware/Chameleon-Mini/Makefile b/Firmware/Chameleon-Mini/Makefile index f67fa3b..0bdbcbf 100644 --- a/Firmware/Chameleon-Mini/Makefile +++ b/Firmware/Chameleon-Mini/Makefile @@ -134,6 +134,7 @@ AVRDUDE_FLAGS = -p $(AVRDUDE_MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) AVRDUDE_WRITE_APP_LATEST = -U application:w:Latest/Chameleon-RevG.hex AVRDUDE_WRITE_EEPROM_LATEST = -U eeprom:w:Latest/Chameleon-RevG.eep +.PHONY: program program-latest dfu-flip dfu-prog check_size style # Default target all: @@ -165,27 +166,46 @@ spmhelper: $(TARGET).elf $(CROSS)-objcopy -O ihex -j .spmhelper $(SPM_HELPER_OBJCOPY) $(TARGET).elf $(TARGET).hex # Program the device using avrdude -program: $(TARGET).hex $(TARGET).eep +program: $(TARGET).hex $(TARGET).eep check_size avrdude $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_APP) $(AVRDUDE_WRITE_EEPROM) # Program the device using avrdude with the latest official firmware -program-latest: +program-latest: check_size avrdude $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_APP_LATEST) $(AVRDUDE_WRITE_EEPROM_LATEST) # Program the device using batchisp and the DFU bootloader # Note that the device has to be in bootloader mode already -dfu-flip: $(TARGET).hex $(TARGET).eep +dfu-flip: $(TARGET).hex $(TARGET).eep check_size cp $(TARGET).eep EEPROM.hex batchisp -hardware usb -device $(MCU) -operation erase f memory FLASH loadbuffer $(TARGET).hex program verify memory EEPROM loadbuffer EEPROM.hex program verify start reset 0 rm EEPROM.hex # Program the device using dfu-programmer -dfu-prog: $(TARGET).hex $(TARGET).eep +dfu-prog: $(TARGET).hex $(TARGET).eep check_size dfu-programmer $(MCU) erase dfu-programmer $(MCU) flash-eeprom $(TARGET).eep dfu-programmer $(MCU) flash $(TARGET).hex dfu-programmer $(MCU) reset +check_size: +ifeq ($(OS),Windows_NT) +# TODO Windows version of this check + @PROGMEM_SIZE = 0 +else + @{ \ + set -e; \ + if [ ! -f $(TARGET).elf ]; then \ + exit 0; \ + fi; \ + PROGMEM_SIZE=$$(avr-size $(TARGET).elf | grep -oP "\d+" | sed -n 4p); \ + MAX_PRGMEM_SIZE=$$(printf "%d\n" $(FLASH_DATA_ADDR)); \ + if [ $$PROGMEM_SIZE -gt $$MAX_PRGMEM_SIZE ]; then \ + echo "make: *** $(TARGET).elf Application Section size $$PROGMEM_SIZE excedes maximum allowed $$MAX_PRGMEM_SIZE. Please disable some features in Makefile"; \ + exit 1; \ + fi; \ + } +endif + style: # Make sure astyle is installed @which astyle >/dev/null || ( echo "Please install 'astyle' package first" ; exit 1 ) From 9c135cd1e49912858477eed1c3a382d7d5842f05 Mon Sep 17 00:00:00 2001 From: Federico Cerutti Date: Wed, 14 Oct 2020 20:22:46 +0200 Subject: [PATCH 2/4] Disabled sniff/reader codecs compilation Had to make some minor changes to Commands.c and CommandLine.c to allow building without these codecs --- Firmware/Chameleon-Mini/Makefile | 14 +++++++------- Firmware/Chameleon-Mini/Terminal/CommandLine.c | 4 ++++ Firmware/Chameleon-Mini/Terminal/Commands.c | 15 +++++++++++---- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Firmware/Chameleon-Mini/Makefile b/Firmware/Chameleon-Mini/Makefile index 0bdbcbf..e97f7ab 100644 --- a/Firmware/Chameleon-Mini/Makefile +++ b/Firmware/Chameleon-Mini/Makefile @@ -15,13 +15,13 @@ SETTINGS += -DCONFIG_MF_CLASSIC_1K_7B_SUPPORT SETTINGS += -DCONFIG_MF_CLASSIC_4K_SUPPORT SETTINGS += -DCONFIG_MF_CLASSIC_4K_7B_SUPPORT SETTINGS += -DCONFIG_MF_ULTRALIGHT_SUPPORT -SETTINGS += -DCONFIG_ISO14443A_SNIFF_SUPPORT -SETTINGS += -DCONFIG_ISO14443A_READER_SUPPORT -SETTINGS += -DCONFIG_VICINITY_SUPPORT -SETTINGS += -DCONFIG_SL2S2002_SUPPORT -SETTINGS += -DCONFIG_TITAGITSTANDARD_SUPPORT -SETTINGS += -DCONFIG_ISO15693_SNIFF_SUPPORT -SETTINGS += -DCONFIG_EM4233_SUPPORT +# SETTINGS += -DCONFIG_ISO14443A_SNIFF_SUPPORT +# SETTINGS += -DCONFIG_ISO14443A_READER_SUPPORT +SETTINGS += -DCONFIG_VICINITY_SUPPORT +SETTINGS += -DCONFIG_SL2S2002_SUPPORT +SETTINGS += -DCONFIG_TITAGITSTANDARD_SUPPORT +# SETTINGS += -DCONFIG_ISO15693_SNIFF_SUPPORT +SETTINGS += -DCONFIG_EM4233_SUPPORT #Support magic mode on mifare classic configuration SETTINGS += -DSUPPORT_MF_CLASSIC_MAGIC_MODE diff --git a/Firmware/Chameleon-Mini/Terminal/CommandLine.c b/Firmware/Chameleon-Mini/Terminal/CommandLine.c index c6f7026..0e6e101 100644 --- a/Firmware/Chameleon-Mini/Terminal/CommandLine.c +++ b/Firmware/Chameleon-Mini/Terminal/CommandLine.c @@ -260,6 +260,7 @@ const PROGMEM CommandEntryType CommandTable[] = { .SetFunc = NO_FUNCTION, .GetFunc = CommandGetSysTick }, +#ifdef CONFIG_ISO14443A_READER_SUPPORT { .Command = COMMAND_SEND_RAW, .ExecFunc = NO_FUNCTION, @@ -302,6 +303,7 @@ const PROGMEM CommandEntryType CommandTable[] = { .SetFunc = NO_FUNCTION, .GetFunc = NO_FUNCTION }, +#endif { .Command = COMMAND_TIMEOUT, .ExecFunc = NO_FUNCTION, @@ -330,6 +332,7 @@ const PROGMEM CommandEntryType CommandTable[] = { .SetFunc = CommandSetField, .GetFunc = CommandGetField }, +#ifdef CONFIG_ISO14443A_READER_SUPPORT { .Command = COMMAND_CLONE, .ExecFunc = CommandExecClone, @@ -337,6 +340,7 @@ const PROGMEM CommandEntryType CommandTable[] = { .SetFunc = NO_FUNCTION, .GetFunc = NO_FUNCTION }, +#endif { .Command = COMMAND_SETUIDMODE, .ExecFunc = NO_FUNCTION, diff --git a/Firmware/Chameleon-Mini/Terminal/Commands.c b/Firmware/Chameleon-Mini/Terminal/Commands.c index b9dd458..3ace97b 100644 --- a/Firmware/Chameleon-Mini/Terminal/Commands.c +++ b/Firmware/Chameleon-Mini/Terminal/Commands.c @@ -458,6 +458,7 @@ CommandStatusIdType CommandGetSysTick(char *OutParam) { return COMMAND_INFO_OK_WITH_TEXT_ID; } +#ifdef CONFIG_ISO14443A_READER_SUPPORT CommandStatusIdType CommandExecParamSend(char *OutMessage, const char *InParams) { if (GlobalSettings.ActiveSettingPtr->Configuration != CONFIG_ISO14443A_READER) return COMMAND_ERR_INVALID_USAGE_ID; @@ -583,6 +584,7 @@ CommandStatusIdType CommandExecIdentifyCard(char *OutMessage) { CommandLinePendingTaskTimeout = &Reader14443AAppTimeout; return TIMEOUT_COMMAND; } +#endif CommandStatusIdType CommandGetTimeout(char *OutParam) { snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("%u ms"), GlobalSettings.ActiveSettingPtr->PendingTaskTimeout * 100); @@ -647,6 +649,7 @@ CommandStatusIdType CommandGetField(char *OutMessage) { CommandStatusIdType CommandExecAutocalibrate(char *OutMessage) { +#ifdef CONFIG_ISO14443A_READER_SUPPORT if (GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_ISO14443A_READER) { ApplicationReset(); @@ -655,19 +658,22 @@ CommandStatusIdType CommandExecAutocalibrate(char *OutMessage) { Reader14443ACodecStart(); CommandLinePendingTaskTimeout = &Reader14443AAppTimeout; return TIMEOUT_COMMAND; - } else if (GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_ISO14443A_SNIFF) { + } +#endif +#ifdef CONFIG_ISO14443A_SNIFF_SUPPORT + if (GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_ISO14443A_SNIFF) { ApplicationReset(); Sniff14443CurrentCommand = Sniff14443_Autocalibrate; Sniff14443AAppInit(); CommandLinePendingTaskTimeout = &Sniff14443AAppTimeout; return TIMEOUT_COMMAND; - } else { - return COMMAND_ERR_INVALID_USAGE_ID; } - +#endif + return COMMAND_ERR_INVALID_USAGE_ID; } +#ifdef CONFIG_ISO14443A_READER_SUPPORT CommandStatusIdType CommandExecClone(char *OutMessage) { ConfigurationSetById(CONFIG_ISO14443A_READER); @@ -680,6 +686,7 @@ CommandStatusIdType CommandExecClone(char *OutMessage) { return TIMEOUT_COMMAND; } +#endif extern uint32_t dwBaudRate; CommandStatusIdType CommandGetBaudrate(char *OutParam) { From 5ef496816d93b30a46e65ce57efb7ff0abecadd5 Mon Sep 17 00:00:00 2001 From: Federico Cerutti Date: Thu, 15 Oct 2020 00:06:16 +0200 Subject: [PATCH 3/4] Working in windows under cygwin --- Firmware/Chameleon-Mini/Makefile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Firmware/Chameleon-Mini/Makefile b/Firmware/Chameleon-Mini/Makefile index e97f7ab..cc23ac0 100644 --- a/Firmware/Chameleon-Mini/Makefile +++ b/Firmware/Chameleon-Mini/Makefile @@ -188,10 +188,6 @@ dfu-prog: $(TARGET).hex $(TARGET).eep check_size dfu-programmer $(MCU) reset check_size: -ifeq ($(OS),Windows_NT) -# TODO Windows version of this check - @PROGMEM_SIZE = 0 -else @{ \ set -e; \ if [ ! -f $(TARGET).elf ]; then \ @@ -204,7 +200,6 @@ else exit 1; \ fi; \ } -endif style: # Make sure astyle is installed From 10ac19d698c00eb8797bbf7bd6bda08730488cbe Mon Sep 17 00:00:00 2001 From: Federico Cerutti Date: Thu, 15 Oct 2020 21:02:24 +0200 Subject: [PATCH 4/4] Additional reader mode ifdefs --- Firmware/Chameleon-Mini/Configuration.c | 8 ++++++++ Firmware/Chameleon-Mini/Memory.c | 9 ++++++++- Firmware/Chameleon-Mini/Settings.c | 7 ++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Firmware/Chameleon-Mini/Configuration.c b/Firmware/Chameleon-Mini/Configuration.c index 7c30348..b0e855e 100644 --- a/Firmware/Chameleon-Mini/Configuration.c +++ b/Firmware/Chameleon-Mini/Configuration.c @@ -505,10 +505,14 @@ void ConfigurationSetById(ConfigurationEnum Configuration) { &ConfigurationTable[Configuration], sizeof(ConfigurationType)); // Configure antenna load as appropriate +#ifdef CONFIG_ISO14443A_READER_SUPPORT if (Configuration == CONFIG_ISO14443A_READER) PORTC.OUTCLR = PIN7_bm; else PORTC.OUTSET = PIN7_bm; +#else + PORTC.OUTSET = PIN7_bm; +#endif CodecInit(); ApplicationInit(); @@ -523,7 +527,11 @@ bool ConfigurationSetByName(const char *Configuration) { if (MapTextToId(ConfigurationMap, ARRAY_COUNT(ConfigurationMap), Configuration, &Id)) { // The last configuration can only be configured as a reader +#ifdef CONFIG_ISO14443A_READER_SUPPORT if (GlobalSettings.ActiveSettingIdx >= SETTINGS_COUNT && Id != CONFIG_ISO14443A_READER) { +#else + if (GlobalSettings.ActiveSettingIdx >= SETTINGS_COUNT) { +#endif return false; } ConfigurationSetById(Id); diff --git a/Firmware/Chameleon-Mini/Memory.c b/Firmware/Chameleon-Mini/Memory.c index 3ea60ab..c950e83 100644 --- a/Firmware/Chameleon-Mini/Memory.c +++ b/Firmware/Chameleon-Mini/Memory.c @@ -172,10 +172,15 @@ INLINE void FRAMWrite(const void *Buffer, uint16_t Address, uint16_t ByteCount) SPIWriteBlock(Buffer, ByteCount); FRAM_PORT.OUTSET = FRAM_CS; - +#ifdef CONFIG_ISO14443A_READER_SUPPORT if (0 == Address && GlobalSettings.ActiveSettingPtr->Configuration != CONFIG_ISO14443A_READER) { ConfigurationSetById(GlobalSettings.ActiveSettingPtr->Configuration); } +#else + if (0 == Address) { + ConfigurationSetById(GlobalSettings.ActiveSettingPtr->Configuration); + } +#endif } INLINE void FlashRead(void *Buffer, uint32_t Address, uint16_t ByteCount) { @@ -421,8 +426,10 @@ void MemoryRecall(void) { if (GlobalSettings.ActiveSettingIdx < SETTINGS_COUNT) FlashToFRAM((uint32_t) GlobalSettings.ActiveSettingIdx * MEMORY_SIZE_PER_SETTING, ActiveConfiguration.MemorySize); +#ifdef CONFIG_ISO14443A_READER_SUPPORT if (GlobalSettings.ActiveSettingPtr->Configuration != CONFIG_ISO14443A_READER) ActiveConfiguration.ApplicationInitFunc(); +#endif SystemTickClearFlag(); } diff --git a/Firmware/Chameleon-Mini/Settings.c b/Firmware/Chameleon-Mini/Settings.c index 290806c..dc557bc 100644 --- a/Firmware/Chameleon-Mini/Settings.c +++ b/Firmware/Chameleon-Mini/Settings.c @@ -39,7 +39,9 @@ SettingsType EEMEM StoredSettings = { .PendingTaskTimeout = DEFAULT_PENDING_TASK_TIMEOUT, .ReaderThreshold = DEFAULT_READER_THRESHOLD, .bSakMode = 0, - }, + } +#ifdef CONFIG_ISO14443A_READER_SUPPORT + , // In the last configuration, there is no storage space. This is used as the card reader mode only [SETTINGS_COUNT] = { @@ -55,6 +57,7 @@ SettingsType EEMEM StoredSettings = { .ReaderThreshold = DEFAULT_READER_THRESHOLD, .bSakMode = 0, } +#endif } }; @@ -77,7 +80,9 @@ void SettingsLoad(void) { GlobalSettings.Settings[i].PendingTaskTimeout = DEFAULT_PENDING_TASK_TIMEOUT; GlobalSettings.Settings[i].ReaderThreshold = DEFAULT_READER_THRESHOLD; } +#ifdef CONFIG_ISO14443A_READER_SUPPORT GlobalSettings.Settings[SETTINGS_COUNT].Configuration = CONFIG_ISO14443A_READER; +#endif SettingsSave(); }