7b539be501c17870bdd7b772093d20f736c6ef79
[mirror_edk2.git] / IntelFrameworkPkg / Library / UefiLibFramework / UefiLibPrint.c
1 /** @file
2 Mde UEFI library API implemention.
3 Print to StdErr or ConOut defined in EFI_SYSTEM_TABLE
4
5 Copyright (c) 2007, Intel Corporation<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiLibFramework.h"
17
18 /**
19 Internal function which prints a formatted Unicode string to the console output device
20 specified by Console
21
22 This function prints a formatted Unicode string to the console output device
23 specified by Console and returns the number of Unicode characters that printed
24 to it. If the length of the formatted Unicode string is greater than PcdUefiLibMaxPrintBufferSize,
25 then only the first PcdUefiLibMaxPrintBufferSize characters are sent to Console.
26
27 @param Format Null-terminated Unicode format string.
28 @param Console The output console.
29 @param Marker VA_LIST marker for the variable argument list.
30
31 If Format is NULL, then ASSERT().
32 If Format is not aligned on a 16-bit boundary, then ASSERT().
33
34 **/
35
36 STATIC
37 UINTN
38 InternalPrint (
39 IN CONST CHAR16 *Format,
40 IN EFI_SIMPLE_TEXT_OUT_PROTOCOL *Console,
41 IN VA_LIST Marker
42 )
43 {
44 UINTN Return;
45 CHAR16 *Buffer;
46 UINTN BufferSize;
47
48 ASSERT (Format != NULL);
49 ASSERT (((UINTN) Format & 0x01) == 0);
50
51 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
52
53 Buffer = (CHAR16 *) AllocatePool(BufferSize);
54 ASSERT (Buffer != NULL);
55
56 Return = UnicodeVSPrint (Buffer, BufferSize, Format, Marker);
57
58 if (Console != NULL) {
59 //
60 // To be extra safe make sure Console has been initialized
61 //
62 Console->OutputString (Console, Buffer);
63 }
64
65 FreePool (Buffer);
66
67 return Return;
68 }
69
70 /**
71 Prints a formatted Unicode string to the console output device specified by
72 ConOut defined in the EFI_SYSTEM_TABLE.
73
74 This function prints a formatted Unicode string to the console output device
75 specified by ConOut in EFI_SYSTEM_TABLE and returns the number of Unicode
76 characters that printed to ConOut. If the length of the formatted Unicode
77 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
78 PcdUefiLibMaxPrintBufferSize characters are sent to ConOut.
79
80 @param Format Null-terminated Unicode format string.
81 @param ... VARARG list consumed to process Format.
82 If Format is NULL, then ASSERT().
83 If Format is not aligned on a 16-bit boundary, then ASSERT().
84
85 **/
86 UINTN
87 EFIAPI
88 Print (
89 IN CONST CHAR16 *Format,
90 ...
91 )
92 {
93 VA_LIST Marker;
94 UINTN Return;
95
96 VA_START (Marker, Format);
97
98 Return = InternalPrint (Format, gST->ConOut, Marker);
99
100 VA_END (Marker);
101
102 return Return;
103 }
104
105 /**
106 Prints a formatted Unicode string to the console output device specified by
107 StdErr defined in the EFI_SYSTEM_TABLE.
108
109 This function prints a formatted Unicode string to the console output device
110 specified by StdErr in EFI_SYSTEM_TABLE and returns the number of Unicode
111 characters that printed to StdErr. If the length of the formatted Unicode
112 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
113 PcdUefiLibMaxPrintBufferSize characters are sent to StdErr.
114
115 @param Format Null-terminated Unicode format string.
116 @param ... VARARG list consumed to process Format.
117 If Format is NULL, then ASSERT().
118 If Format is not aligned on a 16-bit boundary, then ASSERT().
119
120 **/
121
122 UINTN
123 EFIAPI
124 ErrorPrint (
125 IN CONST CHAR16 *Format,
126 ...
127 )
128 {
129 VA_LIST Marker;
130 UINTN Return;
131
132 VA_START (Marker, Format);
133
134 Return = InternalPrint( Format, gST->StdErr, Marker);
135
136 VA_END (Marker);
137
138 return Return;
139 }
140
141
142 /**
143 Internal function which prints a formatted ASCII string to the console output device
144 specified by Console
145
146 This function prints a formatted ASCII string to the console output device
147 specified by Console and returns the number of ASCII characters that printed
148 to it. If the length of the formatted ASCII string is greater than PcdUefiLibMaxPrintBufferSize,
149 then only the first PcdUefiLibMaxPrintBufferSize characters are sent to Console.
150
151 @param Format Null-terminated ASCII format string.
152 @param Console The output console.
153 @param Marker VA_LIST marker for the variable argument list.
154
155 If Format is NULL, then ASSERT().
156
157 **/
158
159 STATIC
160 UINTN
161 AsciiInternalPrint (
162 IN CONST CHAR8 *Format,
163 IN EFI_SIMPLE_TEXT_OUT_PROTOCOL *Console,
164 IN VA_LIST Marker
165 )
166 {
167 UINTN Return;
168 CHAR16 *Buffer;
169 UINTN BufferSize;
170
171 ASSERT (Format != NULL);
172
173 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
174
175 Buffer = (CHAR16 *) AllocatePool(BufferSize);
176 ASSERT (Buffer != NULL);
177
178 Return = UnicodeVSPrintAsciiFormat (Buffer, BufferSize, Format, Marker);
179
180 if (Console != NULL) {
181 //
182 // To be extra safe make sure Console has been initialized
183 //
184 Console->OutputString (Console, Buffer);
185 }
186
187 FreePool (Buffer);
188
189 return Return;
190 }
191
192 /**
193 Prints a formatted ASCII string to the console output device specified by
194 ConOut defined in the EFI_SYSTEM_TABLE.
195
196 This function prints a formatted ASCII string to the console output device
197 specified by ConOut in EFI_SYSTEM_TABLE and returns the number of ASCII
198 characters that printed to ConOut. If the length of the formatted ASCII
199 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
200 PcdUefiLibMaxPrintBufferSize characters are sent to ConOut.
201
202 @param Format Null-terminated ASCII format string.
203 @param ... VARARG list consumed to process Format.
204 If Format is NULL, then ASSERT().
205 If Format is not aligned on a 16-bit boundary, then ASSERT().
206
207 **/
208 UINTN
209 EFIAPI
210 AsciiPrint (
211 IN CONST CHAR8 *Format,
212 ...
213 )
214 {
215 VA_LIST Marker;
216 UINTN Return;
217
218 VA_START (Marker, Format);
219
220 Return = AsciiInternalPrint( Format, gST->ConOut, Marker);
221
222 VA_END (Marker);
223
224 return Return;
225 }
226
227 /**
228 Prints a formatted ASCII string to the console output device specified by
229 StdErr defined in the EFI_SYSTEM_TABLE.
230
231 This function prints a formatted ASCII string to the console output device
232 specified by StdErr in EFI_SYSTEM_TABLE and returns the number of ASCII
233 characters that printed to StdErr. If the length of the formatted ASCII
234 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
235 PcdUefiLibMaxPrintBufferSize characters are sent to StdErr.
236
237 @param Format Null-terminated ASCII format string.
238 @param ... VARARG list consumed to process Format.
239 If Format is NULL, then ASSERT().
240 If Format is not aligned on a 16-bit boundary, then ASSERT().
241
242 **/
243 UINTN
244 EFIAPI
245 AsciiErrorPrint (
246 IN CONST CHAR8 *Format,
247 ...
248 )
249 {
250 VA_LIST Marker;
251 UINTN Return;
252
253 VA_START (Marker, Format);
254
255 Return = AsciiInternalPrint( Format, gST->StdErr, Marker);
256
257 VA_END (Marker);
258
259 return Return;
260 }
261