From 773fe2b7a53278499846f52360c457b54248d9e8 Mon Sep 17 00:00:00 2001 From: Edoardo Lolletti Date: Mon, 18 Aug 2025 01:28:30 +0200 Subject: [PATCH] Skip the first cluster when checking for valid unlaunch install The first cluster now is properly used by unlaunch to save its configs, so it will never be the same across runs of the installer, to account for this, skip the first 0x4000 bytes when checking for its sha1, so that the user configurable part is ignored --- arm9/src/storage.c | 8 ++++---- arm9/src/storage.h | 13 ++++++++++--- arm9/src/unlaunch.cpp | 12 +++++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/arm9/src/storage.c b/arm9/src/storage.c index fc56487..e6cdcd7 100644 --- a/arm9/src/storage.c +++ b/arm9/src/storage.c @@ -176,9 +176,9 @@ bool writeToFile(FILE* fd, const char* buffer, size_t size) return toWrite == 0; } -bool calculateFileSha1(FILE* f, void* digest) +bool calculateFileSha1Offset(FILE* f, void* digest, size_t offset) { - fseek(f, 0, SEEK_SET); + fseek(f, offset, SEEK_SET); swiSHA1context_t ctx; ctx.sha_block = 0; //this is weird but it has to be done @@ -198,14 +198,14 @@ bool calculateFileSha1(FILE* f, void* digest) return true; } -bool calculateFileSha1Path(const char* path, void* digest) +bool calculateFileSha1PathOffset(const char* path, void* digest, size_t offset) { FILE* targetFile = fopen(path, "rb"); if (!targetFile) { return false; } - bool res = calculateFileSha1(targetFile, digest); + bool res = calculateFileSha1Offset(targetFile, digest, offset); fclose(targetFile); return res; } diff --git a/arm9/src/storage.h b/arm9/src/storage.h index 5766511..c5708a6 100644 --- a/arm9/src/storage.h +++ b/arm9/src/storage.h @@ -16,8 +16,15 @@ unsigned long long getFileSize(FILE* f); unsigned long long getFileSizePath(char const* path); bool toggleFileReadOnly(const char* path, bool readOnly); bool writeToFile(FILE* fd, const char* buffer, size_t size); -bool calculateFileSha1(FILE* f, void* digest); -bool calculateFileSha1Path(const char* path, void* digest); +bool calculateFileSha1Offset(FILE* f, void* digest, size_t offset); +bool calculateFileSha1PathOffset(const char* path, void* digest, size_t offset); +static inline bool calculateFileSha1(FILE* f, void* digest) { + return calculateFileSha1Offset(f, digest, 0); +} + +static inline bool calculateFileSha1Path(const char* path, void* digest) { + return calculateFileSha1PathOffset(path, digest, 0); +} //Directories bool safeCreateDir(const char* path); @@ -31,4 +38,4 @@ u32 getClusterSizeForPartition(const char* path); } #endif -#endif \ No newline at end of file +#endif diff --git a/arm9/src/unlaunch.cpp b/arm9/src/unlaunch.cpp index 79495fb..c23f324 100644 --- a/arm9/src/unlaunch.cpp +++ b/arm9/src/unlaunch.cpp @@ -235,14 +235,16 @@ bool uninstallUnlaunch(const consoleInfo& info, bool removeHNAABackup) static bool writeUnlaunchTmd(const char* path) { - Sha1Digest expectedDigest, actualDigest; - swiSHA1Calc(expectedDigest.data(), unlaunchInstallerBuffer, unlaunchInstallerSize + 520); - if(calculateFileSha1Path(path, actualDigest.data()) && expectedDigest == actualDigest) + static constexpr auto unlaunchShaOffset = 0x4000; + Sha1Digest expectedDigest, actualDigest; + swiSHA1Calc(expectedDigest.data(), unlaunchInstallerBuffer + unlaunchShaOffset, + (unlaunchInstallerSize + 520) - unlaunchShaOffset); + if(calculateFileSha1PathOffset(path, actualDigest.data(), unlaunchShaOffset) && expectedDigest == actualDigest) { // the tmd hasn't changed, no need to do anything return true; } - + FILE* targetTmd = fopen(path, "wb"); if (!targetTmd) { @@ -260,7 +262,7 @@ static bool writeUnlaunchTmd(const char* path) fclose(targetTmd); - if(!calculateFileSha1Path(path, actualDigest.data()) || expectedDigest != actualDigest) + if(!calculateFileSha1PathOffset(path, actualDigest.data(), unlaunchShaOffset) || expectedDigest != actualDigest) { removeIfExists(path); messageBox("\x1B[31mError:\x1B[33m Unlaunch tmd was not properly written\n");