]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c
MdePkg/BasePeCoff: Add RISC-V PE/Coff related code.
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / RiscV / PeCoffLoaderEx.c
diff --git a/MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c
new file mode 100644 (file)
index 0000000..23170a6
--- /dev/null
@@ -0,0 +1,133 @@
+/** @file\r
+  PE/Coff loader for RISC-V PE image\r
+\r
+  Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+#include "BasePeCoffLibInternals.h"\r
+#include <Library/BaseLib.h>\r
+\r
+/**\r
+  Performs an RISC-V specific relocation fixup and is a no-op on\r
+  other instruction sets.\r
+  RISC-V splits 32-bit fixup into 20bit and 12-bit with two relocation\r
+  types. We have to know the lower 12-bit fixup first then we can deal\r
+  carry over on high 20-bit fixup. So we log the high 20-bit in\r
+  FixupData.\r
+\r
+  @param  Reloc       The pointer to the relocation record.\r
+  @param  Fixup       The pointer to the address to fix up.\r
+  @param  FixupData   The pointer to a buffer to log the fixups.\r
+  @param  Adjust      The offset to adjust the fixup.\r
+\r
+  @return Status code.\r
+\r
+**/\r
+RETURN_STATUS\r
+PeCoffLoaderRelocateImageEx (\r
+  IN UINT16      *Reloc,\r
+  IN OUT CHAR8   *Fixup,\r
+  IN OUT CHAR8   **FixupData,\r
+  IN UINT64      Adjust\r
+  )\r
+{\r
+  UINT32 Value;\r
+  UINT32 Value2;\r
+  UINT32 *RiscVHi20Fixup;\r
+\r
+  switch ((*Reloc) >> 12) {\r
+  case EFI_IMAGE_REL_BASED_RISCV_HI20:\r
+      *(UINT64 *)(*FixupData) = (UINT64)(UINTN)Fixup;\r
+      break;\r
+\r
+  case EFI_IMAGE_REL_BASED_RISCV_LOW12I:\r
+      RiscVHi20Fixup =  (UINT32 *)(*(UINT64 *)(*FixupData));\r
+      if (RiscVHi20Fixup != NULL) {\r
+\r
+        Value = (UINT32)(RV_X(*RiscVHi20Fixup, 12, 20) << 12);\r
+        Value2 = (UINT32)(RV_X(*(UINT32 *)Fixup, 20, 12));\r
+        if (Value2 & (RISCV_IMM_REACH/2)) {\r
+          Value2 |= ~(RISCV_IMM_REACH-1);\r
+        }\r
+        Value += Value2;\r
+        Value += (UINT32)Adjust;\r
+        Value2 = RISCV_CONST_HIGH_PART (Value);\r
+        *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) |\\r
+                                           (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));\r
+        *(UINT32 *)Fixup = (RV_X (Value, 0, 12) << 20) |\\r
+                           (RV_X (*(UINT32 *)Fixup, 0, 20));\r
+      }\r
+      break;\r
+\r
+  case EFI_IMAGE_REL_BASED_RISCV_LOW12S:\r
+      RiscVHi20Fixup =  (UINT32 *)(*(UINT64 *)(*FixupData));\r
+      if (RiscVHi20Fixup != NULL) {\r
+        Value = (UINT32)(RV_X(*RiscVHi20Fixup, 12, 20) << 12);\r
+        Value2 = (UINT32)(RV_X(*(UINT32 *)Fixup, 7, 5) | (RV_X(*(UINT32 *)Fixup, 25, 7) << 5));\r
+        if (Value2 & (RISCV_IMM_REACH/2)) {\r
+          Value2 |= ~(RISCV_IMM_REACH-1);\r
+        }\r
+        Value += Value2;\r
+        Value += (UINT32)Adjust;\r
+        Value2 = RISCV_CONST_HIGH_PART (Value);\r
+        *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) | \\r
+                                           (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));\r
+        Value2 = *(UINT32 *)Fixup & 0x01fff07f;\r
+        Value &= RISCV_IMM_REACH - 1;\r
+        *(UINT32 *)Fixup = Value2 | (UINT32)(((RV_X(Value, 0, 5) << 7) | (RV_X(Value, 5, 7) << 25)));\r
+      }\r
+      break;\r
+\r
+  default:\r
+      return RETURN_UNSUPPORTED;\r
+\r
+  }\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Returns TRUE if the machine type of PE/COFF image is supported. Supported\r
+  does not mean the image can be executed it means the PE/COFF loader supports\r
+  loading and relocating of the image type. It's up to the caller to support\r
+  the entry point.\r
+\r
+  @param  Machine   Machine type from the PE Header.\r
+\r
+  @return TRUE if this PE/COFF loader can load the image\r
+\r
+**/\r
+BOOLEAN\r
+PeCoffLoaderImageFormatSupported (\r
+  IN  UINT16  Machine\r
+  )\r
+{\r
+  if (Machine ==  IMAGE_FILE_MACHINE_RISCV64) {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Performs an Itanium-based specific re-relocation fixup and is a no-op on other\r
+  instruction sets. This is used to re-relocated the image into the EFI virtual\r
+  space for runtime calls.\r
+\r
+  @param  Reloc       The pointer to the relocation record.\r
+  @param  Fixup       The pointer to the address to fix up.\r
+  @param  FixupData   The pointer to a buffer to log the fixups.\r
+  @param  Adjust      The offset to adjust the fixup.\r
+\r
+  @return Status code.\r
+\r
+**/\r
+RETURN_STATUS\r
+PeHotRelocateImageEx (\r
+  IN UINT16      *Reloc,\r
+  IN OUT CHAR8   *Fixup,\r
+  IN OUT CHAR8   **FixupData,\r
+  IN UINT64      Adjust\r
+  )\r
+{\r
+  return RETURN_UNSUPPORTED;\r
+}\r