1 /*
2  * Copyright (c) 2014 Brian Swetland
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <sys/types.h>
31 
32 #include "network.h"
33 #include "../app/lkboot/lkboot_protocol.h"
34 
readx(int s,void * _data,int len)35 static int readx(int s, void *_data, int len)
36 {
37     char *data = _data;
38     int r;
39     while (len > 0) {
40         r = read(s, data, len);
41         if (r == 0) {
42             fprintf(stderr, "error: eof during socket read\n");
43             return -1;
44         }
45         if (r < 0) {
46             if (errno == EINTR) continue;
47             fprintf(stderr, "error: %s during socket read\n", strerror(errno));
48             return -1;
49         }
50         data += r;
51         len -= r;
52     }
53     return 0;
54 }
55 
upload(int s,int txfd,size_t txlen,int do_endian_swap)56 static int upload(int s, int txfd, size_t txlen, int do_endian_swap)
57 {
58     int err = 0;
59     msg_hdr_t hdr;
60 
61     char *buf = malloc(txlen);
62     if (!buf)
63         return -1;
64 
65     if (readx(txfd, buf, txlen)) {
66         fprintf(stderr, "error: reading from file\n");
67         err = -1;
68         goto done;
69     }
70 
71     /* 4 byte swap data if requested */
72     if (do_endian_swap) {
73         size_t i;
74         for (i = 0; i < txlen; i += 4) {
75             char temp = buf[i];
76             buf[i] = buf[i + 3];
77             buf[i + 3] = temp;
78 
79             temp = buf[i + 1];
80             buf[i + 1] = buf[i + 2];
81             buf[i + 2] = temp;
82         }
83     }
84 
85     size_t pos = 0;
86     while (pos < txlen) {
87         size_t xfer = (txlen - pos > 65536) ? 65536 : txlen - pos;
88 
89         hdr.opcode = MSG_SEND_DATA;
90         hdr.extra = 0;
91         hdr.length = xfer - 1;
92         if (write(s, &hdr, sizeof(hdr)) != sizeof(hdr)) {
93             fprintf(stderr, "error: writing socket\n");
94             err = -1;
95             goto done;
96         }
97         if (write(s, buf + pos, xfer) != xfer) {
98             fprintf(stderr, "error: writing socket\n");
99             err = -1;
100             goto done;
101         }
102         pos += xfer;
103     }
104 
105     hdr.opcode = MSG_END_DATA;
106     hdr.extra = 0;
107     hdr.length = 0;
108     if (write(s, &hdr, sizeof(hdr)) != sizeof(hdr)) {
109         fprintf(stderr, "error: writing socket\n");
110         err = -1;
111         goto done;
112     }
113 
114 done:
115     free(buf);
116 
117     return err;
118 }
119 
trim_fpga_image(int fd,off_t len)120 static off_t trim_fpga_image(int fd, off_t len)
121 {
122     /* fd should be at start of bitfile, seek until the
123      * ff ff ff ff aa 99 55 66 pattern is found and subtract
124      * the number of bytes read until pattern found.
125      */
126     const unsigned char pat[] = { 0xff, 0xff, 0xff, 0xff, 0xaa, 0x99, 0x55, 0x66 };
127     unsigned char buf[sizeof(pat)];
128 
129     memset(buf, 0, sizeof(buf));
130 
131     off_t i;
132     for (i = 0; i < len; i++) {
133         memmove(buf, buf + 1, sizeof(buf) - 1);
134         if (read(fd, &buf[sizeof(buf) - 1], 1) < 1) {
135             return -1;
136         }
137 
138         /* look for pattern */
139         if (memcmp(pat, buf, sizeof(pat)) == 0) {
140             /* found it, rewind the fd and return the truncated length */
141             lseek(fd, -sizeof(pat), SEEK_CUR);
142             return len - (i + 1 - sizeof(pat));
143         }
144     }
145 
146     return -1;
147 }
148 
149 #define DCC_SUBPROCESS "zynq-dcc"
150 
start_dcc_subprocess(int * fd_in,int * fd_out)151 static int start_dcc_subprocess(int *fd_in, int *fd_out)
152 {
153     int outpipe[2];
154     if (pipe(outpipe) != 0)
155         return -1;
156 
157     int inpipe[2];
158     if (pipe(inpipe) != 0)
159         return -1;
160 
161     *fd_in = inpipe[0];
162     *fd_out = outpipe[1];
163 
164     pid_t pid = fork();
165     if (pid == 0) {
166         /* we are the child */
167         close(STDIN_FILENO);
168         close(STDOUT_FILENO);
169 
170         dup2(outpipe[0], STDIN_FILENO);
171         close(outpipe[1]);
172 
173         dup2(inpipe[1], STDOUT_FILENO);
174         close(inpipe[0]);
175 
176         fprintf(stderr, "in the child\n");
177 
178         execlp(DCC_SUBPROCESS, DCC_SUBPROCESS, NULL);
179         fprintf(stderr, "after exec, didn't work!\n");
180     } else {
181         fprintf(stderr, "in parent, pid %u\n", pid);
182 
183         close(outpipe[0]);
184         close(inpipe[1]);
185     }
186 
187     return 0;
188 }
189 
190 #define REPLYMAX (9 * 1024 * 1024)
191 static unsigned char replybuf[REPLYMAX];
192 static unsigned replylen = 0;
193 
lkboot_get_reply(void ** ptr)194 unsigned lkboot_get_reply(void **ptr)
195 {
196     *ptr = replybuf;
197     return replylen;
198 }
199 
lkboot_txn(const char * host,const char * _cmd,int txfd,const char * args)200 int lkboot_txn(const char *host, const char *_cmd, int txfd, const char *args)
201 {
202     msg_hdr_t hdr;
203     char cmd[128];
204     char tmp[65536];
205     off_t txlen = 0;
206     int do_endian_swap = 0;
207     int once = 1;
208     int len;
209     int fd_in, fd_out;
210     int ret = 0;
211 
212     if (txfd != -1) {
213         txlen = lseek(txfd, 0, SEEK_END);
214         if (txlen > (512*1024*1024)) {
215             fprintf(stderr, "error: file too large\n");
216             return -1;
217         }
218         lseek(txfd, 0, SEEK_SET);
219     }
220 
221     if (!strcmp(_cmd, "fpga")) {
222         /* if we were asked to send an fpga image, try to find the sync words and
223          * trim all the data before it
224          */
225         txlen = trim_fpga_image(txfd, txlen);
226         if (txlen < 0) {
227             fprintf(stderr, "error: fpga image doesn't contain sync pattern\n");
228             return -1;
229         }
230 
231         /* it'll need a 4 byte endian swap as well */
232         do_endian_swap = 1;
233     }
234 
235     len = snprintf(cmd, 128, "%s:%d:%s", _cmd, (int) txlen, args);
236     if (len > 127) {
237         fprintf(stderr, "error: command too large\n");
238         return -1;
239     }
240 
241     /* if host is -, use stdin/stdout */
242     if (!strcmp(host, "-")) {
243         fprintf(stderr, "using stdin/stdout for io\n");
244         fd_in = STDIN_FILENO;
245         fd_out = STDOUT_FILENO;
246     } else if (!strcasecmp(host, "jtag")) {
247         fprintf(stderr, "using zynq-dcc utility for io\n");
248         if (start_dcc_subprocess(&fd_in, &fd_out) < 0) {
249             fprintf(stderr, "error starting jtag subprocess, is it in your path?\n");
250             return -1;
251         }
252     } else {
253         in_addr_t addr = lookup_hostname(host);
254         if (addr == 0) {
255             fprintf(stderr, "error: cannot find host '%s'\n", host);
256             return -1;
257         }
258         while ((fd_in = tcp_connect(addr, 1023)) < 0) {
259             if (once) {
260                 fprintf(stderr, "error: cannot connect to host '%s'. retrying...\n", host);
261                 once = 0;
262             }
263             usleep(100000);
264         }
265         fd_out = fd_in;
266     }
267 
268     hdr.opcode = MSG_CMD;
269     hdr.extra = 0;
270     hdr.length = len;
271     if (write(fd_out, &hdr, sizeof(hdr)) != sizeof(hdr)) goto iofail;
272     if (write(fd_out, cmd, len) != len) goto iofail;
273 
274     for (;;) {
275         if (readx(fd_in, &hdr, sizeof(hdr))) goto iofail;
276         switch (hdr.opcode) {
277             case MSG_GO_AHEAD:
278                 if (upload(fd_out, txfd, txlen, do_endian_swap)) {
279                     ret = -1;
280                     goto out;
281                 }
282                 break;
283             case MSG_OKAY:
284                 ret = 0;
285                 goto out;
286             case MSG_FAIL:
287                 len = (hdr.length > 127) ? 127 : hdr.length;
288                 if (readx(fd_in, cmd, len)) {
289                     cmd[0] = 0;
290                 } else {
291                     cmd[len] = 0;
292                 }
293                 fprintf(stderr,"error: remote failure: %s\n", cmd);
294                 ret = -1;
295                 goto out;
296             case MSG_SEND_DATA:
297                 len = hdr.length + 1;
298                 if (readx(fd_in, tmp, len)) goto iofail;
299                 if (len > (REPLYMAX - replylen)) {
300                     fprintf(stderr, "error: too much reply data\n");
301                     ret = -1;
302                     goto out;
303                 }
304                 memcpy(replybuf + replylen, tmp, len);
305                 replylen += len;
306                 break;
307             default:
308                 fprintf(stderr, "error: unknown opcode %d\n", hdr.opcode);
309                 ret = -1;
310                 goto out;
311         }
312     }
313 
314 iofail:
315     fprintf(stderr, "error: socket io\n");
316     ret = -1;
317 
318 out:
319     close(fd_in);
320     if (fd_out != fd_in)
321         close(fd_out);
322     return ret;
323 }
324