diff options
author | Jesse Talavera-Greenberg <jesse@jesse.tg> | 2023-10-24 17:28:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-24 23:28:14 +0200 |
commit | 21590b070976f8fe343fdc44d4fee599cbda94f4 (patch) | |
tree | 89296cfb050314748f090b9d3e6e3f66109b9735 /src/frontend/qt_sdl | |
parent | 8c4e5af737ab0dddb91bd593f51d815d1a04a7b1 (diff) |
Miscellaneous DSi NAND fixes (#1852)
* Replace some standard I/O calls with Platform equivalents
- I missed a spot when I submitted that PR a few months ago
* Include <memory> in DSi_NAND.h
- Because it uses unique_ptr
* Split DSi_NAND::ReadHardwareInfo into ReadSerialData and ReadHardwareInfoN
* Add a RegionMask enum
* Move DSi NAND patching to the frontend
* Add DSiSupportedLanguageMask
- Not currently used by the frontend, but I use it in melonDS DS
* Remove some Platform::ConfigEntry values
- The core no longer needs to know about them
- The corresponding Config values are unchanged
* Mark NANDMount's destructor as noexcept
Diffstat (limited to 'src/frontend/qt_sdl')
-rw-r--r-- | src/frontend/qt_sdl/Platform.cpp | 8 | ||||
-rw-r--r-- | src/frontend/qt_sdl/ROMManager.cpp | 88 |
2 files changed, 75 insertions, 21 deletions
diff --git a/src/frontend/qt_sdl/Platform.cpp b/src/frontend/qt_sdl/Platform.cpp index 2fa0b18..0cb9574 100644 --- a/src/frontend/qt_sdl/Platform.cpp +++ b/src/frontend/qt_sdl/Platform.cpp @@ -207,11 +207,6 @@ int GetConfigInt(ConfigEntry entry) case DSiSD_ImageSize: return imgsizes[Config::DSiSDSize]; - case Firm_Language: return Config::FirmwareLanguage; - case Firm_BirthdayMonth: return Config::FirmwareBirthdayMonth; - case Firm_BirthdayDay: return Config::FirmwareBirthdayDay; - case Firm_Color: return Config::FirmwareFavouriteColour; - case AudioBitDepth: return Config::AudioBitDepth; #ifdef GDBSTUB_ENABLED @@ -244,7 +239,6 @@ bool GetConfigBool(ConfigEntry entry) case DSiSD_ReadOnly: return Config::DSiSDReadOnly != 0; case DSiSD_FolderSync: return Config::DSiSDFolderSync != 0; - case Firm_OverrideSettings: return Config::FirmwareOverrideSettings != 0; case DSi_FullBIOSBoot: return Config::DSiFullBIOSBoot != 0; #ifdef GDBSTUB_ENABLED @@ -267,8 +261,6 @@ std::string GetConfigString(ConfigEntry entry) case DSiSD_ImagePath: return Config::DSiSDPath; case DSiSD_FolderPath: return Config::DSiSDFolderPath; - case Firm_Username: return Config::FirmwareUsername; - case Firm_Message: return Config::FirmwareMessage; case WifiSettingsPath: return Config::WifiSettingsPath; } diff --git a/src/frontend/qt_sdl/ROMManager.cpp b/src/frontend/qt_sdl/ROMManager.cpp index 206332b..cc65dfd 100644 --- a/src/frontend/qt_sdl/ROMManager.cpp +++ b/src/frontend/qt_sdl/ROMManager.cpp @@ -47,6 +47,7 @@ using std::pair; using std::string; using std::tie; using std::unique_ptr; +using std::wstring_convert; using namespace Platform; namespace ROMManager @@ -875,7 +876,7 @@ void LoadUserSettingsFromConfig(SPI_Firmware::Firmware& firmware) UserData& currentData = firmware.EffectiveUserData(); // setting up username - std::string orig_username = Platform::GetConfigString(Platform::Firm_Username); + std::string orig_username = Config::FirmwareUsername; if (!orig_username.empty()) { // If the frontend defines a username, take it. If not, leave the existing one. std::u16string username = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(orig_username); @@ -884,7 +885,7 @@ void LoadUserSettingsFromConfig(SPI_Firmware::Firmware& firmware) memcpy(currentData.Nickname, username.data(), usernameLength * sizeof(char16_t)); } - auto language = static_cast<Language>(Platform::GetConfigInt(Platform::Firm_Language)); + auto language = static_cast<Language>(Config::FirmwareLanguage); if (language != Language::Reserved) { // If the frontend specifies a language (rather than using the existing value)... currentData.Settings &= ~Language::Reserved; // ..clear the existing language... @@ -892,26 +893,26 @@ void LoadUserSettingsFromConfig(SPI_Firmware::Firmware& firmware) } // setting up color - u8 favoritecolor = Platform::GetConfigInt(Platform::Firm_Color); + u8 favoritecolor = Config::FirmwareFavouriteColour; if (favoritecolor != 0xFF) { currentData.FavoriteColor = favoritecolor; } - u8 birthmonth = Platform::GetConfigInt(Platform::Firm_BirthdayMonth); + u8 birthmonth = Config::FirmwareBirthdayMonth; if (birthmonth != 0) { // If the frontend specifies a birth month (rather than using the existing value)... currentData.BirthdayMonth = birthmonth; } - u8 birthday = Platform::GetConfigInt(Platform::Firm_BirthdayDay); + u8 birthday = Config::FirmwareBirthdayDay; if (birthday != 0) { // If the frontend specifies a birthday (rather than using the existing value)... currentData.BirthdayDay = birthday; } // setup message - std::string orig_message = Platform::GetConfigString(Platform::Firm_Message); + std::string orig_message = Config::FirmwareMessage; if (!orig_message.empty()) { std::u16string message = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(orig_message); @@ -966,7 +967,7 @@ static Platform::FileHandle* OpenNANDFile() noexcept FileHandle* orig = Platform::OpenLocalFile(nandpath, FileMode::Read); if (!orig) { - Log(LogLevel::Error, "Failed to open DSi NAND\n"); + Log(LogLevel::Error, "Failed to open DSi NAND from %s\n", nandpath.c_str()); return nullptr; } @@ -984,16 +985,77 @@ bool InstallNAND(const u8* es_keyY) if (!nandfile) return false; - if (auto nand = std::make_unique<DSi_NAND::NANDImage>(nandfile, es_keyY); *nand) + DSi_NAND::NANDImage nandImage(nandfile, es_keyY); + if (!nandImage) { - DSi::NANDImage = std::move(nand); - return true; + Log(LogLevel::Error, "Failed to parse DSi NAND\n"); + return false; } - else + + // scoped so that mount isn't alive when we move the NAND image to DSi::NANDImage { - DSi::NANDImage = nullptr; - return false; + auto mount = DSi_NAND::NANDMount(nandImage); + if (!mount) + { + Log(LogLevel::Error, "Failed to mount DSi NAND\n"); + return false; + } + + DSi_NAND::DSiFirmwareSystemSettings settings {}; + if (!mount.ReadUserData(settings)) + { + Log(LogLevel::Error, "Failed to read DSi NAND user data\n"); + return false; + } + + // override user settings, if needed + if (Config::FirmwareOverrideSettings) + { + // we store relevant strings as UTF-8, so we need to convert them to UTF-16 + auto converter = wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}; + + // setting up username + std::u16string username = converter.from_bytes(Config::FirmwareUsername); + size_t usernameLength = std::min(username.length(), (size_t) 10); + memset(&settings.Nickname, 0, sizeof(settings.Nickname)); + memcpy(&settings.Nickname, username.data(), usernameLength * sizeof(char16_t)); + + // setting language + settings.Language = static_cast<SPI_Firmware::Language>(Config::FirmwareLanguage); + + // setting up color + settings.FavoriteColor = Config::FirmwareFavouriteColour; + + // setting up birthday + settings.BirthdayMonth = Config::FirmwareBirthdayMonth; + settings.BirthdayDay = Config::FirmwareBirthdayDay; + + // setup message + std::u16string message = converter.from_bytes(Config::FirmwareMessage); + size_t messageLength = std::min(message.length(), (size_t) 26); + memset(&settings.Message, 0, sizeof(settings.Message)); + memcpy(&settings.Message, message.data(), messageLength * sizeof(char16_t)); + + // TODO: make other items configurable? + } + + // fix touchscreen coords + settings.TouchCalibrationADC1 = {0, 0}; + settings.TouchCalibrationPixel1 = {0, 0}; + settings.TouchCalibrationADC2 = {255 << 4, 191 << 4}; + settings.TouchCalibrationPixel2 = {255, 191}; + + settings.UpdateHash(); + + if (!mount.ApplyUserData(settings)) + { + Log(LogLevel::Error, "Failed to write patched DSi NAND user data\n"); + return false; + } } + + DSi::NANDImage = std::make_unique<DSi_NAND::NANDImage>(std::move(nandImage)); + return true; } bool InstallFirmware() |