]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c
MdePkg: Apply uncrustify changes
[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
2f88bd3a
MK
28 IN UINT16 *Reloc,\r
29 IN OUT CHAR8 *Fixup,\r
30 IN OUT CHAR8 **FixupData,\r
31 IN UINT64 Adjust\r
54a3d5ec
AC
32 )\r
33{\r
2f88bd3a
MK
34 UINT32 Value;\r
35 UINT32 Value2;\r
36 UINT32 *RiscVHi20Fixup;\r
54a3d5ec
AC
37\r
38 switch ((*Reloc) >> 12) {\r
2f88bd3a 39 case EFI_IMAGE_REL_BASED_RISCV_HI20:\r
54a3d5ec
AC
40 *(UINT64 *)(*FixupData) = (UINT64)(UINTN)Fixup;\r
41 break;\r
42\r
2f88bd3a 43 case EFI_IMAGE_REL_BASED_RISCV_LOW12I:\r
54a3d5ec
AC
44 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));\r
45 if (RiscVHi20Fixup != NULL) {\r
2f88bd3a
MK
46 Value = (UINT32)(RV_X (*RiscVHi20Fixup, 12, 20) << 12);\r
47 Value2 = (UINT32)(RV_X (*(UINT32 *)Fixup, 20, 12));\r
54a3d5ec
AC
48 if (Value2 & (RISCV_IMM_REACH/2)) {\r
49 Value2 |= ~(RISCV_IMM_REACH-1);\r
50 }\r
2f88bd3a
MK
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
54a3d5ec
AC
58 (RV_X (*(UINT32 *)Fixup, 0, 20));\r
59 }\r
2f88bd3a 60\r
54a3d5ec
AC
61 break;\r
62\r
2f88bd3a 63 case EFI_IMAGE_REL_BASED_RISCV_LOW12S:\r
54a3d5ec
AC
64 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));\r
65 if (RiscVHi20Fixup != NULL) {\r
2f88bd3a
MK
66 Value = (UINT32)(RV_X (*RiscVHi20Fixup, 12, 20) << 12);\r
67 Value2 = (UINT32)(RV_X (*(UINT32 *)Fixup, 7, 5) | (RV_X (*(UINT32 *)Fixup, 25, 7) << 5));\r
54a3d5ec
AC
68 if (Value2 & (RISCV_IMM_REACH/2)) {\r
69 Value2 |= ~(RISCV_IMM_REACH-1);\r
70 }\r
2f88bd3a
MK
71\r
72 Value += Value2;\r
73 Value += (UINT32)Adjust;\r
74 Value2 = RISCV_CONST_HIGH_PART (Value);\r
54a3d5ec 75 *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) | \\r
2f88bd3a
MK
76 (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));\r
77 Value2 = *(UINT32 *)Fixup & 0x01fff07f;\r
78 Value &= RISCV_IMM_REACH - 1;\r
79 *(UINT32 *)Fixup = Value2 | (UINT32)(((RV_X (Value, 0, 5) << 7) | (RV_X (Value, 5, 7) << 25)));\r
54a3d5ec 80 }\r
2f88bd3a 81\r
54a3d5ec
AC
82 break;\r
83\r
2f88bd3a 84 default:\r
54a3d5ec 85 return RETURN_UNSUPPORTED;\r
54a3d5ec 86 }\r
2f88bd3a 87\r
54a3d5ec
AC
88 return RETURN_SUCCESS;\r
89}\r
90\r
91/**\r
92 Returns TRUE if the machine type of PE/COFF image is supported. Supported\r
93 does not mean the image can be executed it means the PE/COFF loader supports\r
94 loading and relocating of the image type. It's up to the caller to support\r
95 the entry point.\r
96\r
97 @param Machine Machine type from the PE Header.\r
98\r
99 @return TRUE if this PE/COFF loader can load the image\r
100\r
101**/\r
102BOOLEAN\r
103PeCoffLoaderImageFormatSupported (\r
104 IN UINT16 Machine\r
105 )\r
106{\r
107 if (Machine == IMAGE_FILE_MACHINE_RISCV64) {\r
108 return TRUE;\r
109 }\r
110\r
111 return FALSE;\r
112}\r
113\r
114/**\r
115 Performs an Itanium-based specific re-relocation fixup and is a no-op on other\r
116 instruction sets. This is used to re-relocated the image into the EFI virtual\r
117 space for runtime calls.\r
118\r
119 @param Reloc The pointer to the relocation record.\r
120 @param Fixup The pointer to the address to fix up.\r
121 @param FixupData The pointer to a buffer to log the fixups.\r
122 @param Adjust The offset to adjust the fixup.\r
123\r
124 @return Status code.\r
125\r
126**/\r
127RETURN_STATUS\r
128PeHotRelocateImageEx (\r
2f88bd3a
MK
129 IN UINT16 *Reloc,\r
130 IN OUT CHAR8 *Fixup,\r
131 IN OUT CHAR8 **FixupData,\r
132 IN UINT64 Adjust\r
54a3d5ec
AC
133 )\r
134{\r
135 return RETURN_UNSUPPORTED;\r
136}\r