fwdump working now, adapted fwbc fw (reading/transmitting flash should work now!)
[my-code/arm.git] / betty / fwdump.c
1 /*
2  * fwdump.c - dump the firmware received by uart to your computers harddisk
3  *            use fwbc as a transmitting firmware
4  *
5  * author: hackbard@hackdaworld.org
6  *
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <termios.h>
17
18 #define BUFSIZE         16
19
20 int open_serial_port(char *sdev) {
21
22         struct termios term;
23         int fd;
24
25         /* open serial device */
26
27         fd=open(sdev,O_RDWR);
28         if(fd<0) {
29                 perror("tts open");
30                 return fd;
31         }
32
33         /* configure the serial device */
34
35         tcgetattr(fd,&term);
36
37         // input/output baudrate
38
39         cfsetispeed(&term,B9600);
40         cfsetospeed(&term,B9600);
41
42         // control options -> 8n1
43
44         term.c_cflag&=~PARENB;  // no parity
45         term.c_cflag&=~CSTOPB;  // only 1 stop bit
46         term.c_cflag&=~CSIZE;   // no bit mask for data bits
47         term.c_cflag|=CS8;      // 8 data bits
48
49         // line options -> raw input
50         
51         term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
52
53         // input options -> enable flow control
54         
55         term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
56         
57         // output options
58
59         term.c_oflag=0;
60
61         // more control options -> timeout / flow control
62         
63         term.c_cc[VMIN]=0;
64         term.c_cc[VTIME]=40;    // 4 second timeout
65         term.c_cc[VSTART]=0x11;
66         term.c_cc[VSTOP]=0x13;
67
68         tcsetattr(fd,TCSANOW,&term);
69
70         return fd;
71 }
72
73 int open_dump_file(char *file) {
74
75         int fd;
76
77         /* open dump file */
78
79         fd=open(file,O_WRONLY|O_CREAT);
80
81         if(fd<0)
82                 perror("fw open");
83
84         return fd;
85 }
86
87 int main(int argc,char **argv) {
88
89         int sfd,dfd;
90         unsigned char buf[BUFSIZE];
91         int cnt,size,ret;
92
93         if(argc!=3) {
94                 printf("usage: %s <serial port> <dump file>\n",argv[0]);
95                 return -1;
96         }
97
98         sfd=open_serial_port(argv[1]);
99
100         dfd=open_dump_file(argv[2]);
101
102         if((sfd<0)|(dfd<0))
103                 return -1;
104
105         /* send start byte */
106         buf[0]=0x23;
107         ret=write(sfd,buf,1);
108         if(ret!=1) {
109                 perror("write start byte");
110                 return ret;
111         }
112
113         /* receive flash content */
114         while(ret) {
115                 ret=read(sfd,buf,BUFSIZE);
116                 size=ret;
117                 cnt=0;
118                 while(size) {
119                         ret=write(dfd,buf+cnt,size-cnt);
120                         if(ret<0) {
121                                 perror("write to dump file");
122                                 return ret;
123                         }
124                         size-=ret;
125                         cnt+=ret;
126                 }
127         }
128
129         return 0;
130 }