|
From: <ba...@us...> - 2006-09-18 04:16:33
|
Revision: 376
http://svn.sourceforge.net/cadcdev/?rev=376&view=rev
Author: bardtx
Date: 2006-09-17 21:16:29 -0700 (Sun, 17 Sep 2006)
Log Message:
-----------
dcload-ip: Apply Christian's bfd fix, and make objcopy use -R .stack
Modified Paths:
--------------
dcload/dcload-ip/host-src/tool/dc-tool.c
dcload/dcload-ip/target-src/dcload/Makefile
Modified: dcload/dcload-ip/host-src/tool/dc-tool.c
===================================================================
--- dcload/dcload-ip/host-src/tool/dc-tool.c 2006-09-18 01:10:18 UTC (rev 375)
+++ dcload/dcload-ip/host-src/tool/dc-tool.c 2006-09-18 04:16:29 UTC (rev 376)
@@ -446,13 +446,13 @@
if ((section->flags & SEC_HAS_CONTENTS) && (section->flags & SEC_LOAD)) {
printf("Section %s, ",section->name);
printf("lma 0x%x, ",section->lma);
- printf("size %d\n",section->size);
- if (section->size) {
- size += section->size;
- inbuf = malloc(section->size);
- bfd_get_section_contents(somebfd, section, inbuf, 0, section->size);
+ printf("size %d\n",section->_raw_size);
+ if (section->_raw_size) {
+ size += section->_raw_size;
+ inbuf = malloc(section->_raw_size);
+ bfd_get_section_contents(somebfd, section, inbuf, 0, section->_raw_size);
- send_data(inbuf, section->lma, section->size);
+ send_data(inbuf, section->lma, section->_raw_size);
free(inbuf);
}
Modified: dcload/dcload-ip/target-src/dcload/Makefile
===================================================================
--- dcload/dcload-ip/target-src/dcload/Makefile 2006-09-18 01:10:18 UTC (rev 375)
+++ dcload/dcload-ip/target-src/dcload/Makefile 2006-09-18 04:16:29 UTC (rev 376)
@@ -16,7 +16,7 @@
%.o : %.S
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
%.bin: %
- $(OBJCOPY) -O binary $< $@
+ $(OBJCOPY) -R .stack -O binary $< $@
all: dcload.bin exception.bin
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <c99...@us...> - 2008-02-17 01:15:32
|
Revision: 558
http://cadcdev.svn.sourceforge.net/cadcdev/?rev=558&view=rev
Author: c99koder
Date: 2008-02-16 17:15:24 -0800 (Sat, 16 Feb 2008)
Log Message:
-----------
dcload-ip Malloc fixes and MinGW32 support
There were quite a few cases where dcload-ip didn't have a call to free
after a malloc. This patch fixes most (if not all) those errors. It also
includes some better error checking. This takes care of all warnings. This
also adds MinGW32 support using native WinSocks.
This patch has been tested by Cyle Terry and all seems to be working.
Modified Paths:
--------------
dcload/dcload-ip/Makefile.cfg
dcload/dcload-ip/host-src/tool/commands.h
dcload/dcload-ip/host-src/tool/dc-io.h
dcload/dcload-ip/host-src/tool/dc-tool.c
dcload/dcload-ip/host-src/tool/dcload-types.h
dcload/dcload-ip/host-src/tool/syscalls.c
dcload/dcload-ip/host-src/tool/syscalls.h
dcload/dcload-ip/host-src/tool/unlink.c
Modified: dcload/dcload-ip/Makefile.cfg
===================================================================
--- dcload/dcload-ip/Makefile.cfg 2008-02-14 03:15:15 UTC (rev 557)
+++ dcload/dcload-ip/Makefile.cfg 2008-02-17 01:15:24 UTC (rev 558)
@@ -8,8 +8,8 @@
# the ones in your system
# these must point to your sh-elf bfd, not the system one
-BFDLIB = -L/usr/local/dcdev/lib -lbfd -liberty
-BFDINCLUDE = /usr/local/dcdev/include
+BFDLIB = -L/opt/dc/sh-elf/lib -lbfd -liberty -lintl
+BFDINCLUDE = /opt/dc/sh-elf/include
# cygwin
# these must point to your sh-elf bfd, not the system one
@@ -34,8 +34,8 @@
HOSTCFLAGS += $(VERFLAGS)
# the ip of your dreamcast - set to 0.0.0.0 if you want to use arp on pc
-DREAMCAST_IP = 000.000.000.000
-#DREAMCAST_IP = 192.168.0.4
+#DREAMCAST_IP = 000.000.000.000
+DREAMCAST_IP = 192.168.11.6
#DREAMCAST_IP = 10.0.0.32
# dir to install dc-tool in
Modified: dcload/dcload-ip/host-src/tool/commands.h
===================================================================
--- dcload/dcload-ip/host-src/tool/commands.h 2008-02-14 03:15:15 UTC (rev 557)
+++ dcload/dcload-ip/host-src/tool/commands.h 2008-02-17 01:15:24 UTC (rev 558)
@@ -1,13 +1,15 @@
#ifndef __COMMANDS_H__
#define __COMMANDS_H__
-typedef struct {
- unsigned char id[4] __attribute__ ((packed));
- unsigned int address __attribute__ ((packed));
- unsigned int size __attribute__ ((packed));
- unsigned char data[1] __attribute__ ((packed));
-} command_t;
+struct _command_t {
+ unsigned char id[4];
+ unsigned int address;
+ unsigned int size;
+ unsigned char data[1];
+} __attribute__ ((packed));
+typedef struct _command_t command_t;
+
#define CMD_EXECUTE "EXEC" /* execute */
#define CMD_LOADBIN "LBIN" /* begin receiving binary */
#define CMD_PARTBIN "PBIN" /* part of a binary */
@@ -23,3 +25,4 @@
#define COMMAND_LEN 12
#endif
+
Modified: dcload/dcload-ip/host-src/tool/dc-io.h
===================================================================
--- dcload/dcload-ip/host-src/tool/dc-io.h 2008-02-14 03:15:15 UTC (rev 557)
+++ dcload/dcload-ip/host-src/tool/dc-io.h 2008-02-17 01:15:24 UTC (rev 558)
@@ -1,11 +1,14 @@
#ifndef __DC_IO_H__
#define __DC_IO_H__
-void recv_data(void *data, unsigned int dcaddr, unsigned int total, unsigned int quiet);
-void send_data(unsigned char *addr, unsigned int dcaddr, unsigned int size);
+int recv_data(void *data, unsigned int dcaddr, unsigned int total, unsigned int quiet);
+int send_data(unsigned char *addr, unsigned int dcaddr, unsigned int size);
int recv_response(unsigned char *buffer, int timeout);
-void send_command(unsigned char *command, unsigned int addr, unsigned int size, unsigned char *data, unsigned int dsize);
+int send_command(char *command, unsigned int addr, unsigned int size, unsigned char *data, unsigned int dsize);
+
+/* Convinience macro */
+#define send_cmd(v, w, x, y, z) if(send_command(v, w, x, y, z) == -1) return -1
+
#endif
-
Modified: dcload/dcload-ip/host-src/tool/dc-tool.c
===================================================================
--- dcload/dcload-ip/host-src/tool/dc-tool.c 2008-02-14 03:15:15 UTC (rev 557)
+++ dcload/dcload-ip/host-src/tool/dc-tool.c 2008-02-17 01:15:24 UTC (rev 558)
@@ -1,4 +1,4 @@
-/*
+/*
* dc-tool, a tool for use with the dcload ethernet loader
*
* Copyright (C) 2001 Andrew Kieschnick <an...@au...>
@@ -10,7 +10,7 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
@@ -27,18 +27,12 @@
#include <string.h>
#ifdef _WIN32
#include <windows.h>
+#endif
#include <sys/time.h>
#include <unistd.h>
#include <utime.h>
-#else
-#include <sys/time.h>
-#include <unistd.h>
-#include <utime.h>
-#endif
-#ifdef __MINGW32__
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#else
+#ifndef __MINGW32__
+#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -50,8 +44,11 @@
#include "commands.h"
int _nl_msg_cat_cntr;
-int gdb_server_socket = -1;
+#define DEBUG(x, ...) fprintf(stderr, "DEBUG: "); fprintf(stderr, x, __VA_ARGS__)
+
+#define CatchError(x) if(x) return -1;
+
#define VERSION DCLOAD_VERSION
#ifndef O_BINARY
@@ -66,7 +63,7 @@
/*
* Copyright (c) 1987, 1993, 1994
- * The Regents of the University of California. All rights reserved.
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -78,8 +75,8 @@
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
@@ -87,7 +84,7 @@
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@@ -99,7 +96,7 @@
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
-static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
+static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
#endif
static const char rcsid[] = "$FreeBSD$";
#endif /* LIBC_SCCS and not lint */
@@ -114,17 +111,12 @@
#define BADARG (int)':'
#define EMSG ""
-char *__progname="prboom_server";
+char *__progname="dc-tool";
/*
- * getopt --
- * Parse argc/argv argument vector.
+ * getopt -- Parse argc/argv argument vector.
*/
-int
-getopt(nargc, nargv, ostr)
- int nargc;
- char * const *nargv;
- const char *ostr;
+int getopt(int nargc, char * const *nargv, const char *ostr)
{
extern char *__progname;
static char *place = EMSG; /* option letter processing */
@@ -157,7 +149,7 @@
(void)fprintf(stderr,
"%s: illegal option -- %c\n", __progname, optopt);
return (BADCH);
- }
+ }
if (*++oli != ':') { /* don't need argument */
optarg = NULL;
if (!*place)
@@ -171,7 +163,7 @@
if (*ostr == ':')
ret = BADARG;
else
- ret = BADCH;
+ ret = BADCH;
if (opterr)
(void)fprintf(stderr,
"%s: option requires an argument -- %c\n",
@@ -185,10 +177,33 @@
}
return (optopt); /* dump back option letter */
}
+#endif
+
+#ifndef __MINGW32__
+int dcsocket = 0;
+int gdb_server_socket = -1;
#else
-#include <unistd.h>
+#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
+SOCKET dcsocket = 0;
+SOCKET gdb_server_socket = -1;
#endif
+void cleanup(char **fnames)
+{
+ int counter = 0;
+
+ for(; counter < 4; counter++)
+ if(fnames[counter] != 0)
+ free(fnames[counter]);
+ if(dcsocket)
+#ifndef __MINGW32__
+ close(dcsocket);
+#else
+ closesocket(dcsocket);
+ WSACleanup();
+#endif
+}
+
extern char *optarg;
unsigned int time_in_usec()
@@ -200,13 +215,11 @@
return (unsigned int)(thetime.tv_sec * 1000000) + (unsigned int)thetime.tv_usec;
}
-int dcsocket;
-
/* 250000 = 0.25 seconds */
#define PACKET_TIMEOUT 250000
/* receive total bytes from dc and store in data */
-void recv_data(void *data, unsigned int dcaddr, unsigned int total, unsigned int quiet)
+int recv_data(void *data, unsigned int dcaddr, unsigned int total, unsigned int quiet)
{
unsigned char buffer[2048];
unsigned char *i;
@@ -218,24 +231,31 @@
memset(map, 0, (total+1023)/1024);
- if (!quiet)
- send_command(CMD_SENDBIN, dcaddr, total, NULL, 0);
- else
- send_command(CMD_SENDBINQ, dcaddr, total, NULL, 0);
-
+ if (!quiet) {
+ send_cmd(CMD_SENDBIN, dcaddr, total, NULL, 0);
+ }
+ else {
+ 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 = read(dcsocket, buffer, 2048)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT));
+
+ 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 {
+ 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));
}
}
@@ -245,60 +265,74 @@
for(c = 0; c < (total+1023)/1024; c++)
if (!map[c]) {
- if ( (total - c*1024) >= 1024)
- send_command(CMD_SENDBINQ, dcaddr + c*1024, 1024, NULL, 0);
- else
- send_command(CMD_SENDBINQ, dcaddr + c*1024, total - c*1024, NULL, 0);
+ 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 = read(dcsocket, buffer, 2048)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT));
+ 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)) {
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)); */
+ (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));
}
// Get the DONEBIN
- while(((retval = read(dcsocket, buffer, 2048)) == -1)&&((time_in_usec() - start) < PACKET_TIMEOUT));
+ 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;
}
+
+ free(map);
+
+ return 0;
}
/* send size bytes to dc from addr to dcaddr*/
-void send_data(unsigned char * addr, unsigned int dcaddr, unsigned int size)
+int send_data(unsigned char * addr, unsigned int dcaddr, unsigned int size)
{
- unsigned char buffer[2048];
- unsigned char * i;
+ unsigned char buffer[2048] = {0};
+ unsigned char * i = 0;
unsigned int a = dcaddr;
- unsigned int start;
+ unsigned int start = 0;
int count = 0;
if (!size)
- return;
+ return -1;
do
- send_command(CMD_LOADBIN, dcaddr, size, NULL, 0);
+ {
+ 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_command(CMD_LOADBIN, dcaddr, size, NULL, 0);
+ 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_command(CMD_PARTBIN, dcaddr, 1024, i, 1024);
- else
- send_command(CMD_PARTBIN, dcaddr, (addr + size) - i, i, (addr + size) - i);
+ 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
@@ -308,8 +342,8 @@
if (count == 15) {
start = time_in_usec();
while ((time_in_usec() - start) < PACKET_TIMEOUT/51);
- count = 0;
- }
+ count = 0;
+ }
}
start = time_in_usec();
@@ -317,31 +351,35 @@
while ((time_in_usec() - start) < PACKET_TIMEOUT/10);
do
- send_command(CMD_DONEBIN, 0, 0, NULL, 0);
+ 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");
+
do
- send_command(CMD_LOADBIN, dcaddr, size, NULL, 0);
+ 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_command(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_command(CMD_DONEBIN, 0, 0, NULL, 0);
+ 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");
+
do
- send_command(CMD_LOADBIN, dcaddr, size, NULL, 0);
+ send_cmd(CMD_LOADBIN, dcaddr, size, NULL, 0);
while (recv_response(buffer, PACKET_TIMEOUT) == -1);
}
}
+
+ return 0;
}
void usage(void)
@@ -362,17 +400,36 @@
printf("-r Reset (only works when dcload is in control)\n");
printf("-g Start a GDB server\n");
printf("-h Usage information (you\'re looking at it)\n\n");
- exit(0);
}
-int open_socket(unsigned char *hostname)
+/* Got to make sure WinSock is initalized */
+#ifdef __MINGW32__
+int start_ws()
{
+ WSADATA wsaData;
+ int failed = 0;
+ failed = WSAStartup(MAKEWORD(2,2), &wsaData);
+ if ( failed != NO_ERROR ) {
+ perror("WSAStartup");
+ return 1;
+ }
+
+ return 0;
+}
+#endif
+
+int open_socket(char *hostname)
+{
struct sockaddr_in sin;
- struct hostent * host;
+ struct hostent *host = 0;
- dcsocket = socket(AF_INET, SOCK_DGRAM, 0);
+ dcsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
+#ifndef __MINGW32__
if (dcsocket < 0) {
+#else
+ if (dcsocket == INVALID_SOCKET) {
+#endif
perror("socket");
return -1;
}
@@ -397,10 +454,17 @@
#ifdef __MINGW32__
unsigned long flags = 1;
- ioctlsocket(dcsocket, FIONBIO, &flags);
+ int failed = 0;
+ failed = ioctlsocket(dcsocket, FIONBIO, &flags);
+ if ( failed == SOCKET_ERROR ) {
+ perror("ioctlsocket");
+ return -1;
+ }
#else
fcntl(dcsocket, F_SETFL, O_NONBLOCK);
#endif
+
+ return 0;
}
int recv_response(unsigned char *buffer, int timeout)
@@ -409,15 +473,16 @@
int rv = -1;
while( ((time_in_usec() - start) < timeout) && (rv == -1))
- rv = read(dcsocket, buffer, 2048);
+ rv = recv(dcsocket, (void *)buffer, 2048, 0);
return rv;
}
-void send_command(unsigned char *command, unsigned int addr, unsigned int size, unsigned char *data, unsigned int dsize)
+int send_command(char *command, unsigned int addr, unsigned int size, unsigned char *data, unsigned int dsize)
{
unsigned char c_buff[2048];
unsigned int tmp;
+ int error = 0;
memcpy(c_buff, command, 4);
tmp = htonl(addr);
@@ -427,26 +492,45 @@
if (data != 0)
memcpy(c_buff + 12, data, dsize);
- write(dcsocket, c_buff, 12 + dsize);
+ error = send(dcsocket, (void *)c_buff, 12+dsize, 0);
+
+ if(error == -1) {
+#ifndef __MINGW32__
+ if(errno == EAGAIN)
+ return 0;
+ fprintf(stderr, "error: %s\n", strerror(errno));
+#else
+ /* WSAEWOULDBLOCK is a non-fatal error, so continue */
+ if(WSAGetLastError() == WSAEWOULDBLOCK)
+ return 0;
+
+ fprintf(stderr, "error: %d\n", WSAGetLastError());
+#endif
+
+ return -1;
+ }
+
+ return 0;
}
-unsigned int upload(unsigned char *filename, unsigned int address)
+unsigned int upload(char *filename, unsigned int address)
{
int inputfd;
- int size;
+ int size = 0;
+ int sectsize;
unsigned char *inbuf;
struct timeval starttime, endtime;
- unsigned char c;
double stime, etime;
bfd *somebfd;
somebfd = bfd_openr(filename, 0);
- if (somebfd) { /* try bfd first */
+ if (somebfd) {
if (bfd_check_format(somebfd, bfd_object)) {
+ /* try bfd first */
asection *section;
-
+
printf("File format is %s, ", somebfd->xvec->name);
address = somebfd->start_address;
size = 0;
@@ -454,68 +538,69 @@
gettimeofday(&starttime, 0);
- for (section = somebfd->sections; section != NULL; section = section->next)
+ for (section = somebfd->sections; section != NULL; section = section->next) {
if ((section->flags & SEC_HAS_CONTENTS) && (section->flags & SEC_LOAD)) {
+ sectsize = bfd_section_size(somebfd, section);
printf("Section %s, ",section->name);
- printf("lma 0x%x, ",section->lma);
- printf("size %d\n",bfd_section_size(somebfd, section));
- if (bfd_section_size(somebfd, section)) {
- size += bfd_section_size(somebfd, section);
- inbuf = malloc(bfd_section_size(somebfd, section));
- bfd_get_section_contents(somebfd, section, inbuf, 0, bfd_section_size(somebfd, section));
+ printf("lma 0x%x, ", (unsigned int)section->lma);
+ printf("size %d\n",sectsize);
- send_data(inbuf, section->lma, bfd_section_size(somebfd, section));
+ if (sectsize) {
+ size += sectsize;
+ inbuf = malloc(sectsize);
+ bfd_get_section_contents(somebfd, section, inbuf, 0, sectsize);
+ if(send_data(inbuf, section->lma, sectsize) == -1)
+ return -1;
+
free(inbuf);
}
- }
+ }
+ }
+
bfd_close(somebfd);
} else {
+ /* if that fails, send raw bin */
+ //! Close the bfd here too?
+ inputfd = open(filename, O_RDONLY | O_BINARY);
-/* if that fails, send raw bin */
-
- printf("File format is raw binary, start address is 0x%x\n", address);
- inputfd = open(filename, O_RDONLY | O_BINARY);
-
if (inputfd < 0) {
perror(filename);
- exit(-1);
+ return -1;
}
-
+
+ printf("File format is raw binary, start address is 0x%x\n", address);
+
size = lseek(inputfd, 0, SEEK_END);
lseek(inputfd, 0, SEEK_SET);
-
+
inbuf = malloc(size);
-
read(inputfd, inbuf, size);
-
close(inputfd);
-
+
gettimeofday(&starttime, 0);
- send_data(inbuf, address, size);
-
+ if(send_data(inbuf, address, size) == -1)
+ return -1;
}
+ }
gettimeofday(&endtime, 0);
stime = starttime.tv_sec + starttime.tv_usec / 1000000.0;
etime = endtime.tv_sec + endtime.tv_usec / 1000000.0;
- printf("transferred at %f bytes / sec\n", (double) size / (etime - stime));
+ printf("transferred %d bytes at %f bytes / sec\n", size, (double) size / (etime - stime));
fflush(stdout);
- }
return address;
}
-void download(unsigned char *filename, unsigned int address,
+int download(char *filename, unsigned int address,
unsigned int size, unsigned int quiet)
{
int outputfd;
- unsigned char c;
- unsigned int wrkmem = 0x8cff0000;
unsigned char *data;
struct timeval starttime, endtime;
double stime, etime;
@@ -524,7 +609,7 @@
if (outputfd < 0) {
perror(filename);
- exit(-1);
+ return -1;
}
data = malloc(size);
@@ -542,30 +627,32 @@
printf("transferred at %f bytes / sec\n", (double) size / (etime - stime));
fflush(stdout);
-
+
write(outputfd, data, size);
close(outputfd);
+ free(data);
+ return 0;
}
-void execute(unsigned int address, unsigned int console, unsigned int cdfsredir)
+int execute(unsigned int address, unsigned int console, unsigned int cdfsredir)
{
unsigned char buffer[2048];
printf("Sending execute command (0x%x, console=%d, cdfsredir=%d)...",address,console,cdfsredir);
do
- send_command(CMD_EXECUTE, address, (cdfsredir << 1) | console, NULL, 0);
+ send_cmd(CMD_EXECUTE, address, (cdfsredir << 1) | console, NULL, 0);
while (recv_response(buffer, PACKET_TIMEOUT) == -1);
printf("executing\n");
+ return 0;
}
-void do_console(unsigned char *path, unsigned char *isofile)
+int do_console(char *path, char *isofile)
{
- unsigned char command;
- int isofd;
+ int isofd = 0;
unsigned char buffer[2048];
if (isofile) {
@@ -584,95 +671,123 @@
fflush(stdout);
while(recv_response(buffer, PACKET_TIMEOUT) == -1);
-
+
if (!(memcmp(buffer, CMD_EXIT, 4)))
- exit(0);
+ return -1;
if (!(memcmp(buffer, CMD_FSTAT, 4)))
- dc_fstat(buffer);
+ CatchError(dc_fstat(buffer));
if (!(memcmp(buffer, CMD_WRITE, 4)))
- dc_write(buffer);
+ CatchError(dc_write(buffer));
if (!(memcmp(buffer, CMD_READ, 4)))
- dc_read(buffer);
+ CatchError(dc_read(buffer));
if (!(memcmp(buffer, CMD_OPEN, 4)))
- dc_open(buffer);
+ CatchError(dc_open(buffer));
if (!(memcmp(buffer, CMD_CLOSE, 4)))
- dc_close(buffer);
+ CatchError(dc_close(buffer));
if (!(memcmp(buffer, CMD_CREAT, 4)))
- dc_creat(buffer);
+ CatchError(dc_creat(buffer));
if (!(memcmp(buffer, CMD_LINK, 4)))
- dc_link(buffer);
+ CatchError(dc_link(buffer));
if (!(memcmp(buffer, CMD_UNLINK, 4)))
- dc_unlink(buffer);
+ CatchError(dc_unlink(buffer));
if (!(memcmp(buffer, CMD_CHDIR, 4)))
- dc_chdir(buffer);
+ CatchError(dc_chdir(buffer));
if (!(memcmp(buffer, CMD_CHMOD, 4)))
- dc_chmod(buffer);
+ CatchError(dc_chmod(buffer));
if (!(memcmp(buffer, CMD_LSEEK, 4)))
- dc_lseek(buffer);
+ CatchError(dc_lseek(buffer));
if (!(memcmp(buffer, CMD_TIME, 4)))
- dc_time(buffer);
+ CatchError(dc_time(buffer));
if (!(memcmp(buffer, CMD_STAT, 4)))
- dc_stat(buffer);
+ CatchError(dc_stat(buffer));
if (!(memcmp(buffer, CMD_UTIME, 4)))
- dc_utime(buffer);
+ CatchError(dc_utime(buffer));
if (!(memcmp(buffer, CMD_BAD, 4)))
- printf("command 15 should not happen... (but it did)\n");
+ fprintf(stderr, "command 15 should not happen... (but it did)\n");
if (!(memcmp(buffer, CMD_OPENDIR, 4)))
- dc_opendir(buffer);
+ CatchError(dc_opendir(buffer));
if (!(memcmp(buffer, CMD_CLOSEDIR, 4)))
- dc_closedir(buffer);
+ CatchError(dc_closedir(buffer));
if (!(memcmp(buffer, CMD_READDIR, 4)))
- dc_readdir(buffer);
+ CatchError(dc_readdir(buffer));
if (!(memcmp(buffer, CMD_CDFSREAD, 4)))
- dc_cdfs_redir_read_sectors(isofd, buffer);
+ CatchError(dc_cdfs_redir_read_sectors(isofd, buffer));
if (!(memcmp(buffer, CMD_GDBPACKET, 4)))
- dc_gdbpacket(buffer);
+ CatchError(dc_gdbpacket(buffer));
}
+
+ return 0;
}
int open_gdb_socket(int port)
{
struct sockaddr_in server_addr;
-
+
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons( port );
server_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
- if ( (gdb_server_socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0 ) {
+ gdb_server_socket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
+#ifdef __MINGW32__
+ if ( gdb_server_socket == INVALID_SOCKET) {
+#else
+ if ( gdb_server_socket < 0 ) {
+#endif
perror( "error creating gdb server socket" );
return -1;
}
- if ( bind( gdb_server_socket, (struct sockaddr*)&server_addr, sizeof( server_addr ) ) < 0 ) {
+ int checkbind = bind( gdb_server_socket, (struct sockaddr*)&server_addr, sizeof( server_addr ) );
+#ifdef __MINGW32__
+ if( checkbind == SOCKET_ERROR ) {
+#else
+ if ( checkbind < 0 ) {
+#endif
perror( "error binding gdb server socket" );
return -1;
}
- if ( listen( gdb_server_socket, 0 ) < 0 ) {
+ int checklisten = listen( gdb_server_socket, 0 );
+#ifdef __MINGW32__
+ if ( checklisten == SOCKET_ERROR ) {
+#else
+ if ( checklisten < 0 ) {
+#endif
perror( "error listening to gdb server socket" );
return -1;
}
+
+ return 0;
}
int main(int argc, char *argv[])
{
unsigned int address = 0x8c010000;
unsigned int size = 0;
- unsigned char *filename = 0;
- unsigned char *path = 0;
unsigned int console = 1;
unsigned int quiet = 0;
unsigned char command = 0;
- char *hostname = DREAMCAST_IP;
unsigned int cdfs_redir = 0;
- unsigned char *isofile = 0;
int someopt;
- if (argc < 2)
+ /* Dynamically allocated, so it should be freed */
+ char *filename = 0;
+ char *isofile = 0;
+ char *path = 0;
+ char *hostname = DREAMCAST_IP;
+ char *cleanlist[4] = { 0, 0, 0, 0 };
+
+ if (argc < 2) {
usage();
+ return 0;
+ }
+
#ifdef __MINGW32__
- someopt = getopt(argc, argv, "x:u:d:a:s:t:i:npqhrg");
+ if(start_ws())
+ return -1;
+
+ someopt = getopt(argc, argv, "x:u:d:a:s:t:i:npqhrg");
#else
someopt = getopt(argc, argv, "x:u:d:a:s:t:c:i:npqhrg");
#endif
@@ -680,40 +795,45 @@
switch (someopt) {
case 'x':
if (command) {
- printf("You can only specify one of -x, -u, -d, and -r\n");
- exit(0);
+ fprintf(stderr, "You can only specify one of -x, -u, -d, and -r\n");
+ goto doclean;
}
command = 'x';
filename = malloc(strlen(optarg) + 1);
+ cleanlist[0] = filename;
strcpy(filename, optarg);
break;
case 'u':
if (command) {
- printf("You can only specify one of -x, -u, -d, and -r\n");
- exit(0);
+ fprintf(stderr, "You can only specify one of -x, -u, -d, and -r\n");
+ goto doclean;
}
command = 'u';
filename = malloc(strlen(optarg) + 1);
+ cleanlist[0] = filename;
strcpy(filename, optarg);
break;
case 'd':
if (command) {
- printf("You can only specify one of -x, -u, -d, and -r\n");
- exit(0);
+ fprintf(stderr, "You can only specify one of -x, -u, -d, and -r\n");
+ goto doclean;
}
command = 'd';
filename = malloc(strlen(optarg) + 1);
+ cleanlist[0] = filename;
strcpy(filename, optarg);
break;
#ifndef __MINGW32__
case 'c':
path = malloc(strlen(optarg) + 1);
+ cleanlist[1] = path;
strcpy(path, optarg);
break;
#endif
case 'i':
cdfs_redir = 1;
isofile = malloc(strlen(optarg) + 1);
+ cleanlist[2] = isofile;
strcpy(isofile, optarg);
break;
case 'a':
@@ -724,6 +844,7 @@
break;
case 't':
hostname = malloc(strlen(optarg) + 1);
+ cleanlist[3] = hostname;
strcpy(hostname, optarg);
break;
case 'n':
@@ -734,11 +855,13 @@
break;
case 'h':
usage();
+ cleanup(cleanlist);
+ return 0;
break;
case 'r':
if (command) {
- printf("You can only specify one of -x, -u, -d, and -r\n");
- exit(0);
+ fprintf(stderr, "You can only specify one of -x, -u, -d, and -r\n");
+ goto doclean;
}
command = 'r';
break;
@@ -747,6 +870,9 @@
open_gdb_socket(2159);
break;
default:
+ /* The user obviously mistyped something */
+ usage();
+ goto doclean;
break;
}
#ifdef __MINGW32__
@@ -756,58 +882,73 @@
#endif
}
+ if (quiet)
+ printf("Quiet download\n");
+
if (cdfs_redir & (!console))
console = 1;
- if (console & command=='x')
+ if (console & (command=='x'))
printf("Console enabled\n");
- if (quiet)
- printf("Quiet download\n");
-
#ifndef __MINGW32__
...
[truncated message content] |