aboutsummaryrefslogtreecommitdiff
path: root/recv_image.c
blob: 26a8361004e695bc6338fbcc4d38ce6e3a206fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

#define PROGRAM_NAME "recv_image"
#define _XOPEN_SOURCE 500
#define _BSD_SOURCE	/* struct ip_mreq */

#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <crc32.h>
#include "mtd/mtd-user.h"
#include "mcast_image.h"

#include "common.h"

#define WBUF_SIZE 4096
struct eraseblock {
	uint32_t flash_offset;
	unsigned char wbuf[WBUF_SIZE];
	int wbuf_ofs;
	int nr_pkts;
	int *pkt_indices;
	uint32_t crc;
};

int main(int argc, char **argv)
{
	struct addrinfo *ai;
	struct addrinfo hints;
	struct addrinfo *runp;
	int ret;
	int sock;
	ssize_t len;
	int flfd;
	struct mtd_info_user meminfo;
	unsigned char *eb_buf, *decode_buf, **src_pkts;
	int nr_blocks = 0;
	int pkts_per_block;
	int block_nr = -1;
	uint32_t image_crc = 0;
	int total_pkts = 0;
	int ignored_pkts = 0;
	loff_t mtdoffset = 0;
	int badcrcs = 0;
	int duplicates = 0;
	int file_mode = 0;
	struct fec_parms *fec = NULL;
	int i;
	struct eraseblock *eraseblocks = NULL;
	uint32_t start_seq = 0;
	struct timeval start, now;
	unsigned long fec_time = 0, flash_time = 0, crc_time = 0,
		rflash_time = 0, erase_time = 0, net_time = 0;

	if (argc != 4) {
		fprintf(stderr, "usage: %s <host> <port> <mtddev>\n",
			PROGRAM_NAME);
		exit(1);
	}
	/* Open the device */
	flfd = open(argv[3], O_RDWR);

	if (flfd >= 0) {
		/* Fill in MTD device capability structure */
		if (ioctl(flfd, MEMGETINFO, &meminfo) != 0) {
			perror("MEMGETINFO");
			close(flfd);
			flfd = -1;
		} else {
			printf("Receive to MTD device %s with erasesize %d\n",
			       argv[3], meminfo.erasesize);
		}
	}
	if (flfd == -1) {
		/* Try again, as if it's a file */
		flfd = open(argv[3], O_CREAT|O_TRUNC|O_RDWR, 0644);
		if (flfd < 0) {
			perror("open");
			exit(1);
		}
		meminfo.erasesize = 131072;
		file_mode = 1;
		printf("Receive to file %s with (assumed) erasesize %d\n",
		       argv[3], meminfo.erasesize);
	}

	pkts_per_block = (meminfo.erasesize + PKT_SIZE - 1) / PKT_SIZE;

	eb_buf = malloc(pkts_per_block * PKT_SIZE);
	decode_buf = malloc(pkts_per_block * PKT_SIZE);
	if (!eb_buf && !decode_buf) {
		fprintf(stderr, "No memory for eraseblock buffer\n");
		exit(1);
	}
	src_pkts = malloc(sizeof(unsigned char *) * pkts_per_block);
	if (!src_pkts) {
		fprintf(stderr, "No memory for decode packet pointers\n");
		exit(1);
	}

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_ADDRCONFIG;
	hints.ai_socktype = SOCK_DGRAM;

	ret = getaddrinfo(argv[1], argv[2], &hints, &ai);
	if (ret) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
		exit(1);
	}
	runp = ai;
	for (runp = ai; runp; runp = runp->ai_next) {
		sock = socket(runp->ai_family, runp->ai_socktype,
			      runp->ai_protocol);
		if (sock == -1) {
			perror("socket");
			continue;
		}
		if (runp->ai_family == AF_INET &&
		    IN_MULTICAST( ntohl(((struct sockaddr_in *)runp->ai_addr)->sin_addr.s_addr))) {
			struct ip_mreq rq;
			rq.imr_multiaddr = ((struct sockaddr_in *)runp->ai_addr)->sin_addr;
			rq.imr_interface.s_addr = INADDR_ANY;
			if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &rq, sizeof(rq))) {
				perror("IP_ADD_MEMBERSHIP");
				close(sock);
				continue;
			}

		} else if (runp->ai_family == AF_INET6 &&
			   ((struct sockaddr_in6 *)runp->ai_addr)->sin6_addr.s6_addr[0] == 0xff) {
			struct ipv6_mreq rq;
			rq.ipv6mr_multiaddr =  ((struct sockaddr_in6 *)runp->ai_addr)->sin6_addr;
			rq.ipv6mr_interface = 0;
			if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &rq, sizeof(rq))) {
				perror("IPV6_ADD_MEMBERSHIP");
				close(sock);
				continue;
			}
		}
		if (bind(sock, runp->ai_addr, runp->ai_addrlen)) {
			perror("bind");
			close(sock);
			continue;
		}
		break;
	}
	if (!runp)
		exit(1);

	while (1) {
		struct image_pkt thispkt;

		len = read(sock, &thispkt, sizeof(thispkt));

		if (len < 0) {
			perror("read socket");
			break;
		}
		if (len < sizeof(thispkt)) {
			fprintf(stderr, "Wrong length %zd bytes (expected %zu)\n",
				len, sizeof(thispkt));
			continue;
		}
		if (!eraseblocks) {
			image_crc = thispkt.hdr.totcrc;
			start_seq = ntohl(thispkt.hdr.pkt_sequence);

			if (meminfo.erasesize != ntohl(thispkt.hdr.blocksize)) {
				fprintf(stderr, "Erasesize mismatch (0x%x not 0x%x)\n",
					ntohl(thispkt.hdr.blocksize), meminfo.erasesize);
				exit(1);
			}
			nr_blocks = ntohl(thispkt.hdr.nr_blocks);

			fec = fec_new(pkts_per_block, ntohs(thispkt.hdr.nr_pkts));

			eraseblocks = malloc(nr_blocks * sizeof(*eraseblocks));
			if (!eraseblocks) {
				fprintf(stderr, "No memory for block map\n");
				exit(1);
			}
			for (i = 0; i < nr_blocks; i++) {
				eraseblocks[i].pkt_indices = malloc(sizeof(int) * pkts_per_block);
				if (!eraseblocks[i].pkt_indices) {
					fprintf(stderr, "Failed to allocate packet indices\n");
					exit(1);
				}
				eraseblocks[i].nr_pkts = 0;
				if (!file_mode) {
					if (mtdoffset >= meminfo.size) {
						fprintf(stderr, "Run out of space on flash\n");
						exit(1);
					}
#if 1 /* Deliberately use bad blocks... test write failures */
					while (ioctl(flfd, MEMGETBADBLOCK, &mtdoffset) > 0) {
						printf("Skipping flash bad block at %08x\n", (uint32_t)mtdoffset);
						mtdoffset += meminfo.erasesize;
					}
#endif
				}
				eraseblocks[i].flash_offset = mtdoffset;
				mtdoffset += meminfo.erasesize;
				eraseblocks[i].wbuf_ofs = 0;
			}
			gettimeofday(&start, NULL);
		}
		if (image_crc != thispkt.hdr.totcrc) {
			fprintf(stderr, "\nImage CRC changed from 0x%x to 0x%x. Aborting\n",
				ntohl(image_crc), ntohl(thispkt.hdr.totcrc));
			exit(1);
		}

		block_nr = ntohl(thispkt.hdr.block_nr);
		if (block_nr >= nr_blocks) {
			fprintf(stderr, "\nErroneous block_nr %d (> %d)\n",
				block_nr, nr_blocks);
			exit(1);
		}
		for (i=0; i<eraseblocks[block_nr].nr_pkts; i++) {
			if (eraseblocks[block_nr].pkt_indices[i] == ntohs(thispkt.hdr.pkt_nr)) {
//				printf("Discarding duplicate packet at %08x pkt %d\n",
//				       block_nr * meminfo.erasesize, eraseblocks[block_nr].pkt_indices[i]);
				duplicates++;
				break;
			}
		}
		if (i < eraseblocks[block_nr].nr_pkts) {
			continue;
		}

		if (eraseblocks[block_nr].nr_pkts >= pkts_per_block) {
			/* We have a block which we didn't really need */
			eraseblocks[block_nr].nr_pkts++;
			ignored_pkts++;
			continue;
		}

		if (mtd_crc32(-1, thispkt.data, PKT_SIZE) != ntohl(thispkt.hdr.thiscrc)) {
			printf("\nDiscard %08x pkt %d with bad CRC (%08x not %08x)\n",
			       block_nr * meminfo.erasesize, ntohs(thispkt.hdr.pkt_nr),
			       mtd_crc32(-1, thispkt.data, PKT_SIZE),
			       ntohl(thispkt.hdr.thiscrc));
			badcrcs++;
			continue;
		}
	pkt_again:
		eraseblocks[block_nr].pkt_indices[eraseblocks[block_nr].nr_pkts++] =
			ntohs(thispkt.hdr.pkt_nr);
		total_pkts++;
		if (!(total_pkts % 50) || total_pkts == pkts_per_block * nr_blocks) {
			uint32_t pkts_sent = ntohl(thispkt.hdr.pkt_sequence) - start_seq + 1;
			long time_msec;
			gettimeofday(&now, NULL);

			time_msec = ((now.tv_usec - start.tv_usec) / 1000) +
				(now.tv_sec - start.tv_sec) * 1000;

			printf("\rReceived %d/%d (%d%%) in %lds @%ldKiB/s, %d lost (%d%%), %d dup/xs    ",
			       total_pkts, nr_blocks * pkts_per_block,
			       total_pkts * 100 / nr_blocks / pkts_per_block,
			       time_msec / 1000,
			       total_pkts * PKT_SIZE / 1024 * 1000 / time_msec,
			       pkts_sent - total_pkts - duplicates - ignored_pkts,
			       (pkts_sent - total_pkts - duplicates - ignored_pkts) * 100 / pkts_sent,
			       duplicates + ignored_pkts);
			fflush(stdout);
		}

		if (eraseblocks[block_nr].wbuf_ofs + PKT_SIZE < WBUF_SIZE) {
			/* New packet doesn't full the wbuf */
			memcpy(eraseblocks[block_nr].wbuf + eraseblocks[block_nr].wbuf_ofs,
			       thispkt.data, PKT_SIZE);
			eraseblocks[block_nr].wbuf_ofs += PKT_SIZE;
		} else {
			int fits = WBUF_SIZE - eraseblocks[block_nr].wbuf_ofs;
			ssize_t wrotelen;
			static int faked = 1;

			memcpy(eraseblocks[block_nr].wbuf + eraseblocks[block_nr].wbuf_ofs,
			       thispkt.data, fits);
			wrotelen = pwrite(flfd, eraseblocks[block_nr].wbuf, WBUF_SIZE,
					  eraseblocks[block_nr].flash_offset);

			if (wrotelen < WBUF_SIZE || (block_nr == 5 && eraseblocks[block_nr].nr_pkts == 5 && !faked)) {
				faked = 1;
				if (wrotelen < 0)
					perror("\npacket write");
				else
					fprintf(stderr, "\nshort write of packet wbuf\n");

				if (!file_mode) {
					struct erase_info_user erase;
					/* FIXME: Perhaps we should store pkt crcs and try
					   to recover data from the offending eraseblock */

					/* We have increased nr_pkts but not yet flash_offset */
					erase.start = eraseblocks[block_nr].flash_offset &
						~(meminfo.erasesize - 1);
					erase.length = meminfo.erasesize;

					printf("Will erase at %08x len %08x (bad write was at %08x)\n",
					       erase.start, erase.length, eraseblocks[block_nr].flash_offset);
					if (ioctl(flfd, MEMERASE, &erase)) {
						perror("MEMERASE");
						exit(1);
					}
					if (mtdoffset >= meminfo.size) {
						fprintf(stderr, "Run out of space on flash\n");
						exit(1);
					}
					while (ioctl(flfd, MEMGETBADBLOCK, &mtdoffset) > 0) {
						printf("Skipping flash bad block at %08x\n", (uint32_t)mtdoffset);
						mtdoffset += meminfo.erasesize;
						if (mtdoffset >= meminfo.size) {
							fprintf(stderr, "Run out of space on flash\n");
							exit(1);
						}
					}
					eraseblocks[block_nr].flash_offset = mtdoffset;
					printf("Block #%d will now be at %08lx\n", block_nr, (long)mtdoffset);
					total_pkts -= eraseblocks[block_nr].nr_pkts;
					eraseblocks[block_nr].nr_pkts = 0;
					eraseblocks[block_nr].wbuf_ofs = 0;
					mtdoffset += meminfo.erasesize;
					goto pkt_again;
				}
				else /* Usually nothing we can do in file mode */
					exit(1);
			}
			eraseblocks[block_nr].flash_offset += WBUF_SIZE;
			/* Copy the remainder into the wbuf */
			memcpy(eraseblocks[block_nr].wbuf, &thispkt.data[fits], PKT_SIZE - fits);
			eraseblocks[block_nr].wbuf_ofs = PKT_SIZE - fits;
		}

		if (eraseblocks[block_nr].nr_pkts == pkts_per_block) {
			eraseblocks[block_nr].crc = ntohl(thispkt.hdr.block_crc);

			if (total_pkts == nr_blocks * pkts_per_block)
				break;
		}
	}
	printf("\n");
	gettimeofday(&now, NULL);
	net_time = (now.tv_usec - start.tv_usec) / 1000;
	net_time += (now.tv_sec - start.tv_sec) * 1000;
	close(sock);
	for (block_nr = 0; block_nr < nr_blocks; block_nr++) {
		ssize_t rwlen;
		gettimeofday(&start, NULL);
		eraseblocks[block_nr].flash_offset -= meminfo.erasesize;
		rwlen = pread(flfd, eb_buf, meminfo.erasesize, eraseblocks[block_nr].flash_offset);

		gettimeofday(&now, NULL);
		rflash_time += (now.tv_usec - start.tv_usec) / 1000;
		rflash_time += (now.tv_sec - start.tv_sec) * 1000;
		if (rwlen < 0) {
			perror("read");
			/* Argh. Perhaps we could go back and try again, but if the flash is
			   going to fail to read back what we write to it, and the whole point
			   in this program is to write to it, what's the point? */
			fprintf(stderr, "Packets we wrote to flash seem to be unreadable. Aborting\n");
			exit(1);
		}

		memcpy(eb_buf + meminfo.erasesize, eraseblocks[block_nr].wbuf,
		       eraseblocks[block_nr].wbuf_ofs);

		for (i=0; i < pkts_per_block; i++)
			src_pkts[i] = &eb_buf[i * PKT_SIZE];

		gettimeofday(&start, NULL);
		if (fec_decode(fec, src_pkts, eraseblocks[block_nr].pkt_indices, PKT_SIZE)) {
			/* Eep. This cannot happen */
			printf("The world is broken. fec_decode() returned error\n");
			exit(1);
		}
		gettimeofday(&now, NULL);
		fec_time += (now.tv_usec - start.tv_usec) / 1000;
		fec_time += (now.tv_sec - start.tv_sec) * 1000;

		for (i=0; i < pkts_per_block; i++)
			memcpy(&decode_buf[i*PKT_SIZE], src_pkts[i], PKT_SIZE);

		/* Paranoia */
		gettimeofday(&start, NULL);
		if (mtd_crc32(-1, decode_buf, meminfo.erasesize) != eraseblocks[block_nr].crc) {
			printf("\nCRC mismatch for block #%d: want %08x got %08x\n",
			       block_nr, eraseblocks[block_nr].crc,
			       mtd_crc32(-1, decode_buf, meminfo.erasesize));
			exit(1);
		}
		gettimeofday(&now, NULL);
		crc_time += (now.tv_usec - start.tv_usec) / 1000;
		crc_time += (now.tv_sec - start.tv_sec) * 1000;
		start = now;

		if (!file_mode) {
			struct erase_info_user erase;

			erase.start = eraseblocks[block_nr].flash_offset;
			erase.length = meminfo.erasesize;

			printf("\rErasing block at %08x...", erase.start);

			if (ioctl(flfd, MEMERASE, &erase)) {
				perror("MEMERASE");
				/* This block has dirty data on it. If the erase failed, we're screwed */
				fprintf(stderr, "Erase to clean FEC data from flash failed. Aborting\n");
				exit(1);
			}
			gettimeofday(&now, NULL);
			erase_time += (now.tv_usec - start.tv_usec) / 1000;
			erase_time += (now.tv_sec - start.tv_sec) * 1000;
			start = now;
		}
		else printf("\r");
	write_again:
		rwlen = pwrite(flfd, decode_buf, meminfo.erasesize, eraseblocks[block_nr].flash_offset);
		if (rwlen < meminfo.erasesize) {
			if (rwlen < 0) {
				perror("\ndecoded data write");
			} else
				fprintf(stderr, "\nshort write of decoded data\n");

			if (!file_mode) {
				struct erase_info_user erase;
				erase.start = eraseblocks[block_nr].flash_offset;
				erase.length = meminfo.erasesize;

				printf("Erasing failed block at %08x\n",
				       eraseblocks[block_nr].flash_offset);

				if (ioctl(flfd, MEMERASE, &erase)) {
					perror("MEMERASE");
					exit(1);
				}
				if (mtdoffset >= meminfo.size) {
					fprintf(stderr, "Run out of space on flash\n");
					exit(1);
				}
				while (ioctl(flfd, MEMGETBADBLOCK, &mtdoffset) > 0) {
					printf("Skipping flash bad block at %08x\n", (uint32_t)mtdoffset);
					mtdoffset += meminfo.erasesize;
					if (mtdoffset >= meminfo.size) {
						fprintf(stderr, "Run out of space on flash\n");
						exit(1);
					}
				}
				printf("Will try again at %08lx...", (long)mtdoffset);
				eraseblocks[block_nr].flash_offset = mtdoffset;

				goto write_again;
			}
			else /* Usually nothing we can do in file mode */
				exit(1);
		}
		gettimeofday(&now, NULL);
		flash_time += (now.tv_usec - start.tv_usec) / 1000;
		flash_time += (now.tv_sec - start.tv_sec) * 1000;

		printf("wrote image block %08x (%d pkts)    ",
		       block_nr * meminfo.erasesize, eraseblocks[block_nr].nr_pkts);
		fflush(stdout);
	}
	close(flfd);
	printf("Net rx   %ld.%03lds\n", net_time / 1000, net_time % 1000);
	printf("flash rd %ld.%03lds\n", rflash_time / 1000, rflash_time % 1000);
	printf("FEC time %ld.%03lds\n", fec_time / 1000, fec_time % 1000);
	printf("CRC time %ld.%03lds\n", crc_time / 1000, crc_time % 1000);
	printf("flash wr %ld.%03lds\n", flash_time / 1000, flash_time % 1000);
	printf("flash er %ld.%03lds\n", erase_time / 1000, erase_time % 1000);

	return 0;
}