Skip to content

Commit 9935fca

Browse files
authored
Fixed eeprom_tlvinfo.py to make it Python2/Python3 compatible (#155)
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 <[email protected]>
1 parent 7e23e63 commit 9935fca

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,16 @@ def set_eeprom(self, e, cmd_args):
202202

203203
if self._TLV_HDR_ENABLED:
204204
new_tlvs_len = len(new_tlvs) + 6
205-
new_e = self._TLV_INFO_ID_STRING + bytes([self._TLV_INFO_VERSION]) + \
206-
bytes([(new_tlvs_len >> 8) & 0xFF]) + \
207-
bytes([new_tlvs_len & 0xFF]) + new_tlvs
205+
new_e = self._TLV_INFO_ID_STRING + bytearray([self._TLV_INFO_VERSION]) + \
206+
bytearray([(new_tlvs_len >> 8) & 0xFF]) + \
207+
bytearray([new_tlvs_len & 0xFF]) + new_tlvs
208208
else:
209209
new_e = new_tlvs
210210

211211
if self._TLV_CODE_CRC_32 != self._TLV_CODE_UNDEFINED:
212-
new_e = new_e + bytes([self._TLV_CODE_CRC_32]) + bytes([4])
212+
new_e = new_e + bytearray([self._TLV_CODE_CRC_32]) + bytearray([4])
213213
elif self._TLV_CODE_QUANTA_CRC != self._TLV_CODE_UNDEFINED:
214-
new_e = new_e + bytes([self._TLV_CODE_QUANTA_CRC]) + bytes([2])
214+
new_e = new_e + bytearray([self._TLV_CODE_QUANTA_CRC]) + bytearray([2])
215215
else:
216216
print("\nFailed to formulate new eeprom\n")
217217
exit
@@ -624,19 +624,19 @@ def encoder(self, I, v):
624624
errstr = "A string less than 256 characters"
625625
if len(v) > 255:
626626
raise
627-
value = v
627+
value = v.encode("ascii", "replace")
628628
elif I[0] == self._TLV_CODE_DEVICE_VERSION:
629629
errstr = "A number between 0 and 255"
630630
num = int(v, 0)
631631
if num < 0 or num > 255:
632632
raise
633-
value = chr(num)
633+
value = bytearray([num])
634634
elif I[0] == self._TLV_CODE_MAC_SIZE:
635635
errstr = "A number between 0 and 65535"
636636
num = int(v, 0)
637637
if num < 0 or num > 65535:
638638
raise
639-
value = chr((num >> 8) & 0xFF) + chr(num & 0xFF)
639+
value = bytearray([(num >> 8) & 0xFF, num & 0xFF])
640640
elif I[0] == self._TLV_CODE_MANUF_DATE:
641641
errstr = 'MM/DD/YYYY HH:MM:SS'
642642
date, time = v.split()
@@ -646,22 +646,22 @@ def encoder(self, I, v):
646646
mo < 1 or mo > 12 or da < 1 or da > 31 or yr < 0 or yr > 9999 or \
647647
hr < 0 or hr > 23 or mn < 0 or mn > 59 or sc < 0 or sc > 59:
648648
raise
649-
value = v
649+
value = v.encode("ascii", "replace")
650650
elif I[0] == self._TLV_CODE_MAC_BASE:
651651
errstr = 'XX:XX:XX:XX:XX:XX'
652652
mac_digits = v.split(':')
653653
if len(mac_digits) != 6:
654654
raise
655-
value = ""
655+
value = bytearray()
656656
for c in mac_digits:
657-
value = value + chr(int(c, 16))
657+
value += bytearray([int(c, 16)])
658658
elif I[0] == self._TLV_CODE_MANUF_COUNTRY:
659659
errstr = 'CC, a two character ISO 3166-1 alpha-2 country code'
660660
if len(v) < 2:
661661
raise
662-
value = v[0:2]
662+
value = v.encode("ascii", "replace")
663663
elif I[0] == self._TLV_CODE_CRC_32:
664-
value = ''
664+
value = bytearray()
665665
# Disallow setting any Quanta specific codes
666666
elif I[0] == self._TLV_CODE_QUANTA_MAGIC or \
667667
I[0] == self._TLV_CODE_QUANTA_CARD_TYPE or \
@@ -672,17 +672,17 @@ def encoder(self, I, v):
672672
raise Exception('quanta-read-only')
673673
else:
674674
errstr = '0xXX ... A list of space-separated hexidecimal numbers'
675-
value = ""
675+
value = bytearray()
676676
for c in v.split():
677-
value += chr(int(c, 0))
677+
value += bytearray([int(c, 0)])
678678
except Exception as inst:
679679
if (len(inst.args) > 0) and (inst.args[0] == 'quanta-read-only'):
680680
sys.stderr.write("Error: '" + "0x%02X" % (I[0],) + "' -- Unable to set the read-only Quanta codes.\n")
681681
else:
682682
sys.stderr.write("Error: '" + "0x%02X" % (I[0],) + "' correct format is " + errstr + "\n")
683683
exit(0)
684684

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

687687

688688
def is_checksum_field(self, I):

0 commit comments

Comments
 (0)