]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/EdkDxePrintLib/PrintLib.c
Code Scrub for EdkDxePrintLib.
[mirror_edk2.git] / MdeModulePkg / Library / EdkDxePrintLib / PrintLib.c
1 /** @file
2 Instance of Print Library based on EFI_PRINT2_PROTOCOL.
3
4 Implement the print library instance by wrap the interface
5 provided in the Print protocol. This protocol is defined as the internal
6 protocol related to this implementation, not in the public spec. So, this
7 library instance is only for this code base.
8
9 Copyright (c) 2006 - 2008, Intel Corporation
10 All rights reserved. This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #include <Uefi.h>
21
22 #include <Protocol/Print2.h>
23
24 #include <Library/PrintLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26
27 EFI_PRINT2_PROTOCOL *gPrintProtocol = NULL;
28
29 /**
30 Internal function to locate the EFI_PRINT2_PROTOCOL.
31
32 @retval EFI_SUCCESS EFI_PRINT2_PROTOCOL is successfuly located.
33 @retval EFI_NOT_FOUND EFI_PRINT2_PROTOCOL cannot be found.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 InternalLocatePrintProtocol (
39 VOID
40 )
41 {
42 EFI_STATUS Status;
43
44 if (gPrintProtocol == NULL) {
45 Status = gBS->LocateProtocol (
46 &gEfiPrint2ProtocolGuid,
47 NULL,
48 (VOID **)&gPrintProtocol
49 );
50 if (EFI_ERROR (Status)) {
51 gPrintProtocol = NULL;
52 return Status;
53 }
54 }
55
56 return EFI_SUCCESS;
57 }
58
59 /**
60 Produces a Null-terminated Unicode string in an output buffer based on
61 a Null-terminated Unicode format string and a VA_LIST argument list
62
63 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
64 and BufferSize.
65 The Unicode string is produced by parsing the format string specified by FormatString.
66 Arguments are pulled from the variable argument list specified by Marker based on the
67 contents of the format string.
68 The number of Unicode characters in the produced output buffer is returned not including
69 the Null-terminator.
70 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
71
72 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
73 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
74 If BufferSize > 1 and FormatString is NULL, then ASSERT().
75 If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
76 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
77 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
78 ASSERT().
79 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
80 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
81 Null-terminator, then ASSERT().
82
83 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
84 Unicode string.
85 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
86 @param FormatString Null-terminated Unicode format string.
87 @param Marker VA_LIST marker for the variable argument list.
88
89 @return The number of Unicode characters in the produced output buffer not including the
90 Null-terminator.
91
92 **/
93 UINTN
94 EFIAPI
95 UnicodeVSPrint (
96 OUT CHAR16 *StartOfBuffer,
97 IN UINTN BufferSize,
98 IN CONST CHAR16 *FormatString,
99 IN VA_LIST Marker
100 )
101 {
102 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
103 return 0;
104 }
105
106 return gPrintProtocol->VSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
107 }
108
109 /**
110 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
111 Unicode format string and variable argument list.
112
113 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
114 and BufferSize.
115 The Unicode string is produced by parsing the format string specified by FormatString.
116 Arguments are pulled from the variable argument list based on the contents of the format string.
117 The number of Unicode characters in the produced output buffer is returned not including
118 the Null-terminator.
119 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
120
121 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
122 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
123 If BufferSize > 1 and FormatString is NULL, then ASSERT().
124 If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
125 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
126 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
127 ASSERT().
128 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
129 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
130 Null-terminator, then ASSERT().
131
132 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
133 Unicode string.
134 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
135 @param FormatString Null-terminated Unicode format string.
136 @param ... Variable argument list based on the contents of the format string.
137
138 @return The number of Unicode characters in the produced output buffer not including the
139 Null-terminator.
140
141 **/
142 UINTN
143 EFIAPI
144 UnicodeSPrint (
145 OUT CHAR16 *StartOfBuffer,
146 IN UINTN BufferSize,
147 IN CONST CHAR16 *FormatString,
148 ...
149 )
150 {
151 VA_LIST Marker;
152
153 VA_START (Marker, FormatString);
154 return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
155 }
156
157 /**
158 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
159 ASCII format string and a VA_LIST argument list
160
161 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
162 and BufferSize.
163 The Unicode string is produced by parsing the format string specified by FormatString.
164 Arguments are pulled from the variable argument list specified by Marker based on the
165 contents of the format string.
166 The number of Unicode characters in the produced output buffer is returned not including
167 the Null-terminator.
168 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
169
170 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
171 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
172 If BufferSize > 1 and FormatString is NULL, then ASSERT().
173 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
174 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
175 ASSERT().
176 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
177 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
178 Null-terminator, then ASSERT().
179
180 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
181 Unicode string.
182 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
183 @param FormatString Null-terminated Unicode format string.
184 @param Marker VA_LIST marker for the variable argument list.
185
186 @return The number of Unicode characters in the produced output buffer not including the
187 Null-terminator.
188
189 **/
190 UINTN
191 EFIAPI
192 UnicodeVSPrintAsciiFormat (
193 OUT CHAR16 *StartOfBuffer,
194 IN UINTN BufferSize,
195 IN CONST CHAR8 *FormatString,
196 IN VA_LIST Marker
197 )
198 {
199 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
200 return 0;
201 }
202
203 return gPrintProtocol->UniVSPrintAscii (StartOfBuffer, BufferSize, FormatString, Marker);
204 }
205
206 /**
207 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
208 ASCII format string and variable argument list.
209
210 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
211 and BufferSize.
212 The Unicode string is produced by parsing the format string specified by FormatString.
213 Arguments are pulled from the variable argument list based on the contents of the
214 format string.
215 The number of Unicode characters in the produced output buffer is returned not including
216 the Null-terminator.
217 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
218
219 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
220 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
221 If BufferSize > 1 and FormatString is NULL, then ASSERT().
222 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
223 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
224 ASSERT().
225 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
226 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
227 Null-terminator, then ASSERT().
228
229 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
230 Unicode string.
231 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
232 @param FormatString Null-terminated Unicode format string.
233 @param ... Variable argument list based on the contents of the format string.
234
235 @return The number of Unicode characters in the produced output buffer not including the
236 Null-terminator.
237
238 **/
239 UINTN
240 EFIAPI
241 UnicodeSPrintAsciiFormat (
242 OUT CHAR16 *StartOfBuffer,
243 IN UINTN BufferSize,
244 IN CONST CHAR8 *FormatString,
245 ...
246 )
247 {
248 VA_LIST Marker;
249
250 VA_START (Marker, FormatString);
251 return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);
252 }
253
254 /**
255 Converts a decimal value to a Null-terminated Unicode string.
256
257 Converts the decimal number specified by Value to a Null-terminated Unicode
258 string specified by Buffer containing at most Width characters. No padding of spaces
259 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
260 The number of Unicode characters in Buffer is returned not including the Null-terminator.
261 If the conversion contains more than Width characters, then only the first
262 Width characters are returned, and the total number of characters
263 required to perform the conversion is returned.
264 Additional conversion parameters are specified in Flags.
265
266 The Flags bit LEFT_JUSTIFY is always ignored.
267 All conversions are left justified in Buffer.
268 If Width is 0, PREFIX_ZERO is ignored in Flags.
269 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
270 are inserted every 3rd digit starting from the right.
271 If HEX_RADIX is set in Flags, then the output buffer will be
272 formatted in hexadecimal format.
273 If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.
274 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
275 then Buffer is padded with '0' characters so the combination of the optional '-'
276 sign character, '0' characters, digit characters for Value, and the Null-terminator
277 add up to Width characters.
278 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
279 If Buffer is NULL, then ASSERT().
280 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
281 If unsupported bits are set in Flags, then ASSERT().
282 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
283 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
284
285 @param Buffer Pointer to the output buffer for the produced Null-terminated
286 Unicode string.
287 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
288 @param Value The 64-bit signed value to convert to a string.
289 @param Width The maximum number of Unicode characters to place in Buffer, not including
290 the Null-terminator.
291
292 @return The number of Unicode characters in Buffer not including the Null-terminator.
293
294 **/
295 UINTN
296 EFIAPI
297 UnicodeValueToString (
298 IN OUT CHAR16 *Buffer,
299 IN UINTN Flags,
300 IN INT64 Value,
301 IN UINTN Width
302 )
303 {
304 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
305 return 0;
306 }
307
308 return gPrintProtocol->UniValueToString (Buffer, Flags, Value, Width);
309 }
310
311 /**
312 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
313 ASCII format string and a VA_LIST argument list.
314
315 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
316 and BufferSize.
317 The ASCII string is produced by parsing the format string specified by FormatString.
318 Arguments are pulled from the variable argument list specified by Marker based on
319 the contents of the format string.
320 The number of ASCII characters in the produced output buffer is returned not including
321 the Null-terminator.
322 If BufferSize is 0, then no output buffer is produced and 0 is returned.
323
324 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
325 If BufferSize > 0 and FormatString is NULL, then ASSERT().
326 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
327 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
328 ASSERT().
329 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
330 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
331 Null-terminator, then ASSERT().
332
333 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
334 ASCII string.
335 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
336 @param FormatString Null-terminated Unicode format string.
337 @param Marker VA_LIST marker for the variable argument list.
338
339 @return The number of ASCII characters in the produced output buffer not including the
340 Null-terminator.
341
342 **/
343 UINTN
344 EFIAPI
345 AsciiVSPrint (
346 OUT CHAR8 *StartOfBuffer,
347 IN UINTN BufferSize,
348 IN CONST CHAR8 *FormatString,
349 IN VA_LIST Marker
350 )
351 {
352 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
353 return 0;
354 }
355
356 return gPrintProtocol->AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
357 }
358
359 /**
360 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
361 ASCII format string and variable argument list.
362
363 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
364 and BufferSize.
365 The ASCII string is produced by parsing the format string specified by FormatString.
366 Arguments are pulled from the variable argument list based on the contents of the
367 format string.
368 The number of ASCII characters in the produced output buffer is returned not including
369 the Null-terminator.
370 If BufferSize is 0, then no output buffer is produced and 0 is returned.
371
372 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
373 If BufferSize > 0 and FormatString is NULL, then ASSERT().
374 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
375 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
376 ASSERT().
377 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
378 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
379 Null-terminator, then ASSERT().
380
381 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
382 ASCII string.
383 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
384 @param FormatString Null-terminated Unicode format string.
385 @param ... Variable argument list based on the contents of the format string.
386
387 @return The number of ASCII characters in the produced output buffer not including the
388 Null-terminator.
389
390 **/
391 UINTN
392 EFIAPI
393 AsciiSPrint (
394 OUT CHAR8 *StartOfBuffer,
395 IN UINTN BufferSize,
396 IN CONST CHAR8 *FormatString,
397 ...
398 )
399 {
400 VA_LIST Marker;
401
402 VA_START (Marker, FormatString);
403 return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
404 }
405
406 /**
407 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
408 ASCII format string and a VA_LIST argument list.
409
410 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
411 and BufferSize.
412 The ASCII string is produced by parsing the format string specified by FormatString.
413 Arguments are pulled from the variable argument list specified by Marker based on
414 the contents of the format string.
415 The number of ASCII characters in the produced output buffer is returned not including
416 the Null-terminator.
417 If BufferSize is 0, then no output buffer is produced and 0 is returned.
418
419 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
420 If BufferSize > 0 and FormatString is NULL, then ASSERT().
421 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
422 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
423 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
424 ASSERT().
425 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
426 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
427 Null-terminator, then ASSERT().
428
429 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
430 ASCII string.
431 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
432 @param FormatString Null-terminated Unicode format string.
433 @param Marker VA_LIST marker for the variable argument list.
434
435 @return The number of ASCII characters in the produced output buffer not including the
436 Null-terminator.
437
438 **/
439 UINTN
440 EFIAPI
441 AsciiVSPrintUnicodeFormat (
442 OUT CHAR8 *StartOfBuffer,
443 IN UINTN BufferSize,
444 IN CONST CHAR16 *FormatString,
445 IN VA_LIST Marker
446 )
447 {
448 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
449 return 0;
450 }
451
452 return gPrintProtocol->AsciiVSPrintUni (StartOfBuffer, BufferSize, FormatString, Marker);
453 }
454
455 /**
456 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
457 ASCII format string and variable argument list.
458
459 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
460 and BufferSize.
461 The ASCII string is produced by parsing the format string specified by FormatString.
462 Arguments are pulled from the variable argument list based on the contents of the
463 format string.
464 The number of ASCII characters in the produced output buffer is returned not including
465 the Null-terminator.
466 If BufferSize is 0, then no output buffer is produced and 0 is returned.
467
468 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
469 If BufferSize > 0 and FormatString is NULL, then ASSERT().
470 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
471 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
472 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
473 ASSERT().
474 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
475 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
476 Null-terminator, then ASSERT().
477
478 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
479 ASCII string.
480 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
481 @param FormatString Null-terminated Unicode format string.
482 @param ... Variable argument list based on the contents of the format string.
483
484 @return The number of ASCII characters in the produced output buffer not including the
485 Null-terminator.
486
487 **/
488 UINTN
489 EFIAPI
490 AsciiSPrintUnicodeFormat (
491 OUT CHAR8 *StartOfBuffer,
492 IN UINTN BufferSize,
493 IN CONST CHAR16 *FormatString,
494 ...
495 )
496 {
497 VA_LIST Marker;
498
499 VA_START (Marker, FormatString);
500 return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
501 }
502
503
504 /**
505 Converts a decimal value to a Null-terminated ASCII string.
506
507 Converts the decimal number specified by Value to a Null-terminated ASCII string
508 specified by Buffer containing at most Width characters. No padding of spaces
509 is ever performed.
510 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
511 The number of ASCII characters in Buffer is returned not including the Null-terminator.
512 If the conversion contains more than Width characters, then only the first Width
513 characters are returned, and the total number of characters required to perform
514 the conversion is returned.
515 Additional conversion parameters are specified in Flags.
516 The Flags bit LEFT_JUSTIFY is always ignored.
517 All conversions are left justified in Buffer.
518 If Width is 0, PREFIX_ZERO is ignored in Flags.
519 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
520 are inserted every 3rd digit starting from the right.
521 If HEX_RADIX is set in Flags, then the output buffer will be
522 formatted in hexadecimal format.
523 If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.
524 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
525 then Buffer is padded with '0' characters so the combination of the optional '-'
526 sign character, '0' characters, digit characters for Value, and the Null-terminator
527 add up to Width characters.
528
529 If Buffer is NULL, then ASSERT().
530 If unsupported bits are set in Flags, then ASSERT().
531 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
532 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
533
534 @param Buffer Pointer to the output buffer for the produced Null-terminated
535 ASCII string.
536 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
537 @param Value The 64-bit signed value to convert to a string.
538 @param Width The maximum number of ASCII characters to place in Buffer, not including
539 the Null-terminator.
540
541 @return The number of ASCII characters in Buffer not including the Null-terminator.
542
543 **/
544 UINTN
545 EFIAPI
546 AsciiValueToString (
547 IN OUT CHAR8 *Buffer,
548 IN UINTN Flags,
549 IN INT64 Value,
550 IN UINTN Width
551 )
552 {
553 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
554 return 0;
555 }
556
557 return gPrintProtocol->AsciiValueToString (Buffer, Flags, Value, Width);
558 }