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