]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/CommonLib.c
a0c6603a85ae2bb002f2a72aa241d4081970246c
3 Copyright (c) 2004 - 2008, 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 basic Library Functions
26 #include "CommonLib.h"
27 #include "EfiUtilityMsgs.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
;
158 IN CHAR8
*InputFileName
,
159 OUT CHAR8
**InputFileImage
,
160 OUT UINT32
*BytesRead
166 This function opens a file and reads it into a memory buffer. The function
167 will allocate the memory buffer and returns the size of the buffer.
171 InputFileName The name of the file to read.
172 InputFileImage A pointer to the memory buffer.
173 BytesRead The size of the memory buffer.
177 EFI_SUCCESS The function completed successfully.
178 EFI_INVALID_PARAMETER One of the input parameters was invalid.
179 EFI_ABORTED An error occurred.
180 EFI_OUT_OF_RESOURCES No resource to complete operations.
188 // Verify input parameters.
190 if (InputFileName
== NULL
|| strlen (InputFileName
) == 0 || InputFileImage
== NULL
) {
191 return EFI_INVALID_PARAMETER
;
194 // Open the file and copy contents into a memory buffer.
199 InputFile
= fopen (InputFileName
, "rb");
200 if (InputFile
== NULL
) {
201 Error (NULL
, 0, 0001, "Error opening the input file", InputFileName
);
205 // Go to the end so that we can determine the file size
207 if (fseek (InputFile
, 0, SEEK_END
)) {
208 Error (NULL
, 0, 0004, "Error reading the input file", InputFileName
);
215 FileSize
= ftell (InputFile
);
216 if (FileSize
== -1) {
217 Error (NULL
, 0, 0003, "Error parsing the input file", InputFileName
);
224 *InputFileImage
= malloc (FileSize
);
225 if (*InputFileImage
== NULL
) {
227 return EFI_OUT_OF_RESOURCES
;
230 // Reset to the beginning of the file
232 if (fseek (InputFile
, 0, SEEK_SET
)) {
233 Error (NULL
, 0, 0004, "Error reading the input file", InputFileName
);
235 free (*InputFileImage
);
236 *InputFileImage
= NULL
;
240 // Read all of the file contents.
242 *BytesRead
= fread (*InputFileImage
, sizeof (UINT8
), FileSize
, InputFile
);
243 if (*BytesRead
!= sizeof (UINT8
) * FileSize
) {
244 Error (NULL
, 0, 0004, "Error reading the input file", InputFileName
);
246 free (*InputFileImage
);
247 *InputFileImage
= NULL
;
260 IN CHAR8
*OutputFileName
,
261 IN CHAR8
*OutputFileImage
,
262 IN UINT32 BytesToWrite
268 This function opens a file and writes OutputFileImage into the file.
272 OutputFileName The name of the file to write.
273 OutputFileImage A pointer to the memory buffer.
274 BytesToWrite The size of the memory buffer.
278 EFI_SUCCESS The function completed successfully.
279 EFI_INVALID_PARAMETER One of the input parameters was invalid.
280 EFI_ABORTED An error occurred.
281 EFI_OUT_OF_RESOURCES No resource to complete operations.
289 // Verify input parameters.
291 if (OutputFileName
== NULL
|| strlen (OutputFileName
) == 0 || OutputFileImage
== NULL
) {
292 return EFI_INVALID_PARAMETER
;
295 // Open the file and copy contents into a memory buffer.
300 OutputFile
= fopen (OutputFileName
, "wb");
301 if (OutputFile
== NULL
) {
302 Error (NULL
, 0, 0001, "Error opening the output file", OutputFileName
);
307 // Write all of the file contents.
309 BytesWrote
= fwrite (OutputFileImage
, sizeof (UINT8
), BytesToWrite
, OutputFile
);
310 if (BytesWrote
!= sizeof (UINT8
) * BytesToWrite
) {
311 Error (NULL
, 0, 0002, "Error writing the output file", OutputFileName
);
332 This function calculates the value needed for a valid UINT8 checksum
336 Buffer Pointer to buffer containing byte data of component.
337 Size Size of the buffer
341 The 8 bit checksum value needed.
345 return (UINT8
) (0x100 - CalculateSum8 (Buffer
, Size
));
355 Routine Description::
357 This function calculates the UINT8 sum for the requested region.
361 Buffer Pointer to buffer containing byte data of component.
362 Size Size of the buffer
366 The 8 bit checksum value needed.
376 // Perform the byte sum for buffer
378 for (Index
= 0; Index
< Size
; Index
++) {
379 Sum
= (UINT8
) (Sum
+ Buffer
[Index
]);
386 CalculateChecksum16 (
392 Routine Description::
394 This function calculates the value needed for a valid UINT16 checksum
398 Buffer Pointer to buffer containing byte data of component.
399 Size Size of the buffer
403 The 16 bit checksum value needed.
407 return (UINT16
) (0x10000 - CalculateSum16 (Buffer
, Size
));
419 This function calculates the UINT16 sum for the requested region.
423 Buffer Pointer to buffer containing byte data of component.
424 Size Size of the buffer
438 // Perform the word sum for buffer
440 for (Index
= 0; Index
< Size
; Index
++) {
441 Sum
= (UINT16
) (Sum
+ Buffer
[Index
]);
455 This function prints a GUID to STDOUT.
459 Guid Pointer to a GUID to print.
463 EFI_SUCCESS The GUID was printed.
464 EFI_INVALID_PARAMETER The input was NULL.
469 Error (NULL
, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with a NULL value");
470 return EFI_INVALID_PARAMETER
;
474 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
475 (unsigned) Guid
->Data1
,
493 IN OUT UINT8
*Buffer
,
501 This function prints a GUID to a buffer
505 Guid - Pointer to a GUID to print.
506 Buffer - Pointer to a user-provided buffer to print to
507 BufferLen - Size of the Buffer
508 Uppercase - If use upper case.
512 EFI_SUCCESS The GUID was printed.
513 EFI_INVALID_PARAMETER The input was NULL.
514 EFI_BUFFER_TOO_SMALL The input buffer was not big enough
519 Error (NULL
, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with a NULL value");
520 return EFI_INVALID_PARAMETER
;
523 if (BufferLen
< PRINTED_GUID_BUFFER_SIZE
) {
524 Error (NULL
, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with invalid buffer size");
525 return EFI_BUFFER_TOO_SMALL
;
531 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
532 (unsigned) Guid
->Data1
,
547 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
548 (unsigned) Guid
->Data1
,
567 size_t _filelength(int fd
)
569 struct stat stat_buf
;
570 fstat(fd
, &stat_buf
);
571 return stat_buf
.st_size
;
575 char *strlwr(char *s
)