mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-08 14:06:11 +02:00
Added a major refactoring to include Emulated interface.
It works in combination with virtualsmarcard module (vpcd). It properly installed, it creates a virtual reader that can be interfaced via PCSC+vcpd. At user app level, it has no difference of having a physical smart card. At this moment, it only works emulating a CCID interface. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -20,12 +20,22 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/flash.h"
|
||||
#include "hardware/sync.h"
|
||||
#include "pico/mutex.h"
|
||||
#include "pico/sem.h"
|
||||
#include "pico/multicore.h"
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#define FLASH_SECTOR_SIZE 4096
|
||||
#define PICO_FLASH_SIZE_BYTES (8*1024*1024)
|
||||
int fd_map = 0;
|
||||
uint8_t *map = NULL;
|
||||
#endif
|
||||
#include "hsm.h"
|
||||
#include <string.h>
|
||||
|
||||
@@ -41,23 +51,28 @@ typedef struct page_flash {
|
||||
|
||||
static page_flash_t flash_pages[TOTAL_FLASH_PAGES];
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
static mutex_t mtx_flash;
|
||||
static semaphore_t sem_wait;
|
||||
#endif
|
||||
static bool locked_out = false;
|
||||
|
||||
static uint8_t ready_pages = 0;
|
||||
|
||||
bool flash_available = false;
|
||||
static bool locked_out = false;
|
||||
|
||||
|
||||
//this function has to be called from the core 0
|
||||
void do_flash()
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
if (mutex_try_enter(&mtx_flash, NULL) == true) {
|
||||
#endif
|
||||
if (locked_out == true && flash_available == true && ready_pages > 0) {
|
||||
//printf(" DO_FLASH AVAILABLE\r\n");
|
||||
for (int r = 0; r < TOTAL_FLASH_PAGES; r++) {
|
||||
if (flash_pages[r].ready == true) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
||||
while (multicore_lockout_start_timeout_us(1000) == false);
|
||||
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
||||
@@ -67,53 +82,81 @@ void do_flash()
|
||||
restore_interrupts (ints);
|
||||
while (multicore_lockout_end_timeout_us(1000) == false);
|
||||
//printf("WRITEN %X !\r\n",flash_pages[r].address);
|
||||
|
||||
#else
|
||||
memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE);
|
||||
#endif
|
||||
flash_pages[r].ready = false;
|
||||
ready_pages--;
|
||||
}
|
||||
else if (flash_pages[r].erase == true) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
while (multicore_lockout_start_timeout_us(1000) == false);
|
||||
//printf("WRITTING\r\n");
|
||||
flash_range_erase(flash_pages[r].address-XIP_BASE, flash_pages[r].page_size ? ((int)(flash_pages[r].page_size/FLASH_SECTOR_SIZE))*FLASH_SECTOR_SIZE : FLASH_SECTOR_SIZE);
|
||||
while (multicore_lockout_end_timeout_us(1000) == false);
|
||||
#else
|
||||
memset(map + flash_pages[r].address, 0, FLASH_SECTOR_SIZE);
|
||||
#endif
|
||||
flash_pages[r].erase = false;
|
||||
ready_pages--;
|
||||
}
|
||||
}
|
||||
#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");
|
||||
}
|
||||
}
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
}
|
||||
sem_release(&sem_wait);
|
||||
#endif
|
||||
}
|
||||
|
||||
//this function has to be called from the core 0
|
||||
void low_flash_init() {
|
||||
memset(flash_pages, 0, sizeof(page_flash_t)*TOTAL_FLASH_PAGES);
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_init(&mtx_flash);
|
||||
sem_init(&sem_wait, 0, 1);
|
||||
memset(flash_pages, 0, sizeof(page_flash_t)*TOTAL_FLASH_PAGES);
|
||||
#else
|
||||
fd_map = open("memory.flash", O_RDWR | O_CREAT, (mode_t)0600);
|
||||
lseek(fd_map, PICO_FLASH_SIZE_BYTES-1, SEEK_SET);
|
||||
write(fd_map, "", 1);
|
||||
map = mmap(0, PICO_FLASH_SIZE_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd_map, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void low_flash_init_core1() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
multicore_lockout_victim_init();
|
||||
#endif
|
||||
locked_out = true;
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
}
|
||||
|
||||
void wait_flash_finish() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
sem_acquire_blocking(&sem_wait); //blocks until released
|
||||
//wake up
|
||||
sem_acquire_blocking(&sem_wait); //decrease permits
|
||||
#endif
|
||||
}
|
||||
|
||||
void low_flash_available() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
#endif
|
||||
flash_available = true;
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
}
|
||||
|
||||
page_flash_t *find_free_page(uintptr_t addr) {
|
||||
@@ -143,21 +186,29 @@ int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
|
||||
if (!data || len == 0)
|
||||
return CCID_ERR_NULL_PARAM;
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
#endif
|
||||
if (ready_pages == TOTAL_FLASH_PAGES) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
printf("ERROR: ALL FLASH PAGES CACHED\r\n");
|
||||
return CCID_ERR_NO_MEMORY;
|
||||
}
|
||||
if (!(p = find_free_page(addr)))
|
||||
{
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n");
|
||||
return CCID_ERR_MEMORY_FATAL;
|
||||
}
|
||||
memcpy(&p->page[addr&(FLASH_SECTOR_SIZE-1)], data, len);
|
||||
//printf("Flash: modified page %X with data %x at [%x] (top page %X)\r\n",addr_alg,data,addr&(FLASH_SECTOR_SIZE-1),addr);
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
@@ -175,19 +226,25 @@ int flash_program_uintptr (uintptr_t addr, uintptr_t data) {
|
||||
|
||||
uint8_t *flash_read(uintptr_t addr) {
|
||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
#endif
|
||||
if (ready_pages > 0) {
|
||||
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
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t *v = (uint8_t *)addr;
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -214,21 +271,29 @@ uint8_t flash_read_uint8(uintptr_t addr) {
|
||||
int flash_erase_page (uintptr_t addr, size_t page_size) {
|
||||
page_flash_t *p = NULL;
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
#endif
|
||||
if (ready_pages == TOTAL_FLASH_PAGES) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
printf("ERROR: ALL FLASH PAGES CACHED\r\n");
|
||||
return CCID_ERR_NO_MEMORY;
|
||||
}
|
||||
if (!(p = find_free_page(addr))) {
|
||||
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n");
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
return CCID_ERR_MEMORY_FATAL;
|
||||
}
|
||||
p->erase = true;
|
||||
p->ready = false;
|
||||
p->page_size = page_size;
|
||||
#ifndef ENABLE_EMULATION
|
||||
mutex_exit(&mtx_flash);
|
||||
#endif
|
||||
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user