Skip to content

Commit

Permalink
Merge pull request #149 from shinhub/memory_polish
Browse files Browse the repository at this point in the history
Memory polish
  • Loading branch information
securechicken authored Sep 14, 2019
2 parents f0869fe + aefc473 commit 844caed
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 59 deletions.
17 changes: 8 additions & 9 deletions Firmware/ChameleonMini/Configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* Author: skuser
*/

#include <avr/pgmspace.h>
#include "Configuration.h"
#include "Settings.h"
#include <avr/pgmspace.h>
#include "Map.h"
#include "AntennaLevel.h"

/* Map IDs to text */
static const MapEntryType PROGMEM ConfigurationMap[] = {
Expand Down Expand Up @@ -46,13 +45,13 @@ static void ApplicationInitDummy(void) {}
static void ApplicationResetDummy(void) {}
static void ApplicationTaskDummy(void) {}
static void ApplicationTickDummy(void) {}
static uint16_t ApplicationProcessDummy(uint8_t* ByteBuffer, uint16_t ByteCount) { return 0; }
static void ApplicationGetUidDummy(ConfigurationUidType Uid) { }
static uint16_t ApplicationProcessDummy(uint8_t* ByteBuffer, uint16_t ByteCount) { return CONFIGURATION_DUMMY_UID_PART; }
static void ApplicationGetUidDummy(ConfigurationUidType Uid) { memset(Uid, CONFIGURATION_DUMMY_UID_PART, CONFIGURATION_DUMMY_UID_SIZE); }
static void ApplicationSetUidDummy(ConfigurationUidType Uid) { }
static void ApplicationGetAtqaDummy(uint16_t * Atqa) { }
static void ApplicationGetAtqaDummy(uint16_t * Atqa) { *Atqa = CONFIGURATION_DUMMY_ATQA; }
static void ApplicationSetAtqaDummy(uint16_t Atqa) { }
static void ApplicationGetSakDummy(uint8_t * Atqa) { }
static void ApplicationSetSakDummy(uint8_t Atqa) { }
static void ApplicationGetSakDummy(uint8_t * Sak) { *Sak = CONFIGURATION_DUMMY_SAK; }
static void ApplicationSetSakDummy(uint8_t Sak) { }

static const PROGMEM ConfigurationType ConfigurationTable[] = {
[CONFIG_NONE] = {
Expand Down Expand Up @@ -270,14 +269,14 @@ void ConfigurationGetList(char* List, uint16_t BufferSize)
MapToString(ConfigurationMap, ARRAY_COUNT(ConfigurationMap), List, BufferSize);
}

uint16_t ConfigurationTableGetMemorySizeForId(ConfigurationEnum Configuration) {
uint32_t ConfigurationTableGetMemorySizeForId(ConfigurationEnum Configuration) {
/* Possible other implementation
ConfigurationType ConfForSetting;
memcpy_P( &ConfForSetting,
&(ConfigurationTable[GlobalSettings.Settings[SettingNumber].Configuration]),
sizeof(ConfigurationType) );
return ConfForSetting.MemorySize;
*/
return ( (uint16_t)pgm_read_word( &(ConfigurationTable[Configuration].MemorySize) ) );
return ( (uint32_t)pgm_read_dword( &(ConfigurationTable[Configuration].MemorySize) ) );
}

8 changes: 5 additions & 3 deletions Firmware/ChameleonMini/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Created on: 15.02.2013
* Author: skuser
*/
/** \file */
#ifndef _CM_CONFIGURATION_H_
#define _CM_CONFIGURATION_H_

Expand All @@ -14,6 +13,9 @@
#define CONFIGURATION_NAME_LENGTH_MAX 32
#define CONFIGURATION_UID_SIZE_MAX 16
#define CONFIGURATION_DUMMY_UID_SIZE 4
#define CONFIGURATION_DUMMY_UID_PART 0x00
#define CONFIGURATION_DUMMY_ATQA 0x0000
#define CONFIGURATION_DUMMY_SAK 0x00
#define CONFIGURATION_DUMMY_MEMSIZE 16

typedef uint8_t ConfigurationUidType[CONFIGURATION_UID_SIZE_MAX];
Expand Down Expand Up @@ -133,7 +135,7 @@ typedef struct {
* Defines how many space the configuration needs. For emulating configurations this is the memory space of
* the emulated card.
*/
uint16_t MemorySize;
uint32_t MemorySize;
/**
* Defines the size of the UID for emulating configurations.
*/
Expand All @@ -152,6 +154,6 @@ void ConfigurationSetById(ConfigurationEnum Configuration);
void ConfigurationGetByName(char* Configuration, uint16_t BufferSize);
bool ConfigurationSetByName(const char* Configuration);
void ConfigurationGetList(char* ConfigurationList, uint16_t BufferSize);
uint16_t ConfigurationTableGetMemorySizeForId(ConfigurationEnum Configuration);
uint32_t ConfigurationTableGetMemorySizeForId(ConfigurationEnum Configuration);

#endif /* _CM_CONFIGURATION_H_ */
3 changes: 3 additions & 0 deletions Firmware/ChameleonMini/Memory/EEPROM.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// Dummy init to prepare for future use
bool EEPROMInit(void) {
bool ret = false;
// Enable EEPROM data memory mapping
NVM.CTRLB |= NVM_EEMAPEN_bm;
// Calculate available EEPROM size per setting
if (EEPROM_BYTES_TOTAL > EEPROM_NO_MEMORY) {
EEPROMInfo.bytesTotal = EEPROM_BYTES_TOTAL;
ret = true;
Expand Down
16 changes: 14 additions & 2 deletions Firmware/ChameleonMini/Memory/Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ bool AppMemoryRead(void* Buffer, uint32_t Address, uint32_t ByteCount) {
}

bool AppMemoryDownloadXModem(void* Buffer, uint32_t Address, uint32_t ByteCount) {
return AppMemoryRead(Buffer, Address, ByteCount);
bool ret = false;
uint32_t AvailBytes = getAppMemSizeForSetting(GlobalSettings.ActiveSetting);
if(Address < AvailBytes) {
uint32_t BytesLeft = MIN(ByteCount, AvailBytes - Address);
ret = AppMemoryRead(Buffer, Address, BytesLeft);
}
return ret;
}

/* Memory write operations
Expand All @@ -118,7 +124,13 @@ bool AppMemoryWrite(const void* Buffer, uint32_t Address, uint32_t ByteCount) {
}

bool AppMemoryUploadXModem(void* Buffer, uint32_t Address, uint32_t ByteCount) {
return AppMemoryWrite((const void *)Buffer, Address, ByteCount);
bool ret = false;
uint32_t AvailBytes = getAppMemSizeForSetting(GlobalSettings.ActiveSetting);
if(Address < AvailBytes) {
uint32_t BytesLeft = MIN(ByteCount, AvailBytes - Address);
ret = AppMemoryWrite((const void *)Buffer, Address, BytesLeft);
}
return ret;
}

/* Memory delete/clear operations
Expand Down
33 changes: 19 additions & 14 deletions Firmware/ChameleonMini/Memory/SPIFlash.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "SPIFlash.h"
Expand Down Expand Up @@ -341,20 +342,24 @@ INLINE void FlashClearRangeRound(uint32_t BytesPerItem, uint32_t * Address, uint
* and finally pages */
bool FlashClearRange(uint32_t Address, uint32_t ByteCount) {
bool ret = false;
// We cannot clear less than a Page
if( ByteCount >= FlashInfo.geometry.bytesPerPage ) {
// If we want to clear all bytes from Address 0, then we FlashClearAll
if( (Address == FLASH_NO_OFFSET) && (ByteCount == FlashInfo.geometry.sizeBytes) ) {
ret = FlashClearAll();
} else if( checkAddrConsistency(Address, ByteCount) ) {
// Sectors clear round
FlashClearRangeRound(FlashInfo.geometry.bytesPerSectorN, &Address, &ByteCount);
// Blocks clear round
FlashClearRangeRound(FlashInfo.geometry.bytesPerBlock, &Address, &ByteCount);
// Pages clear round
FlashClearRangeRound(FlashInfo.geometry.bytesPerPage, &Address, &ByteCount);
ret = true;
}
// If we want to clear all bytes from Address 0, then we FlashClearAll
if( (Address == FLASH_NO_OFFSET) && (ByteCount == FlashInfo.geometry.sizeBytes) ) {
ret = FlashClearAll();
// We cannot clear less than a Page at once, so we buffer write instead
} else if( ByteCount < FlashInfo.geometry.bytesPerPage ) {
// Build a clear buffer of ByteCount FF bytes
uint8_t clearBuffer[ByteCount];
memset(clearBuffer, FLASH_CLEAR_BYTE, ByteCount);
// Write it with BufferedBytesWrite
ret = FlashBufferedBytesWrite(clearBuffer, Address, ByteCount);
} else if( checkAddrConsistency(Address, ByteCount) ) {
// Sectors clear round
FlashClearRangeRound(FlashInfo.geometry.bytesPerSectorN, &Address, &ByteCount);
// Blocks clear round
FlashClearRangeRound(FlashInfo.geometry.bytesPerBlock, &Address, &ByteCount);
// Pages clear round
FlashClearRangeRound(FlashInfo.geometry.bytesPerPage, &Address, &ByteCount);
ret = true;
}
return ret;
}
1 change: 1 addition & 0 deletions Firmware/ChameleonMini/Memory/SPIFlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#define FLASH_STATUS_PAGESIZE_BIT 0x01 // Flash page size setting (0 is default, 1 is binary)

#define FLASH_DUMMY_BYTE 0x00
#define FLASH_CLEAR_BYTE 0xFF
#define FLASH_NO_OFFSET 0
#define FLASH_NO_PAGE 0
#define FLASH_B_IN_KB 1024
Expand Down
3 changes: 0 additions & 3 deletions Firmware/ChameleonMini/System.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ void SystemInit(void)
/* Enable RTC with roughly 1kHz clock */
CLK.RTCCTRL = CLK_RTCSRC_ULP_gc | CLK_RTCEN_bm;
RTC.CTRL = RTC_PRESCALER_DIV1_gc;

/* Enable EEPROM data memory mapping */
NVM.CTRLB |= NVM_EEMAPEN_bm;
}

void SystemReset(void)
Expand Down
62 changes: 34 additions & 28 deletions Firmware/ChameleonMini/Terminal/Commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

#include "Commands.h"
#include <stdio.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "XModem.h"
#include "../Settings.h"
Expand Down Expand Up @@ -436,46 +437,51 @@ CommandStatusIdType CommandExecMemoryInfo(char* OutMessage)
#ifdef CONFIG_DEBUG_MEMORYTEST_COMMAND
CommandStatusIdType CommandExecMemoryTest(char* OutMessage)
{
uint8_t bigbuf[512];
uint8_t readbuf[70];
memset(bigbuf, 0x11, 512);
memset(readbuf, 0xAA, 70);
uint8_t bigbuf[128];
uint8_t readbuf[45];
uint8_t expected[45];
HexStringToBuffer(expected, 45, "11111111AA031111111111111111111100FFFFFFFFAA03FFFFFFFFFFFFFFFFFFFF00FFFFFFFFFFFFFFFFFFFF05");
memset(bigbuf, 0x11, 128);
memset(readbuf, 0xAA, 45);

SettingsSetActiveById(3);
ConfigurationSetById(CONFIG_MF_CLASSIC_1K);
ConfigurationSetById(CONFIG_NONE);
SettingsSave();
SettingsSetActiveById(0);
ConfigurationSetById(CONFIG_MF_CLASSIC_4K);
SettingsSave();

AppMemoryWriteForSetting(3, bigbuf, 0, 512);
AppMemoryWriteForSetting(3, bigbuf, 512, 512);
for(uint8_t i = 0; i < 8; i++){
AppMemoryWrite(bigbuf, i*512, 512);
for(uint8_t i = 0; i < 32; i++){
AppMemoryWrite(bigbuf, i*128, 128);
}
FlashUnbufferedBytesRead(readbuf, 3*MemoryMappingInfo.maxFlashBytesPerSlot+1023, 1);
AppMemoryReadForSetting(3, readbuf+1, 250, 9);
readbuf[10] = 0x03;
FlashUnbufferedBytesRead(readbuf+11, 1024, 1);
FlashUnbufferedBytesRead(readbuf+12, 3118, 2);
AppMemoryRead(readbuf+14, 2046, 3);
AppMemoryRead(readbuf+17, 12, 4);
readbuf[21] = 0x00;
FlashClearRange(3*MemoryMappingInfo.maxFlashBytesPerSlot, 1023);
FlashUnbufferedBytesRead(readbuf, 3*MemoryMappingInfo.maxFlashBytesPerSlot, 4);
AppMemoryReadForSetting(3, readbuf+4, 250, 1);
readbuf[5] = 0x03;
FlashUnbufferedBytesRead(readbuf+6, 1024, 1);
FlashUnbufferedBytesRead(readbuf+7, 3118, 2);
AppMemoryRead(readbuf+9, 2046, 3);
AppMemoryRead(readbuf+12, 12, 4);
readbuf[16] = 0x00;
FlashClearRange(3*MemoryMappingInfo.maxFlashBytesPerSlot, 16);
AppMemoryClear();
FlashUnbufferedBytesRead(readbuf+22, 3*MemoryMappingInfo.maxFlashBytesPerSlot+1023, 1);
AppMemoryReadForSetting(3, readbuf+23, 250, 9);
readbuf[32] = 0x03;
FlashUnbufferedBytesRead(readbuf+33, 1024, 1);
FlashUnbufferedBytesRead(readbuf+34, 3118, 2);
AppMemoryRead(readbuf+36, 2046, 3);
AppMemoryRead(readbuf+39, 12, 4);
readbuf[43] = 0x00;
FlashUnbufferedBytesRead(readbuf+44, 5*MemoryMappingInfo.maxFlashBytesPerSlot+11, 10);
readbuf[54] = 0x05;
FlashUnbufferedBytesRead(readbuf+17, 3*MemoryMappingInfo.maxFlashBytesPerSlot, 4);
AppMemoryReadForSetting(3, readbuf+21, 250, 1);
readbuf[22] = 0x03;
FlashUnbufferedBytesRead(readbuf+23, 1024, 1);
FlashUnbufferedBytesRead(readbuf+24, 3118, 2);
AppMemoryRead(readbuf+26, 2046, 3);
AppMemoryRead(readbuf+29, 12, 4);
readbuf[33] = 0x00;
FlashUnbufferedBytesRead(readbuf+34, 5*MemoryMappingInfo.maxFlashBytesPerSlot+11, 10);
readbuf[44] = 0x05;
FlashClearAll();

BufferToHexString(OutMessage, TERMINAL_BUFFER_SIZE, readbuf, 56);
if(memcmp(readbuf, expected, 45)) {
BufferToHexString(OutMessage, TERMINAL_BUFFER_SIZE, readbuf, 45);
} else {
snprintf_P(OutMessage, TERMINAL_BUFFER_SIZE, PSTR("FINE"), NULL);
}

return COMMAND_INFO_OK_WITH_TEXT_ID;
}
Expand Down

0 comments on commit 844caed

Please sign in to comment.