Skip to content

Commit

Permalink
SecuROM foundation and v4 support, PS2 info
Browse files Browse the repository at this point in the history
  • Loading branch information
superg committed Oct 8, 2023
1 parent 07d6066 commit 5105654
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ target_sources(redumper
"scsi/cmd.ixx"
"scsi/mmc.ixx"
"scsi/sptd.ixx"
"systems/securom.ixx"
"systems/system_cdrom.ixx"
"systems/system_iso.ixx"
"systems/system_mcd.ixx"
"systems/system_psx.ixx"
"systems/system_ps2.ixx"
"systems/system_ss.ixx"
"systems/system.ixx"
"systems/systems.ixx"
Expand Down
71 changes: 70 additions & 1 deletion debug.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module;
#include <string>
#include <filesystem>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include "throw_line.hh"

Expand Down Expand Up @@ -63,6 +65,26 @@ export void debug_subchannel(Options &options)
}


#pragma pack(push, 1)
struct SBIEntry
{
MSF msf;
uint8_t one;

struct Q
{
uint8_t adr :4;
uint8_t control :4;
uint8_t tno;
uint8_t point_index;
MSF msf;
uint8_t zero;
MSF a_msf;
} q;
};
#pragma pack(pop)


export void debug(Context &ctx, Options &options)
{
std::string image_prefix = (std::filesystem::path(options.image_path) / options.image_name).string();
Expand All @@ -71,7 +93,54 @@ export void debug(Context &ctx, Options &options)
std::filesystem::path toc_path(image_prefix + ".toc");
std::filesystem::path cdtext_path(image_prefix + ".cdtext");
std::filesystem::path cue_path(image_prefix + ".cue");
// std::filesystem::path sbi_path(image_prefix + ".sbi");

// SBI
if(1)
{
std::vector<std::pair<std::vector<std::string>, std::set<int32_t>>> dictionary;

for(auto &entry : std::filesystem::directory_iterator(options.image_path))
{
LOG("{}", entry.path().string());

std::ifstream ifs(entry.path(), std::ifstream::binary);
if(ifs.is_open())
{
char magic[4];

uint32_t entries_count = (std::filesystem::file_size(entry.path()) - sizeof(magic)) / sizeof(SBIEntry);
std::vector<SBIEntry> entries(entries_count);

ifs.read(magic, sizeof(magic));
for(auto &e : entries)
ifs.read((char *)&e, sizeof(e));

std::set<int32_t> values;
for(auto const &e : entries)
values.insert(BCDMSF_to_LBA(e.msf));

bool add = true;
for(auto &d : dictionary)
if(d.second == values)
{
d.first.push_back(entry.path().filename().string());
add = false;
break;
}
if(add)
{
std::vector<std::string> names;
names.push_back(entry.path().filename().string());
dictionary.emplace_back(names, values);
}

LOG("");
}
}

LOG("");
}
/*
// DVD sectors count
if(1)
Expand Down Expand Up @@ -135,7 +204,7 @@ export void debug(Context &ctx, Options &options)
*/
}

if(1)
if(0)
{
auto cache = asus_cache_read(*ctx.sptd, DriveConfig::Type::LG_ASU3);
LOG("");
Expand Down
1 change: 0 additions & 1 deletion drive.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module;
#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "throw_line.hh"
Expand Down
31 changes: 31 additions & 0 deletions dump.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import options;
import scsi.cmd;
import scsi.mmc;
import scsi.sptd;
import utils.endian;
import utils.file_io;
import utils.logger;


Expand Down Expand Up @@ -113,4 +115,33 @@ export uint32_t sample_offset_r2a(int32_t relative)
return relative - (LBA_START * CD_DATA_SIZE_SAMPLES);
}


export std::vector<ChannelQ> load_subq(const std::filesystem::path &sub_path)
{
std::vector<ChannelQ> subq(std::filesystem::file_size(sub_path) / CD_SUBCODE_SIZE);

std::fstream fs(sub_path, std::fstream::in | std::fstream::binary);
if(!fs.is_open())
throw_line("unable to open file ({})", sub_path.filename().string());

std::vector<uint8_t> sub_buffer(CD_SUBCODE_SIZE);
for(uint32_t lba_index = 0; lba_index < subq.size(); ++lba_index)
{
read_entry(fs, sub_buffer.data(), (uint32_t)sub_buffer.size(), lba_index, 1, 0, 0);
subcode_extract_channel((uint8_t *)&subq[lba_index], sub_buffer.data(), Subchannel::Q);
}

return subq;
}


