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