Commit 2d4337bc authored by Grzegorz Daniluk's avatar Grzegorz Daniluk

sdbfs: python script to generate sdbfs image with MAC and bitstream

parent 0c65999f
#!/usr/bin/python
################################################################################
##
## The script is part of the SPEC and SVEC PTS. It is used to generate SDBFS
## image that is then stored in Flash or EEPROM memory. The image contains MAC
## address and optionally also an FPGA bitstream. The main purpose of including
## it to the PTS is to allow manufacturers assigning official MAC addresses to
## SPEC and SVEC boards.
##
## Copyright (C) 2017 CERN (www.cern.ch)
## Author: Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
##
################################################################################
import sys
import re
import subprocess
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"
###########################################################
def check_mac(mac):
if not re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()):
print "Not a valid MAC"
return 0
return 1
###########################################################
# type can be either "flash" or "eeprom"
def gen_sdb_image(type, mac, bstr, output):
if not check_mac(mac):
return
mac = mac.replace(':', '-')
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')
f.truncate()
for byte in mac_bytes:
f.write(chr(byte))
f.close()
# 2. copy bitstream to SDBFS, if needed
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()
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()
# 3. generate SDBFS image
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
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
###########################################################
#if len(sys.argv) < 3:
# print "Wrong syntax"
# print sys.argv[0] + " <flash/eeprom> <mac> [bitstream]"
# sys.exit()
#
#type = sys.argv[1]
#if type != "flash" and type != "eeprom":
# print "Wrong syntax"
# print sys.argv[0] + " <flash/eeprom> <mac> [bitstream]"
# sys.exit()
#
#mac = check_mac(sys.argv[2])
#
#if len(sys.argv) > 3:
# bitstream = sys.argv[3]
#else:
# bitstream = ""
#
#gen_sdb_image(type, mac, bitstream)
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