]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/UefiMemoryLib/MemLibGeneric.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / UefiMemoryLib / MemLibGeneric.c
... / ...
CommitLineData
1/** @file\r
2 Architecture Independent Base Memory Library Implementation.\r
3\r
4 The following BaseMemoryLib instances contain the same copy of this file:\r
5 BaseMemoryLib\r
6 PeiMemoryLib\r
7 UefiMemoryLib\r
8\r
9 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
10 SPDX-License-Identifier: BSD-2-Clause-Patent\r
11\r
12**/\r
13\r
14#include "MemLibInternals.h"\r
15\r
16/**\r
17 Fills a target buffer with a 16-bit value, and returns the target buffer.\r
18\r
19 @param Buffer The pointer to the target buffer to fill.\r
20 @param Length The count of 16-bit value to fill.\r
21 @param Value The value with which to fill Length bytes of Buffer.\r
22\r
23 @return Buffer\r
24\r
25**/\r
26VOID *\r
27EFIAPI\r
28InternalMemSetMem16 (\r
29 OUT VOID *Buffer,\r
30 IN UINTN Length,\r
31 IN UINT16 Value\r
32 )\r
33{\r
34 for ( ; Length != 0; Length--) {\r
35 ((UINT16 *)Buffer)[Length - 1] = Value;\r
36 }\r
37\r
38 return Buffer;\r
39}\r
40\r
41/**\r
42 Fills a target buffer with a 32-bit value, and returns the target buffer.\r
43\r
44 @param Buffer The pointer to the target buffer to fill.\r
45 @param Length The count of 32-bit value to fill.\r
46 @param Value The value with which to fill Length bytes of Buffer.\r
47\r
48 @return Buffer\r
49\r
50**/\r
51VOID *\r
52EFIAPI\r
53InternalMemSetMem32 (\r
54 OUT VOID *Buffer,\r
55 IN UINTN Length,\r
56 IN UINT32 Value\r
57 )\r
58{\r
59 for ( ; Length != 0; Length--) {\r
60 ((UINT32 *)Buffer)[Length - 1] = Value;\r
61 }\r
62\r
63 return Buffer;\r
64}\r
65\r
66/**\r
67 Fills a target buffer with a 64-bit value, and returns the target buffer.\r
68\r
69 @param Buffer The pointer to the target buffer to fill.\r
70 @param Length The count of 64-bit value to fill.\r
71 @param Value The value with which to fill Length bytes of Buffer.\r
72\r
73 @return Buffer\r
74\r
75**/\r
76VOID *\r
77EFIAPI\r
78InternalMemSetMem64 (\r
79 OUT VOID *Buffer,\r
80 IN UINTN Length,\r
81 IN UINT64 Value\r
82 )\r
83{\r
84 for ( ; Length != 0; Length--) {\r
85 ((UINT64 *)Buffer)[Length - 1] = Value;\r
86 }\r
87\r
88 return Buffer;\r
89}\r
90\r
91/**\r
92 Set Buffer to 0 for Size bytes.\r
93\r
94 @param Buffer Memory to set.\r
95 @param Length The number of bytes to set\r
96\r
97 @return Buffer\r
98\r
99**/\r
100VOID *\r
101EFIAPI\r
102InternalMemZeroMem (\r
103 OUT VOID *Buffer,\r
104 IN UINTN Length\r
105 )\r
106{\r
107 return InternalMemSetMem (Buffer, Length, 0);\r
108}\r
109\r
110/**\r
111 Compares two memory buffers of a given length.\r
112\r
113 @param DestinationBuffer The first memory buffer\r
114 @param SourceBuffer The second memory buffer\r
115 @param Length The length of DestinationBuffer and SourceBuffer memory\r
116 regions to compare. Must be non-zero.\r
117\r
118 @return 0 All Length bytes of the two buffers are identical.\r
119 @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first\r
120 mismatched byte in DestinationBuffer.\r
121\r
122**/\r
123INTN\r
124EFIAPI\r
125InternalMemCompareMem (\r
126 IN CONST VOID *DestinationBuffer,\r
127 IN CONST VOID *SourceBuffer,\r
128 IN UINTN Length\r
129 )\r
130{\r
131 while ((--Length != 0) &&\r
132 (*(INT8 *)DestinationBuffer == *(INT8 *)SourceBuffer))\r
133 {\r
134 DestinationBuffer = (INT8 *)DestinationBuffer + 1;\r
135 SourceBuffer = (INT8 *)SourceBuffer + 1;\r
136 }\r
137\r
138 return (INTN)*(UINT8 *)DestinationBuffer - (INTN)*(UINT8 *)SourceBuffer;\r
139}\r
140\r
141/**\r
142 Scans a target buffer for an 8-bit value, and returns a pointer to the\r
143 matching 8-bit value in the target buffer.\r
144\r
145 @param Buffer The pointer to the target buffer to scan.\r
146 @param Length The count of 8-bit value to scan. Must be non-zero.\r
147 @param Value The value to search for in the target buffer.\r
148\r
149 @return The pointer to the first occurrence or NULL if not found.\r
150\r
151**/\r
152CONST VOID *\r
153EFIAPI\r
154InternalMemScanMem8 (\r
155 IN CONST VOID *Buffer,\r
156 IN UINTN Length,\r
157 IN UINT8 Value\r
158 )\r
159{\r
160 CONST UINT8 *Pointer;\r
161\r
162 Pointer = (CONST UINT8 *)Buffer;\r
163 do {\r
164 if (*(Pointer++) == Value) {\r
165 return --Pointer;\r
166 }\r
167 } while (--Length != 0);\r
168\r
169 return NULL;\r
170}\r
171\r
172/**\r
173 Scans a target buffer for a 16-bit value, and returns a pointer to the\r
174 matching 16-bit value in the target buffer.\r
175\r
176 @param Buffer The pointer to the target buffer to scan.\r
177 @param Length The count of 16-bit value to scan. Must be non-zero.\r
178 @param Value The value to search for in the target buffer.\r
179\r
180 @return The pointer to the first occurrence or NULL if not found.\r
181\r
182**/\r
183CONST VOID *\r
184EFIAPI\r
185InternalMemScanMem16 (\r
186 IN CONST VOID *Buffer,\r
187 IN UINTN Length,\r
188 IN UINT16 Value\r
189 )\r
190{\r
191 CONST UINT16 *Pointer;\r
192\r
193 Pointer = (CONST UINT16 *)Buffer;\r
194 do {\r
195 if (*(Pointer++) == Value) {\r
196 return --Pointer;\r
197 }\r
198 } while (--Length != 0);\r
199\r
200 return NULL;\r
201}\r
202\r
203/**\r
204 Scans a target buffer for a 32-bit value, and returns a pointer to the\r
205 matching 32-bit value in the target buffer.\r
206\r
207 @param Buffer The pointer to the target buffer to scan.\r
208 @param Length The count of 32-bit value to scan. Must be non-zero.\r
209 @param Value The value to search for in the target buffer.\r
210\r
211 @return The pointer to the first occurrence or NULL if not found.\r
212\r
213**/\r
214CONST VOID *\r
215EFIAPI\r
216InternalMemScanMem32 (\r
217 IN CONST VOID *Buffer,\r
218 IN UINTN Length,\r
219 IN UINT32 Value\r
220 )\r
221{\r
222 CONST UINT32 *Pointer;\r
223\r
224 Pointer = (CONST UINT32 *)Buffer;\r
225 do {\r
226 if (*(Pointer++) == Value) {\r
227 return --Pointer;\r
228 }\r
229 } while (--Length != 0);\r
230\r
231 return NULL;\r
232}\r
233\r
234/**\r
235 Scans a target buffer for a 64-bit value, and returns a pointer to the\r
236 matching 64-bit value in the target buffer.\r
237\r
238 @param Buffer The pointer to the target buffer to scan.\r
239 @param Length The count of 64-bit value to scan. Must be non-zero.\r
240 @param Value The value to search for in the target buffer.\r
241\r
242 @return The pointer to the first occurrence or NULL if not found.\r
243\r
244**/\r
245CONST VOID *\r
246EFIAPI\r
247InternalMemScanMem64 (\r
248 IN CONST VOID *Buffer,\r
249 IN UINTN Length,\r
250 IN UINT64 Value\r
251 )\r
252{\r
253 CONST UINT64 *Pointer;\r
254\r
255 Pointer = (CONST UINT64 *)Buffer;\r
256 do {\r
257 if (*(Pointer++) == Value) {\r
258 return --Pointer;\r
259 }\r
260 } while (--Length != 0);\r
261\r
262 return NULL;\r
263}\r
264\r
265/**\r
266 Checks whether the contents of a buffer are all zeros.\r
267\r
268 @param Buffer The pointer to the buffer to be checked.\r
269 @param Length The size of the buffer (in bytes) to be checked.\r
270\r
271 @retval TRUE Contents of the buffer are all zeros.\r
272 @retval FALSE Contents of the buffer are not all zeros.\r
273\r
274**/\r
275BOOLEAN\r
276EFIAPI\r
277InternalMemIsZeroBuffer (\r
278 IN CONST VOID *Buffer,\r
279 IN UINTN Length\r
280 )\r
281{\r
282 CONST UINT8 *BufferData;\r
283 UINTN Index;\r
284\r
285 BufferData = Buffer;\r
286 for (Index = 0; Index < Length; Index++) {\r
287 if (BufferData[Index] != 0) {\r
288 return FALSE;\r
289 }\r
290 }\r
291\r
292 return TRUE;\r
293}\r