2020-10-17 14:31:55 -05:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# Script to read/write the nvram
|
2020-02-18 02:02:33 -06:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import getopt
|
|
|
|
import struct
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
nvram_resource = '/dev/nvram'
|
2020-02-18 02:02:33 -06:00
|
|
|
|
|
|
|
def usage():
|
|
|
|
''' This is the Usage Method '''
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
print('Utility for NVRAM read/write')
|
|
|
|
print('\t\t nvram_rd_wr.py --get --offset <offset>')
|
|
|
|
print('\t\t nvram_rd_wr.py --set --val <val> --offset <offset>')
|
2020-02-18 02:02:33 -06:00
|
|
|
sys.exit(1)
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def nvram_reg_read(nvram_resource, offset):
|
|
|
|
fd = os.open(nvram_resource, os.O_RDONLY)
|
|
|
|
if(fd < 0):
|
|
|
|
print('file open failed %s' % nvram_resource)
|
2020-02-18 02:02:33 -06:00
|
|
|
return
|
|
|
|
if(os.lseek(fd, offset, os.SEEK_SET) != offset):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('lseek failed on %s' % nvram_resource)
|
2020-02-18 02:02:33 -06:00
|
|
|
return
|
2020-10-17 14:31:55 -05:00
|
|
|
buf = os.read(fd, 1)
|
|
|
|
reg_val1 = ord(buf)
|
|
|
|
print('value %x' % reg_val1)
|
2020-02-18 02:02:33 -06:00
|
|
|
os.close(fd)
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
def nvram_reg_write(nvram_resource, offset, val):
|
|
|
|
fd = os.open(nvram_resource, os.O_RDWR)
|
|
|
|
if(fd < 0):
|
|
|
|
print('file open failed %s' % nvram_resource)
|
2020-02-18 02:02:33 -06:00
|
|
|
return
|
|
|
|
if(os.lseek(fd, offset, os.SEEK_SET) != offset):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('lseek failed on %s' % nvram_resource)
|
2020-02-18 02:02:33 -06:00
|
|
|
return
|
2020-10-17 14:31:55 -05:00
|
|
|
ret = os.write(fd, struct.pack('B', val))
|
2020-02-18 02:02:33 -06:00
|
|
|
if(ret != 1):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('write failed %d' % ret)
|
2020-02-18 02:02:33 -06:00
|
|
|
return
|
|
|
|
os.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 = ''
|
|
|
|
offset = ''
|
|
|
|
|
|
|
|
try:
|
2020-10-17 14:31:55 -05:00
|
|
|
opts, args = getopt.getopt(argv, "hgs:",
|
|
|
|
["val=", "offset=", "help", "get", "set"])
|
2020-02-18 02:02:33 -06:00
|
|
|
except getopt.GetoptError:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
if not os.path.exists(nvram_resource):
|
2020-10-17 14:31:55 -05:00
|
|
|
print('NVRAM is not initialized')
|
2020-02-18 02:02:33 -06:00
|
|
|
sys.exit(1)
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
for opt, arg in opts:
|
2020-02-18 02:02:33 -06:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
if opt in ('-h', '--help'):
|
2020-02-18 02:02:33 -06: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 == '--offset':
|
|
|
|
offset = int(arg, 16) - 0xE
|
2020-02-18 02:02:33 -06:00
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
elif opt == '--val':
|
|
|
|
val = int(arg, 16)
|
2020-02-18 02:02:33 -06:00
|
|
|
|
|
|
|
if choice == 'get' and offset != '':
|
2020-10-17 14:31:55 -05:00
|
|
|
nvram_reg_read(nvram_resource, offset)
|
2020-02-18 02:02:33 -06:00
|
|
|
|
|
|
|
elif choice == 'set' and offset != '' and val != '':
|
2020-10-17 14:31:55 -05:00
|
|
|
nvram_reg_write(nvram_resource, offset, val)
|
2020-02-18 02:02:33 -06:00
|
|
|
|
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
|
2020-10-17 14:31:55 -05:00
|
|
|
# Calling the main method
|
2020-02-18 02:02:33 -06:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|