blob: 167a322a3c9b24b4fe8bf21ec64fc906aa787cab [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
96 if ((pkglenbytes > 0) and (pkglen & 0x30)):
97 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
167def aml_processor_start(offset):
168 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
169 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
170 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
171 (offset, aml[offset], aml[offset + 1]));
172 return offset
173
174def aml_processor_string(offset):
175 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
176 start = aml_processor_start(offset)
177 offset += 2
178 pkglenbytes = aml_pkglen_bytes(offset)
179 offset += pkglenbytes
180 return offset
181
182def aml_processor_end(offset):
183 start = aml_processor_start(offset)
184 offset += 2
185 pkglenbytes = aml_pkglen_bytes(offset)
186 pkglen = aml_pkglen(offset)
187 return offset + pkglen
188
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300189def aml_package_start(offset):
190 offset = aml_name_string(offset) + 4
191 # 0x12 PkgLength NumElements PackageElementList
192 if (aml[offset] != 0x12):
193 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
194 (offset, aml[offset]));
195 offset += 1
196 return offset + aml_pkglen_bytes(offset) + 1
197
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200198lineno = 0
199for line in fileinput.input():
200 # Strip trailing newline
201 line = line.rstrip();
202 # line number and debug string to output in case of errors
203 lineno = lineno + 1
204 debug = "input line %d: %s" % (lineno, line)
205 #ASL listing: space, then line#, then ...., then code
206 pasl = re.compile('^\s+([0-9]+)\.\.\.\.\s*')
207 m = pasl.search(line)
208 if (m):
209 add_asl(lineno, pasl.sub("", line));
210 # AML listing: offset in hex, then ...., then code
211 paml = re.compile('^([0-9A-Fa-f]+)\.\.\.\.\s*')
212 m = paml.search(line)
213 if (m):
214 add_aml(m.group(1), paml.sub("", line))
215
216# Now go over code
217# Track AML offset of a previous non-empty ASL command
218prev_aml_offset = -1
219for i in range(len(asl)):
220 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
221
222 l = asl[i].line
223
224 # skip if not an extract directive
225 a = len(re.findall(r'ACPI_EXTRACT', l))
226 if (not a):
227 # If not empty, store AML offset. Will be used for sanity checks
228 # IASL seems to put {}. at random places in the listing.
229 # Ignore any non-words for the purpose of this test.
230 m = re.search(r'\w+', l)
231 if (m):
232 prev_aml_offset = asl[i].aml_offset
233 continue
234
235 if (a > 1):
236 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
237
238 mext = re.search(r'''
239 ^\s* # leading whitespace
240 /\*\s* # start C comment
241 (ACPI_EXTRACT_\w+) # directive: group(1)
242 \s+ # whitspace separates directive from array name
243 (\w+) # array name: group(2)
244 \s*\*/ # end of C comment
245 \s*$ # trailing whitespace
246 ''', l, re.VERBOSE)
247 if (not mext):
248 die("Stray ACPI_EXTRACT in input")
249
250 # previous command must have produced some AML,
251 # otherwise we are in a middle of a block
252 if (prev_aml_offset == asl[i].aml_offset):
253 die("ACPI_EXTRACT directive in the middle of a block")
254
255 directive = mext.group(1)
256 array = mext.group(2)
257 offset = asl[i].aml_offset
258
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200259 if (directive == "ACPI_EXTRACT_ALL_CODE"):
260 if array in output:
261 die("%s directive used more than once" % directive)
262 output[array] = aml
263 continue
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200264 if (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
265 offset = aml_name_dword_const(offset)
266 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
267 offset = aml_name_word_const(offset)
268 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
269 offset = aml_name_byte_const(offset)
270 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
271 offset = aml_name_string(offset)
272 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
273 offset = aml_method_string(offset)
274 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
275 offset = aml_processor_start(offset)
276 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
277 offset = aml_processor_string(offset)
278 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
279 offset = aml_processor_end(offset)
Gleb Natapov94bb3ca2012-05-20 12:03:39 +0300280 elif (directive == "ACPI_EXTRACT_PKG_START"):
281 offset = aml_package_start(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200282 else:
283 die("Unsupported directive %s" % directive)
284
285 if array not in output:
286 output[array] = []
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200287 output[array].append(offset)
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200288
289debug = "at end of file"
290
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200291def get_value_type(maxvalue):
292 #Use type large enough to fit the table
293 if (maxvalue >= 0x10000):
294 return "int"
295 elif (maxvalue >= 0x100):
296 return "short"
297 else:
298 return "char"
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200299
300# Pretty print output
301for array in output.keys():
Michael S. Tsirkin2e55b032011-10-26 23:28:02 +0200302 otype = get_value_type(max(output[array]))
303 odata = []
304 for value in output[array]:
305 odata.append("0x%x" % value)
306 sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
307 sys.stdout.write(",\n".join(odata))
Michael S. Tsirkin2597b752011-10-04 15:26:01 +0200308 sys.stdout.write('\n};\n');