lang-patchsec.py 979 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. import argparse
  3. import elftools.elf.elffile
  4. from lib.io import fatal
  5. def main():
  6. ap = argparse.ArgumentParser()
  7. ap.add_argument('elf', help='Firmware ELF file')
  8. ap.add_argument('cat', help='Binary language catalog file')
  9. ap.add_argument('bin', help='Firmware BIN file')
  10. args = ap.parse_args()
  11. # get the language table position
  12. elf = elftools.elf.elffile.ELFFile(open(args.elf, "rb"))
  13. symtab = elf.get_section_by_name('.symtab')
  14. lang_table_sym = symtab.get_symbol_by_name('_SEC_LANG')[0]
  15. lang_table_start = lang_table_sym.entry.st_value
  16. lang_table_size = lang_table_sym.entry.st_size
  17. # read the binary catalog
  18. cat = open(args.cat, "rb").read()
  19. if len(cat) > lang_table_size:
  20. fatal("language catalog too large")
  21. # patch the symbol
  22. with open(args.bin, "r+b") as fw:
  23. fw.seek(lang_table_start)
  24. fw.write(cat)
  25. return 0
  26. if __name__ == '__main__':
  27. exit(main())