print.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. print.c
  5. Abstract:
  6. Revision History
  7. --*/
  8. #include "lib.h"
  9. #include "efistdarg.h" // !!!
  10. //
  11. // Declare runtime functions
  12. //
  13. #ifdef RUNTIME_CODE
  14. #ifndef __GNUC__
  15. #pragma RUNTIME_CODE(DbgPrint)
  16. // For debugging..
  17. /*
  18. #pragma RUNTIME_CODE(_Print)
  19. #pragma RUNTIME_CODE(PFLUSH)
  20. #pragma RUNTIME_CODE(PSETATTR)
  21. #pragma RUNTIME_CODE(PPUTC)
  22. #pragma RUNTIME_CODE(PGETC)
  23. #pragma RUNTIME_CODE(PITEM)
  24. #pragma RUNTIME_CODE(ValueToHex)
  25. #pragma RUNTIME_CODE(ValueToString)
  26. #pragma RUNTIME_CODE(TimeToString)
  27. */
  28. #endif /* !defined(__GNUC__) */
  29. #endif
  30. //
  31. //
  32. //
  33. #define PRINT_STRING_LEN 200
  34. #define PRINT_ITEM_BUFFER_LEN 100
  35. typedef struct {
  36. BOOLEAN Ascii;
  37. UINTN Index;
  38. union {
  39. CONST CHAR16 *pw;
  40. CONST CHAR8 *pc;
  41. } un;
  42. } POINTER;
  43. #define pw un.pw
  44. #define pc un.pc
  45. typedef struct _pitem {
  46. POINTER Item;
  47. CHAR16 Scratch[PRINT_ITEM_BUFFER_LEN];
  48. UINTN Width;
  49. UINTN FieldWidth;
  50. UINTN *WidthParse;
  51. CHAR16 Pad;
  52. BOOLEAN PadBefore;
  53. BOOLEAN Comma;
  54. BOOLEAN Long;
  55. } PRINT_ITEM;
  56. typedef struct _pstate {
  57. // Input
  58. POINTER fmt;
  59. va_list args;
  60. // Output
  61. CHAR16 *Buffer;
  62. CHAR16 *End;
  63. CHAR16 *Pos;
  64. UINTN Len;
  65. UINTN Attr;
  66. UINTN RestoreAttr;
  67. UINTN AttrNorm;
  68. UINTN AttrHighlight;
  69. UINTN AttrError;
  70. INTN (EFIAPI *Output)(VOID *context, CHAR16 *str);
  71. INTN (EFIAPI *SetAttr)(VOID *context, UINTN attr);
  72. VOID *Context;
  73. // Current item being formatted
  74. struct _pitem *Item;
  75. } PRINT_STATE;
  76. //
  77. // Internal fucntions
  78. //
  79. STATIC
  80. UINTN
  81. _Print (
  82. IN PRINT_STATE *ps
  83. );
  84. STATIC
  85. UINTN
  86. _IPrint (
  87. IN UINTN Column,
  88. IN UINTN Row,
  89. IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
  90. IN CONST CHAR16 *fmt,
  91. IN CONST CHAR8 *fmta,
  92. IN va_list args
  93. );
  94. STATIC
  95. INTN EFIAPI
  96. _DbgOut (
  97. IN VOID *Context,
  98. IN CHAR16 *Buffer
  99. );
  100. STATIC
  101. VOID
  102. PFLUSH (
  103. IN OUT PRINT_STATE *ps
  104. );
  105. STATIC
  106. VOID
  107. PPUTC (
  108. IN OUT PRINT_STATE *ps,
  109. IN CHAR16 c
  110. );
  111. STATIC
  112. VOID
  113. PITEM (
  114. IN OUT PRINT_STATE *ps
  115. );
  116. STATIC
  117. CHAR16
  118. PGETC (
  119. IN POINTER *p
  120. );
  121. STATIC
  122. VOID
  123. PSETATTR (
  124. IN OUT PRINT_STATE *ps,
  125. IN UINTN Attr
  126. );
  127. //
  128. //
  129. //
  130. INTN EFIAPI
  131. _SPrint (
  132. IN VOID *Context,
  133. IN CHAR16 *Buffer
  134. );
  135. INTN EFIAPI
  136. _PoolPrint (
  137. IN VOID *Context,
  138. IN CHAR16 *Buffer
  139. );
  140. INTN
  141. DbgPrint (
  142. IN INTN mask,
  143. IN CONST CHAR8 *fmt,
  144. ...
  145. )
  146. /*++
  147. Routine Description:
  148. Prints a formatted unicode string to the default StandardError console
  149. Arguments:
  150. mask - Bit mask of debug string. If a bit is set in the
  151. mask that is also set in EFIDebug the string is
  152. printed; otherwise, the string is not printed
  153. fmt - Format string
  154. Returns:
  155. Length of string printed to the StandardError console
  156. --*/
  157. {
  158. SIMPLE_TEXT_OUTPUT_INTERFACE *DbgOut;
  159. PRINT_STATE ps;
  160. va_list args;
  161. UINTN back;
  162. UINTN attr;
  163. UINTN SavedAttribute;
  164. if (!(EFIDebug & mask)) {
  165. return 0;
  166. }
  167. va_start (args, fmt);
  168. ZeroMem (&ps, sizeof(ps));
  169. ps.Output = _DbgOut;
  170. ps.fmt.Ascii = TRUE;
  171. ps.fmt.pc = fmt;
  172. va_copy(ps.args, args);
  173. ps.Attr = EFI_TEXT_ATTR(EFI_LIGHTGRAY, EFI_RED);
  174. DbgOut = LibRuntimeDebugOut;
  175. if (!DbgOut) {
  176. DbgOut = ST->StdErr;
  177. }
  178. if (DbgOut) {
  179. ps.Attr = DbgOut->Mode->Attribute;
  180. ps.Context = DbgOut;
  181. ps.SetAttr = (INTN (EFIAPI *)(VOID *, UINTN)) DbgOut->SetAttribute;
  182. }
  183. SavedAttribute = ps.Attr;
  184. back = (ps.Attr >> 4) & 0xf;
  185. ps.AttrNorm = EFI_TEXT_ATTR(EFI_LIGHTGRAY, back);
  186. ps.AttrHighlight = EFI_TEXT_ATTR(EFI_WHITE, back);
  187. ps.AttrError = EFI_TEXT_ATTR(EFI_YELLOW, back);
  188. attr = ps.AttrNorm;
  189. if (mask & D_WARN) {
  190. attr = ps.AttrHighlight;
  191. }
  192. if (mask & D_ERROR) {
  193. attr = ps.AttrError;
  194. }
  195. if (ps.SetAttr) {
  196. ps.Attr = attr;
  197. uefi_call_wrapper(ps.SetAttr, 2, ps.Context, attr);
  198. }
  199. _Print (&ps);
  200. va_end (ps.args);
  201. va_end (args);
  202. //
  203. // Restore original attributes
  204. //
  205. if (ps.SetAttr) {
  206. uefi_call_wrapper(ps.SetAttr, 2, ps.Context, SavedAttribute);
  207. }
  208. return 0;
  209. }
  210. STATIC
  211. INTN
  212. IsLocalPrint(void *func)
  213. {
  214. if (func == _DbgOut || func == _SPrint || func == _PoolPrint)
  215. return 1;
  216. return 0;
  217. }
  218. STATIC
  219. INTN EFIAPI
  220. _DbgOut (
  221. IN VOID *Context,
  222. IN CHAR16 *Buffer
  223. )
  224. // Append string worker for DbgPrint
  225. {
  226. SIMPLE_TEXT_OUTPUT_INTERFACE *DbgOut;
  227. DbgOut = Context;
  228. // if (!DbgOut && ST && ST->ConOut) {
  229. // DbgOut = ST->ConOut;
  230. // }
  231. if (DbgOut) {
  232. if (IsLocalPrint(DbgOut->OutputString))
  233. DbgOut->OutputString(DbgOut, Buffer);
  234. else
  235. uefi_call_wrapper(DbgOut->OutputString, 2, DbgOut, Buffer);
  236. }
  237. return 0;
  238. }
  239. INTN EFIAPI
  240. _SPrint (
  241. IN VOID *Context,
  242. IN CHAR16 *Buffer
  243. )
  244. // Append string worker for SPrint, PoolPrint and CatPrint
  245. {
  246. UINTN len;
  247. POOL_PRINT *spc;
  248. spc = Context;
  249. len = StrLen(Buffer);
  250. //
  251. // Is the string is over the max truncate it
  252. //
  253. if (spc->len + len > spc->maxlen) {
  254. len = spc->maxlen - spc->len;
  255. }
  256. //
  257. // Append the new text
  258. //
  259. CopyMem (spc->str + spc->len, Buffer, len * sizeof(CHAR16));
  260. spc->len += len;
  261. //
  262. // Null terminate it
  263. //
  264. if (spc->len < spc->maxlen) {
  265. spc->str[spc->len] = 0;
  266. } else if (spc->maxlen) {
  267. spc->str[spc->maxlen] = 0;
  268. }
  269. return 0;
  270. }
  271. INTN EFIAPI
  272. _PoolPrint (
  273. IN VOID *Context,
  274. IN CHAR16 *Buffer
  275. )
  276. // Append string worker for PoolPrint and CatPrint
  277. {
  278. UINTN newlen;
  279. POOL_PRINT *spc;
  280. spc = Context;
  281. newlen = spc->len + StrLen(Buffer) + 1;
  282. //
  283. // Is the string is over the max, grow the buffer
  284. //
  285. if (newlen > spc->maxlen) {
  286. //
  287. // Grow the pool buffer
  288. //
  289. newlen += PRINT_STRING_LEN;
  290. spc->maxlen = newlen;
  291. spc->str = ReallocatePool (
  292. spc->str,
  293. spc->len * sizeof(CHAR16),
  294. spc->maxlen * sizeof(CHAR16)
  295. );
  296. if (!spc->str) {
  297. spc->len = 0;
  298. spc->maxlen = 0;
  299. }
  300. }
  301. //
  302. // Append the new text
  303. //
  304. return _SPrint (Context, Buffer);
  305. }
  306. VOID
  307. _PoolCatPrint (
  308. IN CONST CHAR16 *fmt,
  309. IN va_list args,
  310. IN OUT POOL_PRINT *spc,
  311. IN INTN (EFIAPI *Output)(VOID *context, CHAR16 *str)
  312. )
  313. // Dispath function for SPrint, PoolPrint, and CatPrint
  314. {
  315. PRINT_STATE ps;
  316. ZeroMem (&ps, sizeof(ps));
  317. ps.Output = Output;
  318. ps.Context = spc;
  319. ps.fmt.pw = fmt;
  320. va_copy(ps.args, args);
  321. _Print (&ps);
  322. va_end(ps.args);
  323. }
  324. UINTN
  325. VSPrint (
  326. OUT CHAR16 *Str,
  327. IN UINTN StrSize,
  328. IN CONST CHAR16 *fmt,
  329. va_list args
  330. )
  331. /*++
  332. Routine Description:
  333. Prints a formatted unicode string to a buffer using a va_list
  334. Arguments:
  335. Str - Output buffer to print the formatted string into
  336. StrSize - Size of Str. String is truncated to this size.
  337. A size of 0 means there is no limit
  338. fmt - The format string
  339. args - va_list
  340. Returns:
  341. String length returned in buffer
  342. --*/
  343. {
  344. POOL_PRINT spc;
  345. spc.str = Str;
  346. spc.maxlen = StrSize / sizeof(CHAR16) - 1;
  347. spc.len = 0;
  348. _PoolCatPrint (fmt, args, &spc, _SPrint);
  349. return spc.len;
  350. }
  351. UINTN
  352. SPrint (
  353. OUT CHAR16 *Str,
  354. IN UINTN StrSize,
  355. IN CONST CHAR16 *fmt,
  356. ...
  357. )
  358. /*++
  359. Routine Description:
  360. Prints a formatted unicode string to a buffer
  361. Arguments:
  362. Str - Output buffer to print the formatted string into
  363. StrSize - Size of Str. String is truncated to this size.
  364. A size of 0 means there is no limit
  365. fmt - The format string
  366. Returns:
  367. String length returned in buffer
  368. --*/
  369. {
  370. va_list args;
  371. UINTN len;
  372. va_start (args, fmt);
  373. len = VSPrint(Str, StrSize, fmt, args);
  374. va_end (args);
  375. return len;
  376. }
  377. CHAR16 *
  378. VPoolPrint (
  379. IN CONST CHAR16 *fmt,
  380. va_list args
  381. )
  382. /*++
  383. Routine Description:
  384. Prints a formatted unicode string to allocated pool using va_list argument.
  385. The caller must free the resulting buffer.
  386. Arguments:
  387. fmt - The format string
  388. args - The arguments in va_list form
  389. Returns:
  390. Allocated buffer with the formatted string printed in it.
  391. The caller must free the allocated buffer. The buffer
  392. allocation is not packed.
  393. --*/
  394. {
  395. POOL_PRINT spc;
  396. ZeroMem (&spc, sizeof(spc));
  397. _PoolCatPrint (fmt, args, &spc, _PoolPrint);
  398. return spc.str;
  399. }
  400. CHAR16 *
  401. PoolPrint (
  402. IN CONST CHAR16 *fmt,
  403. ...
  404. )
  405. /*++
  406. Routine Description:
  407. Prints a formatted unicode string to allocated pool. The caller
  408. must free the resulting buffer.
  409. Arguments:
  410. fmt - The format string
  411. Returns:
  412. Allocated buffer with the formatted string printed in it.
  413. The caller must free the allocated buffer. The buffer
  414. allocation is not packed.
  415. --*/
  416. {
  417. va_list args;
  418. CHAR16 *pool;
  419. va_start (args, fmt);
  420. pool = VPoolPrint(fmt, args);
  421. va_end (args);
  422. return pool;
  423. }
  424. CHAR16 *
  425. CatPrint (
  426. IN OUT POOL_PRINT *Str,
  427. IN CONST CHAR16 *fmt,
  428. ...
  429. )
  430. /*++
  431. Routine Description:
  432. Concatenates a formatted unicode string to allocated pool.
  433. The caller must free the resulting buffer.
  434. Arguments:
  435. Str - Tracks the allocated pool, size in use, and
  436. amount of pool allocated.
  437. fmt - The format string
  438. Returns:
  439. Allocated buffer with the formatted string printed in it.
  440. The caller must free the allocated buffer. The buffer
  441. allocation is not packed.
  442. --*/
  443. {
  444. va_list args;
  445. va_start (args, fmt);
  446. _PoolCatPrint (fmt, args, Str, _PoolPrint);
  447. va_end (args);
  448. return Str->str;
  449. }
  450. UINTN
  451. Print (
  452. IN CONST CHAR16 *fmt,
  453. ...
  454. )
  455. /*++
  456. Routine Description:
  457. Prints a formatted unicode string to the default console
  458. Arguments:
  459. fmt - Format string
  460. Returns:
  461. Length of string printed to the console
  462. --*/
  463. {
  464. va_list args;
  465. UINTN back;
  466. va_start (args, fmt);
  467. back = _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, fmt, NULL, args);
  468. va_end (args);
  469. return back;
  470. }
  471. UINTN
  472. VPrint (
  473. IN CONST CHAR16 *fmt,
  474. va_list args
  475. )
  476. /*++
  477. Routine Description:
  478. Prints a formatted unicode string to the default console using a va_list
  479. Arguments:
  480. fmt - Format string
  481. args - va_list
  482. Returns:
  483. Length of string printed to the console
  484. --*/
  485. {
  486. return _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, fmt, NULL, args);
  487. }
  488. UINTN
  489. PrintAt (
  490. IN UINTN Column,
  491. IN UINTN Row,
  492. IN CONST CHAR16 *fmt,
  493. ...
  494. )
  495. /*++
  496. Routine Description:
  497. Prints a formatted unicode string to the default console, at
  498. the supplied cursor position
  499. Arguments:
  500. Column, Row - The cursor position to print the string at
  501. fmt - Format string
  502. Returns:
  503. Length of string printed to the console
  504. --*/
  505. {
  506. va_list args;
  507. UINTN back;
  508. va_start (args, fmt);
  509. back = _IPrint (Column, Row, ST->ConOut, fmt, NULL, args);
  510. va_end (args);
  511. return back;
  512. }
  513. UINTN
  514. IPrint (
  515. IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
  516. IN CONST CHAR16 *fmt,
  517. ...
  518. )
  519. /*++
  520. Routine Description:
  521. Prints a formatted unicode string to the specified console
  522. Arguments:
  523. Out - The console to print the string too
  524. fmt - Format string
  525. Returns:
  526. Length of string printed to the console
  527. --*/
  528. {
  529. va_list args;
  530. UINTN back;
  531. va_start (args, fmt);
  532. back = _IPrint ((UINTN) -1, (UINTN) -1, Out, fmt, NULL, args);
  533. va_end (args);
  534. return back;
  535. }
  536. UINTN
  537. IPrintAt (
  538. IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
  539. IN UINTN Column,
  540. IN UINTN Row,
  541. IN CONST CHAR16 *fmt,
  542. ...
  543. )
  544. /*++
  545. Routine Description:
  546. Prints a formatted unicode string to the specified console, at
  547. the supplied cursor position
  548. Arguments:
  549. Out - The console to print the string to
  550. Column, Row - The cursor position to print the string at
  551. fmt - Format string
  552. Returns:
  553. Length of string printed to the console
  554. --*/
  555. {
  556. va_list args;
  557. UINTN back;
  558. va_start (args, fmt);
  559. back = _IPrint (Column, Row, Out, fmt, NULL, args);
  560. va_end (args);
  561. return back;
  562. }
  563. UINTN
  564. _IPrint (
  565. IN UINTN Column,
  566. IN UINTN Row,
  567. IN SIMPLE_TEXT_OUTPUT_INTERFACE *Out,
  568. IN CONST CHAR16 *fmt,
  569. IN CONST CHAR8 *fmta,
  570. IN va_list args
  571. )
  572. // Display string worker for: Print, PrintAt, IPrint, IPrintAt
  573. {
  574. PRINT_STATE ps;
  575. UINTN back;
  576. ZeroMem (&ps, sizeof(ps));
  577. ps.Context = Out;
  578. ps.Output = (INTN (EFIAPI *)(VOID *, CHAR16 *)) Out->OutputString;
  579. ps.SetAttr = (INTN (EFIAPI *)(VOID *, UINTN)) Out->SetAttribute;
  580. ps.Attr = Out->Mode->Attribute;
  581. back = (ps.Attr >> 4) & 0xF;
  582. ps.AttrNorm = EFI_TEXT_ATTR(EFI_LIGHTGRAY, back);
  583. ps.AttrHighlight = EFI_TEXT_ATTR(EFI_WHITE, back);
  584. ps.AttrError = EFI_TEXT_ATTR(EFI_YELLOW, back);
  585. if (fmt) {
  586. ps.fmt.pw = fmt;
  587. } else {
  588. ps.fmt.Ascii = TRUE;
  589. ps.fmt.pc = fmta;
  590. }
  591. va_copy(ps.args, args);
  592. if (Column != (UINTN) -1) {
  593. uefi_call_wrapper(Out->SetCursorPosition, 3, Out, Column, Row);
  594. }
  595. back = _Print (&ps);
  596. va_end(ps.args);
  597. return back;
  598. }
  599. UINTN
  600. APrint (
  601. IN CONST CHAR8 *fmt,
  602. ...
  603. )
  604. /*++
  605. Routine Description:
  606. For those whom really can't deal with unicode, a print
  607. function that takes an ascii format string
  608. Arguments:
  609. fmt - ascii format string
  610. Returns:
  611. Length of string printed to the console
  612. --*/
  613. {
  614. va_list args;
  615. UINTN back;
  616. va_start (args, fmt);
  617. back = _IPrint ((UINTN) -1, (UINTN) -1, ST->ConOut, NULL, fmt, args);
  618. va_end (args);
  619. return back;
  620. }
  621. STATIC
  622. VOID
  623. PFLUSH (
  624. IN OUT PRINT_STATE *ps
  625. )
  626. {
  627. *ps->Pos = 0;
  628. if (IsLocalPrint(ps->Output))
  629. ps->Output(ps->Context, ps->Buffer);
  630. else
  631. uefi_call_wrapper(ps->Output, 2, ps->Context, ps->Buffer);
  632. ps->Pos = ps->Buffer;
  633. }
  634. STATIC
  635. VOID
  636. PSETATTR (
  637. IN OUT PRINT_STATE *ps,
  638. IN UINTN Attr
  639. )
  640. {
  641. PFLUSH (ps);
  642. ps->RestoreAttr = ps->Attr;
  643. if (ps->SetAttr) {
  644. uefi_call_wrapper(ps->SetAttr, 2, ps->Context, Attr);
  645. }
  646. ps->Attr = Attr;
  647. }
  648. STATIC
  649. VOID
  650. PPUTC (
  651. IN OUT PRINT_STATE *ps,
  652. IN CHAR16 c
  653. )
  654. {
  655. // if this is a newline, add a carraige return
  656. if (c == '\n') {
  657. PPUTC (ps, '\r');
  658. }
  659. *ps->Pos = c;
  660. ps->Pos += 1;
  661. ps->Len += 1;
  662. // if at the end of the buffer, flush it
  663. if (ps->Pos >= ps->End) {
  664. PFLUSH(ps);
  665. }
  666. }
  667. STATIC
  668. CHAR16
  669. PGETC (
  670. IN POINTER *p
  671. )
  672. {
  673. CHAR16 c;
  674. c = p->Ascii ? p->pc[p->Index] : p->pw[p->Index];
  675. p->Index += 1;
  676. return c;
  677. }
  678. STATIC
  679. VOID
  680. PITEM (
  681. IN OUT PRINT_STATE *ps
  682. )
  683. {
  684. UINTN Len, i;
  685. PRINT_ITEM *Item;
  686. CHAR16 c;
  687. // Get the length of the item
  688. Item = ps->Item;
  689. Item->Item.Index = 0;
  690. while (Item->Item.Index < Item->FieldWidth) {
  691. c = PGETC(&Item->Item);
  692. if (!c) {
  693. Item->Item.Index -= 1;
  694. break;
  695. }
  696. }
  697. Len = Item->Item.Index;
  698. // if there is no item field width, use the items width
  699. if (Item->FieldWidth == (UINTN) -1) {
  700. Item->FieldWidth = Len;
  701. }
  702. // if item is larger then width, update width
  703. if (Len > Item->Width) {
  704. Item->Width = Len;
  705. }
  706. // if pad field before, add pad char
  707. if (Item->PadBefore) {
  708. for (i=Item->Width; i < Item->FieldWidth; i+=1) {
  709. PPUTC (ps, ' ');
  710. }
  711. }
  712. // pad item
  713. for (i=Len; i < Item->Width; i++) {
  714. PPUTC (ps, Item->Pad);
  715. }
  716. // add the item
  717. Item->Item.Index=0;
  718. while (Item->Item.Index < Len) {
  719. PPUTC (ps, PGETC(&Item->Item));
  720. }
  721. // If pad at the end, add pad char
  722. if (!Item->PadBefore) {
  723. for (i=Item->Width; i < Item->FieldWidth; i+=1) {
  724. PPUTC (ps, ' ');
  725. }
  726. }
  727. }
  728. STATIC
  729. UINTN
  730. _Print (
  731. IN PRINT_STATE *ps
  732. )
  733. /*++
  734. Routine Description:
  735. %w.lF - w = width
  736. l = field width
  737. F = format of arg
  738. Args F:
  739. 0 - pad with zeros
  740. - - justify on left (default is on right)
  741. , - add comma's to field
  742. * - width provided on stack
  743. n - Set output attribute to normal (for this field only)
  744. h - Set output attribute to highlight (for this field only)
  745. e - Set output attribute to error (for this field only)
  746. l - Value is 64 bits
  747. a - ascii string
  748. s - unicode string
  749. X - fixed 8 byte value in hex
  750. x - hex value
  751. d - value as signed decimal
  752. u - value as unsigned decimal
  753. f - value as floating point
  754. c - Unicode char
  755. t - EFI time structure
  756. g - Pointer to GUID
  757. r - EFI status code (result code)
  758. D - pointer to Device Path with normal ending.
  759. N - Set output attribute to normal
  760. H - Set output attribute to highlight
  761. E - Set output attribute to error
  762. % - Print a %
  763. Arguments:
  764. SystemTable - The system table
  765. Returns:
  766. Number of charactors written
  767. --*/
  768. {
  769. CHAR16 c;
  770. UINTN Attr;
  771. PRINT_ITEM Item;
  772. CHAR16 Buffer[PRINT_STRING_LEN];
  773. ps->Len = 0;
  774. ps->Buffer = Buffer;
  775. ps->Pos = Buffer;
  776. ps->End = Buffer + PRINT_STRING_LEN - 1;
  777. ps->Item = &Item;
  778. ps->fmt.Index = 0;
  779. while ((c = PGETC(&ps->fmt))) {
  780. if (c != '%') {
  781. PPUTC ( ps, c );
  782. continue;
  783. }
  784. // setup for new item
  785. Item.FieldWidth = (UINTN) -1;
  786. Item.Width = 0;
  787. Item.WidthParse = &Item.Width;
  788. Item.Pad = ' ';
  789. Item.PadBefore = TRUE;
  790. Item.Comma = FALSE;
  791. Item.Long = FALSE;
  792. Item.Item.Ascii = FALSE;
  793. Item.Item.pw = NULL;
  794. ps->RestoreAttr = 0;
  795. Attr = 0;
  796. while ((c = PGETC(&ps->fmt))) {
  797. switch (c) {
  798. case '%':
  799. //
  800. // %% -> %
  801. //
  802. Item.Scratch[0] = '%';
  803. Item.Scratch[1] = 0;
  804. Item.Item.pw = Item.Scratch;
  805. break;
  806. case '0':
  807. Item.Pad = '0';
  808. break;
  809. case '-':
  810. Item.PadBefore = FALSE;
  811. break;
  812. case ',':
  813. Item.Comma = TRUE;
  814. break;
  815. case '.':
  816. Item.WidthParse = &Item.FieldWidth;
  817. break;
  818. case '*':
  819. *Item.WidthParse = va_arg(ps->args, UINTN);
  820. break;
  821. case '1':
  822. case '2':
  823. case '3':
  824. case '4':
  825. case '5':
  826. case '6':
  827. case '7':
  828. case '8':
  829. case '9':
  830. *Item.WidthParse = 0;
  831. do {
  832. *Item.WidthParse = *Item.WidthParse * 10 + c - '0';
  833. c = PGETC(&ps->fmt);
  834. } while (c >= '0' && c <= '9') ;
  835. ps->fmt.Index -= 1;
  836. break;
  837. case 'a':
  838. Item.Item.pc = va_arg(ps->args, CHAR8 *);
  839. Item.Item.Ascii = TRUE;
  840. if (!Item.Item.pc) {
  841. Item.Item.pc = (CHAR8 *)"(null)";
  842. }
  843. break;
  844. case 's':
  845. Item.Item.pw = va_arg(ps->args, CHAR16 *);
  846. if (!Item.Item.pw) {
  847. Item.Item.pw = L"(null)";
  848. }
  849. break;
  850. case 'c':
  851. Item.Scratch[0] = (CHAR16) va_arg(ps->args, UINTN);
  852. Item.Scratch[1] = 0;
  853. Item.Item.pw = Item.Scratch;
  854. break;
  855. case 'l':
  856. Item.Long = TRUE;
  857. break;
  858. case 'X':
  859. Item.Width = Item.Long ? 16 : 8;
  860. Item.Pad = '0';
  861. #if __GNUC__ >= 7
  862. __attribute__ ((fallthrough));
  863. #endif
  864. case 'x':
  865. ValueToHex (
  866. Item.Scratch,
  867. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  868. );
  869. Item.Item.pw = Item.Scratch;
  870. break;
  871. case 'g':
  872. GuidToString (Item.Scratch, va_arg(ps->args, EFI_GUID *));
  873. Item.Item.pw = Item.Scratch;
  874. break;
  875. case 'u':
  876. ValueToString (
  877. Item.Scratch,
  878. Item.Comma,
  879. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  880. );
  881. Item.Item.pw = Item.Scratch;
  882. break;
  883. case 'd':
  884. ValueToString (
  885. Item.Scratch,
  886. Item.Comma,
  887. Item.Long ? va_arg(ps->args, INT64) : va_arg(ps->args, INT32)
  888. );
  889. Item.Item.pw = Item.Scratch;
  890. break;
  891. case 'D':
  892. {
  893. EFI_DEVICE_PATH *dp = va_arg(ps->args, EFI_DEVICE_PATH *);
  894. CHAR16 *dpstr = DevicePathToStr(dp);
  895. StrnCpy(Item.Scratch, dpstr, PRINT_ITEM_BUFFER_LEN);
  896. Item.Scratch[PRINT_ITEM_BUFFER_LEN-1] = L'\0';
  897. FreePool(dpstr);
  898. Item.Item.pw = Item.Scratch;
  899. break;
  900. }
  901. case 'f':
  902. FloatToString (
  903. Item.Scratch,
  904. Item.Comma,
  905. va_arg(ps->args, double)
  906. );
  907. Item.Item.pw = Item.Scratch;
  908. break;
  909. case 't':
  910. TimeToString (Item.Scratch, va_arg(ps->args, EFI_TIME *));
  911. Item.Item.pw = Item.Scratch;
  912. break;
  913. case 'r':
  914. StatusToString (Item.Scratch, va_arg(ps->args, EFI_STATUS));
  915. Item.Item.pw = Item.Scratch;
  916. break;
  917. case 'n':
  918. PSETATTR(ps, ps->AttrNorm);
  919. break;
  920. case 'h':
  921. PSETATTR(ps, ps->AttrHighlight);
  922. break;
  923. case 'e':
  924. PSETATTR(ps, ps->AttrError);
  925. break;
  926. case 'N':
  927. Attr = ps->AttrNorm;
  928. break;
  929. case 'H':
  930. Attr = ps->AttrHighlight;
  931. break;
  932. case 'E':
  933. Attr = ps->AttrError;
  934. break;
  935. default:
  936. Item.Scratch[0] = '?';
  937. Item.Scratch[1] = 0;
  938. Item.Item.pw = Item.Scratch;
  939. break;
  940. }
  941. // if we have an Item
  942. if (Item.Item.pw) {
  943. PITEM (ps);
  944. break;
  945. }
  946. // if we have an Attr set
  947. if (Attr) {
  948. PSETATTR(ps, Attr);
  949. ps->RestoreAttr = 0;
  950. break;
  951. }
  952. }
  953. if (ps->RestoreAttr) {
  954. PSETATTR(ps, ps->RestoreAttr);
  955. }
  956. }
  957. // Flush buffer
  958. PFLUSH (ps);
  959. return ps->Len;
  960. }
  961. STATIC CHAR8 Hex[] = {'0','1','2','3','4','5','6','7',
  962. '8','9','A','B','C','D','E','F'};
  963. VOID
  964. ValueToHex (
  965. IN CHAR16 *Buffer,
  966. IN UINT64 v
  967. )
  968. {
  969. CHAR8 str[30], *p1;
  970. CHAR16 *p2;
  971. if (!v) {
  972. Buffer[0] = '0';
  973. Buffer[1] = 0;
  974. return ;
  975. }
  976. p1 = str;
  977. p2 = Buffer;
  978. while (v) {
  979. // Without the cast, the MSVC compiler may insert a reference to __allmull
  980. *(p1++) = Hex[(UINTN)(v & 0xf)];
  981. v = RShiftU64 (v, 4);
  982. }
  983. while (p1 != str) {
  984. *(p2++) = *(--p1);
  985. }
  986. *p2 = 0;
  987. }
  988. VOID
  989. ValueToString (
  990. IN CHAR16 *Buffer,
  991. IN BOOLEAN Comma,
  992. IN INT64 v
  993. )
  994. {
  995. STATIC CHAR8 ca[] = { 3, 1, 2 };
  996. CHAR8 str[40], *p1;
  997. CHAR16 *p2;
  998. UINTN c, r;
  999. if (!v) {
  1000. Buffer[0] = '0';
  1001. Buffer[1] = 0;
  1002. return ;
  1003. }
  1004. p1 = str;
  1005. p2 = Buffer;
  1006. if (v < 0) {
  1007. *(p2++) = '-';
  1008. v = -v;
  1009. }
  1010. while (v) {
  1011. v = (INT64)DivU64x32 ((UINT64)v, 10, &r);
  1012. *(p1++) = (CHAR8)r + '0';
  1013. }
  1014. c = (Comma ? ca[(p1 - str) % 3] : 999) + 1;
  1015. while (p1 != str) {
  1016. c -= 1;
  1017. if (!c) {
  1018. *(p2++) = ',';
  1019. c = 3;
  1020. }
  1021. *(p2++) = *(--p1);
  1022. }
  1023. *p2 = 0;
  1024. }
  1025. VOID
  1026. FloatToString (
  1027. IN CHAR16 *Buffer,
  1028. IN BOOLEAN Comma,
  1029. IN double v
  1030. )
  1031. {
  1032. /*
  1033. * Integer part.
  1034. */
  1035. INTN i = (INTN)v;
  1036. ValueToString(Buffer, Comma, i);
  1037. /*
  1038. * Decimal point.
  1039. */
  1040. UINTN x = StrLen(Buffer);
  1041. Buffer[x] = L'.';
  1042. x++;
  1043. /*
  1044. * Keep fractional part.
  1045. */
  1046. float f = (float)(v - i);
  1047. if (f < 0) f = -f;
  1048. /*
  1049. * Leading fractional zeroes.
  1050. */
  1051. f *= 10.0;
  1052. while ( (f != 0)
  1053. && ((INTN)f == 0))
  1054. {
  1055. Buffer[x] = L'0';
  1056. x++;
  1057. f *= 10.0;
  1058. }
  1059. /*
  1060. * Fractional digits.
  1061. */
  1062. while ((float)(INTN)f != f)
  1063. {
  1064. f *= 10;
  1065. }
  1066. ValueToString(Buffer + x, FALSE, (INTN)f);
  1067. return;
  1068. }
  1069. VOID
  1070. TimeToString (
  1071. OUT CHAR16 *Buffer,
  1072. IN EFI_TIME *Time
  1073. )
  1074. {
  1075. UINTN Hour, Year;
  1076. CHAR16 AmPm;
  1077. AmPm = 'a';
  1078. Hour = Time->Hour;
  1079. if (Time->Hour == 0) {
  1080. Hour = 12;
  1081. } else if (Time->Hour >= 12) {
  1082. AmPm = 'p';
  1083. if (Time->Hour >= 13) {
  1084. Hour -= 12;
  1085. }
  1086. }
  1087. Year = Time->Year % 100;
  1088. // bugbug: for now just print it any old way
  1089. SPrint (Buffer, 0, L"%02d/%02d/%02d %02d:%02d%c",
  1090. Time->Month,
  1091. Time->Day,
  1092. Year,
  1093. Hour,
  1094. Time->Minute,
  1095. AmPm
  1096. );
  1097. }
  1098. VOID
  1099. DumpHex (
  1100. IN UINTN Indent,
  1101. IN UINTN Offset,
  1102. IN UINTN DataSize,
  1103. IN VOID *UserData
  1104. )
  1105. {
  1106. CHAR8 *Data, Val[50], Str[20], c;
  1107. UINTN Size, Index;
  1108. UINTN ScreenCount;
  1109. UINTN TempColumn;
  1110. UINTN ScreenSize;
  1111. CHAR16 ReturnStr[1];
  1112. uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &TempColumn, &ScreenSize);
  1113. ScreenCount = 0;
  1114. ScreenSize -= 2;
  1115. Data = UserData;
  1116. while (DataSize) {
  1117. Size = 16;
  1118. if (Size > DataSize) {
  1119. Size = DataSize;
  1120. }
  1121. for (Index=0; Index < Size; Index += 1) {
  1122. c = Data[Index];
  1123. Val[Index*3+0] = Hex[c>>4];
  1124. Val[Index*3+1] = Hex[c&0xF];
  1125. Val[Index*3+2] = (Index == 7)?'-':' ';
  1126. Str[Index] = (c < ' ' || c > 'z') ? '.' : c;
  1127. }
  1128. Val[Index*3] = 0;
  1129. Str[Index] = 0;
  1130. Print (L"%*a%X: %-.48a *%a*\n", Indent, "", Offset, Val, Str);
  1131. Data += Size;
  1132. Offset += Size;
  1133. DataSize -= Size;
  1134. ScreenCount++;
  1135. if (ScreenCount >= ScreenSize && ScreenSize != 0) {
  1136. //
  1137. // If ScreenSize == 0 we have the console redirected so don't
  1138. // block updates
  1139. //
  1140. ScreenCount = 0;
  1141. Print (L"Press Enter to continue :");
  1142. Input (L"", ReturnStr, sizeof(ReturnStr)/sizeof(CHAR16));
  1143. Print (L"\n");
  1144. }
  1145. }
  1146. }