From: ljsebald <ljs...@us...> - 2023-06-06 01:56:05
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "An ethernet program loader for the Dreamcast.". The branch, master has been updated via ad916bc823652e2d361f12ec5b35611d821d57e4 (commit) from d1a1441eb7f08b3a3ae30eb50fc121585c6fcfc5 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit ad916bc823652e2d361f12ec5b35611d821d57e4 Author: Andress Barajas <and...@gm...> Date: Mon Jun 5 18:55:29 2023 -0700 #2 Fix options parser and gdb (#3) ----------------------------------------------------------------------- Summary of changes: host-src/tool/dc-tool.c | 811 ++++++++++++++++++++++--------------------- host-src/tool/syscalls.c | 142 ++++---- make-cd/Makefile | 6 +- target-src/1st_read/Makefile | 2 +- 4 files changed, 496 insertions(+), 465 deletions(-) diff --git a/host-src/tool/dc-tool.c b/host-src/tool/dc-tool.c index 505506e..9f5c38c 100644 --- a/host-src/tool/dc-tool.c +++ b/host-src/tool/dc-tool.c @@ -19,7 +19,7 @@ * */ -#include "config.h" +#include "config.h" // needed for newer BFD library #ifdef WITH_BFD #include <bfd.h> @@ -127,90 +127,123 @@ char *__progname=PACKAGE; */ int getopt(int nargc, char * const *nargv, const char *ostr) { - extern char *__progname; - static char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ - int ret; - - if (optreset || !*place) { /* update scanning pointer */ - optreset = 0; - if (optind >= nargc || *(place = nargv[optind]) != '-') { - place = EMSG; - return (-1); - } - if (place[1] && *++place == '-') { /* found "--" */ - ++optind; - place = EMSG; - return (-1); - } - } /* option letter okay? */ - if ((optopt = (int)*place++) == (int)':' || - !(oli = strchr(ostr, optopt))) { - /* - * if the user didn't specify '-' as an option, - * assume it means -1. - */ - if (optopt == (int)'-') - return (-1); - if (!*place) - ++optind; - if (opterr && *ostr != ':') - (void)fprintf(stderr, - "%s: illegal option -- %c\n", __progname, optopt); - return (BADCH); - } - if (*++oli != ':') { /* don't need argument */ - optarg = NULL; - if (!*place) - ++optind; + extern char *__progname; + static char *place = EMSG; /* option letter processing */ + char *oli; /* option letter list index */ + int ret; + + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') { + place = EMSG; + return (-1); } - else { /* need an argument */ - if (*place) /* no white space */ - optarg = place; - else if (nargc <= ++optind) { /* no arg */ - place = EMSG; - if (*ostr == ':') - ret = BADARG; - else - ret = BADCH; - if (opterr) - (void)fprintf(stderr, - "%s: option requires an argument -- %c\n", - __progname, optopt); - return (ret); - } - else /* white space */ - optarg = nargv[optind]; - place = EMSG; + if (place[1] && *++place == '-') { /* found "--" */ + ++optind; + place = EMSG; + return (-1); + } + } /* option letter okay? */ + if ((optopt = (int)*place++) == (int)':' || + !(oli = strchr(ostr, optopt))) { + /* + * if the user didn't specify '-' as an option, + * assume it means -1. + */ + if (optopt == (int)'-') + return (-1); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void)fprintf(stderr, + "%s: illegal option -- %c\n", __progname, optopt); + + return (BADCH); + } + if (*++oli != ':') { /* don't need argument */ + optarg = NULL; + if (!*place) ++optind; + } + else { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) { /* no arg */ + place = EMSG; + if (*ostr == ':') + ret = BADARG; + else + ret = BADCH; + if (opterr) + (void)fprintf(stderr, + "%s: option requires an argument -- %c\n", + __progname, optopt); + return (ret); } - return (optopt); /* dump back option letter */ + else /* white space */ + optarg = nargv[optind]; + place = EMSG; + ++optind; + } + + return (optopt); /* dump back option letter */ } #endif +int gdb_socket_started = 0; #ifndef __MINGW32__ int dcsocket = 0; +int socket_fd = 0; int gdb_server_socket = -1; #else #define bzero(b,len) (memset((b), '\0', (len)), (void) 0) +/* Winsock SOCKET is defined as an unsigned int, so -1 won't work here */ SOCKET dcsocket = 0; -SOCKET gdb_server_socket = -1; +SOCKET gdb_server_socket = 0; +SOCKET socket_fd = 0; #endif void cleanup(char **fnames) { - int counter = 0; + int counter; + + for(counter = 0; counter < 4; counter++) + { + if(fnames[counter] != 0) + free(fnames[counter]); + } - for(; counter < 4; counter++) - if(fnames[counter] != 0) - free(fnames[counter]); if(dcsocket) #ifndef __MINGW32__ - close(dcsocket); + close(dcsocket); #else - closesocket(dcsocket); - WSACleanup(); + closesocket(dcsocket); #endif + + // Handle GDB + if (gdb_socket_started) { + gdb_socket_started = 0; + + // Send SIGTERM to the GDB Client, telling remote DC program has ended + char gdb_buf[16]; + strcpy(gdb_buf, "+$X0f#ee\0"); + +#ifdef __MINGW32__ + send(socket_fd, gdb_buf, strlen(gdb_buf), 0); + sleep(1); + closesocket(socket_fd); + closesocket(gdb_server_socket); +#else + write(socket_fd, gdb_buf, strlen(gdb_buf)); + sleep(1); + close(socket_fd); + close(gdb_server_socket); +#endif + } + +#ifdef __MINGW32__ + WSACleanup(); +#endif } extern char *optarg; @@ -241,69 +274,70 @@ int recv_data(void *data, unsigned int dcaddr, unsigned int total, unsigned int memset(map, 0, (total+1023)/1024); if (!quiet) { - send_cmd(CMD_SENDBIN, dcaddr, total, NULL, 0); + send_cmd(CMD_SENDBIN, dcaddr, total, NULL, 0); } else { - send_cmd(CMD_SENDBINQ, dcaddr, total, NULL, 0); + send_cmd(CMD_SENDBINQ, dcaddr, total, NULL, 0); } start = time_in_usec(); while (((time_in_usec() - start) < PACKET_TIMEOUT)&&(packets < ((total+1023)/1024 + 1))) { - memset(buffer, 0, 2048); - - while(((retval = recv(dcsocket, (void *)buffer, 2048, 0)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT)); - - if (retval > 0) { - start = time_in_usec(); - if (memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { - if ( ((ntohl(((command_t *)buffer)->address) - dcaddr)/1024) >= ((total + 1024)/1024) ) { - printf("Obviously bad packet, avoiding segfault\n"); - fflush(stdout); - } - else { - map[ (ntohl(((command_t *)buffer)->address) - dcaddr)/1024 ] = 1; - i = data + (ntohl(((command_t *)buffer)->address) - dcaddr); - - memcpy(i, buffer + 12, ntohl(((command_t *)buffer)->size)); - } - } - packets++; - } + memset(buffer, 0, 2048); + + while(((retval = recv(dcsocket, (void *)buffer, 2048, 0)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT)); + + if (retval > 0) { + start = time_in_usec(); + if (memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { + if (((ntohl(((command_t *)buffer)->address) - dcaddr)/1024) >= ((total + 1024)/1024)) { + printf("Obviously bad packet, avoiding segfault\n"); + fflush(stdout); + } + else { + map[ (ntohl(((command_t *)buffer)->address) - dcaddr)/1024 ] = 1; + i = data + (ntohl(((command_t *)buffer)->address) - dcaddr); + + memcpy(i, buffer + 12, ntohl(((command_t *)buffer)->size)); + } + } + packets++; + } } - for(c = 0; c < (total+1023)/1024; c++) - if (!map[c]) { - if ( (total - c*1024) >= 1024) { - send_cmd(CMD_SENDBINQ, dcaddr + c*1024, 1024, NULL, 0); - } - else { - send_cmd(CMD_SENDBINQ, dcaddr + c*1024, total - c*1024, NULL, 0); - } + for(c = 0; c < (total+1023)/1024; c++) { + if (!map[c]) { + if ( (total - c*1024) >= 1024) { + send_cmd(CMD_SENDBINQ, dcaddr + c*1024, 1024, NULL, 0); + } + else { + send_cmd(CMD_SENDBINQ, dcaddr + c*1024, total - c*1024, NULL, 0); + } - start = time_in_usec(); - while(((retval = recv(dcsocket, (void *)buffer, 2048, 0)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT)); + start = time_in_usec(); + while(((retval = recv(dcsocket, (void *)buffer, 2048, 0)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT)); - if (retval > 0) { - start = time_in_usec(); + if (retval > 0) { + start = time_in_usec(); - if (memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { - map[ (ntohl(((command_t *)buffer)->address) - dcaddr)/1024 ] = 1; - /* printf("recv_data: got chunk for %p, %d bytes\n", - (void *)ntohl(((command_t *)buffer)->address), ntohl(((command_t *)buffer)->size)); */ - i = data + (ntohl(((command_t *)buffer)->address) - dcaddr); + if (memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { + map[ (ntohl(((command_t *)buffer)->address) - dcaddr)/1024 ] = 1; + /* printf("recv_data: got chunk for %p, %d bytes\n", + (void *)ntohl(((command_t *)buffer)->address), ntohl(((command_t *)buffer)->size)); */ + i = data + (ntohl(((command_t *)buffer)->address) - dcaddr); - memcpy(i, buffer + 12, ntohl(((command_t *)buffer)->size)); - } + memcpy(i, buffer + 12, ntohl(((command_t *)buffer)->size)); + } - // Get the DONEBIN - while(((retval = recv(dcsocket, (void *)buffer, 2048, 0)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT)); - } + // Get the DONEBIN + while(((retval = recv(dcsocket, (void *)buffer, 2048, 0)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT)); + } - // Force us to go back and recheck - // XXX This should be improved after recv_data can return errors. - c = -1; - } + // Force us to go back and recheck + // XXX This should be improved after recv_data can return errors. + c = -1; + } + } free(map); @@ -320,38 +354,36 @@ int send_data(unsigned char * addr, unsigned int dcaddr, unsigned int size) int count = 0; if (!size) - return -1; + return -1; - do - { - send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); - } - while(recv_response(buffer, PACKET_TIMEOUT) == -1); + do { + send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); + } while(recv_response(buffer, PACKET_TIMEOUT) == -1); while(memcmp(((command_t *)buffer)->id, CMD_LOADBIN, 4)) { - printf("send_data: error in response to CMD_LOADBIN, retrying... %c%c%c%c\n",buffer[0],buffer[1],buffer[2],buffer[3]); - do - send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); - while (recv_response(buffer, PACKET_TIMEOUT) == -1); + printf("send_data: error in response to CMD_LOADBIN, retrying... %c%c%c%c\n",buffer[0],buffer[1],buffer[2],buffer[3]); + do { + send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); + } while (recv_response(buffer, PACKET_TIMEOUT) == -1); } for(i = addr; i < addr + size; i += 1024) { - if ((addr + size - i) >= 1024) { - send_cmd(CMD_PARTBIN, dcaddr, 1024, i, 1024); - } - else { - send_cmd(CMD_PARTBIN, dcaddr, (addr + size) - i, i, (addr + size) - i); - } - dcaddr += 1024; - - /* give the DC a chance to empty its rx fifo - * this increases transfer rate on 100mbit by about 3.4x - */ - count++; - if (count == 15) { - start = time_in_usec(); - while ((time_in_usec() - start) < PACKET_TIMEOUT/51); - count = 0; + if ((addr + size - i) >= 1024) { + send_cmd(CMD_PARTBIN, dcaddr, 1024, i, 1024); + } + else { + send_cmd(CMD_PARTBIN, dcaddr, (addr + size) - i, i, (addr + size) - i); + } + dcaddr += 1024; + + /* give the DC a chance to empty its rx fifo + * this increases transfer rate on 100mbit by about 3.4x + */ + count++; + if (count == 15) { + start = time_in_usec(); + while ((time_in_usec() - start) < PACKET_TIMEOUT/51); + count = 0; } } @@ -359,33 +391,33 @@ int send_data(unsigned char * addr, unsigned int dcaddr, unsigned int size) /* delay a bit to try to make sure all data goes out before CMD_DONEBIN */ while ((time_in_usec() - start) < PACKET_TIMEOUT/10); - do - send_cmd(CMD_DONEBIN, 0, 0, NULL, 0); - while (recv_response(buffer, PACKET_TIMEOUT) == -1); + do { + send_cmd(CMD_DONEBIN, 0, 0, NULL, 0); + } while (recv_response(buffer, PACKET_TIMEOUT) == -1); while(memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { - printf("send_data: error in response to CMD_DONEBIN, retrying...\n"); + printf("send_data: error in response to CMD_DONEBIN, retrying...\n"); - do - send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); - while (recv_response(buffer, PACKET_TIMEOUT) == -1); + do { + send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); + } while (recv_response(buffer, PACKET_TIMEOUT) == -1); } while ( ntohl(((command_t *)buffer)->size) != 0) { -/* printf("%d bytes at 0x%x were missing, resending\n", ntohl(((command_t *)buffer)->size),ntohl(((command_t *)buffer)->address)); */ - send_cmd(CMD_PARTBIN, ntohl(((command_t *)buffer)->address), ntohl(((command_t *)buffer)->size), addr + (ntohl(((command_t *)buffer)->address) - a), ntohl(((command_t *)buffer)->size)); + /* printf("%d bytes at 0x%x were missing, resending\n", ntohl(((command_t *)buffer)->size),ntohl(((command_t *)buffer)->address)); */ + send_cmd(CMD_PARTBIN, ntohl(((command_t *)buffer)->address), ntohl(((command_t *)buffer)->size), addr + (ntohl(((command_t *)buffer)->address) - a), ntohl(((command_t *)buffer)->size)); - do - send_cmd(CMD_DONEBIN, 0, 0, NULL, 0); - while (recv_response(buffer, PACKET_TIMEOUT) == -1); + do { + send_cmd(CMD_DONEBIN, 0, 0, NULL, 0); + } while (recv_response(buffer, PACKET_TIMEOUT) == -1); - while(memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { - printf("send_data: error in response to CMD_DONEBIN, retrying...\n"); + while(memcmp(((command_t *)buffer)->id, CMD_DONEBIN, 4)) { + printf("send_data: error in response to CMD_DONEBIN, retrying...\n"); - do - send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); - while (recv_response(buffer, PACKET_TIMEOUT) == -1); - } + do { + send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0); + } while (recv_response(buffer, PACKET_TIMEOUT) == -1); + } } return 0; @@ -393,7 +425,7 @@ int send_data(unsigned char * addr, unsigned int dcaddr, unsigned int size) void usage(void) { - printf("\n%s %s by <an...@na...>\n\n", PACKAGE, VERSION); + printf("\n%s %s by <an...@na...>\n\n",PACKAGE,VERSION); printf("-x <filename> Upload and execute <filename>\n"); printf("-u <filename> Upload <filename>\n"); printf("-d <filename> Download to <filename>\n"); @@ -419,8 +451,8 @@ int start_ws() int failed = 0; failed = WSAStartup(MAKEWORD(2,2), &wsaData); if ( failed != NO_ERROR ) { - perror("WSAStartup"); - return 1; + perror("WSAStartup"); + return 1; } return 0; @@ -439,8 +471,8 @@ int open_socket(char *hostname) #else if (dcsocket == INVALID_SOCKET) { #endif - perror("socket"); - return -1; + perror("socket"); + return -1; } bzero(&sin, sizeof(sin)); @@ -450,24 +482,24 @@ int open_socket(char *hostname) host = gethostbyname(hostname); if (!host) { - perror("gethostbyname"); - return -1; + perror("gethostbyname"); + return -1; } memcpy((char *)&sin.sin_addr, host->h_addr, host->h_length); if (connect(dcsocket, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - perror("connect"); - return -1; + perror("connect"); + return -1; } #ifdef __MINGW32__ unsigned long flags = 1; int failed = 0; failed = ioctlsocket(dcsocket, FIONBIO, &flags); - if ( failed == SOCKET_ERROR ) { - perror("ioctlsocket"); - return -1; + if (failed == SOCKET_ERROR) { + perror("ioctlsocket"); + return -1; } #else fcntl(dcsocket, F_SETFL, O_NONBLOCK); @@ -481,8 +513,8 @@ int recv_response(unsigned char *buffer, int timeout) int start = time_in_usec(); int rv = -1; - while( ((time_in_usec() - start) < timeout) && (rv == -1)) - rv = recv(dcsocket, (void *)buffer, 2048, 0); + while(((time_in_usec() - start) < timeout) && (rv == -1)) + rv = recv(dcsocket, (void *)buffer, 2048, 0); ...<truncated>... hooks/post-receive -- An ethernet program loader for the Dreamcast. |