blob: 3ed863b6a79412a1276bb905d08f92bd677e9e09 [file] [log] [blame]
Michael S. Tsirkin2597b752011-10-04 15:26:01 +02001#!/usr/bin/python
2# Copyright (C) 2011 Red Hat, Inc., Michael S. Tsirkin <mst@redhat.com>
3#
4# This file may be distributed under the terms of the GNU GPLv3 license.
5
6# Process mixed ASL/AML listing (.lst file) produced by iasl -l
7# Locate and execute ACPI_EXTRACT directives, output offset info
8#
9# Documentation of ACPI_EXTRACT_* directive tags:
10#
11# These directive tags output offset information from AML for BIOS runtime
12# table generation.
13# Each directive is of the form:
14# ACPI_EXTRACT_<TYPE> <array_name> <Operator> (...)
15# and causes the extractor to create an array
16# named <array_name> with offset, in the generated AML,
17# of an object of a given type in the following <Operator>.
18#
19# A directive must fit on a single code line.
20#
21# Object type in AML is verified, a mismatch causes a build failure.
22#
23# Directives and operators currently supported are:
24# ACPI_EXTRACT_NAME_DWORD_CONST - extract a Dword Const object from Name()
25# ACPI_EXTRACT_NAME_WORD_CONST - extract a Word Const object from Name()
26# ACPI_EXTRACT_NAME_BYTE_CONST - extract a Byte Const object from Name()
27# ACPI_EXTRACT_METHOD_STRING - extract a NameString from Method()
28# ACPI_EXTRACT_NAME_STRING - extract a NameString from Name()
29# ACPI_EXTRACT_PROCESSOR_START - start of Processor() block
30# ACPI_EXTRACT_PROCESSOR_STRING - extract a NameString from Processor()
31# ACPI_EXTRACT_PROCESSOR_END - offset at last byte of Processor() + 1
Michael S. Tsirkind4c2e012013-08-01 19:05:09 +030032# ACPI_EXTRACT_DEVICE_START - start of Device() block
33# ACPI_EXTRACT_DEVICE_STRING - extract a NameString from Device()
34# ACPI_EXTRACT_DEVICE_END - offset at last byte of Device() + 1
Gleb Natapov94bb3ca2012-05-20 12:03:39 +030035# ACPI_EXTRACT_PKG_START - start of Package block
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +020036#
37# ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020038#
39# ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
40
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050041import re
42import sys
43import fileinput
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020044
45aml = []
46asl = []
47output = {}
48debug = ""
49
50class asl_line:
51 line = None
52 lineno = None
53 aml_offset = None
54
55def die(diag):
56 sys.stderr.write("Error: %s; %s\n" % (diag, debug))
57 sys.exit(1)
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050058
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020059#Store an ASL command, matching AML offset, and input line (for debugging)
60def add_asl(lineno, line):
61 l = asl_line()
62 l.line = line
63 l.lineno = lineno
64 l.aml_offset = len(aml)
65 asl.append(l)
66
67#Store an AML byte sequence
68#Verify that offset output by iasl matches # of bytes so far
69def add_aml(offset, line):
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050070 o = int(offset, 16)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020071 # Sanity check: offset must match size of code so far
72 if (o != len(aml)):
73 die("Offset 0x%x != 0x%x" % (o, len(aml)))
74 # Strip any trailing dots and ASCII dump after "
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050075 line = re.sub(r'\s*\.*\s*".*$', "", line)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020076 # Strip traling whitespace
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050077 line = re.sub(r'\s+$', "", line)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020078 # Strip leading whitespace
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050079 line = re.sub(r'^\s+', "", line)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020080 # Split on whitespace
81 code = re.split(r'\s+', line)
82 for c in code:
83 # Require a legal hex number, two digits
84 if (not(re.search(r'^[0-9A-Fa-f][0-9A-Fa-f]$', c))):
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050085 die("Unexpected octet %s" % c)
86 aml.append(int(c, 16))
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020087
88# Process aml bytecode array, decoding AML
89def aml_pkglen_bytes(offset):
90 # PkgLength can be multibyte. Bits 8-7 give the # of extra bytes.
Johannes Krampf24ef4fd2014-01-12 10:54:22 -050091 pkglenbytes = aml[offset] >> 6
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020092 return pkglenbytes + 1
93
94def aml_pkglen(offset):
95 pkgstart = offset
96 pkglenbytes = aml_pkglen_bytes(offset)
97 pkglen = aml[offset] & 0x3F
98 # If multibyte, first nibble only uses bits 0-3
Paolo Bonzinif9607822012-08-02 15:07:23 +020099 if ((pkglenbytes > 1) and (pkglen & 0x30)):
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200100 die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
101 (pkglen, pkglen))
102 offset += 1
103 pkglenbytes -= 1
104 for i in range(pkglenbytes):
105 pkglen |= aml[offset + i] << (i * 8 + 4)
106 if (len(aml) < pkgstart + pkglen):
107 die("PckgLen 0x%x at offset 0x%x exceeds AML size 0x%x" %
108 (pkglen, offset, len(aml)))
109 return pkglen
110
111# Given method offset, find its NameString offset
112def aml_method_string(offset):
113 #0x14 MethodOp PkgLength NameString MethodFlags TermList
114 if (aml[offset] != 0x14):
115 die( "Method offset 0x%x: expected 0x14 actual 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500116 (offset, aml[offset]))
117 offset += 1
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200118 pkglenbytes = aml_pkglen_bytes(offset)
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500119 offset += pkglenbytes
120 return offset
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200121
122# Given name offset, find its NameString offset
123def aml_name_string(offset):
124 #0x08 NameOp NameString DataRef
125 if (aml[offset] != 0x08):
126 die( "Name offset 0x%x: expected 0x08 actual 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500127 (offset, aml[offset]))
Gleb Natapov2197ff62012-05-20 12:03:38 +0300128 offset += 1
129 # Block Name Modifier. Skip it.
130 if (aml[offset] == 0x5c or aml[offset] == 0x5e):
131 offset += 1
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500132 return offset
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200133
Kevin O'Connor76e58022013-03-06 21:50:09 -0500134# Given data offset, find 8 byte buffer offset
135def aml_data_buffer8(offset):
136 #0x08 NameOp NameString DataRef
137 expect = [0x11, 0x0B, 0x0A, 0x08]
138 if (aml[offset:offset+4] != expect):
139 die( "Name offset 0x%x: expected %s actual %s" %
140 (offset, aml[offset:offset+4], expect))
141 return offset + len(expect)
142
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200143# Given data offset, find dword const offset
144def aml_data_dword_const(offset):
145 #0x08 NameOp NameString DataRef
146 if (aml[offset] != 0x0C):
147 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500148 (offset, aml[offset]))
149 return offset + 1
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200150
151# Given data offset, find word const offset
152def aml_data_word_const(offset):
153 #0x08 NameOp NameString DataRef
154 if (aml[offset] != 0x0B):
155 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500156 (offset, aml[offset]))
157 return offset + 1
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200158
159# Given data offset, find byte const offset
160def aml_data_byte_const(offset):
161 #0x08 NameOp NameString DataRef
162 if (aml[offset] != 0x0A):
163 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500164 (offset, aml[offset]))
165 return offset + 1
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200166
Kevin O'Connor76e58022013-03-06 21:50:09 -0500167# Find name'd buffer8
168def aml_name_buffer8(offset):
169 return aml_data_buffer8(aml_name_string(offset) + 4)
170
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200171# Given name offset, find dword const offset
172def aml_name_dword_const(offset):
173 return aml_data_dword_const(aml_name_string(offset) + 4)
174
175# Given name offset, find word const offset
176def aml_name_word_const(offset):
177 return aml_data_word_const(aml_name_string(offset) + 4)
178
179# Given name offset, find byte const offset
180def aml_name_byte_const(offset):
181 return aml_data_byte_const(aml_name_string(offset) + 4)
182
Paolo Bonzini2b568eb2012-08-02 15:07:24 +0200183def aml_device_start(offset):
184 #0x5B 0x82 DeviceOp PkgLength NameString
185 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
186 die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500187 (offset, aml[offset], aml[offset + 1]))
Paolo Bonzini2b568eb2012-08-02 15:07:24 +0200188 return offset
189
190def aml_device_string(offset):
191 #0x5B 0x82 DeviceOp PkgLength NameString
192 start = aml_device_start(offset)
193 offset += 2
194 pkglenbytes = aml_pkglen_bytes(offset)
195 offset += pkglenbytes
196 return offset
197
198def aml_device_end(offset):
199 start = aml_device_start(offset)
200 offset += 2
201 pkglenbytes = aml_pkglen_bytes(offset)
202 pkglen = aml_pkglen(offset)
203 return offset + pkglen
204
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200205def aml_processor_start(offset):
206 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
207 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
208 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500209 (offset, aml[offset], aml[offset + 1]))
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200210 return offset
211
212def aml_processor_string(offset):
213 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
214 start = aml_processor_start(offset)
215 offset += 2
216 pkglenbytes = aml_pkglen_bytes(offset)
217 offset += pkglenbytes
218 return offset
219
220def aml_processor_end(offset):
221 start = aml_processor_start(offset)
222 offset += 2
223 pkglenbytes = aml_pkglen_bytes(offset)
224 pkglen = aml_pkglen(offset)
225 return offset + pkglen
226
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300227def aml_package_start(offset):
228 offset = aml_name_string(offset) + 4
229 # 0x12 PkgLength NumElements PackageElementList
230 if (aml[offset] != 0x12):
231 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500232 (offset, aml[offset]))
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300233 offset += 1
234 return offset + aml_pkglen_bytes(offset) + 1
235
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200236def get_value_type(maxvalue):
237 #Use type large enough to fit the table
238 if (maxvalue >= 0x10000):
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500239 return "int"
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200240 elif (maxvalue >= 0x100):
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500241 return "short"
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200242 else:
Johannes Krampf24ef4fd2014-01-12 10:54:22 -0500243 return "char"
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200244
Kevin O'Connor1e7d2ed2015-11-17 18:24:24 -0500245def main():
246 global debug
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))
264
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)
270
271 l = asl[i].line
272
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
283
284 if (a > 1):
285 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
286
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")
298
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")
303
304 directive = mext.group(1)
305 array = mext.group(2)
306 offset = asl[i].aml_offset
307
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_buffer8(offset)
315 elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
316 offset = aml_name_dword_const(offset)
317 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
318 offset = aml_name_word_const(offset)
319 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
320 offset = aml_name_byte_const(offset)
321 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
322 offset = aml_name_string(offset)
323 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
324 offset = aml_method_string(offset)
325 elif (directive == "ACPI_EXTRACT_DEVICE_START"):
326 offset = aml_device_start(offset)
327 elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
328 offset = aml_device_string(offset)
329 elif (directive == "ACPI_EXTRACT_DEVICE_END"):
330 offset = aml_device_end(offset)
331 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
332 offset = aml_processor_start(offset)
333 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
334 offset = aml_processor_string(offset)
335 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
336 offset = aml_processor_end(offset)
337 elif (directive == "ACPI_EXTRACT_PKG_START"):
338 offset = aml_package_start(offset)
339 else:
340 die("Unsupported directive %s" % directive)
341
342 if array not in output:
343 output[array] = []
344 output[array].append(offset)
345
346 debug = "at end of file"
347
348 # Pretty print output
Kevin O'Connor7fdd2fd2015-11-17 18:36:17 -0500349 outstrs = ["/* DO NOT EDIT! This is an autogenerated file."
350 " See scripts/acpi_extract.py. */"]
Kevin O'Connor1e7d2ed2015-11-17 18:24:24 -0500351 for array in output.keys():
352 otype = get_value_type(max(output[array]))
Kevin O'Connor7fdd2fd2015-11-17 18:36:17 -0500353 outstrs.append("static unsigned %s %s[] = {" % (otype, array))
Kevin O'Connor1e7d2ed2015-11-17 18:24:24 -0500354 odata = []
355 for value in output[array]:
Kevin O'Connor7fdd2fd2015-11-17 18:36:17 -0500356 odata.append("0x%02x" % value)
357 if len(odata) >= 8:
358 outstrs.append(" %s," % (', '.join(odata),))
359 del odata[:]
360 outstrs.append(" %s" % (', '.join(odata),))
361 outstrs.append('};')
362 outstrs.append('')
363 sys.stdout.write('\n'.join(outstrs))
Kevin O'Connor1e7d2ed2015-11-17 18:24:24 -0500364
365if __name__ == '__main__':
366 main()