From 96b3fa92751ff06b21c1197cd969fa453b93509e Mon Sep 17 00:00:00 2001 From: Sorgelig Date: Sun, 6 Feb 2022 23:02:12 +0800 Subject: [PATCH] PSX: add encryption map generation (markun). --- support/psx/psx.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/support/psx/psx.cpp b/support/psx/psx.cpp index 5b2dc22..459af85 100644 --- a/support/psx/psx.cpp +++ b/support/psx/psx.cpp @@ -110,6 +110,57 @@ static int get_bin(const char *cue) return res; } +static uint32_t libCryptSectors[16] = +{ + 14105, + 14231, + 14485, + 14579, + 14649, + 14899, + 15056, + 15130, + 15242, + 15312, + 15378, + 15628, + 15919, + 16031, + 16101, + 16167, +}; + +static uint32_t msfToLba(uint32_t m, uint32_t s, uint32_t f) +{ + return (m * 60 + s) * 75 + f; +} + +static uint8_t bcdToDec(uint8_t bcd) +{ + return (bcd >> 4) * 10 + (bcd & 0x0F); +} + +#define SBI_HEADER_SIZE 4 +#define SBI_BLOCK_SIZE 14 + +static uint16_t libCryptMask(const char *sbifile) +{ + int sz; + uint16_t mask = 0; + if ((sz = FileLoad(sbifile, buf, sizeof(buf)))) + { + for (int i = 0;; i++) + { + int pos = SBI_HEADER_SIZE + i * SBI_BLOCK_SIZE; + if (pos >= sz) break; + uint32_t lba = msfToLba(bcdToDec(buf[pos]), bcdToDec(buf[pos + 1]), bcdToDec(buf[pos + 2])); + for (int m = 0; m < 16; m++) if (libCryptSectors[m] == lba) mask |= (1 << (15 - m)); + } + } + + return mask; +} + void psx_mount_cd(int f_index, int s_index, const char *filename) { static char last_dir[1024] = {}; @@ -154,6 +205,15 @@ void psx_mount_cd(int f_index, int s_index, const char *filename) if (loaded) { + strcpy(buf, filename); + strcpy((name_len > 4) ? buf + name_len - 4 : buf + name_len, ".sbi"); + + uint16_t mask = libCryptMask(buf); + user_io_set_index(250); + user_io_set_download(1); + user_io_file_tx_data((const uint8_t*)&mask, 2); + user_io_set_download(0); + user_io_set_index(f_index); process_ss(filename, name_len != 0);