print.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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 UnicodeSPrint, 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. // Dispatch function for UnicodeSPrint, 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. UnicodeVSPrint (
  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. UnicodeSPrint (
  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 = UnicodeVSPrint(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. AsciiPrint (
  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. UINTN
  622. AsciiVSPrint (
  623. OUT CHAR8 *Str,
  624. IN UINTN StrSize,
  625. IN CONST CHAR8 *fmt,
  626. va_list args
  627. )
  628. /*++
  629. Routine Description:
  630. Prints a formatted ascii string to a buffer using a va_list
  631. Arguments:
  632. Str - Output buffer to print the formatted string into
  633. StrSize - Size of Str. String is truncated to this size.
  634. A size of 0 means there is no limit
  635. fmt - The format string
  636. args - va_list
  637. Returns:
  638. String length returned in buffer
  639. --*/
  640. // Use UnicodeVSPrint() and convert back to ASCII
  641. {
  642. CHAR16 *UnicodeStr, *UnicodeFmt;
  643. UINTN i, Len;
  644. UnicodeStr = AllocatePool(StrSize * sizeof(CHAR16));
  645. if (!UnicodeStr)
  646. return 0;
  647. UnicodeFmt = PoolPrint(L"%a", fmt);
  648. if (!UnicodeFmt) {
  649. FreePool(UnicodeStr);
  650. return 0;
  651. }
  652. Len = UnicodeVSPrint(UnicodeStr, StrSize, UnicodeFmt, args);
  653. FreePool(UnicodeFmt);
  654. // The strings are ASCII so just do a plain Unicode conversion
  655. for (i = 0; i < Len; i++)
  656. Str[i] = (CHAR8)UnicodeStr[i];
  657. Str[Len] = 0;
  658. FreePool(UnicodeStr);
  659. return Len;
  660. }
  661. STATIC
  662. VOID
  663. PFLUSH (
  664. IN OUT PRINT_STATE *ps
  665. )
  666. {
  667. *ps->Pos = 0;
  668. if (IsLocalPrint(ps->Output))
  669. ps->Output(ps->Context, ps->Buffer);
  670. else
  671. uefi_call_wrapper(ps->Output, 2, ps->Context, ps->Buffer);
  672. ps->Pos = ps->Buffer;
  673. }
  674. STATIC
  675. VOID
  676. PSETATTR (
  677. IN OUT PRINT_STATE *ps,
  678. IN UINTN Attr
  679. )
  680. {
  681. PFLUSH (ps);
  682. ps->RestoreAttr = ps->Attr;
  683. if (ps->SetAttr) {
  684. uefi_call_wrapper(ps->SetAttr, 2, ps->Context, Attr);
  685. }
  686. ps->Attr = Attr;
  687. }
  688. STATIC
  689. VOID
  690. PPUTC (
  691. IN OUT PRINT_STATE *ps,
  692. IN CHAR16 c
  693. )
  694. {
  695. // if this is a newline, add a carraige return
  696. if (c == '\n') {
  697. PPUTC (ps, '\r');
  698. }
  699. *ps->Pos = c;
  700. ps->Pos += 1;
  701. ps->Len += 1;
  702. // if at the end of the buffer, flush it
  703. if (ps->Pos >= ps->End) {
  704. PFLUSH(ps);
  705. }
  706. }
  707. STATIC
  708. CHAR16
  709. PGETC (
  710. IN POINTER *p
  711. )
  712. {
  713. CHAR16 c;
  714. c = p->Ascii ? p->pc[p->Index] : p->pw[p->Index];
  715. p->Index += 1;
  716. return c;
  717. }
  718. STATIC
  719. VOID
  720. PITEM (
  721. IN OUT PRINT_STATE *ps
  722. )
  723. {
  724. UINTN Len, i;
  725. PRINT_ITEM *Item;
  726. CHAR16 c;
  727. // Get the length of the item
  728. Item = ps->Item;
  729. Item->Item.Index = 0;
  730. while (Item->Item.Index < Item->FieldWidth) {
  731. c = PGETC(&Item->Item);
  732. if (!c) {
  733. Item->Item.Index -= 1;
  734. break;
  735. }
  736. }
  737. Len = Item->Item.Index;
  738. // if there is no item field width, use the items width
  739. if (Item->FieldWidth == (UINTN) -1) {
  740. Item->FieldWidth = Len;
  741. }
  742. // if item is larger then width, update width
  743. if (Len > Item->Width) {
  744. Item->Width = Len;
  745. }
  746. // if pad field before, add pad char
  747. if (Item->PadBefore) {
  748. for (i=Item->Width; i < Item->FieldWidth; i+=1) {
  749. PPUTC (ps, ' ');
  750. }
  751. }
  752. // pad item
  753. for (i=Len; i < Item->Width; i++) {
  754. PPUTC (ps, Item->Pad);
  755. }
  756. // add the item
  757. Item->Item.Index=0;
  758. while (Item->Item.Index < Len) {
  759. PPUTC (ps, PGETC(&Item->Item));
  760. }
  761. // If pad at the end, add pad char
  762. if (!Item->PadBefore) {
  763. for (i=Item->Width; i < Item->FieldWidth; i+=1) {
  764. PPUTC (ps, ' ');
  765. }
  766. }
  767. }
  768. STATIC
  769. UINTN
  770. _Print (
  771. IN PRINT_STATE *ps
  772. )
  773. /*++
  774. Routine Description:
  775. %w.lF - w = width
  776. l = field width
  777. F = format of arg
  778. Args F:
  779. 0 - pad with zeros
  780. - - justify on left (default is on right)
  781. , - add comma's to field
  782. * - width provided on stack
  783. n - Set output attribute to normal (for this field only)
  784. h - Set output attribute to highlight (for this field only)
  785. e - Set output attribute to error (for this field only)
  786. l - Value is 64 bits
  787. a - ascii string
  788. s - unicode string
  789. X - fixed 8 byte value in hex
  790. x - hex value
  791. d - value as signed decimal
  792. u - value as unsigned decimal
  793. f - value as floating point
  794. c - Unicode char
  795. t - EFI time structure
  796. g - Pointer to GUID
  797. r - EFI status code (result code)
  798. D - pointer to Device Path with normal ending.
  799. N - Set output attribute to normal
  800. H - Set output attribute to highlight
  801. E - Set output attribute to error
  802. % - Print a %
  803. Arguments:
  804. SystemTable - The system table
  805. Returns:
  806. Number of charactors written
  807. --*/
  808. {
  809. CHAR16 c;
  810. UINTN Attr;
  811. PRINT_ITEM Item;
  812. CHAR16 Buffer[PRINT_STRING_LEN];
  813. ps->Len = 0;
  814. ps->Buffer = Buffer;
  815. ps->Pos = Buffer;
  816. ps->End = Buffer + PRINT_STRING_LEN - 1;
  817. ps->Item = &Item;
  818. ps->fmt.Index = 0;
  819. while ((c = PGETC(&ps->fmt))) {
  820. if (c != '%') {
  821. PPUTC ( ps, c );
  822. continue;
  823. }
  824. // setup for new item
  825. Item.FieldWidth = (UINTN) -1;
  826. Item.Width = 0;
  827. Item.WidthParse = &Item.Width;
  828. Item.Pad = ' ';
  829. Item.PadBefore = TRUE;
  830. Item.Comma = FALSE;
  831. Item.Long = FALSE;
  832. Item.Item.Ascii = FALSE;
  833. Item.Item.pw = NULL;
  834. ps->RestoreAttr = 0;
  835. Attr = 0;
  836. while ((c = PGETC(&ps->fmt))) {
  837. switch (c) {
  838. case '%':
  839. //
  840. // %% -> %
  841. //
  842. Item.Scratch[0] = '%';
  843. Item.Scratch[1] = 0;
  844. Item.Item.pw = Item.Scratch;
  845. break;
  846. case ',':
  847. Item.Comma = TRUE;
  848. break;
  849. case '-':
  850. Item.PadBefore = FALSE;
  851. break;
  852. case '*':
  853. *Item.WidthParse = va_arg(ps->args, UINTN);
  854. break;
  855. case '.':
  856. Item.WidthParse = &Item.FieldWidth;
  857. break;
  858. case '0':
  859. Item.Pad = '0';
  860. break;
  861. case '1':
  862. case '2':
  863. case '3':
  864. case '4':
  865. case '5':
  866. case '6':
  867. case '7':
  868. case '8':
  869. case '9':
  870. *Item.WidthParse = 0;
  871. do {
  872. *Item.WidthParse = *Item.WidthParse * 10 + c - '0';
  873. c = PGETC(&ps->fmt);
  874. } while (c >= '0' && c <= '9') ;
  875. ps->fmt.Index -= 1;
  876. break;
  877. case 'a':
  878. Item.Item.pc = va_arg(ps->args, CHAR8 *);
  879. Item.Item.Ascii = TRUE;
  880. if (!Item.Item.pc) {
  881. Item.Item.pc = (CHAR8 *)"(null)";
  882. }
  883. break;
  884. case 'c':
  885. Item.Scratch[0] = (CHAR16) va_arg(ps->args, UINTN);
  886. Item.Scratch[1] = 0;
  887. Item.Item.pw = Item.Scratch;
  888. break;
  889. case 'D':
  890. {
  891. EFI_DEVICE_PATH *dp = va_arg(ps->args, EFI_DEVICE_PATH *);
  892. CHAR16 *dpstr = DevicePathToStr(dp);
  893. StrnCpy(Item.Scratch, dpstr, PRINT_ITEM_BUFFER_LEN);
  894. Item.Scratch[PRINT_ITEM_BUFFER_LEN-1] = L'\0';
  895. FreePool(dpstr);
  896. Item.Item.pw = Item.Scratch;
  897. break;
  898. }
  899. case 'd':
  900. ValueToString (
  901. Item.Scratch,
  902. Item.Comma,
  903. Item.Long ? va_arg(ps->args, INT64) : va_arg(ps->args, INT32)
  904. );
  905. Item.Item.pw = Item.Scratch;
  906. break;
  907. case 'E':
  908. Attr = ps->AttrError;
  909. break;
  910. case 'e':
  911. PSETATTR(ps, ps->AttrError);
  912. break;
  913. case 'f':
  914. FloatToString (
  915. Item.Scratch,
  916. Item.Comma,
  917. va_arg(ps->args, double)
  918. );
  919. Item.Item.pw = Item.Scratch;
  920. break;
  921. case 'g':
  922. GuidToString (Item.Scratch, va_arg(ps->args, EFI_GUID *));
  923. Item.Item.pw = Item.Scratch;
  924. break;
  925. case 'H':
  926. Attr = ps->AttrHighlight;
  927. break;
  928. case 'h':
  929. PSETATTR(ps, ps->AttrHighlight);
  930. break;
  931. case 'l':
  932. Item.Long = TRUE;
  933. break;
  934. case 'N':
  935. Attr = ps->AttrNorm;
  936. break;
  937. case 'n':
  938. PSETATTR(ps, ps->AttrNorm);
  939. break;
  940. case 'p':
  941. Item.Width = sizeof(void *) == (8 ? 16 : 8) + 2;
  942. Item.Pad = '0';
  943. Item.Scratch[0] = ' ';
  944. Item.Scratch[1] = ' ';
  945. ValueToHex (
  946. Item.Scratch+2,
  947. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  948. );
  949. Item.Scratch[0] = '0';
  950. Item.Scratch[1] = 'x';
  951. Item.Item.pw = Item.Scratch;
  952. break;
  953. case 'r':
  954. StatusToString (Item.Scratch, va_arg(ps->args, EFI_STATUS));
  955. Item.Item.pw = Item.Scratch;
  956. break;
  957. case 's':
  958. Item.Item.pw = va_arg(ps->args, CHAR16 *);
  959. if (!Item.Item.pw) {
  960. Item.Item.pw = L"(null)";
  961. }
  962. break;
  963. case 't':
  964. TimeToString (Item.Scratch, va_arg(ps->args, EFI_TIME *));
  965. Item.Item.pw = Item.Scratch;
  966. break;
  967. case 'u':
  968. ValueToString (
  969. Item.Scratch,
  970. Item.Comma,
  971. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  972. );
  973. Item.Item.pw = Item.Scratch;
  974. break;
  975. case 'X':
  976. Item.Width = Item.Long ? 16 : 8;
  977. Item.Pad = '0';
  978. #if __GNUC__ >= 7
  979. __attribute__ ((fallthrough));
  980. #endif
  981. case 'x':
  982. ValueToHex (
  983. Item.Scratch,
  984. Item.Long ? va_arg(ps->args, UINT64) : va_arg(ps->args, UINT32)
  985. );
  986. Item.Item.pw = Item.Scratch;
  987. break;
  988. default:
  989. Item.Scratch[0] = '?';
  990. Item.Scratch[1] = 0;
  991. Item.Item.pw = Item.Scratch;
  992. break;
  993. }
  994. // if we have an Item
  995. if (Item.Item.pw) {
  996. PITEM (ps);
  997. break;
  998. }
  999. // if we have an Attr set
  1000. if (Attr) {
  1001. PSETATTR(ps, Attr);
  1002. ps->RestoreAttr = 0;
  1003. break;
  1004. }
  1005. }
  1006. if (ps->RestoreAttr) {
  1007. PSETATTR(ps, ps->RestoreAttr);
  1008. }
  1009. }
  1010. // Flush buffer
  1011. PFLUSH (ps);
  1012. return ps->Len;
  1013. }
  1014. STATIC CHAR8 Hex[] = {'0','1','2','3','4','5','6','7',
  1015. '8','9','A','B','C','D','E','F'};
  1016. VOID
  1017. ValueToHex (
  1018. IN CHAR16 *Buffer,
  1019. IN UINT64 v
  1020. )
  1021. {
  1022. CHAR8 str[30], *p1;
  1023. CHAR16 *p2;
  1024. if (!v) {
  1025. Buffer[0] = '0';
  1026. Buffer[1] = 0;
  1027. return ;
  1028. }
  1029. p1 = str;
  1030. p2 = Buffer;
  1031. while (v) {
  1032. // Without the cast, the MSVC compiler may insert a reference to __allmull
  1033. *(p1++) = Hex[(UINTN)(v & 0xf)];
  1034. v = RShiftU64 (v, 4);
  1035. }
  1036. while (p1 != str) {
  1037. *(p2++) = *(--p1);
  1038. }
  1039. *p2 = 0;
  1040. }
  1041. VOID
  1042. ValueToString (
  1043. IN CHAR16 *Buffer,
  1044. IN BOOLEAN Comma,
  1045. IN INT64 v
  1046. )
  1047. {
  1048. STATIC CHAR8 ca[] = { 3, 1, 2 };
  1049. CHAR8 str[40], *p1;
  1050. CHAR16 *p2;
  1051. UINTN c, r;
  1052. if (!v) {
  1053. Buffer[0] = '0';
  1054. Buffer[1] = 0;
  1055. return ;
  1056. }
  1057. p1 = str;
  1058. p2 = Buffer;
  1059. if (v < 0) {
  1060. *(p2++) = '-';
  1061. v = -v;
  1062. }
  1063. while (v) {
  1064. v = (INT64)DivU64x32 ((UINT64)v, 10, &r);
  1065. *(p1++) = (CHAR8)r + '0';
  1066. }
  1067. c = (UINTN) (Comma ? ca[(p1 - str) % 3] : 999) + 1;
  1068. while (p1 != str) {
  1069. c -= 1;
  1070. if (!c) {
  1071. *(p2++) = ',';
  1072. c = 3;
  1073. }
  1074. *(p2++) = *(--p1);
  1075. }
  1076. *p2 = 0;
  1077. }
  1078. VOID
  1079. FloatToString (
  1080. IN CHAR16 *Buffer,
  1081. IN BOOLEAN Comma,
  1082. IN double v
  1083. )
  1084. {
  1085. /*
  1086. * Integer part.
  1087. */
  1088. INTN i = (INTN)v;
  1089. ValueToString(Buffer, Comma, i);
  1090. /*
  1091. * Decimal point.
  1092. */
  1093. UINTN x = StrLen(Buffer);
  1094. Buffer[x] = L'.';
  1095. x++;
  1096. /*
  1097. * Keep fractional part.
  1098. */
  1099. float f = (float)(v - i);
  1100. if (f < 0) f = -f;
  1101. /*
  1102. * Leading fractional zeroes.
  1103. */
  1104. f *= 10.0;
  1105. while ( (f != 0)
  1106. && ((INTN)f == 0))
  1107. {
  1108. Buffer[x] = L'0';
  1109. x++;
  1110. f *= 10.0;
  1111. }
  1112. /*
  1113. * Fractional digits.
  1114. */
  1115. while ((float)(INTN)f != f)
  1116. {
  1117. f *= 10;
  1118. }
  1119. ValueToString(Buffer + x, FALSE, (INTN)f);
  1120. return;
  1121. }
  1122. VOID
  1123. TimeToString (
  1124. OUT CHAR16 *Buffer,
  1125. IN EFI_TIME *Time
  1126. )
  1127. {
  1128. UINTN Hour, Year;
  1129. CHAR16 AmPm;
  1130. AmPm = 'a';
  1131. Hour = Time->Hour;
  1132. if (Time->Hour == 0) {
  1133. Hour = 12;
  1134. } else if (Time->Hour >= 12) {
  1135. AmPm = 'p';
  1136. if (Time->Hour >= 13) {
  1137. Hour -= 12;
  1138. }
  1139. }
  1140. Year = Time->Year % 100;
  1141. // bugbug: for now just print it any old way
  1142. UnicodeSPrint (Buffer, 0, L"%02d/%02d/%02d %02d:%02d%c",
  1143. Time->Month,
  1144. Time->Day,
  1145. Year,
  1146. Hour,
  1147. Time->Minute,
  1148. AmPm
  1149. );
  1150. }
  1151. VOID
  1152. DumpHex (
  1153. IN UINTN Indent,
  1154. IN UINTN Offset,
  1155. IN UINTN DataSize,
  1156. IN VOID *UserData
  1157. )
  1158. {
  1159. CHAR8 *Data, Val[50], Str[20], c;
  1160. UINTN Size, Index;
  1161. UINTN ScreenCount;
  1162. UINTN TempColumn;
  1163. UINTN ScreenSize;
  1164. CHAR16 ReturnStr[1];
  1165. uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &TempColumn, &ScreenSize);
  1166. ScreenCount = 0;
  1167. ScreenSize -= 2;
  1168. Data = UserData;
  1169. while (DataSize) {
  1170. Size = 16;
  1171. if (Size > DataSize) {
  1172. Size = DataSize;
  1173. }
  1174. for (Index=0; Index < Size; Index += 1) {
  1175. c = Data[Index];
  1176. Val[Index*3+0] = Hex[c>>4];
  1177. Val[Index*3+1] = Hex[c&0xF];
  1178. Val[Index*3+2] = (Index == 7)?'-':' ';
  1179. Str[Index] = (c < ' ' || c > 'z') ? '.' : c;
  1180. }
  1181. Val[Index*3] = 0;
  1182. Str[Index] = 0;
  1183. Print (L"%*a%X: %-.48a *%a*\n", Indent, "", Offset, Val, Str);
  1184. Data += Size;
  1185. Offset += Size;
  1186. DataSize -= Size;
  1187. ScreenCount++;
  1188. if (ScreenCount >= ScreenSize && ScreenSize != 0) {
  1189. //
  1190. // If ScreenSize == 0 we have the console redirected so don't
  1191. // block updates
  1192. //
  1193. ScreenCount = 0;
  1194. Print (L"Press Enter to continue :");
  1195. Input (L"", ReturnStr, sizeof(ReturnStr)/sizeof(CHAR16));
  1196. Print (L"\n");
  1197. }
  1198. }
  1199. }