1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  
     | 
    
      /*++
Copyright (c) 2007 - 2011, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution. The full text of the license may be found at         
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             
Module Name:
  HiiSupport.c
Abstract:
  Implements some helper functions for hii operations.
Revision History
--*/
#include "EfiShellLib.h"
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
//
// Hii relative protocols
//
BOOLEAN  mHiiProtocolsInitialized = FALSE;
EFI_HII_DATABASE_PROTOCOL *gLibHiiDatabase = NULL;
EFI_HII_STRING_PROTOCOL   *gLibHiiString = NULL;
EFI_STATUS
LocateHiiProtocols (
  VOID
  )
/*++
Routine Description:
  This function locate Hii relative protocols for later usage.
Arguments:
  None.
Returns:
  Status code.
--*/
{
  EFI_STATUS  Status;
  if (mHiiProtocolsInitialized) {
    return EFI_SUCCESS;
  }
  Status = LibLocateProtocol (&gEfiHiiDatabaseProtocolGuid, (VOID**)&gLibHiiDatabase);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Status = LibLocateProtocol (&gEfiHiiStringProtocolGuid, (VOID**)&gLibHiiString);
  if (EFI_ERROR (Status)) {
    return Status;
  }  
  
  mHiiProtocolsInitialized = TRUE;
  return EFI_SUCCESS;
}
EFI_HII_PACKAGE_LIST_HEADER *
PreparePackageList (
  IN UINTN                    PkgNumber,
  IN EFI_GUID                 *GuidId,
  ...
  )
/*++
Routine Description:
  Assemble EFI_HII_PACKAGE_LIST according to the passed in packages.
Arguments:
  NumberOfPackages  -  Number of packages.
  GuidId            -  Package GUID.
Returns:
  Pointer of EFI_HII_PACKAGE_LIST_HEADER.
--*/
{
  VA_LIST                     Args;
  EFI_HII_PACKAGE_LIST_HEADER *PkgListHdr;
  CHAR8                       *PkgListData;
  UINT32                      PkgListLen;
  UINT32                      PkgLen;
  EFI_HII_PACKAGE_HEADER      PkgHdr = {0, 0};
  UINT8                       *PkgAddr;
  UINTN                       Index;
  PkgListLen = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
  VA_START (Args, GuidId);
  for (Index = 0; Index < PkgNumber; Index++) {
    CopyMem (&PkgLen, VA_ARG (Args, VOID *), sizeof (UINT32));
    PkgListLen += (PkgLen - sizeof (UINT32));
  }
  //
  // Include the lenght of EFI_HII_PACKAGE_END
  //
  PkgListLen += sizeof (EFI_HII_PACKAGE_HEADER);
  VA_END (Args);
  PkgListHdr = AllocateZeroPool (PkgListLen);
  ASSERT (PkgListHdr != NULL);
  CopyMem (&PkgListHdr->PackageListGuid, GuidId, sizeof (EFI_GUID));
  PkgListHdr->PackageLength = PkgListLen;
  PkgListData = (CHAR8 *)PkgListHdr + sizeof (EFI_HII_PACKAGE_LIST_HEADER);
  VA_START (Args, GuidId);
  for (Index = 0; Index < PkgNumber; Index++) {
    PkgAddr = (UINT8 *)VA_ARG (Args, VOID *);
    CopyMem (&PkgLen, PkgAddr, sizeof (UINT32));
    PkgLen  -= sizeof (UINT32);
    PkgAddr += sizeof (UINT32);
    CopyMem (PkgListData, PkgAddr, PkgLen);
    PkgListData += PkgLen;
  }
  VA_END (Args);
  //
  // Append EFI_HII_PACKAGE_END
  //
  PkgHdr.Type = EFI_HII_PACKAGE_END;
  PkgHdr.Length = sizeof (EFI_HII_PACKAGE_HEADER);
  CopyMem (PkgListData, &PkgHdr, PkgHdr.Length);
  return PkgListHdr;
}
CHAR8 *
GetSupportedLanguages (
  IN EFI_HII_HANDLE           HiiHandle
  )
