-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashing_binaries.py
167 lines (145 loc) · 6.21 KB
/
flashing_binaries.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import multiprocessing
import tarfile, glob, shlex
import os, sys, time, argparse
from functools import partial
from collections import defaultdict
from scapy.all import srp, Ether, ARP, conf
from ChromeTestLib import ChromeTestLib
cwd = os.getcwd()
bin_location = cwd + "/latest"
test = ChromeTestLib()
parser = argparse.ArgumentParser()
parser.add_argument('--IP',dest='ip',nargs='?',type=str,metavar=('IP_list.txt'),default='IPs.txt',help='list of IPs to flash')
args = parser.parse_args()
with open(("%s/%s" % (cwd,args.ip))) as f:
ip_list=list()
ip_lines=f.readlines()
for ip in ip_lines:
ip_list.append(ip.rstrip())
f.close()
def createFolders(absFolderPath):
try:
os.makedirs(absFolderPath)
return True
except IOError as err:
print ("I/O error({0}): {1}".format(err))
sys.exit(1)
except OSError as e:
print ("Exception found!!!")
print (e)
sys.exit(1)
def find_and_return_latest_binaries(binaries_folder_location):
d = dict()
try:
files_array = os.listdir(binaries_folder_location)
except OSError as e:
print (e)
return False
if not files_array or len(files_array) > 2: #it should have max 2 files inside.
return False
for file in files_array:
if file.endswith(".bin"):
file_path = os.path.join(binaries_folder_location, file)
file_size = os.path.getsize(file_path)
if 400000 < file_size < 600000:
abs_ec_image_path = file_path
d["ec"] = abs_ec_image_path
if 15000000 < file_size:
abs_cb_image_path = file_path
d["cb"] = abs_cb_image_path
if not d:
print ("cb/ec images are not copied to binaries folder.")
return False
else:
return d
def FlashBinaries(dut_ip, email, cwd, cbImageSrc = "", ecImageSrc = ""):
flashDict = dict()
flashing_status = "FAIL"
cbFlashStatus = False
ecFlashStatus = False
if test.check_if_remote_system_is_live(dut_ip):
print("DUT IP: %s is live.\n" % dut_ip)
test.adding_to_results(("DUT IP: %s --> ONLINE" % dut_ip),cwd)
before_flash=test.check_bin_version(cwd,dut_ip)
if cbImageSrc:
cbImageDest = "/tmp/autoflashCB.bin"
copy_cb = test.copy_file_from_host_to_dut(cbImageSrc, cbImageDest, dut_ip, cwd="")
cbCmd = "flashrom -p host -w " + cbImageDest
cbFlashStatus = test.run_command_to_check_non_zero_exit_status(cwd, cbCmd, dut_ip)
if cbFlashStatus:
print("DUT IP: %s [CB Flash Successful]\n" % dut_ip)
if not cbFlashStatus:
print("DUT IP: %s [CB Flash Unsuccessful]\n" % dut_ip)
if ecImageSrc:
ecImageDest = "/tmp/autoflashEC.bin"
copy_ec = test.copy_file_from_host_to_dut(ecImageSrc, ecImageDest, dut_ip, cwd="")
ecCmd = "flashrom -p ec -w " + ecImageDest
ecFlashStatus = test.run_command_to_check_non_zero_exit_status(cwd, ecCmd, dut_ip)
if ecFlashStatus:
print("DUT IP: %s [EC Flash Successful]\n" % dut_ip)
if not ecFlashStatus:
print("DUT IP: %s [EC Flash Unsuccessful]\n" % dut_ip)
if cbFlashStatus and ecFlashStatus:
""" this is required for rebooting after flash """
test.run_async_command("sleep 2; reboot > /dev/null 2>&1", dut_ip)
print("Pinging DUT IP: %s\n" % dut_ip)
for i in range(60):
time.sleep(2)
if test.check_if_remote_system_is_live(dut_ip):
flashing_status = "PASS"
flashDict[dut_ip] = flashing_status
after_flash=test.check_bin_version(cwd,dut_ip)
test.comparing_versions(before_flash, after_flash, dut_ip)
test.storing_results(before_flash, after_flash, dut_ip, cwd)
print("DUT IP: %s is back online.\n" % dut_ip)
return flashDict
time.sleep(3)
flashDict[dut_ip] = flashing_status
resultDict.update(flashDict)
return flashDict
else:
flashDict[dut_ip] = flashing_status
resultDict.update(flashDict)
after_flash=test.check_bin_version(cwd,dut_ip)
test.comparing_versions(before_flash, after_flash, dut_ip)
test.storing_results(before_flash, after_flash, dut_ip, cwd)
return flashDict
else:
print("DUT IP: %s is not live.\n" % dut_ip)
test.adding_to_results(("DUT IP: %s --> OFFLINE" % dut_ip),cwd)
flashDict[dut_ip] = flashing_status
resultDict.update(flashDict)
return flashDict
if __name__ == "__main__":
email=input("Please enter an E-mail to receive version comparisons (or press enter to skip): ")
# email='results.cssdesk@gmail.com'
t1=time.perf_counter()
flash_ec = flash_cb = False
resultDict = dict()
#Check if destination folder exist else exit
if not os.path.isdir(bin_location):
print("Binaries folder doesn't exist. Creating one. Copy binaries into folder named latest and rerun flashing script!")
createFolders(bin_location)
sys.exit(1)
binaryDict = find_and_return_latest_binaries(bin_location)
if binaryDict:
if not "ec" in binaryDict:
binaryDict["ec"] = ""
if not "cb" in binaryDict:
binaryDict["cb"] = ""
else:
print("Binaries are not available. Copy binaries into folder named latest and rerun flashing script!")
sys.exit(1)
with multiprocessing.Pool(multiprocessing.cpu_count()) as pool:
resultDict = pool.map(partial(FlashBinaries, email = email, cwd = cwd,
cbImageSrc = binaryDict["cb"], ecImageSrc = binaryDict["ec"]), ip_list)
print("************************************************************************")
print(resultDict)
test.adding_to_results(("*********************\n" + "Flash Results:"), cwd)
test.convert_dict(resultDict, cwd)
test.mailing_results(cwd, email)
t2=time.perf_counter()
tot=t2-t1
minutes=tot/60
seconds=tot%60
print("\nExecution Time: %dm %ds" % (minutes,seconds))