* Modify various PCI register settings for OVMF.
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
CommitLineData
e1f414b6 1/** @file\r
eceb3a4c 2 Print Library internal worker functions.\r
e1f414b6 3\r
eceb3a4c 4 Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
e1f414b6 5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e1f414b6 13**/\r
14\r
e1f414b6 15#include "PrintLibInternal.h"\r
16\r
e389c39b 17#define WARNING_STATUS_NUMBER 4\r
18#define ERROR_STATUS_NUMBER 24\r
19\r
e1f414b6 20GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
21\r
e389c39b 22GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = {\r
23 "Success", // RETURN_SUCCESS = 0\r
24 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1\r
25 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2\r
26 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3\r
27 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4\r
28 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT\r
29 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT\r
30 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT\r
31 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT\r
32 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT\r
33 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT\r
34 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT\r
35 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT\r
36 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT\r
37 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT\r
38 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT\r
39 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT\r
40 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT\r
41 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT\r
42 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT\r
43 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT\r
44 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT\r
45 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT\r
46 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT\r
47 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT\r
48 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT\r
49 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT\r
50 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT\r
51 "Protocol Error" // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT\r
52};\r
53\r
e1f414b6 54\r
55/**\r
56 Internal function that places the character into the Buffer.\r
57\r
58 Internal function that places ASCII or Unicode character into the Buffer.\r
59\r
60 @param Buffer Buffer to place the Unicode or ASCII string.\r
61 @param EndBuffer The end of the input Buffer. No characters will be\r
62 placed after that. \r
63 @param Length Count of character to be placed into Buffer.\r
a2bc29c4 64 (Negative value indicates no buffer fill.)\r
e1f414b6 65 @param Character Character to be placed into Buffer.\r
66 @param Increment Character increment in Buffer.\r
67\r
eceb3a4c 68 @return Buffer Buffer filled with the input Character.\r
e1f414b6 69\r
70**/\r
71CHAR8 *\r
72BasePrintLibFillBuffer (\r
eceb3a4c
LG
73 OUT CHAR8 *Buffer,\r
74 IN CHAR8 *EndBuffer,\r
75 IN INTN Length,\r
76 IN UINTN Character,\r
77 IN INTN Increment\r
e1f414b6 78 )\r
79{\r
a2bc29c4 80 INTN Index;\r
bcccf0b0 81 \r
a2bc29c4 82 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
83 *Buffer = (CHAR8) Character;\r
84 if (Increment != 1) {\r
85 *(Buffer + 1) = (CHAR8)(Character >> 8);\r
86 }\r
87 Buffer += Increment;\r
88 }\r
89\r
90 return Buffer;\r
e1f414b6 91}\r
92\r
93/**\r
7c905091 94 Internal function that convert a number to a string in Buffer.\r
e1f414b6 95\r
7c905091 96 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
e1f414b6 97\r
7c905091 98 @param Buffer Location to place the ASCII string of Value.\r
bcccf0b0 99 @param Value Value to convert to a Decimal or Hexadecimal string in Buffer.\r
e1f414b6 100 @param Radix Radix of the value\r
101\r
7c905091 102 @return A pointer to the end of buffer filled with ASCII string.\r
e1f414b6 103\r
104**/\r
7c905091 105CHAR8 *\r
e1f414b6 106BasePrintLibValueToString (\r
efb23117 107 IN OUT CHAR8 *Buffer, \r
108 IN INT64 Value, \r
e1f414b6 109 IN UINTN Radix\r
110 )\r
111{\r
e1f414b6 112 UINT32 Remainder;\r
113\r
114 //\r
115 // Loop to convert one digit at a time in reverse order\r
116 //\r
7c905091 117 *Buffer = 0;\r
e1f414b6 118 do {\r
119 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
7c905091 120 *(++Buffer) = mHexStr[Remainder];\r
e1f414b6 121 } while (Value != 0);\r
eceb3a4c
LG
122\r
123 //\r
7c905091 124 // Return pointer of the end of filled buffer.\r
eceb3a4c 125 //\r
7c905091 126 return Buffer;\r
e1f414b6 127}\r
128\r
129/**\r
130 Internal function that converts a decimal value to a Null-terminated string.\r
131 \r
132 Converts the decimal number specified by Value to a Null-terminated \r
133 string specified by Buffer containing at most Width characters.\r
134 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
efb23117 135 The total number of characters placed in Buffer is returned.\r
e1f414b6 136 If the conversion contains more than Width characters, then only the first\r
137 Width characters are returned, and the total number of characters \r
138 required to perform the conversion is returned.\r
139 Additional conversion parameters are specified in Flags. \r
140 The Flags bit LEFT_JUSTIFY is always ignored.\r
141 All conversions are left justified in Buffer.\r
142 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
143 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
144 are inserted every 3rd digit starting from the right.\r
efb23117 145 If Value is < 0, then the fist character in Buffer is a '-'.\r
e1f414b6 146 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
147 then Buffer is padded with '0' characters so the combination of the optional '-' \r
148 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
149 add up to Width characters.\r
e1f414b6 150\r
151 If Buffer is NULL, then ASSERT().\r
152 If unsupported bits are set in Flags, then ASSERT().\r
153 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
154\r
155 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
156 string.\r
157 @param Flags The bitmask of flags that specify left justification, zero pad,\r
158 and commas.\r
159 @param Value The 64-bit signed value to convert to a string.\r
160 @param Width The maximum number of characters to place in Buffer, not including\r
161 the Null-terminator.\r
162 @param Increment Character increment in Buffer.\r
163 \r
efb23117 164 @return Total number of characters required to perform the conversion.\r
e1f414b6 165\r
166**/\r
167UINTN\r
168BasePrintLibConvertValueToString (\r
169 IN OUT CHAR8 *Buffer,\r
170 IN UINTN Flags,\r
171 IN INT64 Value,\r
172 IN UINTN Width,\r
173 IN UINTN Increment\r
174 )\r
175{\r
176 CHAR8 *OriginalBuffer;\r
177 CHAR8 *EndBuffer;\r
178 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
7c905091 179 CHAR8 *ValueBufferPtr;\r
e1f414b6 180 UINTN Count;\r
181 UINTN Digits;\r
182 UINTN Index;\r
183 UINTN Radix;\r
184\r
eceb3a4c
LG
185 //\r
186 // Make sure Buffer is not NULL and Width < MAXIMUM\r
187 //\r
e1f414b6 188 ASSERT (Buffer != NULL);\r
189 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r
190 //\r
191 // Make sure Flags can only contain supported bits.\r
192 //\r
193 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);\r
194\r
195 //\r
196 // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()\r
197 //\r
841c0770 198 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));\r
e1f414b6 199\r
200 OriginalBuffer = Buffer;\r
eceb3a4c
LG
201 \r
202 //\r
203 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r
204 //\r
e1f414b6 205 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
206 Flags &= (~PREFIX_ZERO);\r
207 }\r
eceb3a4c
LG
208 //\r
209 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
210 //\r
e1f414b6 211 if (Width == 0) {\r
212 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
213 }\r
214 //\r
215 // Set the tag for the end of the input Buffer.\r
216 //\r
217 EndBuffer = Buffer + Width * Increment;\r
eceb3a4c
LG
218 \r
219 //\r
220 // Convert decimal negative\r
221 //\r
e1f414b6 222 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r
223 Value = -Value;\r
224 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r
225 Width--;\r
226 }\r
eceb3a4c
LG
227 \r
228 //\r
229 // Count the length of the value string.\r
230 //\r
e1f414b6 231 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r
7c905091 232 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
233 Count = ValueBufferPtr - ValueBuffer;\r
eceb3a4c
LG
234 \r
235 //\r
236 // Append Zero\r
237 //\r
e1f414b6 238 if ((Flags & PREFIX_ZERO) != 0) {\r
239 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
240 }\r
eceb3a4c
LG
241 \r
242 //\r
243 // Print Comma type for every 3 characters\r
244 //\r
e1f414b6 245 Digits = Count % 3;\r
246 if (Digits != 0) {\r
247 Digits = 3 - Digits;\r
248 }\r
249 for (Index = 0; Index < Count; Index++) {\r
7c905091 250 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);\r
e1f414b6 251 if ((Flags & COMMA_TYPE) != 0) {\r
252 Digits++;\r
253 if (Digits == 3) {\r
254 Digits = 0;\r
255 if ((Index + 1) < Count) {\r
256 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r
257 }\r
258 }\r
259 }\r
260 }\r
eceb3a4c
LG
261 \r
262 //\r
263 // Print Null-terminator\r
264 //\r
e1f414b6 265 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
266\r
267 return ((Buffer - OriginalBuffer) / Increment);\r
268}\r
269\r
e389c39b 270/**\r
271 Worker function that produces a Null-terminated string in an output buffer \r
272 based on a Null-terminated format string and a VA_LIST argument list.\r
273\r
274 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 275 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
e389c39b 276 this is the main print working routine.\r
277\r
2075236e 278 @param Buffer Character buffer to print the results of the parsing\r
279 of Format into.\r
280 @param BufferSize Maximum number of characters to put into buffer.\r
281 @param Flags Initial flags value.\r
282 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set.\r
283 @param Format Null-terminated format string.\r
284 @param VaListMarker VA_LIST style variable argument list consumed by processing Format.\r
285 @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format.\r
e389c39b 286\r
287 @return Number of characters printed not including the Null-terminator.\r
288\r
289**/\r
290UINTN\r
2075236e 291BasePrintLibSPrintMarker (\r
e389c39b 292 OUT CHAR8 *Buffer,\r
293 IN UINTN BufferSize,\r
294 IN UINTN Flags,\r
295 IN CONST CHAR8 *Format,\r
2075236e 296 IN VA_LIST VaListMarker, OPTIONAL\r
297 IN BASE_LIST BaseListMarker OPTIONAL\r
e389c39b 298 )\r
299{\r
2075236e 300 CHAR8 *OriginalBuffer;\r
301 CHAR8 *EndBuffer;\r
302 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
303 UINTN BytesPerOutputCharacter;\r
304 UINTN BytesPerFormatCharacter;\r
305 UINTN FormatMask;\r
306 UINTN FormatCharacter;\r
307 UINTN Width;\r
308 UINTN Precision;\r
309 INT64 Value;\r
310 CONST CHAR8 *ArgumentString;\r
311 UINTN Character;\r
312 GUID *TmpGuid;\r
313 TIME *TmpTime;\r
314 UINTN Count;\r
315 UINTN ArgumentMask;\r
316 INTN BytesPerArgumentCharacter;\r
317 UINTN ArgumentCharacter;\r
318 BOOLEAN Done;\r
319 UINTN Index;\r
320 CHAR8 Prefix;\r
321 BOOLEAN ZeroPad;\r
322 BOOLEAN Comma;\r
323 UINTN Digits;\r
324 UINTN Radix;\r
325 RETURN_STATUS Status;\r
e389c39b 326\r
327 if (BufferSize == 0) {\r
328 return 0;\r
329 }\r
330 ASSERT (Buffer != NULL);\r
331\r
332 if ((Flags & OUTPUT_UNICODE) != 0) {\r
333 BytesPerOutputCharacter = 2;\r
334 } else {\r
335 BytesPerOutputCharacter = 1;\r
336 }\r
337\r
338 //\r
339 // Reserve space for the Null terminator.\r
340 //\r
341 BufferSize--;\r
342 OriginalBuffer = Buffer;\r
2075236e 343\r
e389c39b 344 //\r
345 // Set the tag for the end of the input Buffer.\r
346 //\r
347 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
348\r
349 if ((Flags & FORMAT_UNICODE) != 0) {\r
350 //\r
351 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
352 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
353 //\r
354 ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
355 BytesPerFormatCharacter = 2;\r
356 FormatMask = 0xffff;\r
357 } else {\r
358 //\r
359 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r
360 // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r
361 //\r
362 ASSERT (AsciiStrSize (Format) != 0);\r
363 BytesPerFormatCharacter = 1;\r
364 FormatMask = 0xff;\r
365 }\r
366\r
e389c39b 367 //\r
368 // Get the first character from the format string\r
369 //\r
370 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
371\r
372 //\r
373 // Loop until the end of the format string is reached or the output buffer is full\r
374 //\r
375 while (FormatCharacter != 0 && Buffer < EndBuffer) {\r
376 //\r
377 // Clear all the flag bits except those that may have been passed in\r
378 //\r
379 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE);\r
380\r
381 //\r
382 // Set the default width to zero, and the default precision to 1\r
383 //\r
384 Width = 0;\r
385 Precision = 1;\r
386 Prefix = 0;\r
387 Comma = FALSE;\r
388 ZeroPad = FALSE;\r
389 Count = 0;\r
390 Digits = 0;\r
391\r
392 switch (FormatCharacter) {\r
393 case '%':\r
394 //\r
395 // Parse Flags and Width\r
396 //\r
397 for (Done = FALSE; !Done; ) {\r
398 Format += BytesPerFormatCharacter;\r
399 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
400 switch (FormatCharacter) {\r
401 case '.': \r
402 Flags |= PRECISION; \r
403 break;\r
404 case '-': \r
405 Flags |= LEFT_JUSTIFY; \r
406 break;\r
407 case '+': \r
408 Flags |= PREFIX_SIGN; \r
409 break;\r
410 case ' ': \r
411 Flags |= PREFIX_BLANK; \r
412 break;\r
413 case ',': \r
414 Flags |= COMMA_TYPE; \r
415 break;\r
416 case 'L':\r
417 case 'l': \r
418 Flags |= LONG_TYPE; \r
419 break;\r
420 case '*':\r
421 if ((Flags & PRECISION) == 0) {\r
422 Flags |= PAD_TO_WIDTH;\r
2075236e 423 if (BaseListMarker == NULL) {\r
424 Width = VA_ARG (VaListMarker, UINTN);\r
425 } else {\r
426 Width = BASE_ARG (BaseListMarker, UINTN);\r
427 }\r
e389c39b 428 } else {\r
2075236e 429 if (BaseListMarker == NULL) {\r
430 Precision = VA_ARG (VaListMarker, UINTN);\r
431 } else {\r
432 Precision = BASE_ARG (BaseListMarker, UINTN);\r
433 }\r
e389c39b 434 }\r
435 break;\r
436 case '0':\r
437 if ((Flags & PRECISION) == 0) {\r
438 Flags |= PREFIX_ZERO;\r
439 }\r
440 case '1':\r
441 case '2':\r
442 case '3':\r
443 case '4':\r
444 case '5':\r
445 case '6':\r
446 case '7':\r
447 case '8':\r
448 case '9':\r
449 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
450 Count = (Count * 10) + FormatCharacter - '0';\r
451 Format += BytesPerFormatCharacter;\r
452 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
453 }\r
454 Format -= BytesPerFormatCharacter;\r
455 if ((Flags & PRECISION) == 0) {\r
456 Flags |= PAD_TO_WIDTH;\r
457 Width = Count;\r
458 } else {\r
459 Precision = Count;\r
460 }\r
461 break;\r
462 \r
463 case '\0':\r
464 //\r
465 // Make no output if Format string terminates unexpectedly when\r
466 // looking up for flag, width, precision and type. \r
467 //\r
468 Format -= BytesPerFormatCharacter;\r
469 Precision = 0;\r
470 //\r
471 // break skipped on purpose.\r
472 //\r
473 default:\r
474 Done = TRUE;\r
475 break;\r
476 }\r
477 } \r
478\r
479 //\r
480 // Handle each argument type\r
481 //\r
482 switch (FormatCharacter) {\r
483 case 'p':\r
484 //\r
485 // Flag space, +, 0, L & l are invalid for type p.\r
486 //\r
487 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);\r
488 if (sizeof (VOID *) > 4) {\r
489 Flags |= LONG_TYPE;\r
490 }\r
491 case 'X':\r
492 Flags |= PREFIX_ZERO;\r
493 //\r
494 // break skipped on purpose\r
495 //\r
496 case 'x':\r
497 Flags |= RADIX_HEX;\r
498 //\r
499 // break skipped on purpose\r
500 //\r
501 case 'd':\r
502 if ((Flags & LONG_TYPE) == 0) {\r
57dd2382 503 //\r
18e81d77 504 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
505 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 506 // Specification for formatted strings. It is recommended that the Base Types be used \r
507 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
508 // provides an implementation that is compatible with that largest possible set of CPU \r
509 // architectures. This is why the type "int" is used in this one case.\r
510 //\r
2075236e 511 if (BaseListMarker == NULL) {\r
512 Value = VA_ARG (VaListMarker, int);\r
513 } else {\r
514 Value = BASE_ARG (BaseListMarker, int);\r
515 }\r
e389c39b 516 } else {\r
2075236e 517 if (BaseListMarker == NULL) {\r
518 Value = VA_ARG (VaListMarker, INT64);\r
519 } else {\r
520 Value = BASE_ARG (BaseListMarker, INT64);\r
521 }\r
e389c39b 522 }\r
523 if ((Flags & PREFIX_BLANK) != 0) {\r
524 Prefix = ' ';\r
525 }\r
526 if ((Flags & PREFIX_SIGN) != 0) {\r
527 Prefix = '+';\r
528 }\r
529 if ((Flags & COMMA_TYPE) != 0) {\r
530 Comma = TRUE;\r
531 }\r
532 if ((Flags & RADIX_HEX) == 0) {\r
533 Radix = 10;\r
534 if (Comma) {\r
535 Flags &= (~PREFIX_ZERO);\r
536 Precision = 1;\r
537 }\r
538 if (Value < 0) {\r
539 Flags |= PREFIX_SIGN;\r
540 Prefix = '-';\r
541 Value = -Value;\r
542 }\r
543 } else {\r
544 Radix = 16;\r
545 Comma = FALSE;\r
546 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
57dd2382 547 //\r
18e81d77 548 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
549 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 550 // Specification for formatted strings. It is recommended that the Base Types be used \r
551 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
552 // provides an implementation that is compatible with that largest possible set of CPU \r
553 // architectures. This is why the type "unsigned int" is used in this one case.\r
554 //\r
e389c39b 555 Value = (unsigned int)Value;\r
556 }\r
557 }\r
558 //\r
559 // Convert Value to a reversed string\r
560 //\r
7c905091 561 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
e389c39b 562 if (Value == 0 && Precision == 0) {\r
563 Count = 0;\r
564 }\r
565 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
566 \r
567 Digits = Count % 3;\r
568 if (Digits != 0) {\r
569 Digits = 3 - Digits;\r
570 }\r
571 if (Comma && Count != 0) {\r
572 Count += ((Count - 1) / 3);\r
573 }\r
574 if (Prefix != 0) {\r
575 Count++;\r
576 Precision++;\r
577 }\r
578 Flags |= ARGUMENT_REVERSED;\r
579 ZeroPad = TRUE;\r
580 if ((Flags & PREFIX_ZERO) != 0) {\r
581 if ((Flags & LEFT_JUSTIFY) == 0) {\r
582 if ((Flags & PAD_TO_WIDTH) != 0) {\r
583 if ((Flags & PRECISION) == 0) {\r
584 Precision = Width;\r
585 }\r
586 }\r
587 }\r
588 }\r
589 break;\r
590\r
591 case 's':\r
592 case 'S':\r
593 Flags |= ARGUMENT_UNICODE;\r
594 //\r
595 // break skipped on purpose\r
596 //\r
597 case 'a':\r
2075236e 598 if (BaseListMarker == NULL) {\r
599 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
600 } else {\r
601 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
602 }\r
e389c39b 603 if (ArgumentString == NULL) {\r
604 Flags &= (~ARGUMENT_UNICODE);\r
605 ArgumentString = "<null string>";\r
606 }\r
607 //\r
608 // Set the default precision for string to be zero if not specified.\r
609 //\r
610 if ((Flags & PRECISION) == 0) {\r
611 Precision = 0;\r
612 }\r
613 break;\r
614\r
615 case 'c':\r
2075236e 616 if (BaseListMarker == NULL) {\r
617 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
618 } else {\r
619 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
620 }\r
e389c39b 621 ArgumentString = (CHAR8 *)&Character;\r
622 Flags |= ARGUMENT_UNICODE;\r
623 break;\r
624\r
625 case 'g':\r
2075236e 626 if (BaseListMarker == NULL) {\r
627 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
628 } else {\r
629 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
630 }\r
e389c39b 631 if (TmpGuid == NULL) {\r
632 ArgumentString = "<null guid>";\r
633 } else {\r
634 BasePrintLibSPrint (\r
635 ValueBuffer,\r
636 MAXIMUM_VALUE_CHARACTERS, \r
637 0,\r
638 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
639 TmpGuid->Data1,\r
640 TmpGuid->Data2,\r
641 TmpGuid->Data3,\r
642 TmpGuid->Data4[0],\r
643 TmpGuid->Data4[1],\r
644 TmpGuid->Data4[2],\r
645 TmpGuid->Data4[3],\r
646 TmpGuid->Data4[4],\r
647 TmpGuid->Data4[5],\r
648 TmpGuid->Data4[6],\r
649 TmpGuid->Data4[7]\r
650 );\r
651 ArgumentString = ValueBuffer;\r
652 }\r
653 break;\r
654\r
655 case 't':\r
2075236e 656 if (BaseListMarker == NULL) {\r
657 TmpTime = VA_ARG (VaListMarker, TIME *); \r
658 } else {\r
659 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
660 }\r
e389c39b 661 if (TmpTime == NULL) {\r
662 ArgumentString = "<null time>";\r
663 } else {\r
664 BasePrintLibSPrint (\r
665 ValueBuffer,\r
666 MAXIMUM_VALUE_CHARACTERS,\r
667 0,\r
668 "%02d/%02d/%04d %02d:%02d",\r
669 TmpTime->Month,\r
670 TmpTime->Day,\r
671 TmpTime->Year,\r
672 TmpTime->Hour,\r
673 TmpTime->Minute\r
674 );\r
675 ArgumentString = ValueBuffer;\r
676 }\r
677 break;\r
678\r
679 case 'r':\r
2075236e 680 if (BaseListMarker == NULL) {\r
681 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
682 } else {\r
683 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
684 }\r
e389c39b 685 ArgumentString = ValueBuffer;\r
686 if (RETURN_ERROR (Status)) {\r
687 //\r
688 // Clear error bit\r
689 //\r
690 Index = Status & ~MAX_BIT;\r
691 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
692 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
693 }\r
694 } else {\r
695 Index = Status;\r
696 if (Index <= WARNING_STATUS_NUMBER) {\r
697 ArgumentString = mStatusString [Index];\r
698 }\r
699 }\r
700 if (ArgumentString == ValueBuffer) {\r
701 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
702 }\r
703 break;\r
704\r
705 case '\n':\r
706 ArgumentString = "\n\r";\r
707 break;\r
708\r
709 case '%':\r
710 default:\r
711 //\r
712 // if the type is '%' or unknown, then print it to the screen\r
713 //\r
714 ArgumentString = (CHAR8 *)&FormatCharacter;\r
715 Flags |= ARGUMENT_UNICODE;\r
716 break;\r
717 }\r
718 break;\r
719 \r
720 case '\n':\r
721 ArgumentString = "\n\r";\r
722 break;\r
723\r
724 default:\r
725 ArgumentString = (CHAR8 *)&FormatCharacter;\r
726 Flags |= ARGUMENT_UNICODE;\r
727 break;\r
728 }\r
729\r
730 //\r
731 // Retrieve the ArgumentString attriubutes\r
732 //\r
733 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
734 ArgumentMask = 0xffff;\r
735 BytesPerArgumentCharacter = 2;\r
736 } else {\r
737 ArgumentMask = 0xff;\r
738 BytesPerArgumentCharacter = 1;\r
739 }\r
740 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
741 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
742 } else {\r
743 //\r
744 // Compute the number of characters in ArgumentString and store it in Count\r
745 // ArgumentString is either null-terminated, or it contains Precision characters\r
746 //\r
747 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
748 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
749 if (ArgumentCharacter == 0) {\r
750 break;\r
751 }\r
752 }\r
753 }\r
754\r
755 if (Precision < Count) {\r
756 Precision = Count;\r
757 }\r
758\r
759 //\r
760 // Pad before the string\r
761 //\r
762 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
763 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
764 }\r
765\r
766 if (ZeroPad) {\r
767 if (Prefix != 0) {\r
768 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
769 }\r
770 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
771 } else {\r
772 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
773 if (Prefix != 0) {\r
774 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
775 }\r
776 }\r
777\r
778 //\r
779 // Output the Prefix character if it is present\r
780 //\r
781 Index = 0;\r
782 if (Prefix != 0) {\r
783 Index++;\r
784 }\r
785\r
786 //\r
787 // Copy the string into the output buffer performing the required type conversions\r
788 //\r
789 while (Index < Count) {\r
790 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
791\r
792 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
793 ArgumentString += BytesPerArgumentCharacter;\r
794 Index++;\r
795 if (Comma) {\r
796 Digits++;\r
797 if (Digits == 3) {\r
798 Digits = 0;\r
799 Index++;\r
800 if (Index < Count) {\r
801 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
802 }\r
803 }\r
804 }\r
805 }\r
806\r
807 //\r
808 // Pad after the string\r
809 //\r
810 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
811 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
812 }\r
813\r
814 //\r
815 // Get the next character from the format string\r
816 //\r
817 Format += BytesPerFormatCharacter;\r
818\r
819 //\r
820 // Get the next character from the format string\r
821 //\r
822 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
823 }\r
824\r
825 //\r
826 // Null terminate the Unicode or ASCII string\r
827 //\r
828 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
829 //\r
830 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
831 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
832 //\r
833 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
834 //\r
835 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
836 // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
837 //\r
838 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
839\r
840 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
841}\r
842\r
843/**\r
844 Worker function that produces a Null-terminated string in an output buffer \r
845 based on a Null-terminated format string and variable argument list.\r
846\r
847 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 848 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
efb23117 849 this is the main print working routine\r
e389c39b 850\r
851 @param StartOfBuffer Character buffer to print the results of the parsing\r
852 of Format into.\r
853 @param BufferSize Maximum number of characters to put into buffer.\r
854 Zero means no limit.\r
7c905091 855 @param Flags Initial flags value.\r
e389c39b 856 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
857 @param FormatString Null-terminated format string.\r
858 @param ... The variable argument list.\r
859\r
efb23117 860 @return Number of characters printed.\r
e389c39b 861\r
862**/\r
863UINTN\r
864BasePrintLibSPrint (\r
865 OUT CHAR8 *StartOfBuffer,\r
866 IN UINTN BufferSize,\r
867 IN UINTN Flags,\r
868 IN CONST CHAR8 *FormatString,\r
869 ...\r
870 )\r
871{\r
872 VA_LIST Marker;\r
873\r
874 VA_START (Marker, FormatString);\r
2075236e 875 return BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
e389c39b 876}\r