Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/frontend/fe_tools.c
1471 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
extern hekate_config h_cfg;
31
32
#pragma GCC push_options
33
#pragma GCC optimize ("Os")
34
35
static void _toggle_autorcm(bool enable)
36
{
37
gfx_clear_partial_grey(0x1B, 0, 1256);
38
gfx_con_setpos(0, 0);
39
40
int i, sect = 0;
41
u8 corr_mod0, mod1;
42
u8 *tempbuf = (u8 *)malloc(0x200);
43
44
// Get the correct RSA modulus byte masks.
45
nx_emmc_get_autorcm_masks(&corr_mod0, &mod1);
46
47
// Iterate BCTs.
48
for (i = 0; i < 4; i++)
49
{
50
sect = (0x200 + (0x4000 * i)) / EMMC_BLOCKSIZE;
51
sdmmc_storage_read(&emmc_storage, sect, 1, tempbuf);
52
53
// Check if 2nd byte of modulus is correct.
54
if (tempbuf[0x11] != mod1)
55
continue;
56
57
if (enable)
58
tempbuf[0x10] = 0;
59
else
60
tempbuf[0x10] = corr_mod0;
61
sdmmc_storage_write(&emmc_storage, sect, 1, tempbuf);
62
}
63
64
free(tempbuf);
65
66
if (enable)
67
gfx_printf("%kAutoRCM mode enabled!%k", TXT_CLR_ORANGE, TXT_CLR_DEFAULT);
68
else
69
gfx_printf("%kAutoRCM mode disabled!%k", TXT_CLR_GREENISH, TXT_CLR_DEFAULT);
70
gfx_printf("\n\nPress any key...\n");
71
72
btn_wait();
73
}
74
75
static void _enable_autorcm() { _toggle_autorcm(true); }
76
static void _disable_autorcm() { _toggle_autorcm(false); }
77
78
bool tools_autorcm_enabled()
79
{
80
u8 mod0, mod1;
81
u8 *tempbuf = (u8 *)malloc(0x200);
82
83
// Get the correct RSA modulus byte masks.
84
nx_emmc_get_autorcm_masks(&mod0, &mod1);
85
86
// Get 1st RSA modulus.
87
emmc_set_partition(EMMC_BOOT0);
88
sdmmc_storage_read(&emmc_storage, 0x200 / EMMC_BLOCKSIZE, 1, tempbuf);
89
90
// Check if 2nd byte of modulus is correct.
91
bool enabled = false;
92
if (tempbuf[0x11] == mod1)
93
if (tempbuf[0x10] != mod0)
94
enabled = true;
95
96
free(tempbuf);
97
98
return enabled;
99
}
100
101
void menu_autorcm()
102
{
103
gfx_clear_grey(0x1B);
104
gfx_con_setpos(0, 0);
105
106
if (h_cfg.rcm_patched)
107
{
108
WPRINTF("This device is RCM patched and the\nfunction is disabled to avoid BRICKS!\n");
109
btn_wait();
110
111
return;
112
}
113
114
if (!emmc_initialize(false))
115
{
116
EPRINTF("Failed to init eMMC.");
117
btn_wait();
118
119
return;
120
}
121
122
// Do a simple check on the main BCT.
123
bool enabled = tools_autorcm_enabled();
124
125
// Create AutoRCM menu.
126
ment_t *ments = (ment_t *)malloc(sizeof(ment_t) * 6);
127
128
ments[0].type = MENT_BACK;
129
ments[0].caption = "Back";
130
131
ments[1].type = MENT_CHGLINE;
132
133
ments[2].type = MENT_CAPTION;
134
ments[3].type = MENT_CHGLINE;
135
if (!enabled)
136
{
137
ments[2].caption = "Status: Disabled!";
138
ments[2].color = TXT_CLR_GREENISH;
139
ments[4].caption = "Enable AutoRCM";
140
ments[4].handler = _enable_autorcm;
141
}
142
else
143
{
144
ments[2].caption = "Status: Enabled!";
145
ments[2].color = TXT_CLR_ORANGE;
146
ments[4].caption = "Disable AutoRCM";
147
ments[4].handler = _disable_autorcm;
148
}
149
ments[4].type = MENT_HDLR_RE;
150
ments[4].data = NULL;
151
152
memset(&ments[5], 0, sizeof(ment_t));
153
menu_t menu = {ments, "This corrupts BOOT0!", 0, 0};
154
155
tui_do_menu(&menu);
156
157
emmc_end();
158
159
free(ments);
160
}
161
162
#pragma GCC pop_options
163
164