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