Skip to content

Commit

Permalink
Stashing changes to DESFire code -- No PSTR wrappers on command names…
Browse files Browse the repository at this point in the history
… as this doesn't change the ELF application size
  • Loading branch information
maxieds committed Jul 1, 2022
1 parent 807d4ac commit 93e77a2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 203 deletions.
8 changes: 0 additions & 8 deletions Doc/DESFireSupportReadme.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,6 @@ DESFire emulation if things suddenly fail after a call to this terminal command.
Putting the Chameleon through a full power recycle (battery off) should reset the setting
to the defaults.

#### DF_LOGMODE -- Sets the depth of (LIVE) logging messages printed at runtime

Syntax -- not guaranteeing that all of these are meaningful or distinct just yet:
```bash
DF_LOGMODE=0
DF_LOGMODE=1
```

## Supported functionality

### Tables of tested support for active commands
Expand Down
126 changes: 6 additions & 120 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireChameleonTerminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,23 @@ This notice must be retained at the top of all source files where indicated.
#include "DESFireLogging.h"

bool IsDESFireConfiguration(void) {
return GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_MF_DESFIRE;
}

CommandStatusIdType ExitOnInvalidConfigurationError(char *OutParam) {
if (OutParam != NULL) {
sprintf_P(OutParam, PSTR("Invalid Configuration: Set `CONFIG=MF_DESFIRE`.\r\n"));
}
return COMMAND_ERR_INVALID_USAGE_ID;
return GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_MF_DESFIRE ||
GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_MF_DESFIRE_2KEV1 ||
GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_MF_DESFIRE_4KEV1 ||
GlobalSettings.ActiveSettingPtr->Configuration == CONFIG_MF_DESFIRE_4KEV2;
}

#ifndef DISABLE_PERMISSIVE_DESFIRE_SETTINGS
CommandStatusIdType CommandDESFireGetHeaderProperty(char *OutParam) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE,
PSTR("%s <ATS(N=5)|ATQA(N=4)|HardwareVersion(N=2)|SoftwareVersion(N=2)|BatchNumber(N=5)|ProductionDate(N=2)> <N-HexDataBytes>"),
DFCOMMAND_SET_HEADER);
return COMMAND_INFO_OK_WITH_TEXT_ID;
}

CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutParam, const char *InParams) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
return COMMAND_ERR_INVALID_USAGE_ID;
}
char hdrPropSpecStr[24];
char propSpecBytesStr[16];
BYTE propSpecBytes[16];
SIZET dataByteCount = 0x00;
BYTE StatusError = 0x00;
if (!sscanf_P(InParams, PSTR("%24s %12s"), hdrPropSpecStr, propSpecBytesStr)) {
CommandDESFireGetHeaderProperty(OutParam);
return COMMAND_ERR_INVALID_PARAM_ID;
}
hdrPropSpecStr[23] = propSpecBytesStr[15] = '\0';
Expand Down Expand Up @@ -119,117 +107,16 @@ CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutParam, const char *
StatusError = 1;
}
if (StatusError) {
CommandDESFireGetHeaderProperty(OutParam);
return COMMAND_ERR_INVALID_USAGE_ID;
}
SynchronizePICCInfo();
return COMMAND_INFO_OK_ID;
}
#endif /* DISABLE_PERMISSIVE_DESFIRE_SETTINGS */

CommandStatusIdType CommandDESFireGetLoggingMode(char *OutParam) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
}
switch (LocalLoggingMode) {
case OFF:
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("OFF"));
break;
case NORMAL:
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("NORMAL"));
break;
case VERBOSE:
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("VERBOSE"));
break;
case DEBUGGING:
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("DEBUGGING"));
break;
default:
break;
}
return COMMAND_INFO_OK_WITH_TEXT_ID;
}

