• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#
13from StringIO import StringIO
14from Common.Misc import *
15from Common.String import StringToArray
16from struct import pack
17from ValidCheckingInfoObject import VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER
18from ValidCheckingInfoObject import VAR_CHECK_PCD_VARIABLE_TAB
19from ValidCheckingInfoObject import VAR_VALID_OBJECT_FACTORY
20from Common.VariableAttributes import VariableAttributes
21
22DATABASE_VERSION = 6
23
24gPcdDatabaseAutoGenC = 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
97gPcdPhaseMap = {
98    "PEI_PCD_DRIVER"    :   "PEI",
99    "DXE_PCD_DRIVER"    :   "DXE"
100}
101
102gPcdDatabaseAutoGenH = 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
124typedef 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
163typedef 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
177typedef 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
203gEmptyPcdDatabaseAutoGenC = 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#
246def 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
264def 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#
283class 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#
359class 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#
381class 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#
445class 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
467class 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#
513class 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#
533class 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#
560class 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#
619def 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#
637def 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#
663def 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#
675def 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#
702def 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#
1024def 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#
1057def 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 Pcd in ReorderedDynPcdList:
1162        VoidStarTypeCurrSize = []
1163        i += 1
1164        CName = Pcd.TokenCName
1165        TokenSpaceGuidCName = Pcd.TokenSpaceGuidCName
1166
1167        for PcdItem in GlobalData.MixedPcd:
1168            if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
1169                CName = PcdItem[0]
1170
1171        if GlobalData.BuildOptionPcd:
1172            for PcdItem in GlobalData.BuildOptionPcd:
1173                if (Pcd.TokenSpaceGuidCName, CName) == (PcdItem[0], PcdItem[1]):
1174                    Pcd.DefaultValue = PcdItem[2]
1175                    break
1176
1177        EdkLogger.debug(EdkLogger.DEBUG_3, "PCD: %s %s (%s : %s)" % (CName, TokenSpaceGuidCName, Pcd.Phase, Phase))
1178
1179        if Pcd.Phase == 'PEI':
1180            NumberOfPeiLocalTokens += 1
1181        if Pcd.Phase == 'DXE':
1182            NumberOfDxeLocalTokens += 1
1183        if Pcd.Phase != Phase:
1184            continue
1185
1186        #
1187        # TODO: need GetGuidValue() definition
1188        #
1189        TokenSpaceGuidStructure = Pcd.TokenSpaceGuidValue
1190        TokenSpaceGuid = GuidStructureStringToGuidValueName(TokenSpaceGuidStructure)
1191        if Pcd.Type in gDynamicExPcd:
1192            if TokenSpaceGuid not in GuidList:
1193                GuidList += [TokenSpaceGuid]
1194                Dict['GUID_STRUCTURE'].append(TokenSpaceGuidStructure)
1195            NumberOfExTokens += 1
1196
1197        ValueList = []
1198        DbValueList = []
1199        StringHeadOffsetList = []
1200        StringDbOffsetList = []
1201        VpdHeadOffsetList = []
1202        VpdDbOffsetList = []
1203        VariableHeadValueList = []
1204        VariableDbValueList = []
1205        Pcd.InitString = 'UNINIT'
1206
1207        if Pcd.DatumType == 'VOID*':
1208            if Pcd.Type not in ["DynamicVpd", "DynamicExVpd"]:
1209                Pcd.TokenTypeList = ['PCD_TYPE_STRING']
1210            else:
1211                Pcd.TokenTypeList = []
1212        elif Pcd.DatumType == 'BOOLEAN':
1213            Pcd.TokenTypeList = ['PCD_DATUM_TYPE_UINT8_BOOLEAN']
1214        else:
1215            Pcd.TokenTypeList = ['PCD_DATUM_TYPE_' + Pcd.DatumType]
1216
1217        if len(Pcd.SkuInfoList) > 1:
1218            Pcd.TokenTypeList += ['PCD_TYPE_SKU_ENABLED']
1219            NumberOfSkuEnabledPcd += 1
1220
1221        SkuIndexTableTmp = []
1222        SkuIndexTableTmp.append(0)
1223        SkuIdIndex = 1
1224        VariableHeadList = []
1225        for SkuName in Pcd.SkuInfoList:
1226            Sku = Pcd.SkuInfoList[SkuName]
1227            SkuId = Sku.SkuId
1228            if SkuId == None or SkuId == '':
1229                continue
1230
1231            if (SkuId + 'U') not in Dict['SKUID_VALUE']:
1232                Dict['SKUID_VALUE'].append(SkuId + 'U')
1233
1234            SkuIndexTableTmp.append(SkuId+'U')
1235            SkuIdIndex += 1
1236
1237            if len(Sku.VariableName) > 0:
1238                VariableGuidStructure = Sku.VariableGuidValue
1239                VariableGuid = GuidStructureStringToGuidValueName(VariableGuidStructure)
1240                if Platform.Platform.VarCheckFlag:
1241                    var_check_obj = VAR_CHECK_PCD_VARIABLE_TAB(VariableGuidStructure, StringToArray(Sku.VariableName))
1242                    try:
1243                        var_check_obj.push_back(VAR_VALID_OBJECT_FACTORY.Get_valid_object(Pcd, Sku.VariableOffset))
1244                        VarAttr, _ = VariableAttributes.GetVarAttributes(Sku.VariableAttribute)
1245                        var_check_obj.SetAttributes(VarAttr)
1246                        var_check_obj.UpdateSize()
1247                        VarCheckTab.push_back(var_check_obj)
1248                    except Exception:
1249                        ValidInfo = ''
1250                        if Pcd.validateranges:
1251                            ValidInfo = Pcd.validateranges[0]
1252                        if Pcd.validlists:
1253                            ValidInfo = Pcd.validlists[0]
1254                        if ValidInfo:
1255                            EdkLogger.error("build", PCD_VALIDATION_INFO_ERROR,
1256                                                "The PCD '%s.%s' Validation information defined in DEC file has incorrect format." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1257                                                ExtraData = "[%s]" % str(ValidInfo))
1258                        else:
1259                            EdkLogger.error("build", PCD_VALIDATION_INFO_ERROR,
1260                                                "The PCD '%s.%s' Validation information defined in DEC file has incorrect format." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1261                Pcd.TokenTypeList += ['PCD_TYPE_HII']
1262                Pcd.InitString = 'INIT'
1263                # Store all variable names of one HII PCD under different SKU to stringTable
1264                # and calculate the VariableHeadStringIndex
1265                if SkuIdIndex - 2 == 0:
1266                    for SkuName2 in Pcd.SkuInfoList:
1267                        SkuInfo = Pcd.SkuInfoList[SkuName2]
1268                        if SkuInfo.SkuId == None or SkuInfo.SkuId == '':
1269                            continue
1270                        VariableNameStructure = StringToArray(SkuInfo.VariableName)
1271                        if VariableNameStructure not in Dict['STRING_TABLE_VALUE']:
1272                            Dict['STRING_TABLE_CNAME'].append(CName)
1273                            Dict['STRING_TABLE_GUID'].append(TokenSpaceGuid)
1274                            if StringTableIndex == 0:
1275                                Dict['STRING_TABLE_INDEX'].append('')
1276                            else:
1277                                Dict['STRING_TABLE_INDEX'].append('_%d' % StringTableIndex)
1278                            VarNameSize = len(VariableNameStructure.replace(',',' ').split())
1279                            Dict['STRING_TABLE_LENGTH'].append(VarNameSize )
1280                            Dict['STRING_TABLE_VALUE'].append(VariableNameStructure)
1281                            StringHeadOffsetList.append(str(StringTableSize) + 'U')
1282                            VarStringDbOffsetList = []
1283                            VarStringDbOffsetList.append(StringTableSize)
1284                            Dict['STRING_DB_VALUE'].append(VarStringDbOffsetList)
1285                            StringTableIndex += 1
1286                            StringTableSize += len(VariableNameStructure.replace(',',' ').split())
1287                        VariableHeadStringIndex = 0
1288                        for Index in range(Dict['STRING_TABLE_VALUE'].index(VariableNameStructure)):
1289                            VariableHeadStringIndex += Dict['STRING_TABLE_LENGTH'][Index]
1290                        VariableHeadList.append(VariableHeadStringIndex)
1291
1292                VariableHeadStringIndex = VariableHeadList[SkuIdIndex - 2]
1293                # store VariableGuid to GuidTable and get the VariableHeadGuidIndex
1294
1295                if VariableGuid not in GuidList:
1296                    GuidList += [VariableGuid]
1297                    Dict['GUID_STRUCTURE'].append(VariableGuidStructure)
1298                VariableHeadGuidIndex = GuidList.index(VariableGuid)
1299
1300                if "PCD_TYPE_STRING" in Pcd.TokenTypeList:
1301                    VariableHeadValueList.append('%dU, offsetof(%s_PCD_DATABASE, Init.%s_%s), %dU, %sU' %
1302                                                 (VariableHeadStringIndex, Phase, CName, TokenSpaceGuid,
1303                                                 VariableHeadGuidIndex, Sku.VariableOffset))
1304                else:
1305                    VariableHeadValueList.append('%dU, offsetof(%s_PCD_DATABASE, Init.%s_%s_VariableDefault_%s), %dU, %sU' %
1306                                                 (VariableHeadStringIndex, Phase, CName, TokenSpaceGuid, SkuIdIndex,
1307                                                 VariableHeadGuidIndex, Sku.VariableOffset))
1308                Dict['VARDEF_CNAME_'+Pcd.DatumType].append(CName)
1309                Dict['VARDEF_GUID_'+Pcd.DatumType].append(TokenSpaceGuid)
1310                Dict['VARDEF_SKUID_'+Pcd.DatumType].append(SkuIdIndex)
1311                if "PCD_TYPE_STRING" in  Pcd.TokenTypeList:
1312                    Dict['VARDEF_VALUE_' + Pcd.DatumType].append("%s_%s[%d]" % (Pcd.TokenCName, TokenSpaceGuid, SkuIdIndex))
1313                else:
1314                    #
1315                    # ULL (for UINT64) or U(other integer type) should be append to avoid
1316                    # warning under linux building environment.
1317                    #
1318                    Dict['VARDEF_DB_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue)
1319
1320                    if Pcd.DatumType == "UINT64":
1321                        Dict['VARDEF_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue + "ULL")
1322                    elif Pcd.DatumType in ("UINT32", "UINT16", "UINT8"):
1323                        Dict['VARDEF_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue + "U")
1324                    elif Pcd.DatumType == "BOOLEAN":
1325                        if eval(Sku.HiiDefaultValue) in [1,0]:
1326                            Dict['VARDEF_VALUE_'+Pcd.DatumType].append(str(eval(Sku.HiiDefaultValue)) + "U")
1327                    else:
1328                        Dict['VARDEF_VALUE_'+Pcd.DatumType].append(Sku.HiiDefaultValue)
1329
1330                # construct the VariableHeader value
1331                if "PCD_TYPE_STRING" in Pcd.TokenTypeList:
1332                    VariableHeadValueList.append('%dU, %dU, %sU, offsetof(%s_PCD_DATABASE, Init.%s_%s)' %
1333                                                 (VariableHeadGuidIndex, VariableHeadStringIndex, Sku.VariableOffset,
1334                                                  Phase, CName, TokenSpaceGuid))
1335                    # the Pcd default value will be filled later on
1336                    VariableOffset = len(Dict['STRING_DB_VALUE'])
1337                    VariableRefTable = Dict['STRING_DB_VALUE']
1338                else:
1339                    VariableHeadValueList.append('%dU, %dU, %sU, offsetof(%s_PCD_DATABASE, Init.%s_%s_VariableDefault_%s)' %
1340                                                 (VariableHeadGuidIndex, VariableHeadStringIndex, Sku.VariableOffset,
1341                                                  Phase, CName, TokenSpaceGuid, SkuIdIndex))
1342                    # the Pcd default value was filled before
1343                    VariableOffset = len(Dict['VARDEF_DB_VALUE_' + Pcd.DatumType]) - 1
1344                    VariableRefTable = Dict['VARDEF_DB_VALUE_' + Pcd.DatumType]
1345                VariableDbValueList.append([VariableHeadGuidIndex, VariableHeadStringIndex, Sku.VariableOffset, VariableOffset, VariableRefTable, Sku.VariableAttribute])
1346
1347            elif Sku.VpdOffset != '':
1348                Pcd.TokenTypeList += ['PCD_TYPE_VPD']
1349                Pcd.InitString = 'INIT'
1350                VpdHeadOffsetList.append(str(Sku.VpdOffset) + 'U')
1351                VpdDbOffsetList.append(Sku.VpdOffset)
1352                # Also add the VOID* string of VPD PCD to SizeTable
1353                if Pcd.DatumType == 'VOID*':
1354                    NumberOfSizeItems += 1
1355                    # For VPD type of PCD, its current size is equal to its MAX size.
1356                    VoidStarTypeCurrSize = [str(Pcd.MaxDatumSize) + 'U']
1357                continue
1358
1359            if Pcd.DatumType == 'VOID*':
1360                Pcd.TokenTypeList += ['PCD_TYPE_STRING']
1361                Pcd.InitString = 'INIT'
1362                if Sku.HiiDefaultValue != '' and Sku.DefaultValue == '':
1363                    Sku.DefaultValue = Sku.HiiDefaultValue
1364                if Sku.DefaultValue != '':
1365                    NumberOfSizeItems += 1
1366                    Dict['STRING_TABLE_CNAME'].append(CName)
1367                    Dict['STRING_TABLE_GUID'].append(TokenSpaceGuid)
1368
1369                    if StringTableIndex == 0:
1370                        Dict['STRING_TABLE_INDEX'].append('')
1371                    else:
1372                        Dict['STRING_TABLE_INDEX'].append('_%d' % StringTableIndex)
1373                    if Sku.DefaultValue[0] == 'L':
1374                        DefaultValueBinStructure = StringToArray(Sku.DefaultValue)
1375                        Size = len(DefaultValueBinStructure.replace(',',' ').split())
1376                        Dict['STRING_TABLE_VALUE'].append(DefaultValueBinStructure)
1377                    elif Sku.DefaultValue[0] == '"':
1378                        DefaultValueBinStructure = StringToArray(Sku.DefaultValue)
1379                        Size = len(Sku.DefaultValue) - 2 + 1
1380                        Dict['STRING_TABLE_VALUE'].append(DefaultValueBinStructure)
1381                    elif Sku.DefaultValue[0] == '{':
1382                        DefaultValueBinStructure = StringToArray(Sku.DefaultValue)
1383                        Size = len(Sku.DefaultValue.split(","))
1384                        Dict['STRING_TABLE_VALUE'].append(DefaultValueBinStructure)
1385
1386                    StringHeadOffsetList.append(str(StringTableSize) + 'U')
1387                    StringDbOffsetList.append(StringTableSize)
1388                    if Pcd.MaxDatumSize != '':
1389                        MaxDatumSize = int(Pcd.MaxDatumSize, 0)
1390                        if MaxDatumSize < Size:
1391                            EdkLogger.error("build", AUTOGEN_ERROR,
1392                                            "The maximum size of VOID* type PCD '%s.%s' is less than its actual size occupied." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1393                                            ExtraData="[%s]" % str(Platform))
1394                    else:
1395                        MaxDatumSize = Size
1396                    StringTabLen = MaxDatumSize
1397                    if StringTabLen % 2:
1398                        StringTabLen += 1
1399                    if Sku.VpdOffset == '':
1400                        VoidStarTypeCurrSize.append(str(Size) + 'U')
1401                    Dict['STRING_TABLE_LENGTH'].append(StringTabLen)
1402                    StringTableIndex += 1
1403                    StringTableSize += (StringTabLen)
1404            else:
1405                if "PCD_TYPE_HII" not in Pcd.TokenTypeList:
1406                    Pcd.TokenTypeList += ['PCD_TYPE_DATA']
1407                    if Sku.DefaultValue == 'TRUE':
1408                        Pcd.InitString = 'INIT'
1409                    else:
1410                        if int(Sku.DefaultValue, 0) != 0:
1411                            Pcd.InitString = 'INIT'
1412                #
1413                # For UNIT64 type PCD's value, ULL should be append to avoid
1414                # warning under linux building environment.
1415                #
1416                if Pcd.DatumType == "UINT64":
1417                    ValueList.append(Sku.DefaultValue + "ULL")
1418                elif Pcd.DatumType in ("UINT32", "UINT16", "UINT8"):
1419                    ValueList.append(Sku.DefaultValue + "U")
1420                elif Pcd.DatumType == "BOOLEAN":
1421                    if Sku.DefaultValue in ["1", "0"]:
1422                        ValueList.append(Sku.DefaultValue + "U")
1423                else:
1424                    ValueList.append(Sku.DefaultValue)
1425
1426                DbValueList.append(Sku.DefaultValue)
1427
1428        Pcd.TokenTypeList = list(set(Pcd.TokenTypeList))
1429        if Pcd.DatumType == 'VOID*':
1430            Dict['SIZE_TABLE_CNAME'].append(CName)
1431            Dict['SIZE_TABLE_GUID'].append(TokenSpaceGuid)
1432            Dict['SIZE_TABLE_MAXIMUM_LENGTH'].append(str(Pcd.MaxDatumSize) + 'U')
1433            Dict['SIZE_TABLE_CURRENT_LENGTH'].append(VoidStarTypeCurrSize)
1434
1435
1436        SkuIndexTableTmp[0] = len(SkuIndexTableTmp) - 1
1437        if len(Pcd.SkuInfoList) > 1:
1438            Dict['SKU_INDEX_VALUE'].append(SkuIndexTableTmp)
1439
1440        if 'PCD_TYPE_HII' in Pcd.TokenTypeList:
1441            Dict['VARIABLE_HEAD_CNAME_DECL'].append(CName)
1442            Dict['VARIABLE_HEAD_GUID_DECL'].append(TokenSpaceGuid)
1443            Dict['VARIABLE_HEAD_NUMSKUS_DECL'].append(len(Pcd.SkuInfoList))
1444            Dict['VARIABLE_HEAD_VALUE'].append('{ %s }\n' % ' },\n    { '.join(VariableHeadValueList))
1445            Dict['VARDEF_HEADER'].append('_Variable_Header')
1446            Dict['VARIABLE_DB_VALUE'].append(VariableDbValueList)
1447        else:
1448            Dict['VARDEF_HEADER'].append('')
1449        if 'PCD_TYPE_VPD' in Pcd.TokenTypeList:
1450            Dict['VPD_HEAD_CNAME_DECL'].append(CName)
1451            Dict['VPD_HEAD_GUID_DECL'].append(TokenSpaceGuid)
1452            Dict['VPD_HEAD_NUMSKUS_DECL'].append(len(Pcd.SkuInfoList))
1453            Dict['VPD_HEAD_VALUE'].append('{ %s }' % ' }, { '.join(VpdHeadOffsetList))
1454            Dict['VPD_DB_VALUE'].append(VpdDbOffsetList)
1455        if 'PCD_TYPE_STRING' in Pcd.TokenTypeList:
1456            Dict['STRING_HEAD_CNAME_DECL'].append(CName)
1457            Dict['STRING_HEAD_GUID_DECL'].append(TokenSpaceGuid)
1458            Dict['STRING_HEAD_NUMSKUS_DECL'].append(len(Pcd.SkuInfoList))
1459            Dict['STRING_HEAD_VALUE'].append(', '.join(StringHeadOffsetList))
1460            Dict['STRING_DB_VALUE'].append(StringDbOffsetList)
1461            PCD_STRING_INDEX_MAP[len(Dict['STRING_HEAD_CNAME_DECL']) -1 ] = len(Dict['STRING_DB_VALUE']) -1
1462        if 'PCD_TYPE_DATA' in Pcd.TokenTypeList:
1463            Dict[Pcd.InitString+'_CNAME_DECL_'+Pcd.DatumType].append(CName)
1464            Dict[Pcd.InitString+'_GUID_DECL_'+Pcd.DatumType].append(TokenSpaceGuid)
1465            Dict[Pcd.InitString+'_NUMSKUS_DECL_'+Pcd.DatumType].append(len(Pcd.SkuInfoList))
1466            if Pcd.InitString == 'UNINIT':
1467                Dict['PCD_DATABASE_UNINIT_EMPTY'] = ''
1468            else:
1469                Dict[Pcd.InitString+'_VALUE_'+Pcd.DatumType].append(', '.join(ValueList))
1470                Dict[Pcd.InitString+'_DB_VALUE_'+Pcd.DatumType].append(DbValueList)
1471
1472    if Phase == 'PEI':
1473        NumberOfLocalTokens = NumberOfPeiLocalTokens
1474    if Phase == 'DXE':
1475        NumberOfLocalTokens = NumberOfDxeLocalTokens
1476
1477    Dict['TOKEN_INIT']       = ['' for x in range(NumberOfLocalTokens)]
1478    Dict['TOKEN_CNAME']      = ['' for x in range(NumberOfLocalTokens)]
1479    Dict['TOKEN_GUID']       = ['' for x in range(NumberOfLocalTokens)]
1480    Dict['TOKEN_TYPE']       = ['' for x in range(NumberOfLocalTokens)]
1481    Dict['LOCAL_TOKEN_NUMBER_DB_VALUE'] = ['' for x in range(NumberOfLocalTokens)]
1482    Dict['PCD_CNAME']        = ['' for x in range(NumberOfLocalTokens)]
1483    Dict['PCD_TOKENSPACE_MAP'] = ['' for x in range(NumberOfLocalTokens)]
1484    Dict['PCD_CNAME_LENGTH'] = [0 for x in range(NumberOfLocalTokens)]
1485    SkuEnablePcdIndex = 0
1486    for Pcd in ReorderedDynPcdList:
1487        CName = Pcd.TokenCName
1488        TokenSpaceGuidCName = Pcd.TokenSpaceGuidCName
1489        if Pcd.Phase != Phase:
1490            continue
1491
1492        TokenSpaceGuid = GuidStructureStringToGuidValueName(Pcd.TokenSpaceGuidValue) #(Platform.PackageList, TokenSpaceGuidCName))
1493        GeneratedTokenNumber = Platform.PcdTokenNumber[CName, TokenSpaceGuidCName] - 1
1494        if Phase == 'DXE':
1495            GeneratedTokenNumber -= NumberOfPeiLocalTokens
1496
1497        if len(Pcd.SkuInfoList) > 1:
1498            Dict['PCD_ORDER_TOKEN_NUMBER_MAP'][GeneratedTokenNumber] = SkuEnablePcdIndex
1499            SkuEnablePcdIndex += 1
1500
1501        for PcdItem in GlobalData.MixedPcd:
1502            if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
1503                CName = PcdItem[0]
1504
1505        if GlobalData.BuildOptionPcd:
1506            for PcdItem in GlobalData.BuildOptionPcd:
1507                if (Pcd.TokenSpaceGuidCName, CName) == (PcdItem[0], PcdItem[1]):
1508                    Pcd.DefaultValue = PcdItem[2]
1509                    break
1510
1511        EdkLogger.debug(EdkLogger.DEBUG_1, "PCD = %s.%s" % (CName, TokenSpaceGuidCName))
1512        EdkLogger.debug(EdkLogger.DEBUG_1, "phase = %s" % Phase)
1513        EdkLogger.debug(EdkLogger.DEBUG_1, "GeneratedTokenNumber = %s" % str(GeneratedTokenNumber))
1514
1515        #
1516        # following four Dict items hold the information for LocalTokenNumberTable
1517        #
1518        Dict['TOKEN_INIT'][GeneratedTokenNumber] = 'Init'
1519        if Pcd.InitString == 'UNINIT':
1520            Dict['TOKEN_INIT'][GeneratedTokenNumber] = 'Uninit'
1521
1522        Dict['TOKEN_CNAME'][GeneratedTokenNumber] = CName
1523        Dict['TOKEN_GUID'][GeneratedTokenNumber] = TokenSpaceGuid
1524        Dict['TOKEN_TYPE'][GeneratedTokenNumber] = ' | '.join(Pcd.TokenTypeList)
1525
1526        if Platform.Platform.PcdInfoFlag:
1527            TokenSpaceGuidCNameArray = StringToArray('"' + TokenSpaceGuidCName + '"' )
1528            if TokenSpaceGuidCNameArray not in Dict['PCD_TOKENSPACE']:
1529                Dict['PCD_TOKENSPACE'].append(TokenSpaceGuidCNameArray)
1530                Dict['PCD_TOKENSPACE_LENGTH'].append( len(TokenSpaceGuidCNameArray.split(",")) )
1531            Dict['PCD_TOKENSPACE_MAP'][GeneratedTokenNumber] = Dict['PCD_TOKENSPACE'].index(TokenSpaceGuidCNameArray)
1532            CNameBinArray = StringToArray('"' + CName + '"' )
1533            Dict['PCD_CNAME'][GeneratedTokenNumber] = CNameBinArray
1534
1535            Dict['PCD_CNAME_LENGTH'][GeneratedTokenNumber] = len(CNameBinArray.split(","))
1536
1537
1538        Pcd.TokenTypeList = list(set(Pcd.TokenTypeList))
1539
1540        # search the Offset and Table, used by LocalTokenNumberTableOffset
1541        if 'PCD_TYPE_HII' in Pcd.TokenTypeList:
1542            # Find index by CName, TokenSpaceGuid
1543            Offset = GetMatchedIndex(CName, Dict['VARIABLE_HEAD_CNAME_DECL'], TokenSpaceGuid, Dict['VARIABLE_HEAD_GUID_DECL'])
1544            assert(Offset != -1)
1545            Table = Dict['VARIABLE_DB_VALUE']
1546        if 'PCD_TYPE_VPD' in Pcd.TokenTypeList:
1547            Offset = GetMatchedIndex(CName, Dict['VPD_HEAD_CNAME_DECL'], TokenSpaceGuid, Dict['VPD_HEAD_GUID_DECL'])
1548            assert(Offset != -1)
1549            Table = Dict['VPD_DB_VALUE']
1550        if 'PCD_TYPE_STRING' in Pcd.TokenTypeList and 'PCD_TYPE_HII' not in Pcd.TokenTypeList:
1551            # Find index by CName, TokenSpaceGuid
1552            Offset = GetMatchedIndex(CName, Dict['STRING_HEAD_CNAME_DECL'], TokenSpaceGuid, Dict['STRING_HEAD_GUID_DECL'])
1553            Offset = PCD_STRING_INDEX_MAP[Offset]
1554            assert(Offset != -1)
1555            Table = Dict['STRING_DB_VALUE']
1556        if 'PCD_TYPE_DATA' in Pcd.TokenTypeList:
1557            # need to store whether it is in init table or not
1558            Offset = GetMatchedIndex(CName, Dict[Pcd.InitString+'_CNAME_DECL_'+Pcd.DatumType], TokenSpaceGuid, Dict[Pcd.InitString+'_GUID_DECL_'+Pcd.DatumType])
1559            assert(Offset != -1)
1560            if Pcd.InitString == 'UNINIT':
1561                Table =  Dict[Pcd.InitString+'_GUID_DECL_'+Pcd.DatumType]
1562            else:
1563                Table = Dict[Pcd.InitString+'_DB_VALUE_'+Pcd.DatumType]
1564        Dict['LOCAL_TOKEN_NUMBER_DB_VALUE'][GeneratedTokenNumber] = (Offset, Table)
1565
1566        #
1567        # Update VARDEF_HEADER
1568        #
1569        if 'PCD_TYPE_HII' in Pcd.TokenTypeList:
1570            Dict['VARDEF_HEADER'][GeneratedTokenNumber] = '_Variable_Header'
1571        else:
1572            Dict['VARDEF_HEADER'][GeneratedTokenNumber] = ''
1573
1574
1575        if Pcd.Type in gDynamicExPcd:
1576
1577            if Phase == 'DXE':
1578                GeneratedTokenNumber += NumberOfPeiLocalTokens
1579            #
1580            # Per, PCD architecture specification, PCD Token Number is 1 based and 0 is defined as invalid token number.
1581            # For each EX type PCD, a PCD Token Number is assigned. When the
1582            # PCD Driver/PEIM map EX_GUID and EX_TOKEN_NUMBER to the PCD Token Number,
1583            # the non-EX Protocol/PPI interface can be called to get/set the value. This assumption is made by
1584            # Pcd Driver/PEIM in MdeModulePkg.
1585            # Therefore, 1 is added to GeneratedTokenNumber to generate a PCD Token Number before being inserted
1586            # to the EXMAPPING_TABLE.
1587            #
1588
1589
1590            Dict['EXMAPPING_TABLE_EXTOKEN'].append(str(Pcd.TokenValue) + 'U')
1591            Dict['EXMAPPING_TABLE_LOCAL_TOKEN'].append(str(GeneratedTokenNumber + 1) + 'U')
1592            Dict['EXMAPPING_TABLE_GUID_INDEX'].append(str(GuidList.index(TokenSpaceGuid)) + 'U')
1593
1594    if Platform.Platform.PcdInfoFlag:
1595        for index in range(len(Dict['PCD_TOKENSPACE_MAP'])):
1596            TokenSpaceIndex = StringTableSize
1597            for i in range(Dict['PCD_TOKENSPACE_MAP'][index]):
1598                TokenSpaceIndex += Dict['PCD_TOKENSPACE_LENGTH'][i]
1599            Dict['PCD_TOKENSPACE_OFFSET'].append(TokenSpaceIndex)
1600        for index in range(len(Dict['PCD_TOKENSPACE'])):
1601            StringTableSize += Dict['PCD_TOKENSPACE_LENGTH'][index]
1602            StringTableIndex += 1
1603        for index in range(len(Dict['PCD_CNAME'])):
1604            Dict['PCD_CNAME_OFFSET'].append(StringTableSize)
1605            Dict['PCD_NAME_OFFSET'].append(Dict['PCD_TOKENSPACE_OFFSET'][index])
1606            Dict['PCD_NAME_OFFSET'].append(StringTableSize)
1607            StringTableSize += Dict['PCD_CNAME_LENGTH'][index]
1608            StringTableIndex += 1
1609    if GuidList != []:
1610        Dict['GUID_TABLE_EMPTY'] = 'FALSE'
1611        Dict['GUID_TABLE_SIZE'] = str(len(GuidList)) + 'U'
1612    else:
1613        Dict['GUID_STRUCTURE'] = [GuidStringToGuidStructureString('00000000-0000-0000-0000-000000000000')]
1614
1615    if StringTableIndex == 0:
1616        Dict['STRING_TABLE_INDEX'].append('')
1617        Dict['STRING_TABLE_LENGTH'].append(1)
1618        Dict['STRING_TABLE_CNAME'].append('')
1619        Dict['STRING_TABLE_GUID'].append('')
1620        Dict['STRING_TABLE_VALUE'].append('{ 0 }')
1621    else:
1622        Dict['STRING_TABLE_EMPTY'] = 'FALSE'
1623        Dict['STRING_TABLE_SIZE'] = str(StringTableSize) + 'U'
1624
1625    if Dict['SIZE_TABLE_CNAME'] == []:
1626        Dict['SIZE_TABLE_CNAME'].append('')
1627        Dict['SIZE_TABLE_GUID'].append('')
1628        Dict['SIZE_TABLE_CURRENT_LENGTH'].append(['0U'])
1629        Dict['SIZE_TABLE_MAXIMUM_LENGTH'].append('0U')
1630
1631    if NumberOfLocalTokens != 0:
1632        Dict['DATABASE_EMPTY']                = 'FALSE'
1633        Dict['LOCAL_TOKEN_NUMBER_TABLE_SIZE'] = NumberOfLocalTokens
1634        Dict['LOCAL_TOKEN_NUMBER']            = NumberOfLocalTokens
1635
1636    if NumberOfExTokens != 0:
1637        Dict['EXMAP_TABLE_EMPTY']    = 'FALSE'
1638        Dict['EXMAPPING_TABLE_SIZE'] = str(NumberOfExTokens) + 'U'
1639        Dict['EX_TOKEN_NUMBER']      = str(NumberOfExTokens) + 'U'
1640    else:
1641        Dict['EXMAPPING_TABLE_EXTOKEN'].append('0U')
1642        Dict['EXMAPPING_TABLE_LOCAL_TOKEN'].append('0U')
1643        Dict['EXMAPPING_TABLE_GUID_INDEX'].append('0U')
1644
1645    if NumberOfSizeItems != 0:
1646        Dict['SIZE_TABLE_SIZE'] = str(NumberOfSizeItems * 2) + 'U'
1647
1648    if NumberOfSkuEnabledPcd != 0:
1649        Dict['SKU_HEAD_SIZE'] = str(NumberOfSkuEnabledPcd) + 'U'
1650
1651    for AvailableSkuNumber in SkuObj.SkuIdNumberSet:
1652        if AvailableSkuNumber not in Dict['SKUID_VALUE']:
1653            Dict['SKUID_VALUE'].append(AvailableSkuNumber)
1654    Dict['SKUID_VALUE'][0] = len(Dict['SKUID_VALUE']) - 1
1655
1656    AutoGenH.Append(gPcdDatabaseAutoGenH.Replace(Dict))
1657    if NumberOfLocalTokens == 0:
1658        AutoGenC.Append(gEmptyPcdDatabaseAutoGenC.Replace(Dict))
1659    else:
1660        #
1661        # Update Size Table to the right order, it should be same with LocalTokenNumberTable
1662        #
1663        SizeCNameTempList = []
1664        SizeGuidTempList = []
1665        SizeCurLenTempList = []
1666        SizeMaxLenTempList = []
1667        ReOrderFlag = True
1668
1669        if len(Dict['SIZE_TABLE_CNAME']) == 1:
1670            if not (Dict['SIZE_TABLE_CNAME'][0] and Dict['SIZE_TABLE_GUID'][0]):
1671                ReOrderFlag = False
1672
1673        if ReOrderFlag:
1674            for Count in range(len(Dict['TOKEN_CNAME'])):
1675                for Count1 in range(len(Dict['SIZE_TABLE_CNAME'])):
1676                    if Dict['TOKEN_CNAME'][Count] == Dict['SIZE_TABLE_CNAME'][Count1] and \
1677                        Dict['TOKEN_GUID'][Count] == Dict['SIZE_TABLE_GUID'][Count1]:
1678                        SizeCNameTempList.append(Dict['SIZE_TABLE_CNAME'][Count1])
1679                        SizeGuidTempList.append(Dict['SIZE_TABLE_GUID'][Count1])
1680                        SizeCurLenTempList.append(Dict['SIZE_TABLE_CURRENT_LENGTH'][Count1])
1681                        SizeMaxLenTempList.append(Dict['SIZE_TABLE_MAXIMUM_LENGTH'][Count1])
1682
1683            for Count in range(len(Dict['SIZE_TABLE_CNAME'])):
1684                Dict['SIZE_TABLE_CNAME'][Count] = SizeCNameTempList[Count]
1685                Dict['SIZE_TABLE_GUID'][Count] = SizeGuidTempList[Count]
1686                Dict['SIZE_TABLE_CURRENT_LENGTH'][Count] = SizeCurLenTempList[Count]
1687                Dict['SIZE_TABLE_MAXIMUM_LENGTH'][Count] = SizeMaxLenTempList[Count]
1688
1689        AutoGenC.Append(gPcdDatabaseAutoGenC.Replace(Dict))
1690
1691    if Platform.Platform.VarCheckFlag:
1692        dest = os.path.join(Platform.BuildDir, 'FV')
1693        VarCheckTab.dump(dest, Phase)
1694    Buffer = BuildExDataBase(Dict)
1695    return AutoGenH, AutoGenC, Buffer
1696
1697def GetOrderedDynamicPcdList(DynamicPcdList, PcdTokenNumberList):
1698    ReorderedDyPcdList = [None for i in range(len(DynamicPcdList))]
1699    for Pcd in DynamicPcdList:
1700        if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in PcdTokenNumberList:
1701            ReorderedDyPcdList[PcdTokenNumberList[Pcd.TokenCName, Pcd.TokenSpaceGuidCName]-1] = Pcd
1702    return ReorderedDyPcdList
1703
1704