Laurent@56: import os, sys Edouard@0: from subprocess import Popen,PIPE Edouard@1: from toolchain_makefile import toolchain_makefile Edouard@0: import hashlib Edouard@0: Edouard@0: class LPC_target(toolchain_makefile): Edouard@0: #extension = ".ld" Edouard@0: #DebugEnabled = False Edouard@0: def __init__(self, CTRInstance): Edouard@0: self.binmd5key = None Edouard@0: toolchain_makefile.__init__(self, CTRInstance) Edouard@0: Edouard@0: def _GetBinMD5FileName(self): Edouard@0: return os.path.join(self.buildpath, "lastbuildPLCbin.md5") Edouard@0: Edouard@0: def _get_md5_header(self): Edouard@0: """Returns signature header""" Laurent@54: buildpath = self.buildpath.encode(sys.getfilesystemencoding()) Edouard@0: size = int(Popen( Laurent@54: ['arm-elf-size','-B',os.path.join(buildpath,"ArmPLC_rom.elf")], Edouard@0: stdout=PIPE).communicate()[0].splitlines()[1].split()[0]) Laurent@54: res = "&" + hashlib.md5(open(os.path.join(buildpath, "ArmPLC_rom.bin"), "rb").read(size)).hexdigest() + '\n' +\ Edouard@0: "$" + str(size) + '\n' Edouard@0: return res Edouard@0: Edouard@0: def GetBinaryCode(self): Edouard@0: """Returns ready to send signed + sized intel formated hex program""" Edouard@0: try: Edouard@0: res = self._get_md5_header() +\ Edouard@0: open(os.path.join(self.buildpath, "ArmPLC_rom.hex"), "r").read() Edouard@0: return res Edouard@0: except Exception, e: Edouard@0: return None Edouard@0: Edouard@0: def _get_cached_md5_header(self): Edouard@0: if self.binmd5key is not None: Edouard@0: return self.binmd5key Edouard@0: else: Edouard@0: try: Edouard@0: return open(self._GetBinMD5FileName(), "r").read() Edouard@0: except IOError, e: Edouard@0: return None Edouard@0: Edouard@0: def ResetBinaryCodeMD5(self, mode): Edouard@0: if mode == "BOOTLOADER": Edouard@0: self.binmd5key = None Edouard@0: try: Edouard@0: os.remove(self._GetBinMD5FileName()) Edouard@0: except Exception, e: Edouard@0: pass Edouard@0: else: Edouard@0: return toolchain_makefile.ResetBinaryCodeMD5(self) Edouard@0: Edouard@0: def GetBinaryCodeMD5(self, mode): Edouard@0: if mode == "BOOTLOADER": Edouard@0: return self._get_cached_md5_header() Edouard@0: else: Edouard@0: return toolchain_makefile.GetBinaryCodeMD5(self) Edouard@0: Edouard@0: def build(self): Edouard@0: res = toolchain_makefile.build(self) Edouard@0: if res: Edouard@0: self.binmd5key = self._get_md5_header() Edouard@0: f = open(self._GetBinMD5FileName(), "w") Edouard@0: f.write(self.binmd5key) Edouard@0: f.close() Edouard@0: try: Edouard@0: self.CTRInstance.logger.write( Edouard@0: _("Binary is %s bytes long\n")% Edouard@0: str(os.path.getsize( Edouard@0: os.path.join(self.buildpath, "ArmPLC_rom.bin")))) Edouard@0: except Exception, e: Edouard@0: pass Edouard@0: return res Edouard@0: