acpi: generate and parse mixed asl/aml listing
[seabios.git] / tools / acpi_extract.py
1 #!/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
32
33 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
34
35 import re;
36 import sys;
37 import fileinput;
38
39 aml = []
40 asl = []
41 output = {}
42 debug = ""
43
44 class asl_line:
45     line = None
46     lineno = None
47     aml_offset = None
48
49 def die(diag):
50     sys.stderr.write("Error: %s; %s\n" % (diag, debug))
51     sys.exit(1)
52     
53 #Store an ASL command, matching AML offset, and input line (for debugging)
54 def add_asl(lineno, line):
55     l = asl_line()
56     l.line = line
57     l.lineno = lineno
58     l.aml_offset = len(aml)
59     asl.append(l)
60
61 #Store an AML byte sequence
62 #Verify that offset output by iasl matches # of bytes so far
63 def add_aml(offset, line):
64     o = int(offset, 16);
65     # Sanity check: offset must match size of code so far
66     if (o != len(aml)):
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)
74     # Split on whitespace
75     code = re.split(r'\s+', line)
76     for c in code:
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));
81
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
87
88 def aml_pkglen(offset):
89     pkgstart = 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" %
95             (pkglen, pkglen))
96     offset += 1
97     pkglenbytes -= 1
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)))
103     return pkglen
104
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]));
111     offset += 1;
112     pkglenbytes = aml_pkglen_bytes(offset)
113     offset += pkglenbytes;
114     return offset;
115
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]));
122     return offset + 1;
123
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]));
130     return offset + 1;
131
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]));
138     return offset + 1;
139
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]));
146     return offset + 1;
147
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)
151
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)
155
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)
159
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]));
165     return offset
166
167 def aml_processor_string(offset):
168     #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
169     start = aml_processor_start(offset)
170     offset += 2
171     pkglenbytes = aml_pkglen_bytes(offset)
172     offset += pkglenbytes
173     return offset
174
175 def aml_processor_end(offset):
176     start = aml_processor_start(offset)
177     offset += 2
178     pkglenbytes = aml_pkglen_bytes(offset)
179     pkglen = aml_pkglen(offset)
180     return offset + pkglen
181
182 lineno = 0
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
187     lineno = lineno + 1
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)
192     if (m):
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)
197     if (m):
198         add_aml(m.group(1), paml.sub("", line))
199
200 # Now go over code
201 # Track AML offset of a previous non-empty ASL command
202 prev_aml_offset = -1
203 for i in range(len(asl)):
204     debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
205
206     l = asl[i].line
207
208     # skip if not an extract directive
209     a = len(re.findall(r'ACPI_EXTRACT', l))
210     if (not a):
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)
215         if (m):
216                 prev_aml_offset = asl[i].aml_offset
217         continue
218
219     if (a > 1):
220         die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
221
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
230                       ''', l, re.VERBOSE)
231     if (not mext):
232         die("Stray ACPI_EXTRACT in input")
233
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")
238
239     directive = mext.group(1)
240     array = mext.group(2)
241     offset = asl[i].aml_offset
242
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)
259     else:
260         die("Unsupported directive %s" % directive)
261
262     if array not in output:
263         output[array] = []
264     output[array].append("0x%x" % offset)
265
266 debug = "at end of file"
267
268 #Use type large enough to fit the table
269 if (len(aml) >= 0x10000):
270         offsettype = "int"
271 elif (len(aml) >= 0x100):
272         offsettype = "short"
273 else:
274         offsettype = "char"
275
276 # Pretty print output
277 for array in output.keys():
278     
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');