X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?p=my-code%2Farm.git;a=blobdiff_plain;f=betty%2Ffwdump.c;h=db0791042eb88ebade3a1c99fe9952f62e49cc91;hp=37695088edd431490f1f1defa2906b8e4b2eb79f;hb=HEAD;hpb=203e912166256906b85dfe2db43508c2d2a381d1 diff --git a/betty/fwdump.c b/betty/fwdump.c index 3769508..db07910 100644 --- a/betty/fwdump.c +++ b/betty/fwdump.c @@ -6,10 +6,134 @@ * */ +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFSIZE 16 + +int open_serial_port(char *sdev) { + + struct termios term; + int fd; + + /* open serial device */ + + fd=open(sdev,O_RDWR); + if(fd<0) { + perror("tts open"); + return fd; + } + + /* configure the serial device */ + + tcgetattr(fd,&term); + + // input/output baudrate + + cfsetispeed(&term,B9600); + cfsetospeed(&term,B9600); + + // control options -> 8n1 + + term.c_cflag&=~PARENB; // no parity + term.c_cflag&=~CSTOPB; // only 1 stop bit + term.c_cflag&=~CSIZE; // no bit mask for data bits + term.c_cflag|=CS8; // 8 data bits + + // line options -> raw input + + term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG); + + // input options -> enable flow control + + term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL); + + // output options + + term.c_oflag=0; + + // more control options -> timeout / flow control + + term.c_cc[VMIN]=0; + term.c_cc[VTIME]=40; // 4 second timeout + term.c_cc[VSTART]=0x11; + term.c_cc[VSTOP]=0x13; + + tcsetattr(fd,TCSANOW,&term); + + return fd; +} + +int open_dump_file(char *file) { + + int fd; + + /* open dump file */ + + fd=open(file,O_WRONLY|O_CREAT); + + if(fd<0) + perror("fw open"); + + return fd; +} + int main(int argc,char **argv) { + int sfd,dfd; + unsigned char buf[BUFSIZE]; + int cnt,size,ret; + + if(argc!=4) { + printf("usage: %s \n",argv[0]); + printf("\nmem:\n"); + printf("0: bank0\n"); + printf("2: bank2\n"); + printf("b: bootoader\n"); + return -1; + } + + if((argv[3][0]!='0')&(argv[3][0]!='2')&(argv[3][0]!='b')) { + printf("unsupported mem type!\n"); + return -1; + } + + sfd=open_serial_port(argv[1]); + dfd=open_dump_file(argv[2]); + + if((sfd<0)|(dfd<0)) + return -1; + + /* send start byte */ + buf[0]=argv[3][0]; + ret=write(sfd,buf,1); + if(ret!=1) { + perror("write start byte"); + return ret; + } + + /* receive flash content */ + while(ret) { + ret=read(sfd,buf,BUFSIZE); + size=ret; + cnt=0; + while(size) { + ret=write(dfd,buf+cnt,size-cnt); + if(ret<0) { + perror("write to dump file"); + return ret; + } + size-=ret; + cnt+=ret; + } + } return 0; } -