Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu.git] / scripts / acpi_extract.py
1 #!/usr/bin/python
2 # Copyright (C) 2011 Red Hat, Inc., Michael S. Tsirkin <mst@redhat.com>
3 #
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.
8 #
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.
13 #
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/>.
16
17 # Process mixed ASL/AML listing (.lst file) produced by iasl -l
18 # Locate and execute ACPI_EXTRACT directives, output offset info
19 #
20 # Documentation of ACPI_EXTRACT_* directive tags:
21 #
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>.
29 #
30 # A directive must fit on a single code line.
31 #
32 # Object type in AML is verified, a mismatch causes a build failure.
33 #
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
44 #
45 # ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
46 #
47 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
48
49 import re;
50 import sys;
51 import fileinput;
52
53 aml = []
54 asl = []
55 output = {}
56 debug = ""
57
58 class asl_line:
59 line = None
60 lineno = None
61 aml_offset = None
62
63 def die(diag):
64 sys.stderr.write("Error: %s; %s\n" % (diag, debug))
65 sys.exit(1)
66
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)
74
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));
95
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
101
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
118
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;
129
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;
141
142 # Given data offset, find 8 byte buffer offset
143 def aml_data_buffer8(offset):
144 #0x08 NameOp NameString DataRef
145 expect = [0x11, 0x0B, 0x0A, 0x08]
146 if (aml[offset:offset+4] != expect):
147 die( "Name offset 0x%x: expected %s actual %s" %
148 (offset, aml[offset:offset+4], expect))
149 return offset + len(expect)
150
151 # Given data offset, find dword const offset
152 def aml_data_dword_const(offset):
153 #0x08 NameOp NameString DataRef
154 if (aml[offset] != 0x0C):
155 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
156 (offset, aml[offset]));
157 return offset + 1;
158
159 # Given data offset, find word const offset
160 def aml_data_word_const(offset):
161 #0x08 NameOp NameString DataRef
162 if (aml[offset] != 0x0B):
163 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
164 (offset, aml[offset]));
165 return offset + 1;
166
167 # Given data offset, find byte const offset
168 def aml_data_byte_const(offset):
169 #0x08 NameOp NameString DataRef
170 if (aml[offset] != 0x0A):
171 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
172 (offset, aml[offset]));
173 return offset + 1;
174
175 # Find name'd buffer8
176 def aml_name_buffer8(offset):
177 return aml_data_buffer8(aml_name_string(offset) + 4)
178
179 # Given name offset, find dword const offset
180 def aml_name_dword_const(offset):
181 return aml_data_dword_const(aml_name_string(offset) + 4)
182
183 # Given name offset, find word const offset
184 def aml_name_word_const(offset):
185 return aml_data_word_const(aml_name_string(offset) + 4)
186
187 # Given name offset, find byte const offset
188 def aml_name_byte_const(offset):
189 return aml_data_byte_const(aml_name_string(offset) + 4)
190
191 def aml_device_start(offset):
192 #0x5B 0x82 DeviceOp PkgLength NameString
193 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
194 die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
195 (offset, aml[offset], aml[offset + 1]));
196 return offset
197
198 def aml_device_string(offset):
199 #0x5B 0x82 DeviceOp PkgLength NameString
200 start = aml_device_start(offset)
201 offset += 2
202 pkglenbytes = aml_pkglen_bytes(offset)
203 offset += pkglenbytes
204 return offset
205
206 def aml_device_end(offset):
207 start = aml_device_start(offset)
208 offset += 2
209 pkglenbytes = aml_pkglen_bytes(offset)
210 pkglen = aml_pkglen(offset)
211 return offset + pkglen
212
213 def aml_processor_start(offset):
214 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
215 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
216 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
217 (offset, aml[offset], aml[offset + 1]));
218 return offset
219
220 def aml_processor_string(offset):
221 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
222 start = aml_processor_start(offset)
223 offset += 2
224 pkglenbytes = aml_pkglen_bytes(offset)
225 offset += pkglenbytes
226 return offset
227
228 def aml_processor_end(offset):
229 start = aml_processor_start(offset)
230 offset += 2
231 pkglenbytes = aml_pkglen_bytes(offset)
232 pkglen = aml_pkglen(offset)
233 return offset + pkglen
234
235 def aml_package_start(offset):
236 offset = aml_name_string(offset) + 4
237 # 0x12 PkgLength NumElements PackageElementList
238 if (aml[offset] != 0x12):
239 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
240 (offset, aml[offset]));
241 offset += 1
242 return offset + aml_pkglen_bytes(offset) + 1
243
244 lineno = 0
245 for line in fileinput.input():
246 # Strip trailing newline
247 line = line.rstrip();
248 # line number and debug string to output in case of errors
249 lineno = lineno + 1
250 debug = "input line %d: %s" % (lineno, line)
251 #ASL listing: space, then line#, then ...., then code
252 pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
253 m = pasl.search(line)
254 if (m):
255 add_asl(lineno, pasl.sub("", line));
256 # AML listing: offset in hex, then ...., then code
257 paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
258 m = paml.search(line)
259 if (m):
260 add_aml(m.group(1), paml.sub("", line))
261
262 # Now go over code
263 # Track AML offset of a previous non-empty ASL command
264 prev_aml_offset = -1
265 for i in range(len(asl)):
266 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
267
268 l = asl[i].line
269
270 # skip if not an extract directive
271 a = len(re.findall(r'ACPI_EXTRACT', l))
272 if (not a):
273 # If not empty, store AML offset. Will be used for sanity checks
274 # IASL seems to put {}. at random places in the listing.
275 # Ignore any non-words for the purpose of this test.
276 m = re.search(r'\w+', l)
277 if (m):
278 prev_aml_offset = asl[i].aml_offset
279 continue
280
281 if (a > 1):
282 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
283
284 mext = re.search(r'''
285 ^\s* # leading whitespace
286 /\*\s* # start C comment
287 (ACPI_EXTRACT_\w+) # directive: group(1)
288 \s+ # whitspace separates directive from array name
289 (\w+) # array name: group(2)
290 \s*\*/ # end of C comment
291 \s*$ # trailing whitespace
292 ''', l, re.VERBOSE)
293 if (not mext):
294 die("Stray ACPI_EXTRACT in input")
295
296 # previous command must have produced some AML,
297 # otherwise we are in a middle of a block
298 if (prev_aml_offset == asl[i].aml_offset):
299 die("ACPI_EXTRACT directive in the middle of a block")
300
301 directive = mext.group(1)
302 array = mext.group(2)
303 offset = asl[i].aml_offset
304
305 if (directive == "ACPI_EXTRACT_ALL_CODE"):
306 if array in output:
307 die("%s directive used more than once" % directive)
308 output[array] = aml
309 continue
310 if (directive == "ACPI_EXTRACT_NAME_BUFFER8"):
311 offset = aml_name_buffer8(offset)
312 elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
313 offset = aml_name_dword_const(offset)
314 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
315 offset = aml_name_word_const(offset)
316 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
317 offset = aml_name_byte_const(offset)
318 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
319 offset = aml_name_string(offset)
320 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
321 offset = aml_method_string(offset)
322 elif (directive == "ACPI_EXTRACT_DEVICE_START"):
323 offset = aml_device_start(offset)
324 elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
325 offset = aml_device_string(offset)
326 elif (directive == "ACPI_EXTRACT_DEVICE_END"):
327 offset = aml_device_end(offset)
328 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
329 offset = aml_processor_start(offset)
330 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
331 offset = aml_processor_string(offset)
332 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
333 offset = aml_processor_end(offset)
334 elif (directive == "ACPI_EXTRACT_PKG_START"):
335 offset = aml_package_start(offset)
336 else:
337 die("Unsupported directive %s" % directive)
338
339 if array not in output:
340 output[array] = []
341 output[array].append(offset)
342
343 debug = "at end of file"
344
345 def get_value_type(maxvalue):
346 #Use type large enough to fit the table
347 if (maxvalue >= 0x10000):
348 return "int"
349 elif (maxvalue >= 0x100):
350 return "short"
351 else:
352 return "char"
353
354 # Pretty print output
355 for array in output.keys():
356 otype = get_value_type(max(output[array]))
357 odata = []
358 for value in output[array]:
359 odata.append("0x%x" % value)
360 sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
361 sys.stdout.write(",\n".join(odata))
362 sys.stdout.write('\n};\n');