xref: /openbmc/u-boot/tools/gdb/gdbsend.c (revision 15855700)
1 /*
2  * (C) Copyright 2000
3  * Murray Jensen <Murray.Jensen@csiro.au>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include "serial.h"
32 #include "error.h"
33 #include "remote.h"
34 
35 char *serialdev = "/dev/term/b";
36 speed_t speed = B230400;
37 int verbose = 0, docont = 0;
38 unsigned long addr = 0x10000UL;
39 
40 int
41 main(int ac, char **av)
42 {
43     int c, sfd, ifd;
44     char *ifn, *image;
45     struct stat ist;
46 
47     if ((pname = strrchr(av[0], '/')) == NULL)
48 	pname = av[0];
49     else
50 	pname++;
51 
52     while ((c = getopt(ac, av, "a:b:cp:v")) != EOF)
53 	switch (c) {
54 
55 	case 'a': {
56 	    char *ep;
57 
58 	    addr = strtol(optarg, &ep, 0);
59 	    if (ep == optarg || *ep != '\0')
60 		Error("can't decode address specified in -a option");
61 	    break;
62 	}
63 
64 	case 'b':
65 	    if ((speed = cvtspeed(optarg)) == B0)
66 		Error("can't decode baud rate specified in -b option");
67 	    break;
68 
69 	case 'c':
70 	    docont = 1;
71 	    break;
72 
73 	case 'p':
74 	    serialdev = optarg;
75 	    break;
76 
77 	case 'v':
78 	    verbose = 1;
79 	    break;
80 
81 	default:
82 	usage:
83 	    fprintf(stderr,
84 		"Usage: %s [-a addr] [-b bps] [-c] [-p dev] [-v] imagefile\n",
85 		pname);
86 	    exit(1);
87 	}
88 
89     if (optind != ac - 1)
90 	goto usage;
91     ifn = av[optind++];
92 
93     if (verbose)
94 	fprintf(stderr, "Opening file and reading image...\n");
95 
96     if ((ifd = open(ifn, O_RDONLY)) < 0)
97 	Perror("can't open kernel image file '%s'", ifn);
98 
99     if (fstat(ifd, &ist) < 0)
100 	Perror("fstat '%s' failed", ifn);
101 
102     if ((image = (char *)malloc(ist.st_size)) == NULL)
103 	Perror("can't allocate %ld bytes for image", ist.st_size);
104 
105     if ((c = read(ifd, image, ist.st_size)) < 0)
106 	Perror("read of %d bytes from '%s' failed", ist.st_size, ifn);
107 
108     if (c != ist.st_size)
109 	Error("read of %ld bytes from '%s' failed (%d)", ist.st_size, ifn, c);
110 
111     if (close(ifd) < 0)
112 	Perror("close of '%s' failed", ifn);
113 
114     if (verbose)
115 	fprintf(stderr, "Opening serial port and sending image...\n");
116 
117     if ((sfd = serialopen(serialdev, speed)) < 0)
118 	Perror("open of serial device '%s' failed", serialdev);
119 
120     remote_desc = sfd;
121     remote_reset();
122     remote_write_bytes(addr, image, ist.st_size);
123 
124     if (docont) {
125 	if (verbose)
126 	    fprintf(stderr, "[continue]");
127 	remote_continue();
128     }
129 
130     if (serialclose(sfd) < 0)
131 	Perror("close of serial device '%s' failed", serialdev);
132 
133     if (verbose)
134 	fprintf(stderr, "Done.\n");
135 
136     return (0);
137 }
138