/*++
Routine Description:
  Retrieves a pointer to the a Null-terminated ASCII string containing the list 
  of languages that an HII handle in the HII Database supports.  The returned 
  string is allocated using AllocatePool().  The caller is responsible for freeing
  the returned string using FreePool().  The format of the returned string follows
  the language format assumed the HII Database.
Arguments:
  HiiHandle      - A handle that was previously registered in the HII Database.
Returns:
  A pointer to the Null-terminated ASCII string of supported languages.
  NULL will return when the list of suported languages could not be retrieved.
--*/
{
  EFI_STATUS  Status;
  UINTN       LanguageSize;
  CHAR8       TempSupportedLanguages;
  CHAR8       *SupportedLanguages;
  //
  // Retrieve the size required for the supported languages buffer.
  //
  LanguageSize = 0;
  Status = gLibHiiString->GetLanguages (gLibHiiString, HiiHandle, &TempSupportedLanguages, &LanguageSize);
  //
  // If GetLanguages() returns EFI_SUCCESS for a zero size, 
  // then there are no supported languages registered for HiiHandle.  If GetLanguages() 
  // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
  // in the HII Database
  //
  if (Status != EFI_BUFFER_TOO_SMALL) {
    //
    // Return NULL if the size can not be retrieved, or if HiiHandle is not in the HII Database
    //
    return NULL;
  }
  //
  // Allocate the supported languages buffer.
  //
  SupportedLanguages = AllocateZeroPool (LanguageSize);
  if (SupportedLanguages == NULL) {
    //
    // Return NULL if allocation fails.
    //
    return NULL;
  }
  //
  // Retrieve the supported languages string
  //
  Status = gLibHiiString->GetLanguages (gLibHiiString, HiiHandle, SupportedLanguages, &LanguageSize);
  if (EFI_ERROR (Status)) {
    //
    // Free the buffer and return NULL if the supported languages can not be retrieved.
    //
    FreePool (SupportedLanguages);
    return NULL;
  }
  //
  // Return the Null-terminated ASCII string of supported languages
  //
  return SupportedLanguages;
}
EFI_STATUS
GetCurrentLanguage (
  OUT     CHAR8               *Lang
  )
/*++
Routine Description:
  Determine what is the current language setting
Arguments:
  Lang      - Pointer of system language
Returns:
  Status code
--*/
{
  EFI_STATUS  Status;
  UINTN       Size;
  //
  // Get current language setting
  //
  Size = RFC_3066_ENTRY_SIZE;
  Status = RT->GetVariable (
                 L"PlatformLang",
                 &gEfiGlobalVariableGuid,
                 NULL,
                 &Size,
                 Lang
                 );
  return Status;
}
BOOLEAN
CompareLanguage (
  IN  CHAR8  *Language1,
  IN  CHAR8  *Language2
  )
/*++
Routine Description:
  Compare whether two names of languages are identical.
Arguments:
  Language1 - Name of language 1
  Language2 - Name of language 2
Returns:
  TRUE      - same
  FALSE     - not same
--*/
{
  UINTN Index;
  
  for (Index = 0; (Language1[Index] != 0) && (Language2[Index] != 0); Index++) {
    if (Language1[Index] != Language2[Index]) {
      return FALSE;
    }
  }
  if (((Language1[Index] == 0) && (Language2[Index] == 0))   || 
      ((Language1[Index] == 0) && (Language2[Index] != ';')) ||
      ((Language1[Index] == ';') && (Language2[Index] != 0)) ||
      ((Language1[Index] == ';') && (Language2[Index] != ';'))) {
    return TRUE;
  }
  return FALSE;
}
EFI_STATUS
LibGetString (
  IN  EFI_HII_HANDLE                  PackageList,
  IN  EFI_STRING_ID                   StringId,
  OUT EFI_STRING                      String,
  IN  OUT UINTN                       *StringSize
  )
/*++
  Routine Description:
    This function try to retrieve string from String package of current language.
    If fail, it try to retrieve string from String package of first language it support.
  Arguments:
    PackageList       - The package list in the HII database to search for the specified string.
    StringId          - The string's id, which is unique within PackageList.
    String            - Points to the new null-terminated string.
    StringSize        - On entry, points to the size of the buffer pointed to by String, in bytes. On return,
                        points to the length of the string, in bytes.
  Returns:
    EFI_SUCCESS            - The string was returned successfully.
    EFI_NOT_FOUND          - The string specified by StringId is not available.
    EFI_BUFFER_TOO_SMALL   - The buffer specified by StringLength is too small to hold the string.
    EFI_INVALID_PARAMETER  - The String or StringSize was NULL.
    EFI_UNSUPPORTED        - This operation is not supported since the protocol
                             interface is unavailable.
--*/
{
  EFI_STATUS Status;
  CHAR8      CurrentLang[RFC_3066_ENTRY_SIZE];
  CHAR8      *SupportedLanguages;
  CHAR8      *BestLanguage;
  Status = LocateHiiProtocols ();
  if (EFI_ERROR (Status)) {
    return EFI_UNSUPPORTED;
  }
  
  Status = GetCurrentLanguage (CurrentLang);
  if (EFI_ERROR (Status)) {
    ZeroMem (CurrentLang, sizeof (CurrentLang));
  }
  SupportedLanguages = GetSupportedLanguages (PackageList);
  if (SupportedLanguages == NULL) {
    return EFI_NOT_FOUND;
  }
  //
  // Get the best matching language from SupportedLanguages
  //
  BestLanguage = GetBestLanguage (
                   SupportedLanguages, 
                   FALSE,                                             // RFC 4646 mode
                   CurrentLang,                                       // Highest priority
                   SupportedLanguages,                                // Lowest priority 
                   NULL
                   );
  
  if (BestLanguage == NULL) {
    FreePool (SupportedLanguages);
    return EFI_NOT_FOUND;
  }
  Status = gLibHiiString->GetString (
                            gLibHiiString,
                            BestLanguage,
                            PackageList,
                            StringId,
                            String,
                            StringSize,
                            NULL
                            );
  
  FreePool (SupportedLanguages);
  FreePool (BestLanguage);
  return Status;
}
#endif
 
     |