Skip to content

Commit

Permalink
added fixmsf command, updated help for skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
superg committed Oct 14, 2024
1 parent 5b8c72f commit 41ddd1e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ target_sources(redumper
"cd/cdrom.ixx"
"cd/ecc.ixx"
"cd/edc.ixx"
"cd/fix_msf.ixx"
"cd/offset_manager.ixx"
"cd/protection.ixx"
"cd/scrambler.ixx"
Expand Down
107 changes: 107 additions & 0 deletions cd/fix_msf.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
module;

#include <filesystem>
#include <fstream>
#include <list>
#include <optional>
#include <sstream>
#include <string>
#include <utility>
#include "systems/system.hh"
#include "throw_line.hh"

export module cd.fix_msf;

import cd.cdrom;
import dump;
import filesystem.iso9660;
import options;
import readers.image_bin_form1_reader;
import readers.image_iso_form1_reader;
import readers.image_raw_reader;
import readers.sector_reader;
import systems.systems;
import utils.hex_bin;
import utils.logger;
import utils.misc;
import utils.strings;



namespace gpsxre
{

enum class TrackType
{
DATA,
AUDIO,
ISO
};

export void redumper_fix_msf(Context &ctx, Options &options)
{
image_check_empty(options);

auto image_prefix = (std::filesystem::path(options.image_path) / options.image_name).string();

std::list<std::filesystem::path> tracks;
if(std::filesystem::exists(image_prefix + ".cue"))
for(auto const &t : cue_get_entries(image_prefix + ".cue"))
if(t.second)
tracks.push_back(std::filesystem::path(options.image_path) / t.first);

if(tracks.empty())
throw_line("no files to process");

uint32_t msf_errors = 0;

for(auto const &t : tracks)
{
uint32_t sectors_count = std::filesystem::file_size(t) / sizeof(Sector);
std::fstream fs(t, std::fstream::binary | std::fstream::in | std::fstream::out);

std::optional<int32_t> lba_base;

for(uint32_t s = 0; s < sectors_count; ++s)
{
Sector sector;
fs.read((char *)&sector, sizeof(sector));
if(fs.fail())
throw_line("read failed");

if(memcmp(sector.sync, CD_DATA_SYNC, sizeof(CD_DATA_SYNC)))
continue;

if(sector.header.mode != 2)
{
LOG("warning: unsupported track mode, skipping (track: {}, mode: {})", t.generic_string(), sector.header.mode);
break;
}

int32_t lba = BCDMSF_to_LBA(sector.header.address);
if(lba_base)
{
int32_t lba_expected = *lba_base + s;
if(lba != lba_expected)
{
sector.header.address = LBA_to_BCDMSF(lba_expected);
fs.seekp(s * sizeof(Sector));
if(fs.fail())
throw_line("read failed");
fs.write((char *)&sector, sizeof(sector));
if(fs.fail())
throw_line("write failed");
fs << std::flush;

++msf_errors;
}
}
else
lba_base = lba;
}
}

LOG("corrected {} sectors", msf_errors);
}

}
2 changes: 2 additions & 0 deletions options.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ export struct Options
LOG("\tsplit \tgenerates BIN/CUE track split from dump files");
LOG("\thash \toutputs XML DAT hash entries (CUE/BIN or ISO)");
LOG("\tinfo \toutputs basic image information (CUE/BIN or ISO)");
LOG("\tskeleton \tgenerates image file with zeroed content");
LOG("\tfixmsf \tcorrects unexpected MSF in all data tracks (only mode2 sectors)");
LOG("");

LOG("OPTIONS:");
Expand Down
4 changes: 3 additions & 1 deletion redumper.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export module redumper;
import cd.cd;
import cd.dump;
import cd.dump_new;
import cd.fix_msf;
import cd.protection;
import cd.scrambler;
import cd.split;
Expand Down Expand Up @@ -156,7 +157,8 @@ const std::map<std::string, std::pair<bool, void (*)(Context &, Options &)>> COM
{ "skeleton", { false, redumper_skeleton } },

{ "subchannel", { false, redumper_subchannel } },
{ "debug", { false, redumper_debug } }
{ "debug", { false, redumper_debug } },
{ "fixmsf", { false, redumper_fix_msf } }
};


Expand Down

0 comments on commit 41ddd1e

Please sign in to comment.