cd_subchannel_replacement.cpp (4931B)
1 // SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #include "cd_subchannel_replacement.h" 5 #include "common/file_system.h" 6 #include "common/log.h" 7 #include "common/path.h" 8 #include <algorithm> 9 #include <memory> 10 Log_SetChannel(CDSubChannelReplacement); 11 12 #pragma pack(push, 1) 13 struct SBIFileEntry 14 { 15 u8 minute_bcd; 16 u8 second_bcd; 17 u8 frame_bcd; 18 u8 type; 19 u8 data[10]; 20 }; 21 struct LSDFileEntry 22 { 23 u8 minute_bcd; 24 u8 second_bcd; 25 u8 frame_bcd; 26 u8 data[12]; 27 }; 28 static_assert(sizeof(LSDFileEntry) == 15); 29 #pragma pack(pop) 30 31 CDSubChannelReplacement::CDSubChannelReplacement() = default; 32 33 CDSubChannelReplacement::~CDSubChannelReplacement() = default; 34 35 static constexpr u32 MSFToLBA(u8 minute_bcd, u8 second_bcd, u8 frame_bcd) 36 { 37 const u8 minute = PackedBCDToBinary(minute_bcd); 38 const u8 second = PackedBCDToBinary(second_bcd); 39 const u8 frame = PackedBCDToBinary(frame_bcd); 40 41 return (ZeroExtend32(minute) * 60 * 75) + (ZeroExtend32(second) * 75) + ZeroExtend32(frame); 42 } 43 44 bool CDSubChannelReplacement::LoadSBI(const std::string& path) 45 { 46 auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb"); 47 if (!fp) 48 return false; 49 50 char header[4]; 51 if (std::fread(header, sizeof(header), 1, fp.get()) != 1) 52 { 53 ERROR_LOG("Failed to read header for '{}'", path); 54 return true; 55 } 56 57 static constexpr char expected_header[] = {'S', 'B', 'I', '\0'}; 58 if (std::memcmp(header, expected_header, sizeof(header)) != 0) 59 { 60 ERROR_LOG("Invalid header in '{}'", path); 61 return true; 62 } 63 64 m_replacement_subq.clear(); 65 66 SBIFileEntry entry; 67 while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1) 68 { 69 if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) || 70 !IsValidPackedBCD(entry.frame_bcd)) 71 { 72 ERROR_LOG("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd, entry.frame_bcd, 73 path); 74 return false; 75 } 76 77 if (entry.type != 1) 78 { 79 ERROR_LOG("Invalid type 0x{:02X} in '{}'", entry.type, path); 80 return false; 81 } 82 83 const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd); 84 85 CDImage::SubChannelQ subq; 86 std::memcpy(subq.data.data(), entry.data, sizeof(entry.data)); 87 88 // generate an invalid crc by flipping all bits from the valid crc (will never collide) 89 const u16 crc = subq.ComputeCRC(subq.data) ^ 0xFFFF; 90 subq.data[10] = Truncate8(crc); 91 subq.data[11] = Truncate8(crc >> 8); 92 93 m_replacement_subq.emplace(lba, subq); 94 } 95 96 INFO_LOG("Loaded {} replacement sectors from SBI '{}'", m_replacement_subq.size(), path); 97 return true; 98 } 99 100 bool CDSubChannelReplacement::LoadLSD(const std::string& path) 101 { 102 auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb"); 103 if (!fp) 104 return false; 105 106 m_replacement_subq.clear(); 107 108 LSDFileEntry entry; 109 while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1) 110 { 111 if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) || 112 !IsValidPackedBCD(entry.frame_bcd)) 113 { 114 ERROR_LOG("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd, entry.frame_bcd, 115 path); 116 return false; 117 } 118 119 const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd); 120 121 CDImage::SubChannelQ subq; 122 std::memcpy(subq.data.data(), entry.data, sizeof(entry.data)); 123 124 DEBUG_LOG("{:02x}:{:02x}:{:02x}: CRC {}", entry.minute_bcd, entry.second_bcd, entry.frame_bcd, 125 subq.IsCRCValid() ? "VALID" : "INVALID"); 126 m_replacement_subq.emplace(lba, subq); 127 } 128 129 INFO_LOG("Loaded {} replacement sectors from LSD '{}'", m_replacement_subq.size(), path); 130 return true; 131 } 132 133 bool CDSubChannelReplacement::LoadFromImagePath(std::string_view image_path) 134 { 135 if (const std::string filename = Path::ReplaceExtension(image_path, "sbi"); LoadSBI(filename.c_str())) 136 return true; 137 138 if (const std::string filename = Path::ReplaceExtension(image_path, "lsd"); LoadLSD(filename.c_str())) 139 return true; 140 141 return false; 142 } 143 144 void CDSubChannelReplacement::AddReplacementSubChannelQ(u32 lba, const CDImage::SubChannelQ& subq) 145 { 146 auto iter = m_replacement_subq.find(lba); 147 if (iter != m_replacement_subq.end()) 148 iter->second.data = subq.data; 149 else 150 m_replacement_subq.emplace(lba, subq); 151 } 152 153 bool CDSubChannelReplacement::GetReplacementSubChannelQ(u8 minute_bcd, u8 second_bcd, u8 frame_bcd, 154 CDImage::SubChannelQ* subq) const 155 { 156 return GetReplacementSubChannelQ(MSFToLBA(minute_bcd, second_bcd, frame_bcd), subq); 157 } 158 159 bool CDSubChannelReplacement::GetReplacementSubChannelQ(u32 lba, CDImage::SubChannelQ* subq) const 160 { 161 const auto iter = m_replacement_subq.find(lba); 162 if (iter == m_replacement_subq.cend()) 163 return false; 164 165 *subq = iter->second; 166 return true; 167 }