Skip to content

Commit

Permalink
Fixed eeprom_tlvinfo.py to make it Python2/Python3 compatible (#155)
Browse files Browse the repository at this point in the history
This PR fixes eeprom_tlvinfo.py to make it compatible with both Python2 & Python3. 

The current implementation causes runtime issue as follows:
```
Dec 14 12:04:34.908841 sonic WARNING pmon#syseepromd[51]: Failed to load platform-specific eeprom from sonic_platform package due to UnicodeDecodeError('ascii', '*\x02\x00\x8c', 3, 4, 'ordinal not in range(128)')
Dec 14 12:04:34.910093 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet56 admin status to up
Dec 14 12:04:34.923442 sonic ERR pmon#syseepromd[51]: Failed to load platform-specific eeprom implementation: UnicodeDecodeError('ascii', '*\x02\x00\x8c', 3, 4, 'ordinal not in range(128)')
Dec 14 12:04:34.925628 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet60 MTU to 9100
Dec 14 12:04:34.939277 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet60 fec to rs
Dec 14 12:04:34.943819 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet60 admin status to up
```
This cause `syseepromd`  crash:
```
Dec 14 12:04:43.194489 sonic INFO pmon#supervisord 2020-12-14 12:04:34,651 INFO spawned: 'syseepromd' with pid 51
Dec 14 12:04:43.194489 sonic INFO pmon#supervisord 2020-12-14 12:04:34,955 INFO exited: syseepromd (exit status 5; not expected)
```

Signed-off-by: Andriy Kokhan <akokhan@barefootnetworks.com>
  • Loading branch information
akokhan authored Dec 15, 2020
1 parent 7e23e63 commit 9935fca
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,16 @@ def set_eeprom(self, e, cmd_args):

if self._TLV_HDR_ENABLED:
new_tlvs_len = len(new_tlvs) + 6
new_e = self._TLV_INFO_ID_STRING + bytes([self._TLV_INFO_VERSION]) + \
bytes([(new_tlvs_len >> 8) & 0xFF]) + \
bytes([new_tlvs_len & 0xFF]) + new_tlvs
new_e = self._TLV_INFO_ID_STRING + bytearray([self._TLV_INFO_VERSION]) + \
bytearray([(new_tlvs_len >> 8) & 0xFF]) + \
bytearray([new_tlvs_len & 0xFF]) + new_tlvs
else:
new_e = new_tlvs

if self._TLV_CODE_CRC_32 != self._TLV_CODE_UNDEFINED:
new_e = new_e + bytes([self._TLV_CODE_CRC_32]) + bytes([4])
new_e = new_e + bytearray([self._TLV_CODE_CRC_32]) + bytearray([4])
elif self._TLV_CODE_QUANTA_CRC != self._TLV_CODE_UNDEFINED:
new_e = new_e + bytes([self._TLV_CODE_QUANTA_CRC]) + bytes([2])
new_e = new_e + bytearray([self._TLV_CODE_QUANTA_CRC]) + bytearray([2])
else:
print("\nFailed to formulate new eeprom\n")
exit
Expand Down Expand Up @@ -624,19 +624,19 @@ def encoder(self, I, v):
errstr = "A string less than 256 characters"
if len(v) > 255:
raise
value = v
value = v.encode("ascii", "replace")
elif I[0] == self._TLV_CODE_DEVICE_VERSION:
errstr = "A number between 0 and 255"
num = int(v, 0)
if num < 0 or num > 255:
raise
value = chr(num)
value = bytearray([num])
elif I[0] == self._TLV_CODE_MAC_SIZE:
errstr = "A number between 0 and 65535"
num = int(v, 0)
if num < 0 or num > 65535:
raise
value = chr((num >> 8) & 0xFF) + chr(num & 0xFF)
value = bytearray([(num >> 8) & 0xFF, num & 0xFF])
elif I[0] == self._TLV_CODE_MANUF_DATE:
errstr = 'MM/DD/YYYY HH:MM:SS'
date, time = v.split()
Expand All @@ -646,22 +646,22 @@ def encoder(self, I, v):
mo < 1 or mo > 12 or da < 1 or da > 31 or yr < 0 or yr > 9999 or \
hr < 0 or hr > 23 or mn < 0 or mn > 59 or sc < 0 or sc > 59:
raise
value = v
value = v.encode("ascii", "replace")
elif I[0] == self._TLV_CODE_MAC_BASE:
errstr = 'XX:XX:XX:XX:XX:XX'
mac_digits = v.split(':')
if len(mac_digits) != 6:
raise
value = ""
value = bytearray()
for c in mac_digits:
value = value + chr(int(c, 16))
value += bytearray([int(c, 16)])
elif I[0] == self._TLV_CODE_MANUF_COUNTRY:
errstr = 'CC, a two character ISO 3166-1 alpha-2 country code'
if len(v) < 2:
raise
value = v[0:2]
value = v.encode("ascii", "replace")
elif I[0] == self._TLV_CODE_CRC_32:
value = ''
value = bytearray()
# Disallow setting any Quanta specific codes
elif I[0] == self._TLV_CODE_QUANTA_MAGIC or \
I[0] == self._TLV_CODE_QUANTA_CARD_TYPE or \
Expand All @@ -672,17 +672,17 @@ def encoder(self, I, v):
raise Exception('quanta-read-only')
else:
errstr = '0xXX ... A list of space-separated hexidecimal numbers'
value = ""
value = bytearray()
for c in v.split():
value += chr(int(c, 0))
value += bytearray([int(c, 0)])
except Exception as inst:
if (len(inst.args) > 0) and (inst.args[0] == 'quanta-read-only'):
sys.stderr.write("Error: '" + "0x%02X" % (I[0],) + "' -- Unable to set the read-only Quanta codes.\n")
else:
sys.stderr.write("Error: '" + "0x%02X" % (I[0],) + "' correct format is " + errstr + "\n")
exit(0)

return (chr(I[0]) + chr(len(value)) + value).encode()
return bytearray([I[0]]) + bytearray([len(value)]) + value


def is_checksum_field(self, I):
Expand Down

0 comments on commit 9935fca

Please sign in to comment.