]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/Common/CommonLib.c
3 Copyright (c) 2004, Intel Corporation
4 All rights reserved. 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
25 #include "CommonLib.h"
36 Set Buffer to zero for Size bytes.
40 Buffer - Memory to set.
42 Size - Number of bytes to set
68 Copy Length bytes from Source to Destination.
72 Destination - Target of copy
74 Source - Place to copy from
76 Length - Number of bytes to copy
87 Destination8
= Destination
;
90 *(Destination8
++) = *(Source8
++);
100 PeiZeroMem (Buffer
, Size
);
105 IN VOID
*Destination
,
110 PeiCopyMem (Destination
, Source
, Length
);
126 Guid1 - guid to compare
127 Guid2 - guid to compare
130 = 0 if Guid1 == Guid2
131 != 0 if Guid1 != Guid2
140 // Compare 32 bits at a time
142 g1
= (INT32
*) Guid1
;
143 g2
= (INT32
*) Guid2
;
155 IN CHAR8
*InputFileName
,
156 OUT CHAR8
**InputFileImage
,
157 OUT UINT32
*BytesRead
163 This function opens a file and reads it into a memory buffer. The function
164 will allocate the memory buffer and returns the size of the buffer.
168 InputFileName The name of the file to read.
169 InputFileImage A pointer to the memory buffer.
170 BytesRead The size of the memory buffer.
174 EFI_SUCCESS The function completed successfully.
175 EFI_INVALID_PARAMETER One of the input parameters was invalid.
176 EFI_ABORTED An error occurred.
177 EFI_OUT_OF_RESOURCES No resource to complete operations.
185 // Verify input parameters.
187 if (InputFileName
== NULL
|| strlen (InputFileName
) == 0 || InputFileImage
== NULL
) {
188 return EFI_INVALID_PARAMETER
;
191 // Open the file and copy contents into a memory buffer.
196 InputFile
= fopen (InputFileName
, "rb");
197 if (InputFile
== NULL
) {
198 printf ("ERROR: Could not open input file \"%s\".\n", InputFileName
);
202 // Go to the end so that we can determine the file size
204 if (fseek (InputFile
, 0, SEEK_END
)) {
205 printf ("ERROR: System error reading input file \"%s\".\n", InputFileName
);
212 FileSize
= ftell (InputFile
);
213 if (FileSize
== -1) {
214 printf ("ERROR: System error parsing input file \"%s\".\n", InputFileName
);
221 *InputFileImage
= malloc (FileSize
);
222 if (*InputFileImage
== NULL
) {
224 return EFI_OUT_OF_RESOURCES
;
227 // Reset to the beginning of the file
229 if (fseek (InputFile
, 0, SEEK_SET
)) {
230 printf ("ERROR: System error reading input file \"%s\".\n", InputFileName
);
232 free (*InputFileImage
);
233 *InputFileImage
= NULL
;
237 // Read all of the file contents.
239 *BytesRead
= fread (*InputFileImage
, sizeof (UINT8
), FileSize
, InputFile
);
240 if (*BytesRead
!= sizeof (UINT8
) * FileSize
) {
241 printf ("ERROR: Reading file \"%s\"%i.\n", InputFileName
);
243 free (*InputFileImage
);
244 *InputFileImage
= NULL
;
264 This function calculates the value needed for a valid UINT8 checksum
268 Buffer Pointer to buffer containing byte data of component.
269 Size Size of the buffer
273 The 8 bit checksum value needed.
277 return (UINT8
) (0x100 - CalculateSum8 (Buffer
, Size
));
287 Routine Description::
289 This function calculates the UINT8 sum for the requested region.
293 Buffer Pointer to buffer containing byte data of component.
294 Size Size of the buffer
298 The 8 bit checksum value needed.
308 // Perform the byte sum for buffer
310 for (Index
= 0; Index
< Size
; Index
++) {
311 Sum
= (UINT8
) (Sum
+ Buffer
[Index
]);
318 CalculateChecksum16 (
324 Routine Description::
326 This function calculates the value needed for a valid UINT16 checksum
330 Buffer Pointer to buffer containing byte data of component.
331 Size Size of the buffer
335 The 16 bit checksum value needed.
339 return (UINT16
) (0x10000 - CalculateSum16 (Buffer
, Size
));
351 This function calculates the UINT16 sum for the requested region.
355 Buffer Pointer to buffer containing byte data of component.
356 Size Size of the buffer
370 // Perform the word sum for buffer
372 for (Index
= 0; Index
< Size
; Index
++) {
373 Sum
= (UINT16
) (Sum
+ Buffer
[Index
]);
387 This function prints a GUID to STDOUT.
391 Guid Pointer to a GUID to print.
395 EFI_SUCCESS The GUID was printed.
396 EFI_INVALID_PARAMETER The input was NULL.
401 printf ("ERROR: PrintGuid called with a NULL value.\n");
402 return EFI_INVALID_PARAMETER
;
406 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
425 IN OUT UINT8
*Buffer
,
433 This function prints a GUID to a buffer
437 Guid - Pointer to a GUID to print.
438 Buffer - Pointer to a user-provided buffer to print to
439 BufferLen - Size of the Buffer
440 Uppercase - If use upper case.
444 EFI_SUCCESS The GUID was printed.
445 EFI_INVALID_PARAMETER The input was NULL.
446 EFI_BUFFER_TOO_SMALL The input buffer was not big enough
451 printf ("ERROR: PrintGuidToBuffer() called with a NULL value\n");
452 return EFI_INVALID_PARAMETER
;
455 if (BufferLen
< PRINTED_GUID_BUFFER_SIZE
) {
456 printf ("ERORR: PrintGuidToBuffer() called with invalid buffer size\n");
457 return EFI_BUFFER_TOO_SMALL
;
463 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
479 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
499 size_t _filelength(int fd
)
501 struct stat stat_buf
;
502 fstat(fd
, &stat_buf
);
503 return stat_buf
.st_size
;
507 char *strlwr(char *s
)