Skip to content

Commit

Permalink
Added patch binary support and updated protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Lameguy64 committed Nov 18, 2018
1 parent 6a5eec2 commit 524b6eb
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 199 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nbproject/private
build
dist
4 changes: 2 additions & 2 deletions SerialClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SerialClass::~SerialClass() {

}

SerialClass::ErrorType SerialClass::OpenPort(const char* name) {
SerialClass::ErrorType SerialClass::OpenPort(const char* name, int rate) {

#ifdef __WIN32__
if ( hComm != INVALID_HANDLE_VALUE ) {
Expand All @@ -71,7 +71,7 @@ SerialClass::ErrorType SerialClass::OpenPort(const char* name) {
return ERROR_CONFIG;
}

dcbSerialParams.BaudRate = CBR_9600; // Setting BaudRate = 9600
dcbSerialParams.BaudRate = rate; // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
Expand Down
2 changes: 1 addition & 1 deletion SerialClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SerialClass {
ERROR_WRITE_FAIL,
};

ErrorType OpenPort(const char* name);
ErrorType OpenPort(const char* name, int rate);
ErrorType SetRate(int rate);
void ClosePort();

Expand Down
2 changes: 2 additions & 0 deletions SiofsClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ void SiofsClass::FsWrite() {

}

std::cout << "FS: Wrote " << info.length << " bytes." << std::endl;

ret = fwrite(buffer, 1, info.length, handles[info.fd]);
free(buffer);

Expand Down
82 changes: 65 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "SerialClass.h"
#include "SiofsClass.h"

#define VERSION "0.80"
#define VERSION "0.82"

#ifdef __WIN32__
#define SERIAL_DEFAULT "COM1"
Expand All @@ -34,9 +34,12 @@ int serial_baud = 115200;

std::string psexe_file;
std::string bin_file;
std::string pat_file;
unsigned int bin_addr;

int old_protocol = false;
int terminal_mode = false;
int no_console = false;
int hex_mode = false;
extern int fs_messages;

Expand Down Expand Up @@ -116,6 +119,7 @@ typedef struct {
typedef struct {
EXEC params;
unsigned int crc32;
unsigned int flags;
} EXEPARAM;

typedef struct {
Expand Down Expand Up @@ -314,8 +318,8 @@ int uploadEXE(const char* exefile, SerialClass* serial) {
fclose(fp);

param.crc32 = crc32(buffer, param.params.t_size, CRC32_REMAINDER);
param.flags = 0;

serial->SetRate(115200);
serial->SendBytes((void*)"MEXE", 4);

char reply[4];
Expand All @@ -336,7 +340,11 @@ int uploadEXE(const char* exefile, SerialClass* serial) {
return -1;
}

serial->SendBytes(&param, sizeof(EXEPARAM));
if( !old_protocol ) {
serial->SendBytes(&param, sizeof(EXEPARAM));
} else {
serial->SendBytes(&param, sizeof(EXEPARAM)-4);
}
Sleep(20);
serial->SendBytes(buffer, param.params.t_size);

Expand All @@ -346,7 +354,7 @@ int uploadEXE(const char* exefile, SerialClass* serial) {

}

int uploadBIN(const char* file, unsigned int addr, SerialClass* serial) {
int uploadBIN(const char* file, unsigned int addr, SerialClass* serial, int patch) {

BINPARAM param;
char* buffer;
Expand All @@ -373,8 +381,11 @@ int uploadBIN(const char* file, unsigned int addr, SerialClass* serial) {

fclose(fp);

serial->SetRate(115200);
serial->SendBytes((void*)"MBIN", 4);
if( patch ) {
serial->SendBytes((void*)"MPAT", 4);
} else {
serial->SendBytes((void*)"MBIN", 4);
}

char reply[4];
int timeout = true;
Expand Down Expand Up @@ -418,7 +429,7 @@ int main(int argc, char** argv) {

if ( argc >= 2 ) {

if ( strcmp( "-h", argv[1] ) == 0 ) {
if( ( strcmp( "-h", argv[1] ) == 0 ) || ( strcmp( "-?", argv[1] ) == 0 ) ) {

std::cout << "Usage:" << std::endl;
std::cout << " mcomms [-dev <device>] [-baud <rate>] [-dir <path>] [-term] [-fsmsg]" <<
Expand All @@ -432,10 +443,13 @@ int main(int argc, char** argv) {
std::cout << " -term - Enable terminal mode (forward keystrokes to serial)." << std::endl;
std::cout << " -hex - Output received data in hex." << std::endl;
#endif
std::cout << " -fsmsg - Enable SIOFS messages." << std::endl << std::endl;
std::cout << " -fsmsg - Enable SIOFS messages." << std::endl;
std::cout << " -nocons - Quit immediately, no console mode." << std::endl;
std::cout << " -old - Use old LITELOAD 1.0 protocol." << std::endl << std::endl;

std::cout << " LITELOAD Parameters (catflap inspired):" << std::endl;
std::cout << " up <file> <addr> - Upload a file to specified address." << std::endl;
std::cout << " patch <file> - Upload a patch binary." << std::endl;
std::cout << " run <file> - Upload a PS-EXE file (CPE format is supported)." << std::endl << std::endl;
std::cout << " Specified memory address must be in hex." << std::endl << std::endl;

Expand Down Expand Up @@ -499,6 +513,14 @@ int main(int argc, char** argv) {
hex_mode = true;

#endif
} else if ( strcmp("-nocons", argv[i]) == 0 ) {

no_console = true;

} else if ( strcmp("-old", argv[i]) == 0 ) {

old_protocol = true;

} else if ( strcmp("-fsmsg", argv[i]) == 0 ) {

fs_messages = true;
Expand All @@ -513,6 +535,16 @@ int main(int argc, char** argv) {
psexe_file = argv[i];
break;

} else if ( strcmp("patch", argv[i]) == 0 ) {

i++;
if ( i >= argc ) {
std::cout << "Missing filename parameter." << std::endl;
return EXIT_FAILURE;
}
pat_file = argv[i];
break;

} else if ( strcmp("up", argv[i]) == 0 ) {

i++;
Expand All @@ -538,7 +570,7 @@ int main(int argc, char** argv) {

}

switch( serial.OpenPort(serial_device.c_str()) ) {
switch( serial.OpenPort(serial_device.c_str(), serial_baud) ) {
case SerialClass::ERROR_OPENING:
std::cout << "ERROR: Unable to open " << serial_device << "." << std::endl;
return EXIT_FAILURE;
Expand All @@ -547,35 +579,51 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

// Upload patch data
if( !pat_file.empty() ) {

std::cout << "Uploading patch file..." << std::endl;

if( uploadBIN(pat_file.c_str(), 0, &serial, 1) < 0 ) {
serial.ClosePort();
return EXIT_FAILURE;
}

serial.ClosePort();
return EXIT_SUCCESS;

}

// Upload binary file if specified
if ( !bin_file.empty() ) {
if( !bin_file.empty() ) {

std::cout << "Uploading binary file..." << std::endl;

if ( uploadBIN(bin_file.c_str(), bin_addr, &serial) < 0 ) {
if( uploadBIN(bin_file.c_str(), bin_addr, &serial, 0) < 0 ) {
serial.ClosePort();
return EXIT_FAILURE;
}

serial.ClosePort();
return EXIT_SUCCESS;

}

// Upload executable if specified
if ( !psexe_file.empty() ) {
if( !psexe_file.empty() ) {

std::cout << "Uploading executable..." << std::endl;

if ( uploadEXE(psexe_file.c_str(), &serial) < 0 ) {
serial.ClosePort();
return EXIT_FAILURE;
}

}


// Set serial speed
if ( serial.SetRate(serial_baud) != SerialClass::OK ) {
std::cout << "ERROR: Unsupported baud rate specified for " << serial_device << "." << std::endl;
return EXIT_FAILURE;
if( no_console ) {
serial.ClosePort();
return EXIT_SUCCESS;
}

std::cout << "Listening " << serial_device << " at " << serial_baud << " baud." << std::endl;
Expand Down
8 changes: 0 additions & 8 deletions nbproject/private/Makefile-variables.mk

This file was deleted.

115 changes: 0 additions & 115 deletions nbproject/private/configurations.xml

This file was deleted.

Loading

0 comments on commit 524b6eb

Please sign in to comment.