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
This commit is contained in:
Edoardo Lolletti 2025-08-18 01:28:30 +02:00
parent f3b3b502bc
commit 773fe2b7a5
3 changed files with 21 additions and 12 deletions

View File

@ -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;
}

View File

@ -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
#endif

View File

@ -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");