blob: dd3ccfdd96d38722309f4a0aff5ba3ebf7860df2 [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
Gleb Natapov94bb3ca2012-05-20 12:03:39 +030032# ACPI_EXTRACT_PKG_START - start of Package block
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +020033#
34# ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020035#
36# ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
37
38import re;
39import sys;
40import fileinput;
41
42aml = []
43asl = []
44output = {}
45debug = ""
46
47class asl_line:
48 line = None
49 lineno = None
50 aml_offset = None
51
52def die(diag):
53 sys.stderr.write("Error: %s; %s\n" % (diag, debug))
54 sys.exit(1)
55
56#Store an ASL command, matching AML offset, and input line (for debugging)
57def add_asl(lineno, line):
58 l = asl_line()
59 l.line = line
60 l.lineno = lineno
61 l.aml_offset = len(aml)
62 asl.append(l)
63
64#Store an AML byte sequence
65#Verify that offset output by iasl matches # of bytes so far
66def add_aml(offset, line):
67 o = int(offset, 16);
68 # Sanity check: offset must match size of code so far
69 if (o != len(aml)):
70 die("Offset 0x%x != 0x%x" % (o, len(aml)))
71 # Strip any trailing dots and ASCII dump after "
72 line = re.sub(r'\s*\.*\s*".*$',"", line)
73 # Strip traling whitespace
74 line = re.sub(r'\s+$',"", line)
75 # Strip leading whitespace
76 line = re.sub(r'^\s+',"", line)
77 # Split on whitespace
78 code = re.split(r'\s+', line)
79 for c in code:
80 # Require a legal hex number, two digits
81 if (not(re.search(r'^[0-9A-Fa-f][0-9A-Fa-f]$', c))):
82 die("Unexpected octet %s" % c);
83 aml.append(int(c, 16));
84
85# Process aml bytecode array, decoding AML
86def aml_pkglen_bytes(offset):
87 # PkgLength can be multibyte. Bits 8-7 give the # of extra bytes.
88 pkglenbytes = aml[offset] >> 6;
89 return pkglenbytes + 1
90
91def aml_pkglen(offset):
92 pkgstart = offset
93 pkglenbytes = aml_pkglen_bytes(offset)
94 pkglen = aml[offset] & 0x3F
95 # If multibyte, first nibble only uses bits 0-3
Paolo Bonzinif9607822012-08-02 15:07:23 +020096 if ((pkglenbytes > 1) and (pkglen & 0x30)):
Michael S. Tsirkin2597b752011-10-04 15:26:01 +020097 die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
98 (pkglen, pkglen))
99 offset += 1
100 pkglenbytes -= 1
101 for i in range(pkglenbytes):
102 pkglen |= aml[offset + i] << (i * 8 + 4)
103 if (len(aml) < pkgstart + pkglen):
104 die("PckgLen 0x%x at offset 0x%x exceeds AML size 0x%x" %
105 (pkglen, offset, len(aml)))
106 return pkglen
107
108# Given method offset, find its NameString offset
109def aml_method_string(offset):
110 #0x14 MethodOp PkgLength NameString MethodFlags TermList
111 if (aml[offset] != 0x14):
112 die( "Method offset 0x%x: expected 0x14 actual 0x%x" %
113 (offset, aml[offset]));
114 offset += 1;
115 pkglenbytes = aml_pkglen_bytes(offset)
116 offset += pkglenbytes;
117 return offset;
118
119# Given name offset, find its NameString offset
120def aml_name_string(offset):
121 #0x08 NameOp NameString DataRef
122 if (aml[offset] != 0x08):
123 die( "Name offset 0x%x: expected 0x08 actual 0x%x" %
124 (offset, aml[offset]));
Gleb Natapov2197ff62012-05-20 12:03:38 +0300125 offset += 1
126 # Block Name Modifier. Skip it.
127 if (aml[offset] == 0x5c or aml[offset] == 0x5e):
128 offset += 1
129 return offset;
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200130
131# Given data offset, find dword const offset
132def aml_data_dword_const(offset):
133 #0x08 NameOp NameString DataRef
134 if (aml[offset] != 0x0C):
135 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
136 (offset, aml[offset]));
137 return offset + 1;
138
139# Given data offset, find word const offset
140def aml_data_word_const(offset):
141 #0x08 NameOp NameString DataRef
142 if (aml[offset] != 0x0B):
143 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
144 (offset, aml[offset]));
145 return offset + 1;
146
147# Given data offset, find byte const offset
148def aml_data_byte_const(offset):
149 #0x08 NameOp NameString DataRef
150 if (aml[offset] != 0x0A):
151 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
152 (offset, aml[offset]));
153 return offset + 1;
154
155# Given name offset, find dword const offset
156def aml_name_dword_const(offset):
157 return aml_data_dword_const(aml_name_string(offset) + 4)
158
159# Given name offset, find word const offset
160def aml_name_word_const(offset):
161 return aml_data_word_const(aml_name_string(offset) + 4)
162
163# Given name offset, find byte const offset
164def aml_name_byte_const(offset):
165 return aml_data_byte_const(aml_name_string(offset) + 4)
166
Paolo Bonzini2b568eb2012-08-02 15:07:24 +0200167def aml_device_start(offset):
168 #0x5B 0x82 DeviceOp PkgLength NameString
169 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
170 die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
171 (offset, aml[offset], aml[offset + 1]));
172 return offset
173
174def aml_device_string(offset):
175 #0x5B 0x82 DeviceOp PkgLength NameString
176 start = aml_device_start(offset)
177 offset += 2
178 pkglenbytes = aml_pkglen_bytes(offset)
179 offset += pkglenbytes
180 return offset
181
182def aml_device_end(offset):
183 start = aml_device_start(offset)
184 offset += 2
185 pkglenbytes = aml_pkglen_bytes(offset)
186 pkglen = aml_pkglen(offset)
187 return offset + pkglen
188
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200189def aml_processor_start(offset):
190 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
191 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
192 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
193 (offset, aml[offset], aml[offset + 1]));
194 return offset
195
196def aml_processor_string(offset):
197 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
198 start = aml_processor_start(offset)
199 offset += 2
200 pkglenbytes = aml_pkglen_bytes(offset)
201 offset += pkglenbytes
202 return offset
203
204def aml_processor_end(offset):
205 start = aml_processor_start(offset)
206 offset += 2
207 pkglenbytes = aml_pkglen_bytes(offset)
208 pkglen = aml_pkglen(offset)
209 return offset + pkglen
210
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300211def aml_package_start(offset):
212 offset = aml_name_string(offset) + 4
213 # 0x12 PkgLength NumElements PackageElementList
214 if (aml[offset] != 0x12):
215 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
216 (offset, aml[offset]));
217 offset += 1
218 return offset + aml_pkglen_bytes(offset) + 1
219
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200220lineno = 0
221for line in fileinput.input():
222 # Strip trailing newline
223 line = line.rstrip();
224 # line number and debug string to output in case of errors
225 lineno = lineno + 1
226 debug = "input line %d: %s" % (lineno, line)
227 #ASL listing: space, then line#, then ...., then code
Kevin O'Connorf3968712013-02-10 21:06:14 -0500228 pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200229 m = pasl.search(line)
230 if (m):
231 add_asl(lineno, pasl.sub("", line));
232 # AML listing: offset in hex, then ...., then code
Kevin O'Connorf3968712013-02-10 21:06:14 -0500233 paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200234 m = paml.search(line)
235 if (m):
236 add_aml(m.group(1), paml.sub("", line))
237
238# Now go over code
239# Track AML offset of a previous non-empty ASL command
240prev_aml_offset = -1
241for i in range(len(asl)):
242 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
243
244 l = asl[i].line
245
246 # skip if not an extract directive
247 a = len(re.findall(r'ACPI_EXTRACT', l))
248 if (not a):
249 # If not empty, store AML offset. Will be used for sanity checks
250 # IASL seems to put {}. at random places in the listing.
251 # Ignore any non-words for the purpose of this test.
252 m = re.search(r'\w+', l)
253 if (m):
254 prev_aml_offset = asl[i].aml_offset
255 continue
256
257 if (a > 1):
258 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
259
260 mext = re.search(r'''
261 ^\s* # leading whitespace
262 /\*\s* # start C comment
263 (ACPI_EXTRACT_\w+) # directive: group(1)
264 \s+ # whitspace separates directive from array name
265 (\w+) # array name: group(2)
266 \s*\*/ # end of C comment
267 \s*$ # trailing whitespace
268 ''', l, re.VERBOSE)
269 if (not mext):
270 die("Stray ACPI_EXTRACT in input")
271
272 # previous command must have produced some AML,
273 # otherwise we are in a middle of a block
274 if (prev_aml_offset == asl[i].aml_offset):
275 die("ACPI_EXTRACT directive in the middle of a block")
276
277 directive = mext.group(1)
278 array = mext.group(2)
279 offset = asl[i].aml_offset
280
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200281 if (directive == "ACPI_EXTRACT_ALL_CODE"):
282 if array in output:
283 die("%s directive used more than once" % directive)
284 output[array] = aml
285 continue
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200286 if (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
287 offset = aml_name_dword_const(offset)
288 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
289 offset = aml_name_word_const(offset)
290 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
291 offset = aml_name_byte_const(offset)
292 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
293 offset = aml_name_string(offset)
294 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
295 offset = aml_method_string(offset)
Paolo Bonzini2b568eb2012-08-02 15:07:24 +0200296 elif (directive == "ACPI_EXTRACT_DEVICE_START"):
297 offset = aml_device_start(offset)
298 elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
299 offset = aml_device_string(offset)
300 elif (directive == "ACPI_EXTRACT_DEVICE_END"):
301 offset = aml_device_end(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200302 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
303 offset = aml_processor_start(offset)
304 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
305 offset = aml_processor_string(offset)
306 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
307 offset = aml_processor_end(offset)
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300308 elif (directive == "ACPI_EXTRACT_PKG_START"):
309 offset = aml_package_start(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200310 else:
311 die("Unsupported directive %s" % directive)
312
313 if array not in output:
314 output[array] = []
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200315 output[array].append(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200316
317debug = "at end of file"
318
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200319def get_value_type(maxvalue):
320 #Use type large enough to fit the table
321 if (maxvalue >= 0x10000):
322 return "int"
323 elif (maxvalue >= 0x100):
324 return "short"
325 else:
326 return "char"
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200327
328# Pretty print output
329for array in output.keys():
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200330 otype = get_value_type(max(output[array]))
331 odata = []
332 for value in output[array]:
333 odata.append("0x%x" % value)
334 sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
335 sys.stdout.write(",\n".join(odata))
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200336 sys.stdout.write('\n};\n');