From 79079a342f98906fb897f66c9b32fe1a6bed0fb8 Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Wed, 15 Jun 2011 16:50:25 -0700 Subject: N2XX: firmware stops device streaming on fw update, and updater script checks hw rev before proceeding --- host/utils/usrp_n2xx_net_burner.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 06af8c860..6be632735 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -17,8 +17,6 @@ # # TODO: make it autodetect UHD devices -# TODO: you should probably watch sequence numbers -# TODO: validate images in 1) size and 2) header content so you can't write a Justin Bieber MP3 to Flash import optparse import math @@ -54,6 +52,14 @@ FLASH_DATA_PACKET_SIZE = 256 FLASH_ARGS_FMT = '!LLLLL256s' FLASH_INFO_FMT = '!LLLLL256x' FLASH_IP_FMT = '!LLLL260x' +FLASH_HW_REV_FMT = '!LLLL260x' + +n2xx_revs = { + 0x0a00: "n200_r3", + 0x0a01: "n200_r4", + 0x0a01: "n210_r3", + 0x0a11: "n210_r4" + } class update_id_t: USRP2_FW_UPDATE_ID_WAT = ord(' ') @@ -72,6 +78,8 @@ class update_id_t: USRP2_FW_UPDATE_ID_KK_READ_TEH_FLASHES_OMG = ord('R') USRP2_FW_UPDATE_ID_RESET_MAH_COMPUTORZ_LOL = ord('s') USRP2_FW_UPDATE_ID_RESETTIN_TEH_COMPUTORZ_OMG = ord('S') + USRP2_FW_UPDATE_ID_I_CAN_HAS_HW_REV_LOL = ord('v') + USRP2_FW_UPDATE_ID_HERES_TEH_HW_REV_OMG = ord('V') USRP2_FW_UPDATE_ID_KTHXBAI = ord('~') _seq = -1 @@ -92,12 +100,18 @@ def unpack_flash_info_fmt(s): def unpack_flash_ip_fmt(s): return struct.unpack(FLASH_IP_FMT, s) #(proto_ver, pktid, seq, ip_addr) +def unpack_flash_hw_rev_fmt(s): + return struct.unpack(FLASH_HW_REV_FMT, s) #proto_ver, pktid, seq, hw_rev + def pack_flash_args_fmt(proto_ver, pktid, seq, flash_addr, length, data=bytes()): return struct.pack(FLASH_ARGS_FMT, proto_ver, pktid, seq, flash_addr, length, data) def pack_flash_info_fmt(proto_ver, pktid, seq, sector_size_bytes, memory_size_bytes): return struct.pack(FLASH_INFO_FMT, proto_ver, pktid, seq, sector_size_bytes, memory_size_bytes) +def pack_flash_hw_rev_fmt(proto_ver, pktid, seq, hw_rev): + return struct.pack(FLASH_HW_REV_FMT, proto_ver, pktid, seq, hw_rev) + def is_valid_fpga_image(fpga_image): for i in range(0,63): if fpga_image[i:i+1] == bytes(b'\xFF'): continue @@ -117,6 +131,7 @@ class burner_socket(object): self._sock.connect((addr, UDP_FW_UPDATE_PORT)) self.set_callbacks(lambda *a: None, lambda *a: None) self.init_update() #check that the device is there + self.get_hw_rev() def set_callbacks(self, progress_cb, status_cb): self._progress_cb = progress_cb @@ -137,7 +152,12 @@ class burner_socket(object): else: raise Exception("Invalid reply received from device.") - # print "Incoming:\n\tVer: %i\n\tID: %c\n\tSeq: %i\n\tIP: %i\n" % (proto_ver, chr(pktid), rxseq, ip_addr) + def get_hw_rev(self): + out_pkt = pack_flash_hw_rev_fmt(USRP2_FW_PROTO_VERSION, update_id_t.USRP2_FW_UPDATE_ID_I_CAN_HAS_HW_REV_LOL, seq(), 0) + in_pkt = self.send_and_recv(out_pkt) + (proto_ver, pktid, rxseq, hw_rev) = unpack_flash_hw_rev_fmt(in_pkt) + if(pktid != update_id_t.USRP2_FW_UPDATE_ID_HERES_TEH_HW_REV_OMG): hw_rev = 0 + return socket.ntohs(hw_rev) memory_size_bytes = 0 sector_size_bytes = 0 @@ -157,10 +177,16 @@ class burner_socket(object): def burn_fw(self, fw, fpga, reset, safe): (flash_size, sector_size) = self.get_flash_info() + hw_rev = self.get_hw_rev() - print("Flash size: %i\nSector size: %i\n\n" % (flash_size, sector_size)) + if(hw_rev != 0): print "Hardware type: %s" % n2xx_revs[hw_rev] + print "Flash size: %i\nSector size: %i\n" % (flash_size, sector_size) if fpga: + #validate fpga image name against hardware rev + if(n2xx_revs[hw_rev] not in fpga and hw_rev != 0): + raise Exception("Error: incorrect FPGA image version. Please use the correct image for device %s" % n2xx_revs[hw_rev]) + if safe: image_location = SAFE_FPGA_IMAGE_LOCATION_ADDR else: image_location = PROD_FPGA_IMAGE_LOCATION_ADDR -- cgit v1.2.3 From 4b0c097cfa67560116ea5380b42c12c89d4d3d8f Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Wed, 15 Jun 2011 17:49:08 -0700 Subject: fw_updater: add support for fictitious "rev 2" N2XX --- host/utils/usrp_n2xx_net_burner.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 6be632735..1fe62671c 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -55,10 +55,10 @@ FLASH_IP_FMT = '!LLLL260x' FLASH_HW_REV_FMT = '!LLLL260x' n2xx_revs = { - 0x0a00: "n200_r3", - 0x0a01: "n200_r4", - 0x0a01: "n210_r3", - 0x0a11: "n210_r4" + 0x0a00: ["n200_r3", "n200_r2"], + 0x0a10: ["n200_r4"], + 0x0a01: ["n210_r3", "n210_r2"], + 0x0a11: ["n210_r4"] } class update_id_t: @@ -179,13 +179,13 @@ class burner_socket(object): (flash_size, sector_size) = self.get_flash_info() hw_rev = self.get_hw_rev() - if(hw_rev != 0): print "Hardware type: %s" % n2xx_revs[hw_rev] + if(hw_rev != 0): print "Hardware type: %s" % n2xx_revs[hw_rev][0] print "Flash size: %i\nSector size: %i\n" % (flash_size, sector_size) if fpga: #validate fpga image name against hardware rev - if(n2xx_revs[hw_rev] not in fpga and hw_rev != 0): - raise Exception("Error: incorrect FPGA image version. Please use the correct image for device %s" % n2xx_revs[hw_rev]) + if(hw_rev != 0 and not any(name in fpga for name in n2xx_revs[hw_rev])): + raise Exception("Error: incorrect FPGA image version. Please use the correct image for device %s" % n2xx_revs[hw_rev][0]) if safe: image_location = SAFE_FPGA_IMAGE_LOCATION_ADDR else: image_location = PROD_FPGA_IMAGE_LOCATION_ADDR -- cgit v1.2.3 From 46fe7d86ee0425d9dc16f5a40c4d4bc061c43505 Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Wed, 15 Jun 2011 18:13:22 -0700 Subject: fw_updater: add broadcast addr finding for *nix/Win --- host/utils/usrp_n2xx_net_burner.py | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 1fe62671c..65ddfa4e7 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -26,6 +26,8 @@ import struct import socket import sys import time +import platform +import subprocess ######################################################################## # constants @@ -121,6 +123,74 @@ def is_valid_fpga_image(fpga_image): def is_valid_fw_image(fw_image): return fw_image[:4] == bytes(b'\x0B\x0B\x0B\x0B') + +######################################################################## +# interface discovery and device enumeration +######################################################################## +def get_interfaces(): + if(platform.system() is "Windows"): return win_get_interfaces() + else: return unix_get_interfaces() + +def unix_get_interfaces(): + ifconfig = subprocess.check_output("/sbin/ifconfig") + ip_addr_re = "cast\D*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" + bcasts = re.findall(ip_addr_re, ifconfig) + return bcasts + +def win_get_interfaces(): + from ctypes import Structure, windll, sizeof + from ctypes import POINTER, byref + from ctypes import c_ulong, c_uint, c_ubyte, c_char + MAX_ADAPTER_DESCRIPTION_LENGTH = 128 + MAX_ADAPTER_NAME_LENGTH = 256 + MAX_ADAPTER_ADDRESS_LENGTH = 8 + class IP_ADDR_STRING(Structure): + pass + LP_IP_ADDR_STRING = POINTER(IP_ADDR_STRING) + IP_ADDR_STRING._fields_ = [ + ("next", LP_IP_ADDR_STRING), + ("ipAddress", c_char * 16), + ("ipMask", c_char * 16), + ("context", c_ulong)] + class IP_ADAPTER_INFO (Structure): + pass + LP_IP_ADAPTER_INFO = POINTER(IP_ADAPTER_INFO) + IP_ADAPTER_INFO._fields_ = [ + ("next", LP_IP_ADAPTER_INFO), + ("comboIndex", c_ulong), + ("adapterName", c_char * (MAX_ADAPTER_NAME_LENGTH + 4)), + ("description", c_char * (MAX_ADAPTER_DESCRIPTION_LENGTH + 4)), + ("addressLength", c_uint), + ("address", c_ubyte * MAX_ADAPTER_ADDRESS_LENGTH), + ("index", c_ulong), + ("type", c_uint), + ("dhcpEnabled", c_uint), + ("currentIpAddress", LP_IP_ADDR_STRING), + ("ipAddressList", IP_ADDR_STRING), + ("gatewayList", IP_ADDR_STRING), + ("dhcpServer", IP_ADDR_STRING), + ("haveWins", c_uint), + ("primaryWinsServer", IP_ADDR_STRING), + ("secondaryWinsServer", IP_ADDR_STRING), + ("leaseObtained", c_ulong), + ("leaseExpires", c_ulong)] + GetAdaptersInfo = windll.iphlpapi.GetAdaptersInfo + GetAdaptersInfo.restype = c_ulong + GetAdaptersInfo.argtypes = [LP_IP_ADAPTER_INFO, POINTER(c_ulong)] + adapterList = (IP_ADAPTER_INFO * 10)() + buflen = c_ulong(sizeof(adapterList)) + rc = GetAdaptersInfo(byref(adapterList[0]), byref(buflen)) + if rc == 0: + for a in adapterList: + adNode = a.ipAddressList + while True: + ipAddr = adNode.ipAddress + if ipAddr: + yield ipAddr + adNode = adNode.next + if not adNode: + break + ######################################################################## # Burner class, holds a socket and send/recv routines ######################################################################## @@ -130,6 +200,7 @@ class burner_socket(object): self._sock.settimeout(UDP_TIMEOUT) self._sock.connect((addr, UDP_FW_UPDATE_PORT)) self.set_callbacks(lambda *a: None, lambda *a: None) + get_interfaces() self.init_update() #check that the device is there self.get_hw_rev() -- cgit v1.2.3 From c713f8a4727b140f8524dd38b81ab2a9077859ff Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Wed, 15 Jun 2011 18:45:44 -0700 Subject: fw_updater: device enumeration support, not integrated into main app yet --- host/utils/usrp_n2xx_net_burner.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 65ddfa4e7..878b8b28c 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -191,6 +191,28 @@ def win_get_interfaces(): if not adNode: break +def enumerate_devices(): + ifaces = get_interfaces() + devices = [] + for bcast_addr in ifaces: + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) + sock.settimeout(UDP_TIMEOUT) + print "Sending bcast pkt to %s" % bcast_addr + out_pkt = pack_flash_args_fmt(USRP2_FW_PROTO_VERSION, update_id_t.USRP2_FW_UPDATE_ID_OHAI_LOL, 0, 0, 0) + sock.sendto(out_pkt, (bcast_addr, UDP_FW_UPDATE_PORT)) + still_goin = True + while(still_goin): + try: + pkt = sock.recv(UDP_MAX_XFER_BYTES) + (proto_ver, pktid, rxseq, ip_addr) = unpack_flash_ip_fmt(pkt) + if(pktid == update_id_t.USRP2_FW_UPDATE_ID_OHAI_OMG): + devices.append(socket.inet_ntoa(struct.pack(" Date: Wed, 15 Jun 2011 18:58:49 -0700 Subject: fw_updater: decrease discovery timeout, modify win to return bcast instead of ip --- host/utils/usrp_n2xx_net_burner.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 878b8b28c..ec2454606 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -184,7 +184,7 @@ def win_get_interfaces(): for a in adapterList: adNode = a.ipAddressList while True: - ipAddr = adNode.ipAddress + ipAddr = (adNode.ipAddress & adNode.ipMask) | ~adNode.ipMask if ipAddr: yield ipAddr adNode = adNode.next @@ -197,8 +197,7 @@ def enumerate_devices(): for bcast_addr in ifaces: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) - sock.settimeout(UDP_TIMEOUT) - print "Sending bcast pkt to %s" % bcast_addr + sock.settimeout(0.1) out_pkt = pack_flash_args_fmt(USRP2_FW_PROTO_VERSION, update_id_t.USRP2_FW_UPDATE_ID_OHAI_LOL, 0, 0, 0) sock.sendto(out_pkt, (bcast_addr, UDP_FW_UPDATE_PORT)) still_goin = True -- cgit v1.2.3 From 88129dc89b753a1e9ac9016ff5ffa02c6492e508 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 16 Jun 2011 11:14:52 -0700 Subject: fw_updater: windows enumeration works --- host/utils/usrp_n2xx_net_burner.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index ec2454606..269126972 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -184,17 +184,21 @@ def win_get_interfaces(): for a in adapterList: adNode = a.ipAddressList while True: - ipAddr = (adNode.ipAddress & adNode.ipMask) | ~adNode.ipMask - if ipAddr: - yield ipAddr + #convert ipAddr and ipMask into hex addrs that can be turned into a bcast addr + ipAddr = adNode.ipAddress + ipMask = adNode.ipMask + if ipAddr and ipMask: + hexAddr = struct.unpack(" Date: Thu, 16 Jun 2011 12:31:57 -0700 Subject: N210: update burner for python3.0, added listbox for device selection in gui --- host/utils/usrp_n2xx_net_burner.py | 32 +++++++++++---------- host/utils/usrp_n2xx_net_burner_gui.py | 51 +++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 21 deletions(-) (limited to 'host/utils/usrp_n2xx_net_burner.py') diff --git a/host/utils/usrp_n2xx_net_burner.py b/host/utils/usrp_n2xx_net_burner.py index 269126972..1df300c76 100755 --- a/host/utils/usrp_n2xx_net_burner.py +++ b/host/utils/usrp_n2xx_net_burner.py @@ -185,8 +185,8 @@ def win_get_interfaces(): adNode = a.ipAddressList while True: #convert ipAddr and ipMask into hex addrs that can be turned into a bcast addr - ipAddr = adNode.ipAddress - ipMask = adNode.ipMask + ipAddr = adNode.ipAddress.decode() + ipMask = adNode.ipMask.decode() if ipAddr and ipMask: hexAddr = struct.unpack(" flash_size: raise Exception("Error: Cannot write past end of device") @@ -317,7 +314,7 @@ class burner_socket(object): if not is_valid_fw_image(fw_image): raise Exception("Error: Invalid firmware image file.") - + if (len(fw_image) + image_location) > flash_size: raise Exception("Error: Cannot write past end of device") @@ -339,7 +336,7 @@ class burner_socket(object): (mem_size, sector_size) = self.get_flash_info() if (addr + len(writedata)) > mem_size: raise Exception("Error: Cannot write past end of device") - + while writedata: out_pkt = pack_flash_args_fmt(USRP2_FW_PROTO_VERSION, update_id_t.USRP2_FW_UPDATE_ID_WRITE_TEH_FLASHES_LOL, seq(), addr, FLASH_DATA_PACKET_SIZE, writedata[:FLASH_DATA_PACKET_SIZE]) in_pkt = self.send_and_recv(out_pkt) @@ -426,7 +423,7 @@ class burner_socket(object): (flash_size, sector_size) = self.get_flash_info() if (addr + length) > flash_size: raise Exception("Cannot erase past end of device") - + out_pkt = pack_flash_args_fmt(USRP2_FW_PROTO_VERSION, update_id_t.USRP2_FW_UPDATE_ID_ERASE_TEH_FLASHES_LOL, seq(), addr, length) in_pkt = self.send_and_recv(out_pkt) @@ -463,6 +460,7 @@ def get_options(): parser.add_option("--reset", action="store_true", help="reset the device after writing", default=False) parser.add_option("--read", action="store_true", help="read to file instead of write from file", default=False) parser.add_option("--overwrite-safe", action="store_true", help="never ever use this option", default=False) + parser.add_option("--list", action="store_true", help="list possible network devices", default=False) (options, args) = parser.parse_args() return options @@ -472,6 +470,12 @@ def get_options(): ######################################################################## if __name__=='__main__': options = get_options() + + if options.list: + print('Possible network devices:') + print(' ' + '\n '.join(enumerate_devices())) + exit() + if not options.addr: raise Exception('no address specified') if not options.fpga and not options.fw and not options.reset: raise Exception('Must specify either a firmware image or FPGA image, and/or reset.') diff --git a/host/utils/usrp_n2xx_net_burner_gui.py b/host/utils/usrp_n2xx_net_burner_gui.py index 3b414a918..e2b79e72c 100755 --- a/host/utils/usrp_n2xx_net_burner_gui.py +++ b/host/utils/usrp_n2xx_net_burner_gui.py @@ -84,6 +84,45 @@ class ProgressBar(tkinter.Canvas): if frac: self.create_rectangle(0, 0, fill_pixels, self._height, fill="#357EC7") else: self.create_rectangle(0, 0, self._width, self._height, fill="#E8E8E8") +class DeviceEntryWidget(tkinter.Frame): + """ + Simple entry widget for getting the network device name. + Combines a label, entry, and helpful text box with hints. + """ + + def __init__(self, root, text=''): + tkinter.Frame.__init__(self, root) + + tkinter.Button(self, text="Rescan for Devices", command=self._reload_cb).pack() + + self._hints = tkinter.Listbox(self) + self._hints.bind("<>", self._listbox_cb) + self._reload_cb() + self._hints.pack(expand=tkinter.YES, fill=tkinter.X) + + frame = tkinter.Frame(self) + frame.pack() + + tkinter.Label(frame, text="Network Address:").pack(side=tkinter.LEFT) + self._entry = tkinter.Entry(frame, width=50) + self._entry.insert(tkinter.END, text) + self._entry.pack(side=tkinter.LEFT) + + def _reload_cb(self): + self._hints.delete(0, tkinter.END) + for hint in usrp_n2xx_net_burner.enumerate_devices(): + self._hints.insert(tkinter.END, hint) + + def _listbox_cb(self, event): + try: + sel = self._hints.get(self._hints.curselection()[0]) + self._entry.delete(0, tkinter.END) + self._entry.insert(0, sel) + except Exception as e: print(e) + + def get_devname(self): + return self._entry.get() + class SectionLabel(tkinter.Label): """ Make a text label with bold font. @@ -115,10 +154,9 @@ class USRPN2XXNetBurnerApp(tkinter.Frame): self._fpga_img_entry.pack() #pack the destination entry widget - SectionLabel(self, text="Select Address").pack(pady=5) - self._addr_entry = tkinter.Entry(self, width=30) - self._addr_entry.insert(tkinter.END, addr) - self._addr_entry.pack() + SectionLabel(self, text="Select Device").pack(pady=5) + self._net_dev_entry = DeviceEntryWidget(self, text=addr) + self._net_dev_entry.pack() #the do it button SectionLabel(self, text="").pack(pady=5) @@ -136,12 +174,10 @@ class USRPN2XXNetBurnerApp(tkinter.Frame): self._pbar.pack(side=tkinter.RIGHT, expand=True) def _burn(self): - self._disable_input() - #grab strings from the gui fw = self._fw_img_entry.get_filename() fpga = self._fpga_img_entry.get_filename() - addr = self._addr_entry.get() + addr = self._net_dev_entry.get_devname() #check input if not addr: @@ -157,6 +193,7 @@ class USRPN2XXNetBurnerApp(tkinter.Frame): tkinter.messagebox.showerror('Error:', 'FPGA image not found!') return + self._disable_input() try: #make a new burner object and attempt the burner operation burner = usrp_n2xx_net_burner.burner_socket(addr=addr) -- cgit v1.2.3