mklist.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. #
  3. # Script for creating a "struct extract" list for fwcutter_list.h
  4. #
  5. # Copyright (c) 2008 Michael Buesch <m@bues.ch>
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. #
  11. # 1. Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # 2. Redistributions in binary form must reproduce the above
  14. # copyright notice, this list of conditions and the following
  15. # disclaimer in the documentation and/or other materials provided
  16. # with the distribution.
  17. #
  18. # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  22. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. import sys
  30. import os
  31. import re
  32. import hashlib
  33. if len(sys.argv) != 2:
  34. print "Usage: %s path/to/wl.o" % sys.argv[0]
  35. sys.exit(1)
  36. fn = sys.argv[1]
  37. pipe = os.popen("objdump -t %s" % fn)
  38. syms = pipe.readlines()
  39. pipe = os.popen("objdump --headers %s" % fn)
  40. headers = pipe.readlines()
  41. # Get the .rodata fileoffset
  42. rodata_fileoffset = None
  43. rofileoff_re = re.compile(r"\d+\s+\.rodata\s+[0-9a-fA-F]+\s+[0-9a-fA-F]+\s+[0-9a-fA-F]+\s+([0-9a-fA-F]+)\s+.")
  44. for line in headers:
  45. line = line.strip()
  46. m = rofileoff_re.match(line)
  47. if m:
  48. rodata_fileoffset = int(m.group(1), 16)
  49. break
  50. if rodata_fileoffset == None:
  51. print "ERROR: Could not find .rodata fileoffset"
  52. sys.exit(1)
  53. md5sum = hashlib.md5(file(fn, "r").read())
  54. print "static struct extract _%s[] =" % md5sum.hexdigest()
  55. print "{"
  56. sym_re = re.compile(r"([0-9a-fA-F]+)\s+g\s+O\s+\.rodata\s+([0-9a-fA-F]+) d11([-_\s\w0-9]+)")
  57. ucode_re = re.compile(r"ucode(\d+)")
  58. for sym in syms:
  59. sym = sym.strip()
  60. m = sym_re.match(sym)
  61. if not m:
  62. continue
  63. pos = int(m.group(1), 16) + rodata_fileoffset
  64. size = int(m.group(2), 16)
  65. name = m.group(3)
  66. if name[-2:] == "sz":
  67. continue
  68. type = None
  69. if "initvals" in name:
  70. type = "EXT_IV"
  71. size -= 8
  72. if "pcm" in name:
  73. type = "EXT_PCM"
  74. if "bommajor" in name:
  75. print "\t/* ucode major version at offset 0x%x */" % pos
  76. continue
  77. if "bomminor" in name:
  78. print "\t/* ucode minor version at offset 0x%x */" % pos
  79. continue
  80. if "ucode_2w" in name:
  81. continue
  82. m = ucode_re.match(name)
  83. if m:
  84. corerev = int(m.group(1))
  85. if corerev <= 4:
  86. type = "EXT_UCODE_1"
  87. elif corerev >= 5 and corerev <= 14:
  88. type = "EXT_UCODE_2"
  89. else:
  90. type = "EXT_UCODE_3"
  91. if not type:
  92. print "\t/* ERROR: Could not guess data type for: %s */" % name
  93. continue
  94. print "\t{ .name = \"%s\", .offset = 0x%X, .type = %s, .length = 0x%X }," % (name, pos, type, size)
  95. print "\tEXTRACT_LIST_END"
  96. print "};"