Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*	$NetBSD: utoppya.c,v 1.6 2015/06/16 22:54:11 christos Exp $	*/

/*-
 * Copyright (c) 2006 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Steve C. Woodford.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <sysexits.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>

#include <dev/usb/utoppy.h>

#define	GLOBAL
#include "progressbar.h"

#define	_PATH_DEV_UTOPPY	"/dev/utoppy0"

/*
 * This looks weird for a reason. The toppy protocol allows for data to be
 * transferred in 65535-byte chunks only. Anything more than this has to be
 * split within the driver. The following value leaves enough space for the
 * packet header plus some alignmnent slop.
 */
#define	TOPPY_IO_SIZE	0xffec

static int toppy_fd;

static void cmd_df(int, char **);
static void cmd_ls(int, char **);
static void cmd_rm(int, char **);
static void cmd_mkdir(int, char **);
static void cmd_rename(int, char **);
static void cmd_get(int, char **);
static void cmd_put(int, char **);

static const struct toppy_command {
	const char *tc_cmd;
	void (*tc_handler)(int, char **);
} toppy_commands[] = {
	{"df",		cmd_df},
	{"ls",		cmd_ls},
	{"get",		cmd_get},
	{"mkdir",	cmd_mkdir},
	{"put",		cmd_put},
	{"rename",	cmd_rename},
	{"rm",		cmd_rm},
	{NULL,		NULL}
};

__dead static void
usage(void)
{

	fprintf(stderr, "usage: %s [-f <path>] <cmd> ...\n",
	    getprogname());

	exit(EX_USAGE);
}

int
main(int argc, char *argv[])
{
	const struct toppy_command *tc;
	const char *devpath;
	int ch;

	setprogname(argv[0]);
	devpath = _PATH_DEV_UTOPPY;

	while ((ch = getopt(argc, argv, "f:")) != -1) {
		switch (ch) {
		case 'f':
			devpath = optarg;
			break;

		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 0)
		usage();

	for (tc = toppy_commands; tc->tc_cmd != NULL; tc++)
		if (strcasecmp(argv[0], tc->tc_cmd) == 0)
			break;

	if (tc->tc_cmd == NULL)
		errx(EX_USAGE, "'%s' is not a valid command", argv[0]);

	if ((toppy_fd = open(devpath, O_RDWR)) < 0)
		err(EX_OSERR, "open(%s)", devpath);

	(*tc->tc_handler)(argc, argv);

	close(toppy_fd);

	return (0);
}

static int
find_toppy_dirent(const char *path, struct utoppy_dirent *udp)
{
	struct utoppy_dirent ud;
	char *d, *b, dir[FILENAME_MAX];
	ssize_t l;

	strncpy(dir, path, sizeof(dir));
	b = basename(dir);
	d = dirname(dir);
	if (strcmp(b, "/") == 0 || strcmp(b, ".") == 0 || strcmp(d, ".") == 0)
		errx(EX_USAGE, "'%s' is not a valid Toppy pathname", path);

	if (ioctl(toppy_fd, UTOPPYIOREADDIR, &d) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOREADDIR, %s)", d);

	if (udp == NULL)
		udp = &ud;

	while ((l = read(toppy_fd, udp, sizeof(*udp))) == sizeof(*udp)) {
		if (strcmp(b, udp->ud_path) == 0)
			break;
	}

	if (l < 0)
		err(EX_OSERR, "read(TOPPYDIR, %s)", d);

	if (l == 0)
		return (0);

	while (read(toppy_fd, &ud, sizeof(ud)) > 0)
		;

	return (1);
}

static void
cmd_df(int argc, char **argv)
{
	struct utoppy_stats us;

	if (ioctl(toppy_fd, UTOPPYIOSTATS, &us) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOSTATS)");

	printf("Hard Disk Size: %" PRId64 " MB\n", us.us_hdd_size / (1024 * 1024));
	printf("Hard Disk Free: %" PRId64 " MB\n", us.us_hdd_free / (1024 * 1024));
}

static void
cmd_ls(int argc, char **argv)
{
	struct utoppy_dirent ud;
	struct tm *tm;
	char *dir, *ext, dirbuf[2], ex, ft, tmbuf[32];
	ssize_t l;

	if (argc == 1) {
		dirbuf[0] = '/';
		dirbuf[1] = '\0';
		dir = dirbuf;
	} else
	if (argc == 2)
		dir = argv[1];
	else
		errx(EX_USAGE, "usage: ls [toppy-pathname]");

	if (ioctl(toppy_fd, UTOPPYIOREADDIR, &dir) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOREADDIR, %s)", dir);

	while ((l = read(toppy_fd, &ud, sizeof(ud))) == sizeof(ud)) {
		switch (ud.ud_type) {
		default:
			ft = '?';
			break;

		case UTOPPY_DIRENT_DIRECTORY:
			ft = 'd';
			break;

		case UTOPPY_DIRENT_FILE:
			ft = '-';
			break;
		}

		if ((ext = strrchr(ud.ud_path, '.')) != NULL &&
		    strcasecmp(ext, ".tap") == 0)
			ex = 'x';
		else
			ex = '-';

		tm = localtime(&ud.ud_mtime);
		strftime(tmbuf, sizeof(tmbuf), "%b %e %G %R", tm);

		printf("%crw%c %11lld %s %s\n", ft, ex, (long long)ud.ud_size,
		    tmbuf, ud.ud_path);
	}

	if (l < 0)
		err(EX_OSERR, "read(utoppy_dirent)");
}

static void
cmd_rm(int argc, char **argv)
{
	char *path;

	if (argc != 2)
		errx(EX_USAGE, "usage: rm <toppy-pathname>");

	path = argv[1];

	if (ioctl(toppy_fd, UTOPPYIODELETE, &path) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIODELETE, %s)", path);
}

static void
cmd_mkdir(int argc, char **argv)
{
	char *path;

	if (argc != 2)
		errx(EX_USAGE, "usage: mkdir <toppy-pathname>");

	path = argv[1];

	if (find_toppy_dirent(path, NULL))
		errx(EX_DATAERR, "'%s' already exists", path);

	if (ioctl(toppy_fd, UTOPPYIOMKDIR, &path) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOMKDIR, %s)", path);
}

static void
cmd_rename(int argc, char **argv)
{
	struct utoppy_dirent ud;
	struct utoppy_rename ur;
	char *oldpath, *newpath, *o, *n;

	if (argc != 3)
		errx(EX_USAGE, "usage: rename <from> <to>");

	o = oldpath = argv[1];
	n = newpath = argv[2];

	for (o = oldpath; *o != '\0'; o++)
		if (*o == '\\')
			*o = '/';
	for (n = newpath; *n != '\0'; n++)
		if (*n == '\\')
			*n = '/';

	for (o = oldpath; *o && *o == '\\'; o++)
		;
	for (n = newpath; *n && *n == '\\'; n++)
		;

	if (strcmp(n, o) == 0)
		errx(EX_DATAERR, "'%s' and '%s' refer to the same file",
		    oldpath, newpath);

	if (find_toppy_dirent(oldpath, &ud) == 0)
		errx(EX_DATAERR, "'%s' does not exist on the Toppy", oldpath);

	if (ud.ud_type != UTOPPY_DIRENT_FILE)
		errx(EX_DATAERR, "%s: not a regular file", oldpath);

	if (find_toppy_dirent(newpath, &ud))
		errx(EX_DATAERR, "'%s' already exists", newpath);

	ur.ur_old_path = o;
	ur.ur_new_path = n;

	if (ioctl(toppy_fd, UTOPPYIORENAME, &ur) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIORENAME, %s, %s)", oldpath,
		    newpath);
}


