blob: ab8ced647512e1e9a804d3c2243cb6bd1a49414d [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
Kevin O'Connor76e58022013-03-06 21:50:09 -0500131# Given data offset, find 8 byte buffer offset
132def aml_data_buffer8(offset):
133 #0x08 NameOp NameString DataRef
134 expect = [0x11, 0x0B, 0x0A, 0x08]
135 if (aml[offset:offset+4] != expect):
136 die( "Name offset 0x%x: expected %s actual %s" %
137 (offset, aml[offset:offset+4], expect))
138 return offset + len(expect)
139
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200140# Given data offset, find dword const offset
141def aml_data_dword_const(offset):
142 #0x08 NameOp NameString DataRef
143 if (aml[offset] != 0x0C):
144 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
145 (offset, aml[offset]));
146 return offset + 1;
147
148# Given data offset, find word const offset
149def aml_data_word_const(offset):
150 #0x08 NameOp NameString DataRef
151 if (aml[offset] != 0x0B):
152 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
153 (offset, aml[offset]));
154 return offset + 1;
155
156# Given data offset, find byte const offset
157def aml_data_byte_const(offset):
158 #0x08 NameOp NameString DataRef
159 if (aml[offset] != 0x0A):
160 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
161 (offset, aml[offset]));
162 return offset + 1;
163
Kevin O'Connor76e58022013-03-06 21:50:09 -0500164# Find name'd buffer8
165def aml_name_buffer8(offset):
166 return aml_data_buffer8(aml_name_string(offset) + 4)
167
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200168# Given name offset, find dword const offset
169def aml_name_dword_const(offset):
170 return aml_data_dword_const(aml_name_string(offset) + 4)
171
172# Given name offset, find word const offset
173def aml_name_word_const(offset):
174 return aml_data_word_const(aml_name_string(offset) + 4)
175
176# Given name offset, find byte const offset
177def aml_name_byte_const(offset):
178 return aml_data_byte_const(aml_name_string(offset) + 4)
179
Paolo Bonzini2b568eb2012-08-02 15:07:24 +0200180def aml_device_start(offset):
181 #0x5B 0x82 DeviceOp PkgLength NameString
182 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
183 die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
184 (offset, aml[offset], aml[offset + 1]));
185 return offset
186
187def aml_device_string(offset):
188 #0x5B 0x82 DeviceOp PkgLength NameString
189 start = aml_device_start(offset)
190 offset += 2
191 pkglenbytes = aml_pkglen_bytes(offset)
192 offset += pkglenbytes
193 return offset
194
195def aml_device_end(offset):
196 start = aml_device_start(offset)
197 offset += 2
198 pkglenbytes = aml_pkglen_bytes(offset)
199 pkglen = aml_pkglen(offset)
200 return offset + pkglen
201
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200202def aml_processor_start(offset):
203 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
204 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
205 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
206 (offset, aml[offset], aml[offset + 1]));
207 return offset
208
209def aml_processor_string(offset):
210 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
211 start = aml_processor_start(offset)
212 offset += 2
213 pkglenbytes = aml_pkglen_bytes(offset)
214 offset += pkglenbytes
215 return offset
216
217def aml_processor_end(offset):
218 start = aml_processor_start(offset)
219 offset += 2
220 pkglenbytes = aml_pkglen_bytes(offset)
221 pkglen = aml_pkglen(offset)
222 return offset + pkglen
223
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300224def aml_package_start(offset):
225 offset = aml_name_string(offset) + 4
226 # 0x12 PkgLength NumElements PackageElementList
227 if (aml[offset] != 0x12):
228 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
229 (offset, aml[offset]));
230 offset += 1
231 return offset + aml_pkglen_bytes(offset) + 1
232
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200233lineno = 0
234for line in fileinput.input():
235 # Strip trailing newline
236 line = line.rstrip();
237 # line number and debug string to output in case of errors
238 lineno = lineno + 1
239 debug = "input line %d: %s" % (lineno, line)
240 #ASL listing: space, then line#, then ...., then code
Kevin O'Connor616fb272013-02-10 21:06:14 -0500241 pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200242 m = pasl.search(line)
243 if (m):
244 add_asl(lineno, pasl.sub("", line));
245 # AML listing: offset in hex, then ...., then code
Kevin O'Connor616fb272013-02-10 21:06:14 -0500246 paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200247 m = paml.search(line)
248 if (m):
249 add_aml(m.group(1), paml.sub("", line))
250
251# Now go over code
252# Track AML offset of a previous non-empty ASL command
253prev_aml_offset = -1
254for i in range(len(asl)):
255 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
256
257 l = asl[i].line
258
259 # skip if not an extract directive
260 a = len(re.findall(r'ACPI_EXTRACT', l))
261 if (not a):
262 # If not empty, store AML offset. Will be used for sanity checks
263 # IASL seems to put {}. at random places in the listing.
264 # Ignore any non-words for the purpose of this test.
265 m = re.search(r'\w+', l)
266 if (m):
267 prev_aml_offset = asl[i].aml_offset
268 continue
269
270 if (a > 1):
271 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
272
273 mext = re.search(r'''
274 ^\s* # leading whitespace
275 /\*\s* # start C comment
276 (ACPI_EXTRACT_\w+) # directive: group(1)
277 \s+ # whitspace separates directive from array name
278 (\w+) # array name: group(2)
279 \s*\*/ # end of C comment
280 \s*$ # trailing whitespace
281 ''', l, re.VERBOSE)
282 if (not mext):
283 die("Stray ACPI_EXTRACT in input")
284
285 # previous command must have produced some AML,
286 # otherwise we are in a middle of a block
287 if (prev_aml_offset == asl[i].aml_offset):
288 die("ACPI_EXTRACT directive in the middle of a block")
289
290 directive = mext.group(1)
291 array = mext.group(2)
292 offset = asl[i].aml_offset
293
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200294 if (directive == "ACPI_EXTRACT_ALL_CODE"):
295 if array in output:
296 die("%s directive used more than once" % directive)
297 output[array] = aml
298 continue
Kevin O'Connor76e58022013-03-06 21:50:09 -0500299 if (directive == "ACPI_EXTRACT_NAME_BUFFER8"):
300 offset = aml_name_buffer8(offset)
301 elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200302 offset = aml_name_dword_const(offset)
303 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
304 offset = aml_name_word_const(offset)
305 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
306 offset = aml_name_byte_const(offset)
307 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
308 offset = aml_name_string(offset)
309 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
310 offset = aml_method_string(offset)
Paolo Bonzini2b568eb2012-08-02 15:07:24 +0200311 elif (directive == "ACPI_EXTRACT_DEVICE_START"):
312 offset = aml_device_start(offset)
313 elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
314 offset = aml_device_string(offset)
315 elif (directive == "ACPI_EXTRACT_DEVICE_END"):
316 offset = aml_device_end(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200317 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
318 offset = aml_processor_start(offset)
319 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
320 offset = aml_processor_string(offset)
321 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
322 offset = aml_processor_end(offset)
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300323 elif (directive == "ACPI_EXTRACT_PKG_START"):
324 offset = aml_package_start(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200325 else:
326 die("Unsupported directive %s" % directive)
327
328 if array not in output:
329 output[array] = []
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200330 output[array].append(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200331
332debug = "at end of file"
333
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200334def get_value_type(maxvalue):
335 #Use type large enough to fit the table
336 if (maxvalue >= 0x10000):
337 return "int"
338 elif (maxvalue >= 0x100):
339 return "short"
340 else:
341 return "char"
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200342
343# Pretty print output
344for array in output.keys():
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200345 otype = get_value_type(max(output[array]))
346 odata = []
347 for value in output[array]:
348 odata.append("0x%x" % value)
349 sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
350 sys.stdout.write(",\n".join(odata))
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200351 sys.stdout.write('\n};\n');