print.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. CHAR16 *pw;
  40. 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 CHAR16 *fmt,
  91. IN 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 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 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 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 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 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 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 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 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 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 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 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 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 CHAR16 *fmt,
  569. IN 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 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. c - Unicode char
  754. t - EFI time structure
  755. g - Pointer to GUID
  756. r - EFI status code (result code)
  757. N - Set output attribute to normal
  758. H - Set output attribute to highlight
  759. E - Set output attribute to error
  760. % - Print a %
  761. Arguments:
  762. SystemTable - The system table
  763. Returns:
  764. Number of charactors written
  765. --*/
  766. {
  767. CHAR16 c;
  768. UINTN Attr;
  769. PRINT_ITEM Item;
  770. CHAR16 Buffer[PRINT_STRING_LEN];
  771. ps->Len = 0;
  772. ps->Buffer = Buffer;
  773. ps->Pos = Buffer;
  774. ps->End = Buffer + PRINT_STRING_LEN - 1;
  775. ps->Item = &Item;
  776. ps->fmt.Index = 0;
  777. while ((c = PGETC(&ps->fmt))) {
  778. if (c != '%') {
  779. PPUTC ( ps, c );
  780. continue;
  781. }
  782. // setup for new item
  783. Item.FieldWidth = (UINTN) -1;
  784. Item.Width = 0;
  785. Item.WidthParse = &Item.Width;
  786. Item.Pad = ' ';
  787. Item.PadBefore = TRUE;
  788. Item.Comma = FALSE;
  789. Item.Long = FALSE;
  790. Item.Item.Ascii = FALSE;
  791. Item.Item.pw = NULL;
  792. ps->RestoreAttr = 0;
  793. Attr = 0;
  794. while ((c = PGETC(&ps->fmt))) {
  795. switch (c) {
  796. case '%':
  797. //
  798. // %% -> %
  799. //
  800. Item.Item.pw = Item.Scratch;
  801. Item.Item.pw[0] = '%';
  802. Item.Item.pw[1] = 0;
  803. break;
  804. case '0':
  805. Item.Pad = '0';
  806. break;
  807. case '-':
  808. Item.PadBefore = FALSE;
  809. break;
  810. case ',':
  811. Item.Comma = TRUE;
  812. break;
  813. case '.':
  814. Item.WidthParse = &Item.FieldWidth;
  815. break;
  816. case '*':
  817. *Item.WidthParse = va_arg(ps->args, UINTN);
  818. break;
  819. case '1':
  820. case '2':
  821. case '3':
  822. case '4':
  823. case '5':
  824. case '6':
  825. case '7':
  826. case '8':
  827. case '9':
  828. *Item.WidthParse = 0;
  829. do {
  830. *Item.WidthParse = *Item.WidthParse * 10 + c - '0';
  831. c = PGETC(&ps->fmt);
  832. } while (c >= '0' && c <= '9') ;
  833. ps->fmt.Index -= 1;
  834. break;
  835. case 'a':
  836. Item.Item.pc = va_arg(ps->args, CHAR8 *);
  837. Item.Item.Ascii = TRUE;
  838. if (!Item.Item.pc) {
  839. Item.Item.pc = (CHAR8 *)"(null)";
  840. }
  841. break;
  842. case 's':
  843. Item.Item.pw = va_arg(ps->args, CHAR16 *);
  844. if (!Item.Item.pw) {
  845. Item.Item.pw = L"(null)";
  846. }
  847. break;
  848. case 'c':
  849. Item.Item.pw = Item.Scratch;
  850. Item.Item.pw[0] = (CHAR16) va_arg(ps->args, UINTN);
  851. Item.Item.pw[1] = 0;
  852. break;
  853. case 'l':
  854. Item.Long = TRUE;
  855. break;
  856. case 'X':
  857. Item.Width = Item.Long ? 16 : 8;
  858. Item.Pad = '0';
  859. case 'x':
  860. Item.Item.pw = Item.Scratch;
  861. ValueToHex (
  862. Item.Item.pw,
  863. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  864. );
  865. break;
  866. case 'g':
  867. Item.Item.pw = Item.Scratch;
  868. GuidToString (Item.Item.pw, va_arg(ps->args, EFI_GUID *));
  869. break;
  870. case 'u':
  871. Item.Item.pw = Item.Scratch;
  872. ValueToString (
  873. Item.Item.pw,
  874. Item.Comma,
  875. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  876. );
  877. break;
  878. case 'd':
  879. Item.Item.pw = Item.Scratch;
  880. ValueToString (
  881. Item.Item.pw,
  882. Item.Comma,
  883. Item.Long ? va_arg(ps->args, INT64) : va_arg(ps->args, INT32)
  884. );
  885. break;
  886. case 't':
  887. Item.Item.pw = Item.Scratch;
  888. TimeToString (Item.Item.pw, va_arg(ps->args, EFI_TIME *));
  889. break;
  890. case 'r':
  891. Item.Item.pw = Item.Scratch;
  892. StatusToString (Item.Item.pw, va_arg(ps->args, EFI_STATUS));
  893. break;
  894. case 'n':
  895. PSETATTR(ps, ps->AttrNorm);
  896. break;
  897. case 'h':
  898. PSETATTR(ps, ps->AttrHighlight);
  899. break;
  900. case 'e':
  901. PSETATTR(ps, ps->AttrError);
  902. break;
  903. case 'N':
  904. Attr = ps->AttrNorm;
  905. break;
  906. case 'H':
  907. Attr = ps->AttrHighlight;
  908. break;
  909. case 'E':
  910. Attr = ps->AttrError;
  911. break;
  912. default:
  913. Item.Item.pw = Item.Scratch;
  914. Item.Item.pw[0] = '?';
  915. Item.Item.pw[1] = 0;
  916. break;
  917. }
  918. // if we have an Item
  919. if (Item.Item.pw) {
  920. PITEM (ps);
  921. break;
  922. }
  923. // if we have an Attr set
  924. if (Attr) {
  925. PSETATTR(ps, Attr);
  926. ps->RestoreAttr = 0;
  927. break;
  928. }
  929. }
  930. if (ps->RestoreAttr) {
  931. PSETATTR(ps, ps->RestoreAttr);
  932. }
  933. }
  934. // Flush buffer
  935. PFLUSH (ps);
  936. return ps->Len;
  937. }
  938. STATIC CHAR8 Hex[] = {'0','1','2','3','4','5','6','7',
  939. '8','9','A','B','C','D','E','F'};
  940. VOID
  941. ValueToHex (
  942. IN CHAR16 *Buffer,
  943. IN UINT64 v
  944. )
  945. {
  946. CHAR8 str[30], *p1;
  947. CHAR16 *p2;
  948. if (!v) {
  949. Buffer[0] = '0';
  950. Buffer[1] = 0;
  951. return ;
  952. }
  953. p1 = str;
  954. p2 = Buffer;
  955. while (v) {
  956. *(p1++) = Hex[v & 0xf];
  957. v = RShiftU64 (v, 4);
  958. }
  959. while (p1 != str) {
  960. *(p2++) = *(--p1);
  961. }
  962. *p2 = 0;
  963. }
  964. VOID
  965. ValueToString (
  966. IN CHAR16 *Buffer,
  967. IN BOOLEAN Comma,
  968. IN INT64 v
  969. )
  970. {
  971. STATIC CHAR8 ca[] = { 3, 1, 2 };
  972. CHAR8 str[40], *p1;
  973. CHAR16 *p2;
  974. UINTN c, r;
  975. if (!v) {
  976. Buffer[0] = '0';
  977. Buffer[1] = 0;
  978. return ;
  979. }
  980. p1 = str;
  981. p2 = Buffer;
  982. if (v < 0) {
  983. *(p2++) = '-';
  984. v = -v;
  985. }
  986. while (v) {
  987. v = (INT64)DivU64x32 ((UINT64)v, 10, &r);
  988. *(p1++) = (CHAR8)r + '0';
  989. }
  990. c = (Comma ? ca[(p1 - str) % 3] : 999) + 1;
  991. while (p1 != str) {
  992. c -= 1;
  993. if (!c) {
  994. *(p2++) = ',';
  995. c = 3;
  996. }
  997. *(p2++) = *(--p1);
  998. }
  999. *p2 = 0;
  1000. }
  1001. VOID
  1002. TimeToString (
  1003. OUT CHAR16 *Buffer,
  1004. IN EFI_TIME *Time
  1005. )
  1006. {
  1007. UINTN Hour, Year;
  1008. CHAR16 AmPm;
  1009. AmPm = 'a';
  1010. Hour = Time->Hour;
  1011. if (Time->Hour == 0) {
  1012. Hour = 12;
  1013. } else if (Time->Hour >= 12) {
  1014. AmPm = 'p';
  1015. if (Time->Hour >= 13) {
  1016. Hour -= 12;
  1017. }
  1018. }
  1019. Year = Time->Year % 100;
  1020. // bugbug: for now just print it any old way
  1021. SPrint (Buffer, 0, L"%02d/%02d/%02d %02d:%02d%c",
  1022. Time->Month,
  1023. Time->Day,
  1024. Year,
  1025. Hour,
  1026. Time->Minute,
  1027. AmPm
  1028. );
  1029. }
  1030. VOID
  1031. DumpHex (
  1032. IN UINTN Indent,
  1033. IN UINTN Offset,
  1034. IN UINTN DataSize,
  1035. IN VOID *UserData
  1036. )
  1037. {
  1038. CHAR8 *Data, Val[50], Str[20], c;
  1039. UINTN Size, Index;
  1040. UINTN ScreenCount;
  1041. UINTN TempColumn;
  1042. UINTN ScreenSize;
  1043. CHAR16 ReturnStr[1];
  1044. uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &TempColumn, &ScreenSize);
  1045. ScreenCount = 0;
  1046. ScreenSize -= 2;
  1047. Data = UserData;
  1048. while (DataSize) {
  1049. Size = 16;
  1050. if (Size > DataSize) {
  1051. Size = DataSize;
  1052. }
  1053. for (Index=0; Index < Size; Index += 1) {
  1054. c = Data[Index];
  1055. Val[Index*3+0] = Hex[c>>4];
  1056. Val[Index*3+1] = Hex[c&0xF];
  1057. Val[Index*3+2] = (Index == 7)?'-':' ';
  1058. Str[Index] = (c < ' ' || c > 'z') ? '.' : c;
  1059. }
  1060. Val[Index*3] = 0;
  1061. Str[Index] = 0;
  1062. Print (L"%*a%X: %-.48a *%a*\n", Indent, "", Offset, Val, Str);
  1063. Data += Size;
  1064. Offset += Size;
  1065. DataSize -= Size;
  1066. ScreenCount++;
  1067. if (ScreenCount >= ScreenSize && ScreenSize != 0) {
  1068. //
  1069. // If ScreenSize == 0 we have the console redirected so don't
  1070. // block updates
  1071. //
  1072. ScreenCount = 0;
  1073. Print (L"Press Enter to continue :");
  1074. Input (L"", ReturnStr, sizeof(ReturnStr)/sizeof(CHAR16));
  1075. Print (L"\n");
  1076. }
  1077. }
  1078. }