mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-12-05 20:24:01 +01:00
add NPUMDIMG support
This commit is contained in:
1 parent
34da6f983a
commit
c9af16e332
8 files changed
+500
-59
No files matched your search
@@ -15,15 +15,18 @@
|
||||
// Official git repository and contact information can be found at
|
||||
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "zlib.h"
|
||||
};
|
||||
|
||||
#include "BlockDevices.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "zlib.h"
|
||||
#include "ext/libkirk/amctrl.h"
|
||||
|
||||
};
|
||||
|
||||
BlockDevice *constructBlockDevice(const char *filename) {
|
||||
// Check for CISO
|
||||
FILE *f = fopen(filename, "rb");
|
||||
@@ -34,6 +37,8 @@ BlockDevice *constructBlockDevice(const char *filename) {
|
||||
fclose(f);
|
||||
if (!memcmp(buffer, "CISO", 4) && size == 4)
|
||||
return new CISOFileBlockDevice(filename);
|
||||
else if (!memcmp(buffer, "\x00PBP", 4) && size == 4)
|
||||
return new NPDRMDemoBlockDevice(filename);
|
||||
else
|
||||
return new FileBlockDevice(filename);
|
||||
}
|
||||
@@ -201,19 +206,143 @@ bool CISOFileBlockDevice::ReadBlock(int blockNumber, u8 *outPtr)
|
||||
}
|
||||
|
||||
|
||||
NPDRMDemoBlockDevice::NPDRMDemoBlockDevice(std::string _filename)
|
||||
: filename_(_filename), pbpReader_(_filename.c_str()) {
|
||||
std::string paramSfo;
|
||||
pbpReader_.GetSubFileAsString(PBP_PARAM_SFO, ¶mSfo);
|
||||
}
|
||||
NPDRMDemoBlockDevice::NPDRMDemoBlockDevice(std::string _filename)
|
||||
: filename_(_filename)
|
||||
{
|
||||
MAC_KEY mkey;
|
||||
CIPHER_KEY ckey;
|
||||
u8 np_header[256];
|
||||
u32 tableOffset, tableSize;
|
||||
u32 lbaStart, lbaEnd;
|
||||
|
||||
NPDRMDemoBlockDevice::~NPDRMDemoBlockDevice() {
|
||||
f = fopen(_filename.c_str(), "rb");
|
||||
|
||||
fseek(f, 0x24, SEEK_SET);
|
||||
fread(&psarOffset, 1, 4, f);
|
||||
fseek(f, psarOffset, SEEK_SET);
|
||||
size_t readSize = fread(&np_header, 1, 256, f);
|
||||
if(readSize!=256){
|
||||
ERROR_LOG(LOADER, "Invalid NPUMDIMG header!");
|
||||
}
|
||||
|
||||
// getkey
|
||||
sceDrmBBMacInit(&mkey, 3);
|
||||
sceDrmBBMacUpdate(&mkey, np_header, 0xc0);
|
||||
bbmac_getkey(&mkey, np_header+0xc0, vkey);
|
||||
|
||||
// decrypt NP header
|
||||
memcpy(hkey, np_header+0xa0, 0x10);
|
||||
sceDrmBBCipherInit(&ckey, 1, 2, hkey, vkey, 0);
|
||||
sceDrmBBCipherUpdate(&ckey, np_header+0x40, 0x60);
|
||||
sceDrmBBCipherFinal(&ckey);
|
||||
|
||||
lbaStart = *(u32*)(np_header+0x54); // LBA start
|
||||
lbaEnd = *(u32*)(np_header+0x64); // LBA end
|
||||
lbaSize = (lbaEnd-lbaStart+1); // LBA size of ISO
|
||||
blockLBAs = *(u32*)(np_header+0x0c); // block size in LBA
|
||||
blockSize = blockLBAs*2048;
|
||||
numBlocks = (lbaSize+blockLBAs-1)/blockLBAs; // total blocks;
|
||||
|
||||
blockBuf = new u8[blockSize];
|
||||
tempBuf = new u8[blockSize];
|
||||
|
||||
tableOffset = *(u32*)(np_header+0x6c); // table offset
|
||||
fseek(f, psarOffset+tableOffset, SEEK_SET);
|
||||
|
||||
tableSize = numBlocks*32;
|
||||
table = new table_info[numBlocks];
|
||||
|
||||
readSize = fread(table, 1, tableSize, f);
|
||||
if(readSize!=tableSize){
|
||||
ERROR_LOG(LOADER, "Invalid NPUMDIMG table!");
|
||||
}
|
||||
|
||||
u32 *p = (u32*)table;
|
||||
u32 i, k0, k1, k2, k3;
|
||||
for(i=0; i<numBlocks; i++){
|
||||
k0 = p[0]^p[1];
|
||||
k1 = p[1]^p[2];
|
||||
k2 = p[0]^p[3];
|
||||
k3 = p[2]^p[3];
|
||||
p[4] ^= k3;
|
||||
p[5] ^= k1;
|
||||
p[6] ^= k2;
|
||||
p[7] ^= k0;
|
||||
p += 8;
|
||||
}
|
||||
|
||||
currentBlock = -1;
|
||||
|
||||
}
|
||||
|
||||
bool NPDRMDemoBlockDevice::ReadBlock(int blockNumber, u8 *outPtr) {
|
||||
// TODO: Fill in decryption code here. Use pbpReader to read the file - might need to
|
||||
// extend its functionality to do it efficiently.
|
||||
NPDRMDemoBlockDevice::~NPDRMDemoBlockDevice()
|
||||
{
|
||||
fclose(f);
|
||||
delete [] table;
|
||||
delete [] tempBuf;
|
||||
delete [] blockBuf;
|
||||
}
|
||||
|
||||
return false;
|
||||
int lzrc_decompress(void *out, int out_len, void *in, int in_len);
|
||||
|
||||
bool NPDRMDemoBlockDevice::ReadBlock(int blockNumber, u8 *outPtr)
|
||||
{
|
||||
CIPHER_KEY ckey;
|
||||
int block, lba, lzsize;
|
||||
size_t readSize;
|
||||
u8 *readBuf;
|
||||
|
||||
lba = blockNumber-currentBlock;
|
||||
if(lba>=0 && lba<blockLBAs){
|
||||
memcpy(outPtr, blockBuf+lba*2048, 2048);
|
||||
return true;
|
||||
}
|
||||
|
||||
block = blockNumber/blockLBAs;
|
||||
lba = blockNumber%blockLBAs;
|
||||
currentBlock = block*blockLBAs;
|
||||
|
||||
if(table[block].unk_1c!=0){
|
||||
if(block==(numBlocks-1))
|
||||
return true; // demos make by fake_np
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
fseek(f, psarOffset+table[block].offset, SEEK_SET);
|
||||
|
||||
if(table[block].size<blockSize)
|
||||
readBuf = tempBuf;
|
||||
else
|
||||
readBuf = blockBuf;
|
||||
|
||||
readSize = fread(readBuf, 1, table[block].size, f);
|
||||
if(readSize!=table[block].size){
|
||||
if(block==(numBlocks-1))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
if((table[block].flag&1)==0){
|
||||
// skip mac check
|
||||
}
|
||||
|
||||
if((table[block].flag&4)==0){
|
||||
sceDrmBBCipherInit(&ckey, 1, 2, hkey, vkey, table[block].offset>>4);
|
||||
sceDrmBBCipherUpdate(&ckey, readBuf, table[block].size);
|
||||
sceDrmBBCipherFinal(&ckey);
|
||||
}
|
||||
|
||||
if(table[block].size<blockSize){
|
||||
lzsize = lzrc_decompress(blockBuf, 0x00100000, readBuf, table[block].size);
|
||||
if(lzsize!=blockSize){
|
||||
ERROR_LOG(LOADER, "LZRC decompress error! lzsize=%d\n", lzsize);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(outPtr, blockBuf+lba*2048, 2048);
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user