1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-17 17:36:44 +00:00

fusee_cpp: tweaks, now completes SecureInitialize on hardware

This commit is contained in:
Michael Scire 2021-08-21 22:20:44 -07:00 committed by SciresM
parent c91f95e8f6
commit f2a1c60218
6 changed files with 24 additions and 41 deletions

View file

@ -26,16 +26,11 @@ namespace ams::nxboot::loader {
constexpr size_t ProgramImageSizeMax = ProgramImageEnd - ProgramImageBase; constexpr size_t ProgramImageSizeMax = ProgramImageEnd - ProgramImageBase;
void CopyBackwards(void *dst, const void *src, size_t size) { void CopyBackwards(void *dst, const void *src, size_t size) {
/* We want to copy 32-bits at a time from destination to source. */ u8 *dst_8 = static_cast<u8 *>(dst) + size;
const size_t words = util::DivideUp(size, sizeof(u32)); const u8 *src_8 = static_cast<const u8 *>(src) + size;
/* Convert to 32-bit pointers. */ for (size_t i = 0; i < size; ++i) {
u32 *dst_32 = static_cast<u32 *>(dst) + words; *(--dst_8) = *(--src_8);
const u32 *src_32 = static_cast<const u32 *>(src) + words;
/* Copy data. */
for (size_t i = 0; i < words; ++i) {
*(--dst_32) = *(--src_32);
} }
} }
@ -43,7 +38,7 @@ namespace ams::nxboot::loader {
NORETURN void UncompressAndExecute(const void *program, size_t program_size) { NORETURN void UncompressAndExecute(const void *program, size_t program_size) {
/* Relocate the compressed binary to a place where we can safely decompress it. */ /* Relocate the compressed binary to a place where we can safely decompress it. */
void *relocated_program = reinterpret_cast<void *>(util::AlignDown(ProgramImageEnd - program_size, sizeof(u32))); void *relocated_program = reinterpret_cast<void *>(ProgramImageEnd - program_size);
if (relocated_program != program) { if (relocated_program != program) {
CopyBackwards(relocated_program, program, program_size); CopyBackwards(relocated_program, program, program_size);
} }

View file

@ -87,7 +87,9 @@ namespace ams::nxboot::loader {
} }
void Copy(size_t size) { void Copy(size_t size) {
__builtin_memcpy(this->dst + this->dst_offset, this->src + this->src_offset, size); for (size_t i = 0; i < size; ++i) {
this->dst[this->dst_offset + i] = this->src[this->src_offset + i];
}
this->dst_offset += size; this->dst_offset += size;
this->src_offset += size; this->src_offset += size;
} }

View file

@ -11,8 +11,10 @@ SECTIONS
.crt0 : .crt0 :
{ {
FILL(0x00000000)
KEEP (*(.crt0 .crt0.*)) KEEP (*(.crt0 .crt0.*))
. = ALIGN(8); . = ORIGIN(main) + 0xC0 - 1;
BYTE(00);
} >main AT>glob } >main AT>glob
.text : .text :
@ -22,37 +24,31 @@ SECTIONS
*(.text.startup .text.startup.*) *(.text.startup .text.startup.*)
*(.text.hot .text.hot.*) *(.text.hot .text.hot.*)
*(.text .stub .text.* .gnu.linkonce.t.*) *(.text .stub .text.* .gnu.linkonce.t.*)
. = ALIGN(8);
} >main AT>glob } >main AT>glob
.init : .init :
{ {
KEEP( *(.init) ) KEEP( *(.init) )
. = ALIGN(8);
} >main AT>glob } >main AT>glob
.plt : .plt :
{ {
*(.plt) *(.plt)
*(.iplt) *(.iplt)
. = ALIGN(8);
} >main AT>glob } >main AT>glob
.fini : .fini :
{ {
KEEP( *(.fini) ) KEEP( *(.fini) )
. = ALIGN(8);
} >main AT>glob } >main AT>glob
/* =========== RODATA section =========== */ /* =========== RODATA section =========== */
. = ALIGN(8);
__rodata_start = . ; __rodata_start = . ;
.rodata : .rodata :
{ {
*(.rodata .rodata.* .gnu.linkonce.r.*) *(.rodata .rodata.* .gnu.linkonce.r.*)
. = ALIGN(8);
} >main AT>glob } >main AT>glob
.eh_frame_hdr : { __eh_frame_hdr_start = .; *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) __eh_frame_hdr_end = .; } >main AT>glob .eh_frame_hdr : { __eh_frame_hdr_start = .; *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) __eh_frame_hdr_end = .; } >main AT>glob
@ -63,7 +59,6 @@ SECTIONS
.hash : { *(.hash) } >main AT>glob .hash : { *(.hash) } >main AT>glob
/* =========== DATA section =========== */ /* =========== DATA section =========== */
. = ALIGN(8);
__data_start = . ; __data_start = . ;
.eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } >main AT>glob .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } >main AT>glob
@ -71,14 +66,14 @@ SECTIONS
.gnu_extab : ONLY_IF_RW { *(.gnu_extab*) } >main AT>glob .gnu_extab : ONLY_IF_RW { *(.gnu_extab*) } >main AT>glob
.exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) } >main AT>glob .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) } >main AT>glob
.preinit_array ALIGN(8) : .preinit_array :
{ {
PROVIDE (__preinit_array_start = .); PROVIDE (__preinit_array_start = .);
KEEP (*(.preinit_array)) KEEP (*(.preinit_array))
PROVIDE (__preinit_array_end = .); PROVIDE (__preinit_array_end = .);
} >main AT>glob } >main AT>glob
.init_array ALIGN(8) : .init_array :
{ {
PROVIDE (__init_array_start = .); PROVIDE (__init_array_start = .);
KEEP (*(SORT(.init_array.*))) KEEP (*(SORT(.init_array.*)))
@ -86,7 +81,7 @@ SECTIONS
PROVIDE (__init_array_end = .); PROVIDE (__init_array_end = .);
} >main AT>glob } >main AT>glob
.fini_array ALIGN(8) : .fini_array :
{ {
PROVIDE (__fini_array_start = .); PROVIDE (__fini_array_start = .);
KEEP (*(.fini_array)) KEEP (*(.fini_array))
@ -94,7 +89,7 @@ SECTIONS
PROVIDE (__fini_array_end = .); PROVIDE (__fini_array_end = .);
} >main AT>glob } >main AT>glob
.ctors ALIGN(8) : .ctors :
{ {
KEEP (*crtbegin.o(.ctors)) /* MUST be first -- GCC requires it */ KEEP (*crtbegin.o(.ctors)) /* MUST be first -- GCC requires it */
KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
@ -102,7 +97,7 @@ SECTIONS
KEEP (*(.ctors)) KEEP (*(.ctors))
} >main AT>glob } >main AT>glob
.dtors ALIGN(8) : .dtors :
{ {
KEEP (*crtbegin.o(.dtors)) KEEP (*crtbegin.o(.dtors))
KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
@ -117,30 +112,26 @@ SECTIONS
__got_end__ = .; __got_end__ = .;
.data ALIGN(8) : .data :
{ {
*(.data .data.* .gnu.linkonce.d.*) *(.data .data.* .gnu.linkonce.d.*)
SORT(CONSTRUCTORS) SORT(CONSTRUCTORS)
} >main AT>glob } >main AT>glob
__bss_start__ = .; __bss_start__ = .;
.bss ALIGN(8) : .main.fill :
{ {
FILL(0x00000000)
*(.dynbss) *(.dynbss)
*(.bss .bss.* .gnu.linkonce.b.*) *(.bss .bss.* .gnu.linkonce.b.*)
*(COMMON) *(COMMON)
. = ALIGN(16); . = ORIGIN(main) + LENGTH(main) - 1;
BYTE(0x00);
} >main AT>glob } >main AT>glob
__bss_end__ = .; __bss_end__ = .;
__main_end__ = ABSOLUTE(.) ; __main_end__ = ABSOLUTE(.) ;
.main.fill : {
FILL(0x00000000)
. = ORIGIN(main) + LENGTH(main) - 1;
BYTE(0x00);
} >main AT>glob
/* ================== /* ==================
==== Metadata ==== ==== Metadata ====
================== */ ================== */

View file

@ -28,7 +28,7 @@ namespace ams::nxboot::crt0 {
} }
void Initialize(uintptr_t bss_start, uintptr_t bss_end) { void Initialize() {
/* TODO: Collect timing information? */ /* TODO: Collect timing information? */
/* Setup exception vectors. */ /* Setup exception vectors. */
@ -43,9 +43,6 @@ namespace ams::nxboot::crt0 {
SetExceptionVector(7, reinterpret_cast<uintptr_t>(::ams::nxboot::ExceptionHandler7)); SetExceptionVector(7, reinterpret_cast<uintptr_t>(::ams::nxboot::ExceptionHandler7));
} }
/* Clear bss. */
std::memset(reinterpret_cast<void *>(bss_start), 0, bss_end - bss_start);
/* Call init array. */ /* Call init array. */
__libc_init_array(); __libc_init_array();
} }

