Commit 42a97e15 authored by Grzegorz Daniluk's avatar Grzegorz Daniluk

sdbfs/gen_flash_image.py: code cleanup

parent 2d4337bc
......@@ -20,12 +20,11 @@ import os
import shutil
#class CSDBGenerator :
MAC_FILE = "sdbfs-<type>/mac-address"
BSTR_FILE = "sdbfs-flash/bitstream"
BSTR_CMD = "cp <bstr> <path>sdbfs-flash/bitstream"
BSTR_TRUNC = "> <path>sdbfs-flash/bitstream"
GEN_FLASH_CMD = "<path>./gensdbfs -b 65536 <path>sdbfs-flash <path>sdbfs-flash-<mac>.bin"
GEN_EEPROM_CMD = "<path>./gensdbfs <path>sdbfs-eeprom <path>sdbfs-eeprom-<mac>.bin"
SDBFS_MAC = "{path}sdbfs-{type}/mac-address"
SDBFS_BSTR = "{path}sdbfs-flash/bitstream"
SDBFS_IMG = "{path}sdbfs-{type}-{mac}.bin"
GEN_FLASH_CMD = "{path}./gensdbfs -b 65536 {path}sdbfs-flash {img}"
GEN_EEPROM_CMD = "{path}./gensdbfs {path}sdbfs-eeprom {img}"
###########################################################
......@@ -39,47 +38,53 @@ def check_mac(mac):
###########################################################
# type can be either "flash" or "eeprom"
def gen_sdb_image(type, mac, bstr, output):
def gen_sdb_image(type, mac, bstr, output=None):
if not check_mac(mac):
return
# Translate MAC to be always in XX-XX-XX-XX-XX-XX format
mac = mac.replace(':', '-')
# Get the absolute path where this script resides. This lets us have always
# access to the sdbfs-flash/eeprom structure, no matter where this script is
# called.
abs_path = os.path.dirname(os.path.abspath(__file__)) + '/'
# 1. write MAC address to the file in SDBFS
mac_bytes = [int(i, 16) for i in mac.split('-')]
fname = MAC_FILE.replace("<type>", type)
f = open(os.path.join(abs_path, fname), 'wb')
sdbfs_mac = SDBFS_MAC.format(path=abs_path, type=type)
f = open(sdbfs_mac, 'wb')
f.truncate()
for byte in mac_bytes:
f.write(chr(byte))
f.close()
# 2. copy bitstream to SDBFS, if needed
sdbfs_bstr = SDBFS_BSTR.format(path=abs_path) #<abs_path>/sdbfs-flash/bitstream
if type == "flash" and bstr:
print "Including bitstream " + bstr
fname = os.path.join(abs_path, BSTR_FILE)
subprocess.Popen("cp "+bstr+" " + fname, shell=True).wait()
shutil.copy(bstr, sdbfs_bstr)
elif type == "flash":
#truncate bitstream file if not given for flash
print "No bitstream given for flash, truncating"
fname = os.path.join(abs_path, BSTR_FILE)
subprocess.Popen("> "+fname, shell=True).wait()
f = open(sdbfs_bstr, 'wb')
f.truncate()
f.close()
# 3. generate SDBFS image
sdbfs_img = SDBFS_IMG.format(path=abs_path, type=type, mac=mac)
if type == "flash":
# gensdbfs for flash
fname = GEN_FLASH_CMD.replace("<mac>",mac)
cmd = fname.replace("<path>", abs_path)
subprocess.Popen(cmd, shell=True).wait()
shutil.copy(abs_path + "sdbfs-flash-"+mac+".bin", output)
print "Generated sdbfs-flash-" + mac
cmd = GEN_FLASH_CMD.format(path=abs_path, img=sdbfs_img)
elif type == "eeprom":
fname = GEN_EEPROM_CMD.replace("<mac>",mac)
path = os.path.join(abs_path, fname)
subprocess.Popen(path, shell=True).wait()
print "Generated sdbfs-eeprom-" + mac
# gensdbfs for eeprom
cmd = GEN_EEPROM_CMD.format(path=abs_path, img=sdbfs_img)
subprocess.Popen(cmd, shell=True).wait()
print "Generated " + sdbfs_img
# 4. Copy generated SDBFS image to <output>
if output:
shutil.copy(sdbfs_img, output)
print "Generated image (" + sdbfs_img + ") copied to " + output
###########################################################
#if len(sys.argv) < 3:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment