crt0_riscv_efi.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 0x5064
  16. #else
  17. #define SIZE_LONG 4
  18. #define SAVE_LONG(reg, idx) sw reg, (idx*SIZE_LONG)(sp)
  19. #define LOAD_LONG(reg, idx) lw reg, (idx*SIZE_LONG)(sp)
  20. #define PE_MACHINE 0x5032
  21. #endif
  22. .section .text.head
  23. /*
  24. * Magic "MZ" signature for PE/COFF
  25. */
  26. .globl ImageBase
  27. ImageBase:
  28. .ascii "MZ"
  29. .skip 58 /* 'MZ' + pad + offset == 64 */
  30. .long pe_header - ImageBase /* Offset to the PE header */
  31. pe_header:
  32. .ascii "PE"
  33. .short 0
  34. coff_header:
  35. .short PE_MACHINE /* RISC-V 64/32-bit */
  36. .short 2 /* nr_sections */
  37. .long 0 /* TimeDateStamp */
  38. .long 0 /* PointerToSymbolTable */
  39. .long 1 /* NumberOfSymbols */
  40. .short section_table - optional_header /* SizeOfOptionalHeader */
  41. /*
  42. * Characteristics: IMAGE_FILE_DEBUG_STRIPPED |
  43. * IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LINE_NUMS_STRIPPED
  44. */
  45. .short 0x206
  46. optional_header:
  47. .short 0x20b /* PE32+ format */
  48. .byte 0x02 /* MajorLinkerVersion */
  49. .byte 0x14 /* MinorLinkerVersion */
  50. .long _edata - _start /* SizeOfCode */
  51. .long 0 /* SizeOfInitializedData */
  52. .long 0 /* SizeOfUninitializedData */
  53. .long _start - ImageBase /* AddressOfEntryPoint */
  54. .long _start - ImageBase /* BaseOfCode */
  55. extra_header_fields:
  56. .quad 0 /* ImageBase */
  57. .long 0x20 /* SectionAlignment */
  58. .long 0x8 /* FileAlignment */
  59. .short 0 /* MajorOperatingSystemVersion */
  60. .short 0 /* MinorOperatingSystemVersion */
  61. .short 0 /* MajorImageVersion */
  62. .short 0 /* MinorImageVersion */
  63. .short 0 /* MajorSubsystemVersion */
  64. .short 0 /* MinorSubsystemVersion */
  65. .long 0 /* Win32VersionValue */
  66. .long _edata - ImageBase /* SizeOfImage */
  67. /*
  68. * Everything before the kernel image is considered part of the header
  69. */
  70. .long _start - ImageBase /* SizeOfHeaders */
  71. .long 0 /* CheckSum */
  72. .short IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
  73. .short 0 /* DllCharacteristics */
  74. .quad 0 /* SizeOfStackReserve */
  75. .quad 0 /* SizeOfStackCommit */
  76. .quad 0 /* SizeOfHeapReserve */
  77. .quad 0 /* SizeOfHeapCommit */
  78. .long 0 /* LoaderFlags */
  79. .long 0x6 /* NumberOfRvaAndSizes */
  80. .quad 0 /* ExportTable */
  81. .quad 0 /* ImportTable */
  82. .quad 0 /* ResourceTable */
  83. .quad 0 /* ExceptionTable */
  84. .quad 0 /* CertificationTable */
  85. .quad 0 /* BaseRelocationTable */
  86. /* Section table */
  87. section_table:
  88. /*
  89. * The EFI application loader requires a relocation section
  90. * because EFI applications must be relocatable. This is a
  91. * dummy section as far as we are concerned.
  92. */
  93. .ascii ".reloc"
  94. .byte 0
  95. .byte 0 /* end of 0 padding of section name */
  96. .long 0
  97. .long 0
  98. .long 0 /* SizeOfRawData */
  99. .long 0 /* PointerToRawData */
  100. .long 0 /* PointerToRelocations */
  101. .long 0 /* PointerToLineNumbers */
  102. .short 0 /* NumberOfRelocations */
  103. .short 0 /* NumberOfLineNumbers */
  104. .long 0x42100040 /* Characteristics (section flags) */
  105. .ascii ".text"
  106. .byte 0
  107. .byte 0
  108. .byte 0 /* end of 0 padding of section name */
  109. .long _edata - _start /* VirtualSize */
  110. .long _start - ImageBase /* VirtualAddress */
  111. .long _edata - _start /* SizeOfRawData */
  112. .long _start - ImageBase /* PointerToRawData */
  113. .long 0 /* PointerToRelocations (0 for executables) */
  114. .long 0 /* PointerToLineNumbers (0 for executables) */
  115. .short 0 /* NumberOfRelocations (0 for executables) */
  116. .short 0 /* NumberOfLineNumbers (0 for executables) */
  117. .long 0xe0500020 /* Characteristics (section flags) */
  118. _start:
  119. addi sp, sp, -(SIZE_LONG * 3)
  120. SAVE_LONG(a0, 0)
  121. SAVE_LONG(a1, 1)
  122. SAVE_LONG(ra, 2)
  123. lla a0, ImageBase
  124. lla a1, _DYNAMIC
  125. call _relocate
  126. bne a0, zero, 0f
  127. LOAD_LONG(a1, 1)
  128. LOAD_LONG(a0, 0)
  129. call efi_main
  130. LOAD_LONG(ra, 2)
  131. 0: addi sp, sp, (SIZE_LONG * 3)
  132. ret