From f2a1c60218d975bda088547246b1da0fe9788ec4 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 21 Aug 2021 22:20:44 -0700 Subject: [PATCH] fusee_cpp: tweaks, now completes SecureInitialize on hardware --- .../loader_stub/source/fusee_loader_main.cpp | 15 +++----- .../source/fusee_loader_uncompress.cpp | 4 ++- fusee_cpp/program/program.ld | 35 +++++++------------ fusee_cpp/program/source/fusee_crt0.cpp | 5 +-- fusee_cpp/program/source/fusee_start.s | 4 +-- libraries/config/arch/arm/cpu/arm7tdmi/cpu.mk | 2 +- 6 files changed, 24 insertions(+), 41 deletions(-) diff --git a/fusee_cpp/loader_stub/source/fusee_loader_main.cpp b/fusee_cpp/loader_stub/source/fusee_loader_main.cpp index f58d5e1bd..a50159c3e 100644 --- a/fusee_cpp/loader_stub/source/fusee_loader_main.cpp +++ b/fusee_cpp/loader_stub/source/fusee_loader_main.cpp @@ -26,16 +26,11 @@ namespace ams::nxboot::loader { constexpr size_t ProgramImageSizeMax = ProgramImageEnd - ProgramImageBase; void CopyBackwards(void *dst, const void *src, size_t size) { - /* We want to copy 32-bits at a time from destination to source. */ - const size_t words = util::DivideUp(size, sizeof(u32)); + u8 *dst_8 = static_cast(dst) + size; + const u8 *src_8 = static_cast(src) + size; - /* Convert to 32-bit pointers. */ - u32 *dst_32 = static_cast(dst) + words; - const u32 *src_32 = static_cast(src) + words; - - /* Copy data. */ - for (size_t i = 0; i < words; ++i) { - *(--dst_32) = *(--src_32); + for (size_t i = 0; i < size; ++i) { + *(--dst_8) = *(--src_8); } } @@ -43,7 +38,7 @@ namespace ams::nxboot::loader { NORETURN void UncompressAndExecute(const void *program, size_t program_size) { /* Relocate the compressed binary to a place where we can safely decompress it. */ - void *relocated_program = reinterpret_cast(util::AlignDown(ProgramImageEnd - program_size, sizeof(u32))); + void *relocated_program = reinterpret_cast(ProgramImageEnd - program_size); if (relocated_program != program) { CopyBackwards(relocated_program, program, program_size); } diff --git a/fusee_cpp/loader_stub/source/fusee_loader_uncompress.cpp b/fusee_cpp/loader_stub/source/fusee_loader_uncompress.cpp index 6926c2046..68742320c 100644 --- a/fusee_cpp/loader_stub/source/fusee_loader_uncompress.cpp +++ b/fusee_cpp/loader_stub/source/fusee_loader_uncompress.cpp @@ -87,7 +87,9 @@ namespace ams::nxboot::loader { } 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->src_offset += size; } diff --git a/fusee_cpp/program/program.ld b/fusee_cpp/program/program.ld index 53c7167a1..89bd75227 100644 --- a/fusee_cpp/program/program.ld +++ b/fusee_cpp/program/program.ld @@ -11,8 +11,10 @@ SECTIONS .crt0 : { + FILL(0x00000000) KEEP (*(.crt0 .crt0.*)) - . = ALIGN(8); + . = ORIGIN(main) + 0xC0 - 1; + BYTE(00); } >main AT>glob .text : @@ -22,37 +24,31 @@ SECTIONS *(.text.startup .text.startup.*) *(.text.hot .text.hot.*) *(.text .stub .text.* .gnu.linkonce.t.*) - . = ALIGN(8); } >main AT>glob .init : { KEEP( *(.init) ) - . = ALIGN(8); } >main AT>glob .plt : { *(.plt) *(.iplt) - . = ALIGN(8); } >main AT>glob .fini : { KEEP( *(.fini) ) - . = ALIGN(8); } >main AT>glob /* =========== RODATA section =========== */ - . = ALIGN(8); __rodata_start = . ; .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) - . = ALIGN(8); } >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 /* =========== DATA section =========== */ - . = ALIGN(8); __data_start = . ; .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 .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) } >main AT>glob - .preinit_array ALIGN(8) : + .preinit_array : { PROVIDE (__preinit_array_start = .); KEEP (*(.preinit_array)) PROVIDE (__preinit_array_end = .); } >main AT>glob - .init_array ALIGN(8) : + .init_array : { PROVIDE (__init_array_start = .); KEEP (*(SORT(.init_array.*))) @@ -86,7 +81,7 @@ SECTIONS PROVIDE (__init_array_end = .); } >main AT>glob - .fini_array ALIGN(8) : + .fini_array : { PROVIDE (__fini_array_start = .); KEEP (*(.fini_array)) @@ -94,7 +89,7 @@ SECTIONS PROVIDE (__fini_array_end = .); } >main AT>glob - .ctors ALIGN(8) : + .ctors : { KEEP (*crtbegin.o(.ctors)) /* MUST be first -- GCC requires it */ KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) @@ -102,7 +97,7 @@ SECTIONS KEEP (*(.ctors)) } >main AT>glob - .dtors ALIGN(8) : + .dtors : { KEEP (*crtbegin.o(.dtors)) KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) @@ -117,30 +112,26 @@ SECTIONS __got_end__ = .; - .data ALIGN(8) : + .data : { *(.data .data.* .gnu.linkonce.d.*) SORT(CONSTRUCTORS) } >main AT>glob __bss_start__ = .; - .bss ALIGN(8) : + .main.fill : { + FILL(0x00000000) *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) - . = ALIGN(16); + . = ORIGIN(main) + LENGTH(main) - 1; + BYTE(0x00); } >main AT>glob __bss_end__ = .; __main_end__ = ABSOLUTE(.) ; - .main.fill : { - FILL(0x00000000) - . = ORIGIN(main) + LENGTH(main) - 1; - BYTE(0x00); - } >main AT>glob - /* ================== ==== Metadata ==== ================== */ diff --git a/fusee_cpp/program/source/fusee_crt0.cpp b/fusee_cpp/program/source/fusee_crt0.cpp index 58cad7488..6e5d7adaf 100644 --- a/fusee_cpp/program/source/fusee_crt0.cpp +++ b/fusee_cpp/program/source/fusee_crt0.cpp @@ -28,7 +28,7 @@ namespace ams::nxboot::crt0 { } - void Initialize(uintptr_t bss_start, uintptr_t bss_end) { + void Initialize() { /* TODO: Collect timing information? */ /* Setup exception vectors. */ @@ -43,9 +43,6 @@ namespace ams::nxboot::crt0 { SetExceptionVector(7, reinterpret_cast(::ams::nxboot::ExceptionHandler7)); } - /* Clear bss. */ - std::memset(reinterpret_cast(bss_start), 0, bss_end - bss_start); - /* Call init array. */ __libc_init_array(); } diff --git a/fusee_cpp/program/source/fusee_start.s b/fusee_cpp/program/source/fusee_start.s index d1af31440..978c9d1e6 100644 --- a/fusee_cpp/program/source/fusee_start.s +++ b/fusee_cpp/program/source/fusee_start.s @@ -62,9 +62,7 @@ _ZN3ams6nxboot5StartEv: ldr lr, =_ZN3ams6nxboot16ExceptionHandlerEv /* Perform runtime initialization. */ - ldr r0, =__bss_start__ - ldr r1, =__bss_end__ - bl _ZN3ams6nxboot4crt010InitializeEjj + bl _ZN3ams6nxboot4crt010InitializeEv /* Perform nx boot procedure. */ bl _ZN3ams6nxboot4MainEv diff --git a/libraries/config/arch/arm/cpu/arm7tdmi/cpu.mk b/libraries/config/arch/arm/cpu/arm7tdmi/cpu.mk index d50dd0607..5e3faf31c 100644 --- a/libraries/config/arch/arm/cpu/arm7tdmi/cpu.mk +++ b/libraries/config/arch/arm/cpu/arm7tdmi/cpu.mk @@ -1,5 +1,5 @@ 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_CXXFLAGS += export ATMOSPHERE_ASFLAGS += \ No newline at end of file