Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kronos2308 committed Jul 22, 2021
1 parent aa137dd commit 365817d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 68 deletions.
31 changes: 0 additions & 31 deletions .github/workflows/main.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ INCLUDES := include
EXEFS_SRC := exefs_src
APP_TITLE := Haku33
APP_AUTHOR := Kronos2308
APP_VERSION := 3.8
APP_VERSION := 3.9
ROMFS := romfs

#---------------------------------------------------------------------------------
Expand Down
90 changes: 54 additions & 36 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
#define IRAM_PAYLOAD_MAX_SIZE 0x24000
static u8 g_reboot_payload[IRAM_PAYLOAD_MAX_SIZE];
char Logs[2024];
bool isXSOS;

extern "C" {
#include "ams_bpc.h"
#include "reboot.h"
}

using namespace std;
Expand Down Expand Up @@ -64,7 +66,18 @@ void copy_me(string origen, string destino) {
dest.close();
}

static void reboot_to_payload(void) {
bool txIsAvailable() {
auto srv_name = smEncodeName("tx");
Handle tmph = 0;
auto rc = smRegisterService(&tmph, srv_name, false, 1);
if(R_FAILED(rc)) {
return true;
}
smUnregisterService(srv_name);
return false;
}

static void reboot_to_payload_AMS(void) {
Result rc = amsBpcSetRebootPayload(g_reboot_payload, IRAM_PAYLOAD_MAX_SIZE);
if (R_FAILED(rc)) {
sprintf(Logs," Failed to set reboot payload: 0x%x\n", rc);
Expand All @@ -84,7 +97,7 @@ void CheckHardware()
}
}

bool can_reboot = true;
bool can_reboot_AMS = true;
void SetupClean (){
if (is_patched){
led_on(1);
Expand All @@ -102,7 +115,11 @@ void SetupClean (){
copy_me("romfs:/startup.te", "/startup.te");
copy_me("romfs:/poweroff.bin", "/poweroff.bin");
copy_me("romfs:/TegraExplorer.bin", "/Haku33.bin");
if (can_reboot) { reboot_to_payload();}
if (isXSOS){
bpcInitialize();
if(init_slp()){reboot_to_payload();}
bpcExit();
}else if (can_reboot_AMS) { reboot_to_payload_AMS();}
}
}

Expand All @@ -118,42 +135,43 @@ int main(int argc, char **argv)
padInitializeDefault(&pad);

Result rc = 0;
bool isXSOS = txIsAvailable();
if (!isXSOS){
if (R_FAILED(rc = setsysInitialize())) {
sprintf(Logs," Failed to initialize set:sys: 0x%x\n", rc);
can_reboot_AMS = false;
}
else {
if (is_patched) {
sprintf(Logs,LG.text8 );
can_reboot_AMS = false;
}
}

if (R_FAILED(rc = setsysInitialize())) {
sprintf(Logs," Failed to initialize set:sys: 0x%x\n", rc);
can_reboot = false;
}
else {
if (is_patched) {
sprintf(Logs,LG.text8 );
can_reboot = false;
}
}

if (can_reboot && R_FAILED(rc = spsmInitialize())) {
sprintf(Logs," Failed to initialize spsm: 0x%x\n", rc);
can_reboot = false;
}
if (can_reboot_AMS && R_FAILED(rc = spsmInitialize())) {
sprintf(Logs," Failed to initialize spsm: 0x%x\n", rc);
can_reboot_AMS = false;
}

if (can_reboot) {
smExit(); //Required to connect to ams:bpc
if R_FAILED(rc = amsBpcInitialize()) {
sprintf(Logs," Failed to initialize ams:bpc: 0x%x\n", rc);
can_reboot = false;
}
}
if (can_reboot_AMS) {
smExit(); //Required to connect to ams:bpc
if R_FAILED(rc = amsBpcInitialize()) {
sprintf(Logs," Failed to initialize ams:bpc: 0x%x\n", rc);
can_reboot_AMS = false;
}
}

if (can_reboot) {
FILE *f = fopen("romfs:/TegraExplorer.bin", "rb");
if (f == NULL) {
sprintf(Logs," Failed to open atmosphere/romfs:/TegraExplorer.bin!\n");
can_reboot = false;
} else {
fread(g_reboot_payload, 1, sizeof(g_reboot_payload), f);
fclose(f);
}
}

if (can_reboot_AMS) {
FILE *f = fopen("romfs:/TegraExplorer.bin", "rb");
if (f == NULL) {
sprintf(Logs," Failed to open atmosphere/romfs:/TegraExplorer.bin!\n");
can_reboot_AMS = false;
} else {
fread(g_reboot_payload, 1, sizeof(g_reboot_payload), f);
fclose(f);
}
}
}
//keys
while (appletMainLoop())
{
Expand Down
78 changes: 78 additions & 0 deletions source/reboot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

#include <switch.h>
#include "reboot.h"

#define IRAM_PAYLOAD_MAX_SIZE 0x2F000
#define IRAM_PAYLOAD_BASE 0x40010000

static alignas(0x1000) u8 g_reboot_payload[IRAM_PAYLOAD_MAX_SIZE];
static alignas(0x1000) u8 g_ff_page[0x1000];
static alignas(0x1000) u8 g_work_page[0x1000];

void do_iram_dram_copy(void *buf, uintptr_t iram_addr, size_t size, int option) {
memcpy(g_work_page, buf, size);

SecmonArgs args = {0};
args.X[0] = 0xF0000201; /* smcAmsIramCopy */
args.X[1] = (uintptr_t)g_work_page; /* DRAM Address */
args.X[2] = iram_addr; /* IRAM Address */
args.X[3] = size; /* Copy size */
args.X[4] = option; /* 0 = Read, 1 = Write */
svcCallSecureMonitor(&args);

memcpy(buf, g_work_page, size);
}

void copy_to_iram(uintptr_t iram_addr, void *buf, size_t size) {
do_iram_dram_copy(buf, iram_addr, size, 1);
}

void copy_from_iram(void *buf, uintptr_t iram_addr, size_t size) {
do_iram_dram_copy(buf, iram_addr, size, 0);
}

static void clear_iram(void) {
memset(g_ff_page, 0xFF, sizeof(g_ff_page));
for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += sizeof(g_ff_page)) {
copy_to_iram(IRAM_PAYLOAD_BASE + i, g_ff_page, sizeof(g_ff_page));
}
}

void reboot_to_payload(void) {
clear_iram();

for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += 0x1000) {
copy_to_iram(IRAM_PAYLOAD_BASE + i, &g_reboot_payload[i], 0x1000);
}

splSetConfig((SplConfigItem)65001, 2);
}

bool init_slp()
{
bool can_reboot = true;
Result rc = splInitialize();
if (R_FAILED(rc)) {
can_reboot = false;
} else {
FILE *f = fopen("romfs:/TegraExplorer.bin", "rb");
if (f == NULL) {
can_reboot = false;
} else {
fread(g_reboot_payload, 1, sizeof(g_reboot_payload), f);
fclose(f);
}
}
return can_reboot;
}

void exit_spl(bool can_reboot)
{
if (can_reboot) {
splExit();
}
return;
}
10 changes: 10 additions & 0 deletions source/reboot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <switch.h>

void reboot_to_payload(void);
bool init_slp(void);
void exit_spl(bool can_reboot);

0 comments on commit 365817d

Please sign in to comment.