2 # Copyright (C) 2011 Red Hat, Inc., Michael S. Tsirkin <mst@redhat.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, see <http://www.gnu.org/licenses/>.
17 # Process mixed ASL/AML listing (.lst file) produced by iasl -l
18 # Locate and execute ACPI_EXTRACT directives, output offset info
20 # Documentation of ACPI_EXTRACT_* directive tags:
22 # These directive tags output offset information from AML for BIOS runtime
24 # Each directive is of the form:
25 # ACPI_EXTRACT_<TYPE> <array_name> <Operator> (...)
26 # and causes the extractor to create an array
27 # named <array_name> with offset, in the generated AML,
28 # of an object of a given type in the following <Operator>.
30 # A directive must fit on a single code line.
32 # Object type in AML is verified, a mismatch causes a build failure.
34 # Directives and operators currently supported are:
35 # ACPI_EXTRACT_NAME_DWORD_CONST - extract a Dword Const object from Name()
36 # ACPI_EXTRACT_NAME_WORD_CONST - extract a Word Const object from Name()
37 # ACPI_EXTRACT_NAME_BYTE_CONST - extract a Byte Const object from Name()
38 # ACPI_EXTRACT_METHOD_STRING - extract a NameString from Method()
39 # ACPI_EXTRACT_NAME_STRING - extract a NameString from Name()
40 # ACPI_EXTRACT_PROCESSOR_START - start of Processor() block
41 # ACPI_EXTRACT_PROCESSOR_STRING - extract a NameString from Processor()
42 # ACPI_EXTRACT_PROCESSOR_END - offset at last byte of Processor() + 1
43 # ACPI_EXTRACT_PKG_START - start of Package block
45 # ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
47 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
64 sys
.stderr
.write("Error: %s; %s\n" % (diag
, debug
))
67 #Store an ASL command, matching AML offset, and input line (for debugging)
68 def add_asl(lineno
, line
):
72 l
.aml_offset
= len(aml
)
75 #Store an AML byte sequence
76 #Verify that offset output by iasl matches # of bytes so far
77 def add_aml(offset
, line
):
79 # Sanity check: offset must match size of code so far
81 die("Offset 0x%x != 0x%x" % (o
, len(aml
)))
82 # Strip any trailing dots and ASCII dump after "
83 line
= re
.sub(r
'\s*\.*\s*".*$',"", line
)
84 # Strip traling whitespace
85 line
= re
.sub(r
'\s+$',"", line
)
86 # Strip leading whitespace
87 line
= re
.sub(r
'^\s+',"", line
)
89 code
= re
.split(r
'\s+', line
)
91 # Require a legal hex number, two digits
92 if (not(re
.search(r
'^[0-9A-Fa-f][0-9A-Fa-f]$', c
))):
93 die("Unexpected octet %s" % c
);
94 aml
.append(int(c
, 16));
96 # Process aml bytecode array, decoding AML
97 def aml_pkglen_bytes(offset
):
98 # PkgLength can be multibyte. Bits 8-7 give the # of extra bytes.
99 pkglenbytes
= aml
[offset
] >> 6;
100 return pkglenbytes
+ 1
102 def aml_pkglen(offset
):
104 pkglenbytes
= aml_pkglen_bytes(offset
)
105 pkglen
= aml
[offset
] & 0x3F
106 # If multibyte, first nibble only uses bits 0-3
107 if ((pkglenbytes
> 1) and (pkglen
& 0x30)):
108 die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
112 for i
in range(pkglenbytes
):
113 pkglen |
= aml
[offset
+ i
] << (i
* 8 + 4)
114 if (len(aml
) < pkgstart
+ pkglen
):
115 die("PckgLen 0x%x at offset 0x%x exceeds AML size 0x%x" %
116 (pkglen
, offset
, len(aml
)))
119 # Given method offset, find its NameString offset
120 def aml_method_string(offset
):
121 #0x14 MethodOp PkgLength NameString MethodFlags TermList
122 if (aml
[offset
] != 0x14):
123 die( "Method offset 0x%x: expected 0x14 actual 0x%x" %
124 (offset
, aml
[offset
]));
126 pkglenbytes
= aml_pkglen_bytes(offset
)
127 offset
+= pkglenbytes
;
130 # Given name offset, find its NameString offset
131 def aml_name_string(offset
):
132 #0x08 NameOp NameString DataRef
133 if (aml
[offset
] != 0x08):
134 die( "Name offset 0x%x: expected 0x08 actual 0x%x" %
135 (offset
, aml
[offset
]));
137 # Block Name Modifier. Skip it.
138 if (aml
[offset
] == 0x5c or aml
[offset
] == 0x5e):
142 # Given data offset, find variable length byte buffer offset
143 def aml_data_buffer(offset
, length
):
144 #0x11 PkgLength BufferSize ByteList
146 die( "Name offset 0x%x: expected a one byte PkgLength (length<=63)" %
148 expect
= [0x11, length
+3, 0x0A, length
]
149 if (aml
[offset
:offset
+4] != expect
):
150 die( "Name offset 0x%x: expected %s actual %s" %
151 (offset
, expect
, aml
[offset
:offset
+4]))
152 return offset
+ len(expect
)
154 # Given data offset, find dword const offset
155 def aml_data_dword_const(offset
):
156 #0x08 NameOp NameString DataRef
157 if (aml
[offset
] != 0x0C):
158 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
159 (offset
, aml
[offset
]));
162 # Given data offset, find word const offset
163 def aml_data_word_const(offset
):
164 #0x08 NameOp NameString DataRef
165 if (aml
[offset
] != 0x0B):
166 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
167 (offset
, aml
[offset
]));
170 # Given data offset, find byte const offset
171 def aml_data_byte_const(offset
):
172 #0x08 NameOp NameString DataRef
173 if (aml
[offset
] != 0x0A):
174 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
175 (offset
, aml
[offset
]));
179 def aml_name_buffer(offset
, length
):
180 return aml_data_buffer(aml_name_string(offset
) + 4, length
)
182 # Given name offset, find dword const offset
183 def aml_name_dword_const(offset
):
184 return aml_data_dword_const(aml_name_string(offset
) + 4)
186 # Given name offset, find word const offset
187 def aml_name_word_const(offset
):
188 return aml_data_word_const(aml_name_string(offset
) + 4)
190 # Given name offset, find byte const offset
191 def aml_name_byte_const(offset
):
192 return aml_data_byte_const(aml_name_string(offset
) + 4)
194 def aml_device_start(offset
):
195 #0x5B 0x82 DeviceOp PkgLength NameString
196 if ((aml
[offset
] != 0x5B) or (aml
[offset
+ 1] != 0x82)):
197 die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
198 (offset
, aml
[offset
], aml
[offset
+ 1]));
201 def aml_device_string(offset
):
202 #0x5B 0x82 DeviceOp PkgLength NameString
203 start
= aml_device_start(offset
)
205 pkglenbytes
= aml_pkglen_bytes(offset
)
206 offset
+= pkglenbytes
209 def aml_device_end(offset
):
210 start
= aml_device_start(offset
)
212 pkglenbytes
= aml_pkglen_bytes(offset
)
213 pkglen
= aml_pkglen(offset
)
214 return offset
+ pkglen
216 def aml_processor_start(offset
):
217 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
218 if ((aml
[offset
] != 0x5B) or (aml
[offset
+ 1] != 0x83)):
219 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
220 (offset
, aml
[offset
], aml
[offset
+ 1]));
223 def aml_processor_string(offset
):
224 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
225 start
= aml_processor_start(offset
)
227 pkglenbytes
= aml_pkglen_bytes(offset
)
228 offset
+= pkglenbytes
231 def aml_processor_end(offset
):
232 start
= aml_processor_start(offset
)
234 pkglenbytes
= aml_pkglen_bytes(offset
)
235 pkglen
= aml_pkglen(offset
)
236 return offset
+ pkglen
238 def aml_package_start(offset
):
239 offset
= aml_name_string(offset
) + 4
240 # 0x12 PkgLength NumElements PackageElementList
241 if (aml
[offset
] != 0x12):
242 die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
243 (offset
, aml
[offset
]));
245 return offset
+ aml_pkglen_bytes(offset
) + 1
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
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
)
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
)
263 add_aml(m
.group(1), paml
.sub("", line
))
266 # Track AML offset of a previous non-empty ASL command
268 for i
in range(len(asl
)):
269 debug
= "input line %d: %s" % (asl
[i
].lineno
, asl
[i
].line
)
273 # skip if not an extract directive
274 a
= len(re
.findall(r
'ACPI_EXTRACT', l
))
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
)
281 prev_aml_offset
= asl
[i
].aml_offset
285 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a
)
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
297 die("Stray ACPI_EXTRACT in input")
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")
304 directive
= mext
.group(1)
305 array
= mext
.group(2)
306 offset
= asl
[i
].aml_offset
308 if (directive
== "ACPI_EXTRACT_ALL_CODE"):
310 die("%s directive used more than once" % directive
)
313 if (directive
== "ACPI_EXTRACT_NAME_BUFFER8"):
314 offset
= aml_name_buffer(offset
, 8)
315 elif (directive
== "ACPI_EXTRACT_NAME_BUFFER16"):
316 offset
= aml_name_buffer(offset
, 16)
317 elif (directive
== "ACPI_EXTRACT_NAME_DWORD_CONST"):
318 offset
= aml_name_dword_const(offset
)
319 elif (directive
== "ACPI_EXTRACT_NAME_WORD_CONST"):
320 offset
= aml_name_word_const(offset
)
321 elif (directive
== "ACPI_EXTRACT_NAME_BYTE_CONST"):
322 offset
= aml_name_byte_const(offset
)
323 elif (directive
== "ACPI_EXTRACT_NAME_STRING"):
324 offset
= aml_name_string(offset
)
325 elif (directive
== "ACPI_EXTRACT_METHOD_STRING"):
326 offset
= aml_method_string(offset
)
327 elif (directive
== "ACPI_EXTRACT_DEVICE_START"):
328 offset
= aml_device_start(offset
)
329 elif (directive
== "ACPI_EXTRACT_DEVICE_STRING"):
330 offset
= aml_device_string(offset
)
331 elif (directive
== "ACPI_EXTRACT_DEVICE_END"):
332 offset
= aml_device_end(offset
)
333 elif (directive
== "ACPI_EXTRACT_PROCESSOR_START"):
334 offset
= aml_processor_start(offset
)
335 elif (directive
== "ACPI_EXTRACT_PROCESSOR_STRING"):
336 offset
= aml_processor_string(offset
)
337 elif (directive
== "ACPI_EXTRACT_PROCESSOR_END"):
338 offset
= aml_processor_end(offset
)
339 elif (directive
== "ACPI_EXTRACT_PKG_START"):
340 offset
= aml_package_start(offset
)
342 die("Unsupported directive %s" % directive
)
344 if array
not in output
:
346 output
[array
].append(offset
)
348 debug
= "at end of file"
350 def get_value_type(maxvalue
):
351 #Use type large enough to fit the table
352 if (maxvalue
>= 0x10000):
354 elif (maxvalue
>= 0x100):
359 # Pretty print output
360 for array
in output
.keys():
361 otype
= get_value_type(max(output
[array
]))
363 for value
in output
[array
]:
364 odata
.append("0x%x" % value
)
365 sys
.stdout
.write("static unsigned %s %s[] = {\n" % (otype
, array
))
366 sys
.stdout
.write(",\n".join(odata
))
367 sys
.stdout
.write('\n};\n');