2020-10-03 15:46:21 -05:00
|
|
|
try:
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import errno
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import logging.config
|
|
|
|
import yaml
|
2021-02-11 17:00:35 -06:00
|
|
|
import re
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
sys.path.append(os.path.dirname(__file__))
|
|
|
|
|
2020-11-25 12:28:36 -06:00
|
|
|
if sys.version_info.major == 3:
|
|
|
|
from io import StringIO
|
|
|
|
else:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
from sonic_platform_base.sonic_eeprom import eeprom_base
|
|
|
|
from sonic_platform_base.sonic_eeprom import eeprom_tlvinfo
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-01-29 19:29:37 -06:00
|
|
|
from platform_thrift_client import thrift_try
|
2020-11-25 12:28:36 -06:00
|
|
|
except ImportError as e:
|
2020-10-03 15:46:21 -05:00
|
|
|
raise ImportError (str(e) + "- required module not found")
|
|
|
|
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
_platform_eeprom_map = {
|
2020-10-03 15:46:21 -05:00
|
|
|
"prod_name" : ("Product Name", "0x21", 12),
|
|
|
|
"odm_pcba_part_num" : ("Part Number", "0x22", 13),
|
|
|
|
"prod_ser_num" : ("Serial Number", "0x23", 12),
|
|
|
|
"ext_mac_addr" : ("Extended MAC Address Base", "0x24", 12),
|
|
|
|
"sys_mfg_date" : ("System Manufacturing Date", "0x25", 4),
|
|
|
|
"prod_ver" : ("Product Version", "0x26", 1),
|
|
|
|
"ext_mac_addr_size" : ("Extende MAC Address Size", "0x2A", 2),
|
|
|
|
"sys_mfger" : ("Manufacturer", "0x2B", 8)
|
|
|
|
}
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
_product_dict = { "Montara" : "Wedge100BF-32X-O-AC-F-BF",
|
2020-10-03 15:46:21 -05:00
|
|
|
"Lower MAV" : "Wedge100BF-65X-O-AC-F-BF",
|
|
|
|
"Upper MAV" : "Wedge100BF-65X-O-AC-F-BF"
|
|
|
|
}
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
_EEPROM_SYMLINK = "/var/run/platform/eeprom/syseeprom"
|
|
|
|
_EEPROM_STATUS = "/var/run/platform/eeprom/status"
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
|
|
|
|
def __init__(self):
|
|
|
|
with open(os.path.dirname(__file__) + "/logging.conf", 'r') as f:
|
|
|
|
config_dict = yaml.load(f, yaml.SafeLoader)
|
|
|
|
logging.config.dictConfig(config_dict)
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
if not os.path.exists(os.path.dirname(_EEPROM_SYMLINK)):
|
2020-10-03 15:46:21 -05:00
|
|
|
try:
|
2021-02-11 17:00:35 -06:00
|
|
|
os.makedirs(os.path.dirname(_EEPROM_SYMLINK))
|
2020-10-03 15:46:21 -05:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
open(_EEPROM_SYMLINK, 'a').close()
|
|
|
|
with open(_EEPROM_STATUS, 'w') as f:
|
|
|
|
f.write("initializing..")
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
self.eeprom_path = _EEPROM_SYMLINK
|
|
|
|
super(Eeprom, self).__init__(self.eeprom_path, 0, _EEPROM_STATUS, True)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2020-10-05 12:50:03 -05:00
|
|
|
def sys_eeprom_get(client):
|
|
|
|
return client.pltfm_mgr.pltfm_mgr_sys_eeprom_get()
|
2020-10-03 15:46:21 -05:00
|
|
|
try:
|
2021-02-11 17:00:35 -06:00
|
|
|
platform_eeprom = thrift_try(sys_eeprom_get)
|
2020-10-03 15:46:21 -05:00
|
|
|
except Exception:
|
2020-10-05 12:50:03 -05:00
|
|
|
raise RuntimeError("eeprom.py: Initialization failed")
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
self.__eeprom_init(platform_eeprom)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
def __eeprom_init(self, platform_eeprom):
|
|
|
|
with open(_EEPROM_STATUS, 'w') as f:
|
|
|
|
f.write("ok")
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
eeprom_params = ""
|
2021-02-11 17:00:35 -06:00
|
|
|
for attr, val in platform_eeprom.__dict__.items():
|
2020-10-03 15:46:21 -05:00
|
|
|
if val is None:
|
|
|
|
continue
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
elem = _platform_eeprom_map.get(attr)
|
2020-10-03 15:46:21 -05:00
|
|
|
if elem is None:
|
|
|
|
continue
|
|
|
|
|
2021-03-11 21:00:19 -06:00
|
|
|
if isinstance(val, str):
|
2020-10-03 15:46:21 -05:00
|
|
|
value = val.replace('\0', '')
|
|
|
|
else:
|
|
|
|
value = str(val)
|
|
|
|
|
|
|
|
if attr == "sys_mfg_date":
|
|
|
|
value = datetime.datetime.strptime(value, '%m-%d-%y').strftime('%m/%d/%Y 00:00:00')
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
product = _product_dict.get(value)
|
2020-10-03 15:46:21 -05:00
|
|
|
if product is not None:
|
|
|
|
value = product
|
|
|
|
if len(eeprom_params) > 0:
|
|
|
|
eeprom_params += ","
|
|
|
|
eeprom_params += "{0:s}={1:s}".format(elem[1], value)
|
|
|
|
|
|
|
|
orig_stdout = sys.stdout
|
|
|
|
sys.stdout = StringIO()
|
2020-10-05 12:50:03 -05:00
|
|
|
try:
|
2021-02-11 17:00:35 -06:00
|
|
|
eeprom_data = eeprom_tlvinfo.TlvInfoDecoder.set_eeprom(self, "", [eeprom_params])
|
2020-10-05 12:50:03 -05:00
|
|
|
finally:
|
2021-02-11 17:00:35 -06:00
|
|
|
decode_output = sys.stdout.getvalue()
|
2020-10-05 12:50:03 -05:00
|
|
|
sys.stdout = orig_stdout
|
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
eeprom_base.EepromDecoder.write_eeprom(self, eeprom_data)
|
|
|
|
self.__eeprom_tlv_dict = self.__parse_output(decode_output)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
def __parse_output(self, decode_output):
|
|
|
|
EEPROM_DECODE_HEADLINES = 6
|
|
|
|
lines = decode_output.replace('\0', '').split('\n')
|
|
|
|
lines = lines[EEPROM_DECODE_HEADLINES:]
|
|
|
|
res = dict()
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-02-11 17:00:35 -06:00
|
|
|
for line in lines:
|
|
|
|
try:
|
|
|
|
# match whitespace-separated tag hex, length and value (value is mathced with its whitespaces)
|
|
|
|
match = re.search('(0x[0-9a-fA-F]{2})([\s]+[\S]+[\s]+)([\S]+[\s]*[\S]*)', line)
|
|
|
|
if match is not None:
|
|
|
|
code = match.group(1)
|
|
|
|
value = match.group(3).rstrip('\0')
|
|
|
|
res[code] = value
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return res
|
|
|
|
|
|
|
|
def __tlv_get(self, code):
|
|
|
|
return self.__eeprom_tlv_dict.get("0x{:X}".format(code), 'N/A')
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def system_eeprom_info(self):
|
2021-02-11 17:00:35 -06:00
|
|
|
return self.__eeprom_tlv_dict
|
|
|
|
|
|
|
|
def serial_number_str(self):
|
|
|
|
return self.__tlv_get(self._TLV_CODE_SERIAL_NUMBER)
|
|
|
|
|
|
|
|
def serial_str(self):
|
|
|
|
return self.serial_number_str()
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-01-24 18:32:18 -06:00
|
|
|
def base_mac_addr(self):
|
2021-02-11 17:00:35 -06:00
|
|
|
return self.__tlv_get(self._TLV_CODE_MAC_BASE)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def part_number_str(self):
|
2021-02-11 17:00:35 -06:00
|
|
|
return self.__tlv_get(self._TLV_CODE_PART_NUMBER)
|
2021-01-24 18:32:18 -06:00
|
|
|
|
|
|
|
def modelstr(self):
|
2021-02-11 17:00:35 -06:00
|
|
|
return self.__tlv_get(self._TLV_CODE_PRODUCT_NAME)
|