2 Instance of Print Library based on gEfiPrint2ProtocolGuid.
4 Implement the print library instance by wrap the interface
5 provided in the Print2 protocol. This protocol is defined as the internal
6 protocol related to this implementation, not in the public spec. So, this
7 library instance is only for this code base.
9 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Protocol/Print2.h>
24 #include <Library/PrintLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/PcdLib.h>
30 #define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0)
35 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
36 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
38 #define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \
40 ASSERT (Expression); \
41 if (!(Expression)) { \
46 EFI_PRINT2_PROTOCOL
*mPrint2Protocol
= NULL
;
49 The constructor function caches the pointer to Print2 protocol.
51 The constructor function locates Print2 protocol from protocol database.
52 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
54 @param ImageHandle The firmware allocated handle for the EFI image.
55 @param SystemTable A pointer to the EFI System Table.
57 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
63 IN EFI_HANDLE ImageHandle
,
64 IN EFI_SYSTEM_TABLE
*SystemTable
69 Status
= SystemTable
->BootServices
->LocateProtocol (
70 &gEfiPrint2ProtocolGuid
,
72 (VOID
**) &mPrint2Protocol
74 ASSERT_EFI_ERROR (Status
);
75 ASSERT (mPrint2Protocol
!= NULL
);
82 Worker function that converts a VA_LIST to a BASE_LIST based on a Null-terminated
85 @param AsciiFormat TRUE if Format is an ASCII string. FALSE if Format is a Unicode string.
86 @param Format Null-terminated format string.
87 @param VaListMarker VA_LIST style variable argument list consumed by processing Format.
88 @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format.
89 @param Size The size, in bytes, of the BaseListMarker buffer.
91 @return TRUE The VA_LIST has been converted to BASE_LIST.
92 @return FALSE The VA_LIST has not been converted to BASE_LIST.
96 DxePrintLibPrint2ProtocolVaListToBaseList (
97 IN BOOLEAN AsciiFormat
,
98 IN CONST CHAR8
*Format
,
99 IN VA_LIST VaListMarker
,
100 OUT BASE_LIST BaseListMarker
,
104 BASE_LIST BaseListStart
;
105 UINTN BytesPerFormatCharacter
;
107 UINTN FormatCharacter
;
111 ASSERT (BaseListMarker
!= NULL
);
112 SAFE_PRINT_CONSTRAINT_CHECK ((Format
!= NULL
), FALSE
);
114 BaseListStart
= BaseListMarker
;
117 if (ASCII_RSIZE_MAX
!= 0) {
118 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), FALSE
);
120 BytesPerFormatCharacter
= 1;
123 if (RSIZE_MAX
!= 0) {
124 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16
*)Format
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), FALSE
);
126 BytesPerFormatCharacter
= 2;
131 // Get the first character from the format string
133 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
135 while (FormatCharacter
!= 0) {
136 if (FormatCharacter
== '%') {
140 // Parse Flags and Width
142 for (Done
= FALSE
; !Done
; ) {
144 // Get the next character from the format string
146 Format
+= BytesPerFormatCharacter
;
149 // Get the next character from the format string
151 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
153 switch (FormatCharacter
) {
175 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
179 // Make no output if Format string terminates unexpectedly when
180 // looking up for flag, width, precision and type.
182 Format
-= BytesPerFormatCharacter
;
184 // break skipped on purpose.
193 // Handle each argument type
195 switch (FormatCharacter
) {
197 if (sizeof (VOID
*) > 4) {
205 BASE_ARG (BaseListMarker
, INT64
) = VA_ARG (VaListMarker
, INT64
);
207 BASE_ARG (BaseListMarker
, int) = VA_ARG (VaListMarker
, int);
215 BASE_ARG (BaseListMarker
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
218 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
221 BASE_ARG (BaseListMarker
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
227 // If BASE_LIST is larger than Size, then return FALSE
229 if ((UINTN
)((UINT8
*)BaseListMarker
- (UINT8
*)BaseListStart
) > Size
) {
230 DEBUG ((DEBUG_ERROR
, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));
235 // Get the next character from the format string
237 Format
+= BytesPerFormatCharacter
;
240 // Get the next character from the format string
242 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
248 Produces a Null-terminated Unicode string in an output buffer based on
249 a Null-terminated Unicode format string and a VA_LIST argument list.
251 This function is similar as vsnprintf_s defined in C11.
253 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
255 The Unicode string is produced by parsing the format string specified by FormatString.
256 Arguments are pulled from the variable argument list specified by Marker based on the
257 contents of the format string.
258 The number of Unicode characters in the produced output buffer is returned not including
261 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
262 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
264 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
265 unmodified and 0 is returned.
266 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
267 unmodified and 0 is returned.
268 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
269 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
270 buffer is unmodified and 0 is returned.
271 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
272 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
273 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
275 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.
277 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
279 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
280 @param FormatString A Null-terminated Unicode format string.
281 @param Marker VA_LIST marker for the variable argument list.
283 @return The number of Unicode characters in the produced output buffer not including the
290 OUT CHAR16
*StartOfBuffer
,
292 IN CONST CHAR16
*FormatString
,
296 UINT64 BaseListMarker
[256 / sizeof (UINT64
)];
299 ASSERT_UNICODE_BUFFER (StartOfBuffer
);
300 ASSERT_UNICODE_BUFFER (FormatString
);
302 Converted
= DxePrintLibPrint2ProtocolVaListToBaseList (
304 (CHAR8
*)FormatString
,
306 (BASE_LIST
)BaseListMarker
,
307 sizeof (BaseListMarker
) - 8
313 return UnicodeBSPrint (StartOfBuffer
, BufferSize
, FormatString
, (BASE_LIST
)BaseListMarker
);
317 Produces a Null-terminated Unicode string in an output buffer based on
318 a Null-terminated Unicode format string and a BASE_LIST argument list.
320 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
322 The Unicode string is produced by parsing the format string specified by FormatString.
323 Arguments are pulled from the variable argument list specified by Marker based on the
324 contents of the format string.
325 The number of Unicode characters in the produced output buffer is returned not including
328 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
329 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
331 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
332 unmodified and 0 is returned.
333 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
334 unmodified and 0 is returned.
335 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
336 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
337 buffer is unmodified and 0 is returned.
338 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
339 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
340 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
342 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.
344 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
346 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
347 @param FormatString A Null-terminated Unicode format string.
348 @param Marker BASE_LIST marker for the variable argument list.
350 @return The number of Unicode characters in the produced output buffer not including the
357 OUT CHAR16
*StartOfBuffer
,
359 IN CONST CHAR16
*FormatString
,
363 ASSERT_UNICODE_BUFFER (StartOfBuffer
);
364 ASSERT_UNICODE_BUFFER (FormatString
);
365 return mPrint2Protocol
->UnicodeBSPrint (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
369 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
370 Unicode format string and variable argument list.
372 This function is similar as snprintf_s defined in C11.
374 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
376 The Unicode string is produced by parsing the format string specified by FormatString.
377 Arguments are pulled from the variable argument list based on the contents of the format string.
378 The number of Unicode characters in the produced output buffer is returned not including
381 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
382 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
384 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
385 unmodified and 0 is returned.
386 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
387 unmodified and 0 is returned.
388 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
389 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
390 buffer is unmodified and 0 is returned.
391 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
392 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
393 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
395 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.
397 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
399 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
400 @param FormatString A Null-terminated Unicode format string.
401 @param ... Variable argument list whose contents are accessed based on the
402 format string specified by FormatString.
404 @return The number of Unicode characters in the produced output buffer not including the
411 OUT CHAR16
*StartOfBuffer
,
413 IN CONST CHAR16
*FormatString
,
418 UINTN NumberOfPrinted
;
420 VA_START (Marker
, FormatString
);
421 NumberOfPrinted
= UnicodeVSPrint (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
423 return NumberOfPrinted
;
427 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
428 ASCII format string and a VA_LIST argument list.
430 This function is similar as vsnprintf_s defined in C11.
432 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
434 The Unicode string is produced by parsing the format string specified by FormatString.
435 Arguments are pulled from the variable argument list specified by Marker based on the
436 contents of the format string.
437 The number of Unicode characters in the produced output buffer is returned not including
440 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
442 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
443 unmodified and 0 is returned.
444 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
445 unmodified and 0 is returned.
446 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
447 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
448 buffer is unmodified and 0 is returned.
449 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
450 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
451 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
453 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
455 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
457 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
458 @param FormatString A Null-terminated ASCII format string.
459 @param Marker VA_LIST marker for the variable argument list.
461 @return The number of Unicode characters in the produced output buffer not including the
467 UnicodeVSPrintAsciiFormat (
468 OUT CHAR16
*StartOfBuffer
,
470 IN CONST CHAR8
*FormatString
,
474 UINT64 BaseListMarker
[256 / sizeof (UINT64
)];
477 ASSERT_UNICODE_BUFFER (StartOfBuffer
);
479 Converted
= DxePrintLibPrint2ProtocolVaListToBaseList (
483 (BASE_LIST
)BaseListMarker
,
484 sizeof (BaseListMarker
) - 8
490 return UnicodeBSPrintAsciiFormat (StartOfBuffer
, BufferSize
, FormatString
, (BASE_LIST
)BaseListMarker
);
494 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
495 ASCII format string and a BASE_LIST argument list.
497 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
499 The Unicode string is produced by parsing the format string specified by FormatString.
500 Arguments are pulled from the variable argument list specified by Marker based on the
501 contents of the format string.
502 The number of Unicode characters in the produced output buffer is returned not including
505 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
507 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
508 unmodified and 0 is returned.
509 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
510 unmodified and 0 is returned.
511 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
512 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
513 buffer is unmodified and 0 is returned.
514 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
515 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
516 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
518 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
520 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
522 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
523 @param FormatString A Null-terminated ASCII format string.
524 @param Marker BASE_LIST marker for the variable argument list.
526 @return The number of Unicode characters in the produced output buffer not including the
532 UnicodeBSPrintAsciiFormat (
533 OUT CHAR16
*StartOfBuffer
,
535 IN CONST CHAR8
*FormatString
,
539 ASSERT_UNICODE_BUFFER (StartOfBuffer
);
540 return mPrint2Protocol
->UnicodeBSPrintAsciiFormat (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
544 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
545 ASCII format string and variable argument list.
547 This function is similar as snprintf_s defined in C11.
549 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
551 The Unicode string is produced by parsing the format string specified by FormatString.
552 Arguments are pulled from the variable argument list based on the contents of the
554 The number of Unicode characters in the produced output buffer is returned not including
557 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
559 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
560 unmodified and 0 is returned.
561 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
562 unmodified and 0 is returned.
563 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
564 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
565 buffer is unmodified and 0 is returned.
566 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
567 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
568 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
570 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
572 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
574 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
575 @param FormatString A Null-terminated ASCII format string.
576 @param ... Variable argument list whose contents are accessed based on the
577 format string specified by FormatString.
579 @return The number of Unicode characters in the produced output buffer not including the
585 UnicodeSPrintAsciiFormat (
586 OUT CHAR16
*StartOfBuffer
,
588 IN CONST CHAR8
*FormatString
,
593 UINTN NumberOfPrinted
;
595 VA_START (Marker
, FormatString
);
596 NumberOfPrinted
= UnicodeVSPrintAsciiFormat (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
598 return NumberOfPrinted
;
602 Converts a decimal value to a Null-terminated Unicode string.
604 Converts the decimal number specified by Value to a Null-terminated Unicode
605 string specified by Buffer containing at most Width characters. No padding of spaces
606 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
607 The number of Unicode characters in Buffer is returned not including the Null-terminator.
608 If the conversion contains more than Width characters, then only the first
609 Width characters are returned, and the total number of characters
610 required to perform the conversion is returned.
611 Additional conversion parameters are specified in Flags.
613 The Flags bit LEFT_JUSTIFY is always ignored.
614 All conversions are left justified in Buffer.
615 If Width is 0, PREFIX_ZERO is ignored in Flags.
616 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
617 are inserted every 3rd digit starting from the right.
618 If RADIX_HEX is set in Flags, then the output buffer will be
619 formatted in hexadecimal format.
620 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
621 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
622 then Buffer is padded with '0' characters so the combination of the optional '-'
623 sign character, '0' characters, digit characters for Value, and the Null-terminator
624 add up to Width characters.
625 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
626 If Buffer is NULL, then ASSERT().
627 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
628 If unsupported bits are set in Flags, then ASSERT().
629 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
630 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
632 @param Buffer Pointer to the output buffer for the produced Null-terminated
634 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
635 @param Value The 64-bit signed value to convert to a string.
636 @param Width The maximum number of Unicode characters to place in Buffer, not including
639 @return The number of Unicode characters in Buffer not including the Null-terminator.
644 UnicodeValueToString (
645 IN OUT CHAR16
*Buffer
,
651 return mPrint2Protocol
->UnicodeValueToString (Buffer
, Flags
, Value
, Width
);
655 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
656 ASCII format string and a VA_LIST argument list.
658 This function is similar as vsnprintf_s defined in C11.
660 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
662 The ASCII string is produced by parsing the format string specified by FormatString.
663 Arguments are pulled from the variable argument list specified by Marker based on
664 the contents of the format string.
665 The number of ASCII characters in the produced output buffer is returned not including
668 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
669 unmodified and 0 is returned.
670 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
671 unmodified and 0 is returned.
672 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
673 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
674 is unmodified and 0 is returned.
675 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
676 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
677 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
679 If BufferSize is 0, then no output buffer is produced and 0 is returned.
681 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
683 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
684 @param FormatString A Null-terminated ASCII format string.
685 @param Marker VA_LIST marker for the variable argument list.
687 @return The number of ASCII characters in the produced output buffer not including the
694 OUT CHAR8
*StartOfBuffer
,
696 IN CONST CHAR8
*FormatString
,
700 UINT64 BaseListMarker
[256 / sizeof (UINT64
)];
703 Converted
= DxePrintLibPrint2ProtocolVaListToBaseList (
707 (BASE_LIST
)BaseListMarker
,
708 sizeof (BaseListMarker
) - 8
714 return AsciiBSPrint (StartOfBuffer
, BufferSize
, FormatString
, (BASE_LIST
)BaseListMarker
);
718 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
719 ASCII format string and a BASE_LIST argument list.
721 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
723 The ASCII string is produced by parsing the format string specified by FormatString.
724 Arguments are pulled from the variable argument list specified by Marker based on
725 the contents of the format string.
726 The number of ASCII characters in the produced output buffer is returned not including
729 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
730 unmodified and 0 is returned.
731 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
732 unmodified and 0 is returned.
733 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
734 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
735 is unmodified and 0 is returned.
736 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
737 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
738 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
740 If BufferSize is 0, then no output buffer is produced and 0 is returned.
742 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
744 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
745 @param FormatString A Null-terminated ASCII format string.
746 @param Marker BASE_LIST marker for the variable argument list.
748 @return The number of ASCII characters in the produced output buffer not including the
755 OUT CHAR8
*StartOfBuffer
,
757 IN CONST CHAR8
*FormatString
,
761 return mPrint2Protocol
->AsciiBSPrint (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
765 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
766 ASCII format string and variable argument list.
768 This function is similar as snprintf_s defined in C11.
770 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
772 The ASCII string is produced by parsing the format string specified by FormatString.
773 Arguments are pulled from the variable argument list based on the contents of the
775 The number of ASCII characters in the produced output buffer is returned not including
778 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
779 unmodified and 0 is returned.
780 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
781 unmodified and 0 is returned.
782 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
783 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
784 is unmodified and 0 is returned.
785 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
786 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
787 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
789 If BufferSize is 0, then no output buffer is produced and 0 is returned.
791 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
793 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
794 @param FormatString A Null-terminated ASCII format string.
795 @param ... Variable argument list whose contents are accessed based on the
796 format string specified by FormatString.
798 @return The number of ASCII characters in the produced output buffer not including the
805 OUT CHAR8
*StartOfBuffer
,
807 IN CONST CHAR8
*FormatString
,
812 UINTN NumberOfPrinted
;
814 VA_START (Marker
, FormatString
);
815 NumberOfPrinted
= AsciiVSPrint (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
817 return NumberOfPrinted
;
821 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
822 Unicode format string and a VA_LIST argument list.
824 This function is similar as vsnprintf_s defined in C11.
826 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
828 The ASCII string is produced by parsing the format string specified by FormatString.
829 Arguments are pulled from the variable argument list specified by Marker based on
830 the contents of the format string.
831 The number of ASCII characters in the produced output buffer is returned not including
834 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
836 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
837 unmodified and 0 is returned.
838 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
839 unmodified and 0 is returned.
840 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
841 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
842 is unmodified and 0 is returned.
843 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
844 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
845 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
847 If BufferSize is 0, then no output buffer is produced and 0 is returned.
849 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
851 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
852 @param FormatString A Null-terminated Unicode format string.
853 @param Marker VA_LIST marker for the variable argument list.
855 @return The number of ASCII characters in the produced output buffer not including the
861 AsciiVSPrintUnicodeFormat (
862 OUT CHAR8
*StartOfBuffer
,
864 IN CONST CHAR16
*FormatString
,
868 UINT64 BaseListMarker
[256 / sizeof (UINT64
)];
871 ASSERT_UNICODE_BUFFER (FormatString
);
873 Converted
= DxePrintLibPrint2ProtocolVaListToBaseList (
875 (CHAR8
*)FormatString
,
877 (BASE_LIST
)BaseListMarker
,
878 sizeof (BaseListMarker
) - 8
884 return AsciiBSPrintUnicodeFormat (StartOfBuffer
, BufferSize
, FormatString
, (BASE_LIST
)BaseListMarker
);
888 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
889 Unicode format string and a BASE_LIST argument list.
891 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
893 The ASCII string is produced by parsing the format string specified by FormatString.
894 Arguments are pulled from the variable argument list specified by Marker based on
895 the contents of the format string.
896 The number of ASCII characters in the produced output buffer is returned not including
899 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
901 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
902 unmodified and 0 is returned.
903 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
904 unmodified and 0 is returned.
905 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
906 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
907 is unmodified and 0 is returned.
908 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
909 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
910 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
912 If BufferSize is 0, then no output buffer is produced and 0 is returned.
914 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
916 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
917 @param FormatString A Null-terminated Unicode format string.
918 @param Marker BASE_LIST marker for the variable argument list.
920 @return The number of ASCII characters in the produced output buffer not including the
926 AsciiBSPrintUnicodeFormat (
927 OUT CHAR8
*StartOfBuffer
,
929 IN CONST CHAR16
*FormatString
,
933 ASSERT_UNICODE_BUFFER (FormatString
);
934 return mPrint2Protocol
->AsciiBSPrintUnicodeFormat (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
938 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
939 Unicode format string and variable argument list.
941 This function is similar as snprintf_s defined in C11.
943 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
945 The ASCII string is produced by parsing the format string specified by FormatString.
946 Arguments are pulled from the variable argument list based on the contents of the
948 The number of ASCII characters in the produced output buffer is returned not including
951 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
953 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
954 unmodified and 0 is returned.
955 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
956 unmodified and 0 is returned.
957 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
958 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
959 is unmodified and 0 is returned.
960 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
961 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
962 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
964 If BufferSize is 0, then no output buffer is produced and 0 is returned.
966 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
968 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
969 @param FormatString A Null-terminated Unicode format string.
970 @param ... Variable argument list whose contents are accessed based on the
971 format string specified by FormatString.
973 @return The number of ASCII characters in the produced output buffer not including the
979 AsciiSPrintUnicodeFormat (
980 OUT CHAR8
*StartOfBuffer
,
982 IN CONST CHAR16
*FormatString
,
987 UINTN NumberOfPrinted
;
989 VA_START (Marker
, FormatString
);
990 NumberOfPrinted
= AsciiVSPrintUnicodeFormat (StartOfBuffer
, BufferSize
, FormatString
, Marker
);
992 return NumberOfPrinted
;
997 Converts a decimal value to a Null-terminated ASCII string.
999 Converts the decimal number specified by Value to a Null-terminated ASCII string
1000 specified by Buffer containing at most Width characters. No padding of spaces
1002 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
1003 The number of ASCII characters in Buffer is returned not including the Null-terminator.
1004 If the conversion contains more than Width characters, then only the first Width
1005 characters are returned, and the total number of characters required to perform
1006 the conversion is returned.
1007 Additional conversion parameters are specified in Flags.
1008 The Flags bit LEFT_JUSTIFY is always ignored.
1009 All conversions are left justified in Buffer.
1010 If Width is 0, PREFIX_ZERO is ignored in Flags.
1011 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
1012 are inserted every 3rd digit starting from the right.
1013 If RADIX_HEX is set in Flags, then the output buffer will be
1014 formatted in hexadecimal format.
1015 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
1016 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
1017 then Buffer is padded with '0' characters so the combination of the optional '-'
1018 sign character, '0' characters, digit characters for Value, and the Null-terminator
1019 add up to Width characters.
1021 If Buffer is NULL, then ASSERT().
1022 If unsupported bits are set in Flags, then ASSERT().
1023 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
1024 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
1026 @param Buffer Pointer to the output buffer for the produced Null-terminated
1028 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
1029 @param Value The 64-bit signed value to convert to a string.
1030 @param Width The maximum number of ASCII characters to place in Buffer, not including
1031 the Null-terminator.
1033 @return The number of ASCII characters in Buffer not including the Null-terminator.
1038 AsciiValueToString (
1045 return mPrint2Protocol
->AsciiValueToString (Buffer
, Flags
, Value
, Width
);
1048 #define PREFIX_SIGN BIT1
1049 #define PREFIX_BLANK BIT2
1050 #define LONG_TYPE BIT4
1051 #define OUTPUT_UNICODE BIT6
1052 #define FORMAT_UNICODE BIT8
1053 #define PAD_TO_WIDTH BIT9
1054 #define ARGUMENT_UNICODE BIT10
1055 #define PRECISION BIT11
1056 #define ARGUMENT_REVERSED BIT12
1057 #define COUNT_ONLY_NO_PRINT BIT13
1058 #define UNSIGNED_TYPE BIT14
1061 // Record date and time information
1077 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
1080 Internal function that convert a number to a string in Buffer.
1082 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
1084 @param Buffer Location to place the ASCII string of Value.
1085 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
1086 @param Radix Radix of the value
1088 @return A pointer to the end of buffer filled with ASCII string.
1092 InternalPrintLibValueToString (
1093 IN OUT CHAR8
*Buffer
,
1101 // Loop to convert one digit at a time in reverse order
1105 Value
= (INT64
)DivU64x32Remainder ((UINT64
)Value
, (UINT32
)Radix
, &Remainder
);
1106 *(++Buffer
) = mHexStr
[Remainder
];
1107 } while (Value
!= 0);
1110 // Return pointer of the end of filled buffer.
1116 Worker function that produces a Null-terminated string in an output buffer
1117 based on a Null-terminated format string and a VA_LIST argument list.
1119 VSPrint function to process format and place the results in Buffer. Since a
1120 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1121 this is the main print working routine.
1123 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1125 @param[out] Buffer The character buffer to print the results of the
1126 parsing of Format into.
1127 @param[in] BufferSize The maximum number of characters to put into
1129 @param[in] Flags Initial flags value.
1130 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1131 and COUNT_ONLY_NO_PRINT set.
1132 @param[in] Format A Null-terminated format string.
1133 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1135 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1136 by processing Format.
1138 @return The number of characters printed not including the Null-terminator.
1139 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1140 modification to Buffer.
1144 InternalPrintLibSPrintMarker (
1146 IN UINTN BufferSize
,
1148 IN CONST CHAR8
*Format
,
1149 IN VA_LIST VaListMarker
, OPTIONAL
1150 IN BASE_LIST BaseListMarker OPTIONAL
1154 Worker function that produces a Null-terminated string in an output buffer
1155 based on a Null-terminated format string and variable argument list.
1157 VSPrint function to process format and place the results in Buffer. Since a
1158 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1159 this is the main print working routine
1161 @param StartOfBuffer The character buffer to print the results of the parsing
1163 @param BufferSize The maximum number of characters to put into buffer.
1164 Zero means no limit.
1165 @param Flags Initial flags value.
1166 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1167 @param FormatString A Null-terminated format string.
1168 @param ... The variable argument list.
1170 @return The number of characters printed.
1175 InternalPrintLibSPrint (
1176 OUT CHAR8
*StartOfBuffer
,
1177 IN UINTN BufferSize
,
1179 IN CONST CHAR8
*FormatString
,
1184 UINTN NumberOfPrinted
;
1186 VA_START (Marker
, FormatString
);
1187 NumberOfPrinted
= InternalPrintLibSPrintMarker (StartOfBuffer
, BufferSize
, Flags
, FormatString
, Marker
, NULL
);
1189 return NumberOfPrinted
;
1192 #define WARNING_STATUS_NUMBER 5
1193 #define ERROR_STATUS_NUMBER 33
1195 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
* CONST mStatusString
[] = {
1196 "Success", // RETURN_SUCCESS = 0
1197 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
1198 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
1199 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
1200 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
1201 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5
1202 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
1203 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
1204 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
1205 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
1206 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
1207 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
1208 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
1209 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
1210 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
1211 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
1212 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
1213 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
1214 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
1215 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
1216 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
1217 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
1218 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
1219 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
1220 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
1221 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
1222 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
1223 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
1224 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
1225 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
1226 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT
1227 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT
1228 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT
1229 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT
1230 "Reserved (29)", // RESERVED = 29 | MAX_BIT
1231 "Reserved (30)", // RESERVED = 30 | MAX_BIT
1232 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT
1233 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT
1234 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT
1238 Internal function that places the character into the Buffer.
1240 Internal function that places ASCII or Unicode character into the Buffer.
1242 @param Buffer The buffer to place the Unicode or ASCII string.
1243 @param EndBuffer The end of the input Buffer. No characters will be
1245 @param Length The count of character to be placed into Buffer.
1246 (Negative value indicates no buffer fill.)
1247 @param Character The character to be placed into Buffer.
1248 @param Increment The character increment in Buffer.
1254 InternalPrintLibFillBuffer (
1256 IN CHAR8
*EndBuffer
,
1264 for (Index
= 0; Index
< Length
&& Buffer
< EndBuffer
; Index
++) {
1265 *Buffer
= (CHAR8
) Character
;
1266 if (Increment
!= 1) {
1267 *(Buffer
+ 1) = (CHAR8
)(Character
>> 8);
1269 Buffer
+= Increment
;
1276 Worker function that produces a Null-terminated string in an output buffer
1277 based on a Null-terminated format string and a VA_LIST argument list.
1279 VSPrint function to process format and place the results in Buffer. Since a
1280 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1281 this is the main print working routine.
1283 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1285 @param[out] Buffer The character buffer to print the results of the
1286 parsing of Format into.
1287 @param[in] BufferSize The maximum number of characters to put into
1289 @param[in] Flags Initial flags value.
1290 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1291 and COUNT_ONLY_NO_PRINT set.
1292 @param[in] Format A Null-terminated format string.
1293 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1295 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1296 by processing Format.
1298 @return The number of characters printed not including the Null-terminator.
1299 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1300 modification to Buffer.
1304 InternalPrintLibSPrintMarker (
1306 IN UINTN BufferSize
,
1308 IN CONST CHAR8
*Format
,
1309 IN VA_LIST VaListMarker
, OPTIONAL
1310 IN BASE_LIST BaseListMarker OPTIONAL
1313 CHAR8
*OriginalBuffer
;
1315 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
1316 UINT32 BytesPerOutputCharacter
;
1317 UINTN BytesPerFormatCharacter
;
1319 UINTN FormatCharacter
;
1323 CONST CHAR8
*ArgumentString
;
1329 INTN BytesPerArgumentCharacter
;
1330 UINTN ArgumentCharacter
;
1338 RETURN_STATUS Status
;
1342 UINTN LengthToReturn
;
1345 // If you change this code be sure to match the 2 versions of this function.
1346 // Nearly identical logic is found in the BasePrintLib and
1347 // DxePrintLibPrint2Protocol (both PrintLib instances).
1351 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and
1352 // COUNT_ONLY_NO_PRINT is not set in Flags.
1354 if ((BufferSize
> 0) && ((Flags
& COUNT_ONLY_NO_PRINT
) == 0)) {
1355 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer
!= NULL
), 0);
1359 // 2. Format shall not be a null pointer when BufferSize > 0 or when
1360 // COUNT_ONLY_NO_PRINT is set in Flags.
1362 if ((BufferSize
> 0) || ((Flags
& COUNT_ONLY_NO_PRINT
) != 0)) {
1363 SAFE_PRINT_CONSTRAINT_CHECK ((Format
!= NULL
), 0);
1367 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or
1368 // ASCII_RSIZE_MAX for Ascii output.
1370 if ((Flags
& OUTPUT_UNICODE
) != 0) {
1371 if (RSIZE_MAX
!= 0) {
1372 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= RSIZE_MAX
), 0);
1374 BytesPerOutputCharacter
= 2;
1376 if (ASCII_RSIZE_MAX
!= 0) {
1377 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= ASCII_RSIZE_MAX
), 0);
1379 BytesPerOutputCharacter
= 1;
1383 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or
1384 // ASCII_RSIZE_MAX Ascii characters.
1386 if ((Flags
& FORMAT_UNICODE
) != 0) {
1387 if (RSIZE_MAX
!= 0) {
1388 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16
*)Format
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), 0);
1390 BytesPerFormatCharacter
= 2;
1391 FormatMask
= 0xffff;
1393 if (ASCII_RSIZE_MAX
!= 0) {
1394 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), 0);
1396 BytesPerFormatCharacter
= 1;
1400 if ((Flags
& COUNT_ONLY_NO_PRINT
) != 0) {
1401 if (BufferSize
== 0) {
1406 // We can run without a Buffer for counting only.
1408 if (BufferSize
== 0) {
1415 OriginalBuffer
= NULL
;
1418 // Reserve space for the Null terminator.
1420 if (Buffer
!= NULL
) {
1422 OriginalBuffer
= Buffer
;
1425 // Set the tag for the end of the input Buffer.
1427 EndBuffer
= Buffer
+ BufferSize
* BytesPerOutputCharacter
;
1431 // Get the first character from the format string
1433 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1436 // Loop until the end of the format string is reached or the output buffer is full
1438 while (FormatCharacter
!= 0) {
1439 if ((Buffer
!= NULL
) && (Buffer
>= EndBuffer
)) {
1443 // Clear all the flag bits except those that may have been passed in
1445 Flags
&= (UINTN
) (OUTPUT_UNICODE
| FORMAT_UNICODE
| COUNT_ONLY_NO_PRINT
);
1448 // Set the default width to zero, and the default precision to 1
1458 switch (FormatCharacter
) {
1461 // Parse Flags and Width
1463 for (Done
= FALSE
; !Done
; ) {
1464 Format
+= BytesPerFormatCharacter
;
1465 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1466 switch (FormatCharacter
) {
1471 Flags
|= LEFT_JUSTIFY
;
1474 Flags
|= PREFIX_SIGN
;
1477 Flags
|= PREFIX_BLANK
;
1480 Flags
|= COMMA_TYPE
;
1487 if ((Flags
& PRECISION
) == 0) {
1488 Flags
|= PAD_TO_WIDTH
;
1489 if (BaseListMarker
== NULL
) {
1490 Width
= VA_ARG (VaListMarker
, UINTN
);
1492 Width
= BASE_ARG (BaseListMarker
, UINTN
);
1495 if (BaseListMarker
== NULL
) {
1496 Precision
= VA_ARG (VaListMarker
, UINTN
);
1498 Precision
= BASE_ARG (BaseListMarker
, UINTN
);
1503 if ((Flags
& PRECISION
) == 0) {
1504 Flags
|= PREFIX_ZERO
;
1515 for (Count
= 0; ((FormatCharacter
>= '0') && (FormatCharacter
<= '9')); ){
1516 Count
= (Count
* 10) + FormatCharacter
- '0';
1517 Format
+= BytesPerFormatCharacter
;
1518 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1520 Format
-= BytesPerFormatCharacter
;
1521 if ((Flags
& PRECISION
) == 0) {
1522 Flags
|= PAD_TO_WIDTH
;
1531 // Make no output if Format string terminates unexpectedly when
1532 // looking up for flag, width, precision and type.
1534 Format
-= BytesPerFormatCharacter
;
1537 // break skipped on purpose.
1546 // Handle each argument type
1548 switch (FormatCharacter
) {
1551 // Flag space, +, 0, L & l are invalid for type p.
1553 Flags
&= ~((UINTN
) (PREFIX_BLANK
| PREFIX_SIGN
| PREFIX_ZERO
| LONG_TYPE
));
1554 if (sizeof (VOID
*) > 4) {
1558 // break skipped on purpose
1561 Flags
|= PREFIX_ZERO
;
1563 // break skipped on purpose
1568 // break skipped on purpose
1571 if ((Flags
& RADIX_HEX
) == 0) {
1572 Flags
&= ~((UINTN
) (PREFIX_SIGN
));
1573 Flags
|= UNSIGNED_TYPE
;
1576 // break skipped on purpose
1579 if ((Flags
& LONG_TYPE
) == 0) {
1581 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1582 // This assumption is made so the format string definition is compatible with the ANSI C
1583 // Specification for formatted strings. It is recommended that the Base Types be used
1584 // everywhere, but in this one case, compliance with ANSI C is more important, and
1585 // provides an implementation that is compatible with that largest possible set of CPU
1586 // architectures. This is why the type "int" is used in this one case.
1588 if (BaseListMarker
== NULL
) {
1589 Value
= VA_ARG (VaListMarker
, int);
1591 Value
= BASE_ARG (BaseListMarker
, int);
1594 if (BaseListMarker
== NULL
) {
1595 Value
= VA_ARG (VaListMarker
, INT64
);
1597 Value
= BASE_ARG (BaseListMarker
, INT64
);
1600 if ((Flags
& PREFIX_BLANK
) != 0) {
1603 if ((Flags
& PREFIX_SIGN
) != 0) {
1606 if ((Flags
& COMMA_TYPE
) != 0) {
1609 if ((Flags
& RADIX_HEX
) == 0) {
1612 Flags
&= ~((UINTN
) PREFIX_ZERO
);
1615 if (Value
< 0 && (Flags
& UNSIGNED_TYPE
) == 0) {
1616 Flags
|= PREFIX_SIGN
;
1619 } else if ((Flags
& UNSIGNED_TYPE
) != 0 && (Flags
& LONG_TYPE
) == 0) {
1621 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1622 // This assumption is made so the format string definition is compatible with the ANSI C
1623 // Specification for formatted strings. It is recommended that the Base Types be used
1624 // everywhere, but in this one case, compliance with ANSI C is more important, and
1625 // provides an implementation that is compatible with that largest possible set of CPU
1626 // architectures. This is why the type "unsigned int" is used in this one case.
1628 Value
= (unsigned int)Value
;
1633 if ((Flags
& LONG_TYPE
) == 0 && Value
< 0) {
1635 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1636 // This assumption is made so the format string definition is compatible with the ANSI C
1637 // Specification for formatted strings. It is recommended that the Base Types be used
1638 // everywhere, but in this one case, compliance with ANSI C is more important, and
1639 // provides an implementation that is compatible with that largest possible set of CPU
1640 // architectures. This is why the type "unsigned int" is used in this one case.
1642 Value
= (unsigned int)Value
;
1646 // Convert Value to a reversed string
1648 Count
= InternalPrintLibValueToString (ValueBuffer
, Value
, Radix
) - ValueBuffer
;
1649 if (Value
== 0 && Precision
== 0) {
1652 ArgumentString
= (CHAR8
*)ValueBuffer
+ Count
;
1656 Digits
= 3 - Digits
;
1658 if (Comma
&& Count
!= 0) {
1659 Count
+= ((Count
- 1) / 3);
1665 Flags
|= ARGUMENT_REVERSED
;
1667 if ((Flags
& PREFIX_ZERO
) != 0) {
1668 if ((Flags
& LEFT_JUSTIFY
) == 0) {
1669 if ((Flags
& PAD_TO_WIDTH
) != 0) {
1670 if ((Flags
& PRECISION
) == 0) {
1680 Flags
|= ARGUMENT_UNICODE
;
1682 // break skipped on purpose
1685 if (BaseListMarker
== NULL
) {
1686 ArgumentString
= VA_ARG (VaListMarker
, CHAR8
*);
1688 ArgumentString
= BASE_ARG (BaseListMarker
, CHAR8
*);
1690 if (ArgumentString
== NULL
) {
1691 Flags
&= (~(UINTN
)ARGUMENT_UNICODE
);
1692 ArgumentString
= "<null string>";
1695 // Set the default precision for string to be zero if not specified.
1697 if ((Flags
& PRECISION
) == 0) {
1703 if (BaseListMarker
== NULL
) {
1704 Character
= VA_ARG (VaListMarker
, UINTN
) & 0xffff;
1706 Character
= BASE_ARG (BaseListMarker
, UINTN
) & 0xffff;
1708 ArgumentString
= (CHAR8
*)&Character
;
1709 Flags
|= ARGUMENT_UNICODE
;
1713 if (BaseListMarker
== NULL
) {
1714 TmpGuid
= VA_ARG (VaListMarker
, GUID
*);
1716 TmpGuid
= BASE_ARG (BaseListMarker
, GUID
*);
1718 if (TmpGuid
== NULL
) {
1719 ArgumentString
= "<null guid>";
1721 GuidData1
= ReadUnaligned32 (&(TmpGuid
->Data1
));
1722 GuidData2
= ReadUnaligned16 (&(TmpGuid
->Data2
));
1723 GuidData3
= ReadUnaligned16 (&(TmpGuid
->Data3
));
1724 InternalPrintLibSPrint (
1726 MAXIMUM_VALUE_CHARACTERS
,
1728 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1741 ArgumentString
= ValueBuffer
;
1746 if (BaseListMarker
== NULL
) {
1747 TmpTime
= VA_ARG (VaListMarker
, TIME
*);
1749 TmpTime
= BASE_ARG (BaseListMarker
, TIME
*);
1751 if (TmpTime
== NULL
) {
1752 ArgumentString
= "<null time>";
1754 InternalPrintLibSPrint (
1756 MAXIMUM_VALUE_CHARACTERS
,
1758 "%02d/%02d/%04d %02d:%02d",
1765 ArgumentString
= ValueBuffer
;
1770 if (BaseListMarker
== NULL
) {
1771 Status
= VA_ARG (VaListMarker
, RETURN_STATUS
);
1773 Status
= BASE_ARG (BaseListMarker
, RETURN_STATUS
);
1775 ArgumentString
= ValueBuffer
;
1776 if (RETURN_ERROR (Status
)) {
1780 Index
= Status
& ~MAX_BIT
;
1781 if (Index
> 0 && Index
<= ERROR_STATUS_NUMBER
) {
1782 ArgumentString
= mStatusString
[Index
+ WARNING_STATUS_NUMBER
];
1786 if (Index
<= WARNING_STATUS_NUMBER
) {
1787 ArgumentString
= mStatusString
[Index
];
1790 if (ArgumentString
== ValueBuffer
) {
1791 InternalPrintLibSPrint ((CHAR8
*) ValueBuffer
, MAXIMUM_VALUE_CHARACTERS
, 0, "%08X", Status
);
1796 Format
+= BytesPerFormatCharacter
;
1797 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1798 if (FormatCharacter
== '\n') {
1800 // Translate '\r\n' to '\r\n'
1802 ArgumentString
= "\r\n";
1805 // Translate '\r' to '\r'
1807 ArgumentString
= "\r";
1808 Format
-= BytesPerFormatCharacter
;
1814 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1816 ArgumentString
= "\r\n";
1817 Format
+= BytesPerFormatCharacter
;
1818 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1819 if (FormatCharacter
!= '\r') {
1820 Format
-= BytesPerFormatCharacter
;
1827 // if the type is '%' or unknown, then print it to the screen
1829 ArgumentString
= (CHAR8
*)&FormatCharacter
;
1830 Flags
|= ARGUMENT_UNICODE
;
1836 Format
+= BytesPerFormatCharacter
;
1837 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1838 if (FormatCharacter
== '\n') {
1840 // Translate '\r\n' to '\r\n'
1842 ArgumentString
= "\r\n";
1845 // Translate '\r' to '\r'
1847 ArgumentString
= "\r";
1848 Format
-= BytesPerFormatCharacter
;
1854 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1856 ArgumentString
= "\r\n";
1857 Format
+= BytesPerFormatCharacter
;
1858 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1859 if (FormatCharacter
!= '\r') {
1860 Format
-= BytesPerFormatCharacter
;
1865 ArgumentString
= (CHAR8
*)&FormatCharacter
;
1866 Flags
|= ARGUMENT_UNICODE
;
1871 // Retrieve the ArgumentString attriubutes
1873 if ((Flags
& ARGUMENT_UNICODE
) != 0) {
1874 ArgumentMask
= 0xffff;
1875 BytesPerArgumentCharacter
= 2;
1877 ArgumentMask
= 0xff;
1878 BytesPerArgumentCharacter
= 1;
1880 if ((Flags
& ARGUMENT_REVERSED
) != 0) {
1881 BytesPerArgumentCharacter
= -BytesPerArgumentCharacter
;
1884 // Compute the number of characters in ArgumentString and store it in Count
1885 // ArgumentString is either null-terminated, or it contains Precision characters
1887 for (Count
= 0; Count
< Precision
|| ((Flags
& PRECISION
) == 0); Count
++) {
1888 ArgumentCharacter
= ((ArgumentString
[Count
* BytesPerArgumentCharacter
] & 0xff) | ((ArgumentString
[Count
* BytesPerArgumentCharacter
+ 1]) << 8)) & ArgumentMask
;
1889 if (ArgumentCharacter
== 0) {
1895 if (Precision
< Count
) {
1900 // Pad before the string
1902 if ((Flags
& (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) == (PAD_TO_WIDTH
)) {
1903 LengthToReturn
+= ((Width
- Precision
) * BytesPerOutputCharacter
);
1904 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1905 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Precision
, ' ', BytesPerOutputCharacter
);
1911 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1912 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1913 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, 1, Prefix
, BytesPerOutputCharacter
);
1916 LengthToReturn
+= ((Precision
- Count
) * BytesPerOutputCharacter
);
1917 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1918 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, Precision
- Count
, '0', BytesPerOutputCharacter
);
1921 LengthToReturn
+= ((Precision
- Count
) * BytesPerOutputCharacter
);
1922 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1923 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, Precision
- Count
, ' ', BytesPerOutputCharacter
);
1926 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1927 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1928 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, 1, Prefix
, BytesPerOutputCharacter
);
1934 // Output the Prefix character if it is present
1942 // Copy the string into the output buffer performing the required type conversions
1944 while (Index
< Count
) {
1945 ArgumentCharacter
= ((*ArgumentString
& 0xff) | (*(ArgumentString
+ 1) << 8)) & ArgumentMask
;
1947 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1948 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1949 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, 1, ArgumentCharacter
, BytesPerOutputCharacter
);
1951 ArgumentString
+= BytesPerArgumentCharacter
;
1958 if (Index
< Count
) {
1959 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1960 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1961 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', BytesPerOutputCharacter
);
1969 // Pad after the string
1971 if ((Flags
& (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) == (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) {
1972 LengthToReturn
+= ((Width
- Precision
) * BytesPerOutputCharacter
);
1973 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1974 Buffer
= InternalPrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Precision
, ' ', BytesPerOutputCharacter
);
1979 // Get the next character from the format string
1981 Format
+= BytesPerFormatCharacter
;
1984 // Get the next character from the format string
1986 FormatCharacter
= ((*Format
& 0xff) | (*(Format
+ 1) << 8)) & FormatMask
;
1989 if ((Flags
& COUNT_ONLY_NO_PRINT
) != 0) {
1990 return (LengthToReturn
/ BytesPerOutputCharacter
);
1993 ASSERT (Buffer
!= NULL
);
1995 // Null terminate the Unicode or ASCII string
1997 InternalPrintLibFillBuffer (Buffer
, EndBuffer
+ BytesPerOutputCharacter
, 1, 0, BytesPerOutputCharacter
);
1999 return ((Buffer
- OriginalBuffer
) / BytesPerOutputCharacter
);
2003 Returns the number of characters that would be produced by if the formatted
2004 output were produced not including the Null-terminator.
2006 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
2008 If FormatString is NULL, then ASSERT() and 0 is returned.
2009 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more
2010 than PcdMaximumUnicodeStringLength Unicode characters not including the
2011 Null-terminator, then ASSERT() and 0 is returned.
2013 @param[in] FormatString A Null-terminated Unicode format string.
2014 @param[in] Marker VA_LIST marker for the variable argument list.
2016 @return The number of characters that would be produced, not including the
2022 IN CONST CHAR16
*FormatString
,
2026 ASSERT_UNICODE_BUFFER (FormatString
);
2027 return InternalPrintLibSPrintMarker (NULL
, 0, FORMAT_UNICODE
| OUTPUT_UNICODE
| COUNT_ONLY_NO_PRINT
, (CHAR8
*)FormatString
, Marker
, NULL
);
2031 Returns the number of characters that would be produced by if the formatted
2032 output were produced not including the Null-terminator.
2034 If FormatString is NULL, then ASSERT() and 0 is returned.
2035 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more
2036 than PcdMaximumAsciiStringLength Ascii characters not including the
2037 Null-terminator, then ASSERT() and 0 is returned.
2039 @param[in] FormatString A Null-terminated ASCII format string.
2040 @param[in] Marker VA_LIST marker for the variable argument list.
2042 @return The number of characters that would be produced, not including the
2047 SPrintLengthAsciiFormat (
2048 IN CONST CHAR8
*FormatString
,
2052 return InternalPrintLibSPrintMarker (NULL
, 0, OUTPUT_UNICODE
| COUNT_ONLY_NO_PRINT
, (CHAR8
*)FormatString
, Marker
, NULL
);