Skip to content

Commit

Permalink
Fix EEPROM vendor extension TLV number counting issue (sonic-net#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
keboliu authored and jleveque committed Dec 7, 2019
1 parent 8f609ad commit 88cf320
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 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 @@ -340,6 +348,7 @@ def read_eeprom_db(self):

return 0


def update_eeprom_db(self, e):
'''
Decode the contents of the EEPROM and update the contents to database
Expand All @@ -362,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 @@ -383,8 +393,8 @@ 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()

Expand All @@ -401,6 +411,7 @@ def update_eeprom_db(self, e):
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 @@ -429,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 @@ -447,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 @@ -458,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 @@ -469,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 @@ -479,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 @@ -488,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 @@ -583,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 @@ -662,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 88cf320

Please sign in to comment.