Example #1
0
def main():
 hf1 = HexFile(None)
 hf1.from_str(open(sys.argv[1]).read())

 hf2 = HexFile(None)
 hf2.from_str(open(sys.argv[2]).read())

 if (hf1.addr!=hf2.addr):
     print >> sys.stderr, "addr mismatch", hf1.addr, hf2.addr

 if len(hf1.bytes) != len(hf2.bytes):
     print >> sys.stderr, "len mismatch", len(hf1.bytes), len(hf2.bytes)

 minlen=min(len(hf1.bytes), len(hf2.bytes))

 for i in range(0, hf1.addr):
     hf1.bytes = [0] + hf1.bytes

 for i in range(0, hf2.addr):
     hf2.bytes = [0] + hf2.bytes

 start = max(hf1.addr, hf2.addr)
 for i in range( start, start + minlen ):
     if hf1.bytes[i] != hf2.bytes[i]:
         print >> sys.stderr, "mismatch %04X %02X %02X" % (i, hf1.bytes[i], hf2.bytes[i])
Example #2
0
def main():
 hf1 = HexFile(None)
 hf1.from_str(open(sys.argv[1]).read())

 if "x" in sys.argv[2]:
      newStart = int(sys.argv[2], 16)
 else:
     newStart = int(sys.argv[2])

 hf1.addr = newStart
 sys.stdout.write(hf1.to_str())
Example #3
0
def main():
 hf1 = HexFile(None)
 hf1.from_str(open(sys.argv[1]).read())

 csum=0
 sys.stdout.write("U0\n")
 sys.stdout.write(":")
 for b in hf1.bytes:
     sys.stdout.write("%02X" % b)
     csum = csum + b
 sys.stdout.write(">")
 sys.stdout.write("%02X" % (len(hf1.bytes) & 0xFF))
 sys.stdout.write("%02X" % (csum & 0xFF))
 sys.stdout.write("\n")
Example #4
0
def main():
    hf1 = HexFile(None)
    hf1.from_str(open(sys.argv[1]).read())

    csum = 0
    sys.stdout.write("U0\n")
    sys.stdout.write(":")
    for b in hf1.bytes:
        sys.stdout.write("%02X" % b)
        csum = csum + b
    sys.stdout.write(">")
    sys.stdout.write("%02X" % (len(hf1.bytes) & 0xFF))
    sys.stdout.write("%02X" % (csum & 0xFF))
    sys.stdout.write("\n")
Example #5
0
def main():
    hf1 = HexFile(None)
    hf1.from_str(open(sys.argv[1]).read())

    if "x" in sys.argv[2]:
        newStart = int(sys.argv[2], 16)
    else:
        newStart = int(sys.argv[2])

    hf1.addr = newStart
    sys.stdout.write(hf1.to_str())
