]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c
MdePkg/BasePeCoff: Add RISC-V PE/Coff related code.
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / RiscV / PeCoffLoaderEx.c
CommitLineData
54a3d5ec
AC
1/** @file\r
2 PE/Coff loader for RISC-V PE image\r
3\r
4 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6**/\r
7#include "BasePeCoffLibInternals.h"\r
8#include <Library/BaseLib.h>\r
9\r
10/**\r
11 Performs an RISC-V specific relocation fixup and is a no-op on\r
12 other instruction sets.\r
13 RISC-V splits 32-bit fixup into 20bit and 12-bit with two relocation\r
14 types. We have to know the lower 12-bit fixup first then we can deal\r
15 carry over on high 20-bit fixup. So we log the high 20-bit in\r
16 FixupData.\r
17\r
18 @param Reloc The pointer to the relocation record.\r
19 @param Fixup The pointer to the address to fix up.\r
20 @param FixupData The pointer to a buffer to log the fixups.\r
21 @param Adjust The offset to adjust the fixup.\r
22\r
23 @return Status code.\r
24\r
25**/\r
26RETURN_STATUS\r
27PeCoffLoaderRelocateImageEx (\r
28 IN UINT16 *Reloc,\r
29 IN OUT CHAR8 *Fixup,\r
30 IN OUT CHAR8 **FixupData,\r
31 IN UINT64 Adjust\r
32 )\r
33{\r
34 UINT32 Value;\r
35 UINT32 Value2;\r
36 UINT32 *RiscVHi20Fixup;\r
37\r
38 switch ((*Reloc) >> 12) {\r
39 case EFI_IMAGE_REL_BASED_RISCV_HI20:\r
40 *(UINT64 *)(*FixupData) = (UINT64)(UINTN)Fixup;\r
41 break;\r
42\r
43 case EFI_IMAGE_REL_BASED_RISCV_LOW12I:\r
44 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));\r
45 if (RiscVHi20Fixup != NULL) {\r
46\r
47 Value = (UINT32)(RV_X(*RiscVHi20Fixup, 12, 20) << 12);\r
48 Value2 = (UINT32)(RV_X(*(UINT32 *)Fixup, 20, 12));\r
49 if (Value2 & (RISCV_IMM_REACH/2)) {\r
50 Value2 |= ~(RISCV_IMM_REACH-1);\r
51 }\r
52 Value += Value2;\r
53 Value += (UINT32)Adjust;\r
54 Value2 = RISCV_CONST_HIGH_PART (Value);\r
55 *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) |\\r
56 (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));\r
57 *(UINT32 *)Fixup = (RV_X (Value, 0, 12) << 20) |\\r
58 (RV_X (*(UINT32 *)Fixup, 0, 20));\r
59 }\r
60 break;\r
61\r
62 case EFI_IMAGE_REL_BASED_RISCV_LOW12S:\r
63 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));\r
64 if (RiscVHi20Fixup != NULL) {\r
65 Value = (UINT32)(RV_X(*RiscVHi20Fixup, 12, 20) << 12);\r
66 Value2 = (UINT32)(RV_X(*(UINT32 *)Fixup, 7, 5) | (RV_X(*(UINT32 *)Fixup, 25, 7) << 5));\r
67 if (Value2 & (RISCV_IMM_REACH/2)) {\r
68 Value2 |= ~(RISCV_IMM_REACH-1);\r
69 }\r
70 Value += Value2;\r
71 Value += (UINT32)Adjust;\r
72 Value2 = RISCV_CONST_HIGH_PART (Value);\r
73 *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) | \\r
74 (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));\r
75 Value2 = *(UINT32 *)Fixup & 0x01fff07f;\r
76 Value &= RISCV_IMM_REACH - 1;\r
77 *(UINT32 *)Fixup = Value2 | (UINT32)(((RV_X(Value, 0, 5) << 7) | (RV_X(Value, 5, 7) << 25)));\r
78 }\r
79 break;\r
80\r
81 default:\r
82 return RETURN_UNSUPPORTED;\r
83\r
84 }\r
85 return RETURN_SUCCESS;\r
86}\r
87\r
88/**\r
89 Returns TRUE if the machine type of PE/COFF image is supported. Supported\r
90 does not mean the image can be executed it means the PE/COFF loader supports\r
91 loading and relocating of the image type. It's up to the caller to support\r
92 the entry point.\r
93\r
94 @param Machine Machine type from the PE Header.\r
95\r
96 @return TRUE if this PE/COFF loader can load the image\r
97\r
98**/\r
99BOOLEAN\r
100PeCoffLoaderImageFormatSupported (\r
101 IN UINT16 Machine\r
102 )\r
103{\r
104 if (Machine == IMAGE_FILE_MACHINE_RISCV64) {\r
105 return TRUE;\r
106 }\r
107\r
108 return FALSE;\r
109}\r
110\r
111/**\r
112 Performs an Itanium-based specific re-relocation fixup and is a no-op on other\r
113 instruction sets. This is used to re-relocated the image into the EFI virtual\r
114 space for runtime calls.\r
115\r
116 @param Reloc The pointer to the relocation record.\r
117 @param Fixup The pointer to the address to fix up.\r
118 @param FixupData The pointer to a buffer to log the fixups.\r
119 @param Adjust The offset to adjust the fixup.\r
120\r
121 @return Status code.\r
122\r
123**/\r
124RETURN_STATUS\r
125PeHotRelocateImageEx (\r
126 IN UINT16 *Reloc,\r
127 IN OUT CHAR8 *Fixup,\r
128 IN OUT CHAR8 **FixupData,\r
129 IN UINT64 Adjust\r
130 )\r
131{\r
132 return RETURN_UNSUPPORTED;\r
133}\r