CommandStatusIdType CommandDESFireSetLoggingMode(char *OutParam, const char *InParams) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
}
char valueStr[16];
if (!sscanf_P(InParams, PSTR("%15s"), valueStr)) {
return COMMAND_ERR_INVALID_PARAM_ID;
}
valueStr[15] = '\0';
if (!strcasecmp_P(valueStr, PSTR("1")) || !strcasecmp_P(valueStr, PSTR("TRUE")) ||
!strcasecmp_P(valueStr, PSTR("ON"))) {
LocalLoggingMode = NORMAL;
return COMMAND_INFO_OK_ID;
} else if (!strcasecmp_P(valueStr, PSTR("0")) || !strcasecmp_P(valueStr, PSTR("FALSE")) ||
!strcasecmp_P(valueStr, PSTR("OFF"))) {
LocalLoggingMode = OFF;
return COMMAND_INFO_OK_ID;
} else if (!strcasecmp_P(valueStr, PSTR("VERBOSE"))) {
LocalLoggingMode = VERBOSE;
return COMMAND_INFO_OK_ID;
} else if (!strcasecmp_P(valueStr, PSTR("DEBUGGING"))) {
LocalLoggingMode = DEBUGGING;
return COMMAND_INFO_OK_ID;
} else {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("%s <ON|OFF|VERBOSE|DEBUGGING>"),
DFCOMMAND_LOGGING_MODE);
return COMMAND_ERR_INVALID_USAGE_ID;
}
}

CommandStatusIdType CommandDESFireGetTestingMode(char *OutParam) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
} else if (LocalTestingMode) {
return COMMAND_INFO_TRUE_ID;
}
return COMMAND_INFO_FALSE_ID;
}

CommandStatusIdType CommandDESFireSetTestingMode(char *OutParam, const char *InParams) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
}
char valueStr[16];
if (!sscanf_P(InParams, PSTR("%15s"), valueStr)) {
return COMMAND_ERR_INVALID_PARAM_ID;
}
valueStr[15] = '\0';
if (!strcasecmp_P(valueStr, PSTR("1")) || !strcasecmp_P(valueStr, PSTR("TRUE")) ||
!strcasecmp_P(valueStr, PSTR("ON"))) {
LocalTestingMode = 0x01;
return COMMAND_INFO_TRUE_ID;
} else if (!strcasecmp_P(valueStr, PSTR("0")) || !strcasecmp_P(valueStr, PSTR("FALSE")) ||
!strcasecmp_P(valueStr, PSTR("OFF"))) {
LocalTestingMode = 0x00;
return COMMAND_INFO_FALSE_ID;
}
return COMMAND_ERR_INVALID_USAGE_ID;
}

CommandStatusIdType CommandDESFireGetCommMode(char *OutParam) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
} else if (DesfireCommMode == DESFIRE_COMMS_PLAINTEXT) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Plaintext"));
} else if (DesfireCommMode == DESFIRE_COMMS_PLAINTEXT_MAC) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Plaintext/MAC"));
} else if (DesfireCommMode == DESFIRE_COMMS_CIPHERTEXT_DES) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Enciphered/DES"));
} else if (DesfireCommMode == DESFIRE_COMMS_CIPHERTEXT_AES128) {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Enciphered/AES128"));
} else {
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Unknown"));
}
return COMMAND_INFO_OK_WITH_TEXT_ID;
}

CommandStatusIdType CommandDESFireSetCommMode(char *OutParam, const char *InParams) {
if (!IsDESFireConfiguration()) {
ExitOnInvalidConfigurationError(OutParam);
return COMMAND_ERR_INVALID_USAGE_ID;
}
char valueStr[16];
if (!sscanf_P(InParams, PSTR("%15s"), valueStr)) {
Expand All @@ -253,7 +140,6 @@ CommandStatusIdType CommandDESFireSetCommMode(char *OutParam, const char *InPara
DesfireCommandState.ActiveCommMode = DesfireCommMode;
return COMMAND_INFO_OK;
}
snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("Options are: Plaintext|Plaintext:MAC|Enciphered:3K3DES|Enciphered:AES128"));
return COMMAND_ERR_INVALID_USAGE_ID;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,12 @@ bool IsDESFireConfiguration(void);

#ifndef DISABLE_PERMISSIVE_DESFIRE_SETTINGS
#define DFCOMMAND_SET_HEADER "DF_SETHDR"
CommandStatusIdType CommandDESFireGetHeaderProperty(char *OutParam);
CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutMessage, const char *InParams);
#endif
#endif /* DISABLE_PERMISSIVE_DESFIRE_SETTINGS */

#define DFCOMMAND_LOGGING_MODE "DF_LOGMODE"
CommandStatusIdType CommandDESFireGetLoggingMode(char *OutParam);
CommandStatusIdType CommandDESFireSetLoggingMode(char *OutMessage, const char *InParams);

#define DFCOMMAND_TESTING_MODE "DF_TESTMODE"
CommandStatusIdType CommandDESFireGetTestingMode(char *OutParam);
CommandStatusIdType CommandDESFireSetTestingMode(char *OutMessage, const char *InParams);

