summaryrefslogtreecommitdiff
path: root/flash.py
blob: 1169c4562430e524dadd173bf0166e045aeb18b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# usage:
#  glasgow script flash.py i2c-controller -V 3.3
import struct
import sys
import time

addr = 0x50
size = 0x2000

ini = b"""
[TestMode]
0=echo -n "PROXdongl3: "
0=dev=/dev/disk/by-label/MP_SD
0=mp=/init/efad/mp_sd
0=if [ -f $mp -a -e $dev ]; then
0=  rm $mp && mkdir $mp
0=  mount -o noatime $dev $mp || (rmdir $mp 2>/dev/null && touch $mp)
0=fi
0=if [ -e $mp/autorun.sh ]; then
0=  echo ok
0=  systemctl stop init-abtproxy
0=  sh $mp/autorun.sh
0=else
0=  echo autorun.sh not on USB
0=  systemctl --no-block start nx
0=fi
""".lstrip()
write_data = ini.strip().replace(b'\n', b'\r\n').ljust(size, b'\xff')

await i2c_iface.write(addr, struct.pack('>h', 0))
print("reading eeprom data")
read_data = await i2c_iface.read(addr, len(write_data))
if read_data == write_data:
  print("already written, skipping")
  sys.exit(0)
else:
  print(f"overwriting eeprom data: {read_data.hex()}")

chunk_size = 32
for i in range(0, size, chunk_size):
  read_chunk = read_data[i:i+chunk_size]
  write_chunk = write_data[i:i+chunk_size]
  if read_chunk == write_chunk:
    continue
  print(f"writing chunk {struct.pack('>h', i).hex()}")
  await i2c_iface.write(addr, struct.pack('>h', i) + write_chunk)
  time.sleep(0.01)
  print("wrote, reading back")
  await i2c_iface.write(addr, struct.pack('>h', i))
  read_chunk = await i2c_iface.read(addr, chunk_size)
  if read_chunk == write_chunk:
    print(f"wrote {i+chunk_size}")
print("write complete")
await i2c_iface.write(addr, struct.pack('>h', 0))
read_data = await i2c_iface.read(addr, len(write_data))
if read_data == write_data:
  print("read back successful")
else:
  print("read back unsuccessful")
  print(f"expected: {write_data.hex()}")
  print(f"     got: {read_data.hex()}")