1/* $OpenBSD: start.S,v 1.4 2022/12/08 01:25:44 guenther Exp $ */ 2/*- 3 * Copyright (c) 2014, 2015 Andrew Turner 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/boot/efi/loader/arch/arm/start.S 282727 2015-05-10 13:24:26Z ian $ 28 */ 29 30#include <machine/asm.h> 31 32/* 33 * We need to be a PE32 file for EFI. On some architectures we can use 34 * objcopy to create the correct file, however on arm we need to do 35 * it ourselves. 36 */ 37 38#define IMAGE_FILE_MACHINE_ARM 0x01c2 39 40#define IMAGE_SCN_CNT_CODE 0x00000020 41#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 42#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 43#define IMAGE_SCN_MEM_EXECUTE 0x20000000 44#define IMAGE_SCN_MEM_READ 0x40000000 45 46 .section .peheader, "a" 47efi_start: 48 /* The MS-DOS Stub, only used to get the offset of the COFF header */ 49 .ascii "MZ" 50 .short 0 51 .space 0x38 52 .long pe_sig - efi_start 53 54 /* The PE32 Signature. Needs to be 8-byte aligned */ 55 .align 3 56pe_sig: 57 .ascii "PE" 58 .short 0 59coff_head: 60 .short IMAGE_FILE_MACHINE_ARM /* ARM file */ 61 .short 2 /* 2 Sections */ 62 .long 0 /* Timestamp */ 63 .long 0 /* No symbol table */ 64 .long 0 /* No symbols */ 65 .short section_table - optional_header /* Optional header size */ 66 .short 0 /* Characteristics TODO: Fill in */ 67 68optional_header: 69 .short 0x010b /* PE32 (32-bit addressing) */ 70 .byte 0 /* Major linker version */ 71 .byte 0 /* Minor linker version */ 72 .long _edata - _end_header /* Code size */ 73 .long 0 /* No initialized data */ 74 .long 0 /* No uninitialized data */ 75 .long _start - efi_start /* Entry point */ 76 .long _end_header - efi_start /* Start of code */ 77 .long 0 /* Start of data */ 78 79optional_windows_header: 80 .long 0 /* Image base */ 81 .long 32 /* Section Alignment */ 82 .long 8 /* File alignment */ 83 .short 0 /* Major OS version */ 84 .short 0 /* Minor OS version */ 85 .short 0 /* Major image version */ 86 .short 0 /* Minor image version */ 87 .short 0 /* Major subsystem version */ 88 .short 0 /* Minor subsystem version */ 89 .long 0 /* Win32 version */ 90 .long _edata - efi_start /* Image size */ 91 .long _end_header - efi_start /* Header size */ 92 .long 0 /* Checksum */ 93 .short 0xa /* Subsystem (EFI app) */ 94 .short 0 /* DLL Characteristics */ 95 .long 0 /* Stack reserve */ 96 .long 0 /* Stack commit */ 97 .long 0 /* Heap reserve */ 98 .long 0 /* Heap commit */ 99 .long 0 /* Loader flags */ 100 .long 6 /* Number of RVAs */ 101 102 /* RVAs: */ 103 .quad 0 104 .quad 0 105 .quad 0 106 .quad 0 107 .quad 0 108 .quad 0 109 110section_table: 111 /* We need a .reloc section for EFI */ 112 .ascii ".reloc" 113 .byte 0 114 .byte 0 /* Pad to 8 bytes */ 115 .long 0 /* Virtual size */ 116 .long 0 /* Virtual address */ 117 .long 0 /* Size of raw data */ 118 .long 0 /* Pointer to raw data */ 119 .long 0 /* Pointer to relocations */ 120 .long 0 /* Pointer to line numbers */ 121 .short 0 /* Number of relocations */ 122 .short 0 /* Number of line numbers */ 123 .long (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \ 124 IMAGE_SCN_MEM_DISCARDABLE) /* Characteristics */ 125 126 /* The contents of the loader */ 127 .ascii ".text" 128 .byte 0 129 .byte 0 130 .byte 0 /* Pad to 8 bytes */ 131 .long _edata - _end_header /* Virtual size */ 132 .long _end_header - efi_start /* Virtual address */ 133 .long _edata - _end_header /* Size of raw data */ 134 .long _end_header - efi_start /* Pointer to raw data */ 135 .long 0 /* Pointer to relocations */ 136 .long 0 /* Pointer to line numbers */ 137 .short 0 /* Number of relocations */ 138 .short 0 /* Number of line numbers */ 139 .long (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \ 140 IMAGE_SCN_MEM_READ) /* Characteristics */ 141_end_header: 142 143 .text 144_start: 145 /* Save the boot params to the stack */ 146 push {r0, r1} 147 148 adr r0, .Lbase 149 ldr r1, [r0] 150 sub r5, r0, r1 151 152 ldr r0, .Limagebase 153 add r0, r0, r5 154 ldr r1, .Ldynamic 155 add r1, r1, r5 156 157 bl self_reloc 158 159 /* Zero the BSS, _reloc fixed the values for us */ 160 ldr r0, .Lbss 161 ldr r1, .Lbssend 162 mov r2, #0 163 1641: cmp r0, r1 165 bge 2f 166 str r2, [r0], #4 167 b 1b 1682: 169 170 pop {r0, r1} 171 bl efi_main 172 1731: b 1b 174 175.Lbase: 176 .word . 177.Limagebase: 178 .word ImageBase 179.Ldynamic: 180 .word _DYNAMIC 181.Lbss: 182 .word __bss_start 183.Lbssend: 184 .word __bss_end 185 186.align 3 187stack: 188 .space 512 189stack_end: 190 191