#!/usr/bin/env python3 # flash1.py : one VBIOS write through a pty. # Answers 'y' only to the confirmation prompt and only if nvflash has already printed both expected versions; any other # prompt gets 'n'. Exit code: nvflash's own code if the write was confirmed, 99 otherwise. import os, pty, re, select, sys, time nv, idx, rom, cur, new = sys.argv[1:6] pid, fd = pty.fork() if pid == 0: os.execv(nv, [nv, "-i", idx, rom]) buf = ""; answered = False; t0 = time.time(); last = "" while True: r, _, _ = select.select([fd], [], [], 1.0) if r: try: data = os.read(fd, 4096) except OSError: break if not data: break s = data.decode("latin1"); buf += s; sys.stdout.write(s); sys.stdout.flush() tail = buf[-400:] q = re.search(r"(Press 'y' to confirm|\(y/n\)|\[y/N\]|to confirm \(any other key to abort\))", tail, re.I) if q and tail != last: last = tail if not answered and cur in buf and new in buf and not re.search(r"mismatch|override|downgrad|different", buf, re.I): os.write(fd, b"y"); answered = True; print("\n[controller] versions ok -> y", flush=True) elif not answered: os.write(fd, b"n"); print("\n[controller] UNEXPECTED PROMPT -> n (abort)", flush=True) # after the confirmation the prompt stays in the tail of the output while progress is printed: press nothing more if time.time() - t0 > 300: print("\n[controller] timeout", flush=True); break _, st = os.waitpid(pid, 0) rc = os.waitstatus_to_exitcode(st) print(f"\n[controller] nvflash rc={rc} answered={answered}") sys.exit(rc if answered else 99)