+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
- EfiUiLib.c\r
-\r
-Abstract:\r
- Collection of usefull UI functions.\r
-\r
-Revision History:\r
-\r
---*/\r
-\r
-#include "EfiUiLib.h"\r
-\r
-#define IS_DIGIT(Ch) (((Ch) >= L'0') && ((Ch) <= L'9'))\r
-\r
-EFI_STATUS\r
-EfiStringToValue (\r
- OUT UINT64 *Val,\r
- IN CHAR16 *String,\r
- OUT UINT8 *EndIdx OPTIONAL\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Parses and converts Unicode string to decimal value.\r
- The returned value is 64-bit.\r
- The string is expected in decimal format,\r
- the string is parsed and format verified.\r
-\r
-Arguments:\r
- Val - pointer to the variable to store the value to\r
- String - string that contains the value to parse and convert\r
- EndIdx - index on which the parsing stopped. It points to the\r
- first character that was not part of the returned Val.\r
- It's valid only if the function returns success.\r
- It's optional and it could be NULL.\r
-\r
-Returns:\r
- EFI_SUCCESS - if successful\r
- EFI_INVALID_PARAMETER - if String is in unexpected format\r
-\r
---*/\r
-{\r
- UINT8 i;\r
- UINT64 TempVal;\r
-\r
- TempVal = 0;\r
- //\r
- // Iterate upto 20 digits, only so many could fit in the UINT64\r
- //\r
- for (i = 0; i <= 20; i++) {\r
- //\r
- // test if the next character is not a digit\r
- //\r
- if (!IS_DIGIT (String[i])) {\r
- //\r
- // If here, there is no more digits,\r
- // return with success if there was at least one to process\r
- //\r
- if (i == 0) {\r
- break;\r
- }\r
-\r
- *Val = TempVal;\r
-\r
- if (EndIdx != NULL) {\r
- *EndIdx = i;\r
- }\r
-\r
- return EFI_SUCCESS;\r
- }\r
- //\r
- // If here, there is a digit to process\r
- //\r
- TempVal = MultU64x32 (TempVal, 10) + String[i] - L'0';\r
- }\r
- //\r
- // if here, there was some sort of format error\r
- //\r
- return EFI_INVALID_PARAMETER;\r
-}\r
-\r
-CHAR16 *\r
-StrHzToString (\r
- OUT CHAR16 *String,\r
- IN UINT64 Val\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts frequency in Hz to Unicode string. \r
- Three significant digits are delivered. \r
- Used for things like processor info display.\r
-\r
-Arguments:\r
- String - string that will contain the frequency.\r
- Val - value to convert, minimum is 100000 i.e., 0.1 MHz.\r
-\r
---*/\r
-// GC_TODO: function comment is missing 'Returns:'\r
-{\r
- CHAR16 HlpStr[8];\r
- UINT32 i;\r
- UINT32 IdxPoint;\r
- UINT32 IdxUnits;\r
- static CHAR16 *FreqUnits[] = { L" Hz", L" kHz", L" MHz", L" GHz", L" THz", L" PHz" };\r
-\r
- //\r
- // Normalize to 9999 or less.\r
- //\r
- i = 0;\r
- while (Val >= 10000) {\r
- Val = DivU64x32 (Val, 10, NULL);\r
- i++;\r
- }\r
- //\r
- // Make it rounded to the nearest, but only by\r
- // a .3. This assures that .6 is not rounded.\r
- //\r
- if (Val >= 1000) {\r
- Val += 3;\r
- Val = DivU64x32 (Val, 10, NULL);\r
- i++;\r
- }\r
-\r
- EfiValueToString (String, Val, 0, 0);\r
-\r
- //\r
- // Get rid of that cursed number!\r
- //\r
- if (!EfiStrCmp (&String[1], L"66")) {\r
- String[2] = L'7';\r
- }\r
- //\r
- // Compute index to the units substrings.\r
- //\r
- IdxUnits = (i + 2) / 3;\r
-\r
- if (IdxUnits >= (sizeof (FreqUnits) / sizeof (FreqUnits)[0])) {\r
- //\r
- // Frequency is too high.\r
- //\r
- EfiStrCpy (String, L"OVERFLOW");\r
- return String;\r
- }\r
- //\r
- // Compute the position of the decimal point.\r
- //\r
- IdxPoint = i % 3;\r
-\r
- //\r
- // Test if decimal point needs to be inserted.\r
- //\r
- if (IdxPoint != 0) {\r
- //\r
- // Save the part after decimal point.\r
- //\r
- EfiStrCpy (HlpStr, &String[IdxPoint]);\r
-\r
- //\r
- // Insert the point.\r
- //\r
- String[IdxPoint] = L'.';\r
-\r
- //\r
- // Reattach the saved part.\r
- //\r
- EfiStrCpy (&String[IdxPoint + 1], HlpStr);\r
-\r
- //\r
- // Clear the insignificant zero.\r
- //\r
- if (String[3] == L'0') {\r
- String[4 - IdxPoint] = L'\0';\r
- }\r
- }\r
- //\r
- // Attach units.\r
- //\r
- EfiStrCat (String, FreqUnits[IdxUnits]);\r
-\r
- return String;\r
-}\r
-\r
-CHAR16 *\r
-StrBytesToString (\r
- OUT CHAR16 *String,\r
- IN UINT64 Val\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts size in bytes to Unicode string.\r
- Used for memory/cache size display.\r
-\r
-Arguments:\r
- String - string that will contain the value\r
- Val - value to convert in bytes\r
-\r
---*/\r
-// GC_TODO: function comment is missing 'Returns:'\r
-{\r
- UINTN i;\r
- UINTN Rem;\r
- static CHAR16 *SizeUnits[] = { L" B", L" kB", L" MB", L" GB", L" TB", L" PB" };\r
-\r
- Rem = 0;\r
-\r
- for (i = 0; i < (sizeof (SizeUnits) / sizeof (SizeUnits)[0]); i++) {\r
-\r
- DivU64x32 (Val, 1024, &Rem);\r
-\r
- //\r
- // Done if:\r
- // 1. less than 1k\r
- // 2. less than 8k and there are fractions of 1k\r
- //\r
- if ((Val < 1024) || ((Val < 8192) && (Rem != 0))) {\r
-\r
- EfiValueToString (String, Val, 0, 0);\r
-\r
- //\r
- // attach units\r
- //\r
- EfiStrCat (String, SizeUnits[i]);\r
- return String;\r
- }\r
- //\r
- // prescale down by 1k with rounding to the nearest\r
- //\r
- Val = DivU64x32 (Val + 511, 1024, NULL);\r
- }\r
-\r
- EfiStrCpy (String, L"OVERFLOW");\r
-\r
- return String;\r
-}\r
-\r
-CHAR16 *\r
-StrVersionToString (\r
- OUT CHAR16 *String,\r
- IN UINT8 Version\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts 8 bit version value to Unicode string.\r
- The upper nibble contains the upper part, the lower nibble contains the minor part.\r
- The output format is <major>.<minor>.\r
-\r
-Arguments:\r
- String - string that will contain the value\r
- Version - value to convert\r
-\r
---*/\r
-// GC_TODO: function comment is missing 'Returns:'\r
-{\r
- CHAR16 HlpStr[4];\r
-\r
- EfiValueToString (String, 0x0F & Version, 0, 0);\r
- EfiStrCat (String, L".");\r
- EfiValueToString (HlpStr, 0x0F & (Version >> 4), 0, 0);\r
- EfiStrCat (String, HlpStr);\r
-\r
- return String;\r
-}\r
-\r
-CHAR16 *\r
-StrMacToString (\r
- OUT CHAR16 *String,\r
- IN EFI_MAC_ADDRESS *MacAddr,\r
- IN UINT32 AddrSize\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts MAC address to Unicode string.\r
- The value is 64-bit and the resulting string will be 12\r
- digit hex number in pairs of digits separated by dashes.\r
-\r
-Arguments:\r
- String - string that will contain the value\r
- Val - value to convert\r
-\r
---*/\r
-// GC_TODO: function comment is missing 'Returns:'\r
-// GC_TODO: MacAddr - add argument and description to function comment\r
-// GC_TODO: AddrSize - add argument and description to function comment\r
-{\r
- UINT32 i;\r
-\r
- for (i = 0; i < AddrSize; i++) {\r
-\r
- EfiValueToHexStr (\r
- &String[2 * i],\r
- MacAddr->Addr[i] & 0xFF,\r
- PREFIX_ZERO,\r
- 2\r
- );\r
- }\r
- //\r
- // Terminate the string.\r
- //\r
- String[2 * AddrSize] = L'\0';\r
-\r
- return String;\r
-}\r
-\r
-CHAR16 *\r
-StrIp4AdrToString (\r
- OUT CHAR16 *String,\r
- IN EFI_IPv4_ADDRESS *Ip4Addr\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts IP v4 address to Unicode string.\r
- The value is 64-bit and the resulting string will\r
- be four decimal values 0-255 separated by dots.\r
-\r
-Arguments:\r
- String - string that will contain the value\r
- Ip4Addr - value to convert from\r
-\r
---*/\r
-// GC_TODO: function comment is missing 'Returns:'\r
-{\r
- INT32 i;\r
- CHAR16 HlpStr[4];\r
-\r
- String[0] = L'\0';\r
-\r
- for (i = 0; i < 4; i++) {\r
-\r
- EfiValueToString (HlpStr, Ip4Addr->Addr[i], 0, 0);\r
- EfiStrCat (String, HlpStr);\r
-\r
- if (i < 3) {\r
- EfiStrCat (String, L".");\r
- }\r
- }\r
-\r
- return String;\r
-}\r
-\r
-EFI_STATUS\r
-StrStringToIp4Adr (\r
- OUT EFI_IPv4_ADDRESS *Ip4Addr,\r
- IN CHAR16 *String\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Parses and converts Unicode string to IP v4 address.\r
- The value will 64-bit.\r
- The string must be four decimal values 0-255 separated by dots.\r
- The string is parsed and format verified.\r
-\r
-Arguments:\r
- Ip4Addr - pointer to the variable to store the value to\r
- String - string that contains the value to parse and convert\r
-\r
-Returns:\r
- EFI_SUCCESS - if successful\r
- EFI_INVALID_PARAMETER - if String contains invalid IP v4 format\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\r
-\r
- EFI_IPv4_ADDRESS RetVal;\r
- UINT64 TempVal;\r
- UINT8 Idx;\r
- UINT8 i;\r
-\r
- Idx = 0;\r
- TempVal = 0;\r
- //\r
- // Iterate the decimal values separated by dots\r
- //\r
- for (i = 0; i < 4; i++) {\r
- //\r
- // get the value of a decimal\r
- //\r
- Status = EfiStringToValue (&TempVal, String, &Idx);\r
- if ((EFI_ERROR (Status)) || (TempVal > 255)) {\r
- break;\r
- }\r
-\r
- RetVal.Addr[i] = (UINT8) TempVal;\r
- String += Idx;\r
-\r
- //\r
- // test if it is the last decimal value\r
- //\r
- if (i == 3) {\r
- if (String[0] != L'\0') {\r
- //\r
- // the string must end with string termination character\r
- //\r
- break;\r
- }\r
-\r
- *Ip4Addr = RetVal;\r
- return EFI_SUCCESS;\r
- }\r
- //\r
- // Test for presence of a dot, it is required between the values\r
- //\r
- if (String++[0] != L'.') {\r
- break;\r
- }\r
- }\r
-\r
- return EFI_INVALID_PARAMETER;\r
-}\r
-\r
-CHAR16 *\r
-Ascii2Unicode (\r
- OUT CHAR16 *UnicodeStr,\r
- IN CHAR8 *AsciiStr\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts ASCII characters to Unicode.\r
-\r
-Arguments:\r
- UnicodeStr - the Unicode string to be written to. The buffer must be large enough.\r
- AsciiStr - The ASCII string to be converted.\r
-\r
-Returns:\r
- The address to the Unicode string - same as UnicodeStr.\r
-\r
---*/\r
-{\r
- CHAR16 *Str;\r
-\r
- Str = UnicodeStr;\r
-\r
- while (TRUE) {\r
-\r
- *(UnicodeStr++) = (CHAR16) *AsciiStr;\r
-\r
- if (*(AsciiStr++) == '\0') {\r
- return Str;\r
- }\r
- }\r
-}\r
-\r
-CHAR8 *\r
-Unicode2Ascii (\r
- OUT CHAR8 *AsciiStr,\r
- IN CHAR16 *UnicodeStr\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Converts ASCII characters to Unicode.\r
- Assumes that the Unicode characters are only these defined in the ASCII set.\r
-\r
-Arguments:\r
- AsciiStr - The ASCII string to be written to. The buffer must be large enough.\r
- UnicodeStr - the Unicode string to be converted.\r
-\r
-Returns:\r
- The address to the ASCII string - same as AsciiStr.\r
-\r
---*/\r
-{\r
- CHAR8 *Str;\r
-\r
- Str = AsciiStr;\r
-\r
- while (TRUE) {\r
-\r
- *AsciiStr = (CHAR8) *(UnicodeStr++);\r
-\r
- if (*(AsciiStr++) == '\0') {\r
- return Str;\r
- }\r
- }\r
-}\r