sonic-buildimage/platform/mellanox/mlnx-platform-api/sonic_platform/eeprom.py

177 lines
6.3 KiB
Python
Raw Normal View History

#############################################################################
# Mellanox
#
# Module contains an implementation of SONiC Platform Base API and
# provides the eeprom information which are available in the platform
#
#############################################################################
import os
import time
import subprocess
[sonic-utilities] Update submodule; Build and install as a Python 3 wheel (#5926) Submodule updates include the following commits: * src/sonic-utilities 9dc58ea...f9eb739 (18): > Remove unnecessary calls to str.encode() now that the package is Python 3; Fix deprecation warning (#1260) > [generate_dump] Ignoring file/directory not found Errors (#1201) > Fixed porstat rate and util issues (#1140) > fix error: interface counters is mismatch after warm-reboot (#1099) > Remove unnecessary calls to str.decode() now that the package is Python 3 (#1255) > [acl-loader] Make list sorting compliant with Python 3 (#1257) > Replace hard-coded fast-reboot with variable. And some typo corrections (#1254) > [configlet][portconfig] Remove calls to dict.has_key() which is not available in Python 3 (#1247) > Remove unnecessary conversions to list() and calls to dict.keys() (#1243) > Clean up LGTM alerts (#1239) > Add 'requests' as install dependency in setup.py (#1240) > Convert to Python 3 (#1128) > Fix mock SonicV2Connector in python3: use decode_responses mode so caller code will be the same as python2 (#1238) > [tests] Do not trim from PATH if we did not append to it; Clean up/fix shebangs in scripts (#1233) > Updates to bgp config and show commands with BGP_INTERNAL_NEIGHBOR table (#1224) > [cli]: NAT show commands newline issue after migrated to Python3 (#1204) > [doc]: Update Command-Reference.md (#1231) > Added 'import sys' in feature.py file (#1232) * src/sonic-py-swsssdk 9d9f0c6...1664be9 (2): > Fix: no need to decode() after redis client scan, so it will work for both python2 and python3 (#96) > FieldValueMap `contains`(`in`) will also work when migrated to libswsscommon(C++ with SWIG wrapper) (#94) - Also fix Python 3-related issues: - Use integer (floor) division in config_samples.py (sonic-config-engine) - Replace print statement with print function in eeprom.py plugin for x86_64-kvm_x86_64-r0 platform - Update all platform plugins to be compatible with both Python 2 and Python 3 - Remove shebangs from plugins files which are not intended to be executable - Replace tabs with spaces in Python plugin files and fix alignment, because Python 3 is more strict - Remove trailing whitespace from plugins files
2020-11-25 12:28:36 -06:00
from sonic_py_common.logger import Logger
from sonic_py_common.device_info import get_platform, get_path_to_platform_dir
try:
from sonic_platform_base.sonic_eeprom import eeprom_tlvinfo
except ImportError as e:
raise ImportError (str(e) + "- required module not found")
from .utils import default_return
logger = Logger()
#
# this is mlnx-specific
# should this be moved to chass.py or here, which better?
#
EEPROM_SYMLINK = "/var/run/hw-management/eeprom/vpd_info"
platform_name = get_platform()
if 'simx' in platform_name:
platform_path = get_path_to_platform_dir()
if not os.path.exists(EEPROM_SYMLINK):
if not os.path.exists(os.path.dirname(EEPROM_SYMLINK)):
os.makedirs(os.path.dirname(EEPROM_SYMLINK))
subprocess.check_call(['/usr/bin/xxd', '-r', '-p', 'syseeprom.hex', EEPROM_SYMLINK], cwd=platform_path)
class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
RETRIES = 3
def __init__(self):
for attempt in range(self.RETRIES):
if not os.path.islink(EEPROM_SYMLINK):
time.sleep(1)
else:
break
if not os.path.exists(EEPROM_SYMLINK):
logger.log_error("Nowhere to read syseeprom from! No symlink or cache file found")
raise RuntimeError("No syseeprom symlink or cache file found")
self.eeprom_path = EEPROM_SYMLINK
super(Eeprom, self).__init__(self.eeprom_path, 0, '', True)
self._eeprom_info_dict = None
@default_return(return_value='Undefined.')
def get_base_mac(self):
"""
Retrieves the base MAC address for the chassis
Returns:
A string containing the MAC address in the format
'XX:XX:XX:XX:XX:XX'
"""
return self._get_eeprom_value(self._TLV_CODE_MAC_BASE)
@default_return(return_value='Undefined.')
def get_serial_number(self):
"""
Retrieves the hardware serial number for the chassis
Returns:
A string containing the hardware serial number for this chassis.
"""
return self._get_eeprom_value(self._TLV_CODE_SERIAL_NUMBER)
@default_return(return_value='Undefined.')
def get_product_name(self):
"""
Retrieves the hardware product name for the chassis
Returns:
A string containing the hardware product name for this chassis.
"""
return self._get_eeprom_value(self._TLV_CODE_PRODUCT_NAME)
@default_return(return_value='Undefined.')
def get_part_number(self):
"""
Retrieves the hardware part number for the chassis
Returns:
A string containing the hardware part number for this chassis.
"""
return self._get_eeprom_value(self._TLV_CODE_PART_NUMBER)
@default_return({})
def get_system_eeprom_info(self):
"""
Retrieves the full content of system EEPROM information for the chassis
Returns:
A dictionary where keys are the type code defined in
OCP ONIE TlvInfo EEPROM format and values are their corresponding
values.
"""
if self._eeprom_info_dict is None:
self._eeprom_info_dict = {}
# Try get from DB first
db_initialized = self._redis_hget('EEPROM_INFO|State', 'Initialized')
if db_initialized == '1':
code = self._TLV_CODE_PRODUCT_NAME
while code <= self._TLV_CODE_SERVICE_TAG:
value = self._redis_hget('EEPROM_INFO|{}'.format(hex(code)), 'Value')
if value:
self._eeprom_info_dict[hex(code)] = value
code += 1
# Handle vendor extension TLV
vendor_extension_tlv_code = hex(self._TLV_CODE_VENDOR_EXT)
try:
vendor_extension_num = int(self._redis_hget('EEPROM_INFO|{}'.format(vendor_extension_tlv_code), 'Num_vendor_ext'))
except (ValueError, TypeError):
vendor_extension_num = 0
if vendor_extension_num != 0:
for i in range(vendor_extension_num):
value = self._redis_hget('EEPROM_INFO|{}'.format(vendor_extension_tlv_code), 'Value_{}'.format(i))
if value:
if vendor_extension_tlv_code not in self._eeprom_info_dict:
self._eeprom_info_dict[vendor_extension_tlv_code] = [value]
else:
self._eeprom_info_dict[vendor_extension_tlv_code].append(value)
# Get CRC
value = self._redis_hget('EEPROM_INFO|{}'.format(hex(self._TLV_CODE_CRC_32)), 'Value')
if value:
self._eeprom_info_dict[hex(self._TLV_CODE_CRC_32)] = value
else:
eeprom = self.read_eeprom()
visitor = EepromContentVisitor(self._eeprom_info_dict)
self.visit_eeprom(eeprom, visitor)
return self._eeprom_info_dict
def _get_eeprom_value(self, code):
"""Helper function to help get EEPROM data by code
Args:
code (int): EEPROM TLV code
Returns:
str: value of EEPROM TLV
"""
eeprom_info_dict = self.get_system_eeprom_info()
return eeprom_info_dict[hex(code)]
class EepromContentVisitor(eeprom_tlvinfo.EepromDefaultVisitor):
def __init__(self, content):
self.content = content
def visit_tlv(self, name, code, length, value):
if code != Eeprom._TLV_CODE_VENDOR_EXT:
self.content[hex(code)] = value.rstrip('\0')
else:
if value:
value = value.rstrip('\0')
if value:
code = hex(code)
if code not in self.content:
self.content[code] = [value]
else:
self.content[code].append(value)