bullshit commit, sync for travel (to zn00H!) :)
[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!=4) {
94                 printf("usage: %s <serial port> <dump file> <mem>\n",argv[0]);
95                 printf("\nmem:\n");
96                 printf("0: bank0\n");
97                 printf("2: bank2\n");
98                 printf("b: bootoader\n");
99                 return -1;
100         }
101
102         if((argv[3][0]!='0')&(argv[3][0]!='2')&(argv[3][0]!='b')) {
103                 printf("unsupported mem type!\n");
104                 return -1;
105         }
106
107         sfd=open_serial_port(argv[1]);
108
109         dfd=open_dump_file(argv[2]);
110
111         if((sfd<0)|(dfd<0))
112                 return -1;
113
114         /* send start byte */
115         buf[0]=argv[3][0];
116         ret=write(sfd,buf,1);
117         if(ret!=1) {
118                 perror("write start byte");
119                 return ret;
120         }
121
122         /* receive flash content */
123         while(ret) {
124                 ret=read(sfd,buf,BUFSIZE);
125                 size=ret;
126                 cnt=0;
127                 while(size) {
128                         ret=write(dfd,buf+cnt,size-cnt);
129                         if(ret<0) {
130                                 perror("write to dump file");
131                                 return ret;
132                         }
133                         size-=ret;
134                         cnt+=ret;
135                 }
136         }
137
138         return 0;
139 }