2
0

acpi_extract.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/usr/bin/python
  2. # Copyright (C) 2011 Red Hat, Inc., Michael S. Tsirkin <mst@redhat.com>
  3. #
  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.
  8. #
  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.
  13. #
  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/>.
  16. # Process mixed ASL/AML listing (.lst file) produced by iasl -l
  17. # Locate and execute ACPI_EXTRACT directives, output offset info
  18. #
  19. # Documentation of ACPI_EXTRACT_* directive tags:
  20. #
  21. # These directive tags output offset information from AML for BIOS runtime
  22. # table generation.
  23. # Each directive is of the form:
  24. # ACPI_EXTRACT_<TYPE> <array_name> <Operator> (...)
  25. # and causes the extractor to create an array
  26. # named <array_name> with offset, in the generated AML,
  27. # of an object of a given type in the following <Operator>.
  28. #
  29. # A directive must fit on a single code line.
  30. #
  31. # Object type in AML is verified, a mismatch causes a build failure.
  32. #
  33. # Directives and operators currently supported are:
  34. # ACPI_EXTRACT_NAME_DWORD_CONST - extract a Dword Const object from Name()
  35. # ACPI_EXTRACT_NAME_WORD_CONST - extract a Word Const object from Name()
  36. # ACPI_EXTRACT_NAME_BYTE_CONST - extract a Byte Const object from Name()
  37. # ACPI_EXTRACT_METHOD_STRING - extract a NameString from Method()
  38. # ACPI_EXTRACT_NAME_STRING - extract a NameString from Name()
  39. # ACPI_EXTRACT_PROCESSOR_START - start of Processor() block
  40. # ACPI_EXTRACT_PROCESSOR_STRING - extract a NameString from Processor()
  41. # ACPI_EXTRACT_PROCESSOR_END - offset at last byte of Processor() + 1
  42. # ACPI_EXTRACT_PKG_START - start of Package block
  43. #
  44. # ACPI_EXTRACT_ALL_CODE - create an array storing the generated AML bytecode
  45. #
  46. # ACPI_EXTRACT is not allowed anywhere else in code, except in comments.
  47. import re;
  48. import sys;
  49. import fileinput;
  50. aml = []
  51. asl = []
  52. output = {}
  53. debug = ""
  54. class asl_line:
  55. line = None
  56. lineno = None
  57. aml_offset = None
  58. def die(diag):
  59. sys.stderr.write("Error: %s; %s\n" % (diag, debug))
  60. sys.exit(1)
  61. #Store an ASL command, matching AML offset, and input line (for debugging)
  62. def add_asl(lineno, line):
  63. l = asl_line()
  64. l.line = line
  65. l.lineno = lineno
  66. l.aml_offset = len(aml)
  67. asl.append(l)
  68. #Store an AML byte sequence
  69. #Verify that offset output by iasl matches # of bytes so far
  70. def add_aml(offset, line):
  71. o = int(offset, 16);
  72. # Sanity check: offset must match size of code so far
  73. if (o != len(aml)):
  74. die("Offset 0x%x != 0x%x" % (o, len(aml)))
  75. # Strip any trailing dots and ASCII dump after "
  76. line = re.sub(r'\s*\.*\s*".*$',"", line)
  77. # Strip traling whitespace
  78. line = re.sub(r'\s+$',"", line)
  79. # Strip leading whitespace
  80. line = re.sub(r'^\s+',"", line)
  81. # Split on whitespace
  82. code = re.split(r'\s+', line)
  83. for c in code:
  84. # Require a legal hex number, two digits
  85. if (not(re.search(r'^[0-9A-Fa-f][0-9A-Fa-f]$', c))):
  86. die("Unexpected octet %s" % c);
  87. 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
  93. def aml_pkglen(offset):
  94. pkgstart = offset
  95. pkglenbytes = aml_pkglen_bytes(offset)
  96. pkglen = aml[offset] & 0x3F
  97. # If multibyte, first nibble only uses bits 0-3
  98. if ((pkglenbytes > 1) and (pkglen & 0x30)):
  99. die("PkgLen bytes 0x%x but first nibble 0x%x expected 0x0X" %
  100. (pkglen, pkglen))
  101. offset += 1
  102. pkglenbytes -= 1
  103. for i in range(pkglenbytes):
  104. pkglen |= aml[offset + i] << (i * 8 + 4)
  105. if (len(aml) < pkgstart + pkglen):
  106. die("PckgLen 0x%x at offset 0x%x exceeds AML size 0x%x" %
  107. (pkglen, offset, len(aml)))
  108. return pkglen
  109. # Given method offset, find its NameString offset
  110. def aml_method_string(offset):
  111. #0x14 MethodOp PkgLength NameString MethodFlags TermList
  112. if (aml[offset] != 0x14):
  113. die( "Method offset 0x%x: expected 0x14 actual 0x%x" %
  114. (offset, aml[offset]));
  115. offset += 1;
  116. pkglenbytes = aml_pkglen_bytes(offset)
  117. offset += pkglenbytes;
  118. return offset;
  119. # Given name offset, find its NameString offset
  120. def aml_name_string(offset):
  121. #0x08 NameOp NameString DataRef
  122. if (aml[offset] != 0x08):
  123. die( "Name offset 0x%x: expected 0x08 actual 0x%x" %
  124. (offset, aml[offset]));
  125. offset += 1
  126. # Block Name Modifier. Skip it.
  127. if (aml[offset] == 0x5c or aml[offset] == 0x5e):
  128. offset += 1
  129. return offset;
  130. # Given data offset, find variable length byte buffer offset
  131. def aml_data_buffer(offset, length):
  132. #0x11 PkgLength BufferSize ByteList
  133. if (length > 63):
  134. die( "Name offset 0x%x: expected a one byte PkgLength (length<=63)" %
  135. (offset));
  136. expect = [0x11, length+3, 0x0A, length]
  137. if (aml[offset:offset+4] != expect):
  138. die( "Name offset 0x%x: expected %s actual %s" %
  139. (offset, expect, aml[offset:offset+4]))
  140. return offset + len(expect)
  141. # Given data offset, find dword const offset
  142. def aml_data_dword_const(offset):
  143. #0x08 NameOp NameString DataRef
  144. if (aml[offset] != 0x0C):
  145. die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
  146. (offset, aml[offset]));
  147. return offset + 1;
  148. # Given data offset, find word const offset
  149. def aml_data_word_const(offset):
  150. #0x08 NameOp NameString DataRef
  151. if (aml[offset] != 0x0B):
  152. die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
  153. (offset, aml[offset]));
  154. return offset + 1;
  155. # Given data offset, find byte const offset
  156. def aml_data_byte_const(offset):
  157. #0x08 NameOp NameString DataRef
  158. if (aml[offset] != 0x0A):
  159. die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
  160. (offset, aml[offset]));
  161. return offset + 1;
  162. # Find name'd buffer
  163. def aml_name_buffer(offset, length):
  164. return aml_data_buffer(aml_name_string(offset) + 4, length)
  165. # Given name offset, find dword const offset
  166. def aml_name_dword_const(offset):
  167. return aml_data_dword_const(aml_name_string(offset) + 4)
  168. # Given name offset, find word const offset
  169. def aml_name_word_const(offset):
  170. return aml_data_word_const(aml_name_string(offset) + 4)
  171. # Given name offset, find byte const offset
  172. def aml_name_byte_const(offset):
  173. return aml_data_byte_const(aml_name_string(offset) + 4)
  174. def aml_device_start(offset):
  175. #0x5B 0x82 DeviceOp PkgLength NameString
  176. if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
  177. die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
  178. (offset, aml[offset], aml[offset + 1]));
  179. return offset
  180. def aml_device_string(offset):
  181. #0x5B 0x82 DeviceOp PkgLength NameString
  182. start = aml_device_start(offset)
  183. offset += 2
  184. pkglenbytes = aml_pkglen_bytes(offset)
  185. offset += pkglenbytes
  186. return offset
  187. def aml_device_end(offset):
  188. start = aml_device_start(offset)
  189. offset += 2
  190. pkglenbytes = aml_pkglen_bytes(offset)
  191. pkglen = aml_pkglen(offset)
  192. return offset + pkglen
  193. def aml_processor_start(offset):
  194. #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
  195. if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
  196. die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
  197. (offset, aml[offset], aml[offset + 1]));
  198. return offset
  199. def aml_processor_string(offset):
  200. #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
  201. start = aml_processor_start(offset)
  202. offset += 2
  203. pkglenbytes = aml_pkglen_bytes(offset)
  204. offset += pkglenbytes
  205. return offset
  206. def aml_processor_end(offset):
  207. start = aml_processor_start(offset)
  208. offset += 2
  209. pkglenbytes = aml_pkglen_bytes(offset)
  210. pkglen = aml_pkglen(offset)
  211. return offset + pkglen
  212. def aml_package_start(offset):
  213. offset = aml_name_string(offset) + 4
  214. # 0x12 PkgLength NumElements PackageElementList
  215. if (aml[offset] != 0x12):
  216. die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
  217. (offset, aml[offset]));
  218. offset += 1
  219. return offset + aml_pkglen_bytes(offset) + 1
  220. lineno = 0
  221. for line in fileinput.input():
  222. # Strip trailing newline
  223. line = line.rstrip();
  224. # line number and debug string to output in case of errors
  225. lineno = lineno + 1
  226. debug = "input line %d: %s" % (lineno, line)
  227. #ASL listing: space, then line#, then ...., then code
  228. pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
  229. m = pasl.search(line)
  230. if (m):
  231. add_asl(lineno, pasl.sub("", line));
  232. # AML listing: offset in hex, then ...., then code
  233. paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
  234. m = paml.search(line)
  235. if (m):
  236. add_aml(m.group(1), paml.sub("", line))
  237. # Now go over code
  238. # Track AML offset of a previous non-empty ASL command
  239. prev_aml_offset = -1
  240. for i in range(len(asl)):
  241. debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
  242. l = asl[i].line
  243. # skip if not an extract directive
  244. a = len(re.findall(r'ACPI_EXTRACT', l))
  245. if (not a):
  246. # If not empty, store AML offset. Will be used for sanity checks
  247. # IASL seems to put {}. at random places in the listing.
  248. # Ignore any non-words for the purpose of this test.
  249. m = re.search(r'\w+', l)
  250. if (m):
  251. prev_aml_offset = asl[i].aml_offset
  252. continue
  253. if (a > 1):
  254. die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
  255. mext = re.search(r'''
  256. ^\s* # leading whitespace
  257. /\*\s* # start C comment
  258. (ACPI_EXTRACT_\w+) # directive: group(1)
  259. \s+ # whitspace separates directive from array name
  260. (\w+) # array name: group(2)
  261. \s*\*/ # end of C comment
  262. \s*$ # trailing whitespace
  263. ''', l, re.VERBOSE)
  264. if (not mext):
  265. die("Stray ACPI_EXTRACT in input")
  266. # previous command must have produced some AML,
  267. # otherwise we are in a middle of a block
  268. if (prev_aml_offset == asl[i].aml_offset):
  269. die("ACPI_EXTRACT directive in the middle of a block")
  270. directive = mext.group(1)
  271. array = mext.group(2)
  272. offset = asl[i].aml_offset
  273. if (directive == "ACPI_EXTRACT_ALL_CODE"):
  274. if array in output:
  275. die("%s directive used more than once" % directive)
  276. output[array] = aml
  277. continue
  278. if (directive == "ACPI_EXTRACT_NAME_BUFFER8"):
  279. offset = aml_name_buffer(offset, 8)
  280. elif (directive == "ACPI_EXTRACT_NAME_BUFFER16"):
  281. offset = aml_name_buffer(offset, 16)
  282. elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
  283. offset = aml_name_dword_const(offset)
  284. elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
  285. offset = aml_name_word_const(offset)
  286. elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
  287. offset = aml_name_byte_const(offset)
  288. elif (directive == "ACPI_EXTRACT_NAME_STRING"):
  289. offset = aml_name_string(offset)
  290. elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
  291. offset = aml_method_string(offset)
  292. elif (directive == "ACPI_EXTRACT_DEVICE_START"):
  293. offset = aml_device_start(offset)
  294. elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
  295. offset = aml_device_string(offset)
  296. elif (directive == "ACPI_EXTRACT_DEVICE_END"):
  297. offset = aml_device_end(offset)
  298. elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
  299. offset = aml_processor_start(offset)
  300. elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
  301. offset = aml_processor_string(offset)
  302. elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
  303. offset = aml_processor_end(offset)
  304. elif (directive == "ACPI_EXTRACT_PKG_START"):
  305. offset = aml_package_start(offset)
  306. else:
  307. die("Unsupported directive %s" % directive)
  308. if array not in output:
  309. output[array] = []
  310. output[array].append(offset)
  311. debug = "at end of file"
  312. def get_value_type(maxvalue):
  313. #Use type large enough to fit the table
  314. if (maxvalue >= 0x10000):
  315. return "int"
  316. elif (maxvalue >= 0x100):
  317. return "short"
  318. else:
  319. return "char"
  320. # Pretty print output
  321. for array in output.keys():
  322. otype = get_value_type(max(output[array]))
  323. odata = []
  324. for value in output[array]:
  325. odata.append("0x%x" % value)
  326. sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
  327. sys.stdout.write(",\n".join(odata))
  328. sys.stdout.write('\n};\n');