]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/Dxe/EfiUiLib/EfiUiLib.c
3 Copyright (c) 2007, 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'))
31 OUT UINT8
*EndIdx OPTIONAL
36 Parses and converts Unicode string to decimal value.
37 The returned value is 64-bit.
38 The string is expected in decimal format,
39 the string is parsed and format verified.
40 This function is missing from the libs. One day it maybe
41 replaced with a lib function when it'll become available.
44 Val - pointer to the variable to store the value to
45 String - string that contains the value to parse and convert
46 EndIdx - index on which the parsing stopped. It points to the
47 first character that was not part of the returned Val.
48 It's valid only if the function returns success.
49 It's optional and it could be NULL.
52 EFI_SUCCESS - if successful
53 EFI_INVALID_PARAMETER - if String is in unexpected format
62 // Iterate upto 20 digits, only so many could fit in the UINT64
64 for (i
= 0; i
<= 20; i
++) {
66 // test if the next character is not a digit
68 if (!IS_DIGIT (String
[i
])) {
70 // If here, there is no more digits,
71 // return with success if there was at least one to process
86 // If here, there is a digit to process
88 TempVal
= MultU64x32 (TempVal
, 10) + String
[i
] - L
'0';
91 // if here, there was some sort of format error
93 return EFI_INVALID_PARAMETER
;
104 Converts frequency in Hz to Unicode string.
105 Three significant digits are delivered.
106 Used for things like processor info display.
109 String - string that will contain the frequency.
110 Val - value to convert, minimum is 100000 i.e., 0.1 MHz.
113 // GC_TODO: function comment is missing 'Returns:'
119 static CHAR16
*FreqUnits
[] = { L
" Hz", L
" kHz", L
" MHz", L
" GHz", L
" THz", L
" PHz" };
122 // Normalize to 9999 or less.
125 while (Val
>= 10000) {
126 Val
= DivU64x32 (Val
, 10, NULL
);
130 // Make it rounded to the nearest, but only by
131 // a .3. This assures that .6 is not rounded.
135 Val
= DivU64x32 (Val
, 10, NULL
);
139 EfiValueToString (String
, Val
, 0, 0);
142 // Get rid of that cursed number!
144 if (!EfiStrCmp (&String
[1], L
"66")) {
148 // Compute index to the units substrings.
150 IdxUnits
= (i
+ 2) / 3;
152 if (IdxUnits
>= (sizeof (FreqUnits
) / sizeof (FreqUnits
)[0])) {
154 // Frequency is too high.
156 EfiStrCpy (String
, L
"OVERFLOW");
160 // Compute the position of the decimal point.
165 // Test if decimal point needs to be inserted.
169 // Save the part after decimal point.
171 EfiStrCpy (HlpStr
, &String
[IdxPoint
]);
176 String
[IdxPoint
] = L
'.';
179 // Reattach the saved part.
181 EfiStrCpy (&String
[IdxPoint
+ 1], HlpStr
);
184 // Clear the insignificant zero.
186 if (String
[3] == L
'0') {
187 String
[4 - IdxPoint
] = L
'\0';
193 EfiStrCat (String
, FreqUnits
[IdxUnits
]);
206 Converts size in bytes to Unicode string.
207 Used for memory/cache size display.
210 String - string that will contain the value
211 Val - value to convert in bytes
214 // GC_TODO: function comment is missing 'Returns:'
218 static CHAR16
*SizeUnits
[] = { L
" B", L
" kB", L
" MB", L
" GB", L
" TB", L
" PB" };
220 for (i
= 0; i
< (sizeof (SizeUnits
) / sizeof (SizeUnits
)[0]); i
++) {
222 DivU64x32 (Val
, 1024, &Rem
);
227 // 2. less than 8k and there are fractions of 1k
229 if ((Val
< 1024) || ((Val
< 8192) && (Rem
!= 0))) {
231 EfiValueToString (String
, Val
, 0, 0);
236 EfiStrCat (String
, SizeUnits
[i
]);
240 // prescale down by 1k with rounding to the nearest
242 Val
= DivU64x32 (Val
+ 511, 1024, NULL
);
245 EfiStrCpy (String
, L
"OVERFLOW");
258 Converts 8 bit version value to Unicode string.
259 The upper nibble contains the upper part, the lower nibble contains the minor part.
260 The output format is <major>.<minor>.
263 String - string that will contain the value
264 Version - value to convert
267 // GC_TODO: function comment is missing 'Returns:'
271 EfiValueToString (String
, 0x0F & Version
, 0, 0);
272 EfiStrCat (String
, L
".");
273 EfiValueToString (HlpStr
, 0x0F & (Version
>> 4), 0, 0);
274 EfiStrCat (String
, HlpStr
);
282 IN EFI_MAC_ADDRESS
*MacAddr
,
288 Converts MAC address to Unicode string.
289 The value is 64-bit and the resulting string will be 12
290 digit hex number in pairs of digits separated by dashes.
293 String - string that will contain the value
294 Val - value to convert
297 // GC_TODO: function comment is missing 'Returns:'
298 // GC_TODO: MacAddr - add argument and description to function comment
299 // GC_TODO: AddrSize - add argument and description to function comment
303 for (i
= 0; i
< AddrSize
; i
++) {
307 MacAddr
->Addr
[i
] & 0xFF,
313 // Terminate the string.
315 String
[2 * AddrSize
] = L
'\0';
323 IN EFI_IPv4_ADDRESS
*Ip4Addr
328 Converts IP v4 address to Unicode string.
329 The value is 64-bit and the resulting string will
330 be four decimal values 0-255 separated by dots.
333 String - string that will contain the value
334 Ip4Addr - value to convert from
337 // GC_TODO: function comment is missing 'Returns:'
344 for (i
= 0; i
< 4; i
++) {
346 EfiValueToString (HlpStr
, Ip4Addr
->Addr
[i
], 0, 0);
347 EfiStrCat (String
, HlpStr
);
350 EfiStrCat (String
, L
".");
359 OUT EFI_IPv4_ADDRESS
*Ip4Addr
,
365 Parses and converts Unicode string to IP v4 address.
366 The value will 64-bit.
367 The string must be four decimal values 0-255 separated by dots.
368 The string is parsed and format verified.
371 Ip4Addr - pointer to the variable to store the value to
372 String - string that contains the value to parse and convert
375 EFI_SUCCESS - if successful
376 EFI_INVALID_PARAMETER - if String contains invalid IP v4 format
382 EFI_IPv4_ADDRESS RetVal
;
390 // Iterate the decimal values separated by dots
392 for (i
= 0; i
< 4; i
++) {
394 // get the value of a decimal
396 Status
= EfiStringToValue (&TempVal
, String
, &Idx
);
397 if ((EFI_ERROR (Status
)) || (TempVal
> 255)) {
401 RetVal
.Addr
[i
] = (UINT8
) TempVal
;
405 // test if it is the last decimal value
408 if (String
[0] != L
'\0') {
410 // the string must end with string termination character
419 // Test for presence of a dot, it is required between the values
421 if (String
++[0] != L
'.') {
426 return EFI_INVALID_PARAMETER
;
431 OUT CHAR16
*UnicodeStr
,
437 Converts ASCII characters to Unicode.
440 UnicodeStr - the Unicode string to be written to. The buffer must be large enough.
441 AsciiStr - The ASCII string to be converted.
444 The address to the Unicode string - same as UnicodeStr.
454 *(UnicodeStr
++) = (CHAR16
) *AsciiStr
;
456 if (*(AsciiStr
++) == '\0') {
465 IN CHAR16
*UnicodeStr
470 Converts ASCII characters to Unicode.
471 Assumes that the Unicode characters are only these defined in the ASCII set.
474 AsciiStr - The ASCII string to be written to. The buffer must be large enough.
475 UnicodeStr - the Unicode string to be converted.
478 The address to the ASCII string - same as AsciiStr.
488 *AsciiStr
= (CHAR8
) *(UnicodeStr
++);
490 if (*(AsciiStr
++) == '\0') {