Skip to content

Commit

Permalink
CDVD: Report open errors to host
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 1, 2023
1 parent 93a4e67 commit a9959bc
Show file tree
Hide file tree
Showing 29 changed files with 592 additions and 547 deletions.
5 changes: 0 additions & 5 deletions common/Pcsx2Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ static constexpr unsigned int __pagemask = __pagesize - 1;
#define likely(x) (!!(x))
#define unlikely(x) (!!(x))

#define CALLBACK __stdcall

#else

// --------------------------------------------------------------------------------------
Expand All @@ -110,9 +108,6 @@ static constexpr unsigned int __pagemask = __pagesize - 1;
// SysV ABI passes vector parameters through registers unconditionally.
#ifndef _WIN32
#define __vectorcall
#define CALLBACK
#else
#define CALLBACK __attribute__((stdcall))
#endif

// Inlining note: GCC needs ((unused)) attributes defined on inlined functions to suppress
Expand Down
125 changes: 64 additions & 61 deletions pcsx2/AsyncFileReader.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2014 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
Expand All @@ -16,54 +16,56 @@
#pragma once

#ifdef _WIN32
# include "common/RedtapeWindows.h"
#include "common/RedtapeWindows.h"
#elif defined(__linux__)
# include <libaio.h>
#include <libaio.h>
#elif defined(__POSIX__)
# include <aio.h>
#include <aio.h>
#endif
#include <memory>
#include <string>

class Error;

class AsyncFileReader
{
protected:
AsyncFileReader() : m_dataoffset(0), m_blocksize(0) {}
AsyncFileReader()
: m_dataoffset(0)
, m_blocksize(0)
{
}

std::string m_filename;

int m_dataoffset;
uint m_blocksize;
u32 m_dataoffset;
u32 m_blocksize;

public:
virtual ~AsyncFileReader() {};

virtual bool Open(std::string fileName)=0;
virtual ~AsyncFileReader(){};

virtual int ReadSync(void* pBuffer, uint sector, uint count)=0;
virtual bool Open(std::string filename, Error* error) = 0;

virtual void BeginRead(void* pBuffer, uint sector, uint count)=0;
virtual int FinishRead(void)=0;
virtual void CancelRead(void)=0;
virtual int ReadSync(void* pBuffer, u32 sector, u32 count) = 0;

virtual void Close(void)=0;
virtual void BeginRead(void* pBuffer, u32 sector, u32 count) = 0;
virtual int FinishRead() = 0;
virtual void CancelRead() = 0;

virtual uint GetBlockCount(void) const=0;
virtual void Close() = 0;

virtual void SetBlockSize(uint bytes) {}
virtual void SetDataOffset(int bytes) {}
virtual u32 GetBlockCount() const = 0;

uint GetBlockSize() const { return m_blocksize; }
virtual void SetBlockSize(u32 bytes) {}
virtual void SetDataOffset(u32 bytes) {}

const std::string& GetFilename() const
{
return m_filename;
}
const std::string& GetFilename() const { return m_filename; }
u32 GetBlockSize() const { return m_blocksize; }
};

class FlatFileReader : public AsyncFileReader
class FlatFileReader final : public AsyncFileReader
{
DeclareNoncopyableObject( FlatFileReader );
DeclareNoncopyableObject(FlatFileReader);

#ifdef _WIN32
HANDLE hOverlappedFile;
Expand All @@ -86,65 +88,66 @@ class FlatFileReader : public AsyncFileReader

public:
FlatFileReader(bool shareWrite = false);
virtual ~FlatFileReader() override;
~FlatFileReader() override;

virtual bool Open(std::string fileName) override;
bool Open(std::string filenae, Error* error) override;

virtual int ReadSync(void* pBuffer, uint sector, uint count) override;
int ReadSync(void* pBuffer, u32 sector, u32 count) override;

virtual void BeginRead(void* pBuffer, uint sector, uint count) override;
virtual int FinishRead(void) override;
virtual void CancelRead(void) override;
void BeginRead(void* pBuffer, u32 sector, u32 count) override;
int FinishRead() override;
void CancelRead() override;

virtual void Close(void) override;
void Close() override;

virtual uint GetBlockCount(void) const override;
u32 GetBlockCount() const override;

virtual void SetBlockSize(uint bytes) override { m_blocksize = bytes; }
virtual void SetDataOffset(int bytes) override { m_dataoffset = bytes; }
void SetBlockSize(u32 bytes) override { m_blocksize = bytes; }
void SetDataOffset(u32 bytes) override { m_dataoffset = bytes; }
};

