1818
1919try :
2020 import binascii
21- import os
2221 import redis
2322 import struct
2423 from collections import OrderedDict
@@ -108,7 +107,7 @@ def _load_system_eeprom(self):
108107 self .serial = 'NA'
109108 return
110109
111- total_length = (ord ( eeprom [9 ]) << 8 ) | ord (eeprom [10 ])
110+ total_length = (eeprom [9 ] << 8 ) | (eeprom [10 ])
112111 tlv_index = self ._TLV_INFO_HDR_LEN
113112 tlv_end = self ._TLV_INFO_HDR_LEN + total_length
114113
@@ -117,21 +116,21 @@ def _load_system_eeprom(self):
117116 break
118117
119118 tlv = eeprom [tlv_index :tlv_index + 2
120- + ord ( eeprom [tlv_index + 1 ]) ]
121- code = "0x%02X" % (ord ( tlv [0 ]) )
119+ + eeprom [tlv_index + 1 ]]
120+ code = "0x%02X" % (tlv [0 ])
122121
123- if ord ( tlv [0 ]) == self ._TLV_CODE_VENDOR_EXT :
124- value = str ((ord ( tlv [2 ]) << 24 ) | (ord ( tlv [3 ]) << 16 ) |
125- (ord ( tlv [4 ]) << 8 ) | ord ( tlv [5 ]) )
126- value += str ( tlv [6 :6 + ord ( tlv [1 ])] )
122+ if tlv [0 ] == self ._TLV_CODE_VENDOR_EXT :
123+ value = str ((tlv [2 ] << 24 ) | (tlv [3 ] << 16 ) |
124+ (tlv [4 ] << 8 ) | tlv [5 ])
125+ value += tlv [6 :6 + tlv [1 ]]. decode ( 'ascii' )
127126 else :
128127 name , value = self .decoder (None , tlv )
129128
130129 self .eeprom_tlv_dict [code ] = value
131- if ord ( eeprom [tlv_index ]) == self ._TLV_CODE_CRC_32 :
130+ if eeprom [tlv_index ] == self ._TLV_CODE_CRC_32 :
132131 break
133132
134- tlv_index += ord ( eeprom [tlv_index + 1 ]) + 2
133+ tlv_index += eeprom [tlv_index + 1 ] + 2
135134
136135 self .base_mac = self .eeprom_tlv_dict .get (
137136 "0x%X" % (self ._TLV_CODE_MAC_BASE ), 'NA' )
@@ -191,7 +190,7 @@ def _load_device_eeprom(self):
191190 else :
192191 self .fan_type = 'NA'
193192
194- def _get_eeprom_field (self , field_name ):
193+ def _get_eeprom_field (self , field_name , decode = True ):
195194 """
196195 For a field name specified in the EEPROM format, returns the
197196 presence of the field and the value for the same.
@@ -200,7 +199,10 @@ def _get_eeprom_field(self, field_name):
200199 for field in self .format :
201200 field_end = field_start + field [2 ]
202201 if field [0 ] == field_name :
203- return (True , self .eeprom_data [field_start :field_end ].decode ('utf-8' ))
202+ if decode :
203+ return (True , self .eeprom_data [field_start :field_end ].decode ('ascii' ))
204+ else :
205+ return (True , self .eeprom_data [field_start :field_end ])
204206 field_start = field_end
205207
206208 return (False , None )
@@ -259,7 +261,7 @@ class EepromS6000(EepromDecoder):
259261 _EEPROM_MAX_LEN = 128
260262
261263 _BLK_HDR_LEN = 6
262- _BLK_HDR_MAGIC = '\x3a \x29 '
264+ _BLK_HDR_MAGIC = b '\x3a \x29 '
263265 _BLK_HDR_REVID = 1
264266
265267 _BLK_CODE_MFG = 0x20
@@ -297,10 +299,10 @@ def _is_valid_block_checksum(self, e):
297299 def _is_valid_block (self , e , blk_code ):
298300 return (e [:2 ] == self ._BLK_HDR_MAGIC
299301 and struct .unpack ('<H' , e [2 :4 ])[0 ] == self ._BLK_INFO [blk_code ]["size" ]
300- and ord ( e [4 ]) == blk_code
301- and ord ( e [5 ]) == self ._BLK_HDR_REVID )
302+ and e [4 ] == blk_code
303+ and e [5 ] == self ._BLK_HDR_REVID )
302304
303- def _get_eeprom_field (self , e , blk_code , field_name ):
305+ def _get_eeprom_field (self , e , blk_code , field_name , decode = True ):
304306 """
305307 For a field name specified in the EEPROM format, returns the
306308 presence of the field and the value for the same.
@@ -314,7 +316,10 @@ def _get_eeprom_field(self, e, blk_code, field_name):
314316 for field in self ._BLK_INFO [blk_code ]["format" ]:
315317 field_end = field_start + field [1 ]
316318 if field [0 ] == field_name :
317- return (True , e [field_start :field_end ])
319+ if decode :
320+ return (True , e [field_start :field_end ].decode ('ascii' ))
321+ else :
322+ return (True , e [field_start :field_end ])
318323 field_start = field_end
319324
320325 return (False , None )
@@ -339,9 +344,9 @@ def decode_eeprom(self, e):
339344 elif f [0 ] == "Card ID" :
340345 data = hex (struct .unpack ('<I' , e [offset :offset + f [1 ]])[0 ])
341346 elif f [0 ] == "Base MAC address" :
342- data = ":" .join ([binascii . b2a_hex (T ) for T in e [offset :offset + f [1 ]]]).upper ()
347+ data = ":" .join (["{:02x}" . format (T ) for T in e [offset :offset + f [1 ]]]).upper ()
343348 else :
344- data = e [offset :offset + f [1 ]]
349+ data = e [offset :offset + f [1 ]]. decode ( 'ascii' )
345350 print ("{:<20s} {:>3d} {:<s}" .format (f [0 ], f [1 ], data ))
346351 offset += f [1 ]
347352
@@ -406,9 +411,9 @@ def update_eeprom_db(self, e):
406411 elif f [0 ] == "Card ID" :
407412 data = hex (struct .unpack ('<I' , e [offset :offset + f [1 ]])[0 ])
408413 elif f [0 ] == "Base MAC address" :
409- data = ":" .join ([binascii . b2a_hex (T ) for T in e [offset :offset + f [1 ]]]).upper ()
414+ data = ":" .join (["{:02x}" . format (T ) for T in e [offset :offset + f [1 ]]]).upper ()
410415 else :
411- data = e [offset :offset + f [1 ]]
416+ data = e [offset :offset + f [1 ]]. decode ( 'ascii' )
412417 client .hset ('EEPROM_INFO|{}' .format (f [0 ]), 'Value' , data )
413418 offset += f [1 ]
414419
@@ -424,10 +429,10 @@ def get_base_mac(self):
424429 """
425430 Returns the base MAC address found in the system EEPROM.
426431 """
427- (valid , data ) = self ._get_eeprom_field (self .eeprom_data ,
428- self . _BLK_CODE_MAC , "Base MAC address" )
432+ (valid , data ) = self ._get_eeprom_field (self .eeprom_data , self . _BLK_CODE_MAC ,
433+ "Base MAC address" , False )
429434 if valid :
430- return ":" .join ([binascii . b2a_hex (T ) for T in data ]).upper ()
435+ return ":" .join (["{:02x}" . format (T ) for T in data ]).upper ()
431436 else :
432437 return 'NA'
433438
@@ -479,9 +484,9 @@ def mgmtaddrstr(self, e):
479484 """
480485 Returns the base MAC address.
481486 """
482- (valid , data ) = self ._get_eeprom_field (e , self ._BLK_CODE_MAC , "Base MAC address" )
487+ (valid , data ) = self ._get_eeprom_field (e , self ._BLK_CODE_MAC , "Base MAC address" , False )
483488 if valid :
484- return ":" .join ([binascii . b2a_hex (T ) for T in data ]).upper ()
489+ return ":" .join (["{:02x}" . format (T ) for T in data ]).upper ()
485490 else :
486491 return 'NA'
487492
0 commit comments