From 6b572ecfa4b7b247c87d02f949a068d3cb0b0b4e Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Thu, 13 Jan 2011 03:46:34 +0000 Subject: [PATCH] Check DSP ROM hashes. Fix the ROMs not actually being write protected. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6839 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/DSPCore/Src/DSPCore.cpp | 44 ++++++++++++++++------ Source/Plugins/Plugin_DSP_LLE/Src/main.cpp | 3 +- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Source/Core/DSPCore/Src/DSPCore.cpp b/Source/Core/DSPCore/Src/DSPCore.cpp index cb0aeebd11..42da6410f9 100644 --- a/Source/Core/DSPCore/Src/DSPCore.cpp +++ b/Source/Core/DSPCore/Src/DSPCore.cpp @@ -24,6 +24,7 @@ ====================================================================*/ #include "Common.h" +#include "Hash.h" #include "Thread.h" #include "DSPCore.h" #include "DSPEmitter.h" @@ -47,27 +48,44 @@ static bool LoadRom(const char *fname, int size_in_words, u16 *rom) const size_t size_in_bytes = size_in_words * sizeof(u16); if (pFile) { - size_t read_bytes = fread(rom, 1, size_in_bytes, pFile); - if (read_bytes != size_in_bytes) - { - PanicAlertT("ROM %s too short : %i/%i", fname, (int)read_bytes, (int)size_in_bytes); - fclose(pFile); - return false; - } + fread(rom, 1, size_in_bytes, pFile); fclose(pFile); // Byteswap the rom. for (int i = 0; i < size_in_words; i++) rom[i] = Common::swap16(rom[i]); + // Always keep ROMs write protected. + WriteProtectMemory(rom, size_in_bytes, false); return true; } - PanicAlertT("Failed to load DSP Rom : %s",fname); - // Always keep ROMs write protected. - WriteProtectMemory(g_dsp.irom, size_in_bytes, false); + + PanicAlertT("Failed to load DSP ROM: %s", fname); return false; } +// Returns false iff the hash fails and the user hits "Yes" +static bool VerifyRoms(const char *irom_filename, const char *coef_filename) +{ + static const u32 hash[] = { 0x66f334fe, 0xf3b93527 }; + static const int size[] = { DSP_IROM_BYTE_SIZE, DSP_COEF_BYTE_SIZE }; + const u16 *data[] = { g_dsp.irom, g_dsp.coef }; + + for (int i = 0; i < 2; i++) + { + if (HashAdler32((u8*)data[i], size[i]) != hash[i]) + { + if (AskYesNoT("%s has an incorrect hash.\n" + "Would you like to stop now to fix the problem?\n" + "If you select \"No\", audio will be garbled.", + (i == 0) ? irom_filename : coef_filename)) + return false; + } + } + + return true; +} + bool DSPCore_Init(const char *irom_filename, const char *coef_filename, bool bUsingJIT) { @@ -80,13 +98,15 @@ bool DSPCore_Init(const char *irom_filename, const char *coef_filename, g_dsp.dram = (u16*)AllocateMemoryPages(DSP_DRAM_BYTE_SIZE); g_dsp.coef = (u16*)AllocateMemoryPages(DSP_COEF_BYTE_SIZE); - // Fill roms with zeros. + // Fill roms with zeros. memset(g_dsp.irom, 0, DSP_IROM_BYTE_SIZE); memset(g_dsp.coef, 0, DSP_COEF_BYTE_SIZE); - // Try to load real ROM contents. Failing this, only homebrew will work correctly with the DSP. + // Try to load real ROM contents. LoadRom(irom_filename, DSP_IROM_SIZE, g_dsp.irom); LoadRom(coef_filename, DSP_COEF_SIZE, g_dsp.coef); + if (!VerifyRoms(irom_filename, coef_filename)) + return false; memset(&g_dsp.r,0,sizeof(g_dsp.r)); diff --git a/Source/Plugins/Plugin_DSP_LLE/Src/main.cpp b/Source/Plugins/Plugin_DSP_LLE/Src/main.cpp index 3f8f200d7e..92d7055e80 100644 --- a/Source/Plugins/Plugin_DSP_LLE/Src/main.cpp +++ b/Source/Plugins/Plugin_DSP_LLE/Src/main.cpp @@ -235,8 +235,9 @@ void Initialize(void *init) if (!bCanWork) { - PanicAlert("DSPLLE: Failed to initialize plugin, exiting"); DSPCore_Shutdown(); + // No way to shutdown Core from here? Hardcore shutdown! + exit(EXIT_FAILURE); return; }