summaryrefslogtreecommitdiffstats
path: root/arch/sandbox/os/common.c
blob: 4eb6d37fffc103daedbe1340e0dece4da40373af (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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

/*
 * common.c - common wrapper functions between barebox and the host
 *
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

/*
 * These are host includes. Never include any barebox header
 * files here...
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <libgen.h>
#include <sys/mman.h>
#include <errno.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <sys/time.h>
#include <signal.h>
/*
 * ...except the ones needed to connect with barebox
 */
#include <mach/linux.h>
#include <mach/hostfile.h>

#define DELETED_OFFSET (sizeof(" (deleted)") - 1)

void __sanitizer_set_death_callback(void (*callback)(void));

int sdl_xres;
int sdl_yres;

static struct termios term_orig, term_vi;
static char erase_char;	/* the users erase character */

static void rawmode(void)
{
	tcgetattr(0, &term_orig);
	term_vi = term_orig;
	term_vi.c_lflag &= (~ICANON & ~ECHO & ~ISIG);
	term_vi.c_iflag &= (~IXON & ~ICRNL);
	term_vi.c_oflag |= (ONLCR);
	term_vi.c_cc[VMIN] = 1;
	term_vi.c_cc[VTIME] = 0;
	erase_char = term_vi.c_cc[VERASE];
	tcsetattr(0, TCSANOW, &term_vi);
}

static void cookmode(void)
{
	fflush(stdout);
	tcsetattr(0, TCSANOW, &term_orig);
}

int linux_tstc(int fd)
{
	struct timeval tv = {
		.tv_usec = 100,
	};
	fd_set rfds;
	int ret;

	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);

	/*
	 * We set the timeout here to 100us, because otherwise
	 * barebox would eat all cpu resources while waiting
	 * for input.
	 */
	ret = select(fd + 1, &rfds, NULL, NULL, &tv);

	if (ret)
		return 1;

	return 0;
}

int arch_ctrlc(void)
{
	char chr;

	if (linux_read_nonblock(0, &chr, 1) == 1 && chr == 3)
		return 1;
	return 0;
}

uint64_t linux_get_time(void)
{
	struct timespec ts;
	uint64_t now;

	clock_gettime(CLOCK_MONOTONIC, &ts);

	now = ts.tv_sec * 1000ULL * 1000 * 1000 + ts.tv_nsec;

	return now;
}

void __attribute__((noreturn)) linux_exit(void)
{
	cookmode();
	exit(0);
}

static size_t saved_argv_len;
static char **saved_argv;

void linux_reexec(void)
{
	char buf[4097];
	ssize_t ret;

	cookmode();

	/* we must follow the symlink, so we can exec an updated executable */
	ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
	if (0 < ret && ret < sizeof(buf) - 1) {
		buf[ret] = '\0';
		execv(buf, saved_argv);
		if (!strcmp(&buf[ret - DELETED_OFFSET], " (deleted)")) {
			printf("barebox image on disk changed. Loading new.\n");
			buf[ret - DELETED_OFFSET] = '\0';
			execv(buf, saved_argv);
		}
	}

	printf("exec(%s) failed: %d\n", buf, errno);
	/* falls through to generic hang() */
}

void linux_hang(void)
{
	cookmode();
	/* falls through to generic hang() */
}

int linux_open(const char *filename, int readwrite)
{
	return open(filename, (readwrite ? O_RDWR : O_RDONLY) | O_CLOEXEC);
}

int linux_read(int fd, void *buf, size_t count)
{
	ssize_t ret;

	if (count == 0)
		return 0;

	do {
		ret = read(fd, buf, count);

		if (ret == 0) {
			printf("read on fd %d returned 0, device gone? - exiting\n", fd);
			linux_exit();
		} else if (ret == -1) {
			if (errno == EAGAIN)
				return -errno;
			else if (errno == EINTR)
				continue;
			else {
				printf("read on fd %d returned -1, errno %d - exiting\n", fd, errno);
				linux_exit();
			}
		}
	} while (ret <= 0);

	return (int)ret;
}

