From 4d06c0fc3d7d250cfa1b0e6e9db1a58f7b65fc2d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 12 Jan 2023 20:03:21 +0100 Subject: [PATCH] Fix accessing to mapped memory. Signed-off-by: Pol Henarejos --- src/fs/file.c | 5 +++-- src/fs/flash.c | 3 ++- src/fs/low_flash.c | 18 +++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 2100bed..72752e0 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -58,8 +58,9 @@ void process_fci(const file_t *pe, int fmd) { res_APDU[res_APDU_size++] = len & 0xff; } else { - res_APDU[res_APDU_size++] = pe->data[1]; - res_APDU[res_APDU_size++] = pe->data[0]; + uint16_t v = file_get_size(pe); + res_APDU[res_APDU_size++] = v >> 8; + res_APDU[res_APDU_size++] = v & 0xff; } } else { diff --git a/src/fs/flash.c b/src/fs/flash.c index 004ae3c..bd146e5 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -53,6 +53,7 @@ extern int flash_program_halfword (uintptr_t addr, uint16_t data); extern int flash_program_uintptr(uintptr_t, uintptr_t); extern uintptr_t flash_read_uintptr(uintptr_t addr); extern uint16_t flash_read_uint16(uintptr_t addr); +extern uint8_t *flash_read(uintptr_t addr); extern void low_flash_available(); @@ -133,7 +134,7 @@ int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t flash_clear_file(file); if (offset > 0) { old_data = (uint8_t *)calloc(1, offset+len); - memcpy(old_data, file->data+sizeof(uint16_t), offset); + memcpy(old_data, flash_read((uintptr_t)(file->data+sizeof(uint16_t))), offset); memcpy(old_data+offset, data, len); len = offset+len; data = old_data; diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index a8f56d4..987ec0e 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -55,7 +55,11 @@ static page_flash_t flash_pages[TOTAL_FLASH_PAGES]; static mutex_t mtx_flash; static semaphore_t sem_wait; #endif +#ifndef ENABLE_EMULATION static bool locked_out = false; +#else +static bool locked_out = true; +#endif static uint8_t ready_pages = 0; @@ -63,8 +67,7 @@ bool flash_available = false; //this function has to be called from the core 0 -void do_flash() -{ +void do_flash() { #ifndef ENABLE_EMULATION if (mutex_try_enter(&mtx_flash, NULL) == true) { #endif @@ -104,11 +107,11 @@ void do_flash() #ifdef ENABLE_EMULATION msync(map, PICO_FLASH_SIZE_BYTES, MS_SYNC); #endif - flash_available = false; if (ready_pages != 0) { printf("ERROR: DO FLASH DOES NOT HAVE ZERO PAGES\n"); } } + flash_available = false; #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); } @@ -169,7 +172,11 @@ page_flash_t *find_free_page(uintptr_t addr) { p = &flash_pages[r]; if (!flash_pages[r].ready && !flash_pages[r].erase) { +#ifndef ENABLE_EMULATION memcpy(p->page, (uint8_t *)addr_alg, FLASH_SECTOR_SIZE); +#else + memcpy(p->page, (uint8_t *)(map+addr_alg), FLASH_SECTOR_SIZE); +#endif ready_pages++; p->address = addr_alg; p->ready = true; @@ -230,8 +237,7 @@ uint8_t *flash_read(uintptr_t addr) { mutex_enter_blocking(&mtx_flash); #endif if (ready_pages > 0) { - for (int r = 0; r < TOTAL_FLASH_PAGES; r++) - { + for (int r = 0; r < TOTAL_FLASH_PAGES; r++) { if (flash_pages[r].ready && flash_pages[r].address == addr_alg) { uint8_t *v = &flash_pages[r].page[addr&(FLASH_SECTOR_SIZE-1)]; #ifndef ENABLE_EMULATION @@ -244,6 +250,8 @@ uint8_t *flash_read(uintptr_t addr) { uint8_t *v = (uint8_t *)addr; #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); +#else + v += (uintptr_t)map; #endif return v; }