daybreak: support resetting to factory settings

(cherry picked from commit 1c0e196eae91cfd85f63064c36cc288a0ea0363f)
This commit is contained in:
Adubbz 2020-07-11 13:37:34 +10:00
parent 09ae33e59e
commit de4a28b713
3 changed files with 99 additions and 4 deletions

View file

@ -42,6 +42,10 @@ extern "C" {
fatalThrow(rc);
}
if (R_FAILED(rc = nsInitialize())) {
fatalThrow(rc);
}
if (R_FAILED(rc = hiddbgInitialize())) {
fatalThrow(rc);
}
@ -50,6 +54,7 @@ extern "C" {
void userAppExit(void) {
hiddbgExit();
nsExit();
splExit();
plExit();
spsmExit();

View file

@ -60,6 +60,8 @@ namespace dbk {
/* Update install state. */
char g_update_path[FS_MAX_PATH];
bool g_reset_to_factory = false;
bool g_exfat_supported = false;
bool g_use_exfat = false;
constexpr u32 MaxTapMovement = 20;
@ -864,13 +866,13 @@ namespace dbk {
break;
}
if (m_update_info.exfat_supported) {
ChangeMenu(std::make_shared<ChooseExfatMenu>(g_current_menu));
} else {
/* Check if exfat is supported. */
g_exfat_supported = m_update_info.exfat_supported;
if (!g_exfat_supported) {
g_use_exfat = false;
ChangeMenu(std::make_shared<WarningMenu>(g_current_menu, std::make_shared<InstallUpdateMenu>(g_current_menu), "Ready to begin update installation", "Are you sure you want to proceed?"));
}
ChangeMenu(std::make_shared<ChooseResetMenu>(g_current_menu));
return;
}
}
@ -890,6 +892,60 @@ namespace dbk {
m_has_drawn = true;
}
ChooseResetMenu::ChooseResetMenu(std::shared_ptr<Menu> prev_menu) : Menu(prev_menu) {
const float x = g_screen_width / 2.0f - WindowWidth / 2.0f;
const float y = g_screen_height / 2.0f - WindowHeight / 2.0f;
const float button_width = (WindowWidth - HorizontalInset * 2.0f) / 2.0f - ButtonHorizontalGap;
/* Add buttons. */
this->AddButton(ResetToFactorySettingsButtonId, "Reset to factory settings", x + HorizontalInset, y + TitleGap, button_width, ButtonHeight);
this->AddButton(PreserveSettingsButtonId, "Preserve settings", x + HorizontalInset + button_width + ButtonHorizontalGap, y + TitleGap, button_width, ButtonHeight);
this->SetButtonSelected(PreserveSettingsButtonId, true);
}
void ChooseResetMenu::Update(u64 ns) {
u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO);
/* Go back if B is pressed. */
if (k_down & KEY_B) {
ReturnToPreviousMenu();
return;
}
/* Take action if a button has been activated. */
if (const Button *activated_button = this->GetActivatedButton(); activated_button != nullptr) {
switch (activated_button->id) {
case ResetToFactorySettingsButtonId:
g_reset_to_factory = true;
break;
case PreserveSettingsButtonId:
g_reset_to_factory = false;
break;
}
if (g_exfat_supported) {
ChangeMenu(std::make_shared<ChooseExfatMenu>(g_current_menu));
} else {
ChangeMenu(std::make_shared<WarningMenu>(g_current_menu, std::make_shared<InstallUpdateMenu>(g_current_menu), "Ready to begin update installation", "Are you sure you want to proceed?"));
}
}
this->UpdateButtons();
/* Fallback on selecting the exfat button. */
if (const Button *selected_button = this->GetSelectedButton(); k_down && selected_button == nullptr) {
this->SetButtonSelected(PreserveSettingsButtonId, true);
}
}
void ChooseResetMenu::Draw(NVGcontext *vg, u64 ns) {
const float x = g_screen_width / 2.0f - WindowWidth / 2.0f;
const float y = g_screen_height / 2.0f - WindowHeight / 2.0f;
DrawWindow(vg, "Select settings mode", x, y, WindowWidth, WindowHeight);
this->DrawButtons(vg, ns);
}
ChooseExfatMenu::ChooseExfatMenu(std::shared_ptr<Menu> prev_menu) : Menu(prev_menu) {
const float x = g_screen_width / 2.0f - WindowWidth / 2.0f;
const float y = g_screen_height / 2.0f - WindowHeight / 2.0f;
@ -1045,6 +1101,25 @@ namespace dbk {
} else {
/* Log success. */
this->LogText("Update applied successfully.\n");
if (g_reset_to_factory) {
if (R_FAILED(rc = nsResetToFactorySettingsForRefurbishment())) {
/* Fallback on ResetToFactorySettings. */
if (rc == MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer)) {
if (R_FAILED(rc = nsResetToFactorySettings())) {
this->LogText("Failed to reset to factory settings.\nResult: 0x%08x\n", rc);
this->MarkForReboot();
return rc;
}
} else {
this->LogText("Failed to reset to factory settings for refurbishment.\nResult: 0x%08x\n", rc);
this->MarkForReboot();
return rc;
}
}
this->LogText("Successfully reset to factory settings.\n", rc);
}
}
this->MarkForReboot();

View file

@ -197,6 +197,21 @@ namespace dbk {
virtual void Draw(NVGcontext *vg, u64 ns) override;
};
class ChooseResetMenu : public Menu {
private:
static constexpr u32 ResetToFactorySettingsButtonId = 0;
static constexpr u32 PreserveSettingsButtonId = 1;
static constexpr float WindowWidth = 600.0f;
static constexpr float WindowHeight = 170.0f;
static constexpr float TitleGap = 90.0f;
public:
ChooseResetMenu(std::shared_ptr<Menu> prev_menu);
virtual void Update(u64 ns) override;
virtual void Draw(NVGcontext *vg, u64 ns) override;
};
class ChooseExfatMenu : public Menu {
private:
static constexpr u32 Fat32ButtonId = 0;