Attempting to add support to esp32s3.

Will it work? Who knows...

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2024-04-04 21:01:55 +02:00
parent 3d0a27c834
commit 023039deb2
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
24 changed files with 229 additions and 74 deletions

View file

@ -15,7 +15,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
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()

6
partitions.csv Executable file
View file

@ -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,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 nvs, data, nvs, 0x9000, 0x6000,
4 phy_init, data, phy, 0xf000, 0x1000,
5 factory, app, factory, 0x10000, 1M,
6 storage, data, fat, , 1M,

View file

@ -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}
)

16
sdkconfig.defaults Executable file
View file

@ -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

5
src/CMakeLists.txt Executable file
View file

@ -0,0 +1,5 @@
idf_component_register(
SRCS ${INTERNAL_SOURCES}
INCLUDE_DIRS . fs rng usb
PRIV_REQUIRES bootloader_support esp_partition
)

View file

@ -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) {

View file

@ -19,7 +19,7 @@
#define _APDU_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#endif
#include "compat.h"

View file

@ -19,7 +19,7 @@
#define _ASN1_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#else
#include <stdint.h>

67
src/esp_compat.h Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View file

@ -20,7 +20,7 @@
#define _FILE_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#else
#include <stdbool.h>

View file

@ -19,13 +19,13 @@
#include <stdint.h>
#include <string.h>
#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"

View file

@ -20,7 +20,7 @@
#include <stdlib.h>
#include <stdio.h>
#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 <unistd.h>
#include <sys/mman.h>
#endif
#include <fcntl.h>
#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 <string.h>

4
src/idf_component.yml Executable file
View file

@ -0,0 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb: "^1.2"
idf: "^5.0"

View file

@ -18,13 +18,15 @@
#include <stdio.h>
// Pico
#ifndef ENABLE_EMULATION
#include "pico/stdlib.h"
#else
#if defined(ENABLE_EMULATION)
#if !defined(_MSC_VER)
#include <sys/time.h>
#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;
}

View file

@ -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 <stdint.h>
@ -47,6 +47,14 @@ extern uint32_t board_millis();
#endif
#include <string.h>
#if defined(ENABLE_EMULATION)
#include <stdbool.h>
#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();

View file

@ -18,7 +18,17 @@
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#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

View file

@ -21,7 +21,7 @@
#define NEUG_PRE_LOOP 32
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#endif

View file

@ -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

View file

@ -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);

View file

@ -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"

View file

@ -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

View file

@ -18,7 +18,7 @@
#include <stdio.h>
// 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

View file

@ -18,12 +18,17 @@
#ifndef _USB_H_
#define _USB_H_
#ifndef ENABLE_EMULATION
#include "pico/util/queue.h"
#else
#if defined(ENABLE_EMULATION)
#include <stdbool.h>
#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

View file

@ -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"