2021-10-17 11:03:02 -05:00
|
|
|
#
|
|
|
|
# Copyright (c) 2019-2021 NVIDIA CORPORATION & AFFILIATES.
|
|
|
|
# Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
2019-07-04 06:29:58 -05:00
|
|
|
#############################################################################
|
|
|
|
# Mellanox
|
|
|
|
#
|
|
|
|
# Module contains an implementation of SONiC Platform Base API and
|
|
|
|
# provides the eeprom information which are available in the platform
|
|
|
|
#
|
|
|
|
#############################################################################
|
|
|
|
import os
|
2021-07-20 03:56:04 -05:00
|
|
|
import subprocess
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2020-12-01 12:44:44 -06:00
|
|
|
from sonic_py_common.logger import Logger
|
2019-07-04 06:29:58 -05:00
|
|
|
try:
|
|
|
|
from sonic_platform_base.sonic_eeprom import eeprom_tlvinfo
|
|
|
|
except ImportError as e:
|
|
|
|
raise ImportError (str(e) + "- required module not found")
|
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
from .device_data import DeviceDataManager
|
|
|
|
from .utils import default_return, is_host
|
2020-12-01 12:44:44 -06:00
|
|
|
|
2021-06-20 09:58:11 -05:00
|
|
|
logger = Logger()
|
2019-07-04 06:29:58 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# this is mlnx-specific
|
|
|
|
# should this be moved to chass.py or here, which better?
|
|
|
|
#
|
|
|
|
EEPROM_SYMLINK = "/var/run/hw-management/eeprom/vpd_info"
|
2021-10-24 23:59:06 -05:00
|
|
|
platform_name = DeviceDataManager.get_platform_name()
|
|
|
|
if platform_name and 'simx' in platform_name:
|
2021-07-20 03:56:04 -05:00
|
|
|
if not os.path.exists(EEPROM_SYMLINK):
|
2021-10-24 23:59:06 -05:00
|
|
|
if is_host():
|
|
|
|
platform_path = os.path.join('/usr/share/sonic/device', platform_name)
|
|
|
|
else:
|
|
|
|
platform_path = '/usr/share/sonic/platform'
|
2021-07-20 03:56:04 -05:00
|
|
|
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)
|
|
|
|
|
2019-07-04 06:29:58 -05:00
|
|
|
|
2021-10-24 23:59:06 -05:00
|
|
|
class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
|
2019-07-04 06:29:58 -05:00
|
|
|
def __init__(self):
|
2021-06-20 09:58:11 -05:00
|
|
|
if not os.path.exists(EEPROM_SYMLINK):
|
2021-10-24 23:59:06 -05:00
|
|
|
logger.log_error("Nowhere to read syseeprom from! No symlink found")
|
|
|
|
raise RuntimeError("No syseeprom symlink found")
|
2019-07-04 06:29:58 -05:00
|
|
|
|
|
|
|
self.eeprom_path = EEPROM_SYMLINK
|
|
|
|
super(Eeprom, self).__init__(self.eeprom_path, 0, '', True)
|
2021-06-20 09:58:11 -05:00
|
|
|
self._eeprom_info_dict = None
|
2019-07-04 06:29:58 -05:00
|
|
|
|
2021-06-20 09:58:11 -05:00
|
|
|
@default_return(return_value='Undefined.')
|
2019-07-04 06:29:58 -05:00
|
|
|
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'
|
|
|
|
"""
|
2021-06-20 09:58:11 -05:00
|
|
|
return self._get_eeprom_value(self._TLV_CODE_MAC_BASE)
|
|
|
|
|
|
|
|
@default_return(return_value='Undefined.')
|
2019-07-04 06:29:58 -05:00
|
|
|
def get_serial_number(self):
|
|
|
|
"""
|
|
|
|
Retrieves the hardware serial number for the chassis
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A string containing the hardware serial number for this chassis.
|
|
|
|
"""
|
2021-06-20 09:58:11 -05:00
|
|
|
return self._get_eeprom_value(self._TLV_CODE_SERIAL_NUMBER)
|
2019-07-04 06:29:58 -05:00
|
|
|
|
2021-06-20 09:58:11 -05:00
|
|
|
@default_return(return_value='Undefined.')
|
2020-09-26 13:20:43 -05:00
|
|
|
def get_product_name(self):
|
|
|
|
"""
|
|
|
|
Retrieves the hardware product name for the chassis
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A string containing the hardware product name for this chassis.
|
|
|
|
"""
|
2021-06-20 09:58:11 -05:00
|
|
|
return self._get_eeprom_value(self._TLV_CODE_PRODUCT_NAME)
|
2020-09-26 13:20:43 -05:00
|
|
|
|
2021-06-20 09:58:11 -05:00
|
|
|
@default_return(return_value='Undefined.')
|
2020-09-26 13:20:43 -05:00
|
|
|
def get_part_number(self):
|
|
|
|
"""
|
|
|
|
Retrieves the hardware part number for the chassis
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A string containing the hardware part number for this chassis.
|
|
|
|
"""
|
2021-06-20 09:58:11 -05:00
|
|
|
return self._get_eeprom_value(self._TLV_CODE_PART_NUMBER)
|
2020-09-26 13:20:43 -05:00
|
|
|
|
2021-06-20 09:58:11 -05:00
|
|
|
@default_return({})
|
2019-07-04 06:29:58 -05:00
|
|
|
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.
|
|
|
|
"""
|
2021-06-20 09:58:11 -05:00
|
|
|
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)
|
2019-07-04 06:29:58 -05:00
|
|
|
return self._eeprom_info_dict
|
2021-06-20 09:58:11 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|