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
33 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
50 sys.stderr.write("Error: %s; %s\n" % (diag, debug))
53 #Store an ASL command, matching AML offset, and input line (for debugging)
54 def add_asl(lineno, line):
58 l.aml_offset = len(aml)
61 #Store an AML byte sequence
62 #Verify that offset output by iasl matches # of bytes so far
63 def add_aml(offset, line):
65 # Sanity check: offset must match size of code so far
67 die("Offset 0x%x != 0x%x" % (o, len(aml)))
68 # Strip any trailing dots and ASCII dump after "
69 line = re.sub(r'\s*\.*\s*".*$',"", line)
70 # Strip traling whitespace
71 line = re.sub(r'\s+$',"", line)
72 # Strip leading whitespace
73 line = re.sub(r'^\s+',"", line)
75 code = re.split(r'\s+', line)
77 # Require a legal hex number, two digits
78 if (not(re.search(r'^[0-9A-Fa-f][0-9A-Fa-f]$', c))):
79 die("Unexpected octet %s" % c);
80 aml.append(int(c, 16));
82 # Process aml bytecode array, decoding AML
83 def aml_pkglen_bytes(offset):
84 # PkgLength can be multibyte. Bits 8-7 give the # of extra bytes.
85 pkglenbytes = aml[offset] >> 6;
86 return pkglenbytes + 1
88 def aml_pkglen(offset):
90 pkglenbytes = aml_pkglen_bytes(offset)
91 pkglen = aml[offset] & 0x3F
92 # If multibyte, first nibble only uses bits 0-3
93 if ((pkglenbytes > 0) and (pkglen & 0x30)):
94 die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
98 for i in range(pkglenbytes):
99 pkglen |= aml[offset + i] << (i * 8 + 4)
100 if (len(aml) < pkgstart + pkglen):
101 die("PckgLen 0x%x at offset 0x%x exceeds AML size 0x%x" %
102 (pkglen, offset, len(aml)))
105 # Given method offset, find its NameString offset
106 def aml_method_string(offset):
107 #0x14 MethodOp PkgLength NameString MethodFlags TermList
108 if (aml[offset] != 0x14):
109 die( "Method offset 0x%x: expected 0x14 actual 0x%x" %
110 (offset, aml[offset]));
112 pkglenbytes = aml_pkglen_bytes(offset)
113 offset += pkglenbytes;
116 # Given name offset, find its NameString offset
117 def aml_name_string(offset):
118 #0x08 NameOp NameString DataRef
119 if (aml[offset] != 0x08):
120 die( "Name offset 0x%x: expected 0x08 actual 0x%x" %
121 (offset, aml[offset]));
124 # Given data offset, find dword const offset
125 def aml_data_dword_const(offset):
126 #0x08 NameOp NameString DataRef
127 if (aml[offset] != 0x0C):
128 die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
129 (offset, aml[offset]));
132 # Given data offset, find word const offset
133 def aml_data_word_const(offset):
134 #0x08 NameOp NameString DataRef
135 if (aml[offset] != 0x0B):
136 die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
137 (offset, aml[offset]));
140 # Given data offset, find byte const offset
141 def aml_data_byte_const(offset):
142 #0x08 NameOp NameString DataRef
143 if (aml[offset] != 0x0A):
144 die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
145 (offset, aml[offset]));
148 # Given name offset, find dword const offset
149 def aml_name_dword_const(offset):
150 return aml_data_dword_const(aml_name_string(offset) + 4)
152 # Given name offset, find word const offset
153 def aml_name_word_const(offset):
154 return aml_data_word_const(aml_name_string(offset) + 4)
156 # Given name offset, find byte const offset
157 def aml_name_byte_const(offset):
158 return aml_data_byte_const(aml_name_string(offset) + 4)
160 def aml_processor_start(offset):
161 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
162 if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
163 die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
164 (offset, aml[offset], aml[offset + 1]));
167 def aml_processor_string(offset):
168 #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
169 start = aml_processor_start(offset)
171 pkglenbytes = aml_pkglen_bytes(offset)
172 offset += pkglenbytes
175 def aml_processor_end(offset):
176 start = aml_processor_start(offset)
178 pkglenbytes = aml_pkglen_bytes(offset)
179 pkglen = aml_pkglen(offset)
180 return offset + pkglen
183 for line in fileinput.input():
184 # Strip trailing newline
185 line = line.rstrip();
186 # line number and debug string to output in case of errors
188 debug = "input line %d: %s" % (lineno, line)
189 #ASL listing: space, then line#, then ...., then code
190 pasl = re.compile('^\s+([0-9]+)\.\.\.\.\s*')
191 m = pasl.search(line)
193 add_asl(lineno, pasl.sub("", line));
194 # AML listing: offset in hex, then ...., then code
195 paml = re.compile('^([0-9A-Fa-f]+)\.\.\.\.\s*')
196 m = paml.search(line)
198 add_aml(m.group(1), paml.sub("", line))
201 # Track AML offset of a previous non-empty ASL command
203 for i in range(len(asl)):
204 debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
208 # skip if not an extract directive
209 a = len(re.findall(r'ACPI_EXTRACT', l))
211 # If not empty, store AML offset. Will be used for sanity checks
212 # IASL seems to put {}. at random places in the listing.
213 # Ignore any non-words for the purpose of this test.
214 m = re.search(r'\w+', l)
216 prev_aml_offset = asl[i].aml_offset
220 die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
222 mext = re.search(r'''
223 ^\s* # leading whitespace
224 /\*\s* # start C comment
225 (ACPI_EXTRACT_\w+) # directive: group(1)
226 \s+ # whitspace separates directive from array name
227 (\w+) # array name: group(2)
228 \s*\*/ # end of C comment
229 \s*$ # trailing whitespace
232 die("Stray ACPI_EXTRACT in input")
234 # previous command must have produced some AML,
235 # otherwise we are in a middle of a block
236 if (prev_aml_offset == asl[i].aml_offset):
237 die("ACPI_EXTRACT directive in the middle of a block")
239 directive = mext.group(1)
240 array = mext.group(2)
241 offset = asl[i].aml_offset
243 if (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
244 offset = aml_name_dword_const(offset)
245 elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
246 offset = aml_name_word_const(offset)
247 elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
248 offset = aml_name_byte_const(offset)
249 elif (directive == "ACPI_EXTRACT_NAME_STRING"):
250 offset = aml_name_string(offset)
251 elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
252 offset = aml_method_string(offset)
253 elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
254 offset = aml_processor_start(offset)
255 elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
256 offset = aml_processor_string(offset)
257 elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
258 offset = aml_processor_end(offset)
260 die("Unsupported directive %s" % directive)
262 if array not in output:
264 output[array].append("0x%x" % offset)
266 debug = "at end of file"
268 #Use type large enough to fit the table
269 if (len(aml) >= 0x10000):
271 elif (len(aml) >= 0x100):
276 # Pretty print output
277 for array in output.keys():
279 sys.stdout.write("static unsigned %s %s[] = {\n" % (offsettype, array))
280 sys.stdout.write(",\n".join(output[array]))
281 sys.stdout.write('\n};\n');