BaseTools: Replace StringIO.StringIO with io.BytesIO
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenPcdDb.py
1 ## @file
2 # Routines for generating Pcd Database
3 #
4 # Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13 from io import BytesIO
14 from Common.Misc import *
15 from Common.StringUtils import StringToArray
16 from struct import pack
17 from ValidCheckingInfoObject import VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER
18 from ValidCheckingInfoObject import VAR_CHECK_PCD_VARIABLE_TAB
19 from ValidCheckingInfoObject import GetValidationObject
20 from Common.VariableAttributes import VariableAttributes
21 import copy
22 from struct import unpack
23 from Common.DataType import *
24
25 DATABASE_VERSION = 7
26
27 gPcdDatabaseAutoGenC = TemplateString("""
28 //
29 // External PCD database debug information
30 //
31 #if 0
32 ${PHASE}_PCD_DATABASE_INIT g${PHASE}PcdDbInit = {
33 /* SkuIdTable */
34 { ${BEGIN}${SKUID_VALUE}, ${END} },
35 ${BEGIN} { ${INIT_VALUE_UINT64} }, /* ${INIT_CNAME_DECL_UINT64}_${INIT_GUID_DECL_UINT64}[${INIT_NUMSKUS_DECL_UINT64}] */
36 ${END}
37 ${BEGIN} ${VARDEF_VALUE_UINT64}, /* ${VARDEF_CNAME_UINT64}_${VARDEF_GUID_UINT64}_VariableDefault_${VARDEF_SKUID_UINT64} */
38 ${END}
39 ${BEGIN} { ${INIT_VALUE_UINT32} }, /* ${INIT_CNAME_DECL_UINT32}_${INIT_GUID_DECL_UINT32}[${INIT_NUMSKUS_DECL_UINT32}] */
40 ${END}
41 ${BEGIN} ${VARDEF_VALUE_UINT32}, /* ${VARDEF_CNAME_UINT32}_${VARDEF_GUID_UINT32}_VariableDefault_${VARDEF_SKUID_UINT32} */
42 ${END}
43 /* VPD */
44 ${BEGIN} { ${VPD_HEAD_VALUE} }, /* ${VPD_HEAD_CNAME_DECL}_${VPD_HEAD_GUID_DECL}[${VPD_HEAD_NUMSKUS_DECL}] */
45 ${END}
46 /* ExMapTable */
47 {
48 ${BEGIN} { ${EXMAPPING_TABLE_EXTOKEN}, ${EXMAPPING_TABLE_LOCAL_TOKEN}, ${EXMAPPING_TABLE_GUID_INDEX} },
49 ${END}
50 },
51 /* LocalTokenNumberTable */
52 {
53 ${BEGIN} offsetof(${PHASE}_PCD_DATABASE, ${TOKEN_INIT}.${TOKEN_CNAME}_${TOKEN_GUID}${VARDEF_HEADER}) | ${TOKEN_TYPE},
54 ${END}
55 },
56 /* GuidTable */
57 {
58 ${BEGIN} ${GUID_STRUCTURE},
59 ${END}
60 },
61 ${BEGIN} { ${STRING_HEAD_VALUE} }, /* ${STRING_HEAD_CNAME_DECL}_${STRING_HEAD_GUID_DECL}[${STRING_HEAD_NUMSKUS_DECL}] */
62 ${END}
63 ${BEGIN} /* ${VARIABLE_HEAD_CNAME_DECL}_${VARIABLE_HEAD_GUID_DECL}_Variable_Header[${VARIABLE_HEAD_NUMSKUS_DECL}] */
64 {
65 ${VARIABLE_HEAD_VALUE}
66 },
67 ${END}
68 /* SkuHead */
69 {
70 ${BEGIN} offsetof (${PHASE}_PCD_DATABASE, ${TOKEN_INIT}.${TOKEN_CNAME}_${TOKEN_GUID}${VARDEF_HEADER}) | ${TOKEN_TYPE}, /* */
71 offsetof (${PHASE}_PCD_DATABASE, ${TOKEN_INIT}.SkuHead) /* */
72 ${END}
73 },
74 /* StringTable */
75 ${BEGIN} ${STRING_TABLE_VALUE}, /* ${STRING_TABLE_CNAME}_${STRING_TABLE_GUID} */
76 ${END}
77 /* SizeTable */
78 {
79 ${BEGIN} ${SIZE_TABLE_MAXIMUM_LENGTH}, ${SIZE_TABLE_CURRENT_LENGTH}, /* ${SIZE_TABLE_CNAME}_${SIZE_TABLE_GUID} */
80 ${END}
81 },
82 ${BEGIN} { ${INIT_VALUE_UINT16} }, /* ${INIT_CNAME_DECL_UINT16}_${INIT_GUID_DECL_UINT16}[${INIT_NUMSKUS_DECL_UINT16}] */
83 ${END}
84 ${BEGIN} ${VARDEF_VALUE_UINT16}, /* ${VARDEF_CNAME_UINT16}_${VARDEF_GUID_UINT16}_VariableDefault_${VARDEF_SKUID_UINT16} */
85 ${END}
86 ${BEGIN} { ${INIT_VALUE_UINT8} }, /* ${INIT_CNAME_DECL_UINT8}_${INIT_GUID_DECL_UINT8}[${INIT_NUMSKUS_DECL_UINT8}] */
87 ${END}
88 ${BEGIN} ${VARDEF_VALUE_UINT8}, /* ${VARDEF_CNAME_UINT8}_${VARDEF_GUID_UINT8}_VariableDefault_${VARDEF_SKUID_UINT8} */
89 ${END}
90 ${BEGIN} { ${INIT_VALUE_BOOLEAN} }, /* ${INIT_CNAME_DECL_BOOLEAN}_${INIT_GUID_DECL_BOOLEAN}[${INIT_NUMSKUS_DECL_BOOLEAN}] */
91 ${END}
92 ${BEGIN} ${VARDEF_VALUE_BOOLEAN}, /* ${VARDEF_CNAME_BOOLEAN}_${VARDEF_GUID_BOOLEAN}_VariableDefault_${VARDEF_SKUID_BOOLEAN} */
93 ${END}
94 ${SYSTEM_SKU_ID_VALUE}
95 };
96 #endif
97 """)
98
99 ## Mapping between PCD driver type and EFI phase
100 gPcdPhaseMap = {
101 "PEI_PCD_DRIVER" : "PEI",
102 "DXE_PCD_DRIVER" : "DXE"
103 }
104
105 gPcdDatabaseAutoGenH = TemplateString("""
106 #define PCD_${PHASE}_SERVICE_DRIVER_VERSION ${SERVICE_DRIVER_VERSION}
107
108 //
109 // External PCD database debug information
110 //
111 #if 0
112 #define ${PHASE}_GUID_TABLE_SIZE ${GUID_TABLE_SIZE}
113 #define ${PHASE}_STRING_TABLE_SIZE ${STRING_TABLE_SIZE}
114 #define ${PHASE}_SKUID_TABLE_SIZE ${SKUID_TABLE_SIZE}
115 #define ${PHASE}_LOCAL_TOKEN_NUMBER_TABLE_SIZE ${LOCAL_TOKEN_NUMBER_TABLE_SIZE}
116 #define ${PHASE}_LOCAL_TOKEN_NUMBER ${LOCAL_TOKEN_NUMBER}
117 #define ${PHASE}_EXMAPPING_TABLE_SIZE ${EXMAPPING_TABLE_SIZE}
118 #define ${PHASE}_EX_TOKEN_NUMBER ${EX_TOKEN_NUMBER}
119 #define ${PHASE}_SIZE_TABLE_SIZE ${SIZE_TABLE_SIZE}
120 #define ${PHASE}_GUID_TABLE_EMPTY ${GUID_TABLE_EMPTY}
121 #define ${PHASE}_STRING_TABLE_EMPTY ${STRING_TABLE_EMPTY}
122 #define ${PHASE}_SKUID_TABLE_EMPTY ${SKUID_TABLE_EMPTY}
123 #define ${PHASE}_DATABASE_EMPTY ${DATABASE_EMPTY}
124 #define ${PHASE}_EXMAP_TABLE_EMPTY ${EXMAP_TABLE_EMPTY}
125
126 typedef struct {
127 UINT64 SkuIdTable[${PHASE}_SKUID_TABLE_SIZE];
128 ${BEGIN} UINT64 ${INIT_CNAME_DECL_UINT64}_${INIT_GUID_DECL_UINT64}[${INIT_NUMSKUS_DECL_UINT64}];
129 ${END}
130 ${BEGIN} UINT64 ${VARDEF_CNAME_UINT64}_${VARDEF_GUID_UINT64}_VariableDefault_${VARDEF_SKUID_UINT64};
131 ${END}
132 ${BEGIN} UINT32 ${INIT_CNAME_DECL_UINT32}_${INIT_GUID_DECL_UINT32}[${INIT_NUMSKUS_DECL_UINT32}];
133 ${END}
134 ${BEGIN} UINT32 ${VARDEF_CNAME_UINT32}_${VARDEF_GUID_UINT32}_VariableDefault_${VARDEF_SKUID_UINT32};
135 ${END}
136 ${BEGIN} VPD_HEAD ${VPD_HEAD_CNAME_DECL}_${VPD_HEAD_GUID_DECL}[${VPD_HEAD_NUMSKUS_DECL}];
137 ${END}
138 DYNAMICEX_MAPPING ExMapTable[${PHASE}_EXMAPPING_TABLE_SIZE];
139 UINT32 LocalTokenNumberTable[${PHASE}_LOCAL_TOKEN_NUMBER_TABLE_SIZE];
140 GUID GuidTable[${PHASE}_GUID_TABLE_SIZE];
141 ${BEGIN} STRING_HEAD ${STRING_HEAD_CNAME_DECL}_${STRING_HEAD_GUID_DECL}[${STRING_HEAD_NUMSKUS_DECL}];
142 ${END}
143 ${BEGIN} VARIABLE_HEAD ${VARIABLE_HEAD_CNAME_DECL}_${VARIABLE_HEAD_GUID_DECL}_Variable_Header[${VARIABLE_HEAD_NUMSKUS_DECL}];
144 ${BEGIN} UINT8 StringTable${STRING_TABLE_INDEX}[${STRING_TABLE_LENGTH}]; /* ${STRING_TABLE_CNAME}_${STRING_TABLE_GUID} */
145 ${END}
146 SIZE_INFO SizeTable[${PHASE}_SIZE_TABLE_SIZE];
147 ${BEGIN} UINT16 ${INIT_CNAME_DECL_UINT16}_${INIT_GUID_DECL_UINT16}[${INIT_NUMSKUS_DECL_UINT16}];
148 ${END}
149 ${BEGIN} UINT16 ${VARDEF_CNAME_UINT16}_${VARDEF_GUID_UINT16}_VariableDefault_${VARDEF_SKUID_UINT16};
150 ${END}
151 ${BEGIN} UINT8 ${INIT_CNAME_DECL_UINT8}_${INIT_GUID_DECL_UINT8}[${INIT_NUMSKUS_DECL_UINT8}];
152 ${END}
153 ${BEGIN} UINT8 ${VARDEF_CNAME_UINT8}_${VARDEF_GUID_UINT8}_VariableDefault_${VARDEF_SKUID_UINT8};
154 ${END}
155 ${BEGIN} BOOLEAN ${INIT_CNAME_DECL_BOOLEAN}_${INIT_GUID_DECL_BOOLEAN}[${INIT_NUMSKUS_DECL_BOOLEAN}];
156 ${END}
157 ${BEGIN} BOOLEAN ${VARDEF_CNAME_BOOLEAN}_${VARDEF_GUID_BOOLEAN}_VariableDefault_${VARDEF_SKUID_BOOLEAN};
158 ${END}
159 ${SYSTEM_SKU_ID}
160 } ${PHASE}_PCD_DATABASE_INIT;
161
162 typedef struct {
163 ${PCD_DATABASE_UNINIT_EMPTY}
164 ${BEGIN} UINT64 ${UNINIT_CNAME_DECL_UINT64}_${UNINIT_GUID_DECL_UINT64}[${UNINIT_NUMSKUS_DECL_UINT64}];
165 ${END}
166 ${BEGIN} UINT32 ${UNINIT_CNAME_DECL_UINT32}_${UNINIT_GUID_DECL_UINT32}[${UNINIT_NUMSKUS_DECL_UINT32}];
167 ${END}
168 ${BEGIN} UINT16 ${UNINIT_CNAME_DECL_UINT16}_${UNINIT_GUID_DECL_UINT16}[${UNINIT_NUMSKUS_DECL_UINT16}];
169 ${END}
170 ${BEGIN} UINT8 ${UNINIT_CNAME_DECL_UINT8}_${UNINIT_GUID_DECL_UINT8}[${UNINIT_NUMSKUS_DECL_UINT8}];
171 ${END}
172 ${BEGIN} BOOLEAN ${UNINIT_CNAME_DECL_BOOLEAN}_${UNINIT_GUID_DECL_BOOLEAN}[${UNINIT_NUMSKUS_DECL_BOOLEAN}];
173 ${END}
174 } ${PHASE}_PCD_DATABASE_UNINIT;
175
176 typedef struct {
177 //GUID Signature; // PcdDataBaseGuid
178 //UINT32 BuildVersion;
179 //UINT32 Length;
180 //SKU_ID SystemSkuId; // Current SkuId value.
181 //UINT32 LengthForAllSkus; // Length of all SKU PCD DB
182 //UINT32 UninitDataBaseSize;// Total size for PCD those default value with 0.
183 //TABLE_OFFSET LocalTokenNumberTableOffset;
184 //TABLE_OFFSET ExMapTableOffset;
185 //TABLE_OFFSET GuidTableOffset;
186 //TABLE_OFFSET StringTableOffset;
187 //TABLE_OFFSET SizeTableOffset;
188 //TABLE_OFFSET SkuIdTableOffset;
189 //TABLE_OFFSET PcdNameTableOffset;
190 //UINT16 LocalTokenCount; // LOCAL_TOKEN_NUMBER for all
191 //UINT16 ExTokenCount; // EX_TOKEN_NUMBER for DynamicEx
192 //UINT16 GuidTableCount; // The Number of Guid in GuidTable
193 //UINT8 Pad[6];
194 ${PHASE}_PCD_DATABASE_INIT Init;
195 ${PHASE}_PCD_DATABASE_UNINIT Uninit;
196 } ${PHASE}_PCD_DATABASE;
197
198 #define ${PHASE}_NEX_TOKEN_NUMBER (${PHASE}_LOCAL_TOKEN_NUMBER - ${PHASE}_EX_TOKEN_NUMBER)
199 #endif
200 """)
201
202
203 gEmptyPcdDatabaseAutoGenC = TemplateString("""
204 //
205 // External PCD database debug information
206 //
207 #if 0
208 ${PHASE}_PCD_DATABASE_INIT g${PHASE}PcdDbInit = {
209 /* SkuIdTable */
210 { 0 },
211 /* ExMapTable */
212 {
213 {0, 0, 0}
214 },
215 /* LocalTokenNumberTable */
216 {
217 0
218 },
219 /* GuidTable */
220 {
221 {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
222 },
223 /* StringTable */
224 { 0 },
225 /* SkuHead */
226 {
227 0, 0
228 },
229 /* SizeTable */
230 {
231 0, 0
232 },
233 ${SYSTEM_SKU_ID_VALUE}
234 };
235 #endif
236 """)
237
238 ## DbItemList
239 #
240 # The class holds the Pcd database items. ItemSize if not zero should match the item datum type in the C structure.
241 # When the structure is changed, remember to check the ItemSize and the related PackStr in PackData()
242 # RawDataList is the RawData that may need some kind of calculation or transformation,
243 # the DataList corresponds to the data that need to be written to database. If DataList is not present, then RawDataList
244 # will be written to the database.
245 #
246 class DbItemList:
247 def __init__(self, ItemSize, DataList=None, RawDataList=None):
248 self.ItemSize = ItemSize
249 self.DataList = DataList if DataList else []
250 self.RawDataList = RawDataList if RawDataList else []
251 self.ListSize = 0
252
253 def GetInterOffset(self, Index):
254 Offset = 0
255 if self.ItemSize == 0:
256 #
257 # Variable length, need to calculate one by one
258 #
259 assert(Index < len(self.RawDataList))
260 for ItemIndex in xrange(Index):
261 Offset += len(self.RawDataList[ItemIndex])
262 else:
263 Offset = self.ItemSize * Index
264
265 return Offset
266
267 def GetListSize(self):
268 if self.ListSize:
269 return self.ListSize
270 if len(self.RawDataList) == 0:
271 self.ListSize = 0
272 return self.ListSize
273 if self.ItemSize == 0:
274 self.ListSize = self.GetInterOffset(len(self.RawDataList) - 1) + len(self.RawDataList[len(self.RawDataList)-1])
275 else:
276 self.ListSize = self.ItemSize * len(self.RawDataList)
277 return self.ListSize
278
279 def PackData(self):
280 ## PackGuid
281 #
282 # Pack the GUID value in C structure format into data array
283 #
284 # @param GuidStructureValue: The GUID value in C structure format
285 #
286 # @retval Buffer: a data array contains the Guid
287 #
288 def PackGuid(GuidStructureValue):
289 GuidString = GuidStructureStringToGuidString(GuidStructureValue)
290 return PackGUID(GuidString.split('-'))
291
292 PackStr = PACK_CODE_BY_SIZE[self.ItemSize]
293
294 Buffer = ''
295 for Datas in self.RawDataList:
296 if type(Datas) in (list, tuple):
297 for Data in Datas:
298 if PackStr:
299 Buffer += pack(PackStr, GetIntegerValue(Data))
300 else:
301 Buffer += PackGuid(Data)
302 else:
303 if PackStr:
304 Buffer += pack(PackStr, GetIntegerValue(Datas))
305 else:
306 Buffer += PackGuid(Datas)
307
308 return Buffer
309
310 ## DbExMapTblItemList
311 #
312 # The class holds the ExMap table
313 #
314 class DbExMapTblItemList (DbItemList):
315 def __init__(self, ItemSize, DataList=None, RawDataList=None):
316 DbItemList.__init__(self, ItemSize, DataList, RawDataList)
317
318 def PackData(self):
319 Buffer = ''
320 PackStr = "=LHH"
321 for Datas in self.RawDataList:
322 Buffer += pack(PackStr,
323 GetIntegerValue(Datas[0]),
324 GetIntegerValue(Datas[1]),
325 GetIntegerValue(Datas[2]))
326 return Buffer
327
328 ## DbComItemList
329 #
330 # The DbComItemList is a special kind of DbItemList in case that the size of the List can not be computed by the
331 # ItemSize multiply the ItemCount.
332 #
333 class DbComItemList (DbItemList):
334 def __init__(self, ItemSize, DataList=None, RawDataList=None):
335 DbItemList.__init__(self, ItemSize, DataList, RawDataList)
336
337 def GetInterOffset(self, Index):
338 Offset = 0
339 if self.ItemSize == 0:
340 #
341 # Variable length, need to calculte one by one
342 # The only variable table is stringtable, it is not Composite item, should not reach here
343 #
344 assert(False)
345 else:
346 assert(Index < len(self.RawDataList))
347 for ItemIndex in xrange(Index):
348 Offset += len(self.RawDataList[ItemIndex]) * self.ItemSize
349
350 return Offset
351
352 def GetListSize(self):
353 if self.ListSize:
354 return self.ListSize
355 if self.ItemSize == 0:
356 assert(False)
357 else:
358 if len(self.RawDataList) == 0:
359 self.ListSize = 0
360 else:
361 self.ListSize = self.GetInterOffset(len(self.RawDataList) - 1) + len(self.RawDataList[len(self.RawDataList)-1]) * self.ItemSize
362
363 return self.ListSize
364
365 def PackData(self):
366 PackStr = PACK_CODE_BY_SIZE[self.ItemSize]
367
368 Buffer = ''
369 for DataList in self.RawDataList:
370 for Data in DataList:
371 if type(Data) in (list, tuple):
372 for SingleData in Data:
373 Buffer += pack(PackStr, GetIntegerValue(SingleData))
374 else:
375 Buffer += pack(PackStr, GetIntegerValue(Data))
376
377 return Buffer
378
379 ## DbVariableTableItemList
380 #
381 # The class holds the Variable header value table
382 #
383 class DbVariableTableItemList (DbComItemList):
384 def __init__(self, ItemSize, DataList=None, RawDataList=None):
385 DbComItemList.__init__(self, ItemSize, DataList, RawDataList)
386
387 def PackData(self):
388 PackStr = "=LLHHLHH"
389 Buffer = ''
390 for DataList in self.RawDataList:
391 for Data in DataList:
392 Buffer += pack(PackStr,
393 GetIntegerValue(Data[0]),
394 GetIntegerValue(Data[1]),
395 GetIntegerValue(Data[2]),
396 GetIntegerValue(Data[3]),
397 GetIntegerValue(Data[4]),
398 GetIntegerValue(Data[5]),
399 GetIntegerValue(0))
400 return Buffer
401
402 class DbStringHeadTableItemList(DbItemList):
403 def __init__(self,ItemSize,DataList=None,RawDataList=None):
404 DbItemList.__init__(self, ItemSize, DataList, RawDataList)
405
406 def GetInterOffset(self, Index):
407 Offset = 0
408 if self.ItemSize == 0:
409 #
410 # Variable length, need to calculate one by one
411 #
412 assert(Index < len(self.RawDataList))
413 for ItemIndex in xrange(Index):
414 Offset += len(self.RawDataList[ItemIndex])
415 else:
416 for innerIndex in range(Index):
417 if type(self.RawDataList[innerIndex]) in (list, tuple):
418 Offset += len(self.RawDataList[innerIndex]) * self.ItemSize
419 else:
420 Offset += self.ItemSize
421
422 return Offset
423
424 def GetListSize(self):
425 if self.ListSize:
426 return self.ListSize
427 if len(self.RawDataList) == 0:
428 self.ListSize = 0
429 return self.ListSize
430 if self.ItemSize == 0:
431 self.ListSize = self.GetInterOffset(len(self.RawDataList) - 1) + len(self.RawDataList[len(self.RawDataList)-1])
432 else:
433 for Datas in self.RawDataList:
434 if type(Datas) in (list, tuple):
435 self.ListSize += len(Datas) * self.ItemSize
436 else:
437 self.ListSize += self.ItemSize
438 return self.ListSize
439
440 ## DbSkuHeadTableItemList
441 #
442 # The class holds the Sku header value table
443 #
444 class DbSkuHeadTableItemList (DbItemList):
445 def __init__(self, ItemSize, DataList=None, RawDataList=None):
446 DbItemList.__init__(self, ItemSize, DataList, RawDataList)
447
448 def PackData(self):
449 PackStr = "=LL"
450 Buffer = ''
451 for Data in self.RawDataList:
452 Buffer += pack(PackStr,
453 GetIntegerValue(Data[0]),
454 GetIntegerValue(Data[1]))
455 return Buffer
456
457 ## DbSizeTableItemList
458 #
459 # The class holds the size table
460 #
461 class DbSizeTableItemList (DbItemList):
462 def __init__(self, ItemSize, DataList=None, RawDataList=None):
463 DbItemList.__init__(self, ItemSize, DataList, RawDataList)
464
465 def GetListSize(self):
466 length = 0
467 for Data in self.RawDataList:
468 length += (1 + len(Data[1]))
469 return length * self.ItemSize
470 def PackData(self):
471 PackStr = "=H"
472 Buffer = ''
473 for Data in self.RawDataList:
474 Buffer += pack(PackStr,
475 GetIntegerValue(Data[0]))
476 for subData in Data[1]:
477 Buffer += pack(PackStr,
478 GetIntegerValue(subData))
479 return Buffer
480
481 ## DbStringItemList
482 #
483 # The class holds the string table
484 #
485 class DbStringItemList (DbComItemList):
486 def __init__(self, ItemSize, DataList=None, RawDataList=None, LenList=None):
487 if DataList is None:
488 DataList = []
489 if RawDataList is None:
490 RawDataList = []
491 if LenList is None:
492 LenList = []
493
494 assert(len(RawDataList) == len(LenList))
495 DataList = []
496 # adjust DataList according to the LenList
497 for Index in xrange(len(RawDataList)):
498 Len = LenList[Index]
499 RawDatas = RawDataList[Index]
500 assert(Len >= len(RawDatas))
501 ActualDatas = []
502 for i in xrange(len(RawDatas)):
503 ActualDatas.append(RawDatas[i])
504 for i in xrange(len(RawDatas), Len):
505 ActualDatas.append(0)
506 DataList.append(ActualDatas)
507 self.LenList = LenList
508 DbComItemList.__init__(self, ItemSize, DataList, RawDataList)
509 def GetInterOffset(self, Index):
510 Offset = 0
511
512 assert(Index < len(self.LenList))
513 for ItemIndex in xrange(Index):
514 Offset += self.LenList[ItemIndex]
515
516 return Offset
517
518 def GetListSize(self):
519 if self.ListSize:
520 return self.ListSize
521
522 if len(self.LenList) == 0:
523 self.ListSize = 0
524 else:
525 self.ListSize = self.GetInterOffset(len(self.LenList) - 1) + self.LenList[len(self.LenList)-1]
526
527 return self.ListSize
528
529 def PackData(self):
530 self.RawDataList = self.DataList
531 return DbComItemList.PackData(self)
532
533
534
535 ## Find the index in two list where the item matches the key separately
536 #
537 # @param Key1 The key used to search the List1
538 # @param List1 The list that Key1 will be searched
539 # @param Key2 The key used to search the List2
540 # @param List2 The list that Key2 will be searched
541 #
542 # @retval Index The position inside the list where list1[Index] == Key1 and list2[Index] == Key2
543 #
544 def GetMatchedIndex(Key1, List1, Key2, List2):
545 StartPos = 0
546 while StartPos < len(List1):
547 Index = List1.index(Key1, StartPos)
548 if List2[Index] == Key2:
549 return Index
550 else:
551 StartPos = Index + 1
552
553 return -1
554
555
556 ## convert StringArray like {0x36, 0x00, 0x34, 0x00, 0x21, 0x00, 0x36, 0x00, 0x34, 0x00, 0x00, 0x00}
557 # to List like [0x36, 0x00, 0x34, 0x00, 0x21, 0x00, 0x36, 0x00, 0x34, 0x00, 0x00, 0x00]
558 #
559 # @param StringArray A string array like {0x36, 0x00, 0x34, 0x00, 0x21, 0x00, 0x36, 0x00, 0x34, 0x00, 0x00, 0x00}
560 #
561 # @retval A list object of integer items
562 #
563 def StringArrayToList(StringArray):
564 StringArray = StringArray[1:-1]
565 StringArray = '[' + StringArray + ']'
566 return eval(StringArray)
567
568
569 ## Convert TokenType String like "PCD_DATUM_TYPE_UINT32 | PCD_TYPE_HII" to TokenType value
570 #
571 # @param TokenType A TokenType string like "PCD_DATUM_TYPE_UINT32 | PCD_TYPE_HII"
572 #
573 # @retval A integer representation of the TokenType
574 #
575 def GetTokenTypeValue(TokenType):
576 TokenTypeDict = {
577 "PCD_TYPE_SHIFT": 28,
578 "PCD_TYPE_DATA": (0x0 << 28),
579 "PCD_TYPE_HII": (0x8 << 28),
580 "PCD_TYPE_VPD": (0x4 << 28),
581 # "PCD_TYPE_SKU_ENABLED":(0x2 << 28),
582 "PCD_TYPE_STRING": (0x1 << 28),
583
584 "PCD_DATUM_TYPE_SHIFT": 24,
585 "PCD_DATUM_TYPE_POINTER": (0x0 << 24),
586 "PCD_DATUM_TYPE_UINT8": (0x1 << 24),
587 "PCD_DATUM_TYPE_UINT16": (0x2 << 24),
588 "PCD_DATUM_TYPE_UINT32": (0x4 << 24),
589 "PCD_DATUM_TYPE_UINT64": (0x8 << 24),
590
591 "PCD_DATUM_TYPE_SHIFT2": 20,
592 "PCD_DATUM_TYPE_UINT8_BOOLEAN": (0x1 << 20 | 0x1 << 24),
593 }
594 return eval(TokenType, TokenTypeDict)
595
596 ## construct the external Pcd database using data from Dict
597 #
598 # @param Dict A dictionary contains Pcd related tables
599 #
600 # @retval Buffer A byte stream of the Pcd database
601 #
602 def BuildExDataBase(Dict):
603 # init Db items
604 InitValueUint64 = Dict['INIT_DB_VALUE_UINT64']
605 DbInitValueUint64 = DbComItemList(8, RawDataList = InitValueUint64)
606 VardefValueUint64 = Dict['VARDEF_DB_VALUE_UINT64']
607 DbVardefValueUint64 = DbItemList(8, RawDataList = VardefValueUint64)
608 InitValueUint32 = Dict['INIT_DB_VALUE_UINT32']
609 DbInitValueUint32 = DbComItemList(4, RawDataList = InitValueUint32)
610 VardefValueUint32 = Dict['VARDEF_DB_VALUE_UINT32']
611 DbVardefValueUint32 = DbItemList(4, RawDataList = VardefValueUint32)
612 VpdHeadValue = Dict['VPD_DB_VALUE']
613 DbVpdHeadValue = DbComItemList(4, RawDataList = VpdHeadValue)
614 ExMapTable = zip(Dict['EXMAPPING_TABLE_EXTOKEN'], Dict['EXMAPPING_TABLE_LOCAL_TOKEN'], Dict['EXMAPPING_TABLE_GUID_INDEX'])
615 DbExMapTable = DbExMapTblItemList(8, RawDataList = ExMapTable)
616 LocalTokenNumberTable = Dict['LOCAL_TOKEN_NUMBER_DB_VALUE']
617 DbLocalTokenNumberTable = DbItemList(4, RawDataList = LocalTokenNumberTable)
618 GuidTable = Dict['GUID_STRUCTURE']
619 DbGuidTable = DbItemList(16, RawDataList = GuidTable)
620 StringHeadValue = Dict['STRING_DB_VALUE']
621 # DbItemList to DbStringHeadTableItemList
622 DbStringHeadValue = DbStringHeadTableItemList(4, RawDataList = StringHeadValue)
623 VariableTable = Dict['VARIABLE_DB_VALUE']
624 DbVariableTable = DbVariableTableItemList(20, RawDataList = VariableTable)
625 NumberOfSkuEnabledPcd = GetIntegerValue(Dict['SKU_HEAD_SIZE'])
626
627 Dict['STRING_TABLE_DB_VALUE'] = [StringArrayToList(x) for x in Dict['STRING_TABLE_VALUE']]
628
629 StringTableValue = Dict['STRING_TABLE_DB_VALUE']
630 # when calcute the offset, should use StringTableLen instead of StringTableValue, as string maxium len may be different with actual len
631 StringTableLen = Dict['STRING_TABLE_LENGTH']
632 DbStringTableLen = DbStringItemList(0, RawDataList = StringTableValue, LenList = StringTableLen)
633
634
635 PcdTokenTable = Dict['PCD_TOKENSPACE']
636 PcdTokenLen = Dict['PCD_TOKENSPACE_LENGTH']
637 PcdTokenTableValue = [StringArrayToList(x) for x in Dict['PCD_TOKENSPACE']]
638 DbPcdTokenTable = DbStringItemList(0, RawDataList = PcdTokenTableValue, LenList = PcdTokenLen)
639
640 PcdCNameTable = Dict['PCD_CNAME']
641 PcdCNameLen = Dict['PCD_CNAME_LENGTH']
642 PcdCNameTableValue = [StringArrayToList(x) for x in Dict['PCD_CNAME']]
643 DbPcdCNameTable = DbStringItemList(0, RawDataList = PcdCNameTableValue, LenList = PcdCNameLen)
644
645 PcdNameOffsetTable = Dict['PCD_NAME_OFFSET']
646 DbPcdNameOffsetTable = DbItemList(4, RawDataList = PcdNameOffsetTable)
647
648 SizeTableValue = zip(Dict['SIZE_TABLE_MAXIMUM_LENGTH'], Dict['SIZE_TABLE_CURRENT_LENGTH'])
649 DbSizeTableValue = DbSizeTableItemList(2, RawDataList = SizeTableValue)
650 InitValueUint16 = Dict['INIT_DB_VALUE_UINT16']
651 DbInitValueUint16 = DbComItemList(2, RawDataList = InitValueUint16)
652 VardefValueUint16 = Dict['VARDEF_DB_VALUE_UINT16']
653 DbVardefValueUint16 = DbItemList(2, RawDataList = VardefValueUint16)
654 InitValueUint8 = Dict['INIT_DB_VALUE_UINT8']
655 DbInitValueUint8 = DbComItemList(1, RawDataList = InitValueUint8)
656 VardefValueUint8 = Dict['VARDEF_DB_VALUE_UINT8']
657 DbVardefValueUint8 = DbItemList(1, RawDataList = VardefValueUint8)
658 InitValueBoolean = Dict['INIT_DB_VALUE_BOOLEAN']
659 DbInitValueBoolean = DbComItemList(1, RawDataList = InitValueBoolean)
660 VardefValueBoolean = Dict['VARDEF_DB_VALUE_BOOLEAN']
661 DbVardefValueBoolean = DbItemList(1, RawDataList = VardefValueBoolean)
662 SkuidValue = Dict['SKUID_VALUE']
663 DbSkuidValue = DbItemList(8, RawDataList = SkuidValue)
664
665
666
667 # Unit Db Items
668 UnInitValueUint64 = Dict['UNINIT_GUID_DECL_UINT64']
669 DbUnInitValueUint64 = DbItemList(8, RawDataList = UnInitValueUint64)
670 UnInitValueUint32 = Dict['UNINIT_GUID_DECL_UINT32']
671 DbUnInitValueUint32 = DbItemList(4, RawDataList = UnInitValueUint32)
672 UnInitValueUint16 = Dict['UNINIT_GUID_DECL_UINT16']
673 DbUnInitValueUint16 = DbItemList(2, RawDataList = UnInitValueUint16)
674 UnInitValueUint8 = Dict['UNINIT_GUID_DECL_UINT8']
675 DbUnInitValueUint8 = DbItemList(1, RawDataList = UnInitValueUint8)
676 UnInitValueBoolean = Dict['UNINIT_GUID_DECL_BOOLEAN']
677 DbUnInitValueBoolean = DbItemList(1, RawDataList = UnInitValueBoolean)
678 PcdTokenNumberMap = Dict['PCD_ORDER_TOKEN_NUMBER_MAP']
679
680 DbNameTotle = ["SkuidValue", "InitValueUint64", "VardefValueUint64", "InitValueUint32", "VardefValueUint32", "VpdHeadValue", "ExMapTable",
681 "LocalTokenNumberTable", "GuidTable", "StringHeadValue", "PcdNameOffsetTable", "VariableTable", "StringTableLen", "PcdTokenTable", "PcdCNameTable",
682 "SizeTableValue", "InitValueUint16", "VardefValueUint16", "InitValueUint8", "VardefValueUint8", "InitValueBoolean",
683 "VardefValueBoolean", "UnInitValueUint64", "UnInitValueUint32", "UnInitValueUint16", "UnInitValueUint8", "UnInitValueBoolean"]
684
685 DbTotal = [SkuidValue, InitValueUint64, VardefValueUint64, InitValueUint32, VardefValueUint32, VpdHeadValue, ExMapTable,
686 LocalTokenNumberTable, GuidTable, StringHeadValue, PcdNameOffsetTable, VariableTable, StringTableLen, PcdTokenTable, PcdCNameTable,
687 SizeTableValue, InitValueUint16, VardefValueUint16, InitValueUint8, VardefValueUint8, InitValueBoolean,
688 VardefValueBoolean, UnInitValueUint64, UnInitValueUint32, UnInitValueUint16, UnInitValueUint8, UnInitValueBoolean]
689 DbItemTotal = [DbSkuidValue, DbInitValueUint64, DbVardefValueUint64, DbInitValueUint32, DbVardefValueUint32, DbVpdHeadValue, DbExMapTable,
690 DbLocalTokenNumberTable, DbGuidTable, DbStringHeadValue, DbPcdNameOffsetTable, DbVariableTable, DbStringTableLen, DbPcdTokenTable, DbPcdCNameTable,
691 DbSizeTableValue, DbInitValueUint16, DbVardefValueUint16, DbInitValueUint8, DbVardefValueUint8, DbInitValueBoolean,
692 DbVardefValueBoolean, DbUnInitValueUint64, DbUnInitValueUint32, DbUnInitValueUint16, DbUnInitValueUint8, DbUnInitValueBoolean]
693
694 # VardefValueBoolean is the last table in the init table items
695 InitTableNum = DbNameTotle.index("VardefValueBoolean") + 1
696 # The FixedHeader length of the PCD_DATABASE_INIT, from Signature to Pad
697 FixedHeaderLen = 80
698
699 # Get offset of SkuId table in the database
700 SkuIdTableOffset = FixedHeaderLen
701 for DbIndex in xrange(len(DbTotal)):
702 if DbTotal[DbIndex] is SkuidValue:
703 break
704 SkuIdTableOffset += DbItemTotal[DbIndex].GetListSize()
705
706
707 # Get offset of SkuValue table in the database
708
709 # Fix up the LocalTokenNumberTable, SkuHeader table
710 for (LocalTokenNumberTableIndex, (Offset, Table)) in enumerate(LocalTokenNumberTable):
711 DbIndex = 0
712 DbOffset = FixedHeaderLen
713 for DbIndex in xrange(len(DbTotal)):
714 if DbTotal[DbIndex] is Table:
715 DbOffset += DbItemTotal[DbIndex].GetInterOffset(Offset)
716 break
717 DbOffset += DbItemTotal[DbIndex].GetListSize()
718 if DbIndex + 1 == InitTableNum:
719 if DbOffset % 8:
720 DbOffset += (8 - DbOffset % 8)
721 else:
722 assert(False)
723
724 TokenTypeValue = Dict['TOKEN_TYPE'][LocalTokenNumberTableIndex]
725 TokenTypeValue = GetTokenTypeValue(TokenTypeValue)
726 LocalTokenNumberTable[LocalTokenNumberTableIndex] = DbOffset|int(TokenTypeValue)
727 # if PCD_TYPE_SKU_ENABLED, then we need to fix up the SkuTable
728
729
730
731
732 # resolve variable table offset
733 for VariableEntries in VariableTable:
734 skuindex = 0
735 for VariableEntryPerSku in VariableEntries:
736 (VariableHeadGuidIndex, VariableHeadStringIndex, SKUVariableOffset, VariableOffset, VariableRefTable, VariableAttribute) = VariableEntryPerSku[:]
737 DbIndex = 0
738 DbOffset = FixedHeaderLen
739 for DbIndex in xrange(len(DbTotal)):
740 if DbTotal[DbIndex] is VariableRefTable:
741 DbOffset += DbItemTotal[DbIndex].GetInterOffset(VariableOffset)
742 break
743 DbOffset += DbItemTotal[DbIndex].GetListSize()
744 if DbIndex + 1 == InitTableNum:
745 if DbOffset % 8:
746 DbOffset += (8 - DbOffset % 8)
747 else:
748 assert(False)
749 if isinstance(VariableRefTable[0], list):
750 DbOffset += skuindex * 4
751 skuindex += 1
752 if DbIndex >= InitTableNum:
753 assert(False)
754 VarAttr, VarProp = VariableAttributes.GetVarAttributes(VariableAttribute)
755 VariableEntryPerSku[:] = (VariableHeadStringIndex, DbOffset, VariableHeadGuidIndex, SKUVariableOffset, VarAttr, VarProp)
756
757 # calculate various table offset now
758 DbTotalLength = FixedHeaderLen
759 for DbIndex in xrange(len(DbItemTotal)):
760 if DbItemTotal[DbIndex] is DbLocalTokenNumberTable:
761 LocalTokenNumberTableOffset = DbTotalLength
762 elif DbItemTotal[DbIndex] is DbExMapTable:
763 ExMapTableOffset = DbTotalLength
764 elif DbItemTotal[DbIndex] is DbGuidTable:
765 GuidTableOffset = DbTotalLength
766 elif DbItemTotal[DbIndex] is DbStringTableLen:
767 StringTableOffset = DbTotalLength
768 elif DbItemTotal[DbIndex] is DbSizeTableValue:
769 SizeTableOffset = DbTotalLength
770 elif DbItemTotal[DbIndex] is DbSkuidValue:
771 SkuIdTableOffset = DbTotalLength
772 elif DbItemTotal[DbIndex] is DbPcdNameOffsetTable:
773 DbPcdNameOffset = DbTotalLength
774
775
776 DbTotalLength += DbItemTotal[DbIndex].GetListSize()
777 if not Dict['PCD_INFO_FLAG']:
778 DbPcdNameOffset = 0
779 LocalTokenCount = GetIntegerValue(Dict['LOCAL_TOKEN_NUMBER'])
780 ExTokenCount = GetIntegerValue(Dict['EX_TOKEN_NUMBER'])
781 GuidTableCount = GetIntegerValue(Dict['GUID_TABLE_SIZE'])
782 SystemSkuId = GetIntegerValue(Dict['SYSTEM_SKU_ID_VALUE'])
783 Pad = 0xDA
784
785 UninitDataBaseSize = 0
786 for Item in (DbUnInitValueUint64, DbUnInitValueUint32, DbUnInitValueUint16, DbUnInitValueUint8, DbUnInitValueBoolean):
787 UninitDataBaseSize += Item.GetListSize()
788
789 if (DbTotalLength - UninitDataBaseSize) % 8:
790 DbTotalLength += (8 - (DbTotalLength - UninitDataBaseSize) % 8)
791 # Construct the database buffer
792 Guid = "{0x3c7d193c, 0x682c, 0x4c14, 0xa6, 0x8f, 0x55, 0x2d, 0xea, 0x4f, 0x43, 0x7e}"
793 Guid = StringArrayToList(Guid)
794 Buffer = PackByteFormatGUID(Guid)
795
796 b = pack("=L", DATABASE_VERSION)
797 Buffer += b
798
799 b = pack('=L', DbTotalLength - UninitDataBaseSize)
800
801 Buffer += b
802 b = pack('=Q', SystemSkuId)
803
804 Buffer += b
805 b = pack('=L', 0)
806
807 Buffer += b
808 b = pack('=L', UninitDataBaseSize)
809
810 Buffer += b
811 b = pack('=L', LocalTokenNumberTableOffset)
812
813 Buffer += b
814 b = pack('=L', ExMapTableOffset)
815
816 Buffer += b
817 b = pack('=L', GuidTableOffset)
818
819 Buffer += b
820 b = pack('=L', StringTableOffset)
821
822 Buffer += b
823 b = pack('=L', SizeTableOffset)
824
825 Buffer += b
826 b = pack('=L', SkuIdTableOffset)
827
828 Buffer += b
829 b = pack('=L', DbPcdNameOffset)
830
831 Buffer += b
832 b = pack('=H', LocalTokenCount)
833
834 Buffer += b
835 b = pack('=H', ExTokenCount)
836
837 Buffer += b
838 b = pack('=H', GuidTableCount)
839
840 Buffer += b
841 b = pack('=B', Pad)
842 Buffer += b
843 Buffer += b
844 Buffer += b
845 Buffer += b
846 Buffer += b
847 Buffer += b
848
849 Index = 0
850 for Item in DbItemTotal:
851 Index +=1
852 b = Item.PackData()
853 Buffer += b
854 if Index == InitTableNum:
855 if len(Buffer) % 8:
856 for num in range(8 - len(Buffer) % 8):
857 b = pack('=B', Pad)
858 Buffer += b
859 break
860 return Buffer
861
862 ## Create code for PCD database
863 #
864 # @param Info The ModuleAutoGen object
865 # @param AutoGenC The TemplateString object for C code
866 # @param AutoGenH The TemplateString object for header file
867 #
868 def CreatePcdDatabaseCode (Info, AutoGenC, AutoGenH):
869 if Info.PcdIsDriver == "":
870 return
871 if Info.PcdIsDriver not in gPcdPhaseMap:
872 EdkLogger.error("build", AUTOGEN_ERROR, "Not supported PcdIsDriver type:%s" % Info.PcdIsDriver,
873 ExtraData="[%s]" % str(Info))
874
875 AdditionalAutoGenH, AdditionalAutoGenC, PcdDbBuffer = NewCreatePcdDatabasePhaseSpecificAutoGen (Info.PlatformInfo, 'PEI')
876 AutoGenH.Append(AdditionalAutoGenH.String)
877
878 Phase = gPcdPhaseMap[Info.PcdIsDriver]
879 if Phase == 'PEI':
880 AutoGenC.Append(AdditionalAutoGenC.String)
881
882 if Phase == 'DXE':
883 AdditionalAutoGenH, AdditionalAutoGenC, PcdDbBuffer = NewCreatePcdDatabasePhaseSpecificAutoGen (Info.PlatformInfo, Phase)
884 AutoGenH.Append(AdditionalAutoGenH.String)
885 AutoGenC.Append(AdditionalAutoGenC.String)
886
887 if Info.IsBinaryModule:
888 DbFileName = os.path.join(Info.PlatformInfo.BuildDir, TAB_FV_DIRECTORY, Phase + "PcdDataBase.raw")
889 else:
890 DbFileName = os.path.join(Info.OutputDir, Phase + "PcdDataBase.raw")
891 DbFile = BytesIO()
892 DbFile.write(PcdDbBuffer)
893 Changed = SaveFileOnChange(DbFileName, DbFile.getvalue(), True)
894 def CreatePcdDataBase(PcdDBData):
895 delta = {}
896 for skuname, skuid in PcdDBData:
897 if len(PcdDBData[(skuname, skuid)][1]) != len(PcdDBData[(TAB_DEFAULT, "0")][1]):
898 EdkLogger.ERROR("The size of each sku in one pcd are not same")
899 for skuname, skuid in PcdDBData:
900 if skuname == TAB_DEFAULT:
901 continue
902 delta[(skuname, skuid)] = [(index, data, hex(data)) for index, data in enumerate(PcdDBData[(skuname, skuid)][1]) if PcdDBData[(skuname, skuid)][1][index] != PcdDBData[(TAB_DEFAULT, "0")][1][index]]
903 databasebuff = PcdDBData[(TAB_DEFAULT, "0")][0]
904
905 for skuname, skuid in delta:
906 # 8 byte align
907 if len(databasebuff) % 8 > 0:
908 for i in range(8 - (len(databasebuff) % 8)):
909 databasebuff += pack("=B", 0)
910 databasebuff += pack('=Q', int(skuid))
911 databasebuff += pack('=Q', 0)
912 databasebuff += pack('=L', 8+8+4+4*len(delta[(skuname, skuid)]))
913 for item in delta[(skuname, skuid)]:
914 databasebuff += pack("=L", item[0])
915 databasebuff = databasebuff[:-1] + pack("=B", item[1])
916 totallen = len(databasebuff)
917 totallenbuff = pack("=L", totallen)
918 newbuffer = databasebuff[:32]
919 for i in range(4):
920 newbuffer += totallenbuff[i]
921 for i in range(36, totallen):
922 newbuffer += databasebuff[i]
923
924 return newbuffer
925
926 def CreateVarCheckBin(VarCheckTab):
927 return VarCheckTab[(TAB_DEFAULT, "0")]
928
929 def CreateAutoGen(PcdDriverAutoGenData):
930 autogenC = TemplateString()
931 for skuname, skuid in PcdDriverAutoGenData:
932 autogenC.Append("//SKUID: %s" % skuname)
933 autogenC.Append(PcdDriverAutoGenData[(skuname, skuid)][1].String)
934 return (PcdDriverAutoGenData[(skuname, skuid)][0], autogenC)
935 def NewCreatePcdDatabasePhaseSpecificAutoGen(Platform, Phase):
936 def prune_sku(pcd, skuname):
937 new_pcd = copy.deepcopy(pcd)
938 new_pcd.SkuInfoList = {skuname:pcd.SkuInfoList[skuname]}
939 new_pcd.isinit = 'INIT'
940 if new_pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES:
941 for skuobj in pcd.SkuInfoList.values():
942 if skuobj.DefaultValue:
943 defaultvalue = int(skuobj.DefaultValue, 16) if skuobj.DefaultValue.upper().startswith("0X") else int(skuobj.DefaultValue, 10)
944 if defaultvalue != 0:
945 new_pcd.isinit = "INIT"
946 break
947 elif skuobj.VariableName:
948 new_pcd.isinit = "INIT"
949 break
950 else:
951 new_pcd.isinit = "UNINIT"
952 return new_pcd
953 DynamicPcds = Platform.DynamicPcdList
954 DynamicPcdSet_Sku = {(SkuName, skuobj.SkuId):[] for pcd in DynamicPcds for (SkuName, skuobj) in pcd.SkuInfoList.items() }
955 for skuname, skuid in DynamicPcdSet_Sku:
956 DynamicPcdSet_Sku[(skuname, skuid)] = [prune_sku(pcd, skuname) for pcd in DynamicPcds]
957 PcdDBData = {}
958 PcdDriverAutoGenData = {}
959 VarCheckTableData = {}
960 if DynamicPcdSet_Sku:
961 for skuname, skuid in DynamicPcdSet_Sku:
962 AdditionalAutoGenH, AdditionalAutoGenC, PcdDbBuffer, VarCheckTab = CreatePcdDatabasePhaseSpecificAutoGen (Platform, DynamicPcdSet_Sku[(skuname, skuid)], Phase)
963 final_data = ()
964 for item in PcdDbBuffer:
965 final_data += unpack("B", item)
966 PcdDBData[(skuname, skuid)] = (PcdDbBuffer, final_data)
967 PcdDriverAutoGenData[(skuname, skuid)] = (AdditionalAutoGenH, AdditionalAutoGenC)
968 VarCheckTableData[(skuname, skuid)] = VarCheckTab
969 if Platform.Platform.VarCheckFlag:
970 dest = os.path.join(Platform.BuildDir, TAB_FV_DIRECTORY)
971 VarCheckTable = CreateVarCheckBin(VarCheckTableData)
972 VarCheckTable.dump(dest, Phase)
973 AdditionalAutoGenH, AdditionalAutoGenC = CreateAutoGen(PcdDriverAutoGenData)
974 else:
975 AdditionalAutoGenH, AdditionalAutoGenC, PcdDbBuffer, VarCheckTab = CreatePcdDatabasePhaseSpecificAutoGen (Platform, {}, Phase)
976 final_data = ()
977 for item in PcdDbBuffer:
978 final_data += unpack("B", item)
979 PcdDBData[(TAB_DEFAULT, "0")] = (PcdDbBuffer, final_data)
980
981 return AdditionalAutoGenH, AdditionalAutoGenC, CreatePcdDataBase(PcdDBData)
982 ## Create PCD database in DXE or PEI phase
983 #
984 # @param Platform The platform object
985 # @retval tuple Two TemplateString objects for C code and header file,
986 # respectively
987 #
988 def CreatePcdDatabasePhaseSpecificAutoGen (Platform, DynamicPcdList, Phase):
989 AutoGenC = TemplateString()
990 AutoGenH = TemplateString()
991
992 Dict = {
993 'PHASE' : Phase,
994 'SERVICE_DRIVER_VERSION' : DATABASE_VERSION,
995 'GUID_TABLE_SIZE' : '1U',
996 'STRING_TABLE_SIZE' : '1U',
997 'SKUID_TABLE_SIZE' : '1U',
998 'LOCAL_TOKEN_NUMBER_TABLE_SIZE' : '0U',
999 'LOCAL_TOKEN_NUMBER' : '0U',
1000 'EXMAPPING_TABLE_SIZE' : '1U',
1001 'EX_TOKEN_NUMBER' : '0U',
1002 'SIZE_TABLE_SIZE' : '2U',
1003 'SKU_HEAD_SIZE' : '1U',
1004 'GUID_TABLE_EMPTY' : 'TRUE',
1005 'STRING_TABLE_EMPTY' : 'TRUE',
1006 'SKUID_TABLE_EMPTY' : 'TRUE',
1007 'DATABASE_EMPTY' : 'TRUE',
1008 'EXMAP_TABLE_EMPTY' : 'TRUE',
1009 'PCD_DATABASE_UNINIT_EMPTY' : ' UINT8 dummy; /* PCD_DATABASE_UNINIT is emptry */',
1010 'SYSTEM_SKU_ID' : ' SKU_ID SystemSkuId;',
1011 'SYSTEM_SKU_ID_VALUE' : '0U'
1012 }
1013
1014 SkuObj = Platform.Platform.SkuIdMgr
1015 Dict['SYSTEM_SKU_ID_VALUE'] = 0 if SkuObj.SkuUsageType == SkuObj.SINGLE else Platform.Platform.SkuIds[SkuObj.SystemSkuId][0]
1016
1017 Dict['PCD_INFO_FLAG'] = Platform.Platform.PcdInfoFlag
1018
1019 for DatumType in TAB_PCD_NUMERIC_TYPES_VOID:
1020 Dict['VARDEF_CNAME_' + DatumType] = []
1021 Dict['VARDEF_GUID_' + DatumType] = []
1022 Dict['VARDEF_SKUID_' + DatumType] = []
1023 Dict['VARDEF_VALUE_' + DatumType] = []
1024 Dict['VARDEF_DB_VALUE_' + DatumType] = []
1025 for Init in ['INIT', 'UNINIT']:
1026 Dict[Init+'_CNAME_DECL_' + DatumType] = []
1027 Dict[Init+'_GUID_DECL_' + DatumType] = []
1028 Dict[Init+'_NUMSKUS_DECL_' + DatumType] = []
1029 Dict[Init+'_VALUE_' + DatumType] = []
1030 Dict[Init+'_DB_VALUE_'+DatumType] = []
1031
1032 for Type in ['STRING_HEAD', 'VPD_HEAD', 'VARIABLE_HEAD']:
1033 Dict[Type + '_CNAME_DECL'] = []
1034 Dict[Type + '_GUID_DECL'] = []
1035 Dict[Type + '_NUMSKUS_DECL'] = []
1036 Dict[Type + '_VALUE'] = []
1037
1038 Dict['STRING_DB_VALUE'] = []
1039 Dict['VPD_DB_VALUE'] = []
1040 Dict['VARIABLE_DB_VALUE'] = []
1041
1042 Dict['STRING_TABLE_INDEX'] = []
1043 Dict['STRING_TABLE_LENGTH'] = []
1044 Dict['STRING_TABLE_CNAME'] = []
1045 Dict['STRING_TABLE_GUID'] = []
1046 Dict['STRING_TABLE_VALUE'] = []
1047 Dict['STRING_TABLE_DB_VALUE'] = []
1048
1049 Dict['SIZE_TABLE_CNAME'] = []
1050 Dict['SIZE_TABLE_GUID'] = []
1051 Dict['SIZE_TABLE_CURRENT_LENGTH'] = []
1052 Dict['SIZE_TABLE_MAXIMUM_LENGTH'] = []
1053
1054 Dict['EXMAPPING_TABLE_EXTOKEN'] = []
1055 Dict['EXMAPPING_TABLE_LOCAL_TOKEN'] = []
1056 Dict['EXMAPPING_TABLE_GUID_INDEX'] = []
1057
1058 Dict['GUID_STRUCTURE'] = []
1059 Dict['SKUID_VALUE'] = [0] # init Dict length
1060 Dict['VARDEF_HEADER'] = []
1061
1062 Dict['LOCAL_TOKEN_NUMBER_DB_VALUE'] = []
1063 Dict['VARIABLE_DB_VALUE'] = []
1064
1065 Dict['PCD_TOKENSPACE'] = []
1066 Dict['PCD_CNAME'] = []
1067 Dict['PCD_TOKENSPACE_LENGTH'] = []
1068 Dict['PCD_CNAME_LENGTH'] = []
1069 Dict['PCD_TOKENSPACE_OFFSET'] = []
1070 Dict['PCD_CNAME_OFFSET'] = []
1071 Dict['PCD_TOKENSPACE_MAP'] = []
1072 Dict['PCD_NAME_OFFSET'] = []
1073
1074 Dict['PCD_ORDER_TOKEN_NUMBER_MAP'] = {}
1075 PCD_STRING_INDEX_MAP = {}
1076
1077 StringTableIndex = 0
1078 StringTableSize = 0
1079 NumberOfLocalTokens = 0
1080 NumberOfPeiLocalTokens = 0
1081 NumberOfDxeLocalTokens = 0
1082 NumberOfExTokens = 0
1083 NumberOfSizeItems = 0
1084 NumberOfSkuEnabledPcd = 0
1085 GuidList = []
1086 VarCheckTab = VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER()
1087 i = 0
1088 ReorderedDynPcdList = GetOrderedDynamicPcdList(DynamicPcdList, Platform.PcdTokenNumber)
1089 for item in ReorderedDynPcdList:
1090 if item.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1091 item.DatumType = TAB_VOID
1092 for Pcd in ReorderedDynPcdList:
1093 VoidStarTypeCurrSize = []
1094 i += 1
1095 CName = Pcd.TokenCName
1096 TokenSpaceGuidCName = Pcd.TokenSpaceGuidCName
1097
1098 for PcdItem in GlobalData.MixedPcd:
1099 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
1100 CName = PcdItem[0]
1101
1102 EdkLogger.debug(EdkLogger.DEBUG_3, "PCD: %s %s (%s : %s)" % (CName, TokenSpaceGuidCName, Pcd.Phase, Phase))
1103
1104 if Pcd.Phase == 'PEI':
1105 NumberOfPeiLocalTokens += 1
1106 if Pcd.Phase == 'DXE':
1107 NumberOfDxeLocalTokens += 1
1108 if Pcd.Phase != Phase:
1109 continue
1110
1111 #
1112 # TODO: need GetGuidValue() definition
1113 #
1114 TokenSpaceGuidStructure = Pcd.TokenSpaceGuidValue
1115 TokenSpaceGuid = GuidStructureStringToGuidValueName(TokenSpaceGuidStructure)
1116 if Pcd.Type in PCD_DYNAMIC_EX_TYPE_SET:
1117 if TokenSpaceGuid not in GuidList:
1118 GuidList.append(TokenSpaceGuid)
1119 Dict['GUID_STRUCTURE'].append(TokenSpaceGuidStructure)
1120 NumberOfExTokens += 1
1121
1122 ValueList = []
1123 DbValueList = []
1124 StringHeadOffsetList = []
1125 StringDbOffsetList = []
1126 VpdHeadOffsetList = []
1127 VpdDbOffsetList = []
1128 VariableHeadValueList = []
1129 VariableDbValueList = []
1130 Pcd.InitString = 'UNINIT'
1131
1132 if Pcd.DatumType == TAB_VOID:
1133 if Pcd.Type not in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD]:
1134 Pcd.TokenTypeList = ['PCD_TYPE_STRING']
1135 else:
1136 Pcd.TokenTypeList = []
1137 elif Pcd.DatumType == 'BOOLEAN':
1138 Pcd.TokenTypeList = ['PCD_DATUM_TYPE_UINT8_BOOLEAN']
1139 else:
1140 Pcd.TokenTypeList = ['PCD_DATUM_TYPE_' + Pcd.DatumType]
1141
1142 if len(Pcd.SkuInfoList) > 1:
1143 NumberOfSkuEnabledPcd += 1
1144
1145 SkuIdIndex = 1
1146 VariableHeadList = []
1147 for SkuName in Pcd.SkuInfoList:
1148 Sku = Pcd.SkuInfoList[SkuName]
1149 SkuId = Sku.SkuId
1150 if SkuId is None or SkuId == '':
1151 continue
1152
1153
1154 SkuIdIndex += 1
1155
1156 if len(Sku.VariableName) > 0:
1157 VariableGuidStructure = Sku.VariableGuidValue
1158 VariableGuid = GuidStructureStringToGuidValueName(VariableGuidStructure)
1159 if Platform.Platform.VarCheckFlag:
1160 var_check_obj = VAR_CHECK_PCD_VARIABLE_TAB(VariableGuidStructure, StringToArray(Sku.VariableName))
1161 try:
1162 var_check_obj.push_back(GetValidationObject(Pcd, Sku.VariableOffset))
1163 VarAttr, _ = VariableAttributes.GetVarAttributes(Sku.VariableAttribute)
1164 var_check_obj.SetAttributes(VarAttr)
1165 var_check_obj.UpdateSize()
1166 VarCheckTab.push_back(var_check_obj)
1167 except Exception:
1168 ValidInfo = ''
1169 if Pcd.validateranges:
1170 ValidInfo = Pcd.validateranges[0]
1171 if Pcd.validlists:
1172 ValidInfo = Pcd.validlists[0]
1173 if ValidInfo:
1174 EdkLogger.error("build", PCD_VALIDATION_INFO_ERROR,
1175 "The PCD '%s.%s' Validation information defined in DEC file has incorrect format." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1176 ExtraData = "[%s]" % str(ValidInfo))
1177 else:
1178 EdkLogger.error("build", PCD_VALIDATION_INFO_ERROR,
1179 "The PCD '%s.%s' Validation information defined in DEC file has incorrect format." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1180 Pcd.TokenTypeList.append('PCD_TYPE_HII')
1181 Pcd.InitString = 'INIT'
1182 # Store all variable names of one HII PCD under different SKU to stringTable
1183 # and calculate the VariableHeadStringIndex
1184
1185 VariableNameStructure = StringToArray(Sku.VariableName)
1186 if VariableNameStructure not in Dict['STRING_TABLE_VALUE']:
1187 Dict['STRING_TABLE_CNAME'].append(CName)
1188 Dict['STRING_TABLE_GUID'].append(TokenSpaceGuid)
1189 if StringTableIndex == 0:
1190 Dict['STRING_TABLE_INDEX'].append('')
1191 else:
1192 Dict['STRING_TABLE_INDEX'].append('_%d' % StringTableIndex)
1193 VarNameSize = len(VariableNameStructure.replace(',', ' ').split())
1194 Dict['STRING_TABLE_LENGTH'].append(VarNameSize )
1195 Dict['STRING_TABLE_VALUE'].append(VariableNameStructure)
1196 StringHeadOffsetList.append(str(StringTableSize) + 'U')
1197 VarStringDbOffsetList = []
1198 VarStringDbOffsetList.append(StringTableSize)
1199 Dict['STRING_DB_VALUE'].append(VarStringDbOffsetList)
1200 StringTableIndex += 1
1201 StringTableSize += len(VariableNameStructure.replace(',', ' ').split())
1202 VariableHeadStringIndex = 0
1203 for Index in range(Dict['STRING_TABLE_VALUE'].index(VariableNameStructure)):
1204 VariableHeadStringIndex += Dict['STRING_TABLE_LENGTH'][Index]
1205 VariableHeadList.append(VariableHeadStringIndex)
1206
1207 VariableHeadStringIndex = VariableHeadList[SkuIdIndex - 2]
1208 # store VariableGuid to GuidTable and get the VariableHeadGuidIndex
1209
1210 if VariableGuid not in GuidList:
1211 GuidList.append(VariableGuid)
1212 Dict['GUID_STRUCTURE'].append(VariableGuidStructure)
1213 VariableHeadGuidIndex = GuidList.index(VariableGuid)
1214
1215 if "PCD_TYPE_STRING" in Pcd.TokenTypeList:
1216 VariableHeadValueList.append('%dU, offsetof(%s_PCD_DATABASE, Init.%s_%s), %dU, %sU' %
1217 (VariableHeadStringIndex, Phase, CName, TokenSpaceGuid,
1218 VariableHeadGuidIndex, Sku.VariableOffset))
1219 else:
1220 VariableHeadValueList.append('%dU, offsetof(%s_PCD_DATABASE, Init.%s_%s_VariableDefault_%s), %dU, %sU' %
1221 (VariableHeadStringIndex, Phase, CName, TokenSpaceGuid, SkuIdIndex,
1222 VariableHeadGuidIndex, Sku.VariableOffset))
1223 Dict['VARDEF_CNAME_'+Pcd.DatumType].append(CName)
1224 Dict['VARDEF_GUID_'+Pcd.DatumType].append(TokenSpaceGuid)
1225 Dict['VARDEF_SKUID_'+Pcd.DatumType].append(SkuIdIndex)
1226 if "PCD_TYPE_STRING" in Pcd.TokenTypeList:
1227 Dict['VARDEF_VALUE_' + Pcd.DatumType].append("%s_%s[%d]" % (Pcd.TokenCName, TokenSpaceGuid, SkuIdIndex))
1228 else:
1229 #
1230 # ULL (for UINT64) or U(other integer type) should be append to avoid
1231 # warning under linux building environment.
1232 #
1233 Dict['VARDEF_DB_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue)
1234
1235 if Pcd.DatumType == TAB_UINT64:
1236 Dict['VARDEF_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue + "ULL")
1237 elif Pcd.DatumType in (TAB_UINT32, TAB_UINT16, TAB_UINT8):
1238 Dict['VARDEF_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue + "U")
1239 elif Pcd.DatumType == "BOOLEAN":
1240 if eval(Sku.HiiDefaultValue) in [1, 0]:
1241 Dict['VARDEF_VALUE_'+Pcd.DatumType].append(str(eval(Sku.HiiDefaultValue)) + "U")
1242 else:
1243 Dict['VARDEF_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue)
1244
1245 # construct the VariableHeader value
1246 if "PCD_TYPE_STRING" in Pcd.TokenTypeList:
1247 VariableHeadValueList.append('%dU, %dU, %sU, offsetof(%s_PCD_DATABASE, Init.%s_%s)' %
1248 (VariableHeadGuidIndex, VariableHeadStringIndex, Sku.VariableOffset,
1249 Phase, CName, TokenSpaceGuid))
1250 # the Pcd default value will be filled later on
1251 VariableOffset = len(Dict['STRING_DB_VALUE'])
1252 VariableRefTable = Dict['STRING_DB_VALUE']
1253 else:
1254 VariableHeadValueList.append('%dU, %dU, %sU, offsetof(%s_PCD_DATABASE, Init.%s_%s_VariableDefault_%s)' %
1255 (VariableHeadGuidIndex, VariableHeadStringIndex, Sku.VariableOffset,
1256 Phase, CName, TokenSpaceGuid, SkuIdIndex))
1257 # the Pcd default value was filled before
1258 VariableOffset = len(Dict['VARDEF_DB_VALUE_' + Pcd.DatumType]) - 1
1259 VariableRefTable = Dict['VARDEF_DB_VALUE_' + Pcd.DatumType]
1260 VariableDbValueList.append([VariableHeadGuidIndex, VariableHeadStringIndex, Sku.VariableOffset, VariableOffset, VariableRefTable, Sku.VariableAttribute])
1261
1262 elif Sku.VpdOffset != '':
1263 Pcd.TokenTypeList.append('PCD_TYPE_VPD')
1264 Pcd.InitString = 'INIT'
1265 VpdHeadOffsetList.append(str(Sku.VpdOffset) + 'U')
1266 VpdDbOffsetList.append(Sku.VpdOffset)
1267 # Also add the VOID* string of VPD PCD to SizeTable
1268 if Pcd.DatumType == TAB_VOID:
1269 NumberOfSizeItems += 1
1270 # For VPD type of PCD, its current size is equal to its MAX size.
1271 VoidStarTypeCurrSize = [str(Pcd.MaxDatumSize) + 'U']
1272 continue
1273
1274 if Pcd.DatumType == TAB_VOID:
1275 Pcd.TokenTypeList.append('PCD_TYPE_STRING')
1276 Pcd.InitString = 'INIT'
1277 if Sku.HiiDefaultValue != '' and Sku.DefaultValue == '':
1278 Sku.DefaultValue = Sku.HiiDefaultValue
1279 if Sku.DefaultValue != '':
1280 NumberOfSizeItems += 1
1281 Dict['STRING_TABLE_CNAME'].append(CName)
1282 Dict['STRING_TABLE_GUID'].append(TokenSpaceGuid)
1283
1284 if StringTableIndex == 0:
1285 Dict['STRING_TABLE_INDEX'].append('')
1286 else:
1287 Dict['STRING_TABLE_INDEX'].append('_%d' % StringTableIndex)
1288 if Sku.DefaultValue[0] == 'L':
1289 DefaultValueBinStructure = StringToArray(Sku.DefaultValue)
1290 Size = len(DefaultValueBinStructure.replace(',', ' ').split())
1291 Dict['STRING_TABLE_VALUE'].append(DefaultValueBinStructure)
1292 elif Sku.DefaultValue[0] == '"':
1293 DefaultValueBinStructure = StringToArray(Sku.DefaultValue)
1294 Size = len(Sku.DefaultValue) - 2 + 1
1295 Dict['STRING_TABLE_VALUE'].append(DefaultValueBinStructure)
1296 elif Sku.DefaultValue[0] == '{':
1297 DefaultValueBinStructure = StringToArray(Sku.DefaultValue)
1298 Size = len(Sku.DefaultValue.split(","))
1299 Dict['STRING_TABLE_VALUE'].append(DefaultValueBinStructure)
1300
1301 StringHeadOffsetList.append(str(StringTableSize) + 'U')
1302 StringDbOffsetList.append(StringTableSize)
1303 if Pcd.MaxDatumSize != '':
1304 MaxDatumSize = int(Pcd.MaxDatumSize, 0)
1305 if MaxDatumSize < Size:
1306 if Pcd.MaxSizeUserSet:
1307 EdkLogger.error("build", AUTOGEN_ERROR,
1308 "The maximum size of VOID* type PCD '%s.%s' is less than its actual size occupied." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1309 ExtraData="[%s]" % str(Platform))
1310 else:
1311 MaxDatumSize = Size
1312 else:
1313 MaxDatumSize = Size
1314 StringTabLen = MaxDatumSize
1315 if StringTabLen % 2:
1316 StringTabLen += 1
1317 if Sku.VpdOffset == '':
1318 VoidStarTypeCurrSize.append(str(Size) + 'U')
1319 Dict['STRING_TABLE_LENGTH'].append(StringTabLen)
1320 StringTableIndex += 1
1321 StringTableSize += (StringTabLen)
1322 else:
1323 if "PCD_TYPE_HII" not in Pcd.TokenTypeList:
1324 Pcd.TokenTypeList.append('PCD_TYPE_DATA')
1325 if Sku.DefaultValue == 'TRUE':
1326 Pcd.InitString = 'INIT'
1327 else:
1328 Pcd.InitString = Pcd.isinit
1329 #
1330 # For UNIT64 type PCD's value, ULL should be append to avoid
1331 # warning under linux building environment.
1332 #
1333 if Pcd.DatumType == TAB_UINT64:
1334 ValueList.append(Sku.DefaultValue + "ULL")
1335 elif Pcd.DatumType in (TAB_UINT32, TAB_UINT16, TAB_UINT8):
1336 ValueList.append(Sku.DefaultValue + "U")
1337 elif Pcd.DatumType == "BOOLEAN":
1338 if Sku.DefaultValue in ["1", "0"]:
1339 ValueList.append(Sku.DefaultValue + "U")
1340 else:
1341 ValueList.append(Sku.DefaultValue)
1342
1343 DbValueList.append(Sku.DefaultValue)
1344
1345 Pcd.TokenTypeList = list(set(Pcd.TokenTypeList))
1346 if Pcd.DatumType == TAB_VOID:
1347 Dict['SIZE_TABLE_CNAME'].append(CName)
1348 Dict['SIZE_TABLE_GUID'].append(TokenSpaceGuid)
1349 Dict['SIZE_TABLE_MAXIMUM_LENGTH'].append(str(Pcd.MaxDatumSize) + 'U')
1350 Dict['SIZE_TABLE_CURRENT_LENGTH'].append(VoidStarTypeCurrSize)
1351
1352
1353
1354 if 'PCD_TYPE_HII' in Pcd.TokenTypeList:
1355 Dict['VARIABLE_HEAD_CNAME_DECL'].append(CName)
1356 Dict['VARIABLE_HEAD_GUID_DECL'].append(TokenSpaceGuid)
1357 Dict['VARIABLE_HEAD_NUMSKUS_DECL'].append(len(Pcd.SkuInfoList))
1358 Dict['VARIABLE_HEAD_VALUE'].append('{ %s }\n' % ' },\n { '.join(VariableHeadValueList))
1359 Dict['VARDEF_HEADER'].append('_Variable_Header')
1360 Dict['VARIABLE_DB_VALUE'].append(VariableDbValueList)
1361 else:
1362 Dict['VARDEF_HEADER'].append('')
1363 if 'PCD_TYPE_VPD' in Pcd.TokenTypeList:
1364 Dict['VPD_HEAD_CNAME_DECL'].append(CName)
1365 Dict['VPD_HEAD_GUID_DECL'].append(TokenSpaceGuid)
1366 Dict['VPD_HEAD_NUMSKUS_DECL'].append(len(Pcd.SkuInfoList))
1367 Dict['VPD_HEAD_VALUE'].append('{ %s }' % ' }, { '.join(VpdHeadOffsetList))
1368 Dict['VPD_DB_VALUE'].append(VpdDbOffsetList)
1369 if 'PCD_TYPE_STRING' in Pcd.TokenTypeList:
1370 Dict['STRING_HEAD_CNAME_DECL'].append(CName)
1371 Dict['STRING_HEAD_GUID_DECL'].append(TokenSpaceGuid)
1372 Dict['STRING_HEAD_NUMSKUS_DECL'].append(len(Pcd.SkuInfoList))
1373 Dict['STRING_HEAD_VALUE'].append(', '.join(StringHeadOffsetList))
1374 Dict['STRING_DB_VALUE'].append(StringDbOffsetList)
1375 PCD_STRING_INDEX_MAP[len(Dict['STRING_HEAD_CNAME_DECL']) -1 ] = len(Dict['STRING_DB_VALUE']) -1
1376 if 'PCD_TYPE_DATA' in Pcd.TokenTypeList:
1377 Dict[Pcd.InitString+'_CNAME_DECL_'+Pcd.DatumType].append(CName)
1378 Dict[Pcd.InitString+'_GUID_DECL_'+Pcd.DatumType].append(TokenSpaceGuid)
1379 Dict[Pcd.InitString+'_NUMSKUS_DECL_'+Pcd.DatumType].append(len(Pcd.SkuInfoList))
1380 if Pcd.InitString == 'UNINIT':
1381 Dict['PCD_DATABASE_UNINIT_EMPTY'] = ''
1382 else:
1383 Dict[Pcd.InitString+'_VALUE_'+Pcd.DatumType].append(', '.join(ValueList))
1384 Dict[Pcd.InitString+'_DB_VALUE_'+Pcd.DatumType].append(DbValueList)
1385
1386 if Phase == 'PEI':
1387 NumberOfLocalTokens = NumberOfPeiLocalTokens
1388 if Phase == 'DXE':
1389 NumberOfLocalTokens = NumberOfDxeLocalTokens
1390
1391 Dict['TOKEN_INIT'] = ['' for x in range(NumberOfLocalTokens)]
1392 Dict['TOKEN_CNAME'] = ['' for x in range(NumberOfLocalTokens)]
1393 Dict['TOKEN_GUID'] = ['' for x in range(NumberOfLocalTokens)]
1394 Dict['TOKEN_TYPE'] = ['' for x in range(NumberOfLocalTokens)]
1395 Dict['LOCAL_TOKEN_NUMBER_DB_VALUE'] = ['' for x in range(NumberOfLocalTokens)]
1396 Dict['PCD_CNAME'] = ['' for x in range(NumberOfLocalTokens)]
1397 Dict['PCD_TOKENSPACE_MAP'] = ['' for x in range(NumberOfLocalTokens)]
1398 Dict['PCD_CNAME_LENGTH'] = [0 for x in range(NumberOfLocalTokens)]
1399 SkuEnablePcdIndex = 0
1400 for Pcd in ReorderedDynPcdList:
1401 CName = Pcd.TokenCName
1402 TokenSpaceGuidCName = Pcd.TokenSpaceGuidCName
1403 if Pcd.Phase != Phase:
1404 continue
1405
1406 TokenSpaceGuid = GuidStructureStringToGuidValueName(Pcd.TokenSpaceGuidValue) #(Platform.PackageList, TokenSpaceGuidCName))
1407 GeneratedTokenNumber = Platform.PcdTokenNumber[CName, TokenSpaceGuidCName] - 1
1408 if Phase == 'DXE':
1409 GeneratedTokenNumber -= NumberOfPeiLocalTokens
1410
1411 if len(Pcd.SkuInfoList) > 1:
1412 Dict['PCD_ORDER_TOKEN_NUMBER_MAP'][GeneratedTokenNumber] = SkuEnablePcdIndex
1413 SkuEnablePcdIndex += 1
1414
1415 for PcdItem in GlobalData.MixedPcd:
1416 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
1417 CName = PcdItem[0]
1418
1419 EdkLogger.debug(EdkLogger.DEBUG_1, "PCD = %s.%s" % (CName, TokenSpaceGuidCName))
1420 EdkLogger.debug(EdkLogger.DEBUG_1, "phase = %s" % Phase)
1421 EdkLogger.debug(EdkLogger.DEBUG_1, "GeneratedTokenNumber = %s" % str(GeneratedTokenNumber))
1422
1423 #
1424 # following four Dict items hold the information for LocalTokenNumberTable
1425 #
1426 Dict['TOKEN_INIT'][GeneratedTokenNumber] = 'Init'
1427 if Pcd.InitString == 'UNINIT':
1428 Dict['TOKEN_INIT'][GeneratedTokenNumber] = 'Uninit'
1429
1430 Dict['TOKEN_CNAME'][GeneratedTokenNumber] = CName
1431 Dict['TOKEN_GUID'][GeneratedTokenNumber] = TokenSpaceGuid
1432 Dict['TOKEN_TYPE'][GeneratedTokenNumber] = ' | '.join(Pcd.TokenTypeList)
1433
1434 if Platform.Platform.PcdInfoFlag:
1435 TokenSpaceGuidCNameArray = StringToArray('"' + TokenSpaceGuidCName + '"' )
1436 if TokenSpaceGuidCNameArray not in Dict['PCD_TOKENSPACE']:
1437 Dict['PCD_TOKENSPACE'].append(TokenSpaceGuidCNameArray)
1438 Dict['PCD_TOKENSPACE_LENGTH'].append( len(TokenSpaceGuidCNameArray.split(",")) )
1439 Dict['PCD_TOKENSPACE_MAP'][GeneratedTokenNumber] = Dict['PCD_TOKENSPACE'].index(TokenSpaceGuidCNameArray)
1440 CNameBinArray = StringToArray('"' + CName + '"' )
1441 Dict['PCD_CNAME'][GeneratedTokenNumber] = CNameBinArray
1442
1443 Dict['PCD_CNAME_LENGTH'][GeneratedTokenNumber] = len(CNameBinArray.split(","))
1444
1445
1446 Pcd.TokenTypeList = list(set(Pcd.TokenTypeList))
1447
1448 # search the Offset and Table, used by LocalTokenNumberTableOffset
1449 if 'PCD_TYPE_HII' in Pcd.TokenTypeList:
1450 # Find index by CName, TokenSpaceGuid
1451 Offset = GetMatchedIndex(CName, Dict['VARIABLE_HEAD_CNAME_DECL'], TokenSpaceGuid, Dict['VARIABLE_HEAD_GUID_DECL'])
1452 assert(Offset != -1)
1453 Table = Dict['VARIABLE_DB_VALUE']
1454 if 'PCD_TYPE_VPD' in Pcd.TokenTypeList:
1455 Offset = GetMatchedIndex(CName, Dict['VPD_HEAD_CNAME_DECL'], TokenSpaceGuid, Dict['VPD_HEAD_GUID_DECL'])
1456 assert(Offset != -1)
1457 Table = Dict['VPD_DB_VALUE']
1458 if 'PCD_TYPE_STRING' in Pcd.TokenTypeList and 'PCD_TYPE_HII' not in Pcd.TokenTypeList:
1459 # Find index by CName, TokenSpaceGuid
1460 Offset = GetMatchedIndex(CName, Dict['STRING_HEAD_CNAME_DECL'], TokenSpaceGuid, Dict['STRING_HEAD_GUID_DECL'])
1461 Offset = PCD_STRING_INDEX_MAP[Offset]
1462 assert(Offset != -1)
1463 Table = Dict['STRING_DB_VALUE']
1464 if 'PCD_TYPE_DATA' in Pcd.TokenTypeList:
1465 # need to store whether it is in init table or not
1466 Offset = GetMatchedIndex(CName, Dict[Pcd.InitString+'_CNAME_DECL_'+Pcd.DatumType], TokenSpaceGuid, Dict[Pcd.InitString+'_GUID_DECL_'+Pcd.DatumType])
1467 assert(Offset != -1)
1468 if Pcd.InitString == 'UNINIT':
1469 Table = Dict[Pcd.InitString+'_GUID_DECL_'+Pcd.DatumType]
1470 else:
1471 Table = Dict[Pcd.InitString+'_DB_VALUE_'+Pcd.DatumType]
1472 Dict['LOCAL_TOKEN_NUMBER_DB_VALUE'][GeneratedTokenNumber] = (Offset, Table)
1473
1474 #
1475 # Update VARDEF_HEADER
1476 #
1477 if 'PCD_TYPE_HII' in Pcd.TokenTypeList:
1478 Dict['VARDEF_HEADER'][GeneratedTokenNumber] = '_Variable_Header'
1479 else:
1480 Dict['VARDEF_HEADER'][GeneratedTokenNumber] = ''
1481
1482
1483 if Pcd.Type in PCD_DYNAMIC_EX_TYPE_SET:
1484
1485 if Phase == 'DXE':
1486 GeneratedTokenNumber += NumberOfPeiLocalTokens
1487 #
1488 # Per, PCD architecture specification, PCD Token Number is 1 based and 0 is defined as invalid token number.
1489 # For each EX type PCD, a PCD Token Number is assigned. When the
1490 # PCD Driver/PEIM map EX_GUID and EX_TOKEN_NUMBER to the PCD Token Number,
1491 # the non-EX Protocol/PPI interface can be called to get/set the value. This assumption is made by
1492 # Pcd Driver/PEIM in MdeModulePkg.
1493 # Therefore, 1 is added to GeneratedTokenNumber to generate a PCD Token Number before being inserted
1494 # to the EXMAPPING_TABLE.
1495 #
1496
1497
1498 Dict['EXMAPPING_TABLE_EXTOKEN'].append(str(Pcd.TokenValue) + 'U')
1499 Dict['EXMAPPING_TABLE_LOCAL_TOKEN'].append(str(GeneratedTokenNumber + 1) + 'U')
1500 Dict['EXMAPPING_TABLE_GUID_INDEX'].append(str(GuidList.index(TokenSpaceGuid)) + 'U')
1501
1502 if Platform.Platform.PcdInfoFlag:
1503 for index in range(len(Dict['PCD_TOKENSPACE_MAP'])):
1504 TokenSpaceIndex = StringTableSize
1505 for i in range(Dict['PCD_TOKENSPACE_MAP'][index]):
1506 TokenSpaceIndex += Dict['PCD_TOKENSPACE_LENGTH'][i]
1507 Dict['PCD_TOKENSPACE_OFFSET'].append(TokenSpaceIndex)
1508 for index in range(len(Dict['PCD_TOKENSPACE'])):
1509 StringTableSize += Dict['PCD_TOKENSPACE_LENGTH'][index]
1510 StringTableIndex += 1
1511 for index in range(len(Dict['PCD_CNAME'])):
1512 Dict['PCD_CNAME_OFFSET'].append(StringTableSize)
1513 Dict['PCD_NAME_OFFSET'].append(Dict['PCD_TOKENSPACE_OFFSET'][index])
1514 Dict['PCD_NAME_OFFSET'].append(StringTableSize)
1515 StringTableSize += Dict['PCD_CNAME_LENGTH'][index]
1516 StringTableIndex += 1
1517 if GuidList != []:
1518 Dict['GUID_TABLE_EMPTY'] = 'FALSE'
1519 Dict['GUID_TABLE_SIZE'] = str(len(GuidList)) + 'U'
1520 else:
1521 Dict['GUID_STRUCTURE'] = [GuidStringToGuidStructureString('00000000-0000-0000-0000-000000000000')]
1522
1523 if StringTableIndex == 0:
1524 Dict['STRING_TABLE_INDEX'].append('')
1525 Dict['STRING_TABLE_LENGTH'].append(1)
1526 Dict['STRING_TABLE_CNAME'].append('')
1527 Dict['STRING_TABLE_GUID'].append('')
1528 Dict['STRING_TABLE_VALUE'].append('{ 0 }')
1529 else:
1530 Dict['STRING_TABLE_EMPTY'] = 'FALSE'
1531 Dict['STRING_TABLE_SIZE'] = str(StringTableSize) + 'U'
1532
1533 if Dict['SIZE_TABLE_CNAME'] == []:
1534 Dict['SIZE_TABLE_CNAME'].append('')
1535 Dict['SIZE_TABLE_GUID'].append('')
1536 Dict['SIZE_TABLE_CURRENT_LENGTH'].append(['0U'])
1537 Dict['SIZE_TABLE_MAXIMUM_LENGTH'].append('0U')
1538
1539 if NumberOfLocalTokens != 0:
1540 Dict['DATABASE_EMPTY'] = 'FALSE'
1541 Dict['LOCAL_TOKEN_NUMBER_TABLE_SIZE'] = NumberOfLocalTokens
1542 Dict['LOCAL_TOKEN_NUMBER'] = NumberOfLocalTokens
1543
1544 if NumberOfExTokens != 0:
1545 Dict['EXMAP_TABLE_EMPTY'] = 'FALSE'
1546 Dict['EXMAPPING_TABLE_SIZE'] = str(NumberOfExTokens) + 'U'
1547 Dict['EX_TOKEN_NUMBER'] = str(NumberOfExTokens) + 'U'
1548 else:
1549 Dict['EXMAPPING_TABLE_EXTOKEN'].append('0U')
1550 Dict['EXMAPPING_TABLE_LOCAL_TOKEN'].append('0U')
1551 Dict['EXMAPPING_TABLE_GUID_INDEX'].append('0U')
1552
1553 if NumberOfSizeItems != 0:
1554 Dict['SIZE_TABLE_SIZE'] = str(NumberOfSizeItems * 2) + 'U'
1555
1556 if NumberOfSkuEnabledPcd != 0:
1557 Dict['SKU_HEAD_SIZE'] = str(NumberOfSkuEnabledPcd) + 'U'
1558
1559 for AvailableSkuNumber in SkuObj.SkuIdNumberSet:
1560 if AvailableSkuNumber not in Dict['SKUID_VALUE']:
1561 Dict['SKUID_VALUE'].append(AvailableSkuNumber)
1562 Dict['SKUID_VALUE'][0] = len(Dict['SKUID_VALUE']) - 1
1563
1564 AutoGenH.Append(gPcdDatabaseAutoGenH.Replace(Dict))
1565 if NumberOfLocalTokens == 0:
1566 AutoGenC.Append(gEmptyPcdDatabaseAutoGenC.Replace(Dict))
1567 else:
1568 #
1569 # Update Size Table to the right order, it should be same with LocalTokenNumberTable
1570 #
1571 SizeCNameTempList = []
1572 SizeGuidTempList = []
1573 SizeCurLenTempList = []
1574 SizeMaxLenTempList = []
1575 ReOrderFlag = True
1576
1577 if len(Dict['SIZE_TABLE_CNAME']) == 1:
1578 if not (Dict['SIZE_TABLE_CNAME'][0] and Dict['SIZE_TABLE_GUID'][0]):
1579 ReOrderFlag = False
1580
1581 if ReOrderFlag:
1582 for Count in range(len(Dict['TOKEN_CNAME'])):
1583 for Count1 in range(len(Dict['SIZE_TABLE_CNAME'])):
1584 if Dict['TOKEN_CNAME'][Count] == Dict['SIZE_TABLE_CNAME'][Count1] and \
1585 Dict['TOKEN_GUID'][Count] == Dict['SIZE_TABLE_GUID'][Count1]:
1586 SizeCNameTempList.append(Dict['SIZE_TABLE_CNAME'][Count1])
1587 SizeGuidTempList.append(Dict['SIZE_TABLE_GUID'][Count1])
1588 SizeCurLenTempList.append(Dict['SIZE_TABLE_CURRENT_LENGTH'][Count1])
1589 SizeMaxLenTempList.append(Dict['SIZE_TABLE_MAXIMUM_LENGTH'][Count1])
1590
1591 for Count in range(len(Dict['SIZE_TABLE_CNAME'])):
1592 Dict['SIZE_TABLE_CNAME'][Count] = SizeCNameTempList[Count]
1593 Dict['SIZE_TABLE_GUID'][Count] = SizeGuidTempList[Count]
1594 Dict['SIZE_TABLE_CURRENT_LENGTH'][Count] = SizeCurLenTempList[Count]
1595 Dict['SIZE_TABLE_MAXIMUM_LENGTH'][Count] = SizeMaxLenTempList[Count]
1596
1597 AutoGenC.Append(gPcdDatabaseAutoGenC.Replace(Dict))
1598
1599
1600 # print Phase
1601 Buffer = BuildExDataBase(Dict)
1602 return AutoGenH, AutoGenC, Buffer, VarCheckTab
1603
1604 def GetOrderedDynamicPcdList(DynamicPcdList, PcdTokenNumberList):
1605 ReorderedDyPcdList = [None for i in range(len(DynamicPcdList))]
1606 for Pcd in DynamicPcdList:
1607 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in PcdTokenNumberList:
1608 ReorderedDyPcdList[PcdTokenNumberList[Pcd.TokenCName, Pcd.TokenSpaceGuidCName]-1] = Pcd
1609 return ReorderedDyPcdList
1610