#define DFCOMMAND_COMM_MODE "DF_COMM_MODE"
CommandStatusIdType CommandDESFireGetCommMode(char *OutParam);
#define DFCOMMAND_COMM_MODE "DF_COMM_MODE"
CommandStatusIdType CommandDESFireSetCommMode(char *OutMessage, const char *InParams);

#endif
#endif /* DESFire Support */

#endif
#endif /* __DESFIRE_CHAMELEON_TERMINAL_H__ */
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,13 @@ This notice must be retained at the top of all source files where indicated.
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = CommandDESFireSetHeaderProperty,
.GetFunc = CommandDESFireGetHeaderProperty
}, {
.Command = DFCOMMAND_LOGGING_MODE,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = CommandDESFireSetLoggingMode,
.GetFunc = CommandDESFireGetLoggingMode
}, {
.Command = DFCOMMAND_TESTING_MODE,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = CommandDESFireSetTestingMode,
.GetFunc = CommandDESFireGetTestingMode
.GetFunc = NO_FUNCTION
}, {
.Command = DFCOMMAND_COMM_MODE,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = CommandDESFireSetCommMode,
.GetFunc = CommandDESFireGetCommMode
.GetFunc = NO_FUNCTION
},

#endif
Expand Down
7 changes: 3 additions & 4 deletions Firmware/Chameleon-Mini/Application/Reader14443A.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if defined(CONFIG_ISO14443A_READER_SUPPORT) || \
defined(CONFIG_ISO14443A_SNIFF_SUPPORT)
#if defined(CONFIG_ISO14443A_READER_SUPPORT) || defined(CONFIG_ISO14443A_SNIFF_SUPPORT)

