mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 20:42:44 +02:00
119 lines
2.5 KiB
C
119 lines
2.5 KiB
C
#include "types.h"
|
|
#include "result.h"
|
|
#include "arm/atomics.h"
|
|
#include "kernel/ipc.h"
|
|
#include "kernel/detect.h"
|
|
#include "services/sm.h"
|
|
#include "services/ns.h"
|
|
|
|
static Service g_nsAppManSrv, g_nsGetterSrv;
|
|
static u64 g_nsRefCnt;
|
|
|
|
static Result _nsGetInterface(Service* srv_out, u64 cmd_id);
|
|
|
|
Result nsInitialize(void)
|
|
{
|
|
Result rc=0;
|
|
|
|
atomicIncrement64(&g_nsRefCnt);
|
|
|
|
if (serviceIsActive(&g_nsGetterSrv) || serviceIsActive(&g_nsAppManSrv))
|
|
return 0;
|
|
|
|
if(!kernelAbove300())
|
|
return smGetService(&g_nsAppManSrv, "ns:am");
|
|
|
|
rc = smGetService(&g_nsGetterSrv, "ns:am2");//TODO: Support the other services?(Only useful when ns:am2 isn't accessible)
|
|
if (R_FAILED(rc)) return rc;
|
|
|
|
rc = _nsGetInterface(&g_nsAppManSrv, 7996);
|
|
|
|
if (R_FAILED(rc)) serviceClose(&g_nsGetterSrv);
|
|
|
|
return rc;
|
|
}
|
|
|
|
void nsExit(void)
|
|
{
|
|
if (atomicDecrement64(&g_nsRefCnt) == 0) {
|
|
serviceClose(&g_nsAppManSrv);
|
|
if(!kernelAbove300()) return;
|
|
|
|
serviceClose(&g_nsGetterSrv);
|
|
}
|
|
}
|
|
|
|
static Result _nsGetInterface(Service* srv_out, u64 cmd_id) {
|
|
IpcCommand c;
|
|
ipcInitialize(&c);
|
|
|
|
struct {
|
|
u64 magic;
|
|
u64 cmd_id;
|
|
} *raw;
|
|
|
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
|
|
|
raw->magic = SFCI_MAGIC;
|
|
raw->cmd_id = cmd_id;
|
|
|
|
Result rc = serviceIpcDispatch(&g_nsGetterSrv);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
IpcParsedCommand r;
|
|
ipcParse(&r);
|
|
|
|
struct {
|
|
u64 magic;
|
|
u64 result;
|
|
} *resp = r.Raw;
|
|
|
|
rc = resp->result;
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
serviceCreate(srv_out, r.Handles[0]);
|
|
}
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
Result nsGetApplicationControlData(u8 flag, u64 titleID, NsApplicationControlData* buffer, size_t size, size_t* actual_size) {
|
|
IpcCommand c;
|
|
ipcInitialize(&c);
|
|
ipcAddRecvBuffer(&c, buffer, size, 0);
|
|
|
|
struct {
|
|
u64 magic;
|
|
u64 cmd_id;
|
|
u8 flag;
|
|
u64 titleID;
|
|
} *raw;
|
|
|
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
|
|
|
raw->magic = SFCI_MAGIC;
|
|
raw->cmd_id = 400;
|
|
raw->flag = flag;
|
|
raw->titleID = titleID;
|
|
|
|
Result rc = serviceIpcDispatch(&g_nsAppManSrv);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
IpcParsedCommand r;
|
|
ipcParse(&r);
|
|
|
|
struct {
|
|
u64 magic;
|
|
u64 result;
|
|
u64 actual_size;
|
|
} *resp = r.Raw;
|
|
|
|
rc = resp->result;
|
|
|
|
if (R_SUCCEEDED(rc) && actual_size) *actual_size = resp->actual_size;
|
|
}
|
|
|
|
return rc;
|
|
}
|