|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# Quick MAC address adjustment script |
| 4 | +# Takes a Net.hex file in Intel format |
| 5 | +# Looks for a MAC address (strictly 6 consecutive bytes) of a known pattern |
| 6 | + |
| 7 | +# Changes that MAC address as required and outputs a new Net.hex file of |
| 8 | +# "NetXXXXXXXXXXXX.hex" where XX..XX is the new MAC in hex digits |
| 9 | + |
| 10 | +# Avoids recompile to allows rapid production of many devices |
| 11 | + |
| 12 | +# Fails if the 6-byte sequence is not unique in the file, or not in the file at all. |
| 13 | + |
| 14 | +import sys |
| 15 | +import re |
| 16 | + |
| 17 | +NEW_MAC="AABBCCDDEEFF" # Hex digits (6 bytes) |
| 18 | +OLD_MAC="324050607080" |
| 19 | + |
| 20 | +with open("Net.hex") as f: |
| 21 | + lines=f.readlines() |
| 22 | + |
| 23 | +lineStarts={} |
| 24 | +code="" |
| 25 | +newFile=[] |
| 26 | +for n,line in enumerate(lines): |
| 27 | + newFile.append(line.rstrip()) |
| 28 | + if (line[0]!=":"): continue |
| 29 | + recLen=int(line[1:3],16) |
| 30 | + if (line[7:9]!="00"): continue |
| 31 | + |
| 32 | + if (len(line.rstrip())!=(9+2*(recLen+1))): continue |
| 33 | + |
| 34 | + lineStarts[len(code)]=n |
| 35 | + code+=line[9:9+recLen*2] |
| 36 | + |
| 37 | +allMACs=re.findall(OLD_MAC,code) |
| 38 | + |
| 39 | +print (allMACs) |
| 40 | + |
| 41 | +if (len(allMACs)>1): |
| 42 | + print ("MAC address not unique in file. Exiting") |
| 43 | + sys.exit(0) |
| 44 | + |
| 45 | +location=code.find(OLD_MAC) |
| 46 | + |
| 47 | +if (location<0): |
| 48 | + print ("Old MAC address not detected. Exiting") |
| 49 | + sys.exit(0) |
| 50 | + |
| 51 | +print (location) |
| 52 | +offset=0 |
| 53 | +while (not (location-offset) in lineStarts): |
| 54 | + offset+=2 |
| 55 | + |
| 56 | +sourceLine=location-offset |
| 57 | +source=newFile[lineStarts[sourceLine]] |
| 58 | +CS=int(source[-2:],16) |
| 59 | +replacement=source[0:9+offset] |
| 60 | +copyOffset=0 |
| 61 | + |
| 62 | +for i in range(6): |
| 63 | + if (9+offset+2*i+2>=len(source)): # move onto next line |
| 64 | + newFile[lineStarts[sourceLine]]=replacement+"{:02X}".format(CS&0xFF) |
| 65 | + print ("Replaced",lines[lineStarts[sourceLine]],"with ",newFile[lineStarts[sourceLine]]) |
| 66 | + sourceLine+=int(lines[lineStarts[location-offset]][1:3],16)*2 |
| 67 | + source=newFile[lineStarts[sourceLine]] |
| 68 | + CS=int(source[-2:],16) |
| 69 | + offset=0 |
| 70 | + copyOffset=i |
| 71 | + replacement=source[0:9+offset] |
| 72 | + replacement+=NEW_MAC[i*2:i*2+2] |
| 73 | + delta=int(OLD_MAC[i*2:i*2+2],16)-int(NEW_MAC[i*2:i*2+2],16) |
| 74 | + |
| 75 | + CS+=delta |
| 76 | + if (i==5): |
| 77 | + replacement+=source[9+offset+2*(i-copyOffset)+2:-2] |
| 78 | + newFile[lineStarts[sourceLine]]=replacement+"{:02X}".format(CS&0xFF) |
| 79 | + print ("Replaced",lines[lineStarts[sourceLine]],"with ",newFile[lineStarts[sourceLine]]) |
| 80 | + |
| 81 | +f=open("Net"+NEW_MAC+".hex","w") |
| 82 | +for line in newFile: |
| 83 | + f.write(line+"\n") |
| 84 | +f.close() |
| 85 | +print ("New file created ","Net"+NEW_MAC+".hex") |
| 86 | + |
| 87 | + |
0 commit comments