2020-10-17 14:31:55 -05:00
|
|
|
#!/usr/bin/python3
|
2020-04-28 15:16:19 -05:00
|
|
|
|
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
from os import *
|
|
|
|
from mmap import *
|
|
|
|
|
|
|
|
def usage():
|
2020-10-17 14:31:55 -05:00
|
|
|
''' This is the Usage Method '''
|
2020-04-28 15:16:19 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
print('\t\t pcisysfs.py --get --offset <offset> --res <resource>')
|
|
|
|
print('\t\t pcisysfs.py --set --val <val> --offset <offset> --res <resource>')
|
|
|
|
sys.exit(1)
|
2020-04-28 15:16:19 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def pci_mem_read(mm, offset):
|
2020-04-28 15:16:19 -05:00
|
|
|
mm.seek(offset)
|
2020-10-17 14:31:55 -05:00
|
|
|
read_data_stream = mm.read(4)
|
|
|
|
print("")
|
|
|
|
reg_val = struct.unpack('I', read_data_stream)
|
|
|
|
print("reg_val read:%x" % reg_val)
|
2020-04-28 15:16:19 -05:00
|
|
|
return reg_val
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def pci_mem_write(mm, offset, data):
|
2020-04-28 15:16:19 -05:00
|
|
|
mm.seek(offset)
|
2020-10-17 14:31:55 -05:00
|
|
|
#print("data to write:%x" % data)
|
|
|
|
mm.write(struct.pack('I', data))
|
|
|
|
|
|
|
|
def pci_set_value(resource, val, offset):
|
|
|
|
fd = open(resource, O_RDWR)
|
|
|
|
mm = mmap(fd, 0)
|
|
|
|
pci_mem_write(mm, offset, val)
|
|
|
|
mm.close()
|
2020-04-28 15:16:19 -05:00
|
|
|
close(fd)
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def pci_get_value(resource, offset):
|
|
|
|
fd = open(resource, O_RDWR)
|
|
|
|
mm = mmap(fd, 0)
|
|
|
|
pci_mem_read(mm, offset)
|
|
|
|
mm.close()
|
2020-04-28 15:16:19 -05:00
|
|
|
close(fd)
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
|
|
|
''' The main function will read the user input from the
|
|
|
|
command line argument and process the request '''
|
|
|
|
|
|
|
|
opts = ''
|
|
|
|
val = ''
|
|
|
|
choice = ''
|
|
|
|
resource = ''
|
|
|
|
offset = ''
|
|
|
|
|
|
|
|
try:
|
2020-10-17 14:31:55 -05:00
|
|
|
opts, args = getopt.getopt(argv, "hgsv:",
|
|
|
|
["val=", "res=", "offset=", "help", "get", "set"])
|
|
|
|
|
2020-04-28 15:16:19 -05:00
|
|
|
except getopt.GetoptError:
|
|
|
|
usage()
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
for opt, arg in opts:
|
2020-04-28 15:16:19 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
if opt in ('-h', '--help'):
|
2020-04-28 15:16:19 -05:00
|
|
|
choice = 'help'
|
|
|
|
|
|
|
|
elif opt in ('-g', '--get'):
|
|
|
|
choice = 'get'
|
|
|
|
|
|
|
|
elif opt in ('-s', '--set'):
|
|
|
|
choice = 'set'
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif opt == '--res':
|
2020-04-28 15:16:19 -05:00
|
|
|
resource = arg
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif opt == '--val':
|
|
|
|
val = int(arg, 16)
|
2020-04-28 15:16:19 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif opt == '--offset':
|
|
|
|
offset = int(arg, 16)
|
2020-04-28 15:16:19 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
if choice == 'set' and val != '' and offset != '' and resource != '':
|
|
|
|
pci_set_value(resource, val, offset)
|
2020-04-28 15:16:19 -05:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif choice == 'get' and offset != '' and resource != '':
|
|
|
|
pci_get_value(resource, offset)
|
2020-04-28 15:16:19 -05:00
|
|
|
|
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
# Calling the main method
|
2020-04-28 15:16:19 -05:00
|
|
|
if __name__ == "__main__":
|
2020-10-17 14:31:55 -05:00
|
|
|
main(sys.argv[1:])
|