]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Sample/Tools/Source/Common/CommonLib.c
3 Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Common Library Functions
22 #include "TianoCommon.h"
27 #include "CommonLib.h"
38 Set Buffer to zero for Size bytes.
42 Buffer - Memory to set.
44 Size - Number of bytes to set
70 Copy Length bytes from Source to Destination.
74 Destination - Target of copy
76 Source - Place to copy from
78 Length - Number of bytes to copy
89 Destination8
= Destination
;
92 *(Destination8
++) = *(Source8
++);
102 PeiZeroMem (Buffer
, Size
);
107 IN VOID
*Destination
,
112 PeiCopyMem (Destination
, Source
, Length
);
128 Guid1 - guid to compare
129 Guid2 - guid to compare
132 = 0 if Guid1 == Guid2
133 != 0 if Guid1 != Guid2
142 // Compare 32 bits at a time
144 g1
= (INT32
*) Guid1
;
145 g2
= (INT32
*) Guid2
;
157 IN CHAR8
*InputFileName
,
158 OUT CHAR8
**InputFileImage
,
159 OUT UINT32
*BytesRead
165 This function opens a file and reads it into a memory buffer. The function
166 will allocate the memory buffer and returns the size of the buffer.
170 InputFileName The name of the file to read.
171 InputFileImage A pointer to the memory buffer.
172 BytesRead The size of the memory buffer.
176 EFI_SUCCESS The function completed successfully.
177 EFI_INVALID_PARAMETER One of the input parameters was invalid.
178 EFI_ABORTED An error occurred.
179 EFI_OUT_OF_RESOURCES No resource to complete operations.
187 // Verify input parameters.
189 if (InputFileName
== NULL
|| strlen (InputFileName
) == 0 || InputFileImage
== NULL
) {
190 return EFI_INVALID_PARAMETER
;
193 // Open the file and copy contents into a memory buffer.
198 InputFile
= fopen (InputFileName
, "rb");
199 if (InputFile
== NULL
) {
200 printf ("ERROR: Could not open input file \"%s\".\n", InputFileName
);
204 // Go to the end so that we can determine the file size
206 if (fseek (InputFile
, 0, SEEK_END
)) {
207 printf ("ERROR: System error reading input file \"%s\".\n", InputFileName
);
214 FileSize
= ftell (InputFile
);
215 if (FileSize
== -1) {
216 printf ("ERROR: System error parsing input file \"%s\".\n", InputFileName
);
223 *InputFileImage
= malloc (FileSize
);
224 if (*InputFileImage
== NULL
) {
226 return EFI_OUT_OF_RESOURCES
;
229 // Reset to the beginning of the file
231 if (fseek (InputFile
, 0, SEEK_SET
)) {
232 printf ("ERROR: System error reading input file \"%s\".\n", InputFileName
);
234 free (*InputFileImage
);
235 *InputFileImage
= NULL
;
239 // Read all of the file contents.
241 *BytesRead
= fread (*InputFileImage
, sizeof (UINT8
), FileSize
, InputFile
);
242 if (*BytesRead
!= sizeof (UINT8
) * FileSize
) {
243 printf ("ERROR: Reading file \"%s\"%i.\n", InputFileName
);
245 free (*InputFileImage
);
246 *InputFileImage
= NULL
;
266 This function calculates the value needed for a valid UINT8 checksum
270 Buffer Pointer to buffer containing byte data of component.
271 Size Size of the buffer
275 The 8 bit checksum value needed.
279 return (UINT8
) (0x100 - CalculateSum8 (Buffer
, Size
));
289 Routine Description::
291 This function calculates the UINT8 sum for the requested region.
295 Buffer Pointer to buffer containing byte data of component.
296 Size Size of the buffer
300 The 8 bit checksum value needed.
310 // Perform the byte sum for buffer
312 for (Index
= 0; Index
< Size
; Index
++) {
313 Sum
= (UINT8
) (Sum
+ Buffer
[Index
]);
320 CalculateChecksum16 (
326 Routine Description::
328 This function calculates the value needed for a valid UINT16 checksum
332 Buffer Pointer to buffer containing byte data of component.
333 Size Size of the buffer
337 The 16 bit checksum value needed.
341 return (UINT16
) (0x10000 - CalculateSum16 (Buffer
, Size
));
353 This function calculates the UINT16 sum for the requested region.
357 Buffer Pointer to buffer containing byte data of component.
358 Size Size of the buffer
372 // Perform the word sum for buffer
374 for (Index
= 0; Index
< Size
; Index
++) {
375 Sum
= (UINT16
) (Sum
+ Buffer
[Index
]);
389 This function prints a GUID to STDOUT.
393 Guid Pointer to a GUID to print.
397 EFI_SUCCESS The GUID was printed.
398 EFI_INVALID_PARAMETER The input was NULL.
403 printf ("ERROR: PrintGuid called with a NULL value.\n");
404 return EFI_INVALID_PARAMETER
;
408 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
427 IN OUT UINT8
*Buffer
,
435 This function prints a GUID to a buffer
439 Guid - Pointer to a GUID to print.
440 Buffer - Pointer to a user-provided buffer to print to
441 BufferLen - Size of the Buffer
442 Uppercase - If use upper case.
446 EFI_SUCCESS The GUID was printed.
447 EFI_INVALID_PARAMETER The input was NULL.
448 EFI_BUFFER_TOO_SMALL The input buffer was not big enough
453 printf ("ERROR: PrintGuidToBuffer() called with a NULL value\n");
454 return EFI_INVALID_PARAMETER
;
457 if (BufferLen
< PRINTED_GUID_BUFFER_SIZE
) {
458 printf ("ERORR: PrintGuidToBuffer() called with invalid buffer size\n");
459 return EFI_BUFFER_TOO_SMALL
;
465 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
481 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",