-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
usbeval.py
147 lines (127 loc) · 4.14 KB
/
usbeval.py
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
139
140
141
142
143
144
145
146
147
if (len(argv) < 2):
print('usbeval.py')
print('Evaluates a Lua script on a Playdate device, via USB')
print('Requires pdc from the Playdate SDK as well as the pyusb library')
print('Usage:')
print('python3 usbeval.py ./input.lua')
exit()
import tempfile
import subprocess
import usb.core
import usb.util
from sys import argv
from pathlib import Path
from struct import unpack
from zlib import decompress
from time import sleep
# Playdate USB vendor and product IDs
PLAYDATE_VID = 0x1331;
PLAYDATE_PID = 0x5740;
IN_SIZE = 64;
def pdz_extract_entry(data, entry):
ptr = 0x10
while ptr < len(data):
flags = data[ptr]
is_compressed = (flags >> 7) & 0x1
innerlen = data[ptr + 1] | (data[ptr + 2] << 8)
filename = data[ptr + 4 : data.find(b"\0", ptr + 4)]
outerheadersize = 4 + len(filename) + 1
outerheadersize = ((ptr + outerheadersize + 3) & ~3) - ptr
zlibdata = data[ptr + outerheadersize + 4: ptr + outerheadersize + innerlen]
if filename.decode('utf-8') == entry:
return decompress(zlibdata)
ptr += outerheadersize + innerlen
return None
def usb_connect():
# find our playdate device
device = usb.core.find(idVendor=PLAYDATE_VID, idProduct=PLAYDATE_PID)
if device is None:
raise ValueError('Device not found')
# set the active configuration. With no arguments, the first
# configuration will be the active one
device.set_configuration()
# get an endpoint instance
cfg = device.get_active_configuration()
intf = cfg[(1,0)]
epOut = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
epIn = usb.util.find_descriptor(
intf,
# match the first IN endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN)
assert epOut is not None
assert epIn is not None
device.reset()
return epOut, epIn
def usb_read_bytes(endPoint):
res = bytearray()
has_started = False
while True:
try:
b = bytearray(epIn.read(IN_SIZE))
res += b
if b != b'': has_started = True
if has_started and b == b'': break
except usb.core.USBTimeoutError:
break
return res
with tempfile.NamedTemporaryFile(prefix='main', suffix='.lua') as luafile, tempfile.TemporaryDirectory(suffix='.pdx') as pdxdir:
# copy lua file
print('reading input file')
with open(argv[1], 'rb') as infile:
luafile.write(infile.read())
luafile.seek(0)
# compile lua with pdc
print('compiling lua with pdc')
subprocess.run(['pdc', luafile.name, pdxdir])
luastem = Path(luafile.name).stem
# extract lua bytecode from pdz
print('extracting lua bytecode')
with open(Path(pdxdir, luastem + '.pdz'), 'rb') as pdzfile:
pdz = pdzfile.read()
bytecode = pdz_extract_entry(pdz, luastem)
# connect to playdate over usb
print('finding playdate connected to usb...')
epOut, epIn = usb_connect()
print('successfully connected to playdate!')
# set usb echo mode to off
print('setting usb echo to off')
epOut.write('echo off\n')
resp = usb_read_bytes(epIn)
# get version info (to test things work, but also looks cool :^))
# print('playdate version info:')
# epOut.write('version\n')
# resp = usb_read_bytes(epIn)
# print(resp.decode("utf-8").strip())
# consume printed console content until there's nothing new
print('clearing current console data')
epOut.write('eval\n')
sleep(.2)
usb_read_bytes(epIn)
# send lua bytecode to the device
print('sending payload for device to eval...')
header = b'eval %d\n' % len(bytecode)
payload = header + bytecode
epOut.write(payload)
sleep(.2) # payload seems to take a bit to execute, you may need to adjust this if you have a big payload
resp = usb_read_bytes(epIn)
print('===============')
print('console output:')
print(resp.decode("utf-8").strip())
# keep polling for new console output
while True:
try:
sleep(.1)
resp = usb_read_bytes(epIn)
text = resp.decode("utf-8").strip()
if text: print(text)
except KeyboardInterrupt:
break