From 3677d395a296297e6ad2419fd46e4a2e9d83e6d5 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Tue, 31 Jul 2018 13:53:23 +0200 Subject: [PATCH 1/4] Add bpc service header --- nx/include/switch/services/bpc.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 nx/include/switch/services/bpc.h diff --git a/nx/include/switch/services/bpc.h b/nx/include/switch/services/bpc.h new file mode 100644 index 00000000..96e6d870 --- /dev/null +++ b/nx/include/switch/services/bpc.h @@ -0,0 +1,14 @@ +/** + * @file bpc.h + * @brief Board power control (bpc) service IPC wrapper. + * @author XorTroll + * @copyright libnx Authors + */ +#pragma once +#include "../types.h" + +Result bpcInitialize(void); +void bpcExit(void); + +Result bpcShutdownSystem(void); +Result bpcRebootSystem(void); From 183ba3822b3a7ba830ff68e4e11b9c8ceda2b577 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Tue, 31 Jul 2018 13:57:00 +0200 Subject: [PATCH 2/4] Add bpc IPC commands --- nx/source/services/bpc.c | 86 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 nx/source/services/bpc.c diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c new file mode 100644 index 00000000..5e1dbe83 --- /dev/null +++ b/nx/source/services/bpc.c @@ -0,0 +1,86 @@ +#include "types.h" +#include "result.h" +#include "arm/atomics.h" +#include "kernel/ipc.h" +#include "services/bpc.h" +#include "services/sm.h" + +static Service g_bpcSrv; +static u64 g_refCnt; + +Result bpcInitialize(void) +{ + atomicIncrement64(&g_refCnt); + + if (serviceIsActive(&g_bpcSrv)) return 0; + + Result rc = 0; + + rc = smGetService(&g_bpcSrv, "bpc"); + + return rc; +} + +void apmExit(void) +{ + if (atomicDecrement64(&g_refCnt) == 0) + { + serviceClose(&g_bpcSrv); + } +} + +Result bpcShutdownSystem(void) +{ + IpcCommand c; + ipcInitialize(&c); + struct + { + u64 magic; + u64 cmd_id; + } * raw; + raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw->magic = SFCI_MAGIC; + raw->cmd_id = 0; + Result rc = 0; + rc = serviceIpcDispatch(&g_bpcSrv); + if(R_SUCCEEDED(rc)) + { + IpcParsedCommand r; + ipcParse(&r); + struct + { + u64 magic; + u64 result; + } *resp = r.Raw; + rc = resp->result; + } + return rc; +} + +Result bpcRebootSystem(void) +{ + IpcCommand c; + ipcInitialize(&c); + struct + { + u64 magic; + u64 cmd_id; + } * raw; + raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw->magic = SFCI_MAGIC; + raw->cmd_id = 1; + Result rc = 0; + rc = serviceIpcDispatch(&g_bpcSrv); + if(R_SUCCEEDED(rc)) + { + IpcParsedCommand r; + ipcParse(&r); + struct + { + u64 magic; + u64 result; + } *resp = r.Raw; + rc = resp->result; + } + return rc; +} From 0a77a95fae27ce6da31f380a2cbb19930da6f2e2 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Tue, 31 Jul 2018 13:57:22 +0200 Subject: [PATCH 3/4] Fix name typo --- nx/source/services/bpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 5e1dbe83..8622ff21 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -21,7 +21,7 @@ Result bpcInitialize(void) return rc; } -void apmExit(void) +void bpcExit(void) { if (atomicDecrement64(&g_refCnt) == 0) { From 6af9fb0592041c7bbc2097ddf4cbfe157d4c55dc Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Wed, 1 Aug 2018 00:49:49 +0200 Subject: [PATCH 4/4] Fix for 1.0.0 firmware --- nx/source/services/bpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 8622ff21..246d1202 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -15,8 +15,8 @@ Result bpcInitialize(void) if (serviceIsActive(&g_bpcSrv)) return 0; Result rc = 0; - - rc = smGetService(&g_bpcSrv, "bpc"); + if(!kernelAbove100()) rc = smGetService(&g_bpcSrv, "bpc:c"); + else rc = smGetService(&g_bpcSrv, "bpc"); return rc; }