1515 */
1616#include " payload.hpp"
1717
18- #include " ini.h"
1918#include " reboot_to_payload.h"
19+ #include " ams_bpc.h"
20+ #include " ini.h"
2021
2122#include < cstdio>
2223#include < cstring>
@@ -27,10 +28,26 @@ namespace Payload {
2728
2829 namespace {
2930
31+ void RebootToPayload () {
32+ /* Try reboot with safe ams bpc api. */
33+ Result rc = amsBpcInitialize ();
34+ if (R_SUCCEEDED (rc)) {
35+ rc = amsBpcSetRebootPayload (g_reboot_payload, IRAM_PAYLOAD_MAX_SIZE);
36+ if (R_SUCCEEDED (rc)) {
37+ spsmShutdown (true );
38+ }
39+ amsBpcExit ();
40+ }
41+
42+ /* Fallback to old smc reboot to payload. */
43+ if (R_FAILED (rc))
44+ smc_reboot_to_payload ();
45+ }
46+
3047 int HekateConfigHandler (void *user, char const *section, char const *name, char const *value) {
3148 auto const list = reinterpret_cast <HekateConfigList *>(user);
3249
33- /* ignore pre-config and global config entries. */
50+ /* Ignore pre-config and global config entries. */
3451 if (section[0 ] == ' \0 ' || std::strcmp (section, " config" ) == 0 ) {
3552 return 1 ;
3653 }
@@ -193,7 +210,8 @@ namespace Payload {
193210 return res;
194211 }
195212
196- bool RebootToHekate () {
213+ template <typename ConfigureFunction>
214+ bool Reboot (ConfigureFunction func) {
197215 /* Load payload. */
198216 if (!LoadHekatePayload ())
199217 return false ;
@@ -204,55 +222,38 @@ namespace Payload {
204222 /* Clear boot storage. */
205223 std::memset (storage, 0 , sizeof (BootStorage));
206224
225+ /* Configure boot storage */
226+ func (storage);
227+
207228 /* Reboot */
208- reboot_to_payload ();
229+ RebootToPayload ();
209230
210231 return true ;
211232 }
212233
213- bool RebootToHekateConfig (HekateConfig const &config, bool const autoboot_list) {
214- /* Load payload. */
215- if (!LoadHekatePayload ())
216- return false ;
217-
218- /* Get boot storage pointer. */
219- auto const storage = reinterpret_cast <BootStorage *>(g_reboot_payload + BootStorageOffset);
220-
221- /* Clear boot storage. */
222- std::memset (storage, 0 , sizeof (BootStorage));
223-
224- /* Force autoboot and set boot id. */
225- storage->boot_cfg = BootCfg_ForceAutoBoot;
226- storage->autoboot = config.index ;
227- storage->autoboot_list = autoboot_list;
228-
229- /* Reboot */
230- reboot_to_payload ();
234+ bool RebootToHekate () {
235+ return Reboot ([&] (BootStorage *storage) {
236+ /* No-Op */
237+ });
238+ }
231239
232- return true ;
240+ bool RebootToHekateConfig (HekateConfig const &config, bool const autoboot_list) {
241+ return Reboot ([&] (BootStorage *storage) {
242+ /* Force autoboot and set boot id. */
243+ storage->boot_cfg = BootCfg_ForceAutoBoot;
244+ storage->autoboot = config.index ;
245+ storage->autoboot_list = autoboot_list;
246+ });
233247 }
234248
235249 bool RebootToHekateUMS (UmsTarget const target) {
236- /* Load payload. */
237- if (!LoadHekatePayload ())
238- return false ;
239-
240- /* Get boot storage pointer. */
241- auto const storage = reinterpret_cast <BootStorage *>(g_reboot_payload + BootStorageOffset);
242-
243- /* Clear boot storage. */
244- std::memset (storage, 0 , sizeof (BootStorage));
245-
246- /* Force boot to menu, target UMS and select target. */
247- storage->boot_cfg = BootCfg_ForceAutoBoot;
248- storage->extra_cfg = ExtraCfg_NyxUms;
249- storage->autoboot = 0 ;
250- storage->ums = target;
251-
252- /* Reboot */
253- reboot_to_payload ();
254-
255- return true ;
250+ return Reboot ([&] (BootStorage *storage) {
251+ /* Force boot to menu, target UMS and select target. */
252+ storage->boot_cfg = BootCfg_ForceAutoBoot;
253+ storage->extra_cfg = ExtraCfg_NyxUms;
254+ storage->autoboot = 0 ;
255+ storage->ums = target;
256+ });
256257 }
257258
258259 bool RebootToPayload (PayloadConfig const &config) {
@@ -261,7 +262,7 @@ namespace Payload {
261262 return false ;
262263
263264 /* Reboot */
264- reboot_to_payload ();
265+ RebootToPayload ();
265266
266267 return true ;
267268 }
0 commit comments