X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FLibrary%2FDxePrintLibPrint2Protocol%2FPrintLib.c;h=01378687fd58e7303c0c42bfc946eace70ed6cd9;hp=3b4d4e03adb26c5c105d5158229dd8f74daab088;hb=9b002aa44721ab1b5417be60f3ebd55781be27c4;hpb=7538d53684792266c37e91f11a3ce2b7fb1e32d9 diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c index 3b4d4e03ad..01378687fd 100644 --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c @@ -6,7 +6,7 @@ protocol related to this implementation, not in the public spec. So, this library instance is only for this code base. -Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -71,7 +71,8 @@ PrintLibConstructor ( @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format. @param Size The size, in bytes, of the BaseListMarker buffer. - @return The number of bytes in BaseListMarker. 0 if BaseListMarker is too small. + @return TRUE The VA_LIST has been converted to BASE_LIST. + @return FALSE The VA_LIST has not been converted to BASE_LIST. **/ BOOLEAN @@ -177,6 +178,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList ( } case 'X': case 'x': + case 'u': case 'd': if (Long) { BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64); @@ -204,6 +206,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList ( // If BASE_LIST is larger than Size, then return FALSE // if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) { + DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n")); return FALSE; } @@ -263,15 +266,19 @@ UnicodeVSPrint ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - FALSE, - (CHAR8 *)FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + FALSE, + (CHAR8 *)FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } @@ -366,9 +373,12 @@ UnicodeSPrint ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } /** @@ -413,15 +423,19 @@ UnicodeVSPrintAsciiFormat ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - TRUE, - FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + TRUE, + FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } @@ -515,9 +529,12 @@ UnicodeSPrintAsciiFormat ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } /** @@ -614,15 +631,19 @@ AsciiVSPrint ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - TRUE, - FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + TRUE, + FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } @@ -714,9 +735,12 @@ AsciiSPrint ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } /** @@ -761,15 +785,19 @@ AsciiVSPrintUnicodeFormat ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - FALSE, - (CHAR8 *)FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + FALSE, + (CHAR8 *)FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } @@ -863,9 +891,12 @@ AsciiSPrintUnicodeFormat ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } @@ -931,6 +962,7 @@ AsciiValueToString ( #define PRECISION BIT11 #define ARGUMENT_REVERSED BIT12 #define COUNT_ONLY_NO_PRINT BIT13 +#define UNSIGNED_TYPE BIT14 // // Record date and time information @@ -1056,20 +1088,24 @@ InternalPrintLibSPrint ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL); + NumberOfPrinted = InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL); + VA_END (Marker); + return NumberOfPrinted; } -#define WARNING_STATUS_NUMBER 4 -#define ERROR_STATUS_NUMBER 24 +#define WARNING_STATUS_NUMBER 5 +#define ERROR_STATUS_NUMBER 33 -GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = { +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = { "Success", // RETURN_SUCCESS = 0 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4 + "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT @@ -1093,9 +1129,56 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = { "Aborted", // RETURN_ABORTED = 21 | MAX_BIT "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT - "Protocol Error" // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT + "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT + "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT + "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT + "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT + "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT + "Reserved (29)", // RESERVED = 29 | MAX_BIT + "Reserved (30)", // RESERVED = 30 | MAX_BIT + "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT + "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT + "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT }; +/** + Internal function that places the character into the Buffer. + + Internal function that places ASCII or Unicode character into the Buffer. + + @param Buffer The buffer to place the Unicode or ASCII string. + @param EndBuffer The end of the input Buffer. No characters will be + placed after that. + @param Length The count of character to be placed into Buffer. + (Negative value indicates no buffer fill.) + @param Character The character to be placed into Buffer. + @param Increment The character increment in Buffer. + + @return Buffer. + +**/ +CHAR8 * +InternalPrintLibFillBuffer ( + OUT CHAR8 *Buffer, + IN CHAR8 *EndBuffer, + IN INTN Length, + IN UINTN Character, + IN INTN Increment + ) +{ + INTN Index; + + for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) { + *Buffer = (CHAR8) Character; + if (Increment != 1) { + *(Buffer + 1) = (CHAR8)(Character >> 8); + } + Buffer += Increment; + } + + return Buffer; +} + /** Worker function that produces a Null-terminated string in an output buffer based on a Null-terminated format string and a VA_LIST argument list. @@ -1134,6 +1217,7 @@ InternalPrintLibSPrintMarker ( IN BASE_LIST BaseListMarker OPTIONAL ) { + CHAR8 *OriginalBuffer; CHAR8 *EndBuffer; CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS]; UINT32 BytesPerOutputCharacter; @@ -1170,7 +1254,19 @@ InternalPrintLibSPrintMarker ( // DxePrintLibPrint2Protocol (both PrintLib instances). // - ASSERT(Flags & COUNT_ONLY_NO_PRINT); + if ((Flags & COUNT_ONLY_NO_PRINT) != 0) { + if (BufferSize == 0) { + Buffer = NULL; + } + } else { + // + // We can run without a Buffer for counting only. + // + if (BufferSize == 0) { + return 0; + } + ASSERT (Buffer != NULL); + } if ((Flags & OUTPUT_UNICODE) != 0) { BytesPerOutputCharacter = 2; @@ -1179,16 +1275,21 @@ InternalPrintLibSPrintMarker ( } LengthToReturn = 0; + EndBuffer = NULL; + OriginalBuffer = NULL; // // Reserve space for the Null terminator. // - BufferSize--; + if (Buffer != NULL) { + BufferSize--; + OriginalBuffer = Buffer; - // - // Set the tag for the end of the input Buffer. - // - EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter; + // + // Set the tag for the end of the input Buffer. + // + EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter; + } if ((Flags & FORMAT_UNICODE) != 0) { // @@ -1216,11 +1317,14 @@ InternalPrintLibSPrintMarker ( // // Loop until the end of the format string is reached or the output buffer is full // - while (FormatCharacter != 0 && Buffer < EndBuffer) { + while (FormatCharacter != 0) { + if ((Buffer != NULL) && (Buffer >= EndBuffer)) { + break; + } // // Clear all the flag bits except those that may have been passed in // - Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT); + Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT); // // Set the default width to zero, and the default precision to 1 @@ -1328,10 +1432,13 @@ InternalPrintLibSPrintMarker ( // // Flag space, +, 0, L & l are invalid for type p. // - Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE); + Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE)); if (sizeof (VOID *) > 4) { Flags |= LONG_TYPE; } + // + // break skipped on purpose + // case 'X': Flags |= PREFIX_ZERO; // @@ -1342,10 +1449,18 @@ InternalPrintLibSPrintMarker ( // // break skipped on purpose // + case 'u': + if ((Flags & RADIX_HEX) == 0) { + Flags &= ~((UINTN) (PREFIX_SIGN)); + Flags |= UNSIGNED_TYPE; + } + // + // break skipped on purpose + // case 'd': if ((Flags & LONG_TYPE) == 0) { // - // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". + // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". // This assumption is made so the format string definition is compatible with the ANSI C // Specification for formatted strings. It is recommended that the Base Types be used // everywhere, but in this one case, compliance with ANSI C is more important, and @@ -1376,20 +1491,30 @@ InternalPrintLibSPrintMarker ( if ((Flags & RADIX_HEX) == 0) { Radix = 10; if (Comma) { - Flags &= (~PREFIX_ZERO); + Flags &= ~((UINTN) PREFIX_ZERO); Precision = 1; } - if (Value < 0) { + if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) { Flags |= PREFIX_SIGN; Prefix = '-'; Value = -Value; + } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) { + // + // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". + // This assumption is made so the format string definition is compatible with the ANSI C + // Specification for formatted strings. It is recommended that the Base Types be used + // everywhere, but in this one case, compliance with ANSI C is more important, and + // provides an implementation that is compatible with that largest possible set of CPU + // architectures. This is why the type "unsigned int" is used in this one case. + // + Value = (unsigned int)Value; } } else { Radix = 16; Comma = FALSE; if ((Flags & LONG_TYPE) == 0 && Value < 0) { // - // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". + // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". // This assumption is made so the format string definition is compatible with the ANSI C // Specification for formatted strings. It is recommended that the Base Types be used // everywhere, but in this one case, compliance with ANSI C is more important, and @@ -1445,7 +1570,7 @@ InternalPrintLibSPrintMarker ( ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *); } if (ArgumentString == NULL) { - Flags &= (~ARGUMENT_UNICODE); + Flags &= (~(UINTN)ARGUMENT_UNICODE); ArgumentString = ""; } // @@ -1658,17 +1783,32 @@ InternalPrintLibSPrintMarker ( // if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) { LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter); + } } if (ZeroPad) { if (Prefix != 0) { LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter); + } } LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter); + } } else { LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter); + } if (Prefix != 0) { LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter); + } } } @@ -1687,6 +1827,9 @@ InternalPrintLibSPrintMarker ( ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask; LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter); + } ArgumentString += BytesPerArgumentCharacter; Index++; if (Comma) { @@ -1696,6 +1839,9 @@ InternalPrintLibSPrintMarker ( Index++; if (Index < Count) { LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter); + } } } } @@ -1706,6 +1852,9 @@ InternalPrintLibSPrintMarker ( // if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) { LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter); + } } // @@ -1719,7 +1868,27 @@ InternalPrintLibSPrintMarker ( FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask; } - return (LengthToReturn / BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) != 0) { + return (LengthToReturn / BytesPerOutputCharacter); + } + + ASSERT (Buffer != NULL); + // + // Null terminate the Unicode or ASCII string + // + InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter); + // + // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength + // Unicode characters if PcdMaximumUnicodeStringLength is not zero. + // + ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0)); + // + // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength + // ASCII characters if PcdMaximumAsciiStringLength is not zero. + // + ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0)); + + return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter); } /**