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