Migrating to static memory to avoid malloc for new files. Let's see how it works.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
65
file.c
65
file.c
@@ -135,10 +135,9 @@ file_t *file_retries_pin1 = NULL;
|
|||||||
file_t *file_sopin = NULL;
|
file_t *file_sopin = NULL;
|
||||||
file_t *file_retries_sopin = NULL;
|
file_t *file_retries_sopin = NULL;
|
||||||
|
|
||||||
file_chain_t *ef_prkdf = NULL;
|
#define MAX_DYNAMIC_FILES 64
|
||||||
file_chain_t *ef_kf = NULL;
|
uint16_t dynamic_files = 0;
|
||||||
file_chain_t *ef_pukdf = NULL;
|
file_t dynamic_file[MAX_DYNAMIC_FILES];
|
||||||
file_chain_t *ef_cdf = NULL;
|
|
||||||
|
|
||||||
bool card_terminated = false;
|
bool card_terminated = false;
|
||||||
|
|
||||||
@@ -234,27 +233,26 @@ void initialize_chain(file_chain_t **chain) {
|
|||||||
file_chain_t *next;
|
file_chain_t *next;
|
||||||
for (file_chain_t *f = *chain; f; f = next) {
|
for (file_chain_t *f = *chain; f; f = next) {
|
||||||
next = f->next;
|
next = f->next;
|
||||||
free(f->file);
|
|
||||||
free(f);
|
free(f);
|
||||||
}
|
}
|
||||||
*chain = NULL;
|
*chain = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_flash() {
|
void initialize_flash(bool hard) {
|
||||||
const uint8_t empty[8] = { 0 };
|
if (hard) {
|
||||||
flash_program_block(end_data_pool, empty, sizeof(empty));
|
const uint8_t empty[8] = { 0 };
|
||||||
low_flash_available();
|
flash_program_block(end_data_pool, empty, sizeof(empty));
|
||||||
initialize_chain(&ef_prkdf);
|
low_flash_available();
|
||||||
initialize_chain(&ef_pukdf);
|
}
|
||||||
initialize_chain(&ef_kf);
|
|
||||||
initialize_chain(&ef_cdf);
|
|
||||||
for (file_t *f = file_entries; f != file_last; f++) {
|
for (file_t *f = file_entries; f != file_last; f++) {
|
||||||
if ((f->type & FILE_FLASH) == FILE_FLASH)
|
if ((f->type & FILE_FLASH) == FILE_FLASH)
|
||||||
f->data = NULL;
|
f->data = NULL;
|
||||||
}
|
}
|
||||||
|
dynamic_files = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
if (*(uintptr_t *)end_data_pool == 0xffffffff && *(uintptr_t *)(end_data_pool+sizeof(uintptr_t)) == 0xffffffff)
|
||||||
{
|
{
|
||||||
printf("First initialization (or corrupted!)\r\n");
|
printf("First initialization (or corrupted!)\r\n");
|
||||||
@@ -274,21 +272,18 @@ void scan_flash() {
|
|||||||
printf("scan fid %x\r\n",fid);
|
printf("scan fid %x\r\n",fid);
|
||||||
file_t *file = (file_t *)search_by_fid(fid, NULL, SPECIFY_EF);
|
file_t *file = (file_t *)search_by_fid(fid, NULL, SPECIFY_EF);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
file = file_new(fid);
|
||||||
if ((fid & 0xff00) == (KEY_PREFIX << 8)) {
|
if ((fid & 0xff00) == (KEY_PREFIX << 8)) {
|
||||||
file = file_new(fid);
|
//add_file_to_chain(file, &ef_kf);
|
||||||
add_file_to_chain(file, &ef_kf);
|
|
||||||
}
|
}
|
||||||
else if ((fid & 0xff00) == (PRKD_PREFIX << 8)) {
|
else if ((fid & 0xff00) == (PRKD_PREFIX << 8)) {
|
||||||
file = file_new(fid);
|
//add_file_to_chain(file, &ef_prkdf);
|
||||||
add_file_to_chain(file, &ef_prkdf);
|
|
||||||
}
|
}
|
||||||
else if ((fid & 0xff00) == (CD_PREFIX << 8)) {
|
else if ((fid & 0xff00) == (CD_PREFIX << 8)) {
|
||||||
file = file_new(fid);
|
//add_file_to_chain(file, &ef_cdf);
|
||||||
add_file_to_chain(file, &ef_cdf);
|
|
||||||
}
|
}
|
||||||
else if ((fid & 0xff00) == (EE_CERTIFICATE_PREFIX << 8)) {
|
else if ((fid & 0xff00) == (EE_CERTIFICATE_PREFIX << 8)) {
|
||||||
file = file_new(fid);
|
//add_file_to_chain(file, &ef_pukdf);
|
||||||
add_file_to_chain(file, &ef_pukdf);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
TU_LOG1("SCAN FOUND ORPHAN FILE: %x\r\n",fid);
|
TU_LOG1("SCAN FOUND ORPHAN FILE: %x\r\n",fid);
|
||||||
@@ -381,8 +376,22 @@ uint8_t file_read_uint8(const uint8_t *addr) {
|
|||||||
return flash_read_uint8((uintptr_t)addr);
|
return flash_read_uint8((uintptr_t)addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file_t *search_dynamic_file(uint16_t fid) {
|
||||||
|
for (int i = 0; i < dynamic_files; i++) {
|
||||||
|
if (dynamic_file[i].fid == fid)
|
||||||
|
return &dynamic_file[i];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
file_t *file_new(uint16_t fid) {
|
file_t *file_new(uint16_t fid) {
|
||||||
file_t *f = (file_t *)malloc(sizeof(file_t));
|
file_t *f;
|
||||||
|
if ((f = search_dynamic_file(fid)))
|
||||||
|
return f;
|
||||||
|
if (dynamic_files == MAX_DYNAMIC_FILES)
|
||||||
|
return NULL;
|
||||||
|
f = &dynamic_file[dynamic_files];
|
||||||
|
dynamic_files++;
|
||||||
file_t file = {
|
file_t file = {
|
||||||
.fid = fid,
|
.fid = fid,
|
||||||
.parent = 5,
|
.parent = 5,
|
||||||
@@ -398,11 +407,13 @@ file_t *file_new(uint16_t fid) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
file_chain_t *add_file_to_chain(file_t *file, file_chain_t **chain) {
|
file_chain_t *add_file_to_chain(file_t *file, file_chain_t **chain) {
|
||||||
file_chain_t *f_chain = (file_chain_t *)malloc(sizeof(file_chain_t));
|
if (search_file_chain(file->fid, *chain))
|
||||||
f_chain->file = file;
|
return NULL;
|
||||||
f_chain->next = *chain;
|
file_chain_t *fc = (file_chain_t *)malloc(sizeof(file_chain_t));
|
||||||
*chain = f_chain;
|
fc->file = file;
|
||||||
return f_chain;
|
fc->next = *chain;
|
||||||
|
*chain = fc;
|
||||||
|
return fc;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *search_file_chain(uint16_t fid, file_chain_t *chain) {
|
file_t *search_file_chain(uint16_t fid, file_chain_t *chain) {
|
||||||
|
|||||||
9
file.h
9
file.h
@@ -81,7 +81,7 @@ extern file_t *search_by_path(const uint8_t *pe_path, uint8_t pathlen, const fil
|
|||||||
extern bool authenticate_action(const file_t *ef, uint8_t op);
|
extern bool authenticate_action(const file_t *ef, uint8_t op);
|
||||||
extern void process_fci(const file_t *pe);
|
extern void process_fci(const file_t *pe);
|
||||||
extern void scan_flash();
|
extern void scan_flash();
|
||||||
extern void initialize_flash();
|
extern void initialize_flash(bool);
|
||||||
|
|
||||||
extern file_t file_entries[];
|
extern file_t file_entries[];
|
||||||
|
|
||||||
@@ -90,10 +90,9 @@ extern uint16_t file_read_uint16(const uint8_t *addr);
|
|||||||
extern uint8_t file_read_uint8(const uint8_t *addr);
|
extern uint8_t file_read_uint8(const uint8_t *addr);
|
||||||
extern file_t *file_new(uint16_t);
|
extern file_t *file_new(uint16_t);
|
||||||
|
|
||||||
extern file_chain_t *ef_prkdf; //key description
|
extern uint16_t dynamic_files;
|
||||||
extern file_chain_t *ef_kf; //key blob
|
extern file_t dynamic_file[];
|
||||||
extern file_chain_t *ef_pukdf; //cvc csr
|
extern file_t *search_dynamic_file(uint16_t);
|
||||||
extern file_chain_t *ef_cdf; //ce
|
|
||||||
|
|
||||||
extern file_chain_t *add_file_to_chain(file_t *file, file_chain_t **chain);
|
extern file_chain_t *add_file_to_chain(file_t *file, file_chain_t **chain);
|
||||||
extern file_t *search_file_chain(uint16_t fid, file_chain_t *chain);
|
extern file_t *search_file_chain(uint16_t fid, file_chain_t *chain);
|
||||||
|
|||||||
Reference in New Issue
Block a user