2019-12-12 22:38:11 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-12 22:38:11 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* For some reason GAS doesn't know about it, even with .cpu cortex-a57 */
|
|
|
|
#define cpuactlr_el1 s3_1_c15_c2_0
|
|
|
|
#define cpuectlr_el1 s3_1_c15_c2_1
|
|
|
|
|
2020-07-15 04:52:41 +02:00
|
|
|
#define LOAD_IMMEDIATE_32(reg, val) \
|
|
|
|
mov reg, #(((val) >> 0x00) & 0xFFFF); \
|
|
|
|
movk reg, #(((val) >> 0x10) & 0xFFFF), lsl#16
|
|
|
|
|
2019-12-12 22:38:11 +01:00
|
|
|
.section .crt0.text.start, "ax", %progbits
|
|
|
|
.global _start
|
|
|
|
_start:
|
2020-04-14 16:38:01 +02:00
|
|
|
b _main
|
|
|
|
__metadata_begin:
|
|
|
|
.ascii "MLD0" /* Magic */
|
|
|
|
__metadata_target_firmware:
|
2020-04-14 19:07:24 +02:00
|
|
|
.word 0xCCCCCCCC /* Target Firmware. */
|
2020-04-14 16:38:01 +02:00
|
|
|
__metadata_reserved:
|
2020-04-14 19:07:24 +02:00
|
|
|
.word 0xCCCCCCCC /* Reserved. */
|
2020-04-14 16:38:01 +02:00
|
|
|
_main:
|
2019-12-13 10:21:43 +01:00
|
|
|
/* KernelLdr_Main(uintptr_t kernel_base_address, KernelMap *kernel_map, uintptr_t ini1_base_address); */
|
|
|
|
adr x18, _start
|
|
|
|
adr x16, __external_references
|
|
|
|
ldr x17, [x16, #0x8] /* bss end */
|
|
|
|
ldr x16, [x16, #0x0] /* bss start */
|
|
|
|
add x16, x16, x18
|
|
|
|
add x17, x17, x18
|
|
|
|
clear_bss:
|
|
|
|
cmp x16, x17
|
|
|
|
b.cs clear_bss_done
|
|
|
|
str xzr, [x16],#0x8
|
|
|
|
b clear_bss
|
|
|
|
clear_bss_done:
|
|
|
|
adr x17, __external_references
|
|
|
|
ldr x17, [x17, #0x10] /* stack top */
|
|
|
|
add sp, x17, x18
|
|
|
|
|
2020-07-15 04:52:41 +02:00
|
|
|
/* Stack is now set up, so save important state. */
|
2020-01-29 07:09:47 +01:00
|
|
|
sub sp, sp, #0x30
|
2019-12-13 10:21:43 +01:00
|
|
|
stp x0, x1, [sp, #0x00]
|
|
|
|
stp x2, x30, [sp, #0x10]
|
2020-01-29 07:09:47 +01:00
|
|
|
stp xzr, xzr, [sp, #0x20]
|
2020-07-15 04:52:41 +02:00
|
|
|
|
2020-08-10 21:42:19 +02:00
|
|
|
#ifdef ATMOSPHERE_BOARD_NINTENDO_NX
|
2020-07-15 04:52:41 +02:00
|
|
|
/* Get the target firmware from exosphere. */
|
|
|
|
LOAD_IMMEDIATE_32(w0, 0xC3000004)
|
|
|
|
mov w1, #65000
|
|
|
|
smc #1
|
|
|
|
cmp x0, #0
|
|
|
|
0:
|
|
|
|
b.ne 0b
|
|
|
|
|
|
|
|
/* Store the target firmware. */
|
|
|
|
adr x0, __metadata_target_firmware
|
|
|
|
str w1, [x0]
|
2020-08-10 21:42:19 +02:00
|
|
|
#endif
|
2020-07-15 04:52:41 +02:00
|
|
|
|
|
|
|
/* Apply relocations and call init array for KernelLdr. */
|
2019-12-13 10:21:43 +01:00
|
|
|
adr x0, _start
|
|
|
|
adr x1, __external_references
|
|
|
|
ldr x1, [x1, #0x18] /* .dynamic. */
|
|
|
|
add x1, x0, x1
|
|
|
|
|
2019-12-18 00:02:59 +01:00
|
|
|
/* branch to ams::kern::init::Elf::ApplyRelocations(uintptr_t, const ams::kern::init::Elf::Elf64::Dyn *); */
|
|
|
|
bl _ZN3ams4kern4init3Elf16ApplyRelocationsEmPKNS2_5Elf643DynE
|
2019-12-13 10:21:43 +01:00
|
|
|
|
2019-12-18 00:02:59 +01:00
|
|
|
/* branch to ams::kern::init::Elf::CallInitArrayFuncs(uintptr_t, uintptr_t) */
|
2019-12-13 10:21:43 +01:00
|
|
|
adr x2, _start
|
|
|
|
adr x1, __external_references
|
|
|
|
ldr x0, [x1, #0x20] /* init_array_start */
|
|
|
|
ldr x1, [x1, #0x28] /* init_array_end */
|
|
|
|
add x0, x0, x2
|
|
|
|
add x1, x1, x2
|
2019-12-18 00:02:59 +01:00
|
|
|
bl _ZN3ams4kern4init3Elf18CallInitArrayFuncsEmm
|
2019-12-13 10:21:43 +01:00
|
|
|
|
|
|
|
/* Setup system registers, for detection of errors during init later. */
|
2021-04-07 17:36:03 +02:00
|
|
|
msr tpidr_el1, xzr
|
|
|
|
msr cntv_cval_el0, xzr
|
2019-12-13 10:21:43 +01:00
|
|
|
adr x0, __external_references
|
|
|
|
adr x1, _start
|
|
|
|
ldr x0, [x0, #0x30]
|
|
|
|
add x0, x1, x0
|
|
|
|
msr vbar_el1, x0
|
|
|
|
isb
|
|
|
|
|
|
|
|
/* Call ams::kern::init::loader::Main(uintptr_t, ams::kern::init::KernelLayout *, uintptr_t) */
|
|
|
|
ldp x0, x1, [sp, #0x00]
|
|
|
|
ldr x2, [sp, #0x10]
|
2020-02-14 07:05:20 +01:00
|
|
|
|
2019-12-13 10:21:43 +01:00
|
|
|
bl _ZN3ams4kern4init6loader4MainEmPNS1_12KernelLayoutEm
|
|
|
|
str x0, [sp, #0x00]
|
|
|
|
|
2020-01-29 07:09:47 +01:00
|
|
|
/* Get ams::kern::init::loader::AllocateKernelInitStack(). */
|
|
|
|
bl _ZN3ams4kern4init6loader23AllocateKernelInitStackEv
|
|
|
|
str x0, [sp, #0x20]
|
|
|
|
|
|
|
|
|
2021-09-18 18:58:02 +02:00
|
|
|
/* Call ams::kern::init::loader::GetFinalState() */
|
|
|
|
bl _ZN3ams4kern4init6loader13GetFinalStateEv
|
2019-12-13 10:21:43 +01:00
|
|
|
|
2021-09-18 18:58:02 +02:00
|
|
|
/* X0 is now the saved state. */
|
2019-12-17 14:30:29 +01:00
|
|
|
/* We will return this to the kernel. */
|
|
|
|
|
2023-10-11 11:59:41 +02:00
|
|
|
/* Adjust return address to point to the relocated kernel. */
|
2019-12-13 10:21:43 +01:00
|
|
|
ldr x1, [sp, #0x18] /* Return address to Kernel */
|
2019-12-17 09:37:55 +01:00
|
|
|
ldr x2, [sp, #0x00] /* Relocated kernel base address diff. */
|
2019-12-13 10:21:43 +01:00
|
|
|
add x1, x2, x1
|
2023-10-11 11:59:41 +02:00
|
|
|
|
|
|
|
/* Translate the relocated address back to a physical address. */
|
|
|
|
and x4, x1, #0xFFF
|
|
|
|
sub x3, x1, x4
|
|
|
|
at s1e1r, x3
|
|
|
|
isb
|
|
|
|
mrs x3, par_el1
|
|
|
|
1:
|
|
|
|
tbnz w3, #0, 1b
|
|
|
|
and x3, x3, #0xFFFFFFFFF000
|
|
|
|
add x3, x3, x4
|
|
|
|
|
|
|
|
/* Return the difference between relocated and physical in x1. */
|
|
|
|
sub x1, x1, x3
|
|
|
|
|
|
|
|
/* Setup stack, and return to the kernel. */
|
2020-01-29 07:09:47 +01:00
|
|
|
ldr x2, [sp, #0x20]
|
|
|
|
mov sp, x2
|
2023-10-11 11:59:41 +02:00
|
|
|
br x3
|
2019-12-13 10:21:43 +01:00
|
|
|
|
2020-08-10 21:42:19 +02:00
|
|
|
#ifdef ATMOSPHERE_BOARD_NINTENDO_NX
|
2020-04-14 16:38:01 +02:00
|
|
|
.global _ZN3ams4kern17GetTargetFirmwareEv
|
|
|
|
.type _ZN3ams4kern17GetTargetFirmwareEv, %function
|
|
|
|
_ZN3ams4kern17GetTargetFirmwareEv:
|
|
|
|
adr x0, __metadata_target_firmware
|
2020-04-19 07:19:09 +02:00
|
|
|
ldr w0, [x0]
|
2020-04-14 16:38:01 +02:00
|
|
|
ret
|
2020-08-10 21:42:19 +02:00
|
|
|
#endif
|
2020-04-14 16:38:01 +02:00
|
|
|
|
2020-02-14 07:53:26 +01:00
|
|
|
.balign 8
|
2019-12-13 10:21:43 +01:00
|
|
|
__external_references:
|
|
|
|
.quad __bss_start__ - _start
|
|
|
|
.quad __bss_end__ - _start
|
|
|
|
.quad __stack_end - _start
|
2019-12-13 21:41:19 +01:00
|
|
|
.quad _DYNAMIC - _start
|
2019-12-13 10:21:43 +01:00
|
|
|
.quad __init_array_start - _start
|
|
|
|
.quad __init_array_end - _start
|
|
|
|
.quad __vectors_start__ - _start
|