From ed4c7b358b6221cf9c4bc3fd80fc2e9031a742dc Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Tue, 21 Feb 2023 22:25:01 +0100 Subject: [PATCH] Changed SC64 firmware version checking --- src/flashcart/sc64/sc64.c | 15 +++++++++------ src/flashcart/sc64/sc64_internal.c | 15 ++++++++------- src/flashcart/sc64/sc64_internal.h | 2 +- src/main.c | 17 ++++------------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/flashcart/sc64/sc64.c b/src/flashcart/sc64/sc64.c index d63afe5c..41b6e38e 100644 --- a/src/flashcart/sc64/sc64.c +++ b/src/flashcart/sc64/sc64.c @@ -18,7 +18,8 @@ #define SHADOW_ADDRESS (0x1FFC0000) #define EEPROM_ADDRESS (0x1FFE2000) -#define MIN_SUPPORTED_API_VERSION (1) +#define SUPPORTED_MAJOR_VERSION (2) +#define SUPPORTED_MINOR_VERSION (12) static flashcart_error_t load_to_flash (FIL *fil, void *address, size_t size, UINT *br) { @@ -52,19 +53,21 @@ static flashcart_error_t load_to_flash (FIL *fil, void *address, size_t size, UI static flashcart_error_t sc64_init (void) { - uint32_t api_version; + uint16_t major; + uint16_t minor; sc64_unlock(); if (!sc64_check_presence()) { return FLASHCART_ERROR_UNSUPPORTED; } - - if (sc64_get_api_version(&api_version) != SC64_OK) { + if (sc64_get_version(&major, &minor) != SC64_OK) { return FLASHCART_ERROR_OUTDATED; } - - if (api_version < MIN_SUPPORTED_API_VERSION) { + if (major != SUPPORTED_MAJOR_VERSION) { + return FLASHCART_ERROR_OUTDATED; + } + if (minor < SUPPORTED_MINOR_VERSION) { return FLASHCART_ERROR_OUTDATED; } diff --git a/src/flashcart/sc64/sc64_internal.c b/src/flashcart/sc64/sc64_internal.c index 7230aecb..b381ec26 100644 --- a/src/flashcart/sc64/sc64_internal.c +++ b/src/flashcart/sc64/sc64_internal.c @@ -6,7 +6,7 @@ typedef struct { uint32_t SR_CMD; uint32_t DATA[2]; - uint32_t VERSION; + uint32_t IDENTIFIER; uint32_t KEY; } sc64_regs_t; @@ -17,7 +17,7 @@ typedef struct { #define SC64_SR_CMD_ERROR (1 << 30) #define SC64_SR_CPU_BUSY (1 << 31) -#define SC64_VERSION_2 (0x53437632) +#define SC64_V2_IDENTIFIER (0x53437632) #define SC64_KEY_RESET (0x00000000UL) #define SC64_KEY_UNLOCK_1 (0x5F554E4CUL) @@ -26,7 +26,7 @@ typedef struct { typedef enum { - CMD_ID_API_VERSION_GET = 'V', + CMD_ID_VERSION_GET = 'V', CMD_ID_CONFIG_GET = 'c', CMD_ID_CONFIG_SET = 'C', CMD_ID_WRITEBACK_SD_INFO = 'W', @@ -75,7 +75,7 @@ void sc64_lock (void) { } bool sc64_check_presence (void) { - return (io_read((uint32_t) (&SC64_REGS->VERSION)) == SC64_VERSION_2); + return (io_read((uint32_t) (&SC64_REGS->IDENTIFIER)) == SC64_V2_IDENTIFIER); } void sc64_read_data (void *src, void *dst, size_t length) { @@ -90,10 +90,11 @@ void sc64_write_data (void *src, void *dst, size_t length) { dma_wait(); } -sc64_error_t sc64_get_api_version (uint32_t *api_version) { - sc64_cmd_t cmd = { .id = CMD_ID_API_VERSION_GET }; +sc64_error_t sc64_get_version (uint16_t *major, uint16_t *minor) { + sc64_cmd_t cmd = { .id = CMD_ID_VERSION_GET }; sc64_error_t error = sc64_execute_cmd(&cmd); - *api_version = cmd.rsp[0]; + *major = ((cmd.rsp[0] >> 16) & 0xFFFF); + *minor = (cmd.rsp[0] & 0xFFFF); return error; } diff --git a/src/flashcart/sc64/sc64_internal.h b/src/flashcart/sc64/sc64_internal.h index 2b818f98..a24918a9 100644 --- a/src/flashcart/sc64/sc64_internal.h +++ b/src/flashcart/sc64/sc64_internal.h @@ -60,7 +60,7 @@ void sc64_lock (void); bool sc64_check_presence (void); void sc64_read_data (void *src, void *dst, size_t length); void sc64_write_data (void *src, void *dst, size_t length); -sc64_error_t sc64_get_api_version (uint32_t *api_version); +sc64_error_t sc64_get_version (uint16_t *major, uint16_t *minor); sc64_error_t sc64_get_config (sc64_cfg_t cfg, void *value); sc64_error_t sc64_set_config (sc64_cfg_t cfg, uint32_t value); sc64_error_t sc64_writeback_enable (void *address); diff --git a/src/main.c b/src/main.c index deb7a1ec..b228d833 100644 --- a/src/main.c +++ b/src/main.c @@ -10,20 +10,11 @@ static void init (void) { assertf(usb_initialize() != CART_NONE, "No flashcart was detected"); - switch (flashcart_init()) { - case FLASHCART_OK: - break; - case FLASHCART_ERROR_OUTDATED: - assertf(false, "Outdated flashcart firmware"); - break; - case FLASHCART_ERROR_UNSUPPORTED: - assertf(false, "Unsupported flashcart"); - break; - default: - assertf(false, "Unknown error while initializing flashcart"); - break; - } assertf(debug_init_sdfs("sd:/", -1), "Couldn't initialize SD card"); + flashcart_error_t error = flashcart_init(); + assertf(error != FLASHCART_ERROR_OUTDATED, "Outdated flashcart firmware"); + assertf(error != FLASHCART_ERROR_UNSUPPORTED, "Unsupported flashcart"); + assertf(error == FLASHCART_OK, "Unknown error while initializing flashcart"); } static void deinit (void) {