-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovl-upload.py
executable file
·633 lines (331 loc) · 16.1 KB
/
ovl-upload.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#!/usr/bin/env python
# ovl-upload.c, by ABelliqueux, 04-2021 license GNU General Public License v3.0
# This working version corrected with the help of sickle :
# https://discord.com/channels/642647820683444236/663664210525290507/836029253593858060
# Corrected script by Sickle : http://psx.arthus.net/code/rawdog.py
# Sickle - 26/04/2021 :
# " Ooh, you were like frustratingly close dude! Few tiny issues:
# - first of your 3 rolling buffers was bugged (other 2 were spot on)
# - waiting too long between commands at points, unirom timed out
# - var i was missing the i += chunkSize so we were stuck in a loop there (e.g. tried to send a second chonk)
# - exit was gummed up with the main logic being in a while True: "
# As suggested:
# - Removed while True: loop
# - moved rolling buffer loops to WaitForResponse()
# - reduced sleeps
# - inc var i with chunkSize
import sys
import os
import serial
import time
import calendar
import math
import signal
DEBUG = 0
SERIAL_DEVICE = '/dev/ttyUSB0'
SERIAL_SPEED = 115200
# Names of the overlay files to load.
# See l.550
overlayFile0 = "Overlay.ovl0"
overlayFile1 = "Overlay.ovl1"
# Serial connection setup
ser = serial.Serial(SERIAL_DEVICE)
# Unirom can do 115200 and 510000 ( https://github.com/JonathanDotCel/NOTPSXSerial/blob/bce29e87cb858769fe60eb34d8eb123f9f36c8db/NOTPSXSERIAL.CS#L842 )
ser.baudrate = SERIAL_SPEED
# Working directory
cwd = os.getcwd()
dataFolder = cwd + os.sep
# Should we listen for commands on serial ?
Listen = 1
# Will be set once unirom is in debug mode
uniDebugMode = 0
# Will hold the commands received from the psx
Command = ""
memAddr = ""
flagAddr = ""
loadFile = ""
levelId = ""
# One byte
#uno = int(1).to_bytes(1, byteorder='little', signed=False)
data = 0
# checkSum is the checkSum for the full data
checkSum = 0
# If set, it means the data transfer has been initiated
Transfer = 0
# Delay between write operations. These seem to be needed for the connection not to hang.
sleepTime = 0.08 # Seems like safe minimum
def sig_interrupt_handler(signal, frame):
global Run
Run = False
def setDEBG():
global sleepTime, ser, uniDebugMode
if DEBUG:
print("Sending DEBG command...")
ser.write( bytes( 'DEBG' , 'ascii' ) )
time.sleep(sleepTime)
# Empty in waiting buffer
ser.reset_input_buffer()
time.sleep(sleepTime)
uniDebugMode = 1
def WaitForResponse( expectedAnswer ):
# Get incoming data from the serial port in a rolling buffer
# when the content of the buffer corresponds to 'expectedAnswer', returns True
global DEBUG
responseBuffer = ""
success = False
while True:
if DEBUG > 1:
print("Waiting for data in serial input buffer.")
# If data in serial's incoming buffer
if ser.in_waiting:
if DEBUG > 1:
print("Brace yourself, data is coming...")
# Read 1 byte
byteValue = ser.read(1)
# Make sure byte value is < 128 so that it can be decoded to ascii
if byteValue[0] < 128:
responseBuffer += byteValue.decode('ascii')
else:
responseBuffer += '.'
# Always keep response buffer 4 chars long
if len( responseBuffer ) > 4:
# Remove first char in buffer
responseBuffer = responseBuffer[1:]
# If response is ERR!, checksum check does not check, so check it again
if responseBuffer == "ERR!":
if DEBUG > 1:
print("Checksum error !")
success = False
break
# When expected response shows up, break from the while loop
if responseBuffer == expectedAnswer:
success = True
break
if DEBUG > 1:
print( "Got : " + responseBuffer )
responseBuffer = ""
return success
def CalculateChecksum( inBytes, skipFirstSector = False):
returnVal = 0;
i = 0
if skipFirstSector:
i = 2048
while i < len( inBytes ):
returnVal += inBytes[i];
i += 1
return returnVal;
def WriteBytes( inData ):
if DEBUG:
print("Preparing to write bytes...")
# The data needs to be split in 2K chunks
chunkSize = 2048
# BEGIN WHILE DATA
i = 0
while i < len( inData ):
# BEGIN WHILE TRUE
while True:
# BEGIN TRY/EXCEPT
try:
# Calculate number of 2K chunks we're about to send
numChunk = math.ceil( len( inData ) / chunkSize )
# Calculate current chunk
currentChunk = math.ceil( (i + 1) / chunkSize)
if DEBUG:
print( str ( numChunk + 1 - currentChunk ) + " chunks of " + str ( chunkSize) + " bytes to send " )
# Avoid going out of range
if ( i + chunkSize ) > len( inData ):
chunkSize = len( inData ) - i
print("Writing chunk " + str( currentChunk ) + " of " + str( numChunk ) )
# ~ ser.write(inData)
chunkChecksum = 0
# Send inData in 2048B chunks
for byte in range( chunkSize ):
# Send byte
if DEBUG > 1:
print("Writing " + str( inData[ i + byte ].to_bytes(1, byteorder='little', signed=False) ) + " to serial..." )
ser.write( inData[ i + byte ].to_bytes(1, byteorder='little', signed=False) )
# Calculate chunk checksum
chunkChecksum += inData[ i + byte ]
time.sleep(sleepTime)
if DEBUG:
print( "Chunk cheksum : " + str( chunkChecksum ) )
# Wait for output buffer to be empty
# REMOVE ? Is this needed ?
while ser.out_waiting:
print("*")
wait += 1
time.sleep(sleepTime)
# Wait for unirom to request the checksum
if DEBUG > 1:
print( "Chunk " + str( currentChunk ) + " waiting for unirom to request checksum (CHEK)..." )
WaitForResponse( "CHEK" )
# Send checksum
if DEBUG:
print( "Sending checksum to unirom..." );
# ~ chunkChecksum = 170
bytesChunkChecksum = chunkChecksum.to_bytes( 4, byteorder='little', signed = False )
ser.write( bytesChunkChecksum )
# ~ time.sleep( sleepTime )
if DEBUG > 1:
print( "Waiting for unirom to request more data (MORE)..." )
# Wait for unirom to request MORE inData ( next chunk )
if not WaitForResponse("MORE"):
if DEBUG:
print("ERROR ! Retrying...")
raise Exception()
if DEBUG:
print( str( currentChunk ) + " chunk sent with correct checksum.")
# Increment i from chunkSize
i += chunkSize
except Exception:
continue
# END TRY/EXCEPT
break
# END WHILE TRUE
numChunk = 0
# END WHILE DATA
def SendBin( inData, memAddr ):
global sleepTime
dataSize = len( inData )
if DEBUG:
print("Data size : " + str( dataSize ) )
# Prepare unirom for data reception - sent "SBIN" - received : "OKV2"
if DEBUG > 1:
print("Sending SBIN command...")
ser.write( bytes( 'SBIN' , 'ascii' ) )
time.sleep(sleepTime)
# We're using unirom in debug mode, which means protocol version 2 is available
# Upgrade protocol - sent "UPV2" - received : "OKAY"
ser.write( bytes( 'UPV2' , 'ascii' ) )
time.sleep(sleepTime)
# Initialisation done, set flag
# ~ Init = 1
# From now on, we're using the rolling buffer
if DEBUG > 1:
print("Waiting for OKAY...")
WaitForResponse("OKAY")
# Calculate data checkSum
checkSum = CalculateChecksum( inData )
if DEBUG :
print("Data checkSum : " + str(checkSum) )
# Send memory address to load data to, size of data and checkSum
# Unirom expects unsigned longs ( 32bits ), byte endianness little
# Convert address from string to integer, then to ulong 32b
bytesAddr = int( memAddr, 16 ).to_bytes( 4, byteorder='little', signed=False )
# Write address to serial
ser.write( bytesAddr )
time.sleep(sleepTime)
# Convert and write int size to serial
bytesSize = dataSize.to_bytes( 4, byteorder='little', signed = False )
ser.write( bytesSize )
time.sleep(sleepTime)
# Convert and write int chekSum to serial
bytesChk = checkSum.to_bytes( 4, byteorder='little', signed = False )
ser.write( bytesChk )
time.sleep(sleepTime)
# Send dat data
WriteBytes( inData )
def resetListener():
global checkSum, data, Listen, Transfer, dataSize, memAddr, loadFile, flagAddr, levelId
memAddr = ""
flagAddr = ""
loadFile = ""
checkSum = 0
data = 0
dataSize = 0
Transfer = 0
levelId = 0
Listen = 1
ser.reset_input_buffer()
ser.reset_output_buffer()
def main(args):
while True:
global checkSum, data, Listen, Transfer, dataSize, memAddr, loadFile, flagAddr, levelId
# Flush serial buffers to avoid residual data
ser.reset_input_buffer()
ser.reset_output_buffer()
inputBuffer = ""
# Listen to incomming connections on serial
if Listen:
print("Listening for incoming data...")
if DEBUG > 1:
print("memAddr : " + str(memAddr) + " - loadFile" + loadFile )
while True:
# If data on serial, fill buffer
while ser.in_waiting:
inputBuffer += ser.read().decode('ascii')
if inputBuffer:
if DEBUG:
print( "Incoming data : " + inputBuffer )
# parse command CMD:ARG1:ARG2(:ARGn)
argList = []
argList = inputBuffer.split(':')
# Send command
if argList[0] == "load" and len(argList) == 4:
if len(argList[1]) < 8 or len(argList[2]) < 8:
if DEBUG:
print("Wrong data format, aborting...")
break
memAddr = argList[1]
flagAddr = argList[2]
loadFile = argList[3]
ser.reset_input_buffer()
inputBuffer = ""
if DEBUG > 1:
print( memAddr + " - " + flagAddr + " - " + loadFile )
Listen = 0
break
else:
ser.reset_input_buffer()
inputBuffer = ""
break
if memAddr and loadFile:
# Remove separator and ';1' at end of the string
# ~ fileClean = loadFile.split(';')[0][1:]
fileID = loadFile
print("Received addresses and file ID : " + memAddr + " - " + flagAddr + " - " + fileID)
# TODO : replace with a proper level naming scheme
# right now, we're receiving currently loaded file
# so we have to switch manually here.
binFileName = ""
if fileID == "0":
binFileName = overlayFile1
levelId = 1
if fileID == "1":
binFileName = overlayFile0
levelId = 0
if DEBUG:
print(
"Load Data to : " + memAddr + "\n" +
"Reset flag at: " + flagAddr + "\n" +
"File : " + loadFile + "\n" +
"Bin : " + binFileName + " - ID : " + str(levelId)
)
# Open file as binary if bin filename is defined
if binFileName:
binFile = open( dataFolder + binFileName, 'rb' )
data = binFile.read()
Transfer = 1
else:
print(" No filename provided, doing nothing ")
resetListener()
# If Init was set, initialize transfer and send data
if Transfer:
print("Initializing data transfer...")
if not uniDebugMode:
# Set unirom to debugmode - sent : "DEBG" - received : "DEBGOKAY"
setDEBG()
# Send level data
SendBin( data, memAddr )
# Set level changed flag
if DEBUG:
print("Sending value " + str( levelId.to_bytes(1, byteorder='little', signed=False) ) + " to " + flagAddr )
time.sleep( sleepTime )
SendBin( levelId.to_bytes(1, byteorder='little', signed=False) , flagAddr)
# Reset everything
resetListener()
print("DONE!")
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))