Skip to content

Commit

Permalink
[DellEMC]: EEPROM platform API Python3 compliance changes (#5960)
Browse files Browse the repository at this point in the history
Make EEPROM platform APIs Python3 compliant in DellEMC platforms by handling bytearray type returned by read_eeprom and read_eeprom_bytes methods.
  • Loading branch information
ArunSaravananBalachandran authored Nov 25, 2020
1 parent 5038730 commit dc15fbc
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
try:
import os.path
from sonic_eeprom import eeprom_tlvinfo
import binascii
except ImportError as e:
raise ImportError(str(e) + "- required module not found")

Expand Down Expand Up @@ -40,7 +39,7 @@ def __init__(self):
if not self.is_valid_tlvinfo_header(eeprom):
return

total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | eeprom[10]
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length

Expand All @@ -49,16 +48,16 @@ def __init__(self):
break

tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % tlv[0]

name, value = self.decoder(None, tlv)

self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break

tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2

def serial_number_str(self):
"""
Expand All @@ -68,7 +67,7 @@ def serial_number_str(self):
self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')

def base_mac_addr(self, e):
"""
Expand All @@ -79,7 +78,7 @@ def base_mac_addr(self, e):
if not is_valid or t[1] != 6:
return super(eeprom_tlvinfo.TlvInfoDecoder, self).switchaddrstr(t)

return ":".join([binascii.b2a_hex(T) for T in t[2]])
return ":".join(["{:02x}".format(T) for T in t[2]]).upper()

def modelstr(self):
"""
Expand All @@ -90,7 +89,7 @@ def modelstr(self):
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def part_number_str(self):
"""
Expand All @@ -101,7 +100,7 @@ def part_number_str(self):
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def serial_str(self):
"""
Expand All @@ -112,7 +111,7 @@ def serial_str(self):
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def revision_str(self):
"""
Expand All @@ -123,7 +122,7 @@ def revision_str(self):
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def system_eeprom_info(self):
"""
Expand All @@ -132,5 +131,3 @@ def system_eeprom_info(self):
found in the system EEPROM.
"""
return self.eeprom_tlv_dict


Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

try:
import binascii
import os
import redis
import struct
from collections import OrderedDict
Expand Down Expand Up @@ -108,7 +107,7 @@ def _load_system_eeprom(self):
self.serial = 'NA'
return

total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | (eeprom[10])
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length

Expand All @@ -117,21 +116,21 @@ def _load_system_eeprom(self):
break

tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % (tlv[0])

if ord(tlv[0]) == self._TLV_CODE_VENDOR_EXT:
value = str((ord(tlv[2]) << 24) | (ord(tlv[3]) << 16) |
(ord(tlv[4]) << 8) | ord(tlv[5]))
value += str(tlv[6:6 + ord(tlv[1])])
if tlv[0] == self._TLV_CODE_VENDOR_EXT:
value = str((tlv[2] << 24) | (tlv[3] << 16) |
(tlv[4] << 8) | tlv[5])
value += tlv[6:6 + tlv[1]].decode('ascii')
else:
name, value = self.decoder(None, tlv)

self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break

tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2

self.base_mac = self.eeprom_tlv_dict.get(
"0x%X" % (self._TLV_CODE_MAC_BASE), 'NA')
Expand Down Expand Up @@ -191,7 +190,7 @@ def _load_device_eeprom(self):
else:
self.fan_type = 'NA'

def _get_eeprom_field(self, field_name):
def _get_eeprom_field(self, field_name, decode=True):
"""
For a field name specified in the EEPROM format, returns the
presence of the field and the value for the same.
Expand All @@ -200,7 +199,10 @@ def _get_eeprom_field(self, field_name):
for field in self.format:
field_end = field_start + field[2]
if field[0] == field_name:
return (True, self.eeprom_data[field_start:field_end].decode('utf-8'))
if decode:
return (True, self.eeprom_data[field_start:field_end].decode('ascii'))
else:
return (True, self.eeprom_data[field_start:field_end])
field_start = field_end

return (False, None)
Expand Down Expand Up @@ -259,7 +261,7 @@ class EepromS6000(EepromDecoder):
_EEPROM_MAX_LEN = 128

_BLK_HDR_LEN = 6
_BLK_HDR_MAGIC = '\x3a\x29'
_BLK_HDR_MAGIC = b'\x3a\x29'
_BLK_HDR_REVID = 1

_BLK_CODE_MFG = 0x20
Expand Down Expand Up @@ -297,10 +299,10 @@ def _is_valid_block_checksum(self, e):
def _is_valid_block(self, e, blk_code):
return (e[:2] == self._BLK_HDR_MAGIC
and struct.unpack('<H', e[2:4])[0] == self._BLK_INFO[blk_code]["size"]
and ord(e[4]) == blk_code
and ord(e[5]) == self._BLK_HDR_REVID)
and e[4] == blk_code
and e[5] == self._BLK_HDR_REVID)

def _get_eeprom_field(self, e, blk_code, field_name):
def _get_eeprom_field(self, e, blk_code, field_name, decode=True):
"""
For a field name specified in the EEPROM format, returns the
presence of the field and the value for the same.
Expand All @@ -314,7 +316,10 @@ def _get_eeprom_field(self, e, blk_code, field_name):
for field in self._BLK_INFO[blk_code]["format"]:
field_end = field_start + field[1]
if field[0] == field_name:
return (True, e[field_start:field_end])
if decode:
return (True, e[field_start:field_end].decode('ascii'))
else:
return (True, e[field_start:field_end])
field_start = field_end

return (False, None)
Expand All @@ -339,9 +344,9 @@ def decode_eeprom(self, e):
elif f[0] == "Card ID":
data = hex(struct.unpack('<I', e[offset:offset+f[1]])[0])
elif f[0] == "Base MAC address":
data = ":".join([binascii.b2a_hex(T) for T in e[offset:offset+f[1]]]).upper()
data = ":".join(["{:02x}".format(T) for T in e[offset:offset+f[1]]]).upper()
else:
data = e[offset:offset+f[1]]
data = e[offset:offset+f[1]].decode('ascii')
print("{:<20s} {:>3d} {:<s}".format(f[0], f[1], data))
offset += f[1]

Expand Down Expand Up @@ -406,9 +411,9 @@ def update_eeprom_db(self, e):
elif f[0] == "Card ID":
data = hex(struct.unpack('<I', e[offset:offset+f[1]])[0])
elif f[0] == "Base MAC address":
data = ":".join([binascii.b2a_hex(T) for T in e[offset:offset+f[1]]]).upper()
data = ":".join(["{:02x}".format(T) for T in e[offset:offset+f[1]]]).upper()
else:
data = e[offset:offset+f[1]]
data = e[offset:offset+f[1]].decode('ascii')
client.hset('EEPROM_INFO|{}'.format(f[0]), 'Value', data)
offset += f[1]

Expand All @@ -424,10 +429,10 @@ def get_base_mac(self):
"""
Returns the base MAC address found in the system EEPROM.
"""
(valid, data) = self._get_eeprom_field(self.eeprom_data,
self._BLK_CODE_MAC, "Base MAC address")
(valid, data) = self._get_eeprom_field(self.eeprom_data, self._BLK_CODE_MAC,
"Base MAC address", False)
if valid:
return ":".join([binascii.b2a_hex(T) for T in data]).upper()
return ":".join(["{:02x}".format(T) for T in data]).upper()
else:
return 'NA'

Expand Down Expand Up @@ -479,9 +484,9 @@ def mgmtaddrstr(self, e):
"""
Returns the base MAC address.
"""
(valid, data) = self._get_eeprom_field(e, self._BLK_CODE_MAC, "Base MAC address")
(valid, data) = self._get_eeprom_field(e, self._BLK_CODE_MAC, "Base MAC address", False)
if valid:
return ":".join([binascii.b2a_hex(T) for T in data]).upper()
return ":".join(["{:02x}".format(T) for T in data]).upper()
else:
return 'NA'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

try:
from sonic_eeprom import eeprom_tlvinfo
import binascii
except ImportError as e:
raise ImportError(str(e) + "- required module not found")

Expand All @@ -30,7 +29,7 @@ def __init__(self, i2c_line=0, iom_eeprom=False):

try:
if self.is_module:
self.write_eeprom("\x00\x00")
self.write_eeprom(b"\x00\x00")
self.eeprom_data = self.read_eeprom_bytes(256)
else:
self.eeprom_data = self.read_eeprom()
Expand All @@ -48,7 +47,7 @@ def __init__(self, i2c_line=0, iom_eeprom=False):
if not self.is_valid_tlvinfo_header(eeprom):
return

total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | eeprom[10]
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length

Expand All @@ -57,38 +56,37 @@ def __init__(self, i2c_line=0, iom_eeprom=False):
break

tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % tlv[0]

if ord(tlv[0]) == self._TLV_CODE_VENDOR_EXT:
value = str((ord(tlv[2]) << 24) | (ord(tlv[3]) << 16) |
(ord(tlv[4]) << 8) | ord(tlv[5]))
value += str(tlv[6:6 + ord(tlv[1])])
if tlv[0] == self._TLV_CODE_VENDOR_EXT:
value = str((tlv[2] << 24) | (tlv[3] << 16) |
(tlv[4] << 8) | tlv[5])
value += tlv[6:6 + tlv[1]].decode('ascii')
else:
name, value = self.decoder(None, tlv)

self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break

tlv_index += ord(eeprom[tlv_index+1]) + 2

tlv_index += eeprom[tlv_index+1] + 2

def serial_number_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def base_mac_addr(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_MAC_BASE)
if not is_valid or results[1] != 6:
return super(TlvInfoDecoder, self).switchaddrstr(e)

return ":".join([binascii.b2a_hex(T) for T in results[2]])
return ":".join(["{:02x}".format(T) for T in results[2]]).upper()

def modelstr(self):
if self.is_module:
Expand All @@ -100,31 +98,31 @@ def modelstr(self):
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def part_number_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_PART_NUMBER)
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def serial_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_SERVICE_TAG)
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def revision_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_DEVICE_VERSION)
if not is_valid:
return "N/A"

return results[2]
return results[2].decode('ascii')

def system_eeprom_info(self):
"""
Expand Down
Loading

0 comments on commit dc15fbc

Please sign in to comment.