Example #6
0
def main():
    parser = OptionParser(usage="supervisor [options] command",
            description="Commands: ...")

    parser.add_option("-A", "--addr", dest="addr",
         help="address", metavar="ADDR", type="int", default=0)
    parser.add_option("-C", "--count", dest="count",
         help="count", metavar="ADDR", type="int", default=65536)
    parser.add_option("-V", "--value", dest="value",
         help="value", metavar="VAL", type="int", default=0)
    parser.add_option("-P", "--ascii", dest="ascii",
         help="print ascii value", action="store_true", default=False)
    parser.add_option("-R", "--rate", dest="rate",
         help="rate for slow clock", metavar="HERTZ", type="int", default=10)
    parser.add_option("-B", "--bank", dest="bank",
         help="bank number to select on ram-rom board", metavar="NUMBER", type="int", default=None)
    parser.add_option("-v", "--verbose", dest="verbose",
         help="verbose", action="store_true", default=False)
    parser.add_option("-f", "--filename", dest="filename",
         help="filename", default=None)
    parser.add_option("-r", "--reset", dest="reset_on_release",
         help="reset on release of bus", action="store_true", default=False)
    parser.add_option("-n", "--norelease", dest="norelease",
         help="do not release bus", action="store_true", default=False)

    #parser.disable_interspersed_args()

    (options, args) = parser.parse_args(sys.argv[1:])

    cmd = args[0]
    args=args[1:]

    bus = smbus.SMBus(1)
    super = Supervisor(bus, 0x20, options.verbose)

    if (cmd=="reset"):
        super.reset()

    elif (cmd=="memdump"):
        try:
            super.take_bus(setBank=options.bank)
            for i in range(options.addr,options.addr+options.count):
                val = super.mem_read(i)
                if options.ascii:
                    print "%04X %02X %s" % (i, val, hex_escape(chr(val)))
                else:
                    print "%04X %02X" % (i, val)
        finally:
            if not options.norelease:
                super.release_bus()

    elif (cmd=="savehex"):
        hexdumper = HexFile(options.addr)
        try:
            super.take_bus(setBank=options.bank)
            for i in range(options.addr,options.addr+options.count):
                val = super.mem_read(i)
                hexdumper.write(val)
        finally:
            if not options.norelease:
                super.release_bus()

        if options.filename:
            file(options.filename,"w").write(hexdumper.to_str())
        else:
            sys.stdout.write(hexdumper.to_str())

        # a little self-test ...
        h2 = HexFile(None)
        h2.from_str(hexdumper.to_str())
        print "okay?", h2.to_str() == hexdumper.to_str()


    elif (cmd=="loadhex"):
        hexloader = HexFile(None)
        if options.filename:
            hexloader.from_str(file(options.filename,"r").read())
        else:
            hexloader.from_str(sys.stdin.read())

        try:
            super.take_bus(setBank=options.bank)
            offset = hexloader.addr
            for val in hexloader.bytes:
                super.mem_write(offset, val)
                offset = offset + 1
        finally:
            if not options.norelease:
                super.release_bus(reset=options.reset_on_release)


    elif (cmd=="peek"):
        try:
            super.take_bus(setBank=options.bank)
            print "%02X" % super.mem_read(options.addr)
        finally:
            if not options.norelease:
                super.release_bus()

    elif (cmd=="poke"):
        try:
            super.take_bus(setBank=options.bank)
            super.mem_write(options.addr, options.value)
        finally:
            if not options.norelease:
                super.release_bus()

    elif (cmd=="ioread"):
        try:
            super.take_bus(setBank=options.bank)
            print "%02X" % super.io_read(options.addr)
        finally:
            if not options.norelease:
                super.release_bus()

    elif (cmd=="iowatch"):
        last=None
        try:
            super.take_bus(setBank=options.bank)
            while True:
                x=super.io_read(options.addr)
                if (x!=last):
                    print "%02X" % x
                    last=x
        finally:
            if not options.norelease:
                super.release_bus()

    elif (cmd=="iowrite"):
        try:
            super.take_bus(setBank=options.bank)
            super.io_write(options.addr, options.value)
        finally:
            if not options.norelease:
                super.release_bus()

    elif (cmd=="slowclock"):
        try:
            super.slow_clock(rate=options.rate)
        finally:
            super.normal_clock()

    elif (cmd=="singlestep"):
        try:
            from getch import getch
            super.singlestep_on()
            while True:
                print "press `s` to step, 'q' to quit",
                ch = getch()
                if ch=="s":
                    # Reading gpio will clear interrupt, but the problem is that
                    # M1 may still be low and will immediately re-interrupt.
                    bits=super.ixData.get_gpio(1)
                    while (bits & M1)==0:
                       bits = super.ixData.get_gpio(1)

                    print ""
                if ch=="q":
                    break
        finally:
            super.singlestep_off()

    elif (cmd=="singlestep"):
        try:
            from getch import getch
            super.singlestep_on()
            while True:
                print "press `s` to step, 'q' to quit",
                ch = getch()
                if ch=="s":
                    # Reading gpio will clear interrupt, but the problem is that
                    # M1 may still be low and will immediately re-interrupt. So
                    # keep reading it until we see that M1 has gone high.
                    bits=super.ixData.get_gpio(1)
                    while (bits & M1)==0:
                       bits = super.ixData.get_gpio(1)

                    print ""
                if ch=="q":
                    break
        finally:
            super.singlestep_off()

    elif (cmd=="autostep"):
        try:
            super.autostep(rate=options.rate)
        finally:
            super.normal_clock()

    elif (cmd=="showint"):
        last=None
        while True:
            v = ((super.ixData.get_gpio(1)&INT) !=0)
            if v!=last:
                print v
                last=v
