diff --git a/include/arm11/main.h b/include/arm11/main.h deleted file mode 100644 index 80419ea..0000000 --- a/include/arm11/main.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -/* - * This file is part of fastboot 3DS - * Copyright (C) 2017 derrek, profi200 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -// 0 if no FIRM loaded / 1...3 FIRM loaded from slot / > 3 FIRM loaded from elsewhere -extern volatile bool g_startFirmLaunch; diff --git a/include/arm11/menu/menu.h b/include/arm11/menu/menu.h index cc5c303..e217c25 100644 --- a/include/arm11/menu/menu.h +++ b/include/arm11/menu/menu.h @@ -22,7 +22,16 @@ #include "arm11/menu/bootslot.h" #include "arm11/console.h" -#define MENU_LEAVE_PARAM ((u32) -1) + +enum +{ + MENU_OK, + MENU_FAIL, + MENU_RET_CONTINUE, + MENU_RET_FIRMLOADED, + MENU_RET_POWEROFF, + MENU_RET_REBOOT +}; #define MENU_MAX_ENTRIES 8 #define MENU_MAX_DEPTH 4 diff --git a/include/arm11/menu/menu_fb3ds.h b/include/arm11/menu/menu_fb3ds.h index 3f63858..ef9f042 100644 --- a/include/arm11/menu/menu_fb3ds.h +++ b/include/arm11/menu/menu_fb3ds.h @@ -84,7 +84,7 @@ { "Main Menu", 6, NULL, 0, { - { "Continue boot", DESC_CONTINUE, NULL, MENU_LEAVE_PARAM }, + { "Continue boot", DESC_CONTINUE, &menuReturn, MENU_RET_CONTINUE }, { "Boot menu...", DESC_BOOT_MENU, NULL, 1 }, { "Boot setup...", DESC_BOOT_SETUP, NULL, 2 }, { "Boot from file...", DESC_BOOT_FILE, &menuLaunchFirm, 0xFF }, diff --git a/include/arm11/menu/menu_func.h b/include/arm11/menu/menu_func.h index 2211feb..9478398 100644 --- a/include/arm11/menu/menu_func.h +++ b/include/arm11/menu/menu_func.h @@ -39,6 +39,7 @@ u32 menuPresetSlotConfig9(void); u32 menuPresetBootMode(void); +u32 menuReturn(PrintConsole* term_con, PrintConsole* menu_con, u32 param); u32 menuSetBootMode(PrintConsole* term_con, PrintConsole* menu_con, u32 param); u32 menuSetupBootSlot(PrintConsole* term_con, PrintConsole* menu_con, u32 param); u32 menuSetupBootKeys(PrintConsole* term_con, PrintConsole* menu_con, u32 param); diff --git a/source/arm11/main.c b/source/arm11/main.c index 718214d..65f00ee 100644 --- a/source/arm11/main.c +++ b/source/arm11/main.c @@ -35,14 +35,13 @@ #include "fsutils.h" -volatile bool g_startFirmLaunch = false; - - int main(void) { + bool startFirmLaunch = false; bool show_menu = false; bool gfx_initialized = false; + u32 menu_ret = MENU_OK; s32 firm_err = 0; // local result of loadVerifyFirm() char* err_string = NULL; @@ -88,7 +87,7 @@ keysToString(kHeld & 0xfff, combo_str); err_ptr += ee_sprintf(err_ptr, "Keys held on boot: %s\n", combo_str); - for(u32 i = 0; (i < N_BOOTSLOTS) && !g_startFirmLaunch; i++) + for(u32 i = 0; (i < N_BOOTSLOTS) && !startFirmLaunch; i++) { if(!configDataExist(KBootOption1 + i) || !configDataExist(KBootOption1Buttons + i)) @@ -103,11 +102,11 @@ firm_err = loadVerifyFirm(path, false); if (firm_err >= 0) { - g_startFirmLaunch = true; + startFirmLaunch = true; storeBootslot(i + 1); } - err_ptr += ee_sprintf(err_ptr, "Load slot #%lu %s.\n", (i+1), g_startFirmLaunch ? "success" : "failed"); + err_ptr += ee_sprintf(err_ptr, "Load slot #%lu %s.\n", (i+1), startFirmLaunch ? "success" : "failed"); break; } } @@ -129,7 +128,7 @@ if (hidKeysDown() & KEY_HOME) { show_menu = true; - g_startFirmLaunch = false; + startFirmLaunch = false; } } } @@ -137,7 +136,7 @@ // main loop PrintConsole term_con; - while(!g_startFirmLaunch) + while(!startFirmLaunch) { // init screens / console (if we'll need them below) if(show_menu || err_string) @@ -173,7 +172,7 @@ consoleInit(SCREEN_SUB, &menu_con, false); // run menu - menuProcess(&menu_con, &term_con, menu_fb3ds); + menu_ret = menuProcess(&menu_con, &term_con, menu_fb3ds); // clear consoles and screen consoleSelect(&menu_con); @@ -188,10 +187,17 @@ // check POWER button if(hidGetPowerButton(false)) break; + + // check menu return value + if (menu_ret == MENU_RET_FIRMLOADED) + startFirmLaunch = true; + else if ((menu_ret == MENU_RET_POWEROFF) || (menu_ret == MENU_RET_REBOOT)) + break; + } // search for a bootable firmware (all slots) - if (!g_startFirmLaunch) + if (!startFirmLaunch) { err_string = (char*) malloc(512); char* err_ptr = err_string; @@ -201,7 +207,7 @@ if (!nextBootSlot) { err_ptr += ee_sprintf(err_ptr, "Continuing bootloader...\n"); - for (u32 i = 0; (i < N_BOOTSLOTS) && !g_startFirmLaunch; i++) + for (u32 i = 0; (i < N_BOOTSLOTS) && !startFirmLaunch; i++) { // FIRM not set for slot or slot not set to autoboot if (!configDataExist(KBootOption1 + i) || configDataExist(KBootOption1Buttons + i)) @@ -213,11 +219,11 @@ firm_err = loadVerifyFirm(path, false); if (firm_err >= 0) { - g_startFirmLaunch = true; + startFirmLaunch = true; storeBootslot(i + 1); } - err_ptr += ee_sprintf(err_ptr, "Load slot #%lu %s.\n", (i+1), g_startFirmLaunch ? "success" : "failed"); + err_ptr += ee_sprintf(err_ptr, "Load slot #%lu %s.\n", (i+1), startFirmLaunch ? "success" : "failed"); break; } } @@ -236,14 +242,14 @@ err_ptr += ee_sprintf(err_ptr, "Boot path is %s\n", path); firm_err = loadVerifyFirm(path, false); - g_startFirmLaunch = (firm_err >= 0); + startFirmLaunch = (firm_err >= 0); // no need to store the bootslot here as it stays as is - err_ptr += ee_sprintf(err_ptr, "Load slot #%lu %s.\n", nextBootSlot, g_startFirmLaunch ? "success" : "failed"); + err_ptr += ee_sprintf(err_ptr, "Load slot #%lu %s.\n", nextBootSlot, startFirmLaunch ? "success" : "failed"); } } - if (!g_startFirmLaunch) + if (!startFirmLaunch) { err_ptr += ee_sprintf(err_ptr, "Firmware not loaded!\nAre boot slots properly set up?\n"); } @@ -266,14 +272,21 @@ if(err_string) free(err_string); // power off - if(hidGetPowerButton(true)) + if(hidGetPowerButton(true) || (menu_ret == MENU_RET_POWEROFF)) { storeBootslot(0); power_off(); } + + // reboot + if(menu_ret == MENU_RET_REBOOT) + { + storeBootslot(0); + power_reboot(); + } // firm launch - if(g_startFirmLaunch) + if(startFirmLaunch) { // make sure GFX is in the right state if(!gfx_initialized && (firm_err == 1)) diff --git a/source/arm11/menu/menu.c b/source/arm11/menu/menu.c index 2af41a6..67384eb 100644 --- a/source/arm11/menu/menu.c +++ b/source/arm11/menu/menu.c @@ -33,7 +33,6 @@ #include "arm11/console.h" #include "arm11/debug.h" #include "arm11/fmt.h" -#include "arm11/main.h" #include "fs.h" // for drive names static BatteryState battery; @@ -305,7 +304,8 @@ getBatteryState(&battery); // main menu processing loop - while (!g_startFirmLaunch) + u32 menu_ret = MENU_OK; + while (menu_ret < MENU_RET_CONTINUE) { // handle power button if(hidGetPowerButton(false)) @@ -336,10 +336,6 @@ { u32 next_menu_idx = curr_menu->entries[index].param; - // special handling for MENU_LEAVE_PARAM - if (next_menu_idx == MENU_LEAVE_PARAM) - break; - // store previous menu and index for return if (menu_lvl < MENU_MAX_DEPTH) { @@ -355,7 +351,7 @@ { // call menu entry function MenuEntry* entry = &(curr_menu->entries[index]); - (*(entry->function))(desc_con, menu_con, entry->param); + menu_ret = (*(entry->function))(desc_con, menu_con, entry->param); // force redraw (somewhat hacky) last_menu = NULL; } @@ -385,5 +381,5 @@ } } - return 0; + return menu_ret; } diff --git a/source/arm11/menu/menu_func.c b/source/arm11/menu/menu_func.c index f3f28f1..a569443 100644 --- a/source/arm11/menu/menu_func.c +++ b/source/arm11/menu/menu_func.c @@ -27,6 +27,7 @@ #include "fsutils.h" #include "arm11/menu/battery.h" #include "arm11/menu/bootslot.h" +#include "arm11/menu/menu.h" #include "arm11/menu/menu_color.h" #include "arm11/menu/menu_fsel.h" #include "arm11/menu/menu_func.h" @@ -38,7 +39,6 @@ #include "arm11/debug.h" #include "arm11/fmt.h" #include "arm11/firm.h" -#include "arm11/main.h" @@ -128,11 +128,18 @@ } +u32 menuReturn(PrintConsole* term_con, PrintConsole* menu_con, u32 param) +{ + (void) term_con; + (void) menu_con; + return param; +} + u32 menuSetBootMode(PrintConsole* term_con, PrintConsole* menu_con, u32 param) { (void) term_con; (void) menu_con; - u32 res = (configSetKeyData(KBootMode, ¶m)) ? 0 : 1; + u32 res = (configSetKeyData(KBootMode, ¶m)) ? MENU_OK : MENU_FAIL; return res; } @@ -150,7 +157,7 @@ { configDeleteKey(KBootOption1Buttons + slot); configDeleteKey(KBootOption1 + slot); - return 0; + return MENU_OK; } if (configDataExist(KBootOption1 + slot)) @@ -162,9 +169,9 @@ ee_printf_screen_center("Select a firmware file for slot #%lu.\nPress [HOME] to cancel.", slot + 1); updateScreens(); - u32 res = 0; + u32 res = MENU_OK; if (menuFileSelector(res_path, menu_con, start, "*firm*", true)) - res = (configSetKeyData(KBootOption1 + slot, res_path)) ? 0 : 1; + res = (configSetKeyData(KBootOption1 + slot, res_path)) ? MENU_OK : MENU_FAIL; free(res_path); return res; @@ -180,13 +187,13 @@ // don't allow setting this up if firm is not set if (!configDataExist(KBootOption1 + slot)) - return 0; + return MENU_OK; // if bit4 of param is set, delete boot keys and return if (param & 0x10) { configDeleteKey(KBootOption1Buttons + slot); - return 0; + return MENU_OK; } hidScanInput(); @@ -237,7 +244,7 @@ do { GFX_waitForEvent(GFX_EVENT_PDC0, true); - if(hidGetPowerButton(false)) return 1; + if(hidGetPowerButton(false)) return MENU_FAIL; hidScanInput(); kHeldNew = hidKeysHeld(); @@ -246,7 +253,7 @@ while ((kHeld == kHeldNew) && (++vBlanks < 100)); // check HOME key - if (kHeldNew & KEY_HOME) return 1; + if (kHeldNew & KEY_HOME) return MENU_FAIL; } while (!((kHeld|kHeldNew) & 0xfff)); // repeat checks until actual buttons are held @@ -256,7 +263,7 @@ } // if we arrive here, we have a button combo - u32 res = (configSetKeyData(KBootOption1Buttons + slot, &kHeld)) ? 0 : 1; + u32 res = (configSetKeyData(KBootOption1Buttons + slot, &kHeld)) ? MENU_OK : MENU_FAIL; return res; } @@ -287,7 +294,7 @@ path = path_store; if (!menuFileSelector(path, menu_con, NULL, "*firm*", true)) - return 1; + return MENU_FAIL; // back to terminal console consoleSelect(term_con); @@ -304,13 +311,12 @@ } ee_printf("\nFirm load success, launching firm..."); // <-- you will never see this - g_startFirmLaunch = true; // store the bootslot u32 slot = (param < N_BOOTSLOTS) ? (param + 1) : 0; storeBootslot(slot); - return 0; + return MENU_RET_FIRMLOADED; fail: @@ -318,7 +324,7 @@ updateScreens(); outputEndWait(); - return 1; + return MENU_FAIL; } u32 menuBackupNand(PrintConsole* term_con, PrintConsole* menu_con, u32 param) @@ -326,7 +332,7 @@ (void) menu_con; (void) param; s32 error = 0; - u32 result = 1; + u32 result = MENU_FAIL; // select & clear console consoleSelect(term_con); @@ -430,14 +436,14 @@ fFreeDeviceBuffer(dbufHandle); fClose(fHandle); fUnlink(fpath); - return 1; + return MENU_FAIL; } } // NAND access finalized ee_printf_progress("NAND backup", PROGRESS_WIDTH, nand_size, nand_size); ee_printf("\n" ESC_SCHEME_GOOD "NAND backup finished.\n" ESC_RESET); - result = 0; + result = MENU_OK; fail_close_handles: @@ -455,7 +461,7 @@ outputEndWait(); - if (result != 0) fUnlink(fpath); + if (result != MENU_OK) fUnlink(fpath); hidScanInput(); // throw away any input from impatient users return result; } @@ -464,7 +470,7 @@ { bool forced = param; // if param != 0 -> forced restore s32 error = 0; - u32 result = 1; + u32 result = MENU_FAIL; // select & clear console @@ -502,7 +508,7 @@ char fpath[FF_MAX_LFN + 1]; if (!menuFileSelector(fpath, menu_con, NAND_BACKUP_PATH, "*.bin", false)) - return 1; // canceled by user + return MENU_FAIL; // canceled by user // select & clear console consoleSelect(term_con); @@ -511,11 +517,11 @@ // ask the user for confirmation if (forced) { - if (!askConfirmation(ESC_SCHEME_BAD "WARNING:" ESC_RESET "\nYou're about to force-restore a NAND image to\nyour system. Doing this with an incompatible\nNAND image will **BRICK** your console! Make\nsure you backed up your important data!")) return 1; + if (!askConfirmation(ESC_SCHEME_BAD "WARNING:" ESC_RESET "\nYou're about to force-restore a NAND image to\nyour system. Doing this with an incompatible\nNAND image will **BRICK** your console! Make\nsure you backed up your important data!")) return MENU_FAIL; } else { - if (!askConfirmation(ESC_SCHEME_BAD "WARNING:" ESC_RESET "\nYou're about to restore a NAND image to\nyour system. Make sure you have backups of\nyour important data!")) return 1; + if (!askConfirmation(ESC_SCHEME_BAD "WARNING:" ESC_RESET "\nYou're about to restore a NAND image to\nyour system. Make sure you have backups of\nyour important data!")) return MENU_FAIL; } consoleClear(); @@ -602,14 +608,14 @@ fFinalizeRawAccess(devHandle); fFreeDeviceBuffer(dbufHandle); fClose(fHandle); - return 1; + return MENU_FAIL; } } // NAND access finalized ee_printf_progress("NAND restore", PROGRESS_WIDTH, file_size, file_size); ee_printf("\n" ESC_SCHEME_GOOD "NAND restore finished.\n" ESC_RESET); - result = 0; + result = MENU_OK; fail_close_handles: @@ -634,7 +640,7 @@ { char firm_drv[8] = { 'f', 'i', 'r', 'm', '0' + param, ':', '\0' }; char firm_path[FF_MAX_LFN + 1]; - u32 result = 1; + u32 result = MENU_FAIL; // clear console @@ -651,7 +657,7 @@ ee_printf_screen_center("Select a firmware file to install.\nPress [HOME] to cancel."); updateScreens(); if (!menuFileSelector(firm_path, menu_con, NULL, "*firm*", true)) - return 1; // cancel by user + return MENU_FAIL; // cancel by user // select and clear console @@ -659,7 +665,7 @@ consoleClear(); // ask the user for confirmation - if (!askConfirmation(ESC_SCHEME_BAD "WARNING:" ESC_RESET "\nYou're about to install a firmware to %s.\nFlashing incompatible firmwares may lead to\nunexpected results.", firm_drv)) return 1; + if (!askConfirmation(ESC_SCHEME_BAD "WARNING:" ESC_RESET "\nYou're about to install a firmware to %s.\nFlashing incompatible firmwares may lead to\nunexpected results.", firm_drv)) return MENU_FAIL; consoleClear(); ee_printf(ESC_SCHEME_ACCENT1 "Flashing firmware to %s:\n%s\n" ESC_RESET "\nLoading firmware... ", firm_drv, firm_path); @@ -686,7 +692,7 @@ ee_printf(ESC_SCHEME_GOOD "OK\n" ESC_RESET); ee_printf(ESC_SCHEME_GOOD "\nFirm was flashed to %s.\n" ESC_RESET, firm_drv); - result = 0; + result = MENU_OK; fail: @@ -714,7 +720,7 @@ ee_printf_screen_center("Select fastboot3DS update file.\nPress [HOME] to cancel."); updateScreens(); if (!menuFileSelector(firm_path, menu_con, "sdmc:", "*firm*", true)) - return 1; // cancel by user + return MENU_FAIL; // cancel by user // verify and install update @@ -776,8 +782,8 @@ } ee_printf(ESC_SCHEME_GOOD "OK\n" ESC_RESET); - ee_printf(ESC_SCHEME_GOOD "\nfastboot3DS was updated.\n" ESC_RESET); - result = 0; + ee_printf(ESC_SCHEME_GOOD "\nfastboot3DS was updated.\nSystem will reboot.\n" ESC_RESET); + result = MENU_RET_REBOOT; fail: @@ -873,9 +879,10 @@ } - return 0; + return MENU_OK; } +/* u32 menuDummyFunc(PrintConsole* term_con, PrintConsole* menu_con, u32 param) { (void) menu_con; @@ -889,10 +896,9 @@ updateScreens(); outputEndWait(); - return 0; + return MENU_OK; } -/* u32 debugSettingsView(PrintConsole* term_con, PrintConsole* menu_con, u32 param) { (void) menu_con;