]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/EdkDxePrintLib/PrintLib.c
78c49ad38638af6ffa1eb5e69fbe5835ca85385a
[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 whose contents are accessed based on the
137 format string specified by FormatString.
138
139 @return The number of Unicode characters in the produced output buffer not including the
140 Null-terminator.
141
142 **/
143 UINTN
144 EFIAPI
145 UnicodeSPrint (
146 OUT CHAR16 *StartOfBuffer,
147 IN UINTN BufferSize,
148 IN CONST CHAR16 *FormatString,
149 ...
150 )
151 {
152 VA_LIST Marker;
153
154 VA_START (Marker, FormatString);
155 return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
156 }
157
158 /**
159 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
160 ASCII format string and a VA_LIST argument list
161
162 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
163 and BufferSize.
164 The Unicode string is produced by parsing the format string specified by FormatString.
165 Arguments are pulled from the variable argument list specified by Marker based on the
166 contents of the format string.
167 The number of Unicode characters in the produced output buffer is returned not including
168 the Null-terminator.
169 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
170
171 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
172 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
173 If BufferSize > 1 and FormatString is NULL, then ASSERT().
174 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
175 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
176 ASSERT().
177 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
178 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
179 Null-terminator, then ASSERT().
180
181 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
182 Unicode string.
183 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
184 @param FormatString Null-terminated Unicode format string.
185 @param Marker VA_LIST marker for the variable argument list.
186
187 @return The number of Unicode characters in the produced output buffer not including the
188 Null-terminator.
189
190 **/
191 UINTN
192 EFIAPI
193 UnicodeVSPrintAsciiFormat (
194 OUT CHAR16 *StartOfBuffer,
195 IN UINTN BufferSize,
196 IN CONST CHAR8 *FormatString,
197 IN VA_LIST Marker
198 )
199 {
200 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
201 return 0;
202 }
203
204 return gPrintProtocol->UniVSPrintAscii (StartOfBuffer, BufferSize, FormatString, Marker);
205 }
206
207 /**
208 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
209 ASCII format string and variable argument list.
210
211 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
212 and BufferSize.
213 The Unicode string is produced by parsing the format string specified by FormatString.
214 Arguments are pulled from the variable argument list based on the contents of the
215 format string.
216 The number of Unicode characters in the produced output buffer is returned not including
217 the Null-terminator.
218 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
219
220 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().
221 If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
222 If BufferSize > 1 and FormatString is NULL, then ASSERT().
223 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
224 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
225 ASSERT().
226 If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string
227 contains more than PcdMaximumUnicodeStringLength Unicode characters not including the
228 Null-terminator, then ASSERT().
229
230 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
231 Unicode string.
232 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
233 @param FormatString Null-terminated Unicode format string.
234 @param ... Variable argument list whose contents are accessed based on the
235 format string specified by FormatString.
236
237 @return The number of Unicode characters in the produced output buffer not including the
238 Null-terminator.
239
240 **/
241 UINTN
242 EFIAPI
243 UnicodeSPrintAsciiFormat (
244 OUT CHAR16 *StartOfBuffer,
245 IN UINTN BufferSize,
246 IN CONST CHAR8 *FormatString,
247 ...
248 )
249 {
250 VA_LIST Marker;
251
252 VA_START (Marker, FormatString);
253 return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);
254 }
255
256 /**
257 Converts a decimal value to a Null-terminated Unicode string.
258
259 Converts the decimal number specified by Value to a Null-terminated Unicode
260 string specified by Buffer containing at most Width characters. No padding of spaces
261 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
262 The number of Unicode characters in Buffer is returned not including the Null-terminator.
263 If the conversion contains more than Width characters, then only the first
264 Width characters are returned, and the total number of characters
265 required to perform the conversion is returned.
266 Additional conversion parameters are specified in Flags.
267
268 The Flags bit LEFT_JUSTIFY is always ignored.
269 All conversions are left justified in Buffer.
270 If Width is 0, PREFIX_ZERO is ignored in Flags.
271 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
272 are inserted every 3rd digit starting from the right.
273 If HEX_RADIX is set in Flags, then the output buffer will be
274 formatted in hexadecimal format.
275 If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.
276 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
277 then Buffer is padded with '0' characters so the combination of the optional '-'
278 sign character, '0' characters, digit characters for Value, and the Null-terminator
279 add up to Width characters.
280 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
281 If Buffer is NULL, then ASSERT().
282 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
283 If unsupported bits are set in Flags, then ASSERT().
284 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
285 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
286
287 @param Buffer Pointer to the output buffer for the produced Null-terminated
288 Unicode string.
289 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
290 @param Value The 64-bit signed value to convert to a string.
291 @param Width The maximum number of Unicode characters to place in Buffer, not including
292 the Null-terminator.
293
294 @return The number of Unicode characters in Buffer not including the Null-terminator.
295
296 **/
297 UINTN
298 EFIAPI
299 UnicodeValueToString (
300 IN OUT CHAR16 *Buffer,
301 IN UINTN Flags,
302 IN INT64 Value,
303 IN UINTN Width
304 )
305 {
306 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
307 return 0;
308 }
309
310 return gPrintProtocol->UniValueToString (Buffer, Flags, Value, Width);
311 }
312
313 /**
314 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
315 ASCII format string and a VA_LIST argument list.
316
317 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
318 and BufferSize.
319 The ASCII string is produced by parsing the format string specified by FormatString.
320 Arguments are pulled from the variable argument list specified by Marker based on
321 the contents of the format string.
322 The number of ASCII characters in the produced output buffer is returned not including
323 the Null-terminator.
324 If BufferSize is 0, then no output buffer is produced and 0 is returned.
325
326 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
327 If BufferSize > 0 and FormatString is NULL, then ASSERT().
328 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
329 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
330 ASSERT().
331 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
332 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
333 Null-terminator, then ASSERT().
334
335 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
336 ASCII string.
337 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
338 @param FormatString Null-terminated Unicode format string.
339 @param Marker VA_LIST marker for the variable argument list.
340
341 @return The number of ASCII characters in the produced output buffer not including the
342 Null-terminator.
343
344 **/
345 UINTN
346 EFIAPI
347 AsciiVSPrint (
348 OUT CHAR8 *StartOfBuffer,
349 IN UINTN BufferSize,
350 IN CONST CHAR8 *FormatString,
351 IN VA_LIST Marker
352 )
353 {
354 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
355 return 0;
356 }
357
358 return gPrintProtocol->AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
359 }
360
361 /**
362 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
363 ASCII format string and variable argument list.
364
365 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
366 and BufferSize.
367 The ASCII string is produced by parsing the format string specified by FormatString.
368 Arguments are pulled from the variable argument list based on the contents of the
369 format string.
370 The number of ASCII characters in the produced output buffer is returned not including
371 the Null-terminator.
372 If BufferSize is 0, then no output buffer is produced and 0 is returned.
373
374 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
375 If BufferSize > 0 and FormatString is NULL, then ASSERT().
376 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
377 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then
378 ASSERT().
379 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
380 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
381 Null-terminator, then ASSERT().
382
383 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
384 ASCII string.
385 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
386 @param FormatString Null-terminated Unicode format string.
387 @param ... Variable argument list whose contents are accessed based on the
388 format string specified by FormatString.
389
390 @return The number of ASCII characters in the produced output buffer not including the
391 Null-terminator.
392
393 **/
394 UINTN
395 EFIAPI
396 AsciiSPrint (
397 OUT CHAR8 *StartOfBuffer,
398 IN UINTN BufferSize,
399 IN CONST CHAR8 *FormatString,
400 ...
401 )
402 {
403 VA_LIST Marker;
404
405 VA_START (Marker, FormatString);
406 return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
407 }
408
409 /**
410 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
411 ASCII format string and a VA_LIST argument list.
412
413 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
414 and BufferSize.
415 The ASCII string is produced by parsing the format string specified by FormatString.
416 Arguments are pulled from the variable argument list specified by Marker based on
417 the contents of the format string.
418 The number of ASCII characters in the produced output buffer is returned not including
419 the Null-terminator.
420 If BufferSize is 0, then no output buffer is produced and 0 is returned.
421
422 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
423 If BufferSize > 0 and FormatString is NULL, then ASSERT().
424 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
425 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
426 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
427 ASSERT().
428 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
429 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
430 Null-terminator, then ASSERT().
431
432 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
433 ASCII string.
434 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
435 @param FormatString Null-terminated Unicode format string.
436 @param Marker VA_LIST marker for the variable argument list.
437
438 @return The number of ASCII characters in the produced output buffer not including the
439 Null-terminator.
440
441 **/
442 UINTN
443 EFIAPI
444 AsciiVSPrintUnicodeFormat (
445 OUT CHAR8 *StartOfBuffer,
446 IN UINTN BufferSize,
447 IN CONST CHAR16 *FormatString,
448 IN VA_LIST Marker
449 )
450 {
451 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
452 return 0;
453 }
454
455 return gPrintProtocol->AsciiVSPrintUni (StartOfBuffer, BufferSize, FormatString, Marker);
456 }
457
458 /**
459 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
460 ASCII format string and variable argument list.
461
462 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
463 and BufferSize.
464 The ASCII string is produced by parsing the format string specified by FormatString.
465 Arguments are pulled from the variable argument list based on the contents of the
466 format string.
467 The number of ASCII characters in the produced output buffer is returned not including
468 the Null-terminator.
469 If BufferSize is 0, then no output buffer is produced and 0 is returned.
470
471 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().
472 If BufferSize > 0 and FormatString is NULL, then ASSERT().
473 If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().
474 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
475 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
476 ASSERT().
477 If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string
478 contains more than PcdMaximumAsciiStringLength ASCII characters not including the
479 Null-terminator, then ASSERT().
480
481 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
482 ASCII string.
483 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
484 @param FormatString Null-terminated Unicode format string.
485 @param ... Variable argument list whose contents are accessed based on the
486 format string specified by FormatString.
487
488 @return The number of ASCII characters in the produced output buffer not including the
489 Null-terminator.
490
491 **/
492 UINTN
493 EFIAPI
494 AsciiSPrintUnicodeFormat (
495 OUT CHAR8 *StartOfBuffer,
496 IN UINTN BufferSize,
497 IN CONST CHAR16 *FormatString,
498 ...
499 )
500 {
501 VA_LIST Marker;
502
503 VA_START (Marker, FormatString);
504 return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
505 }
506
507
508 /**
509 Converts a decimal value to a Null-terminated ASCII string.
510
511 Converts the decimal number specified by Value to a Null-terminated ASCII string
512 specified by Buffer containing at most Width characters. No padding of spaces
513 is ever performed.
514 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
515 The number of ASCII characters in Buffer is returned not including the Null-terminator.
516 If the conversion contains more than Width characters, then only the first Width
517 characters are returned, and the total number of characters required to perform
518 the conversion is returned.
519 Additional conversion parameters are specified in Flags.
520 The Flags bit LEFT_JUSTIFY is always ignored.
521 All conversions are left justified in Buffer.
522 If Width is 0, PREFIX_ZERO is ignored in Flags.
523 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
524 are inserted every 3rd digit starting from the right.
525 If HEX_RADIX is set in Flags, then the output buffer will be
526 formatted in hexadecimal format.
527 If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.
528 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
529 then Buffer is padded with '0' characters so the combination of the optional '-'
530 sign character, '0' characters, digit characters for Value, and the Null-terminator
531 add up to Width characters.
532
533 If Buffer is NULL, then ASSERT().
534 If unsupported bits are set in Flags, then ASSERT().
535 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
536 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
537
538 @param Buffer Pointer to the output buffer for the produced Null-terminated
539 ASCII string.
540 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
541 @param Value The 64-bit signed value to convert to a string.
542 @param Width The maximum number of ASCII characters to place in Buffer, not including
543 the Null-terminator.
544
545 @return The number of ASCII characters in Buffer not including the Null-terminator.
546
547 **/
548 UINTN
549 EFIAPI
550 AsciiValueToString (
551 OUT CHAR8 *Buffer,
552 IN UINTN Flags,
553 IN INT64 Value,
554 IN UINTN Width
555 )
556 {
557 if (InternalLocatePrintProtocol() != EFI_SUCCESS) {
558 return 0;
559 }
560
561 return gPrintProtocol->AsciiValueToString (Buffer, Flags, Value, Width);
562 }