From d7aed433339bbfc564deab00ff8a08a6c3c3f18f Mon Sep 17 00:00:00 2001 From: "fix94.1" Date: Sun, 30 Mar 2014 21:45:47 +0000 Subject: [PATCH] -changed up some booting code to hopefully fix those random freezes on game and homebrew bootup --- resources/extldr/crt0.s | 74 +++++++++++++++++++++++++++++++----- resources/extldr/link.ld | 2 +- resources/extldr/main.c | 4 +- resources/extldr/string.c | 10 ++--- resources/extldr/string.h | 6 +-- source/homebrew/homebrew.cpp | 33 +++++++++++++--- 6 files changed, 99 insertions(+), 30 deletions(-) diff --git a/resources/extldr/crt0.s b/resources/extldr/crt0.s index 47cc2509..82d1aaa2 100644 --- a/resources/extldr/crt0.s +++ b/resources/extldr/crt0.s @@ -1,22 +1,76 @@ # Copyright 2008-2009 Segher Boessenkool # This code is licensed to you under the terms of the GNU GPL, version 2; # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt +.set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4; +.set r5,5; .set r6,6; .set r7,7; .set r8,8; .set r9,9; +.set r10,10; .set r11,11; .set r12,12; .set r13,13; .set r14,14; +.set r15,15; .set r16,16; .set r17,17; .set r18,18; .set r19,19; +.set r20,20; .set r21,21; .set r22,22; .set r23,23; .set r24,24; +.set r25,25; .set r26,26; .set r27,27; .set r28,28; .set r29,29; +.set r30,30; .set r31,31; .extern _main .globl _start _start: + # Disable interrupts + mfmsr r3 + rlwinm r3,r3,0,17,15 + mtmsr r3 + isync - # Disable interrupts, enable FP. - mfmsr 3 ; rlwinm 3,3,0,17,15 ; ori 3,3,0x2000 ; mtmsr 3 ; isync - - # Setup stack. - lis 1,_stack_top@ha ; addi 1,1,_stack_top@l ; li 0,0 ; stwu 0,-64(1) + # Reset our registers + bl __init_registers # Clear BSS. - lis 3,__bss_start@ha ; addi 3,3,__bss_start@l - li 4,0 - lis 5,__bss_end@ha ; addi 5,5,__bss_end@l ; sub 5,5,3 - bl memset + lis r3,__bss_start@h + ori r3,r3,__bss_start@l + li r4,0 + lis r5,__bss_end@h + ori r5,r5,__bss_end@l + subf r5,r3,r5 + bl _memset # Go! - bl _main + bl _main + +#thanks to megazig for that +__init_registers: + li r0,0 + li r3,0 + li r4,0 + li r5,0 + li r6,0 + li r7,0 + li r8,0 + li r9,0 + li r10,0 + li r11,0 + li r12,0 + li r14,0 + li r15,0 + li r16,0 + li r17,0 + li r18,0 + li r19,0 + li r20,0 + li r21,0 + li r22,0 + li r23,0 + li r24,0 + li r25,0 + li r26,0 + li r27,0 + li r28,0 + li r29,0 + li r30,0 + li r31,0 + lis r1,__stack_top@h + ori r1,r1,__stack_top@l + addi r1,r1,-4 + stw r0,0(r1) + stwu r1,-0x38(r1) + lis r2,0 + ori r2,r2,0x8000 + lis r13,0 + ori r13,r13,0x8000 + blr diff --git a/resources/extldr/link.ld b/resources/extldr/link.ld index d144f557..fc419114 100644 --- a/resources/extldr/link.ld +++ b/resources/extldr/link.ld @@ -22,6 +22,6 @@ SECTIONS { . = ALIGN(0x40); .stack : { . += 0x8000; - _stack_top = .; + __stack_top = .; } } diff --git a/resources/extldr/main.c b/resources/extldr/main.c index a9739fdc..0c80f026 100644 --- a/resources/extldr/main.c +++ b/resources/extldr/main.c @@ -1,5 +1,3 @@ -#include -#include #include "string.h" #include "sync.h" #include "usbgecko.h" @@ -11,7 +9,7 @@ void _main(void) { usbgecko_init(); gprintf("Copying External Booter...\n"); - memcpy(start, buffer, 0xF0000); //960kb safe copying of booter + _memcpy(start, buffer, 0xF0000); //960kb safe copying of booter sync_before_exec(start, 0xF0000); gprintf("Done! Jumping to Entrypoint...\n"); exeEntryPoint(); diff --git a/resources/extldr/string.c b/resources/extldr/string.c index 016396b8..bc4d00d5 100644 --- a/resources/extldr/string.c +++ b/resources/extldr/string.c @@ -2,11 +2,9 @@ // This code is licensed to you under the terms of the GNU GPL, version 2; // see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -#include - -void *memset(void *b, int c, size_t len) +void * _memset(void *b, int c, unsigned int len) { - size_t i; + unsigned int i; for (i = 0; i < len; i++) ((unsigned char *)b)[i] = c; @@ -14,9 +12,9 @@ void *memset(void *b, int c, size_t len) return b; } -void *memcpy(void *dst, const void *src, size_t len) +void * _memcpy(void *dst, const void *src, unsigned int len) { - size_t i; + unsigned int i; for (i = 0; i < len; i++) ((unsigned char *)dst)[i] = ((unsigned char *)src)[i]; diff --git a/resources/extldr/string.h b/resources/extldr/string.h index 4df2aed8..bbbf4b92 100644 --- a/resources/extldr/string.h +++ b/resources/extldr/string.h @@ -1,9 +1,7 @@ #ifndef STRING_H_ #define STRING_H_ -#include - -void *memcpy(void *dst, const void *src, size_t len); -void *memset(void *b, int c, size_t len); +void * _memcpy(void *dst, const void *src, unsigned int len); +void * _memset(void *b, int c, unsigned int len); #endif diff --git a/source/homebrew/homebrew.cpp b/source/homebrew/homebrew.cpp index 9db572f8..62bf898e 100644 --- a/source/homebrew/homebrew.cpp +++ b/source/homebrew/homebrew.cpp @@ -51,9 +51,18 @@ void AddBootArgument(const char * argv) bool LoadAppBooter(const char *filepath) { - appbooter_ptr = fsop_ReadFile(filepath, &appbooter_size); - if(appbooter_size == 0 || appbooter_ptr == NULL) + u8 *tmp_ptr = fsop_ReadFile(filepath, &appbooter_size); + if(appbooter_size == 0 || tmp_ptr == NULL) return false; + appbooter_ptr = (u8*)MEM2_lo_alloc(appbooter_size); /* safety because upper mem2 is dol */ + if(appbooter_ptr == NULL) + { + free(tmp_ptr); + return false; + } + memcpy(appbooter_ptr, tmp_ptr, appbooter_size); + DCFlushRange(appbooter_ptr, appbooter_size); + free(tmp_ptr); return true; } @@ -134,9 +143,21 @@ void BootHomebrew() JumpToEntry(BOOTER_ENTRY); } +extern "C" { extern void __exception_closeall(); } +u32 AppEntrypoint = 0; void JumpToEntry(entry EntryPoint) { - gprintf("Jumping to %08x\n", EntryPoint); - SYS_ResetSystem(SYS_SHUTDOWN, 0, 0); - __lwp_thread_stopmultitasking(EntryPoint); -} + AppEntrypoint = (u32)EntryPoint; + gprintf("Jumping to %08x\n", AppEntrypoint); + u32 level = IRQ_Disable(); + __IOS_ShutdownSubsystems(); + __exception_closeall(); + asm volatile ( + "lis %r3, AppEntrypoint@h\n" + "ori %r3, %r3, AppEntrypoint@l\n" + "lwz %r3, 0(%r3)\n" + "mtlr %r3\n" + "blr\n" + ); + IRQ_Restore(level); +} \ No newline at end of file