Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue of generating incorrect GPT backup #130

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions NxNandManager/NxStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,9 @@ int NxStorage::dump(NxHandle *outHandle, params_t par, void(*updateProgress)(Pro
return error(ERR_WHILE_COPY);

// NAND resize => Update GPT
unsigned char gpt_header_backup[0x200];
bool gen_gpt_backup = false;
BYTE gpt_header_backup[0x200];
BYTE *gpt_entries_backup = new BYTE[0x4000];
if (par.user_new_size)
{
// Cluster align new size (32 sectors per cluster)
Expand All @@ -1172,18 +1174,31 @@ int NxStorage::dump(NxHandle *outHandle, params_t par, void(*updateProgress)(Pro
hdr->alt_lba = hdr->last_use_lba + 33;

// New CRC32 for header
unsigned char header[92];
memcpy(&header[0], &hdr[0], 92);
memset(&header[16], 0, 4);
hdr->c_crc32 = crc32Hash(header, 92);
hdr->c_crc32 = 0; // clear old crc32 before compute new crc32
hdr->c_crc32 = crc32Hash(hdr, hdr->size);

// Save GPT header
memcpy(gpt_header_backup, &hdr[0], 0x200);
gen_gpt_backup = true;
}
else if (isBackupGptMissing())
{
// Backup GPT header
memcpy(gpt_header_backup, buffer + 0x200, 0x200);
gen_gpt_backup = true;
}

// Generate gpt backup
if (gen_gpt_backup) {
// Copy GPT backup
memcpy(gpt_header_backup, &buffer[0x200], 0x200);
memcpy(gpt_entries_backup, &buffer[0x400], 0x4000);

GptHeader *hdr = (GptHeader *)(buffer + 0x200);
GptHeader *hdr_bak = (GptHeader *)gpt_header_backup;
hdr_bak->my_lba = hdr->alt_lba;
hdr_bak->alt_lba = hdr->my_lba;
hdr_bak->part_ent_lba = hdr_bak->my_lba - 32;

// New CRC32 for header
hdr_bak->c_crc32 = 0; // clear old crc32 before compute new crc32
hdr_bak->c_crc32 = crc32Hash(hdr_bak, hdr_bak->size);
}

// Write UserDataRoot
Expand Down Expand Up @@ -1380,9 +1395,9 @@ int NxStorage::dump(NxHandle *outHandle, params_t par, void(*updateProgress)(Pro
}

// Ovewrite backup GPT after resize or if backup GPT is missing from input
bool save_gpt_backup = false;
if (par.user_new_size)
{
GptHeader *hdr = (GptHeader *)(gpt_header_backup);
outHandle->clearHandle();
if (outHandle->getChunkSize())
{
Expand All @@ -1392,13 +1407,27 @@ int NxStorage::dump(NxHandle *outHandle, params_t par, void(*updateProgress)(Pro
}
else outHandle->createHandle();

u64 alt_off = (u64)(hdr->alt_lba * NX_BLOCKSIZE) + rawnand_off;
auto res = outHandle->setPointer(alt_off);
if(!outHandle->write(gpt_header_backup, &bytesWrite, NX_BLOCKSIZE))
save_gpt_backup = true;
}
else if (isBackupGptMissing()) {
save_gpt_backup = true;
}

// Save gpt backup
if (save_gpt_backup) {
// Write gpt partition entries backup
GptHeader *hdr_bak = (GptHeader *)gpt_header_backup;
u64 write_off = (u64)(hdr_bak->part_ent_lba * NX_BLOCKSIZE) + rawnand_off;
outHandle->setPointer(write_off);
if(!outHandle->write(gpt_entries_backup, &bytesWrite, NX_BLOCKSIZE * 32))
return error(ERR_WHILE_COPY);

// Write gpt header backup
write_off = (u64)(hdr_bak->my_lba * NX_BLOCKSIZE) + rawnand_off;
outHandle->setPointer(write_off);
if(!outHandle->write(gpt_header_backup, &bytesWrite, NX_BLOCKSIZE))
return error(ERR_WHILE_COPY);
}
else if (isBackupGptMissing() && !outHandle->write(gpt_header_backup, &bytesWrite, NX_BLOCKSIZE))
return error(ERR_WHILE_COPY);

if (isDrive())
nxHandle->unlockVolume();
Expand Down
19 changes: 10 additions & 9 deletions NxNandManager/gui/NxNandManager.pro
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,20 @@ HEADERS += \
dump.h \
progress.h \
emunand.h \
../lib/ZipLib/*.h \
../lib/ZipLib/utils/*.h \
../lib/ZipLib/detail/*.h \
../lib/ZipLib/extlibs/bzip2/*.h \
../lib/ZipLib/extlibs/lzma/*.h \
$$files(../lib/ZipLib/*.h, false) \
$$files(../lib/ZipLib/utils/*.h, false) \
$$files(../lib/ZipLib/detail/*.h, false) \
$$files(../lib/ZipLib/extlibs/bzip2/*.h, false) \
$$files(../lib/ZipLib/extlibs/lzma/*.h, false) \
debug.h

CONFIG(STATIC) {
HEADERS -= ../lib/ZipLib/extlibs/zlib/zconf.h
SOURCES += $$files(../lib/ZipLib/extlibs/zlib/*.c, false)
HEADERS += $$files(../lib/ZipLib/extlibs/zlib/*.h)
}
CONFIG(DYNAMIC) {
SOURCES += $$files(../lib/ZipLib/extlibs/zlib/*.c, false)
HEADERS += ../lib/ZipLib/extlibs/zlib/*.h \
HEADERS += $$files(../lib/ZipLib/extlibs/zlib/*.h)
}

FORMS += \
Expand Down Expand Up @@ -158,14 +159,14 @@ RC_FILE = NxNandManager.rc
CONFIG(ARCH32) {
DEFINES += ARCH32
#OPENSSL PATH
OPENSSL_LIB_PATH = $$PWD/../../../../../mingw32
OPENSSL_LIB_PATH = $$PWD/../../../OpenSSL_mingw32
LIBS += -L$$PWD/../virtual_fs/dokan/x86/lib/ -ldokan1

}
CONFIG(ARCH64) {
DEFINES += ARCH64
#OPENSSL PATH
OPENSSL_LIB_PATH = $$PWD/../../../../../mingw64
OPENSSL_LIB_PATH = $$PWD/../../../OpenSSL_mingw64
LIBS += -L$$PWD/../virtual_fs/dokan/x64/lib/ -ldokan1
}

Expand Down
2 changes: 1 addition & 1 deletion NxNandManager/gui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<enum>Qt::NoFocus</enum>
</property>
<property name="windowTitle">
<string> NxNandManager v5.2 by eliboa</string>
<string> NxNandManager v5.2.1 by eliboa</string>
</property>
<property name="windowIcon">
<iconset resource="application.qrc">
Expand Down
8 changes: 7 additions & 1 deletion NxNandManager/lib/fatfs/ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ typedef DWORD TCHAR;
#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
#error Wrong FF_LFN_UNICODE setting
#else /* ANSI/OEM code in SBCS/DBCS */
//typedef char TCHAR;
#ifndef _TCHAR_DEFINED
typedef char TCHAR;
#endif
#ifndef _T
#define _T(x) x
#endif
#ifndef _TEXT
#define _TEXT(x) x
#endif
#endif



Expand Down
2 changes: 1 addition & 1 deletion NxNandManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ int main(int argc, char *argv[])
//std::setlocale(LC_ALL, "en_US.utf8");
std::setlocale(LC_ALL, "");
std::locale::global(std::locale(""));
printf("[ NxNandManager v5.2 by eliboa ]\n\n");
printf("[ NxNandManager v5.2.1 by eliboa ]\n\n");
const char *input = nullptr, *output = nullptr, *partitions = nullptr, *keyset = nullptr, *user_resize = nullptr, *boot0 = nullptr, *boot1 = nullptr, *std_redir_output = nullptr;
wchar_t driveLetter = L'\0';
BOOL dokan_install = false, info = false, gui = false, setAutoRCM = false, autoRCM = false, decrypt = false, encrypt = false;
Expand Down
Loading