Fix when clearing file. It was omitting fid field and thus causing misalignment and corruption data.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2022-02-18 13:25:19 +01:00
parent 1630c7b52d
commit 4bb756b17c
No known key found for this signature in database
GPG Key ID: C0095B7870A4CCD3

10
flash.c
View File

@ -94,6 +94,8 @@ uintptr_t allocate_free_addr(uint16_t size) {
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);
//printf("nb %x %x %x %x\r\n",base,next_base,addr_alg,potential_addr);
//printf("fid %x\r\n",flash_read_uint16(next_base+sizeof(uintptr_t)));
if (next_base == 0x0) { //we are at the end if (next_base == 0x0) { //we are at the end
//now we check if we fit in the current sector //now we check if we fit in the current sector
if (addr_alg <= potential_addr) //it fits in the current sector if (addr_alg <= potential_addr) //it fits in the current sector
@ -111,7 +113,7 @@ uintptr_t allocate_free_addr(uint16_t size) {
return 0x0; return 0x0;
} }
//we check if |base-(next_addr+size_next_addr)| > |base-potential_addr| only if fid != 1xxx (not size blocked) //we check if |base-(next_addr+size_next_addr)| > |base-potential_addr| only if fid != 1xxx (not size blocked)
else if (addr_alg <= potential_addr && base-(next_base+flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uint16_t))) > base-potential_addr && flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uint16_t)) & 0x1000 != 0x1000) { else if (addr_alg <= potential_addr && base-(next_base+flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uint16_t))+2*sizeof(uint16_t)) > base-potential_addr && flash_read_uint16(next_base+sizeof(uintptr_t)) & 0x1000 != 0x1000) {
flash_program_uintptr(potential_addr, next_base); flash_program_uintptr(potential_addr, next_base);
flash_program_uintptr(base, potential_addr); flash_program_uintptr(base, potential_addr);
return potential_addr; return potential_addr;
@ -122,14 +124,17 @@ uintptr_t allocate_free_addr(uint16_t size) {
int flash_clear_file(file_t *file) { int flash_clear_file(file_t *file) {
uintptr_t prev_addr = (uintptr_t)(file->data+flash_read_uint16((uintptr_t)file->data)+sizeof(uint16_t)); uintptr_t prev_addr = (uintptr_t)(file->data+flash_read_uint16((uintptr_t)file->data)+sizeof(uint16_t));
uintptr_t base_addr = (uintptr_t)file->data-sizeof(uintptr_t); uintptr_t base_addr = (uintptr_t)(file->data-sizeof(uintptr_t)-sizeof(uint16_t));
uintptr_t next_addr = flash_read_uintptr(base_addr); uintptr_t next_addr = flash_read_uintptr(base_addr);
//printf("nc %x %x %x\r\n",prev_addr,base_addr,next_addr);
flash_program_uintptr(prev_addr, next_addr); flash_program_uintptr(prev_addr, next_addr);
flash_program_halfword((uintptr_t)file->data, 0); flash_program_halfword((uintptr_t)file->data, 0);
return 0; return 0;
} }
int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) { int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) {
if (!file)
return HSM_ERR_NULL_PARAM;
if (len > FLASH_SECTOR_SIZE) if (len > FLASH_SECTOR_SIZE)
return HSM_ERR_NO_MEMORY; return HSM_ERR_NO_MEMORY;
if (file->data) { //already in flash if (file->data) { //already in flash
@ -144,6 +149,7 @@ int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) {
} }
} }
uintptr_t new_addr = allocate_free_addr(len); uintptr_t new_addr = allocate_free_addr(len);
//printf("na %x\r\n",new_addr);
if (new_addr == 0x0) if (new_addr == 0x0)
return HSM_ERR_NO_MEMORY; return HSM_ERR_NO_MEMORY;
file->data = (uint8_t *)new_addr+sizeof(uintptr_t)+sizeof(uint16_t); //next addr+fid file->data = (uint8_t *)new_addr+sizeof(uintptr_t)+sizeof(uint16_t); //next addr+fid