Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/hos/secmon_exo.c
1476 views
1
/*
2
* Copyright (c) 2018-2024 CTCaer
3
* Copyright (c) 2019 Atmosphère-NX
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <string.h>
19
#include <stdlib.h>
20
21
#include <bdk.h>
22
23
#include "hos.h"
24
#include "../config.h"
25
#include <libs/fatfs/ff.h>
26
#include "../storage/emummc.h"
27
28
extern hekate_config h_cfg;
29
30
enum emuMMC_Type
31
{
32
emuMMC_None = 0,
33
emuMMC_Partition,
34
emuMMC_File,
35
emuMMC_MAX
36
};
37
38
/* "EFS0" */
39
#define EMUMMC_MAGIC 0x30534645
40
#define EMUMMC_FILE_PATH_MAX 0x80
41
42
typedef struct
43
{
44
u32 magic;
45
u32 type;
46
u32 id;
47
u32 fs_ver;
48
} emummc_base_config_t;
49
50
typedef struct
51
{
52
u64 start_sector;
53
} emummc_partition_config_t;
54
55
typedef struct
56
{
57
char path[EMUMMC_FILE_PATH_MAX];
58
} emummc_file_config_t;
59
60
typedef struct
61
{
62
emummc_base_config_t base_cfg;
63
union
64
{
65
emummc_partition_config_t partition_cfg;
66
emummc_file_config_t file_cfg;
67
};
68
char nintendo_path[EMUMMC_FILE_PATH_MAX];
69
} exo_emummc_config_t;
70
71
typedef struct _exo_cfg_t
72
{
73
u32 magic;
74
u32 fwno;
75
u32 flags[2];
76
u16 display_id;
77
u8 uart_port;
78
u8 uart_invert;
79
u32 uart_baudrate;
80
u32 rsvd1[2];
81
exo_emummc_config_t emummc_cfg;
82
} exo_cfg_t;
83
84
typedef struct _atm_meta_t
85
{
86
u32 magic;
87
u32 fwno;
88
} wb_cfg_t;
89
90
// Atmosphère reboot-to-fatal-error.
91
typedef struct _atm_fatal_error_ctx
92
{
93
u32 magic;
94
u32 error_desc;
95
u64 title_id;
96
union
97
{
98
u64 gprs[32];
99
struct
100
{
101
u64 _gprs[29];
102
u64 fp;
103
u64 lr;
104
u64 sp;
105
};
106
};
107
u64 pc;
108
u64 module_base;
109
u32 pstate;
110
u32 afsr0;
111
u32 afsr1;
112
u32 esr;
113
u64 far;
114
u64 report_identifier; // Normally just system tick.
115
u64 stack_trace_size;
116
u64 stack_dump_size;
117
u64 stack_trace[0x20];
118
u8 stack_dump[0x100];
119
u8 tls[0x100];
120
} atm_fatal_error_ctx;
121
122
#define ATM_FATAL_ERR_CTX_ADDR 0x4003E000
123
#define ATM_FATAL_MAGIC 0x30454641 // AFE0
124
125
#define ATM_EXO_FATAL_ADDR 0x80020000
126
#define ATM_EXO_FATAL_SIZE SZ_128K
127
128
#define ATM_WB_HEADER_OFF 0x244
129
#define ATM_WB_MAGIC 0x30544257 // WBT0
130
131
// Exosphère mailbox defines.
132
#define EXO_CFG_ADDR 0x8000F000
133
#define EXO_MAGIC_VAL 0x304F5845
134
#define EXO_FLAG_DBG_PRIV BIT(1)
135
#define EXO_FLAG_DBG_USER BIT(2)
136
#define EXO_FLAG_NO_USER_EXC BIT(3)
137
#define EXO_FLAG_USER_PMU BIT(4)
138
#define EXO_FLAG_CAL0_BLANKING BIT(5)
139
#define EXO_FLAG_CAL0_WRITES_SYS BIT(6)
140
#define EXO_FLAG_ENABLE_USB3 BIT(7)
141
142
#define EXO_FW_VER(mj, mn) (((mj) << 24) | ((mn) << 16))
143
144
void config_exosphere(launch_ctxt_t *ctxt, u32 warmboot_base)
145
{
146
u32 exo_fw_no;
147
u32 exo_flags = 0;
148
bool usb3_force = false;
149
bool user_debug = false;
150
bool cal0_blanking = false;
151
bool cal0_allow_writes_sys = false;
152
153
memset((exo_cfg_t *)EXO_CFG_ADDR, 0, sizeof(exo_cfg_t));
154
155
volatile exo_cfg_t *exo_cfg = (exo_cfg_t *)EXO_CFG_ADDR;
156
157
//! TODO: Replace current HOS version decoding (as it's bound to break in the future).
158
159
// Old exosphere target versioning.
160
if (ctxt->pkg1_id->kb >= HOS_KB_VERSION_1210) // 12.1.0+
161
exo_fw_no = ctxt->pkg1_id->kb + 4;
162
else if (ctxt->pkg1_id->fuses <= 3 || ctxt->pkg1_id->fuses >= 10) // 1.0.0 - 3.0.0, 8.1.0 - 12.0.3.
163
exo_fw_no = ctxt->pkg1_id->fuses;
164
else
165
exo_fw_no = ctxt->pkg1_id->fuses - 1; // 3.0.1 - 7.0.1, 8.0.x.
166
167
// Handle versions that change API and do not burn new fuse.
168
if (!memcmp(ctxt->pkg1_id->id, "20190314", 8) || // 8.0.x, same fuses with 7.0.1.
169
!memcmp(ctxt->pkg1_id->id, "20210129", 8) // 12.0.0, same fuses with 11.0.0.
170
)
171
exo_fw_no++;
172
173
// Set 12.1.0 specific revision.
174
if (ctxt->pkg1_id->kb == HOS_KB_VERSION_1210)
175
ctxt->exo_ctx.hos_revision = 1;
176
177
// Feed old exosphere target versioning to new.
178
switch (exo_fw_no)
179
{
180
case 1 ... 4:
181
case 6:
182
exo_fw_no = EXO_FW_VER(exo_fw_no, 0);
183
break;
184
case 5:
185
exo_fw_no = EXO_FW_VER(5, ctxt->exo_ctx.hos_revision);
186
break;
187
case 7:
188
exo_fw_no = EXO_FW_VER(6, 2);
189
break;
190
case 8 ... 9:
191
exo_fw_no = EXO_FW_VER(exo_fw_no - 1, 0);
192
break;
193
case 10:
194
exo_fw_no = EXO_FW_VER(8, 1);
195
break;
196
case 11:
197
exo_fw_no = EXO_FW_VER(9, 0);
198
break;
199
case 12:
200
exo_fw_no = EXO_FW_VER(9, 1);
201
break;
202
case 13 ... 23: //!TODO: Update on API changes. 23: 20.0.0.
203
exo_fw_no = EXO_FW_VER(exo_fw_no - 3, ctxt->exo_ctx.hos_revision);
204
break;
205
}
206
207
// Parse exosphere.ini.
208
if (!ctxt->stock)
209
{
210
LIST_INIT(ini_exo_sections);
211
if (ini_parse(&ini_exo_sections, "exosphere.ini", false))
212
{
213
LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, &ini_exo_sections, link)
214
{
215
// Only parse exosphere section.
216
if (!(ini_sec->type == INI_CHOICE) || strcmp(ini_sec->name, "exosphere"))
217
continue;
218
219
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link)
220
{
221
if (!strcmp("debugmode_user", kv->key))
222
user_debug = atoi(kv->val);
223
else if (!strcmp("log_port", kv->key))
224
exo_cfg->uart_port = atoi(kv->val);
225
else if (!strcmp("log_inverted", kv->key))
226
exo_cfg->uart_invert = atoi(kv->val);
227
else if (!strcmp("log_baud_rate", kv->key))
228
exo_cfg->uart_baudrate = atoi(kv->val);
229
else if (emu_cfg.enabled && !h_cfg.emummc_force_disable)
230
{
231
if (!strcmp("blank_prodinfo_emummc", kv->key))
232
cal0_blanking = atoi(kv->val);
233
}
234
else
235
{
236
if (!strcmp("blank_prodinfo_sysmmc", kv->key))
237
cal0_blanking = atoi(kv->val);
238
else if (!strcmp("allow_writing_to_cal_sysmmc", kv->key))
239
cal0_allow_writes_sys = atoi(kv->val);
240
}
241
}
242
break;
243
}
244
}
245
246
// Parse usb mtim settings. Avoid parsing if it's overridden.
247
if (!ctxt->exo_ctx.usb3_force)
248
{
249
LIST_INIT(ini_sys_sections);
250
if (ini_parse(&ini_sys_sections, "atmosphere/config/system_settings.ini", false))
251
{
252
LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, &ini_sys_sections, link)
253
{
254
// Only parse usb section.
255
if (!(ini_sec->type == INI_CHOICE) || strcmp(ini_sec->name, "usb"))
256
continue;
257
258
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link)
259
{
260
if (!strcmp("usb30_force_enabled", kv->key))
261
{
262
usb3_force = !strcmp("u8!0x1", kv->val);
263
break; // Only parse usb30_force_enabled key.
264
}
265
}
266
break;
267
}
268
}
269
}
270
}
271
272
// Private debug mode always on for CFW mode.
273
if (!ctxt->stock)
274
exo_flags |= EXO_FLAG_DBG_PRIV;
275
276
// Enable user debug.
277
if (user_debug)
278
exo_flags |= EXO_FLAG_DBG_USER;
279
280
// Disable proper failure handling.
281
if (ctxt->exo_ctx.no_user_exceptions)
282
exo_flags |= EXO_FLAG_NO_USER_EXC;
283
284
// Enable user access to PMU.
285
if (ctxt->exo_ctx.user_pmu)
286
exo_flags |= EXO_FLAG_USER_PMU;
287
288
// Enable USB 3.0. Check if system_settings ini value is overridden. If not, check if enabled in ini.
289
if ((ctxt->exo_ctx.usb3_force && *ctxt->exo_ctx.usb3_force)
290
|| (!ctxt->exo_ctx.usb3_force && usb3_force))
291
exo_flags |= EXO_FLAG_ENABLE_USB3;
292
293
// Enable prodinfo blanking. Check if exo ini value is overridden. If not, check if enabled in exo ini.
294
if ((ctxt->exo_ctx.cal0_blank && *ctxt->exo_ctx.cal0_blank)
295
|| (!ctxt->exo_ctx.cal0_blank && cal0_blanking))
296
exo_flags |= EXO_FLAG_CAL0_BLANKING;
297
298
// Allow prodinfo writes. Check if exo ini value is overridden. If not, check if enabled in exo ini.
299
if ((ctxt->exo_ctx.cal0_allow_writes_sys && *ctxt->exo_ctx.cal0_allow_writes_sys)
300
|| (!ctxt->exo_ctx.cal0_allow_writes_sys && cal0_allow_writes_sys))
301
exo_flags |= EXO_FLAG_CAL0_WRITES_SYS;
302
303
// Set mailbox values.
304
exo_cfg->magic = EXO_MAGIC_VAL;
305
exo_cfg->fwno = exo_fw_no;
306
exo_cfg->flags[0] = exo_flags;
307
308
// If warmboot is lp0fw, add in RSA modulus.
309
volatile wb_cfg_t *wb_cfg = (wb_cfg_t *)(warmboot_base + ATM_WB_HEADER_OFF);
310
311
if (wb_cfg->magic == ATM_WB_MAGIC)
312
{
313
wb_cfg->fwno = exo_fw_no;
314
315
// Set warmboot binary rsa modulus.
316
pkg1_warmboot_rsa_mod(warmboot_base);
317
}
318
319
if (emu_cfg.enabled && !h_cfg.emummc_force_disable)
320
{
321
exo_cfg->emummc_cfg.base_cfg.magic = EMUMMC_MAGIC;
322
exo_cfg->emummc_cfg.base_cfg.type = emu_cfg.sector ? emuMMC_Partition : emuMMC_File;
323
exo_cfg->emummc_cfg.base_cfg.fs_ver = emu_cfg.fs_ver;
324
exo_cfg->emummc_cfg.base_cfg.id = emu_cfg.id;
325
326
if (emu_cfg.sector)
327
exo_cfg->emummc_cfg.partition_cfg.start_sector = emu_cfg.sector;
328
else
329
strcpy((char *)exo_cfg->emummc_cfg.file_cfg.path, emu_cfg.path);
330
331
if (!ctxt->stock && emu_cfg.nintendo_path && emu_cfg.nintendo_path[0])
332
strcpy((char *)exo_cfg->emummc_cfg.nintendo_path, emu_cfg.nintendo_path);
333
else
334
strcpy((char *)exo_cfg->emummc_cfg.nintendo_path, "Nintendo");
335
}
336
337
// Copy over exosphere fatal for Mariko.
338
if (h_cfg.t210b01)
339
{
340
memset((void *)ATM_EXO_FATAL_ADDR, 0, ATM_EXO_FATAL_SIZE);
341
if (ctxt->exofatal)
342
memcpy((void *)ATM_EXO_FATAL_ADDR, ctxt->exofatal, ctxt->exofatal_size);
343
344
// Set display id.
345
exo_cfg->display_id = display_get_decoded_panel_id();
346
}
347
348
#ifdef DEBUG_UART_PORT
349
// Ovverride logging parameters if set in compile time.
350
if (!ctxt->stock)
351
{
352
exo_cfg->uart_port = DEBUG_UART_PORT;
353
exo_cfg->uart_invert = DEBUG_UART_INVERT;
354
exo_cfg->uart_baudrate = DEBUG_UART_BAUDRATE;
355
}
356
#endif
357
}
358
359
static const char *get_error_desc(u32 error_desc)
360
{
361
switch (error_desc)
362
{
363
case 0x100:
364
return "IABRT"; // Instruction Abort.
365
case 0x101:
366
return "DABRT"; // Data Abort.
367
case 0x102:
368
return "IUA"; // Instruction Unaligned Access.
369
case 0x103:
370
return "DUA"; // Data Unaligned Access.
371
case 0x104:
372
return "UDF"; // Undefined Instruction.
373
case 0x106:
374
return "SYS"; // System Error.
375
case 0x301:
376
return "SVC"; // Bad arguments or unimplemented SVC.
377
case 0xF00:
378
return "KRNL"; // Kernel panic.
379
case 0xFFD:
380
return "SO"; // Stack Overflow.
381
case 0xFFE:
382
return "std::abort";
383
default:
384
return "UNK";
385
}
386
}
387
388
#define HOS_PID_BOOT2 0x8
389
390
void secmon_exo_check_panic()
391
{
392
volatile atm_fatal_error_ctx *rpt = (atm_fatal_error_ctx *)ATM_FATAL_ERR_CTX_ADDR;
393
394
// Mask magic to maintain compatibility with any AFE version, thanks to additive struct members.
395
if ((rpt->magic & 0xF0FFFFFF) != ATM_FATAL_MAGIC)
396
return;
397
398
gfx_clear_grey(0x1B);
399
gfx_con_setpos(0, 0);
400
401
WPRINTF("Panic occurred while running Atmosphere.\n\n");
402
WPRINTFARGS("Title ID: %08X%08X", (u32)((u64)rpt->title_id >> 32), (u32)rpt->title_id);
403
WPRINTFARGS("Error: %s (0x%x)\n", get_error_desc(rpt->error_desc), rpt->error_desc);
404
405
// Check if mixed atmosphere sysmodules.
406
if ((u32)rpt->title_id == HOS_PID_BOOT2)
407
WPRINTF("Mismatched Atmosphere files?\n");
408
409
// Save context to the SD card.
410
char filepath[0x40];
411
f_mkdir("atmosphere/fatal_errors");
412
strcpy(filepath, "atmosphere/fatal_errors/report_");
413
itoa((u32)((u64)rpt->report_identifier >> 32), filepath + strlen(filepath), 16);
414
itoa((u32)(rpt->report_identifier), filepath + strlen(filepath), 16);
415
strcat(filepath, ".bin");
416
417
if (!sd_save_to_file((void *)rpt, sizeof(atm_fatal_error_ctx), filepath))
418
{
419
gfx_con.fntsz = 8;
420
WPRINTFARGS("Report saved to %s\n", filepath);
421
gfx_con.fntsz = 16;
422
}
423
424
// Change magic to invalid, to prevent double-display of error/bootlooping.
425
rpt->magic = 0;
426
427
gfx_printf("\n\nPress POWER to continue.\n");
428
429
display_backlight_brightness(100, 1000);
430
msleep(1000);
431
432
while (!(btn_wait() & BTN_POWER))
433
;
434
435
display_backlight_brightness(0, 1000);
436
gfx_con_setpos(0, 0);
437
}
438
439