From 019c5929a2ad3b581be21317b8aa56b8e441d614 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 17 Apr 2024 11:21:10 +0200 Subject: [PATCH 1/6] Remove carriage return \r for better debug. Signed-off-by: Pol Henarejos --- src/apdu.c | 6 +++--- src/apdu.h | 8 ++++---- src/fs/file.c | 6 +++--- src/fs/flash.c | 10 +++++----- src/usb/ccid/ccid.c | 16 ++++++++-------- src/usb/usb.c | 2 +- src/usb/usb_descriptors.c | 2 +- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/apdu.c b/src/apdu.c index 7ac3a81..bc997c0 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -121,9 +121,9 @@ uint16_t apdu_process(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) } } } - //printf("apdu.nc %ld, apdu.ne %ld\r\n",apdu.nc,apdu.ne); + //printf("apdu.nc %ld, apdu.ne %ld\n",apdu.nc,apdu.ne); if (apdu.header[1] == 0xc0) { - //printf("apdu.ne %u, apdu.rlen %d, bk %x\r\n",apdu.ne,apdu.rlen,rdata_bk); + //printf("apdu.ne %u, apdu.rlen %d, bk %x\n",apdu.ne,apdu.rlen,rdata_bk); timeout_stop(); *(uint16_t *) rdata_gr = rdata_bk; if (apdu.rlen <= apdu.ne) { @@ -215,7 +215,7 @@ done: ; uint32_t flag = EV_EXEC_FINISHED; queue_add_blocking(&card_to_usb_q, &flag); } - //printf("EXIT !!!!!!\r\n"); + //printf("EXIT !!!!!!\n"); if (current_app && current_app->unload) { current_app->unload(); current_app = NULL; diff --git a/src/apdu.h b/src/apdu.h index e7ae1c8..05d508a 100644 --- a/src/apdu.h +++ b/src/apdu.h @@ -43,7 +43,7 @@ typedef struct cmd { #if defined(DEBUG_APDU) && DEBUG_APDU == 1 #define DEBUG_PAYLOAD(_p, _s) { \ - printf("Payload %s (%d bytes):\r\n", #_p, (int) (_s)); \ + printf("Payload %s (%d bytes):\n", #_p, (int) (_s)); \ for (int _i = 0; _i < _s; _i += 16) { \ printf("%" PRIxPTR "h : ", (uintptr_t) (_i + _p)); \ for (int _j = 0; _j < 16; _j++) { \ @@ -56,12 +56,12 @@ typedef struct cmd { else printf(" "); \ if (_j == 7) printf(" "); \ } \ - printf("\r\n"); \ - } printf("\r\n"); \ + printf("\n"); \ + } printf("\n"); \ } #define DEBUG_DATA(_p, _s) \ { \ - printf("Data %s (%d bytes):\r\n", #_p, (int) (_s)); \ + printf("Data %s (%d bytes):\n", #_p, (int) (_s)); \ char *tmp = (char *) calloc(1, 2 * _s + 1); \ for (int _i = 0; _i < _s; _i++) \ { \ diff --git a/src/fs/file.c b/src/fs/file.c index c6fad0c..c1d6c6f 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -247,7 +247,7 @@ void scan_region(bool persistent) { } uint16_t fid = flash_read_uint16(base + sizeof(uintptr_t) + sizeof(uintptr_t)); - printf("[%x] scan fid %x, len %d\r\n", (unsigned int) base, fid, + printf("[%x] scan fid %x, len %d\n", (unsigned int) base, fid, flash_read_uint16(base + sizeof(uintptr_t) + sizeof(uintptr_t) + sizeof(uint16_t))); file_t *file = (file_t *) search_by_fid(fid, NULL, SPECIFY_EF); if (!file) { @@ -267,7 +267,7 @@ void scan_flash() { initialize_flash(false); //soft initialization if (*(uintptr_t *) flash_read(end_rom_pool) == 0xffffffff && *(uintptr_t *) flash_read(end_rom_pool + sizeof(uintptr_t)) == 0xffffffff) { - printf("First initialization (or corrupted!)\r\n"); + printf("First initialization (or corrupted!)\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)); @@ -275,7 +275,7 @@ void scan_flash() { //low_flash_available(); //wait_flash_finish(); } - printf("SCAN\r\n"); + printf("SCAN\n"); scan_region(true); scan_region(false); } diff --git a/src/fs/flash.c b/src/fs/flash.c index c94a35e..f8e16bc 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -75,8 +75,8 @@ uintptr_t allocate_free_addr(uint16_t size, bool persistent) { uintptr_t addr_alg = base & -FLASH_SECTOR_SIZE; //start address of sector uintptr_t potential_addr = base - real_size; 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))); + //printf("nb %x %x %x %x\n",base,next_base,addr_alg,potential_addr); + //printf("fid %x\n",flash_read_uint16(next_base+sizeof(uintptr_t))); if (next_base == 0x0) { //we are at the end //now we check if we fit in the current sector if (addr_alg <= potential_addr) { //it fits in the current sector @@ -121,7 +121,7 @@ int flash_clear_file(file_t *file) { (uintptr_t)(file->data - sizeof(uintptr_t) - sizeof(uint16_t) - sizeof(uintptr_t)); uintptr_t prev_addr = flash_read_uintptr(base_addr + sizeof(uintptr_t)); uintptr_t next_addr = flash_read_uintptr(base_addr); - //printf("nc %lx->%lx %lx->%lx\r\n",prev_addr,flash_read_uintptr(prev_addr),base_addr,next_addr); + //printf("nc %lx->%lx %lx->%lx\n",prev_addr,flash_read_uintptr(prev_addr),base_addr,next_addr); flash_program_uintptr(prev_addr, next_addr); flash_program_halfword((uintptr_t) file->data, 0); if (next_addr > 0) { @@ -130,7 +130,7 @@ int flash_clear_file(file_t *file) { flash_program_uintptr(base_addr, 0); flash_program_uintptr(base_addr + sizeof(uintptr_t), 0); file->data = NULL; - //printf("na %lx->%lx\r\n",prev_addr,flash_read_uintptr(prev_addr)); + //printf("na %lx->%lx\n",prev_addr,flash_read_uintptr(prev_addr)); return CCID_OK; } @@ -165,7 +165,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, (file->type & FILE_PERSISTENT) == FILE_PERSISTENT); - //printf("na %x\r\n",new_addr); + //printf("na %x\n",new_addr); if (new_addr == 0x0) { return CCID_ERR_NO_MEMORY; } diff --git a/src/usb/ccid/ccid.c b/src/usb/ccid/ccid.c index e54c0d8..3ea9a2b 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -157,7 +157,7 @@ int driver_process_usb_nopacket_ccid() { int driver_process_usb_packet_ccid(uint8_t itf, uint16_t rx_read) { if (rx_read >= 10) { driver_init_ccid(itf); - //printf("%ld %d %x %x\r\n",ccid_header->dwLength,rx_read-10,ccid_header->bMessageType,ccid_header->bSeq); + //printf("%ld %d %x %x\n",ccid_header->dwLength,rx_read-10,ccid_header->bMessageType,ccid_header->bSeq); if (ccid_header[itf]->dwLength <= rx_read - 10) { size_t apdu_sent = 0; if (ccid_header[itf]->bMessageType != CCID_SLOT_STATUS) { @@ -180,7 +180,7 @@ int driver_process_usb_packet_ccid(uint8_t itf, uint16_t rx_read) { ccid_response[itf]->bSeq = ccid_header[itf]->bSeq; ccid_response[itf]->abRFU0 = 0; ccid_response[itf]->abRFU1 = 0; - //printf("1 %x %x %x || %x %x %x\r\n",ccid_response->apdu,apdu.rdata,ccid_response,ccid_header,ccid_header->apdu,apdu.data); + //printf("1 %x %x %x || %x %x %x\n",ccid_response->apdu,apdu.rdata,ccid_response,ccid_header,ccid_header->apdu,apdu.data); memcpy(&ccid_response[itf]->apdu, ccid_atr + 1, size_atr); if (ccid_status == 1) { card_start(apdu_thread); @@ -284,19 +284,19 @@ uint8_t *driver_prepare_response_ccid(uint8_t itf) { #define MAX_USB_POWER 1 static void ccid_init_cb(void) { - TU_LOG1("-------- CCID INIT\r\n"); + TU_LOG1("-------- CCID INIT\n"); vendord_init(); } static void ccid_reset_cb(uint8_t rhport) { - TU_LOG1("-------- CCID RESET\r\n"); + TU_LOG1("-------- CCID RESET\n"); itf_num = 0; vendord_reset(rhport); } static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) { uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t) * max_len); - //TU_LOG1("-------- CCID OPEN\r\n"); + //TU_LOG1("-------- CCID OPEN\n"); TU_VERIFY( itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0, 0); @@ -327,13 +327,13 @@ static bool ccid_control_xfer_cb(uint8_t __unused rhport, uint8_t stage, tusb_control_request_t const *request) { // nothing to do with DATA & ACK stage - TU_LOG2("-------- CCID CTRL XFER\r\n"); + TU_LOG2("-------- CCID CTRL XFER\n"); if (stage != CONTROL_STAGE_SETUP) { return true; } if (request->wIndex == itf_num) { - TU_LOG2("-------- bmRequestType %x, bRequest %x, wValue %x, wLength %x\r\n", + TU_LOG2("-------- bmRequestType %x, bRequest %x, wValue %x, wLength %x\n", request->bmRequestType, request->bRequest, request->wValue, @@ -371,7 +371,7 @@ static bool ccid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { - //printf("------ CALLED XFER_CB\r\n"); + //printf("------ CALLED XFER_CB\n"); return vendord_xfer_cb(rhport, ep_addr, result, xferred_bytes); //return true; } diff --git a/src/usb/usb.c b/src/usb/usb.c index 94155ed..bd0d794 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -283,7 +283,7 @@ void usb_task() { uint32_t m = 0x0; bool has_m = queue_try_remove(&card_to_usb_q, &m); //if (m != 0) - // printf("\r\n ------ M = %lu\r\n",m); + // printf("\n ------ M = %lu\n",m); if (has_m) { if (m == EV_EXEC_FINISHED) { timeout_stop(); diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 0d01607..92a7cb2 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -349,7 +349,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ if (request->bRequest == 0x22) { web_serial_connected = (request->wValue != 0); if (web_serial_connected) { - printf("\r\nWebUSB interface connected\r\n"); + printf("\nWebUSB interface connected\n"); } return tud_control_status(rhport, request); } From 22d4e629522ca6402634b7947f3b5d421b17391d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 17 Apr 2024 11:31:11 +0200 Subject: [PATCH 2/6] Add procedure to compute unique ID at startup. Signed-off-by: Pol Henarejos --- src/crypto_utils.c | 7 +------ src/fs/file.c | 9 +++++++++ src/fs/file.h | 4 ++++ src/main.c | 13 ++++++++++++- src/pico_keys.h | 8 ++++++++ src/usb/ccid/ccid.c | 20 ++++++-------------- src/usb/hid/hid.c | 7 ++----- 7 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/crypto_utils.c b/src/crypto_utils.c index 3c8d53a..76a0c16 100644 --- a/src/crypto_utils.c +++ b/src/crypto_utils.c @@ -37,14 +37,9 @@ void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]) { mbedtls_sha256_context ctx; mbedtls_sha256_init(&ctx); uint16_t iters = 256; -#ifndef ENABLE_EMULATION - pico_unique_board_id_t unique_id; - - pico_get_unique_board_id(&unique_id); -#endif mbedtls_sha256_starts(&ctx, 0); #ifndef ENABLE_EMULATION - mbedtls_sha256_update(&ctx, unique_id.id, sizeof(unique_id.id)); + mbedtls_sha256_update(&ctx, pico_serial.id, sizeof(pico_serial.id)); #endif while (iters > len) { diff --git a/src/fs/file.c b/src/fs/file.c index c1d6c6f..3aa0742 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -263,6 +263,9 @@ void scan_region(bool persistent) { } } void wait_flash_finish(); +#ifndef ENABLE_EMULATION +extern uint16_t usb_vid, usb_pid; +#endif void scan_flash() { initialize_flash(false); //soft initialization if (*(uintptr_t *) flash_read(end_rom_pool) == 0xffffffff && @@ -278,6 +281,12 @@ void scan_flash() { printf("SCAN\n"); scan_region(true); scan_region(false); +#ifndef ENABLE_EMULATION + file_t *ef_vp = search_dynamic_file(EF_VP); + if (file_has_data(ef_vp)) { + + } +#endif } uint8_t *file_read(const uint8_t *addr) { diff --git a/src/fs/file.h b/src/fs/file.h index 157e022..c3b7478 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -67,6 +67,10 @@ #define EF_SKDFS 0x6045 #define EF_META 0xE010 +#ifndef ENABLE_EMULATION +#define EF_VP 0xE020 +#endif + #define MAX_DEPTH 4 typedef PACK(struct file { diff --git a/src/main.c b/src/main.c index f7fc085..c3f1913 100644 --- a/src/main.c +++ b/src/main.c @@ -170,7 +170,7 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp) (void)tzp; // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) - // until 00:00:00 January 1, 1970 + // until 00:00:00 January 1, 1970 static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL); SYSTEMTIME system_time; @@ -313,7 +313,18 @@ void execute_tasks() { led_blinking_task(); } +char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; +pico_unique_board_id_t pico_serial; +#ifdef ENABLE_EMULATION +#define pico_get_unique_board_id(a) memset(a, 0, sizeof(*(a))) +#endif + int main(void) { + pico_get_unique_board_id(&pico_serial); + memset(pico_serial_str, 0, sizeof(pico_serial_str)); + for (int i = 0; i < sizeof(pico_serial); i++) { + snprintf(&pico_serial_str[2 * i], 3, "%02X", pico_serial.id[i]); + } #ifndef ENABLE_EMULATION usb_init(); diff --git a/src/pico_keys.h b/src/pico_keys.h index f9b28d9..2b8d9d3 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -156,4 +156,12 @@ extern uint32_t button_timeout; #define CCID_WRONG_PADDING -1011 #define CCID_VERIFICATION_FAILED -1012 +#if defined(ENABLE_EMULATION) +#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 +typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; +#endif + +extern pico_unique_board_id_t pico_serial; +extern char pico_serial_str[]; + #endif diff --git a/src/usb/ccid/ccid.c b/src/usb/ccid/ccid.c index 3ea9a2b..b659acb 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -101,7 +101,6 @@ struct ccid_header { uint8_t ccid_status = 1; static uint8_t itf_num; -extern tusb_desc_endpoint_t const desc_ep3; void ccid_write_offset(uint8_t itf, uint16_t size, uint16_t offset) { if (*usb_get_tx(itf) + offset != 0x81) { @@ -284,38 +283,31 @@ uint8_t *driver_prepare_response_ccid(uint8_t itf) { #define MAX_USB_POWER 1 static void ccid_init_cb(void) { - TU_LOG1("-------- CCID INIT\n"); vendord_init(); } static void ccid_reset_cb(uint8_t rhport) { - TU_LOG1("-------- CCID RESET\n"); itf_num = 0; vendord_reset(rhport); } static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) { uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t) * max_len); - //TU_LOG1("-------- CCID OPEN\n"); - TU_VERIFY( - itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0, - 0); + TU_VERIFY( itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0, 0); //vendord_open expects a CLASS_VENDOR interface class + uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 3 * sizeof(tusb_desc_endpoint_t); memcpy(itf_vendor, itf_desc, sizeof(uint8_t) * max_len); ((tusb_desc_interface_t *) itf_vendor)->bInterfaceClass = TUSB_CLASS_VENDOR_SPECIFIC; ((tusb_desc_interface_t *) itf_vendor)->bNumEndpoints -= 1; - vendord_open(rhport, - (tusb_desc_interface_t *) itf_vendor, - max_len - sizeof(tusb_desc_endpoint_t)); - TU_ASSERT(usbd_edpt_open(rhport, &desc_ep3), 0); + vendord_open(rhport, (tusb_desc_interface_t *) itf_vendor, max_len - sizeof(tusb_desc_endpoint_t)); + tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *)((uint8_t *)itf_desc + drv_len - sizeof(tusb_desc_endpoint_t)); + TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0); free(itf_vendor); uint8_t msg[] = { 0x50, 0x03 }; - usbd_edpt_xfer(rhport, desc_ep3.bEndpointAddress, msg, sizeof(msg)); + usbd_edpt_xfer(rhport, desc_ep->bEndpointAddress, msg, sizeof(msg)); - uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + - 3 * sizeof(tusb_desc_endpoint_t); TU_VERIFY(max_len >= drv_len, 0); itf_num = itf_desc->bInterfaceNumber; diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index 016a76b..80ea2d2 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -427,15 +427,12 @@ int driver_process_usb_packet_hid(uint16_t read) { memset(ctap_resp, 0, 64); ctap_resp->cid = ctap_req->cid; ctap_resp->init.cmd = ctap_req->init.cmd; -#ifndef ENABLE_EMULATION - pico_unique_board_id_t rpiid; - pico_get_unique_board_id(&rpiid); -#else +#ifdef ENABLE_EMULATION struct { uint8_t id[8]; } rpiid = { 0 }; #endif - memcpy(ctap_resp->init.data, rpiid.id, sizeof(rpiid.id)); + memcpy(ctap_resp->init.data, pico_serial.id, sizeof(pico_serial.id)); ctap_resp->init.bcntl = 16; hid_write(64); msg_packet.len = msg_packet.current_len = 0; From cf1e0764539f6d3c139233fb63c845548de2fb65 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 17 Apr 2024 11:40:01 +0200 Subject: [PATCH 3/6] Introducing EF_PHY to store PHY (VIDPID and LED no.). Signed-off-by: Pol Henarejos --- src/fs/file.c | 20 ++++++++++---------- src/fs/file.h | 15 ++++++++++++--- src/usb/usb.c | 17 ++++++++++++++++- src/usb/usb_descriptors.c | 4 ++-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 3aa0742..6e21340 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -41,6 +41,11 @@ extern uint8_t flash_read_uint8(uintptr_t addr); extern uint8_t *flash_read(uintptr_t addr); extern void low_flash_available(); +#ifndef ENABLE_EMULATION +file_t sef_phy = {.fid = EF_PHY, .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff}}; +file_t *ef_phy = &sef_phy; +#endif + //puts FCI in the RAPDU void process_fci(const file_t *pe, int fmd) { res_APDU_size = 0; @@ -144,7 +149,11 @@ file_t *search_by_name(uint8_t *name, uint16_t namelen) { } file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp) { - +#ifndef ENABLE_EMULATION + if (fid == EF_PHY) { + return ef_phy; + } +#endif for (file_t *p = file_entries; p != file_last; p++) { if (p->fid != 0x0000 && p->fid == fid) { if (!parent || (parent && is_parent(p, parent))) { @@ -263,9 +272,6 @@ void scan_region(bool persistent) { } } void wait_flash_finish(); -#ifndef ENABLE_EMULATION -extern uint16_t usb_vid, usb_pid; -#endif void scan_flash() { initialize_flash(false); //soft initialization if (*(uintptr_t *) flash_read(end_rom_pool) == 0xffffffff && @@ -281,12 +287,6 @@ void scan_flash() { printf("SCAN\n"); scan_region(true); scan_region(false); -#ifndef ENABLE_EMULATION - file_t *ef_vp = search_dynamic_file(EF_VP); - if (file_has_data(ef_vp)) { - - } -#endif } uint8_t *file_read(const uint8_t *addr) { diff --git a/src/fs/file.h b/src/fs/file.h index c3b7478..a74d002 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -67,9 +67,14 @@ #define EF_SKDFS 0x6045 #define EF_META 0xE010 -#ifndef ENABLE_EMULATION -#define EF_VP 0xE020 -#endif +#define EF_PHY 0xE020 + +#define PHY_VID 0x0 +#define PHY_PID 0x2 +#define PHY_LED_GPIO 0x4 +#define PHY_LED_MODE 0x5 + +#define PHY_MAX_SIZE 6 #define MAX_DEPTH 4 @@ -129,4 +134,8 @@ extern int meta_delete(uint16_t fid); extern int meta_add(uint16_t fid, const uint8_t *data, uint16_t len); extern int delete_file(file_t *ef); +#ifndef ENABLE_EMULATION +extern file_t *ef_phy; +#endif + #endif diff --git a/src/usb/usb.c b/src/usb/usb.c index bd0d794..b0d2972 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -21,13 +21,16 @@ #ifndef ENABLE_EMULATION #include "pico/stdlib.h" #include "pico/multicore.h" -#include "tusb.h" #include "bsp/board.h" #endif #include "pico_keys.h" #include "usb.h" #include "apdu.h" +#ifndef ENABLE_EMULATION +#include "tusb.h" +#endif + // For memcpy #include #include @@ -158,8 +161,20 @@ queue_t usb_to_card_q; queue_t card_to_usb_q; #endif +#ifndef ENABLE_EMULATION +extern tusb_desc_device_t desc_device; +#endif void usb_init() { #ifndef ENABLE_EMULATION + uint16_t usb_vid = USB_VID, usb_pid = USB_PID; + if (file_has_data(ef_phy) && file_get_size(ef_phy) >= 4) { + uint8_t *data = file_get_data(ef_phy); + usb_vid = (data[PHY_VID] << 8) | data[PHY_VID+1]; + usb_pid = (data[PHY_PID] << 8) | data[PHY_PID+1]; + } + desc_device.idVendor = usb_vid; + desc_device.idProduct = usb_pid; + queue_init(&card_to_usb_q, sizeof(uint32_t), 64); queue_init(&usb_to_card_q, sizeof(uint32_t), 64); #endif diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 92a7cb2..7591607 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -20,6 +20,7 @@ #include "pico/unique_id.h" #include "pico_keys_version.h" #include "usb.h" +#include "pico_keys.h" #ifndef USB_VID #define USB_VID 0xFEFF @@ -34,11 +35,10 @@ #define MAX_USB_POWER 1 - //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ -tusb_desc_device_t const desc_device = { +tusb_desc_device_t desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = (USB_BCD), From 6f2721aba343adab1aa0a336e4b15e41fbb77569 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 17 Apr 2024 11:42:57 +0200 Subject: [PATCH 4/6] Use persistent memory for storing dynamic VID/PID. Signed-off-by: Pol Henarejos --- src/fs/file.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fs/file.h b/src/fs/file.h index a74d002..6eb3556 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -138,4 +138,4 @@ extern int delete_file(file_t *ef); extern file_t *ef_phy; #endif -#endif +#endif // _FILE_H_ From ef196bf10ba29410df712b54beef8a2c4876300a Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 17 Apr 2024 16:36:15 +0200 Subject: [PATCH 5/6] Use new methods search_file() and file_put_data(). Signed-off-by: Pol Henarejos --- src/fs/file.c | 43 ++++++++++++++++++++++++++----------------- src/fs/file.h | 5 ++++- src/pico_keys.h | 2 -- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 6e21340..3e0bef1 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -27,18 +27,12 @@ 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_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_word(uintptr_t addr, uint32_t data); -extern int flash_program_uintptr(uintptr_t addr, uintptr_t data); extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len); extern uintptr_t flash_read_uintptr(uintptr_t addr); extern uint16_t flash_read_uint16(uintptr_t addr); extern uint8_t flash_read_uint8(uintptr_t addr); extern uint8_t *flash_read(uintptr_t addr); +extern int flash_clear_file(file_t *ef); extern void low_flash_available(); #ifndef ENABLE_EMULATION @@ -168,6 +162,14 @@ file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp return NULL; } +file_t *search_file(const uint16_t fid) { + file_t *ef = search_by_fid(fid, NULL, SPECIFY_EF); + if (ef) { + return ef; + } + return search_dynamic_file(fid); +} + uint8_t make_path_buf(const file_t *pe, uint8_t *buf, uint8_t buflen, const file_t *top) { if (!buflen) { return 0; @@ -295,8 +297,11 @@ uint8_t *file_read(const uint8_t *addr) { uint16_t file_read_uint16(const uint8_t *addr) { return flash_read_uint16((uintptr_t) addr); } -uint8_t file_read_uint8(const uint8_t *addr) { - return flash_read_uint8((uintptr_t) addr); +uint8_t file_read_uint8_offset(const file_t *ef, const uint16_t offset) { + return flash_read_uint8((uintptr_t) (file_get_data(ef) + offset)); +} +uint8_t file_read_uint8(const file_t *ef) { + return file_read_uint8_offset(ef, 0); } uint8_t *file_get_data(const file_t *tf) { @@ -313,6 +318,10 @@ uint16_t file_get_size(const file_t *tf) { return file_read_uint16(tf->data); } +int file_put_data(file_t *file, const uint8_t *data, uint16_t len) { + return flash_write_data_to_file(file, data, len); +} + file_t *search_dynamic_file(uint16_t fid) { for (int i = 0; i < dynamic_files; i++) { if (dynamic_file[i].fid == fid) { @@ -340,7 +349,7 @@ int delete_dynamic_file(file_t *f) { file_t *file_new(uint16_t fid) { file_t *f; - if ((f = search_dynamic_file(fid)) || (f = search_by_fid(fid, NULL, SPECIFY_EF))) { + if ((f = search_file(fid))) { return f; } if (dynamic_files == MAX_DYNAMIC_FILES) { @@ -362,7 +371,7 @@ file_t *file_new(uint16_t fid) { return f; } uint16_t meta_find(uint16_t fid, uint8_t **out) { - file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF); + file_t *ef = search_file(EF_META); if (!ef) { return 0; } @@ -386,7 +395,7 @@ uint16_t meta_find(uint16_t fid, uint8_t **out) { return 0; } int meta_delete(uint16_t fid) { - file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF); + file_t *ef = search_file(EF_META); if (!ef) { return CCID_ERR_FILE_NOT_FOUND; } @@ -415,7 +424,7 @@ int meta_delete(uint16_t fid) { if (ctxi.data + ctxi.len > p) { memcpy(fdata + (tpos - ctxi.data), p, ctxi.data + ctxi.len - p); } - int r = flash_write_data_to_file(ef, fdata, new_len); + int r = file_put_data(ef, fdata, new_len); free(fdata); if (r != CCID_OK) { return CCID_EXEC_ERROR; @@ -429,7 +438,7 @@ int meta_delete(uint16_t fid) { } int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { int r; - file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF); + file_t *ef = search_file(EF_META); if (!ef) { return CCID_ERR_FILE_NOT_FOUND; } @@ -449,7 +458,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { if (cfid == fid) { if (tag_len - 2 == len) { //an update memcpy(p - tag_len + 2, data, len); - r = flash_write_data_to_file(ef, fdata, ef_size); + r = file_put_data(ef, fdata, ef_size); free(fdata); if (r != CCID_OK) { return CCID_EXEC_ERROR; @@ -478,7 +487,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { *f++ = fid >> 8; *f++ = fid & 0xff; memcpy(f, data, len); - r = flash_write_data_to_file(ef, fdata, ef_size); + r = file_put_data(ef, fdata, ef_size); free(fdata); if (r != CCID_OK) { return CCID_EXEC_ERROR; @@ -494,7 +503,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { *f++ = fid >> 8; *f++ = fid & 0xff; memcpy(f, data, len); - r = flash_write_data_to_file(ef, fdata, ef_size + (uint16_t)asn1_len_tag(fid & 0x1f, len + 2)); + r = file_put_data(ef, fdata, ef_size + (uint16_t)asn1_len_tag(fid & 0x1f, len + 2)); free(fdata); if (r != CCID_OK) { return CCID_EXEC_ERROR; diff --git a/src/fs/file.h b/src/fs/file.h index 6eb3556..2fe438d 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -105,6 +105,7 @@ extern file_t *file_sopin; extern file_t *file_retries_sopin; extern file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp); +extern file_t *search_file(const uint16_t fid); extern file_t *search_by_name(uint8_t *name, uint16_t namelen); extern file_t *search_by_path(const uint8_t *pe_path, uint8_t pathlen, const file_t *parent); extern bool authenticate_action(const file_t *ef, uint8_t op); @@ -116,9 +117,11 @@ extern file_t file_entries[]; extern uint8_t *file_read(const uint8_t *addr); 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 file_t *ef); +extern uint8_t file_read_uint8_offset(const file_t *ef, const uint16_t offset); extern uint8_t *file_get_data(const file_t *tf); extern uint16_t file_get_size(const file_t *tf); +extern int file_put_data(file_t *file, const uint8_t *data, uint16_t len); extern file_t *file_new(uint16_t); file_t *get_parent(file_t *f); diff --git a/src/pico_keys.h b/src/pico_keys.h index 2b8d9d3..0fc6b0d 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -62,9 +62,7 @@ static inline void put_uint16_t(uint16_t n, uint8_t *b) { *b = n & 0xff; } -extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len); extern void low_flash_available(); -extern int flash_clear_file(file_t *file); extern void timeout_stop(); extern void timeout_start(); From a5f19a135673d5bd4edb9f10cea65758a3e7e7d5 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 21 Apr 2024 22:51:17 +0200 Subject: [PATCH 6/6] Only T=1 protocol is supported. Signed-off-by: Pol Henarejos --- src/usb/usb_descriptors.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 7591607..aaa3fdc 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -96,9 +96,7 @@ static const struct ccid_class_descriptor desc_ccid = { .bcdCCID = (0x0110), .bMaxSlotIndex = 0, .bVoltageSupport = 0x01, // 5.0V - .dwProtocols = ( - 0x01 | // T=0 - 0x02), // T=1 + .dwProtocols = (0x02), // T=1 .dwDefaultClock = (0xDFC), .dwMaximumClock = (0xDFC), .bNumClockSupport = 0,