2 # Copyright (C) 2011 Red Hat, Inc., Michael S. Tsirkin <mst@redhat.com>
4 # This file may be distributed under the terms of the GNU GPLv3 license.
6 # Process mixed ASL/AML listing (.lst file) produced by iasl -l
7 # Locate and execute ACPI_EXTRACT directives, output offset info
9 # Documentation of ACPI_EXTRACT_* directive tags:
11 # These directive tags output offset information from AML for BIOS runtime
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>.
19 # A directive must fit on a single code line.
21 # Object type in AML is verified, a mismatch causes a build failure.
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
32 # 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
35 # ACPI_EXTRACT_PKG_START - start of Package block
37 # ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
39 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
56 sys.stderr.write("Error: %s; %s\n" % (diag, debug))
59 #Store an ASL command, matching AML offset, and input line (for debugging)
60 def add_asl(lineno, line):
64 l.aml_offset = len(aml)
67 #Store an AML byte sequence
68 #Verify that offset output by iasl matches # of bytes so far
69 def add_aml(offset, line):
71 # Sanity check: offset must match size of code so far
73 die("Offset 0x%x != 0x%x" % (o, len(aml)))
74 # Strip any trailing dots and ASCII dump after "
75 line = re.sub(r'\s*\.*\s*".*$', "", line)
76 # Strip traling whitespace
77 line = re.sub(r'\s+$', "", line)
78 # Strip leading whitespace
79 line = re.sub(r'^\s+', "", line)
81 code = re.split(r'\s+', line)
83 # Require a legal hex number, two digits
84 if (not(re.search(r'^[0-9A-Fa-f][0-9A-Fa-f]$', c))):
85 die("Unexpected octet %s" % c)
86 aml.append(int(c, 16))
88 # Process aml bytecode array, decoding AML
89 def aml_pkglen_bytes(offset):
90 # PkgLength can be multibyte. Bits 8-7 give the # of extra bytes.
91 pkglenbytes = aml[offset] >> 6
92 return pkglenbytes + 1
94 def aml_pkglen(offset):
96 pkglenbytes = aml_pkglen_bytes(offset)
97 pkglen = aml[offset] & 0x3F
98 # If multibyte, first nibble only uses bits 0-3
99 if ((pkglenbytes > 1) and (pkglen & 0x30)):
100 die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
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)))
111 # Given method offset, find its NameString offset
112 def 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" %
116 (offset, aml[offset]))
118 pkglenbytes = aml_pkglen_bytes(offset)
119 offset += pkglenbytes
122 # Given name offset, find its NameString offset
123 def 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" %
127 (offset, aml[offset]))
129 # Block Name Modifier. Skip it.
130 if (aml[offset] == 0x5c or aml[offset] == 0x5e):
134 # Given data offset, find 8 byte buffer offset
135 def 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)
143 # Given data offset, find dword const offset
144 def 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" %
148 (offset, aml[offset]))
151 # Given data offset, find word const offset
152 def 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" %
156 (offset, aml[offset]))
159 # Given data offset, find byte const offset
160 def 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" %
164 (offset, aml[offset]))
167 # Find name'd buffer8
168 def aml_name_buffer8(offset):
169 return aml_data_buffer8(aml_name_string(offset) + 4)
171 # Given name offset, find dword const offset
172 def aml_name_dword_const(offset):
173 return aml_data_dword_const(aml_name_string(offset) + 4)
175 # Given name offset, find word const offset
176 def aml_name_word_const(offset):
177 return aml_data_word_const(aml_name_string(offset) + 4)
179 # Given name offset, find byte const offset
180 def aml_name_byte_const(offset):
181 return aml_data_byte_const(aml_name_string(offset) + 4)
183 def 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" %
187 (offset, aml[offset], aml[offset + 1]))
190 def aml_device_string(offset):
191 #0x5B 0x82 DeviceOp PkgLength NameString
192 start = aml_device_start(offset)
194 pkglenbytes = aml_pkglen_bytes(offset)
195 offset += pkglenbytes
198 def aml_device_end(offset):
199 start = aml_device_start(offset)
201 pkglenbytes = aml_pkglen_bytes(offset)
202 pkglen = aml_pkglen(offset)
203 return offset + pkglen
205 def 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" %
209 (offset, aml[offset], aml[offset + 1]))
212 def aml_processor_string(offset):
213 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
214 start = aml_processor_start(offset)
216 pkglenbytes = aml_pkglen_bytes(offset)
217 offset += pkglenbytes
220 def aml_processor_end(offset):
221 start = aml_processor_start(offset)
223 pkglenbytes = aml_pkglen_bytes(offset)
224 pkglen = aml_pkglen(offset)
225 return offset + pkglen
227 def 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" %
232 (offset, aml[offset]))
234 return offset + aml_pkglen_bytes(offset) + 1
237 for line in fileinput.input():
238 # Strip trailing newline
240 # line number and debug string to output in case of errors
242 debug = "input line %d: %s" % (lineno, line)
243 #ASL listing: space, then line#, then ...., then code
244 pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
245 m = pasl.search(line)
247 add_asl(lineno, pasl.sub("", line))
248 # AML listing: offset in hex, then ...., then code
249 paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
250 m = paml.search(line)
252 add_aml(m.group(1), paml.sub("", line))
255 # Track AML offset of a previous non-empty ASL command
257 for i in range(len(asl)):
258 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
262 # skip if not an extract directive
263 a = len(re.findall(r'ACPI_EXTRACT', l))
265 # If not empty, store AML offset. Will be used for sanity checks
266 # IASL seems to put {}. at random places in the listing.
267 # Ignore any non-words for the purpose of this test.
268 m = re.search(r'\w+', l)
270 prev_aml_offset = asl[i].aml_offset
274 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
276 mext = re.search(r'''
277 ^\s* # leading whitespace
278 /\*\s* # start C comment
279 (ACPI_EXTRACT_\w+) # directive: group(1)
280 \s+ # whitspace separates directive from array name
281 (\w+) # array name: group(2)
282 \s*\*/ # end of C comment
283 \s*$ # trailing whitespace
286 die("Stray ACPI_EXTRACT in input")
288 # previous command must have produced some AML,
289 # otherwise we are in a middle of a block
290 if (prev_aml_offset == asl[i].aml_offset):
291 die("ACPI_EXTRACT directive in the middle of a block")
293 directive = mext.group(1)
294 array = mext.group(2)
295 offset = asl[i].aml_offset
297 if (directive == "ACPI_EXTRACT_ALL_CODE"):
299 die("%s directive used more than once" % directive)
302 if (directive == "ACPI_EXTRACT_NAME_BUFFER8"):
303 offset = aml_name_buffer8(offset)
304 elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
305 offset = aml_name_dword_const(offset)
306 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
307 offset = aml_name_word_const(offset)
308 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
309 offset = aml_name_byte_const(offset)
310 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
311 offset = aml_name_string(offset)
312 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
313 offset = aml_method_string(offset)
314 elif (directive == "ACPI_EXTRACT_DEVICE_START"):
315 offset = aml_device_start(offset)
316 elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
317 offset = aml_device_string(offset)
318 elif (directive == "ACPI_EXTRACT_DEVICE_END"):
319 offset = aml_device_end(offset)
320 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
321 offset = aml_processor_start(offset)
322 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
323 offset = aml_processor_string(offset)
324 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
325 offset = aml_processor_end(offset)
326 elif (directive == "ACPI_EXTRACT_PKG_START"):
327 offset = aml_package_start(offset)
329 die("Unsupported directive %s" % directive)
331 if array not in output:
333 output[array].append(offset)
335 debug = "at end of file"
337 def get_value_type(maxvalue):
338 #Use type large enough to fit the table
339 if (maxvalue >= 0x10000):
341 elif (maxvalue >= 0x100):
346 # Pretty print output
347 for array in output.keys():
348 otype = get_value_type(max(output[array]))
350 for value in output[array]:
351 odata.append("0x%x" % value)
352 sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
353 sys.stdout.write(",\n".join(odata))
354 sys.stdout.write('\n};\n')