Added permanent memory region to store data that remains persistent even after an initialization. To delete it, the device must be fully wiped.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2022-08-18 19:48:13 +02:00
parent 2df878232b
commit 7738c1902e
2 changed files with 37 additions and 19 deletions

View File

@@ -23,6 +23,8 @@
extern const uintptr_t end_data_pool; extern const uintptr_t end_data_pool;
extern const uintptr_t start_data_pool; extern const uintptr_t start_data_pool;
extern const uintptr_t end_rom_pool;
extern const uintptr_t start_rom_pool;
extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len); extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len);
extern int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t len, uint16_t offset); extern int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t len, uint16_t offset);
extern int flash_program_halfword (uintptr_t addr, uint16_t data); extern int flash_program_halfword (uintptr_t addr, uint16_t data);
@@ -209,19 +211,13 @@ void initialize_flash(bool hard) {
dynamic_files = 0; dynamic_files = 0;
} }
void scan_flash() { void scan_region(bool persistent) {
initialize_flash(false); //soft initialization uintptr_t endp = end_data_pool, startp = start_data_pool;
if (*(uintptr_t *)end_data_pool == 0xffffffff && *(uintptr_t *)(end_data_pool+sizeof(uintptr_t)) == 0xffffffff) if (persistent) {
{ endp = end_rom_pool;
printf("First initialization (or corrupted!)\r\n"); startp = start_rom_pool;
const uint8_t empty[8] = { 0 };
flash_program_block(end_data_pool, empty, sizeof(empty));
//low_flash_available();
//wait_flash_finish();
} }
printf("SCAN\r\n"); for (uintptr_t base = flash_read_uintptr(endp); base >= startp; base = flash_read_uintptr(base)) {
for (uintptr_t base = flash_read_uintptr(end_data_pool); base >= start_data_pool; base = flash_read_uintptr(base)) {
if (base == 0x0) //all is empty if (base == 0x0) //all is empty
break; break;
@@ -239,6 +235,23 @@ void scan_flash() {
} }
} }
void scan_flash() {
initialize_flash(false); //soft initialization
if (*(uintptr_t *)end_data_pool == 0xffffffff && *(uintptr_t *)(end_data_pool+sizeof(uintptr_t)) == 0xffffffff)
{
printf("First initialization (or corrupted!)\r\n");
uint8_t empty[sizeof(uintptr_t)*2+sizeof(uint32_t)];
memset(empty, 0, sizeof(empty));
flash_program_block(end_data_pool, empty, sizeof(empty));
flash_program_block(end_rom_pool, empty, sizeof(empty));
//low_flash_available();
//wait_flash_finish();
}
printf("SCAN\r\n");
scan_region(true);
scan_region(false);
}
uint8_t *file_read(const uint8_t *addr) { uint8_t *file_read(const uint8_t *addr) {
return flash_read((uintptr_t)addr); return flash_read((uintptr_t)addr);
} }

View File

@@ -38,8 +38,9 @@
//To avoid possible future allocations, data region starts at the end of flash and goes upwards to the center region //To avoid possible future allocations, data region starts at the end of flash and goes upwards to the center region
const uintptr_t start_data_pool = (XIP_BASE + FLASH_TARGET_OFFSET); const uintptr_t start_data_pool = (XIP_BASE + FLASH_TARGET_OFFSET);
const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-FLASH_PERMANENT_REGION; //This is a fixed value. DO NOT CHANGE const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-FLASH_PERMANENT_REGION-FLASH_DATA_HEADER_SIZE-4; //This is a fixed value. DO NOT CHANGE
#define FLASH_ADDR_DATA_STORAGE_START start_data_pool const uintptr_t end_rom_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-4; //This is a fixed value. DO NOT CHANGE
const uintptr_t start_rom_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-FLASH_PERMANENT_REGION; //This is a fixed value. DO NOT CHANGE
extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len); extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len);
extern int flash_program_halfword (uintptr_t addr, uint16_t data); extern int flash_program_halfword (uintptr_t addr, uint16_t data);
@@ -49,12 +50,16 @@ extern uint16_t flash_read_uint16(uintptr_t addr);
extern void low_flash_available(); extern void low_flash_available();
uintptr_t allocate_free_addr(uint16_t size) { uintptr_t allocate_free_addr(uint16_t size, bool persistent) {
if (size > FLASH_SECTOR_SIZE) if (size > FLASH_SECTOR_SIZE)
return 0x0; //ERROR return 0x0; //ERROR
size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //len+len size+next address+fid+prev_addr size size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //len+len size+next address+fid+prev_addr size
uintptr_t next_base = 0x0; uintptr_t next_base = 0x0, endp = end_data_pool, startp = start_data_pool;
for (uintptr_t base = end_data_pool; base >= start_data_pool; base = next_base) { if (persistent) {
endp = end_rom_pool;
startp = start_rom_pool;
}
for (uintptr_t base = endp; base >= startp; base = next_base) {
uintptr_t addr_alg = base & -FLASH_SECTOR_SIZE; //start address of sector uintptr_t addr_alg = base & -FLASH_SECTOR_SIZE; //start address of sector
uintptr_t potential_addr = base-real_size; uintptr_t potential_addr = base-real_size;
next_base = flash_read_uintptr(base); next_base = flash_read_uintptr(base);
@@ -69,7 +74,7 @@ uintptr_t allocate_free_addr(uint16_t size) {
flash_program_uintptr(base, potential_addr); flash_program_uintptr(base, potential_addr);
return potential_addr; return potential_addr;
} }
else if (addr_alg-FLASH_SECTOR_SIZE >= start_data_pool) { //check whether it fits in the next sector, so we take addr_aligned as the base else if (addr_alg-FLASH_SECTOR_SIZE >= startp) { //check whether it fits in the next sector, so we take addr_aligned as the base
potential_addr = addr_alg-real_size; potential_addr = addr_alg-real_size;
flash_program_uintptr(potential_addr, 0x0); flash_program_uintptr(potential_addr, 0x0);
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base); flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
@@ -128,7 +133,7 @@ int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t
} }
} }
uintptr_t new_addr = allocate_free_addr(len); uintptr_t new_addr = allocate_free_addr(len, (file->type & FILE_PERSISTENT) == FILE_PERSISTENT);
//printf("na %x\r\n",new_addr); //printf("na %x\r\n",new_addr);
if (new_addr == 0x0) if (new_addr == 0x0)
return CCID_ERR_NO_MEMORY; return CCID_ERR_NO_MEMORY;