git subrepo pull --force emummc

subrepo:
  subdir:   "emummc"
  merged:   "2fc47cbb8"
upstream:
  origin:   "https://github.com/lulle2007200/emuMMC.git"
  branch:   "develop"
  commit:   "2fc47cbb8"
git-subrepo:
  version:  "0.4.9"
  origin:   "https://github.com/ingydotnet/git-subrepo.git"
  commit:   "30db3b8"
This commit is contained in:
lulle2007200
2025-05-12 14:22:12 +02:00
parent 0188898af4
commit 92c599f0f0
342 changed files with 106677 additions and 974 deletions

View File

@@ -16,7 +16,13 @@
*/
#include <string.h>
#include <switch/sf/service.h>
#include "fatal.h"
#include "../utils/util.h"
#if EMUMMC_HAS_FATAL_PAYLOAD
#include "fatal_handler_bin.h"
#endif
void __attribute__((noreturn)) fatal_abort(enum FatalReason abortReason)
{
@@ -24,17 +30,44 @@ void __attribute__((noreturn)) fatal_abort(enum FatalReason abortReason)
memset(&error_ctx, 0, sizeof(atmosphere_fatal_error_ctx));
// Basic error storage for Atmosphere
// TODO: Maybe include a small reboot2payload stub?
error_ctx.magic = ATMOSPHERE_REBOOT_TO_FATAL_MAGIC;
error_ctx.title_id = 0x0100000000000000; // FS
error_ctx.program_id = 0x0100000000000000; // FS
error_ctx.error_desc = abortReason;
// Copy fatal context
smcCopyToIram(ATMOSPHERE_FATAL_ERROR_ADDR, &error_ctx, sizeof(atmosphere_fatal_error_ctx));
// Try using bpc:ams to show fatal error
Handle h;
Service s;
Result rc = svcConnectToNamedPort(&h, "bpc:ams");
u32 retry_cnt = 20;
while (R_VALUE(rc) == KERNELRESULT(NotFound) && retry_cnt != 0) {
svcSleepThread(50000000ul);
rc = svcConnectToNamedPort(&h, "bpc:ams");
retry_cnt--;
}
// Reboot to RCM
if (R_SUCCEEDED(rc)){
serviceCreate(&s, h);
serviceDispatch(&s, 65000,
.buffer_attrs = { SfBufferAttr_In | SfBufferAttr_HipcMapAlias | SfBufferAttr_FixedSize },
.buffers = { { &error_ctx, 0x450 } }
);
}
// bpc:ams not available yet
// Copy error context to iram and reboot to fatal payload
memcpy(&working_buf, &error_ctx, sizeof(error_ctx));
smcCopyToIram(ATMOSPHERE_FATAL_ERROR_ADDR, &working_buf, sizeof(error_ctx));
#if EMUMMC_HAS_FATAL_PAYLOAD
for (size_t ofs = 0; ofs < fatal_handler_bin_size; ofs += 4096) {
memcpy(&working_buf, fatal_handler_bin + ofs, MIN(fatal_handler_bin_size - ofs, 4096));
smcCopyToIram(ATMOSPHERE_IRAM_PAYLOAD_BASE + ofs, &working_buf, MIN(fatal_handler_bin_size - ofs, 4096));
}
smcRebootToIramPayload();
#else
smcRebootToRcm();
#endif
while (true)
; // Should never be reached
while(true){}
}

View File

