ldr_config.py: better exception handling
This commit is contained in:
116
ldr_config.py
116
ldr_config.py
@@ -1,5 +1,4 @@
|
|||||||
#!python
|
#!python3
|
||||||
|
|
||||||
cust_conf = {
|
cust_conf = {
|
||||||
# DRAM Timing:
|
# DRAM Timing:
|
||||||
# 0: AUTO_ADJ_MARIKO_SAFE: Auto adjust timings for LPDDR4 ≤3733 Mbps specs, 8Gb density (Default).
|
# 0: AUTO_ADJ_MARIKO_SAFE: Auto adjust timings for LPDDR4 ≤3733 Mbps specs, 8Gb density (Default).
|
||||||
@@ -52,20 +51,20 @@ cust_conf = {
|
|||||||
# Not enabled by default.
|
# Not enabled by default.
|
||||||
"eristaEmcMaxClock": 1862400,
|
"eristaEmcMaxClock": 1862400,
|
||||||
"eristaEmcVolt": 0
|
"eristaEmcVolt": 0
|
||||||
}
|
}
|
||||||
|
|
||||||
cust_range = {"mtcConf": (0, 3),
|
cust_range = {
|
||||||
|
"mtcConf": (0, 3),
|
||||||
"marikoCpuMaxClock": (1785000, 3000000),
|
"marikoCpuMaxClock": (1785000, 3000000),
|
||||||
"marikoCpuMaxVolt": (1100, 1300),
|
"marikoCpuMaxVolt": (1100, 1300),
|
||||||
"marikoGpuMaxClock": (768000, 1536000),
|
"marikoGpuMaxClock": (768000, 1536000),
|
||||||
"marikoEmcMaxClock": (1612800, 2400000),
|
"marikoEmcMaxClock": (1612800, 2400000),
|
||||||
"eristaCpuMaxVolt": (1100, 1400),
|
"eristaCpuMaxVolt": (1100, 1400),
|
||||||
"eristaEmcMaxClock": (1600000, 2400000),
|
"eristaEmcMaxClock": (1600000, 2400000),
|
||||||
"eristaEmcVolt": (1100000, 1250000)}
|
"eristaEmcVolt": (1100000, 1250000)
|
||||||
|
}
|
||||||
|
|
||||||
import struct
|
import struct
|
||||||
import csv
|
|
||||||
from pprint import pprint
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
cust_rev = 1
|
cust_rev = 1
|
||||||
@@ -81,37 +80,7 @@ parser.add_argument("--save", "-s", action="store_true", help="Save configuratio
|
|||||||
parser.add_argument("--ignore", action="store_true", help="Ignore range safety check")
|
parser.add_argument("--ignore", action="store_true", help="Ignore range safety check")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
def CSVRead(file_loc):
|
def KIPCustParse(file_loc, conf_print=True) -> (int, dict):
|
||||||
with open(file_loc, 'r') as file:
|
|
||||||
rd = csv.reader(file)
|
|
||||||
dct = {rows[0]:rows[1] for rows in rd}
|
|
||||||
file.close()
|
|
||||||
return dct
|
|
||||||
|
|
||||||
def CSVWrite(file_loc, dct):
|
|
||||||
with open(file_loc, 'w') as file:
|
|
||||||
for key, value in dct.items():
|
|
||||||
file.write('{0},{1}\n'.format(key, value))
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
def CustSafetyCheck(cust):
|
|
||||||
warn_cnt = 0
|
|
||||||
for i in cust_body:
|
|
||||||
val = int(cust[i])
|
|
||||||
if val and val not in range(*cust_range[i]):
|
|
||||||
warn_cnt += 1
|
|
||||||
print("[!] %s = %u (Expected range: %u ≤ value ≤ %u)" % (i, val, *cust_range[i]))
|
|
||||||
return warn_cnt
|
|
||||||
|
|
||||||
def KIPCustHandler(file_loc, cust={}):
|
|
||||||
if len(cust) != 0:
|
|
||||||
missing = set(cust_body) - set(cust.keys())
|
|
||||||
if missing:
|
|
||||||
print("Invalid cust dict! Missing: %s" % missing)
|
|
||||||
return
|
|
||||||
if CustSafetyCheck(cust) and args.ignore == False:
|
|
||||||
return
|
|
||||||
|
|
||||||
with open(file_loc, "rb") as file:
|
with open(file_loc, "rb") as file:
|
||||||
header_str = b'KIP1Loader'
|
header_str = b'KIP1Loader'
|
||||||
header = file.read(len(header_str))
|
header = file.read(len(header_str))
|
||||||
@@ -120,8 +89,7 @@ def KIPCustHandler(file_loc, cust={}):
|
|||||||
cust_pos = file.read().find(cust_magic)
|
cust_pos = file.read().find(cust_magic)
|
||||||
|
|
||||||
if header != header_str or cust_pos == -1:
|
if header != header_str or cust_pos == -1:
|
||||||
print("Invalid kip file!")
|
raise Exception("\n Invalid kip file!")
|
||||||
return False
|
|
||||||
|
|
||||||
file.seek(cust_pos)
|
file.seek(cust_pos)
|
||||||
cust_fmt = '<4s2H8I'
|
cust_fmt = '<4s2H8I'
|
||||||
@@ -129,36 +97,70 @@ def KIPCustHandler(file_loc, cust={}):
|
|||||||
cust_buf = file.read(cust_size)
|
cust_buf = file.read(cust_size)
|
||||||
cust_val = struct.unpack(cust_fmt, cust_buf)
|
cust_val = struct.unpack(cust_fmt, cust_buf)
|
||||||
cust_dict = dict(zip(cust_key, cust_val))
|
cust_dict = dict(zip(cust_key, cust_val))
|
||||||
file.close()
|
|
||||||
|
|
||||||
if cust_dict['custRev'] != cust_rev:
|
if cust_dict['custRev'] != cust_rev:
|
||||||
print("custRev does NOT match, expected: %u, got: %u!" % (cust_rev, cust_dict['custRev']))
|
raise Exception(f"\n custRev does NOT match, expected: {cust_rev}, got: {cust_dict['custRev']}!")
|
||||||
return False
|
|
||||||
|
|
||||||
if cust == {}:
|
|
||||||
[cust_dict.pop(key) for key in cust_head]
|
[cust_dict.pop(key) for key in cust_head]
|
||||||
return cust_dict
|
|
||||||
|
if conf_print:
|
||||||
|
print("Configuration from file")
|
||||||
|
[print(f"- {i:18s} : {cust_dict[i]:8d}") for i in cust_dict]
|
||||||
|
|
||||||
|
return (cust_pos, cust_dict)
|
||||||
|
|
||||||
|
|
||||||
|
def CustRangeCheck(cust):
|
||||||
|
range_error_str = ""
|
||||||
|
for i in cust_range:
|
||||||
|
val = int(cust[i])
|
||||||
|
if val and (val < cust_range[i][0] or val > cust_range[i][1]) :
|
||||||
|
range_error_str += f"\n- {i:18s} = {val:8d}, Expected range: {[*cust_range[i]]}"
|
||||||
|
|
||||||
|
if range_error_str:
|
||||||
|
raise ValueError(range_error_str)
|
||||||
|
|
||||||
|
|
||||||
|
def KIPCustSave(file_loc, cust_pos, cust_dict, range_check=True, cust_to_save={}):
|
||||||
|
missing = set(cust_body) - set(cust_to_save.keys())
|
||||||
|
if missing:
|
||||||
|
missing_str = "\n Invalid cust! Missing: "
|
||||||
|
for i in missing:
|
||||||
|
missing_str += f"\n- {i}"
|
||||||
|
raise Exception(missing_str)
|
||||||
|
|
||||||
|
if range_check:
|
||||||
|
CustRangeCheck(cust_to_save)
|
||||||
|
|
||||||
|
diff_count = 0
|
||||||
|
for i in cust_body:
|
||||||
|
diff_str = ""
|
||||||
|
if cust_dict[i] != cust_conf[i]:
|
||||||
|
diff_str = f"-> {cust_conf[i]:8d}"
|
||||||
|
diff_count += 1
|
||||||
|
print(f"- {i:18s} : {cust_dict[i]:8d} {diff_str}")
|
||||||
|
|
||||||
|
if not diff_count:
|
||||||
|
print("Cust is identical, abort saving!")
|
||||||
|
return
|
||||||
|
|
||||||
with open(file_loc, "rb+") as file:
|
with open(file_loc, "rb+") as file:
|
||||||
cust_head_fmt = '<4s1H'
|
cust_head_fmt = '<4s1H'
|
||||||
cust_body_fmt = '<1H8I'
|
cust_body_fmt = '<1H8I'
|
||||||
cust_bin = struct.pack(cust_body_fmt, *[cust[i] for i in cust_body])
|
cust_bin = struct.pack(cust_body_fmt, *[cust_to_save[i] for i in cust_body])
|
||||||
file.seek(cust_pos + struct.calcsize(cust_head_fmt))
|
file.seek(cust_pos + struct.calcsize(cust_head_fmt))
|
||||||
file.write(cust_bin)
|
file.write(cust_bin)
|
||||||
file.close()
|
|
||||||
print("Done!")
|
print("Done!")
|
||||||
|
|
||||||
def main(file_loc, save=False):
|
|
||||||
cust = KIPCustHandler(file_loc)
|
def main(file_loc, ignore=False, save=False):
|
||||||
if not cust:
|
(cust_pos, cust_dict) = KIPCustParse(file_loc, conf_print=(not save))
|
||||||
return
|
|
||||||
print("Configuration from file:")
|
|
||||||
pprint(cust, sort_dicts=False)
|
|
||||||
|
|
||||||
if save:
|
if save:
|
||||||
print("Saving new configuration...:")
|
print("Saving new configuration...")
|
||||||
pprint(cust_conf, sort_dicts=False)
|
KIPCustSave(file_loc, cust_pos, cust_dict, range_check=(not ignore), cust_to_save=cust_conf)
|
||||||
KIPCustHandler(file_loc, cust_conf)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main(args.file, args.save)
|
main(args.file, args.ignore, args.save)
|
||||||
|
|||||||
Reference in New Issue
Block a user