Skip to content

Commit

Permalink
Merge branch 'github-201911' into sonic-cfg-mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenggen-xu committed Jan 23, 2020
2 parents d8a89fe + 6ddd012 commit ed88e95
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions sonic_platform_base/device_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def get_name(self):
Returns:
string: The name of the device
"""
raise NotImplementedError


def get_presence(self):
"""
Expand Down
1 change: 1 addition & 0 deletions sonic_platform_base/fan_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FanBase(device_base.DeviceBase):
# Possible fan directions (relative to port-side of device)
FAN_DIRECTION_INTAKE = "intake"
FAN_DIRECTION_EXHAUST = "exhaust"
FAN_DIRECTION_NOT_APPLICABLE = "N/A"

# Possible fan status LED colors
STATUS_LED_COLOR_GREEN = "green"
Expand Down
49 changes: 44 additions & 5 deletions sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TlvInfoDecoder(eeprom_base.EepromDecoder):

# TLV Value Display Switch
_TLV_DISPLAY_VENDOR_EXT = False
_TLV_NUM_VENDOR_EXT = 0


def __init__(self, path, start, status, ro, max_len=_TLV_INFO_MAX_LEN):
super(TlvInfoDecoder, self).__init__(path, \
Expand All @@ -86,6 +86,7 @@ def __init__(self, path, start, status, ro, max_len=_TLV_INFO_MAX_LEN):
self.eeprom_start = start
self.eeprom_max_len = max_len


def __print_db(self, db, code, num=0):
if not num:
field_name = db.hget('EEPROM_INFO|{}'.format(hex(code)), 'Name')
Expand All @@ -102,6 +103,7 @@ def __print_db(self, db, code, num=0):
field_value = db.hget('EEPROM_INFO|{}'.format(hex(code)), 'Value_{}'.format(index))
print("%-20s 0x%02X %3s %s" % (field_name, code, field_len, field_value))


def decode_eeprom(self, e):
'''
Decode and print out the contents of the EEPROM.
Expand Down Expand Up @@ -136,6 +138,7 @@ def decode_eeprom(self, e):
return
tlv_index += ord(e[tlv_index+1]) + 2


def set_eeprom(self, e, cmd_args):
'''
Returns the new contents of the EEPROM. If command line arguments are supplied,
Expand Down Expand Up @@ -225,6 +228,7 @@ def set_eeprom(self, e, cmd_args):
exit(1)
return new_e


def is_valid_tlvinfo_header(self, e):
'''
Perform sanity checks on the first 11 bytes of the TlvInfo EEPROM
Expand All @@ -240,6 +244,7 @@ def is_valid_tlvinfo_header(self, e):
ord(e[8]) == self._TLV_INFO_VERSION and \
((ord(e[9]) << 8) | ord(e[10])) <= self._TLV_TOTAL_LEN_MAX


def is_valid_tlv(self, e):
'''
Perform basic sanity checks on a TLV field. The TLV is in the string
Expand All @@ -250,6 +255,7 @@ def is_valid_tlv(self, e):
'''
return (len(e) >= 2 and (2 + ord(e[1]) <= len(e)))


def is_checksum_valid(self, e):
'''
Validate the checksum in the provided TlvInfo EEPROM data.
Expand All @@ -271,6 +277,7 @@ def is_checksum_valid(self, e):

return (False, crc)


def read_eeprom(self):
'''
Read the eeprom contents. This is performed in two steps. First
Expand Down Expand Up @@ -300,6 +307,7 @@ def read_eeprom(self):
"but only read %d" %(len(t)))
return h + t


def read_eeprom_db(self):
'''
Print out the contents of the EEPROM from database
Expand Down Expand Up @@ -329,8 +337,18 @@ def read_eeprom_db(self):
self.__print_db(client, self._TLV_CODE_VENDOR_EXT, num_vendor_ext)

self.__print_db(client, self._TLV_CODE_CRC_32)

print("")

is_valid = client.hget('EEPROM_INFO|Checksum', 'Valid')
if is_valid != '1':
print("(*** checksum invalid)")
else:
print("(checksum valid)")

return 0


def update_eeprom_db(self, e):
'''
Decode the contents of the EEPROM and update the contents to database
Expand All @@ -353,16 +371,17 @@ def update_eeprom_db(self, e):
tlv_index = self.eeprom_start
tlv_end = self._TLV_INFO_MAX_LEN

vendor_ext_tlv_num = 0
while (tlv_index + 2) < len(e) and tlv_index < tlv_end:
if not self.is_valid_tlv(e[tlv_index:]):
break
tlv = e[tlv_index:tlv_index + 2 + ord(e[tlv_index + 1])]
tlv_code = ord(tlv[0])
if tlv_code == self._TLV_CODE_VENDOR_EXT:
vendor_index = str(self._TLV_NUM_VENDOR_EXT)
vendor_index = str(vendor_ext_tlv_num)
fvs['Len_{}'.format(vendor_index)] = ord(tlv[1])
fvs['Name_{}'.format(vendor_index)], fvs['Value_{}'.format(vendor_index)] = self.decoder(None, tlv)
self._TLV_NUM_VENDOR_EXT += 1
vendor_ext_tlv_num += 1
else:
fvs['Len'] = ord(tlv[1])
fvs['Name'], fvs['Value'] = self.decoder(None, tlv)
Expand All @@ -374,15 +393,25 @@ def update_eeprom_db(self, e):
else:
tlv_index += ord(e[tlv_index + 1]) + 2

if self._TLV_NUM_VENDOR_EXT:
fvs['Num_vendor_ext'] = self._TLV_NUM_VENDOR_EXT
if vendor_ext_tlv_num > 0:
fvs['Num_vendor_ext'] = str(vendor_ext_tlv_num)
client.hmset('EEPROM_INFO|{}'.format(hex(self._TLV_CODE_VENDOR_EXT)), fvs)
fvs.clear()

(is_valid, valid_crc) = self.is_checksum_valid(e)
if is_valid:
fvs['Valid'] = '1'
else:
fvs['Valid'] = '0'

client.hmset('EEPROM_INFO|Checksum', fvs)
fvs.clear()

fvs['Initialized'] = '1'
client.hmset('EEPROM_INFO|State', fvs)
return 0


def get_tlv_field(self, e, code):
'''
Given an EEPROM string the TLV field for the provided code is
Expand Down Expand Up @@ -411,6 +440,7 @@ def get_tlv_field(self, e, code):
tlv_index += ord(e[tlv_index+1]) + 2
return (False, None)


def get_tlv_index(self, e, code):
'''
Given an EEPROM string with just TLV fields (no TlvInfo header)
Expand All @@ -429,6 +459,7 @@ def get_tlv_index(self, e, code):
tlv_index += ord(e[tlv_index+1]) + 2
return (False, 0)


def base_mac_addr(self, e):
'''
Returns the value field of the MAC #1 Base TLV formatted as a string
Expand All @@ -440,6 +471,7 @@ def base_mac_addr(self, e):

return ":".join([binascii.b2a_hex(T) for T in t[2]])


def switchaddrrange(self, e):
'''
Returns the value field of the MAC #1 Size TLV formatted as a decimal
Expand All @@ -451,6 +483,7 @@ def switchaddrrange(self, e):

return str((ord(t[2][0]) << 8) | ord(t[2][1]))


def modelstr(self, e):
'''
Returns the value field of the Product Name TLV as a string
Expand All @@ -461,6 +494,7 @@ def modelstr(self, e):

return t[2]


def serial_number_str(self, e):
'''
Returns the value field of the Serial Number TLV as a string
Expand All @@ -470,6 +504,7 @@ def serial_number_str(self, e):
return super(TlvInfoDecoder, self).serial_number_str(e)
return t[2]


def decoder(self, s, t):
'''
Return a string representing the contents of the TLV field. The format of
Expand Down Expand Up @@ -565,6 +600,7 @@ def decoder(self, s, t):
value += "0x%02X " % (ord(c),)
return name, value


def encoder(self, I, v):
'''
Validate and encode the string 'v' into the TLV specified by 'I'.
Expand Down Expand Up @@ -644,11 +680,14 @@ def encoder(self, I, v):

return chr(I[0]) + chr(len(value)) + value


def is_checksum_field(self, I):
return False


def checksum_field_size(self):
return 4


def checksum_type(self):
return 'crc32'

0 comments on commit ed88e95

Please sign in to comment.