export std::ostream &redump_print_subq(std::ostream &os, int32_t lba, const ChannelQ &Q)
{
MSF msf = LBA_to_MSF(lba);
os << std::format("MSF: {:02}:{:02}:{:02} Q-Data: {:X}{:X}{:02X}{:02X} {:02X}:{:02X}:{:02X} {:02X} {:02X}:{:02X}:{:02X} {:04X}",
msf.m, msf.s, msf.f, (uint8_t)Q.control, (uint8_t)Q.adr, Q.mode1.tno, Q.mode1.point_index, Q.mode1.msf.m, Q.mode1.msf.s, Q.mode1.msf.f, Q.mode1.zero, Q.mode1.a_msf.m, Q.mode1.a_msf.s, Q.mode1.a_msf.f, endian_swap<uint16_t>(Q.crc)) << std::endl;

return os;
}

}
12 changes: 1 addition & 11 deletions split.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -1068,17 +1068,7 @@ export void redumper_split_cd(const Options &options)
std::vector<ChannelQ> subq;
if(std::filesystem::exists(sub_path))
{
std::fstream fs(sub_path, std::fstream::in | std::fstream::binary);
if(!fs.is_open())
throw_line("unable to open file ({})", sub_path.filename().string());

subq.resize(sectors_count);
std::vector<uint8_t> sub_buffer(CD_SUBCODE_SIZE);
for(uint32_t lba_index = 0; lba_index < sectors_count; ++lba_index)
{
read_entry(fs, sub_buffer.data(), (uint32_t)sub_buffer.size(), lba_index, 1, 0, 0);
subcode_extract_channel((uint8_t *)&subq[lba_index], sub_buffer.data(), Subchannel::Q);
}
subq = load_subq(sub_path);

// correct Q
LOG_F("correcting Q... ");
Expand Down
88 changes: 88 additions & 0 deletions systems/securom.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module;
#include <filesystem>
#include <format>
#include <fstream>
#include <ostream>
#include <vector>
#include "throw_line.hh"

export module systems.securom;

import cd.cd;
import cd.subcode;
import crc.crc16_gsm;
import dump;
import readers.sector_reader;
import systems.system;
import utils.endian;
import utils.file_io;
import utils.misc;



namespace gpsxre
{

export class SystemSecuROM : public System
{
public:
std::string getName() override
{
return "SecuROM";
}


Type getType() override
{
return Type::RAW_DATA;
}


void printInfo(std::ostream &os, SectorReader *sector_reader, const std::filesystem::path &track_path) const override
{
std::filesystem::path sub_path = track_extract_basename(track_path.string()) + ".subcode";
if(!std::filesystem::exists(sub_path))
return;

std::vector<ChannelQ> subq = load_subq(sub_path);
std::vector<ChannelQ> subq_fixed = subq;
if(!subcode_correct_subq(subq_fixed.data(), subq_fixed.size()))
return;

std::vector<int32_t> candidates, candidates_8001;

for(uint32_t lba_index = 0; lba_index < subq_fixed.size(); ++lba_index)
{
if(!subq_fixed[lba_index].isValid() || subq[lba_index].isValid())
continue;

int32_t lba = lba_index + LBA_START;

uint16_t crc_current = endian_swap(subq[lba_index].crc);
uint16_t crc_expected = CRC16_GSM().update(subq[lba_index].raw, sizeof(subq[lba_index].raw)).final();
uint16_t crc_fixed = endian_swap(subq_fixed[lba_index].crc);

if((crc_current ^ 0x0080) == crc_expected)
candidates.push_back(lba);
else if((crc_current ^ 0x8001) == crc_fixed)
candidates_8001.push_back(lba);
}

uint32_t version = 0;

if(candidates_8001.size() == 10 || candidates_8001.size() == 11 && candidates_8001.front() == -1)
{
candidates.swap(candidates_8001);
version = 4;
}

if(version)
{
os << std::format(" version: {}", version) << std::endl;
for(auto const &c : candidates)
redump_print_subq(os, c, subq[c - LBA_START]);
}
}
};

}
Loading

0 comments on commit 5105654

Please sign in to comment.