Skip to content

Instantly share code, notes, and snippets.

@igrr
Created June 15, 2023 10:08
Show Gist options
  • Save igrr/3537f8ddb03a50aa980e2e0c121c7581 to your computer and use it in GitHub Desktop.
Save igrr/3537f8ddb03a50aa980e2e0c121c7581 to your computer and use it in GitHub Desktop.
import argparse
import binascii
import sys
def main():
parser = argparse.ArgumentParser(description="Generate efuse binary")
parser.add_argument(
"--mac", type=str, default="11:22:33:44:55:66", help="MAC address to use"
)
parser.add_argument("output_file", type=argparse.FileType("wb"), help="Output file")
args = parser.parse_args()
mac_addr_str = args.mac
mac_addr = binascii.unhexlify(mac_addr_str.replace(":", ""))
mac_addr_crc = calc_mac_addr_crc(mac_addr)
efuse_data = bytearray(124)
# MAC address
efuse_data[4:10] = mac_addr[::-1]
efuse_data[10] = mac_addr_crc
# Chip revision bits
efuse_data[13] = 0x80
efuse_data[22] = 0x10
args.output_file.write(efuse_data)
def calc_mac_addr_crc(mac_addr):
i = 0
j = 0
crc = 0
length = len(mac_addr)
while length > 0:
length -= 1
crc = crc ^ mac_addr[j]
j += 1
for i in range(8):
if (crc) & 0x01:
crc = ((crc >> 1) ^ (0x8C)) & 0xFF
else:
crc = (crc >> 1) & 0xFF
return crc
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment