#! /bin/sh "true" '''\' if command -v python2 > /dev/null; then exec python2 "$0" "$@" else exec python "$0" "$@" fi exit $? ''' # # Add CRC checksum and version information to an ELF file # Copyright (C)2015 Thomas Kindler # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # import os, struct import argparse, subprocess import getpass, platform from StringIO import StringIO from datetime import datetime from crc32 import CRC32 from elf import ELFObject class VersionInfo: format = " 0x%08x %8d" % (s.name, s.lma, s.sh_addr, s.sh_size) ) ########## dprint("\nPatching section \"%s\"..." % args.section) section = elf.getSection(args.section) if section == None: raise Exception("Section not found") info = VersionInfo(elf) patch_section(elf, elf_data, section, info.pack()) info.image_crc = CRC32().forge( 0x00000000, elf_to_bin(elf, elf_data), section.lma - elf.getSections()[0].lma ) patch_section(elf, elf_data, section, info.pack()) dprint(" image_crc = 0x%08x" % info.image_crc) dprint(" image_size = %d" % info.image_size) ########## dprint("\nSaving \"%s\"..." % args.target) with open(args.target, "wb") as file: file.write(elf_data) if __name__ == '__main__': try: parse_args() patch_elf() except Exception as e: print (e) exit(1)