Debian Codegraph @

gnu-efi/lib/print.c

raw
1 /*++
2
3 Copyright (c) 1998 Intel Corporation
4
5 Module Name:
6
7 print.c
8
9 Abstract:
10
11
12
13
14 Revision History
15
16 --*/
17
18 #include "lib.h"
19 #include "efistdarg.h" // !!!
20
21 //
22 // Declare runtime functions
23 //
24
25 #ifdef RUNTIME_CODE
26 #ifndef __GNUC__
27 #pragma RUNTIME_CODE(DbgPrint)
28
29 // For debugging..
30
31 /*
32 #pragma RUNTIME_CODE(_Print)
33 #pragma RUNTIME_CODE(PFLUSH)
34 #pragma RUNTIME_CODE(PSETATTR)
35 #pragma RUNTIME_CODE(PPUTC)
36 #pragma RUNTIME_CODE(PGETC)
37 #pragma RUNTIME_CODE(PITEM)
38 #pragma RUNTIME_CODE(ValueToHex)
39 #pragma RUNTIME_CODE(ValueToString)
40 #pragma RUNTIME_CODE(TimeToString)
41 */
42
43 #endif /* !defined(__GNUC__) */
44 #endif
45
46 //
47 //
48 //
49
50
51 #define PRINT_STRING_LEN 200
52 #define PRINT_ITEM_BUFFER_LEN 100
53
54 typedef struct {
55 BOOLEAN Ascii;
56 UINTN Index;
57 union {
58 CONST CHAR16 *pw;
59 CONST CHAR8 *pc;
60 } un;
61 } POINTER;
62
63 #define pw un.pw
64 #define pc un.pc
65
66 typedef struct _pitem {
67
68 POINTER Item;
69 CHAR16 Scratch[PRINT_ITEM_BUFFER_LEN];
70 UINTN Width;
71 UINTN FieldWidth;
72 UINTN *WidthParse;
73 CHAR16 Pad;
74 BOOLEAN PadBefore;
75 BOOLEAN Comma;
76 BOOLEAN Long;
77 } PRINT_ITEM;
78
79
80 typedef struct _pstate {
81 // Input
82 POINTER fmt;
83 ms_va_list args;
84
85 // Output
86 CHAR16 *Buffer;
87 CHAR16 *End;
88 CHAR16 *Pos;
89 UINTN Len;
90
91 UINTN Attr;
92 UINTN RestoreAttr;
93
94 UINTN AttrNorm;
95 UINTN AttrHighlight;
96 UINTN AttrError;
97
98 INTN (EFIAPI *Output)(VOID *context, CHAR16 *str);
99 INTN (EFIAPI *SetAttr)(VOID *context, UINTN attr);
100 VOID *Context;
101
102 // Current item being formatted
103 struct _pitem *Item;
104 } PRINT_STATE;
105
106 //
107 // Internal fucntions
108 //
109
110 STATIC
111 UINTN
112 _Print (
113 IN PRINT_STATE *ps
114 );
115
116 STATIC
117 UINTN EFIAPI
118 _IPrint (
119 IN UINTN Column,
120 IN UINTN Row,
121 IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
122 IN CONST CHAR16 *fmt,
123 IN CONST CHAR8 *fmta,
124 IN ms_va_list args
125 );
126
127 STATIC
128 INTN EFIAPI
129 _DbgOut (
130 IN VOID *Context,
131 IN CHAR16 *Buffer
132 );
133
134 STATIC
135 VOID
136 PFLUSH (
137 IN OUT PRINT_STATE *ps
138 );
139
140 STATIC
141 VOID
142 PPUTC (
143 IN OUT PRINT_STATE *ps,
144 IN CHAR16 c
145 );
146
147 STATIC
148 VOID
149 PITEM (
150 IN OUT PRINT_STATE *ps
151 );
152
153 STATIC
154 CHAR16
155 PGETC (
156 IN POINTER *p
157 );
158
159 STATIC
160 VOID
161 PSETATTR (
162 IN OUT PRINT_STATE *ps,
163 IN UINTN Attr
164 );
165
166 //
167 //
168 //
169
170 INTN EFIAPI
171 _SPrint (
172 IN VOID *Context,
173 IN CHAR16 *Buffer
174 );
175
176 INTN EFIAPI
177 _PoolPrint (
178 IN VOID *Context,
179 IN CHAR16 *Buffer
180 );
181
182 INTN EFIAPI
183 DbgPrint (
184 IN INTN mask,
185 IN CONST CHAR8 *fmt,
186 ...
187 )
188 /*++
189
190 Routine Description:
191
192 Prints a formatted unicode string to the default StandardError console
193
194 Arguments:
195
196 mask - Bit mask of debug string. If a bit is set in the
197 mask that is also set in EFIDebug the string is
198 printed; otherwise, the string is not printed
199
200 fmt - Format string
201
202 Returns:
203
204 Length of string printed to the StandardError console
205
206 --*/
207 {
208 SIMPLE_TEXT_OUTPUT_INTERFACE *DbgOut;
209 PRINT_STATE ps;
210 ms_va_list args;
211 UINTN back;
212 UINTN attr;
213 UINTN SavedAttribute;
214
215
216 if (!(EFIDebug & mask)) {
217 return 0;
218 }
219
220 ms_va_start (args, fmt);
221 ZeroMem (&ps, sizeof(ps));
222
223 ps.Output = _DbgOut;
224 ps.fmt.Ascii = TRUE;
225 ps.fmt.pc = fmt;
226 ms_va_copy(ps.args, args);
227 ps.Attr = EFI_TEXT_ATTR(EFI_LIGHTGRAY, EFI_RED);
228
229 DbgOut = LibRuntimeDebugOut;
230
231 if (!DbgOut) {
232 DbgOut = ST->StdErr;
233 }
234
235 if (DbgOut) {
236 ps.Attr = DbgOut->Mode->Attribute;
237 ps.Context = DbgOut;
238 ps.SetAttr = (INTN (EFIAPI *)(VOID *, UINTN)) DbgOut->SetAttribute;
239 }
240
241 SavedAttribute = ps.Attr;
242
243 back = (ps.Attr >> 4) & 0xf;
244 ps.AttrNorm = EFI_TEXT_ATTR(EFI_LIGHTGRAY, back);
245 ps.AttrHighlight = EFI_TEXT_ATTR(EFI_WHITE, back);
246 ps.AttrError = EFI_TEXT_ATTR(EFI_YELLOW, back);
247
248 attr = ps.AttrNorm;
249
250 if (mask & D_WARN) {
251 attr = ps.AttrHighlight;
252 }
253
254 if (mask & D_ERROR) {
255 attr = ps.AttrError;
256 }
257
258 if (ps.SetAttr) {
259 ps.Attr = attr;
260 uefi_call_wrapper(ps.SetAttr, 2, ps.Context, attr);
261 }
262
263 _Print (&ps);
264
265 ms_va_end (ps.args);
266 ms_va_end (args);
267
268 //
269 // Restore original attributes
270 //
271
272 if (ps.SetAttr) {
273 uefi_call_wrapper(ps.SetAttr, 2, ps.Context, SavedAttribute);
274 }
275
276 return 0;
277 }
278
279 STATIC
280 INTN
281 IsLocalPrint(void *func)
282 {
283 if (func == _DbgOut || func == _SPrint || func == _PoolPrint)
284 return 1;
285 return 0;
286 }
287
288 STATIC
289 INTN EFIAPI
290 _DbgOut (
291 IN VOID *Context,
292 IN CHAR16 *Buffer
293 )
294 // Append string worker for DbgPrint
295 {
296 SIMPLE_TEXT_OUTPUT_INTERFACE *DbgOut;
297
298 DbgOut = Context;
299 // if (!DbgOut && ST && ST->ConOut) {
300 // DbgOut = ST->ConOut;
301 // }
302
303 if (DbgOut) {
304 if (IsLocalPrint(DbgOut->OutputString))
305 DbgOut->OutputString(DbgOut, Buffer);
306 else
307 uefi_call_wrapper(DbgOut->OutputString, 2, DbgOut, Buffer);
308 }
309
310 return 0;
311 }
312
313 INTN EFIAPI
314 _SPrint (
315 IN VOID *Context,
316 IN CHAR16 *Buffer
317 )
318 // Append string worker for SPrint, PoolPrint and CatPrint
319 {
320 UINTN len;
321 POOL_PRINT *spc;
322
323 spc = Context;
324 len = StrLen(Buffer);
325
326 //
327 // Is the string is over the max truncate it
328 //
329
330 if (spc->len + len > spc->maxlen) {
331 len = spc->maxlen - spc->len;
332 }
333
334 //
335 // Append the new text
336 //
337
338 CopyMem (spc->str + spc->len, Buffer, len * sizeof(CHAR16));
339 spc->len += len;
340
341 //
342 // Null terminate it
343 //
344
345 if (spc->len < spc->maxlen) {
346 spc->str[spc->len] = 0;
347 } else if (spc->maxlen) {
348 spc->str[spc->maxlen] = 0;
349 }
350
351 return 0;
352 }
353
354
355 INTN EFIAPI
356 _PoolPrint (
357 IN VOID *Context,
358 IN CHAR16 *Buffer
359 )
360 // Append string worker for PoolPrint and CatPrint
361 {
362 UINTN newlen;
363 POOL_PRINT *spc;
364
365 spc = Context;
366 newlen = spc->len + StrLen(Buffer) + 1;
367
368 //
369 // Is the string is over the max, grow the buffer
370 //
371
372 if (newlen > spc->maxlen) {
373
374 //
375 // Grow the pool buffer
376 //
377
378 newlen += PRINT_STRING_LEN;
379 spc->maxlen = newlen;
380 spc->str = ReallocatePool (
381 spc->str,
382 spc->len * sizeof(CHAR16),
383 spc->maxlen * sizeof(CHAR16)
384 );
385
386 if (!spc->str) {
387 spc->len = 0;
388 spc->maxlen = 0;
389 }
390 }
391
392 //
393 // Append the new text
394 //
395
396 return _SPrint (Context, Buffer);
397 }
398
399
400
401 VOID EFIAPI
402 _PoolCatPrint (
403 IN CONST CHAR16 *fmt,
404 IN ms_va_list args,
405 IN OUT POOL_PRINT *spc,
406 IN INTN (EFIAPI *Output)(VOID *context, CHAR16 *str)
407 )
408 // Dispatch function for SPrint, PoolPrint, and CatPrint
409 {
410 PRINT_STATE ps;
411
412 ZeroMem (&ps, sizeof(ps));
413 ps.Output = Output;
414 ps.Context = spc;
415 ps.fmt.pw = fmt;
416 ms_va_copy(ps.args, args);
417 _Print (&ps);
418 ms_va_end(ps.args);
419 }
420
421
422
423 UINTN EFIAPI
424 VSPrint (
425 OUT CHAR16 *Str,
426 IN UINTN StrSize,
427 IN CONST CHAR16 *fmt,
428 ms_va_list args
429 )
430 /*++
431
432 Routine Description:
433
434 Prints a formatted unicode string to a buffer using a ms_va_list
435
436 Arguments:
437
438 Str - Output buffer to print the formatted string into
439
440 StrSize - Size of Str. String is truncated to this size.
441 A size of 0 means there is no limit
442
443 fmt - The format string
444
445 args - ms_va_list
446
447
448 Returns:
449
450 String length returned in buffer
451
452 --*/
453 {
454 POOL_PRINT spc;
455
456 spc.str = Str;
457 spc.maxlen = StrSize / sizeof(CHAR16) - 1;
458 spc.len = 0;
459
460 _PoolCatPrint (fmt, args, &spc, _SPrint);
461
462 return spc.len;
463 }
464
465 UINTN EFIAPI
466 SPrint (
467 OUT CHAR16 *Str,
468 IN UINTN StrSize,
469 IN CONST CHAR16 *fmt,
470 ...
471 )
472 /*++
473
474 Routine Description:
475
476 Prints a formatted unicode string to a buffer
477
478 Arguments:
479
480 Str - Output buffer to print the formatted string into
481
482 StrSize - Size of Str. String is truncated to this size.
483 A size of 0 means there is no limit
484
485 fmt - The format string
486
487 Returns:
488
489 String length returned in buffer
490
491 --*/
492 {
493 ms_va_list args;
494 UINTN len;
495
496 ms_va_start (args, fmt);
497 len = VSPrint(Str, StrSize, fmt, args);
498 ms_va_end (args);
499
500 return len;
501 }
502
503 CHAR16 * EFIAPI
504 VPoolPrint (
505 IN CONST CHAR16 *fmt,
506 ms_va_list args
507 )
508 /*++
509
510 Routine Description:
511
512 Prints a formatted unicode string to allocated pool using ms_va_list argument.
513 The caller must free the resulting buffer.
514
515 Arguments:
516
517 fmt - The format string
518 args - The arguments in ms_va_list form
519
520 Returns:
521
522 Allocated buffer with the formatted string printed in it.
523 The caller must free the allocated buffer. The buffer
524 allocation is not packed.
525
526 --*/
527 {
528 POOL_PRINT spc;
529 ZeroMem (&spc, sizeof(spc));
530 _PoolCatPrint (fmt, args, &spc, _PoolPrint);
531 return spc.str;
532 }
533
534 CHAR16 * EFIAPI
535 PoolPrint (
536 IN CONST CHAR16 *fmt,
537 ...
538 )
539 /*++
540
541 Routine Description:
542
543 Prints a formatted unicode string to allocated pool. The caller
544 must free the resulting buffer.
545
546 Arguments:
547
548 fmt - The format string
549
550 Returns:
551
552 Allocated buffer with the formatted string printed in it.
553 The caller must free the allocated buffer. The buffer
554 allocation is not packed.
555
556 --*/
557 {
558 ms_va_list args;
559 CHAR16 *pool;
560 ms_va_start (args, fmt);
561 pool = VPoolPrint(fmt, args);
562 ms_va_end (args);
563 return pool;
564 }
565
566 CHAR16 * EFIAPI
567 CatPrint (
568 IN OUT POOL_PRINT *Str,
569 IN CONST CHAR16 *fmt,
570 ...
571 )
572 /*++
573
574 Routine Description:
575
576 Concatenates a formatted unicode string to allocated pool.
577 The caller must free the resulting buffer.
578
579 Arguments:
580
581 Str - Tracks the allocated pool, size in use, and
582 amount of pool allocated.
583
584 fmt - The format string
585
586 Returns:
587
588 Allocated buffer with the formatted string printed in it.
589 The caller must free the allocated buffer. The buffer
590 allocation is not packed.
591
592 --*/
593 {
594 ms_va_list args;
595
596 ms_va_start (args, fmt);
597 _PoolCatPrint (fmt, args, Str, _PoolPrint);
598 ms_va_end (args);
599 return Str->str;
600 }
601
602
603
604 UINTN EFIAPI
605 Print (
606 IN CONST CHAR16 *fmt,
607 ...
608 )
609 /*++
610
611 Routine Description:
612
613 Prints a formatted unicode string to the default console
614
615 Arguments:
616
617 fmt - Format string
618
619 Returns:
620
621 Length of string printed to the console
622
623 --*/
624 {
625 ms_va_list args;
626 UINTN back;
627
628 ms_va_start (args, fmt);
629 back = _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, fmt, NULL, args);
630 ms_va_end (args);
631 return back;
632 }
633
634 UINTN EFIAPI
635 VPrint (
636 IN CONST CHAR16 *fmt,
637 ms_va_list args
638 )
639 /*++
640
641 Routine Description:
642
643 Prints a formatted unicode string to the default console using a ms_va_list
644
645 Arguments:
646
647 fmt - Format string
648 args - ms_va_list
649 Returns:
650
651 Length of string printed to the console
652
653 --*/
654 {
655 return _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, fmt, NULL, args);
656 }
657
658
659 UINTN EFIAPI
660 PrintAt (
661 IN UINTN Column,
662 IN UINTN Row,
663 IN CONST CHAR16 *fmt,
664 ...
665 )
666 /*++
667
668 Routine Description:
669
670 Prints a formatted unicode string to the default console, at
671 the supplied cursor position
672
673 Arguments:
674
675 Column, Row - The cursor position to print the string at
676
677 fmt - Format string
678
679 Returns:
680
681 Length of string printed to the console
682
683 --*/
684 {
685 ms_va_list args;
686 UINTN back;
687
688 ms_va_start (args, fmt);
689 back = _IPrint (Column, Row, ST->ConOut, fmt, NULL, args);
690 ms_va_end (args);
691 return back;
692 }
693
694
695 UINTN EFIAPI
696 IPrint (
697 IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
698 IN CONST CHAR16 *fmt,
699 ...
700 )
701 /*++
702
703 Routine Description:
704
705 Prints a formatted unicode string to the specified console
706
707 Arguments:
708
709 Out - The console to print the string too
710
711 fmt - Format string
712
713 Returns:
714
715 Length of string printed to the console
716
717 --*/
718 {
719 ms_va_list args;
720 UINTN back;
721
722 ms_va_start (args, fmt);
723 back = _IPrint ((UINTN) -1, (UINTN) -1, Out, fmt, NULL, args);
724 ms_va_end (args);
725 return back;
726 }
727
728
729 UINTN EFIAPI
730 IPrintAt (
731 IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
732 IN UINTN Column,
733 IN UINTN Row,
734 IN CONST CHAR16 *fmt,
735 ...
736 )
737 /*++
738
739 Routine Description:
740
741 Prints a formatted unicode string to the specified console, at
742 the supplied cursor position
743
744 Arguments:
745
746 Out - The console to print the string to
747
748 Column, Row - The cursor position to print the string at
749
750 fmt - Format string
751
752 Returns:
753
754 Length of string printed to the console
755
756 --*/
757 {
758 ms_va_list args;
759 UINTN back;
760
761 ms_va_start (args, fmt);
762 back = _IPrint (Column, Row, Out, fmt, NULL, args);
763 ms_va_end (args);
764 return back;
765 }
766
767
768 UINTN EFIAPI
769 _IPrint (
770 IN UINTN Column,
771 IN UINTN Row,
772 IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
773 IN CONST CHAR16 *fmt,
774 IN CONST CHAR8 *fmta,
775 IN ms_va_list args
776 )
777 // Display string worker for: Print, PrintAt, IPrint, IPrintAt
778 {
779 PRINT_STATE ps;
780 UINTN back;
781
782 ZeroMem (&ps, sizeof(ps));
783 ps.Context = Out;
784 ps.Output = (INTN (EFIAPI *)(VOID *, CHAR16 *)) Out->OutputString;
785 ps.SetAttr = (INTN (EFIAPI *)(VOID *, UINTN)) Out->SetAttribute;
786 ps.Attr = Out->Mode->Attribute;
787
788 back = (ps.Attr >> 4) & 0xF;
789 ps.AttrNorm = EFI_TEXT_ATTR(EFI_LIGHTGRAY, back);
790 ps.AttrHighlight = EFI_TEXT_ATTR(EFI_WHITE, back);
791 ps.AttrError = EFI_TEXT_ATTR(EFI_YELLOW, back);
792
793 if (fmt) {
794 ps.fmt.pw = fmt;
795 } else {
796 ps.fmt.Ascii = TRUE;
797 ps.fmt.pc = fmta;
798 }
799
800 ms_va_copy(ps.args, args);
801
802 if (Column != (UINTN) -1) {
803 uefi_call_wrapper(Out->SetCursorPosition, 3, Out, Column, Row);
804 }
805
806 back = _Print (&ps);
807 ms_va_end(ps.args);
808 return back;
809 }
810
811
812 UINTN EFIAPI
813 AsciiPrint (
814 IN CONST CHAR8 *fmt,
815 ...
816 )
817 /*++
818
819 Routine Description:
820
821 For those whom really can't deal with unicode, a print
822 function that takes an ascii format string
823
824 Arguments:
825
826 fmt - ascii format string
827
828 Returns:
829
830 Length of string printed to the console
831
832 --*/
833
834 {
835 ms_va_list args;
836 UINTN back;
837
838 ms_va_start (args, fmt);
839 back = _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, NULL, fmt, args);
840 ms_va_end (args);
841 return back;
842 }
843
844
845 UINTN EFIAPI
846 AsciiVSPrint (
847 OUT CHAR8 *Str,
848 IN UINTN StrSize,
849 IN CONST CHAR8 *fmt,
850 ms_va_list args
851 )
852 /*++
853
854 Routine Description:
855
856 Prints a formatted ascii string to a buffer using a ms_va_list
857
858 Arguments:
859
860 Str - Output buffer to print the formatted string into
861
862 StrSize - Size of Str. String is truncated to this size.
863 A size of 0 means there is no limit
864
865 fmt - The format string
866
867 args - ms_va_list
868
869
870 Returns:
871
872 String length returned in buffer
873
874 --*/
875 // Use Unicode VSPrint() and convert back to ASCII
876 {
877 CHAR16 *UnicodeStr, *UnicodeFmt;
878 UINTN i, Len;
879
880 UnicodeStr = AllocatePool(StrSize * sizeof(CHAR16));
881 if (!UnicodeStr)
882 return 0;
883
884 UnicodeFmt = PoolPrint(L"%a", fmt);
885 if (!UnicodeFmt) {
886 FreePool(UnicodeStr);
887 return 0;
888 }
889
890 Len = VSPrint(UnicodeStr, StrSize, UnicodeFmt, args);
891 FreePool(UnicodeFmt);
892
893 // The strings are ASCII so just do a plain Unicode conversion
894 for (i = 0; i < Len; i++)
895 Str[i] = (CHAR8)UnicodeStr[i];
896 Str[Len] = 0;
897 FreePool(UnicodeStr);
898
899 return Len;
900 }
901
902 UINTN EFIAPI
903 AsciiSPrint (
904 OUT CHAR8 *Str,
905 IN UINTN StrSize,
906 IN CONST CHAR8 *fmt,
907 ...
908 )
909 /*++
910
911 Routine Description:
912
913 Prints a formatted unicode string to a buffer
914
915 Arguments:
916
917 Str - Output buffer to print the formatted string into
918
919 StrSize - Size of Str. String is truncated to this size.
920 A size of 0 means there is no limit
921
922 fmt - The format string
923
924 Returns:
925
926 String length returned in buffer
927
928 --*/
929 {
930 ms_va_list args;
931 UINTN len;
932
933 ms_va_start (args, fmt);
934 len = AsciiVSPrint(Str, StrSize, fmt, args);
935 ms_va_end (args);
936
937 return len;
938 }
939
940 STATIC
941 VOID
942 PFLUSH (
943 IN OUT PRINT_STATE *ps
944 )
945 {
946 *ps->Pos = 0;
947 if (IsLocalPrint(ps->Output))
948 ps->Output(ps->Context, ps->Buffer);
949 else
950 uefi_call_wrapper(ps->Output, 2, ps->Context, ps->Buffer);
951 ps->Pos = ps->Buffer;
952 }
953
954 STATIC
955 VOID
956 PSETATTR (
957 IN OUT PRINT_STATE *ps,
958 IN UINTN Attr
959 )
960 {
961 PFLUSH (ps);
962
963 ps->RestoreAttr = ps->Attr;
964 if (ps->SetAttr) {
965 uefi_call_wrapper(ps->SetAttr, 2, ps->Context, Attr);
966 }
967
968 ps->Attr = Attr;
969 }
970
971 STATIC
972 VOID
973 PPUTC (
974 IN OUT PRINT_STATE *ps,
975 IN CHAR16 c
976 )
977 {
978 // if this is a newline, add a carraige return
979 if (c == '\n') {
980 PPUTC (ps, '\r');
981 }
982
983 *ps->Pos = c;
984 ps->Pos += 1;
985 ps->Len += 1;
986
987 // if at the end of the buffer, flush it
988 if (ps->Pos >= ps->End) {
989 PFLUSH(ps);
990 }
991 }
992
993
994 STATIC
995 CHAR16
996 PGETC (
997 IN POINTER *p
998 )
999 {
1000 CHAR16 c;
1001
1002 c = p->Ascii ? p->pc[p->Index] : p->pw[p->Index];
1003 p->Index += 1;
1004
1005 return c;
1006 }
1007
1008
1009 STATIC
1010 VOID
1011 PITEM (
1012 IN OUT PRINT_STATE *ps
1013 )
1014 {
1015 UINTN Len, i;
1016 PRINT_ITEM *Item;
1017 CHAR16 c;
1018
1019 // Get the length of the item
1020 Item = ps->Item;
1021 Item->Item.Index = 0;
1022 while (Item->Item.Index < Item->FieldWidth) {
1023 c = PGETC(&Item->Item);
1024 if (!c) {
1025 Item->Item.Index -= 1;
1026 break;
1027 }
1028 }
1029 Len = Item->Item.Index;
1030
1031 // if there is no item field width, use the items width
1032 if (Item->FieldWidth == (UINTN) -1) {
1033 Item->FieldWidth = Len;
1034 }
1035
1036 // if item is larger then width, update width
1037 if (Len > Item->Width) {
1038 Item->Width = Len;
1039 }
1040
1041
1042 // if pad field before, add pad char
1043 if (Item->PadBefore) {
1044 for (i=Item->Width; i < Item->FieldWidth; i+=1) {
1045 PPUTC (ps, ' ');
1046 }
1047 }
1048
1049 // pad item
1050 for (i=Len; i < Item->Width; i++) {
1051 PPUTC (ps, Item->Pad);
1052 }
1053
1054 // add the item
1055 Item->Item.Index=0;
1056 while (Item->Item.Index < Len) {
1057 PPUTC (ps, PGETC(&Item->Item));
1058 }
1059
1060 // If pad at the end, add pad char
1061 if (!Item->PadBefore) {
1062 for (i=Item->Width; i < Item->FieldWidth; i+=1) {
1063 PPUTC (ps, ' ');
1064 }
1065 }
1066 }
1067
1068
1069 STATIC
1070 UINTN
1071 _Print (
1072 IN PRINT_STATE *ps
1073 )
1074 /*++
1075
1076 Routine Description:
1077
1078 %w.lF - w = width
1079 l = field width
1080 F = format of arg
1081
1082 Args F:
1083 0 - pad with zeros
1084 - - justify on left (default is on right)
1085 , - add comma's to field
1086 * - width provided on stack
1087 n - Set output attribute to normal (for this field only)
1088 h - Set output attribute to highlight (for this field only)
1089 e - Set output attribute to error (for this field only)
1090 l - Value is 64 bits
1091
1092 a - ascii string
1093 s - unicode string
1094 X - fixed 8 byte value in hex
1095 x - hex value
1096 d - value as signed decimal
1097 u - value as unsigned decimal
1098 f - value as floating point
1099 c - Unicode char
1100 t - EFI time structure
1101 g - Pointer to GUID
1102 r - EFI status code (result code)
1103 D - pointer to Device Path with normal ending.
1104
1105 N - Set output attribute to normal
1106 H - Set output attribute to highlight
1107 E - Set output attribute to error
1108 % - Print a %
1109
1110 Arguments:
1111
1112 SystemTable - The system table
1113
1114 Returns:
1115
1116 Number of charactors written
1117
1118 --*/
1119 {
1120 CHAR16 c;
1121 UINTN Attr;
1122 PRINT_ITEM Item;
1123 CHAR16 Buffer[PRINT_STRING_LEN];
1124
1125 ps->Len = 0;
1126 ps->Buffer = Buffer;
1127 ps->Pos = Buffer;
1128 ps->End = Buffer + PRINT_STRING_LEN - 1;
1129 ps->Item = &Item;
1130
1131 ps->fmt.Index = 0;
1132 while ((c = PGETC(&ps->fmt))) {
1133
1134 if (c != '%') {
1135 PPUTC ( ps, c );
1136 continue;
1137 }
1138
1139 // setup for new item
1140 Item.FieldWidth = (UINTN) -1;
1141 Item.Width = 0;
1142 Item.WidthParse = &Item.Width;
1143 Item.Pad = ' ';
1144 Item.PadBefore = TRUE;
1145 Item.Comma = FALSE;
1146 Item.Long = FALSE;
1147 Item.Item.Ascii = FALSE;
1148 Item.Item.pw = NULL;
1149 ps->RestoreAttr = 0;
1150 Attr = 0;
1151
1152 while ((c = PGETC(&ps->fmt))) {
1153
1154 switch (c) {
1155
1156 case '%':
1157 //
1158 // %% -> %
1159 //
1160 Item.Scratch[0] = '%';
1161 Item.Scratch[1] = 0;
1162 Item.Item.pw = Item.Scratch;
1163 break;
1164
1165 case '0':
1166 Item.Pad = '0';
1167 break;
1168
1169 case '-':
1170 Item.PadBefore = FALSE;
1171 break;
1172
1173 case ',':
1174 Item.Comma = TRUE;
1175 break;
1176
1177 case '.':
1178 Item.WidthParse = &Item.FieldWidth;
1179 break;
1180
1181 case '*':
1182 *Item.WidthParse = ms_va_arg(ps->args, UINTN);
1183 break;
1184
1185 case '1':
1186 case '2':
1187 case '3':
1188 case '4':
1189 case '5':
1190 case '6':
1191 case '7':
1192 case '8':
1193 case '9':
1194 *Item.WidthParse = 0;
1195 do {
1196 *Item.WidthParse = *Item.WidthParse * 10 + c - '0';
1197 c = PGETC(&ps->fmt);
1198 } while (c >= '0' && c <= '9') ;
1199 ps->fmt.Index -= 1;
1200 break;
1201
1202 case 'a':
1203 Item.Item.pc = ms_va_arg(ps->args, CHAR8 *);
1204 Item.Item.Ascii = TRUE;
1205 if (!Item.Item.pc) {
1206 Item.Item.pc = (CHAR8 *)"(null)";
1207 }
1208 break;
1209
1210 case 's':
1211 Item.Item.pw = ms_va_arg(ps->args, CHAR16 *);
1212 if (!Item.Item.pw) {
1213 Item.Item.pw = L"(null)";
1214 }
1215 break;
1216
1217 case 'c':
1218 Item.Scratch[0] = (CHAR16) ms_va_arg(ps->args, UINTN);
1219 Item.Scratch[1] = 0;
1220 Item.Item.pw = Item.Scratch;
1221 break;
1222
1223 case 'l':
1224 Item.Long = TRUE;
1225 break;
1226
1227 case 'X':
1228 Item.Width = Item.Long ? 16 : 8;
1229 Item.Pad = '0';
1230 #if __GNUC__ >= 7
1231 __attribute__ ((fallthrough));
1232 #endif
1233 case 'x':
1234 ValueToHex (
1235 Item.Scratch,
1236 Item.Long ? ms_va_arg(ps->args, UINT64) : ms_va_arg(ps->args, UINT32)
1237 );
1238 Item.Item.pw = Item.Scratch;
1239
1240 break;
1241
1242
1243 case 'g':
1244 GuidToString (Item.Scratch, ms_va_arg(ps->args, EFI_GUID *));
1245 Item.Item.pw = Item.Scratch;
1246 break;
1247
1248 case 'u':
1249 ValueToString (
1250 Item.Scratch,
1251 Item.Comma,
1252 Item.Long ? ms_va_arg(ps->args, UINT64) : ms_va_arg(ps->args, UINT32)
1253 );
1254 Item.Item.pw = Item.Scratch;
1255 break;
1256
1257 case 'd':
1258 ValueToString (
1259 Item.Scratch,
1260 Item.Comma,
1261 Item.Long ? ms_va_arg(ps->args, INT64) : ms_va_arg(ps->args, INT32)
1262 );
1263 Item.Item.pw = Item.Scratch;
1264 break;
1265
1266 case 'D':
1267 {
1268 EFI_DEVICE_PATH *dp = ms_va_arg(ps->args, EFI_DEVICE_PATH *);
1269 CHAR16 *dpstr = DevicePathToStr(dp);
1270 StrnCpy(Item.Scratch, dpstr, PRINT_ITEM_BUFFER_LEN);
1271 Item.Scratch[PRINT_ITEM_BUFFER_LEN-1] = L'\0';
1272 FreePool(dpstr);
1273
1274 Item.Item.pw = Item.Scratch;
1275 break;
1276 }
1277
1278 case 'f':
1279 FloatToString (
1280 Item.Scratch,
1281 Item.Comma,
1282 ms_va_arg(ps->args, double)
1283 );
1284 Item.Item.pw = Item.Scratch;
1285 break;
1286
1287 case 't':
1288 TimeToString (Item.Scratch, ms_va_arg(ps->args, EFI_TIME *));
1289 Item.Item.pw = Item.Scratch;
1290 break;
1291
1292 case 'r':
1293 StatusToString (Item.Scratch, ms_va_arg(ps->args, EFI_STATUS));
1294 Item.Item.pw = Item.Scratch;
1295 break;
1296
1297 case 'n':
1298 PSETATTR(ps, ps->AttrNorm);
1299 break;
1300
1301 case 'h':
1302 PSETATTR(ps, ps->AttrHighlight);
1303 break;
1304
1305 case 'e':
1306 PSETATTR(ps, ps->AttrError);
1307 break;
1308
1309 case 'N':
1310 Attr = ps->AttrNorm;
1311 break;
1312
1313 case 'H':
1314 Attr = ps->AttrHighlight;
1315 break;
1316
1317 case 'E':
1318 Attr = ps->AttrError;
1319 break;
1320
1321 default:
1322 Item.Scratch[0] = '?';
1323 Item.Scratch[1] = 0;
1324 Item.Item.pw = Item.Scratch;
1325 break;
1326 }
1327
1328 // if we have an Item
1329 if (Item.Item.pw) {
1330 PITEM (ps);
1331 break;
1332 }
1333
1334 // if we have an Attr set
1335 if (Attr) {
1336 PSETATTR(ps, Attr);
1337 ps->RestoreAttr = 0;
1338 break;
1339 }
1340 }
1341
1342 if (ps->RestoreAttr) {
1343 PSETATTR(ps, ps->RestoreAttr);
1344 }
1345 }
1346
1347 // Flush buffer
1348 PFLUSH (ps);
1349 return ps->Len;
1350 }
1351
1352 STATIC CHAR8 Hex[] = {'0','1','2','3','4','5','6','7',
1353 '8','9','A','B','C','D','E','F'};
1354
1355 VOID
1356 ValueToHex (
1357 IN CHAR16 *Buffer,
1358 IN UINT64 v
1359 )
1360 {
1361 CHAR8 str[30], *p1;
1362 CHAR16 *p2;
1363
1364 if (!v) {
1365 Buffer[0] = '0';
1366 Buffer[1] = 0;
1367 return ;
1368 }
1369
1370 p1 = str;
1371 p2 = Buffer;
1372
1373 while (v) {
1374 // Without the cast, the MSVC compiler may insert a reference to __allmull
1375 *(p1++) = Hex[(UINTN)(v & 0xf)];
1376 v = RShiftU64 (v, 4);
1377 }
1378
1379 while (p1 != str) {
1380 *(p2++) = *(--p1);
1381 }
1382 *p2 = 0;
1383 }
1384
1385
1386 VOID
1387 ValueToString (
1388 IN CHAR16 *Buffer,
1389 IN BOOLEAN Comma,
1390 IN INT64 v
1391 )
1392 {
1393 STATIC CHAR8 ca[] = { 3, 1, 2 };
1394 CHAR8 str[40], *p1;
1395 CHAR16 *p2;
1396 UINTN c, r;
1397
1398 if (!v) {
1399 Buffer[0] = '0';
1400 Buffer[1] = 0;
1401 return ;
1402 }
1403
1404 p1 = str;
1405 p2 = Buffer;
1406
1407 if (v < 0) {
1408 *(p2++) = '-';
1409 v = -v;
1410 }
1411
1412 while (v) {
1413 v = (INT64)DivU64x32 ((UINT64)v, 10, &r);
1414 *(p1++) = (CHAR8)r + '0';
1415 }
1416
1417 c = (Comma ? ca[(p1 - str) % 3] : 999) + 1;
1418 while (p1 != str) {
1419
1420 c -= 1;
1421 if (!c) {
1422 *(p2++) = ',';
1423 c = 3;
1424 }
1425
1426 *(p2++) = *(--p1);
1427 }
1428 *p2 = 0;
1429 }
1430
1431 VOID
1432 FloatToString (
1433 IN CHAR16 *Buffer,
1434 IN BOOLEAN Comma,
1435 IN double v
1436 )
1437 {
1438 /*
1439 * Integer part.
1440 */
1441 INTN i = (INTN)v;
1442 ValueToString(Buffer, Comma, i);
1443
1444
1445 /*
1446 * Decimal point.
1447 */
1448 UINTN x = StrLen(Buffer);
1449 Buffer[x] = L'.';
1450 x++;
1451
1452
1453 /*
1454 * Keep fractional part.
1455 */
1456 float f = (float)(v - i);
1457 if (f < 0) f = -f;
1458
1459
1460 /*
1461 * Leading fractional zeroes.
1462 */
1463 f *= 10.0;
1464 while ( (f != 0)
1465 && ((INTN)f == 0))
1466 {
1467 Buffer[x] = L'0';
1468 x++;
1469 f *= 10.0;
1470 }
1471
1472
1473 /*
1474 * Fractional digits.
1475 */
1476 while ((float)(INTN)f != f)
1477 {
1478 f *= 10;
1479 }
1480 ValueToString(Buffer + x, FALSE, (INTN)f);
1481 return;
1482 }
1483
1484 VOID
1485 TimeToString (
1486 OUT CHAR16 *Buffer,
1487 IN EFI_TIME *Time
1488 )
1489 {
1490 UINTN Hour, Year;
1491 CHAR16 AmPm;
1492
1493 AmPm = 'a';
1494 Hour = Time->Hour;
1495 if (Time->Hour == 0) {
1496 Hour = 12;
1497 } else if (Time->Hour >= 12) {
1498 AmPm = 'p';
1499 if (Time->Hour >= 13) {
1500 Hour -= 12;
1501 }
1502 }
1503
1504 Year = Time->Year % 100;
1505
1506 // bugbug: for now just print it any old way
1507 SPrint (Buffer, 0, L"%02d/%02d/%02d %02d:%02d%c",
1508 Time->Month,
1509 Time->Day,
1510 Year,
1511 Hour,
1512 Time->Minute,
1513 AmPm
1514 );
1515 }
1516
1517
1518
1519
1520 VOID
1521 DumpHex (
1522 IN UINTN Indent,
1523 IN UINTN Offset,
1524 IN UINTN DataSize,
1525 IN VOID *UserData
1526 )
1527 {
1528 CHAR8 *Data, Val[50], Str[20], c;
1529 UINTN Size, Index;
1530
1531 UINTN ScreenCount;
1532 UINTN TempColumn;
1533 UINTN ScreenSize;
1534 CHAR16 ReturnStr[1];
1535
1536
1537 uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &TempColumn, &ScreenSize);
1538 ScreenCount = 0;
1539 ScreenSize -= 2;
1540
1541 Data = UserData;
1542 while (DataSize) {
1543 Size = 16;
1544 if (Size > DataSize) {
1545 Size = DataSize;
1546 }
1547
1548 for (Index=0; Index < Size; Index += 1) {
1549 c = Data[Index];
1550 Val[Index*3+0] = Hex[c>>4];
1551 Val[Index*3+1] = Hex[c&0xF];
1552 Val[Index*3+2] = (Index == 7)?'-':' ';
1553 Str[Index] = (c < ' ' || c > 'z') ? '.' : c;
1554 }
1555
1556 Val[Index*3] = 0;
1557 Str[Index] = 0;
1558 Print (L"%*a%X: %-.48a *%a*\n", Indent, "", Offset, Val, Str);
1559
1560 Data += Size;
1561 Offset += Size;
1562 DataSize -= Size;
1563
1564 ScreenCount++;
1565 if (ScreenCount >= ScreenSize && ScreenSize != 0) {
1566 //
1567 // If ScreenSize == 0 we have the console redirected so don't
1568 // block updates
1569 //
1570 ScreenCount = 0;
1571 Print (L"Press Enter to continue :");
1572 Input (L"", ReturnStr, sizeof(ReturnStr)/sizeof(CHAR16));
1573 Print (L"\n");
1574 }
1575
1576 }
1577 }