Merge pull request #1518 from PCSX2/pcsx2-high-level-fopen

Pcsx2 high level fopen
This commit is contained in:
Gregory Hainaut 2016-08-15 15:41:27 +02:00 committed by GitHub
commit 252c043409
1 changed files with 656 additions and 645 deletions

View File

@ -86,37 +86,35 @@ static int mg_BIToffset(u8 *buffer)
return ofs + 0x20; return ofs + 0x20;
} }
FILE *_cdvdOpenMechaVer() static void cdvdGetMechaVer(u8* ver)
{ {
// get the name of the bios file
wxFileName mecfile(EmuConfig.BiosFilename); wxFileName mecfile(EmuConfig.BiosFilename);
mecfile.SetExt( L"mec" ); mecfile.SetExt( L"mec" );
const wxString fname( mecfile.GetFullPath() ); const wxString fname( mecfile.GetFullPath() );
// if file doesnt exist, create empty one // Likely a bad idea to go further
FILE* fd = wxFopen(fname, L"r+b"); if (mecfile.IsDir())
if (fd == NULL)
{
Console.Warning("MEC File Not Found, creating substitute...");
fd = wxFopen(fname, L"wb");
if (fd == NULL)
throw Exception::CannotCreateStream(fname); throw Exception::CannotCreateStream(fname);
fputc(0x03, fd);
fputc(0x06, fd);
fputc(0x02, fd);
fputc(0x00, fd);
}
return fd;
}
static void cdvdGetMechaVer(u8* ver) if (Path::GetFileSize(fname) < 4) {
{ Console.Warning("MEC File Not Found, creating substitute...");
FILE* fd = _cdvdOpenMechaVer();
fseek(fd, 0, SEEK_SET); wxFFile fp(fname, L"wb");
fread(ver, 1, 4, fd); if (!fp.IsOpened())
fclose(fd); throw Exception::CannotCreateStream(fname);
u8 version[4] = {0x3, 0x6, 0x2, 0x0};
fp.Write(version, sizeof(version));
}
wxFFile fp(fname, L"rb");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
size_t ret = fp.Read(ver, 4);
if (ret != 4)
Console.Error(L"Failed to read from %s. Did only %zu/4 bytes", WX_STR(fname), ret);
} }
NVMLayout* getNvmLayout() NVMLayout* getNvmLayout()
@ -133,52 +131,58 @@ NVMLayout* getNvmLayout()
// Throws Exception::CannotCreateStream if the file cannot be opened for reading, or cannot // Throws Exception::CannotCreateStream if the file cannot be opened for reading, or cannot
// be created for some reason. // be created for some reason.
FILE* _cdvdOpenNVM() static void cdvdNVM(u8 *buffer, int offset, size_t bytes, bool read)
{ {
wxFileName nvmfile(EmuConfig.BiosFilename); wxFileName nvmfile(EmuConfig.BiosFilename);
nvmfile.SetExt( L"nvm" ); nvmfile.SetExt(L"nvm");
const wxString fname( nvmfile.GetFullPath() ); const wxString fname(nvmfile.GetFullPath());
// if file doesn't exist, create empty one // Likely a bad idea to go further
FILE* fd = wxFopen(fname, L"r+b"); if (nvmfile.IsDir())
if (fd == NULL)
{
Console.Warning("NVM File Not Found, Creating Blank File");
fd = wxFopen(fname, L"wb");
if (fd == NULL)
throw Exception::CannotCreateStream(fname); throw Exception::CannotCreateStream(fname);
for (int i=0; i<1024; i++) fputc(0, fd); if (Path::GetFileSize(fname) < 1024) {
Console.Warning("NVM File Not Found, creating substitute...");
wxFFile fp(fname, L"wb");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
u8 zero[1024] = {0};
fp.Write(zero, sizeof(zero));
//Write NVM ILink area with dummy data (Age of Empires 2) //Write NVM ILink area with dummy data (Age of Empires 2)
NVMLayout* nvmLayout = getNvmLayout(); NVMLayout* nvmLayout = getNvmLayout();
u8 ILinkID_Data[8] = { 0x00, 0xAC, 0xFF, 0xFF, 0xFF, 0xFF, 0xB9, 0x86 }; u8 ILinkID_Data[8] = { 0x00, 0xAC, 0xFF, 0xFF, 0xFF, 0xFF, 0xB9, 0x86 };
fseek(fd, *(s32*)(((u8*)nvmLayout)+offsetof(NVMLayout, ilinkId)), SEEK_SET); fp.Seek(*(s32*)(((u8*)nvmLayout) + offsetof(NVMLayout, ilinkId)));
fwrite(ILinkID_Data, 1, 8, fd); fp.Write(ILinkID_Data, sizeof(ILinkID_Data));
} }
return fd;
wxFFile fp(fname, L"r+b");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
fp.Seek(offset);
size_t ret;
if (read)
ret = fp.Read(buffer, bytes);
else
ret = fp.Write(buffer, bytes);
if (ret != bytes)
Console.Error(L"Failed to %s %s. Did only %zu/%zu bytes",
read ? L"read from" : L"write to", WX_STR(fname), ret, bytes);
} }
//
// the following 'cdvd' functions all return 0 if successful
//
static void cdvdReadNVM(u8 *dst, int offset, int bytes) { static void cdvdReadNVM(u8 *dst, int offset, int bytes) {
FILE* fd = _cdvdOpenNVM(); cdvdNVM(dst, offset, bytes, true);
fseek(fd, offset, SEEK_SET);
fread(dst, 1, bytes, fd);
fclose(fd);
} }
static void cdvdWriteNVM(const u8 *src, int offset, int bytes) { static void cdvdWriteNVM(const u8 *src, int offset, int bytes) {
FILE* fd = _cdvdOpenNVM(); cdvdNVM(const_cast<u8*>(src), offset, bytes, false);
fseek(fd, offset, SEEK_SET);
fwrite(src, 1, bytes, fd);
fclose(fd);
} }
void getNvmData(u8* buffer, s32 offset, s32 size, s32 fmtOffset) void getNvmData(u8* buffer, s32 offset, s32 size, s32 fmtOffset)
@ -1417,9 +1421,10 @@ static __fi void fail_pol_cal()
static void cdvdWrite16(u8 rt) // SCOMMAND static void cdvdWrite16(u8 rt) // SCOMMAND
{ {
// cdvdTN diskInfo; try {
// cdvdTD trackInfo; // cdvdTN diskInfo;
// int i, lbn, type, min, sec, frm, address; // cdvdTD trackInfo;
// int i, lbn, type, min, sec, frm, address;
int address; int address;
u8 tmp; u8 tmp;
@ -1429,10 +1434,10 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvd.Result[0] = 0; // assume success -- failures will overwrite this with an error code. cdvd.Result[0] = 0; // assume success -- failures will overwrite this with an error code.
switch (rt) { switch (rt) {
// case 0x01: // GetDiscType - from cdvdman (0:1) // case 0x01: // GetDiscType - from cdvdman (0:1)
// SetResultSize(1); // SetResultSize(1);
// cdvd.Result[0] = 0; // cdvd.Result[0] = 0;
// break; // break;
case 0x02: // CdReadSubQ (0:11) case 0x02: // CdReadSubQ (0:11)
SetResultSize(11); SetResultSize(11);
@ -1578,8 +1583,8 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
} }
break; break;
// case 0x0C: // sceCdSetHDMode (1:1) // case 0x0C: // sceCdSetHDMode (1:1)
// break; // break;
case 0x0F: // sceCdPowerOff (0:1)- Call74 from Xcdvdman case 0x0F: // sceCdPowerOff (0:1)- Call74 from Xcdvdman
@ -1624,8 +1629,8 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvdWriteModelNumber(&cdvd.Param[1], cdvd.Param[0]); cdvdWriteModelNumber(&cdvd.Param[1], cdvd.Param[0]);
break; break;
// case 0x19: // sceCdForbidRead (0:1) - from xcdvdman // case 0x19: // sceCdForbidRead (0:1) - from xcdvdman
// break; // break;
case 0x1A: // sceCdBootCertify (4:1)//(4:16 in psx?) case 0x1A: // sceCdBootCertify (4:1)//(4:16 in psx?)
SetResultSize(1);//on input there are 4 bytes: 1;?10;J;C for 18000; 1;60;E;C for 39002 from ROMVER SetResultSize(1);//on input there are 4 bytes: 1;?10;J;C for 18000; 1;60;E;C for 39002 from ROMVER
@ -1642,8 +1647,8 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvd.Result[0] = 0; cdvd.Result[0] = 0;
break; break;
// case 0x1D: // cdvdman_call116 (0:5) - In V10 Bios // case 0x1D: // cdvdman_call116 (0:5) - In V10 Bios
// break; // break;
case 0x1E: // sceRemote2Read (0:5) - // 00 14 AA BB CC -> remote key code case 0x1E: // sceRemote2Read (0:5) - // 00 14 AA BB CC -> remote key code
SetResultSize(5); SetResultSize(5);
@ -1654,8 +1659,8 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvd.Result[4] = 0x00; cdvd.Result[4] = 0x00;
break; break;
// case 0x1F: // sceRemote2_7 (2:1) - cdvdman_call117 // case 0x1F: // sceRemote2_7 (2:1) - cdvdman_call117
// break; // break;
case 0x20: // sceRemote2_6 (0:3) // 00 01 00 case 0x20: // sceRemote2_6 (0:3) // 00 01 00
SetResultSize(3); SetResultSize(3);
@ -1664,8 +1669,8 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvd.Result[2] = 0x00; cdvd.Result[2] = 0x00;
break; break;
// case 0x21: // sceCdWriteWakeUpTime (8:1) // case 0x21: // sceCdWriteWakeUpTime (8:1)
// break; // break;
case 0x22: // sceCdReadWakeUpTime (0:10) case 0x22: // sceCdReadWakeUpTime (0:10)
SetResultSize(10); SetResultSize(10);
@ -1687,37 +1692,37 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvd.Result[0] = 0; cdvd.Result[0] = 0;
break; break;
// case 0x25: // cdvdman_call120 (1:1) - In V10 Bios // case 0x25: // cdvdman_call120 (1:1) - In V10 Bios
// break; // break;
// case 0x26: // cdvdman_call128 (0,3) - In V10 Bios // case 0x26: // cdvdman_call128 (0,3) - In V10 Bios
// break; // break;
// case 0x27: // cdvdman_call148 (0:13) - In V10 Bios // case 0x27: // cdvdman_call148 (0:13) - In V10 Bios
// break; // break;
// case 0x28: // cdvdman_call150 (1:1) - In V10 Bios // case 0x28: // cdvdman_call150 (1:1) - In V10 Bios
// break; // break;
case 0x29: //sceCdNoticeGameStart (1:1) case 0x29: //sceCdNoticeGameStart (1:1)
SetResultSize(1); SetResultSize(1);
cdvd.Result[0] = 0; cdvd.Result[0] = 0;
break; break;
// case 0x2C: //sceCdXBSPowerCtl (2:2) // case 0x2C: //sceCdXBSPowerCtl (2:2)
// break; // break;
// case 0x2D: //sceCdXLEDCtl (2:2) // case 0x2D: //sceCdXLEDCtl (2:2)
// break; // break;
// case 0x2E: //sceCdBuzzerCtl (0:1) // case 0x2E: //sceCdBuzzerCtl (0:1)
// break; // break;
// case 0x2F: //cdvdman_call167 (16:1) // case 0x2F: //cdvdman_call167 (16:1)
// break; // break;
// case 0x30: //cdvdman_call169 (1:9) // case 0x30: //cdvdman_call169 (1:9)
// break; // break;
case 0x31: //sceCdSetMediumRemoval (1:1) case 0x31: //sceCdSetMediumRemoval (1:1)
SetResultSize(1); SetResultSize(1);
@ -1730,8 +1735,8 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
//cdvd.Result[0] = 0; // fixme: I'm pretty sure that the same variable shouldn't be set twice here. Perhaps cdvd.Result[1]? //cdvd.Result[0] = 0; // fixme: I'm pretty sure that the same variable shouldn't be set twice here. Perhaps cdvd.Result[1]?
break; break;
// case 0x33: //sceCdXDVRPReset (1:1) // case 0x33: //sceCdXDVRPReset (1:1)
// break; // break;
case 0x36: //cdvdman_call189 [__sceCdReadRegionParams - made up name] (0:15) i think it is 16, not 15 case 0x36: //cdvdman_call189 [__sceCdReadRegionParams - made up name] (0:15) i think it is 16, not 15
SetResultSize(15); SetResultSize(15);
@ -1742,14 +1747,14 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
cdvd.Result[1] = 1 << cdvd.Result[1]; //encryption zone; see offset 0x1C in encrypted headers cdvd.Result[1] = 1 << cdvd.Result[1]; //encryption zone; see offset 0x1C in encrypted headers
////////////////////////////////////////// //////////////////////////////////////////
cdvd.Result[2] = 0; //?? cdvd.Result[2] = 0; //??
// cdvd.Result[3] == ROMVER[4] == *0xBFC7FF04 // cdvd.Result[3] == ROMVER[4] == *0xBFC7FF04
// cdvd.Result[4] == OSDVER[4] == CAP Jjpn, Aeng, Eeng, Heng, Reng, Csch, Kkor? // cdvd.Result[4] == OSDVER[4] == CAP Jjpn, Aeng, Eeng, Heng, Reng, Csch, Kkor?
// cdvd.Result[5] == OSDVER[5] == small // cdvd.Result[5] == OSDVER[5] == small
// cdvd.Result[6] == OSDVER[6] == small // cdvd.Result[6] == OSDVER[6] == small
// cdvd.Result[7] == OSDVER[7] == small // cdvd.Result[7] == OSDVER[7] == small
// cdvd.Result[8] == VERSTR[0x22] == *0xBFC7FF52 // cdvd.Result[8] == VERSTR[0x22] == *0xBFC7FF52
// cdvd.Result[9] == DVDID J U O E A R C M // cdvd.Result[9] == DVDID J U O E A R C M
// cdvd.Result[10]== 0; //?? // cdvd.Result[10]== 0; //??
cdvd.Result[11] = 0; //?? cdvd.Result[11] = 0; //??
cdvd.Result[12] = 0; //?? cdvd.Result[12] = 0; //??
////////////////////////////////////////// //////////////////////////////////////////
@ -2031,6 +2036,12 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
//Console.WriteLn("SCMD - 0x%x\n", rt); //Console.WriteLn("SCMD - 0x%x\n", rt);
cdvd.ParamP = 0; cdvd.ParamP = 0;
cdvd.ParamC = 0; cdvd.ParamC = 0;
} catch (Exception::CannotCreateStream& ex) {
Cpu->ThrowException(Exception::RuntimeError()
.SetDiagMsg(L"Failed to read/write NMV/MEC file.")
.SetUserMsg(pxE( L"Failed to read/write NMV/MEC file. Check your bios setup/permission settings"))
);
}
} }
static __fi void cdvdWrite17(u8 rt) { // SDATAIN static __fi void cdvdWrite17(u8 rt) { // SDATAIN