Example #7
0
elif extension == "hex":
    # The hex filename is just the passed filename
    hfilename = filename
# If it's not HEX or ASM, what is it?
else:
    raise Exception("Unknown file extension")

# If a serial port has not been passed, use the first available one
if len(sys.argv) < 3:
    sport = glob.glob('/dev/ttyUSB*')[0]
    # Otherwise used the passed one
else:
    sport = sys.argv[2]

# Init new HexFile and PIC instances
hf = HexFile(hfilename)

prog = Pic(sport, 9600, 0x03FF)

# Write and verify the hex file to the pic
prog.write_file(hf)
prog.verify_file(hf)

# Write and verify the config word from the hex file to the pic
prog.write_verify_config_file(hf)

# Run the program on the pic
prog.execute()

# Close the connection with the pic
prog.close()
Example #8
0
	def __init__(self, source):
		self.hex = HexFile(source, 24, 4, 2)
		self._parse_block()
Example #9
0
class ControllerBlock:
	"""
	The metadata block in a pic24 controller firmware image.  This includes
	data like the specific hardware version that the controller is compiled
	for as well as checksums for various parts of the image that allow the
	controller board's bootloader to verify that the firmware image is valid
	and complete.
	"""

	firmware_length = 0xA600

	@param("source", "path", "readable", desc="hex file to load")
	def __init__(self, source):
		self.hex = HexFile(source, 24, 4, 2)
		self._parse_block()

	def _calculate_checksum(self, start, length):
		checksum = 0

		for i in xrange(start, start+length, 2):
			checksum += ((self.hex[i] >> 16) + self.hex[i])

		return ((~checksum) + 1) & 0xFFFF

	def _parse_block(self):
		base = 0x200
		self.magic = (self.hex[base+0], self.hex[base+2])
		self.reset = (self.hex[base+4], self.hex[base+6])
		self.firmware_end = self.hex[base+8]

		self.hw_version = ""
		for i in xrange(10, 10+8, 2):
			val = self.hex[base+i]
			low = val & 0xFF
			high = val >> 8 & 0xFF
			self.hw_version += chr(low)
			self.hw_version += chr(high)

		self.ivt_checksum = self.hex[base+18]
		self.aivt_checksum = self.hex[base+20]
		self.code_checksum = self.hex[base+22]
		self.block_checksum = self.hex[base+30]

	def _write_block(self):
		base = 0x200
		self.hex[base+0] = self.magic[0]
		self.hex[base+2] = self.magic[1]
		
		self.hex[base+4] = self.reset[0]
		self.hex[base+6] = self.reset[1]

		self.hex[base+8] = self.firmware_end

		for i in xrange(0, 8, 2):
			if i < (len(self.hw_version)-1):
				low = ord(self.hw_version[ i+ 0])
				high = ord(self.hw_version[i+ 1])
			elif i < len(self.hw_version):
				low = ord(self.hw_version[i])
				high = ord(' ')
			else:
				low = ord(' ')
				high = ord(' ')

			word = ((high & 0xFF) << 8) | (low & 0xFF)
			self.hex[base + 10 + i] = word

		self.hex[base+18] = self.ivt_checksum
		self.hex[base+20] = self.aivt_checksum
		self.hex[base+22] = self.code_checksum
		for i in xrange(24, 30, 2):
			self.hex[base+i] = 0

		self.hex[base+30] = self._calculate_checksum(0x200, 30)

	@param("dest", "path", "writeable", desc="output hex file path")
	def save(self, dest):
		"""
		Save the hexfile with a potentially updated metadata block
		into the path specified by dest.
		"""

		self._write_block()
		self.hex.save(dest)

	@annotated
	def update_checksums(self):
		"""
		Calculate the correct checksums that should be stored in the metadata block
		and add them to the in-memory copy of the block.
		"""

		self.ivt_checksum 	= self._calculate_checksum(0x04, 0x100 - 0x04)
		self.aivt_checksum	= self._calculate_checksum(0x100, 0x100)
		self.code_checksum	= self._calculate_checksum(0x200 + 32, ControllerBlock.firmware_length - 32)
		self.block_checksum = self._calculate_checksum(0x200, 30)

	@returns('HW Compatibility Version')
	def get_hw_version(self):
		return self.hw_version

	@param("hw", "string", desc="a string up to 16 bytes long")
	def set_hw_version(self, hw):
		"""
		Set the embedded hw compatibility string for this metadata block.  This is an
		ASCII string up to 16 bytes in length not including the terminating NULL.  It 
		is checked upon bootup against the hardcoded hw_version stored in the mainboard
		bootloader and if it does not match the controller hex file is not executed.
		"""

		if len(hw) > 16:
			hw = hw[:16]

		self.hw_version = hw

	@returns("Controller Block Status", printer=print_validity)
	def validate(self):
		"""
		Validate the data contained in this metadata block.
		"""

		status = {}
		status['magic'] = self._validate_magic()
		status['firmware_end'] 	= self._validate_firmware_length()
		status['ivt_checksum'] 	= self._validate_firmware_region(0x04, 0x100 - 0x04, self.ivt_checksum)
		status['aivt_checksum']	= self._validate_firmware_region(0x100, 0x100, self.aivt_checksum)
		status['code_checksum']	= self._validate_firmware_region(0x200 + 32, ControllerBlock.firmware_length - 32, self.code_checksum)
		status['block_checksum']= self._validate_firmware_region(0x200, 30, self.block_checksum)

		valid = reduce(lambda x,y: x and y, map(lambda x: x[0], status.itervalues()), True)
		status['valid'] = valid
		return status

	def _validate_magic(self):
		if self.magic[0] == 0xBAAD and self.magic[1] == 0xDAAD:
			return (True, self.magic[0], self.magic[1])
		else:
			return (False, self.magic[0], self.magic[1])

	def _validate_firmware_length(self):
		if self.firmware_end == ControllerBlock.firmware_length:
			return (True, self.firmware_end)

		return (False, self.firmware_end, ControllerBlock.firmware_length)

	def _validate_firmware_region(self, start, length, comp):
		check = self._calculate_checksum(start, length)
		if comp == check:
			return (True, check, comp)

		return (False, check, comp)
