From 860f77a45bc6d4e4ba37c7b04422f6e8f22f28ab Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 24 Jan 2026 01:15:10 +0100 Subject: [PATCH] Move rtc Signed-off-by: Pol Henarejos --- src/main.c | 29 +++++++++++++++++++++++++++++ src/pico_keys.h | 13 +++++++++++++ src/rescue.c | 27 +++++---------------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/main.c b/src/main.c index 2ccdaa1..99185c1 100644 --- a/src/main.c +++ b/src/main.c @@ -237,6 +237,35 @@ bool wait_button() { } #endif +bool set_rtc = false; + +bool has_set_rtc() { + return set_rtc; +} + +void set_rtc_time(time_t t) { +#ifdef PICO_PLATFORM + struct timespec tv = {.tv_sec = t, .tv_nsec = 0}; + aon_timer_set_time(&tv); +#else + struct timeval tv = {.tv_sec = t, .tv_usec = 0}; + settimeofday(&tv, NULL); +#endif + set_rtc = true; +} + +time_t get_rtc_time() { +#ifdef PICO_PLATFORM + struct timespec tv; + aon_timer_get_time(&tv); + return tv.tv_sec; +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec; +#endif +} + struct apdu apdu; void init_rtc() { diff --git a/src/pico_keys.h b/src/pico_keys.h index d271607..d9e6bfa 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -61,6 +61,15 @@ #include "pico/util/queue.h" #endif +#ifdef PICO_PLATFORM +#include "pico/bootrom.h" +#include "hardware/watchdog.h" +#include "pico/aon_timer.h" +#else +#include +#include +#endif + extern bool wait_button(); extern void low_flash_init_core1(); @@ -242,4 +251,8 @@ extern uint8_t pico_serial_hash[32]; #define multicore_launch_func_core1(a) multicore_launch_core1((void (*) (void))a) #endif +extern bool has_set_rtc(); +extern time_t get_rtc_time(); +extern void set_rtc_time(time_t tv_sec); + #endif diff --git a/src/rescue.c b/src/rescue.c index 603c781..241d881 100644 --- a/src/rescue.c +++ b/src/rescue.c @@ -19,14 +19,6 @@ #include "apdu.h" #include "pico_keys_version.h" #include "otp.h" -#ifdef PICO_PLATFORM -#include "pico/bootrom.h" -#include "hardware/watchdog.h" -#include "pico/aon_timer.h" -#else -#include -#include -#endif #include "mbedtls/ecdsa.h" #include "mbedtls/sha256.h" #include "random.h" @@ -38,7 +30,6 @@ extern char __flash_binary_end; int rescue_process_apdu(); int rescue_unload(); -bool set_rtc = false; const uint8_t rescue_aid[] = { 8, @@ -220,14 +211,7 @@ int cmd_write() { uint32_t t = (apdu.data[0] << 24) | (apdu.data[1] << 16) | (apdu.data[2] << 8) | apdu.data[3]; tv_sec = (time_t)t; } -#ifdef PICO_PLATFORM - struct timespec tv = {.tv_sec = tv_sec, .tv_nsec = 0}; - aon_timer_set_time(&tv); -#else - struct timeval tv = {.tv_sec = tv_sec, .tv_usec = 0}; - settimeofday(&tv, NULL); -#endif - set_rtc = true; + set_rtc_time(tv_sec); } led_3_blinks(); return SW_OK(); @@ -277,16 +261,15 @@ int cmd_read() { if (p2 != 0x1 && p2 != 0x2) { return SW_INCORRECT_P1P2(); } - if (!set_rtc) { + if (!has_set_rtc()) { return SW_CONDITIONS_NOT_SATISFIED(); } res_APDU_size = 0; + time_t tv_sec = get_rtc_time(); #ifdef PICO_PLATFORM - struct timespec tv; - aon_timer_get_time(&tv); + struct timespec tv = {.tv_sec = tv_sec, .tv_nsec = 0}; #else - struct timeval tv; - gettimeofday(&tv, NULL); + struct timeval tv = {.tv_sec = tv_sec, .tv_usec = 0}; #endif if (p2 == 0x1) { struct tm *tm = localtime(&tv.tv_sec);