Skip to content

Commit

Permalink
cd: added .ecm file format support #41
Browse files Browse the repository at this point in the history
Due to ECM format design it is impossible to access data in random order.
Whole file needs to be processed first to build LUT table for offsets of sectors in .ecm file.
This implementation loads the whole processed file to RAM which will result in up to 800MB allocated by emulator when loading .ecm file.
Loading might freeze emulator for few seconds depending on drive speed - loader is single-threaded for now.
  • Loading branch information
JaCzekanski committed Jun 27, 2020
1 parent 5cbb48c commit f715365
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ add_library(core STATIC
src/disc/format/chd_format.cpp
src/disc/format/cue.cpp
src/disc/format/cue_parser.cpp
src/disc/format/ecm.cpp
src/disc/format/ecm_parser.cpp
src/disc/load.cpp
src/disc/position.cpp
src/disc/subchannel_q.cpp
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ See [Avocado compatibility list](https://avocado-db.czekanski.info)

## Changelog

*28.06.2020* - .ecm format support

*16.09.2019* - Save states

*5.09.2019* - Vibration support
Expand Down Expand Up @@ -72,7 +74,7 @@ Currently Avocado requires OpenGL 3.2. In the future this limitation will be lif
Avocado requires the BIOS from real console in the `data/bios` directory. (use `File->Open Avocado directory` to locate the directory on your system)
Selection of a BIOS rom will be required on the first run. The rom can be changed under `Options->BIOS` or by modifying the **config.json** file.

To load a `.cue/.bin/.img/.chd` or `.exe/.psexe/.psf/.minipsf` file just drag and drop it.
To load a `.cue/.bin/.img/.chd/.ecm` or `.exe/.psexe/.psf/.minipsf` file just drag and drop it.

PAL games with LibCrypt protection need additional subchannel info - download proper file `.SBI` or `.LSD` file from [Redump](http://redump.org/discs/system/psx/), place it in the same folder as game image and make sure has identical name as `.cue/.bin/...` file.

Expand Down
32 changes: 32 additions & 0 deletions src/disc/format/ecm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "ecm.h"
#include <utility>
#include <array>

namespace disc::format {
Ecm::Ecm(std::string file, std::vector<uint8_t> data) : file(std::move(file)), data(std::move(data)) {}

std::string Ecm::getFile() const { return file; }

disc::Position Ecm::getDiskSize() const { return disc::Position::fromLba(data.size() / Track::SECTOR_SIZE); }

size_t Ecm::getTrackCount() const { return 1; }

disc::Position Ecm::getTrackStart(int track) const { return disc::Position(0, 2, 0); }

disc::Position Ecm::getTrackLength(int track) const { return disc::Position::fromLba(data.size() / Track::SECTOR_SIZE); }

int Ecm::getTrackByPosition(disc::Position pos) const { return 1; }

disc::Sector Ecm::read(disc::Position pos) {
std::vector<uint8_t> sector(Track::SECTOR_SIZE);

size_t lba = (pos - disc::Position(0, 2, 0)).toLba() * Track::SECTOR_SIZE;
if (lba + Track::SECTOR_SIZE >= data.size()) {
return std::make_pair(sector, TrackType::INVALID);
}

std::copy(data.begin() + lba, data.begin() + lba + Track::SECTOR_SIZE, sector.begin());

return std::make_pair(sector, TrackType::DATA);
}
} // namespace disc::format
30 changes: 30 additions & 0 deletions src/disc/format/ecm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <cstdio>
#include <memory>
#include <optional>
#include <unordered_map>
#include <vector>
#include "disc/disc.h"
#include "disc/position.h"
#include "disc/track.h"
#include "utils/file.h"

namespace disc::format {
struct Ecm : public Disc {
private:
std::string file;
std::vector<uint8_t> data;

public:
Ecm(std::string file, std::vector<uint8_t> data);

std::string getFile() const override;
Position getDiskSize() const override;
size_t getTrackCount() const override;
Position getTrackStart(int track) const override;
Position getTrackLength(int track) const override;
int getTrackByPosition(Position pos) const override;

disc::Sector read(Position pos) override;
};
} // namespace disc::format
226 changes: 226 additions & 0 deletions src/disc/format/ecm_parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#include "ecm_parser.h"
#include <fmt/core.h>
#include <utility>
#include <array>

namespace {
constexpr std::array<uint32_t, 256> edcLUT = []() {
std::array<uint32_t, 256> lut = {};
for (int i = 0; i < 256; i++) {
uint32_t edc = i;

for (int j = 0; j < 8; j++) {
bool carry = edc & 1;
edc = (edc >> 1) ^ (carry ? 0xD8018001 : 0);
}

lut[i] = edc;
}
return lut;
}();

constexpr uint32_t eccEntry(uint8_t i) { return (i << 1) ^ (i & 0x80 ? 0x11d : 0); }

constexpr std::array<uint8_t, 256> eccfLUT = []() {
std::array<uint8_t, 256> lut = {};
for (int i = 0; i < 256; i++) {
lut[i] = eccEntry(i);
}
return lut;
}();

constexpr std::array<uint8_t, 256> eccbLUT = []() {
std::array<uint8_t, 256> lut = {};
for (int i = 0; i < 256; i++) {
lut[i ^ eccEntry(i)] = i;
}
return lut;
}();
} // namespace

namespace disc::format {

uint32_t EcmParser::calculateEDC(size_t addr, size_t size) const {
uint32_t edc = 0;
for (size_t i = 0; i < size; i++) {
edc ^= frame[addr + i];
edc = (edc >> 8) ^ edcLUT[edc & 0xff];
}
return edc;
}

void EcmParser::adjustEDC(size_t addr, size_t size) {
uint32_t edc = calculateEDC(addr, size);
size_t offset = addr + size;

for (int i = 0; i < 4; i++) {
frame[offset + i] = (edc >> (i * 8)) & 0xff;
}
}

void EcmParser::adjustSync() {
frame[0] = 0;
for (int i = 1; i < 11; i++) frame[i] = 0xff;
frame[11] = 0;
}

void EcmParser::copySubheader() {
for (int i = 0x10; i < 0x14; i++) {
frame[i] = frame[i + 4];
}
}

void EcmParser::computeECCblock(uint8_t* src, uint32_t majorCount, uint32_t minorCount, uint32_t majorMult, uint32_t minorInc,
uint8_t* dst) {
uint32_t size = majorCount * minorCount;
for (uint32_t major = 0; major < majorCount; major++) {
uint32_t index = (major >> 1) * majorMult + (major & 1);
uint32_t a = 0, b = 0;

for (uint32_t minor = 0; minor < minorCount; minor++) {
uint8_t temp = src[index];
index += minorInc;
if (index >= size) index -= size;
a ^= temp;
b ^= temp;

a = eccfLUT[a];
}
a = eccbLUT[eccfLUT[a] ^ b];
dst[major] = a;
dst[major + majorCount] = a ^ b;
}
}

void EcmParser::calculateAndAdjustECC() {
computeECCblock(frame + 0xc, 86, 24, 2, 86, frame + 0x81c);
computeECCblock(frame + 0x0c, 52, 43, 86, 88, frame + 0x8c8);
}

void EcmParser::handleMode1() {
fread(frame + 0x0c, 1, 0x804, f.get());

adjustSync();
frame[0xf] = 0x01;

adjustEDC(0x00, 0x810);
for (int i = 0x814; i < 0x818; i++) frame[i] = 0;

calculateAndAdjustECC();

data.insert(data.end(), frame, frame + 2352);
}

void EcmParser::handleMode2Form1() {
fread(frame + 0x14, 1, 0x804, f.get());

adjustSync();
frame[0xf] = 0x02;
copySubheader();

adjustEDC(0x10, 0x808);

uint8_t _address[4];
for (int i = 0; i < 4; i++) {
_address[i] = frame[12 + i];
frame[12 + i] = 0;
}

calculateAndAdjustECC();

for (int i = 0; i < 4; i++) {
frame[12 + i] = _address[i];
}

data.insert(data.end(), frame + 0x10, frame + 0x10 + 2336);
}

void EcmParser::handleMode2Form2() {
fread(frame + 0x14, 1, 0x918, f.get());

adjustSync();
frame[0xf] = 0x02;
copySubheader();

adjustEDC(0x10, 0x91c);
// Mode2Form2 has no ECC

data.insert(data.end(), frame + 0x10, frame + 0x10 + 2336);
}
std::unique_ptr<Ecm> EcmParser::parse(const char* file) {
f = unique_ptr_file(fopen(file, "rb"));
if (!f) {
fmt::print("[ECM] Cannot open {}.\n", file);
return {};
}

// Check header
char header[4];
fread(header, 1, 4, f.get());

if (memcmp("ECM\0", header, 4) != 0) {
fmt::print("[ECM] Invalid header.\n");
return {};
}

data.clear();
data.reserve(disc::Track::SECTOR_SIZE * 300000);

for (;;) {
int type = 0;
uint32_t count = 0;

for (int i = 0; i < 5; i++) {
uint8_t byte = fgetc(f.get());

if (i == 0) {
type = byte & 0b11;

count |= (byte & 0b0111'1100) >> 2;
} else {
int shift = 5 + (i - 1) * 7;
count |= (byte & 0x7f) << shift;
}

if ((byte & 0x80) == 0) break;
}

if (count == 0xffff'ffff) {
break;
}

count += 1;

uint32_t sector = data.size() / Track::SECTOR_SIZE;

if (count > 0x8000'0000) {
// Corrupt file
fmt::print("[ECM] Sector {} invalid count.\n", sector);
return {};
}

if (type == 0) {
while (count > 0) {
int len = std::min<int>(Track::SECTOR_SIZE, count);
fread(frame, 1, len, f.get());

data.insert(data.end(), frame, frame + len);
count -= len;
}
} else if (type == 1) {
for (; count > 0; count--) handleMode1();
} else if (type == 2) {
for (; count > 0; count--) handleMode2Form1();
} else if (type == 3) {
for (; count > 0; count--) handleMode2Form2();
} else {
fmt::print("[ECM] Sector {}, invalid type ({}, expected 0..3)\n", sector, type);
return {};
}
}

data.shrink_to_fit();
return std::make_unique<Ecm>(file, std::move(data));
}

} // namespace disc::format
28 changes: 28 additions & 0 deletions src/disc/format/ecm_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <cstdio>
#include <memory>
#include <vector>
#include "disc/disc.h"
#include "ecm.h"

namespace disc::format {
class EcmParser {
private:
unique_ptr_file f;
std::vector<uint8_t> data;
uint8_t frame[2352];

uint32_t calculateEDC(size_t addr, size_t size) const;
void adjustEDC(size_t addr, size_t size);
void adjustSync();
void copySubheader();
void calculateAndAdjustECC();
void computeECCblock(uint8_t* src, uint32_t majorCount, uint32_t minorCount, uint32_t majorMult, uint32_t minorInc, uint8_t* dst);
void handleMode1();
void handleMode2Form1();
void handleMode2Form2();

public:
std::unique_ptr<Ecm> parse(const char* file);
};
} // namespace disc::format
8 changes: 5 additions & 3 deletions src/disc/load.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "load.h"
#include <array>
#include <disc/format/ecm_parser.h>
#include "disc/format/chd_format.h"
#include "disc/format/cue_parser.h"
#include "utils/file.h"

namespace disc {
const std::array<std::string, 5> discFormats = {
"chd", "cue", "iso", "bin", "img",
};
const std::array<std::string, 6> discFormats = {"chd", "cue", "iso", "bin", "img", "ecm"};

bool isDiscImage(const std::string& path) {
std::string ext = getExtension(path);
Expand All @@ -28,6 +27,9 @@ std::unique_ptr<disc::Disc> load(const std::string& path) {
disc = parser.parse(path.c_str());
} else if (ext == "iso" || ext == "bin" || ext == "img") {
disc = disc::format::Cue::fromBin(path.c_str());
} else if (ext == "ecm") {
disc::format::EcmParser parser;
disc = parser.parse(path.c_str());
}

return disc;
Expand Down
3 changes: 2 additions & 1 deletion src/platform/windows/gui/file/open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace gui::file {
Open::Open() { windowName = "Open file##file_dialog"; }

bool Open::isFileSupported(const gui::helper::File& f) {
constexpr std::array<const char*, 9> supportedFiles = {
constexpr std::array<const char*, 10> supportedFiles = {
".iso", //
".cue", //
".bin", //
".img", //
".chd", //
".ecm", //
".exe", //
".psexe", //
".psf", //
Expand Down

0 comments on commit f715365

Please sign in to comment.