-
Notifications
You must be signed in to change notification settings - Fork 3
/
bf_dpu_update.py
executable file
·1244 lines (1011 loc) · 45.6 KB
/
bf_dpu_update.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
import time
import re
import sys
import os
import json
import socket
import getpass
import subprocess
import stat
import datetime
from multiprocessing import Process
from error_num import *
class BF_DPU_Update(object):
module_resource = {
'BMC' : 'BMC_Firmware',
'CEC' : 'Bluefield_FW_ERoT',
'ATF' : 'DPU_ATF',
'UEFI' : 'DPU_UEFI',
'BSP' : 'DPU_BSP',
'NIC' : 'DPU_NIC',
'NODE' : 'DPU_NODE',
'OFED' : 'DPU_OFED',
'OS' : 'DPU_OS',
'SYS_IMAGE' : 'DPU_SYS_IMAGE',
'ARM_IMAGE' : 'golden_image_arm',
'NIC_IMAGE' : 'golden_image_nic',
'CONF_IMAGE': 'golden_image_config',
'BOARD' : 'DPU_BOARD'
}
def __init__(self, bmc_ip, bmc_port, username, password, fw_file_path, module, oem_fru, skip_same_version, debug=False, log_file=None, use_curl=True, bfb_update_protocol = None):
self.bmc_ip = self._parse_bmc_addr(bmc_ip)
self.bmc_port = bmc_port
self.username = username
self.password = password
self.fw_file_path = fw_file_path
self.module = module
self.oem_fru = oem_fru
self.skip_same_version = skip_same_version
self.debug = debug
self.log_file = log_file
self.protocol = 'https://'
self.redfish_root = '/redfish/v1'
self.process_flag = True
self._http_server_process = None
self._http_server_port_file = "/tmp/dpu_update_http_server_port_{}.txt".format(os.getpid())
self._local_http_server_port = None
self.use_curl = use_curl
self.http_accessor = self._get_http_accessor()
self.bfb_update_protocol = bfb_update_protocol
def _get_prot_ip_port(self):
port = '' if self.bmc_port is None else ':{}'.format(self.bmc_port)
return self.protocol + self._format_ip(self.bmc_ip) + port
def _get_url_base(self):
return self._get_prot_ip_port() + self.redfish_root
def _get_local_ip(self):
if self._is_ipv4:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.connect((self.bmc_ip, 0))
return s.getsockname()[0]
def _get_local_user(self):
return getpass.getuser()
def _get_http_accessor(self):
if self.use_curl:
from http_accessor_curl import HTTP_Accessor
else:
from http_accessor_requests import HTTP_Accessor
return HTTP_Accessor
def _http_get(self, url, headers=None, timeout=(60, 60)):
return self.http_accessor(url, 'GET', self.username, self.password, headers, timeout).access()
def _http_post(self, url, data, headers=None, timeout=(60, 60)):
return self.http_accessor(url, 'POST', self.username, self.password, headers, timeout).access(data)
def _http_patch(self, url, data, headers=None, timeout=(60, 60)):
return self.http_accessor(url, 'PATCH', self.username, self.password, headers, timeout).access(data)
def _http_put(self, url, data, headers=None, timeout=(60, 60)):
return self.http_accessor(url, 'PUT', self.username, self.password, headers, timeout).access(data)
def _upload_file(self, url, file_path, headers=None, timeout=(60, 60)):
return self.http_accessor(url, 'POST', self.username, self.password, headers, timeout).upload_file(file_path)
def _multi_part_push(self, url, param, headers=None, timeout=(60, 60)):
return self.http_accessor(url, 'POST', self.username, self.password, headers, timeout).multi_part_push(param)
def _get_truncated_data(self, data):
if len(data) > 1024:
return data[0:1024] + '... ... [Truncated]'
else:
return data
def _parse_bmc_addr(self, address):
self.raw_bmc_addr = address
# IPV4?
if self._is_valid_ipv4(address):
self._is_ipv4 = True
return address
# IPV6?
if self._is_valid_ipv6(address):
self._is_ipv4 = False
return address
# Host name(ipv4) ?
ipv4 = self._get_ipv4_from_name(address)
if ipv4 is not None:
self._is_ipv4 = True
return ipv4
# Host name(ipv6) ?
ipv6 = self._get_ipv6_from_name(address)
if ipv6 is not None:
self._is_ipv4 = False
return ipv6
raise Err_Exception(Err_Num.INVALID_BMC_ADDRESS, '{} is neither a valid IPV4/IPV6 nor a resolvable host name'.format(address))
@staticmethod
def _is_valid_ipv4(address):
try:
socket.inet_pton(socket.AF_INET, address)
return True
except:
return False
@staticmethod
def _is_valid_ipv6(address):
try:
socket.inet_pton(socket.AF_INET6, address)
return True
except:
return False
@staticmethod
def _get_ipv4_from_name(address):
try:
ipv4_list = socket.getaddrinfo(address, None, socket.AF_INET)
return ipv4_list[0][4][0]
except:
return None
@staticmethod
def _get_ipv6_from_name(address):
try:
ipv6_list = socket.getaddrinfo(address, None, socket.AF_INET6)
return ipv6_list[0][4][0]
except:
return None
def _format_ip(self, ip):
if self._is_ipv4:
return ip
else:
return '[{}]'.format(ip)
def _validate_fru_date_format(self, date_str):
try:
datetime.datetime.strptime(date_str, "%d/%m/%Y %H:%M:%S")
return True
except ValueError:
return False
def log(self, msg, resp):
data = '[======== ' + msg + ' ========]: ' + '\n'
data += '[Request Line]: ' + '\n'
data += str(resp.request.method) + ' ' + resp.url + '\n'
data += '[Request Headers]:' + '\n'
data += str(resp.request.headers) + '\n'
data += '[Request Body]:' + '\n'
data += self._get_truncated_data(str(resp.request.body)) + '\n'
data += "[Response status line]:" + '\n'
data += str(resp.status_code) + ' ' + resp.reason + '\n'
data += "[Response Headers]:" + '\n'
data += json.dumps(str(resp.headers), indent=4) + '\n'
data += "[Response Body]:" + '\n'
data += resp.text + '\n'
if self.debug:
print(data, end='')
if self.log_file is not None:
with open(self.log_file, 'a') as f:
f.write(data)
def _handle_status_code(self, response, acceptable_codes, err_handler=None):
if response.status_code in acceptable_codes:
return
try:
msg = response.json()['error']['message']
except:
try:
msg = response.json()['[email protected]'][0]['Message']
except:
msg = ''
# Raise exception for different cases
if response.status_code == 401:
if 'Account temporarily locked out' in msg:
raise Err_Exception(Err_Num.ACCOUNT_LOCKED, msg)
elif 'Invalid username or password' in msg:
raise Err_Exception(Err_Num.INVALID_USERNAME_OR_PASSWORD, msg)
if err_handler is not None:
err_handler(response)
raise Err_Exception(Err_Num.INVALID_STATUS_CODE, 'status code: {}; {}'.format(response.status_code, msg))
def get_ver_by_uri(self, uri):
url = self._get_prot_ip_port() + uri
response = self._http_get(url)
self.log('Get {} Firmware Version'.format(uri.split('/')[-1]), response)
self._handle_status_code(response, [200])
ver = ''
try:
ver = response.json()['Version']
except Exception as e:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract firmware version')
return ver
def get_ver(self, module):
return self.get_ver_by_uri(self._get_firmware_uri_by_resource(self.module_resource[module]))
def _extract_task_handle(self, response):
'''
{
"@odata.id": "/redfish/v1/TaskService/Tasks/6",
"@odata.type": "#Task.v1_4_3.Task",
"Id": "6",
"TaskState": "Running",
"TaskStatus": "OK"
}
'''
try:
return response.json()["@odata.id"]
except:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract task handle')
def get_simple_update_protocols(self):
url = self._get_url_base() + '/UpdateService'
response = self._http_get(url)
self.log('Get UpdateService Attribute', response)
self._handle_status_code(response, [200])
protocols = []
'''
{
...
"Actions": {
"#UpdateService.SimpleUpdate": {
"[email protected]": [
"SCP",
"HTTP",
"HTTPS"
],
},
}
...
}
'''
try:
protocols = response.json()['Actions']['#UpdateService.SimpleUpdate']['[email protected]']
except Exception as e:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract SimpleUpdate protocols')
return protocols
def get_push_uri(self):
url = self._get_url_base() + '/UpdateService'
response = self._http_get(url)
self.log('Get UpdateService Attribute', response)
self._handle_status_code(response, [200])
deprecated_uri = None
multi_part_uri = None
'''
{
...
"HttpPushUri": "/redfish/v1/UpdateService/update",
"MultipartHttpPushUri": "/redfish/v1/UpdateService/update-multipart",
...
}
'''
try:
deprecated_uri = response.json()['HttpPushUri']
except:
deprecated_uri = None
try:
multi_part_uri = response.json()['MultipartHttpPushUri']
except:
multi_part_uri = None
return (multi_part_uri, deprecated_uri)
@staticmethod
def _update_in_progress_err_handler(response):
try:
msg = response.json()['error']['message']
except:
msg = ''
if response.status_code == 400:
if 'An update is in progress' in msg:
raise Err_Exception(Err_Num.ANOTHER_UPDATE_IS_IN_PROGRESS, 'Please try to update the firmware later')
def simple_update(self):
protocols_supported_by_bmc = self.get_simple_update_protocols()
# Current script only support HTTP/SCP
protocols = []
if 'HTTP' in protocols_supported_by_bmc:
protocols.append('HTTP')
if 'SCP' in protocols_supported_by_bmc:
protocols.append('SCP')
# Select protocol to be used
protocol = None
if self.bfb_update_protocol is not None:
# Use the protocol provided by user
if self.bfb_update_protocol not in protocols:
raise Err_Exception(Err_Num.NOT_SUPPORT_SIMPLE_UPDATE_PROTOCOL, '{} is not in supported BFB update protocols {}'.format(self.bfb_update_protocol, protocols))
protocol = self.bfb_update_protocol
else:
# Perfer to use HTTP, if user did not provide a protocol
if 'HTTP' in protocols:
protocol = 'HTTP'
elif 'SCP' in protocols:
protocol = 'SCP'
if protocol is None:
raise Err_Exception(Err_Num.NOT_SUPPORT_SIMPLE_UPDATE_PROTOCOL, 'The current supported BFB update protocols are {}'.format(protocols))
return (protocol, self.simple_update_by_protocol(protocol))
def simple_update_by_protocol(self, protocol):
if protocol == 'HTTP':
return self.simple_update_by_http()
elif protocol == 'SCP':
return self.simple_update_by_scp()
def get_simple_update_targets(self):
if self.module == 'BIOS':
return ['redfish/v1/UpdateService/FirmwareInventory/DPU_OS']
elif self.module == 'CONFIG':
return ["redfish/v1/UpdateService/FirmwareInventory/golden_image_config"]
else:
raise Err_Exception(Err_Num.UNSUPPORTED_MODULE, "Only BIOS and CONFIG can be updated by SimpleUpdate")
def simple_update_impl(self, protocol, image_uri):
url = self._get_url_base() + '/UpdateService/Actions/UpdateService.SimpleUpdate'
headers = {
'Content-Type' : 'application/json'
}
data = {
'TransferProtocol' : protocol,
'ImageURI' : image_uri,
'Targets' : self.get_simple_update_targets(),
'Username' : self._get_local_user()
}
response = self._http_post(url, data=json.dumps(data), headers=headers)
self.log('Do Simple Update (Update BFB or Configurations ...)', response)
self._handle_status_code(response, [100, 200, 202], self._update_in_progress_err_handler)
return self._extract_task_handle(response)
def simple_update_by_scp(self):
self.confirm_ssh_key_with_bmc()
print("Start to do Simple Update (SCP)")
return self.simple_update_impl('SCP', self._format_ip(self._get_local_ip()) + '/' + os.path.abspath(self.fw_file_path))
def http_server(self):
debug = self.debug
from http.server import HTTPServer, SimpleHTTPRequestHandler
class _SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
if debug:
super().log_message(format, *args)
abs_dir = os.path.dirname(os.path.abspath(self.fw_file_path))
os.chdir(abs_dir)
if self._is_ipv4:
_HTTPServer = HTTPServer
else:
class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6
_HTTPServer = HTTPServerV6
httpd = _HTTPServer((self._get_local_ip(), 0), _SimpleHTTPRequestHandler)
self._local_http_server_port = httpd.server_address[1]
with open(self._http_server_port_file, 'w') as f:
f.write(str(self._local_http_server_port))
httpd.serve_forever()
def create_http_server_thread(self):
import threading
thread = threading.Thread(target=self.http_server, daemon=True)
thread.start()
time.sleep(2) # Wait thread start and set the port.
if self._local_http_server_port is None:
raise Err_Exception(Err_Num.FAILED_TO_START_HTTP_SERVER)
def create_http_server_process(self):
self._http_server_process = Process(target=self.http_server)
self._http_server_process.daemon = True
self._http_server_process.start()
time.sleep(2) # Wait process start and set the port.
port = None
with open(self._http_server_port_file, 'r') as f:
port = int(f.read())
if os.access(self._http_server_port_file, os.F_OK):
os.remove(self._http_server_port_file)
self._local_http_server_port = port
if not self._http_server_process.is_alive() or self._local_http_server_port is None:
raise Err_Exception(Err_Num.FAILED_TO_START_HTTP_SERVER)
def simple_update_by_http(self):
self.create_http_server_process()
print("Start to do Simple Update (HTTP)")
return self.simple_update_impl('HTTP', self._format_ip(self._get_local_ip()) + ':' + str(self._local_http_server_port) + '//' + os.path.basename(self.fw_file_path))
def update_bmc_fw_multipart(self, url):
update_params = {
"ForceUpdate": not self.skip_same_version
}
multi_part_param = {
'UpdateParameters' : {
'data' : json.dumps(update_params),
'is_file_path' : False,
'type' : None
},
'UpdateFile' : {
'data' : self.fw_file_path,
'is_file_path' : True,
'type' : 'application/octet-stream'
}
}
response = self._multi_part_push(url, multi_part_param)
self.log('Update Firmware', response)
self._handle_status_code(response, [100, 200, 202], self._update_in_progress_err_handler)
return self._extract_task_handle(response)
def update_bmc_fw_deprecated(self, url):
headers = {
'Content-Type' : 'application/octet-stream'
}
response = self._upload_file(url, self.fw_file_path, headers=headers)
self.log('Update Firmware', response)
self._handle_status_code(response, [100, 200, 202], self._update_in_progress_err_handler)
return self._extract_task_handle(response)
def update_bmc_fw(self):
multi_part_uri, deprecated_uri = self.get_push_uri()
if multi_part_uri is not None:
task_handle = self.update_bmc_fw_multipart(self._get_prot_ip_port() + multi_part_uri)
elif deprecated_uri is not None:
task_handle = self.update_bmc_fw_deprecated(self._get_prot_ip_port() + deprecated_uri)
else:
raise Err_Exception(Err_Num.PUSH_URI_NOT_FOUND)
return task_handle
def _get_task_status(self, task_handle):
url = self._get_prot_ip_port() + task_handle
response = self._http_get(url)
self.log('Get Task Satatus', response)
self._handle_status_code(response, [200])
'''
{
"PercentComplete": 0,
"StartTime": "2024-06-05T13:16:37+00:00",
"TaskMonitor": "/redfish/v1/TaskService/Tasks/11/Monitor",
"TaskState": "Running",
"TaskStatus": "OK"
}
'''
try:
percent = response.json()['PercentComplete']
state = response.json()['TaskState']
status = response.json()['TaskStatus']
message = response.json()['Messages']
return {'state': state, 'status': status, 'percent': percent, 'message': str(message)}
except:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract task status')
def reboot_bmc(self):
print("Restart BMC to make new firmware take effect")
url = self._get_url_base() + '/Managers/Bluefield_BMC/Actions/Manager.Reset'
headers = {
'Content-Type' : 'application/octet-stream'
}
data = {
'ResetType' : 'GracefulRestart'
}
response = self._http_post(url, data=json.dumps(data), headers=headers)
self.log('Reboot BMC', response)
self._handle_status_code(response, [200])
self._wait_for_bmc_on()
def reboot_cec(self):
print("Restart CEC to make new firmware take effect")
url = self._get_url_base() + '/Chassis/Bluefield_ERoT/Actions/Chassis.Reset'
headers = {
'Content-Type' : 'application/json'
}
data = {
'ResetType' : 'GracefulRestart'
}
response = self._http_post(url, data=json.dumps(data), headers=headers)
self.log('Reboot CEC', response)
self._handle_status_code(response, [200, 400])
if response.status_code == 400:
raise Err_Exception(Err_Num.NOT_SUPPORT_CEC_RESTART, 'Please use power cycle of the whole system instead')
self._wait_for_bmc_on()
def _wait_for_bmc_on(self):
timeout = 60 * 3 # Wait up to 3 minutes
start = int(time.time())
end = start + timeout
while True:
cur = int(time.time())
if cur > end:
self._print_process(100)
break
time.sleep(4)
try:
self.get_ver('BMC')
self.get_ver('CEC')
self._print_process(100)
break
except Exception as e:
self._print_process(100 * (cur - start) / timeout)
print()
def factory_reset_bmc(self):
print("Factory reset BMC configuration")
url = self._get_url_base() + '/Managers/Bluefield_BMC/Actions/Manager.ResetToDefaults'
headers = {
'Content-Type' : 'application/json'
}
data = {
'ResetToDefaultsType' : 'ResetAll'
}
response = self._http_post(url, data=json.dumps(data), headers=headers)
self.log('Factory Reset BMC', response)
self._handle_status_code(response, [200])
self._wait_for_bmc_on()
def _print_process(self, percent):
print('\r', end='')
flag = '|' if self.process_flag else '-'
self.process_flag = not self.process_flag
print('Process%s: %3d%%:'%(flag, percent), '░' * (int(percent) // 2), end='')
def _sleep_with_process_with_percent(self, sec, start_percent=0, end_percent=100):
for i in range(1, sec+1):
time.sleep(1)
self._print_process(start_percent + ((i * (end_percent - start_percent)) // sec))
def _sleep_with_process(self, sec):
self._sleep_with_process_with_percent(sec)
print()
def _extract_ver_from_fw_file(self, pattern):
file_name = os.path.basename(self.fw_file_path)
match = re.search(pattern, file_name)
substring = match.group(0)
return substring
def extract_cec_ver_from_fw_file(self):
return self._extract_ver_from_fw_file(r'\d\d.\d\d.\d\d\d\d.\d\d\d\d')
def extract_bmc_ver_from_fw_file(self):
return self._extract_ver_from_fw_file(r'\d\d.\d\d-\d')
def extract_atf_uefi_ver_from_fw_file(self):
command = 'strings {} | grep -m 1 "(\(release\|debug\))"'.format(self.fw_file_path)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
raise Err_Exception(Err_Num.FAILED_TO_GET_VER_FROM_FILE, 'Command "{}" failed with return code {}'.format(command, process.returncode))
return str(out.decode()).strip()
def is_fw_file_for_bmc(self):
command = 'strings {} | grep -i apfw'.format(self.fw_file_path)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
return False
return True
def is_fw_file_for_cec(self):
command = 'strings {} | grep -i ecfw'.format(self.fw_file_path)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
return False
return True
def is_fw_file_for_atf_uefi(self):
try:
self.extract_atf_uefi_ver_from_fw_file()
except:
return False
return True
def is_fw_file_for_conf(self):
if not self.is_fw_file_for_atf_uefi():
return False
command = 'strings {} | grep -i toutiao'.format(self.fw_file_path)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
return False
return True
# return True: task completed successfully
# return False: task cancelled for skip_same_version
def _wait_task(self, task_handle, max_second=15*60, check_step=10, err_handler=None):
# Check the task status within a loop
for i in range(1, max_second//check_step + 1):
task_state = self._get_task_status(task_handle)
if task_state['state'] != "Running":
break
self._print_process(task_state['percent'])
time.sleep(check_step)
# Check the task is completed successfully
if task_state['state'] == 'Completed' and task_state['status'] == 'OK' and task_state['percent'] == 100:
self._print_process(100)
print()
elif task_state['state'] == 'Running':
raise Err_Exception(Err_Num.TASK_TIMEOUT, "The task {} is timeout".format(task_handle))
else:
if err_handler is not None:
err_handler(task_state)
if 'Component image is identical' in task_state['message']:
return False
elif 'Wait for background copy operation' in task_state['message']:
raise Err_Exception(Err_Num.BMC_BACKGROUND_BUSY, 'Please try to update the firmware later')
raise Err_Exception(Err_Num.TASK_FAILED, task_state['message'])
return True
def validate_args(self, items):
if 'UserName' in items:
if self.username is None:
raise Err_Exception(Err_Num.USERNAME_NOT_GIVEN)
if 'Password' in items:
if self.password is None:
raise Err_Exception(Err_Num.PASSWORD_NOT_GIVEN)
if 'BmcIP' in items:
if self.bmc_ip is None:
raise Err_Exception(Err_Num.BMC_IP_NOT_GIVEN)
if 'Module' in items:
if self.module is None:
raise Err_Exception(Err_Num.MODULE_NOT_GIVEN)
if 'FwFile' in items:
if self.fw_file_path is None:
raise Err_Exception(Err_Num.FW_FILE_NOT_GIVEN)
if not os.access(self.fw_file_path, os.R_OK):
raise Err_Exception(Err_Num.FILE_NOT_ACCESSIBLE, 'Firmware file: {}'.format(self.fw_file_path))
if 'FRU' in items:
if not self.oem_fru:
raise Err_Exception(Err_Num.FRU_NOT_GIVEN)
# Always validate log_file if provided
if self.log_file is not None:
accessible_file = os.access(self.log_file, os.W_OK)
accessible_dir = os.access(os.path.abspath(os.path.dirname(self.log_file)), os.W_OK)
if not accessible_file and not accessible_dir:
raise Err_Exception(Err_Num.FILE_NOT_ACCESSIBLE, 'Log file: {}'.format(self.log_file))
def validate_arg_for_update(self):
self.validate_args(['UserName', 'Password', 'BmcIP', 'Module', 'FwFile'])
def validate_arg_for_fru(self):
self.validate_args(['UserName', 'Password', 'BmcIP', 'Module', 'FRU'])
def validate_arg_for_show_versions(self):
self.validate_args(['UserName', 'Password', 'BmcIP'])
def validate_arg_for_reset_config(self):
self.validate_args(['UserName', 'Password', 'BmcIP', 'Module'])
def is_bmc_background_copy_in_progress(self):
url = self._get_url_base() + '/Chassis/Bluefield_ERoT'
response = self._http_get(url)
self.log('Get ERoT status', response)
self._handle_status_code(response, [200])
'''
{
...
"Oem": {
"Nvidia": {
"@odata.type": "#NvidiaChassis.v1_0_0.NvidiaChassis",
"AutomaticBackgroundCopyEnabled": true,
"BackgroundCopyStatus": "Completed",
"InbandUpdatePolicyEnabled": true
}
},
...
}
'''
status = ''
try:
status = response.json()['Oem']['Nvidia']['BackgroundCopyStatus']
except Exception as e:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract BackgroundCopyStatus')
if status != 'Completed':
return True
else:
return False
def update_bmc_or_cec(self, is_bmc):
self.validate_arg_for_update()
# Check firmare file is for BMC/CEC
correct_file = self.is_fw_file_for_bmc() if is_bmc else self.is_fw_file_for_cec()
if not correct_file:
raise Err_Exception(Err_Num.FW_FILE_NOT_MATCH_MODULE)
old_ver = self.get_ver('BMC') if is_bmc else self.get_ver('CEC')
if old_ver == '':
raise Err_Exception(Err_Num.EMPTY_FW_VER, 'Get empty {} version'.format('BMC' if is_bmc else 'CEC'))
if self.is_bmc_background_copy_in_progress():
raise Err_Exception(Err_Num.BMC_BACKGROUND_BUSY, 'Please try to update the firmware later')
# Start firmware update task
print("Start to upload firmware")
task_handle = self.update_bmc_fw()
ret = self._wait_task(task_handle, max_second=(20*60 if is_bmc else 4*60), check_step=(10 if is_bmc else 2))
if not ret:
print("Skip updating the same version: {}".format(old_ver))
return
# Reboot bmc/cec
self.reboot_bmc() if is_bmc else self.reboot_cec()
new_ver = self.get_ver('BMC') if is_bmc else self.get_ver('CEC')
print('OLD {} Firmware Version: \n\t{}'.format(('BMC' if is_bmc else 'CEC'), old_ver))
print('New {} Firmware Version: \n\t{}'.format(('BMC' if is_bmc else 'CEC'), new_ver))
def is_rshim_enabled_on_bmc(self):
url = self._get_url_base() + '/Managers/Bluefield_BMC/Oem/Nvidia'
headers = {
'Content-Type' : 'application/json'
}
response = self._http_get(url, headers=headers)
self.log('Get rshim enable state', response)
self._handle_status_code(response, [200])
try:
return response.json()['BmcRShim']['BmcRShimEnabled']
except:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract BmcRShimEnabled')
def enable_rshim_on_bmc(self, enable):
url = self._get_url_base() + '/Managers/Bluefield_BMC/Oem/Nvidia'
headers = {
'Content-Type' : 'application/json'
}
data = {
"BmcRShim": { "BmcRShimEnabled": enable }
}
response = self._http_patch(url, json.dumps(data), headers=headers)
self.log('{} rshim on BMC'.format("Enable" if enable else "Disable"), response)
self._handle_status_code(response, [200])
def try_enable_rshim_on_bmc(self):
if self.is_rshim_enabled_on_bmc():
return True
print("Try to enable rshim on BMC")
self.enable_rshim_on_bmc(True)
self._sleep_with_process_with_percent(10, 0, 30)
if self.is_rshim_enabled_on_bmc():
self._sleep_with_process_with_percent(1, 30, 100)
print()
return True
# Try again if failed
self.enable_rshim_on_bmc(False)
self._sleep_with_process_with_percent(10, 30, 60)
self.enable_rshim_on_bmc(True)
self._sleep_with_process_with_percent(10, 60, 90)
if self.is_rshim_enabled_on_bmc():
self._sleep_with_process_with_percent(1, 90, 100)
print()
return True
print()
return False
def _wait_for_bios_ready(self):
print('Wait for BIOS ready')
timeout = 60 * 3 # Wait up to 3 minutes
start = int(time.time())
end = start + timeout
while True:
cur = int(time.time())
if cur > end:
self._print_process(100)
break
ver = self.get_ver('ATF')
if ver != '':
self._print_process(100)
break
else:
self._print_process(100 * (cur - start) / timeout)
time.sleep(4)
print()
def get_local_user_ssh_pub_key(self):
command = 'ssh-keyscan {}'.format(self._get_local_ip())
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
raise Err_Exception(Err_Num.FAILED_TO_GET_LOCAL_KEY, 'Command "{}" failed with return code {}'.format(command, process.returncode))
'''
127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLxvoG8lUk0CyiQ2Jk9IlTlrESlRtLzyIhQnPsXe5//YWl5nHa6oTSbkIlwk090tchoUi9nwFtTDE5Lihs1qJEc=
127.0.0.1 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPzhBRfJL2pZ6LNikFnlBg7iqYXh7BDbQpfg9f1R7nch
'''
try:
key_list = out.decode().split('\n')
ret_list = []
for key in key_list:
if key.strip() == '':
continue
ret_list.append(' '.join(key.split(' ')[1:]))
if len(ret_list) == 0:
raise Err_Exception(Err_Num.FAILED_TO_GET_LOCAL_KEY)
return ret_list
except:
raise Err_Exception(Err_Num.FAILED_TO_GET_LOCAL_KEY, 'There may be no ssh-key locally (for user {}). Please run ssh-keygen firstly'.format(self._get_local_user()))
def exchange_ssh_key_with_bmc(self, local_key):
url = self._get_url_base() + "/UpdateService/Actions/Oem/NvidiaUpdateService.PublicKeyExchange"
headers = {
'Content-Type' : 'application/json'
}
msg = {
"RemoteServerIP" : self._get_local_ip(),
"RemoteServerKeyString" : local_key,
}
response = self._http_post(url, data=json.dumps(msg), headers=headers)
self.log('Exchange SSH key with BMC', response)
self._handle_status_code(response, [200])
'''
{
"@Message.ExtendedInfo":
[
{
"@odata.type": "#Message.v1_1_1.Message",
"Message": "Please add the following public
key info to ~/.ssh/authorized_keys on the
remote server",
"MessageArgs": [
"<type> <bmc_public_key> root@dpu-bmc"
]
},
{
....
}
]
}
'''
try:
return response.json()['@Message.ExtendedInfo'][0]['MessageArgs'][0]
except:
raise Err_Exception(Err_Num.BAD_RESPONSE_FORMAT, 'Failed to extract BMC SSH key')
def is_bmc_key_in_local_authorized_keys(self, bmc_key):
file_path = os.path.expanduser("~") + '/.ssh/authorized_keys'
process = subprocess.Popen('grep "{}" {}'.format(bmc_key, file_path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
return False
return True
def set_bmc_key_into_local_authorized_keys(self, bmc_key):
file_path = os.path.expanduser("~") + '/.ssh/authorized_keys'
# Check and set write permission for ~/.ssh/authorized_keys
old_permission = None
if not os.access(file_path, os.W_OK):
old_permission = os.stat(file_path).st_mode
os.chmod(file_path, old_permission | stat.S_IWUSR)
# Append the bmc key into authorized_keys
with open(file_path, 'a') as f:
f.write(bmc_key + '\n')
# Recover the permission
if old_permission is not None:
os.chmod(file_path, old_permission)
old_permission = os.stat(file_path).st_mode
def confirm_ssh_key_with_bmc(self):
local_keys = self.get_local_user_ssh_pub_key()
for local_key in local_keys:
bmc_key = self.exchange_ssh_key_with_bmc(local_key)
if not self.is_bmc_key_in_local_authorized_keys(bmc_key):
self.set_bmc_key_into_local_authorized_keys(bmc_key)
def update_bios(self):
self.validate_arg_for_update()
if not self.is_fw_file_for_atf_uefi():
raise Err_Exception(Err_Num.FW_FILE_NOT_MATCH_MODULE)
# Skip the same firmware version, if need