1 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
2 # This program and the accompanying materials
3 # are licensed and made available under the terms and conditions of the BSD License
4 # which accompanies this distribution. The full text of the license may be found at
5 # http://opensource.org/licenses/bsd-license.php
7 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
8 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 #This file is used to parse a strings file and create or add to a string database file.
18 import Common
.EdkLogger
as EdkLogger
19 from Common
.BuildToolError
import *
20 from UniClassObject
import *
21 from StringIO
import StringIO
22 from struct
import pack
27 EFI_HII_SIBT_END
= '0x00'
28 EFI_HII_SIBT_STRING_SCSU
= '0x10'
29 EFI_HII_SIBT_STRING_SCSU_FONT
= '0x11'
30 EFI_HII_SIBT_STRINGS_SCSU
= '0x12'
31 EFI_HII_SIBT_STRINGS_SCSU_FONT
= '0x13'
32 EFI_HII_SIBT_STRING_UCS2
= '0x14'
33 EFI_HII_SIBT_STRING_UCS2_FONT
= '0x15'
34 EFI_HII_SIBT_STRINGS_UCS2
= '0x16'
35 EFI_HII_SIBT_STRINGS_UCS2_FONT
= '0x17'
36 EFI_HII_SIBT_DUPLICATE
= '0x20'
37 EFI_HII_SIBT_SKIP2
= '0x21'
38 EFI_HII_SIBT_SKIP1
= '0x22'
39 EFI_HII_SIBT_EXT1
= '0x30'
40 EFI_HII_SIBT_EXT2
= '0x31'
41 EFI_HII_SIBT_EXT4
= '0x32'
42 EFI_HII_SIBT_FONT
= '0x40'
44 EFI_HII_PACKAGE_STRINGS
= '0x04'
45 EFI_HII_PACKAGE_FORM
= '0x02'
47 StringPackageType
= EFI_HII_PACKAGE_STRINGS
48 StringPackageForm
= EFI_HII_PACKAGE_FORM
49 StringBlockType
= EFI_HII_SIBT_STRING_UCS2
50 StringSkipType
= EFI_HII_SIBT_SKIP2
55 DEFINE_STR
= '#define'
56 COMMENT_DEFINE_STR
= COMMENT
+ DEFINE_STR
57 NOT_REFERENCED
= 'not referenced'
58 COMMENT_NOT_REFERENCED
= ' ' + COMMENT
+ NOT_REFERENCED
59 CHAR_ARRAY_DEFIN
= 'unsigned char'
60 COMMON_FILE_NAME
= 'Strings'
64 STRING_TOKEN
= re
.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re
.MULTILINE | re
.UNICODE
)
65 COMPATIBLE_STRING_TOKEN
= re
.compile('STRING_TOKEN *\(([A-Za-z0-9_]+) *\)', re
.MULTILINE | re
.UNICODE
)
67 EFI_HII_ARRAY_SIZE_LENGTH
= 4
68 EFI_HII_PACKAGE_HEADER_LENGTH
= 4
69 EFI_HII_HDR_SIZE_LENGTH
= 4
70 EFI_HII_STRING_OFFSET_LENGTH
= 4
72 EFI_STRING_ID_LENGTH
= 2
73 EFI_HII_LANGUAGE_WINDOW
= 0
74 EFI_HII_LANGUAGE_WINDOW_LENGTH
= 2
75 EFI_HII_LANGUAGE_WINDOW_NUMBER
= 16
76 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
78 H_C_FILE_HEADER
= ['//', \
79 '// DO NOT EDIT -- auto-generated file', \
81 '// This file is generated by the StrGather utility', \
83 LANGUAGE_NAME_STRING_NAME
= '$LANGUAGE_NAME'
84 PRINTABLE_LANGUAGE_NAME_STRING_NAME
= '$PRINTABLE_LANGUAGE_NAME'
86 ## Convert a dec number to a hex string
88 # Convert a dec number to a formatted hex string in length digit
89 # The digit is set to default 8
90 # The hex string starts with "0x"
91 # DecToHexStr(1000) is '0x000003E8'
92 # DecToHexStr(1000, 6) is '0x0003E8'
94 # @param Dec: The number in dec format
95 # @param Digit: The needed digit of hex string
97 # @retval: The formatted hex string
99 def DecToHexStr(Dec
, Digit
= 8):
100 return eval("'0x%0" + str(Digit
) + "X' % int(Dec)")
102 ## Convert a dec number to a hex list
104 # Convert a dec number to a formatted hex list in size digit
105 # The digit is set to default 8
106 # DecToHexList(1000) is ['0xE8', '0x03', '0x00', '0x00']
107 # DecToHexList(1000, 6) is ['0xE8', '0x03', '0x00']
109 # @param Dec: The number in dec format
110 # @param Digit: The needed digit of hex list
112 # @retval: A list for formatted hex string
114 def DecToHexList(Dec
, Digit
= 8):
115 Hex
= eval("'%0" + str(Digit
) + "X' % int(Dec)" )
117 for Bit
in range(Digit
- 2, -1, -2):
118 List
.append(HexHeader
+ Hex
[Bit
:Bit
+ 2])
121 ## Convert a acsii string to a hex list
123 # Convert a acsii string to a formatted hex list
124 # AscToHexList('en-US') is ['0x65', '0x6E', '0x2D', '0x55', '0x53']
126 # @param Ascii: The acsii string
128 # @retval: A list for formatted hex string
130 def AscToHexList(Ascii
):
133 List
.append('0x%2X' % ord(Item
))
137 ## Create header of .h file
139 # Create a header of .h file
141 # @param BaseName: The basename of strings
143 # @retval Str: A string for .h file header
145 def CreateHFileHeader(BaseName
):
147 for Item
in H_C_FILE_HEADER
:
148 Str
= WriteLine(Str
, Item
)
149 Str
= WriteLine(Str
, '#ifndef _' + BaseName
.upper() + '_STRINGS_DEFINE_H_')
150 Str
= WriteLine(Str
, '#define _' + BaseName
.upper() + '_STRINGS_DEFINE_H_')
153 ## Create content of .h file
155 # Create content of .h file
157 # @param BaseName: The basename of strings
158 # @param UniObjectClass A UniObjectClass instance
159 # @param IsCompatibleMode Compatible mode
160 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
162 # @retval Str: A string of .h file content
164 def CreateHFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, UniGenCFlag
):
167 Line
= COMMENT_DEFINE_STR
+ ' ' + LANGUAGE_NAME_STRING_NAME
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ LANGUAGE_NAME_STRING_NAME
)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED
168 Str
= WriteLine(Str
, Line
)
169 Line
= COMMENT_DEFINE_STR
+ ' ' + PRINTABLE_LANGUAGE_NAME_STRING_NAME
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ PRINTABLE_LANGUAGE_NAME_STRING_NAME
)) + DecToHexStr(1, 4) + COMMENT_NOT_REFERENCED
170 Str
= WriteLine(Str
, Line
)
172 #Group the referred STRING token together.
173 for Index
in range(2, len(UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[0][0]])):
174 StringItem
= UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[0][0]][Index
]
175 Name
= StringItem
.StringName
176 Token
= StringItem
.Token
177 Referenced
= StringItem
.Referenced
180 if Referenced
== True:
181 if (ValueStartPtr
- len(DEFINE_STR
+ Name
)) <= 0:
182 Line
= DEFINE_STR
+ ' ' + Name
+ ' ' + DecToHexStr(Token
, 4)
184 Line
= DEFINE_STR
+ ' ' + Name
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ Name
)) + DecToHexStr(Token
, 4)
185 Str
= WriteLine(Str
, Line
)
187 #Group the unused STRING token together.
188 for Index
in range(2, len(UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[0][0]])):
189 StringItem
= UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[0][0]][Index
]
190 Name
= StringItem
.StringName
191 Token
= StringItem
.Token
192 Referenced
= StringItem
.Referenced
195 if Referenced
== False:
196 if (ValueStartPtr
- len(DEFINE_STR
+ Name
)) <= 0:
197 Line
= COMMENT_DEFINE_STR
+ ' ' + Name
+ ' ' + DecToHexStr(Token
, 4) + COMMENT_NOT_REFERENCED
199 Line
= COMMENT_DEFINE_STR
+ ' ' + Name
+ ' ' * (ValueStartPtr
- len(DEFINE_STR
+ Name
)) + DecToHexStr(Token
, 4) + COMMENT_NOT_REFERENCED
200 Str
= WriteLine(Str
, Line
)
202 Str
= WriteLine(Str
, '')
203 if IsCompatibleMode
or UniGenCFlag
:
204 Str
= WriteLine(Str
, 'extern unsigned char ' + BaseName
+ 'Strings[];')
207 ## Create a complete .h file
209 # Create a complet .h file with file header and file content
211 # @param BaseName: The basename of strings
212 # @param UniObjectClass A UniObjectClass instance
213 # @param IsCompatibleMode Compatible mode
214 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
216 # @retval Str: A string of complete .h file
218 def CreateHFile(BaseName
, UniObjectClass
, IsCompatibleMode
, UniGenCFlag
):
219 HFile
= WriteLine('', CreateHFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, UniGenCFlag
))
223 ## Create header of .c file
225 # Create a header of .c file
227 # @retval Str: A string for .c file header
229 def CreateCFileHeader():
231 for Item
in H_C_FILE_HEADER
:
232 Str
= WriteLine(Str
, Item
)
236 ## Create a buffer to store all items in an array
238 # @param BinBuffer Buffer to contain Binary data.
239 # @param Array: The array need to be formatted
241 def CreateBinBuffer(BinBuffer
, Array
):
243 BinBuffer
.write(pack("B", int(Item
,16)))
245 ## Create a formatted string all items in an array
247 # Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)
249 # @param Array: The array need to be formatted
250 # @param Width: The line length, the default value is set to 16
252 # @retval ArrayItem: A string for all formatted array items
254 def CreateArrayItem(Array
, Width
= 16):
261 if Index
< MaxLength
:
262 Line
= Line
+ Item
+ ', '
265 ArrayItem
= WriteLine(ArrayItem
, Line
)
266 Line
= ' ' + Item
+ ', '
268 ArrayItem
= Write(ArrayItem
, Line
.rstrip())
272 ## CreateCFileStringValue
274 # Create a line with string value
276 # @param Value: Value of the string
278 # @retval Str: A formatted string with string value
281 def CreateCFileStringValue(Value
):
282 Value
= [StringBlockType
] + Value
283 Str
= WriteLine('', CreateArrayItem(Value
))
288 ## Create content of .c file
290 # Create content of .c file
292 # @param BaseName: The basename of strings
293 # @param UniObjectClass A UniObjectClass instance
294 # @param IsCompatibleMode Compatible mode
295 # @param UniBinBuffer UniBinBuffer to contain UniBinary data.
297 # @retval Str: A string of .c file content
299 def CreateCFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
, UniBinBuffer
=None):
303 TotalLength
= EFI_HII_ARRAY_SIZE_LENGTH
308 # Create lines for each language's strings
310 for IndexI
in range(len(UniObjectClass
.LanguageDef
)):
311 Language
= UniObjectClass
.LanguageDef
[IndexI
][0]
312 LangPrintName
= UniObjectClass
.LanguageDef
[IndexI
][1]
314 StringBuffer
= StringIO()
317 NumberOfUseOtherLangDef
= 0
319 for IndexJ
in range(1, len(UniObjectClass
.OrderedStringList
[UniObjectClass
.LanguageDef
[IndexI
][0]])):
320 Item
= UniObjectClass
.FindByToken(IndexJ
, Language
)
321 Name
= Item
.StringName
322 Value
= Item
.StringValueByteList
323 Referenced
= Item
.Referenced
326 UseOtherLangDef
= Item
.UseOtherLangDef
328 if UseOtherLangDef
!= '' and Referenced
:
329 NumberOfUseOtherLangDef
= NumberOfUseOtherLangDef
+ 1
332 if NumberOfUseOtherLangDef
> 0:
333 StrStringValue
= WriteLine(StrStringValue
, CreateArrayItem([StringSkipType
] + DecToHexList(NumberOfUseOtherLangDef
, 4)))
334 CreateBinBuffer (StringBuffer
, ([StringSkipType
] + DecToHexList(NumberOfUseOtherLangDef
, 4)))
335 NumberOfUseOtherLangDef
= 0
336 ArrayLength
= ArrayLength
+ 3
337 if Referenced
and Item
.Token
> 0:
339 StrStringValue
= WriteLine(StrStringValue
, "// %s: %s:%s" % (DecToHexStr(Index
, 4), Name
, DecToHexStr(Token
, 4)))
340 StrStringValue
= Write(StrStringValue
, CreateCFileStringValue(Value
))
341 CreateBinBuffer (StringBuffer
, [StringBlockType
] + Value
)
342 ArrayLength
= ArrayLength
+ Item
.Length
+ 1 # 1 is for the length of string type
345 # EFI_HII_PACKAGE_HEADER
347 Offset
= EFI_HII_STRING_PACKAGE_HDR_LENGTH
+ len(Language
) + 1
348 ArrayLength
= Offset
+ ArrayLength
+ 1
351 # Create PACKAGE HEADER
353 Str
= WriteLine(Str
, '// PACKAGE HEADER\n')
354 TotalLength
= TotalLength
+ ArrayLength
356 List
= DecToHexList(ArrayLength
, 6) + \
357 [StringPackageType
] + \
358 DecToHexList(Offset
) + \
359 DecToHexList(Offset
) + \
360 DecToHexList(EFI_HII_LANGUAGE_WINDOW
, EFI_HII_LANGUAGE_WINDOW_LENGTH
* 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER
+ \
361 DecToHexList(EFI_STRING_ID
, 4) + \
362 AscToHexList(Language
) + \
364 Str
= WriteLine(Str
, CreateArrayItem(List
, 16) + '\n')
367 # Create PACKAGE DATA
369 Str
= WriteLine(Str
, '// PACKAGE DATA\n')
370 Str
= Write(Str
, StrStringValue
)
373 # Add an EFI_HII_SIBT_END at last
375 Str
= WriteLine(Str
, ' ' + EFI_HII_SIBT_END
+ ",")
378 # Create binary UNI string
381 CreateBinBuffer (UniBinBuffer
, List
)
382 UniBinBuffer
.write (StringBuffer
.getvalue())
383 UniBinBuffer
.write (pack("B", int(EFI_HII_SIBT_END
,16)))
387 # Create line for string variable name
388 # "unsigned char $(BaseName)Strings[] = {"
390 AllStr
= WriteLine('', CHAR_ARRAY_DEFIN
+ ' ' + BaseName
+ COMMON_FILE_NAME
+ '[] = {\n' )
394 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode
396 AllStr
= WriteLine(AllStr
, '// FRAMEWORK PACKAGE HEADER Length')
397 AllStr
= WriteLine(AllStr
, CreateArrayItem(DecToHexList(TotalLength
+ 2)) + '\n')
398 AllStr
= WriteLine(AllStr
, '// FRAMEWORK PACKAGE HEADER Type')
399 AllStr
= WriteLine(AllStr
, CreateArrayItem(DecToHexList(2, 4)) + '\n')
402 # Create whole array length in UEFI mode
404 AllStr
= WriteLine(AllStr
, '// STRGATHER_OUTPUT_HEADER')
405 AllStr
= WriteLine(AllStr
, CreateArrayItem(DecToHexList(TotalLength
)) + '\n')
410 AllStr
= Write(AllStr
, Str
)
414 ## Create end of .c file
416 # Create end of .c file
418 # @retval Str: A string of .h file end
420 def CreateCFileEnd():
421 Str
= Write('', '};')
426 # Create a complete .c file
428 # @param BaseName: The basename of strings
429 # @param UniObjectClass A UniObjectClass instance
430 # @param IsCompatibleMode Compatible Mode
432 # @retval CFile: A string of complete .c file
434 def CreateCFile(BaseName
, UniObjectClass
, IsCompatibleMode
):
436 #CFile = WriteLine(CFile, CreateCFileHeader())
437 CFile
= WriteLine(CFile
, CreateCFileContent(BaseName
, UniObjectClass
, IsCompatibleMode
))
438 CFile
= WriteLine(CFile
, CreateCFileEnd())
443 # Get a list for all files
445 # @param IncludeList: A list of all path to be searched
446 # @param SkipList: A list of all types of file could be skipped
448 # @retval FileList: A list of all files found
450 def GetFileList(SourceFileList
, IncludeList
, SkipList
):
451 if IncludeList
== None:
452 EdkLogger
.error("UnicodeStringGather", AUTOGEN_ERROR
, "Include path for unicode file is not defined")
458 for File
in SourceFileList
:
459 for Dir
in IncludeList
:
460 if not os
.path
.exists(Dir
):
462 File
= os
.path
.join(Dir
, File
.Path
)
466 if os
.path
.isfile(File
) != True:
469 # Ignore file listed in skip list
472 for Skip
in SkipList
:
473 if os
.path
.splitext(File
)[1].upper() == Skip
.upper():
474 EdkLogger
.verbose("Skipped %s for string token uses search" % File
)
479 FileList
.append(File
)
487 # Search whether all string defined in UniObjectClass are referenced
488 # All string used should be set to Referenced
490 # @param UniObjectClass: Input UniObjectClass
491 # @param FileList: Search path list
492 # @param IsCompatibleMode Compatible Mode
494 # @retval UniObjectClass: UniObjectClass after searched
496 def SearchString(UniObjectClass
, FileList
, IsCompatibleMode
):
498 return UniObjectClass
500 for File
in FileList
:
501 if os
.path
.isfile(File
):
502 Lines
= open(File
, 'r')
504 if not IsCompatibleMode
:
505 StringTokenList
= STRING_TOKEN
.findall(Line
)
507 StringTokenList
= COMPATIBLE_STRING_TOKEN
.findall(Line
)
508 for StrName
in StringTokenList
:
509 EdkLogger
.debug(EdkLogger
.DEBUG_5
, "Found string identifier: " + StrName
)
510 UniObjectClass
.SetStringReferenced(StrName
)
512 UniObjectClass
.ReToken()
514 return UniObjectClass
518 # This function is used for UEFI2.1 spec
521 def GetStringFiles(UniFilList
, SourceFileList
, IncludeList
, IncludePathList
, SkipList
, BaseName
, IsCompatibleMode
= False, ShellMode
= False, UniGenCFlag
= True, UniGenBinBuffer
= None):
525 if len(UniFilList
) > 0:
528 # support ISO 639-2 codes in .UNI files of EDK Shell
530 Uni
= UniFileClassObject(sorted (UniFilList
), True, IncludePathList
)
532 Uni
= UniFileClassObject(sorted (UniFilList
), IsCompatibleMode
, IncludePathList
)
534 EdkLogger
.error("UnicodeStringGather", AUTOGEN_ERROR
, 'No unicode files given')
536 FileList
= GetFileList(SourceFileList
, IncludeList
, SkipList
)
538 Uni
= SearchString(Uni
, sorted (FileList
), IsCompatibleMode
)
540 HFile
= CreateHFile(BaseName
, Uni
, IsCompatibleMode
, UniGenCFlag
)
542 if IsCompatibleMode
or UniGenCFlag
:
543 CFile
= CreateCFile(BaseName
, Uni
, IsCompatibleMode
)
545 CreateCFileContent(BaseName
, Uni
, IsCompatibleMode
, UniGenBinBuffer
)
552 def Write(Target
, Item
):
556 # Write an item with a break line
558 def WriteLine(Target
, Item
):
559 return Target
+ Item
+ '\n'
561 # This acts like the main() function for the script, unless it is 'import'ed into another
563 if __name__
== '__main__':
564 EdkLogger
.info('start')
567 r
'C:\\Edk\\Strings2.uni',
568 r
'C:\\Edk\\Strings.uni'
572 for Root
, Dirs
, Files
in os
.walk('C:\\Edk'):
574 SrcFileList
.append(File
)
580 SkipList
= ['.inf', '.uni']
581 BaseName
= 'DriverSample'
582 (h
, c
) = GetStringFiles(UniFileList
, SrcFileList
, IncludeList
, SkipList
, BaseName
, True)
583 hfile
= open('unistring.h', 'w')
584 cfile
= open('unistring.c', 'w')
588 EdkLogger
.info('end')