]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
MdeModulePkg: Remove UefiBootServicesTableLib dependency from DxePrintLibPrint2Protocol.
[mirror_edk2.git] / MdeModulePkg / Library / DxePrintLibPrint2Protocol / PrintLib.c
1 /** @file
2 Instance of Print Library based on gEfiPrint2ProtocolGuid.
3
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.
8
9 Copyright (c) 2009 - 2011, 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
14
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.
17
18 **/
19
20 #include <Uefi.h>
21 #include <Base.h>
22 #include <Protocol/Print2.h>
23
24 #include <Library/PrintLib.h>
25
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28
29 EFI_PRINT2_PROTOCOL *mPrint2Protocol = NULL;
30
31 /**
32 The constructor function caches the pointer to Print2 protocol.
33
34 The constructor function locates Print2 protocol from protocol database.
35 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
36
37 @param ImageHandle The firmware allocated handle for the EFI image.
38 @param SystemTable A pointer to the EFI System Table.
39
40 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
41
42 **/
43 EFI_STATUS
44 EFIAPI
45 PrintLibConstructor (
46 IN EFI_HANDLE ImageHandle,
47 IN EFI_SYSTEM_TABLE *SystemTable
48 )
49 {
50 EFI_STATUS Status;
51
52 Status = SystemTable->BootServices->LocateProtocol (
53 &gEfiPrint2ProtocolGuid,
54 NULL,
55 (VOID**) &mPrint2Protocol
56 );
57 ASSERT_EFI_ERROR (Status);
58 ASSERT (mPrint2Protocol != NULL);
59
60 return Status;
61 }
62
63
64 /**
65 Worker function that converts a VA_LIST to a BASE_LIST based on a Null-terminated
66 format string.
67
68 @param AsciiFormat TRUE if Format is an ASCII string. FALSE if Format is a Unicode string.
69 @param Format Null-terminated format string.
70 @param VaListMarker VA_LIST style variable argument list consumed by processing Format.
71 @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format.
72 @param Size The size, in bytes, of the BaseListMarker buffer.
73
74 @return The number of bytes in BaseListMarker. 0 if BaseListMarker is too small.
75
76 **/
77 BOOLEAN
78 DxePrintLibPrint2ProtocolVaListToBaseList (
79 IN BOOLEAN AsciiFormat,
80 IN CONST CHAR8 *Format,
81 IN VA_LIST VaListMarker,
82 OUT BASE_LIST BaseListMarker,
83 IN UINTN Size
84 )
85 {
86 BASE_LIST BaseListStart;
87 UINTN BytesPerFormatCharacter;
88 UINTN FormatMask;
89 UINTN FormatCharacter;
90 BOOLEAN Long;
91 BOOLEAN Done;
92
93 ASSERT (Format != NULL);
94 ASSERT (BaseListMarker != NULL);
95
96 BaseListStart = BaseListMarker;
97
98 if (AsciiFormat) {
99 ASSERT (AsciiStrSize (Format) != 0);
100 BytesPerFormatCharacter = 1;
101 FormatMask = 0xff;
102 } else {
103 ASSERT (StrSize ((CHAR16 *) Format) != 0);
104 BytesPerFormatCharacter = 2;
105 FormatMask = 0xffff;
106 }
107
108 //
109 // Get the first character from the format string
110 //
111 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
112
113 while (FormatCharacter != 0) {
114 if (FormatCharacter == '%') {
115 Long = FALSE;
116
117 //
118 // Parse Flags and Width
119 //
120 for (Done = FALSE; !Done; ) {
121 //
122 // Get the next character from the format string
123 //
124 Format += BytesPerFormatCharacter;
125
126 //
127 // Get the next character from the format string
128 //
129 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
130
131 switch (FormatCharacter) {
132 case '.':
133 case '-':
134 case '+':
135 case ' ':
136 case ',':
137 case '0':
138 case '1':
139 case '2':
140 case '3':
141 case '4':
142 case '5':
143 case '6':
144 case '7':
145 case '8':
146 case '9':
147 break;
148 case 'L':
149 case 'l':
150 Long = TRUE;
151 break;
152 case '*':
153 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
154 break;
155 case '\0':
156 //
157 // Make no output if Format string terminates unexpectedly when
158 // looking up for flag, width, precision and type.
159 //
160 Format -= BytesPerFormatCharacter;
161 //
162 // break skipped on purpose.
163 //
164 default:
165 Done = TRUE;
166 break;
167 }
168 }
169
170 //
171 // Handle each argument type
172 //
173 switch (FormatCharacter) {
174 case 'p':
175 if (sizeof (VOID *) > 4) {
176 Long = TRUE;
177 }
178 case 'X':
179 case 'x':
180 case 'd':
181 if (Long) {
182 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);
183 } else {
184 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);
185 }
186 break;
187 case 's':
188 case 'S':
189 case 'a':
190 case 'g':
191 case 't':
192 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);
193 break;
194 case 'c':
195 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
196 break;
197 case 'r':
198 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);
199 break;
200 }
201 }
202
203 //
204 // If BASE_LIST is larger than Size, then return FALSE
205 //
206 if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {
207 return FALSE;
208 }
209
210 //
211 // Get the next character from the format string
212 //
213 Format += BytesPerFormatCharacter;
214
215 //
216 // Get the next character from the format string
217 //
218 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
219 }
220 return TRUE;
221 }
222
223 /**
224 Produces a Null-terminated Unicode string in an output buffer based on
225 a Null-terminated Unicode format string and a VA_LIST argument list
226
227 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
228 and BufferSize.
229 The Unicode string is produced by parsing the format string specified by FormatString.
230 Arguments are pulled from the variable argument list specified by Marker based on the
231 contents of the format string.
232 The number of Unicode characters in the produced output buffer is returned not including
233 the Null-terminator.
234 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
235
236 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
237 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
238 If BufferSize > 1 and FormatString is NULL, then ASSERT().
239 If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
240 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
241 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
242 ASSERT().
243 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
244 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
245 Null-terminator, then ASSERT().
246
247 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
248 Unicode string.
249 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
250 @param FormatString Null-terminated Unicode format string.
251 @param Marker VA_LIST marker for the variable argument list.
252
253 @return The number of Unicode characters in the produced output buffer not including the
254 Null-terminator.
255
256 **/
257 UINTN
258 EFIAPI
259 UnicodeVSPrint (
260 OUT CHAR16 *StartOfBuffer,
261 IN UINTN BufferSize,
262 IN CONST CHAR16 *FormatString,
263 IN VA_LIST Marker
264 )
265 {
266 UINT64 BaseListMarker[256 / sizeof (UINT64)];
267
268 DxePrintLibPrint2ProtocolVaListToBaseList (
269 FALSE,
270 (CHAR8 *)FormatString,
271 Marker,
272 (BASE_LIST)BaseListMarker,
273 sizeof (BaseListMarker) - 8
274 );
275
276 return UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
277 }
278
279 /**
280 Produces a Null-terminated Unicode string in an output buffer based on
281 a Null-terminated Unicode format string and a BASE_LIST argument list
282
283 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
284 and BufferSize.
285 The Unicode string is produced by parsing the format string specified by FormatString.
286 Arguments are pulled from the variable argument list specified by Marker based on the
287 contents of the format string.
288 The number of Unicode characters in the produced output buffer is returned not including
289 the Null-terminator.
290 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
291
292 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
293 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
294 If BufferSize > 1 and FormatString is NULL, then ASSERT().
295 If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
296 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
297 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
298 ASSERT().
299 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
300 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
301 Null-terminator, then ASSERT().
302
303 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
304 Unicode string.
305 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
306 @param FormatString Null-terminated Unicode format string.
307 @param Marker BASE_LIST marker for the variable argument list.
308
309 @return The number of Unicode characters in the produced output buffer not including the
310 Null-terminator.
311
312 **/
313 UINTN
314 EFIAPI
315 UnicodeBSPrint (
316 OUT CHAR16 *StartOfBuffer,
317 IN UINTN BufferSize,
318 IN CONST CHAR16 *FormatString,
319 IN BASE_LIST Marker
320 )
321 {
322 return mPrint2Protocol->UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
323 }
324
325 /**
326 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
327 Unicode format string and variable argument list.
328
329 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
330 and BufferSize.
331 The Unicode string is produced by parsing the format string specified by FormatString.
332 Arguments are pulled from the variable argument list based on the contents of the format string.
333 The number of Unicode characters in the produced output buffer is returned not including
334 the Null-terminator.
335 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
336
337 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
338 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
339 If BufferSize > 1 and FormatString is NULL, then ASSERT().
340 If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
341 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
342 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
343 ASSERT().
344 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
345 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
346 Null-terminator, then ASSERT().
347
348 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
349 Unicode string.
350 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
351 @param FormatString Null-terminated Unicode format string.
352 @param ... Variable argument list whose contents are accessed based on the
353 format string specified by FormatString.
354
355 @return The number of Unicode characters in the produced output buffer not including the
356 Null-terminator.
357
358 **/
359 UINTN
360 EFIAPI
361 UnicodeSPrint (
362 OUT CHAR16 *StartOfBuffer,
363 IN UINTN BufferSize,
364 IN CONST CHAR16 *FormatString,
365 ...
366 )
367 {
368 VA_LIST Marker;
369
370 VA_START (Marker, FormatString);
371 return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
372 }
373
374 /**
375 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
376 ASCII format string and a VA_LIST argument list
377
378 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
379 and BufferSize.
380 The Unicode string is produced by parsing the format string specified by FormatString.
381 Arguments are pulled from the variable argument list specified by Marker based on the
382 contents of the format string.
383 The number of Unicode characters in the produced output buffer is returned not including
384 the Null-terminator.
385 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
386
387 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
388 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
389 If BufferSize > 1 and FormatString is NULL, then ASSERT().
390 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
391 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
392 ASSERT().
393 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
394 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
395 Null-terminator, then ASSERT().
396
397 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
398 Unicode string.
399 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
400 @param FormatString Null-terminated Unicode format string.
401 @param Marker VA_LIST marker for the variable argument list.
402
403 @return The number of Unicode characters in the produced output buffer not including the
404 Null-terminator.
405
406 **/
407 UINTN
408 EFIAPI
409 UnicodeVSPrintAsciiFormat (
410 OUT CHAR16 *StartOfBuffer,
411 IN UINTN BufferSize,
412 IN CONST CHAR8 *FormatString,
413 IN VA_LIST Marker
414 )
415 {
416 UINT64 BaseListMarker[256 / sizeof (UINT64)];
417
418 DxePrintLibPrint2ProtocolVaListToBaseList (
419 TRUE,
420 FormatString,
421 Marker,
422 (BASE_LIST)BaseListMarker,
423 sizeof (BaseListMarker) - 8
424 );
425
426 return UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
427 }
428
429 /**
430 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
431 ASCII format string and a BASE_LIST argument list
432
433 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
434 and BufferSize.
435 The Unicode string is produced by parsing the format string specified by FormatString.
436 Arguments are pulled from the variable argument list specified by Marker based on the
437 contents of the format string.
438 The number of Unicode characters in the produced output buffer is returned not including
439 the Null-terminator.
440 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
441
442 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
443 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
444 If BufferSize > 1 and FormatString is NULL, then ASSERT().
445 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
446 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
447 ASSERT().
448 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
449 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
450 Null-terminator, then ASSERT().
451
452 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
453 Unicode string.
454 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
455 @param FormatString Null-terminated Unicode format string.
456 @param Marker BASE_LIST marker for the variable argument list.
457
458 @return The number of Unicode characters in the produced output buffer not including the
459 Null-terminator.
460
461 **/
462 UINTN
463 EFIAPI
464 UnicodeBSPrintAsciiFormat (
465 OUT CHAR16 *StartOfBuffer,
466 IN UINTN BufferSize,
467 IN CONST CHAR8 *FormatString,
468 IN BASE_LIST Marker
469 )
470 {
471 return mPrint2Protocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);
472 }
473
474 /**
475 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
476 ASCII format string and variable argument list.
477
478 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
479 and BufferSize.
480 The Unicode string is produced by parsing the format string specified by FormatString.
481 Arguments are pulled from the variable argument list based on the contents of the
482 format string.
483 The number of Unicode characters in the produced output buffer is returned not including
484 the Null-terminator.
485 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
486
487 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
488 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
489 If BufferSize > 1 and FormatString is NULL, then ASSERT().
490 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
491 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
492 ASSERT().
493 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
494 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
495 Null-terminator, then ASSERT().
496
497 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
498 Unicode string.
499 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
500 @param FormatString Null-terminated Unicode format string.
501 @param ... Variable argument list whose contents are accessed based on the
502 format string specified by FormatString.
503
504 @return The number of Unicode characters in the produced output buffer not including the
505 Null-terminator.
506
507 **/
508 UINTN
509 EFIAPI
510 UnicodeSPrintAsciiFormat (
511 OUT CHAR16 *StartOfBuffer,
512 IN UINTN BufferSize,
513 IN CONST CHAR8 *FormatString,
514 ...
515 )
516 {
517 VA_LIST Marker;
518
519 VA_START (Marker, FormatString);
520 return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);
521 }
522
523 /**
524 Converts a decimal value to a Null-terminated Unicode string.
525
526 Converts the decimal number specified by Value to a Null-terminated Unicode
527 string specified by Buffer containing at most Width characters. No padding of spaces
528 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
529 The number of Unicode characters in Buffer is returned not including the Null-terminator.
530 If the conversion contains more than Width characters, then only the first
531 Width characters are returned, and the total number of characters
532 required to perform the conversion is returned.
533 Additional conversion parameters are specified in Flags.
534
535 The Flags bit LEFT_JUSTIFY is always ignored.
536 All conversions are left justified in Buffer.
537 If Width is 0, PREFIX_ZERO is ignored in Flags.
538 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
539 are inserted every 3rd digit starting from the right.
540 If RADIX_HEX is set in Flags, then the output buffer will be
541 formatted in hexadecimal format.
542 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
543 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
544 then Buffer is padded with '0' characters so the combination of the optional '-'
545 sign character, '0' characters, digit characters for Value, and the Null-terminator
546 add up to Width characters.
547 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
548 If Buffer is NULL, then ASSERT().
549 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
550 If unsupported bits are set in Flags, then ASSERT().
551 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
552 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
553
554 @param Buffer Pointer to the output buffer for the produced Null-terminated
555 Unicode string.
556 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
557 @param Value The 64-bit signed value to convert to a string.
558 @param Width The maximum number of Unicode characters to place in Buffer, not including
559 the Null-terminator.
560
561 @return The number of Unicode characters in Buffer not including the Null-terminator.
562
563 **/
564 UINTN
565 EFIAPI
566 UnicodeValueToString (
567 IN OUT CHAR16 *Buffer,
568 IN UINTN Flags,
569 IN INT64 Value,
570 IN UINTN Width
571 )
572 {
573 return mPrint2Protocol->UnicodeValueToString (Buffer, Flags, Value, Width);
574 }
575
576 /**
577 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
578 ASCII format string and a VA_LIST argument list.
579
580 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
581 and BufferSize.
582 The ASCII string is produced by parsing the format string specified by FormatString.
583 Arguments are pulled from the variable argument list specified by Marker based on
584 the contents of the format string.
585 The number of ASCII characters in the produced output buffer is returned not including
586 the Null-terminator.
587 If BufferSize is 0, then no output buffer is produced and 0 is returned.
588
589 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
590 If BufferSize > 0 and FormatString is NULL, then ASSERT().
591 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
592 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
593 ASSERT().
594 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
595 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
596 Null-terminator, then ASSERT().
597
598 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
599 ASCII string.
600 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
601 @param FormatString Null-terminated Unicode format string.
602 @param Marker VA_LIST marker for the variable argument list.
603
604 @return The number of ASCII characters in the produced output buffer not including the
605 Null-terminator.
606
607 **/
608 UINTN
609 EFIAPI
610 AsciiVSPrint (
611 OUT CHAR8 *StartOfBuffer,
612 IN UINTN BufferSize,
613 IN CONST CHAR8 *FormatString,
614 IN VA_LIST Marker
615 )
616 {
617 UINT64 BaseListMarker[256 / sizeof (UINT64)];
618
619 DxePrintLibPrint2ProtocolVaListToBaseList (
620 TRUE,
621 FormatString,
622 Marker,
623 (BASE_LIST)BaseListMarker,
624 sizeof (BaseListMarker) - 8
625 );
626
627 return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
628 }
629
630 /**
631 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
632 ASCII format string and a BASE_LIST argument list.
633
634 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
635 and BufferSize.
636 The ASCII string is produced by parsing the format string specified by FormatString.
637 Arguments are pulled from the variable argument list specified by Marker based on
638 the contents of the format string.
639 The number of ASCII characters in the produced output buffer is returned not including
640 the Null-terminator.
641 If BufferSize is 0, then no output buffer is produced and 0 is returned.
642
643 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
644 If BufferSize > 0 and FormatString is NULL, then ASSERT().
645 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
646 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
647 ASSERT().
648 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
649 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
650 Null-terminator, then ASSERT().
651
652 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
653 ASCII string.
654 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
655 @param FormatString Null-terminated Unicode format string.
656 @param Marker BASE_LIST marker for the variable argument list.
657
658 @return The number of ASCII characters in the produced output buffer not including the
659 Null-terminator.
660
661 **/
662 UINTN
663 EFIAPI
664 AsciiBSPrint (
665 OUT CHAR8 *StartOfBuffer,
666 IN UINTN BufferSize,
667 IN CONST CHAR8 *FormatString,
668 IN BASE_LIST Marker
669 )
670 {
671 return mPrint2Protocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
672 }
673
674 /**
675 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
676 ASCII format string and variable argument list.
677
678 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
679 and BufferSize.
680 The ASCII string is produced by parsing the format string specified by FormatString.
681 Arguments are pulled from the variable argument list based on the contents of the
682 format string.
683 The number of ASCII characters in the produced output buffer is returned not including
684 the Null-terminator.
685 If BufferSize is 0, then no output buffer is produced and 0 is returned.
686
687 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
688 If BufferSize > 0 and FormatString is NULL, then ASSERT().
689 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
690 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
691 ASSERT().
692 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
693 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
694 Null-terminator, then ASSERT().
695
696 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
697 ASCII string.
698 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
699 @param FormatString Null-terminated Unicode format string.
700 @param ... Variable argument list whose contents are accessed based on the
701 format string specified by FormatString.
702
703 @return The number of ASCII characters in the produced output buffer not including the
704 Null-terminator.
705
706 **/
707 UINTN
708 EFIAPI
709 AsciiSPrint (
710 OUT CHAR8 *StartOfBuffer,
711 IN UINTN BufferSize,
712 IN CONST CHAR8 *FormatString,
713 ...
714 )
715 {
716 VA_LIST Marker;
717
718 VA_START (Marker, FormatString);
719 return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
720 }
721
722 /**
723 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
724 ASCII format string and a VA_LIST argument list.
725
726 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
727 and BufferSize.
728 The ASCII string is produced by parsing the format string specified by FormatString.
729 Arguments are pulled from the variable argument list specified by Marker based on
730 the contents of the format string.
731 The number of ASCII characters in the produced output buffer is returned not including
732 the Null-terminator.
733 If BufferSize is 0, then no output buffer is produced and 0 is returned.
734
735 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
736 If BufferSize > 0 and FormatString is NULL, then ASSERT().
737 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
738 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
739 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
740 ASSERT().
741 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
742 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
743 Null-terminator, then ASSERT().
744
745 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
746 ASCII string.
747 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
748 @param FormatString Null-terminated Unicode format string.
749 @param Marker VA_LIST marker for the variable argument list.
750
751 @return The number of ASCII characters in the produced output buffer not including the
752 Null-terminator.
753
754 **/
755 UINTN
756 EFIAPI
757 AsciiVSPrintUnicodeFormat (
758 OUT CHAR8 *StartOfBuffer,
759 IN UINTN BufferSize,
760 IN CONST CHAR16 *FormatString,
761 IN VA_LIST Marker
762 )
763 {
764 UINT64 BaseListMarker[256 / sizeof (UINT64)];
765
766 DxePrintLibPrint2ProtocolVaListToBaseList (
767 FALSE,
768 (CHAR8 *)FormatString,
769 Marker,
770 (BASE_LIST)BaseListMarker,
771 sizeof (BaseListMarker) - 8
772 );
773
774 return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
775 }
776
777 /**
778 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
779 ASCII format string and a BASE_LIST argument list.
780
781 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
782 and BufferSize.
783 The ASCII string is produced by parsing the format string specified by FormatString.
784 Arguments are pulled from the variable argument list specified by Marker based on
785 the contents of the format string.
786 The number of ASCII characters in the produced output buffer is returned not including
787 the Null-terminator.
788 If BufferSize is 0, then no output buffer is produced and 0 is returned.
789
790 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
791 If BufferSize > 0 and FormatString is NULL, then ASSERT().
792 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
793 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
794 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
795 ASSERT().
796 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
797 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
798 Null-terminator, then ASSERT().
799
800 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
801 ASCII string.
802 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
803 @param FormatString Null-terminated Unicode format string.
804 @param Marker BASE_LIST marker for the variable argument list.
805
806 @return The number of ASCII characters in the produced output buffer not including the
807 Null-terminator.
808
809 **/
810 UINTN
811 EFIAPI
812 AsciiBSPrintUnicodeFormat (
813 OUT CHAR8 *StartOfBuffer,
814 IN UINTN BufferSize,
815 IN CONST CHAR16 *FormatString,
816 IN BASE_LIST Marker
817 )
818 {
819 return mPrint2Protocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
820 }
821
822 /**
823 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
824 ASCII format string and variable argument list.
825
826 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
827 and BufferSize.
828 The ASCII string is produced by parsing the format string specified by FormatString.
829 Arguments are pulled from the variable argument list based on the contents of the
830 format string.
831 The number of ASCII characters in the produced output buffer is returned not including
832 the Null-terminator.
833 If BufferSize is 0, then no output buffer is produced and 0 is returned.
834
835 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
836 If BufferSize > 0 and FormatString is NULL, then ASSERT().
837 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
838 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
839 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
840 ASSERT().
841 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
842 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
843 Null-terminator, then ASSERT().
844
845 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
846 ASCII string.
847 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
848 @param FormatString Null-terminated Unicode format string.
849 @param ... Variable argument list whose contents are accessed based on the
850 format string specified by FormatString.
851
852 @return The number of ASCII characters in the produced output buffer not including the
853 Null-terminator.
854
855 **/
856 UINTN
857 EFIAPI
858 AsciiSPrintUnicodeFormat (
859 OUT CHAR8 *StartOfBuffer,
860 IN UINTN BufferSize,
861 IN CONST CHAR16 *FormatString,
862 ...
863 )
864 {
865 VA_LIST Marker;
866
867 VA_START (Marker, FormatString);
868 return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
869 }
870
871
872 /**
873 Converts a decimal value to a Null-terminated ASCII string.
874
875 Converts the decimal number specified by Value to a Null-terminated ASCII string
876 specified by Buffer containing at most Width characters. No padding of spaces
877 is ever performed.
878 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
879 The number of ASCII characters in Buffer is returned not including the Null-terminator.
880 If the conversion contains more than Width characters, then only the first Width
881 characters are returned, and the total number of characters required to perform
882 the conversion is returned.
883 Additional conversion parameters are specified in Flags.
884 The Flags bit LEFT_JUSTIFY is always ignored.
885 All conversions are left justified in Buffer.
886 If Width is 0, PREFIX_ZERO is ignored in Flags.
887 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
888 are inserted every 3rd digit starting from the right.
889 If RADIX_HEX is set in Flags, then the output buffer will be
890 formatted in hexadecimal format.
891 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
892 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
893 then Buffer is padded with '0' characters so the combination of the optional '-'
894 sign character, '0' characters, digit characters for Value, and the Null-terminator
895 add up to Width characters.
896
897 If Buffer is NULL, then ASSERT().
898 If unsupported bits are set in Flags, then ASSERT().
899 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
900 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
901
902 @param Buffer Pointer to the output buffer for the produced Null-terminated
903 ASCII string.
904 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
905 @param Value The 64-bit signed value to convert to a string.
906 @param Width The maximum number of ASCII characters to place in Buffer, not including
907 the Null-terminator.
908
909 @return The number of ASCII characters in Buffer not including the Null-terminator.
910
911 **/
912 UINTN
913 EFIAPI
914 AsciiValueToString (
915 OUT CHAR8 *Buffer,
916 IN UINTN Flags,
917 IN INT64 Value,
918 IN UINTN Width
919 )
920 {
921 return mPrint2Protocol->AsciiValueToString (Buffer, Flags, Value, Width);
922 }
923
924 #define PREFIX_SIGN BIT1
925 #define PREFIX_BLANK BIT2
926 #define LONG_TYPE BIT4
927 #define OUTPUT_UNICODE BIT6
928 #define FORMAT_UNICODE BIT8
929 #define PAD_TO_WIDTH BIT9
930 #define ARGUMENT_UNICODE BIT10
931 #define PRECISION BIT11
932 #define ARGUMENT_REVERSED BIT12
933 #define COUNT_ONLY_NO_PRINT BIT13
934
935 //
936 // Record date and time information
937 //
938 typedef struct {
939 UINT16 Year;
940 UINT8 Month;
941 UINT8 Day;
942 UINT8 Hour;
943 UINT8 Minute;
944 UINT8 Second;
945 UINT8 Pad1;
946 UINT32 Nanosecond;
947 INT16 TimeZone;
948 UINT8 Daylight;
949 UINT8 Pad2;
950 } TIME;
951
952 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
953
954 /**
955 Internal function that convert a number to a string in Buffer.
956
957 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
958
959 @param Buffer Location to place the ASCII string of Value.
960 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
961 @param Radix Radix of the value
962
963 @return A pointer to the end of buffer filled with ASCII string.
964
965 **/
966 CHAR8 *
967 InternalPrintLibValueToString (
968 IN OUT CHAR8 *Buffer,
969 IN INT64 Value,
970 IN UINTN Radix
971 )
972 {
973 UINT32 Remainder;
974
975 //
976 // Loop to convert one digit at a time in reverse order
977 //
978 *Buffer = 0;
979 do {
980 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
981 *(++Buffer) = mHexStr[Remainder];
982 } while (Value != 0);
983
984 //
985 // Return pointer of the end of filled buffer.
986 //
987 return Buffer;
988 }
989
990 /**
991 Worker function that produces a Null-terminated string in an output buffer
992 based on a Null-terminated format string and a VA_LIST argument list.
993
994 VSPrint function to process format and place the results in Buffer. Since a
995 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
996 this is the main print working routine.
997
998 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
999
1000 @param[out] Buffer The character buffer to print the results of the
1001 parsing of Format into.
1002 @param[in] BufferSize The maximum number of characters to put into
1003 buffer.
1004 @param[in] Flags Initial flags value.
1005 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1006 and COUNT_ONLY_NO_PRINT set.
1007 @param[in] Format A Null-terminated format string.
1008 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1009 processing Format.
1010 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1011 by processing Format.
1012
1013 @return The number of characters printed not including the Null-terminator.
1014 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1015 modification to Buffer.
1016
1017 **/
1018 UINTN
1019 InternalPrintLibSPrintMarker (
1020 OUT CHAR8 *Buffer,
1021 IN UINTN BufferSize,
1022 IN UINTN Flags,
1023 IN CONST CHAR8 *Format,
1024 IN VA_LIST VaListMarker, OPTIONAL
1025 IN BASE_LIST BaseListMarker OPTIONAL
1026 );
1027
1028 /**
1029 Worker function that produces a Null-terminated string in an output buffer
1030 based on a Null-terminated format string and variable argument list.
1031
1032 VSPrint function to process format and place the results in Buffer. Since a
1033 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1034 this is the main print working routine
1035
1036 @param StartOfBuffer The character buffer to print the results of the parsing
1037 of Format into.
1038 @param BufferSize The maximum number of characters to put into buffer.
1039 Zero means no limit.
1040 @param Flags Initial flags value.
1041 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1042 @param FormatString A Null-terminated format string.
1043 @param ... The variable argument list.
1044
1045 @return The number of characters printed.
1046
1047 **/
1048 UINTN
1049 EFIAPI
1050 InternalPrintLibSPrint (
1051 OUT CHAR8 *StartOfBuffer,
1052 IN UINTN BufferSize,
1053 IN UINTN Flags,
1054 IN CONST CHAR8 *FormatString,
1055 ...
1056 )
1057 {
1058 VA_LIST Marker;
1059
1060 VA_START (Marker, FormatString);
1061 return InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);
1062 }
1063
1064 #define WARNING_STATUS_NUMBER 4
1065 #define ERROR_STATUS_NUMBER 24
1066
1067 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = {
1068 "Success", // RETURN_SUCCESS = 0
1069 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
1070 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
1071 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
1072 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
1073 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
1074 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
1075 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
1076 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
1077 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
1078 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
1079 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
1080 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
1081 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
1082 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
1083 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
1084 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
1085 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
1086 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
1087 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
1088 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
1089 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
1090 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
1091 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
1092 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
1093 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
1094 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
1095 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
1096 "Protocol Error" // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
1097 };
1098
1099 /**
1100 Worker function that produces a Null-terminated string in an output buffer
1101 based on a Null-terminated format string and a VA_LIST argument list.
1102
1103 VSPrint function to process format and place the results in Buffer. Since a
1104 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1105 this is the main print working routine.
1106
1107 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1108
1109 @param[out] Buffer The character buffer to print the results of the
1110 parsing of Format into.
1111 @param[in] BufferSize The maximum number of characters to put into
1112 buffer.
1113 @param[in] Flags Initial flags value.
1114 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1115 and COUNT_ONLY_NO_PRINT set.
1116 @param[in] Format A Null-terminated format string.
1117 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1118 processing Format.
1119 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1120 by processing Format.
1121
1122 @return The number of characters printed not including the Null-terminator.
1123 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1124 modification to Buffer.
1125
1126 **/
1127 UINTN
1128 InternalPrintLibSPrintMarker (
1129 OUT CHAR8 *Buffer,
1130 IN UINTN BufferSize,
1131 IN UINTN Flags,
1132 IN CONST CHAR8 *Format,
1133 IN VA_LIST VaListMarker, OPTIONAL
1134 IN BASE_LIST BaseListMarker OPTIONAL
1135 )
1136 {
1137 CHAR8 *OriginalBuffer;
1138 CHAR8 *EndBuffer;
1139 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
1140 UINT32 BytesPerOutputCharacter;
1141 UINTN BytesPerFormatCharacter;
1142 UINTN FormatMask;
1143 UINTN FormatCharacter;
1144 UINTN Width;
1145 UINTN Precision;
1146 INT64 Value;
1147 CONST CHAR8 *ArgumentString;
1148 UINTN Character;
1149 GUID *TmpGuid;
1150 TIME *TmpTime;
1151 UINTN Count;
1152 UINTN ArgumentMask;
1153 INTN BytesPerArgumentCharacter;
1154 UINTN ArgumentCharacter;
1155 BOOLEAN Done;
1156 UINTN Index;
1157 CHAR8 Prefix;
1158 BOOLEAN ZeroPad;
1159 BOOLEAN Comma;
1160 UINTN Digits;
1161 UINTN Radix;
1162 RETURN_STATUS Status;
1163 UINT32 GuidData1;
1164 UINT16 GuidData2;
1165 UINT16 GuidData3;
1166 UINTN LengthToReturn;
1167
1168 //
1169 // If you change this code be sure to match the 2 versions of this function.
1170 // Nearly identical logic is found in the BasePrintLib and
1171 // DxePrintLibPrint2Protocol (both PrintLib instances).
1172 //
1173
1174 ASSERT(Flags & COUNT_ONLY_NO_PRINT);
1175
1176 if ((Flags & OUTPUT_UNICODE) != 0) {
1177 BytesPerOutputCharacter = 2;
1178 } else {
1179 BytesPerOutputCharacter = 1;
1180 }
1181
1182 LengthToReturn = 0;
1183
1184 //
1185 // Reserve space for the Null terminator.
1186 //
1187 BufferSize--;
1188 OriginalBuffer = Buffer;
1189
1190 //
1191 // Set the tag for the end of the input Buffer.
1192 //
1193 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
1194
1195 if ((Flags & FORMAT_UNICODE) != 0) {
1196 //
1197 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
1198 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
1199 //
1200 ASSERT (StrSize ((CHAR16 *) Format) != 0);
1201 BytesPerFormatCharacter = 2;
1202 FormatMask = 0xffff;
1203 } else {
1204 //
1205 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength
1206 // Ascii characters if PcdMaximumAsciiStringLength is not zero.
1207 //
1208 ASSERT (AsciiStrSize (Format) != 0);
1209 BytesPerFormatCharacter = 1;
1210 FormatMask = 0xff;
1211 }
1212
1213 //
1214 // Get the first character from the format string
1215 //
1216 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1217
1218 //
1219 // Loop until the end of the format string is reached or the output buffer is full
1220 //
1221 while (FormatCharacter != 0 && Buffer < EndBuffer) {
1222 //
1223 // Clear all the flag bits except those that may have been passed in
1224 //
1225 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
1226
1227 //
1228 // Set the default width to zero, and the default precision to 1
1229 //
1230 Width = 0;
1231 Precision = 1;
1232 Prefix = 0;
1233 Comma = FALSE;
1234 ZeroPad = FALSE;
1235 Count = 0;
1236 Digits = 0;
1237
1238 switch (FormatCharacter) {
1239 case '%':
1240 //
1241 // Parse Flags and Width
1242 //
1243 for (Done = FALSE; !Done; ) {
1244 Format += BytesPerFormatCharacter;
1245 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1246 switch (FormatCharacter) {
1247 case '.':
1248 Flags |= PRECISION;
1249 break;
1250 case '-':
1251 Flags |= LEFT_JUSTIFY;
1252 break;
1253 case '+':
1254 Flags |= PREFIX_SIGN;
1255 break;
1256 case ' ':
1257 Flags |= PREFIX_BLANK;
1258 break;
1259 case ',':
1260 Flags |= COMMA_TYPE;
1261 break;
1262 case 'L':
1263 case 'l':
1264 Flags |= LONG_TYPE;
1265 break;
1266 case '*':
1267 if ((Flags & PRECISION) == 0) {
1268 Flags |= PAD_TO_WIDTH;
1269 if (BaseListMarker == NULL) {
1270 Width = VA_ARG (VaListMarker, UINTN);
1271 } else {
1272 Width = BASE_ARG (BaseListMarker, UINTN);
1273 }
1274 } else {
1275 if (BaseListMarker == NULL) {
1276 Precision = VA_ARG (VaListMarker, UINTN);
1277 } else {
1278 Precision = BASE_ARG (BaseListMarker, UINTN);
1279 }
1280 }
1281 break;
1282 case '0':
1283 if ((Flags & PRECISION) == 0) {
1284 Flags |= PREFIX_ZERO;
1285 }
1286 case '1':
1287 case '2':
1288 case '3':
1289 case '4':
1290 case '5':
1291 case '6':
1292 case '7':
1293 case '8':
1294 case '9':
1295 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
1296 Count = (Count * 10) + FormatCharacter - '0';
1297 Format += BytesPerFormatCharacter;
1298 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1299 }
1300 Format -= BytesPerFormatCharacter;
1301 if ((Flags & PRECISION) == 0) {
1302 Flags |= PAD_TO_WIDTH;
1303 Width = Count;
1304 } else {
1305 Precision = Count;
1306 }
1307 break;
1308
1309 case '\0':
1310 //
1311 // Make no output if Format string terminates unexpectedly when
1312 // looking up for flag, width, precision and type.
1313 //
1314 Format -= BytesPerFormatCharacter;
1315 Precision = 0;
1316 //
1317 // break skipped on purpose.
1318 //
1319 default:
1320 Done = TRUE;
1321 break;
1322 }
1323 }
1324
1325 //
1326 // Handle each argument type
1327 //
1328 switch (FormatCharacter) {
1329 case 'p':
1330 //
1331 // Flag space, +, 0, L & l are invalid for type p.
1332 //
1333 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);
1334 if (sizeof (VOID *) > 4) {
1335 Flags |= LONG_TYPE;
1336 }
1337 case 'X':
1338 Flags |= PREFIX_ZERO;
1339 //
1340 // break skipped on purpose
1341 //
1342 case 'x':
1343 Flags |= RADIX_HEX;
1344 //
1345 // break skipped on purpose
1346 //
1347 case 'd':
1348 if ((Flags & LONG_TYPE) == 0) {
1349 //
1350 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1351 // This assumption is made so the format string definition is compatible with the ANSI C
1352 // Specification for formatted strings. It is recommended that the Base Types be used
1353 // everywhere, but in this one case, compliance with ANSI C is more important, and
1354 // provides an implementation that is compatible with that largest possible set of CPU
1355 // architectures. This is why the type "int" is used in this one case.
1356 //
1357 if (BaseListMarker == NULL) {
1358 Value = VA_ARG (VaListMarker, int);
1359 } else {
1360 Value = BASE_ARG (BaseListMarker, int);
1361 }
1362 } else {
1363 if (BaseListMarker == NULL) {
1364 Value = VA_ARG (VaListMarker, INT64);
1365 } else {
1366 Value = BASE_ARG (BaseListMarker, INT64);
1367 }
1368 }
1369 if ((Flags & PREFIX_BLANK) != 0) {
1370 Prefix = ' ';
1371 }
1372 if ((Flags & PREFIX_SIGN) != 0) {
1373 Prefix = '+';
1374 }
1375 if ((Flags & COMMA_TYPE) != 0) {
1376 Comma = TRUE;
1377 }
1378 if ((Flags & RADIX_HEX) == 0) {
1379 Radix = 10;
1380 if (Comma) {
1381 Flags &= (~PREFIX_ZERO);
1382 Precision = 1;
1383 }
1384 if (Value < 0) {
1385 Flags |= PREFIX_SIGN;
1386 Prefix = '-';
1387 Value = -Value;
1388 }
1389 } else {
1390 Radix = 16;
1391 Comma = FALSE;
1392 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
1393 //
1394 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1395 // This assumption is made so the format string definition is compatible with the ANSI C
1396 // Specification for formatted strings. It is recommended that the Base Types be used
1397 // everywhere, but in this one case, compliance with ANSI C is more important, and
1398 // provides an implementation that is compatible with that largest possible set of CPU
1399 // architectures. This is why the type "unsigned int" is used in this one case.
1400 //
1401 Value = (unsigned int)Value;
1402 }
1403 }
1404 //
1405 // Convert Value to a reversed string
1406 //
1407 Count = InternalPrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
1408 if (Value == 0 && Precision == 0) {
1409 Count = 0;
1410 }
1411 ArgumentString = (CHAR8 *)ValueBuffer + Count;
1412
1413 Digits = Count % 3;
1414 if (Digits != 0) {
1415 Digits = 3 - Digits;
1416 }
1417 if (Comma && Count != 0) {
1418 Count += ((Count - 1) / 3);
1419 }
1420 if (Prefix != 0) {
1421 Count++;
1422 Precision++;
1423 }
1424 Flags |= ARGUMENT_REVERSED;
1425 ZeroPad = TRUE;
1426 if ((Flags & PREFIX_ZERO) != 0) {
1427 if ((Flags & LEFT_JUSTIFY) == 0) {
1428 if ((Flags & PAD_TO_WIDTH) != 0) {
1429 if ((Flags & PRECISION) == 0) {
1430 Precision = Width;
1431 }
1432 }
1433 }
1434 }
1435 break;
1436
1437 case 's':
1438 case 'S':
1439 Flags |= ARGUMENT_UNICODE;
1440 //
1441 // break skipped on purpose
1442 //
1443 case 'a':
1444 if (BaseListMarker == NULL) {
1445 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
1446 } else {
1447 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
1448 }
1449 if (ArgumentString == NULL) {
1450 Flags &= (~ARGUMENT_UNICODE);
1451 ArgumentString = "<null string>";
1452 }
1453 //
1454 // Set the default precision for string to be zero if not specified.
1455 //
1456 if ((Flags & PRECISION) == 0) {
1457 Precision = 0;
1458 }
1459 break;
1460
1461 case 'c':
1462 if (BaseListMarker == NULL) {
1463 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
1464 } else {
1465 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
1466 }
1467 ArgumentString = (CHAR8 *)&Character;
1468 Flags |= ARGUMENT_UNICODE;
1469 break;
1470
1471 case 'g':
1472 if (BaseListMarker == NULL) {
1473 TmpGuid = VA_ARG (VaListMarker, GUID *);
1474 } else {
1475 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
1476 }
1477 if (TmpGuid == NULL) {
1478 ArgumentString = "<null guid>";
1479 } else {
1480 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
1481 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
1482 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
1483 InternalPrintLibSPrint (
1484 ValueBuffer,
1485 MAXIMUM_VALUE_CHARACTERS,
1486 0,
1487 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1488 GuidData1,
1489 GuidData2,
1490 GuidData3,
1491 TmpGuid->Data4[0],
1492 TmpGuid->Data4[1],
1493 TmpGuid->Data4[2],
1494 TmpGuid->Data4[3],
1495 TmpGuid->Data4[4],
1496 TmpGuid->Data4[5],
1497 TmpGuid->Data4[6],
1498 TmpGuid->Data4[7]
1499 );
1500 ArgumentString = ValueBuffer;
1501 }
1502 break;
1503
1504 case 't':
1505 if (BaseListMarker == NULL) {
1506 TmpTime = VA_ARG (VaListMarker, TIME *);
1507 } else {
1508 TmpTime = BASE_ARG (BaseListMarker, TIME *);
1509 }
1510 if (TmpTime == NULL) {
1511 ArgumentString = "<null time>";
1512 } else {
1513 InternalPrintLibSPrint (
1514 ValueBuffer,
1515 MAXIMUM_VALUE_CHARACTERS,
1516 0,
1517 "%02d/%02d/%04d %02d:%02d",
1518 TmpTime->Month,
1519 TmpTime->Day,
1520 TmpTime->Year,
1521 TmpTime->Hour,
1522 TmpTime->Minute
1523 );
1524 ArgumentString = ValueBuffer;
1525 }
1526 break;
1527
1528 case 'r':
1529 if (BaseListMarker == NULL) {
1530 Status = VA_ARG (VaListMarker, RETURN_STATUS);
1531 } else {
1532 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
1533 }
1534 ArgumentString = ValueBuffer;
1535 if (RETURN_ERROR (Status)) {
1536 //
1537 // Clear error bit
1538 //
1539 Index = Status & ~MAX_BIT;
1540 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
1541 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
1542 }
1543 } else {
1544 Index = Status;
1545 if (Index <= WARNING_STATUS_NUMBER) {
1546 ArgumentString = mStatusString [Index];
1547 }
1548 }
1549 if (ArgumentString == ValueBuffer) {
1550 InternalPrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
1551 }
1552 break;
1553
1554 case '\r':
1555 Format += BytesPerFormatCharacter;
1556 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1557 if (FormatCharacter == '\n') {
1558 //
1559 // Translate '\r\n' to '\r\n'
1560 //
1561 ArgumentString = "\r\n";
1562 } else {
1563 //
1564 // Translate '\r' to '\r'
1565 //
1566 ArgumentString = "\r";
1567 Format -= BytesPerFormatCharacter;
1568 }
1569 break;
1570
1571 case '\n':
1572 //
1573 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1574 //
1575 ArgumentString = "\r\n";
1576 Format += BytesPerFormatCharacter;
1577 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1578 if (FormatCharacter != '\r') {
1579 Format -= BytesPerFormatCharacter;
1580 }
1581 break;
1582
1583 case '%':
1584 default:
1585 //
1586 // if the type is '%' or unknown, then print it to the screen
1587 //
1588 ArgumentString = (CHAR8 *)&FormatCharacter;
1589 Flags |= ARGUMENT_UNICODE;
1590 break;
1591 }
1592 break;
1593
1594 case '\r':
1595 Format += BytesPerFormatCharacter;
1596 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1597 if (FormatCharacter == '\n') {
1598 //
1599 // Translate '\r\n' to '\r\n'
1600 //
1601 ArgumentString = "\r\n";
1602 } else {
1603 //
1604 // Translate '\r' to '\r'
1605 //
1606 ArgumentString = "\r";
1607 Format -= BytesPerFormatCharacter;
1608 }
1609 break;
1610
1611 case '\n':
1612 //
1613 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1614 //
1615 ArgumentString = "\r\n";
1616 Format += BytesPerFormatCharacter;
1617 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1618 if (FormatCharacter != '\r') {
1619 Format -= BytesPerFormatCharacter;
1620 }
1621 break;
1622
1623 default:
1624 ArgumentString = (CHAR8 *)&FormatCharacter;
1625 Flags |= ARGUMENT_UNICODE;
1626 break;
1627 }
1628
1629 //
1630 // Retrieve the ArgumentString attriubutes
1631 //
1632 if ((Flags & ARGUMENT_UNICODE) != 0) {
1633 ArgumentMask = 0xffff;
1634 BytesPerArgumentCharacter = 2;
1635 } else {
1636 ArgumentMask = 0xff;
1637 BytesPerArgumentCharacter = 1;
1638 }
1639 if ((Flags & ARGUMENT_REVERSED) != 0) {
1640 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
1641 } else {
1642 //
1643 // Compute the number of characters in ArgumentString and store it in Count
1644 // ArgumentString is either null-terminated, or it contains Precision characters
1645 //
1646 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
1647 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
1648 if (ArgumentCharacter == 0) {
1649 break;
1650 }
1651 }
1652 }
1653
1654 if (Precision < Count) {
1655 Precision = Count;
1656 }
1657
1658 //
1659 // Pad before the string
1660 //
1661 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
1662 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1663 }
1664
1665 if (ZeroPad) {
1666 if (Prefix != 0) {
1667 LengthToReturn += (1 * BytesPerOutputCharacter);
1668 }
1669 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1670 } else {
1671 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1672 if (Prefix != 0) {
1673 LengthToReturn += (1 * BytesPerOutputCharacter);
1674 }
1675 }
1676
1677 //
1678 // Output the Prefix character if it is present
1679 //
1680 Index = 0;
1681 if (Prefix != 0) {
1682 Index++;
1683 }
1684
1685 //
1686 // Copy the string into the output buffer performing the required type conversions
1687 //
1688 while (Index < Count) {
1689 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
1690
1691 LengthToReturn += (1 * BytesPerOutputCharacter);
1692 ArgumentString += BytesPerArgumentCharacter;
1693 Index++;
1694 if (Comma) {
1695 Digits++;
1696 if (Digits == 3) {
1697 Digits = 0;
1698 Index++;
1699 if (Index < Count) {
1700 LengthToReturn += (1 * BytesPerOutputCharacter);
1701 }
1702 }
1703 }
1704 }
1705
1706 //
1707 // Pad after the string
1708 //
1709 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
1710 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1711 }
1712
1713 //
1714 // Get the next character from the format string
1715 //
1716 Format += BytesPerFormatCharacter;
1717
1718 //
1719 // Get the next character from the format string
1720 //
1721 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1722 }
1723
1724 return (LengthToReturn / BytesPerOutputCharacter);
1725 }
1726
1727 /**
1728 Returns the number of characters that would be produced by if the formatted
1729 output were produced not including the Null-terminator.
1730
1731 If FormatString is NULL, then ASSERT().
1732 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
1733
1734 @param[in] FormatString A Null-terminated Unicode format string.
1735 @param[in] Marker VA_LIST marker for the variable argument list.
1736
1737 @return The number of characters that would be produced, not including the
1738 Null-terminator.
1739 **/
1740 UINTN
1741 EFIAPI
1742 SPrintLength (
1743 IN CONST CHAR16 *FormatString,
1744 IN VA_LIST Marker
1745 )
1746 {
1747 ASSERT(FormatString != NULL);
1748 return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
1749 }
1750
1751 /**
1752 Returns the number of characters that would be produced by if the formatted
1753 output were produced not including the Null-terminator.
1754
1755 If FormatString is NULL, then ASSERT().
1756
1757 @param[in] FormatString A Null-terminated ASCII format string.
1758 @param[in] Marker VA_LIST marker for the variable argument list.
1759
1760 @return The number of characters that would be produced, not including the
1761 Null-terminator.
1762 **/
1763 UINTN
1764 EFIAPI
1765 SPrintLengthAsciiFormat (
1766 IN CONST CHAR8 *FormatString,
1767 IN VA_LIST Marker
1768 )
1769 {
1770 ASSERT(FormatString != NULL);
1771 return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
1772 }