acpi_extract.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 8 byte buffer offset
  131. def aml_data_buffer8(offset):
  132. #0x08 NameOp NameString DataRef
  133. expect = [0x11, 0x0B, 0x0A, 0x08]
  134. if (aml[offset:offset+4] != expect):
  135. die( "Name offset 0x%x: expected %s actual %s" %
  136. (offset, aml[offset:offset+4], expect))
  137. return offset + len(expect)
  138. # Given data offset, find dword const offset
  139. def aml_data_dword_const(offset):
  140. #0x08 NameOp NameString DataRef
  141. if (aml[offset] != 0x0C):
  142. die( "Name offset 0x%x: expected 0x0C actual 0x%x" %
  143. (offset, aml[offset]));
  144. return offset + 1;
  145. # Given data offset, find word const offset
  146. def aml_data_word_const(offset):
  147. #0x08 NameOp NameString DataRef
  148. if (aml[offset] != 0x0B):
  149. die( "Name offset 0x%x: expected 0x0B actual 0x%x" %
  150. (offset, aml[offset]));
  151. return offset + 1;
  152. # Given data offset, find byte const offset
  153. def aml_data_byte_const(offset):
  154. #0x08 NameOp NameString DataRef
  155. if (aml[offset] != 0x0A):
  156. die( "Name offset 0x%x: expected 0x0A actual 0x%x" %
  157. (offset, aml[offset]));
  158. return offset + 1;
  159. # Find name'd buffer8
  160. def aml_name_buffer8(offset):
  161. return aml_data_buffer8(aml_name_string(offset) + 4)
  162. # Given name offset, find dword const offset
  163. def aml_name_dword_const(offset):
  164. return aml_data_dword_const(aml_name_string(offset) + 4)
  165. # Given name offset, find word const offset
  166. def aml_name_word_const(offset):
  167. return aml_data_word_const(aml_name_string(offset) + 4)
  168. # Given name offset, find byte const offset
  169. def aml_name_byte_const(offset):
  170. return aml_data_byte_const(aml_name_string(offset) + 4)
  171. def aml_device_start(offset):
  172. #0x5B 0x82 DeviceOp PkgLength NameString
  173. if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x82)):
  174. die( "Name offset 0x%x: expected 0x5B 0x82 actual 0x%x 0x%x" %
  175. (offset, aml[offset], aml[offset + 1]));
  176. return offset
  177. def aml_device_string(offset):
  178. #0x5B 0x82 DeviceOp PkgLength NameString
  179. start = aml_device_start(offset)
  180. offset += 2
  181. pkglenbytes = aml_pkglen_bytes(offset)
  182. offset += pkglenbytes
  183. return offset
  184. def aml_device_end(offset):
  185. start = aml_device_start(offset)
  186. offset += 2
  187. pkglenbytes = aml_pkglen_bytes(offset)
  188. pkglen = aml_pkglen(offset)
  189. return offset + pkglen
  190. def aml_processor_start(offset):
  191. #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
  192. if ((aml[offset] != 0x5B) or (aml[offset + 1] != 0x83)):
  193. die( "Name offset 0x%x: expected 0x5B 0x83 actual 0x%x 0x%x" %
  194. (offset, aml[offset], aml[offset + 1]));
  195. return offset
  196. def aml_processor_string(offset):
  197. #0x5B 0x83 ProcessorOp PkgLength NameString ProcID
  198. start = aml_processor_start(offset)
  199. offset += 2
  200. pkglenbytes = aml_pkglen_bytes(offset)
  201. offset += pkglenbytes
  202. return offset
  203. def aml_processor_end(offset):
  204. start = aml_processor_start(offset)
  205. offset += 2
  206. pkglenbytes = aml_pkglen_bytes(offset)
  207. pkglen = aml_pkglen(offset)
  208. return offset + pkglen
  209. def aml_package_start(offset):
  210. offset = aml_name_string(offset) + 4
  211. # 0x12 PkgLength NumElements PackageElementList
  212. if (aml[offset] != 0x12):
  213. die( "Name offset 0x%x: expected 0x12 actual 0x%x" %
  214. (offset, aml[offset]));
  215. offset += 1
  216. return offset + aml_pkglen_bytes(offset) + 1
  217. lineno = 0
  218. for line in fileinput.input():
  219. # Strip trailing newline
  220. line = line.rstrip();
  221. # line number and debug string to output in case of errors
  222. lineno = lineno + 1
  223. debug = "input line %d: %s" % (lineno, line)
  224. #ASL listing: space, then line#, then ...., then code
  225. pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*')
  226. m = pasl.search(line)
  227. if (m):
  228. add_asl(lineno, pasl.sub("", line));
  229. # AML listing: offset in hex, then ...., then code
  230. paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*')
  231. m = paml.search(line)
  232. if (m):
  233. add_aml(m.group(1), paml.sub("", line))
  234. # Now go over code
  235. # Track AML offset of a previous non-empty ASL command
  236. prev_aml_offset = -1
  237. for i in range(len(asl)):
  238. debug = "input line %d: %s" % (asl[i].lineno, asl[i].line)
  239. l = asl[i].line
  240. # skip if not an extract directive
  241. a = len(re.findall(r'ACPI_EXTRACT', l))
  242. if (not a):
  243. # If not empty, store AML offset. Will be used for sanity checks
  244. # IASL seems to put {}. at random places in the listing.
  245. # Ignore any non-words for the purpose of this test.
  246. m = re.search(r'\w+', l)
  247. if (m):
  248. prev_aml_offset = asl[i].aml_offset
  249. continue
  250. if (a > 1):
  251. die("Expected at most one ACPI_EXTRACT per line, actual %d" % a)
  252. mext = re.search(r'''
  253. ^\s* # leading whitespace
  254. /\*\s* # start C comment
  255. (ACPI_EXTRACT_\w+) # directive: group(1)
  256. \s+ # whitspace separates directive from array name
  257. (\w+) # array name: group(2)
  258. \s*\*/ # end of C comment
  259. \s*$ # trailing whitespace
  260. ''', l, re.VERBOSE)
  261. if (not mext):
  262. die("Stray ACPI_EXTRACT in input")
  263. # previous command must have produced some AML,
  264. # otherwise we are in a middle of a block
  265. if (prev_aml_offset == asl[i].aml_offset):
  266. die("ACPI_EXTRACT directive in the middle of a block")
  267. directive = mext.group(1)
  268. array = mext.group(2)
  269. offset = asl[i].aml_offset
  270. if (directive == "ACPI_EXTRACT_ALL_CODE"):
  271. if array in output:
  272. die("%s directive used more than once" % directive)
  273. output[array] = aml
  274. continue
  275. if (directive == "ACPI_EXTRACT_NAME_BUFFER8"):
  276. offset = aml_name_buffer8(offset)
  277. elif (directive == "ACPI_EXTRACT_NAME_DWORD_CONST"):
  278. offset = aml_name_dword_const(offset)
  279. elif (directive == "ACPI_EXTRACT_NAME_WORD_CONST"):
  280. offset = aml_name_word_const(offset)
  281. elif (directive == "ACPI_EXTRACT_NAME_BYTE_CONST"):
  282. offset = aml_name_byte_const(offset)
  283. elif (directive == "ACPI_EXTRACT_NAME_STRING"):
  284. offset = aml_name_string(offset)
  285. elif (directive == "ACPI_EXTRACT_METHOD_STRING"):
  286. offset = aml_method_string(offset)
  287. elif (directive == "ACPI_EXTRACT_DEVICE_START"):
  288. offset = aml_device_start(offset)
  289. elif (directive == "ACPI_EXTRACT_DEVICE_STRING"):
  290. offset = aml_device_string(offset)
  291. elif (directive == "ACPI_EXTRACT_DEVICE_END"):
  292. offset = aml_device_end(offset)
  293. elif (directive == "ACPI_EXTRACT_PROCESSOR_START"):
  294. offset = aml_processor_start(offset)
  295. elif (directive == "ACPI_EXTRACT_PROCESSOR_STRING"):
  296. offset = aml_processor_string(offset)
  297. elif (directive == "ACPI_EXTRACT_PROCESSOR_END"):
  298. offset = aml_processor_end(offset)
  299. elif (directive == "ACPI_EXTRACT_PKG_START"):
  300. offset = aml_package_start(offset)
  301. else:
  302. die("Unsupported directive %s" % directive)
  303. if array not in output:
  304. output[array] = []
  305. output[array].append(offset)
  306. debug = "at end of file"
  307. def get_value_type(maxvalue):
  308. #Use type large enough to fit the table
  309. if (maxvalue >= 0x10000):
  310. return "int"
  311. elif (maxvalue >= 0x100):
  312. return "short"
  313. else:
  314. return "char"
  315. # Pretty print output
  316. for array in output.keys():
  317. otype = get_value_type(max(output[array]))
  318. odata = []
  319. for value in output[array]:
  320. odata.append("0x%x" % value)
  321. sys.stdout.write("static unsigned %s %s[] = {\n" % (otype, array))
  322. sys.stdout.write(",\n".join(odata))
  323. sys.stdout.write('\n};\n');