2 # This file is used to parse a strings file and create or add to a string database
5 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 # 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
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.
17 from __future__
import absolute_import
19 import Common
.EdkLogger
as EdkLogger
20 from Common
.BuildToolError
import *
21 from .UniClassObject
import *
22 from io
import BytesIO
23 from struct
import pack
, unpack
24 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
29 EFI_HII_SIBT_END
= '0x00'
30 EFI_HII_SIBT_STRING_SCSU
= '0x10'
31 EFI_HII_SIBT_STRING_SCSU_FONT
= '0x11'
32 EFI_HII_SIBT_STRINGS_SCSU
= '0x12'
33 EFI_HII_SIBT_STRINGS_SCSU_FONT
= '0x13'
34 EFI_HII_SIBT_STRING_UCS2
= '0x14'
35 EFI_HII_SIBT_STRING_UCS2_FONT
= '0x15'
36 EFI_HII_SIBT_STRINGS_UCS2
= '0x16'
37 EFI_HII_SIBT_STRINGS_UCS2_FONT
= '0x17'
38 EFI_HII_SIBT_DUPLICATE
= '0x20'
39 EFI_HII_SIBT_SKIP2
= '0x21'
40 EFI_HII_SIBT_SKIP1
= '0x22'
41 EFI_HII_SIBT_EXT1
= '0x30'
42 EFI_HII_SIBT_EXT2
= '0x31'
43 EFI_HII_SIBT_EXT4
= '0x32'
44 EFI_HII_SIBT_FONT
= '0x40'
46 EFI_HII_PACKAGE_STRINGS
= '0x04'
47 EFI_HII_PACKAGE_FORM
= '0x02'
49 StringPackageType
= EFI_HII_PACKAGE_STRINGS
50 StringPackageForm
= EFI_HII_PACKAGE_FORM
51 StringBlockType
= EFI_HII_SIBT_STRING_UCS2
52 StringSkipType
= EFI_HII_SIBT_SKIP2
57 DEFINE_STR
= '#define'
58 COMMENT_DEFINE_STR
= COMMENT
+ DEFINE_STR
59 NOT_REFERENCED
= 'not referenced'
60 COMMENT_NOT_REFERENCED
= ' ' + COMMENT
+ NOT_REFERENCED
61 CHAR_ARRAY_DEFIN
= 'unsigned char'
62 COMMON_FILE_NAME
= 'Strings'
63 STRING_TOKEN
= re
.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re
.MULTILINE | re
.UNICODE
)
65 EFI_HII_ARRAY_SIZE_LENGTH
= 4
66 EFI_HII_PACKAGE_HEADER_LENGTH
= 4
67 EFI_HII_HDR_SIZE_LENGTH
= 4
68 EFI_HII_STRING_OFFSET_LENGTH
= 4
70 EFI_STRING_ID_LENGTH
= 2
71 EFI_HII_LANGUAGE_WINDOW
= 0
72 EFI_HII_LANGUAGE_WINDOW_LENGTH
= 2
73 EFI_HII_LANGUAGE_WINDOW_NUMBER
= 16
74 EFI_HII_STRING_PACKAGE_HDR_LENGTH
= EFI_HII_PACKAGE_HEADER_LENGTH
+ EFI_HII_HDR_SIZE_LENGTH
+ EFI_HII_STRING_OFFSET_LENGTH
+ EFI_HII_LANGUAGE_WINDOW_LENGTH
* EFI_HII_LANGUAGE_WINDOW_NUMBER
+ EFI_STRING_ID_LENGTH
76 H_C_FILE_HEADER
= ['//', \
77 '// DO NOT EDIT -- auto-generated file', \
79 '// This file is generated by the StrGather utility', \
81 LANGUAGE_NAME_STRING_NAME
= '$LANGUAGE_NAME'
82 PRINTABLE_LANGUAGE_NAME_STRING_NAME
= '$PRINTABLE_LANGUAGE_NAME'
84 ## Convert a dec number to a hex string
86 # Convert a dec number to a formatted hex string in length digit
87 # The digit is set to default 8
88 # The hex string starts with "0x"
89 # DecToHexStr(1000) is '0x000003E8'
90 # DecToHexStr(1000, 6) is '0x0003E8'
92 # @param Dec: The number in dec format
93 # @param Digit: The needed digit of hex string
95 # @retval: The formatted hex string
97 def DecToHexStr(Dec
, Digit
= 8):
98 return '0x{0:0{1}X}'.format(Dec
, Digit
)
100 ## Convert a dec number to a hex list
102 # Convert a dec number to a formatted hex list in size digit
103 # The digit is set to default 8
104 # DecToHexList(1000) is ['0xE8', '0x03', '0x00', '0x00']
105 # DecToHexList(1000, 6) is ['0xE8', '0x03', '0x00']
107 # @param Dec: The number in dec format
108 # @param Digit: The needed digit of hex list
110 # @retval: A list for formatted hex string
112 def DecToHexList(Dec
, Digit
= 8):
113 Hex
= '{0:0{1}X}'.format(Dec
, Digit
)
114 return ["0x" + Hex
[Bit
:Bit
+ 2] for Bit
in range(Digit
- 2, -1, -2)]
116 ## Convert a acsii string to a hex list
118 # Convert a acsii string to a formatted hex list
119 # AscToHexList('en-US') is ['0x65', '0x6E', '0x2D', '0x55', '0x53']
121 # @param Ascii: The acsii string
123 # @retval: A list for formatted hex string
125 def AscToHexList(Ascii
):
127 return ['0x{0:02X}'.format(Item
) for Item
in Ascii
]
129 return ['0x{0:02X}'.format(ord(Item
)) for Item
in Ascii
]
131 ## Create content of .h file
133 # Create content of .h file
135 # @param BaseName: The basename of strings
136 # @param UniObjectClass A UniObjectClass instance
137 # @param IsCompatibleMode Compatible mode
138 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
140 # @retval Str: A string of .h file content
142 def CreateHFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, UniGenCFlag
):
145 Line
= COMMENT_DEFINE_STR
+ ' ' + LANGUAGE_NAME_STRING_NAME
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ LANGUAGE_NAME_STRING_NAME
)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED
146 Str
= WriteLine(Str
, Line
)
147 Line
= COMMENT_DEFINE_STR
+ ' ' + PRINTABLE_LANGUAGE_NAME_STRING_NAME
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ PRINTABLE_LANGUAGE_NAME_STRING_NAME
)) + DecToHexStr(1, 4) + COMMENT_NOT_REFERENCED
148 Str
= WriteLine(Str
, Line
)
151 #Group the referred/Unused STRING token together.
152 for Index
in range(2, len(UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[0][0]])):
153 StringItem
= UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[0][0]][Index
]
154 Name
= StringItem
.StringName
155 Token
= StringItem
.Token
156 Referenced
= StringItem
.Referenced
159 if Referenced
== True:
160 if (ValueStartPtr
- len(DEFINE_STR
+ Name
)) <= 0:
161 Line
= DEFINE_STR
+ ' ' + Name
+ ' ' + DecToHexStr(Token
, 4)
163 Line
= DEFINE_STR
+ ' ' + Name
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ Name
)) + DecToHexStr(Token
, 4)
164 Str
= WriteLine(Str
, Line
)
166 if (ValueStartPtr
- len(DEFINE_STR
+ Name
)) <= 0:
167 Line
= COMMENT_DEFINE_STR
+ ' ' + Name
+ ' ' + DecToHexStr(Token
, 4) + COMMENT_NOT_REFERENCED
169 Line
= COMMENT_DEFINE_STR
+ ' ' + Name
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ Name
)) + DecToHexStr(Token
, 4) + COMMENT_NOT_REFERENCED
170 UnusedStr
= WriteLine(UnusedStr
, Line
)
172 Str
.extend( UnusedStr
)
174 Str
= WriteLine(Str
, '')
175 if IsCompatibleMode
or UniGenCFlag
:
176 Str
= WriteLine(Str
, 'extern unsigned char ' + BaseName
+ 'Strings[];')
179 ## Create a complete .h file
181 # Create a complet .h file with file header and file content
183 # @param BaseName: The basename of strings
184 # @param UniObjectClass A UniObjectClass instance
185 # @param IsCompatibleMode Compatible mode
186 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
188 # @retval Str: A string of complete .h file
190 def CreateHFile(BaseName
, UniObjectClass
, IsCompatibleMode
, UniGenCFlag
):
191 HFile
= WriteLine('', CreateHFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, UniGenCFlag
))
193 return "".join(HFile
)
195 ## Create a buffer to store all items in an array
197 # @param BinBuffer Buffer to contain Binary data.
198 # @param Array: The array need to be formatted
200 def CreateBinBuffer(BinBuffer
, Array
):
202 BinBuffer
.write(pack("B", int(Item
, 16)))
204 ## Create a formatted string all items in an array
206 # Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)
208 # @param Array: The array need to be formatted
209 # @param Width: The line length, the default value is set to 16
211 # @retval ArrayItem: A string for all formatted array items
213 def CreateArrayItem(Array
, Width
= 16):
220 if Index
< MaxLength
:
221 Line
= Line
+ Item
+ ', '
224 ArrayItem
= WriteLine(ArrayItem
, Line
)
225 Line
= ' ' + Item
+ ', '
227 ArrayItem
= Write(ArrayItem
, Line
.rstrip())
229 return "".join(ArrayItem
)
231 ## CreateCFileStringValue
233 # Create a line with string value
235 # @param Value: Value of the string
237 # @retval Str: A formatted string with string value
240 def CreateCFileStringValue(Value
):
241 Value
= [StringBlockType
] + Value
242 Str
= WriteLine('', CreateArrayItem(Value
))
246 ## GetFilteredLanguage
248 # apply get best language rules to the UNI language code list
250 # @param UniLanguageList: language code definition list in *.UNI file
251 # @param LanguageFilterList: language code filter list of RFC4646 format in DSC file
253 # @retval UniLanguageListFiltered: the filtered language code
255 def GetFilteredLanguage(UniLanguageList
, LanguageFilterList
):
256 UniLanguageListFiltered
= []
257 # if filter list is empty, then consider there is no filter
258 if LanguageFilterList
== []:
259 UniLanguageListFiltered
= UniLanguageList
260 return UniLanguageListFiltered
261 for Language
in LanguageFilterList
:
262 # first check for exact match
263 if Language
in UniLanguageList
:
264 if Language
not in UniLanguageListFiltered
:
265 UniLanguageListFiltered
.append(Language
)
266 # find the first one with the same/equivalent primary tag
268 if Language
.find('-') != -1:
269 PrimaryTag
= Language
[0:Language
.find('-')].lower()
271 PrimaryTag
= Language
273 if len(PrimaryTag
) == 3:
274 PrimaryTag
= LangConvTable
.get(PrimaryTag
)
276 for UniLanguage
in UniLanguageList
:
277 if UniLanguage
.find('-') != -1:
278 UniLanguagePrimaryTag
= UniLanguage
[0:UniLanguage
.find('-')].lower()
280 UniLanguagePrimaryTag
= UniLanguage
282 if len(UniLanguagePrimaryTag
) == 3:
283 UniLanguagePrimaryTag
= LangConvTable
.get(UniLanguagePrimaryTag
)
285 if PrimaryTag
== UniLanguagePrimaryTag
:
286 if UniLanguage
not in UniLanguageListFiltered
:
287 UniLanguageListFiltered
.append(UniLanguage
)
290 # Here is rule 3 for "get best language"
291 # If tag is not listed in the Unicode file, the default ("en") tag should be used for that language
292 # for better processing, find the one that best suit for it.
294 if DefaultTag
not in UniLanguageListFiltered
:
295 # check whether language code with primary code equivalent with DefaultTag already in the list, if so, use that
296 for UniLanguage
in UniLanguageList
:
297 if UniLanguage
.startswith('en-') or UniLanguage
.startswith('eng-'):
298 if UniLanguage
not in UniLanguageListFiltered
:
299 UniLanguageListFiltered
.append(UniLanguage
)
302 UniLanguageListFiltered
.append(DefaultTag
)
303 return UniLanguageListFiltered
306 ## Create content of .c file
308 # Create content of .c file
310 # @param BaseName: The basename of strings
311 # @param UniObjectClass A UniObjectClass instance
312 # @param IsCompatibleMode Compatible mode
313 # @param UniBinBuffer UniBinBuffer to contain UniBinary data.
314 # @param FilterInfo Platform language filter information
316 # @retval Str: A string of .c file content
318 def CreateCFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, UniBinBuffer
, FilterInfo
):
322 TotalLength
= EFI_HII_ARRAY_SIZE_LENGTH
326 EDK2Module
= FilterInfo
[0]
328 LanguageFilterList
= FilterInfo
[1]
330 # EDK module is using ISO639-2 format filter, convert to the RFC4646 format
331 LanguageFilterList
= [LangConvTable
.get(F
.lower()) for F
in FilterInfo
[1]]
334 for IndexI
in range(len(UniObjectClass
.LanguageDef
)):
335 UniLanguageList
+= [UniObjectClass
.LanguageDef
[IndexI
][0]]
337 UniLanguageListFiltered
= GetFilteredLanguage(UniLanguageList
, LanguageFilterList
)
341 # Create lines for each language's strings
343 for IndexI
in range(len(UniObjectClass
.LanguageDef
)):
344 Language
= UniObjectClass
.LanguageDef
[IndexI
][0]
345 if Language
not in UniLanguageListFiltered
:
348 StringBuffer
= BytesIO()
351 NumberOfUseOtherLangDef
= 0
353 for IndexJ
in range(1, len(UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[IndexI
][0]])):
354 Item
= UniObjectClass
.OrderedStringListByToken
[Language
][IndexJ
]
356 Name
= Item
.StringName
357 Value
= Item
.StringValueByteList
358 Referenced
= Item
.Referenced
360 UseOtherLangDef
= Item
.UseOtherLangDef
362 if UseOtherLangDef
!= '' and Referenced
:
363 NumberOfUseOtherLangDef
= NumberOfUseOtherLangDef
+ 1
366 if NumberOfUseOtherLangDef
> 0:
367 StrStringValue
= WriteLine(StrStringValue
, CreateArrayItem([StringSkipType
] + DecToHexList(NumberOfUseOtherLangDef
, 4)))
368 CreateBinBuffer (StringBuffer
, ([StringSkipType
] + DecToHexList(NumberOfUseOtherLangDef
, 4)))
369 NumberOfUseOtherLangDef
= 0
370 ArrayLength
= ArrayLength
+ 3
371 if Referenced
and Item
.Token
> 0:
373 StrStringValue
= WriteLine(StrStringValue
, "// %s: %s:%s" % (DecToHexStr(Index
, 4), Name
, DecToHexStr(Token
, 4)))
374 StrStringValue
= Write(StrStringValue
, CreateCFileStringValue(Value
))
375 CreateBinBuffer (StringBuffer
, [StringBlockType
] + Value
)
376 ArrayLength
= ArrayLength
+ Item
.Length
+ 1 # 1 is for the length of string type
379 # EFI_HII_PACKAGE_HEADER
381 Offset
= EFI_HII_STRING_PACKAGE_HDR_LENGTH
+ len(Language
) + 1
382 ArrayLength
= Offset
+ ArrayLength
+ 1
385 # Create PACKAGE HEADER
387 Str
= WriteLine(Str
, '// PACKAGE HEADER\n')
388 TotalLength
= TotalLength
+ ArrayLength
390 List
= DecToHexList(ArrayLength
, 6) + \
391 [StringPackageType
] + \
392 DecToHexList(Offset
) + \
393 DecToHexList(Offset
) + \
394 DecToHexList(EFI_HII_LANGUAGE_WINDOW
, EFI_HII_LANGUAGE_WINDOW_LENGTH
* 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER
+ \
395 DecToHexList(EFI_STRING_ID
, 4) + \
396 AscToHexList(Language
) + \
398 Str
= WriteLine(Str
, CreateArrayItem(List
, 16) + '\n')
401 # Create PACKAGE DATA
403 Str
= WriteLine(Str
, '// PACKAGE DATA\n')
404 Str
= Write(Str
, StrStringValue
)
407 # Add an EFI_HII_SIBT_END at last
409 Str
= WriteLine(Str
, ' ' + EFI_HII_SIBT_END
+ ",")
412 # Create binary UNI string
415 CreateBinBuffer (UniBinBuffer
, List
)
416 UniBinBuffer
.write (StringBuffer
.getvalue())
417 UniBinBuffer
.write (pack("B", int(EFI_HII_SIBT_END
, 16)))
421 # Create line for string variable name
422 # "unsigned char $(BaseName)Strings[] = {"
424 AllStr
= WriteLine('', CHAR_ARRAY_DEFIN
+ ' ' + BaseName
+ COMMON_FILE_NAME
+ '[] = {\n')
428 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode
430 AllStr
= WriteLine(AllStr
, '// FRAMEWORK PACKAGE HEADER Length')
431 AllStr
= WriteLine(AllStr
, CreateArrayItem(DecToHexList(TotalLength
+ 2)) + '\n')
432 AllStr
= WriteLine(AllStr
, '// FRAMEWORK PACKAGE HEADER Type')
433 AllStr
= WriteLine(AllStr
, CreateArrayItem(DecToHexList(2, 4)) + '\n')
436 # Create whole array length in UEFI mode
438 AllStr
= WriteLine(AllStr
, '// STRGATHER_OUTPUT_HEADER')
439 AllStr
= WriteLine(AllStr
, CreateArrayItem(DecToHexList(TotalLength
)) + '\n')
444 AllStr
= Write(AllStr
, Str
)
446 return "".join(AllStr
)
448 ## Create end of .c file
450 # Create end of .c file
452 # @retval Str: A string of .h file end
454 def CreateCFileEnd():
455 Str
= Write('', '};')
460 # Create a complete .c file
462 # @param BaseName: The basename of strings
463 # @param UniObjectClass A UniObjectClass instance
464 # @param IsCompatibleMode Compatible Mode
465 # @param FilterInfo Platform language filter information
467 # @retval CFile: A string of complete .c file
469 def CreateCFile(BaseName
, UniObjectClass
, IsCompatibleMode
, FilterInfo
):
471 CFile
= WriteLine(CFile
, CreateCFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, None, FilterInfo
))
472 CFile
= WriteLine(CFile
, CreateCFileEnd())
473 return "".join(CFile
)
477 # Get a list for all files
479 # @param IncludeList: A list of all path to be searched
480 # @param SkipList: A list of all types of file could be skipped
482 # @retval FileList: A list of all files found
484 def GetFileList(SourceFileList
, IncludeList
, SkipList
):
485 if IncludeList
is None:
486 EdkLogger
.error("UnicodeStringGather", AUTOGEN_ERROR
, "Include path for unicode file is not defined")
492 for File
in SourceFileList
:
493 for Dir
in IncludeList
:
494 if not os
.path
.exists(Dir
):
496 File
= os
.path
.join(Dir
, File
.Path
)
500 if os
.path
.isfile(File
) != True:
503 # Ignore file listed in skip list
506 for Skip
in SkipList
:
507 if os
.path
.splitext(File
)[1].upper() == Skip
.upper():
508 EdkLogger
.verbose("Skipped %s for string token uses search" % File
)
513 FileList
.append(File
)
521 # Search whether all string defined in UniObjectClass are referenced
522 # All string used should be set to Referenced
524 # @param UniObjectClass: Input UniObjectClass
525 # @param FileList: Search path list
526 # @param IsCompatibleMode Compatible Mode
528 # @retval UniObjectClass: UniObjectClass after searched
530 def SearchString(UniObjectClass
, FileList
, IsCompatibleMode
):
532 return UniObjectClass
534 for File
in FileList
:
535 if os
.path
.isfile(File
):
536 Lines
= open(File
, 'r')
538 for StrName
in STRING_TOKEN
.findall(Line
):
539 EdkLogger
.debug(EdkLogger
.DEBUG_5
, "Found string identifier: " + StrName
)
540 UniObjectClass
.SetStringReferenced(StrName
)
542 UniObjectClass
.ReToken()
544 return UniObjectClass
548 # This function is used for UEFI2.1 spec
551 def GetStringFiles(UniFilList
, SourceFileList
, IncludeList
, IncludePathList
, SkipList
, BaseName
, IsCompatibleMode
= False, ShellMode
= False, UniGenCFlag
= True, UniGenBinBuffer
= None, FilterInfo
= [True, []]):
552 if len(UniFilList
) > 0:
555 # support ISO 639-2 codes in .UNI files of EDK Shell
557 Uni
= UniFileClassObject(sorted(UniFilList
, key
=lambda x
: x
.File
), True, IncludePathList
)
559 Uni
= UniFileClassObject(sorted(UniFilList
, key
=lambda x
: x
.File
), IsCompatibleMode
, IncludePathList
)
561 EdkLogger
.error("UnicodeStringGather", AUTOGEN_ERROR
, 'No unicode files given')
563 FileList
= GetFileList(SourceFileList
, IncludeList
, SkipList
)
565 Uni
= SearchString(Uni
, sorted (FileList
), IsCompatibleMode
)
567 HFile
= CreateHFile(BaseName
, Uni
, IsCompatibleMode
, UniGenCFlag
)
569 if IsCompatibleMode
or UniGenCFlag
:
570 CFile
= CreateCFile(BaseName
, Uni
, IsCompatibleMode
, FilterInfo
)
572 CreateCFileContent(BaseName
, Uni
, IsCompatibleMode
, UniGenBinBuffer
, FilterInfo
)
579 def Write(Target
, Item
):
580 if isinstance(Target
,str):
584 if isinstance(Item
,list):
591 # Write an item with a break line
593 def WriteLine(Target
, Item
):
594 if isinstance(Target
,str):
598 if isinstance(Item
, list):
605 # This acts like the main() function for the script, unless it is 'import'ed into another
607 if __name__
== '__main__':
608 EdkLogger
.info('start')
611 r
'C:\\Edk\\Strings2.uni',
612 r
'C:\\Edk\\Strings.uni'
616 for Root
, Dirs
, Files
in os
.walk('C:\\Edk'):
618 SrcFileList
.append(File
)
624 SkipList
= ['.inf', '.uni']
625 BaseName
= 'DriverSample'
626 (h
, c
) = GetStringFiles(UniFileList
, SrcFileList
, IncludeList
, SkipList
, BaseName
, True)
627 hfile
= open('unistring.h', 'w')
628 cfile
= open('unistring.c', 'w')
632 EdkLogger
.info('end')