Home | History | Annotate | Download | only in BdsDxe
      1 /** @file
      2   Language settings
      3 
      4 Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
      5 This program and the accompanying materials
      6 are licensed and made available under the terms and conditions of the BSD License
      7 which accompanies this distribution.  The full text of the license may be found at
      8 http://opensource.org/licenses/bsd-license.php
      9 
     10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13 **/
     14 
     15 #include "Bds.h"
     16 #define ISO_639_2_ENTRY_SIZE 3
     17 
     18 /**
     19   Check if lang is in supported language codes according to language string.
     20 
     21   This code is used to check if lang is in in supported language codes. It can handle
     22   RFC4646 and ISO639 language tags.
     23   In ISO639 language tags, take 3-characters as a delimitation to find matched string.
     24   In RFC4646 language tags, take semicolon as a delimitation to find matched string.
     25 
     26   For example:
     27     SupportedLang  = "engfraengfra"
     28     Iso639Language = TRUE
     29     Lang           = "eng", the return value is "TRUE", or
     30     Lang           = "chs", the return value is "FALSE".
     31   Another example:
     32     SupportedLang  = "en;fr;en-US;fr-FR"
     33     Iso639Language = FALSE
     34     Lang           = "en", the return value is "TRUE", or
     35     Lang           = "zh", the return value is "FALSE".
     36 
     37   @param  SupportedLang               Platform supported language codes.
     38   @param  Lang                        Configured language.
     39   @param  Iso639Language              A bool value to signify if the handler is operated on ISO639 or RFC4646.
     40 
     41   @retval TRUE  lang is in supported language codes.
     42   @retval FALSE lang is not in supported language codes.
     43 
     44 **/
     45 BOOLEAN
     46 IsLangInSupportedLangCodes(
     47   IN  CHAR8            *SupportedLang,
     48   IN  CHAR8            *Lang,
     49   IN  BOOLEAN          Iso639Language
     50   )
     51 {
     52   UINTN    Index;
     53   UINTN    CompareLength;
     54   UINTN    LanguageLength;
     55 
     56   if (Iso639Language) {
     57     CompareLength = ISO_639_2_ENTRY_SIZE;
     58     for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
     59       if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
     60         //
     61         // Successfully find the Lang string in SupportedLang string.
     62         //
     63         return TRUE;
     64       }
     65     }
     66     return FALSE;
     67   } else {
     68     //
     69     // Compare RFC4646 language code
     70     //
     71     for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);
     72 
     73     for (; *SupportedLang != '\0'; SupportedLang += CompareLength) {
     74       //
     75       // Skip ';' characters in SupportedLang
     76       //
     77       for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);
     78       //
     79       // Determine the length of the next language code in SupportedLang
     80       //
     81       for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);
     82 
     83       if ((CompareLength == LanguageLength) &&
     84           (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {
     85         //
     86         // Successfully find the Lang string in SupportedLang string.
     87         //
     88         return TRUE;
     89       }
     90     }
     91     return FALSE;
     92   }
     93 }
     94 
     95 /**
     96   Initialize Lang or PlatformLang variable, if Lang or PlatformLang variable is not found,
     97   or it has been set to an unsupported value(not one of platform supported language codes),
     98   set the default language code to it.
     99 
    100   @param  LangName                    Language name, L"Lang" or L"PlatformLang".
    101   @param  SupportedLang               Platform supported language codes.
    102   @param  DefaultLang                 Default language code.
    103   @param  Iso639Language              A bool value to signify if the handler is operated on ISO639 or RFC4646,
    104                                       TRUE for L"Lang" LangName or FALSE for L"PlatformLang" LangName.
    105 
    106 **/
    107 VOID
    108 InitializeLangVariable (
    109   IN CHAR16     *LangName,
    110   IN CHAR8      *SupportedLang,
    111   IN CHAR8      *DefaultLang,
    112   IN BOOLEAN    Iso639Language
    113   )
    114 {
    115   CHAR8       *Lang;
    116 
    117   //
    118   // Find current Lang or PlatformLang from EFI Variable.
    119   //
    120   GetEfiGlobalVariable2 (LangName, (VOID **) &Lang, NULL);
    121 
    122   //
    123   // If Lang or PlatformLang variable is not found,
    124   // or it has been set to an unsupported value(not one of the supported language codes),
    125   // set the default language code to it.
    126   //
    127   if ((Lang == NULL) || !IsLangInSupportedLangCodes (SupportedLang, Lang, Iso639Language)) {
    128     //
    129     // The default language code should be one of the supported language codes.
    130     //
    131     ASSERT (IsLangInSupportedLangCodes (SupportedLang, DefaultLang, Iso639Language));
    132     BdsDxeSetVariableAndReportStatusCodeOnError (
    133       LangName,
    134       &gEfiGlobalVariableGuid,
    135       EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
    136       AsciiStrSize (DefaultLang),
    137       DefaultLang
    138       );
    139   }
    140 
    141   if (Lang != NULL) {
    142     FreePool (Lang);
    143   }
    144 }
    145 
    146 /**
    147   Determine the current language that will be used
    148   based on language related EFI Variables.
    149 
    150   @param LangCodesSettingRequired - If required to set LangCodes variable
    151 
    152 **/
    153 VOID
    154 InitializeLanguage (
    155   BOOLEAN LangCodesSettingRequired
    156   )
    157 {
    158   EFI_STATUS  Status;
    159   CHAR8       *LangCodes;
    160   CHAR8       *PlatformLangCodes;
    161 
    162   LangCodes = (CHAR8 *)PcdGetPtr (PcdUefiVariableDefaultLangCodes);
    163   PlatformLangCodes = (CHAR8 *)PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes);
    164   if (LangCodesSettingRequired) {
    165     if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {
    166       //
    167       // UEFI 2.1 depricated this variable so we support turning it off
    168       //
    169       Status = gRT->SetVariable (
    170                       L"LangCodes",
    171                       &gEfiGlobalVariableGuid,
    172                       EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
    173                       AsciiStrSize (LangCodes),
    174                       LangCodes
    175                       );
    176       //
    177       // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
    178       //
    179       ASSERT_EFI_ERROR(Status);
    180     }
    181 
    182     Status = gRT->SetVariable (
    183                     L"PlatformLangCodes",
    184                     &gEfiGlobalVariableGuid,
    185                     EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
    186                     AsciiStrSize (PlatformLangCodes),
    187                     PlatformLangCodes
    188                     );
    189     //
    190     // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
    191     //
    192     ASSERT_EFI_ERROR(Status);
    193   }
    194 
    195   if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {
    196     //
    197     // UEFI 2.1 depricated this variable so we support turning it off
    198     //
    199     InitializeLangVariable (L"Lang", LangCodes, (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang), TRUE);
    200   }
    201   InitializeLangVariable (L"PlatformLang", PlatformLangCodes, (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang), FALSE);
    202 }
    203