From 023039deb2f2fd81d7e6173b3ec12ffb3002b4db Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 4 Apr 2024 21:01:55 +0200 Subject: [PATCH 01/18] Attempting to add support to esp32s3. Will it work? Who knows... Signed-off-by: Pol Henarejos --- CMakeLists.txt | 14 ++++++-- partitions.csv | 6 ++++ pico_keys_sdk_import.cmake | 7 +++- sdkconfig.defaults | 16 +++++++++ src/CMakeLists.txt | 5 +++ src/apdu.c | 6 ++++ src/apdu.h | 2 +- src/asn1.h | 2 +- src/esp_compat.h | 67 +++++++++++++++++++++++++++++++++++ src/fs/file.h | 2 +- src/fs/flash.c | 8 ++--- src/fs/low_flash.c | 6 +++- src/idf_component.yml | 4 +++ src/main.c | 72 +++++++++++++++++++++++--------------- src/pico_keys.h | 10 +++++- src/rng/hwrng.c | 38 +++++++++++++------- src/rng/hwrng.h | 2 +- src/rng/random.c | 12 ------- src/rng/random.h | 3 -- src/usb/hid/hid.c | 2 ++ src/usb/tusb_config.h | 2 +- src/usb/usb.c | 4 +-- src/usb/usb.h | 11 ++++-- src/usb/usb_descriptors.c | 2 ++ 24 files changed, 229 insertions(+), 74 deletions(-) create mode 100755 partitions.csv create mode 100755 sdkconfig.defaults create mode 100755 src/CMakeLists.txt create mode 100644 src/esp_compat.h create mode 100755 src/idf_component.yml diff --git a/CMakeLists.txt b/CMakeLists.txt index 30b9be0..1a81050 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,16 @@ # along with this program. If not, see . # - cmake_minimum_required(VERSION 3.13) + cmake_minimum_required(VERSION 3.16) + +if(ESP_PLATFORM) +set(EXTRA_COMPONENT_DIRS src) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(USB_ITF_CCID 1) +set(USB_ITF_HID 1) +include(pico_keys_sdk_import.cmake) +project(pico_keys_sdk) +else() if(ENABLE_EMULATION) else() @@ -28,7 +37,7 @@ set(CMAKE_CXX_STANDARD 17) if(ENABLE_EMULATION) - else() + else() pico_sdk_init() endif() @@ -70,3 +79,4 @@ pico_add_extra_outputs(pico_keys_sdk_exe) target_link_libraries(pico_keys_sdk_exe PRIVATE pico_keys_sdk pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc tinyusb_device tinyusb_board) endif() +endif() diff --git a/partitions.csv b/partitions.csv new file mode 100755 index 0000000..1c79321 --- /dev/null +++ b/partitions.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1M, +storage, data, fat, , 1M, diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index b3ec23d..0819921 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -84,7 +84,9 @@ if(USB_ITF_CCID) message(STATUS "USB CCID Interface:\t\t enabled") endif(USB_ITF_CCID) add_definitions(-DDEBUG_APDU=${DEBUG_APDU}) +if (NOT ESP_PLATFORM) add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h") +endif() message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}") @@ -263,13 +265,16 @@ if (MSVC) COMPILE_FLAGS " -W3 -wd4242 -wd4065" ) endif() +set(INTERNAL_SOURCES ${SOURCES}) +set(SOURCES ${SOURCES} ${EXTERNAL_SOURCES}) if (NOT TARGET pico_keys_sdk) if (ENABLE_EMULATION) add_impl_library(pico_keys_sdk) + elseif(ESP_PLATFORM) + add_impl_library(pico_keys_sdk) else() pico_add_library(pico_keys_sdk) endif() - set(SOURCES ${SOURCES} ${EXTERNAL_SOURCES}) target_sources(pico_keys_sdk INTERFACE ${SOURCES} ) diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100755 index 0000000..12e3b2f --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1,16 @@ +# This file was generated using idf.py save-defconfig. It can be edited manually. +# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration +# +CONFIG_TINYUSB=y +CONFIG_TINYUSB_MSC_ENABLED=y + +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_WL_SECTOR_SIZE_512=y +CONFIG_WL_SECTOR_MODE_PERF=y + +CONFIG_TINYUSB_CDC_ENABLED=y +# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set +CONFIG_TINYUSB_DESC_CUSTOM_PID=0x4001 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100755 index 0000000..9e427f1 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS ${INTERNAL_SOURCES} + INCLUDE_DIRS . fs rng usb + PRIV_REQUIRES bootloader_support esp_partition +) diff --git a/src/apdu.c b/src/apdu.c index 7ac3a81..999bc20 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -196,7 +196,10 @@ void apdu_thread() { card_init_core1(); while (1) { uint32_t m = 0; +#if defined(ESP_PLATFORM) +#else queue_remove_blocking(&usb_to_card_q, &m); +#endif if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE) { set_res_sw(0x6f, 0x00); @@ -213,7 +216,10 @@ done: ; finished_data_size = apdu_next(); uint32_t flag = EV_EXEC_FINISHED; +#if defined(ESP_PLATFORM) +#else queue_add_blocking(&card_to_usb_q, &flag); +#endif } //printf("EXIT !!!!!!\r\n"); if (current_app && current_app->unload) { diff --git a/src/apdu.h b/src/apdu.h index e7ae1c8..54260b1 100644 --- a/src/apdu.h +++ b/src/apdu.h @@ -19,7 +19,7 @@ #define _APDU_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #endif #include "compat.h" diff --git a/src/asn1.h b/src/asn1.h index 3d4c5e1..ae2e471 100644 --- a/src/asn1.h +++ b/src/asn1.h @@ -19,7 +19,7 @@ #define _ASN1_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #else #include diff --git a/src/esp_compat.h b/src/esp_compat.h new file mode 100644 index 0000000..dba3c20 --- /dev/null +++ b/src/esp_compat.h @@ -0,0 +1,67 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ESP_COMPAT_H_ +#define __ESP_COMPAT_H_ + +#ifdef ESP_PLATFORM + +#include "freertos/freertos.h" +#include "freertos/queue.h" +typedef QueueHandle_t queue_t; +#define queue_init(a,b,c) do { *(a) = xQueueCreate(c, b); } while(0) +#define queue_add_blocking(a,b) xQueueSend(*(a), b, portMAX_DELAY) +#define queue_try_add(a,b) xQueueSend(*(a), b, 0) +#define queue_is_empty(a) (uxQueueMessagesWaiting(*(a)) == 0) +#define queue_try_remove(a,b) xQueueReceive(*(a), b, 0) +extern TaskHandle_t hcore0, hcore1; +#define multicore_launch_core1(a) xTaskCreate(a, "core1", 512, NULL, tskIDLE_PRIORITY, &hcore1) +#define multicore_reset_core1 vTaskEndScheduler +#define sleep_ms(a) vTaskDelay(a / portTICK_PERIOD_MS) +#define board_millis xTaskGetTickCount +#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 +typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; +#define pico_get_unique_board_id(a) memset(a, 0, sizeof(pico_unique_board_id_t)) +#define pico_get_unique_board_id_string(a, b) \ + do { \ + pico_unique_board_id_t t; \ + pico_get_unique_board_id(&t); \ + snprintf(a, b, "%02X%02X%02X%02X%02X%02X%02X%02X", t.id[0], t.id[1], t.id[2], t.id[3], t.id[4], t.id[5], t.id[6], t.id[7]); } while(0) +typedef SemaphoreHandle_t mutex_t; +typedef SemaphoreHandle_t semaphore_t; +#define mutex_init(a) do { *(a) = xSemaphoreCreateMutex();} while(0) +#define mutex_try_enter(a,b) xSemaphoreTake(*(a), 0) +#define mutex_enter_blocking(a) xSemaphoreTake(*(a), portMAX_DELAY) +#define mutex_exit(a) xSemaphoreGive(*(a)) +#define sem_init(a,b,c) do { *(a) = xSemaphoreCreateCounting(c, b); } while(0) +#define sem_release(a) xSemaphoreGive(*(a)) +#define sem_acquire_blocking(a) xSemaphoreTake(*(a), portMAX_DELAY) +#define multicore_lockout_victim_init() (void)0 +static inline bool multicore_lockout_start_timeout_us(int a) { + vTaskSuspend(hcore1); return true; } +static inline bool multicore_lockout_end_timeout_us(int a) { + vTaskResume(hcore1); return true; } +#define save_and_disable_interrupts() 1 + +#include "esp_partition.h" +extern esp_partition_t part0; +#define flash_range_erase(a,b) esp_partition_erase_range(&part0, a, b) +#define flash_range_program(a,b,c) esp_partition_write(&part0, a, b, c) +#define restore_interrupts(a) (void)a +#endif + +#endif diff --git a/src/fs/file.h b/src/fs/file.h index 157e022..031d0ba 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -20,7 +20,7 @@ #define _FILE_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #else #include diff --git a/src/fs/flash.c b/src/fs/flash.c index c94a35e..8d83a7e 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -19,13 +19,13 @@ #include #include -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#include "hardware/flash.h" -#else +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) #define XIP_BASE 0 #define FLASH_SECTOR_SIZE 4096 #define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024) +#else +#include "pico/stdlib.h" +#include "hardware/flash.h" #endif #include "pico_keys.h" #include "file.h" diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index bb2cf5c..2383678 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -20,7 +20,7 @@ #include #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #include "hardware/flash.h" #include "hardware/sync.h" @@ -38,15 +38,19 @@ #define lseek _lseek #include "mman.h" #else +#ifdef ESP_PLATFORM +#else #include #include #endif #include #define FLASH_SECTOR_SIZE 4096 #define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024) +#define XIP_BASE 0 int fd_map = 0; uint8_t *map = NULL; #endif +#endif #include "pico_keys.h" #include diff --git a/src/idf_component.yml b/src/idf_component.yml new file mode 100755 index 0000000..06b047d --- /dev/null +++ b/src/idf_component.yml @@ -0,0 +1,4 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/esp_tinyusb: "^1.2" + idf: "^5.0" diff --git a/src/main.c b/src/main.c index f7fc085..269b30c 100644 --- a/src/main.c +++ b/src/main.c @@ -18,13 +18,15 @@ #include // Pico -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#else + +#if defined(ENABLE_EMULATION) #if !defined(_MSC_VER) #include #endif #include "emulation.h" +#elif defined(ESP_PLATFORM) +#else +#include "pico/stdlib.h" #endif // For memcpy @@ -170,7 +172,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 +315,40 @@ void execute_tasks() { led_blinking_task(); } +void core0_loop() { + while (1) { + execute_tasks(); + neug_task(); + do_flash(); +#ifndef ENABLE_EMULATION + if (board_millis() > 1000 && !is_busy()) { // wait 1 second to boot up + bool current_button_state = board_button_read(); + if (current_button_state != button_pressed_state) { + if (current_button_state == false) { // unpressed + if (button_pressed_time == 0 || button_pressed_time + 1000 > board_millis()) { + button_press++; + } + button_pressed_time = board_millis(); + } + button_pressed_state = current_button_state; + } + if (button_pressed_time > 0 && button_press > 0 && button_pressed_time + 1000 < board_millis() && button_pressed_state == false) { + if (button_pressed_cb != NULL) { + (*button_pressed_cb)(button_press); + } + button_pressed_time = button_press = 0; + } + } +#endif + } +} + +#ifdef ESP_PLATFORM +TaskHandle_t hcore0 = NULL, hcore1 = NULL; +void app_main(void) { +#else int main(void) { +#endif #ifndef ENABLE_EMULATION usb_init(); @@ -351,31 +386,12 @@ int main(void) { //ccid_prepare_receive(&ccid); - while (1) { - execute_tasks(); - neug_task(); - do_flash(); -#ifndef ENABLE_EMULATION - if (board_millis() > 1000 && !is_busy()) { // wait 1 second to boot up - bool current_button_state = board_button_read(); - if (current_button_state != button_pressed_state) { - if (current_button_state == false) { // unpressed - if (button_pressed_time == 0 || button_pressed_time + 1000 > board_millis()) { - button_press++; - } - button_pressed_time = board_millis(); - } - button_pressed_state = current_button_state; - } - if (button_pressed_time > 0 && button_press > 0 && button_pressed_time + 1000 < board_millis() && button_pressed_state == false) { - if (button_pressed_cb != NULL) { - (*button_pressed_cb)(button_press); - } - button_pressed_time = button_press = 0; - } - } +#ifdef ESP_PLATFORM + xTaskCreate(core0_loop, "core0", 512, NULL, tskIDLE_PRIORITY, &hcore0); + vTaskStartScheduler(); +#else + core0_loop(); #endif - } return 0; } diff --git a/src/pico_keys.h b/src/pico_keys.h index f9b28d9..850deb1 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -19,7 +19,7 @@ #define _PICO_KEYS_H_ #include "file.h" -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/unique_id.h" #else #include @@ -47,6 +47,14 @@ extern uint32_t board_millis(); #endif #include +#if defined(ENABLE_EMULATION) +#include +#elif defined(ESP_PLATFORM) +#include "esp_compat.h" +#else +#include "pico/util/queue.h" +#endif + extern bool wait_button(); extern void low_flash_init_core1(); diff --git a/src/rng/hwrng.c b/src/rng/hwrng.c index 1f778c2..d57859f 100644 --- a/src/rng/hwrng.c +++ b/src/rng/hwrng.c @@ -18,7 +18,17 @@ #include #include #include -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" + +mbedtls_ctr_drbg_context ctr_drbg; +extern uint32_t board_millis(); +#elif (ESP_PLATFORM) +#include "bootloader_random.h" +#include "esp_random.h" +#include "esp_compat.h" +#else #include "pico/stdlib.h" #include "hwrng.h" @@ -28,16 +38,13 @@ #include "bsp/board.h" #include "pico/time.h" -#else -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" - -mbedtls_ctr_drbg_context ctr_drbg; -extern uint32_t board_millis(); #endif void adc_start() { -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#elif defined(ESP_PLATFORM) + bootloader_random_enable(); +#else adc_init(); adc_gpio_init(27); adc_select_input(1); @@ -46,7 +53,7 @@ void adc_start() { void adc_stop() { } -#ifdef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) uint32_t adc_read() { return 0; } @@ -76,7 +83,12 @@ static int ep_process() { ep_init(); } uint64_t word = 0x0; -#ifndef ENABLE_EMULATION + +#if defined(ENABLE_EMULATION) + mbedtls_ctr_drbg_random(&ctr_drbg, (uint8_t *) &word, sizeof(word)); +#elif defined(ESP_PLATFORM) + esp_fill_random((uint8_t *)&word, sizeof(word)); +#else for (int n = 0; n < 64; n++) { uint8_t bit1, bit2; do { @@ -86,8 +98,6 @@ static int ep_process() { } while (bit1 == bit2); word = (word << 1) | bit1; } -#else - mbedtls_ctr_drbg_random(&ctr_drbg, (uint8_t *) &word, sizeof(word)); #endif random_word ^= word ^ board_millis() ^ adc_read(); random_word *= 0x00000100000001B3; @@ -200,7 +210,11 @@ uint32_t neug_get() { void neug_wait_full() { struct rng_rb *rb = &the_ring_buffer; #ifndef ENABLE_EMULATION +#ifdef ESP_PLATFORM + uint8_t core = xTaskGetCurrentTaskHandle() == hcore1 ? 1 : 0; +#else uint core = get_core_num(); +#endif #endif while (!rb->full) { #ifndef ENABLE_EMULATION diff --git a/src/rng/hwrng.h b/src/rng/hwrng.h index 857e4ec..cc5f33f 100644 --- a/src/rng/hwrng.h +++ b/src/rng/hwrng.h @@ -21,7 +21,7 @@ #define NEUG_PRE_LOOP 32 #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #endif diff --git a/src/rng/random.c b/src/rng/random.c index 1e097ee..fbbe491 100644 --- a/src/rng/random.c +++ b/src/rng/random.c @@ -65,18 +65,6 @@ void random_bytes_free(const uint8_t *p) { neug_flush(); } -/* - * Return 4-byte salt - */ -void random_get_salt(uint8_t *p) { - uint32_t rnd; - - rnd = neug_get(); - memcpy(p, &rnd, sizeof(uint32_t)); - rnd = neug_get(); - memcpy(p + sizeof(uint32_t), &rnd, sizeof(uint32_t)); -} - /* * Random byte iterator diff --git a/src/rng/random.h b/src/rng/random.h index bb4ad24..b0a5d7b 100644 --- a/src/rng/random.h +++ b/src/rng/random.h @@ -32,9 +32,6 @@ void random_fini(void); const uint8_t *random_bytes_get(size_t); void random_bytes_free(const uint8_t *p); -/* 8-byte salt */ -void random_get_salt(uint8_t *p); - /* iterator returning a byta at a time */ extern int random_gen(void *arg, unsigned char *output, size_t output_len); diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index 016a76b..6ae835e 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -17,8 +17,10 @@ #ifndef ENABLE_EMULATION #include "tusb.h" +#ifndef ESP_PLATFORM #include "bsp/board.h" #endif +#endif #include "ctap_hid.h" #include "pico_keys.h" #include "pico_keys_version.h" diff --git a/src/usb/tusb_config.h b/src/usb/tusb_config.h index 64f419c..6422571 100644 --- a/src/usb/tusb_config.h +++ b/src/usb/tusb_config.h @@ -64,7 +64,7 @@ extern "C" { #endif #ifndef CFG_TUSB_OS -#define CFG_TUSB_OS OPT_OS_PICO +//#define CFG_TUSB_OS OPT_OS_PICO #endif #ifndef CFG_TUSB_DEBUG diff --git a/src/usb/usb.c b/src/usb/usb.c index 94155ed..2a67451 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -18,7 +18,7 @@ #include // Pico -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #include "pico/multicore.h" #include "tusb.h" @@ -153,7 +153,7 @@ void usb_clear_rx(uint8_t itf) { #define USB_BCD 0x0200 -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) queue_t usb_to_card_q; queue_t card_to_usb_q; #endif diff --git a/src/usb/usb.h b/src/usb/usb.h index 8872468..14883c6 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -18,12 +18,17 @@ #ifndef _USB_H_ #define _USB_H_ -#ifndef ENABLE_EMULATION -#include "pico/util/queue.h" -#else +#if defined(ENABLE_EMULATION) #include +#elif defined(ESP_PLATFORM) +#include "esp_compat.h" +#else +#include "pico/util/queue.h" #endif + +#include "esp_compat.h" + /* USB thread */ #define EV_CARD_CHANGE 1 #define EV_TX_FINISHED 2 diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 0d01607..5b0d5ed 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -17,7 +17,9 @@ #include "tusb.h" #include "usb_descriptors.h" +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/unique_id.h" +#endif #include "pico_keys_version.h" #include "usb.h" From 49f05e9e130b837e0f94c42044b628ea168be129 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 5 Apr 2024 17:57:56 +0200 Subject: [PATCH 02/18] Now it builds the image. Signed-off-by: Pol Henarejos --- pico_keys_sdk_import.cmake | 2 ++ sdkconfig.defaults | 8 ++++++-- src/CMakeLists.txt | 4 ++-- src/crypto_utils.c | 5 ++++- src/crypto_utils.h | 4 ---- src/eac.h | 4 ---- src/fs/low_flash.c | 2 ++ src/idf_component.yml | 3 +-- src/main.c | 38 ++++++++++++++++---------------------- src/rng/random.h | 3 --- src/usb/ccid/ccid.c | 21 ++------------------- src/usb/tusb_config.h | 6 ++---- src/usb/usb.c | 2 +- 13 files changed, 38 insertions(+), 64 deletions(-) diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index 0819921..5425498 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -86,6 +86,8 @@ endif(USB_ITF_CCID) add_definitions(-DDEBUG_APDU=${DEBUG_APDU}) if (NOT ESP_PLATFORM) add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h") +else() +add_definitions(-DCFG_TUSB_CONFIG_FILE="../../src/usb/tusb_config.h") endif() message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}") diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 12e3b2f..e029fb7 100755 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -1,8 +1,9 @@ # This file was generated using idf.py save-defconfig. It can be edited manually. # Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration # +IGNORE_UNKNOWN_FILES_FOR_MANAGED_COMPONENTS=1 + CONFIG_TINYUSB=y -CONFIG_TINYUSB_MSC_ENABLED=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" @@ -11,6 +12,9 @@ CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_WL_SECTOR_SIZE_512=y CONFIG_WL_SECTOR_MODE_PERF=y -CONFIG_TINYUSB_CDC_ENABLED=y +CONFIG_TINYUSB_VENDOR_ENABLED=y +CONFIG_TINYUSB_VENDOR_COUNT=2 +CONFIG_TINYUSB_HID_ENABLED=y +CONFIG_TINYUSB_HID_COUNT=2 # CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set CONFIG_TINYUSB_DESC_CUSTOM_PID=0x4001 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e427f1..b7b3e1f 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS ${INTERNAL_SOURCES} - INCLUDE_DIRS . fs rng usb - PRIV_REQUIRES bootloader_support esp_partition + INCLUDE_DIRS . fs rng usb ../mbedtls/include + REQUIRES bootloader_support esp_partition tinyusb ) diff --git a/src/crypto_utils.c b/src/crypto_utils.c index 3c8d53a..aadb069 100644 --- a/src/crypto_utils.c +++ b/src/crypto_utils.c @@ -15,7 +15,10 @@ * along with this program. If not, see . */ -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#elif defined(ESP_PLATFORM) +#include "esp_compat.h" +#else #include #endif #include "mbedtls/md.h" diff --git a/src/crypto_utils.h b/src/crypto_utils.h index 2a8d99b..9c729d5 100644 --- a/src/crypto_utils.h +++ b/src/crypto_utils.h @@ -18,10 +18,6 @@ #ifndef _CRYPTO_UTILS_H_ #define _CRYPTO_UTILS_H_ -#include "stdlib.h" -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#endif #include "mbedtls/ecp.h" #include "mbedtls/md.h" diff --git a/src/eac.h b/src/eac.h index 6343906..f178a01 100644 --- a/src/eac.h +++ b/src/eac.h @@ -18,10 +18,6 @@ #ifndef _EAC_H_ #define _EAC_H_ -#include -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#endif #include "pico_keys.h" typedef enum MSE_protocol { diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index 2383678..0b40636 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -39,6 +39,8 @@ #include "mman.h" #else #ifdef ESP_PLATFORM +#include "esp_compat.h" +esp_partition_t part0; #else #include #include diff --git a/src/idf_component.yml b/src/idf_component.yml index 06b047d..c91e372 100755 --- a/src/idf_component.yml +++ b/src/idf_component.yml @@ -1,4 +1,3 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_tinyusb: "^1.2" - idf: "^5.0" + espressif/tinyusb: "^0.15.0" diff --git a/src/main.c b/src/main.c index 269b30c..2531918 100644 --- a/src/main.c +++ b/src/main.c @@ -25,28 +25,11 @@ #endif #include "emulation.h" #elif defined(ESP_PLATFORM) +#include "tusb.h" #else #include "pico/stdlib.h" #endif -// For memcpy -#include - -#ifndef ENABLE_EMULATION -// Include descriptor struct definitions -//#include "usb_common.h" -// USB register definitions from pico-sdk -#include "hardware/regs/usb.h" -// USB hardware struct definitions from pico-sdk -#include "hardware/structs/usb.h" -// For interrupt enable and numbers -#include "hardware/irq.h" -// For resetting the USB controller -#include "hardware/resets.h" - -#include "pico/multicore.h" -#endif - #include "random.h" #include "pico_keys.h" #include "apdu.h" @@ -101,11 +84,14 @@ static inline void ws2812_program_init(PIO pio, } #endif -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#else #include "usb.h" +#ifndef ESP_PLATFORM #include "hardware/rtc.h" #include "bsp/board.h" #endif +#endif extern void do_flash(); extern void low_flash_init(); @@ -196,6 +182,11 @@ uint32_t board_millis() { } #else +#ifdef ESP_PLATFORM +bool board_button_read() { + return true; +} +#endif bool button_pressed_state = false; uint32_t button_pressed_time = 0; uint8_t button_press = 0; @@ -289,7 +280,9 @@ void led_off_all() { } void init_rtc() { -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#elif defined(ESP_PLATFORM) +#else rtc_init(); datetime_t dt = { .year = 2020, @@ -345,15 +338,16 @@ void core0_loop() { #ifdef ESP_PLATFORM TaskHandle_t hcore0 = NULL, hcore1 = NULL; -void app_main(void) { +int app_main() { #else int main(void) { #endif #ifndef ENABLE_EMULATION usb_init(); - +#ifndef ESP_PLATFORM board_init(); stdio_init_all(); +#endif #ifdef PIMORONI_TINY2040 gpio_init(TINY2040_LED_R_PIN); diff --git a/src/rng/random.h b/src/rng/random.h index b0a5d7b..5899c91 100644 --- a/src/rng/random.h +++ b/src/rng/random.h @@ -21,9 +21,6 @@ #include #include -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#endif void random_init(void); void random_fini(void); diff --git a/src/usb/ccid/ccid.c b/src/usb/ccid/ccid.c index e54c0d8..3bb6974 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -15,28 +15,11 @@ * along with this program. If not, see . */ -#include - -// Pico -#include "pico/stdlib.h" - -// For memcpy -#include - -// Include descriptor struct definitions -//#include "usb_common.h" -// USB register definitions from pico-sdk -#include "hardware/regs/usb.h" -// USB hardware struct definitions from pico-sdk -#include "hardware/structs/usb.h" -// For interrupt enable and numbers -#include "hardware/irq.h" -// For resetting the USB controller -#include "hardware/resets.h" - #include "random.h" #include "pico_keys.h" +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "hardware/rtc.h" +#endif #include "tusb.h" #include "ccid.h" #include "device/usbd_pvt.h" diff --git a/src/usb/tusb_config.h b/src/usb/tusb_config.h index 6422571..c52fdb5 100644 --- a/src/usb/tusb_config.h +++ b/src/usb/tusb_config.h @@ -26,8 +26,6 @@ #ifndef _TUSB_CONFIG_H_ #define _TUSB_CONFIG_H_ -#include "usb.h" - #ifdef __cplusplus extern "C" { #endif @@ -64,7 +62,7 @@ extern "C" { #endif #ifndef CFG_TUSB_OS -//#define CFG_TUSB_OS OPT_OS_PICO +#define CFG_TUSB_OS OPT_OS_FREERTOS #endif #ifndef CFG_TUSB_DEBUG @@ -115,7 +113,7 @@ extern "C" { #ifdef USB_ITF_CCID #define CFG_TUD_VENDOR 2 #else -#define CFG_TUD_VENDOR 0 +#define CFG_TUD_VENDOR 2 #endif // HID buffer size Should be sufficient to hold ID (if any) + Data diff --git a/src/usb/usb.c b/src/usb/usb.c index 2a67451..45bc825 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -153,7 +153,7 @@ void usb_clear_rx(uint8_t itf) { #define USB_BCD 0x0200 -#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) +#if !defined(ENABLE_EMULATION) queue_t usb_to_card_q; queue_t card_to_usb_q; #endif From 06fd241f49538d2edf886d3d45b7cc5d2808b51f Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 8 Apr 2024 16:47:16 +0200 Subject: [PATCH 03/18] Second round to make it work for ESP32S3. Signed-off-by: Pol Henarejos --- CMakeLists.txt | 5 +- partitions.csv | 6 +- sdkconfig.defaults | 7 -- src/CMakeLists.txt | 2 +- src/apdu.c | 15 ++- src/eac.c | 10 +- src/esp_compat.h | 15 +-- src/fs/low_flash.c | 14 ++- src/idf_component.yml | 4 +- src/main.c | 42 ++++++-- src/pico_keys.h | 12 ++- src/usb/tusb_config.h | 28 +++--- src/usb/usb_descriptors.c | 200 ++++++++++++++++++-------------------- 13 files changed, 196 insertions(+), 164 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a81050..4e9afdc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,10 @@ if(ESP_PLATFORM) set(EXTRA_COMPONENT_DIRS src) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(USB_ITF_CCID 1) -set(USB_ITF_HID 1) +#set(USB_ITF_HID 1) +set(USB_VID 0x20a0) +set(USB_PID 0x4230) +set(DEBUG_APDU 1) include(pico_keys_sdk_import.cmake) project(pico_keys_sdk) else() diff --git a/partitions.csv b/partitions.csv index 1c79321..9d74ef7 100755 --- a/partitions.csv +++ b/partitions.csv @@ -1,6 +1,4 @@ # Name, Type, SubType, Offset, Size, Flags # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap -nvs, data, nvs, 0x9000, 0x6000, -phy_init, data, phy, 0xf000, 0x1000, -factory, app, factory, 0x10000, 1M, -storage, data, fat, , 1M, +factory, app, factory, 0x10000, 1M, +part0, data, nvs, 0x200000, 1M, diff --git a/sdkconfig.defaults b/sdkconfig.defaults index e029fb7..3c810cc 100755 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -11,10 +11,3 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_WL_SECTOR_SIZE_512=y CONFIG_WL_SECTOR_MODE_PERF=y - -CONFIG_TINYUSB_VENDOR_ENABLED=y -CONFIG_TINYUSB_VENDOR_COUNT=2 -CONFIG_TINYUSB_HID_ENABLED=y -CONFIG_TINYUSB_HID_COUNT=2 -# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set -CONFIG_TINYUSB_DESC_CUSTOM_PID=0x4001 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7b3e1f..66ea656 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS ${INTERNAL_SOURCES} INCLUDE_DIRS . fs rng usb ../mbedtls/include - REQUIRES bootloader_support esp_partition tinyusb + REQUIRES bootloader_support esp_partition esp_tinyusb ) diff --git a/src/apdu.c b/src/apdu.c index 999bc20..ad5763d 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -19,6 +19,9 @@ #include "pico_keys.h" #include "usb.h" #include +#ifdef ESP_PLATFORM +#include "esp_compat.h" +#endif uint8_t *rdata_gr = NULL; uint16_t rdata_bk = 0x0; @@ -196,8 +199,7 @@ void apdu_thread() { card_init_core1(); while (1) { uint32_t m = 0; -#if defined(ESP_PLATFORM) -#else +#ifndef ENABLE_EMULATION queue_remove_blocking(&usb_to_card_q, &m); #endif @@ -216,9 +218,11 @@ done: ; finished_data_size = apdu_next(); uint32_t flag = EV_EXEC_FINISHED; -#if defined(ESP_PLATFORM) -#else +#ifndef ENABLE_EMULATION queue_add_blocking(&card_to_usb_q, &flag); +#endif +#ifdef ESP_PLATFORM + vTaskDelay(pdMS_TO_TICKS(10)); #endif } //printf("EXIT !!!!!!\r\n"); @@ -226,6 +230,9 @@ done: ; current_app->unload(); current_app = NULL; } +#ifdef ESP_PLATFORM + vTaskDelete(NULL); +#endif } #endif diff --git a/src/eac.c b/src/eac.c index 77181da..7fef272 100644 --- a/src/eac.c +++ b/src/eac.c @@ -103,10 +103,8 @@ int sm_unwrap() { if (r != CCID_OK) { return r; } - uint16_t le = sm_get_le(); - if (le >= 0) { - apdu.ne = le; - } + apdu.ne = sm_get_le(); + uint8_t *body = NULL; uint16_t body_size = 0; bool is87 = false; @@ -306,12 +304,12 @@ int sm_verify() { } uint16_t sm_remove_padding(const uint8_t *data, uint16_t data_len) { - uint16_t i = data_len - 1; + int32_t i = data_len - 1; for (; i >= 0 && data[i] == 0; i--) { ; } if (i < 0 || data[i] != 0x80) { return 0; } - return i; + return (uint16_t)i; } diff --git a/src/esp_compat.h b/src/esp_compat.h index dba3c20..24736fa 100644 --- a/src/esp_compat.h +++ b/src/esp_compat.h @@ -25,14 +25,17 @@ typedef QueueHandle_t queue_t; #define queue_init(a,b,c) do { *(a) = xQueueCreate(c, b); } while(0) #define queue_add_blocking(a,b) xQueueSend(*(a), b, portMAX_DELAY) +#define queue_remove_blocking(a,b) xQueueReceive(*(a), b, portMAX_DELAY) #define queue_try_add(a,b) xQueueSend(*(a), b, 0) #define queue_is_empty(a) (uxQueueMessagesWaiting(*(a)) == 0) #define queue_try_remove(a,b) xQueueReceive(*(a), b, 0) extern TaskHandle_t hcore0, hcore1; -#define multicore_launch_core1(a) xTaskCreate(a, "core1", 512, NULL, tskIDLE_PRIORITY, &hcore1) -#define multicore_reset_core1 vTaskEndScheduler +#define multicore_launch_core1(a) xTaskCreate((void(*)(void *))a, "core1", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 2, &hcore1) +#define multicore_reset_core1() do { if (hcore1) { eTaskState e = eTaskGetState(hcore1); if (e == eRunning) { vTaskDelete(hcore1); }} }while(0) #define sleep_ms(a) vTaskDelay(a / portTICK_PERIOD_MS) -#define board_millis xTaskGetTickCount +static inline uint32_t board_millis(void) { + return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ ); +} #define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; #define pico_get_unique_board_id(a) memset(a, 0, sizeof(pico_unique_board_id_t)) @@ -57,10 +60,8 @@ static inline bool multicore_lockout_end_timeout_us(int a) { vTaskResume(hcore1); return true; } #define save_and_disable_interrupts() 1 -#include "esp_partition.h" -extern esp_partition_t part0; -#define flash_range_erase(a,b) esp_partition_erase_range(&part0, a, b) -#define flash_range_program(a,b,c) esp_partition_write(&part0, a, b, c) +#define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b) +#define flash_range_program(a,b,c) esp_partition_write(part0, a, b, c) #define restore_interrupts(a) (void)a #endif diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index 0b40636..ec96437 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -40,7 +40,8 @@ #else #ifdef ESP_PLATFORM #include "esp_compat.h" -esp_partition_t part0; +#include "esp_partition.h" +const esp_partition_t *part0; #else #include #include @@ -156,14 +157,17 @@ sem_release(&sem_wait); //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); -#else +#if defined(ENABLE_EMULATION) 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); +#else + mutex_init(&mtx_flash); + sem_init(&sem_wait, 0, 1); +#if defined(ESP_PLATFORM) + part0 = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, "part0"); +#endif #endif } diff --git a/src/idf_component.yml b/src/idf_component.yml index c91e372..81670f8 100755 --- a/src/idf_component.yml +++ b/src/idf_component.yml @@ -1,3 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/tinyusb: "^0.15.0" + espressif/esp_tinyusb: "^1.4.4" + #espressif/tinyusb: "^0.15.0" + zorxx/neopixel: "^1.0.4" diff --git a/src/main.c b/src/main.c index 2531918..11a78c8 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ */ #include +#include // Pico @@ -222,6 +223,22 @@ bool wait_button() { struct apdu apdu; +#ifdef ESP_PLATFORM +#include "driver/gpio.h" +#include "neopixel.h" +tNeopixelContext neopixel = NULL; +tNeopixel pixel[] = + { + { 0, NP_RGB(0, 0, 0) }, /* off */ + { 0, NP_RGB(255, 0, 255) }, /* magenta */ + { 0, NP_RGB(255, 0, 0) }, /* green */ + { 0, NP_RGB(0, 255, 0) }, /* red */ + { 0, NP_RGB(0, 0, 255) }, /* red */ + { 0, NP_RGB(255, 255, 0) }, /* yellow */ + { 0, NP_RGB(0, 255, 255) }, /* cyan */ + { 0, NP_RGB(255, 255, 255) }, /* white */ + }; +#endif void led_blinking_task() { static uint32_t start_ms = 0; static uint8_t led_state = false; @@ -254,6 +271,8 @@ void led_blinking_task() { } #elif defined(CYW43_WL_GPIO_LED_PIN) cyw43_arch_gpio_put(led_color, led_state); +#elif ESP_PLATFORM + neopixel_SetPixel(neopixel, &pixel[led_state], 1); #endif led_state ^= 1; // toggle } @@ -299,10 +318,10 @@ void init_rtc() { extern void neug_task(); extern void usb_task(); - -void execute_tasks() { +void execute_tasks() +{ usb_task(); -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) tud_task(); // tinyusb device task #endif led_blinking_task(); @@ -332,23 +351,31 @@ void core0_loop() { button_pressed_time = button_press = 0; } } +#endif +#ifdef ESP_PLATFORM + vTaskDelay(pdMS_TO_TICKS(10)); #endif } } #ifdef ESP_PLATFORM +#include "tinyusb.h" +extern const tinyusb_config_t tusb_cfg; TaskHandle_t hcore0 = NULL, hcore1 = NULL; int app_main() { #else int main(void) { #endif + #ifndef ENABLE_EMULATION +#ifdef ESP_PLATFORM + tinyusb_driver_install(&tusb_cfg); +#endif usb_init(); #ifndef ESP_PLATFORM board_init(); stdio_init_all(); #endif - #ifdef PIMORONI_TINY2040 gpio_init(TINY2040_LED_R_PIN); gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT); @@ -365,7 +392,6 @@ int main(void) { led_off_all(); - tusb_init(); //prepare_ccid(); #else @@ -379,10 +405,12 @@ int main(void) { init_rtc(); //ccid_prepare_receive(&ccid); +#ifdef ESP_PLATFORM + neopixel = neopixel_Init(1, GPIO_NUM_48); +#endif #ifdef ESP_PLATFORM - xTaskCreate(core0_loop, "core0", 512, NULL, tskIDLE_PRIORITY, &hcore0); - vTaskStartScheduler(); + xTaskCreate(core0_loop, "core0", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 1, &hcore0); #else core0_loop(); #endif diff --git a/src/pico_keys.h b/src/pico_keys.h index 850deb1..3e3ad55 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -19,11 +19,11 @@ #define _PICO_KEYS_H_ #include "file.h" -#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) -#include "pico/unique_id.h" -#else +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) #include +#ifdef ENABLE_EMULATION extern uint32_t board_millis(); +#endif #if !defined(MIN) #if defined(_MSC_VER) #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -44,6 +44,8 @@ extern uint32_t board_millis(); _a > _b ? _a : _b; }) #endif #endif +#else +#include "pico/unique_id.h" #endif #include @@ -59,10 +61,10 @@ extern bool wait_button(); extern void low_flash_init_core1(); -static inline const uint16_t make_uint16_t(uint8_t b1, uint8_t b2) { +static inline uint16_t make_uint16_t(uint8_t b1, uint8_t b2) { return (b1 << 8) | b2; } -static inline const uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) { +static inline uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) { return make_uint16_t(b[offset], b[offset + 1]); } static inline void put_uint16_t(uint16_t n, uint8_t *b) { diff --git a/src/usb/tusb_config.h b/src/usb/tusb_config.h index c52fdb5..d90a387 100644 --- a/src/usb/tusb_config.h +++ b/src/usb/tusb_config.h @@ -45,7 +45,7 @@ extern "C" { #endif //-------------------------------------------------------------------- -// COMMON CONFIGURATION +// Common Configuration //-------------------------------------------------------------------- // defined by compiler flags for flexibility @@ -53,20 +53,20 @@ extern "C" { #error CFG_TUSB_MCU must be defined #endif -#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || \ - CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \ - CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) -#else -#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#ifndef CFG_TUSB_OS +#if CFG_TUSB_MCU == OPT_MCU_RP2040 +#define CFG_TUSB_OS OPT_OS_PICO +#elif CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3 +#define CFG_TUSB_OS OPT_OS_FREERTOS +#endif #endif -#ifndef CFG_TUSB_OS -#define CFG_TUSB_OS OPT_OS_FREERTOS +#ifndef CFG_TUSB_RHPORT0_MODE +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED) #endif #ifndef CFG_TUSB_DEBUG -#define CFG_TUSB_DEBUG 1 +#define CFG_TUSB_DEBUG 0 #endif // Enable Device stack @@ -113,11 +113,17 @@ extern "C" { #ifdef USB_ITF_CCID #define CFG_TUD_VENDOR 2 #else -#define CFG_TUD_VENDOR 2 +#define CFG_TUD_VENDOR 0 #endif // HID buffer size Should be sufficient to hold ID (if any) + Data #define CFG_TUD_HID_EP_BUFSIZE 64 +// CDC FIFO size of TX and RX +#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) +#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) + +// CDC Endpoint transfer buffer size, more is faster +#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #ifdef __cplusplus } diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 5b0d5ed..5ad07cc 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -20,6 +20,9 @@ #if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/unique_id.h" #endif +#ifdef ESP_PLATFORM +#include "tinyusb.h" +#endif #include "pico_keys_version.h" #include "usb.h" @@ -61,37 +64,91 @@ tusb_desc_device_t const desc_device = { .bNumConfigurations = 1 }; +#ifndef ESP_PLATFORM uint8_t const *tud_descriptor_device_cb(void) { return (uint8_t const *) &desc_device; } - - +#endif //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ -tusb_desc_configuration_t const desc_config = { - .bLength = sizeof(tusb_desc_configuration_t), - .bDescriptorType = TUSB_DESC_CONFIGURATION, - .wTotalLength = (sizeof(tusb_desc_configuration_t) -#ifdef USB_ITF_CCID - + (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + - 3 * sizeof(tusb_desc_endpoint_t)) - + (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + - 2 * sizeof(tusb_desc_endpoint_t)) -#endif +#define TUD_INTERFACE_DESC_LEN 9 +#define TUD_ENDPOINT_DESC_LEN 7 +#define TUSB_SMARTCARD_LEN 54 +#define TUD_SMARTCARD_DESC_LEN ((TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 3 * TUD_ENDPOINT_DESC_LEN) + (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 2 * TUD_ENDPOINT_DESC_LEN)) + +enum { + TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN #ifdef USB_ITF_HID - + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN + + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN +#endif +#ifdef USB_ITF_CCID + + TUD_SMARTCARD_DESC_LEN #endif - ), - .bNumInterfaces = ITF_TOTAL, - .bConfigurationValue = 1, - .iConfiguration = 4, - .bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, - .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER + 1), }; + +#ifdef USB_ITF_HID +uint8_t const desc_hid_report[] = { + TUD_HID_REPORT_DESC_FIDO_U2F(CFG_TUD_HID_EP_BUFSIZE) +}; +uint8_t const desc_hid_report_kb[] = { + TUD_HID_REPORT_DESC_KEYBOARD(HID_USAGE(HID_USAGE_DESKTOP_GAMEPAD), HID_LOGICAL_MIN(0), HID_LOGICAL_MAX_N(255, 2), HID_REPORT_COUNT(8), HID_REPORT_SIZE(8), HID_FEATURE( HID_DATA | HID_VARIABLE | HID_ABSOLUTE), ) +}; +#define EPNUM_HID 0x04 +#endif + #ifdef USB_ITF_CCID +#define TUD_SMARTCARD_DESCRIPTOR_WEB(_itf, _strix, _epout, _epin, _epsize) \ + 9, TUSB_DESC_INTERFACE, _itf, 0, 2, 0xFF, 0, 0, _strix, \ + 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0 +#define TUD_SMARTCARD_DESCRIPTOR(_itf, _strix, _epout, _epin, _epint, _epsize) \ + 9, TUSB_DESC_INTERFACE, _itf, 0, 3, TUSB_CLASS_SMART_CARD, 0, 0, _strix, \ + 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ + 7, TUSB_DESC_ENDPOINT, _epint, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), 0 +#endif + +const uint8_t desc_config[] = { + TUD_CONFIG_DESCRIPTOR(1, ITF_TOTAL, 4, TUSB_DESC_TOTAL_LEN, USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), +#ifdef USB_ITF_HID + TUD_HID_INOUT_DESCRIPTOR(ITF_HID, ITF_HID + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, TUSB_DIR_IN_MASK | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10), + TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), TUSB_DIR_IN_MASK | (EPNUM_HID + 1), 16, 5), +#endif +#ifdef USB_ITF_CCID + TUD_SMARTCARD_DESCRIPTOR(ITF_CCID, ITF_CCID+5, 1, TUSB_DIR_IN_MASK | 1, TUSB_DIR_IN_MASK | 2, 64), + TUD_SMARTCARD_DESCRIPTOR_WEB(ITF_WCID, ITF_WCID+5, 3, TUSB_DIR_IN_MASK | 3, 64), +#endif +}; + +/* +tusb_desc_configuration_t const desc_config = { + .bLength = sizeof(tusb_desc_configuration_t), + .bDescriptorType = TUSB_DESC_CONFIGURATION, + .wTotalLength = (sizeof(tusb_desc_configuration_t) +#ifdef USB_ITF_CCID + + (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + + 3 * sizeof(tusb_desc_endpoint_t)) + + (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + + 2 * sizeof(tusb_desc_endpoint_t)) +#endif +#ifdef USB_ITF_HID + + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN +#endif + ), + .bNumInterfaces = ITF_TOTAL, + .bConfigurationValue = 1, + .iConfiguration = 4, + .bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, + .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER + 1), +}; +*/ +#ifdef USB_ITF_CCID +/* static const struct ccid_class_descriptor desc_ccid = { .bLength = sizeof(struct ccid_class_descriptor), .bDescriptorType = 0x21, @@ -118,7 +175,7 @@ static const struct ccid_class_descriptor desc_ccid = { .bPINSupport = 0x0, .bMaxCCIDBusySlots = 0x01, }; - +*/ tusb_desc_interface_t const desc_interface = { .bLength = sizeof(tusb_desc_interface_t), .bDescriptorType = TUSB_DESC_INTERFACE, @@ -170,62 +227,9 @@ tusb_desc_endpoint_t const desc_ep3 = { .bInterval = 0 }; -tusb_desc_endpoint_t const desc_ep1_wcid = { - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = (TUSB_DIR_IN_MASK | 1) + 3, - .bmAttributes.xfer = TUSB_XFER_BULK, - .wMaxPacketSize = (64), - .bInterval = 0 -}; - -tusb_desc_endpoint_t const desc_ep2_wcid = { - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = 1 + 3, - .bmAttributes.xfer = TUSB_XFER_BULK, - .wMaxPacketSize = (64), - .bInterval = 0 -}; #endif -static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t) -#ifdef USB_ITF_CCID - + (sizeof(tusb_desc_interface_t) + - sizeof(struct ccid_class_descriptor) + 3 * - sizeof(tusb_desc_endpoint_t)) - + (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + - 2 * sizeof(tusb_desc_endpoint_t)) -#endif -#ifdef USB_ITF_HID - + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN -#endif -]; - #ifdef USB_ITF_HID -uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_FIDO_U2F(CFG_TUD_HID_EP_BUFSIZE) -}; -uint8_t const desc_hid_report_kb[] = { - TUD_HID_REPORT_DESC_KEYBOARD(HID_USAGE(HID_USAGE_DESKTOP_GAMEPAD), HID_LOGICAL_MIN(0), HID_LOGICAL_MAX_N(255, 2), HID_REPORT_COUNT(8), HID_REPORT_SIZE(8), HID_FEATURE( HID_DATA | HID_VARIABLE | HID_ABSOLUTE), ) -}; -#define EPNUM_HID 0x03 - -static uint8_t desc_hid[] = { - TUD_HID_INOUT_DESCRIPTOR(ITF_HID, - ITF_HID + 5, - HID_ITF_PROTOCOL_NONE, - sizeof(desc_hid_report), - EPNUM_HID, - 0x80 | EPNUM_HID, - CFG_TUD_HID_EP_BUFSIZE, - 10) -}; - -static uint8_t desc_hid_kb[] = { - TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, - sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID + 1), 16, 5) -}; #include "apdu.h" uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) { printf("report_cb %d\n", itf); @@ -239,39 +243,15 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) { return NULL; } #endif - +#ifndef ESP_PLATFORM uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { (void) index; // for multiple configurations - - static uint8_t initd = 0; - if (initd == 0) { - uint8_t *p = desc_config_extended; - memcpy(p, &desc_config, sizeof(tusb_desc_configuration_t)); - p += sizeof(tusb_desc_configuration_t); -#ifdef USB_ITF_HID - memcpy(p, &desc_hid, sizeof(desc_hid)); p += sizeof(desc_hid); - memcpy(p, &desc_hid_kb, sizeof(desc_hid_kb)); p += sizeof(desc_hid_kb); -#endif -#ifdef USB_ITF_CCID - memcpy(p, &desc_interface, sizeof(tusb_desc_interface_t)); - p += sizeof(tusb_desc_interface_t); - memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor)); - p += sizeof(struct ccid_class_descriptor); - memcpy(p, &desc_ep1, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); - memcpy(p, &desc_ep2, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); - memcpy(p, &desc_ep3, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); - - memcpy(p, &desc_interface_wcid, sizeof(tusb_desc_interface_t)); - p += sizeof(tusb_desc_interface_t); - memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor)); - p += sizeof(struct ccid_class_descriptor); - memcpy(p, &desc_ep1_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); - memcpy(p, &desc_ep2_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t); -#endif - initd = 1; - } - return (const uint8_t *) desc_config_extended; + printf("tud_descriptor_configuration_cb %d\n",index); + return desc_config; } +#endif + +#ifdef USB_ITF_CCID #define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN) #define MS_OS_20_DESC_LEN 0xB2 @@ -376,7 +356,7 @@ uint8_t const *tud_descriptor_bos_cb(void) { printf("!!!!!!!!!!!! tud_descriptor_bos_cb\n"); return desc_bos; } - +#endif //--------------------------------------------------------------------+ // String Descriptors //--------------------------------------------------------------------+ @@ -397,7 +377,16 @@ char const *string_desc_arr [] = { , "Pico Key WebCCID Interface" #endif }; - +#ifdef ESP_PLATFORM +const tinyusb_config_t tusb_cfg = { + .device_descriptor = &desc_device, + .string_descriptor = string_desc_arr, + .string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]), + .external_phy = false, + .configuration_descriptor = desc_config, +}; +#endif +#ifndef ESP_PLATFORM static uint16_t _desc_str[32]; uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { @@ -441,3 +430,4 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { return _desc_str; } +#endif From 1ba109bd0a46bed18c097d750f5ebee568d734d4 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 8 Apr 2024 19:38:42 +0200 Subject: [PATCH 04/18] Fix TUSB definition. Signed-off-by: Pol Henarejos --- pico_keys_sdk_import.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index 5425498..d1760ed 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -87,7 +87,7 @@ add_definitions(-DDEBUG_APDU=${DEBUG_APDU}) if (NOT ESP_PLATFORM) add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h") else() -add_definitions(-DCFG_TUSB_CONFIG_FILE="../../src/usb/tusb_config.h") +add_definitions(-DCFG_TUSB_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/src/usb/tusb_config.h") endif() message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}") From ab1b245f4f62f253a0cbf8197bea048046c44efa Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 9 Apr 2024 19:58:33 +0200 Subject: [PATCH 05/18] Fix write & read to flash partition. Signed-off-by: Pol Henarejos --- partitions.csv | 5 ++++- src/fs/flash.c | 8 ++++++++ src/fs/low_flash.c | 12 +++++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/partitions.csv b/partitions.csv index 9d74ef7..89fe79f 100755 --- a/partitions.csv +++ b/partitions.csv @@ -1,4 +1,7 @@ # Name, Type, SubType, Offset, Size, Flags # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap + +nvs, data, nvs, 0x9000, 0x6000 +phy_init, data, phy, 0xf000, 0x1000 factory, app, factory, 0x10000, 1M, -part0, data, nvs, 0x200000, 1M, +part0, 0x40, 0x1, 0x200000, 1M, diff --git a/src/fs/flash.c b/src/fs/flash.c index 8d83a7e..430b1d1 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -22,7 +22,11 @@ #if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) #define XIP_BASE 0 #define FLASH_SECTOR_SIZE 4096 +#ifdef ESP_PLATFORM +#define PICO_FLASH_SIZE_BYTES (1 * 1024 * 1024) +#else #define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024) +#endif #else #include "pico/stdlib.h" #include "hardware/flash.h" @@ -38,7 +42,11 @@ * | | * ------------------------------------------------------ */ +#ifdef ESP_PLATFORM +#define FLASH_TARGET_OFFSET 0 +#else #define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES >> 1) // DATA starts at the mid of flash +#endif #define FLASH_DATA_HEADER_SIZE (sizeof(uintptr_t) + sizeof(uint32_t)) #define FLASH_PERMANENT_REGION (4 * FLASH_SECTOR_SIZE) // 4 sectors (16kb) of permanent memory diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index ec96437..b5c2837 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -166,7 +166,8 @@ void low_flash_init() { mutex_init(&mtx_flash); sem_init(&sem_wait, 0, 1); #if defined(ESP_PLATFORM) - part0 = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, "part0"); + part0 = esp_partition_find_first(0x40, 0x1, "part0"); + esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map); #endif #endif } @@ -208,12 +209,12 @@ page_flash_t *find_free_page(uintptr_t addr) { flash_pages[r].address == addr_alg) { //first available p = &flash_pages[r]; if (!flash_pages[r].ready && !flash_pages[r].erase) { -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) memcpy(p->page, (uint8_t *) addr_alg, FLASH_SECTOR_SIZE); #else memcpy(p->page, (addr >= start_data_pool && - addr <= end_rom_pool) ? (uint8_t *) (map + addr_alg) : (uint8_t *) addr_alg, + addr <= end_rom_pool + sizeof(uintptr_t)) ? (uint8_t *) (map + addr_alg) : (uint8_t *) addr_alg, FLASH_SECTOR_SIZE); #endif ready_pages++; @@ -289,8 +290,9 @@ uint8_t *flash_read(uintptr_t addr) { uint8_t *v = (uint8_t *) addr; #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); -#else - if (addr >= start_data_pool && addr <= end_rom_pool) { +#endif +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) + if (addr >= start_data_pool && addr <= end_rom_pool + sizeof(uintptr_t)) { v += (uintptr_t) map; } #endif From 6adfe4344f66dcc7d42aa04a95ffd637bf819f08 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 10 Apr 2024 18:16:05 +0200 Subject: [PATCH 06/18] When flash is written, map must be reopened. Signed-off-by: Pol Henarejos --- src/fs/low_flash.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index b5c2837..791e3f7 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -42,6 +42,10 @@ #include "esp_compat.h" #include "esp_partition.h" const esp_partition_t *part0; +#define save_and_disable_interrupts() 1 +#define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b) +#define flash_range_program(a,b,c) esp_partition_write(part0, a, b, c); +#define restore_interrupts(a) (void)a #else #include #include @@ -94,15 +98,15 @@ void do_flash() { 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"); + //printf(" DO_FLASH AVAILABLE\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); + //printf("WRITTING %X\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); + //printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE); uint32_t ints = save_and_disable_interrupts(); flash_range_erase(flash_pages[r].address - XIP_BASE, FLASH_SECTOR_SIZE); flash_range_program(flash_pages[r].address - XIP_BASE, @@ -112,7 +116,7 @@ void do_flash() { while (multicore_lockout_end_timeout_us(1000) == false) { ; } - //printf("WRITEN %X !\r\n",flash_pages[r].address); + //printf("WRITEN %X !\n",flash_pages[r].address); #else memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE); #endif @@ -124,7 +128,7 @@ void do_flash() { while (multicore_lockout_start_timeout_us(1000) == false) { ; } - //printf("WRITTING\r\n"); + //printf("WRITTING\n"); flash_range_erase(flash_pages[r].address - XIP_BASE, flash_pages[r].page_size ? ((int) (flash_pages[r].page_size / FLASH_SECTOR_SIZE)) * @@ -147,6 +151,10 @@ void do_flash() { } } flash_available = false; +#ifdef ESP_PLATFORM + esp_partition_munmap(fd_map); + esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map); +#endif #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); } @@ -241,18 +249,18 @@ int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) { #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); #endif - printf("ERROR: ALL FLASH PAGES CACHED\r\n"); + printf("ERROR: ALL FLASH PAGES CACHED\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"); + printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\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); + //printf("Flash: modified page %X with data %x at [%x]\n",(uintptr_t)addr,(uintptr_t)data,addr&(FLASH_SECTOR_SIZE-1)); #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); #endif @@ -329,11 +337,11 @@ int flash_erase_page(uintptr_t addr, size_t page_size) { #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); #endif - printf("ERROR: ALL FLASH PAGES CACHED\r\n"); + printf("ERROR: ALL FLASH PAGES CACHED\n"); return CCID_ERR_NO_MEMORY; } if (!(p = find_free_page(addr))) { - printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n"); + printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\n"); #ifndef ENABLE_EMULATION mutex_exit(&mtx_flash); #endif From 7def35f87cfb79ac52c203a31bb5282b20dde0a9 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 10 Apr 2024 18:16:38 +0200 Subject: [PATCH 07/18] Remove carriage return \r for better debug. Signed-off-by: Pol Henarejos --- src/apdu.c | 6 +++--- src/apdu.h | 8 ++++---- src/esp_compat.h | 4 ---- 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 +- 8 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/apdu.c b/src/apdu.c index ad5763d..61e96e8 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -124,9 +124,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) { @@ -225,7 +225,7 @@ done: ; vTaskDelay(pdMS_TO_TICKS(10)); #endif } - //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 54260b1..e30e12b 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/esp_compat.h b/src/esp_compat.h index 24736fa..dd70088 100644 --- a/src/esp_compat.h +++ b/src/esp_compat.h @@ -58,11 +58,7 @@ static inline bool multicore_lockout_start_timeout_us(int a) { vTaskSuspend(hcore1); return true; } static inline bool multicore_lockout_end_timeout_us(int a) { vTaskResume(hcore1); return true; } -#define save_and_disable_interrupts() 1 -#define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b) -#define flash_range_program(a,b,c) esp_partition_write(part0, a, b, c) -#define restore_interrupts(a) (void)a #endif #endif 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 430b1d1..75527c2 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -83,8 +83,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 @@ -129,7 +129,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) { @@ -138,7 +138,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; } @@ -173,7 +173,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 3bb6974..65786d6 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -140,7 +140,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) { @@ -163,7 +163,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); @@ -267,19 +267,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); @@ -310,13 +310,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, @@ -354,7 +354,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 45bc825..78fc553 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 5ad07cc..186db0f 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -331,7 +331,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 10ab0d561cee68212003be18508b58bb826140ac Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 11 Apr 2024 15:15:18 +0200 Subject: [PATCH 08/18] Upate build parameters. Signed-off-by: Pol Henarejos --- pico_keys_sdk_import.cmake | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index d1760ed..bcad1ca 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -92,7 +92,7 @@ endif() message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}") -set(EXTERNAL_SOURCES +set(MBEDTLS_SOURCES ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/aes.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/asn1parse.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/asn1write.c @@ -157,13 +157,15 @@ set(INCLUDES ${INCLUDES} ) if(USB_ITF_HID OR ENABLE_EMULATION) - set(EXTERNAL_SOURCES ${EXTERNAL_SOURCES} + set(MBEDTLS_SOURCES ${MBEDTLS_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/x509write_crt.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/x509_create.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/x509write_csr.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/pk.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/pk_wrap.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/pkwrite.c + ) + set(CBOR_SOURCES ${CMAKE_CURRENT_LIST_DIR}/tinycbor/src/cborencoder.c ${CMAKE_CURRENT_LIST_DIR}/tinycbor/src/cborparser.c ${CMAKE_CURRENT_LIST_DIR}/tinycbor/src/cborparser_dup_string.c @@ -216,7 +218,7 @@ if (ENABLE_EMULATION) set(SOURCES ${SOURCES} ${CMAKE_CURRENT_LIST_DIR}/src/usb/emulation/emulation.c ) - set(EXTERNAL_SOURCES ${EXTERNAL_SOURCES} + set(MBEDTLS_SOURCES ${MBEDTLS_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ctr_drbg.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/entropy.c ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/entropy_poll.c @@ -242,6 +244,10 @@ else() ${CMAKE_CURRENT_LIST_DIR}/src/usb/usb_descriptors.c ) endif() +set(EXTERNAL_SOURCES ${CBOR_SOURCES}) +if(NOT ESP_PLATFORM) + set(EXTERNAL_SOURCES ${EXTERNAL_SOURCES} ${MBEDTLS_SOURCES}) +endif() if (MSVC) target_compile_options(pico_hsm PUBLIC -wd4820 @@ -270,9 +276,7 @@ endif() set(INTERNAL_SOURCES ${SOURCES}) set(SOURCES ${SOURCES} ${EXTERNAL_SOURCES}) if (NOT TARGET pico_keys_sdk) - if (ENABLE_EMULATION) - add_impl_library(pico_keys_sdk) - elseif(ESP_PLATFORM) + if (ENABLE_EMULATION OR ESP_PLATFORM) add_impl_library(pico_keys_sdk) else() pico_add_library(pico_keys_sdk) From a7be923783e57cf30ce2f855fba1f7305423cab2 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 15 Apr 2024 23:44:12 +0200 Subject: [PATCH 09/18] ITF WCID can be compiled separately. Signed-off-by: Pol Henarejos --- pico_keys_sdk_import.cmake | 17 +++++- src/usb/usb_descriptors.c | 112 +++++++------------------------------ 2 files changed, 34 insertions(+), 95 deletions(-) diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index bcad1ca..45bb1c3 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -51,6 +51,15 @@ elseif (VIDPID STREQUAL "GnuPG") set(USB_PID 0x2440) endif() +if(ESP_PLATFORM) + if (DEFINED CONFIG_TINYUSB_DESC_CUSTOM_VID) + set(USB_VID CONFIG_TINYUSB_DESC_CUSTOM_VID) + endif() + if(DEFINED CONFIG_TINYUSB_DESC_CUSTOM_PID) + set(USB_PID CONFIG_TINYUSB_DESC_CUSTOM_PID) + endif() +endif() + if (NOT DEFINED USB_VID) set(USB_VID 0xFEFF) endif() @@ -82,12 +91,16 @@ endif(USB_ITF_HID) if(USB_ITF_CCID) add_definitions(-DUSB_ITF_CCID=1) message(STATUS "USB CCID Interface:\t\t enabled") + if(USB_ITF_WCID) + add_definitions(-DUSB_ITF_WCID=1) + message(STATUS "USB WebCCID Interface:\t enabled") + endif(USB_ITF_WCID) endif(USB_ITF_CCID) add_definitions(-DDEBUG_APDU=${DEBUG_APDU}) if (NOT ESP_PLATFORM) -add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h") + add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h") else() -add_definitions(-DCFG_TUSB_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/src/usb/tusb_config.h") + add_definitions(-DCFG_TUSB_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/src/usb/tusb_config.h") endif() message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}") diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 186db0f..e032c73 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -76,7 +76,8 @@ uint8_t const *tud_descriptor_device_cb(void) { #define TUD_INTERFACE_DESC_LEN 9 #define TUD_ENDPOINT_DESC_LEN 7 #define TUSB_SMARTCARD_LEN 54 -#define TUD_SMARTCARD_DESC_LEN ((TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 3 * TUD_ENDPOINT_DESC_LEN) + (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 2 * TUD_ENDPOINT_DESC_LEN)) +#define TUSB_SMARTCARD_CCID_DESC_LEN (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 3 * TUD_ENDPOINT_DESC_LEN) +#define TUSB_SMARTCARD_WCID_DESC_LEN (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 2 * TUD_ENDPOINT_DESC_LEN) enum { TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN @@ -84,11 +85,13 @@ enum { + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN #endif #ifdef USB_ITF_CCID - + TUD_SMARTCARD_DESC_LEN + + TUSB_SMARTCARD_CCID_DESC_LEN +#ifdef USB_ITF_WCID + + TUSB_SMARTCARD_WCID_DESC_LEN +#endif #endif }; - #ifdef USB_ITF_HID uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_FIDO_U2F(CFG_TUD_HID_EP_BUFSIZE) @@ -100,11 +103,13 @@ uint8_t const desc_hid_report_kb[] = { #endif #ifdef USB_ITF_CCID +#ifdef USB_ITF_WCID #define TUD_SMARTCARD_DESCRIPTOR_WEB(_itf, _strix, _epout, _epin, _epsize) \ 9, TUSB_DESC_INTERFACE, _itf, 0, 2, 0xFF, 0, 0, _strix, \ 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0 +#endif #define TUD_SMARTCARD_DESCRIPTOR(_itf, _strix, _epout, _epin, _epint, _epsize) \ 9, TUSB_DESC_INTERFACE, _itf, 0, 3, TUSB_CLASS_SMART_CARD, 0, 0, _strix, \ 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ @@ -121,61 +126,13 @@ const uint8_t desc_config[] = { #endif #ifdef USB_ITF_CCID TUD_SMARTCARD_DESCRIPTOR(ITF_CCID, ITF_CCID+5, 1, TUSB_DIR_IN_MASK | 1, TUSB_DIR_IN_MASK | 2, 64), +#ifdef USB_ITF_WCID TUD_SMARTCARD_DESCRIPTOR_WEB(ITF_WCID, ITF_WCID+5, 3, TUSB_DIR_IN_MASK | 3, 64), #endif +#endif }; -/* -tusb_desc_configuration_t const desc_config = { - .bLength = sizeof(tusb_desc_configuration_t), - .bDescriptorType = TUSB_DESC_CONFIGURATION, - .wTotalLength = (sizeof(tusb_desc_configuration_t) #ifdef USB_ITF_CCID - + (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + - 3 * sizeof(tusb_desc_endpoint_t)) + - (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + - 2 * sizeof(tusb_desc_endpoint_t)) -#endif -#ifdef USB_ITF_HID - + TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN -#endif - ), - .bNumInterfaces = ITF_TOTAL, - .bConfigurationValue = 1, - .iConfiguration = 4, - .bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, - .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER + 1), -}; -*/ -#ifdef USB_ITF_CCID -/* -static const struct ccid_class_descriptor desc_ccid = { - .bLength = sizeof(struct ccid_class_descriptor), - .bDescriptorType = 0x21, - .bcdCCID = (0x0110), - .bMaxSlotIndex = 0, - .bVoltageSupport = 0x01, // 5.0V - .dwProtocols = ( - 0x01 | // T=0 - 0x02), // T=1 - .dwDefaultClock = (0xDFC), - .dwMaximumClock = (0xDFC), - .bNumClockSupport = 0, - .dwDataRate = (0x2580), - .dwMaxDataRate = (0x2580), - .bNumDataRatesSupported = 0, - .dwMaxIFSD = (0xFE), // IFSD is handled by the real reader driver - .dwSynchProtocols = (0), - .dwMechanical = (0), - .dwFeatures = 0x40840, //USB-ICC, short & extended APDU - .dwMaxCCIDMessageLength = 65544 + 10, - .bClassGetResponse = 0xFF, - .bclassEnvelope = 0xFF, - .wLcdLayout = 0x0, - .bPINSupport = 0x0, - .bMaxCCIDBusySlots = 0x01, -}; -*/ tusb_desc_interface_t const desc_interface = { .bLength = sizeof(tusb_desc_interface_t), .bDescriptorType = TUSB_DESC_INTERFACE, @@ -188,6 +145,7 @@ tusb_desc_interface_t const desc_interface = { .iInterface = ITF_CCID + 5, }; +#ifdef USB_ITF_WCID tusb_desc_interface_t const desc_interface_wcid = { .bLength = sizeof(tusb_desc_interface_t), .bDescriptorType = TUSB_DESC_INTERFACE, @@ -199,34 +157,7 @@ tusb_desc_interface_t const desc_interface_wcid = { .bInterfaceProtocol = 0, .iInterface = ITF_WCID + 5, }; - -tusb_desc_endpoint_t const desc_ep1 = { - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = TUSB_DIR_IN_MASK | 1, - .bmAttributes.xfer = TUSB_XFER_BULK, - .wMaxPacketSize = (64), - .bInterval = 0 -}; - -tusb_desc_endpoint_t const desc_ep2 = { - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = 1, - .bmAttributes.xfer = TUSB_XFER_BULK, - .wMaxPacketSize = (64), - .bInterval = 0 -}; - -tusb_desc_endpoint_t const desc_ep3 = { - .bLength = sizeof(tusb_desc_endpoint_t), - .bDescriptorType = TUSB_DESC_ENDPOINT, - .bEndpointAddress = TUSB_DIR_IN_MASK | 2, - .bmAttributes.xfer = TUSB_XFER_INTERRUPT, - .wMaxPacketSize = (64), - .bInterval = 0 -}; - +#endif #endif #ifdef USB_ITF_HID @@ -251,18 +182,16 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { } #endif -#ifdef USB_ITF_CCID - +#ifdef USB_ITF_WCID #define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN) #define MS_OS_20_DESC_LEN 0xB2 - enum { VENDOR_REQUEST_WEBUSB = 1, VENDOR_REQUEST_MICROSOFT = 2 }; -#define URL "picokeys.com/pki/" +#define URL "www.picokeys.com/pki/" static bool web_serial_connected = false; const tusb_desc_webusb_url_t desc_url = @@ -353,7 +282,6 @@ uint8_t const desc_bos[] = { }; uint8_t const *tud_descriptor_bos_cb(void) { - printf("!!!!!!!!!!!! tud_descriptor_bos_cb\n"); return desc_bos; } #endif @@ -374,9 +302,12 @@ char const *string_desc_arr [] = { #endif #ifdef USB_ITF_CCID , "Pico Key CCID Interface" +#ifdef USB_ITF_WCID , "Pico Key WebCCID Interface" #endif +#endif }; + #ifdef ESP_PLATFORM const tinyusb_config_t tusb_cfg = { .device_descriptor = &desc_device, @@ -385,8 +316,7 @@ const tinyusb_config_t tusb_cfg = { .external_phy = false, .configuration_descriptor = desc_config, }; -#endif -#ifndef ESP_PLATFORM +#else static uint16_t _desc_str[32]; uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { @@ -407,12 +337,8 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { } const char *str = string_desc_arr[index]; - char unique_id_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; if (index == 3) { - pico_unique_board_id_t unique_id; - pico_get_unique_board_id(&unique_id); - pico_get_unique_board_id_string(unique_id_str, 2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1); - str = unique_id_str; + str = pico_serial_str; } chr_count = strlen(str); From 8d86a8c56b4e8dd4b525570d2ca324a29d5b901a Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 15 Apr 2024 23:44:51 +0200 Subject: [PATCH 10/18] Add procedure to compute unique ID at startup. Signed-off-by: Pol Henarejos --- src/CMakeLists.txt | 2 +- src/crypto_utils.c | 7 +------ src/esp_compat.h | 6 ------ src/fs/file.c | 9 +++++++++ src/fs/file.h | 4 ++++ src/main.c | 24 +++++++++++++++++++----- src/pico_keys.h | 3 +++ src/usb/ccid/ccid.c | 20 ++++++-------------- src/usb/hid/hid.c | 7 ++----- src/usb/usb.c | 9 +++++++-- 10 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 66ea656..6d96dd5 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS ${INTERNAL_SOURCES} INCLUDE_DIRS . fs rng usb ../mbedtls/include - REQUIRES bootloader_support esp_partition esp_tinyusb + REQUIRES bootloader_support esp_partition esp_tinyusb efuse ) diff --git a/src/crypto_utils.c b/src/crypto_utils.c index aadb069..f98d7ef 100644 --- a/src/crypto_utils.c +++ b/src/crypto_utils.c @@ -40,14 +40,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/esp_compat.h b/src/esp_compat.h index dd70088..10a1c56 100644 --- a/src/esp_compat.h +++ b/src/esp_compat.h @@ -38,12 +38,6 @@ static inline uint32_t board_millis(void) { } #define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; -#define pico_get_unique_board_id(a) memset(a, 0, sizeof(pico_unique_board_id_t)) -#define pico_get_unique_board_id_string(a, b) \ - do { \ - pico_unique_board_id_t t; \ - pico_get_unique_board_id(&t); \ - snprintf(a, b, "%02X%02X%02X%02X%02X%02X%02X%02X", t.id[0], t.id[1], t.id[2], t.id[3], t.id[4], t.id[5], t.id[6], t.id[7]); } while(0) typedef SemaphoreHandle_t mutex_t; typedef SemaphoreHandle_t semaphore_t; #define mutex_init(a) do { *(a) = xSemaphoreCreateMutex();} while(0) 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 031d0ba..af4d57b 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 11a78c8..930e319 100644 --- a/src/main.c +++ b/src/main.c @@ -360,18 +360,23 @@ void core0_loop() { #ifdef ESP_PLATFORM #include "tinyusb.h" +#include "esp_efuse.h" +#define pico_get_unique_board_id(a) do { uint32_t value; esp_efuse_read_block(EFUSE_BLK1, &value, 0, 32); memcpy((uint8_t *)(a), &value, sizeof(uint32_t)); esp_efuse_read_block(EFUSE_BLK1, &value, 32, 32); memcpy((uint8_t *)(a)+4, &value, sizeof(uint32_t)); } while(0) extern const tinyusb_config_t tusb_cfg; TaskHandle_t hcore0 = NULL, hcore1 = NULL; +char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; +pico_unique_board_id_t pico_serial; int app_main() { #else int main(void) { #endif + 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 -#ifdef ESP_PLATFORM - tinyusb_driver_install(&tusb_cfg); -#endif - usb_init(); #ifndef ESP_PLATFORM board_init(); stdio_init_all(); @@ -392,7 +397,6 @@ int main(void) { led_off_all(); - //prepare_ccid(); #else emul_init("127.0.0.1", 35963); @@ -402,8 +406,18 @@ int main(void) { low_flash_init(); + scan_flash(); + init_rtc(); +#ifndef ENABLE_EMULATION + usb_init(); +#ifdef ESP_PLATFORM + tusb_cfg.string_descriptor[3] = pico_serial_str; + tinyusb_driver_install(&tusb_cfg); +#endif +#endif + //ccid_prepare_receive(&ccid); #ifdef ESP_PLATFORM neopixel = neopixel_Init(1, GPIO_NUM_48); diff --git a/src/pico_keys.h b/src/pico_keys.h index 3e3ad55..60b116b 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -166,4 +166,7 @@ extern uint32_t button_timeout; #define CCID_WRONG_PADDING -1011 #define CCID_VERIFICATION_FAILED -1012 +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 65786d6..09aae89 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -84,7 +84,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) { @@ -267,38 +266,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 6ae835e..7b8a23f 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -429,15 +429,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; diff --git a/src/usb/usb.c b/src/usb/usb.c index 78fc553..1243226 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -21,12 +21,12 @@ #if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #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" +#include "tusb.h" // For memcpy #include @@ -158,10 +158,15 @@ queue_t usb_to_card_q; queue_t card_to_usb_q; #endif -void usb_init() { +extern uint16_t usb_vid, usb_pid; +extern tusb_desc_device_t desc_device; +void usb_init() +{ #ifndef ENABLE_EMULATION queue_init(&card_to_usb_q, sizeof(uint32_t), 64); queue_init(&usb_to_card_q, sizeof(uint32_t), 64); + desc_device.idVendor = usb_vid; + desc_device.idProduct = usb_pid; #endif } From b6b53010cc825610f388627b2958f5166fd106c9 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 16 Apr 2024 00:03:43 +0200 Subject: [PATCH 11/18] Fix build for emulated interface. Signed-off-by: Pol Henarejos --- src/esp_compat.h | 2 -- src/main.c | 7 +++++-- src/pico_keys.h | 6 +++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/esp_compat.h b/src/esp_compat.h index 10a1c56..cfc0d9b 100644 --- a/src/esp_compat.h +++ b/src/esp_compat.h @@ -36,8 +36,6 @@ extern TaskHandle_t hcore0, hcore1; static inline uint32_t board_millis(void) { return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ ); } -#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 -typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; typedef SemaphoreHandle_t mutex_t; typedef SemaphoreHandle_t semaphore_t; #define mutex_init(a) do { *(a) = xSemaphoreCreateMutex();} while(0) diff --git a/src/main.c b/src/main.c index 930e319..e9dd284 100644 --- a/src/main.c +++ b/src/main.c @@ -358,16 +358,19 @@ void core0_loop() { } } +char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; +pico_unique_board_id_t pico_serial; #ifdef ESP_PLATFORM #include "tinyusb.h" #include "esp_efuse.h" #define pico_get_unique_board_id(a) do { uint32_t value; esp_efuse_read_block(EFUSE_BLK1, &value, 0, 32); memcpy((uint8_t *)(a), &value, sizeof(uint32_t)); esp_efuse_read_block(EFUSE_BLK1, &value, 32, 32); memcpy((uint8_t *)(a)+4, &value, sizeof(uint32_t)); } while(0) extern const tinyusb_config_t tusb_cfg; TaskHandle_t hcore0 = NULL, hcore1 = NULL; -char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; -pico_unique_board_id_t pico_serial; int app_main() { #else +#ifdef ENABLE_EMULATION +#define pico_get_unique_board_id(a) memset(a, 0, sizeof(*(a))) +#endif int main(void) { #endif pico_get_unique_board_id(&pico_serial); diff --git a/src/pico_keys.h b/src/pico_keys.h index 60b116b..c72ffd7 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -166,7 +166,11 @@ extern uint32_t button_timeout; #define CCID_WRONG_PADDING -1011 #define CCID_VERIFICATION_FAILED -1012 +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) +#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[]; +extern char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; #endif From 6f7ab69a9df3950c9b538a72661bc3810e978778 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 16 Apr 2024 00:04:23 +0200 Subject: [PATCH 12/18] Added support for dynamic USB_VID / USB_PID. It can be changed on runtime without rebuilding or patching. Signed-off-by: Pol Henarejos --- src/fs/file.c | 6 ++++-- src/usb/usb.c | 4 ++++ src/usb/usb_descriptors.c | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 3aa0742..bee9812 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -283,8 +283,10 @@ void scan_flash() { scan_region(false); #ifndef ENABLE_EMULATION file_t *ef_vp = search_dynamic_file(EF_VP); - if (file_has_data(ef_vp)) { - + if (file_has_data(ef_vp) && file_get_size(ef_vp) >= 4) { + uint8_t *data = file_get_data(ef_vp); + usb_vid = (data[0] << 8) | data[1]; + usb_pid = (data[2] << 8) | data[3]; } #endif } diff --git a/src/usb/usb.c b/src/usb/usb.c index 1243226..c73a0b0 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -26,7 +26,9 @@ #include "pico_keys.h" #include "usb.h" #include "apdu.h" +#ifndef ENABLE_EMULATION #include "tusb.h" +#endif // For memcpy #include @@ -158,8 +160,10 @@ queue_t usb_to_card_q; queue_t card_to_usb_q; #endif +#ifndef ENABLE_EMULATION extern uint16_t usb_vid, usb_pid; extern tusb_desc_device_t desc_device; +#endif void usb_init() { #ifndef ENABLE_EMULATION diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index e032c73..d2a448e 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -25,6 +25,7 @@ #endif #include "pico_keys_version.h" #include "usb.h" +#include "pico_keys.h" #ifndef USB_VID #define USB_VID 0xFEFF @@ -39,11 +40,14 @@ #define MAX_USB_POWER 1 +uint16_t usb_vid = USB_VID; +uint16_t usb_pid = USB_PID; + //--------------------------------------------------------------------+ // 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 7b35000e1163ea183775e6d28b0cc156cb1b8c02 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 16 Apr 2024 16:18:28 +0200 Subject: [PATCH 13/18] Use persistent memory for storing dynamic VID/PID. Signed-off-by: Pol Henarejos --- src/fs/file.c | 3 ++- src/fs/file.h | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/fs/file.c b/src/fs/file.c index bee9812..2fc9633 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -265,6 +265,8 @@ void scan_region(bool persistent) { void wait_flash_finish(); #ifndef ENABLE_EMULATION extern uint16_t usb_vid, usb_pid; +file_t sef_vp = {.fid = EF_VP, .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_vp = &sef_vp; #endif void scan_flash() { initialize_flash(false); //soft initialization @@ -282,7 +284,6 @@ void scan_flash() { scan_region(true); scan_region(false); #ifndef ENABLE_EMULATION - file_t *ef_vp = search_dynamic_file(EF_VP); if (file_has_data(ef_vp) && file_get_size(ef_vp) >= 4) { uint8_t *data = file_get_data(ef_vp); usb_vid = (data[0] << 8) | data[1]; diff --git a/src/fs/file.h b/src/fs/file.h index af4d57b..b7d77e9 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -129,4 +129,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_vp; #endif + +#endif // _FILE_H_ From afc71f694225ef66d2618b4c287cf17990689649 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 16 Apr 2024 16:24:05 +0200 Subject: [PATCH 14/18] Moving EF_VP to more generic EF_PHY, which stores PHY aspects from the chip. Signed-off-by: Pol Henarejos --- src/fs/file.c | 13 +++++++------ src/fs/file.h | 10 ++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 2fc9633..828d59c 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -41,6 +41,9 @@ extern uint8_t flash_read_uint8(uintptr_t addr); extern uint8_t *flash_read(uintptr_t addr); extern void low_flash_available(); +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; + //puts FCI in the RAPDU void process_fci(const file_t *pe, int fmd) { res_APDU_size = 0; @@ -265,8 +268,6 @@ void scan_region(bool persistent) { void wait_flash_finish(); #ifndef ENABLE_EMULATION extern uint16_t usb_vid, usb_pid; -file_t sef_vp = {.fid = EF_VP, .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_vp = &sef_vp; #endif void scan_flash() { initialize_flash(false); //soft initialization @@ -284,10 +285,10 @@ void scan_flash() { scan_region(true); scan_region(false); #ifndef ENABLE_EMULATION - if (file_has_data(ef_vp) && file_get_size(ef_vp) >= 4) { - uint8_t *data = file_get_data(ef_vp); - usb_vid = (data[0] << 8) | data[1]; - usb_pid = (data[2] << 8) | data[3]; + 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]; } #endif } diff --git a/src/fs/file.h b/src/fs/file.h index b7d77e9..9a140c4 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -67,9 +67,11 @@ #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 MAX_DEPTH 4 @@ -130,7 +132,7 @@ 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_vp; +extern file_t *ef_phy; #endif #endif // _FILE_H_ From ade730ffb5e38b37afaab21247a2c9ac6db2ac1b Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 16 Apr 2024 23:22:11 +0200 Subject: [PATCH 15/18] Introducing EF_PHY to store PHY (VIDPID and LED no.). Signed-off-by: Pol Henarejos --- src/fs/file.c | 16 +++++----------- src/fs/file.h | 3 +++ src/main.c | 6 +++++- src/usb/usb.c | 15 ++++++++++----- src/usb/usb_descriptors.c | 4 ---- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 828d59c..b3cdc41 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -41,8 +41,10 @@ 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) { @@ -147,7 +149,9 @@ 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) { - + if (fid == EF_PHY) { + return ef_phy; + } for (file_t *p = file_entries; p != file_last; p++) { if (p->fid != 0x0000 && p->fid == fid) { if (!parent || (parent && is_parent(p, parent))) { @@ -266,9 +270,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 && @@ -284,13 +285,6 @@ void scan_flash() { printf("SCAN\n"); scan_region(true); scan_region(false); -#ifndef ENABLE_EMULATION - 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]; - } -#endif } uint8_t *file_read(const uint8_t *addr) { diff --git a/src/fs/file.h b/src/fs/file.h index 9a140c4..527b00c 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -72,6 +72,9 @@ #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 diff --git a/src/main.c b/src/main.c index e9dd284..990d55d 100644 --- a/src/main.c +++ b/src/main.c @@ -423,7 +423,11 @@ int main(void) { //ccid_prepare_receive(&ccid); #ifdef ESP_PLATFORM - neopixel = neopixel_Init(1, GPIO_NUM_48); + uint8_t gpio = GPIO_NUM_48; + if (file_has_data(ef_phy)) { + gpio = file_get_data(ef_phy)[PHY_LED_GPIO]; + } + neopixel = neopixel_Init(1, gpio); #endif #ifdef ESP_PLATFORM diff --git a/src/usb/usb.c b/src/usb/usb.c index c73a0b0..00fbe0e 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -161,16 +161,21 @@ queue_t card_to_usb_q; #endif #ifndef ENABLE_EMULATION -extern uint16_t usb_vid, usb_pid; extern tusb_desc_device_t desc_device; #endif -void usb_init() -{ +void usb_init() { #ifndef ENABLE_EMULATION - queue_init(&card_to_usb_q, sizeof(uint32_t), 64); - queue_init(&usb_to_card_q, sizeof(uint32_t), 64); + 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 d2a448e..6f537e6 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -40,10 +40,6 @@ #define MAX_USB_POWER 1 -uint16_t usb_vid = USB_VID; -uint16_t usb_pid = USB_PID; - - //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ From fc5f4299cc5754fae53e84461be2a7f6e5c18820 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 17 Apr 2024 11:56:54 +0200 Subject: [PATCH 16/18] Fix emulation build. Signed-off-by: Pol Henarejos --- src/fs/file.c | 2 ++ src/usb/usb.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/fs/file.c b/src/fs/file.c index b3cdc41..6e21340 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -149,9 +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))) { diff --git a/src/usb/usb.c b/src/usb/usb.c index 00fbe0e..fe860ba 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -30,6 +30,10 @@ #include "tusb.h" #endif +#ifndef ENABLE_EMULATION +#include "tusb.h" +#endif + // For memcpy #include #include From 0535138211ef2c7722d172a078234c3e0405baeb Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 21 Apr 2024 22:52:16 +0200 Subject: [PATCH 17/18] Only T=1 protocol is supported. Signed-off-by: Pol Henarejos --- src/usb/usb_descriptors.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 6f537e6..cd6bb5e 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -106,13 +106,13 @@ uint8_t const desc_hid_report_kb[] = { #ifdef USB_ITF_WCID #define TUD_SMARTCARD_DESCRIPTOR_WEB(_itf, _strix, _epout, _epin, _epsize) \ 9, TUSB_DESC_INTERFACE, _itf, 0, 2, 0xFF, 0, 0, _strix, \ - 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ + 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0 #endif #define TUD_SMARTCARD_DESCRIPTOR(_itf, _strix, _epout, _epin, _epint, _epsize) \ 9, TUSB_DESC_INTERFACE, _itf, 0, 3, TUSB_CLASS_SMART_CARD, 0, 0, _strix, \ - 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ + 54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \ 7, TUSB_DESC_ENDPOINT, _epint, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), 0 From f0074e5367b278f2de1e1d81c7068c0ad4ca36bd Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 22 Apr 2024 23:35:19 +0200 Subject: [PATCH 18/18] CCID and WCID structs not used. Signed-off-by: Pol Henarejos --- src/usb/usb_descriptors.c | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index cd6bb5e..4c6ab4e 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -132,34 +132,6 @@ const uint8_t desc_config[] = { #endif }; -#ifdef USB_ITF_CCID -tusb_desc_interface_t const desc_interface = { - .bLength = sizeof(tusb_desc_interface_t), - .bDescriptorType = TUSB_DESC_INTERFACE, - .bInterfaceNumber = ITF_CCID, - .bAlternateSetting = 0, - .bNumEndpoints = 3, - .bInterfaceClass = TUSB_CLASS_SMART_CARD, - .bInterfaceSubClass = 0, - .bInterfaceProtocol = 0, - .iInterface = ITF_CCID + 5, -}; - -#ifdef USB_ITF_WCID -tusb_desc_interface_t const desc_interface_wcid = { - .bLength = sizeof(tusb_desc_interface_t), - .bDescriptorType = TUSB_DESC_INTERFACE, - .bInterfaceNumber = ITF_WCID, - .bAlternateSetting = 0, - .bNumEndpoints = 2, - .bInterfaceClass = 0xFF, - .bInterfaceSubClass = 0, - .bInterfaceProtocol = 0, - .iInterface = ITF_WCID + 5, -}; -#endif -#endif - #ifdef USB_ITF_HID #include "apdu.h" uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {