Add cmd checksum check

This commit is contained in:
ABelliqueux 2021-05-01 14:51:25 +02:00
parent da195bbba5
commit a865750dca
8 changed files with 187 additions and 156 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -110,7 +110,7 @@ char * ptrToChar[8]; // Will hold the name of the file to load.
volatile u_char loadFileID = 0; // Will hold an ID that's unique for each file.
u_char overlayFileID, loadFileIDwas;
u_char overlayFileID = 0, loadFileIDwas = 0;
// Timer for the pad
@ -124,6 +124,8 @@ u_char protocol = 1; // Hypothetical ID number for the pcdrv protocol in
u_char command = LOAD; // We're loading the data here
uint32_t checkSum = 0;
// Prototypes
void init(void);
@ -429,15 +431,12 @@ int main() {
nextpri += sizeof(DR_MODE);
FntPrint("Hello overlay!\n");
#ifndef USECD
//~ u_int cmdChecksum = 8 + ptrChecksum(&load_all_overlays_here) +
//~ 8 + ptrChecksum((u_long *)&loadFileID) +
//~ overlayFileID;
FntPrint("Overlay with id %d loaded at 0x%08x", overlayFileID, &load_all_overlays_here );
FntPrint("Overlay with id %d loaded at 0x%08x\n%x", overlayFileID, &load_all_overlays_here, &overlayFileID );
#endif

Binary file not shown.

View File

@ -25,7 +25,6 @@ import serial
import time
import calendar
import math
import signal
DEBUG = 1
@ -100,12 +99,6 @@ Transfer = 0
sleepTime = 0.08 # Seems like safe minimum
def sig_interrupt_handler(signal, frame):
global Run
Run = False
def setDEBG():
global sleepTime, ser, uniDebugMode
@ -126,6 +119,19 @@ def setDEBG():
uniDebugMode = 1
# stole it here : https://gist.github.com/amakukha/7854a3e910cb5866b53bf4b2af1af968
# TODO : fixit to have the same results as https://github.com/grumpycoders/pcsx-redux/blob/main/src/mips/common/util/djbhash.h
def hash_djb2(s):
hash = 5381
for x in s:
hash = ((( hash << 5) + hash) ^ x) & 0xFFFFFFFF
return hash
def WaitForResponse( expectedAnswer ):
# Get incoming data from the serial port in a rolling buffer
@ -499,31 +505,39 @@ def main(args):
# We're expecting the command with format : 00 01 06 08 xx xx xx xx 08 xx xx xx xx 01 xx xx (16 bytes)
# Rolling buffer
# ~ byte = ser.read(1)
# Make sure byte value is < 128 so that it can be decoded to ascii
# ~ if byte[0] < 128:
# ~ inputBuffer += byte.decode('ascii')
# ~ else:
# ~ inputBuffer += '.'
if len(inputBuffer) >= 32:
if len(inputBuffer) == 38:
if DEBUG:
print( "Incoming data : " + inputBuffer )
# If inputBuffer is > 32, remove first char
# Get the checksum and remove it from the buffer
# ~ if len(inputBuffer) > 32:
CmdCheckSum = inputBuffer[-10:]
# ~ inputBuffer = inputBuffer[1:]
if DEBUG:
print( "Received ChkSm: " + CmdCheckSum )
# Remove checksum from data
inputBuffer = inputBuffer[:28]
dataCheckSum = hash_djb2(bytes(inputBuffer, 'ascii'))
if DEBUG:
print( "Computed ChkSm: " + str(dataCheckSum) )
# Not using the checksum for now
# ~ dataCheckSum = 0
# ~ CmdCheckSum = 0
# Check
if int(dataCheckSum) == int(CmdCheckSum):
# Parse command
@ -557,44 +571,12 @@ def main(args):
inputBuffer = inputBuffer[2:]
# Get 2 checksum bytes in buffer
CmdCheckSum = inputBuffer[-4:]
if DEBUG:
print( "Received ChkSm: " + CmdCheckSum )
# Calculate checksum on the data - 2 checksum bytes
dataInBuffer = inputBuffer[:-4]
dataInBuffer = inputBuffer
if DEBUG:
print( "Data in buffer: " + dataInBuffer )
# TODO find a way to calc command checksum in ovl-upload.c ( see l.440 )
# ~ work = dataInBuffer
# ~ for c in dataInBuffer:
# ~ dataCheckSum += int(c, 16)
# ~ if DEBUG:
# ~ print( "Generated ChkSm: " + str(dataCheckSum) )
# Not using the checksum for now
dataCheckSum = 0
CmdCheckSum = 0
# Check
if int(dataCheckSum) == int(CmdCheckSum):
dataLen = 0
# Get data length byte
@ -657,6 +639,15 @@ def main(args):
Listen = 0
break
else:
print("Command checksum not matching ! Aborting...")
ser.reset_input_buffer()
inputBuffer = ""
break
else:

27
pcdrv.c
View File

@ -1,6 +1,6 @@
#include "pcdrv.h"
int ptrChecksum( u_long * pointer ){
int doChecksum( u_long * pointer ){
u_int checksum = 0;
@ -19,12 +19,25 @@ int ptrChecksum( u_long * pointer ){
};
// Hashing algorythm from @nicolasnoble : https://github.com/grumpycoders/pcsx-redux/blob/main/src/mips/common/util/djbhash.h
static inline uint32_t djbProcess(uint32_t hash, const char str[], unsigned n) {
return n ? djbProcess ( ( ( hash << 5 ) + hash ) ^ str[0], str + 1, n - 1) : hash;
}
static inline uint32_t djbHash( const char* str, unsigned n ){
return djbProcess( 5381, str, n);
};
int waitForSIODone( int * flag ){
// This should wait for a signal from the SIO to tell when it's done
// Returns val < 0 if wrong
uint timeOut = 1000;
uint16_t timeOut = 1000;
char result = 0;
@ -51,8 +64,14 @@ void PCload( u_long * loadAddress, volatile u_char * flagAddress, u_char overlay
u_char protocol = 1; // Hypothetical protocol indicator for unirom
u_char cmdChecksum = 0; // Not using that yet, ideally, should allow to check that the received command on the pc is the same as the one sent by the psx
//~ u_char cmdChecksum = 0; // Not using that yet, ideally, should allow to check that the received command on the pc is the same as the one sent by the psx
printf("%02u%02u%02u08%08x08%08x%02u%04u", escape, protocol, LOAD, loadAddress, flagAddress, overlayFileID, cmdChecksum);
char commandBuffer[28];
sprintf(commandBuffer, "%02u%02u%02u08%08x08%08x%02u", escape, protocol, LOAD, loadAddress, flagAddress, overlayFileID);
u_int cmdChecksum = djbHash(commandBuffer, 28);
printf("%s%10u", commandBuffer, cmdChecksum);
};

28
pcdrv.h
View File

@ -1,7 +1,24 @@
// https://discord.com/channels/642647820683444236/646765703143227394/836663602718048297
// 28-04-2021 - @nicolasnoble:
//
// step 0 : have a protocol that can handle open / close / seek / read / write
//
// step 1 : have an unhandled exception handler installed in the kernel to capture the pcdrv functions; that sounds scary, but it's extremely straightforward (I think @sickle currently has one for his debugger)
//
// step 1.5: implement the pcopen / pcclose / pcread / pcwrite / pcseek functions, which are basically the same model as my syscalls.h file
//
// step 2 : implement the unhandler exception handler in a way that properly redirects the calls to the step 0 protocol, and returns gracefully to the caller when done - that can be a bit tricky, but it's totally doable
// this step requires understanding the ReturnFromException mechanism basically which, to be fair, is sort of understandable from this one file:
// https://github.com/grumpycoders/pcsx-redux/blob/main/src/mips/openbios/handlers/syscall.c
// you can see the syscall_unresolvedException() call at the bottom , this just needs to follow the same pattern
// and break down the caller, modify the current thread's registers, and return to the caller
// at this point, we have a working basic pcdrv feature, using pcopen / pcclose / pcread / pcwrite / pcseek
//
// step 3 : add a kernel driver for "pcdrv:" that just piggy backs on pc* functions so that people can simply do a int file = open("pcdrv:blah.txt", O_RDONLY);
// (and thus support things like CSOTN technically)
//
// https://discord.com/channels/642647820683444236/646765703143227394/837416216640618558
// So like we'd have, say,
// 00 01 00 03 58 58 58 01 xx for pcdrv' file open to open the file name "XXX" and attribute 1
// (escape) (pcdrv command) (pcdrv open) (filename with length as prefix) (attributes) (checksum)
@ -10,6 +27,7 @@
#pragma once
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
@ -23,7 +41,11 @@
#define CREATE 5
#define LOAD 6
int ptrChecksum( u_long * pointer );
static inline uint32_t djbHash( const char* str, unsigned n );
static inline uint32_t djbProcess(uint32_t hash, const char str[], unsigned n);
int doChecksum( u_long * pointer );
int waitForSIODone( int * flag );