class MultipartFileReader : public AsyncFileReader
class MultipartFileReader final : public AsyncFileReader
{
DeclareNoncopyableObject( MultipartFileReader );
DeclareNoncopyableObject(MultipartFileReader);

static const int MaxParts = 8;

struct Part {
uint start;
uint end; // exclusive
struct Part
{
u32 start;
u32 end; // exclusive
bool isReading;
AsyncFileReader* reader;
} m_parts[MaxParts];
uint m_numparts;
u32 m_numparts;

uint GetFirstPart(uint lsn);
u32 GetFirstPart(u32 lsn);
void FindParts();

public:
MultipartFileReader(AsyncFileReader* firstPart);
virtual ~MultipartFileReader() override;
~MultipartFileReader() override;

virtual bool Open(std::string fileName) override;
bool Open(std::string filename, Error* error) override;

virtual int ReadSync(void* pBuffer, uint sector, uint count) override;
int ReadSync(void* pBuffer, u32 sector, u32 count) override;

virtual void BeginRead(void* pBuffer, uint sector, uint count) override;
virtual int FinishRead(void) override;
virtual void CancelRead(void) override;
void BeginRead(void* pBuffer, u32 sector, u32 count) override;
int FinishRead() override;
void CancelRead() override;

virtual void Close(void) override;
void Close() override;

virtual uint GetBlockCount(void) const override;
u32 GetBlockCount() const override;

virtual void SetBlockSize(uint bytes) override;
void SetBlockSize(u32 bytes) override;

static AsyncFileReader* DetectMultipart(AsyncFileReader* reader);
};

class BlockdumpFileReader : public AsyncFileReader
{
DeclareNoncopyableObject( BlockdumpFileReader );
DeclareNoncopyableObject(BlockdumpFileReader);

std::FILE* m_file;

Expand All @@ -160,21 +163,21 @@ class BlockdumpFileReader : public AsyncFileReader

public:
BlockdumpFileReader();
virtual ~BlockdumpFileReader() override;
~BlockdumpFileReader() override;

virtual bool Open(std::string fileName) override;
bool Open(std::string filename, Error* error) override;

virtual int ReadSync(void* pBuffer, uint sector, uint count) override;
int ReadSync(void* pBuffer, u32 sector, u32 count) override;

virtual void BeginRead(void* pBuffer, uint sector, uint count) override;
virtual int FinishRead(void) override;
virtual void CancelRead(void) override;
void BeginRead(void* pBuffer, u32 sector, u32 count) override;
int FinishRead() override;
void CancelRead() override;

virtual void Close(void) override;
void Close() override;

virtual uint GetBlockCount(void) const override;
u32 GetBlockCount() const override;

static bool DetectBlockdump(AsyncFileReader* reader);

int GetBlockOffset() { return m_blockofs; }
s32 GetBlockOffset() { return m_blockofs; }
};
38 changes: 23 additions & 15 deletions pcsx2/CDVD/BlockdumpFileReader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2014 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
Expand All @@ -18,6 +18,7 @@
#include "IsoFileFormats.h"
#include "common/Assertions.h"
#include "common/FileSystem.h"
#include "common/Error.h"

#include <errno.h>

Expand All @@ -27,11 +28,11 @@ enum isoFlags
ISOFLAGS_BLOCKDUMP_V3 = 0x0020
};

static const uint BlockDumpHeaderSize = 16;
static constexpr u32 BlockDumpHeaderSize = 16;