int linux_read_nonblock(int fd, void *buf, size_t count)
{
	int oldflags, ret;

	oldflags = fcntl(fd, F_GETFL);
	if (oldflags == -1)
		goto err_out;

	if (fcntl(fd, F_SETFL, oldflags | O_NONBLOCK) == -1)
		goto err_out;

	ret = linux_read(fd, buf, count);

	if (fcntl(fd, F_SETFL, oldflags) == -1)
		goto err_out;

	return ret;

err_out:
	perror("fcntl");
	return -1;
}

ssize_t linux_write(int fd, const void *buf, size_t count)
{
	return write(fd, buf, count);
}

loff_t linux_lseek(int fd, loff_t offset)
{
	return lseek(fd, offset, SEEK_SET);
}

int linux_execve(const char * filename, char *const argv[], char *const envp[])
{
	pid_t pid, tpid;
	int execve_status;

	pid = fork();

	if (pid == -1) {
		perror("linux_execve");
		return pid;
	} else if (pid == 0) {
		exit(execve(filename, argv, envp));
	} else {
		do {
			tpid = wait(&execve_status);
		} while(tpid != pid);

		return execve_status;
	}
}

static void linux_watchdog(int signo)
{
	linux_reexec();
	_exit(0);
}

int linux_watchdog_set_timeout(unsigned int timeout)
{
	static int signal_handler_installed;

	if (!signal_handler_installed) {
		struct sigaction sact = {
			.sa_flags = SA_NODEFER, .sa_handler = linux_watchdog
		};

		sigemptyset(&sact.sa_mask);
		sigaction(SIGALRM, &sact, NULL);
		signal_handler_installed = 1;
	}

	return alarm(timeout);
}

extern void start_barebox(void);
extern void mem_malloc_init(void *start, void *end);

extern char * strsep_unescaped(char **s, const char *ct);

static int add_image(const char *_str, char *devname_template, int *devname_number)
{
	struct hf_info *hf = calloc(1, sizeof(struct hf_info));
	char *str, *filename, *devname;
	char tmp[16];
	char *opt;
	int ret;

	if (!hf)
		return -1;

	str = strdup(_str);

	filename = strsep_unescaped(&str, ",");
	while ((opt = strsep_unescaped(&str, ","))) {
		if (!strcmp(opt, "ro"))
			hf->is_readonly = 1;
		if (!strcmp(opt, "cdev"))
			hf->is_cdev = 1;
		if (!strcmp(opt, "blkdev"))
			hf->is_blockdev = 1;
	}

	/* parses: "devname=filename" */
	devname = strsep_unescaped(&filename, "=");
	filename = strsep_unescaped(&filename, "=");
	if (!filename) {
		filename = devname;
		snprintf(tmp, sizeof(tmp),
			 devname_template, (*devname_number)++);
		devname = tmp;
	}

	hf->filename = filename;
	hf->devname = strdup(devname);

	ret = barebox_register_filedev(hf);
	if (ret)
		free(hf);

	return ret;
}

