detect: Detect firmware during lib init, inline version checks

This commit is contained in:
plutooo 2018-10-23 23:23:46 +02:00
parent ccfb1fd4d2
commit fa9f93342f
3 changed files with 35 additions and 52 deletions

View File

@ -7,15 +7,37 @@
#pragma once #pragma once
#include "../types.h" #include "../types.h"
extern bool g_IsAbove200;
extern bool g_IsAbove300;
extern bool g_IsAbove400;
extern bool g_IsAbove500;
extern bool g_IsAbove600;
/// Returns true if the kernel version is equal to or above 2.0.0. /// Returns true if the kernel version is equal to or above 2.0.0.
bool kernelAbove200(void); static inline bool kernelAbove200(void) {
return g_IsAbove200;
}
/// Returns true if the kernel version is equal to or above 3.0.0. /// Returns true if the kernel version is equal to or above 3.0.0.
bool kernelAbove300(void); static inline bool kernelAbove300(void) {
return g_IsAbove300;
}
/// Returns true if the kernel version is equal to or above 4.0.0. /// Returns true if the kernel version is equal to or above 4.0.0.
bool kernelAbove400(void); static inline bool kernelAbove400(void) {
return g_IsAbove400;
}
/// Returns true if the kernel version is equal to or above 5.0.0. /// Returns true if the kernel version is equal to or above 5.0.0.
bool kernelAbove500(void); static inline bool kernelAbove500(void) {
return g_IsAbove500;
}
/// Returns true if the kernel version is equal to or above 6.0.0. /// Returns true if the kernel version is equal to or above 6.0.0.
bool kernelAbove600(void); static inline bool kernelAbove600(void) {
return g_IsAbove600;
}
/// Returns true if the process has a debugger attached. /// Returns true if the process has a debugger attached.
bool detectDebugger(void); bool detectDebugger(void);

View File

@ -4,26 +4,14 @@
#include "kernel/mutex.h" #include "kernel/mutex.h"
#include "kernel/svc.h" #include "kernel/svc.h"
static bool g_IsAbove200; bool g_IsAbove200;
static bool g_IsAbove300; bool g_IsAbove300;
static bool g_IsAbove400; bool g_IsAbove400;
static bool g_IsAbove500; bool g_IsAbove500;
static bool g_IsAbove600; bool g_IsAbove600;
static bool g_HasCached = 0;
static Mutex g_Mutex;
static void _CacheValues(void) void detectSetup(void)
{ {
if (__atomic_load_n(&g_HasCached, __ATOMIC_SEQ_CST))
return;
mutexLock(&g_Mutex);
if (g_HasCached) {
mutexUnlock(&g_Mutex);
return;
}
u64 tmp; u64 tmp;
g_IsAbove200 = (svcGetInfo(&tmp, 12, INVALID_HANDLE, 0) != 0xF001); g_IsAbove200 = (svcGetInfo(&tmp, 12, INVALID_HANDLE, 0) != 0xF001);
g_IsAbove300 = (svcGetInfo(&tmp, 18, INVALID_HANDLE, 0) != 0xF001); g_IsAbove300 = (svcGetInfo(&tmp, 18, INVALID_HANDLE, 0) != 0xF001);
@ -35,35 +23,6 @@ static void _CacheValues(void)
g_IsAbove400 |= g_IsAbove500; g_IsAbove400 |= g_IsAbove500;
g_IsAbove300 |= g_IsAbove400; g_IsAbove300 |= g_IsAbove400;
g_IsAbove200 |= g_IsAbove300; g_IsAbove200 |= g_IsAbove300;
__atomic_store_n(&g_HasCached, true, __ATOMIC_SEQ_CST);
mutexUnlock(&g_Mutex);
}
bool kernelAbove200(void) {
_CacheValues();
return g_IsAbove200;
}
bool kernelAbove300(void) {
_CacheValues();
return g_IsAbove300;
}
bool kernelAbove400(void) {
_CacheValues();
return g_IsAbove400;
}
bool kernelAbove500(void) {
_CacheValues();
return g_IsAbove500;
}
bool kernelAbove600(void) {
_CacheValues();
return g_IsAbove600;
} }
bool detectDebugger(void) { bool detectDebugger(void) {

View File

@ -11,6 +11,7 @@
void* __stack_top; void* __stack_top;
void NORETURN __nx_exit(Result rc, LoaderReturnFn retaddr); void NORETURN __nx_exit(Result rc, LoaderReturnFn retaddr);
void detectSetup(void);
void virtmemSetup(void); void virtmemSetup(void);
void newlibSetup(void); void newlibSetup(void);
void argvSetup(void); void argvSetup(void);
@ -139,6 +140,7 @@ void __attribute__((weak)) __libnx_init(void* ctx, Handle main_thread, void* sav
// Called by crt0. // Called by crt0.
// Libnx initialization goes here. // Libnx initialization goes here.
detectSetup();
envSetup(ctx, main_thread, saved_lr); envSetup(ctx, main_thread, saved_lr);
newlibSetup(); newlibSetup();
virtmemSetup(); virtmemSetup();