From 2f277db81abe7b2fc02daf39fa1835dc88a9d98c Mon Sep 17 00:00:00 2001 From: profi200 Date: Sun, 7 Jan 2018 00:11:23 +0100 Subject: [PATCH] Deinit critical hardware before FIRM launch. --- include/system.h | 26 ++++++++++++++++++++++++++ source/arm11/firm.c | 2 ++ source/arm11/power.c | 2 ++ source/arm11/start.s | 9 ++++----- source/arm11/system.c | 9 +++++++-- source/arm9/firm.c | 2 ++ source/arm9/start.s | 6 ++++-- source/arm9/system.c | 8 +++++++- 8 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 include/system.h diff --git a/include/system.h b/include/system.h new file mode 100644 index 00000000..3435ffc0 --- /dev/null +++ b/include/system.h @@ -0,0 +1,26 @@ +#pragma once + +/* + * This file is part of fastboot 3DS + * Copyright (C) 2018 derrek, profi200 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "types.h" + + + +//void WEAK __systemInit(void); +void WEAK __systemDeinit(void); diff --git a/source/arm11/firm.c b/source/arm11/firm.c index 19f8f966..65fd174e 100644 --- a/source/arm11/firm.c +++ b/source/arm11/firm.c @@ -21,6 +21,7 @@ #include "mem_map.h" #include "arm11/start.h" #include "hardware/pxi.h" +#include "system.h" #include "ipc_handler.h" @@ -67,6 +68,7 @@ noreturn void firmLaunch(void) // Relocate ARM11 stub memcpy((void*)A11_STUB_ENTRY, (const void*)firmLaunchStub, A11_STUB_SIZE); + __systemDeinit(); deinitCpu(); // Change sp to a safe location diff --git a/source/arm11/power.c b/source/arm11/power.c index 6f709bf4..0422e741 100644 --- a/source/arm11/power.c +++ b/source/arm11/power.c @@ -25,6 +25,7 @@ #include "hardware/cache.h" #include "hardware/gfx.h" #include "hardware/pxi.h" +#include "system.h" #include "arm.h" @@ -37,6 +38,7 @@ static void power_safe_halt(void) // give the screens a bit of time to turn off TIMER_sleepMs(400); + __systemDeinit(); flushDCache(); __cpsid(aif); } diff --git a/source/arm11/start.s b/source/arm11/start.s index e022c5cb..7e814068 100644 --- a/source/arm11/start.s +++ b/source/arm11/start.s @@ -48,8 +48,9 @@ .extern setupMmu .extern __libc_init_array .extern core123Init -.extern systemInit +.extern __systemInit .extern main +.extern power_off .section ".crt0", "ax" @@ -142,14 +143,12 @@ _start_skip_bss_init_array: blx setupMmu bl setupVfp cpsie a - blx systemInit + blx __systemInit mov r0, #0 @ argc mov r1, #0 @ argv blx main - _start_lp: - wfi - b _start_lp + b power_off #define MAKE_BRANCH(src, dst) (0xEA000000 | (((((dst) - (src)) >> 2) - 2) & 0xFFFFFF)) diff --git a/source/arm11/system.c b/source/arm11/system.c index f82b7366..e73f0163 100644 --- a/source/arm11/system.c +++ b/source/arm11/system.c @@ -1,6 +1,6 @@ /* * This file is part of fastboot 3DS - * Copyright (C) 2017 derrek, profi200 + * Copyright (C) 2018 derrek, profi200 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,7 +34,7 @@ static void systemRestoreHwState(void) MCU_disableLEDs(); } -void WEAK systemInit(void) +void WEAK __systemInit(void) { IRQ_init(); TIMER_init(); @@ -62,3 +62,8 @@ void WEAK systemInit(void) CPU_poweroffCore23(); #endif } + +void WEAK __systemDeinit(void) +{ + IRQ_init(); +} diff --git a/source/arm9/firm.c b/source/arm9/firm.c index 86d73b87..92f032f0 100644 --- a/source/arm9/firm.c +++ b/source/arm9/firm.c @@ -32,6 +32,7 @@ #include "arm9/dev.h" #include "fs.h" #include "hardware/gfx.h" +#include "system.h" typedef struct @@ -329,6 +330,7 @@ noreturn void firmLaunch(void) { memcpy((void*)A9_STUB_ENTRY, (const void*)firmLaunchStub, A9_STUB_SIZE); + __systemDeinit(); deinitCpu(); ((void (*)(int, const char**))A9_STUB_ENTRY)(firmLaunchArgc, (const char**)(ITCM_KERNEL_MIRROR + 0x7470)); diff --git a/source/arm9/start.s b/source/arm9/start.s index e89e2008..73fd330e 100644 --- a/source/arm9/start.s +++ b/source/arm9/start.s @@ -42,8 +42,9 @@ .extern fake_heap_start .extern fake_heap_end .extern __libc_init_array -.extern systemInit +.extern __systemInit .extern main +.extern __systemDeinit .extern irqHandler .extern undefInstrHandler .extern prefetchAbortHandler @@ -110,11 +111,12 @@ _start: ldr r1, =fake_heap_end str r0, [r1] blx __libc_init_array @ Initialize ctors and dtors - blx systemInit + blx __systemInit mov r0, #0 @ argc mov r1, #0 @ argv blx main + blx __systemDeinit _start_lp: mov r0, #0 mcr p15, 0, r0, c7, c0, 4 @ Wait for interrupt diff --git a/source/arm9/system.c b/source/arm9/system.c index d3560021..57b37aea 100644 --- a/source/arm9/system.c +++ b/source/arm9/system.c @@ -25,7 +25,7 @@ -void systemInit(void) +void WEAK __systemInit(void) { IRQ_init(); NDMA_init(); @@ -36,3 +36,9 @@ void systemInit(void) leaveCriticalSection(0); // Enables interrupts } + +void WEAK __systemDeinit(void) +{ + NDMA_init(); + IRQ_init(); +}