X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ArmPlatformPkg%2FBds%2FBdsHelper.c;h=693bc7d12a1494ae2e87c9581857e88699da6662;hp=b5a3fa91b1cf2b69be9d8ded9ce9422aee07ad7b;hb=cf30b996d5d47835bf72921f351bb34c6790d8be;hpb=9fc9aa46ccf2d942b98d921bb22987fd232f6248 diff --git a/ArmPlatformPkg/Bds/BdsHelper.c b/ArmPlatformPkg/Bds/BdsHelper.c index b5a3fa91b1..693bc7d12a 100644 --- a/ArmPlatformPkg/Bds/BdsHelper.c +++ b/ArmPlatformPkg/Bds/BdsHelper.c @@ -1,6 +1,6 @@ /** @file * -* Copyright (c) 2011-2013, ARM Limited. All rights reserved. +* Copyright (c) 2011 - 2014, ARM Limited. All rights reserved. * * This program and the accompanying materials * are licensed and made available under the terms and conditions of the BSD License @@ -12,6 +12,7 @@ * **/ +#include #include "BdsInternal.h" EFI_STATUS @@ -137,44 +138,99 @@ GetHIInputInteger ( return Status; } +/** + Get an IPv4 address + + The function asks the user for an IPv4 address. If the input + string defines a valid IPv4 address, the four bytes of the + corresponding IPv4 address are extracted from the string and returned by + the function. As long as the user does not define a valid IP + address, he is asked for one. He can always escape by + pressing ESC. + + @param[out] EFI_IP_ADDRESS OutIpAddr Returned IPv4 address. Valid if + and only if the returned value + is equal to EFI_SUCCESS + + @retval EFI_SUCCESS Input completed + @retval EFI_ABORTED Editing aborted by the user + @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to + lack of resource +**/ EFI_STATUS GetHIInputIP ( - OUT EFI_IP_ADDRESS *Ip + OUT EFI_IP_ADDRESS *OutIpAddr ) { - CHAR16 CmdLine[255]; - CHAR16 *Str; EFI_STATUS Status; + CHAR16 CmdLine[48]; - CmdLine[0] = '\0'; - Status = EditHIInputStr (CmdLine,255); - if (!EFI_ERROR(Status)) { - Str = CmdLine; - Ip->v4.Addr[0] = (UINT8)StrDecimalToUintn (Str); - - Str = StrStr (Str, L"."); - if (Str == NULL) { - return EFI_INVALID_PARAMETER; + while (TRUE) { + CmdLine[0] = '\0'; + Status = EditHIInputStr (CmdLine, 48); + if (EFI_ERROR (Status)) { + return EFI_ABORTED; } - Ip->v4.Addr[1] = (UINT8)StrDecimalToUintn (++Str); - - Str = StrStr (Str, L"."); - if (Str == NULL) { - return EFI_INVALID_PARAMETER; + Status = NetLibStrToIp4 (CmdLine, &OutIpAddr->v4); + if (Status == EFI_INVALID_PARAMETER) { + Print (L"Invalid address\n"); + } else { + return Status; } + } +} - Ip->v4.Addr[2] = (UINT8)StrDecimalToUintn (++Str); - - Str = StrStr (Str, L"."); - if (Str == NULL) { - return EFI_INVALID_PARAMETER; +/** + Edit an IPv4 address + + The function displays as a string following the "%d.%d.%d.%d" format the + IPv4 address that is passed in and asks the user to modify it. If the + resulting string defines a valid IPv4 address, the four bytes of the + corresponding IPv4 address are extracted from the string and returned by + the function. As long as the user does not define a valid IP + address, he is asked for one. He can always escape by + pressing ESC. + + @param[in ] EFI_IP_ADDRESS InIpAddr Input IPv4 address + @param[out] EFI_IP_ADDRESS OutIpAddr Returned IPv4 address. Valid if + and only if the returned value + is equal to EFI_SUCCESS + + @retval EFI_SUCCESS Update completed + @retval EFI_ABORTED Editing aborted by the user + @retval EFI_INVALID_PARAMETER The string returned by the user is + mal-formated + @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to + lack of resource +**/ +EFI_STATUS +EditHIInputIP ( + IN EFI_IP_ADDRESS *InIpAddr, + OUT EFI_IP_ADDRESS *OutIpAddr + ) +{ + EFI_STATUS Status; + CHAR16 CmdLine[48]; + + while (TRUE) { + UnicodeSPrint ( + CmdLine, 48, L"%d.%d.%d.%d", + InIpAddr->v4.Addr[0], InIpAddr->v4.Addr[1], + InIpAddr->v4.Addr[2], InIpAddr->v4.Addr[3] + ); + + Status = EditHIInputStr (CmdLine, 48); + if (EFI_ERROR (Status)) { + return EFI_ABORTED; + } + Status = NetLibStrToIp4 (CmdLine, &OutIpAddr->v4); + if (Status == EFI_INVALID_PARAMETER) { + Print (L"Invalid address\n"); + } else { + return Status; } - - Ip->v4.Addr[3] = (UINT8)StrDecimalToUintn (++Str); } - - return Status; } EFI_STATUS @@ -338,3 +394,79 @@ IsUnicodeString ( return FALSE; } } + +/* + * Try to detect if the given string is an ASCII or Unicode string + * + * There are actually few limitation to this function but it is mainly to give + * a user friendly output. + * + * Some limitations: + * - it only supports unicode string that use ASCII character (< 0x100) + * - single character ASCII strings are interpreted as Unicode string + * - string cannot be longer than 2 x BOOT_DEVICE_OPTION_MAX (600 bytes) + * + * @param String Buffer that might contain a Unicode or Ascii string + * @param IsUnicode If not NULL this boolean value returns if the string is an + * ASCII or Unicode string. + */ +BOOLEAN +IsPrintableString ( + IN VOID* String, + OUT BOOLEAN *IsUnicode + ) +{ + BOOLEAN UnicodeDetected; + BOOLEAN IsPrintable; + UINTN Index; + CHAR16 Character; + + // We do not support NULL pointer + ASSERT (String != NULL); + + // Test empty string + if (*(CHAR16*)String == L'\0') { + if (IsUnicode) { + *IsUnicode = TRUE; + } + return TRUE; + } else if (*(CHAR16*)String == '\0') { + if (IsUnicode) { + *IsUnicode = FALSE; + } + return TRUE; + } + + // Limitation: if the string is an ASCII single character string. This comparison + // will assume it is a Unicode string. + if (*(CHAR16*)String < 0x100) { + UnicodeDetected = TRUE; + } else { + UnicodeDetected = FALSE; + } + + IsPrintable = FALSE; + for (Index = 0; Index < BOOT_DEVICE_OPTION_MAX * 2; Index++) { + if (UnicodeDetected) { + Character = ((CHAR16*)String)[Index]; + } else { + Character = ((CHAR8*)String)[Index]; + } + + if (Character == '\0') { + // End of the string + IsPrintable = TRUE; + break; + } else if ((Character < 0x20) || (Character > 0x7f)) { + // We only support the range of printable ASCII character + IsPrintable = FALSE; + break; + } + } + + if (IsPrintable && IsUnicode) { + *IsUnicode = UnicodeDetected; + } + + return IsPrintable; +}