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