@@ -36,46 +36,45 @@ enum FatalReason
Fatal_FatfsFileOpen,
Fatal_FatfsMemExhaustion,
Fatal_InvalidEnum,
Fatal_InvalidPartition,
Fatal_PartitionSwitchFail,
Fatal_OOB,
Fatal_Max
};
#define AMS_FATAL_ERROR_MAX_STACKTRACE 0x20
#define AMS_FATAL_ERROR_MAX_STACKDUMP 0x100
/* Atmosphere reboot-to-fatal-error. */
typedef struct
{
uint32_t magic;
uint32_t error_desc;
uint64_t title_id;
typedef struct {
u32 magic;
u32 error_desc;
u64 program_id;
union {
uint64_t gprs[32];
struct
{
uint64_t _gprs[29];
uint64_t fp;
uint64_t lr;
uint64_t sp;
u64 gprs[32];
struct {
u64 _gprs[29];
u64 fp;
u64 lr;
u64 sp;
};
};
uint64_t pc;
uint64_t module_base;
uint32_t pstate;
uint32_t afsr0;
uint32_t afsr1;
uint32_t esr;
uint64_t far;
uint64_t report_identifier; /* Normally just system tick. */
uint64_t stack_trace_size;
uint64_t stack_dump_size;
uint64_t stack_trace[AMS_FATAL_ERROR_MAX_STACKTRACE];
uint8_t stack_dump[AMS_FATAL_ERROR_MAX_STACKDUMP];
} atmosphere_fatal_error_ctx;
u64 pc;
u64 module_base;
u32 pstate;
u32 afsr0;
u32 afsr1;
u32 esr;
u64 far;
u64 report_identifier; /* Normally just system tick. */
u64 stack_trace_size;
u64 stack_dump_size;
u64 stack_trace[0x20];
u8 stack_dump[0x100];
u8 tls[0x100];
}atmosphere_fatal_error_ctx;
/* "AFE1" */
#define ATMOSPHERE_REBOOT_TO_FATAL_MAGIC 0x31454641
/* "AFE0" */
#define ATMOSPHERE_REBOOT_TO_FATAL_MAGIC_0 0x30454641
/* "AFE2" */
#define ATMOSPHERE_REBOOT_TO_FATAL_MAGIC 0x32454641
#define ATMOSPHERE_IRAM_PAYLOAD_BASE 0x40010000
#define ATMOSPHERE_FATAL_ERROR_ADDR 0x4003E000
#define ATMOSPHERE_FATAL_ERROR_CONTEXT ((volatile atmosphere_fatal_error_ctx *)(ATMOSPHERE_FATAL_ERROR_ADDR))

View File

@@ -17,79 +17,22 @@
#ifndef _TYPES_H_
#define _TYPES_H_
#include <stdint.h>
#include <stdbool.h>
#include <switch/types.h>
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define OFFSET_OF(t, m) ((u32)&((t *)NULL)->m)
#define CONTAINER_OF(mp, t, mn) ((t *)((u32)mp - OFFSET_OF(t, mn)))
/// Creates a bitmask from a bit number.
#ifndef BIT
#define BIT(n) (1U<<(n))
#endif
typedef int8_t s8;
typedef int16_t s16;
typedef int16_t SHORT;
typedef int32_t s32;
typedef int32_t INT;
typedef int64_t LONG;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint8_t BYTE;
typedef uint16_t u16;
typedef int16_t SHORT;
typedef int64_t LONG;
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint16_t WCHAR;
typedef uint32_t u32;
typedef uint32_t UINT;
typedef uint32_t DWORD;
typedef uint64_t QWORD;
typedef uint64_t u64;
typedef volatile uint8_t vu8;
typedef volatile uint16_t vu16;
typedef volatile uint32_t vu32;
typedef u32 Handle; ///< Kernel object handle.
typedef u32 Result; ///< Function error code result type.
#define INVALID_HANDLE ((Handle) 0)
#define CUR_PROCESS_HANDLE ((Handle) 0xFFFF8001)
#define BOOT_CFG_AUTOBOOT_EN (1 << 0)
#define BOOT_CFG_FROM_LAUNCH (1 << 1)
#define BOOT_CFG_SEPT_RUN (1 << 7)
#define EXTRA_CFG_KEYS (1 << 0)
#define EXTRA_CFG_PAYLOAD (1 << 1)
#define EXTRA_CFG_MODULE (1 << 2)
typedef struct __attribute__((__packed__)) _boot_cfg_t
{
u8 boot_cfg;
u8 autoboot;
u8 autoboot_list;
u8 extra_cfg;
u8 rsvd[128];
} boot_cfg_t;
typedef struct __attribute__((__packed__)) _ipl_ver_meta_t
{
u32 magic;
u32 version;
u16 rsvd0;
u16 rsvd1;
} ipl_ver_meta_t;
typedef struct __attribute__((__packed__)) _reloc_meta_t
{
u32 start;
u32 stack;
u32 end;
u32 ep;
} reloc_meta_t;
#endif

View File

@@ -21,8 +21,24 @@
#include "fatal.h"
#include "types.h"
#include "../nx/counter.h"
#include "../nx/svc.h"
// #include "../nx/svc.h"
#include "../soc/t210.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <switch/kernel/svc.h>
_Alignas(4096) u8 working_buf[4096];
typedef struct _log_ctx
{
u32 magic;
u32 sz;
u32 start;
u32 end;
char buf[];
} log_ctx_t;
typedef struct _io_mapping_t
{
@@ -41,7 +57,7 @@ static inline uintptr_t _GetIoMapping(u64 io_addr, u64 io_size)
if (emuMMC_ctx.fs_ver >= FS_VER_10_0_0) {
u64 out_size;
if (svcQueryIoMapping(&vaddr, &out_size, aligned_addr, aligned_size) != 0) {
if (svcQueryMemoryMapping(&vaddr, &out_size, aligned_addr, aligned_size) != 0) {
fatal_abort(Fatal_IoMapping);
}
} else {
@@ -117,3 +133,119 @@ void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops)
for (u32 i = 0; i < num_ops; i++)
base[ops[i].off] = ops[i].val;
}
#define IRAM_LOG_CTX_ADDR 0x4003C000
#define IRAM_LOG_MAX_SZ 4096
void log_iram(const char* fmt, ...) {
static const u32 max_log_sz = sizeof(working_buf) - sizeof(log_ctx_t);
static bool init_done = false;
log_ctx_t *log_ctx = (log_ctx_t*)working_buf;
smcCopyFromIram(working_buf, IRAM_LOG_CTX_ADDR, sizeof(working_buf));
if(!init_done){
init_done = true;
log_ctx->buf[0] = '\0';
log_ctx->magic = 0xaabbccdd;
log_ctx->start = 0;
log_ctx->end = 0;
}
va_list args;
va_start(args, fmt);
int res = vsnprintf(log_ctx->buf + log_ctx->end, sizeof(working_buf) - sizeof(log_ctx_t) - log_ctx->end, fmt, args);
va_end(args);
if(res < 0 || log_ctx->start + res + 1 > max_log_sz) {
return;
}
log_ctx->end += res;
smcCopyToIram(IRAM_LOG_CTX_ADDR, working_buf, sizeof(working_buf));
// static const u32 max_log_sz = IRAM_LOG_MAX_SZ - sizeof(log_ctx_t);
// static u32 cur_log_offset = 0;
// static u32 start = 0;
// static bool init_done = false;
// if(!init_done){
// init_done = true;
// __attribute__((aligned(0x20))) log_ctx_t log_ctx;
// log_ctx.magic = 0xaabbccdd;
// log_ctx.sz = max_log_sz;
// log_ctx.start = 0;
// log_ctx.end = 0;
// smcCopyToIram(IRAM_LOG_CTX_ADDR, &log_ctx, sizeof(log_ctx_t));
// }
// if(working_buf[0] == '\x00') {
// return;
// }
// u32 len = strlen((char*)working_buf);
// u32 bytes_left = len + 1;
// if(cur_log_offset % 4) {
// char __attribute__((aligned(4))) prev[4] = {0};
// smcCopyFromIram(&prev, IRAM_LOG_CTX_ADDR + sizeof(log_ctx_t) + cur_log_offset, 4);
// uintptr_t target_addr = (uintptr_t)IRAM_LOG_CTX_ADDR + sizeof(log_ctx_t) + (cur_log_offset & ~3);
// u32 prev_len = cur_log_offset % 4;
// u32 prev_free = 4 - prev_len;
// u32 bytes_to_cpy = MIN(prev_free, bytes_left);
// memcpy(prev + prev_len, working_buf, bytes_to_cpy);
// bytes_left -= bytes_to_cpy;
// smcCopyToIram(target_addr, prev, 4);
// memmove(working_buf, working_buf + bytes_to_cpy, bytes_left);
// cur_log_offset += bytes_to_cpy;
// }
// if(cur_log_offset >= max_log_sz) {
// cur_log_offset = 0;
// start = cur_log_offset + 1;
// }
// char *cur = (char*)working_buf;
// while(bytes_left) {
// // cur_log_offset is now 4 byte aligned
// uintptr_t target_addr = (uintptr_t)IRAM_LOG_CTX_ADDR + sizeof(log_ctx_t) + cur_log_offset;
// u32 bytes_to_cpy = MIN(4096, bytes_left);
// bytes_to_cpy = MIN(bytes_to_cpy, 4096 - (target_addr % 4096));
// bytes_to_cpy = MIN(bytes_to_cpy, 4096 - (((uintptr_t)cur) % 4096));
// smcCopyToIram(target_addr, cur, bytes_to_cpy);
// bytes_left -= bytes_to_cpy;
// if(cur_log_offset >= max_log_sz) {
// cur_log_offset = 0;
// start = cur_log_offset + 1;
// }
// }
// __attribute__((aligned(0x20))) log_ctx_t log_ctx;
// log_ctx.magic = 0xaabbccdd;
// log_ctx.sz = max_log_sz;
// log_ctx.end = cur_log_offset;
// log_ctx.start = start;
// smcCopyToIram(IRAM_LOG_CTX_ADDR, &log_ctx, sizeof(log_ctx_t));
// if(cur_log_offset != 0) {
// cur_log_offset--;
// } else {
// cur_log_offset = max_log_sz - 1;
// }
}

View File

@@ -21,6 +21,12 @@
#include "types.h"
#include "../emuMMC/emummc_ctx.h"
// #define DEBUG_LOG_ARGS(fmt, ...) log_iram(fmt, __VA_ARGS__)
// #define DEBUG_LOG(fmt) log_iram(fmt)
#define DEBUG_LOG_ARGS(fmt, ...)
#define DEBUG_LOG(fmt)
intptr_t QueryIoMapping(u64 addr, u64 size);
#define byte_swap_32(num) (((num >> 24) & 0xff) | ((num << 8) & 0xff0000) | \
((num >> 8 )& 0xff00) | ((num << 24) & 0xff000000))
@@ -38,11 +44,9 @@ void usleep(u64 ticks);
void msleep(u64 milliseconds);
void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops);
static inline void *armGetTls(void) {
void *ret;
__asm__ __volatile__("MRS %x[data], TPIDRRO_EL0" : [data]"=r"(ret));
return ret;
}
void log_iram(const char* fmt, ...);
extern u8 working_buf[4096];
extern volatile emuMMC_ctx_t emuMMC_ctx;