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