/*-1* Copyright (c) 2008 Semihalf, Rafal Czubak2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <machine/asm.h>27#include <machine/armreg.h>2829.text30.extern _C_LABEL(self_reloc), _C_LABEL(main)31.weak _DYNAMIC3233/*34* Entry point to the loader that U-Boot passes control to.35*/36.globl _start37_start:3839mrc p15, 0, ip, c1, c0, 040orr ip, ip, #(CPU_CONTROL_UNAL_ENABLE)41orr ip, ip, #(CPU_CONTROL_AFLT_ENABLE)42mcr p15, 0, ip, c1, c0, 04344/* Save the arguments and return register before calling self_reloc */45push {r0, r1, r9, lr}4647/*48* Do self-relocation when the weak external symbol _DYNAMIC is non-NULL.49* When linked as a dynamic relocatable file, the linker automatically50* defines _DYNAMIC with a value that is the offset of the dynamic51* relocation info section.52* Note that we're still on u-boot's stack here, but the self_reloc53* code uses only a couple dozen bytes of stack space.54*/55adr ip, .here_off /* .here_off is a symbol whose value */56ldr r0, [ip] /* is its own offset in the text seg. */57sub r0, ip, r0 /* Get its pc-relative address and */58ldr r1, .dynamic_off /* subtract its value and we get */59teq r1, #0 /* r0 = physaddr we were loaded at. */60addne r1, r1, r0 /* r1 = dynamic section physaddr. */61blne _C_LABEL(self_reloc) /* Do reloc if _DYNAMIC is non-NULL. */6263/* Restore saved arguments */64pop {r0, r1, r9, lr}6566/* Hint where to look for the API signature */67ldr ip, =uboot_address68str sp, [ip]6970/* Save U-Boot's r8 and r9 for syscall trampoline */71ldr ip, =saved_regs72str r8, [ip, #0] /* old gd pointer (use to hold lr) */73str r9, [ip, #4] /* new gd pointer */7475/*76* Start loader. Save return address first (r8 is available from77* trampoline save).78*/79mov r8, lr80bl main81mov lr, r88283/* Restore U-Boot environment */84ldr ip, =saved_regs85ldr r8, [ip, #0]86ldr r9, [ip, #4]87mov pc, lr8889/*90* Data for self-relocation, in the text segment for pc-rel access.91*/92.here_off:93.word .94.dynamic_off:95.word _DYNAMIC9697/*98* syscall()99*/100ENTRY(syscall)101/* Save caller's lr, r8 and r9 */102ldr ip, =saved_regs103str r8, [ip, #8]104str r9, [ip, #12]105str lr, [ip, #16]106/* Restore U-Boot's r8 and r9 */107ldr r8, [ip, #0]108ldr r9, [ip, #4]109/* Call into U-Boot */110ldr lr, =return_from_syscall111ldr ip, =syscall_ptr112ldr pc, [ip]113return_from_syscall:114/* Restore loader's r8, r9 and lr */115ldr ip, =saved_regs116ldr lr, [ip, #16]117ldr r9, [ip, #12]118ldr r8, [ip, #8]119/* Return to caller */120mov pc, lr121122/*123* Data section124*/125.data126.align 4127.globl syscall_ptr128syscall_ptr:129.long 0130131.globl uboot_address132uboot_address:133.long 0134135saved_regs:136.long 0 /* U-Boot's r8 */137.long 0 /* U-Boot's r9 */138.long 0 /* Loader's r8 */139.long 0 /* Loader's r9 */140.long 0 /* Loader's lr */141142143