1
0
Fork 0
mirror of https://github.com/Scandal-UK/Incognito_RCM.git synced 2024-11-22 20:06:42 +00:00
Incognito_RCM/source/main.c

327 lines
8.2 KiB
C
Raw Normal View History

2019-03-04 23:05:42 +00:00
/*
* Copyright (c) 2018 naehrwert
*
2019-05-11 22:51:44 +01:00
* Copyright (c) 2018-2019 CTCaer
2019-03-04 23:05:42 +00:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 <http://www.gnu.org/licenses/>.
*/
#include <string.h>
2019-09-17 05:18:41 +01:00
#include "config/config.h"
2019-03-04 23:05:42 +00:00
#include "gfx/di.h"
#include "gfx/gfx.h"
#include "gfx/tui.h"
2019-03-04 23:05:42 +00:00
#include "libs/fatfs/ff.h"
#include "mem/heap.h"
#include "power/max77620.h"
#include "rtc/max77620-rtc.h"
#include "soc/bpmp.h"
2019-03-04 23:05:42 +00:00
#include "soc/hw_init.h"
#include "storage/emummc.h"
2019-03-04 23:05:42 +00:00
#include "storage/sdmmc.h"
#include "utils/sprintf.h"
2019-03-04 23:05:42 +00:00
#include "utils/util.h"
#include "utils/btn.h"
2019-03-04 23:05:42 +00:00
#include "keys/keys.h"
sdmmc_t sd_sdmmc;
sdmmc_storage_t sd_storage;
2019-10-03 17:16:41 +01:00
__attribute__((aligned(16))) FATFS sd_fs;
2019-03-04 23:05:42 +00:00
static bool sd_mounted;
2019-09-17 05:18:41 +01:00
hekate_config h_cfg;
2019-10-03 17:16:41 +01:00
boot_cfg_t __attribute__((section("._boot_cfg"))) b_cfg;
2019-03-04 23:05:42 +00:00
bool sd_mount()
{
if (sd_mounted)
return true;
if (!sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_1, SDMMC_BUS_WIDTH_4, 11))
{
EPRINTF("Failed to init SD card.\nMake sure that it is inserted.\nOr that SD reader is properly seated!");
}
else
{
int res = 0;
res = f_mount(&sd_fs, "sd:", 1);
2019-03-04 23:05:42 +00:00
if (res == FR_OK)
{
sd_mounted = 1;
return true;
}
else
{
EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res);
}
}
return false;
}
void sd_unmount()
{
if (sd_mounted)
{
f_mount(NULL, "sd:", 1);
2019-03-04 23:05:42 +00:00
sdmmc_storage_end(&sd_storage);
sd_mounted = false;
}
}
2019-05-11 22:51:44 +01:00
void *sd_file_read(const char *path, u32 *fsize)
2019-03-04 23:05:42 +00:00
{
FIL fp;
if (f_open(&fp, path, FA_READ) != FR_OK)
return NULL;
2019-03-04 23:05:42 +00:00
u32 size = f_size(&fp);
if (fsize)
*fsize = size;
2019-03-04 23:05:42 +00:00
void *buf = malloc(size);
2019-03-04 23:05:42 +00:00
if (f_read(&fp, buf, size, NULL) != FR_OK)
{
free(buf);
f_close(&fp);
2019-03-04 23:05:42 +00:00
return NULL;
}
2019-05-11 22:51:44 +01:00
f_close(&fp);
2019-03-04 23:05:42 +00:00
return buf;
2019-03-04 23:05:42 +00:00
}
int sd_save_to_file(void *buf, u32 size, const char *filename)
{
FIL fp;
u32 res = 0;
res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (res)
{
EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename);
return res;
2019-03-04 23:05:42 +00:00
}
f_write(&fp, buf, size, NULL);
f_close(&fp);
return 0;
}
2019-04-18 17:47:34 +01:00
// This is a safe and unused DRAM region for our payloads.
2019-10-03 17:16:41 +01:00
#define RELOC_META_OFF 0x7C
#define PATCHED_RELOC_SZ 0x94
2019-04-18 17:47:34 +01:00
#define PATCHED_RELOC_STACK 0x40007000
2019-10-03 17:16:41 +01:00
#define COREBOOT_ADDR (0xD0000000 - 0x100000)
#define CBFS_DRAM_EN_ADDR 0x4003e000
#define CBFS_DRAM_MAGIC 0x4452414D // "DRAM"
2019-03-04 23:05:42 +00:00
2019-04-18 17:47:34 +01:00
void reloc_patcher(u32 payload_dst, u32 payload_src, u32 payload_size)
2019-03-04 23:05:42 +00:00
{
2019-04-18 17:47:34 +01:00
memcpy((u8 *)payload_src, (u8 *)IPL_LOAD_ADDR, PATCHED_RELOC_SZ);
2019-03-04 23:05:42 +00:00
2019-04-18 17:47:34 +01:00
volatile reloc_meta_t *relocator = (reloc_meta_t *)(payload_src + RELOC_META_OFF);
2019-03-04 23:05:42 +00:00
2019-04-18 17:47:34 +01:00
relocator->start = payload_dst - ALIGN(PATCHED_RELOC_SZ, 0x10);
relocator->stack = PATCHED_RELOC_STACK;
2019-10-03 17:16:41 +01:00
relocator->end = payload_dst + payload_size;
relocator->ep = payload_dst;
2019-03-04 23:05:42 +00:00
2019-04-18 17:47:34 +01:00
if (payload_size == 0x7000)
{
memcpy((u8 *)(payload_src + ALIGN(PATCHED_RELOC_SZ, 0x10)), (u8 *)COREBOOT_ADDR, 0x7000); //Bootblock
*(vu32 *)CBFS_DRAM_EN_ADDR = CBFS_DRAM_MAGIC;
}
2019-03-04 23:05:42 +00:00
}
2019-10-03 17:16:41 +01:00
void incognito_sysnand()
{
2019-10-03 17:16:41 +01:00
h_cfg.emummc_force_disable = true;
b_cfg.extra_cfg &= ~EXTRA_CFG_DUMP_EMUMMC;
2019-10-03 17:16:41 +01:00
if (!dump_keys())
2019-10-04 19:48:24 +01:00
goto out;
if (!incognito())
2019-10-03 17:16:41 +01:00
{
2019-10-04 19:48:24 +01:00
gfx_printf("%kError applying Incognito!\nWill restore backup!\n", COLOR_RED);
backupProdinfo();
}
if (!verifyProdinfo())
{
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED);
2019-10-03 17:16:41 +01:00
}
2019-10-04 19:48:24 +01:00
out:
cleanUp();
2019-10-03 17:16:41 +01:00
gfx_printf("\n%k---------------\n%kPress any key to return to the main menu.", COLOR_YELLOW, COLOR_ORANGE);
btn_wait();
}
2019-10-03 17:16:41 +01:00
void incognito_emunand()
{
if (h_cfg.emummc_force_disable)
return;
emu_cfg.enabled = 1;
b_cfg.extra_cfg |= EXTRA_CFG_DUMP_EMUMMC;
2019-10-03 17:16:41 +01:00
if (!dump_keys())
2019-10-04 19:48:24 +01:00
goto out;
if (!incognito())
2019-10-03 17:16:41 +01:00
{
2019-10-04 19:48:24 +01:00
gfx_printf("%kError applying Incognito!\nWill restore backup!\n", COLOR_RED);
backupProdinfo();
}
if (!verifyProdinfo())
{
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED);
2019-10-03 17:16:41 +01:00
}
2019-10-04 19:48:24 +01:00
out:
cleanUp();
2019-10-03 17:16:41 +01:00
gfx_printf("\n%k---------------\n%kPress any key to return to the main menu.", COLOR_YELLOW, COLOR_ORANGE);
btn_wait();
}
2019-10-03 17:16:41 +01:00
void backup_sysnand()
{
h_cfg.emummc_force_disable = true;
b_cfg.extra_cfg &= ~EXTRA_CFG_DUMP_EMUMMC;
2019-10-03 17:16:41 +01:00
if (!dump_keys())
2019-10-04 19:48:24 +01:00
goto out;
backupProdinfo();
2019-10-04 19:48:24 +01:00
out:
cleanUp();
2019-10-03 17:16:41 +01:00
gfx_printf("\n%k---------------\n%kPress any key to return to the main menu.", COLOR_YELLOW, COLOR_ORANGE);
btn_wait();
}
2019-10-03 17:16:41 +01:00
void backup_emunand()
{
if (h_cfg.emummc_force_disable)
return;
emu_cfg.enabled = 1;
b_cfg.extra_cfg |= EXTRA_CFG_DUMP_EMUMMC;
2019-10-03 17:16:41 +01:00
if (!dump_keys())
2019-10-04 19:48:24 +01:00
goto out;
backupProdinfo();
2019-10-04 19:48:24 +01:00
out:
cleanUp();
2019-10-03 17:16:41 +01:00
gfx_printf("\n%k---------------\n%kPress any key to return to the main menu.", COLOR_YELLOW, COLOR_ORANGE);
btn_wait();
}
2019-10-03 17:16:41 +01:00
void restore_sysnand()
{
h_cfg.emummc_force_disable = true;
b_cfg.extra_cfg &= ~EXTRA_CFG_DUMP_EMUMMC;
2019-10-03 17:16:41 +01:00
if (!dump_keys())
2019-10-04 19:48:24 +01:00
goto out;
restoreProdinfo();
2019-10-04 19:48:24 +01:00
if (!verifyProdinfo())
{
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED);
}
out:
cleanUp();
2019-10-03 17:16:41 +01:00
gfx_printf("\n%k---------------\n%kPress any key to return to the main menu.", COLOR_YELLOW, COLOR_ORANGE);
btn_wait();
}
2019-10-03 17:16:41 +01:00
void restore_emunand()
{
if (h_cfg.emummc_force_disable)
return;
emu_cfg.enabled = 1;
b_cfg.extra_cfg |= EXTRA_CFG_DUMP_EMUMMC;
2019-10-03 17:16:41 +01:00
if (!dump_keys())
2019-10-04 19:48:24 +01:00
goto out;
restoreProdinfo();
2019-10-04 19:48:24 +01:00
if (!verifyProdinfo())
{
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED);
}
out:
cleanUp();
2019-10-03 17:16:41 +01:00
gfx_printf("\n%k---------------\n%kPress any key to return to the main menu.", COLOR_YELLOW, COLOR_ORANGE);
btn_wait();
}
2019-10-04 19:48:24 +01:00
ment_t ment_top[] = {
MDEF_HANDLER("Backup (SysNAND)", backup_sysnand, COLOR_ORANGE),
MDEF_HANDLER("Backup (emuMMC)", backup_emunand, COLOR_ORANGE),
MDEF_CAPTION("", COLOR_YELLOW),
MDEF_HANDLER("Incognito (SysNAND)", incognito_sysnand, COLOR_ORANGE),
MDEF_HANDLER("Incognito (emuMMC)", incognito_emunand, COLOR_ORANGE),
MDEF_CAPTION("", COLOR_YELLOW),
MDEF_HANDLER("Restore (SysNAND)", restore_sysnand, COLOR_ORANGE),
MDEF_HANDLER("Restore (emuMMC)", restore_emunand, COLOR_ORANGE),
MDEF_CAPTION("", COLOR_YELLOW),
2019-10-03 17:16:41 +01:00
MDEF_CAPTION("---------------", COLOR_YELLOW),
MDEF_HANDLER("Reboot (Normal)", reboot_normal, COLOR_GREEN),
MDEF_HANDLER("Reboot (RCM)", reboot_rcm, COLOR_BLUE),
MDEF_HANDLER("Power off", power_off, COLOR_VIOLET),
MDEF_END()};
2019-10-03 17:16:41 +01:00
menu_t menu_top = {ment_top, NULL, 0, 0};
2019-10-03 17:16:41 +01:00
#define IPL_STACK_TOP 0x4003F000
2019-04-18 17:47:34 +01:00
#define IPL_HEAP_START 0x90020000
2019-03-04 23:05:42 +00:00
extern void pivot_stack(u32 stack_top);
2019-05-11 22:51:44 +01:00
void ipl_main()
{
2019-03-04 23:05:42 +00:00
config_hw();
2019-04-18 17:47:34 +01:00
pivot_stack(IPL_STACK_TOP);
heap_init(IPL_HEAP_START);
2019-03-04 23:05:42 +00:00
2019-09-17 05:18:41 +01:00
set_default_configuration();
2019-03-04 23:05:42 +00:00
display_init();
u32 *fb = display_init_framebuffer();
2019-04-18 17:47:34 +01:00
gfx_init_ctxt(fb, 720, 1280, 720);
gfx_con_init();
2019-03-04 23:05:42 +00:00
display_backlight_pwm_init();
bpmp_clk_rate_set(BPMP_CLK_SUPER_BOOST);
h_cfg.emummc_force_disable = emummc_load_cfg();
if (b_cfg.boot_cfg & BOOT_CFG_SEPT_RUN)
{
if (!(b_cfg.extra_cfg & EXTRA_CFG_DUMP_EMUMMC))
h_cfg.emummc_force_disable = true;
dump_keys();
}
if (h_cfg.emummc_force_disable)
{
ment_top[1].type = MENT_CAPTION;
ment_top[1].color = 0xFF555555;
ment_top[1].handler = NULL;
}
while (true)
tui_do_menu(&menu_top);
while (true)
bpmp_halt();
2019-03-04 23:05:42 +00:00
}