-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDImage: Support loading subchannel from LSD files
- Loading branch information
Showing
9 changed files
with
71 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <[email protected]> | ||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <[email protected]> | ||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) | ||
|
||
#include "cd_subchannel_replacement.h" | ||
|
@@ -18,6 +18,14 @@ struct SBIFileEntry | |
u8 type; | ||
u8 data[10]; | ||
}; | ||
struct LSDFileEntry | ||
{ | ||
u8 minute_bcd; | ||
u8 second_bcd; | ||
u8 frame_bcd; | ||
u8 data[12]; | ||
}; | ||
static_assert(sizeof(LSDFileEntry) == 15); | ||
#pragma pack(pop) | ||
|
||
CDSubChannelReplacement::CDSubChannelReplacement() = default; | ||
|
@@ -33,23 +41,23 @@ static constexpr u32 MSFToLBA(u8 minute_bcd, u8 second_bcd, u8 frame_bcd) | |
return (ZeroExtend32(minute) * 60 * 75) + (ZeroExtend32(second) * 75) + ZeroExtend32(frame); | ||
} | ||
|
||
bool CDSubChannelReplacement::LoadSBI(const char* path) | ||
bool CDSubChannelReplacement::LoadSBI(const std::string& path) | ||
{ | ||
auto fp = FileSystem::OpenManagedCFile(path, "rb"); | ||
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb"); | ||
if (!fp) | ||
return false; | ||
|
||
char header[4]; | ||
if (std::fread(header, sizeof(header), 1, fp.get()) != 1) | ||
{ | ||
Log_ErrorPrintf("Failed to read header for '%s'", path); | ||
Log_ErrorFmt("Failed to read header for '{}'", path); | ||
return true; | ||
} | ||
|
||
static constexpr char expected_header[] = {'S', 'B', 'I', '\0'}; | ||
if (std::memcmp(header, expected_header, sizeof(header)) != 0) | ||
{ | ||
Log_ErrorPrintf("Invalid header in '%s'", path); | ||
Log_ErrorFmt("Invalid header in '{}'", path); | ||
return true; | ||
} | ||
|
||
|
@@ -59,14 +67,14 @@ bool CDSubChannelReplacement::LoadSBI(const char* path) | |
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) || | ||
!IsValidPackedBCD(entry.frame_bcd)) | ||
{ | ||
Log_ErrorPrintf("Invalid position [%02x:%02x:%02x] in '%s'", entry.minute_bcd, entry.second_bcd, entry.frame_bcd, | ||
path); | ||
Log_ErrorFmt("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd, | ||
entry.frame_bcd, path); | ||
return false; | ||
} | ||
|
||
if (entry.type != 1) | ||
{ | ||
Log_ErrorPrintf("Invalid type 0x%02X in '%s'", entry.type, path); | ||
Log_ErrorFmt("Invalid type 0x{:02X} in '{}'", entry.type, path); | ||
return false; | ||
} | ||
|
||
|
@@ -83,13 +91,50 @@ bool CDSubChannelReplacement::LoadSBI(const char* path) | |
m_replacement_subq.emplace(lba, subq); | ||
} | ||
|
||
Log_InfoPrintf("Loaded %zu replacement sectors from '%s'", m_replacement_subq.size(), path); | ||
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; | ||
|
||
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); | ||
return true; | ||
} | ||
|
||
bool CDSubChannelReplacement::LoadSBIFromImagePath(const char* image_path) | ||
bool CDSubChannelReplacement::LoadFromImagePath(std::string_view image_path) | ||
{ | ||
return LoadSBI(Path::ReplaceExtension(image_path, "sbi").c_str()); | ||
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; | ||
} | ||
|
||
void CDSubChannelReplacement::AddReplacementSubChannelQ(u32 lba, const CDImage::SubChannelQ& subq) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters