12192efc0SMitchell Horne/*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 32192efc0SMitchell Horne * 42192efc0SMitchell Horne * Copyright (c) 2020 Mitchell Horne <mhorne@FreeBSD.org> 52192efc0SMitchell Horne * 62192efc0SMitchell Horne * Redistribution and use in source and binary forms, with or without 72192efc0SMitchell Horne * modification, are permitted provided that the following conditions 82192efc0SMitchell Horne * are met: 92192efc0SMitchell Horne * 1. Redistributions of source code must retain the above copyright 102192efc0SMitchell Horne * notice, this list of conditions and the following disclaimer. 112192efc0SMitchell Horne * 2. Redistributions in binary form must reproduce the above copyright 122192efc0SMitchell Horne * notice, this list of conditions and the following disclaimer in the 132192efc0SMitchell Horne * documentation and/or other materials provided with the distribution. 142192efc0SMitchell Horne * 152192efc0SMitchell Horne * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 162192efc0SMitchell Horne * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 172192efc0SMitchell Horne * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 182192efc0SMitchell Horne * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 192192efc0SMitchell Horne * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 202192efc0SMitchell Horne * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 212192efc0SMitchell Horne * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 222192efc0SMitchell Horne * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 232192efc0SMitchell Horne * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 242192efc0SMitchell Horne * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 252192efc0SMitchell Horne * SUCH DAMAGE. 262192efc0SMitchell Horne */ 272192efc0SMitchell Horne 282192efc0SMitchell Horne#include <machine/asm.h> 292192efc0SMitchell Horne 302192efc0SMitchell Horne/* 312192efc0SMitchell Horne * We need to be a PE32+ file for EFI. On some architectures we can use 322192efc0SMitchell Horne * objcopy to create the correct file, however on RISC-V we need to do 332192efc0SMitchell Horne * it ourselves. 342192efc0SMitchell Horne */ 352192efc0SMitchell Horne 362192efc0SMitchell Horne#define IMAGE_FILE_MACHINE_RISCV64 0x5064 372192efc0SMitchell Horne 382192efc0SMitchell Horne#define IMAGE_SCN_CNT_CODE 0x00000020 392192efc0SMitchell Horne#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 402192efc0SMitchell Horne#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 412192efc0SMitchell Horne#define IMAGE_SCN_MEM_EXECUTE 0x20000000 422192efc0SMitchell Horne#define IMAGE_SCN_MEM_READ 0x40000000 432192efc0SMitchell Horne 442192efc0SMitchell Horne .section .peheader,"a" 452192efc0SMitchell Horneefi_start: 462192efc0SMitchell Horne /* The MS-DOS Stub, only used to get the offset of the COFF header */ 472192efc0SMitchell Horne .ascii "MZ" 482192efc0SMitchell Horne .short 0 492192efc0SMitchell Horne .space 0x38 502192efc0SMitchell Horne .long pe_sig - efi_start 512192efc0SMitchell Horne 522192efc0SMitchell Horne /* The PE32 Signature. Needs to be 8-byte aligned */ 532192efc0SMitchell Horne .align 3 542192efc0SMitchell Hornepe_sig: 552192efc0SMitchell Horne .ascii "PE" 562192efc0SMitchell Horne .short 0 572192efc0SMitchell Hornecoff_head: 582192efc0SMitchell Horne .short IMAGE_FILE_MACHINE_RISCV64 /* RISC-V 64 file */ 592192efc0SMitchell Horne .short 2 /* 2 Sections */ 602192efc0SMitchell Horne .long 0 /* Timestamp */ 612192efc0SMitchell Horne .long 0 /* No symbol table */ 622192efc0SMitchell Horne .long 0 /* No symbols */ 632192efc0SMitchell Horne .short section_table - optional_header /* Optional header size */ 642192efc0SMitchell Horne .short 0 /* Characteristics TODO: Fill in */ 652192efc0SMitchell Horne 662192efc0SMitchell Horneoptional_header: 672192efc0SMitchell Horne .short 0x020b /* PE32+ (64-bit addressing) */ 682192efc0SMitchell Horne .byte 0 /* Major linker version */ 692192efc0SMitchell Horne .byte 0 /* Minor linker version */ 702192efc0SMitchell Horne .long _edata - _end_header /* Code size */ 712192efc0SMitchell Horne .long 0 /* No initialized data */ 722192efc0SMitchell Horne .long 0 /* No uninitialized data */ 732192efc0SMitchell Horne .long _start - efi_start /* Entry point */ 742192efc0SMitchell Horne .long _end_header - efi_start /* Start of code */ 752192efc0SMitchell Horne 762192efc0SMitchell Horneoptional_windows_header: 772192efc0SMitchell Horne .quad 0 /* Image base */ 782192efc0SMitchell Horne .long 32 /* Section Alignment */ 792192efc0SMitchell Horne .long 8 /* File alignment */ 802192efc0SMitchell Horne .short 0 /* Major OS version */ 812192efc0SMitchell Horne .short 0 /* Minor OS version */ 822192efc0SMitchell Horne .short 0 /* Major image version */ 832192efc0SMitchell Horne .short 0 /* Minor image version */ 842192efc0SMitchell Horne .short 0 /* Major subsystem version */ 852192efc0SMitchell Horne .short 0 /* Minor subsystem version */ 862192efc0SMitchell Horne .long 0 /* Win32 version */ 872192efc0SMitchell Horne .long _edata - efi_start /* Image size */ 882192efc0SMitchell Horne .long _end_header - efi_start /* Header size */ 892192efc0SMitchell Horne .long 0 /* Checksum */ 902192efc0SMitchell Horne .short 0xa /* Subsystem (EFI app) */ 912192efc0SMitchell Horne .short 0 /* DLL Characteristics */ 922192efc0SMitchell Horne .quad 0 /* Stack reserve */ 932192efc0SMitchell Horne .quad 0 /* Stack commit */ 942192efc0SMitchell Horne .quad 0 /* Heap reserve */ 952192efc0SMitchell Horne .quad 0 /* Heap commit */ 962192efc0SMitchell Horne .long 0 /* Loader flags */ 972192efc0SMitchell Horne .long 6 /* Number of RVAs */ 982192efc0SMitchell Horne 992192efc0SMitchell Horne /* RVAs: */ 1002192efc0SMitchell Horne .quad 0 1012192efc0SMitchell Horne .quad 0 1022192efc0SMitchell Horne .quad 0 1032192efc0SMitchell Horne .quad 0 1042192efc0SMitchell Horne .quad 0 1052192efc0SMitchell Horne .quad 0 1062192efc0SMitchell Horne 1072192efc0SMitchell Hornesection_table: 1082192efc0SMitchell Horne /* We need a .reloc section for EFI */ 1092192efc0SMitchell Horne .ascii ".reloc" 1102192efc0SMitchell Horne .byte 0 1112192efc0SMitchell Horne .byte 0 /* Pad to 8 bytes */ 1122192efc0SMitchell Horne .long 0 /* Virtual size */ 1132192efc0SMitchell Horne .long 0 /* Virtual address */ 1142192efc0SMitchell Horne .long 0 /* Size of raw data */ 1152192efc0SMitchell Horne .long 0 /* Pointer to raw data */ 1162192efc0SMitchell Horne .long 0 /* Pointer to relocations */ 1172192efc0SMitchell Horne .long 0 /* Pointer to line numbers */ 1182192efc0SMitchell Horne .short 0 /* Number of relocations */ 1192192efc0SMitchell Horne .short 0 /* Number of line numbers */ 1202192efc0SMitchell Horne .long (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \ 1212192efc0SMitchell Horne IMAGE_SCN_MEM_DISCARDABLE) /* Characteristics */ 1222192efc0SMitchell Horne 1232192efc0SMitchell Horne /* The contents of the loader */ 1242192efc0SMitchell Horne .ascii ".text" 1252192efc0SMitchell Horne .byte 0 1262192efc0SMitchell Horne .byte 0 1272192efc0SMitchell Horne .byte 0 /* Pad to 8 bytes */ 1282192efc0SMitchell Horne .long _edata - _end_header /* Virtual size */ 1292192efc0SMitchell Horne .long _end_header - efi_start /* Virtual address */ 1302192efc0SMitchell Horne .long _edata - _end_header /* Size of raw data */ 1312192efc0SMitchell Horne .long _end_header - efi_start /* Pointer to raw data */ 1322192efc0SMitchell Horne .long 0 /* Pointer to relocations */ 1332192efc0SMitchell Horne .long 0 /* Pointer to line numbers */ 1342192efc0SMitchell Horne .short 0 /* Number of relocations */ 1352192efc0SMitchell Horne .short 0 /* Number of line numbers */ 1362192efc0SMitchell Horne .long (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \ 1372192efc0SMitchell Horne IMAGE_SCN_MEM_READ) /* Characteristics */ 1382192efc0SMitchell Horne_end_header: 1392192efc0SMitchell Horne 1402192efc0SMitchell Horne .text 1412192efc0SMitchell Horne .globl _start 1422192efc0SMitchell Horne_start: 1432192efc0SMitchell Horne /* Save the boot params to the stack */ 1442192efc0SMitchell Horne addi sp, sp, -16 1452192efc0SMitchell Horne sd a0, 0(sp) 1462192efc0SMitchell Horne sd a1, 8(sp) 1472192efc0SMitchell Horne 1482192efc0SMitchell Horne /* Zero the BSS */ 1492192efc0SMitchell Horne lla t0, __bss_start 1502192efc0SMitchell Horne lla t1, __bss_end 1512192efc0SMitchell Horne 1522192efc0SMitchell Horne1: sd zero, 0(t0) 1532192efc0SMitchell Horne addi t0, t0, 8 1542192efc0SMitchell Horne bltu t0, t1, 1b 1552192efc0SMitchell Horne 1562192efc0SMitchell Horne lla a0, ImageBase 1572192efc0SMitchell Horne lla a1, _DYNAMIC 1582192efc0SMitchell Horne call _C_LABEL(self_reloc) 1592192efc0SMitchell Horne 1602192efc0SMitchell Horne ld a1, 8(sp) 1612192efc0SMitchell Horne ld a0, 0(sp) 1622192efc0SMitchell Horne tail _C_LABEL(efi_main) 1632192efc0SMitchell Horne 1642192efc0SMitchell Horne /* NOTREACHED */ 1652192efc0SMitchell Horne2: wfi 1662192efc0SMitchell Horne j 2b 167