Skip to content

Instantly share code, notes, and snippets.

@djGrrr
Last active July 30, 2023 01:49
Show Gist options
  • Save djGrrr/9bd545c03e2a42ce54581f3ee6fef915 to your computer and use it in GitHub Desktop.
Save djGrrr/9bd545c03e2a42ce54581f3ee6fef915 to your computer and use it in GitHub Desktop.
<?php
# to get a eeprom dump from the stick:
# ethtool -m pon0 raw on | base64
function dH(int $dec) { $hex = strtoupper(dechex($dec)); $l = strlen($hex); return "0x".str_pad($hex, ceil($l / 2) * 2, "0", STR_PAD_LEFT); }
function sH(string $string) { return strtoupper(bin2hex($string)); }
function cH(string $char) { return dH(ord($char)); }
function dB(int $dec) { $bin = decbin($dec); $l = strlen($bin); return str_pad($bin, ceil($l/8) * 8, "0", STR_PAD_LEFT); }
function bl(bool $bool) { return $bool ? 'true' : 'false'; }
function err(string $message) { fwrite(STDERR, $message."\n"); }
function fail(string $message, int $code = 1) { err($message); exit($code); }
if (!isset($argv[1]))
fail("syntax: php {$_SERVER['SCRIPT_FILENAME']} {dump.base64.file}");
$eeprom = base64_decode(file_get_contents($argv[1]));
$length = strlen($eeprom);
if (!in_array($length, [256, 512]))
fail("EEPROM length incorrect ($length)");
$a0_eeprom = substr($eeprom, 0, 256);
$a2_eeprom = substr($eeprom, 256, 256);
$vendor_name = substr($a0_eeprom, 20, 16);
$vendor_oui = implode(":", str_split(sH(substr($a0_eeprom, 37, 3)), 2));
$vendor_pn = substr($a0_eeprom, 40, 16);
$vendor_rev = substr($a0_eeprom, 56, 4);
$wavelength = hexdec(bin2hex(substr($a0_eeprom, 60, 2)));
$options_1 = ord($a0_eeprom[64]);
$options_2 = ord($a0_eeprom[65]);
$vendor_sn = substr($a0_eeprom, 68, 16);
$dianostics_monitoring = ord($a0_eeprom[92]);
$enhanced_options = ord($a0_eeprom[93]);
$rx_los = (bool)($options_2 & 0x2);
$tx_fault = (bool)($options_2 & 0x8);
$tx_disable = (bool)($options_2 & 0x10);
$rate_select = (bool)($options_2 & 0x20);
$enh_rx_los = (bool)($enhanced_options & 0x10);
$enh_tx_fault = (bool)($enhanced_options & 0x20);
$enh_tx_disable = (bool)($enhanced_options & 0x40);
$enh_rate_select = (bool)($enhanced_options & 0x8);
$new_a0_eeprom = $a0_eeprom;
$new_options_2 = $options_2;
$new_options_2 &= ~(0x2 | 0x8);
$new_a0_eeprom[65] = chr($new_options_2);
$new_enhanced_options = $enhanced_options;
$new_enhanced_options &= ~(0x10 | 0x20);
$new_a0_eeprom[93] = chr($new_enhanced_options);
$cc_base = ord($a0_eeprom[63]);
$new_cc_base = 0;
for ($i = 0; $i < 63; $i++)
$new_cc_base += ord($new_a0_eeprom[$i]);
$new_cc_base &= 0xff;
$new_a0_eeprom[63] = chr($new_cc_base);
$cc_base_valid = $cc_base == $new_cc_base;
$cc_ext = ord($a0_eeprom[95]);
$new_cc_ext = 0;
for ($i = 64; $i < 95; $i++)
$new_cc_ext += ord($new_a0_eeprom[$i]);
$new_cc_ext &= 0xff;
$cc_ext_valid = $cc_ext == $new_cc_ext;
$new_a0_eeprom[95] = chr($new_cc_ext);
echo "======= BASE =======\n";
echo "Vendor Name: $vendor_name\n";
echo "Vendor OUI: $vendor_oui\n";
echo "Vendor PN: $vendor_pn\n";
echo "Vendor Revision: $vendor_rev\n";
echo "Wavelength: {$wavelength}nm\n";
echo "\nCC_BASE = ".dH($cc_base).($cc_base_valid ? ' (correct)' : ' (incorrect)')."\n";
if (!$cc_base_valid)
echo 'NEW CC_BASE = '.dH($new_cc_base)."\n";
echo "====================\n\n";
echo "===== Extended =====\n";
echo "Options: ".dH($options_1)." ".dH($options_2)." (".dB($options_1)." ".dB($options_2).")\n";
echo "Vendor SN: $vendor_sn\n";
echo "Diagnostic Monitoring: ".dH($dianostics_monitoring)." (".dB($dianostics_monitoring).")\n";
echo "Enhanced Options: ".dH($enhanced_options)." (".dB($enhanced_options).")\n";
echo "\nCC_EXT = ".dH($cc_ext).($cc_ext_valid ? ' (correct)' : ' (incorrect)')."\n";
if (!$cc_ext_valid)
echo "NEW CC_EXT = ".dH($new_cc_ext)."\n";
echo "====================\n\n";
echo "===== Options =====\n";
echo "RX_LOS: ".bl($rx_los)."\n";
echo "TX_FAULT: ".bl($tx_fault)."\n";
echo "TX_DISABLE: ".bl($tx_disable)."\n";
echo "RATE_SELECT: ".bl($rate_select)."\n";
echo "====================\n\n";
echo "= Enhanced Options =\n";
echo "RX_LOS: ".bl($enh_rx_los)."\n";
echo "TX_FAULT: ".bl($enh_tx_fault)."\n";
echo "TX_DISABLE: ".bl($enh_tx_disable)."\n";
echo "RATE_SELECT: ".bl($enh_rate_select)."\n";
echo "====================\n";
$a0_match = $a0_eeprom === $new_a0_eeprom;
if (!$a0_match) {
echo "\n----------------------------\n";
echo "i2c_cmd write 0x51 0x7B 4 0x12 0x34 0x56 0x78\n";
for ($i = 0; $i < 256; $i++) {
if ($a0_eeprom[$i] != $new_a0_eeprom[$i])
echo "i2c_cmd write 0x50 ".dH($i)." 1 ".cH($new_a0_eeprom[$i])."\n";
}
echo "i2c_cmd write 0x51 0x7B 4 0x00 0x00 0x00 0x00\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment