1/** @file
2  This file implements the protocol functions related to string package.
3
4Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5This program and the accompanying materials
6are licensed and made available under the terms and conditions of the BSD License
7which accompanies this distribution.  The full text of the license may be found at
8http://opensource.org/licenses/bsd-license.php
9
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15
16#include "HiiDatabase.h"
17
18/**
19  Test if all of the characters in a string have corresponding font characters.
20
21  This is a deprecated API. No Framework HII module is calling it. This function will ASSERT and
22  return EFI_UNSUPPORTED.
23
24  @param This            A pointer to the EFI_HII_PROTOCOL instance.
25  @param StringToTest    A pointer to a Unicode string.
26  @param FirstMissing    A pointer to an index into the string. On input, the index of
27                         the first character in the StringToTest to examine. On exit, the index
28                         of the first character encountered for which a glyph is unavailable.
29                         If all glyphs in the string are available, the index is the index of the terminator
30                         of the string.
31  @param GlyphBufferSize A pointer to a value. On output, if the function returns EFI_SUCCESS,
32                         it contains the amount of memory that is required to store the string? glyph equivalent.
33
34  @retval EFI_UNSUPPORTED  The function performs nothing and return EFI_UNSUPPORTED.
35**/
36EFI_STATUS
37EFIAPI
38HiiTestString (
39  IN     EFI_HII_PROTOCOL   *This,
40  IN     CHAR16             *StringToTest,
41  IN OUT UINT32             *FirstMissing,
42  OUT    UINT32             *GlyphBufferSize
43  )
44{
45  ASSERT (FALSE);
46
47  return EFI_UNSUPPORTED;
48}
49
50
51/**
52  Find the corressponding TAG GUID from a Framework HII Handle given.
53
54  @param Private      The HII Thunk Module Private context.
55  @param FwHiiHandle  The Framemwork HII Handle.
56  @param TagGuid      The output of TAG GUID found.
57
58  @return NULL        If Framework HII Handle is invalid.
59  @return The corresponding HII Thunk Context.
60**/
61EFI_STATUS
62GetTagGuidByFwHiiHandle (
63  IN  CONST HII_THUNK_PRIVATE_DATA      *Private,
64  IN        FRAMEWORK_EFI_HII_HANDLE    FwHiiHandle,
65  OUT       EFI_GUID                    *TagGuid
66  )
67{
68  LIST_ENTRY                                *Link;
69  HII_THUNK_CONTEXT                          *ThunkContext;
70
71  ASSERT (TagGuid != NULL);
72
73  Link = GetFirstNode (&Private->ThunkContextListHead);
74  while (!IsNull (&Private->ThunkContextListHead, Link)) {
75
76    ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
77
78    if (FwHiiHandle == ThunkContext->FwHiiHandle) {
79      CopyGuid (TagGuid, &ThunkContext->TagGuid);
80      return EFI_SUCCESS;
81    }
82
83    Link = GetNextNode (&Private->ThunkContextListHead, Link);
84  }
85
86  return EFI_NOT_FOUND;
87}
88
89/**
90  Create or update the String given a new string and String ID.
91
92  @param ThunkContext           The Thunk Context.
93  @param Rfc4646AsciiLanguage   The RFC 4646 Language code in ASCII string format.
94  @param NewString              The new string.
95  @param StringId               The String ID. If StringId is 0, a new String Token
96                                is created. Otherwise, the String Token StringId is
97                                updated.
98
99
100  @retval EFI_SUCCESS           The new string is created or updated successfully.
101                                The new String Token ID is returned in StringId if
102                                *StringId is 0 on input.
103  @return Others                The update of string failed.
104
105**/
106EFI_STATUS
107UpdateString (
108  IN CONST HII_THUNK_CONTEXT        *ThunkContext,
109  IN CONST CHAR8                    *Rfc4646AsciiLanguage,
110  IN       CHAR16                   *NewString,
111  IN OUT STRING_REF                 *StringId
112  )
113{
114  EFI_STRING_ID                             NewStringId;
115
116  NewStringId = HiiSetString (ThunkContext->UefiHiiHandle, *StringId, NewString, Rfc4646AsciiLanguage);
117  *StringId = NewStringId;
118  if (NewStringId == 0) {
119    //
120    // Only EFI_INVALID_PARAMETER is defined in HII 0.92 specification.
121    //
122    return EFI_INVALID_PARAMETER;
123  } else {
124    return EFI_SUCCESS;
125  }
126}
127
128/**
129  Create or update a String Token in a String Package.
130
131  If *Reference == 0, a new String Token is created.
132
133  @param This            A pointer to the EFI_HII_PROTOCOL instance.
134  @param Language        Pointer to a NULL-terminated string containing a single ISO 639-2 language
135                         identifier, indicating the language to print. A string consisting of
136                         all spaces indicates that the string is applicable to all languages.
137  @param Handle          The handle of the language pack to which the string is to be added.
138  @param Reference       The string token assigned to the string.
139  @param NewString       The string to be added.
140
141
142  @retval EFI_SUCCESS             The string was effectively registered.
143  @retval EFI_INVALID_PARAMETER   The Handle was unknown. The string is not created or updated in the
144                                  the string package.
145**/
146EFI_STATUS
147EFIAPI
148HiiNewString (
149  IN     EFI_HII_PROTOCOL           *This,
150  IN     CHAR16                     *Language,
151  IN     FRAMEWORK_EFI_HII_HANDLE   Handle,
152  IN OUT STRING_REF                 *Reference,
153  IN     CHAR16                     *NewString
154  )
155{
156  EFI_STATUS                                Status;
157  HII_THUNK_PRIVATE_DATA                    *Private;
158  EFI_GUID                                  TagGuid;
159  LIST_ENTRY                                *Link;
160  HII_THUNK_CONTEXT                          *ThunkContext;
161  HII_THUNK_CONTEXT                          *StringPackThunkContext;
162  EFI_STRING_ID                             StringId;
163  EFI_STRING_ID                             LastStringId;
164  CHAR8                                     AsciiLanguage[ISO_639_2_ENTRY_SIZE + 1];
165  CHAR16                                    LanguageCopy[ISO_639_2_ENTRY_SIZE + 1];
166  CHAR8                                     *Rfc4646AsciiLanguage;
167
168  LastStringId      = (EFI_STRING_ID) 0;
169  StringId          = (EFI_STRING_ID) 0;
170  Rfc4646AsciiLanguage = NULL;
171
172  if (Language != NULL) {
173    ZeroMem (AsciiLanguage, sizeof (AsciiLanguage));;
174    ZeroMem (LanguageCopy, sizeof (LanguageCopy));
175    CopyMem (LanguageCopy, Language, ISO_639_2_ENTRY_SIZE * sizeof (CHAR16));
176    UnicodeStrToAsciiStr (LanguageCopy, AsciiLanguage);
177    Rfc4646AsciiLanguage = ConvertLanguagesIso639ToRfc4646 (AsciiLanguage);
178    ASSERT (Rfc4646AsciiLanguage != NULL);
179  }
180
181  Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
182
183  StringPackThunkContext = FwHiiHandleToThunkContext (Private, Handle);
184  if (StringPackThunkContext == NULL) {
185    return EFI_INVALID_PARAMETER;
186  }
187
188  if (StringPackThunkContext->SharingStringPack) {
189    Status = GetTagGuidByFwHiiHandle (Private, Handle, &TagGuid);
190    ASSERT_EFI_ERROR (Status);
191
192    Link = GetFirstNode (&Private->ThunkContextListHead);
193    while (!IsNull (&Private->ThunkContextListHead, Link)) {
194      ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
195
196      if (CompareGuid (&TagGuid, &ThunkContext->TagGuid)) {
197        if (ThunkContext->SharingStringPack) {
198          StringId = *Reference;
199          Status = UpdateString (ThunkContext, Rfc4646AsciiLanguage, NewString, &StringId);
200          if (EFI_ERROR (Status)) {
201            break;
202          }
203
204          DEBUG_CODE_BEGIN ();
205          if (*Reference == 0) {
206            //
207            // When creating new string token, make sure all created token is the same
208            // for all string packages registered using FW HII interface.
209            //
210            if (LastStringId == (EFI_STRING_ID) 0) {
211              LastStringId = StringId;
212            } else {
213              if (LastStringId != StringId) {
214                ASSERT(FALSE);
215              }
216            }
217          }
218          DEBUG_CODE_END ();
219
220        }
221      }
222
223      Link = GetNextNode (&Private->ThunkContextListHead, Link);
224    }
225  } else {
226    StringId = *Reference;
227    Status = UpdateString (StringPackThunkContext, Rfc4646AsciiLanguage, NewString, &StringId);
228  }
229
230  if (!EFI_ERROR (Status)) {
231    if (*Reference == 0) {
232      *Reference = StringId;
233    }
234  } else {
235    //
236    // Only EFI_INVALID_PARAMETER is defined in HII 0.92 specification.
237    //
238    Status = EFI_INVALID_PARAMETER;
239  }
240
241  return Status;
242}
243
244/**
245  This function removes any new strings that were added after the initial string export for this handle.
246  UEFI HII String Protocol does not have Reset String function. This function perform nothing.
247
248  @param This            A pointer to the EFI_HII_PROTOCOL instance.
249  @param Handle          The HII handle on which the string resides.
250
251  @retval EFI_SUCCESS    This function is a NOP and always return EFI_SUCCESS.
252
253**/
254EFI_STATUS
255EFIAPI
256HiiResetStrings (
257  IN     EFI_HII_PROTOCOL   *This,
258  IN     FRAMEWORK_EFI_HII_HANDLE      Handle
259  )
260{
261  return EFI_SUCCESS;
262}
263
264/**
265 This function extracts a string from a package already registered with the EFI HII database.
266
267  @param This            A pointer to the EFI_HII_PROTOCOL instance.
268  @param Handle          The HII handle on which the string resides.
269  @param Token           The string token assigned to the string.
270  @param Raw             If TRUE, the string is returned unedited in the internal storage format described
271                         above. If false, the string returned is edited by replacing <cr> with <space>
272                         and by removing special characters such as the <wide> prefix.
273  @param LanguageString  Pointer to a NULL-terminated string containing a single ISO 639-2 language
274                         identifier, indicating the language to print. If the LanguageString is empty (starts
275                         with a NULL), the default system language will be used to determine the language.
276  @param BufferLength    Length of the StringBuffer. If the status reports that the buffer width is too
277                         small, this parameter is filled with the length of the buffer needed.
278  @param StringBuffer    The buffer designed to receive the characters in the string. Type EFI_STRING is
279                         defined in String.
280
281  @retval EFI_INVALID_PARAMETER If input parameter is invalid.
282  @retval EFI_BUFFER_TOO_SMALL  If the *BufferLength is too small.
283  @retval EFI_SUCCESS           Operation is successful.
284
285**/
286EFI_STATUS
287EFIAPI
288HiiThunkGetString (
289  IN     EFI_HII_PROTOCOL           *This,
290  IN     FRAMEWORK_EFI_HII_HANDLE   Handle,
291  IN     STRING_REF                 Token,
292  IN     BOOLEAN                    Raw,
293  IN     CHAR16                     *LanguageString,
294  IN OUT UINTN                      *BufferLength,
295  OUT    EFI_STRING                 StringBuffer
296  )
297{
298  HII_THUNK_PRIVATE_DATA                *Private;
299  CHAR8                                 *Iso639AsciiLanguage;
300  CHAR8                                 *Rfc4646AsciiLanguage;
301  CHAR8                                 *SupportedLanguages;
302  CHAR8                                 *PlatformLanguage;
303  CHAR8                                 *BestLanguage;
304  EFI_HII_HANDLE                        UefiHiiHandle;
305  EFI_STATUS                            Status;
306
307  Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
308
309  Rfc4646AsciiLanguage = NULL;
310  SupportedLanguages   = NULL;
311  PlatformLanguage     = NULL;
312  Status               = EFI_SUCCESS;
313
314  if (LanguageString != NULL) {
315    Iso639AsciiLanguage = AllocateZeroPool (StrLen (LanguageString) + 1);
316    if (Iso639AsciiLanguage == NULL) {
317      return EFI_OUT_OF_RESOURCES;
318    }
319    UnicodeStrToAsciiStr  (LanguageString, Iso639AsciiLanguage);
320
321    //
322    // Caller of Framework HII Interface uses the Language Identification String defined
323    // in Iso639. So map it to the Language Identifier defined in RFC4646.
324    //
325    Rfc4646AsciiLanguage = ConvertLanguagesIso639ToRfc4646 (Iso639AsciiLanguage);
326    FreePool (Iso639AsciiLanguage);
327
328    //
329    // If Rfc4646AsciiLanguage is NULL, more language mapping must be added to
330    // Iso639ToRfc4646Map.
331    //
332    ASSERT (Rfc4646AsciiLanguage != NULL);
333  }
334
335  UefiHiiHandle = FwHiiHandleToUefiHiiHandle (Private, Handle);
336  if (UefiHiiHandle == NULL) {
337    Status = EFI_NOT_FOUND;
338    goto Done;
339  }
340
341  //
342  // Get the languages that the package specified by HiiHandle supports
343  //
344  SupportedLanguages = HiiGetSupportedLanguages (UefiHiiHandle);
345  if (SupportedLanguages == NULL) {
346    goto Done;
347  }
348
349  //
350  // Get the current platform language setting
351  //
352  GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);
353
354  //
355  // Get the best matching language from SupportedLanguages
356  //
357  BestLanguage = GetBestLanguage (
358                   SupportedLanguages,
359                   FALSE,                // RFC 4646 mode
360                   (Rfc4646AsciiLanguage != NULL) ? Rfc4646AsciiLanguage : "", // Highest priority
361                   (PlatformLanguage != NULL) ? PlatformLanguage : "",         // Next highest priority
362                   SupportedLanguages,   // Lowest priority
363                   NULL
364                   );
365  if (BestLanguage != NULL) {
366    Status = mHiiStringProtocol->GetString (
367                                 mHiiStringProtocol,
368                                 BestLanguage,
369                                 UefiHiiHandle,
370                                 Token,
371                                 StringBuffer,
372                                 BufferLength,
373                                 NULL
374                                 );
375    FreePool (BestLanguage);
376  } else {
377    Status = EFI_INVALID_PARAMETER;
378  }
379
380Done:
381	if (Rfc4646AsciiLanguage != NULL) {
382    FreePool (Rfc4646AsciiLanguage);
383  }
384
385  if (SupportedLanguages != NULL) {
386    FreePool (SupportedLanguages);
387  }
388
389  if (PlatformLanguage != NULL) {
390    FreePool (PlatformLanguage);
391  }
392  return Status;
393}
394
395/**
396
397  This function allows a program to extract a part of a string of not more than a given width.
398  With repeated calls, this allows a calling program to extract "lines" of text that fit inside
399  columns.  The effort of measuring the fit of strings inside columns is localized to this call.
400
401  This is a deprecated API. No Framework HII module is calling it. This function will ASSERT and
402  return EFI_UNSUPPORTED.
403
404  @param This            A pointer to the EFI_HII_PROTOCOL instance.
405  @param Handle          The HII handle on which the string resides.
406  @param Token           The string token assigned to the string.
407  @param Index           On input, the offset into the string where the line is to start.
408                         On output, the index is updated to point to beyond the last character returned
409                         in the call.
410  @param LineWidth       The maximum width of the line in units of narrow glyphs.
411  @param LanguageString  Pointer to a NULL-terminated string containing a single ISO 639-2 language
412                         identifier, indicating the language to print. If the LanguageString is empty (starts
413                         with a NULL), the default system language will be used to determine the language.
414  @param BufferLength    Length of the StringBuffer. If the status reports that the buffer width is too
415                         small, this parameter is filled with the length of the buffer needed.
416  @param StringBuffer    The buffer designed to receive the characters in the string. Type EFI_STRING is
417                         defined in String.
418
419  @retval EFI_UNSUPPORTED.
420**/
421EFI_STATUS
422EFIAPI
423HiiGetLine (
424  IN     EFI_HII_PROTOCOL           *This,
425  IN     FRAMEWORK_EFI_HII_HANDLE   Handle,
426  IN     STRING_REF                 Token,
427  IN OUT UINT16                     *Index,
428  IN     UINT16                     LineWidth,
429  IN     CHAR16                     *LanguageString,
430  IN OUT UINT16                     *BufferLength,
431  OUT    EFI_STRING                 StringBuffer
432  )
433{
434  ASSERT (FALSE);
435  return EFI_UNSUPPORTED;
436}
437
438
439