12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466 |
- /*++
- Copyright (c) 1998 Intel Corporation
- Module Name:
- print.c
- Abstract:
- Revision History
- --*/
- #include "lib.h"
- #include "efistdarg.h" // !!!
- //
- // Declare runtime functions
- //
- #ifdef RUNTIME_CODE
- #ifndef __GNUC__
- #pragma RUNTIME_CODE(DbgPrint)
- // For debugging..
- /*
- #pragma RUNTIME_CODE(_Print)
- #pragma RUNTIME_CODE(PFLUSH)
- #pragma RUNTIME_CODE(PSETATTR)
- #pragma RUNTIME_CODE(PPUTC)
- #pragma RUNTIME_CODE(PGETC)
- #pragma RUNTIME_CODE(PITEM)
- #pragma RUNTIME_CODE(ValueToHex)
- #pragma RUNTIME_CODE(ValueToString)
- #pragma RUNTIME_CODE(TimeToString)
- */
- #endif /* !defined(__GNUC__) */
- #endif
- //
- //
- //
- #define PRINT_STRING_LEN 200
- #define PRINT_ITEM_BUFFER_LEN 100
- typedef struct {
- BOOLEAN Ascii;
- UINTN Index;
- union {
- CHAR16 *pw;
- CHAR8 *pc;
- } un;
- } POINTER;
- #define pw un.pw
- #define pc un.pc
- typedef struct _pitem {
- POINTER Item;
- CHAR16 Scratch[PRINT_ITEM_BUFFER_LEN];
- UINTN Width;
- UINTN FieldWidth;
- UINTN *WidthParse;
- CHAR16 Pad;
- BOOLEAN PadBefore;
- BOOLEAN Comma;
- BOOLEAN Long;
- } PRINT_ITEM;
- typedef struct _pstate {
- // Input
- POINTER fmt;
- va_list args;
- // Output
- CHAR16 *Buffer;
- CHAR16 *End;
- CHAR16 *Pos;
- UINTN Len;
- UINTN Attr;
- UINTN RestoreAttr;
- UINTN AttrNorm;
- UINTN AttrHighlight;
- UINTN AttrError;
- INTN (EFIAPI *Output)(VOID *context, CHAR16 *str);
- INTN (EFIAPI *SetAttr)(VOID *context, UINTN attr);
- VOID *Context;
- // Current item being formatted
- struct _pitem *Item;
- } PRINT_STATE;
- //
- // Internal fucntions
- //
- STATIC
- UINTN
- _Print (
- IN PRINT_STATE *ps
- );
- STATIC
- UINTN
- _IPrint (
- IN UINTN Column,
- IN UINTN Row,
- IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
- IN CHAR16 *fmt,
- IN CHAR8 *fmta,
- IN va_list args
- );
- STATIC
- INTN EFIAPI
- _DbgOut (
- IN VOID *Context,
- IN CHAR16 *Buffer
- );
- STATIC
- VOID
- PFLUSH (
- IN OUT PRINT_STATE *ps
- );
- STATIC
- VOID
- PPUTC (
- IN OUT PRINT_STATE *ps,
- IN CHAR16 c
- );
- STATIC
- VOID
- PITEM (
- IN OUT PRINT_STATE *ps
- );
- STATIC
- CHAR16
- PGETC (
- IN POINTER *p
- );
- STATIC
- VOID
- PSETATTR (
- IN OUT PRINT_STATE *ps,
- IN UINTN Attr
- );
- //
- //
- //
- INTN EFIAPI
- _SPrint (
- IN VOID *Context,
- IN CHAR16 *Buffer
- );
- INTN EFIAPI
- _PoolPrint (
- IN VOID *Context,
- IN CHAR16 *Buffer
- );
- INTN
- DbgPrint (
- IN INTN mask,
- IN CHAR8 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to the default StandardError console
- Arguments:
- mask - Bit mask of debug string. If a bit is set in the
- mask that is also set in EFIDebug the string is
- printed; otherwise, the string is not printed
- fmt - Format string
- Returns:
- Length of string printed to the StandardError console
- --*/
- {
- SIMPLE_TEXT_OUTPUT_INTERFACE *DbgOut;
- PRINT_STATE ps;
- va_list args;
- UINTN back;
- UINTN attr;
- UINTN SavedAttribute;
- if (!(EFIDebug & mask)) {
- return 0;
- }
- va_start (args, fmt);
- ZeroMem (&ps, sizeof(ps));
- ps.Output = _DbgOut;
- ps.fmt.Ascii = TRUE;
- ps.fmt.pc = fmt;
- va_copy(ps.args, args);
- ps.Attr = EFI_TEXT_ATTR(EFI_LIGHTGRAY, EFI_RED);
- DbgOut = LibRuntimeDebugOut;
- if (!DbgOut) {
- DbgOut = ST->StdErr;
- }
- if (DbgOut) {
- ps.Attr = DbgOut->Mode->Attribute;
- ps.Context = DbgOut;
- ps.SetAttr = (INTN (EFIAPI *)(VOID *, UINTN)) DbgOut->SetAttribute;
- }
- SavedAttribute = ps.Attr;
- back = (ps.Attr >> 4) & 0xf;
- ps.AttrNorm = EFI_TEXT_ATTR(EFI_LIGHTGRAY, back);
- ps.AttrHighlight = EFI_TEXT_ATTR(EFI_WHITE, back);
- ps.AttrError = EFI_TEXT_ATTR(EFI_YELLOW, back);
- attr = ps.AttrNorm;
- if (mask & D_WARN) {
- attr = ps.AttrHighlight;
- }
- if (mask & D_ERROR) {
- attr = ps.AttrError;
- }
- if (ps.SetAttr) {
- ps.Attr = attr;
- uefi_call_wrapper(ps.SetAttr, 2, ps.Context, attr);
- }
- _Print (&ps);
- va_end (ps.args);
- va_end (args);
- //
- // Restore original attributes
- //
- if (ps.SetAttr) {
- uefi_call_wrapper(ps.SetAttr, 2, ps.Context, SavedAttribute);
- }
- return 0;
- }
- STATIC
- INTN
- IsLocalPrint(void *func)
- {
- if (func == _DbgOut || func == _SPrint || func == _PoolPrint)
- return 1;
- return 0;
- }
- STATIC
- INTN EFIAPI
- _DbgOut (
- IN VOID *Context,
- IN CHAR16 *Buffer
- )
- // Append string worker for DbgPrint
- {
- SIMPLE_TEXT_OUTPUT_INTERFACE *DbgOut;
- DbgOut = Context;
- // if (!DbgOut && ST && ST->ConOut) {
- // DbgOut = ST->ConOut;
- // }
- if (DbgOut) {
- if (IsLocalPrint(DbgOut->OutputString))
- DbgOut->OutputString(DbgOut, Buffer);
- else
- uefi_call_wrapper(DbgOut->OutputString, 2, DbgOut, Buffer);
- }
- return 0;
- }
- INTN EFIAPI
- _SPrint (
- IN VOID *Context,
- IN CHAR16 *Buffer
- )
- // Append string worker for SPrint, PoolPrint and CatPrint
- {
- UINTN len;
- POOL_PRINT *spc;
- spc = Context;
- len = StrLen(Buffer);
- //
- // Is the string is over the max truncate it
- //
- if (spc->len + len > spc->maxlen) {
- len = spc->maxlen - spc->len;
- }
- //
- // Append the new text
- //
- CopyMem (spc->str + spc->len, Buffer, len * sizeof(CHAR16));
- spc->len += len;
- //
- // Null terminate it
- //
- if (spc->len < spc->maxlen) {
- spc->str[spc->len] = 0;
- } else if (spc->maxlen) {
- spc->str[spc->maxlen] = 0;
- }
- return 0;
- }
- INTN EFIAPI
- _PoolPrint (
- IN VOID *Context,
- IN CHAR16 *Buffer
- )
- // Append string worker for PoolPrint and CatPrint
- {
- UINTN newlen;
- POOL_PRINT *spc;
- spc = Context;
- newlen = spc->len + StrLen(Buffer) + 1;
- //
- // Is the string is over the max, grow the buffer
- //
- if (newlen > spc->maxlen) {
- //
- // Grow the pool buffer
- //
- newlen += PRINT_STRING_LEN;
- spc->maxlen = newlen;
- spc->str = ReallocatePool (
- spc->str,
- spc->len * sizeof(CHAR16),
- spc->maxlen * sizeof(CHAR16)
- );
- if (!spc->str) {
- spc->len = 0;
- spc->maxlen = 0;
- }
- }
- //
- // Append the new text
- //
- return _SPrint (Context, Buffer);
- }
- VOID
- _PoolCatPrint (
- IN CHAR16 *fmt,
- IN va_list args,
- IN OUT POOL_PRINT *spc,
- IN INTN (EFIAPI *Output)(VOID *context, CHAR16 *str)
- )
- // Dispath function for SPrint, PoolPrint, and CatPrint
- {
- PRINT_STATE ps;
- ZeroMem (&ps, sizeof(ps));
- ps.Output = Output;
- ps.Context = spc;
- ps.fmt.pw = fmt;
- va_copy(ps.args, args);
- _Print (&ps);
- va_end(ps.args);
- }
- UINTN
- VSPrint (
- OUT CHAR16 *Str,
- IN UINTN StrSize,
- IN CHAR16 *fmt,
- va_list args
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to a buffer using a va_list
- Arguments:
- Str - Output buffer to print the formatted string into
- StrSize - Size of Str. String is truncated to this size.
- A size of 0 means there is no limit
- fmt - The format string
- args - va_list
- Returns:
- String length returned in buffer
- --*/
- {
- POOL_PRINT spc;
- spc.str = Str;
- spc.maxlen = StrSize / sizeof(CHAR16) - 1;
- spc.len = 0;
- _PoolCatPrint (fmt, args, &spc, _SPrint);
- return spc.len;
- }
- UINTN
- SPrint (
- OUT CHAR16 *Str,
- IN UINTN StrSize,
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to a buffer
- Arguments:
- Str - Output buffer to print the formatted string into
- StrSize - Size of Str. String is truncated to this size.
- A size of 0 means there is no limit
- fmt - The format string
- Returns:
- String length returned in buffer
- --*/
- {
- va_list args;
- UINTN len;
- va_start (args, fmt);
- len = VSPrint(Str, StrSize, fmt, args);
- va_end (args);
- return len;
- }
- CHAR16 *
- VPoolPrint (
- IN CHAR16 *fmt,
- va_list args
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to allocated pool using va_list argument.
- The caller must free the resulting buffer.
- Arguments:
- fmt - The format string
- args - The arguments in va_list form
- Returns:
- Allocated buffer with the formatted string printed in it.
- The caller must free the allocated buffer. The buffer
- allocation is not packed.
- --*/
- {
- POOL_PRINT spc;
- ZeroMem (&spc, sizeof(spc));
- _PoolCatPrint (fmt, args, &spc, _PoolPrint);
- return spc.str;
- }
- CHAR16 *
- PoolPrint (
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to allocated pool. The caller
- must free the resulting buffer.
- Arguments:
- fmt - The format string
- Returns:
- Allocated buffer with the formatted string printed in it.
- The caller must free the allocated buffer. The buffer
- allocation is not packed.
- --*/
- {
- va_list args;
- CHAR16 *pool;
- va_start (args, fmt);
- pool = VPoolPrint(fmt, args);
- va_end (args);
- return pool;
- }
- CHAR16 *
- CatPrint (
- IN OUT POOL_PRINT *Str,
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Concatenates a formatted unicode string to allocated pool.
- The caller must free the resulting buffer.
- Arguments:
- Str - Tracks the allocated pool, size in use, and
- amount of pool allocated.
- fmt - The format string
- Returns:
- Allocated buffer with the formatted string printed in it.
- The caller must free the allocated buffer. The buffer
- allocation is not packed.
- --*/
- {
- va_list args;
- va_start (args, fmt);
- _PoolCatPrint (fmt, args, Str, _PoolPrint);
- va_end (args);
- return Str->str;
- }
- UINTN
- Print (
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to the default console
- Arguments:
- fmt - Format string
- Returns:
- Length of string printed to the console
- --*/
- {
- va_list args;
- UINTN back;
- va_start (args, fmt);
- back = _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, fmt, NULL, args);
- va_end (args);
- return back;
- }
- UINTN
- VPrint (
- IN CHAR16 *fmt,
- va_list args
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to the default console using a va_list
- Arguments:
- fmt - Format string
- args - va_list
- Returns:
- Length of string printed to the console
- --*/
- {
- return _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, fmt, NULL, args);
- }
- UINTN
- PrintAt (
- IN UINTN Column,
- IN UINTN Row,
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to the default console, at
- the supplied cursor position
- Arguments:
- Column, Row - The cursor position to print the string at
- fmt - Format string
- Returns:
- Length of string printed to the console
- --*/
- {
- va_list args;
- UINTN back;
- va_start (args, fmt);
- back = _IPrint (Column, Row, ST->ConOut, fmt, NULL, args);
- va_end (args);
- return back;
- }
- UINTN
- IPrint (
- IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to the specified console
- Arguments:
- Out - The console to print the string too
- fmt - Format string
- Returns:
- Length of string printed to the console
- --*/
- {
- va_list args;
- UINTN back;
- va_start (args, fmt);
- back = _IPrint ((UINTN) -1, (UINTN) -1, Out, fmt, NULL, args);
- va_end (args);
- return back;
- }
- UINTN
- IPrintAt (
- IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
- IN UINTN Column,
- IN UINTN Row,
- IN CHAR16 *fmt,
- ...
- )
- /*++
- Routine Description:
- Prints a formatted unicode string to the specified console, at
- the supplied cursor position
- Arguments:
- Out - The console to print the string to
- Column, Row - The cursor position to print the string at
- fmt - Format string
- Returns:
- Length of string printed to the console
- --*/
- {
- va_list args;
- UINTN back;
- va_start (args, fmt);
- back = _IPrint (Column, Row, Out, fmt, NULL, args);
- va_end (args);
- return back;
- }
- UINTN
- _IPrint (
- IN UINTN Column,
- IN UINTN Row,
- IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
- IN CHAR16 *fmt,
- IN CHAR8 *fmta,
- IN va_list args
- )
- // Display string worker for: Print, PrintAt, IPrint, IPrintAt
- {
- PRINT_STATE ps;
- UINTN back;
- ZeroMem (&ps, sizeof(ps));
- ps.Context = Out;
- ps.Output = (INTN (EFIAPI *)(VOID *, CHAR16 *)) Out->OutputString;
- ps.SetAttr = (INTN (EFIAPI *)(VOID *, UINTN)) Out->SetAttribute;
- ps.Attr = Out->Mode->Attribute;
- back = (ps.Attr >> 4) & 0xF;
- ps.AttrNorm = EFI_TEXT_ATTR(EFI_LIGHTGRAY, back);
- ps.AttrHighlight = EFI_TEXT_ATTR(EFI_WHITE, back);
- ps.AttrError = EFI_TEXT_ATTR(EFI_YELLOW, back);
- if (fmt) {
- ps.fmt.pw = fmt;
- } else {
- ps.fmt.Ascii = TRUE;
- ps.fmt.pc = fmta;
- }
- va_copy(ps.args, args);
- if (Column != (UINTN) -1) {
- uefi_call_wrapper(Out->SetCursorPosition, 3, Out, Column, Row);
- }
- back = _Print (&ps);
- va_end(ps.args);
- return back;
- }
- UINTN
- APrint (
- IN CHAR8 *fmt,
- ...
- )
- /*++
- Routine Description:
- For those whom really can't deal with unicode, a print
- function that takes an ascii format string
- Arguments:
- fmt - ascii format string
- Returns:
- Length of string printed to the console
- --*/
- {
- va_list args;
- UINTN back;
- va_start (args, fmt);
- back = _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, NULL, fmt, args);
- va_end (args);
- return back;
- }
- STATIC
- VOID
- PFLUSH (
- IN OUT PRINT_STATE *ps
- )
- {
- *ps->Pos = 0;
- if (IsLocalPrint(ps->Output))
- ps->Output(ps->Context, ps->Buffer);
- else
- uefi_call_wrapper(ps->Output, 2, ps->Context, ps->Buffer);
- ps->Pos = ps->Buffer;
- }
- STATIC
- VOID
- PSETATTR (
- IN OUT PRINT_STATE *ps,
- IN UINTN Attr
- )
- {
- PFLUSH (ps);
- ps->RestoreAttr = ps->Attr;
- if (ps->SetAttr) {
- uefi_call_wrapper(ps->SetAttr, 2, ps->Context, Attr);
- }
- ps->Attr = Attr;
- }
- STATIC
- VOID
- PPUTC (
- IN OUT PRINT_STATE *ps,
- IN CHAR16 c
- )
- {
- // if this is a newline, add a carraige return
- if (c == '\n') {
- PPUTC (ps, '\r');
- }
- *ps->Pos = c;
- ps->Pos += 1;
- ps->Len += 1;
- // if at the end of the buffer, flush it
- if (ps->Pos >= ps->End) {
- PFLUSH(ps);
- }
- }
- STATIC
- CHAR16
- PGETC (
- IN POINTER *p
- )
- {
- CHAR16 c;
- c = p->Ascii ? p->pc[p->Index] : p->pw[p->Index];
- p->Index += 1;
- return c;
- }
- STATIC
- VOID
- PITEM (
- IN OUT PRINT_STATE *ps
- )
- {
- UINTN Len, i;
- PRINT_ITEM *Item;
- CHAR16 c;
- // Get the length of the item
- Item = ps->Item;
- Item->Item.Index = 0;
- while (Item->Item.Index < Item->FieldWidth) {
- c = PGETC(&Item->Item);
- if (!c) {
- Item->Item.Index -= 1;
- break;
- }
- }
- Len = Item->Item.Index;
- // if there is no item field width, use the items width
- if (Item->FieldWidth == (UINTN) -1) {
- Item->FieldWidth = Len;
- }
- // if item is larger then width, update width
- if (Len > Item->Width) {
- Item->Width = Len;
- }
- // if pad field before, add pad char
- if (Item->PadBefore) {
- for (i=Item->Width; i < Item->FieldWidth; i+=1) {
- PPUTC (ps, ' ');
- }
- }
- // pad item
- for (i=Len; i < Item->Width; i++) {
- PPUTC (ps, Item->Pad);
- }
- // add the item
- Item->Item.Index=0;
- while (Item->Item.Index < Len) {
- PPUTC (ps, PGETC(&Item->Item));
- }
- // If pad at the end, add pad char
- if (!Item->PadBefore) {
- for (i=Item->Width; i < Item->FieldWidth; i+=1) {
- PPUTC (ps, ' ');
- }
- }
- }
- STATIC
- UINTN
- _Print (
- IN PRINT_STATE *ps
- )
- /*++
- Routine Description:
- %w.lF - w = width
- l = field width
- F = format of arg
- Args F:
- 0 - pad with zeros
- - - justify on left (default is on right)
- , - add comma's to field
- * - width provided on stack
- n - Set output attribute to normal (for this field only)
- h - Set output attribute to highlight (for this field only)
- e - Set output attribute to error (for this field only)
- l - Value is 64 bits
- a - ascii string
- s - unicode string
- X - fixed 8 byte value in hex
- x - hex value
- d - value as signed decimal
- u - value as unsigned decimal
- f - value as floating point
- c - Unicode char
- t - EFI time structure
- g - Pointer to GUID
- r - EFI status code (result code)
- N - Set output attribute to normal
- H - Set output attribute to highlight
- E - Set output attribute to error
- % - Print a %
- Arguments:
- SystemTable - The system table
- Returns:
- Number of charactors written
- --*/
- {
- CHAR16 c;
- UINTN Attr;
- PRINT_ITEM Item;
- CHAR16 Buffer[PRINT_STRING_LEN];
- ps->Len = 0;
- ps->Buffer = Buffer;
- ps->Pos = Buffer;
- ps->End = Buffer + PRINT_STRING_LEN - 1;
- ps->Item = &Item;
- ps->fmt.Index = 0;
- while ((c = PGETC(&ps->fmt))) {
- if (c != '%') {
- PPUTC ( ps, c );
- continue;
- }
- // setup for new item
- Item.FieldWidth = (UINTN) -1;
- Item.Width = 0;
- Item.WidthParse = &Item.Width;
- Item.Pad = ' ';
- Item.PadBefore = TRUE;
- Item.Comma = FALSE;
- Item.Long = FALSE;
- Item.Item.Ascii = FALSE;
- Item.Item.pw = NULL;
- ps->RestoreAttr = 0;
- Attr = 0;
- while ((c = PGETC(&ps->fmt))) {
- switch (c) {
- case '%':
- //
- // %% -> %
- //
- Item.Item.pw = Item.Scratch;
- Item.Item.pw[0] = '%';
- Item.Item.pw[1] = 0;
- break;
- case '0':
- Item.Pad = '0';
- break;
- case '-':
- Item.PadBefore = FALSE;
- break;
- case ',':
- Item.Comma = TRUE;
- break;
- case '.':
- Item.WidthParse = &Item.FieldWidth;
- break;
- case '*':
- *Item.WidthParse = va_arg(ps->args, UINTN);
- break;
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- *Item.WidthParse = 0;
- do {
- *Item.WidthParse = *Item.WidthParse * 10 + c - '0';
- c = PGETC(&ps->fmt);
- } while (c >= '0' && c <= '9') ;
- ps->fmt.Index -= 1;
- break;
- case 'a':
- Item.Item.pc = va_arg(ps->args, CHAR8 *);
- Item.Item.Ascii = TRUE;
- if (!Item.Item.pc) {
- Item.Item.pc = (CHAR8 *)"(null)";
- }
- break;
- case 's':
- Item.Item.pw = va_arg(ps->args, CHAR16 *);
- if (!Item.Item.pw) {
- Item.Item.pw = L"(null)";
- }
- break;
- case 'c':
- Item.Item.pw = Item.Scratch;
- Item.Item.pw[0] = (CHAR16) va_arg(ps->args, UINTN);
- Item.Item.pw[1] = 0;
- break;
- case 'l':
- Item.Long = TRUE;
- break;
- case 'X':
- Item.Width = Item.Long ? 16 : 8;
- Item.Pad = '0';
- case 'x':
- Item.Item.pw = Item.Scratch;
- ValueToHex (
- Item.Item.pw,
- Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
- );
- break;
- case 'g':
- Item.Item.pw = Item.Scratch;
- GuidToString (Item.Item.pw, va_arg(ps->args, EFI_GUID *));
- break;
- case 'u':
- Item.Item.pw = Item.Scratch;
- ValueToString (
- Item.Item.pw,
- Item.Comma,
- Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
- );
- break;
- case 'd':
- Item.Item.pw = Item.Scratch;
- ValueToString (
- Item.Item.pw,
- Item.Comma,
- Item.Long ? va_arg(ps->args, INT64) : va_arg(ps->args, INT32)
- );
- break;
- case 'f':
- Item.Item.pw = Item.Scratch;
- FloatToString (
- Item.Item.pw,
- Item.Comma,
- va_arg(ps->args, double)
- );
- break;
- case 't':
- Item.Item.pw = Item.Scratch;
- TimeToString (Item.Item.pw, va_arg(ps->args, EFI_TIME *));
- break;
- case 'r':
- Item.Item.pw = Item.Scratch;
- StatusToString (Item.Item.pw, va_arg(ps->args, EFI_STATUS));
- break;
- case 'n':
- PSETATTR(ps, ps->AttrNorm);
- break;
- case 'h':
- PSETATTR(ps, ps->AttrHighlight);
- break;
- case 'e':
- PSETATTR(ps, ps->AttrError);
- break;
- case 'N':
- Attr = ps->AttrNorm;
- break;
- case 'H':
- Attr = ps->AttrHighlight;
- break;
- case 'E':
- Attr = ps->AttrError;
- break;
- default:
- Item.Item.pw = Item.Scratch;
- Item.Item.pw[0] = '?';
- Item.Item.pw[1] = 0;
- break;
- }
- // if we have an Item
- if (Item.Item.pw) {
- PITEM (ps);
- break;
- }
- // if we have an Attr set
- if (Attr) {
- PSETATTR(ps, Attr);
- ps->RestoreAttr = 0;
- break;
- }
- }
- if (ps->RestoreAttr) {
- PSETATTR(ps, ps->RestoreAttr);
- }
- }
- // Flush buffer
- PFLUSH (ps);
- return ps->Len;
- }
- STATIC CHAR8 Hex[] = {'0','1','2','3','4','5','6','7',
- '8','9','A','B','C','D','E','F'};
- VOID
- ValueToHex (
- IN CHAR16 *Buffer,
- IN UINT64 v
- )
- {
- CHAR8 str[30], *p1;
- CHAR16 *p2;
- if (!v) {
- Buffer[0] = '0';
- Buffer[1] = 0;
- return ;
- }
- p1 = str;
- p2 = Buffer;
- while (v) {
- // Without the cast, the MSVC compiler may insert a reference to __allmull
- *(p1++) = Hex[(UINTN)(v & 0xf)];
- v = RShiftU64 (v, 4);
- }
- while (p1 != str) {
- *(p2++) = *(--p1);
- }
- *p2 = 0;
- }
- VOID
- ValueToString (
- IN CHAR16 *Buffer,
- IN BOOLEAN Comma,
- IN INT64 v
- )
- {
- STATIC CHAR8 ca[] = { 3, 1, 2 };
- CHAR8 str[40], *p1;
- CHAR16 *p2;
- UINTN c, r;
- if (!v) {
- Buffer[0] = '0';
- Buffer[1] = 0;
- return ;
- }
- p1 = str;
- p2 = Buffer;
- if (v < 0) {
- *(p2++) = '-';
- v = -v;
- }
- while (v) {
- v = (INT64)DivU64x32 ((UINT64)v, 10, &r);
- *(p1++) = (CHAR8)r + '0';
- }
- c = (Comma ? ca[(p1 - str) % 3] : 999) + 1;
- while (p1 != str) {
- c -= 1;
- if (!c) {
- *(p2++) = ',';
- c = 3;
- }
- *(p2++) = *(--p1);
- }
- *p2 = 0;
- }
- VOID
- FloatToString (
- IN CHAR16 *Buffer,
- IN BOOLEAN Comma,
- IN double v
- )
- {
- /*
- * Integer part.
- */
- INTN i = (INTN)v;
- ValueToString(Buffer, Comma, i);
- /*
- * Decimal point.
- */
- UINTN x = StrLen(Buffer);
- Buffer[x] = L'.';
- x++;
- /*
- * Keep fractional part.
- */
- float f = v - (float)i;
- if (f < 0) f = -f;
- /*
- * Leading fractional zeroes.
- */
- f *= 10.0;
- while ( (f != 0)
- && ((INTN)f == 0))
- {
- Buffer[x] = L'0';
- x++;
- f *= 10.0;
- }
- /*
- * Fractional digits.
- */
- while ((float)(INTN)f != f)
- {
- f *= 10;
- }
- ValueToString(Buffer + x, FALSE, (INTN)f);
- return;
- }
- VOID
- TimeToString (
- OUT CHAR16 *Buffer,
- IN EFI_TIME *Time
- )
- {
- UINTN Hour, Year;
- CHAR16 AmPm;
- AmPm = 'a';
- Hour = Time->Hour;
- if (Time->Hour == 0) {
- Hour = 12;
- } else if (Time->Hour >= 12) {
- AmPm = 'p';
- if (Time->Hour >= 13) {
- Hour -= 12;
- }
- }
- Year = Time->Year % 100;
- // bugbug: for now just print it any old way
- SPrint (Buffer, 0, L"%02d/%02d/%02d %02d:%02d%c",
- Time->Month,
- Time->Day,
- Year,
- Hour,
- Time->Minute,
- AmPm
- );
- }
- VOID
- DumpHex (
- IN UINTN Indent,
- IN UINTN Offset,
- IN UINTN DataSize,
- IN VOID *UserData
- )
- {
- CHAR8 *Data, Val[50], Str[20], c;
- UINTN Size, Index;
- UINTN ScreenCount;
- UINTN TempColumn;
- UINTN ScreenSize;
- CHAR16 ReturnStr[1];
- uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &TempColumn, &ScreenSize);
- ScreenCount = 0;
- ScreenSize -= 2;
- Data = UserData;
- while (DataSize) {
- Size = 16;
- if (Size > DataSize) {
- Size = DataSize;
- }
- for (Index=0; Index < Size; Index += 1) {
- c = Data[Index];
- Val[Index*3+0] = Hex[c>>4];
- Val[Index*3+1] = Hex[c&0xF];
- Val[Index*3+2] = (Index == 7)?'-':' ';
- Str[Index] = (c < ' ' || c > 'z') ? '.' : c;
- }
- Val[Index*3] = 0;
- Str[Index] = 0;
- Print (L"%*a%X: %-.48a *%a*\n", Indent, "", Offset, Val, Str);
- Data += Size;
- Offset += Size;
- DataSize -= Size;
- ScreenCount++;
- if (ScreenCount >= ScreenSize && ScreenSize != 0) {
- //
- // If ScreenSize == 0 we have the console redirected so don't
- // block updates
- //
- ScreenCount = 0;
- Print (L"Press Enter to continue :");
- Input (L"", ReturnStr, sizeof(ReturnStr)/sizeof(CHAR16));
- Print (L"\n");
- }
- }
- }
|