xref: /openbmc/u-boot/tools/gdb/gdbsend.c (revision e8f80a5a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Murray Jensen <Murray.Jensen@csiro.au>
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include "serial.h"
15 #include "error.h"
16 #include "remote.h"
17 
18 char *serialdev = "/dev/term/b";
19 speed_t speed = B230400;
20 int verbose = 0, docont = 0;
21 unsigned long addr = 0x10000UL;
22 
23 int
main(int ac,char ** av)24 main(int ac, char **av)
25 {
26     int c, sfd, ifd;
27     char *ifn, *image;
28     struct stat ist;
29 
30     if ((pname = strrchr(av[0], '/')) == NULL)
31 	pname = av[0];
32     else
33 	pname++;
34 
35     while ((c = getopt(ac, av, "a:b:cp:v")) != EOF)
36 	switch (c) {
37 
38 	case 'a': {
39 	    char *ep;
40 
41 	    addr = strtol(optarg, &ep, 0);
42 	    if (ep == optarg || *ep != '\0')
43 		Error("can't decode address specified in -a option");
44 	    break;
45 	}
46 
47 	case 'b':
48 	    if ((speed = cvtspeed(optarg)) == B0)
49 		Error("can't decode baud rate specified in -b option");
50 	    break;
51 
52 	case 'c':
53 	    docont = 1;
54 	    break;
55 
56 	case 'p':
57 	    serialdev = optarg;
58 	    break;
59 
60 	case 'v':
61 	    verbose = 1;
62 	    break;
63 
64 	default:
65 	usage:
66 	    fprintf(stderr,
67 		"Usage: %s [-a addr] [-b bps] [-c] [-p dev] [-v] imagefile\n",
68 		pname);
69 	    exit(1);
70 	}
71 
72     if (optind != ac - 1)
73 	goto usage;
74     ifn = av[optind++];
75 
76     if (verbose)
77 	fprintf(stderr, "Opening file and reading image...\n");
78 
79     if ((ifd = open(ifn, O_RDONLY)) < 0)
80 	Perror("can't open kernel image file '%s'", ifn);
81 
82     if (fstat(ifd, &ist) < 0)
83 	Perror("fstat '%s' failed", ifn);
84 
85     if ((image = (char *)malloc(ist.st_size)) == NULL)
86 	Perror("can't allocate %ld bytes for image", ist.st_size);
87 
88     if ((c = read(ifd, image, ist.st_size)) < 0)
89 	Perror("read of %d bytes from '%s' failed", ist.st_size, ifn);
90 
91     if (c != ist.st_size)
92 	Error("read of %ld bytes from '%s' failed (%d)", ist.st_size, ifn, c);
93 
94     if (close(ifd) < 0)
95 	Perror("close of '%s' failed", ifn);
96 
97     if (verbose)
98 	fprintf(stderr, "Opening serial port and sending image...\n");
99 
100     if ((sfd = serialopen(serialdev, speed)) < 0)
101 	Perror("open of serial device '%s' failed", serialdev);
102 
103     remote_desc = sfd;
104     remote_reset();
105     remote_write_bytes(addr, image, ist.st_size);
106 
107     if (docont) {
108 	if (verbose)
109 	    fprintf(stderr, "[continue]");
110 	remote_continue();
111     }
112 
113     if (serialclose(sfd) < 0)
114 	Perror("close of serial device '%s' failed", serialdev);
115 
116     if (verbose)
117 	fprintf(stderr, "Done.\n");
118 
119     return (0);
120 }
121