static void
init_progress(FILE *to, char *f, off_t fsize, off_t restart)
{
	struct ttysize ts;

	if (ioctl(fileno(to), TIOCGSIZE, &ts) == -1)
		ttywidth = 80;
	else
		ttywidth = ts.ts_cols;

	ttyout = to;
	progress = 1;
	bytes = 0;
	filesize = fsize;
	restart_point = restart;
	prefix = f;
}

static void
cmd_get(int argc, char **argv)
{
	struct utoppy_readfile ur;
	struct utoppy_dirent ud;
	struct stat st;
	char *dst, dstbuf[FILENAME_MAX];
	uint8_t *buf;
	ssize_t l;
	size_t rv;
	int ch, turbo_mode = 0, reget = 0, progbar = 0;
	FILE *ofp, *to;

	optind = 1;
	optreset = 1;

	while ((ch = getopt(argc, argv, "prt")) != -1) {
		switch (ch) {
		case 'p':
			progbar = 1;
			break;
		case 'r':
			reget = 1;
			break;
		case 't':
			turbo_mode = 1;
			break;
		default:
 get_usage:
			errx(EX_USAGE, "usage: get [-prt] <toppy-pathname> "
			    "[file | directory]");
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 1)
		dst = basename(argv[0]);
	else
	if (argc == 2) {
		dst = argv[1];
		if (stat(dst, &st) == 0 && S_ISDIR(st.st_mode)) {
			snprintf(dstbuf, sizeof(dstbuf), "%s/%s", dst,
			    basename(argv[0]));
			dst = dstbuf;
		}
	} else
		goto get_usage;

	ur.ur_path = argv[0];
	ur.ur_offset = 0;

	if ((buf = malloc(TOPPY_IO_SIZE)) == NULL)
		err(EX_OSERR, "malloc(TOPPY_IO_SIZE)");

	if (strcmp(dst, "-") == 0) {
		ofp = stdout;
		to = stderr;
		if (reget)
			warnx("Ignoring -r option in combination with stdout");
	} else {
		to = stdout;

		if (reget) {
			if (stat(dst, &st) < 0) {
				if (errno != ENOENT)
					err(EX_OSERR, "stat(%s)", dst);
			} else
			if (!S_ISREG(st.st_mode))
				errx(EX_DATAERR, "-r only works with regular "
				    "files");
			else
				ur.ur_offset = st.st_size;
		}

		if ((ofp = fopen(dst, reget ? "a" : "w")) == NULL)
			err(EX_OSERR, "fopen(%s)", dst);
	}

	if (progbar) {
		if (find_toppy_dirent(ur.ur_path, &ud) == 0)
			ud.ud_size = 0;
		init_progress(to, dst, ud.ud_size, ur.ur_offset);
	}

	if (ioctl(toppy_fd, UTOPPYIOTURBO, &turbo_mode) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOTURBO, %d)", turbo_mode);

	if (ioctl(toppy_fd, UTOPPYIOREADFILE, &ur) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOREADFILE, %s)", ur.ur_path);

	if (progbar)
		progressmeter(-1);

	for (;;) {
		while ((l = read(toppy_fd, buf, TOPPY_IO_SIZE)) < 0 &&
		    errno == EINTR)
			;

		if (l <= 0)
			break;

		rv = fwrite(buf, 1, l, ofp);

		if (rv != (size_t)l) {
			if (ofp != stdout)
				fclose(ofp);
			progressmeter(1);
			err(EX_OSERR, "fwrite(%s)", dst);
		}
		bytes += l;
	}

	if (progbar)
		progressmeter(1);

	if (ofp != stdout)
		fclose(ofp);

	if (l < 0)
		err(EX_OSERR, "read(TOPPY: ur.ur_path)");

	free(buf);
}

static void
cmd_put(int argc, char **argv)
{
	struct utoppy_writefile uw;
	struct utoppy_dirent ud;
	struct stat st;
	char dstbuf[FILENAME_MAX];
	char *src;
	void *buf;
	ssize_t rv;
	size_t l;
	int ch, turbo_mode = 0, reput = 0, progbar = 0;
	FILE *ifp;

	optind = 1;
	optreset = 1;

	while ((ch = getopt(argc, argv, "prt")) != -1) {
		switch (ch) {
		case 'p':
			progbar = 1;
			break;
		case 'r':
			reput = 1;
			break;
		case 't':
			turbo_mode = 1;
			break;
		default:
 put_usage:
			errx(EX_USAGE, "usage: put [-prt] <local-pathname> "
			    "<toppy-pathname>");
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 2)
		goto put_usage;

	src = argv[0];
	uw.uw_path = argv[1];

	if (stat(src, &st) < 0)
		err(EX_OSERR, "%s", src);

	if (!S_ISREG(st.st_mode))
		errx(EX_DATAERR, "'%s' is not a regular file", src);

	uw.uw_size = st.st_size;
	uw.uw_mtime = st.st_mtime;
	uw.uw_offset = 0;

	if (find_toppy_dirent(uw.uw_path, &ud)) {
		if (ud.ud_type == UTOPPY_DIRENT_DIRECTORY) {
			snprintf(dstbuf, sizeof(dstbuf), "%s/%s", uw.uw_path,
			    basename(src));
			uw.uw_path = dstbuf;
		} else
		if (ud.ud_type != UTOPPY_DIRENT_FILE)
			errx(EX_DATAERR, "'%s' is not a regular file.",
			    uw.uw_path);
		else
		if (reput) {
			if (ud.ud_size > uw.uw_size)
				errx(EX_DATAERR, "'%s' is already larger than "
				    "'%s'", uw.uw_path, src);

			uw.uw_size -= ud.ud_size;
			uw.uw_offset = ud.ud_size;
		}
	}

	if ((buf = malloc(TOPPY_IO_SIZE)) == NULL)
		err(EX_OSERR, "malloc(TOPPY_IO_SIZE)");

	if ((ifp = fopen(src, "r")) == NULL)
		err(EX_OSERR, "fopen(%s)", src);

	if (ioctl(toppy_fd, UTOPPYIOTURBO, &turbo_mode) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOTURBO, %d)", turbo_mode);

	if (ioctl(toppy_fd, UTOPPYIOWRITEFILE, &uw) < 0)
		err(EX_OSERR, "ioctl(UTOPPYIOWRITEFILE, %s)", uw.uw_path);

	if (progbar)
		init_progress(stdout, src, st.st_size, uw.uw_offset);

	if (progbar)
		progressmeter(-1);

	while ((l = fread(buf, 1, TOPPY_IO_SIZE, ifp)) > 0) {
		rv = write(toppy_fd, buf, l);
		if ((size_t)rv != l) {
			fclose(ifp);
			if (progbar)
				progressmeter(1);
			err(EX_OSERR, "write(TOPPY: %s)", uw.uw_path);
		}
		bytes += l;
	}

	if (progbar)
		progressmeter(1);

	if (ferror(ifp))
		err(EX_OSERR, "fread(%s)", src);

	fclose(ifp);
	free(buf);
}