]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
3b4d4e03adb26c5c105d5158229dd8f74daab088
[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 *EndBuffer;
1138 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
1139 UINT32 BytesPerOutputCharacter;
1140 UINTN BytesPerFormatCharacter;
1141 UINTN FormatMask;
1142 UINTN FormatCharacter;
1143 UINTN Width;
1144 UINTN Precision;
1145 INT64 Value;
1146 CONST CHAR8 *ArgumentString;
1147 UINTN Character;
1148 GUID *TmpGuid;
1149 TIME *TmpTime;
1150 UINTN Count;
1151 UINTN ArgumentMask;
1152 INTN BytesPerArgumentCharacter;
1153 UINTN ArgumentCharacter;
1154 BOOLEAN Done;
1155 UINTN Index;
1156 CHAR8 Prefix;
1157 BOOLEAN ZeroPad;
1158 BOOLEAN Comma;
1159 UINTN Digits;
1160 UINTN Radix;
1161 RETURN_STATUS Status;
1162 UINT32 GuidData1;
1163 UINT16 GuidData2;
1164 UINT16 GuidData3;
1165 UINTN LengthToReturn;
1166
1167 //
1168 // If you change this code be sure to match the 2 versions of this function.
1169 // Nearly identical logic is found in the BasePrintLib and
1170 // DxePrintLibPrint2Protocol (both PrintLib instances).
1171 //
1172
1173 ASSERT(Flags & COUNT_ONLY_NO_PRINT);
1174
1175 if ((Flags & OUTPUT_UNICODE) != 0) {
1176 BytesPerOutputCharacter = 2;
1177 } else {
1178 BytesPerOutputCharacter = 1;
1179 }
1180
1181 LengthToReturn = 0;
1182
1183 //
1184 // Reserve space for the Null terminator.
1185 //
1186 BufferSize--;
1187
1188 //
1189 // Set the tag for the end of the input Buffer.
1190 //
1191 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
1192
1193 if ((Flags & FORMAT_UNICODE) != 0) {
1194 //
1195 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
1196 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
1197 //
1198 ASSERT (StrSize ((CHAR16 *) Format) != 0);
1199 BytesPerFormatCharacter = 2;
1200 FormatMask = 0xffff;
1201 } else {
1202 //
1203 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength
1204 // Ascii characters if PcdMaximumAsciiStringLength is not zero.
1205 //
1206 ASSERT (AsciiStrSize (Format) != 0);
1207 BytesPerFormatCharacter = 1;
1208 FormatMask = 0xff;
1209 }
1210
1211 //
1212 // Get the first character from the format string
1213 //
1214 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1215
1216 //
1217 // Loop until the end of the format string is reached or the output buffer is full
1218 //
1219 while (FormatCharacter != 0 && Buffer < EndBuffer) {
1220 //
1221 // Clear all the flag bits except those that may have been passed in
1222 //
1223 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
1224
1225 //
1226 // Set the default width to zero, and the default precision to 1
1227 //
1228 Width = 0;
1229 Precision = 1;
1230 Prefix = 0;
1231 Comma = FALSE;
1232 ZeroPad = FALSE;
1233 Count = 0;
1234 Digits = 0;
1235
1236 switch (FormatCharacter) {
1237 case '%':
1238 //
1239 // Parse Flags and Width
1240 //
1241 for (Done = FALSE; !Done; ) {
1242 Format += BytesPerFormatCharacter;
1243 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1244 switch (FormatCharacter) {
1245 case '.':
1246 Flags |= PRECISION;
1247 break;
1248 case '-':
1249 Flags |= LEFT_JUSTIFY;
1250 break;
1251 case '+':
1252 Flags |= PREFIX_SIGN;
1253 break;
1254 case ' ':
1255 Flags |= PREFIX_BLANK;
1256 break;
1257 case ',':
1258 Flags |= COMMA_TYPE;
1259 break;
1260 case 'L':
1261 case 'l':
1262 Flags |= LONG_TYPE;
1263 break;
1264 case '*':
1265 if ((Flags & PRECISION) == 0) {
1266 Flags |= PAD_TO_WIDTH;
1267 if (BaseListMarker == NULL) {
1268 Width = VA_ARG (VaListMarker, UINTN);
1269 } else {
1270 Width = BASE_ARG (BaseListMarker, UINTN);
1271 }
1272 } else {
1273 if (BaseListMarker == NULL) {
1274 Precision = VA_ARG (VaListMarker, UINTN);
1275 } else {
1276 Precision = BASE_ARG (BaseListMarker, UINTN);
1277 }
1278 }
1279 break;
1280 case '0':
1281 if ((Flags & PRECISION) == 0) {
1282 Flags |= PREFIX_ZERO;
1283 }
1284 case '1':
1285 case '2':
1286 case '3':
1287 case '4':
1288 case '5':
1289 case '6':
1290 case '7':
1291 case '8':
1292 case '9':
1293 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
1294 Count = (Count * 10) + FormatCharacter - '0';
1295 Format += BytesPerFormatCharacter;
1296 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1297 }
1298 Format -= BytesPerFormatCharacter;
1299 if ((Flags & PRECISION) == 0) {
1300 Flags |= PAD_TO_WIDTH;
1301 Width = Count;
1302 } else {
1303 Precision = Count;
1304 }
1305 break;
1306
1307 case '\0':
1308 //
1309 // Make no output if Format string terminates unexpectedly when
1310 // looking up for flag, width, precision and type.
1311 //
1312 Format -= BytesPerFormatCharacter;
1313 Precision = 0;
1314 //
1315 // break skipped on purpose.
1316 //
1317 default:
1318 Done = TRUE;
1319 break;
1320 }
1321 }
1322
1323 //
1324 // Handle each argument type
1325 //
1326 switch (FormatCharacter) {
1327 case 'p':
1328 //
1329 // Flag space, +, 0, L & l are invalid for type p.
1330 //
1331 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);
1332 if (sizeof (VOID *) > 4) {
1333 Flags |= LONG_TYPE;
1334 }
1335 case 'X':
1336 Flags |= PREFIX_ZERO;
1337 //
1338 // break skipped on purpose
1339 //
1340 case 'x':
1341 Flags |= RADIX_HEX;
1342 //
1343 // break skipped on purpose
1344 //
1345 case 'd':
1346 if ((Flags & LONG_TYPE) == 0) {
1347 //
1348 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1349 // This assumption is made so the format string definition is compatible with the ANSI C
1350 // Specification for formatted strings. It is recommended that the Base Types be used
1351 // everywhere, but in this one case, compliance with ANSI C is more important, and
1352 // provides an implementation that is compatible with that largest possible set of CPU
1353 // architectures. This is why the type "int" is used in this one case.
1354 //
1355 if (BaseListMarker == NULL) {
1356 Value = VA_ARG (VaListMarker, int);
1357 } else {
1358 Value = BASE_ARG (BaseListMarker, int);
1359 }
1360 } else {
1361 if (BaseListMarker == NULL) {
1362 Value = VA_ARG (VaListMarker, INT64);
1363 } else {
1364 Value = BASE_ARG (BaseListMarker, INT64);
1365 }
1366 }
1367 if ((Flags & PREFIX_BLANK) != 0) {
1368 Prefix = ' ';
1369 }
1370 if ((Flags & PREFIX_SIGN) != 0) {
1371 Prefix = '+';
1372 }
1373 if ((Flags & COMMA_TYPE) != 0) {
1374 Comma = TRUE;
1375 }
1376 if ((Flags & RADIX_HEX) == 0) {
1377 Radix = 10;
1378 if (Comma) {
1379 Flags &= (~PREFIX_ZERO);
1380 Precision = 1;
1381 }
1382 if (Value < 0) {
1383 Flags |= PREFIX_SIGN;
1384 Prefix = '-';
1385 Value = -Value;
1386 }
1387 } else {
1388 Radix = 16;
1389 Comma = FALSE;
1390 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
1391 //
1392 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1393 // This assumption is made so the format string definition is compatible with the ANSI C
1394 // Specification for formatted strings. It is recommended that the Base Types be used
1395 // everywhere, but in this one case, compliance with ANSI C is more important, and
1396 // provides an implementation that is compatible with that largest possible set of CPU
1397 // architectures. This is why the type "unsigned int" is used in this one case.
1398 //
1399 Value = (unsigned int)Value;
1400 }
1401 }
1402 //
1403 // Convert Value to a reversed string
1404 //
1405 Count = InternalPrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
1406 if (Value == 0 && Precision == 0) {
1407 Count = 0;
1408 }
1409 ArgumentString = (CHAR8 *)ValueBuffer + Count;
1410
1411 Digits = Count % 3;
1412 if (Digits != 0) {
1413 Digits = 3 - Digits;
1414 }
1415 if (Comma && Count != 0) {
1416 Count += ((Count - 1) / 3);
1417 }
1418 if (Prefix != 0) {
1419 Count++;
1420 Precision++;
1421 }
1422 Flags |= ARGUMENT_REVERSED;
1423 ZeroPad = TRUE;
1424 if ((Flags & PREFIX_ZERO) != 0) {
1425 if ((Flags & LEFT_JUSTIFY) == 0) {
1426 if ((Flags & PAD_TO_WIDTH) != 0) {
1427 if ((Flags & PRECISION) == 0) {
1428 Precision = Width;
1429 }
1430 }
1431 }
1432 }
1433 break;
1434
1435 case 's':
1436 case 'S':
1437 Flags |= ARGUMENT_UNICODE;
1438 //
1439 // break skipped on purpose
1440 //
1441 case 'a':
1442 if (BaseListMarker == NULL) {
1443 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
1444 } else {
1445 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
1446 }
1447 if (ArgumentString == NULL) {
1448 Flags &= (~ARGUMENT_UNICODE);
1449 ArgumentString = "<null string>";
1450 }
1451 //
1452 // Set the default precision for string to be zero if not specified.
1453 //
1454 if ((Flags & PRECISION) == 0) {
1455 Precision = 0;
1456 }
1457 break;
1458
1459 case 'c':
1460 if (BaseListMarker == NULL) {
1461 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
1462 } else {
1463 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
1464 }
1465 ArgumentString = (CHAR8 *)&Character;
1466 Flags |= ARGUMENT_UNICODE;
1467 break;
1468
1469 case 'g':
1470 if (BaseListMarker == NULL) {
1471 TmpGuid = VA_ARG (VaListMarker, GUID *);
1472 } else {
1473 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
1474 }
1475 if (TmpGuid == NULL) {
1476 ArgumentString = "<null guid>";
1477 } else {
1478 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
1479 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
1480 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
1481 InternalPrintLibSPrint (
1482 ValueBuffer,
1483 MAXIMUM_VALUE_CHARACTERS,
1484 0,
1485 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1486 GuidData1,
1487 GuidData2,
1488 GuidData3,
1489 TmpGuid->Data4[0],
1490 TmpGuid->Data4[1],
1491 TmpGuid->Data4[2],
1492 TmpGuid->Data4[3],
1493 TmpGuid->Data4[4],
1494 TmpGuid->Data4[5],
1495 TmpGuid->Data4[6],
1496 TmpGuid->Data4[7]
1497 );
1498 ArgumentString = ValueBuffer;
1499 }
1500 break;
1501
1502 case 't':
1503 if (BaseListMarker == NULL) {
1504 TmpTime = VA_ARG (VaListMarker, TIME *);
1505 } else {
1506 TmpTime = BASE_ARG (BaseListMarker, TIME *);
1507 }
1508 if (TmpTime == NULL) {
1509 ArgumentString = "<null time>";
1510 } else {
1511 InternalPrintLibSPrint (
1512 ValueBuffer,
1513 MAXIMUM_VALUE_CHARACTERS,
1514 0,
1515 "%02d/%02d/%04d %02d:%02d",
1516 TmpTime->Month,
1517 TmpTime->Day,
1518 TmpTime->Year,
1519 TmpTime->Hour,
1520 TmpTime->Minute
1521 );
1522 ArgumentString = ValueBuffer;
1523 }
1524 break;
1525
1526 case 'r':
1527 if (BaseListMarker == NULL) {
1528 Status = VA_ARG (VaListMarker, RETURN_STATUS);
1529 } else {
1530 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
1531 }
1532 ArgumentString = ValueBuffer;
1533 if (RETURN_ERROR (Status)) {
1534 //
1535 // Clear error bit
1536 //
1537 Index = Status & ~MAX_BIT;
1538 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
1539 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
1540 }
1541 } else {
1542 Index = Status;
1543 if (Index <= WARNING_STATUS_NUMBER) {
1544 ArgumentString = mStatusString [Index];
1545 }
1546 }
1547 if (ArgumentString == ValueBuffer) {
1548 InternalPrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
1549 }
1550 break;
1551
1552 case '\r':
1553 Format += BytesPerFormatCharacter;
1554 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1555 if (FormatCharacter == '\n') {
1556 //
1557 // Translate '\r\n' to '\r\n'
1558 //
1559 ArgumentString = "\r\n";
1560 } else {
1561 //
1562 // Translate '\r' to '\r'
1563 //
1564 ArgumentString = "\r";
1565 Format -= BytesPerFormatCharacter;
1566 }
1567 break;
1568
1569 case '\n':
1570 //
1571 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1572 //
1573 ArgumentString = "\r\n";
1574 Format += BytesPerFormatCharacter;
1575 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1576 if (FormatCharacter != '\r') {
1577 Format -= BytesPerFormatCharacter;
1578 }
1579 break;
1580
1581 case '%':
1582 default:
1583 //
1584 // if the type is '%' or unknown, then print it to the screen
1585 //
1586 ArgumentString = (CHAR8 *)&FormatCharacter;
1587 Flags |= ARGUMENT_UNICODE;
1588 break;
1589 }
1590 break;
1591
1592 case '\r':
1593 Format += BytesPerFormatCharacter;
1594 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1595 if (FormatCharacter == '\n') {
1596 //
1597 // Translate '\r\n' to '\r\n'
1598 //
1599 ArgumentString = "\r\n";
1600 } else {
1601 //
1602 // Translate '\r' to '\r'
1603 //
1604 ArgumentString = "\r";
1605 Format -= BytesPerFormatCharacter;
1606 }
1607 break;
1608
1609 case '\n':
1610 //
1611 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1612 //
1613 ArgumentString = "\r\n";
1614 Format += BytesPerFormatCharacter;
1615 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1616 if (FormatCharacter != '\r') {
1617 Format -= BytesPerFormatCharacter;
1618 }
1619 break;
1620
1621 default:
1622 ArgumentString = (CHAR8 *)&FormatCharacter;
1623 Flags |= ARGUMENT_UNICODE;
1624 break;
1625 }
1626
1627 //
1628 // Retrieve the ArgumentString attriubutes
1629 //
1630 if ((Flags & ARGUMENT_UNICODE) != 0) {
1631 ArgumentMask = 0xffff;
1632 BytesPerArgumentCharacter = 2;
1633 } else {
1634 ArgumentMask = 0xff;
1635 BytesPerArgumentCharacter = 1;
1636 }
1637 if ((Flags & ARGUMENT_REVERSED) != 0) {
1638 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
1639 } else {
1640 //
1641 // Compute the number of characters in ArgumentString and store it in Count
1642 // ArgumentString is either null-terminated, or it contains Precision characters
1643 //
1644 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
1645 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
1646 if (ArgumentCharacter == 0) {
1647 break;
1648 }
1649 }
1650 }
1651
1652 if (Precision < Count) {
1653 Precision = Count;
1654 }
1655
1656 //
1657 // Pad before the string
1658 //
1659 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
1660 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1661 }
1662
1663 if (ZeroPad) {
1664 if (Prefix != 0) {
1665 LengthToReturn += (1 * BytesPerOutputCharacter);
1666 }
1667 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1668 } else {
1669 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1670 if (Prefix != 0) {
1671 LengthToReturn += (1 * BytesPerOutputCharacter);
1672 }
1673 }
1674
1675 //
1676 // Output the Prefix character if it is present
1677 //
1678 Index = 0;
1679 if (Prefix != 0) {
1680 Index++;
1681 }
1682
1683 //
1684 // Copy the string into the output buffer performing the required type conversions
1685 //
1686 while (Index < Count) {
1687 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
1688
1689 LengthToReturn += (1 * BytesPerOutputCharacter);
1690 ArgumentString += BytesPerArgumentCharacter;
1691 Index++;
1692 if (Comma) {
1693 Digits++;
1694 if (Digits == 3) {
1695 Digits = 0;
1696 Index++;
1697 if (Index < Count) {
1698 LengthToReturn += (1 * BytesPerOutputCharacter);
1699 }
1700 }
1701 }
1702 }
1703
1704 //
1705 // Pad after the string
1706 //
1707 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
1708 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1709 }
1710
1711 //
1712 // Get the next character from the format string
1713 //
1714 Format += BytesPerFormatCharacter;
1715
1716 //
1717 // Get the next character from the format string
1718 //
1719 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1720 }
1721
1722 return (LengthToReturn / BytesPerOutputCharacter);
1723 }
1724
1725 /**
1726 Returns the number of characters that would be produced by if the formatted
1727 output were produced not including the Null-terminator.
1728
1729 If FormatString is NULL, then ASSERT().
1730 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
1731
1732 @param[in] FormatString A Null-terminated Unicode format string.
1733 @param[in] Marker VA_LIST marker for the variable argument list.
1734
1735 @return The number of characters that would be produced, not including the
1736 Null-terminator.
1737 **/
1738 UINTN
1739 EFIAPI
1740 SPrintLength (
1741 IN CONST CHAR16 *FormatString,
1742 IN VA_LIST Marker
1743 )
1744 {
1745 ASSERT(FormatString != NULL);
1746 return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
1747 }
1748
1749 /**
1750 Returns the number of characters that would be produced by if the formatted
1751 output were produced not including the Null-terminator.
1752
1753 If FormatString is NULL, then ASSERT().
1754
1755 @param[in] FormatString A Null-terminated ASCII format string.
1756 @param[in] Marker VA_LIST marker for the variable argument list.
1757
1758 @return The number of characters that would be produced, not including the
1759 Null-terminator.
1760 **/
1761 UINTN
1762 EFIAPI
1763 SPrintLengthAsciiFormat (
1764 IN CONST CHAR8 *FormatString,
1765 IN VA_LIST Marker
1766 )
1767 {
1768 ASSERT(FormatString != NULL);
1769 return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
1770 }