2023-11-15 09:05:21 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-12-06 06:23:08 +00:00
|
|
|
#include "cd_subchannel_replacement.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "common/file_system.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
#include "common/path.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <algorithm>
|
2019-12-06 06:23:08 +00:00
|
|
|
#include <memory>
|
|
|
|
Log_SetChannel(CDSubChannelReplacement);
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct SBIFileEntry
|
|
|
|
{
|
|
|
|
u8 minute_bcd;
|
|
|
|
u8 second_bcd;
|
|
|
|
u8 frame_bcd;
|
|
|
|
u8 type;
|
|
|
|
u8 data[10];
|
|
|
|
};
|
2023-11-15 09:05:21 +00:00
|
|
|
struct LSDFileEntry
|
|
|
|
{
|
|
|
|
u8 minute_bcd;
|
|
|
|
u8 second_bcd;
|
|
|
|
u8 frame_bcd;
|
|
|
|
u8 data[12];
|
|
|
|
};
|
|
|
|
static_assert(sizeof(LSDFileEntry) == 15);
|
2019-12-06 06:23:08 +00:00
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
CDSubChannelReplacement::CDSubChannelReplacement() = default;
|
|
|
|
|
|
|
|
CDSubChannelReplacement::~CDSubChannelReplacement() = default;
|
|
|
|
|
|
|
|
static constexpr u32 MSFToLBA(u8 minute_bcd, u8 second_bcd, u8 frame_bcd)
|
|
|
|
{
|
|
|
|
const u8 minute = PackedBCDToBinary(minute_bcd);
|
|
|
|
const u8 second = PackedBCDToBinary(second_bcd);
|
|
|
|
const u8 frame = PackedBCDToBinary(frame_bcd);
|
|
|
|
|
|
|
|
return (ZeroExtend32(minute) * 60 * 75) + (ZeroExtend32(second) * 75) + ZeroExtend32(frame);
|
|
|
|
}
|
|
|
|
|
2023-11-15 09:05:21 +00:00
|
|
|
bool CDSubChannelReplacement::LoadSBI(const std::string& path)
|
2019-12-06 06:23:08 +00:00
|
|
|
{
|
2023-11-15 09:05:21 +00:00
|
|
|
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb");
|
2019-12-06 06:23:08 +00:00
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char header[4];
|
|
|
|
if (std::fread(header, sizeof(header), 1, fp.get()) != 1)
|
|
|
|
{
|
2023-11-15 09:05:21 +00:00
|
|
|
Log_ErrorFmt("Failed to read header for '{}'", path);
|
2019-12-06 06:23:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr char expected_header[] = {'S', 'B', 'I', '\0'};
|
|
|
|
if (std::memcmp(header, expected_header, sizeof(header)) != 0)
|
|
|
|
{
|
2023-11-15 09:05:21 +00:00
|
|
|
Log_ErrorFmt("Invalid header in '{}'", path);
|
2019-12-06 06:23:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:46:56 +00:00
|
|
|
m_replacement_subq.clear();
|
|
|
|
|
2019-12-06 06:23:08 +00:00
|
|
|
SBIFileEntry entry;
|
|
|
|
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
|
|
|
|
{
|
|
|
|
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
|
|
|
|
!IsValidPackedBCD(entry.frame_bcd))
|
|
|
|
{
|
2023-11-15 09:05:21 +00:00
|
|
|
Log_ErrorFmt("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd,
|
|
|
|
entry.frame_bcd, path);
|
2019-12-06 06:23:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.type != 1)
|
|
|
|
{
|
2023-11-15 09:05:21 +00:00
|
|
|
Log_ErrorFmt("Invalid type 0x{:02X} in '{}'", entry.type, path);
|
2019-12-06 06:23:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd);
|
|
|
|
|
2020-07-21 14:02:44 +00:00
|
|
|
CDImage::SubChannelQ subq;
|
|
|
|
std::copy_n(entry.data, countof(entry.data), subq.data.data());
|
2019-12-06 06:23:08 +00:00
|
|
|
|
|
|
|
// generate an invalid crc by flipping all bits from the valid crc (will never collide)
|
2020-07-21 14:02:44 +00:00
|
|
|
const u16 crc = subq.ComputeCRC(subq.data) ^ 0xFFFF;
|
|
|
|
subq.data[10] = Truncate8(crc);
|
|
|
|
subq.data[11] = Truncate8(crc >> 8);
|
2019-12-06 06:23:08 +00:00
|
|
|
|
2020-07-21 14:02:44 +00:00
|
|
|
m_replacement_subq.emplace(lba, subq);
|
2019-12-06 06:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 09:05:21 +00:00
|
|
|
Log_InfoFmt("Loaded {} replacement sectors from SBI '{}'", m_replacement_subq.size(), path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDSubChannelReplacement::LoadLSD(const std::string& path)
|
|
|
|
{
|
|
|
|
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb");
|
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
|
2024-01-01 04:46:56 +00:00
|
|
|
m_replacement_subq.clear();
|
|
|
|
|
2023-11-15 09:05:21 +00:00
|
|
|
LSDFileEntry entry;
|
|
|
|
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
|
|
|
|
{
|
|
|
|
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
|
|
|
|
!IsValidPackedBCD(entry.frame_bcd))
|
|
|
|
{
|
|
|
|
Log_ErrorFmt("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd,
|
|
|
|
entry.frame_bcd, path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd);
|
|
|
|
|
|
|
|
CDImage::SubChannelQ subq;
|
|
|
|
std::copy_n(entry.data, countof(entry.data), subq.data.data());
|
|
|
|
|
|
|
|
Log_DebugFmt("{:02x}:{:02x}:{:02x}: CRC {}", entry.minute_bcd, entry.second_bcd, entry.frame_bcd,
|
|
|
|
subq.IsCRCValid() ? "VALID" : "INVALID");
|
|
|
|
m_replacement_subq.emplace(lba, subq);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_InfoFmt("Loaded {} replacement sectors from LSD '{}'", m_replacement_subq.size(), path);
|
2019-12-06 06:23:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-15 09:05:21 +00:00
|
|
|
bool CDSubChannelReplacement::LoadFromImagePath(std::string_view image_path)
|
2021-04-24 06:03:28 +00:00
|
|
|
{
|
2023-11-15 09:05:21 +00:00
|
|
|
if (const std::string filename = Path::ReplaceExtension(image_path, "sbi"); LoadSBI(filename.c_str()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const std::string filename = Path::ReplaceExtension(image_path, "lsd"); LoadLSD(filename.c_str()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2021-04-24 06:03:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 14:02:44 +00:00
|
|
|
void CDSubChannelReplacement::AddReplacementSubChannelQ(u32 lba, const CDImage::SubChannelQ& subq)
|
|
|
|
{
|
|
|
|
auto iter = m_replacement_subq.find(lba);
|
|
|
|
if (iter != m_replacement_subq.end())
|
|
|
|
iter->second.data = subq.data;
|
|
|
|
else
|
|
|
|
m_replacement_subq.emplace(lba, subq);
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:04:16 +00:00
|
|
|
bool CDSubChannelReplacement::GetReplacementSubChannelQ(u8 minute_bcd, u8 second_bcd, u8 frame_bcd,
|
2020-07-21 14:02:44 +00:00
|
|
|
CDImage::SubChannelQ* subq) const
|
2019-12-06 06:23:08 +00:00
|
|
|
{
|
2020-07-21 14:02:44 +00:00
|
|
|
return GetReplacementSubChannelQ(MSFToLBA(minute_bcd, second_bcd, frame_bcd), subq);
|
2019-12-06 06:23:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 14:02:44 +00:00
|
|
|
bool CDSubChannelReplacement::GetReplacementSubChannelQ(u32 lba, CDImage::SubChannelQ* subq) const
|
2019-12-06 06:23:08 +00:00
|
|
|
{
|
2020-06-23 09:04:16 +00:00
|
|
|
const auto iter = m_replacement_subq.find(lba);
|
|
|
|
if (iter == m_replacement_subq.cend())
|
2019-12-06 06:23:08 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-21 14:02:44 +00:00
|
|
|
*subq = iter->second;
|
2019-12-06 06:23:08 +00:00
|
|
|
return true;
|
|
|
|
}
|