| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Petr Pavlu
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <abi/asmtool.h>
|
|---|
| 30 | #include <arch/arch.h>
|
|---|
| 31 | #include <arch/regutils.h>
|
|---|
| 32 |
|
|---|
| 33 | .section BOOTSTRAP
|
|---|
| 34 |
|
|---|
| 35 | /* MS-DOS stub */
|
|---|
| 36 | msdos_stub:
|
|---|
| 37 | .ascii "MZ" /* MS-DOS signature */
|
|---|
| 38 | .space 0x3a /* Ignore fields up to byte at 0x3c */
|
|---|
| 39 | .long pe_header - msdos_stub /* Offset to the PE header */
|
|---|
| 40 |
|
|---|
| 41 | /* Portable Executable header */
|
|---|
| 42 | pe_header:
|
|---|
| 43 | /* PE signature */
|
|---|
| 44 | .ascii "PE\x0\x0"
|
|---|
| 45 |
|
|---|
| 46 | /* COFF File Header */
|
|---|
| 47 | .short 0xaa64 /* Machine = IMAGE_FILE_MACHINE_ARM64 */
|
|---|
| 48 | .short 1 /* Number of sections */
|
|---|
| 49 | .long 0 /* Time date stamp */
|
|---|
| 50 | .long 0 /* Pointer to symbol table */
|
|---|
| 51 | .long 0 /* Number of symbols */
|
|---|
| 52 | .short sec_table - opt_header /* Size of optional header */
|
|---|
| 53 | /* Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE |
|
|---|
| 54 | * IMAGE_FILE_LARGE_ADDRESS_AWARE */
|
|---|
| 55 | .short 0x22
|
|---|
| 56 |
|
|---|
| 57 | /* Optional header standard fields */
|
|---|
| 58 | opt_header:
|
|---|
| 59 | .short 0x20b /* Magic = PE32+ */
|
|---|
| 60 | .byte 0 /* Major linker version */
|
|---|
| 61 | .byte 0 /* Minor linker version */
|
|---|
| 62 | .long payload_end - msdos_stub /* Size of code */
|
|---|
| 63 | .long 0 /* Size of initialized data */
|
|---|
| 64 | .long 0 /* Size of uninitialized data */
|
|---|
| 65 | .long start - msdos_stub /* Address of entry point */
|
|---|
| 66 | .long start - msdos_stub /* Base of code */
|
|---|
| 67 |
|
|---|
| 68 | /* Optional header Windows-specific fields */
|
|---|
| 69 | .quad 0 /* Image base */
|
|---|
| 70 | .long 4 /* Section alignment */
|
|---|
| 71 | .long 4 /* File alignment */
|
|---|
| 72 | .short 0 /* Major operating system version */
|
|---|
| 73 | .short 0 /* Minor operating system version */
|
|---|
| 74 | .short 0 /* Major image version */
|
|---|
| 75 | .short 0 /* Minor image version */
|
|---|
| 76 | .short 0 /* Major subsystem version */
|
|---|
| 77 | .short 0 /* Minor subsystem version */
|
|---|
| 78 | .long 0 /* Win32 version value */
|
|---|
| 79 | .long payload_end - msdos_stub /* Size of image */
|
|---|
| 80 | .long start - msdos_stub /* Size of headers */
|
|---|
| 81 | .long 0 /* Checksum */
|
|---|
| 82 | .short 10 /* Subsystem = EFI application */
|
|---|
| 83 | .short 0 /* DLL characteristics */
|
|---|
| 84 | .quad 0 /* Size of stack reserve */
|
|---|
| 85 | .quad 0 /* Size of stack commit */
|
|---|
| 86 | .quad 0 /* Size of heap reserve */
|
|---|
| 87 | .quad 0 /* Size of heap commit */
|
|---|
| 88 | .long 0 /* Loader flags */
|
|---|
| 89 | .long 0 /* Number of RVA and sizes */
|
|---|
| 90 |
|
|---|
| 91 | sec_table:
|
|---|
| 92 | .ascii ".text\x0\x0\x0" /* Name */
|
|---|
| 93 | .long payload_end - start /* Virtual size */
|
|---|
| 94 | .long start - msdos_stub /* Virtual address */
|
|---|
| 95 | .long payload_end - start /* Size of raw data */
|
|---|
| 96 | .long start - msdos_stub /* Pointer to raw data */
|
|---|
| 97 | .long 0 /* Pointer to relocations */
|
|---|
| 98 | .long 0 /* Pointer to line numbers */
|
|---|
| 99 | .short 0 /* Number of relocations */
|
|---|
| 100 | .short 0 /* Number of line numbers */
|
|---|
| 101 | /* Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE |
|
|---|
| 102 | * IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE */
|
|---|
| 103 | .long 0xe0000020
|
|---|
| 104 |
|
|---|
| 105 | SYMBOL(start)
|
|---|
| 106 | .hidden start
|
|---|
| 107 |
|
|---|
| 108 | /*
|
|---|
| 109 | * Parameters:
|
|---|
| 110 | * x0 is the image handle.
|
|---|
| 111 | * x1 is a pointer to the UEFI system table.
|
|---|
| 112 | */
|
|---|
| 113 |
|
|---|
| 114 | /*
|
|---|
| 115 | * Stay on the UEFI stack. Its size is at least 128 kB, plenty for this
|
|---|
| 116 | * boot loader.
|
|---|
| 117 | */
|
|---|
| 118 | stp x29, x30, [sp, #-32]!
|
|---|
| 119 | mov x29, sp
|
|---|
| 120 | stp x0, x1, [sp, #16]
|
|---|
| 121 |
|
|---|
| 122 | /*
|
|---|
| 123 | * Self-relocate the image. Pass a load address of the image (x0) and a
|
|---|
| 124 | * pointer to the dynamic array (x1).
|
|---|
| 125 | */
|
|---|
| 126 | adr x0, msdos_stub
|
|---|
| 127 | adrp x1, _DYNAMIC
|
|---|
| 128 | add x1, x1, #:lo12:_DYNAMIC
|
|---|
| 129 | bl self_relocate
|
|---|
| 130 | cbnz x0, 0f
|
|---|
| 131 |
|
|---|
| 132 | /*
|
|---|
| 133 | * Pass the image handle (x0), a pointer to the UEFI system table (x1),
|
|---|
| 134 | * and the image load address (x2) to the boostrap function.
|
|---|
| 135 | */
|
|---|
| 136 | ldp x0, x1, [sp, #16]
|
|---|
| 137 | adr x2, msdos_stub
|
|---|
| 138 | bl bootstrap
|
|---|
| 139 |
|
|---|
| 140 | 0:
|
|---|
| 141 | ldp x29, x30, [sp], #32
|
|---|
| 142 | ret
|
|---|
| 143 |
|
|---|
| 144 | FUNCTION_BEGIN(halt)
|
|---|
| 145 | .hidden halt
|
|---|
| 146 |
|
|---|
| 147 | b halt
|
|---|
| 148 | FUNCTION_END(halt)
|
|---|
| 149 |
|
|---|
| 150 | FUNCTION_BEGIN(jump_to_kernel)
|
|---|
| 151 | .hidden jump_to_kernel
|
|---|
| 152 |
|
|---|
| 153 | /*
|
|---|
| 154 | * Parameters:
|
|---|
| 155 | * x0 is kernel entry point.
|
|---|
| 156 | * x1 is pointer to the bootinfo structure.
|
|---|
| 157 | */
|
|---|
| 158 |
|
|---|
| 159 | /* Disable MMU (removes the identity mapping provided by UEFI). */
|
|---|
| 160 | mrs x2, sctlr_el1
|
|---|
| 161 | bic x2, x2, #SCTLR_M_FLAG
|
|---|
| 162 | msr sctlr_el1, x2
|
|---|
| 163 | isb
|
|---|
| 164 |
|
|---|
| 165 | br x0
|
|---|
| 166 | FUNCTION_END(jump_to_kernel)
|
|---|