-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_device_id.py
51 lines (40 loc) · 1.42 KB
/
gen_device_id.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import argparse, sys, re
parser = argparse.ArgumentParser()
# Hardware Revision Pattern (Semantic Versioned) e.g. 0.1.0
hw_pattern = re.compile("^([0-9]+)\.([0-9]+)\.([0-9]+)$")
# Serial Number Pattern
# A##-YYWW-####
# A(amp revision)-(year last two)(week number)-(unique identifier)
sn_pattern = re.compile("^A[0-9a-zA-Z]{2}-[0-9]{4}-[0-9a-zA-Z]{4}$")
parser.add_argument('--hw', help='Hardware Revision')
parser.add_argument('--sn', help='Serial Number')
parser.add_argument('--output', '-o', help="Output File")
args = parser.parse_args()
def get_hw_bytes():
split_hw = args.hw.split(".")
major = int(split_hw[0])
minor = int(split_hw[1])
patch = int(split_hw[2])
return [major, minor, patch]
def get_sn_bytes():
split_sn = args.sn.split("-")
model = int("0x{0}".format(split_sn[0][1:]), 16)
year = int(split_sn[1][0:2])
week = int(split_sn[1][2:])
unique_one = int("0x{0}".format(split_sn[2][0:2]), 16)
unique_two = int("0x{0}".format(split_sn[2][2:]), 16)
return [model, year, week, unique_one, unique_two]
hw_result = hw_pattern.match(args.hw)
sn_result = sn_pattern.match(args.sn)
if not args.hw or not args.sn or hw_result is None or sn_result is None:
print("valid hw and sn required")
exit()
if (not args.output):
filename = "device_id.bin"
else:
filename = args.output
data = []
data.extend(get_hw_bytes())
data.extend(get_sn_bytes())
output = open(filename, "wb")
output.write(bytearray(data))