#include "Reader14443A.h"
#include "LEDHook.h"
Expand Down Expand Up @@ -99,8 +98,8 @@ static const CardIdentificationType PROGMEM CardIdentificationList[] = {
[CardType_NXP_MIFARE_Classic_1k] = { .ATQA = 0x0004, .ATQARelevant = true, .SAK = 0x08, .SAKRelevant = true, .ATSRelevant = false, .Manufacturer = "NXP", .Type = "MIFARE Classic 1k" },
[CardType_NXP_MIFARE_Classic_4k] = { .ATQA = 0x0002, .ATQARelevant = true, .SAK = 0x18, .SAKRelevant = true, .ATSRelevant = false, .Manufacturer = "NXP", .Type = "MIFARE Classic 4k" },
[CardType_NXP_MIFARE_Ultralight] = { .ATQA = 0x0044, .ATQARelevant = true, .SAK = 0x00, .SAKRelevant = true, .ATSRelevant = false, .Manufacturer = "NXP", .Type = "MIFARE Ultralight" },
// [CardType_NXP_MIFARE_Ultralight_C] = { .ATQA=0x0044, .ATQARelevant=true, .SAK=0x00, .SAKRelevant=true, .ATSRelevant=false, .Manufacturer="NXP", .Type="MIFARE Ultralight C" },
// [CardType_NXP_MIFARE_Ultralight_EV1] = { .ATQA=0x0044, .ATQARelevant=true, .SAK=0x00, .SAKRelevant=false, .ATSRelevant=false, .Manufacturer="NXP", .Type="MIFARE Ultralight EV1" },
//[CardType_NXP_MIFARE_Ultralight_C] = { .ATQA=0x0044, .ATQARelevant=true, .SAK=0x00, .SAKRelevant=true, .ATSRelevant=false, .Manufacturer="NXP", .Type="MIFARE Ultralight C" },
//[CardType_NXP_MIFARE_Ultralight_EV1] = { .ATQA=0x0044, .ATQARelevant=true, .SAK=0x00, .SAKRelevant=false, .ATSRelevant=false, .Manufacturer="NXP", .Type="MIFARE Ultralight EV1" },
// for the following two, setting ATSRelevant to true would cause checking the ATS value, but the NXP paper for distinguishing cards does not recommend this
[CardType_NXP_MIFARE_DESFire] = { .ATQA = 0x0344, .ATQARelevant = true, .SAK = 0x20, .SAKRelevant = true, .ATSRelevant = false, .ATSSize = 5, .ATS = {0x75, 0x77, 0x81, 0x02, 0x80}, .Manufacturer = "NXP", .Type = "MIFARE DESFire" },
[CardType_NXP_MIFARE_DESFire_EV1] = { .ATQA = 0x0344, .ATQARelevant = true, .SAK = 0x20, .SAKRelevant = true, .ATSRelevant = false, .ATSSize = 5, .ATS = {0x75, 0x77, 0x81, 0x02, 0x80}, .Manufacturer = "NXP", .Type = "MIFARE DESFire EV1" },
Expand Down
7 changes: 4 additions & 3 deletions Firmware/Chameleon-Mini/Configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ static const MapEntryType PROGMEM ConfigurationMap[] = {
{ .Id = CONFIG_EM4233, .Text = "EM4233" },
#endif
#ifdef CONFIG_MF_DESFIRE_SUPPORT
{ .Id = CONFIG_MF_DESFIRE, .Text = "MF_DESFIRE" },
{ .Id = CONFIG_MF_DESFIRE_2KEV1, .Text = "MF_DESFIRE_2KEV1" },
{ .Id = CONFIG_MF_DESFIRE_4KEV1, .Text = "MF_DESFIRE_4KEV1" },
{ .Id = CONFIG_MF_DESFIRE, .Text = "MF_DESFIRE" },
{ .Id = CONFIG_MF_DESFIRE_2KEV1, .Text = "MF_DESFIRE_2KEV1" },
{ .Id = CONFIG_MF_DESFIRE_4KEV1, .Text = "MF_DESFIRE_4KEV1" },
{ .Id = CONFIG_MF_DESFIRE_4KEV2, .Text = "MF_DESFIRE_4KEV2" },
#endif
};

Expand Down
14 changes: 8 additions & 6 deletions Firmware/Chameleon-Mini/Terminal/CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define CHAR_GET_MODE '?' /* <Command>? */
#define CHAR_SET_MODE '=' /* <Command>=<Param> */
#define CHAR_EXEC_MODE '\0' /* <Command> */
#define CHAR_EXEC_MODE_PARAM ' ' /* <Command> <Param> ... <ParamN> */
#define CHAR_EXEC_MODE_PARAM ' ' /* <Command> <Param> ... <ParamN> */

#define IS_COMMAND_DELIMITER(c) ( \
((c) == CHAR_EXEC_MODE) || ((c) == CHAR_GET_MODE) || ((c) == CHAR_SET_MODE) || ((c) == CHAR_EXEC_MODE_PARAM) \
Expand All @@ -34,7 +34,7 @@
#define NO_FUNCTION ((void*) 0)

#define STATUS_MESSAGE_TRAILER "\r\n"
#define OPTIONAL_ANSWER_TRAILER "\r\n"
#define OPTIONAL_ANSWER_TRAILER "\r\n"

/* Include all command functions */
#include "Commands.h"
Expand Down Expand Up @@ -492,10 +492,12 @@ static void DecodeCommand(void) {
TerminalSendString(pTerminalBuffer);
TerminalSendStringP(PSTR(OPTIONAL_ANSWER_TRAILER));
if (StringLength(pTerminalBuffer, TERMINAL_BUFFER_SIZE) + 1 >= TERMINAL_BUFFER_SIZE) {
// Notify the user that the command line output is truncated. This can come up in the
// 'CONFIG=MF_DESFIRE' variants where the Makefile setting 'MEMORY_LIMITED_TESTING' is
// enabled by default to save space for other necessary components.
TerminalSendStringP(PSTR("-- OUTPUT TRUNCATED --"));
/*
* Notify the user that the command line output is truncated. This can come up in the
* 'CONFIG=MF_DESFIRE' variants where the Makefile setting 'MEMORY_LIMITED_TESTING' is
* enabled by default to save space for other necessary components.
*/
TerminalSendStringP(PSTR("--TRUNCATED OUTPUT--"));
TerminalSendStringP(PSTR(OPTIONAL_ANSWER_TRAILER));
}
}
Expand Down
1 change: 1 addition & 0 deletions Firmware/Chameleon-Mini/Terminal/Commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifdef CONFIG_ISO15693_SNIFF_SUPPORT
#include "../Codec/SniffISO15693.h"
#endif /*#ifdef CONFIG_ISO15693_SNIFF_SUPPORT*/

extern Reader14443Command Reader14443CurrentCommand;
extern Sniff14443Command Sniff14443CurrentCommand;

Expand Down
Loading

0 comments on commit 93e77a2

Please sign in to comment.