1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
35char *serialdev = "/dev/term/b";
36speed_t speed = B230400;
37int verbose = 0, docont = 0;
38unsigned long addr = 0x10000UL;
39
40int
41main(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