]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/String.c
Update HiiDataBase driver to use GetBestLanguage() for matching RFC 4646 languages.
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / String.c
1 /** @file
2 Implementation for EFI_HII_STRING_PROTOCOL.
3
4
5 Copyright (c) 2007, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16
17 #include "HiiDatabase.h"
18
19 CHAR16 mLanguageWindow[16] = {
20 0x0000, 0x0080, 0x0100, 0x0300,
21 0x2000, 0x2080, 0x2100, 0x3000,
22 0x0080, 0x00C0, 0x0400, 0x0600,
23 0x0900, 0x3040, 0x30A0, 0xFF00
24 };
25
26
27 /**
28 This function checks whether a global font info is referred by local
29 font info list or not. (i.e. HII_FONT_INFO is generated.) If not, create
30 a HII_FONT_INFO to refer it locally.
31
32 This is a internal function.
33
34
35 @param Private Hii database private structure.
36 @param StringPackage HII string package instance.
37 @param FontId Font identifer, which must be unique within the string package.
38 @param DuplicateEnable If true, duplicate HII_FONT_INFO which refers to
39 the same EFI_FONT_INFO is permitted. Otherwise it
40 is not allowed.
41 @param GlobalFontInfo Input a global font info which specify a
42 EFI_FONT_INFO.
43 @param LocalFontInfo Output a local font info which refers to a
44 EFI_FONT_INFO.
45
46 @retval TRUE Already referred before calling this function.
47 @retval FALSE Not referred before calling this function.
48
49 **/
50 BOOLEAN
51 ReferFontInfoLocally (
52 IN HII_DATABASE_PRIVATE_DATA *Private,
53 IN HII_STRING_PACKAGE_INSTANCE *StringPackage,
54 IN UINT8 FontId,
55 IN BOOLEAN DuplicateEnable,
56 IN HII_GLOBAL_FONT_INFO *GlobalFontInfo,
57 OUT HII_FONT_INFO **LocalFontInfo
58 )
59 {
60 HII_FONT_INFO *LocalFont;
61 LIST_ENTRY *Link;
62
63 ASSERT (Private != NULL && StringPackage != NULL && GlobalFontInfo != NULL && LocalFontInfo != NULL);
64
65 if (!DuplicateEnable) {
66 for (Link = StringPackage->FontInfoList.ForwardLink;
67 Link != &StringPackage->FontInfoList;
68 Link = Link->ForwardLink
69 ) {
70 LocalFont = CR (Link, HII_FONT_INFO, Entry, HII_FONT_INFO_SIGNATURE);
71 if (LocalFont->GlobalEntry == &GlobalFontInfo->Entry) {
72 //
73 // Already referred by local font info list, return directly.
74 //
75 *LocalFontInfo = LocalFont;
76 return TRUE;
77 }
78 }
79 }
80 // FontId identifies EFI_FONT_INFO in local string package uniquely.
81 // GlobalEntry points to a HII_GLOBAL_FONT_INFO which identifies
82 // EFI_FONT_INFO uniquely in whole hii database.
83 //
84 LocalFont = (HII_FONT_INFO *) AllocateZeroPool (sizeof (HII_FONT_INFO));
85 ASSERT (LocalFont != NULL);
86
87 LocalFont->Signature = HII_FONT_INFO_SIGNATURE;
88 LocalFont->FontId = FontId;
89 LocalFont->GlobalEntry = &GlobalFontInfo->Entry;
90 InsertTailList (&StringPackage->FontInfoList, &LocalFont->Entry);
91
92 *LocalFontInfo = LocalFont;
93 return FALSE;
94 }
95
96
97 /**
98 Convert Ascii string text to unicode string test.
99
100 This is a internal function.
101
102
103 @param StringDest Buffer to store the string text. If it is NULL,
104 only the size will be returned.
105 @param StringSrc Points to current null-terminated string.
106 @param BufferSize Length of the buffer.
107
108 @retval EFI_SUCCESS The string text was outputed successfully.
109 @retval EFI_BUFFER_TOO_SMALL Buffer is insufficient to store the found string
110 text. BufferSize is updated to the required buffer
111 size.
112
113 **/
114 EFI_STATUS
115 ConvertToUnicodeText (
116 OUT EFI_STRING StringDest,
117 IN CHAR8 *StringSrc,
118 IN OUT UINTN *BufferSize
119 )
120 {
121 UINTN StringSize;
122 UINTN Index;
123
124 ASSERT (StringSrc != NULL && BufferSize != NULL);
125
126 StringSize = AsciiStrSize (StringSrc) * 2;
127 if (*BufferSize < StringSize || StringDest == NULL) {
128 *BufferSize = StringSize;
129 return EFI_BUFFER_TOO_SMALL;
130 }
131
132 for (Index = 0; Index < AsciiStrLen (StringSrc); Index++) {
133 StringDest[Index] = (CHAR16) StringSrc[Index];
134 }
135
136 StringDest[Index] = 0;
137 return EFI_SUCCESS;
138 }
139
140
141 /**
142 Calculate the size of StringSrc and output it. If StringDest is not NULL,
143 copy string text from src to dest.
144
145 This is a internal function.
146
147 @param StringDest Buffer to store the string text. If it is NULL,
148 only the size will be returned.
149 @param StringSrc Points to current null-terminated string.
150 @param BufferSize Length of the buffer.
151
152 @retval EFI_SUCCESS The string text was outputed successfully.
153 @retval EFI_BUFFER_TOO_SMALL Buffer is insufficient to store the found string
154 text. BufferSize is updated to the required buffer
155 size.
156
157 **/
158 EFI_STATUS
159 GetUnicodeStringTextOrSize (
160 OUT EFI_STRING StringDest, OPTIONAL
161 IN UINT8 *StringSrc,
162 IN OUT UINTN *BufferSize
163 )
164 {
165 UINTN StringSize;
166 UINT8 *StringPtr;
167
168 ASSERT (StringSrc != NULL && BufferSize != NULL);
169
170 StringSize = sizeof (CHAR16);
171 StringPtr = StringSrc;
172 while (ReadUnaligned16 ((UINT16 *) StringPtr) != 0) {
173 StringSize += sizeof (CHAR16);
174 StringPtr += sizeof (CHAR16);
175 }
176
177 if (*BufferSize < StringSize) {
178 *BufferSize = StringSize;
179 return EFI_BUFFER_TOO_SMALL;
180 }
181 if (StringDest != NULL) {
182 CopyMem (StringDest, StringSrc, StringSize);
183 }
184
185 *BufferSize = StringSize;
186 return EFI_SUCCESS;
187 }
188
189
190 /**
191 Copy string font info to a buffer.
192
193 This is a internal function.
194
195 @param StringPackage Hii string package instance.
196 @param FontId Font identifier which is unique in a string
197 package.
198 @param StringFontInfo Buffer to record the output font info. It's
199 caller's responsibility to free this buffer.
200
201 @retval EFI_SUCCESS The string font is outputed successfully.
202 @retval EFI_NOT_FOUND The specified font id does not exist.
203
204 **/
205 EFI_STATUS
206 GetStringFontInfo (
207 IN HII_STRING_PACKAGE_INSTANCE *StringPackage,
208 IN UINT8 FontId,
209 OUT EFI_FONT_INFO **StringFontInfo
210 )
211 {
212 LIST_ENTRY *Link;
213 HII_FONT_INFO *FontInfo;
214 HII_GLOBAL_FONT_INFO *GlobalFont;
215
216 ASSERT (StringFontInfo != NULL && StringPackage != NULL);
217
218 for (Link = StringPackage->FontInfoList.ForwardLink; Link != &StringPackage->FontInfoList; Link = Link->ForwardLink) {
219 FontInfo = CR (Link, HII_FONT_INFO, Entry, HII_FONT_INFO_SIGNATURE);
220 if (FontInfo->FontId == FontId) {
221 GlobalFont = CR (FontInfo->GlobalEntry, HII_GLOBAL_FONT_INFO, Entry, HII_GLOBAL_FONT_INFO_SIGNATURE);
222 *StringFontInfo = (EFI_FONT_INFO *) AllocateZeroPool (GlobalFont->FontInfoSize);
223 if (*StringFontInfo == NULL) {
224 return EFI_OUT_OF_RESOURCES;
225 }
226 CopyMem (*StringFontInfo, GlobalFont->FontInfo, GlobalFont->FontInfoSize);
227 return EFI_SUCCESS;
228 }
229 }
230
231 return EFI_NOT_FOUND;
232 }
233
234
235 /**
236 Parse all string blocks to find a String block specified by StringId.
237 If StringId = (EFI_STRING_ID) (-1), find out all EFI_HII_SIBT_FONT blocks
238 within this string package and backup its information.
239 If StringId = 0, output the string id of last string block (EFI_HII_SIBT_END).
240
241 @param Private Hii database private structure.
242 @param StringPackage Hii string package instance.
243 @param StringId The string's id, which is unique within
244 PackageList.
245 @param BlockType Output the block type of found string block.
246 @param StringBlockAddr Output the block address of found string block.
247 @param StringTextOffset Offset, relative to the found block address, of
248 the string text information.
249 @param LastStringId Output the last string id when StringId = 0.
250
251 @retval EFI_SUCCESS The string text and font is retrieved
252 successfully.
253 @retval EFI_NOT_FOUND The specified text or font info can not be found
254 out.
255 @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the
256 task.
257
258 **/
259 EFI_STATUS
260 FindStringBlock (
261 IN HII_DATABASE_PRIVATE_DATA *Private,
262 IN HII_STRING_PACKAGE_INSTANCE *StringPackage,
263 IN EFI_STRING_ID StringId,
264 OUT UINT8 *BlockType, OPTIONAL
265 OUT UINT8 **StringBlockAddr, OPTIONAL
266 OUT UINTN *StringTextOffset, OPTIONAL
267 OUT EFI_STRING_ID *LastStringId OPTIONAL
268 )
269 {
270 UINT8 *BlockHdr;
271 EFI_STRING_ID CurrentStringId;
272 UINTN BlockSize;
273 UINTN Index;
274 UINT8 *StringTextPtr;
275 UINTN Offset;
276 HII_FONT_INFO *LocalFont;
277 EFI_FONT_INFO *FontInfo;
278 HII_GLOBAL_FONT_INFO *GlobalFont;
279 UINTN FontInfoSize;
280 UINT16 StringCount;
281 UINT16 SkipCount;
282 EFI_HII_FONT_STYLE FontStyle;
283 UINT16 FontSize;
284 UINT8 Length8;
285 EFI_HII_SIBT_EXT2_BLOCK Ext2;
286 UINT8 FontId;
287 UINT32 Length32;
288 UINTN StringSize;
289 CHAR16 Zero;
290
291 ASSERT (StringPackage != NULL);
292 ASSERT (StringPackage->Signature == HII_STRING_PACKAGE_SIGNATURE);
293
294 CurrentStringId = 1;
295
296 if (StringId != (EFI_STRING_ID) (-1) && StringId != 0) {
297 ASSERT (BlockType != NULL && StringBlockAddr != NULL && StringTextOffset != NULL);
298 } else {
299 ASSERT (Private != NULL && Private->Signature == HII_DATABASE_PRIVATE_DATA_SIGNATURE);
300 }
301
302 ZeroMem (&Zero, sizeof (CHAR16));
303
304 //
305 // Parse the string blocks to get the string text and font.
306 //
307 BlockHdr = StringPackage->StringBlock;
308 BlockSize = 0;
309 Offset = 0;
310 while (*BlockHdr != EFI_HII_SIBT_END) {
311 switch (*BlockHdr) {
312 case EFI_HII_SIBT_STRING_SCSU:
313 Offset = sizeof (EFI_HII_STRING_BLOCK);
314 StringTextPtr = BlockHdr + Offset;
315 BlockSize += Offset + AsciiStrSize ((CHAR8 *) StringTextPtr);
316 CurrentStringId++;
317 break;
318
319 case EFI_HII_SIBT_STRING_SCSU_FONT:
320 Offset = sizeof (EFI_HII_SIBT_STRING_SCSU_FONT_BLOCK) - sizeof (UINT8);
321 StringTextPtr = BlockHdr + Offset;
322 BlockSize += Offset + AsciiStrSize ((CHAR8 *) StringTextPtr);
323 CurrentStringId++;
324 break;
325
326 case EFI_HII_SIBT_STRINGS_SCSU:
327 CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
328 StringTextPtr = BlockHdr + sizeof (EFI_HII_SIBT_STRINGS_SCSU_BLOCK) - sizeof (UINT8);
329 BlockSize += StringTextPtr - BlockHdr;
330
331 for (Index = 0; Index < StringCount; Index++) {
332 BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr);
333 if (CurrentStringId == StringId) {
334 *BlockType = *BlockHdr;
335 *StringBlockAddr = BlockHdr;
336 *StringTextOffset = StringTextPtr - BlockHdr;
337 return EFI_SUCCESS;
338 }
339 StringTextPtr = StringTextPtr + AsciiStrSize ((CHAR8 *) StringTextPtr);
340 CurrentStringId++;
341 }
342 break;
343
344 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
345 CopyMem (
346 &StringCount,
347 BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8),
348 sizeof (UINT16)
349 );
350 StringTextPtr = BlockHdr + sizeof (EFI_HII_SIBT_STRINGS_SCSU_FONT_BLOCK) - sizeof (UINT8);
351 BlockSize += StringTextPtr - BlockHdr;
352
353 for (Index = 0; Index < StringCount; Index++) {
354 BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr);
355 if (CurrentStringId == StringId) {
356 *BlockType = *BlockHdr;
357 *StringBlockAddr = BlockHdr;
358 *StringTextOffset = StringTextPtr - BlockHdr;
359 return EFI_SUCCESS;
360 }
361 StringTextPtr = StringTextPtr + AsciiStrSize ((CHAR8 *) StringTextPtr);
362 CurrentStringId++;
363 }
364 break;
365
366 case EFI_HII_SIBT_STRING_UCS2:
367 Offset = sizeof (EFI_HII_STRING_BLOCK);
368 StringTextPtr = BlockHdr + Offset;
369 //
370 // Use StringSize to store the size of the specified string, including the NULL
371 // terminator.
372 //
373 GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize);
374 BlockSize += Offset + StringSize;
375 CurrentStringId++;
376 break;
377
378 case EFI_HII_SIBT_STRING_UCS2_FONT:
379 Offset = sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK) - sizeof (CHAR16);
380 StringTextPtr = BlockHdr + Offset;
381 //
382 // Use StrSize to store the size of the specified string, including the NULL
383 // terminator.
384 //
385 GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize);
386 BlockSize += Offset + StringSize;
387 CurrentStringId++;
388 break;
389
390 case EFI_HII_SIBT_STRINGS_UCS2:
391 Offset = sizeof (EFI_HII_SIBT_STRINGS_UCS2_BLOCK) - sizeof (CHAR16);
392 StringTextPtr = BlockHdr + Offset;
393 BlockSize += Offset;
394 CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
395 for (Index = 0; Index < StringCount; Index++) {
396 GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize);
397 BlockSize += StringSize;
398 if (CurrentStringId == StringId) {
399 *BlockType = *BlockHdr;
400 *StringBlockAddr = BlockHdr;
401 *StringTextOffset = StringTextPtr - BlockHdr;
402 return EFI_SUCCESS;
403 }
404 StringTextPtr = StringTextPtr + StringSize;
405 CurrentStringId++;
406 }
407 break;
408
409 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
410 Offset = sizeof (EFI_HII_SIBT_STRINGS_UCS2_FONT_BLOCK) - sizeof (CHAR16);
411 StringTextPtr = BlockHdr + Offset;
412 BlockSize += Offset;
413 CopyMem (
414 &StringCount,
415 BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8),
416 sizeof (UINT16)
417 );
418 for (Index = 0; Index < StringCount; Index++) {
419 GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize);
420 BlockSize += StringSize;
421 if (CurrentStringId == StringId) {
422 *BlockType = *BlockHdr;
423 *StringBlockAddr = BlockHdr;
424 *StringTextOffset = StringTextPtr - BlockHdr;
425 return EFI_SUCCESS;
426 }
427 StringTextPtr = StringTextPtr + StringSize;
428 CurrentStringId++;
429 }
430 break;
431
432 case EFI_HII_SIBT_DUPLICATE:
433 if (CurrentStringId == StringId) {
434 //
435 // Incoming StringId is an id of a duplicate string block.
436 // Update the StringId to be the previous string block.
437 // Go back to the header of string block to search.
438 //
439 CopyMem (
440 &StringId,
441 BlockHdr + sizeof (EFI_HII_STRING_BLOCK),
442 sizeof (EFI_STRING_ID)
443 );
444 ASSERT (StringId != CurrentStringId);
445 CurrentStringId = 1;
446 BlockSize = 0;
447 } else {
448 BlockSize += sizeof (EFI_HII_SIBT_DUPLICATE_BLOCK);
449 CurrentStringId++;
450 }
451 break;
452
453 case EFI_HII_SIBT_SKIP1:
454 SkipCount = (UINT16) (*(BlockHdr + sizeof (EFI_HII_STRING_BLOCK)));
455 CurrentStringId = (UINT16) (CurrentStringId + SkipCount);
456 BlockSize += sizeof (EFI_HII_SIBT_SKIP1_BLOCK);
457 break;
458
459 case EFI_HII_SIBT_SKIP2:
460 CopyMem (&SkipCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
461 CurrentStringId = (UINT16) (CurrentStringId + SkipCount);
462 BlockSize += sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
463 break;
464
465 case EFI_HII_SIBT_EXT1:
466 CopyMem (
467 &Length8,
468 BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8),
469 sizeof (UINT8)
470 );
471 BlockSize += Length8;
472 break;
473
474 case EFI_HII_SIBT_EXT2:
475 CopyMem (&Ext2, BlockHdr, sizeof (EFI_HII_SIBT_EXT2_BLOCK));
476 if (Ext2.BlockType2 == EFI_HII_SIBT_FONT && StringId == (EFI_STRING_ID) (-1)) {
477 //
478 // Find the relationship between global font info and the font info of
479 // this EFI_HII_SIBT_FONT block then backup its information in local package.
480 //
481 BlockHdr += sizeof (EFI_HII_SIBT_EXT2_BLOCK);
482 CopyMem (&FontId, BlockHdr, sizeof (UINT8));
483 BlockHdr += sizeof (UINT8);
484 CopyMem (&FontSize, BlockHdr, sizeof (UINT16));
485 BlockHdr += sizeof (UINT16);
486 CopyMem (&FontStyle, BlockHdr, sizeof (EFI_HII_FONT_STYLE));
487 BlockHdr += sizeof (EFI_HII_FONT_STYLE);
488 GetUnicodeStringTextOrSize (NULL, BlockHdr, &StringSize);
489
490 FontInfoSize = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + StringSize;
491 FontInfo = (EFI_FONT_INFO *) AllocateZeroPool (FontInfoSize);
492 if (FontInfo == NULL) {
493 return EFI_OUT_OF_RESOURCES;
494 }
495 FontInfo->FontStyle = FontStyle;
496 FontInfo->FontSize = FontSize;
497 CopyMem (FontInfo->FontName, BlockHdr, StringSize);
498
499 //
500 // If find the corresponding global font info, save the relationship.
501 // Otherwise ignore this EFI_HII_SIBT_FONT block.
502 //
503 if (IsFontInfoExisted (Private, FontInfo, NULL, NULL, &GlobalFont)) {
504 ReferFontInfoLocally (Private, StringPackage, FontId, TRUE, GlobalFont, &LocalFont);
505 }
506
507 //
508 // Since string package tool set FontId initially to 0 and increases it
509 // progressively by one, StringPackage->FondId always represents an unique
510 // and available FontId.
511 //
512 StringPackage->FontId++;
513
514 FreePool (FontInfo);
515 }
516
517 BlockSize += Ext2.Length;
518
519 break;
520
521 case EFI_HII_SIBT_EXT4:
522 CopyMem (
523 &Length32,
524 BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8),
525 sizeof (UINT32)
526 );
527
528 BlockSize += Length32;
529 break;
530
531 default:
532 break;
533 }
534
535 if (StringId > 0) {
536 if (StringId == CurrentStringId - 1) {
537 *BlockType = *BlockHdr;
538 *StringBlockAddr = BlockHdr;
539 *StringTextOffset = Offset;
540 return EFI_SUCCESS;
541 }
542
543 if (StringId < CurrentStringId - 1) {
544 return EFI_NOT_FOUND;
545 }
546 }
547 BlockHdr = StringPackage->StringBlock + BlockSize;
548
549 }
550
551 if (StringId == (EFI_STRING_ID) (-1)) {
552 return EFI_SUCCESS;
553 }
554
555 if (StringId == 0 && LastStringId != NULL) {
556 *LastStringId = CurrentStringId;
557 return EFI_SUCCESS;
558 }
559
560 return EFI_NOT_FOUND;
561 }
562
563
564 /**
565 Parse all string blocks to get a string specified by StringId.
566
567 This is a internal function.
568
569 @param Private Hii database private structure.
570 @param StringPackage Hii string package instance.
571 @param StringId The string's id, which is unique within
572 PackageList.
573 @param String Points to retrieved null-terminated string.
574 @param StringSize On entry, points to the size of the buffer pointed
575 to by String, in bytes. On return, points to the
576 length of the string, in bytes.
577 @param StringFontInfo If not NULL, allocate a buffer to record the
578 output font info. It's caller's responsibility to
579 free this buffer.
580
581 @retval EFI_SUCCESS The string text and font is retrieved
582 successfully.
583 @retval EFI_NOT_FOUND The specified text or font info can not be found
584 out.
585 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringSize is too small to
586 hold the string.
587
588 **/
589 EFI_STATUS
590 GetStringWorker (
591 IN HII_DATABASE_PRIVATE_DATA *Private,
592 IN HII_STRING_PACKAGE_INSTANCE *StringPackage,
593 IN EFI_STRING_ID StringId,
594 OUT EFI_STRING String,
595 IN OUT UINTN *StringSize,
596 OUT EFI_FONT_INFO **StringFontInfo OPTIONAL
597 )
598 {
599 UINT8 *StringTextPtr;
600 UINT8 BlockType;
601 UINT8 *StringBlockAddr;
602 UINTN StringTextOffset;
603 EFI_STATUS Status;
604 UINT8 FontId;
605
606 ASSERT (StringPackage != NULL && StringSize != NULL);
607 ASSERT (Private != NULL && Private->Signature == HII_DATABASE_PRIVATE_DATA_SIGNATURE);
608
609 //
610 // Find the specified string block
611 //
612 Status = FindStringBlock (
613 Private,
614 StringPackage,
615 StringId,
616 &BlockType,
617 &StringBlockAddr,
618 &StringTextOffset,
619 NULL
620 );
621 if (EFI_ERROR (Status)) {
622 return Status;
623 }
624
625 //
626 // Get the string text.
627 //
628 StringTextPtr = StringBlockAddr + StringTextOffset;
629 switch (BlockType) {
630 case EFI_HII_SIBT_STRING_SCSU:
631 case EFI_HII_SIBT_STRING_SCSU_FONT:
632 case EFI_HII_SIBT_STRINGS_SCSU:
633 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
634 Status = ConvertToUnicodeText (String, (CHAR8 *) StringTextPtr, StringSize);
635 break;
636 case EFI_HII_SIBT_STRING_UCS2:
637 case EFI_HII_SIBT_STRING_UCS2_FONT:
638 case EFI_HII_SIBT_STRINGS_UCS2:
639 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
640 Status = GetUnicodeStringTextOrSize (String, StringTextPtr, StringSize);
641 break;
642 default:
643 return EFI_NOT_FOUND;
644 }
645 if (EFI_ERROR (Status)) {
646 return Status;
647 }
648
649 //
650 // Get the string font. The FontId 0 is the default font for those string blocks which
651 // do not specify a font identifier. If default font is not specified, return NULL.
652 //
653 if (StringFontInfo != NULL) {
654 switch (BlockType) {
655 case EFI_HII_SIBT_STRING_SCSU_FONT:
656 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
657 case EFI_HII_SIBT_STRING_UCS2_FONT:
658 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
659 FontId = *(StringBlockAddr + sizeof (EFI_HII_STRING_BLOCK));
660 break;
661 default:
662 FontId = 0;
663 }
664 Status = GetStringFontInfo (StringPackage, FontId, StringFontInfo);
665 if (Status == EFI_NOT_FOUND) {
666 *StringFontInfo = NULL;
667 }
668 }
669
670 return EFI_SUCCESS;
671 }
672
673
674 /**
675 Parse all string blocks to set a String specified by StringId.
676
677 This is a internal function.
678
679 @param Private HII database driver private structure.
680 @param StringPackage HII string package instance.
681 @param StringId The string's id, which is unique within
682 PackageList.
683 @param String Points to the new null-terminated string.
684 @param StringFontInfo Points to the input font info.
685
686 @retval EFI_SUCCESS The string was updated successfully.
687 @retval EFI_NOT_FOUND The string specified by StringId is not in the
688 database.
689 @retval EFI_INVALID_PARAMETER The String or Language was NULL.
690 @retval EFI_INVALID_PARAMETER The specified StringFontInfo does not exist in
691 current database.
692 @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the
693 task.
694
695 **/
696 EFI_STATUS
697 SetStringWorker (
698 IN HII_DATABASE_PRIVATE_DATA *Private,
699 IN OUT HII_STRING_PACKAGE_INSTANCE *StringPackage,
700 IN EFI_STRING_ID StringId,
701 IN EFI_STRING String,
702 IN EFI_FONT_INFO *StringFontInfo OPTIONAL
703 )
704 {
705 UINT8 *StringTextPtr;
706 UINT8 BlockType;
707 UINT8 *StringBlockAddr;
708 UINTN StringTextOffset;
709 EFI_STATUS Status;
710 UINT8 *Block;
711 UINT8 *BlockPtr;
712 UINTN BlockSize;
713 UINTN OldBlockSize;
714 HII_FONT_INFO *LocalFont;
715 HII_GLOBAL_FONT_INFO *GlobalFont;
716 BOOLEAN Referred;
717 EFI_HII_SIBT_EXT2_BLOCK Ext2;
718 UINTN StringSize;
719 UINTN TmpSize;
720
721
722 ASSERT (Private != NULL && StringPackage != NULL && String != NULL);
723 ASSERT (Private->Signature == HII_DATABASE_PRIVATE_DATA_SIGNATURE);
724 //
725 // Find the specified string block
726 //
727 Status = FindStringBlock (
728 Private,
729 StringPackage,
730 StringId,
731 &BlockType,
732 &StringBlockAddr,
733 &StringTextOffset,
734 NULL
735 );
736 if (EFI_ERROR (Status)) {
737 return Status;
738 }
739
740 LocalFont = NULL;
741 GlobalFont = NULL;
742 Referred = FALSE;
743
744 //
745 // The input StringFontInfo should exist in current database if specified.
746 //
747 if (StringFontInfo != NULL) {
748 if (!IsFontInfoExisted (Private, StringFontInfo, NULL, NULL, &GlobalFont)) {
749 return EFI_INVALID_PARAMETER;
750 } else {
751 Referred = ReferFontInfoLocally (
752 Private,
753 StringPackage,
754 StringPackage->FontId,
755 FALSE,
756 GlobalFont,
757 &LocalFont
758 );
759 if (!Referred) {
760 StringPackage->FontId++;
761 }
762 }
763 //
764 // Update the FontId of the specified string block to input font info.
765 //
766 switch (BlockType) {
767 case EFI_HII_SIBT_STRING_SCSU_FONT:
768 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
769 case EFI_HII_SIBT_STRING_UCS2_FONT:
770 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
771 *(StringBlockAddr + sizeof (EFI_HII_STRING_BLOCK)) = LocalFont->FontId;
772 break;
773 default:
774 //
775 // When modify the font info of these blocks, the block type should be updated
776 // to contain font info thus the whole structure should be revised.
777 // It is recommended to use tool to modify the block type not in the code.
778 //
779 return EFI_UNSUPPORTED;
780 }
781 }
782
783 OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize;
784
785 //
786 // Set the string text and font.
787 //
788 StringTextPtr = StringBlockAddr + StringTextOffset;
789 switch (BlockType) {
790 case EFI_HII_SIBT_STRING_SCSU:
791 case EFI_HII_SIBT_STRING_SCSU_FONT:
792 case EFI_HII_SIBT_STRINGS_SCSU:
793 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
794 BlockSize = OldBlockSize + StrLen (String);
795 BlockSize -= AsciiStrLen ((CHAR8 *) StringTextPtr);
796 Block = AllocateZeroPool (BlockSize);
797 if (Block == NULL) {
798 return EFI_OUT_OF_RESOURCES;
799 }
800
801 CopyMem (Block, StringPackage->StringBlock, StringTextPtr - StringPackage->StringBlock);
802 BlockPtr = Block + (StringTextPtr - StringPackage->StringBlock);
803
804 while (*String != 0) {
805 *BlockPtr++ = (CHAR8) *String++;
806 }
807 *BlockPtr++ = 0;
808
809
810 TmpSize = OldBlockSize - (StringTextPtr - StringPackage->StringBlock) - AsciiStrSize ((CHAR8 *) StringTextPtr);
811 CopyMem (
812 BlockPtr,
813 StringTextPtr + AsciiStrSize ((CHAR8 *)StringTextPtr),
814 TmpSize
815 );
816
817 FreePool (StringPackage->StringBlock);
818 StringPackage->StringBlock = Block;
819 StringPackage->StringPkgHdr->Header.Length += (UINT32) (BlockSize - OldBlockSize);
820 break;
821
822 case EFI_HII_SIBT_STRING_UCS2:
823 case EFI_HII_SIBT_STRING_UCS2_FONT:
824 case EFI_HII_SIBT_STRINGS_UCS2:
825 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
826 //
827 // Use StrSize to store the size of the specified string, including the NULL
828 // terminator.
829 //
830 GetUnicodeStringTextOrSize (NULL, StringTextPtr, &StringSize);
831
832 BlockSize = OldBlockSize + StrSize (String) - StringSize;
833 Block = AllocateZeroPool (BlockSize);
834 if (Block == NULL) {
835 return EFI_OUT_OF_RESOURCES;
836 }
837
838 CopyMem (Block, StringPackage->StringBlock, StringTextPtr - StringPackage->StringBlock);
839 BlockPtr = Block + (StringTextPtr - StringPackage->StringBlock);
840
841 CopyMem (BlockPtr, String, StrSize (String));
842 BlockPtr += StrSize (String);
843
844 CopyMem (
845 BlockPtr,
846 StringTextPtr + StringSize,
847 OldBlockSize - (StringTextPtr - StringPackage->StringBlock) - StringSize
848 );
849
850 FreePool (StringPackage->StringBlock);
851 StringPackage->StringBlock = Block;
852 StringPackage->StringPkgHdr->Header.Length += (UINT32) (BlockSize - OldBlockSize);
853 break;
854
855 default:
856 return EFI_NOT_FOUND;
857 }
858
859 //
860 // Insert a new EFI_HII_SIBT_FONT_BLOCK to the header of string block, if incoming
861 // StringFontInfo does not exist in current string package.
862 //
863 // This new block does not impact on the value of StringId.
864 //
865 //
866 if (StringFontInfo == NULL || Referred) {
867 return EFI_SUCCESS;
868 }
869
870 OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize;
871 BlockSize = OldBlockSize + sizeof (EFI_HII_SIBT_FONT_BLOCK) - sizeof (CHAR16) +
872 StrSize (GlobalFont->FontInfo->FontName);
873
874 Block = AllocateZeroPool (BlockSize);
875 if (Block == NULL) {
876 return EFI_OUT_OF_RESOURCES;
877 }
878
879 BlockPtr = Block;
880 Ext2.Header.BlockType = EFI_HII_SIBT_EXT2;
881 Ext2.BlockType2 = EFI_HII_SIBT_FONT;
882 Ext2.Length = (UINT16) (BlockSize - OldBlockSize);
883 CopyMem (BlockPtr, &Ext2, sizeof (EFI_HII_SIBT_EXT2_BLOCK));
884 BlockPtr += sizeof (EFI_HII_SIBT_EXT2_BLOCK);
885
886 *BlockPtr = LocalFont->FontId;
887 BlockPtr += sizeof (UINT8);
888 CopyMem (BlockPtr, &GlobalFont->FontInfo->FontSize, sizeof (UINT16));
889 BlockPtr += sizeof (UINT16);
890 CopyMem (BlockPtr, &GlobalFont->FontInfo->FontStyle, sizeof (UINT32));
891 BlockPtr += sizeof (UINT32);
892 CopyMem (
893 BlockPtr,
894 GlobalFont->FontInfo->FontName,
895 StrSize (GlobalFont->FontInfo->FontName)
896 );
897 BlockPtr += StrSize (GlobalFont->FontInfo->FontName);
898
899 CopyMem (BlockPtr, StringPackage->StringBlock, OldBlockSize);
900
901 FreePool (StringPackage->StringBlock);
902 StringPackage->StringBlock = Block;
903 StringPackage->StringPkgHdr->Header.Length += Ext2.Length;
904
905 return EFI_SUCCESS;
906
907 }
908
909
910 /**
911 This function adds the string String to the group of strings owned by PackageList, with the
912 specified font information StringFontInfo and returns a new string id.
913
914 @param This A pointer to the EFI_HII_STRING_PROTOCOL instance.
915 @param PackageList Handle of the package list where this string will
916 be added.
917 @param StringId On return, contains the new strings id, which is
918 unique within PackageList.
919 @param Language Points to the language for the new string.
920 @param LanguageName Points to the printable language name to associate
921 with the passed in Language field.If LanguageName
922 is not NULL and the string package header's
923 LanguageName associated with a given Language is
924 not zero, the LanguageName being passed in will
925 be ignored.
926 @param String Points to the new null-terminated string.
927 @param StringFontInfo Points to the new string's font information or
928 NULL if the string should have the default system
929 font, size and style.
930
931 @retval EFI_SUCCESS The new string was added successfully.
932 @retval EFI_NOT_FOUND The specified PackageList could not be found in
933 database.
934 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
935 @retval EFI_INVALID_PARAMETER String is NULL or StringId is NULL or Language is
936 NULL.
937 @retval EFI_INVALID_PARAMETER The specified StringFontInfo does not exist in
938 current database.
939
940 **/
941 EFI_STATUS
942 EFIAPI
943 HiiNewString (
944 IN CONST EFI_HII_STRING_PROTOCOL *This,
945 IN EFI_HII_HANDLE PackageList,
946 OUT EFI_STRING_ID *StringId,
947 IN CONST CHAR8 *Language,
948 IN CONST CHAR16 *LanguageName, OPTIONAL
949 IN CONST EFI_STRING String,
950 IN CONST EFI_FONT_INFO *StringFontInfo OPTIONAL
951 )
952 {
953 EFI_STATUS Status;
954 LIST_ENTRY *Link;
955 BOOLEAN Matched;
956 HII_DATABASE_PRIVATE_DATA *Private;
957 HII_DATABASE_RECORD *DatabaseRecord;
958 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
959 HII_STRING_PACKAGE_INSTANCE *StringPackage;
960 UINT32 HeaderSize;
961 UINT32 BlockSize;
962 UINT32 OldBlockSize;
963 UINT8 *StringBlock;
964 UINT8 *BlockPtr;
965 UINT32 Ucs2BlockSize;
966 UINT32 FontBlockSize;
967 UINT32 Ucs2FontBlockSize;
968 EFI_HII_SIBT_EXT2_BLOCK Ext2;
969 HII_FONT_INFO *LocalFont;
970 HII_GLOBAL_FONT_INFO *GlobalFont;
971 CHAR8 *MatchedLanguage;
972
973 if (This == NULL || String == NULL || StringId == NULL || Language == NULL || PackageList == NULL) {
974 return EFI_INVALID_PARAMETER;
975 }
976
977 if (!IsHiiHandleValid (PackageList)) {
978 return EFI_NOT_FOUND;
979 }
980
981 Private = HII_STRING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
982 GlobalFont = NULL;
983
984 //
985 // If StringFontInfo specify a paritcular font, it should exist in current database.
986 //
987 if (StringFontInfo != NULL) {
988 if (!IsFontInfoExisted (Private, (EFI_FONT_INFO *) StringFontInfo, NULL, NULL, &GlobalFont)) {
989 return EFI_INVALID_PARAMETER;
990 }
991 }
992
993 //
994 // Get the matching package list.
995 //
996 PackageListNode = NULL;
997 for (Link = Private->DatabaseList.ForwardLink; Link != &Private->DatabaseList; Link = Link->ForwardLink) {
998 DatabaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
999 if (DatabaseRecord->Handle == PackageList) {
1000 PackageListNode = DatabaseRecord->PackageList;
1001 break;
1002 }
1003 }
1004 if (PackageListNode == NULL) {
1005 return EFI_NOT_FOUND;
1006 }
1007
1008 //
1009 // Try to get the matching string package. Create a new string package when failed.
1010 //
1011 StringPackage = NULL;
1012 Matched = FALSE;
1013 for (Link = PackageListNode->StringPkgHdr.ForwardLink;
1014 Link != &PackageListNode->StringPkgHdr;
1015 Link = Link->ForwardLink
1016 ) {
1017 StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1018 MatchedLanguage = GetBestLanguage (StringPackage->StringPkgHdr->Language, FALSE, (CHAR8 *) Language, NULL);
1019 if (MatchedLanguage != NULL) {
1020 FreePool (MatchedLanguage);
1021 Matched = TRUE;
1022 break;
1023 }
1024 }
1025
1026 if (!Matched) {
1027 //
1028 // LanguageName is required to create a new string package.
1029 //
1030 if (LanguageName == NULL) {
1031 return EFI_INVALID_PARAMETER;
1032 }
1033
1034 StringPackage = AllocateZeroPool (sizeof (HII_STRING_PACKAGE_INSTANCE));
1035 if (StringPackage == NULL) {
1036 return EFI_OUT_OF_RESOURCES;
1037 }
1038
1039 StringPackage->Signature = HII_STRING_PACKAGE_SIGNATURE;
1040 StringPackage->FontId = 0;
1041 InitializeListHead (&StringPackage->FontInfoList);
1042
1043 //
1044 // Fill in the string package header
1045 //
1046 HeaderSize = (UINT32) (AsciiStrSize ((CHAR8 *) Language) - 1 + sizeof (EFI_HII_STRING_PACKAGE_HDR));
1047 StringPackage->StringPkgHdr = AllocateZeroPool (HeaderSize);
1048 if (StringPackage->StringPkgHdr == NULL) {
1049 FreePool (StringPackage);
1050 return EFI_OUT_OF_RESOURCES;
1051 }
1052 StringPackage->StringPkgHdr->Header.Type = EFI_HII_PACKAGE_STRINGS;
1053 StringPackage->StringPkgHdr->HdrSize = HeaderSize;
1054 StringPackage->StringPkgHdr->StringInfoOffset = HeaderSize;
1055 CopyMem (StringPackage->StringPkgHdr->LanguageWindow, mLanguageWindow, 16 * sizeof (CHAR16));;
1056 StringPackage->StringPkgHdr->LanguageName = 1;
1057 AsciiStrCpy (StringPackage->StringPkgHdr->Language, (CHAR8 *) Language);
1058
1059 //
1060 // Calculate the length of the string blocks, including string block to record
1061 // printable language full name and EFI_HII_SIBT_END_BLOCK.
1062 //
1063 Ucs2BlockSize = (UINT32) (StrSize ((CHAR16 *) LanguageName) +
1064 sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK) - sizeof (CHAR16));
1065
1066 BlockSize = Ucs2BlockSize + sizeof (EFI_HII_SIBT_END_BLOCK);
1067 StringPackage->StringBlock = (UINT8 *) AllocateZeroPool (BlockSize);
1068 if (StringPackage->StringBlock == NULL) {
1069 FreePool (StringPackage->StringPkgHdr);
1070 FreePool (StringPackage);
1071 return EFI_OUT_OF_RESOURCES;
1072 }
1073
1074 //
1075 // Insert the string block of printable language full name
1076 //
1077 BlockPtr = StringPackage->StringBlock;
1078 *BlockPtr = EFI_HII_SIBT_STRING_UCS2;
1079 BlockPtr += sizeof (EFI_HII_STRING_BLOCK);
1080 CopyMem (BlockPtr, (EFI_STRING) LanguageName, StrSize ((EFI_STRING) LanguageName));
1081 BlockPtr += StrSize ((EFI_STRING) LanguageName);
1082
1083 //
1084 // Insert the end block
1085 //
1086 *BlockPtr = EFI_HII_SIBT_END;
1087
1088 //
1089 // Append this string package node to string package array in this package list.
1090 //
1091 StringPackage->StringPkgHdr->Header.Length = HeaderSize + BlockSize;
1092 PackageListNode->PackageListHdr.PackageLength += StringPackage->StringPkgHdr->Header.Length;
1093 InsertTailList (&PackageListNode->StringPkgHdr, &StringPackage->StringEntry);
1094
1095 }
1096
1097 //
1098 // Create a string block and corresponding font block if exists, then append them
1099 // to the end of the string package.
1100 //
1101 Status = FindStringBlock (
1102 Private,
1103 StringPackage,
1104 0,
1105 NULL,
1106 NULL,
1107 NULL,
1108 StringId
1109 );
1110 if (EFI_ERROR (Status)) {
1111 return Status;
1112 }
1113
1114 OldBlockSize = StringPackage->StringPkgHdr->Header.Length - StringPackage->StringPkgHdr->HdrSize;
1115
1116 if (StringFontInfo == NULL) {
1117 //
1118 // Create a EFI_HII_SIBT_STRING_UCS2_BLOCK since font info is not specified.
1119 //
1120
1121 Ucs2BlockSize = (UINT32) (StrSize (String) + sizeof (EFI_HII_SIBT_STRING_UCS2_BLOCK)
1122 - sizeof (CHAR16));
1123
1124 StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + Ucs2BlockSize);
1125 if (StringBlock == NULL) {
1126 return EFI_OUT_OF_RESOURCES;
1127 }
1128 //
1129 // Copy original string blocks, except the EFI_HII_SIBT_END.
1130 //
1131 CopyMem (StringBlock, StringPackage->StringBlock, OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK));
1132 //
1133 // Create a EFI_HII_SIBT_STRING_UCS2 block
1134 //
1135 BlockPtr = StringBlock + OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK);
1136 *BlockPtr = EFI_HII_SIBT_STRING_UCS2;
1137 BlockPtr += sizeof (EFI_HII_STRING_BLOCK);
1138 CopyMem (BlockPtr, (EFI_STRING) String, StrSize ((EFI_STRING) String));
1139 BlockPtr += StrSize ((EFI_STRING) String);
1140
1141 //
1142 // Append a EFI_HII_SIBT_END block to the end.
1143 //
1144 *BlockPtr = EFI_HII_SIBT_END;
1145 FreePool (StringPackage->StringBlock);
1146 StringPackage->StringBlock = StringBlock;
1147 StringPackage->StringPkgHdr->Header.Length += Ucs2BlockSize;
1148 PackageListNode->PackageListHdr.PackageLength += Ucs2BlockSize;
1149
1150 } else {
1151 //
1152 // StringFontInfo is specified here. If there is a EFI_HII_SIBT_FONT_BLOCK
1153 // which refers to this font info, create a EFI_HII_SIBT_STRING_UCS2_FONT block
1154 // only. Otherwise create a EFI_HII_SIBT_FONT block with a EFI_HII_SIBT_STRING
1155 // _UCS2_FONT block.
1156 //
1157 Ucs2FontBlockSize = (UINT32) (StrSize (String) + sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK) -
1158 sizeof (CHAR16));
1159 if (ReferFontInfoLocally (Private, StringPackage, StringPackage->FontId, FALSE, GlobalFont, &LocalFont)) {
1160 //
1161 // Create a EFI_HII_SIBT_STRING_UCS2_FONT block only.
1162 //
1163 StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + Ucs2FontBlockSize);
1164 if (StringBlock == NULL) {
1165 return EFI_OUT_OF_RESOURCES;
1166 }
1167 //
1168 // Copy original string blocks, except the EFI_HII_SIBT_END.
1169 //
1170 CopyMem (StringBlock, StringPackage->StringBlock, OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK));
1171 //
1172 // Create a EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK
1173 //
1174 BlockPtr = StringBlock + OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK);
1175 *BlockPtr = EFI_HII_SIBT_STRING_UCS2_FONT;
1176 BlockPtr += sizeof (EFI_HII_STRING_BLOCK);
1177 *BlockPtr = LocalFont->FontId;
1178 BlockPtr += sizeof (UINT8);
1179 CopyMem (BlockPtr, (EFI_STRING) String, StrSize ((EFI_STRING) String));
1180 BlockPtr += StrSize ((EFI_STRING) String);
1181
1182 //
1183 // Append a EFI_HII_SIBT_END block to the end.
1184 //
1185 *BlockPtr = EFI_HII_SIBT_END;
1186 FreePool (StringPackage->StringBlock);
1187 StringPackage->StringBlock = StringBlock;
1188 StringPackage->StringPkgHdr->Header.Length += Ucs2FontBlockSize;
1189 PackageListNode->PackageListHdr.PackageLength += Ucs2FontBlockSize;
1190
1191 } else {
1192 //
1193 // EFI_HII_SIBT_FONT_BLOCK does not exist in current string package, so
1194 // create a EFI_HII_SIBT_FONT block to record the font info, then generate
1195 // a EFI_HII_SIBT_STRING_UCS2_FONT block to record the incoming string.
1196 //
1197 FontBlockSize = (UINT32) (StrSize (((EFI_FONT_INFO *) StringFontInfo)->FontName) +
1198 sizeof (EFI_HII_SIBT_FONT_BLOCK) - sizeof (CHAR16));
1199 StringBlock = (UINT8 *) AllocateZeroPool (OldBlockSize + FontBlockSize + Ucs2FontBlockSize);
1200 if (StringBlock == NULL) {
1201 return EFI_OUT_OF_RESOURCES;
1202 }
1203 //
1204 // Copy original string blocks, except the EFI_HII_SIBT_END.
1205 //
1206 CopyMem (StringBlock, StringPackage->StringBlock, OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK));
1207
1208 //
1209 // Create a EFI_HII_SIBT_FONT block firstly and then backup its info in string
1210 // package instance for future reference.
1211 //
1212 BlockPtr = StringBlock + OldBlockSize - sizeof (EFI_HII_SIBT_END_BLOCK);
1213
1214 Ext2.Header.BlockType = EFI_HII_SIBT_EXT2;
1215 Ext2.BlockType2 = EFI_HII_SIBT_FONT;
1216 Ext2.Length = (UINT16) FontBlockSize;
1217 CopyMem (BlockPtr, &Ext2, sizeof (EFI_HII_SIBT_EXT2_BLOCK));
1218 BlockPtr += sizeof (EFI_HII_SIBT_EXT2_BLOCK);
1219
1220 *BlockPtr = LocalFont->FontId;
1221 BlockPtr += sizeof (UINT8);
1222 CopyMem (BlockPtr, &((EFI_FONT_INFO *) StringFontInfo)->FontSize, sizeof (UINT16));
1223 BlockPtr += sizeof (UINT16);
1224 CopyMem (BlockPtr, &((EFI_FONT_INFO *) StringFontInfo)->FontStyle, sizeof (EFI_HII_FONT_STYLE));
1225 BlockPtr += sizeof (EFI_HII_FONT_STYLE);
1226 CopyMem (
1227 BlockPtr,
1228 &((EFI_FONT_INFO *) StringFontInfo)->FontName,
1229 StrSize (((EFI_FONT_INFO *) StringFontInfo)->FontName)
1230 );
1231
1232 //
1233 // Create a EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK
1234 //
1235 *BlockPtr = EFI_HII_SIBT_STRING_UCS2_FONT;
1236 BlockPtr += sizeof (EFI_HII_STRING_BLOCK);
1237 *BlockPtr = LocalFont->FontId;
1238 BlockPtr += sizeof (UINT8);
1239 CopyMem (BlockPtr, (EFI_STRING) String, StrSize ((EFI_STRING) String));
1240 BlockPtr += StrSize ((EFI_STRING) String);
1241
1242 //
1243 // Append a EFI_HII_SIBT_END block to the end.
1244 //
1245 *BlockPtr = EFI_HII_SIBT_END;
1246 FreePool (StringPackage->StringBlock);
1247 StringPackage->StringBlock = StringBlock;
1248 StringPackage->StringPkgHdr->Header.Length += FontBlockSize + Ucs2FontBlockSize;
1249 PackageListNode->PackageListHdr.PackageLength += FontBlockSize + Ucs2FontBlockSize;
1250
1251 //
1252 // Increase the FontId to make it unique since we already add
1253 // a EFI_HII_SIBT_FONT block to this string package.
1254 //
1255 StringPackage->FontId++;
1256 }
1257 }
1258
1259 //
1260 // Trigger any registered notification function
1261 //
1262 if (!Matched) {
1263 return InvokeRegisteredFunction (
1264 Private,
1265 EFI_HII_DATABASE_NOTIFY_NEW_PACK,
1266 (VOID *) StringPackage,
1267 EFI_HII_PACKAGE_STRINGS,
1268 PackageList
1269 );
1270 }
1271
1272 return EFI_SUCCESS;
1273 }
1274
1275
1276 /**
1277 This function retrieves the string specified by StringId which is associated
1278 with the specified PackageList in the language Language and copies it into
1279 the buffer specified by String.
1280
1281 @param This A pointer to the EFI_HII_STRING_PROTOCOL instance.
1282 @param Language Points to the language for the retrieved string.
1283 @param PackageList The package list in the HII database to search for
1284 the specified string.
1285 @param StringId The string's id, which is unique within
1286 PackageList.
1287 @param String Points to the new null-terminated string.
1288 @param StringSize On entry, points to the size of the buffer pointed
1289 to by String, in bytes. On return, points to the
1290 length of the string, in bytes.
1291 @param StringFontInfo If not NULL, points to the string's font
1292 information. It's caller's responsibility to free
1293 this buffer.
1294
1295 @retval EFI_SUCCESS The string was returned successfully.
1296 @retval EFI_NOT_FOUND The string specified by StringId is not available.
1297 @retval EFI_NOT_FOUND The string specified by StringId is available but
1298 not in the specified language.
1299 The specified PackageList is not in the database.
1300 @retval EFI_INVALID_LANGUAGE - The string specified by StringId is available but
1301 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringSize is too small to
1302 hold the string.
1303 @retval EFI_INVALID_PARAMETER The String or Language or StringSize was NULL.
1304 @retval EFI_OUT_OF_RESOURCES There were insufficient resources to complete the
1305 request.
1306
1307 **/
1308 EFI_STATUS
1309 EFIAPI
1310 HiiGetString (
1311 IN CONST EFI_HII_STRING_PROTOCOL *This,
1312 IN CONST CHAR8 *Language,
1313 IN EFI_HII_HANDLE PackageList,
1314 IN EFI_STRING_ID StringId,
1315 OUT EFI_STRING String,
1316 IN OUT UINTN *StringSize,
1317 OUT EFI_FONT_INFO **StringFontInfo OPTIONAL
1318 )
1319 {
1320 EFI_STATUS Status;
1321 LIST_ENTRY *Link;
1322 HII_DATABASE_PRIVATE_DATA *Private;
1323 HII_DATABASE_RECORD *DatabaseRecord;
1324 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
1325 HII_STRING_PACKAGE_INSTANCE *StringPackage;
1326 CHAR8 *MatchedLanguage;
1327
1328 if (This == NULL || Language == NULL || StringId < 1 || StringSize == NULL || PackageList == NULL) {
1329 return EFI_INVALID_PARAMETER;
1330 }
1331
1332 if (String == NULL && *StringSize != 0) {
1333 return EFI_INVALID_PARAMETER;
1334 }
1335
1336 if (!IsHiiHandleValid (PackageList)) {
1337 return EFI_NOT_FOUND;
1338 }
1339
1340 Private = HII_STRING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1341 PackageListNode = NULL;
1342
1343 for (Link = Private->DatabaseList.ForwardLink; Link != &Private->DatabaseList; Link = Link->ForwardLink) {
1344 DatabaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
1345 if (DatabaseRecord->Handle == PackageList) {
1346 PackageListNode = DatabaseRecord->PackageList;
1347 break;
1348 }
1349 }
1350
1351 if (PackageListNode != NULL) {
1352 //
1353 // First search: to match the StringId in the specified language.
1354 //
1355 for (Link = PackageListNode->StringPkgHdr.ForwardLink;
1356 Link != &PackageListNode->StringPkgHdr;
1357 Link = Link->ForwardLink
1358 ) {
1359 StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1360 MatchedLanguage = GetBestLanguage (StringPackage->StringPkgHdr->Language, FALSE, (CHAR8 *) Language, NULL);
1361 if (MatchedLanguage != NULL) {
1362 FreePool (MatchedLanguage);
1363 Status = GetStringWorker (Private, StringPackage, StringId, String, StringSize, StringFontInfo);
1364 if (Status != EFI_NOT_FOUND) {
1365 return Status;
1366 }
1367 }
1368 }
1369 //
1370 // Second search: to match the StringId in other available languages if exist.
1371 //
1372 for (Link = PackageListNode->StringPkgHdr.ForwardLink;
1373 Link != &PackageListNode->StringPkgHdr;
1374 Link = Link->ForwardLink
1375 ) {
1376 StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1377 Status = GetStringWorker (Private, StringPackage, StringId, String, StringSize, StringFontInfo);
1378 if (!EFI_ERROR (Status)) {
1379 return EFI_INVALID_LANGUAGE;
1380 }
1381 }
1382 }
1383
1384 return EFI_NOT_FOUND;
1385 }
1386
1387
1388
1389 /**
1390 This function updates the string specified by StringId in the specified PackageList to the text
1391 specified by String and, optionally, the font information specified by StringFontInfo.
1392
1393 @param This A pointer to the EFI_HII_STRING_PROTOCOL instance.
1394 @param PackageList The package list containing the strings.
1395 @param StringId The string's id, which is unique within
1396 PackageList.
1397 @param Language Points to the language for the updated string.
1398 @param String Points to the new null-terminated string.
1399 @param StringFontInfo Points to the string's font information or NULL if
1400 the string font information is not changed.
1401
1402 @retval EFI_SUCCESS The string was updated successfully.
1403 @retval EFI_NOT_FOUND The string specified by StringId is not in the
1404 database.
1405 @retval EFI_INVALID_PARAMETER The String or Language was NULL.
1406 @retval EFI_INVALID_PARAMETER The specified StringFontInfo does not exist in
1407 current database.
1408 @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the
1409 task.
1410
1411 **/
1412 EFI_STATUS
1413 EFIAPI
1414 HiiSetString (
1415 IN CONST EFI_HII_STRING_PROTOCOL *This,
1416 IN EFI_HII_HANDLE PackageList,
1417 IN EFI_STRING_ID StringId,
1418 IN CONST CHAR8 *Language,
1419 IN CONST EFI_STRING String,
1420 IN CONST EFI_FONT_INFO *StringFontInfo OPTIONAL
1421 )
1422 {
1423 EFI_STATUS Status;
1424 LIST_ENTRY *Link;
1425 HII_DATABASE_PRIVATE_DATA *Private;
1426 HII_DATABASE_RECORD *DatabaseRecord;
1427 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
1428 HII_STRING_PACKAGE_INSTANCE *StringPackage;
1429 UINT32 OldPackageLen;
1430 CHAR8 *MatchedLanguage;
1431
1432 if (This == NULL || Language == NULL || StringId < 1 || String == NULL || PackageList == NULL) {
1433 return EFI_INVALID_PARAMETER;
1434 }
1435
1436 if (!IsHiiHandleValid (PackageList)) {
1437 return EFI_NOT_FOUND;
1438 }
1439
1440 Private = HII_STRING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1441 PackageListNode = NULL;
1442
1443 for (Link = Private->DatabaseList.ForwardLink; Link != &Private->DatabaseList; Link = Link->ForwardLink) {
1444 DatabaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
1445 if (DatabaseRecord->Handle == PackageList) {
1446 PackageListNode = (HII_DATABASE_PACKAGE_LIST_INSTANCE *) (DatabaseRecord->PackageList);
1447 }
1448 }
1449
1450 if (PackageListNode != NULL) {
1451 for (Link = PackageListNode->StringPkgHdr.ForwardLink;
1452 Link != &PackageListNode->StringPkgHdr;
1453 Link = Link->ForwardLink
1454 ) {
1455 StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1456 MatchedLanguage = GetBestLanguage (StringPackage->StringPkgHdr->Language, FALSE, (CHAR8 *) Language, NULL);
1457 if (MatchedLanguage != NULL) {
1458 FreePool (MatchedLanguage);
1459 OldPackageLen = StringPackage->StringPkgHdr->Header.Length;
1460 Status = SetStringWorker (
1461 Private,
1462 StringPackage,
1463 StringId,
1464 (EFI_STRING) String,
1465 (EFI_FONT_INFO *) StringFontInfo
1466 );
1467 if (EFI_ERROR (Status)) {
1468 return Status;
1469 }
1470 PackageListNode->PackageListHdr.PackageLength += StringPackage->StringPkgHdr->Header.Length - OldPackageLen;
1471 return EFI_SUCCESS;
1472 }
1473 }
1474 }
1475
1476 return EFI_NOT_FOUND;
1477 }
1478
1479
1480
1481 /**
1482 This function returns the list of supported languages, in the format specified
1483 in Appendix M of UEFI 2.1 spec.
1484
1485 @param This A pointer to the EFI_HII_STRING_PROTOCOL instance.
1486 @param PackageList The package list to examine.
1487 @param Languages Points to the buffer to hold the returned string.
1488 @param LanguagesSize On entry, points to the size of the buffer pointed
1489 to by Languages, in bytes. On return, points to
1490 the length of Languages, in bytes.
1491
1492 @retval EFI_SUCCESS The languages were returned successfully.
1493 @retval EFI_INVALID_PARAMETER The Languages or LanguagesSize was NULL.
1494 @retval EFI_BUFFER_TOO_SMALL The LanguagesSize is too small to hold the list of
1495 supported languages. LanguageSize is updated to
1496 contain the required size.
1497 @retval EFI_NOT_FOUND Could not find string package in specified
1498 packagelist.
1499
1500 **/
1501 EFI_STATUS
1502 EFIAPI
1503 HiiGetLanguages (
1504 IN CONST EFI_HII_STRING_PROTOCOL *This,
1505 IN EFI_HII_HANDLE PackageList,
1506 IN OUT CHAR8 *Languages,
1507 IN OUT UINTN *LanguagesSize
1508 )
1509 {
1510 LIST_ENTRY *Link;
1511 HII_DATABASE_PRIVATE_DATA *Private;
1512 HII_DATABASE_RECORD *DatabaseRecord;
1513 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
1514 HII_STRING_PACKAGE_INSTANCE *StringPackage;
1515 UINTN ResultSize;
1516
1517 if (This == NULL || Languages == NULL || LanguagesSize == NULL || PackageList == NULL) {
1518 return EFI_INVALID_PARAMETER;
1519 }
1520 if (!IsHiiHandleValid (PackageList)) {
1521 return EFI_NOT_FOUND;
1522 }
1523
1524 Private = HII_STRING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1525
1526 PackageListNode = NULL;
1527 for (Link = Private->DatabaseList.ForwardLink; Link != &Private->DatabaseList; Link = Link->ForwardLink) {
1528 DatabaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
1529 if (DatabaseRecord->Handle == PackageList) {
1530 PackageListNode = DatabaseRecord->PackageList;
1531 break;
1532 }
1533 }
1534 if (PackageListNode == NULL) {
1535 return EFI_NOT_FOUND;
1536 }
1537
1538 //
1539 // Search the languages in the specified packagelist.
1540 //
1541 ResultSize = 0;
1542 for (Link = PackageListNode->StringPkgHdr.ForwardLink;
1543 Link != &PackageListNode->StringPkgHdr;
1544 Link = Link->ForwardLink
1545 ) {
1546 StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1547 ResultSize += AsciiStrSize (StringPackage->StringPkgHdr->Language);
1548 if (ResultSize <= *LanguagesSize) {
1549 AsciiStrCpy (Languages, StringPackage->StringPkgHdr->Language);
1550 Languages += AsciiStrSize (StringPackage->StringPkgHdr->Language);
1551 *(Languages - 1) = L';';
1552 }
1553 }
1554 if (ResultSize == 0) {
1555 return EFI_NOT_FOUND;
1556 }
1557
1558 if (*LanguagesSize < ResultSize) {
1559 *LanguagesSize = ResultSize;
1560 return EFI_BUFFER_TOO_SMALL;
1561 }
1562
1563 *(Languages - 1) = 0;
1564 return EFI_SUCCESS;
1565 }
1566
1567
1568 /**
1569 Each string package has associated with it a single primary language and zero
1570 or more secondary languages. This routine returns the secondary languages
1571 associated with a package list.
1572
1573 @param This A pointer to the EFI_HII_STRING_PROTOCOL instance.
1574 @param PackageList The package list to examine.
1575 @param FirstLanguage Points to the primary language.
1576 @param SecondaryLanguages Points to the buffer to hold the returned list of
1577 secondary languages for the specified
1578 FirstLanguage. If there are no secondary
1579 languages, the function returns successfully, but
1580 this is set to NULL.
1581 @param SecondaryLanguagesSize On entry, points to the size of the buffer pointed
1582 to by SecondaryLanguages, in bytes. On return,
1583 points to the length of SecondaryLanguages in bytes.
1584
1585 @retval EFI_SUCCESS Secondary languages were correctly returned.
1586 @retval EFI_INVALID_PARAMETER FirstLanguage or SecondaryLanguages or
1587 SecondaryLanguagesSize was NULL.
1588 @retval EFI_BUFFER_TOO_SMALL The buffer specified by SecondaryLanguagesSize is
1589 too small to hold the returned information.
1590 SecondLanguageSize is updated to hold the size of
1591 the buffer required.
1592 @retval EFI_INVALID_LANGUAGE The language specified by FirstLanguage is not
1593 present in the specified package list.
1594 @retval EFI_NOT_FOUND The specified PackageList is not in the Database.
1595
1596 **/
1597 EFI_STATUS
1598 EFIAPI
1599 HiiGetSecondaryLanguages (
1600 IN CONST EFI_HII_STRING_PROTOCOL *This,
1601 IN EFI_HII_HANDLE PackageList,
1602 IN CONST CHAR8 *FirstLanguage,
1603 IN OUT CHAR8 *SecondaryLanguages,
1604 IN OUT UINTN *SecondaryLanguagesSize
1605 )
1606 {
1607 LIST_ENTRY *Link;
1608 LIST_ENTRY *Link1;
1609 HII_DATABASE_PRIVATE_DATA *Private;
1610 HII_DATABASE_RECORD *DatabaseRecord;
1611 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
1612 HII_STRING_PACKAGE_INSTANCE *StringPackage;
1613 CHAR8 *Languages;
1614 UINTN ResultSize;
1615 CHAR8 *MatchedLanguage;
1616
1617 if (This == NULL || PackageList == NULL || FirstLanguage == NULL) {
1618 return EFI_INVALID_PARAMETER;
1619 }
1620 if (SecondaryLanguages == NULL || SecondaryLanguagesSize == NULL) {
1621 return EFI_INVALID_PARAMETER;
1622 }
1623 if (!IsHiiHandleValid (PackageList)) {
1624 return EFI_NOT_FOUND;
1625 }
1626
1627 Private = HII_STRING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1628
1629 PackageListNode = NULL;
1630 for (Link = Private->DatabaseList.ForwardLink; Link != &Private->DatabaseList; Link = Link->ForwardLink) {
1631 DatabaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
1632 if (DatabaseRecord->Handle == PackageList) {
1633 PackageListNode = (HII_DATABASE_PACKAGE_LIST_INSTANCE *) (DatabaseRecord->PackageList);
1634 break;
1635 }
1636 }
1637 if (PackageListNode == NULL) {
1638 return EFI_NOT_FOUND;
1639 }
1640
1641 Languages = NULL;
1642 ResultSize = 0;
1643 for (Link1 = PackageListNode->StringPkgHdr.ForwardLink;
1644 Link1 != &PackageListNode->StringPkgHdr;
1645 Link1 = Link1->ForwardLink
1646 ) {
1647 StringPackage = CR (Link1, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1648 MatchedLanguage = GetBestLanguage (StringPackage->StringPkgHdr->Language, FALSE, (CHAR8 *) FirstLanguage, NULL);
1649 if (MatchedLanguage != NULL) {
1650 FreePool (MatchedLanguage);
1651 Languages = StringPackage->StringPkgHdr->Language;
1652 //
1653 // Language is a series of ';' terminated strings, first one is primary
1654 // language and following with other secondary languages or NULL if no
1655 // secondary languages any more.
1656 //
1657 Languages = AsciiStrStr (Languages, ";");
1658 if (Languages == NULL) {
1659 break;
1660 }
1661 Languages++;
1662
1663 ResultSize = AsciiStrSize (Languages);
1664 if (ResultSize <= *SecondaryLanguagesSize) {
1665 AsciiStrCpy (SecondaryLanguages, Languages);
1666 } else {
1667 *SecondaryLanguagesSize = ResultSize;
1668 return EFI_BUFFER_TOO_SMALL;
1669 }
1670
1671 return EFI_SUCCESS;
1672 }
1673 }
1674
1675 return EFI_INVALID_LANGUAGE;
1676 }
1677