Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / scripts / 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 # 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
36 #
37 # ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
38
39 # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
40
41 import re
42 import sys
43 import fileinput
44
45 aml = []
46 asl = []
47 output = {}
48 debug = ""
49
50 class asl_line:
51     line = None
52     lineno = None
53     aml_offset = None
54
55 def die(diag):
56     sys.stderr.write("Error: %s; %s\n" % (diag, debug))
57     sys.exit(1)
58
59 #Store an ASL command, matching AML offset, and input line (for debugging)
60 def add_asl(lineno, line):
61     l = asl_line()
62     l.line = line
63     l.lineno = lineno
64     l.aml_offset = len(aml)
65     asl.append(l)
66
67 #Store an AML byte sequence
68 #Verify that offset output by iasl matches # of bytes so far
69 def add_aml(offset, line):
70     o = int(offset, 16)
71     # Sanity check: offset must match size of code so far
72     if (o != len(aml)):
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)
80     # Split on whitespace
81     code = re.split(r'\s+', line)
82     for c in code:
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))
87
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
93
94 def aml_pkglen(offset):
95     pkgstart = 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" %
101             (pkglen, pkglen))
102     offset += 1
103     pkglenbytes -= 1
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)))
109     return pkglen
110
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]))
117     offset += 1
118     pkglenbytes = aml_pkglen_bytes(offset)
119     offset += pkglenbytes
120     return offset
121
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]))
128     offset += 1
129     # Block Name Modifier. Skip it.
130     if (aml[offset] == 0x5c or aml[offset] == 0x5e):
131         offset += 1
132     return offset
133
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)
142
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]))
149     return offset + 1
150
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]))
157     return offset + 1
158
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]))
165     return offset + 1
166
167 # Find name'd buffer8
168 def aml_name_buffer8(offset):
169     return aml_data_buffer8(aml_name_string(offset) + 4)
170
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)
174
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)
178
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)
182
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]))
188     return offset
189
190 def aml_device_string(offset):
191     #0x5B 0x82 DeviceOp PkgLength NameString
192     start = aml_device_start(offset)
193     offset += 2
194     pkglenbytes = aml_pkglen_bytes(offset)
195     offset += pkglenbytes
196     return offset
197
198 def aml_device_end(offset):
199     start = aml_device_start(offset)
200     offset += 2
201     pkglenbytes = aml_pkglen_bytes(offset)
202     pkglen = aml_pkglen(offset)
203     return offset + pkglen
204
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]))
210     return offset
211
212 def aml_processor_string(offset):
213     #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
214     start = aml_processor_start(offset)
215     offset += 2
216     pkglenbytes = aml_pkglen_bytes(offset)
217     offset += pkglenbytes
218     return offset
219
220 def aml_processor_end(offset):
221     start = aml_processor_start(offset)
222     offset += 2
223     pkglenbytes = aml_pkglen_bytes(offset)
224     pkglen = aml_pkglen(offset)
225     return offset + pkglen
226
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]))
233     offset += 1
234     return offset + aml_pkglen_bytes(offset) + 1
235
236 lineno = 0
237 for line in fileinput.input():
238     # Strip trailing newline
239     line = line.rstrip()
240     # line number and debug string to output in case of errors
241     lineno = lineno + 1
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)
246     if (m):
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)
251     if (m):
252         add_aml(m.group(1), paml.sub("", line))
253
254 # Now go over code
255 # Track AML offset of a previous non-empty ASL command
256 prev_aml_offset = -1
257 for i in range(len(asl)):
258     debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
259
260     l = asl[i].line
261
262     # skip if not an extract directive
263     a = len(re.findall(r'ACPI_EXTRACT', l))
264     if (not a):
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)
269         if (m):
270             prev_aml_offset = asl[i].aml_offset
271         continue
272
273     if (a > 1):
274         die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
275
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
284                       ''', l, re.VERBOSE)
285     if (not mext):
286         die("Stray ACPI_EXTRACT in input")
287
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")
292
293     directive = mext.group(1)
294     array = mext.group(2)
295     offset = asl[i].aml_offset
296
297     if (directive == "ACPI_EXTRACT_ALL_CODE"):
298         if array in output:
299             die("%s directive used more than once" % directive)
300         output[array] = aml
301         continue
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)
328     else:
329         die("Unsupported directive %s" % directive)
330
331     if array not in output:
332         output[array] = []
333     output[array].append(offset)
334
335 debug = "at end of file"
336
337 def get_value_type(maxvalue):
338     #Use type large enough to fit the table
339     if (maxvalue >= 0x10000):
340         return "int"
341     elif (maxvalue >= 0x100):
342         return "short"
343     else:
344         return "char"
345
346 # Pretty print output
347 for array in output.keys():
348     otype = get_value_type(max(output[array]))
349     odata = []
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')