From d8edab069e4108180b03255d08b9d7b7d5bb4e22 Mon Sep 17 00:00:00 2001 From: Marcoen Hirschberg Date: Mon, 21 Feb 2022 21:40:10 +0100 Subject: [PATCH] PSX: game ID based SBI lookup (#552) * PSX: look up SBI files based on Game ID from PSX/sbi.zip * PSX: do not depend on STL for Game ID lookup --- support/psx/psx.cpp | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/support/psx/psx.cpp b/support/psx/psx.cpp index edc23e3..b99fdcb 100644 --- a/support/psx/psx.cpp +++ b/support/psx/psx.cpp @@ -4,9 +4,6 @@ #include #include -#include -#include - #include "../../file_io.h" #include "../../user_io.h" #include "../../spi.h" @@ -87,11 +84,11 @@ static uint8_t bcdToDec(uint8_t bcd) #define SBI_HEADER_SIZE 4 #define SBI_BLOCK_SIZE 14 -static uint16_t libCryptMask(const char *sbifile) +static uint16_t libCryptMask(fileTYPE* sbi_file) { int sz; uint16_t mask = 0; - if ((sz = FileLoad(sbifile, buf, sizeof(buf)))) + if ((sz = FileReadAdv(sbi_file, buf, sizeof(buf)))) { for (int i = 0;; i++) { @@ -502,7 +499,7 @@ void psx_read_cd(uint8_t *buffer, int lba, int cnt) #define ROOT_FOLDER_LBA 150 + 22 -std::vector game_id_prefixes +const char* game_id_prefixes[] { "SCES", "SLES", @@ -527,9 +524,9 @@ const char* psx_get_game_id() //hexdump(buffer, CD_SECTOR_LEN); char* start = nullptr; - for (const auto& prefix : game_id_prefixes) + for (const char* prefix : game_id_prefixes) { - start = (char*)memmem(buffer, CD_SECTOR_LEN, prefix.c_str(), prefix.size()); + start = (char*)memmem(buffer, CD_SECTOR_LEN, prefix, 4); if (start) break; } @@ -642,9 +639,29 @@ void psx_mount_cd(int f_index, int s_index, const char *filename) send_cue(&toc); - strcpy(buf, filename); - strcpy((name_len > 4) ? buf + name_len - 4 : buf + name_len, ".sbi"); - uint16_t mask = libCryptMask(buf); + uint16_t mask = 0; + + fileTYPE sbi_file = {}; + bool has_sbi_file = false; + + // search for .sbi file in PSX/sbi.zip + sprintf(buf, "%s/sbi.zip/%s.sbi", HomeDir(), psx_get_game_id()); + has_sbi_file = (FileOpen(&sbi_file, buf, 1)); + + if (!has_sbi_file) + { + // search for .sbi file base on image name + strcpy(buf, filename); + strcpy((name_len > 4) ? buf + name_len - 4 : buf + name_len, ".sbi"); + has_sbi_file = (FileOpen(&sbi_file, buf, 1)); + } + + if (has_sbi_file) + { + printf("Found SBI file: %s\n", buf); + mask = libCryptMask(&sbi_file); + } + user_io_set_index(250); user_io_set_download(1); user_io_file_tx_data((const uint8_t*)&mask, 2);