Example #10
0
    def dispatch_command(self, command):
        """
        Apply user command
        
        @param command : user command
        """
        if len(command) == 1:
            if command[0] == "help":
                self.print_help()
            elif command[0] == "version":
                print self.VERSION_NUMBER
            elif command[0] == "quit":
                self.stop()
                sys.exit(0)
        elif len(command) < 2:
            print "Incorrect command. Run \"help\" to get more information"
        # Print SWAP traffic
        elif command[0] == "traffic":
            if command[1].lower() == "on":
                self.verbose = True
                self.server.verbose = True
                self.server.modem.verbose = True
                self.server.modem._serport._verbose = True
            elif command[1].lower() == "off":
                self.verbose = False
                self.server.verbose = False
                self.server.modem.verbose = False
                self.server.modem._serport._verbose = False
            else:
                print command[1] + " is not a correct value"
        # Set HEX file
        elif command[0] == "hexfile":
            # HEX file object
            self.hexfile = HexFile(command[1])
        # Print list of nodes
        elif command[0] == "list":
            if command[1] == "nodes":
                if len(self.network.motes) == 0:
                    print "No nodes detected"
                else:
                    for mote in self.network.motes:
                        print "Addr: " + hex(
                            mote.address) + " - " + mote.definition.product
        # Clear list list of nodes
        elif command[0] == "clear":
            if command[1] == "nodes":
                self.server.network.clear()
                self.server.network.save()
        # Change device device property
        elif command[0] == "node":
            if len(command) < 3:
                print "Insufficient arguments"
                print "Correct format is: node <address> <options...>"
            else:
                addr = self.str_to_int(command[1])
                if addr is None:
                    print "Incorrect address format"
                else:
                    # Get mote object
                    mote = self.server.network.get_mote(address=addr)

                    if mote is None:
                        print "Node not found in data base"
                        print "Clear your list of nodes and restart them again"
                    else:
                        # Program node
                        if command[2] == "program":
                            if (self.hexfile == None):
                                print "Please set a hexfile before running \"program\""
                            else:
                                # Create progress bar
                                self.progress = AnimatedProgressBar(
                                    end=self.hexfile.nbof_data_lines, width=50)
                                # Save address of node being programmed
                                self.prog_address = addr
                                # Transmit product code
                                self.transmit_product_code()

                                # Put node in upgrade mode
                                val = SwapValue(SwapState.UPGRADE, 1)
                                if mote.cmdRegisterWack(
                                        SwapRegId.ID_SYSTEM_STATE, val):
                                    print "Node now in programming mode"
                                elif self.hexfile_line == 0:
                                    print "Unable to put node in progamming mode"
                                    self.prog_address = None
                        # Restart node (if not sleeping)
                        elif command[2] == "restart":
                            if mote.restart():
                                print "Node restarting"
                            else:
                                print "Got no response from node. It's probably sleeping"
                        # Show details of device
                        elif command[2] == "details":
                            print "SWAP address : " + hex(mote.address)
                            print "Developer : " + mote.definition.manufacturer
                            print "Product name : " + mote.definition.product
                            if mote.config_registers is not None:
                                print "Config registers :"
                                for reg in mote.config_registers:
                                    print "Register ID : " + hex(reg.id)
                                    print "Register name : " + reg.name
                                    print "Register value : 0x" + reg.value.toAsciiHex(
                                    )
                                    for param in reg.parameters:
                                        print "  Parameter name : " + param.name
                                        print "  Parameter value : 0x" + param.value.toAsciiHex(
                                        )
                            if mote.regular_registers is not None:
                                print "Regular registers :"
                                for reg in mote.regular_registers:
                                    print "Register ID : " + hex(reg.id)
                                    print "Register name : " + reg.name
                                    print "Register value : 0x" + reg.value.toAsciiHex(
                                    )
                                    for endp in reg.parameters:
                                        print "  Endpoint name : " + endp.name
                                        print "  Endpoint value : 0x" + endp.value.toAsciiHex(
                                        )
                        # Change device address
                        elif command[2] == "address":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_DEVICE_ADDR), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_addr = self.str_to_int(command[3])
                                if new_addr is not None:
                                    self.print_confirmation(
                                        mote.setAddress(new_addr), command[2],
                                        command[3])
                        # Change Tx interval
                        elif command[2] == "txinterval":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_TX_INTERVAL), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_interval = self.str_to_int(command[3])
                                if new_interval is not None:
                                    self.print_confirmation(
                                        mote.setTxInterval(new_interval),
                                        command[2], command[3])
                        # Change network id
                        elif command[2] == "netid":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_NETWORK_ID), "network ID")
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_netid = self.str_to_int(command[3])
                                if new_netid is not None:
                                    self.print_confirmation(
                                        mote.setNetworkId(new_netid),
                                        command[2], command[3])
                        # Change frequency channel
                        elif command[2] == "channel":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_FREQ_CHANNEL), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_channel = self.str_to_int(command[3])
                                if new_channel is not None:
                                    self.print_confirmation(
                                        mote.setFreqChannel(new_channel),
                                        command[2], command[3])
                        # Read/write register
                        elif command[2] == "reg":
                            if len(command) > 3:
                                reg_id = self.str_to_int(command[3])
                                if reg_id is not None:
                                    if len(command) == 4:
                                        self.print_response(
                                            mote.qryRegisterWack(reg_id),
                                            "register value")
                                    elif len(command) == 5:
                                        reg = mote.getRegister(reg_id)
                                        val = SwapValue(
                                            command[4], reg.value.getLength())
                                        self.print_confirmation(
                                            mote.cmdRegisterWack(reg_id, val),
                                            "register value", command[4])
                                    else:
                                        print "Too many arguments"
                                        print "Correct format is: node <address> reg <reg_id> [<reg_val>]"
                            else:
                                print "Insufficient arguments"
                                print "Correct format is: node <address> reg <reg_id> [<reg_val>]"
                        else:
                            print "Command not supported"
        else:
            print "Command not supported"
Example #11
0
def main():
  hf1 = HexFile(None)
  hf1.from_str(open(sys.argv[1]).read())

  for arg in sys.argv[2:]:
      hfm = HexFile(None)
      hfm.from_str(open(arg).read())
      hf1.merge(hfm)

  sys.stdout.write(hf1.to_str())