bool BlockdumpFileReader::DetectBlockdump(AsyncFileReader* reader)
{
uint oldbs = reader->GetBlockSize();
u32 oldbs = reader->GetBlockSize();

reader->SetBlockSize(1);

Expand All @@ -45,7 +46,7 @@ bool BlockdumpFileReader::DetectBlockdump(AsyncFileReader* reader)
return isbd;
}

BlockdumpFileReader::BlockdumpFileReader(void)
BlockdumpFileReader::BlockdumpFileReader()
: m_file(NULL)
, m_blocks(0)
, m_blockofs(0)
Expand All @@ -54,19 +55,22 @@ BlockdumpFileReader::BlockdumpFileReader(void)
{
}

BlockdumpFileReader::~BlockdumpFileReader(void)
BlockdumpFileReader::~BlockdumpFileReader()
{
Close();
}

bool BlockdumpFileReader::Open(std::string fileName)
bool BlockdumpFileReader::Open(std::string filename, Error* error)
{
char signature[4];

m_filename = std::move(fileName);
m_file = FileSystem::OpenCFile(m_filename.c_str(), "rb");
if (!m_file || std::fread(signature, sizeof(signature), 1, m_file) != 1 || std::memcmp(signature, "BDV2", sizeof(signature)) != 0)
m_filename = std::move(filename);
if (!(m_file = FileSystem::OpenCFile(m_filename.c_str(), "rb", error)))
return false;

if (std::fread(signature, sizeof(signature), 1, m_file) != 1 || std::memcmp(signature, "BDV2", sizeof(signature)) != 0)
{
Error::SetString(error, "Block dump signature is invalid.");
return false;
}

Expand All @@ -75,6 +79,7 @@ bool BlockdumpFileReader::Open(std::string fileName)
|| std::fread(&m_blocks, sizeof(m_blocks), 1, m_file) != 1
|| std::fread(&m_blockofs, sizeof(m_blockofs), 1, m_file) != 1)
{
Error::SetString(error, "Failed to read block dump information.");
return false;
}

Expand All @@ -87,7 +92,10 @@ bool BlockdumpFileReader::Open(std::string fileName)
m_dtable = std::make_unique<u32[]>(m_dtablesize);

if (FileSystem::FSeek64(m_file, BlockDumpHeaderSize, SEEK_SET) != 0)
{
Error::SetString(error, "Failed to seek to block dump data.");
return false;
}

u32 bs = 1024 * 1024;
u32 off = 0;
Expand All @@ -112,7 +120,7 @@ bool BlockdumpFileReader::Open(std::string fileName)
return true;
}

int BlockdumpFileReader::ReadSync(void* pBuffer, uint lsn, uint count)
int BlockdumpFileReader::ReadSync(void* pBuffer, u32 lsn, u32 count)
{
u8* dst = (u8*)pBuffer;
// Console.WriteLn("_isoReadBlockD %u, blocksize=%u, blockofs=%u\n", lsn, iso->blocksize, iso->blockofs);
Expand Down Expand Up @@ -159,17 +167,17 @@ int BlockdumpFileReader::ReadSync(void* pBuffer, uint lsn, uint count)
return 0;
}

void BlockdumpFileReader::BeginRead(void* pBuffer, uint sector, uint count)
void BlockdumpFileReader::BeginRead(void* pBuffer, u32 sector, u32 count)
{
m_lresult = ReadSync(pBuffer, sector, count);
}

int BlockdumpFileReader::FinishRead(void)
int BlockdumpFileReader::FinishRead()
{
return m_lresult;
}

void BlockdumpFileReader::CancelRead(void)
void BlockdumpFileReader::CancelRead()
{
}

Expand All @@ -178,11 +186,11 @@ void BlockdumpFileReader::Close(void)
if (m_file)
{
std::fclose(m_file);
m_file = NULL;
m_file = nullptr;
}
}

uint BlockdumpFileReader::GetBlockCount(void) const
u32 BlockdumpFileReader::GetBlockCount() const
{
return m_blocks;
}
Loading

0 comments on commit a9959bc

Please sign in to comment.