Skip to content

Commit

Permalink
fix romfs hash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dnasdw committed Jan 11, 2017
1 parent e49c3cc commit d882018
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if(MSVC14)
endif()
set(_3DSTOOL_MAJOR 1)
set(_3DSTOOL_MINOR 0)
set(_3DSTOOL_PATCHLEVEL 18)
set(_3DSTOOL_PATCHLEVEL 19)
if(UNIX)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ An all-in-one tool for extracting/creating 3ds roms.
- v1.0.16 @ 2016.11.01 - Support huffman, runlength, yaz0 compression, romfs remap ignore and VS2008SP1
- v1.0.17 @ 2016.11.06 - Fix romfs hash bug
- v1.0.18 @ 2016.12.04 - Compatible with yaz0 with alignment property
- v1.0.19 @ 2017.01.11 - Fix romfs hash bug

## Platforms

Expand Down
59 changes: 31 additions & 28 deletions src/ncch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,51 +1062,54 @@ bool CNcch::createRomFs()
{
if (m_pRomFsFileName != nullptr)
{
bool bEncrypted = !CRomFs::IsRomFsFile(m_pRomFsFileName);
FILE* fp = FFopen(m_pRomFsFileName, "rb");
if (fp == nullptr)
{
clearRomFs();
return false;
}
if (bEncrypted && !m_bNotUpdateRomFsHash)
{
printf("INFO: romfs is encrypted\n");
m_bNotUpdateRomFsHash = true;
}
if (m_bVerbose)
{
printf("load: %s\n", m_pRomFsFileName);
}
FFseek(fp, 0, SEEK_END);
n64 nFileSize = FFtell(fp);
n64 nSuperBlockSize = FAlign(sizeof(SRomFsHeader), CRomFs::s_nSHA256BlockSize);
if (nFileSize < nSuperBlockSize)
{
fclose(fp);
clearRomFs();
printf("ERROR: romfs is too short\n\n");
return false;
}
FFseek(fp, 0, SEEK_SET);
SRomFsHeader romFsHeader;
fread(&romFsHeader, sizeof(romFsHeader), 1, fp);
nSuperBlockSize = FAlign(FAlign(sizeof(SRomFsHeader), CRomFs::s_nSHA256BlockSize) + romFsHeader.Level0Size, m_nMediaUnitSize);
if (nFileSize < nSuperBlockSize)
{
fclose(fp);
clearRomFs();
printf("ERROR: romfs is too short\n\n");
return false;
}
m_NcchHeader.Ncch.RomFsOffset = m_NcchHeader.Ncch.ContentSize;
m_NcchHeader.Ncch.RomFsSize = static_cast<u32>(FAlign(nFileSize, m_nMediaUnitSize) / m_nMediaUnitSize);
if (!m_bNotUpdateRomFsHash)
{
n64 nSuperBlockSize = FAlign(sizeof(SRomFsHeader), CRomFs::s_nSHA256BlockSize);
if (nFileSize < nSuperBlockSize)
{
fclose(fp);
clearRomFs();
printf("ERROR: romfs is too short\n\n");
return false;
}
FFseek(fp, 0, SEEK_SET);
SRomFsHeader romFsHeader;
fread(&romFsHeader, sizeof(romFsHeader), 1, fp);
nSuperBlockSize = FAlign(FAlign(sizeof(SRomFsHeader), CRomFs::s_nSHA256BlockSize) + romFsHeader.Level0Size, m_nMediaUnitSize);
if (nFileSize < nSuperBlockSize)
{
fclose(fp);
clearRomFs();
printf("ERROR: romfs is too short\n\n");
return false;
}
m_NcchHeader.Ncch.RomFsHashRegionSize = static_cast<u32>(nSuperBlockSize / m_nMediaUnitSize);
}
FFseek(fp, 0, SEEK_SET);
u8* pBuffer = new u8[static_cast<size_t>(nSuperBlockSize)];
fread(pBuffer, 1, static_cast<size_t>(nSuperBlockSize), fp);
if (!m_bNotUpdateRomFsHash)
{
FFseek(fp, 0, SEEK_SET);
u8* pBuffer = new u8[static_cast<size_t>(nSuperBlockSize)];
fread(pBuffer, 1, static_cast<size_t>(nSuperBlockSize), fp);
SHA256(pBuffer, static_cast<size_t>(nSuperBlockSize), m_NcchHeader.Ncch.RomFsSuperBlockHash);
delete[] pBuffer;
}
delete[] pBuffer;
m_NcchHeader.Ncch.RomFsOffset = m_NcchHeader.Ncch.ContentSize;
m_NcchHeader.Ncch.RomFsSize = static_cast<u32>(FAlign(nFileSize, m_nMediaUnitSize) / m_nMediaUnitSize);
calculateCounter(kAesCtrTypeRomFs);
if (m_nEncryptMode == kEncryptModeNone || (m_nEncryptMode == kEncryptModeXor && m_pRomFsXorFileName == nullptr && !m_bRomFsAutoKey))
{
Expand Down

1 comment on commit d882018

@dnasdw
Copy link
Owner Author

@dnasdw dnasdw commented on d882018 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #6

Please sign in to comment.