]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/Dxe/EfiUiLib/EfiUiLib.c
da3904d238baa152464e52fbeb945431f19f9d8c
3 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 Collection of usefull UI functions.
24 #define IS_DIGIT(Ch) (((Ch) >= L'0') && ((Ch) <= L'9'))
30 OUT UINT8
*EndIdx OPTIONAL
35 Parses and converts Unicode string to decimal value.
36 The returned value is 64-bit.
37 The string is expected in decimal format,
38 the string is parsed and format verified.
41 Val - pointer to the variable to store the value to
42 String - string that contains the value to parse and convert
43 EndIdx - index on which the parsing stopped. It points to the
44 first character that was not part of the returned Val.
45 It's valid only if the function returns success.
46 It's optional and it could be NULL.
49 EFI_SUCCESS - if successful
50 EFI_INVALID_PARAMETER - if String is in unexpected format
59 // Iterate upto 20 digits, only so many could fit in the UINT64
61 for (i
= 0; i
<= 20; i
++) {
63 // test if the next character is not a digit
65 if (!IS_DIGIT (String
[i
])) {
67 // If here, there is no more digits,
68 // return with success if there was at least one to process
83 // If here, there is a digit to process
85 TempVal
= MultU64x32 (TempVal
, 10) + String
[i
] - L
'0';
88 // if here, there was some sort of format error
90 return EFI_INVALID_PARAMETER
;
101 Converts frequency in Hz to Unicode string.
102 Three significant digits are delivered.
103 Used for things like processor info display.
106 String - string that will contain the frequency.
107 Val - value to convert, minimum is 100000 i.e., 0.1 MHz.
110 // GC_TODO: function comment is missing 'Returns:'
116 static CHAR16
*FreqUnits
[] = { L
" Hz", L
" kHz", L
" MHz", L
" GHz", L
" THz", L
" PHz" };
119 // Normalize to 9999 or less.
122 while (Val
>= 10000) {
123 Val
= DivU64x32 (Val
, 10, NULL
);
127 // Make it rounded to the nearest, but only by
128 // a .3. This assures that .6 is not rounded.
132 Val
= DivU64x32 (Val
, 10, NULL
);
136 EfiValueToString (String
, Val
, 0, 0);
139 // Get rid of that cursed number!
141 if (!EfiStrCmp (&String
[1], L
"66")) {
145 // Compute index to the units substrings.
147 IdxUnits
= (i
+ 2) / 3;
149 if (IdxUnits
>= (sizeof (FreqUnits
) / sizeof (FreqUnits
)[0])) {
151 // Frequency is too high.
153 EfiStrCpy (String
, L
"OVERFLOW");
157 // Compute the position of the decimal point.
162 // Test if decimal point needs to be inserted.
166 // Save the part after decimal point.
168 EfiStrCpy (HlpStr
, &String
[IdxPoint
]);
173 String
[IdxPoint
] = L
'.';
176 // Reattach the saved part.
178 EfiStrCpy (&String
[IdxPoint
+ 1], HlpStr
);
181 // Clear the insignificant zero.
183 if (String
[3] == L
'0') {
184 String
[4 - IdxPoint
] = L
'\0';
190 EfiStrCat (String
, FreqUnits
[IdxUnits
]);
203 Converts size in bytes to Unicode string.
204 Used for memory/cache size display.
207 String - string that will contain the value
208 Val - value to convert in bytes
211 // GC_TODO: function comment is missing 'Returns:'
215 static CHAR16
*SizeUnits
[] = { L
" B", L
" kB", L
" MB", L
" GB", L
" TB", L
" PB" };
219 for (i
= 0; i
< (sizeof (SizeUnits
) / sizeof (SizeUnits
)[0]); i
++) {
221 DivU64x32 (Val
, 1024, &Rem
);
226 // 2. less than 8k and there are fractions of 1k
228 if ((Val
< 1024) || ((Val
< 8192) && (Rem
!= 0))) {
230 EfiValueToString (String
, Val
, 0, 0);
235 EfiStrCat (String
, SizeUnits
[i
]);
239 // prescale down by 1k with rounding to the nearest
241 Val
= DivU64x32 (Val
+ 511, 1024, NULL
);
244 EfiStrCpy (String
, L
"OVERFLOW");
257 Converts 8 bit version value to Unicode string.
258 The upper nibble contains the upper part, the lower nibble contains the minor part.
259 The output format is <major>.<minor>.
262 String - string that will contain the value
263 Version - value to convert
266 // GC_TODO: function comment is missing 'Returns:'
270 EfiValueToString (String
, 0x0F & Version
, 0, 0);
271 EfiStrCat (String
, L
".");
272 EfiValueToString (HlpStr
, 0x0F & (Version
>> 4), 0, 0);
273 EfiStrCat (String
, HlpStr
);
281 IN EFI_MAC_ADDRESS
*MacAddr
,
287 Converts MAC address to Unicode string.
288 The value is 64-bit and the resulting string will be 12
289 digit hex number in pairs of digits separated by dashes.
292 String - string that will contain the value
293 Val - value to convert
296 // GC_TODO: function comment is missing 'Returns:'
297 // GC_TODO: MacAddr - add argument and description to function comment
298 // GC_TODO: AddrSize - add argument and description to function comment
302 for (i
= 0; i
< AddrSize
; i
++) {
306 MacAddr
->Addr
[i
] & 0xFF,
312 // Terminate the string.
314 String
[2 * AddrSize
] = L
'\0';
322 IN EFI_IPv4_ADDRESS
*Ip4Addr
327 Converts IP v4 address to Unicode string.
328 The value is 64-bit and the resulting string will
329 be four decimal values 0-255 separated by dots.
332 String - string that will contain the value
333 Ip4Addr - value to convert from
336 // GC_TODO: function comment is missing 'Returns:'
343 for (i
= 0; i
< 4; i
++) {
345 EfiValueToString (HlpStr
, Ip4Addr
->Addr
[i
], 0, 0);
346 EfiStrCat (String
, HlpStr
);
349 EfiStrCat (String
, L
".");
358 OUT EFI_IPv4_ADDRESS
*Ip4Addr
,
364 Parses and converts Unicode string to IP v4 address.
365 The value will 64-bit.
366 The string must be four decimal values 0-255 separated by dots.
367 The string is parsed and format verified.
370 Ip4Addr - pointer to the variable to store the value to
371 String - string that contains the value to parse and convert
374 EFI_SUCCESS - if successful
375 EFI_INVALID_PARAMETER - if String contains invalid IP v4 format
381 EFI_IPv4_ADDRESS RetVal
;
389 // Iterate the decimal values separated by dots
391 for (i
= 0; i
< 4; i
++) {
393 // get the value of a decimal
395 Status
= EfiStringToValue (&TempVal
, String
, &Idx
);
396 if ((EFI_ERROR (Status
)) || (TempVal
> 255)) {
400 RetVal
.Addr
[i
] = (UINT8
) TempVal
;
404 // test if it is the last decimal value
407 if (String
[0] != L
'\0') {
409 // the string must end with string termination character
418 // Test for presence of a dot, it is required between the values
420 if (String
++[0] != L
'.') {
425 return EFI_INVALID_PARAMETER
;
430 OUT CHAR16
*UnicodeStr
,
436 Converts ASCII characters to Unicode.
439 UnicodeStr - the Unicode string to be written to. The buffer must be large enough.
440 AsciiStr - The ASCII string to be converted.
443 The address to the Unicode string - same as UnicodeStr.
453 *(UnicodeStr
++) = (CHAR16
) *AsciiStr
;
455 if (*(AsciiStr
++) == '\0') {
464 IN CHAR16
*UnicodeStr
469 Converts ASCII characters to Unicode.
470 Assumes that the Unicode characters are only these defined in the ASCII set.
473 AsciiStr - The ASCII string to be written to. The buffer must be large enough.
474 UnicodeStr - the Unicode string to be converted.
477 The address to the ASCII string - same as AsciiStr.
487 *AsciiStr
= (CHAR8
) *(UnicodeStr
++);
489 if (*(AsciiStr
++) == '\0') {