3 Copyright (c) 2004 - 2014, 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 basic Library Functions
31 #include "CommonLib.h"
32 #include "EfiUtilityMsgs.h"
43 Set Buffer to zero for Size bytes.
47 Buffer - Memory to set.
49 Size - Number of bytes to set
75 Copy Length bytes from Source to Destination.
79 Destination - Target of copy
81 Source - Place to copy from
83 Length - Number of bytes to copy
94 Destination8
= Destination
;
97 *(Destination8
++) = *(Source8
++);
107 PeiZeroMem (Buffer
, Size
);
112 IN VOID
*Destination
,
117 PeiCopyMem (Destination
, Source
, Length
);
133 Guid1 - guid to compare
134 Guid2 - guid to compare
137 = 0 if Guid1 == Guid2
138 != 0 if Guid1 != Guid2
147 // Compare 32 bits at a time
149 g1
= (INT32
*) Guid1
;
150 g2
= (INT32
*) Guid2
;
163 IN CHAR8
*InputFileName
,
164 OUT CHAR8
**InputFileImage
,
165 OUT UINT32
*BytesRead
171 This function opens a file and reads it into a memory buffer. The function
172 will allocate the memory buffer and returns the size of the buffer.
176 InputFileName The name of the file to read.
177 InputFileImage A pointer to the memory buffer.
178 BytesRead The size of the memory buffer.
182 EFI_SUCCESS The function completed successfully.
183 EFI_INVALID_PARAMETER One of the input parameters was invalid.
184 EFI_ABORTED An error occurred.
185 EFI_OUT_OF_RESOURCES No resource to complete operations.
193 // Verify input parameters.
195 if (InputFileName
== NULL
|| strlen (InputFileName
) == 0 || InputFileImage
== NULL
) {
196 return EFI_INVALID_PARAMETER
;
199 // Open the file and copy contents into a memory buffer.
204 InputFile
= fopen (LongFilePath (InputFileName
), "rb");
205 if (InputFile
== NULL
) {
206 Error (NULL
, 0, 0001, "Error opening the input file", InputFileName
);
210 // Go to the end so that we can determine the file size
212 if (fseek (InputFile
, 0, SEEK_END
)) {
213 Error (NULL
, 0, 0004, "Error reading the input file", InputFileName
);
220 FileSize
= ftell (InputFile
);
221 if (FileSize
== -1) {
222 Error (NULL
, 0, 0003, "Error parsing the input file", InputFileName
);
229 *InputFileImage
= malloc (FileSize
);
230 if (*InputFileImage
== NULL
) {
232 return EFI_OUT_OF_RESOURCES
;
235 // Reset to the beginning of the file
237 if (fseek (InputFile
, 0, SEEK_SET
)) {
238 Error (NULL
, 0, 0004, "Error reading the input file", InputFileName
);
240 free (*InputFileImage
);
241 *InputFileImage
= NULL
;
245 // Read all of the file contents.
247 *BytesRead
= fread (*InputFileImage
, sizeof (UINT8
), FileSize
, InputFile
);
248 if (*BytesRead
!= sizeof (UINT8
) * FileSize
) {
249 Error (NULL
, 0, 0004, "Error reading the input file", InputFileName
);
251 free (*InputFileImage
);
252 *InputFileImage
= NULL
;
265 IN CHAR8
*OutputFileName
,
266 IN CHAR8
*OutputFileImage
,
267 IN UINT32 BytesToWrite
273 This function opens a file and writes OutputFileImage into the file.
277 OutputFileName The name of the file to write.
278 OutputFileImage A pointer to the memory buffer.
279 BytesToWrite The size of the memory buffer.
283 EFI_SUCCESS The function completed successfully.
284 EFI_INVALID_PARAMETER One of the input parameters was invalid.
285 EFI_ABORTED An error occurred.
286 EFI_OUT_OF_RESOURCES No resource to complete operations.
294 // Verify input parameters.
296 if (OutputFileName
== NULL
|| strlen (OutputFileName
) == 0 || OutputFileImage
== NULL
) {
297 return EFI_INVALID_PARAMETER
;
300 // Open the file and copy contents into a memory buffer.
305 OutputFile
= fopen (LongFilePath (OutputFileName
), "wb");
306 if (OutputFile
== NULL
) {
307 Error (NULL
, 0, 0001, "Error opening the output file", OutputFileName
);
312 // Write all of the file contents.
314 BytesWrote
= fwrite (OutputFileImage
, sizeof (UINT8
), BytesToWrite
, OutputFile
);
315 if (BytesWrote
!= sizeof (UINT8
) * BytesToWrite
) {
316 Error (NULL
, 0, 0002, "Error writing the output file", OutputFileName
);
337 This function calculates the value needed for a valid UINT8 checksum
341 Buffer Pointer to buffer containing byte data of component.
342 Size Size of the buffer
346 The 8 bit checksum value needed.
350 return (UINT8
) (0x100 - CalculateSum8 (Buffer
, Size
));
360 Routine Description::
362 This function calculates the UINT8 sum for the requested region.
366 Buffer Pointer to buffer containing byte data of component.
367 Size Size of the buffer
371 The 8 bit checksum value needed.
381 // Perform the byte sum for buffer
383 for (Index
= 0; Index
< Size
; Index
++) {
384 Sum
= (UINT8
) (Sum
+ Buffer
[Index
]);
391 CalculateChecksum16 (
397 Routine Description::
399 This function calculates the value needed for a valid UINT16 checksum
403 Buffer Pointer to buffer containing byte data of component.
404 Size Size of the buffer
408 The 16 bit checksum value needed.
412 return (UINT16
) (0x10000 - CalculateSum16 (Buffer
, Size
));
424 This function calculates the UINT16 sum for the requested region.
428 Buffer Pointer to buffer containing byte data of component.
429 Size Size of the buffer
443 // Perform the word sum for buffer
445 for (Index
= 0; Index
< Size
; Index
++) {
446 Sum
= (UINT16
) (Sum
+ Buffer
[Index
]);
460 This function prints a GUID to STDOUT.
464 Guid Pointer to a GUID to print.
468 EFI_SUCCESS The GUID was printed.
469 EFI_INVALID_PARAMETER The input was NULL.
474 Error (NULL
, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with a NULL value");
475 return EFI_INVALID_PARAMETER
;
479 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
480 (unsigned) Guid
->Data1
,
498 IN OUT UINT8
*Buffer
,
506 This function prints a GUID to a buffer
510 Guid - Pointer to a GUID to print.
511 Buffer - Pointer to a user-provided buffer to print to
512 BufferLen - Size of the Buffer
513 Uppercase - If use upper case.
517 EFI_SUCCESS The GUID was printed.
518 EFI_INVALID_PARAMETER The input was NULL.
519 EFI_BUFFER_TOO_SMALL The input buffer was not big enough
524 Error (NULL
, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with a NULL value");
525 return EFI_INVALID_PARAMETER
;
528 if (BufferLen
< PRINTED_GUID_BUFFER_SIZE
) {
529 Error (NULL
, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with invalid buffer size");
530 return EFI_BUFFER_TOO_SMALL
;
536 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
537 (unsigned) Guid
->Data1
,
552 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
553 (unsigned) Guid
->Data1
,
572 size_t _filelength(int fd
)
574 struct stat stat_buf
;
575 fstat(fd
, &stat_buf
);
576 return stat_buf
.st_size
;
580 char *strlwr(char *s
)
591 #define WINDOWS_EXTENSION_PATH "\\\\?\\"
592 #define WINDOWS_UNC_EXTENSION_PATH "\\\\?\\UNC"
595 // Global data to store full file path. It is not required to be free.
597 CHAR8 mCommonLibFullPath
[MAX_LONG_FILE_PATH
];
606 Convert FileName to the long file path, which can support larger than 260 length.
612 LongFilePath A pointer to the converted long file path.
618 // __GNUC__ may not be good way to differentiate unix and windows. Need more investigation here.
619 // unix has no limitation on file path. Just return FileName.
627 PathPointer
= (CHAR8
*) FileName
;
629 if (FileName
!= NULL
) {
631 // Add the extension string first to support long file path.
633 mCommonLibFullPath
[0] = 0;
634 strcpy (mCommonLibFullPath
, WINDOWS_EXTENSION_PATH
);
636 if (strlen (FileName
) > 1 && FileName
[0] == '\\' && FileName
[1] == '\\') {
638 // network path like \\server\share to \\?\UNC\server\share
640 strcpy (mCommonLibFullPath
, WINDOWS_UNC_EXTENSION_PATH
);
642 } else if (strlen (FileName
) < 3 || FileName
[1] != ':' || (FileName
[2] != '\\' && FileName
[2] != '/')) {
644 // Relative file path. Convert it to absolute path.
646 RootPath
= getcwd (NULL
, 0);
647 if (RootPath
!= NULL
) {
648 strcat (mCommonLibFullPath
, RootPath
);
649 if (FileName
[0] != '\\' && FileName
[0] != '/') {
651 // Attach directory separator
653 strcat (mCommonLibFullPath
, "\\");
660 // Construct the full file path
662 strcat (mCommonLibFullPath
, FileName
);
665 // Convert directory separator '/' to '\\'
667 PathPointer
= (CHAR8
*) mCommonLibFullPath
;
669 if (*PathPointer
== '/') {
672 } while (*PathPointer
++ != '\0');
675 // Convert "\\.\\" to "\\", because it doesn't work with WINDOWS_EXTENSION_PATH.
677 while ((PathPointer
= strstr (mCommonLibFullPath
, "\\.\\")) != NULL
) {
679 strcat (mCommonLibFullPath
, PathPointer
+ 2);
683 // Convert "\\..\\" to last directory, because it doesn't work with WINDOWS_EXTENSION_PATH.
685 while ((PathPointer
= strstr (mCommonLibFullPath
, "\\..\\")) != NULL
) {
686 NextPointer
= PathPointer
+ 3;
689 } while (PathPointer
> mCommonLibFullPath
&& *PathPointer
!= ':' && *PathPointer
!= '\\');
691 if (*PathPointer
== '\\') {
693 // Skip one directory
696 strcat (mCommonLibFullPath
, NextPointer
);
699 // No directory is found. Just break.
705 PathPointer
= mCommonLibFullPath
;