]>
Commit | Line | Data |
---|---|---|
e1f414b6 | 1 | /** @file\r |
2 | Architecture Independent Base Memory Library Implementation.\r | |
3 | \r | |
d531bfee | 4 | The following BaseMemoryLib instances contain the same copy of this file:\r |
5 | BaseMemoryLib\r | |
6 | PeiMemoryLib\r | |
1fef058f | 7 | UefiMemoryLib\r |
d531bfee | 8 | \r |
1944b02b | 9 | Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r |
9344f092 | 10 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
e1f414b6 | 11 | \r |
e1f414b6 | 12 | **/\r |
13 | \r | |
e1f414b6 | 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 | |
15c952e7 | 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 | |
e1f414b6 | 22 | \r |
23 | @return Buffer\r | |
24 | \r | |
25 | **/\r | |
26 | VOID *\r | |
27 | EFIAPI\r | |
28 | InternalMemSetMem16 (\r | |
2f88bd3a MK |
29 | OUT VOID *Buffer,\r |
30 | IN UINTN Length,\r | |
31 | IN UINT16 Value\r | |
e1f414b6 | 32 | )\r |
33 | {\r | |
2f88bd3a MK |
34 | for ( ; Length != 0; Length--) {\r |
35 | ((UINT16 *)Buffer)[Length - 1] = Value;\r | |
9088c61e | 36 | }\r |
2f88bd3a | 37 | \r |
e1f414b6 | 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 | |
15c952e7 | 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 | |
e1f414b6 | 47 | \r |
48 | @return Buffer\r | |
49 | \r | |
50 | **/\r | |
51 | VOID *\r | |
52 | EFIAPI\r | |
53 | InternalMemSetMem32 (\r | |
2f88bd3a MK |
54 | OUT VOID *Buffer,\r |
55 | IN UINTN Length,\r | |
56 | IN UINT32 Value\r | |
e1f414b6 | 57 | )\r |
58 | {\r | |
2f88bd3a MK |
59 | for ( ; Length != 0; Length--) {\r |
60 | ((UINT32 *)Buffer)[Length - 1] = Value;\r | |
9088c61e | 61 | }\r |
2f88bd3a | 62 | \r |
e1f414b6 | 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 | |
15c952e7 | 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 | |
e1f414b6 | 72 | \r |
73 | @return Buffer\r | |
74 | \r | |
75 | **/\r | |
76 | VOID *\r | |
77 | EFIAPI\r | |
78 | InternalMemSetMem64 (\r | |
2f88bd3a MK |
79 | OUT VOID *Buffer,\r |
80 | IN UINTN Length,\r | |
81 | IN UINT64 Value\r | |
e1f414b6 | 82 | )\r |
83 | {\r | |
2f88bd3a MK |
84 | for ( ; Length != 0; Length--) {\r |
85 | ((UINT64 *)Buffer)[Length - 1] = Value;\r | |
9088c61e | 86 | }\r |
2f88bd3a | 87 | \r |
e1f414b6 | 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 | |
2fc59a00 | 95 | @param Length The number of bytes to set.\r |
e1f414b6 | 96 | \r |
97 | @return Buffer\r | |
98 | \r | |
99 | **/\r | |
100 | VOID *\r | |
101 | EFIAPI\r | |
102 | InternalMemZeroMem (\r | |
2f88bd3a MK |
103 | OUT VOID *Buffer,\r |
104 | IN UINTN Length\r | |
e1f414b6 | 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 | |
15c952e7 | 113 | @param DestinationBuffer The first memory buffer.\r |
114 | @param SourceBuffer The second memory buffer.\r | |
e1f414b6 | 115 | @param Length Length of DestinationBuffer and SourceBuffer memory\r |
116 | regions to compare. Must be non-zero.\r | |
117 | \r | |
d531bfee | 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 | |
e1f414b6 | 121 | \r |
122 | **/\r | |
123 | INTN\r | |
124 | EFIAPI\r | |
125 | InternalMemCompareMem (\r | |
2f88bd3a MK |
126 | IN CONST VOID *DestinationBuffer,\r |
127 | IN CONST VOID *SourceBuffer,\r | |
128 | IN UINTN Length\r | |
e1f414b6 | 129 | )\r |
130 | {\r | |
131 | while ((--Length != 0) &&\r | |
2f88bd3a MK |
132 | (*(INT8 *)DestinationBuffer == *(INT8 *)SourceBuffer))\r |
133 | {\r | |
134 | DestinationBuffer = (INT8 *)DestinationBuffer + 1;\r | |
135 | SourceBuffer = (INT8 *)SourceBuffer + 1;\r | |
e1f414b6 | 136 | }\r |
2f88bd3a MK |
137 | \r |
138 | return (INTN)*(UINT8 *)DestinationBuffer - (INTN)*(UINT8 *)SourceBuffer;\r | |
e1f414b6 | 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 | |
15c952e7 | 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 | |
e1f414b6 | 148 | \r |
15c952e7 | 149 | @return The pointer to the first occurrence, or NULL if not found.\r |
e1f414b6 | 150 | \r |
151 | **/\r | |
152 | CONST VOID *\r | |
153 | EFIAPI\r | |
154 | InternalMemScanMem8 (\r | |
2f88bd3a MK |
155 | IN CONST VOID *Buffer,\r |
156 | IN UINTN Length,\r | |
157 | IN UINT8 Value\r | |
e1f414b6 | 158 | )\r |
159 | {\r | |
2f88bd3a | 160 | CONST UINT8 *Pointer;\r |
e1f414b6 | 161 | \r |
2f88bd3a | 162 | Pointer = (CONST UINT8 *)Buffer;\r |
e1f414b6 | 163 | do {\r |
af072124 | 164 | if (*Pointer == Value) {\r |
165 | return Pointer;\r | |
e1f414b6 | 166 | }\r |
2f88bd3a | 167 | \r |
af072124 | 168 | ++Pointer;\r |
e1f414b6 | 169 | } while (--Length != 0);\r |
2f88bd3a | 170 | \r |
e1f414b6 | 171 | return NULL;\r |
172 | }\r | |
173 | \r | |
174 | /**\r | |
175 | Scans a target buffer for a 16-bit value, and returns a pointer to the\r | |
176 | matching 16-bit value in the target buffer.\r | |
177 | \r | |
15c952e7 | 178 | @param Buffer The pointer to the target buffer to scan.\r |
179 | @param Length The count of 16-bit value to scan. Must be non-zero.\r | |
180 | @param Value The value to search for in the target buffer.\r | |
e1f414b6 | 181 | \r |
15c952e7 | 182 | @return The pointer to the first occurrence, or NULL if not found.\r |
e1f414b6 | 183 | \r |
184 | **/\r | |
185 | CONST VOID *\r | |
186 | EFIAPI\r | |
187 | InternalMemScanMem16 (\r | |
2f88bd3a MK |
188 | IN CONST VOID *Buffer,\r |
189 | IN UINTN Length,\r | |
190 | IN UINT16 Value\r | |
e1f414b6 | 191 | )\r |
192 | {\r | |
2f88bd3a | 193 | CONST UINT16 *Pointer;\r |
e1f414b6 | 194 | \r |
2f88bd3a | 195 | Pointer = (CONST UINT16 *)Buffer;\r |
e1f414b6 | 196 | do {\r |
af072124 | 197 | if (*Pointer == Value) {\r |
198 | return Pointer;\r | |
e1f414b6 | 199 | }\r |
2f88bd3a | 200 | \r |
af072124 | 201 | ++Pointer;\r |
e1f414b6 | 202 | } while (--Length != 0);\r |
2f88bd3a | 203 | \r |
e1f414b6 | 204 | return NULL;\r |
205 | }\r | |
206 | \r | |
207 | /**\r | |
208 | Scans a target buffer for a 32-bit value, and returns a pointer to the\r | |
209 | matching 32-bit value in the target buffer.\r | |
210 | \r | |
15c952e7 | 211 | @param Buffer The pointer to the target buffer to scan.\r |
212 | @param Length The count of 32-bit value to scan. Must be non-zero.\r | |
213 | @param Value The value to search for in the target buffer.\r | |
e1f414b6 | 214 | \r |
15c952e7 | 215 | @return The pointer to the first occurrence, or NULL if not found.\r |
e1f414b6 | 216 | \r |
217 | **/\r | |
218 | CONST VOID *\r | |
219 | EFIAPI\r | |
220 | InternalMemScanMem32 (\r | |
2f88bd3a MK |
221 | IN CONST VOID *Buffer,\r |
222 | IN UINTN Length,\r | |
223 | IN UINT32 Value\r | |
e1f414b6 | 224 | )\r |
225 | {\r | |
2f88bd3a | 226 | CONST UINT32 *Pointer;\r |
e1f414b6 | 227 | \r |
2f88bd3a | 228 | Pointer = (CONST UINT32 *)Buffer;\r |
e1f414b6 | 229 | do {\r |
af072124 | 230 | if (*Pointer == Value) {\r |
231 | return Pointer;\r | |
e1f414b6 | 232 | }\r |
2f88bd3a | 233 | \r |
af072124 | 234 | ++Pointer;\r |
e1f414b6 | 235 | } while (--Length != 0);\r |
2f88bd3a | 236 | \r |
e1f414b6 | 237 | return NULL;\r |
238 | }\r | |
239 | \r | |
240 | /**\r | |
241 | Scans a target buffer for a 64-bit value, and returns a pointer to the\r | |
242 | matching 64-bit value in the target buffer.\r | |
243 | \r | |
15c952e7 | 244 | @param Buffer The pointer to the target buffer to scan.\r |
245 | @param Length The count of 64-bit value to scan. Must be non-zero.\r | |
246 | @param Value The value to search for in the target buffer.\r | |
e1f414b6 | 247 | \r |
15c952e7 | 248 | @return The pointer to the first occurrence, or NULL if not found.\r |
e1f414b6 | 249 | \r |
250 | **/\r | |
251 | CONST VOID *\r | |
252 | EFIAPI\r | |
253 | InternalMemScanMem64 (\r | |
2f88bd3a MK |
254 | IN CONST VOID *Buffer,\r |
255 | IN UINTN Length,\r | |
256 | IN UINT64 Value\r | |
e1f414b6 | 257 | )\r |
258 | {\r | |
2f88bd3a | 259 | CONST UINT64 *Pointer;\r |
e1f414b6 | 260 | \r |
2f88bd3a | 261 | Pointer = (CONST UINT64 *)Buffer;\r |
e1f414b6 | 262 | do {\r |
af072124 | 263 | if (*Pointer == Value) {\r |
264 | return Pointer;\r | |
e1f414b6 | 265 | }\r |
2f88bd3a | 266 | \r |
af072124 | 267 | ++Pointer;\r |
e1f414b6 | 268 | } while (--Length != 0);\r |
2f88bd3a | 269 | \r |
e1f414b6 | 270 | return NULL;\r |
271 | }\r | |
1944b02b HW |
272 | \r |
273 | /**\r | |
274 | Checks whether the contents of a buffer are all zeros.\r | |
275 | \r | |
276 | @param Buffer The pointer to the buffer to be checked.\r | |
277 | @param Length The size of the buffer (in bytes) to be checked.\r | |
278 | \r | |
279 | @retval TRUE Contents of the buffer are all zeros.\r | |
280 | @retval FALSE Contents of the buffer are not all zeros.\r | |
281 | \r | |
282 | **/\r | |
283 | BOOLEAN\r | |
284 | EFIAPI\r | |
285 | InternalMemIsZeroBuffer (\r | |
286 | IN CONST VOID *Buffer,\r | |
287 | IN UINTN Length\r | |
288 | )\r | |
289 | {\r | |
2f88bd3a MK |
290 | CONST UINT8 *BufferData;\r |
291 | UINTN Index;\r | |
1944b02b HW |
292 | \r |
293 | BufferData = Buffer;\r | |
294 | for (Index = 0; Index < Length; Index++) {\r | |
295 | if (BufferData[Index] != 0) {\r | |
296 | return FALSE;\r | |
297 | }\r | |
298 | }\r | |
2f88bd3a | 299 | \r |
1944b02b HW |
300 | return TRUE;\r |
301 | }\r |