]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Workspace/MetaFileTable.py
Fixed potential security issue introduced by SmramCpuNvs variable.
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileTable.py
CommitLineData
30fdf114
LG
1## @file\r
2# This file is used to create/update/query/erase a meta file table\r
3#\r
82a6a960 4# Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5# This program and the accompanying materials\r
30fdf114
LG
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
14##\r
15# Import Modules\r
16#\r
0d2711a6
LG
17import uuid\r
18\r
30fdf114 19import Common.EdkLogger as EdkLogger\r
0d2711a6
LG
20\r
21from MetaDataTable import Table, TableFile\r
30fdf114 22from MetaDataTable import ConvertToSqlString\r
0d2711a6
LG
23from CommonDataClass.DataClass import MODEL_FILE_DSC, MODEL_FILE_DEC, MODEL_FILE_INF, \\r
24 MODEL_FILE_OTHERS\r
30fdf114 25\r
0d2711a6 26class MetaFileTable(Table):\r
30fdf114 27 # TRICK: use file ID as the part before '.'\r
0d2711a6
LG
28 _ID_STEP_ = 0.00000001\r
29 _ID_MAX_ = 0.99999999\r
30\r
31 ## Constructor\r
32 def __init__(self, Cursor, MetaFile, FileType, Temporary):\r
33 self.MetaFile = MetaFile\r
34\r
35 self._FileIndexTable = TableFile(Cursor)\r
36 self._FileIndexTable.Create(False)\r
37\r
38 FileId = self._FileIndexTable.GetFileId(MetaFile)\r
39 if not FileId:\r
40 FileId = self._FileIndexTable.InsertFile(MetaFile, FileType)\r
41\r
42 if Temporary:\r
43 TableName = "_%s_%s_%s" % (FileType, FileId, uuid.uuid4().hex)\r
44 else:\r
45 TableName = "_%s_%s" % (FileType, FileId)\r
46\r
47 #Table.__init__(self, Cursor, TableName, FileId, False)\r
48 Table.__init__(self, Cursor, TableName, FileId, Temporary)\r
49 self.Create(not self.IsIntegrity())\r
50\r
51 def IsIntegrity(self):\r
52 try:\r
64b2609f 53 TimeStamp = self.MetaFile.TimeStamp\r
0d2711a6
LG
54 Result = self.Cur.execute("select ID from %s where ID<0" % (self.Table)).fetchall()\r
55 if not Result:\r
64b2609f
LG
56 # update the timestamp in database\r
57 self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp) \r
0d2711a6
LG
58 return False\r
59\r
0d2711a6
LG
60 if TimeStamp != self._FileIndexTable.GetFileTimeStamp(self.IdBase):\r
61 # update the timestamp in database\r
62 self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp)\r
63 return False\r
64 except Exception, Exc:\r
65 EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc))\r
66 return False\r
67 return True\r
68\r
69## Python class representation of table storing module data\r
70class ModuleTable(MetaFileTable):\r
30fdf114
LG
71 _ID_STEP_ = 0.00000001\r
72 _ID_MAX_ = 0.99999999\r
73 _COLUMN_ = '''\r
74 ID REAL PRIMARY KEY,\r
75 Model INTEGER NOT NULL,\r
76 Value1 TEXT NOT NULL,\r
77 Value2 TEXT,\r
78 Value3 TEXT,\r
79 Scope1 TEXT,\r
80 Scope2 TEXT,\r
81 BelongsToItem REAL NOT NULL,\r
82 StartLine INTEGER NOT NULL,\r
83 StartColumn INTEGER NOT NULL,\r
84 EndLine INTEGER NOT NULL,\r
85 EndColumn INTEGER NOT NULL,\r
86 Enabled INTEGER DEFAULT 0\r
87 '''\r
88 # used as table end flag, in case the changes to database is not committed to db file\r
89 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1"\r
90\r
91 ## Constructor\r
0d2711a6
LG
92 def __init__(self, Cursor, MetaFile, Temporary):\r
93 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_INF, Temporary)\r
30fdf114
LG
94\r
95 ## Insert a record into table Inf\r
96 #\r
97 # @param Model: Model of a Inf item\r
98 # @param Value1: Value1 of a Inf item\r
99 # @param Value2: Value2 of a Inf item\r
100 # @param Value3: Value3 of a Inf item\r
101 # @param Scope1: Arch of a Inf item\r
102 # @param Scope2 Platform os a Inf item\r
103 # @param BelongsToItem: The item belongs to which another item\r
104 # @param StartLine: StartLine of a Inf item\r
105 # @param StartColumn: StartColumn of a Inf item\r
106 # @param EndLine: EndLine of a Inf item\r
107 # @param EndColumn: EndColumn of a Inf item\r
108 # @param Enabled: If this item enabled\r
109 #\r
110 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',\r
111 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):\r
112 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))\r
113 return Table.Insert(\r
114 self, \r
115 Model, \r
116 Value1, \r
117 Value2, \r
118 Value3, \r
119 Scope1, \r
120 Scope2,\r
121 BelongsToItem, \r
122 StartLine, \r
123 StartColumn, \r
124 EndLine, \r
125 EndColumn, \r
126 Enabled\r
127 )\r
128\r
129 ## Query table\r
130 #\r
131 # @param Model: The Model of Record \r
132 # @param Arch: The Arch attribute of Record \r
133 # @param Platform The Platform attribute of Record \r
134 #\r
135 # @retval: A recordSet of all found records \r
136 #\r
e8a47801 137 def Query(self, Model, Arch=None, Platform=None, BelongsToItem=None):\r
30fdf114
LG
138 ConditionString = "Model=%s AND Enabled>=0" % Model\r
139 ValueString = "Value1,Value2,Value3,Scope1,Scope2,ID,StartLine"\r
140\r
141 if Arch != None and Arch != 'COMMON':\r
142 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch\r
143 if Platform != None and Platform != 'COMMON':\r
144 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Platform\r
e8a47801
LG
145 if BelongsToItem != None:\r
146 ConditionString += " AND BelongsToItem=%s" % BelongsToItem\r
30fdf114
LG
147\r
148 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)\r
149 return self.Exec(SqlCommand)\r
150\r
151## Python class representation of table storing package data\r
0d2711a6 152class PackageTable(MetaFileTable):\r
30fdf114
LG
153 _COLUMN_ = '''\r
154 ID REAL PRIMARY KEY,\r
155 Model INTEGER NOT NULL,\r
156 Value1 TEXT NOT NULL,\r
157 Value2 TEXT,\r
158 Value3 TEXT,\r
159 Scope1 TEXT,\r
160 Scope2 TEXT,\r
161 BelongsToItem REAL NOT NULL,\r
162 StartLine INTEGER NOT NULL,\r
163 StartColumn INTEGER NOT NULL,\r
164 EndLine INTEGER NOT NULL,\r
165 EndColumn INTEGER NOT NULL,\r
166 Enabled INTEGER DEFAULT 0\r
167 '''\r
168 # used as table end flag, in case the changes to database is not committed to db file\r
169 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1"\r
170\r
171 ## Constructor\r
0d2711a6
LG
172 def __init__(self, Cursor, MetaFile, Temporary):\r
173 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DEC, Temporary)\r
30fdf114
LG
174\r
175 ## Insert table\r
176 #\r
177 # Insert a record into table Dec\r
178 #\r
179 # @param Model: Model of a Dec item\r
180 # @param Value1: Value1 of a Dec item\r
181 # @param Value2: Value2 of a Dec item\r
182 # @param Value3: Value3 of a Dec item\r
183 # @param Scope1: Arch of a Dec item\r
184 # @param Scope2: Module type of a Dec item\r
185 # @param BelongsToItem: The item belongs to which another item\r
186 # @param StartLine: StartLine of a Dec item\r
187 # @param StartColumn: StartColumn of a Dec item\r
188 # @param EndLine: EndLine of a Dec item\r
189 # @param EndColumn: EndColumn of a Dec item\r
190 # @param Enabled: If this item enabled\r
191 #\r
192 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',\r
193 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):\r
194 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))\r
195 return Table.Insert(\r
196 self, \r
197 Model, \r
198 Value1, \r
199 Value2, \r
200 Value3, \r
201 Scope1, \r
202 Scope2,\r
203 BelongsToItem, \r
204 StartLine, \r
205 StartColumn, \r
206 EndLine, \r
207 EndColumn, \r
208 Enabled\r
209 )\r
210\r
211 ## Query table\r
212 #\r
213 # @param Model: The Model of Record \r
214 # @param Arch: The Arch attribute of Record \r
215 #\r
216 # @retval: A recordSet of all found records \r
217 #\r
218 def Query(self, Model, Arch=None):\r
219 ConditionString = "Model=%s AND Enabled>=0" % Model\r
220 ValueString = "Value1,Value2,Value3,Scope1,ID,StartLine"\r
221\r
222 if Arch != None and Arch != 'COMMON':\r
223 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch\r
224\r
225 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)\r
226 return self.Exec(SqlCommand)\r
227\r
82a6a960
BF
228 def GetValidExpression(self, TokenSpaceGuid, PcdCName):\r
229 SqlCommand = "select Value1 from %s WHERE Value2='%s' and Value3='%s'" % (self.Table, TokenSpaceGuid, PcdCName)\r
230 self.Cur.execute(SqlCommand)\r
231 validateranges = []\r
232 validlists = []\r
233 expressions = []\r
234 for row in self.Cur:\r
235 comment = row[0]\r
236 comment = comment.strip("#")\r
237 comment = comment.strip()\r
238 if comment.startswith("@ValidRange"):\r
239 comment = comment.replace("@ValidRange", "", 1)\r
240 validateranges.append(comment.split("|")[1].strip())\r
241 if comment.startswith("@ValidList"):\r
242 comment = comment.replace("@ValidList", "", 1)\r
243 validlists.append(comment.split("|")[1].strip())\r
244 if comment.startswith("@Expression"):\r
245 comment = comment.replace("@Expression", "", 1)\r
246 expressions.append(comment.split("|")[1].strip())\r
247 return set(validateranges), set(validlists), set(expressions)\r
30fdf114 248## Python class representation of table storing platform data\r
0d2711a6 249class PlatformTable(MetaFileTable):\r
30fdf114
LG
250 _COLUMN_ = '''\r
251 ID REAL PRIMARY KEY,\r
252 Model INTEGER NOT NULL,\r
253 Value1 TEXT NOT NULL,\r
254 Value2 TEXT,\r
255 Value3 TEXT,\r
256 Scope1 TEXT,\r
257 Scope2 TEXT,\r
258 BelongsToItem REAL NOT NULL,\r
259 FromItem REAL NOT NULL,\r
260 StartLine INTEGER NOT NULL,\r
261 StartColumn INTEGER NOT NULL,\r
262 EndLine INTEGER NOT NULL,\r
263 EndColumn INTEGER NOT NULL,\r
264 Enabled INTEGER DEFAULT 0\r
265 '''\r
266 # used as table end flag, in case the changes to database is not committed to db file\r
267 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1, -1"\r
268\r
269 ## Constructor\r
0d2711a6
LG
270 def __init__(self, Cursor, MetaFile, Temporary):\r
271 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DSC, Temporary)\r
30fdf114
LG
272\r
273 ## Insert table\r
274 #\r
275 # Insert a record into table Dsc\r
276 #\r
277 # @param Model: Model of a Dsc item\r
278 # @param Value1: Value1 of a Dsc item\r
279 # @param Value2: Value2 of a Dsc item\r
280 # @param Value3: Value3 of a Dsc item\r
281 # @param Scope1: Arch of a Dsc item\r
282 # @param Scope2: Module type of a Dsc item\r
283 # @param BelongsToItem: The item belongs to which another item\r
284 # @param FromItem: The item belongs to which dsc file\r
285 # @param StartLine: StartLine of a Dsc item\r
286 # @param StartColumn: StartColumn of a Dsc item\r
287 # @param EndLine: EndLine of a Dsc item\r
288 # @param EndColumn: EndColumn of a Dsc item\r
289 # @param Enabled: If this item enabled\r
290 #\r
291 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON', BelongsToItem=-1, \r
292 FromItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=1):\r
293 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))\r
294 return Table.Insert(\r
295 self, \r
296 Model, \r
297 Value1, \r
298 Value2, \r
299 Value3, \r
300 Scope1, \r
301 Scope2,\r
302 BelongsToItem, \r
303 FromItem,\r
304 StartLine, \r
305 StartColumn, \r
306 EndLine, \r
307 EndColumn, \r
308 Enabled\r
309 )\r
310\r
311 ## Query table\r
312 #\r
313 # @param Model: The Model of Record \r
314 # @param Scope1: Arch of a Dsc item\r
315 # @param Scope2: Module type of a Dsc item\r
316 # @param BelongsToItem: The item belongs to which another item\r
317 # @param FromItem: The item belongs to which dsc file\r
318 #\r
319 # @retval: A recordSet of all found records \r
320 #\r
321 def Query(self, Model, Scope1=None, Scope2=None, BelongsToItem=None, FromItem=None):\r
0d2711a6 322 ConditionString = "Model=%s AND Enabled>0" % Model\r
30fdf114
LG
323 ValueString = "Value1,Value2,Value3,Scope1,Scope2,ID,StartLine"\r
324\r
325 if Scope1 != None and Scope1 != 'COMMON':\r
326 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Scope1\r
327 if Scope2 != None and Scope2 != 'COMMON':\r
328 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Scope2\r
329\r
330 if BelongsToItem != None:\r
331 ConditionString += " AND BelongsToItem=%s" % BelongsToItem\r
332 else:\r
333 ConditionString += " AND BelongsToItem<0"\r
334\r
335 if FromItem != None:\r
336 ConditionString += " AND FromItem=%s" % FromItem\r
337\r
338 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)\r
339 return self.Exec(SqlCommand)\r
340\r
0d2711a6
LG
341## Factory class to produce different storage for different type of meta-file\r
342class MetaFileStorage(object):\r
343 _FILE_TABLE_ = {\r
344 MODEL_FILE_INF : ModuleTable,\r
345 MODEL_FILE_DEC : PackageTable,\r
346 MODEL_FILE_DSC : PlatformTable,\r
347 MODEL_FILE_OTHERS : MetaFileTable,\r
348 }\r
349\r
350 _FILE_TYPE_ = {\r
351 ".inf" : MODEL_FILE_INF,\r
352 ".dec" : MODEL_FILE_DEC,\r
353 ".dsc" : MODEL_FILE_DSC,\r
354 }\r
355\r
356 ## Constructor\r
357 def __new__(Class, Cursor, MetaFile, FileType=None, Temporary=False):\r
358 # no type given, try to find one\r
359 if not FileType:\r
360 if MetaFile.Type in self._FILE_TYPE_:\r
361 FileType = Class._FILE_TYPE_[MetaFile.Type]\r
362 else:\r
363 FileType = MODEL_FILE_OTHERS\r
364\r
365 # don't pass the type around if it's well known\r
366 if FileType == MODEL_FILE_OTHERS:\r
367 Args = (Cursor, MetaFile, FileType, Temporary)\r
368 else:\r
369 Args = (Cursor, MetaFile, Temporary)\r
370\r
371 # create the storage object and return it to caller\r
372 return Class._FILE_TABLE_[FileType](*Args)\r
373\r