-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathepsflasher
executable file
·138 lines (105 loc) · 3.7 KB
/
epsflasher
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
import dirty4
import argparse
import rcx
import sys
import os
import time
import logging
# optional, non-core modules
try:
import tqdm
except:
pass
try:
import colorlog
except:
colorlog = logging
parser = argparse.ArgumentParser(description='Flash a firmware to an Epson printer.')
parser.add_argument('filename', metavar='firmware.rcx',
help='file to flash (RCX format)')
parser.add_argument('--device', default='/dev/usb/lp0',
help='printer to flash')
parser.add_argument('-d', '--debug', action='store_true',
help='output lots of debug spam')
parser.add_argument('--only-first-block', action='store_true',
help='only write the first firmware block; useful when the printer is in recovery mode')
args = parser.parse_args()
log_level = logging.INFO
if args.debug:
log_level = logging.DEBUG
logformat = '%(levelname)-8s %(message)s'
if colorlog != logging:
logformat = '%(log_color)s' + logformat
colorlog.basicConfig(level=log_level, format=logformat)
rcx_content = open(args.filename, 'rb').read()
header_len, cfg = rcx.parse_rcx(rcx_content)
block_sizes = []
for name, section in cfg.items():
if name.startswith('Z_'):
block_sizes.append(int(section['5']))
firmware_blob = rcx_content[header_len:]
if len(firmware_blob) != sum(block_sizes):
logging.error("Firmware blob is %d bytes long but the header suggests %d" % (len(firmware_blob), sum(block_sizes)))
sys.exit(1)
if args.only_first_block:
firmware_blob = firmware_blob[:block_sizes[0]]
def strip_pref_suff(string, prefix, suffix=';\f'):
if not string.startswith(prefix):
logging.error("Unexpected response %s does not start with %s" % (repr(string), repr(prefix)))
sys.exit(1)
if not string.endswith(suffix):
logging.error("Unexpected response %s does not end with %s" % (repr(string), repr(suffix)))
sys.exit(1)
return string[len(prefix):-len(suffix)]
port = os.open(args.device, os.O_RDWR | os.O_SYNC)
d4 = dirty4.Dirty4(port)
with d4.channel("EPSON-CTRL") as chan:
version_info = strip_pref_suff(chan.cmd2('vi', 4), 'vi:04:')
logging.info("Original firmware version: '%s'" % version_info)
strip_pref_suff(chan.cmd2('fl', 1), 'fl:')
gm_stat = strip_pref_suff(chan.cmd2('gm', 1), 'fl:01:')
if gm_stat != 'OK':
logging.error("Entering firmware download mode returned status '%s'" % gm_stat)
sys.exit(1)
logging.info("Writing firmware...")
progress = None
try:
bar = tqdm.tqdm(total=len(firmware_blob), unit='byte', unit_scale=True, unit_divisor=1024)
def progress(increment):
bar.update(increment)
except:
pass
with d4.channel("EPSON-DATA") as chan:
chan.write(firmware_blob, progress=progress)
if progress:
bar.close()
with d4.channel("EPSON-CTRL") as chan:
gm_stat = strip_pref_suff(chan.cmd2('gm', 3), 'fl:03:')
if gm_stat != 'OK':
logging.warning("Leaving firmware download mode returned status '%s'" % gm_stat)
sys.exit(1)
logging.info("Waiting for printer to go away...")
try:
while True:
os.read(port, 1024)
time.sleep(0.1)
except OSError:
pass
os.close(port)
logging.info("Waiting for printer to come back...")
while True:
try:
port = os.open(args.device, os.O_RDWR | os.O_SYNC)
break
except OSError:
time.sleep(1)
d4 = dirty4.Dirty4(port)
logging.info("Waiting for printer to be ready...")
with d4.channel("EPSON-CTRL") as chan:
while True:
version_info = strip_pref_suff(chan.cmd2('vi', 4), 'vi:04:')
if version_info != 'BUSY':
break
time.sleep(1)
logging.info("OK. New firmware version: '%s'" % version_info)