| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2015 Petr Pavlu
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <abi/asmtool.h>
|
|---|
| 8 | #include <arch/arch.h>
|
|---|
| 9 | #include <arch/regutils.h>
|
|---|
| 10 |
|
|---|
| 11 | .section BOOTSTRAP
|
|---|
| 12 |
|
|---|
| 13 | #define DIRECTORY_ENTRIES 16
|
|---|
| 14 |
|
|---|
| 15 | /* MS-DOS stub */
|
|---|
| 16 | msdos_stub:
|
|---|
| 17 | .ascii "MZ" /* MS-DOS signature */
|
|---|
| 18 | .space 0x3a /* Ignore fields up to byte at 0x3c */
|
|---|
| 19 | .long pe_header - msdos_stub /* Offset to the PE header */
|
|---|
| 20 |
|
|---|
| 21 | /* Portable Executable header */
|
|---|
| 22 | pe_header:
|
|---|
| 23 | /* PE signature */
|
|---|
| 24 | .ascii "PE\x0\x0"
|
|---|
| 25 |
|
|---|
| 26 | /* COFF File Header */
|
|---|
| 27 | .short 0xaa64 /* Machine = IMAGE_FILE_MACHINE_ARM64 */
|
|---|
| 28 | .short 1 /* Number of sections */
|
|---|
| 29 | .long 0 /* Time date stamp */
|
|---|
| 30 | .long 0 /* Pointer to symbol table */
|
|---|
| 31 | .long 0 /* Number of symbols */
|
|---|
| 32 | .short sec_table - opt_header /* Size of optional header */
|
|---|
| 33 | /*
|
|---|
| 34 | * Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE |
|
|---|
| 35 | * IMAGE_FILE_LARGE_ADDRESS_AWARE
|
|---|
| 36 | */
|
|---|
| 37 | .short 0x22
|
|---|
| 38 |
|
|---|
| 39 | /* Optional header standard fields */
|
|---|
| 40 | opt_header:
|
|---|
| 41 | .short 0x20b /* Magic = PE32+ */
|
|---|
| 42 | .byte 0 /* Major linker version */
|
|---|
| 43 | .byte 0 /* Minor linker version */
|
|---|
| 44 | .long payload_end - msdos_stub /* Size of code */
|
|---|
| 45 | .long 0 /* Size of initialized data */
|
|---|
| 46 | .long 0 /* Size of uninitialized data */
|
|---|
| 47 | .long start - msdos_stub /* Address of entry point */
|
|---|
| 48 | .long start - msdos_stub /* Base of code */
|
|---|
| 49 |
|
|---|
| 50 | /* Optional header Windows-specific fields */
|
|---|
| 51 | .quad 0 /* Image base */
|
|---|
| 52 | .long 4 /* Section alignment */
|
|---|
| 53 | .long 4 /* File alignment */
|
|---|
| 54 | .short 0 /* Major operating system version */
|
|---|
| 55 | .short 0 /* Minor operating system version */
|
|---|
| 56 | .short 0 /* Major image version */
|
|---|
| 57 | .short 0 /* Minor image version */
|
|---|
| 58 | .short 0 /* Major subsystem version */
|
|---|
| 59 | .short 0 /* Minor subsystem version */
|
|---|
| 60 | .long 0 /* Win32 version value */
|
|---|
| 61 | .long payload_end - msdos_stub /* Size of image */
|
|---|
| 62 | .long start - msdos_stub /* Size of headers */
|
|---|
| 63 | .long 0 /* Checksum */
|
|---|
| 64 | .short 10 /* Subsystem = EFI application */
|
|---|
| 65 | .short 0 /* DLL characteristics */
|
|---|
| 66 | .quad 0 /* Size of stack reserve */
|
|---|
| 67 | .quad 0 /* Size of stack commit */
|
|---|
| 68 | .quad 0 /* Size of heap reserve */
|
|---|
| 69 | .quad 0 /* Size of heap commit */
|
|---|
| 70 | .long 0 /* Loader flags */
|
|---|
| 71 | .long DIRECTORY_ENTRIES /* Number of RVA and sizes */
|
|---|
| 72 | .space DIRECTORY_ENTRIES * 8 /* Directory entries */
|
|---|
| 73 |
|
|---|
| 74 | sec_table:
|
|---|
| 75 | .ascii ".text\x0\x0\x0" /* Name */
|
|---|
| 76 | .long payload_end - start /* Virtual size */
|
|---|
| 77 | .long start - msdos_stub /* Virtual address */
|
|---|
| 78 | .long payload_end - start /* Size of raw data */
|
|---|
| 79 | .long start - msdos_stub /* Pointer to raw data */
|
|---|
| 80 | .long 0 /* Pointer to relocations */
|
|---|
| 81 | .long 0 /* Pointer to line numbers */
|
|---|
| 82 | .short 0 /* Number of relocations */
|
|---|
| 83 | .short 0 /* Number of line numbers */
|
|---|
| 84 | /*
|
|---|
| 85 | * Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE |
|
|---|
| 86 | * IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
|
|---|
| 87 | */
|
|---|
| 88 | .long 0xe0000020
|
|---|
| 89 |
|
|---|
| 90 | /** Boot loader entry point
|
|---|
| 91 | *
|
|---|
| 92 | * @param x0 UEFI image handle.
|
|---|
| 93 | * @param x1 Pointer to the UEFI system table.
|
|---|
| 94 | *
|
|---|
| 95 | */
|
|---|
| 96 | SYMBOL(start)
|
|---|
| 97 | .hidden start
|
|---|
| 98 |
|
|---|
| 99 | /*
|
|---|
| 100 | * Stay on the UEFI stack. Its size is at least 128 KiB, plenty for this
|
|---|
| 101 | * boot loader.
|
|---|
| 102 | */
|
|---|
| 103 | stp x29, x30, [sp, #-32]!
|
|---|
| 104 | mov x29, sp
|
|---|
| 105 | stp x0, x1, [sp, #16]
|
|---|
| 106 |
|
|---|
| 107 | /*
|
|---|
| 108 | * Self-relocate the image. Pass a load address of the image (x0) and a
|
|---|
| 109 | * pointer to the dynamic array (x1).
|
|---|
| 110 | */
|
|---|
| 111 | adr x0, msdos_stub
|
|---|
| 112 | adrp x1, _DYNAMIC
|
|---|
| 113 | add x1, x1, #:lo12:_DYNAMIC
|
|---|
| 114 | bl self_relocate
|
|---|
| 115 | cbnz x0, __uefi_exit
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * Flush the instruction cache of the relocated boot loader image.
|
|---|
| 119 | */
|
|---|
| 120 | adr x0, msdos_stub
|
|---|
| 121 | adrp x1, payload_end
|
|---|
| 122 | sub x1, x1, x0
|
|---|
| 123 | bl smc_coherence
|
|---|
| 124 |
|
|---|
| 125 | /*
|
|---|
| 126 | * Pass the image handle (x0), a pointer to the UEFI system table (x1),
|
|---|
| 127 | * and the image load address (x2) to the boostrap function.
|
|---|
| 128 | */
|
|---|
| 129 | ldp x0, x1, [sp, #16]
|
|---|
| 130 | adr x2, msdos_stub
|
|---|
| 131 | bl bootstrap
|
|---|
| 132 |
|
|---|
| 133 | __uefi_exit:
|
|---|
| 134 | ldp x29, x30, [sp], #32
|
|---|
| 135 | ret
|
|---|
| 136 |
|
|---|
| 137 | FUNCTION_BEGIN(halt)
|
|---|
| 138 | .hidden halt
|
|---|
| 139 |
|
|---|
| 140 | b halt
|
|---|
| 141 | FUNCTION_END(halt)
|
|---|
| 142 |
|
|---|
| 143 | /** Flush instruction caches
|
|---|
| 144 | *
|
|---|
| 145 | * @param x0 Starting address of the flushing.
|
|---|
| 146 | * @param x1 Number of bytes to flush.
|
|---|
| 147 | *
|
|---|
| 148 | */
|
|---|
| 149 | FUNCTION_BEGIN(smc_coherence)
|
|---|
| 150 | .hidden smc_coherence
|
|---|
| 151 |
|
|---|
| 152 | /* Initialize loop */
|
|---|
| 153 | mov x9, x0
|
|---|
| 154 | mov x10, xzr
|
|---|
| 155 |
|
|---|
| 156 | __dc_loop:
|
|---|
| 157 | /* Data or Unified Cache Line Clean */
|
|---|
| 158 | dc cvau, x9
|
|---|
| 159 | add x9, x9, #4
|
|---|
| 160 | add x10, x10, #4
|
|---|
| 161 | cmp x10, x1
|
|---|
| 162 | blo __dc_loop
|
|---|
| 163 |
|
|---|
| 164 | dsb ish
|
|---|
| 165 |
|
|---|
| 166 | /* Initialize loop */
|
|---|
| 167 | mov x9, x0
|
|---|
| 168 | mov x10, xzr
|
|---|
| 169 |
|
|---|
| 170 | __ic_loop:
|
|---|
| 171 | /* Instruction Cache Line Invalidate */
|
|---|
| 172 | ic ivau, x9
|
|---|
| 173 | add x9, x9, #4
|
|---|
| 174 | add x10, x10, #4
|
|---|
| 175 | cmp x10, x1
|
|---|
| 176 | blo __ic_loop
|
|---|
| 177 |
|
|---|
| 178 | dsb ish
|
|---|
| 179 | isb
|
|---|
| 180 | ret
|
|---|
| 181 | FUNCTION_END(smc_coherence)
|
|---|
| 182 |
|
|---|
| 183 | /** Flush data caches
|
|---|
| 184 | *
|
|---|
| 185 | * @param x0 Starting address of the flushing.
|
|---|
| 186 | * @param x1 Number of bytes to flush.
|
|---|
| 187 | *
|
|---|
| 188 | */
|
|---|
| 189 | FUNCTION_BEGIN(dcache_flush)
|
|---|
| 190 | .hidden dcache_flush
|
|---|
| 191 |
|
|---|
| 192 | mov x9, x0
|
|---|
| 193 | mov x10, xzr
|
|---|
| 194 |
|
|---|
| 195 | __dc_flush_loop:
|
|---|
| 196 | /* Data or Unified Cache Line Clean */
|
|---|
| 197 | dc cvau, x9
|
|---|
| 198 | add x9, x9, #4
|
|---|
| 199 | add x10, x10, #4
|
|---|
| 200 | cmp x10, x1
|
|---|
| 201 | blo __dc_flush_loop
|
|---|
| 202 |
|
|---|
| 203 | dsb ish
|
|---|
| 204 | isb
|
|---|
| 205 | ret
|
|---|
| 206 | FUNCTION_END(dcache_flush)
|
|---|
| 207 |
|
|---|
| 208 | /** Kernel entry
|
|---|
| 209 | *
|
|---|
| 210 | * @param x0 Kernel entry point.
|
|---|
| 211 | * @param x1 Pointer to the bootinfo structure.
|
|---|
| 212 | *
|
|---|
| 213 | */
|
|---|
| 214 | FUNCTION_BEGIN(jump_to_kernel)
|
|---|
| 215 | .hidden jump_to_kernel
|
|---|
| 216 |
|
|---|
| 217 | mrs x9, CurrentEL
|
|---|
| 218 | lsr x9, x9, 2
|
|---|
| 219 |
|
|---|
| 220 | cmp x9, #3
|
|---|
| 221 | b.eq __el3
|
|---|
| 222 |
|
|---|
| 223 | cmp x9, #2
|
|---|
| 224 | b.eq __el2
|
|---|
| 225 |
|
|---|
| 226 | cmp x9, #1
|
|---|
| 227 | b.eq __el1
|
|---|
| 228 |
|
|---|
| 229 | b halt
|
|---|
| 230 |
|
|---|
| 231 | __el3:
|
|---|
| 232 | msr sctlr_el2, xzr
|
|---|
| 233 | msr hcr_el2, xzr
|
|---|
| 234 | isb
|
|---|
| 235 |
|
|---|
| 236 | /* EL2 is AArch64, EL1 is Non-secure World */
|
|---|
| 237 | mov x9, #(1 << 10)
|
|---|
| 238 | orr x9, x9, #(1 << 0)
|
|---|
| 239 | msr scr_el3, x9
|
|---|
| 240 | isb
|
|---|
| 241 |
|
|---|
| 242 | /* EL2h */
|
|---|
| 243 | mov x9, #0x9
|
|---|
| 244 | msr spsr_el3, x9
|
|---|
| 245 | isb
|
|---|
| 246 |
|
|---|
| 247 | adr x9, __el2
|
|---|
| 248 | msr elr_el3, x9
|
|---|
| 249 | isb
|
|---|
| 250 |
|
|---|
| 251 | /* Switch to EL2 */
|
|---|
| 252 | eret
|
|---|
| 253 |
|
|---|
| 254 | __el2:
|
|---|
| 255 | msr sctlr_el1, xzr
|
|---|
| 256 | isb
|
|---|
| 257 |
|
|---|
| 258 | /* EL1 is AArch64 */
|
|---|
| 259 | mov x9, #(1 << 31)
|
|---|
| 260 | msr hcr_el2, x9
|
|---|
| 261 | isb
|
|---|
| 262 |
|
|---|
| 263 | /* EL1h */
|
|---|
| 264 | mov x9, #0x5
|
|---|
| 265 | msr spsr_el2, x9
|
|---|
| 266 | isb
|
|---|
| 267 |
|
|---|
| 268 | adr x9, __el1
|
|---|
| 269 | msr elr_el2, x9
|
|---|
| 270 | isb
|
|---|
| 271 |
|
|---|
| 272 | /* Switch to EL1 */
|
|---|
| 273 | eret
|
|---|
| 274 |
|
|---|
| 275 | __el1:
|
|---|
| 276 | /* Do not trap on FPU instructions */
|
|---|
| 277 | mrs x9, cpacr_el1
|
|---|
| 278 | orr x9, x9, #(3 << 20)
|
|---|
| 279 | msr cpacr_el1, x9
|
|---|
| 280 | dmb ish
|
|---|
| 281 |
|
|---|
| 282 | /* Disable MMU (removes the identity mapping provided by UEFI) */
|
|---|
| 283 | mrs x9, sctlr_el1
|
|---|
| 284 | bic x9, x9, #SCTLR_M_FLAG
|
|---|
| 285 | msr sctlr_el1, x9
|
|---|
| 286 | isb
|
|---|
| 287 |
|
|---|
| 288 | br x0
|
|---|
| 289 | FUNCTION_END(jump_to_kernel)
|
|---|