hw/arm/virt-acpi-build: Add SPCR table
[qemu/ar7.git] / scripts / acpi_extract.py
blob10c1ffb368cbfc5994fe993071a10b1ee075fa4d
1 #!/usr/bin/python
2 # Copyright (C) 2011 Red Hat, Inc., Michael S. Tsirkin <mst@redhat.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, see <http://www.gnu.org/licenses/>.
17 # Process mixed ASL/AML listing (.lst file) produced by iasl -l
18 # Locate and execute ACPI_EXTRACT directives, output offset info
20 # Documentation of ACPI_EXTRACT_* directive tags:
22 # These directive tags output offset information from AML for BIOS runtime
23 # table generation.
24 # Each directive is of the form:
25 # ACPI_EXTRACT_<TYPE> <array_name> <Operator> (...)
26 # and causes the extractor to create an array
27 # named <array_name> with offset, in the generated AML,
28 # of an object of a given type in the following <Operator>.
30 # A directive must fit on a single code line.
32 # Object type in AML is verified, a mismatch causes a build failure.
34 # Directives and operators currently supported are:
35 # ACPI_EXTRACT_NAME_DWORD_CONST - extract a Dword Const object from Name()
36 # ACPI_EXTRACT_NAME_WORD_CONST - extract a Word Const object from Name()
37 # ACPI_EXTRACT_NAME_BYTE_CONST - extract a Byte Const object from Name()
38 # ACPI_EXTRACT_METHOD_STRING - extract a NameString from Method()
39 # ACPI_EXTRACT_NAME_STRING - extract a NameString from Name()
40 # ACPI_EXTRACT_PROCESSOR_START - start of Processor() block
41 # ACPI_EXTRACT_PROCESSOR_STRING - extract a NameString from Processor()
42 # ACPI_EXTRACT_PROCESSOR_END - offset at last byte of Processor() + 1
43 # ACPI_EXTRACT_PKG_START - start of Package block
45 # ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
47 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
49 import re;
50 import sys;
51 import fileinput;
53 aml = []
54 asl = []
55 output = {}
56 debug = ""
58 class asl_line:
59 line = None
60 lineno = None
61 aml_offset = None
63 def die(diag):
64 sys.stderr.write("Error: %s; %s\n" % (diag, debug))
65 sys.exit(1)
67 #Store an ASL command, matching AML offset, and input line (for debugging)
68 def add_asl(lineno, line):
69 l = asl_line()
70 l.line = line
71 l.lineno = lineno
72 l.aml_offset = len(aml)
73 asl.append(l)
75 #Store an AML byte sequence
76 #Verify that offset output by iasl matches # of bytes so far
77 def add_aml(offset, line):
78 o = int(offset, 16);
79 # Sanity check: offset must match size of code so far
80 if (o != len(aml)):
81 die("Offset 0x%x != 0x%x" % (o, len(aml)))
82 # Strip any trailing dots and ASCII dump after "
83 line = re.sub(r'\s*\.*\s*".*$',"", line)
84 # Strip traling whitespace
85 line = re.sub(r'\s+$',"", line)
86 # Strip leading whitespace
87 line = re.sub(r'^\s+',"", line)
88 # Split on whitespace
89 code = re.split(r'\s+', line)
90 for c in code:
91 # Require a legal hex number, two digits
92 if (not(re.search(r'^[0-9A-Fa-f][0-9A-Fa-f]$', c))):
93 die("Unexpected octet %s" % c);
94 aml.append(int(c, 16));
96 # Process aml bytecode array, decoding AML
97 def aml_pkglen_bytes(offset):
98 # PkgLength can be multibyte. Bits 8-7 give the # of extra bytes.
99 pkglenbytes = aml[offset] >> 6;
100 return pkglenbytes + 1
102 def aml_pkglen(offset):
103 pkgstart = offset
104 pkglenbytes = aml_pkglen_bytes(offset)
105 pkglen = aml[offset] & 0x3F
106 # If multibyte, first nibble only uses bits 0-3
107 if ((pkglenbytes > 1) and (pkglen & 0x30)):
108 die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
109 (pkglen, pkglen))
110 offset += 1
111 pkglenbytes -= 1
112 for i in range(pkglenbytes):
113 pkglen |= aml[offset + i] << (i * 8 + 4)
114 if (len(aml) < pkgstart + pkglen):
115 die("PckgLen 0x%x at offset 0x%x exceeds AML size 0x%x" %
116 (pkglen, offset, len(aml)))
117 return pkglen
119 # Given method offset, find its NameString offset
120 def aml_method_string(offset):
121 #0x14 MethodOp PkgLength NameString MethodFlags TermList
122 if (aml[offset] != 0x14):
123 die( "Method offset 0x%x: expected 0x14 actual 0x%x" %
124 (offset, aml[offset]));
125 offset += 1;
126 pkglenbytes = aml_pkglen_bytes(offset)
127 offset += pkglenbytes;
128 return offset;
130 # Given name offset, find its NameString offset
131 def aml_name_string(offset):
132 #0x08 NameOp NameString DataRef
133 if (aml[offset] != 0x08):
134 die( "Name offset 0x%x: expected 0x08 actual 0x%x" %
135 (offset, aml[offset]));
136 offset += 1
137 # Block Name Modifier. Skip it.
138 if (aml[offset] == 0x5c or aml[offset] == 0x5e):
139 offset += 1
140 return offset;
142 # Given data offset, find variable length byte buffer offset
143 def aml_data_buffer(offset, length):
144 #0x11 PkgLength BufferSize ByteList
145 if (length > 63):
146 die( "Name offset 0x%x: expected a one byte PkgLength (length<=63)" %
147 (offset));
148 expect = [0x11, length+3, 0x0A, length]
149 if (aml[offset:offset+4] != expect):
150 die( "Name offset 0x%x: expected %s actual %s" %
151 (offset, expect, aml[offset:offset+4]))
152 return offset + len(expect)
154 # Given data offset, find dword const offset
155 def aml_data_dword_const(offset):
156 #0x08 NameOp NameString DataRef
157 if (aml[offset] != 0x0C):
158 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
159 (offset, aml[offset]));
160 return offset + 1;
162 # Given data offset, find word const offset
163 def aml_data_word_const(offset):
164 #0x08 NameOp NameString DataRef
165 if (aml[offset] != 0x0B):
166 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
167 (offset, aml[offset]));
168 return offset + 1;
170 # Given data offset, find byte const offset
171 def aml_data_byte_const(offset):
172 #0x08 NameOp NameString DataRef
173 if (aml[offset] != 0x0A):
174 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
175 (offset, aml[offset]));
176 return offset + 1;
178 # Find name'd buffer
179 def aml_name_buffer(offset, length):
180 return aml_data_buffer(aml_name_string(offset) + 4, length)
182 # Given name offset, find dword const offset
183 def aml_name_dword_const(offset):
184 return aml_data_dword_const(aml_name_string(offset) + 4)
186 # Given name offset, find word const offset
187 def aml_name_word_const(offset):
188 return aml_data_word_const(aml_name_string(offset) + 4)
190 # Given name offset, find byte const offset
191 def aml_name_byte_const(offset):
192 return aml_data_byte_const(aml_name_string(offset) + 4)
194 def aml_device_start(offset):
195 #0x5B 0x82 DeviceOp PkgLength NameString
196 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
197 die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
198 (offset, aml[offset], aml[offset + 1]));
199 return offset
201 def aml_device_string(offset):
202 #0x5B 0x82 DeviceOp PkgLength NameString
203 start = aml_device_start(offset)
204 offset += 2
205 pkglenbytes = aml_pkglen_bytes(offset)
206 offset += pkglenbytes
207 return offset
209 def aml_device_end(offset):
210 start = aml_device_start(offset)
211 offset += 2
212 pkglenbytes = aml_pkglen_bytes(offset)
213 pkglen = aml_pkglen(offset)
214 return offset + pkglen
216 def aml_processor_start(offset):
217 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
218 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
219 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
220 (offset, aml[offset], aml[offset + 1]));
221 return offset
223 def aml_processor_string(offset):
224 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
225 start = aml_processor_start(offset)
226 offset += 2
227 pkglenbytes = aml_pkglen_bytes(offset)
228 offset += pkglenbytes
229 return offset
231 def aml_processor_end(offset):
232 start = aml_processor_start(offset)
233 offset += 2
234 pkglenbytes = aml_pkglen_bytes(offset)
235 pkglen = aml_pkglen(offset)
236 return offset + pkglen
238 def aml_package_start(offset):
239 offset = aml_name_string(offset) + 4
240 # 0x12 PkgLength NumElements PackageElementList
241 if (aml[offset] != 0x12):
242 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
243 (offset, aml[offset]));
244 offset += 1
245 return offset + aml_pkglen_bytes(offset) + 1
247 lineno = 0
248 for line in fileinput.input():
249 # Strip trailing newline
250 line = line.rstrip();
251 # line number and debug string to output in case of errors
252 lineno = lineno + 1
253 debug = "input line %d: %s" % (lineno, line)
254 #ASL listing: space, then line#, then ...., then code
255 pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
256 m = pasl.search(line)
257 if (m):
258 add_asl(lineno, pasl.sub("", line));
259 # AML listing: offset in hex, then ...., then code
260 paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
261 m = paml.search(line)
262 if (m):
263 add_aml(m.group(1), paml.sub("", line))
265 # Now go over code
266 # Track AML offset of a previous non-empty ASL command
267 prev_aml_offset = -1
268 for i in range(len(asl)):
269 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
271 l = asl[i].line
273 # skip if not an extract directive
274 a = len(re.findall(r'ACPI_EXTRACT', l))
275 if (not a):
276 # If not empty, store AML offset. Will be used for sanity checks
277 # IASL seems to put {}. at random places in the listing.
278 # Ignore any non-words for the purpose of this test.
279 m = re.search(r'\w+', l)
280 if (m):
281 prev_aml_offset = asl[i].aml_offset
282 continue
284 if (a > 1):
285 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
287 mext = re.search(r'''
288 ^\s* # leading whitespace
289 /\*\s* # start C comment
290 (ACPI_EXTRACT_\w+) # directive: group(1)
291 \s+ # whitspace separates directive from array name
292 (\w+) # array name: group(2)
293 \s*\*/ # end of C comment
294 \s*$ # trailing whitespace
295 ''', l, re.VERBOSE)
296 if (not mext):
297 die("Stray ACPI_EXTRACT in input")
299 # previous command must have produced some AML,
300 # otherwise we are in a middle of a block
301 if (prev_aml_offset == asl[i].aml_offset):
302 die("ACPI_EXTRACT directive in the middle of a block")
304 directive = mext.group(1)
305 array = mext.group(2)
306 offset = asl[i].aml_offset
308 if (directive == "ACPI_EXTRACT_ALL_CODE"):
309 if array in output:
310 die("%s directive used more than once" % directive)
311 output[array] = aml
312 continue
313 if (directive == "ACPI_EXTRACT_NAME_BUFFER8"):
314 offset = aml_name_buffer(offset, 8)
315 elif (directive == "ACPI_EXTRACT_NAME_BUFFER16"):
316 offset = aml_name_buffer(offset, 16)
317 elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
318 offset = aml_name_dword_const(offset)
319 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
320 offset = aml_name_word_const(offset)
321 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
322 offset = aml_name_byte_const(offset)
323 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
324 offset = aml_name_string(offset)
325 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
326 offset = aml_method_string(offset)
327 elif (directive == "ACPI_EXTRACT_DEVICE_START"):
328 offset = aml_device_start(offset)
329 elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
330 offset = aml_device_string(offset)
331 elif (directive == "ACPI_EXTRACT_DEVICE_END"):
332 offset = aml_device_end(offset)
333 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
334 offset = aml_processor_start(offset)
335 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
336 offset = aml_processor_string(offset)
337 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
338 offset = aml_processor_end(offset)
339 elif (directive == "ACPI_EXTRACT_PKG_START"):
340 offset = aml_package_start(offset)
341 else:
342 die("Unsupported directive %s" % directive)
344 if array not in output:
345 output[array] = []
346 output[array].append(offset)
348 debug = "at end of file"
350 def get_value_type(maxvalue):
351 #Use type large enough to fit the table
352 if (maxvalue >= 0x10000):
353 return "int"
354 elif (maxvalue >= 0x100):
355 return "short"
356 else:
357 return "char"
359 # Pretty print output
360 for array in output.keys():
361 otype = get_value_type(max(output[array]))
362 odata = []
363 for value in output[array]:
364 odata.append("0x%x" % value)
365 sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
366 sys.stdout.write(",\n".join(odata))
367 sys.stdout.write('\n};\n');