Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/frontend/fe_tools.c
3693 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2024 CTCaer
4
* Copyright (c) 2018 Reisyukaku
5
*
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms and conditions of the GNU General Public License,
8
* version 2, as published by the Free Software Foundation.
9
*
10
* This program is distributed in the hope it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
* more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <string.h>
20
#include <stdlib.h>
21
22
#include <bdk.h>
23
24
#include "fe_tools.h"
25
#include "../config.h"
26
#include "../gfx/tui.h"
27
#include <libs/fatfs/ff.h>
28
29
extern boot_cfg_t b_cfg;
30
31
#pragma GCC push_options
32
#pragma GCC optimize ("Os")
33
34
static void _toggle_autorcm(bool enable)
35
{
36
gfx_clear_partial_grey(0x1B, 0, 1256);
37
gfx_con_setpos(0, 0);
38
39
int i, sect = 0;
40
u8 corr_mod0, mod1;
41
u8 *tempbuf = (u8 *)malloc(0x200);
42
43
// Get the correct RSA modulus byte masks.
44
nx_emmc_get_autorcm_masks(&corr_mod0, &mod1);
45
46
// Iterate BCTs.
47
for (i = 0; i < 4; i++)
48
{
49
sect = (0x200 + (0x4000 * i)) / EMMC_BLOCKSIZE;
50
sdmmc_storage_read(&emmc_storage, sect, 1, tempbuf);
51
52
// Check if 2nd byte of modulus is correct.
53
if (tempbuf[0x11] != mod1)
54
continue;
55
56
if (enable)
57
tempbuf[0x10] = 0;
58
else
59
tempbuf[0x10] = corr_mod0;
60
sdmmc_storage_write(&emmc_storage, sect, 1, tempbuf);
61
}
62
63
free(tempbuf);
64
65
if (enable)
66
gfx_printf("%kAutoRCM mode enabled!%k", TXT_CLR_ORANGE, TXT_CLR_DEFAULT);
67
else
68
gfx_printf("%kAutoRCM mode disabled!%k", TXT_CLR_GREENISH, TXT_CLR_DEFAULT);
69
gfx_printf("\n\nPress any key...\n");
70
71
btn_wait();
72
}
73
74
static void _enable_autorcm() { _toggle_autorcm(true); }
75
static void _disable_autorcm() { _toggle_autorcm(false); }
76
77
bool tools_autorcm_enabled()
78
{
79
u8 mod0, mod1;
80
u8 *tempbuf = (u8 *)malloc(0x200);
81
82
// Get the correct RSA modulus byte masks.
83
nx_emmc_get_autorcm_masks(&mod0, &mod1);
84
85
// Get 1st RSA modulus.
86
emmc_set_partition(EMMC_BOOT0);
87
sdmmc_storage_read(&emmc_storage, 0x200 / EMMC_BLOCKSIZE, 1, tempbuf);
88
89
// Check if 2nd byte of modulus is correct.
90
bool enabled = false;
91
if (tempbuf[0x11] == mod1)
92
if (tempbuf[0x10] != mod0)
93
enabled = true;
94
95
free(tempbuf);
96
97
return enabled;
98
}
99
100
void menu_autorcm()
101
{
102
gfx_clear_grey(0x1B);
103
gfx_con_setpos(0, 0);
104
105
if (h_cfg.rcm_patched)
106
{
107
WPRINTF("This device is RCM patched and the\nfunction is disabled to avoid BRICKS!\n");
108
btn_wait();
109
110
return;
111
}
112
113
if (emmc_initialize(false))
114
{
115
EPRINTF("Failed to init eMMC.");
116
btn_wait();
117
118
return;
119
}
120
121
// Do a simple check on the main BCT.
122
bool enabled = tools_autorcm_enabled();
123
124
// Create AutoRCM menu.
125
ment_t *ments = (ment_t *)malloc(sizeof(ment_t) * 6);
126
127
ments[0].type = MENT_BACK;
128
ments[0].caption = "Back";
129
130
ments[1].type = MENT_CHGLINE;
131
132
ments[2].type = MENT_CAPTION;
133
ments[3].type = MENT_CHGLINE;
134
if (!enabled)
135
{
136
ments[2].caption = "Status: Disabled!";
137
ments[2].color = TXT_CLR_GREENISH;
138
ments[4].caption = "Enable AutoRCM";
139
ments[4].handler = _enable_autorcm;
140
}
141
else
142
{
143
ments[2].caption = "Status: Enabled!";
144
ments[2].color = TXT_CLR_ORANGE;
145
ments[4].caption = "Disable AutoRCM";
146
ments[4].handler = _disable_autorcm;
147
}
148
ments[4].type = MENT_HDLR_RE;
149
ments[4].data = NULL;
150
151
memset(&ments[5], 0, sizeof(ment_t));
152
menu_t menu = {ments, "This corrupts BOOT0!", 0, 0};
153
154
tui_do_menu(&menu);
155
156
emmc_end();
157
158
free(ments);
159
}
160
161
#pragma GCC pop_options
162
163