crt0_riscv_efi.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * crt0-efi-riscv.S - PE/COFF header for RISC-V EFI applications
  4. *
  5. * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  6. * Copright (C) 2018 Alexander Graf <agraf@suse.de>
  7. *
  8. * This file is inspired by arch/arm/lib/crt0_aarch64_efi.S
  9. */
  10. #include <asm-generic/pe.h>
  11. #if __riscv_xlen == 64
  12. #define SIZE_LONG 8
  13. #define SAVE_LONG(reg, idx) sd reg, (idx*SIZE_LONG)(sp)
  14. #define LOAD_LONG(reg, idx) ld reg, (idx*SIZE_LONG)(sp)
  15. #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV64
  16. #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
  17. #else
  18. #define SIZE_LONG 4
  19. #define SAVE_LONG(reg, idx) sw reg, (idx*SIZE_LONG)(sp)
  20. #define LOAD_LONG(reg, idx) lw reg, (idx*SIZE_LONG)(sp)
  21. #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32
  22. #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
  23. #endif
  24. .section .text.head
  25. /*
  26. * Magic "MZ" signature for PE/COFF
  27. */
  28. .globl ImageBase
  29. ImageBase:
  30. .short IMAGE_DOS_SIGNATURE /* 'MZ' */
  31. .skip 46 /* 'MZ' + pad + offset == 64 */
  32. .long 0x43534952 /* Linux magic "RISCV */
  33. .long 0x00000056
  34. .long 0x05435352 /* Linux magic2 "RSC\x05*/
  35. .long pe_header - ImageBase /* Offset to the PE header */
  36. pe_header:
  37. .long IMAGE_NT_SIGNATURE /* 'PE' */
  38. coff_header:
  39. .short PE_MACHINE /* RISC-V 64/32-bit */
  40. .short 2 /* nr_sections */
  41. .long 0 /* TimeDateStamp */
  42. .long 0 /* PointerToSymbolTable */
  43. .long 0 /* NumberOfSymbols */
  44. .short section_table - optional_header /* SizeOfOptionalHeader */
  45. /* Characteristics */
  46. .short (IMAGE_FILE_EXECUTABLE_IMAGE | \
  47. IMAGE_FILE_LINE_NUMS_STRIPPED | \
  48. IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
  49. IMAGE_FILE_DEBUG_STRIPPED)
  50. optional_header:
  51. .short PE_MAGIC /* PE32(+) format */
  52. .byte 0x02 /* MajorLinkerVersion */
  53. .byte 0x14 /* MinorLinkerVersion */
  54. .long _edata - _start /* SizeOfCode */
  55. .long 0 /* SizeOfInitializedData */
  56. .long 0 /* SizeOfUninitializedData */
  57. .long _start - ImageBase /* AddressOfEntryPoint */
  58. .long _start - ImageBase /* BaseOfCode */
  59. #if __riscv_xlen == 32
  60. .long 0 /* BaseOfData */
  61. #endif
  62. extra_header_fields:
  63. #if __riscv_xlen == 32
  64. .long 0 /* ImageBase */
  65. #else
  66. .quad 0 /* ImageBase */
  67. #endif
  68. .long 0x20 /* SectionAlignment */
  69. .long 0x8 /* FileAlignment */
  70. .short 0 /* MajorOperatingSystemVersion */
  71. .short 0 /* MinorOperatingSystemVersion */
  72. .short 1 /* MajorImageVersion */
  73. .short 0 /* MinorImageVersion */
  74. .short 0 /* MajorSubsystemVersion */
  75. .short 0 /* MinorSubsystemVersion */
  76. .long 0 /* Win32VersionValue */
  77. .long _edata - ImageBase /* SizeOfImage */
  78. /*
  79. * Everything before the kernel image is considered part of the header
  80. */
  81. .long _start - ImageBase /* SizeOfHeaders */
  82. .long 0 /* CheckSum */
  83. .short IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
  84. .short 0 /* DllCharacteristics */
  85. #if __riscv_xlen == 32
  86. .long 0 /* SizeOfStackReserve */
  87. .long 0 /* SizeOfStackCommit */
  88. .long 0 /* SizeOfHeapReserve */
  89. .long 0 /* SizeOfHeapCommit */
  90. #else
  91. .quad 0 /* SizeOfStackReserve */
  92. .quad 0 /* SizeOfStackCommit */
  93. .quad 0 /* SizeOfHeapReserve */
  94. .quad 0 /* SizeOfHeapCommit */
  95. #endif
  96. .long 0 /* LoaderFlags */
  97. .long 0x6 /* NumberOfRvaAndSizes */
  98. .quad 0 /* ExportTable */
  99. .quad 0 /* ImportTable */
  100. .quad 0 /* ResourceTable */
  101. .quad 0 /* ExceptionTable */
  102. .quad 0 /* CertificationTable */
  103. .quad 0 /* BaseRelocationTable */
  104. .quad 0 /* Debug */
  105. .quad 0 /* Architecture */
  106. .quad 0 /* Global Ptr */
  107. .quad 0 /* TLS Table */
  108. .quad 0 /* Load Config Table */
  109. .quad 0 /* Bound Import */
  110. .quad 0 /* IAT */
  111. .quad 0 /* Delay Import Descriptor */
  112. .quad 0 /* CLR Runtime Header */
  113. .quad 0 /* Reserved */
  114. /* Section table */
  115. section_table:
  116. /*
  117. * The EFI application loader requires a relocation section
  118. * because EFI applications must be relocatable. This is a
  119. * dummy section as far as we are concerned.
  120. */
  121. .ascii ".reloc"
  122. .byte 0
  123. .byte 0 /* end of 0 padding of section name */
  124. .long 0
  125. .long 0
  126. .long 0 /* SizeOfRawData */
  127. .long 0 /* PointerToRawData */
  128. .long 0 /* PointerToRelocations */
  129. .long 0 /* PointerToLineNumbers */
  130. .short 0 /* NumberOfRelocations */
  131. .short 0 /* NumberOfLineNumbers */
  132. .long 0x42100040 /* Characteristics (section flags) */
  133. .ascii ".text"
  134. .byte 0
  135. .byte 0
  136. .byte 0 /* end of 0 padding of section name */
  137. .long _edata - _start /* VirtualSize */
  138. .long _start - ImageBase /* VirtualAddress */
  139. .long _edata - _start /* SizeOfRawData */
  140. .long _start - ImageBase /* PointerToRawData */
  141. .long 0 /* PointerToRelocations (0 for executables) */
  142. .long 0 /* PointerToLineNumbers (0 for executables) */
  143. .short 0 /* NumberOfRelocations (0 for executables) */
  144. .short 0 /* NumberOfLineNumbers (0 for executables) */
  145. .long 0xe0500020 /* Characteristics (section flags) */
  146. _start:
  147. addi sp, sp, -(SIZE_LONG * 3)
  148. SAVE_LONG(a0, 0)
  149. SAVE_LONG(a1, 1)
  150. SAVE_LONG(ra, 2)
  151. lla a0, ImageBase
  152. lla a1, _DYNAMIC
  153. call _relocate
  154. bne a0, zero, 0f
  155. LOAD_LONG(a1, 1)
  156. LOAD_LONG(a0, 0)
  157. call efi_main
  158. LOAD_LONG(ra, 2)
  159. 0: addi sp, sp, (SIZE_LONG * 3)
  160. ret