int linux_open_hostfile(struct hf_info *hf)
{
	char *buf = NULL;
	struct stat s;
	int fd = -1;

	printf("add %s %sbacked by file %s%s\n", hf->devname,
	       hf->filename ? "" : "initially un", hf->filename ?: "",
	       hf->is_readonly ? "(ro)" : "");

	if (hf->filename) {
		fd = hf->fd = open(hf->filename, (hf->is_readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
	} else {
		char *filename;
		int ret;

		ret = asprintf(&buf, "--image=%s=/tmp/barebox-hostfileXXXXXX", hf->devname);
		if (ret < 0) {
			perror("asprintf");
			goto err_out;
		}

		filename = buf + strlen("--image==") + strlen(hf->devname);

		fd = hf->fd = mkstemp(filename);
		if (fd >= 0) {
			ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
			if (ret < 0) {
				perror("fcntl");
				goto err_out;
			}

			ret = ftruncate(fd, hf->size);
			if (ret < 0) {
				perror("ftruncate");
				goto err_out;
			}

			hf->filename = filename;

			saved_argv = realloc(saved_argv,
					     ++saved_argv_len * sizeof(*saved_argv));
			if (!saved_argv)
				exit(1);
			saved_argv[saved_argv_len - 2] = buf;
			saved_argv[saved_argv_len - 1] = NULL;
		}
	}

	if (fd < 0) {
		perror("open");
		goto err_out;
	}

	if (fstat(fd, &s)) {
		perror("fstat");
		goto err_out;
	}

	hf->base = (unsigned long)MAP_FAILED;
	hf->size = s.st_size;

	if (S_ISBLK(s.st_mode)) {
		if (ioctl(fd, BLKGETSIZE64, &hf->size) == -1) {
			perror("ioctl");
			goto err_out;
		}
		if (!hf->is_cdev)
			hf->is_blockdev = 1;
	}
	if (hf->size <= SIZE_MAX) {
		hf->base = (unsigned long)mmap(NULL, hf->size,
				PROT_READ | (hf->is_readonly ? 0 : PROT_WRITE),
				MAP_SHARED, fd, 0);

		if (hf->base == (unsigned long)MAP_FAILED)
			printf("warning: mmapping %s failed: %s\n",
			       hf->filename, strerror(errno));
	} else {
		printf("warning: %s: contiguous map failed\n", hf->filename);
	}

	if (hf->is_blockdev && hf->size % 512 != 0) {
		printf("warning: registering %s as block device failed: invalid block size\n",
		       hf->filename);
		return -EINVAL;
	}

	return 0;

err_out:
	if (fd >= 0)
		close(fd);
	free(buf);
	return -1;
}

static int add_dtb(const char *file)
{
	struct stat s;
	void *dtb = NULL;
	int fd;

	fd = open(file, O_RDONLY | O_CLOEXEC);
	if (fd < 0) {
		perror("open");
		goto err_out;
	}

	if (fstat(fd, &s)) {
		perror("fstat");
		goto err_out;
	}

	dtb = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
	if (dtb == MAP_FAILED) {
		perror("mmap");
		goto err_out;
	}

	if (barebox_register_dtb(dtb))
		goto err_out;

	return 0;

 err_out:
	if (dtb)
		munmap(dtb, s.st_size);
	if (fd > 0)
		close(fd);
	return -1;
}

static void print_usage(const char*);

static struct option long_options[] = {
	{"help",     0, 0, 'h'},
	{"malloc",   1, 0, 'm'},
	{"image",    1, 0, 'i'},
	{"env",      1, 0, 'e'},
	{"dtb",      1, 0, 'd'},
	{"stdout",   1, 0, 'O'},
	{"stdin",    1, 0, 'I'},
	{"stdinout", 1, 0, 'B'},
	{"xres",     1, 0, 'x'},
	{"yres",     1, 0, 'y'},
	{0, 0, 0, 0},
};

static const char optstring[] = "hm:i:e:d:O:I:B:x:y:";

int main(int argc, char *argv[])
{
	void *ram;
	int opt, ret, fd, fd2;
	int malloc_size = CONFIG_MALLOC_SIZE;
	int fdno = 0, envno = 0, option_index = 0;
	char *aux;

#ifdef CONFIG_ASAN
	__sanitizer_set_death_callback(cookmode);
#endif

	while (1) {
		option_index = 0;
		opt = getopt_long(argc, argv, optstring,
			long_options, &option_index);

		if (opt == -1)
			break;

		switch (opt) {
		case 'h':
			print_usage(basename(argv[0]));
			exit(0);
		case 'm':
			malloc_size = strtoul(optarg, NULL, 0);
			break;
		case 'i':
			break;
		case 'e':
			break;
		case 'd':
			ret = add_dtb(optarg);
			if (ret) {
				printf("Failed to load dtb: '%s'\n", optarg);
				exit(1);
			}
			break;
		case 'x':
			sdl_xres = strtoul(optarg, NULL, 0);
			break;
		case 'y':
			sdl_yres = strtoul(optarg, NULL, 0);
			break;
		default:
			break;
		}
	}

	saved_argv_len = argc + 1;
	saved_argv = calloc(saved_argv_len, sizeof(*saved_argv));
	if (!saved_argv)
		exit(1);
	memcpy(saved_argv, argv, saved_argv_len * sizeof(*saved_argv));

	ram = malloc(malloc_size);
	if (!ram) {
		printf("unable to get malloc space\n");
		exit(1);
	}
	mem_malloc_init(ram, ram + malloc_size - 1);

	/*
	 * Reset getopt.
	 * We need to run a second getopt to count -i parameters.
	 * This is for /dev/fd# devices.
	 */
	optind = 1;

	while (1) {
		option_index = 0;
		opt = getopt_long(argc, argv, optstring,
			long_options, &option_index);

		if (opt == -1)
			break;

		switch (opt) {
		case 'i':
			ret = add_image(optarg, "fd%d", &fdno);
			if (ret)
				exit(1);
			break;
		case 'e':
			ret = add_image(optarg, "env%d", &envno);
			if (ret)
				exit(1);
			break;
		case 'O':
			fd = open(optarg, O_WRONLY | O_CLOEXEC);
			if (fd < 0) {
				perror("open");
				exit(1);
			}

			barebox_register_console(-1, fd);
			break;
		case 'I':
			fd = open(optarg, O_RDWR | O_CLOEXEC);
			if (fd < 0) {
				perror("open");
				exit(1);
			}

			barebox_register_console(fd, -1);
			break;
		case 'B':
			aux = strchr(optarg, ',');
			if (!aux) {
				printf("-B, --stdinout requires two file paths given\n");
				exit(1);
			}

			/* open stdout file */
			fd = open(aux + 1, O_WRONLY | O_CLOEXEC);
			if (fd < 0) {
				perror("open stdout");
				exit(1);
			}

			/* open stdin file */
			aux = strndup(optarg, aux - optarg);
			fd2 = open(aux, O_RDWR | O_CLOEXEC);
			if (fd2 < 0) {
				perror("open stdin");
				exit(1);
			}
			free(aux);

			barebox_register_console(fd2, fd);
			break;
		default:
			break;
		}
	}

	barebox_register_console(fileno(stdin), fileno(stdout));

	rawmode();
	start_barebox();

	/* never reached */
	return 0;
}

#ifdef __PPC__
/* HACK: we need this symbol on PPC, better ask a PPC export about this :) */
char _SDA_BASE_[4096];
#endif

static void print_usage(const char *prgname)
{
	printf(
"Usage: %s [OPTIONS]\n"
"Start barebox.\n\n"
"Options:\n\n"
"  -m, --malloc=<size>  Start sandbox with a specified malloc-space size in bytes.\n"
"  -i, --image=<file>   Map an image file to barebox. This option can be given\n"
"                       multiple times. The files will show up as\n"
"                       /dev/fd0 ... /dev/fdx under barebox.\n"
"  -i, --image=<dev>=<file>\n"
"                       Same as above, the files will show up as\n"
"                       /dev/<dev>\n"
"  -e, --env=<file>     Map a file with an environment to barebox. With this \n"
"                       option, files are mapped as /dev/env0 ... /dev/envx\n"
"                       and thus are used as the default environment.\n"
"                       An empty file generated with dd will do to get started\n"
"                       with an empty environment.\n"
"  -d, --dtb=<file>     Map a device tree binary blob (dtb) into barebox.\n"
"  -O, --stdout=<file>  Register a file as a console capable of doing stdout.\n"
"                       <file> can be a regular file or a FIFO.\n"
"  -I, --stdin=<file>   Register a file as a console capable of doing stdin.\n"
"                       <file> can be a regular file or a FIFO.\n"
"  -B, --stdinout=<filein>,<fileout>\n"
"                       Register a bidirectional console capable of doing both\n"
"                       stdin and stdout. <filein> and <fileout> can be regular\n"
"                       files or FIFOs.\n"
"  -x, --xres=<res>     SDL width.\n"
"  -y, --yres=<res>     SDL height.\n",
	prgname
	);
}