View file

@ -62,9 +62,7 @@ _ZN3ams6nxboot5StartEv:
ldr lr, =_ZN3ams6nxboot16ExceptionHandlerEv ldr lr, =_ZN3ams6nxboot16ExceptionHandlerEv
/* Perform runtime initialization. */ /* Perform runtime initialization. */
ldr r0, =__bss_start__ bl _ZN3ams6nxboot4crt010InitializeEv
ldr r1, =__bss_end__
bl _ZN3ams6nxboot4crt010InitializeEjj
/* Perform nx boot procedure. */ /* Perform nx boot procedure. */
bl _ZN3ams6nxboot4MainEv bl _ZN3ams6nxboot4MainEv

View file

@ -1,5 +1,5 @@
export ATMOSPHERE_DEFINES += -DATMOSPHERE_CPU_ARM7TDMI export ATMOSPHERE_DEFINES += -DATMOSPHERE_CPU_ARM7TDMI
export ATMOSPHERE_SETTINGS += -march=armv4t -mtune=arm7tdmi -mthumb -mthumb-interwork export ATMOSPHERE_SETTINGS += -march=armv4t -mtune=arm7tdmi -mthumb -mthumb-interwork -fstrict-volatile-bitfields
export ATMOSPHERE_CFLAGS += export ATMOSPHERE_CFLAGS +=
export ATMOSPHERE_CXXFLAGS += export ATMOSPHERE_CXXFLAGS +=
export ATMOSPHERE_ASFLAGS += export ATMOSPHERE_ASFLAGS +=